diff --git a/Tests/Transport/FailoverTransportTest.php b/Tests/Transport/FailoverTransportTest.php index 99be0e0..21a5b72 100644 --- a/Tests/Transport/FailoverTransportTest.php +++ b/Tests/Transport/FailoverTransportTest.php @@ -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]); @@ -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); @@ -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); diff --git a/Tests/Transport/RoundRobinTransportTest.php b/Tests/Transport/RoundRobinTransportTest.php index 08edb24..bac1ce1 100644 --- a/Tests/Transport/RoundRobinTransportTest.php +++ b/Tests/Transport/RoundRobinTransportTest.php @@ -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);