Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] Passing actual Envelope to WorkerMessageRetriedEvent #54057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function onMessageFailed(WorkerMessageFailedEvent $event)
$retryEnvelope = $this->withLimitedHistory($envelope, new DelayStamp($delay), new RedeliveryStamp($retryCount));

// re-send the message for retry
$this->getSenderForTransport($event->getReceiverName())->send($retryEnvelope);
$retryEnvelope = $this->getSenderForTransport($event->getReceiverName())->send($retryEnvelope);

if (null !== $this->eventDispatcher) {
$this->eventDispatcher->dispatch(new WorkerMessageRetriedEvent($retryEnvelope, $event->getReceiverName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageRetriedEvent;
use Symfony\Component\Messenger\EventListener\SendFailedMessageForRetryListener;
use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException;
use Symfony\Component\Messenger\Retry\RetryStrategyInterface;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

Expand Down Expand Up @@ -190,4 +193,47 @@ public function testEnvelopeKeepOnlyTheLast10Stamps()

$listener->onMessageFailed($event);
}

public function testRetriedEnvelopePassesToRetriedEvent()
{
$exception = new \Exception('no!');
$envelope = new Envelope(new \stdClass());

$sender = $this->createMock(SenderInterface::class);
$sender->expects($this->once())->method('send')->willReturnCallback(static function (Envelope $envelope) {
return $envelope->with(new TransportMessageIdStamp(123));
});

$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects($this->once())->method('dispatch')->willReturnCallback(
function (WorkerMessageRetriedEvent $retriedEvent) {
$envelope = $retriedEvent->getEnvelope();

$transportIdStamp = $envelope->last(TransportMessageIdStamp::class);
$this->assertNotNull($transportIdStamp);

return $retriedEvent;
});

$senderLocator = new Container();
$senderLocator->set('my_receiver', $sender);

$retryStrategy = $this->createMock(RetryStrategyInterface::class);
$retryStrategy->expects($this->once())->method('isRetryable')->willReturn(true);
$retryStrategy->expects($this->once())->method('getWaitingTime')->willReturn(1000);

$retryStrategyLocator = new Container();
$retryStrategyLocator->set('my_receiver', $retryStrategy);

$listener = new SendFailedMessageForRetryListener(
$senderLocator,
$retryStrategyLocator,
null,
$eventDispatcher
);

$event = new WorkerMessageFailedEvent($envelope, 'my_receiver', $exception);

$listener->onMessageFailed($event);
}
}