Skip to content

Commit

Permalink
Merge branch '5.1'
Browse files Browse the repository at this point in the history
* 5.1:
  [Mailer] Fix failover transport
  • Loading branch information
fabpot committed Jul 20, 2020
2 parents 367aa1d + 08ff65f commit 8a7c776
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(''));
Expand Down Expand Up @@ -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(''));
Expand All @@ -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);
Expand Down Expand Up @@ -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(''));
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Transport/FailoverTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ protected function getNextTransport(): ?TransportInterface
return $this->currentTransport;
}

protected function getInitialCursor(): int
{
return 0;
}

protected function getNameSymbol(): string
{
return 'failover';
Expand Down
11 changes: 8 additions & 3 deletions src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 8a7c776

Please sign in to comment.