8000 [Messenger] fix wrong use of generator returns by Tobion · Pull Request #31389 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] fix wrong use of generator returns #31389

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
May 6, 2019
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 @@ -32,7 +32,7 @@ public function __construct(string $exceptionMessage, string $originalReceiverNa
$this->exceptionMessage = $exceptionMessage;
$this->originalReceiverName = $originalReceiverName;
$this->flattenException = $flattenException;
$this->sentAt = new \DateTime();
$this->sentAt = new \DateTimeImmutable();
}

public function getExceptionMessage(): string
Expand All @@ -50,7 +50,7 @@ public function getFlattenException(): ?FlattenException
return $this->flattenException;
}

public function getSentAt(): \DateTime
public function getSentAt(): \DateTimeInterface
{
return $this->sentAt;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public function testGetters()
$this->assertSame('exception message', $stamp->getExceptionMessage());
$this->assertSame('original_receiver', $stamp->getOriginalReceiverName());
$this->assertSame($flattenException, $stamp->getFlattenException());
$this->assertInstanceOf(\DateTime::class, $stamp->getSentAt());
$this->assertInstanceOf(\DateTimeInterface::class, $stamp->getSentAt());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testItReturnsTheDecodedMessageToTheHandler()
$connection->method('get')->willReturn($doctrineEnvelope);

$receiver = new DoctrineReceiver($connection, $serializer);
$actualEnvelopes = iterator_to_array($receiver->get());
$actualEnvelopes = $receiver->get();
$this->assertCount(1, $actualEnvelopes);
/** @var Envelope $actualEnvelope */
$actualEnvelope = $actualEnvelopes[0];
Expand Down Expand Up @@ -67,7 +67,7 @@ public function testItRejectTheMessageIfThereIsAMessageDecodingFailedException()
$connection->expects($this->once())->method('reject');

$receiver = new DoctrineReceiver($connection, $serializer);
iterator_to_array($receiver->get());
$receiver->get();
}

public function testAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testReceivesMessages()
$serializer->method('decode')->with(['body' => 'body', 'headers' => ['my' => 'header']])->willReturn(new Envelope($decodedMessage));
$connection->method('get')->willReturn($doctrineEnvelope);

$envelopes = iterator_to_array($transport->get());
$envelopes = $transport->get();
$this->assertSame($decodedMessage, $envelopes[0]->getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Transport\Receiver\SingleMessageReceiver;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
use Symfony\Component\Messenger\Transport\Sender\SendersLocator;

class SingleMessageReceiverTest extends TestCase
{
Expand All @@ -28,11 +24,11 @@ public function testItReceivesOnlyOneMessage()
$envelope = new Envelope(new \stdClass());

$receiver = new SingleMessageReceiver($innerReceiver, $envelope);
$received = \iterator_to_array($receiver->get());
$received = $receiver->get();
$this->assertCount(1, $received);
$this->assertSame($received[0], $envelope);

$this->assertEmpty(\iterator_to_array($receiver->get()));
$this->assertEmpty($receiver->get());
}

public function testCallsAreForwarded()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testItReturnsTheDecodedMessageToTheHandler()
$connection->method('get')->willReturn($redisEnvelop);

$receiver = new RedisReceiver($connection, $serializer);
$actualEnvelopes = iterator_to_array($receiver->get());
$actualEnvelopes = $receiver->get();
$this->assertCount(1, $actualEnvelopes);
$this->assertEquals(new DummyMessage('Hi'), $actualEnvelopes[0]->getMessage());
}
Expand All @@ -51,7 +51,7 @@ public function testItRejectTheMessageIfThereIsAMessageDecodingFailedException()
$connection->expects($this->once())->method('reject');

$receiver = new RedisReceiver($connection, $serializer);
iterator_to_array($receiver->get());
$receiver->get();
}

