-
Notifications
You must be signed in to change notification settings - Fork 3
Implement RPUSH for priority messages #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\E2E\Adapter; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Utopia\Queue\Broker\Redis as RedisBroker; | ||
| use Utopia\Queue\Connection\Redis; | ||
| use Utopia\Queue\Queue; | ||
|
|
||
| /** | ||
| * Verifies that priority jobs (pushed to the tail via rightPushArray) are consumed | ||
| * before normal jobs (pushed to the head via leftPushArray) when BRPOP reads from | ||
| * the tail. | ||
| * | ||
| * This test bypasses the worker and reads directly from the queue so it can assert ordering. | ||
| */ | ||
| class RedisPriorityTest extends TestCase | ||
| { | ||
| private RedisBroker $broker; | ||
| private Queue $queue; | ||
| private Redis $connection; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->connection = new Redis('redis', 6379); | ||
| $this->broker = new RedisBroker($this->connection); | ||
| $this->queue = new Queue('priority-e2e-test'); | ||
|
|
||
| // Flush any leftover state from previous runs. | ||
| $key = "{$this->queue->namespace}.queue.{$this->queue->name}"; | ||
| while ($this->connection->rightPopArray($key, 0) !== false) { | ||
| // drain | ||
| } | ||
| } | ||
|
|
||
| public function testPriorityJobIsConsumedBeforeNormalJobs(): void | ||
| { | ||
| // Enqueue three normal jobs (pushed to head/left). | ||
| $this->broker->enqueue($this->queue, ['order' => 'normal-1']); | ||
| $this->broker->enqueue($this->queue, ['order' => 'normal-2']); | ||
| $this->broker->enqueue($this->queue, ['order' => 'normal-3']); | ||
|
|
||
| // Enqueue one priority job (pushed to tail/right — same end BRPOP reads from). | ||
| $this->broker->enqueue($this->queue, ['order' => 'priority'], priority: true); | ||
|
|
||
| $key = "{$this->queue->namespace}.queue.{$this->queue->name}"; | ||
|
|
||
| // The first pop should yield the priority job. | ||
| $first = $this->connection->rightPopArray($key, 1); | ||
| $this->assertNotFalse($first, 'Expected a job but queue was empty'); | ||
| $this->assertSame('priority', $first['payload']['order'], 'Priority job should be consumed first'); | ||
|
|
||
| // The remaining three should be normal jobs (consumed oldest-first). | ||
| $second = $this->connection->rightPopArray($key, 1); | ||
| $this->assertSame('normal-1', $second['payload']['order']); | ||
|
|
||
| $third = $this->connection->rightPopArray($key, 1); | ||
| $this->assertSame('normal-2', $third['payload']['order']); | ||
|
|
||
| $fourth = $this->connection->rightPopArray($key, 1); | ||
| $this->assertSame('normal-3', $fourth['payload']['order']); | ||
|
|
||
| // Queue should now be empty. | ||
| $this->assertFalse($this->connection->rightPopArray($key, 0)); | ||
|
hmacr marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public function testEnqueuePriorityReturnsBool(): void | ||
| { | ||
| $result = $this->broker->enqueue($this->queue, ['check' => 'return-value'], priority: true); | ||
| $this->assertIsBool($result); | ||
| $this->assertTrue($result); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Unit\Broker; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Utopia\Queue\Broker\Redis; | ||
| use Utopia\Queue\Connection; | ||
| use Utopia\Queue\Queue; | ||
|
|
||
| class RedisBrokerTest extends TestCase | ||
| { | ||
| private Connection $connection; | ||
| private Redis $broker; | ||
| private Queue $queue; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->connection = $this->createMock(Connection::class); | ||
| $this->broker = new Redis($this->connection); | ||
|
hmacr marked this conversation as resolved.
Outdated
|
||
| $this->queue = new Queue('test'); | ||
| } | ||
|
|
||
| public function testEnqueueNormalUsesLeftPush(): void | ||
| { | ||
| $this->connection | ||
| ->expects($this->once()) | ||
| ->method('leftPushArray') | ||
| ->with( | ||
| $this->equalTo('utopia-queue.queue.test'), | ||
| $this->callback(fn($p) => $p['queue'] === 'test' && $p['payload'] === ['foo' => 'bar']) | ||
| ) | ||
| ->willReturn(true); | ||
|
|
||
| $this->connection->expects($this->never())->method('rightPushArray'); | ||
|
|
||
| $result = $this->broker->enqueue($this->queue, ['foo' => 'bar']); | ||
|
|
||
| $this->assertTrue($result); | ||
| } | ||
|
|
||
| public function testEnqueuePriorityFalseUsesLeftPush(): void | ||
| { | ||
| $this->connection | ||
| ->expects($this->once()) | ||
| ->method('leftPushArray') | ||
| ->willReturn(true); | ||
|
|
||
| $this->connection->expects($this->never())->method('rightPushArray'); | ||
|
|
||
| $result = $this->broker->enqueue($this->queue, ['foo' => 'bar'], priority: false); | ||
|
|
||
| $this->assertTrue($result); | ||
| } | ||
|
|
||
| public function testEnqueuePriorityUsesRightPush(): void | ||
| { | ||
| $this->connection | ||
| ->expects($this->once()) | ||
| ->method('rightPushArray') | ||
| ->with( | ||
| $this->equalTo('utopia-queue.queue.test'), | ||
| $this->callback(fn($p) => $p['queue'] === 'test' && $p['payload'] === ['urgent' => true]) | ||
| ) | ||
| ->willReturn(true); | ||
|
|
||
| $this->connection->expects($this->never())->method('leftPushArray'); | ||
|
|
||
| $result = $this->broker->enqueue($this->queue, ['urgent' => true], priority: true); | ||
|
|
||
| $this->assertTrue($result); | ||
| } | ||
|
|
||
| public function testEnqueuePriorityPayloadHasRequiredFields(): void | ||
| { | ||
| $capturedPayload = null; | ||
|
|
||
| $this->connection | ||
| ->expects($this->once()) | ||
| ->method('rightPushArray') | ||
| ->with( | ||
| $this->anything(), | ||
| $this->callback(function ($p) use (&$capturedPayload) { | ||
| $capturedPayload = $p; | ||
| return true; | ||
| }) | ||
| ) | ||
| ->willReturn(true); | ||
|
|
||
| $this->broker->enqueue($this->queue, ['data' => 1], priority: true); | ||
|
|
||
| $this->assertArrayHasKey('pid', $capturedPayload); | ||
| $this->assertArrayHasKey('queue', $capturedPayload); | ||
| $this->assertArrayHasKey('timestamp', $capturedPayload); | ||
| $this->assertArrayHasKey('payload', $capturedPayload); | ||
| $this->assertNotEmpty($capturedPayload['pid']); | ||
| } | ||
| } | ||
|
hmacr marked this conversation as resolved.
Outdated
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.