From fdfcf9eceb7cd8ae480e1b9ce489812fd87c2212 Mon Sep 17 00:00:00 2001 From: William Trevisan <73068886+williamtrevisan@users.noreply.github.com> Date: Sun, 2 Apr 2023 13:18:09 -0300 Subject: [PATCH] allows receive closure in retry method from PendingRequest (#46653) --- src/Illuminate/Http/Client/PendingRequest.php | 6 ++--- tests/Http/HttpClientTest.php | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) 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) {