From 1f73d0590f6ef61e151539bea4bad387a19cb266 Mon Sep 17 00:00:00 2001 From: alkinoy Date: Tue, 9 Jan 2024 10:42:09 +0100 Subject: [PATCH] retry attempts fixed. Exception throwing for the retry() method unified for all cases --- src/retry/src/Aeon/Retry/Retry.php | 14 +------- .../tests/Aeon/Retry/Tests/Unit/RetryTest.php | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/retry/src/Aeon/Retry/Retry.php b/src/retry/src/Aeon/Retry/Retry.php index 6f2e8493..d7d1c2b2 100644 --- a/src/retry/src/Aeon/Retry/Retry.php +++ b/src/retry/src/Aeon/Retry/Retry.php @@ -84,19 +84,7 @@ public function execute(callable $function) */ $exceptions = []; - if ($this->retries === 0) { - $lastReturn = $function($this->lastExecution = new Execution(0)); - - $terminationException = $this->lastExecution->terminationException(); - - if ($terminationException) { - throw $terminationException; - } - - return $lastReturn; - } - - for ($retry = 0; $retry < $this->retries; $retry++) { + for ($retry = 0; $retry < ($this->retries + 1); $retry++) { try { $lastReturn = $function($this->lastExecution = new Execution($retry, $exceptions)); diff --git a/src/retry/tests/Aeon/Retry/Tests/Unit/RetryTest.php b/src/retry/tests/Aeon/Retry/Tests/Unit/RetryTest.php index b6af7851..43e727cb 100644 --- a/src/retry/tests/Aeon/Retry/Tests/Unit/RetryTest.php +++ b/src/retry/tests/Aeon/Retry/Tests/Unit/RetryTest.php @@ -249,4 +249,37 @@ public function test_throws_the_last_exception() : void ))->modifyDelay(new ConstantDelay()) ->execute($callable); } + + /** + * @dataProvider retryExceptionDataProvider + */ + public function test_throws_the_last_exception_for_given_retries($retries, $expectedException) : void + { + $this->expectExceptionMessage($expectedException); + $queue = [ + new \RuntimeException('first'), + new \RuntimeException('second'), + new \RuntimeException('third'), + ]; + + $callable = function () use (&$queue) : void { + throw \array_shift($queue); + }; + + (new Retry( + new DummyProcess(), + $retries, + TimeUnit::seconds(3) + ))->modifyDelay(new ConstantDelay()) + ->execute($callable); + } + + public function retryExceptionDataProvider() : array + { + return [ + [0, 'first'], + [1, 'second'], + [2, 'third'], + ]; + } }