diff --git a/tests/e2e/General/WorkerConcurrencyTest.php b/tests/e2e/General/WorkerConcurrencyTest.php index aa2a67f6a8..20ad5e1226 100644 --- a/tests/e2e/General/WorkerConcurrencyTest.php +++ b/tests/e2e/General/WorkerConcurrencyTest.php @@ -63,6 +63,32 @@ final class WorkerConcurrencyTest extends TestCase $this->assertSame(3, $maxActive['v1-functions'], 'sibling queues keep their own higher caps'); } + /** + * A batched sibling must not cost the databases queue its serialisation. + * + * Batching changes how many messages one receive claims, not how many run at + * once -- the cap still decides that. The way it could go wrong is at the + * boundary between queues: a batch claimed for one queue must not occupy the + * slots another one is serialised by, and must not lose or duplicate the + * messages it claimed. So this is the combined-mode case with the parallel + * sibling batching, asserted on what came out rather than on how it was + * fetched. + */ + public function testABatchedSiblingLeavesTheDatabasesQueueSerial(): void + { + [$processed, $maxActive] = $this->runQueues( + queues: [ + ['name' => 'database_db_main', 'messages' => 6, 'maxCoroutines' => 1], + ['name' => 'v1-functions', 'messages' => 9, 'maxCoroutines' => 3, 'batch' => 3], + ], + ); + + $this->assertSame(6, $processed['database_db_main']); + $this->assertSame(9, $processed['v1-functions'], 'a batch must deliver every message it claimed, exactly once'); + $this->assertSame(1, $maxActive['database_db_main'], 'databases stays serial beside a batching sibling'); + $this->assertSame(3, $maxActive['v1-functions'], 'a batch is claimed three at a time, not run three-deep past the cap'); + } + public function testMessageWithoutFreeDatabasesSlotStaysInBroker(): void { $connection = new InMemoryConnection(); @@ -106,39 +132,7 @@ final class WorkerConcurrencyTest extends TestCase } /** - * The fake has to serve a batched receive, not just a single pop. - * - * queue 3.0 gave Broker\Redis a receiveBatch() that takes the message it - * waited for plus whatever is already behind it in one round trip, over - * Connection::rightPopMany(). A fake that answered that with an empty batch - * would leave every batched wiring untestable here, and would do it quietly: - * an empty receive is what an idle queue looks like. - */ - public function testTheFakeServesABatchedReceive(): void - { - $connection = new InMemoryConnection(); - $broker = new Redis($connection, $connection); - $queue = new Queue('v1-functions', self::NAMESPACE); - - $broker->publish($queue, ['n' => 0]); - $broker->publish($queue, ['n' => 1]); - $broker->publish($queue, ['n' => 2]); - - // Asking for more than the list holds: a short batch is the ordinary - // answer, not an error, because only the first message is waited for. - $claimed = $broker->receiveBatch($queue, 1, 5); - - $this->assertCount(3, $claimed); - $this->assertSame( - [0, 1, 2], - \array_map(static fn ($message): int => $message->getPayload()['n'], $claimed), - 'a batch keeps the order the messages were published in', - ); - $this->assertSame(0, $broker->getQueueSize($queue), 'one receive claimed the whole backlog'); - } - - /** - * @param list $queues + * @param list $queues * @return array{0: array, 1: array} [processedByQueue, maxActiveByQueue] */ private function runQueues(array $queues): array @@ -167,7 +161,11 @@ final class WorkerConcurrencyTest extends TestCase for ($i = 0; $i < $spec['messages']; $i++) { $broker->publish($queue, ['n' => $i]); } - $specs[] = ['queue' => $queue, 'maxCoroutines' => $spec['maxCoroutines']]; + $specs[] = [ + 'queue' => $queue, + 'maxCoroutines' => $spec['maxCoroutines'], + 'batch' => $spec['batch'] ?? 1, + ]; } $adapter = new Swoole($broker, 1, self::NAMESPACE);