Skip to content

Commit 7edcb11

Browse files
authored
nitpick time_sleep_until_basic.phpt
was randomly failing on my windows-WSL linux system. remove rounding of target time. remove rounding of tolerance on windows, it looked dodgy. hardcode tolerance rather than dynamically adjusting, makes it easier to reason about. Only tested on WSL-Linux as of writing, but the github CI should do the rest.
1 parent 9d60dc1 commit 7edcb11

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

ext/standard/tests/misc/time_sleep_until_basic.phpt

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,29 @@ Michele Orselli [email protected]
1111
#PHPTestFest Cesena Italia on 2009-06-20
1212
--FILE--
1313
<?php
14-
$time = microtime(true) + 2;
15-
$sleepUntil = (int) $time;
14+
/**
15+
* Checks if a value is within a specified tolerance of a target value.
16+
*
17+
* @param float $val The value to test.
18+
* @param float $target The target value.
19+
* @param float $tolerance The allowed tolerance.
20+
*
21+
* @return bool True if the absolute difference is less than or equal to the tolerance, false otherwise.
22+
*/
23+
function isWithinTolerance(float $val, float $target, float $tolerance): bool {
24+
return abs($val - $target) <= $tolerance;
25+
}
26+
27+
// Calculate the target sleep time (2 seconds from now)
28+
$targetTime = microtime(true) + 2;
29+
$sleepUntil = $targetTime;
30+
31+
// Sleep until the target time and output the result of time_sleep_until()
1632
var_dump(time_sleep_until($sleepUntil));
17-
$now = microtime(true);
18-
if (substr(PHP_OS, 0, 3) == 'WIN') {
33+
34+
// Capture the current time immediately after sleep
35+
$currentTime = microtime(true);
36+
if (stripos(PHP_OS, 'WIN') === 0) {
1937
// on windows, time_sleep_until has millisecond accuracy while microtime() is accurate
2038
// to 10th of a second. this means there can be up to a .9 millisecond difference
2139
// which will fail this test. this test randomly fails on Windows and this is the cause.
@@ -24,20 +42,22 @@ if (substr(PHP_OS, 0, 3) == 'WIN') {
2442
// passes for up to .5 milliseconds less, fails for more than .5 milliseconds
2543
// should be fine since time_sleep_until() on Windows is accurate to the
2644
// millisecond(.5 rounded up is 1 millisecond)
27-
// In practice, on slower machines even that can fail, so giving yet 50ms or more.
28-
$tmp = round($now, 3);
29-
$now = $tmp >= (int)$time ? $tmp : $tmp + .05;
45+
$tolerance = 0.05;
46+
} elseif (stripos(PHP_OS, 'DARWIN') === 0) {
47+
// macOS: time_sleep_until() may wake up slightly early for unknown reasons. Allow a larger tolerance.
48+
$tolerance = 0.004;
49+
} else {
50+
// Default tolerance
51+
$tolerance = 0.002;
3052
}
3153

32-
// Add some tolerance for early wake on macos. Reason unknown.
33-
if ($now + 0.002 >= $sleepUntil) {
34-
echo "Success\n";
54+
if ($currentTime >= $sleepUntil && isWithinTolerance($currentTime, $sleepUntil, $tolerance)) {
55+
echo "Success" . PHP_EOL;
3556
} else {
36-
echo "Sleep until (before truncation): ", $time, "\n";
37-
echo "Sleep until: ", $sleepUntil, "\n";
38-
echo "Now: ", $now, "\n";
57+
echo "Sleep until (before truncation): {$targetTime}" . PHP_EOL;
58+
echo "Sleep until: {$sleepUntil}" . PHP_EOL;
59+
echo "Now: {$currentTime}" . PHP_EOL;
3960
}
40-
4161
?>
4262
--EXPECT--
4363
bool(true)

0 commit comments

Comments
 (0)