Skip to content

Commit

Permalink
retry attempts fixed. Exception throwing for the retry() method unifi…
Browse files Browse the repository at this point in the history
…ed for all cases
  • Loading branch information
alkinoy authored and norberttech committed Jan 24, 2025
1 parent 3e8415e commit 56a2483
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
14 changes: 1 addition & 13 deletions src/retry/src/Aeon/Retry/Retry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
33 changes: 33 additions & 0 deletions src/retry/tests/Aeon/Retry/Tests/Unit/RetryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}
}

0 comments on commit 56a2483

Please sign in to comment.