From a0fd9a00ace123bbbe5c8d5abbf5da7a865ba9c9 Mon Sep 17 00:00:00 2001 From: Levi van Noort <73097785+levivannoort@users.noreply.github.com> Date: Fri, 18 Sep 2026 15:23:18 +0200 Subject: [PATCH] test(queue): prove the in-memory fake serves a batched receive Review flagged rightPopMany() for ignoring $timeout. It does, as every pop in this fake does -- nothing here blocks, an empty pop yields, and that is what keeps a busy receive loop from starving the handlers it shares a thread with. Making one method block would be a new behaviour, not a fix. The real defect underneath was quieter: each message after the first went back through pop(), so a batch shorter than asked ended on a yield the real connection never takes. LMPOP waits for the first message only and takes the company it finds. The rest now comes straight off the list. A batch the fake cannot serve looks exactly like an idle queue, so the failure it would cause is silence. The test claims a three-message backlog in one receive and asserts the order and the drained queue -- seen red against a rightPopMany() that returns nothing. --- tests/e2e/General/WorkerConcurrencyTest.php | 32 +++++++++++++++++++ tests/extensions/Queue/InMemoryConnection.php | 24 +++++++++++--- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/tests/e2e/General/WorkerConcurrencyTest.php b/tests/e2e/General/WorkerConcurrencyTest.php index 5b742b9796..aa2a67f6a8 100644 --- a/tests/e2e/General/WorkerConcurrencyTest.php +++ b/tests/e2e/General/WorkerConcurrencyTest.php @@ -105,6 +105,38 @@ 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 * @return array{0: array, 1: array} [processedByQueue, maxActiveByQueue] diff --git a/tests/extensions/Queue/InMemoryConnection.php b/tests/extensions/Queue/InMemoryConnection.php index 006a5d07ab..08d079c700 100644 --- a/tests/extensions/Queue/InMemoryConnection.php +++ b/tests/extensions/Queue/InMemoryConnection.php @@ -92,15 +92,31 @@ final class InMemoryConnection implements Connection * Pop up to $count payloads from the tail, in pop order. * * Fewer than asked — including none — is the ordinary answer on a list that - * holds fewer, matching what LMPOP does for the real connection. + * holds fewer, matching what LMPOP does for the real connection: it waits + * only for the first message and then takes whatever company it finds. + * + * $timeout is unused, as it is in every other pop here. Nothing in this fake + * blocks; an empty pop yields instead, which is what keeps a busy receive + * loop from starving the handlers it shares a thread with. * * @return list */ public function rightPopMany(string $queue, int $count, int $timeout): array { - $batch = []; - for ($taken = 0; $taken < $count; $taken++) { - $value = $this->pop($queue, fromTail: true); + // The first one goes through pop(), so an empty queue costs exactly what + // it costs rightPop(). + $first = $this->pop($queue, fromTail: true); + if (!\is_string($first)) { + return []; + } + + $batch = [$first]; + + // The rest is only what is already on the list. Going back through pop() + // would yield once more on the message that is not there, turning a + // short batch into a sleep the real connection never takes. + while (\count($batch) < $count && !empty($this->lists[$queue])) { + $value = array_pop($this->lists[$queue]); if (!\is_string($value)) { break; }