diff --git a/src/ExponentialBackOffStrategy.php b/src/ExponentialBackOffStrategy.php index c7652f3..2a30342 100644 --- a/src/ExponentialBackOffStrategy.php +++ b/src/ExponentialBackOffStrategy.php @@ -50,6 +50,9 @@ public function backOff(int $tries, Throwable $throwable): void } $delay = (int) ($this->initialDelayMs * $this->base ** ($tries - 1)); + if($delay < 0){ + $delay = $this->maxDelay; + } $delay = min($this->maxDelay, $delay); $delay = $this->jitter->jitter($delay); diff --git a/src/ExponentialBackOffStrategyTest.php b/src/ExponentialBackOffStrategyTest.php index 5ee82a8..9a32d29 100644 --- a/src/ExponentialBackOffStrategyTest.php +++ b/src/ExponentialBackOffStrategyTest.php @@ -148,4 +148,13 @@ public function it_does_not_throw_an_exception_when_max_tries_is_infinite(): voi $backoff->backOff(PHP_INT_MAX, $exception); } + + /** @test */ + public function it_does_not_throw_exception_when_sleep_time_overflows_and_instead_respects_the_max_sleep_time() + { + $backoff = new ExponentialBackOffStrategy(50, 10000, 5000, 2.0, $this->sleeper); + $backoff->backOff(60, new RuntimeException('oops')); + + self::assertEquals(5000, $this->recordedSleep); + } }