Skip to content

Commit

Permalink
accept closure for sleepMilliseconds (#38035)
Browse files Browse the repository at this point in the history
  • Loading branch information
bonroyage authored Jul 16, 2021
1 parent 345c086 commit 035a0b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function preg_replace_array($pattern, array $replacements, $subject)
*
* @param int $times
* @param callable $callback
* @param int $sleepMilliseconds
* @param int|\Closure $sleepMilliseconds
* @param callable|null $when
* @return mixed
*
Expand All @@ -238,7 +238,7 @@ function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)
}

if ($sleepMilliseconds) {
usleep($sleepMilliseconds * 1000);
usleep(value($sleepMilliseconds, $attempts) * 1000);
}

goto beginning;
Expand Down
21 changes: 21 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,27 @@ public function testRetry()
$this->assertEqualsWithDelta(0.1, microtime(true) - $startTime, 0.02);
}

public function testRetryWithPassingSleepCallback()
{
$startTime = microtime(true);

$attempts = retry(3, function ($attempts) {
if ($attempts > 2) {
return $attempts;
}

throw new RuntimeException;
}, function ($attempt) {
return $attempt * 100;
});

// Make sure we made three attempts
$this->assertEquals(3, $attempts);

// Make sure we waited 300ms for the first two attempts
$this->assertEqualsWithDelta(0.3, microtime(true) - $startTime, 0.02);
}

public function testRetryWithPassingWhenCallback()
{
$startTime = microtime(true);
Expand Down

0 comments on commit 035a0b2

Please sign in to comment.