Skip to content

Commit f981223

Browse files
authored
[10.x] Fix flaky test using microtime (#48156)
* Use the Sleep::fake feature instead of microtime to fix flakiness * StyleCI
1 parent 1a1969c commit f981223

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

tests/Support/SupportHelpersTest.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Support\Htmlable;
99
use Illuminate\Support\Env;
1010
use Illuminate\Support\Optional;
11+
use Illuminate\Support\Sleep;
1112
use Illuminate\Support\Stringable;
1213
use Illuminate\Tests\Support\Fixtures\IntBackedEnum;
1314
use Illuminate\Tests\Support\Fixtures\StringBackedEnum;
@@ -775,7 +776,7 @@ public function something()
775776

776777
public function testRetry()
777778
{
778-
$startTime = microtime(true);
779+
Sleep::fake();
779780

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

791792
// Make sure we waited 100ms for the first attempt
792-
$this->assertEqualsWithDelta(0.1, microtime(true) - $startTime, 0.03);
793+
Sleep::assertSleptTimes(1);
794+
795+
Sleep::assertSequence([
796+
Sleep::usleep(100_000),
797+
]);
793798
}
794799

795800
public function testRetryWithPassingSleepCallback()
796801
{
797-
$startTime = microtime(true);
802+
Sleep::fake();
798803

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

814819
// Make sure we waited 300ms for the first two attempts
815-
$this->assertEqualsWithDelta(0.3, microtime(true) - $startTime, 0.03);
820+
Sleep::assertSleptTimes(2);
821+
822+
Sleep::assertSequence([
823+
Sleep::usleep(100_000),
824+
Sleep::usleep(200_000),
825+
]);
816826
}
817827

818828
public function testRetryWithPassingWhenCallback()
819829
{
820-
$startTime = microtime(true);
830+
Sleep::fake();
821831

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

835845
// Make sure we waited 100ms for the first attempt
836-
$this->assertEqualsWithDelta(0.1, microtime(true) - $startTime, 0.03);
846+
Sleep::assertSleptTimes(1);
847+
848+
Sleep::assertSequence([
849+
Sleep::usleep(100_000),
850+
]);
837851
}
838852

839853
public function testRetryWithFailingWhenCallback()
@@ -853,7 +867,8 @@ public function testRetryWithFailingWhenCallback()
853867

854868
public function testRetryWithBackoff()
855869
{
856-
$startTime = microtime(true);
870+
Sleep::fake();
871+
857872
$attempts = retry([50, 100, 200], function ($attempts) {
858873
if ($attempts > 3) {
859874
return $attempts;
@@ -865,7 +880,13 @@ public function testRetryWithBackoff()
865880
// Make sure we made four attempts
866881
$this->assertEquals(4, $attempts);
867882

868-
$this->assertEqualsWithDelta(0.05 + 0.1 + 0.2, microtime(true) - $startTime, 0.05);
883+
Sleep::assertSleptTimes(3);
884+
885+
Sleep::assertSequence([
886+
Sleep::usleep(50_000),
887+
Sleep::usleep(100_000),
888+
Sleep::usleep(200_000),
889+
]);
869890
}
870891

871892
public function testTransform()

0 commit comments

Comments
 (0)