diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 71660e1c2298..ceb657736b5d 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -131,7 +131,7 @@ class PendingRequest /** * The number of milliseconds to wait between retries. * - * @var int + * @var Closure|int */ protected $retryDelay = 100; @@ -557,12 +557,12 @@ public function connectTimeout(int $seconds) * Specify the number of times the request should be attempted. * * @param int $times - * @param int $sleepMilliseconds + * @param Closure|int $sleepMilliseconds * @param callable|null $when * @param bool $throw * @return $this */ - public function retry(int $times, int $sleepMilliseconds = 0, ?callable $when = null, bool $throw = true) + public function retry(int $times, Closure|int $sleepMilliseconds = 0, ?callable $when = null, bool $throw = true) { $this->tries = $times; $this->retryDelay = $sleepMilliseconds; diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index d0fd33198ab5..f15b0ae71d93 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -1576,6 +1576,31 @@ public function testExceptionThrownInRetryCallbackWithoutRetrying() $this->factory->assertSentCount(1); } + public function testRequestsWillBeWaitingSleepMillisecondsReceivedBeforeRetry() + { + $startTime = microtime(true); + + $this->factory->fake([ + '*' => $this->factory->sequence() + ->push(['error'], 500) + ->push(['error'], 500) + ->push(['ok'], 200), + ]); + + $this->factory + ->retry(3, function ($attempt, $exception) { + $this->assertInstanceOf(RequestException::class, $exception); + + return $attempt * 100; + }, null, true) + ->get('http://foo.com/get'); + + $this->factory->assertSentCount(3); + + // Make sure was waited 300ms for the first two attempts + $this->assertEqualsWithDelta(0.3, microtime(true) - $startTime, 0.03); + } + public function testMiddlewareRunsWhenFaked() { $this->factory->fake(function (Request $request) {