Skip to content

Commit

Permalink
Remove calls to onConsecutiveCalls()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Apr 30, 2024
1 parent 93543ff commit 85ba4ca
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 24 deletions.
73 changes: 51 additions & 22 deletions Tests/Transport/FailoverTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,30 @@ public function testSendOneDead()
public function testSendOneDeadAndRecoveryWithinRetryPeriod()
{
$t1 = $this->createMock(TransportInterface::class);
$t1->method('send')->willReturnOnConsecutiveCalls($this->throwException(new TransportException()));

$t1Matcher = $this->any();
$t1->expects($t1Matcher)
->method('send')
->willReturnCallback(function () use ($t1Matcher) {
if (1 === $t1Matcher->getInvocationCount()) {
throw new TransportException();
}

return null;
});

$t2 = $this->createMock(TransportInterface::class);
$t2->expects($this->exactly(4))
$t2Matcher = $this->exactly(4);
$t2->expects($t2Matcher)
->method('send')
->willReturnOnConsecutiveCalls(
null,
null,
null,
$this->throwException(new TransportException())
);
->willReturnCallback(function () use ($t2Matcher) {
if (4 === $t2Matcher->getInvocationCount()) {
throw new TransportException();
}

return null;
});

$t = new FailoverTransport([$t1, $t2], 6);
$t->send(new RawMessage('')); // t1>fail - t2>sent
$this->assertTransports($t, 0, [$t1]);
Expand All @@ -115,16 +129,19 @@ public function testSendOneDeadAndRecoveryWithinRetryPeriod()
public function testSendAllDeadWithinRetryPeriod()
{
$t1 = $this->createMock(TransportInterface::class);
$t1->method('send')->will($this->throwException(new TransportException()));
$t1->method('send')->willThrowException(new TransportException());
$t1->expects($this->once())->method('send');
$t2 = $this->createMock(TransportInterface::class);
$t2->expects($this->exactly(3))
$matcher = $this->exactly(3);
$t2->expects($matcher)
->method('send')
->willReturnOnConsecutiveCalls(
null,
null,
$this->throwException(new TransportException())
);
->willReturnCallback(function () use ($matcher) {
if (3 === $matcher->getInvocationCount()) {
throw new TransportException();
}

return null;
});
$t = new FailoverTransport([$t1, $t2], 40);
$t->send(new RawMessage(''));
sleep(4);
Expand All @@ -137,15 +154,27 @@ public function testSendAllDeadWithinRetryPeriod()

public function testSendOneDeadButRecover()
{
$t1Matcher = $this->any();
$t1 = $this->createMock(TransportInterface::class);
$t1->method('send')->willReturnOnConsecutiveCalls($this->throwException(new TransportException()));
$t1->expects($t1Matcher)->method('send')->willReturnCallback(function () use ($t1Matcher) {
if (1 === $t1Matcher->getInvocationCount()) {
throw new TransportException();
}

return null;
});

$t2 = $this->createMock(TransportInterface::class);
$t2->expects($this->exactly(3))
->method('send')->willReturnOnConsecutiveCalls(
null,
null,
$this->throwException(new TransportException())
);
$matcher = $this->exactly(3);
$t2->expects($matcher)
->method('send')
->willReturnCallback(function () use ($matcher) {
if (3 === $matcher->getInvocationCount()) {
throw new TransportException();
}

return null;
});
$t = new FailoverTransport([$t1, $t2], 1);
$t->send(new RawMessage(''));
sleep(1);
Expand Down
12 changes: 10 additions & 2 deletions Tests/Transport/RoundRobinTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,18 @@ public function testSendOneDeadAndRecoveryWithinRetryPeriod()
{
$t1 = $this->createMock(TransportInterface::class);
$t1->expects($this->exactly(3))->method('send');

$matcher = $this->exactly(2);
$t2 = $this->createMock(TransportInterface::class);
$t2->expects($this->exactly(2))
$t2->expects($matcher)
->method('send')
->willReturnOnConsecutiveCalls($this->throwException(new TransportException()));
->willReturnCallback(function () use ($matcher) {
if (1 === $matcher->getInvocationCount()) {
throw new TransportException();
}

return null;
});
$t = new RoundRobinTransport([$t1, $t2], 3);
$p = new \ReflectionProperty($t, 'cursor');
$p->setAccessible(true);
Expand Down

0 comments on commit 85ba4ca

Please sign in to comment.