Skip to content

Commit 9f255a7

Browse files
[10.x] HTTP retry method can accept array as first param (#50064)
* HTTP retry method can accept array as first param * Update PendingRequest.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent d3533e1 commit 9f255a7

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,13 @@ public function connectTimeout(int $seconds)
600600
/**
601601
* Specify the number of times the request should be attempted.
602602
*
603-
* @param int $times
603+
* @param array|int $times
604604
* @param Closure|int $sleepMilliseconds
605605
* @param callable|null $when
606606
* @param bool $throw
607607
* @return $this
608608
*/
609-
public function retry(int $times, Closure|int $sleepMilliseconds = 0, ?callable $when = null, bool $throw = true)
609+
public function retry(array|int $times, Closure|int $sleepMilliseconds = 0, ?callable $when = null, bool $throw = true)
610610
{
611611
$this->tries = $times;
612612
$this->retryDelay = $sleepMilliseconds;

tests/Http/HttpClientTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,34 @@ public function testRequestsWillBeWaitingSleepMillisecondsReceivedBeforeRetry()
18581858
]);
18591859
}
18601860

1861+
public function testRequestsWillBeWaitingSleepMillisecondsReceivedInBackoffArray()
1862+
{
1863+
Sleep::fake();
1864+
1865+
$this->factory->fake([
1866+
'*' => $this->factory->sequence()
1867+
->push(['error'], 500)
1868+
->push(['error'], 500)
1869+
->push(['error'], 500)
1870+
->push(['ok'], 200),
1871+
]);
1872+
1873+
$this->factory
1874+
->retry([50, 100, 200], 0, null, true)
1875+
->get('http://foo.com/get');
1876+
1877+
$this->factory->assertSentCount(4);
1878+
1879+
// Make sure we waited 300ms for the first two attempts
1880+
Sleep::assertSleptTimes(3);
1881+
1882+
Sleep::assertSequence([
1883+
Sleep::usleep(50_000),
1884+
Sleep::usleep(100_000),
1885+
Sleep::usleep(200_000),
1886+
]);
1887+
}
1888+
18611889
public function testMiddlewareRunsWhenFaked()
18621890
{
18631891
$this->factory->fake(function (Request $request) {

0 commit comments

Comments
 (0)