|
9 | 9 |
|
10 | 10 | class SwooleRedisClusterTest extends Base |
11 | 11 | { |
| 12 | + private function getConnection(): RedisCluster |
| 13 | + { |
| 14 | + return new RedisCluster([ |
| 15 | + 'redis-cluster-0:6379', |
| 16 | + 'redis-cluster-1:6379', |
| 17 | + 'redis-cluster-2:6379', |
| 18 | + ]); |
| 19 | + } |
| 20 | + |
12 | 21 | protected function getPublisher(): Publisher |
13 | 22 | { |
14 | | - $connection = new RedisCluster(['redis-cluster-0:6379', 'redis-cluster-1:6379', 'redis-cluster-2:6379']); |
15 | | - return new Redis($connection); |
| 23 | + return new Redis($this->getConnection()); |
16 | 24 | } |
17 | 25 |
|
18 | 26 | protected function getQueue(): Queue |
19 | 27 | { |
20 | 28 | return new Queue('swoole-redis-cluster'); |
21 | 29 | } |
| 30 | + |
| 31 | + public function testPriorityJobIsConsumedBeforeNormalJobs(): void |
| 32 | + { |
| 33 | + $connection = $this->getConnection(); |
| 34 | + $key = "{$this->getQueue()->namespace}.queue.{$this->getQueue()->name}"; |
| 35 | + |
| 36 | + // Flush any leftover state from previous runs. |
| 37 | + while ($connection->rightPopArray($key, 1) !== false) { |
| 38 | + // drain |
| 39 | + } |
| 40 | + |
| 41 | + // Enqueue three normal jobs (pushed to head/left). |
| 42 | + $this->getPublisher()->enqueue($this->getQueue(), ['order' => 'normal-1']); |
| 43 | + $this->getPublisher()->enqueue($this->getQueue(), ['order' => 'normal-2']); |
| 44 | + $this->getPublisher()->enqueue($this->getQueue(), ['order' => 'normal-3']); |
| 45 | + |
| 46 | + // Enqueue one priority job (pushed to tail/right — same end BRPOP reads from). |
| 47 | + $this->getPublisher()->enqueue($this->getQueue(), ['order' => 'priority'], priority: true); |
| 48 | + |
| 49 | + // The first pop should yield the priority job. |
| 50 | + $first = $connection->rightPopArray($key, 1); |
| 51 | + $this->assertNotFalse($first, 'Expected a job but queue was empty'); |
| 52 | + $this->assertSame('priority', $first['payload']['order'], 'Priority job should be consumed first'); |
| 53 | + |
| 54 | + // The remaining three should be normal jobs (consumed oldest-first). |
| 55 | + $second = $connection->rightPopArray($key, 1); |
| 56 | + $this->assertSame('normal-1', $second['payload']['order']); |
| 57 | + |
| 58 | + $third = $connection->rightPopArray($key, 1); |
| 59 | + $this->assertSame('normal-2', $third['payload']['order']); |
| 60 | + |
| 61 | + $fourth = $connection->rightPopArray($key, 1); |
| 62 | + $this->assertSame('normal-3', $fourth['payload']['order']); |
| 63 | + |
| 64 | + // Queue should now be empty. |
| 65 | + $this->assertFalse($connection->rightPopArray($key, 1)); |
| 66 | + } |
22 | 67 | } |
0 commit comments