|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\E2E\Adapter; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Utopia\Queue\Broker\Redis as RedisBroker; |
| 7 | +use Utopia\Queue\Connection\Redis; |
| 8 | +use Utopia\Queue\Queue; |
| 9 | + |
| 10 | +/** |
| 11 | + * Verifies that priority jobs (pushed to the tail via rightPushArray) are consumed |
| 12 | + * before normal jobs (pushed to the head via leftPushArray) when BRPOP reads from |
| 13 | + * the tail. |
| 14 | + * |
| 15 | + * This test bypasses the worker and reads directly from the queue so it can assert ordering. |
| 16 | + */ |
| 17 | +class RedisPriorityTest extends TestCase |
| 18 | +{ |
| 19 | + private RedisBroker $broker; |
| 20 | + private Queue $queue; |
| 21 | + private Redis $connection; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + $this->connection = new Redis('redis', 6379); |
| 26 | + $this->broker = new RedisBroker($this->connection); |
| 27 | + $this->queue = new Queue('priority-e2e-test'); |
| 28 | + |
| 29 | + // Flush any leftover state from previous runs. |
| 30 | + $key = "{$this->queue->namespace}.queue.{$this->queue->name}"; |
| 31 | + while ($this->connection->rightPopArray($key, 0) !== false) { |
| 32 | + // drain |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public function testPriorityJobIsConsumedBeforeNormalJobs(): void |
| 37 | + { |
| 38 | + // Enqueue three normal jobs (pushed to head/left). |
| 39 | + $this->broker->enqueue($this->queue, ['order' => 'normal-1']); |
| 40 | + $this->broker->enqueue($this->queue, ['order' => 'normal-2']); |
| 41 | + $this->broker->enqueue($this->queue, ['order' => 'normal-3']); |
| 42 | + |
| 43 | + // Enqueue one priority job (pushed to tail/right — same end BRPOP reads from). |
| 44 | + $this->broker->enqueue($this->queue, ['order' => 'priority'], priority: true); |
| 45 | + |
| 46 | + $key = "{$this->queue->namespace}.queue.{$this->queue->name}"; |
| 47 | + |
| 48 | + // The first pop should yield the priority job. |
| 49 | + $first = $this->connection->rightPopArray($key, 1); |
| 50 | + $this->assertNotFalse($first, 'Expected a job but queue was empty'); |
| 51 | + $this->assertSame('priority', $first['payload']['order'], 'Priority job should be consumed first'); |
| 52 | + |
| 53 | + // The remaining three should be normal jobs (consumed oldest-first). |
| 54 | + $second = $this->connection->rightPopArray($key, 1); |
| 55 | + $this->assertSame('normal-1', $second['payload']['order']); |
| 56 | + |
| 57 | + $third = $this->connection->rightPopArray($key, 1); |
| 58 | + $this->assertSame('normal-2', $third['payload']['order']); |
| 59 | + |
| 60 | + $fourth = $this->connection->rightPopArray($key, 1); |
| 61 | + $this->assertSame('normal-3', $fourth['payload']['order']); |
| 62 | + |
| 63 | + // Queue should now be empty. |
| 64 | + $this->assertFalse($this->connection->rightPopArray($key, 0)); |
| 65 | + } |
| 66 | + |
| 67 | + public function testEnqueuePriorityReturnsBool(): void |
| 68 | + { |
| 69 | + $result = $this->broker->enqueue($this->queue, ['check' => 'return-value'], priority: true); |
| 70 | + $this->assertIsBool($result); |
| 71 | + $this->assertTrue($result); |
| 72 | + } |
| 73 | +} |
0 commit comments