Skip to content

Commit

Permalink
Merge pull request #6 from EventSaucePHP/feature/allow-infinite-tries…
Browse files Browse the repository at this point in the history
…-on-any-strategy

Fixes #5: allow infinite retries on any implementation.
  • Loading branch information
frankdejonge authored Sep 17, 2023
2 parents 1fe47e1 + 623d555 commit b7429a9
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ExponentialBackOffStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(
*/
public function backOff(int $tries, Throwable $throwable): void
{
if ($tries > $this->maxTries) {
if ($this->maxTries !== -1 && $tries > $this->maxTries) {
throw $throwable;
}

Expand Down
14 changes: 14 additions & 0 deletions src/ExponentialBackOffStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use LogicException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use const PHP_INT_MAX;

class ExponentialBackOffStrategyTest extends TestCase
{
Expand Down Expand Up @@ -134,4 +135,17 @@ public function dpExpectedSleepFromExponent(): iterable
[3, 2.5, 625],
];
}

/**
* @test
*/
public function it_does_not_throw_an_exception_when_max_tries_is_infinite(): void
{
$this->expectNotToPerformAssertions();

$backoff = new ExponentialBackOffStrategy(0, -1);
$exception = new RuntimeException('oops');

$backoff->backOff(PHP_INT_MAX, $exception);
}
}
2 changes: 1 addition & 1 deletion src/FibonacciBackOffStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(

public function backOff(int $tries, Throwable $throwable): void
{
if ($tries > $this->maxTries) {
if ($this->maxTries !== -1 && $tries > $this->maxTries) {
throw $throwable;
}

Expand Down
14 changes: 14 additions & 0 deletions src/FibonacciBackOffStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use LogicException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use const PHP_INT_MAX;

class FibonacciBackOffStrategyTest extends TestCase
{
Expand Down Expand Up @@ -61,4 +62,17 @@ public function it_throws_when_over_max_tries(): void

$backOff->backOff(26, $exception);
}

/**
* @test
*/
public function it_does_not_throw_an_exception_when_max_tries_is_infinite(): void
{
$this->expectNotToPerformAssertions();

$backoff = new FibonacciBackOffStrategy(0, -1);
$exception = new RuntimeException('oops');

$backoff->backOff(PHP_INT_MAX, $exception);
}
}
2 changes: 1 addition & 1 deletion src/LinearBackOffStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(
*/
public function backOff(int $tries, Throwable $throwable): void
{
if ($tries > $this->maxTries) {
if ($this->maxTries !== -1 && $tries > $this->maxTries) {
throw $throwable;
}

Expand Down
16 changes: 16 additions & 0 deletions src/LinearBackOffStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use EventSauce\BackOff\Jitter\NoJitter;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use const PHP_INT_MAX;

class LinearBackOffStrategyTest extends TestCase
{
Expand Down Expand Up @@ -97,6 +98,21 @@ public function dpGoingOverTheMaxTries(): iterable
];
}



/**
* @test
*/
public function it_does_not_throw_an_exception_when_max_tries_is_infinite(): void
{
$this->expectNotToPerformAssertions();

$backoff = new LinearBackOffStrategy(0, -1);
$exception = new RuntimeException('oops');

$backoff->backOff(PHP_INT_MAX, $exception);
}

/**
* @test
*/
Expand Down

0 comments on commit b7429a9

Please sign in to comment.