From 8b673f5a81352dcf4d399eb04e96ee8fb8dfdffe Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 20 Jul 2020 10:18:07 +0200 Subject: [PATCH] [Mailer] Fix failover transport --- .../Mailer/Tests/Transport/FailoverTransportTest.php | 12 ------------ .../Component/Mailer/Transport/FailoverTransport.php | 5 +++++ .../Mailer/Transport/RoundRobinTransport.php | 11 ++++++++--- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Component/Mailer/Tests/Transport/FailoverTransportTest.php b/src/Symfony/Component/Mailer/Tests/Transport/FailoverTransportTest.php index c22a3bfaf3b50..7e66309cabb41 100644 --- a/src/Symfony/Component/Mailer/Tests/Transport/FailoverTransportTest.php +++ b/src/Symfony/Component/Mailer/Tests/Transport/FailoverTransportTest.php @@ -46,9 +46,6 @@ public function testSendFirstWork() $t2 = $this->createMock(TransportInterface::class); $t2->expects($this->never())->method('send'); $t = new FailoverTransport([$t1, $t2]); - $p = new \ReflectionProperty(RoundRobinTransport::class, 'cursor'); - $p->setAccessible(true); - $p->setValue($t, 0); $t->send(new RawMessage('')); $this->assertTransports($t, 1, []); $t->send(new RawMessage('')); @@ -77,9 +74,6 @@ public function testSendOneDead() $t2 = $this->createMock(TransportInterface::class); $t2->expects($this->exactly(3))->method('send'); $t = new FailoverTransport([$t1, $t2]); - $p = new \ReflectionProperty(RoundRobinTransport::class, 'cursor'); - $p->setAccessible(true); - $p->setValue($t, 0); $t->send(new RawMessage('')); $this->assertTransports($t, 0, [$t1]); $t->send(new RawMessage('')); @@ -99,9 +93,6 @@ public function testSendOneDeadAndRecoveryWithinRetryPeriod() $t2->expects($this->at(2))->method('send'); $t2->expects($this->at(3))->method('send')->will($this->throwException(new TransportException())); $t = new FailoverTransport([$t1, $t2], 6); - $p = new \ReflectionProperty(RoundRobinTransport::class, 'cursor'); - $p->setAccessible(true); - $p->setValue($t, 0); $t->send(new RawMessage('')); // t1>fail - t2>sent $this->assertTransports($t, 0, [$t1]); sleep(4); @@ -148,9 +139,6 @@ public function testSendOneDeadButRecover() $t2->expects($this->at(1))->method('send'); $t2->expects($this->at(2))->method('send')->will($this->throwException(new TransportException())); $t = new FailoverTransport([$t1, $t2], 1); - $p = new \ReflectionProperty(RoundRobinTransport::class, 'cursor'); - $p->setAccessible(true); - $p->setValue($t, 0); $t->send(new RawMessage('')); sleep(1); $t->send(new RawMessage('')); diff --git a/src/Symfony/Component/Mailer/Transport/FailoverTransport.php b/src/Symfony/Component/Mailer/Transport/FailoverTransport.php index 7d8f54c011f9c..49139013f005a 100644 --- a/src/Symfony/Component/Mailer/Transport/FailoverTransport.php +++ b/src/Symfony/Component/Mailer/Transport/FailoverTransport.php @@ -29,6 +29,11 @@ protected function getNextTransport(): ?TransportInterface return $this->currentTransport; } + protected function getInitialCursor(): int + { + return 0; + } + protected function getNameSymbol(): string { return 'failover'; diff --git a/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php b/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php index 873ab584814b2..469673563b9a1 100644 --- a/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php +++ b/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php @@ -67,9 +67,7 @@ public function __toString(): string protected function getNextTransport(): ?TransportInterface { if (-1 === $this->cursor) { - // the cursor initial value is randomized so that - // when are not in a daemon, we are still rotating the transports - $this->cursor = mt_rand(0, \count($this->transports) - 1); + $this->cursor = $this->getInitialCursor(); } $cursor = $this->cursor; @@ -101,6 +99,13 @@ protected function isTransportDead(TransportInterface $transport): bool return $this->deadTransports->contains($transport); } + protected function getInitialCursor(): int + { + // the cursor initial value is randomized so that + // when are not in a daemon, we are still rotating the transports + return mt_rand(0, \count($this->transports) - 1); + } + protected function getNameSymbol(): string { return 'roundrobin';