mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
test(queue): assert the batched sibling at the worker boundary
The previous test's subject was the fake itself -- it drove receiveBatch() directly and asserted the broker's return. Review called that implementation-coupled and it is: a test whose subject is a fixture proves the fixture, not the worker. What matters about batching here is the boundary between queues. Batching changes how many messages one receive claims, not how many run at once, so the way it could go wrong is by occupying slots another queue is serialised by, or by losing or duplicating what it claimed. The combined -mode case now runs its parallel sibling batched and asserts what came out: every message once, databases still serial, the cap still the cap. Worth knowing what this cannot see: a batch size that is ignored outright behaves identically at this boundary, because the whole point of a batch is fewer round trips and nothing else. Claiming is not observable from out here.
This commit is contained in:
1 parent
a0fd9a00ac
commit
2a0f45ec4b
1 file changed
+32
-34
@@ -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<array{name: string, messages: int, maxCoroutines: int}> $queues
|
||||
* @param list<array{name: string, messages: int, maxCoroutines: int, batch?: int}> $queues
|
||||
* @return array{0: array<string, int>, 1: array<string, int>} [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);
|
||||
|
||||
Reference in new issue
Block a user