private function createRedisEnvelope()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testReceivesMessages()
$serializer->method('decode')->with(['body' => 'body', 'headers' => ['my' => 'header']])->willReturn(new Envelope($decodedMessage));
$connection->method('get')->willReturn($redisEnvelope);

$envelopes = iterator_to_array($transport->get());
$envelopes = $transport->get();
$this->assertSame($decodedMessage, $envelopes[0]->getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private function getEnvelope(string $queueName): iterable
}

if (null === $amqpEnvelope) {
return [];
return;
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ public function getMessageCount(): int
return ($this->receiver ?? $this->getReceiver())->getMessageCount();
}

private function getReceiver()
private function getReceiver(): AmqpReceiver
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added typehints to make it consistent with DoctrineTransport.

{
return $this->receiver = new AmqpReceiver($this->connection, $this->serializer);
}

private function getSender()
private function getSender(): AmqpSender
{
return $this->sender = new AmqpSender($this->connection, $this->serializer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function get(): iterable
return [];
}

yield $this->createEnvelopeFromData($doctrineEnvelope);
return [$this->createEnvelopeFromData($doctrineEnvelope)];
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Transport\Receiver;

use Symfony\Component\Messenger\Envelope;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Transport\Receiver;

use Symfony\Component\Messenger\Envelope;
Expand Down Expand Up @@ -32,7 +41,7 @@ public function get(): iterable

$this->hasReceived = true;

yield $this->envelope;
return [$this->envelope];
}

public function ack(Envelope $envelope): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function get(): iterable
throw $exception;
}

yield $envelope->with(new RedisReceivedStamp($redisEnvelope['id']));
return [$envelope->with(new RedisReceivedStamp($redisEnvelope['id']))];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public function setup(): void
$this->connection->setup();
}

private function getReceiver()
private function getReceiver(): RedisReceiver
{
return $this->receiver = new RedisReceiver($this->connection, $this->serializer);
}

private function getSender()
private function getSender(): RedisSender
{
return $this->sender = new RedisSender($this->connection, $this->serializer);
}
Expand Down
13 changes: 2 additions & 11 deletions src/Symfony/Component/Messenger/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,11 @@ private function handleMessage(Envelope $envelope, ReceiverInterface $receiver,
$envelope = $throwable->getEnvelope();
}

$shouldRetry = $this->shouldRetry($throwable, $envelope, $retryStrategy);
$shouldRetry = $retryStrategy && $this->shouldRetry($throwable, $envelope, $retryStrategy);

$this->dispatchEvent(new WorkerMessageFailedEvent($envelope, $transportName, $throwable, $shouldRetry));

if ($shouldRetry) {
if (null === $retryStrategy) {
// not logically allowed, but check just in case
throw new LogicException('Retrying is not supported without a retry strategy.');
}

$retryCount = $this->getRetryCount($envelope) + 1;
if (null !== $this->logger) {
$this->logger->error('Retrying {class} - retry #{retryCount}.', $context + ['retryCount' => $retryCount, 'error' => $throwable]);
Expand Down Expand Up @@ -194,16 +189,12 @@ private function dispatchEvent($event)
$this->eventDispatcher->dispatch($event);
}

private function shouldRetry(\Throwable $e, Envelope $envelope, ?RetryStrategyInterface $retryStrategy): bool
private function shouldRetry(\Throwable $e, Envelope $envelope, RetryStrategyInterface $retryStrategy): bool
{
if ($e instanceof UnrecoverableMessageHandlingException) {
return false;
}

if (null === $retryStrategy) {
return false;
}

$sentStamp = $envelope->last(SentStamp::class);
if (null === $sentStamp) {
if (null !== $this->logger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function stop(): void
$this->decoratedWorker->stop();
}

private function shouldRestart(float $workerStartedAt)
private function shouldRestart(float $workerStartedAt): bool
{
$cacheItem = $this->cachePool->getItem(self::RESTART_REQUESTED_TIMESTAMP_KEY);

Expand Down
0