Skip to content

Commit

Permalink
allows receive closure in retry method from PendingRequest (#46653)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamtrevisan authored Apr 2, 2023
1 parent 04a7042 commit fdfcf9e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class PendingRequest
/**
* The number of milliseconds to wait between retries.
*
* @var int
* @var Closure|int
*/
protected $retryDelay = 100;

Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit fdfcf9e

Please sign in to comment.