Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Internal/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function consume(
$watcher,
$completionMarker,
$context,
$polls,
): void {
$watcher->watch();

Expand All @@ -66,6 +67,12 @@ function consume(
}

$tx->commit();

// A full batch means the queue may hold more messages. Inserts that already happened will not notify
// again, so waiting for the next signal would leave the backlog idle for up to $pollInterval.
if (\count($messages) === $config->batch && !$polls->isComplete()) {
$polls->pushAsync(null)->ignore();
}
} catch (\Throwable $e) {
$tx->rollback();
$completionMarker->error($e);
Expand Down
33 changes: 33 additions & 0 deletions tests/PgmqTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Amp\Postgres\PostgresConnection;
use Amp\Postgres\PostgresConnectionPool;
use Amp\Postgres\PostgresQueryError;
use Amp\TimeoutCancellation;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -326,6 +327,38 @@ static function (array $messages, ConsumeController $ctrl) use (&$consumed): voi
self::assertSame(0, $queue->metrics()->length);
}

public function testConsumerDrainsBacklogWithoutWaitingForPollInterval(): void
{
$queue = createQueue($this->pg, $this->randomQueueName());
$queue->sendBatch(array_fill(0, 25, new SendMessage(self::TESTING_MESSAGE)));

$consumed = 0;

$consumer = createConsumer($this->pg);
$context = $consumer->consume(
static function (array $messages, ConsumeController $ctrl) use (&$consumed): void {
$consumed += \count($messages);
$ctrl->ack($messages);

if ($consumed === 25) {
$ctrl->stop();
}
},
new ConsumeConfig(
queue: $queue->name,
batch: 10,
pollInterval: TimeSpan::fromSeconds(10),
),
);

// The backlog is already there: no further inserts will wake the consumer up, so a full batch must
// trigger the next read immediately instead of waiting for the poll interval (2 x 10 s for 25 messages).
$context->awaitCompletion(new TimeoutCancellation(2));

self::assertSame(25, $consumed);
self::assertSame(0, $queue->metrics()->length);
}

public function testNackBatch(): void
{
$queue = createQueue($this->pg, $this->randomQueueName());
Expand Down