Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/Internal/Delivery/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Thesis\Amqp\Internal\Delivery;

use Revolt\EventLoop;
use Thesis\Amqp\Channel;
use Thesis\Amqp\DeliveryMessage;

Expand All @@ -23,7 +24,7 @@ public function __construct(DeliverySupervisor $supervisor)
$supervisor->addConsumeListener(static function (DeliveryMessage $delivery, Channel $channel) use (&$consumers): void {
$consumer = $consumers[$delivery->consumerTag] ?? null;
if ($consumer !== null) {
$consumer($delivery, $channel);
EventLoop::queue(static fn() => $consumer($delivery, $channel));
}
});

Expand Down
48 changes: 47 additions & 1 deletion tests/AmqpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ public function testPublishConsume(string $exchange, string $queue, string $rout
$channel->consume(static function (DeliveryMessage $delivery) use (&$consumedMessages, $messageCount, $deferred): void {
$consumedMessages[$delivery->exchange][] = $delivery->message->body;
$delivery->ack();
if (\count($consumedMessages[$delivery->exchange]) === $messageCount) {
if (\count($consumedMessages[$delivery->exchange]) === $messageCount && !$deferred->isComplete()) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the changes in the Consumer, a race condition appeared here.

Here is a minimized race condition.
/** @var DeferredFuture<null> */
$deferred = new DeferredFuture();

/** @var \ArrayObject<int, string> */
$consumed = new \ArrayObject();

$callback = static function () use ($consumed, $deferred) {
    $consumed[] = 'elem';

    // Simulates an I/O operation (e.g., $delivery->ack())
    delay(0.1);

    // After resuming, both fibers will see that \count($consumed) === 2
    if (\count($consumed) === 2) {
        // Both fibers attempt to complete the same DeferredFuture
        // Error: the operation is no longer pending
        $deferred->complete();
    }
};

await([
    async($callback),
    async($callback),
    $deferred->getFuture(),
]);

$deferred->complete($consumedMessages);
}
}, $queue);
Expand All @@ -592,6 +592,52 @@ public function testPublishConsume(string $exchange, string $queue, string $rout
$channel->close();
}

/**
* @param non-empty-string $exchange
* @param non-empty-string $queue
* @param non-empty-string $routingKey
*/
#[TestWith(['events', 'events.orders', 'orders'])]
public function testConcurrentConsume(string $exchange, string $queue, string $routingKey): void
{
$channel = $this->client->channel();
$channel->qos(prefetchCount: 2);

$channel->queueUnbind($queue, $exchange, $routingKey);
$channel->exchangeDelete($exchange);
$channel->queueDelete($queue);

$channel->queueDeclare($queue, autoDelete: true);
$channel->exchangeDeclare($exchange, autoDelete: true);
$channel->queueBind($queue, $exchange, $routingKey);

$channel->publish(new Message('order#1'), $exchange, $routingKey);
$channel->publish(new Message('order#2'), $exchange, $routingKey);

$consumed = [];
$deferred = new DeferredFuture();

$consumerTag = $channel->consume(static function (DeliveryMessage $delivery) use (&$consumed, $deferred): void {
if ($delivery->message->body === 'order#1') {
$deferred->getFuture()->await();
}

if ($delivery->message->body === 'order#2') {
$deferred->complete();
}

$consumed[] = $delivery->message->body;
$delivery->ack();
}, $queue);

$deferred->getFuture()->await();

$channel->cancel($consumerTag);
$channel->close();

self::assertEquals(['order#2', 'order#1'], $consumed);
}

public function testPublishConsumeBatch(): void
{
$channel = $this->client->channel();
Expand Down