Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] allow $sleepMilliseconds parameter receive a Closure in retry method from PendingRequest #46653

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could fail in slow machines

Copy link
Contributor Author

@williamtrevisan williamtrevisan May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know another approach that I can use here to test this? Or maybe it's not necessary test that functionality knowing that is already tested in retry helper scope? Basically I have used the same approach that exists there

}

public function testMiddlewareRunsWhenFaked()
{
$this->factory->fake(function (Request $request) {
Expand Down