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.
This commit is contained in:
Levi van Noort committed 2026-09-18 15:23:18 +02:00
1 parent 657b1af2ef
commit a0fd9a00ac
2 files changed
+52 -4

No files matched your search

@@ -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<array{name: string, messages: int, maxCoroutines: int}> $queues
* @return array{0: array<string, int>, 1: array<string, int>} [processedByQueue, maxActiveByQueue]
+20 -4
View File
@@ -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<string>
*/
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;
}