Skip to content

[10.x] Fix flaky test using microtime #48156

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

Merged
merged 2 commits into from
Aug 24, 2023
Merged
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
37 changes: 29 additions & 8 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Env;
use Illuminate\Support\Optional;
use Illuminate\Support\Sleep;
use Illuminate\Support\Stringable;
use Illuminate\Tests\Support\Fixtures\IntBackedEnum;
use Illuminate\Tests\Support\Fixtures\StringBackedEnum;
Expand Down Expand Up @@ -775,7 +776,7 @@ public function something()

public function testRetry()
{
$startTime = microtime(true);
Sleep::fake();

$attempts = retry(2, function ($attempts) {
if ($attempts > 1) {
Expand All @@ -789,12 +790,16 @@ public function testRetry()
$this->assertEquals(2, $attempts);

// Make sure we waited 100ms for the first attempt
$this->assertEqualsWithDelta(0.1, microtime(true) - $startTime, 0.03);
Sleep::assertSleptTimes(1);

Sleep::assertSequence([
Sleep::usleep(100_000),
]);
}

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

$attempts = retry(3, function ($attempts) {
if ($attempts > 2) {
Expand All @@ -812,12 +817,17 @@ public function testRetryWithPassingSleepCallback()
$this->assertEquals(3, $attempts);

// Make sure we waited 300ms for the first two attempts
$this->assertEqualsWithDelta(0.3, microtime(true) - $startTime, 0.03);
Sleep::assertSleptTimes(2);

Sleep::assertSequence([
Sleep::usleep(100_000),
Sleep::usleep(200_000),
]);
}

public function testRetryWithPassingWhenCallback()
{
$startTime = microtime(true);
Sleep::fake();

$attempts = retry(2, function ($attempts) {
if ($attempts > 1) {
Expand All @@ -833,7 +843,11 @@ public function testRetryWithPassingWhenCallback()
$this->assertEquals(2, $attempts);

// Make sure we waited 100ms for the first attempt
$this->assertEqualsWithDelta(0.1, microtime(true) - $startTime, 0.03);
Sleep::assertSleptTimes(1);

Sleep::assertSequence([
Sleep::usleep(100_000),
]);
}

public function testRetryWithFailingWhenCallback()
Expand All @@ -853,7 +867,8 @@ public function testRetryWithFailingWhenCallback()

public function testRetryWithBackoff()
{
$startTime = microtime(true);
Sleep::fake();

$attempts = retry([50, 100, 200], function ($attempts) {
if ($attempts > 3) {
return $attempts;
Expand All @@ -865,7 +880,13 @@ public function testRetryWithBackoff()
// Make sure we made four attempts
$this->assertEquals(4, $attempts);

$this->assertEqualsWithDelta(0.05 + 0.1 + 0.2, microtime(true) - $startTime, 0.05);
Sleep::assertSleptTimes(3);

Sleep::assertSequence([
Sleep::usleep(50_000),
Sleep::usleep(100_000),
Sleep::usleep(200_000),
]);
}

public function testTransform()
Expand Down