From 7edcb111f8101c6dbab28989dd17001b8fd1fff9 Mon Sep 17 00:00:00 2001 From: divinity76 Date: Tue, 11 Mar 2025 11:27:21 +0100 Subject: [PATCH 1/5] 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. --- .../tests/misc/time_sleep_until_basic.phpt | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/ext/standard/tests/misc/time_sleep_until_basic.phpt b/ext/standard/tests/misc/time_sleep_until_basic.phpt index f26c6a5ceead5..357ca974541ce 100644 --- a/ext/standard/tests/misc/time_sleep_until_basic.phpt +++ b/ext/standard/tests/misc/time_sleep_until_basic.phpt @@ -11,11 +11,29 @@ Michele Orselli mo@ideato.it #PHPTestFest Cesena Italia on 2009-06-20 --FILE-- = (int)$time ? $tmp : $tmp + .05; + $tolerance = 0.05; +} elseif (stripos(PHP_OS, 'DARWIN') === 0) { + // macOS: time_sleep_until() may wake up slightly early for unknown reasons. Allow a larger tolerance. + $tolerance = 0.004; +} else { + // Default tolerance + $tolerance = 0.002; } -// Add some tolerance for early wake on macos. Reason unknown. -if ($now + 0.002 >= $sleepUntil) { - echo "Success\n"; +if ($currentTime >= $sleepUntil && isWithinTolerance($currentTime, $sleepUntil, $tolerance)) { + echo "Success" . PHP_EOL; } else { - echo "Sleep until (before truncation): ", $time, "\n"; - echo "Sleep until: ", $sleepUntil, "\n"; - echo "Now: ", $now, "\n"; + echo "Sleep until (before truncation): {$targetTime}" . PHP_EOL; + echo "Sleep until: {$sleepUntil}" . PHP_EOL; + echo "Now: {$currentTime}" . PHP_EOL; } - ?> --EXPECT-- bool(true) From 59b1b3eb6ab040497d32480ec92caa00254e497c Mon Sep 17 00:00:00 2001 From: divinity76 Date: Tue, 11 Mar 2025 11:30:52 +0100 Subject: [PATCH 2/5] forgot MacOS early-wakeup --- ext/standard/tests/misc/time_sleep_until_basic.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/standard/tests/misc/time_sleep_until_basic.phpt b/ext/standard/tests/misc/time_sleep_until_basic.phpt index 357ca974541ce..80116da77b7b5 100644 --- a/ext/standard/tests/misc/time_sleep_until_basic.phpt +++ b/ext/standard/tests/misc/time_sleep_until_basic.phpt @@ -51,7 +51,7 @@ if (stripos(PHP_OS, 'WIN') === 0) { $tolerance = 0.002; } -if ($currentTime >= $sleepUntil && isWithinTolerance($currentTime, $sleepUntil, $tolerance)) { +if (isWithinTolerance($currentTime, $sleepUntil, $tolerance)) { echo "Success" . PHP_EOL; } else { echo "Sleep until (before truncation): {$targetTime}" . PHP_EOL; From 43ddd467069bf36f7741d37e48334ebfc10f651a Mon Sep 17 00:00:00 2001 From: divinity76 Date: Tue, 11 Mar 2025 11:52:30 +0100 Subject: [PATCH 3/5] CI larger tolerance --- ext/standard/tests/misc/time_sleep_until_basic.phpt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/standard/tests/misc/time_sleep_until_basic.phpt b/ext/standard/tests/misc/time_sleep_until_basic.phpt index 80116da77b7b5..a8c4a87f976e0 100644 --- a/ext/standard/tests/misc/time_sleep_until_basic.phpt +++ b/ext/standard/tests/misc/time_sleep_until_basic.phpt @@ -45,10 +45,10 @@ if (stripos(PHP_OS, 'WIN') === 0) { $tolerance = 0.05; } elseif (stripos(PHP_OS, 'DARWIN') === 0) { // macOS: time_sleep_until() may wake up slightly early for unknown reasons. Allow a larger tolerance. - $tolerance = 0.004; + $tolerance = 0.005; } else { // Default tolerance - $tolerance = 0.002; + $tolerance = 0.004; } if (isWithinTolerance($currentTime, $sleepUntil, $tolerance)) { From 3e77d3bb33a21c2d0df89307bb0fe4d66343e24f Mon Sep 17 00:00:00 2001 From: divinity76 Date: Fri, 14 Mar 2025 09:17:40 +0100 Subject: [PATCH 4/5] test ci --- .../tests/misc/time_sleep_until_basic.phpt | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/ext/standard/tests/misc/time_sleep_until_basic.phpt b/ext/standard/tests/misc/time_sleep_until_basic.phpt index a8c4a87f976e0..5dfb4dbe5279d 100644 --- a/ext/standard/tests/misc/time_sleep_until_basic.phpt +++ b/ext/standard/tests/misc/time_sleep_until_basic.phpt @@ -20,19 +20,19 @@ Michele Orselli mo@ideato.it * * @return bool True if the absolute difference is less than or equal to the tolerance, false otherwise. */ -function isWithinTolerance(float $val, float $target, float $tolerance): bool { +function isWithinTolerance(float $val, float $target, float $tolerance): bool +{ return abs($val - $target) <= $tolerance; } +$startTime = microtime(true); // Calculate the target sleep time (2 seconds from now) -$targetTime = microtime(true) + 2; -$sleepUntil = $targetTime; - -// Sleep until the target time and output the result of time_sleep_until() -var_dump(time_sleep_until($sleepUntil)); - +$targetTime = $startTime + 2; +// Sleep until the target time +$dump = time_sleep_until($targetTime); // Capture the current time immediately after sleep -$currentTime = microtime(true); +$timeAfterSleep = microtime(true); +var_dump($dump); if (stripos(PHP_OS, 'WIN') === 0) { // on windows, time_sleep_until has millisecond accuracy while microtime() is accurate // to 10th of a second. this means there can be up to a .9 millisecond difference @@ -45,18 +45,24 @@ if (stripos(PHP_OS, 'WIN') === 0) { $tolerance = 0.05; } elseif (stripos(PHP_OS, 'DARWIN') === 0) { // macOS: time_sleep_until() may wake up slightly early for unknown reasons. Allow a larger tolerance. - $tolerance = 0.005; + $tolerance = 0.02; } else { // Default tolerance - $tolerance = 0.004; + $tolerance = 0.01; } -if (isWithinTolerance($currentTime, $sleepUntil, $tolerance)) { +if (1 && isWithinTolerance($timeAfterSleep, $targetTime, $tolerance)) { echo "Success" . PHP_EOL; } else { - echo "Sleep until (before truncation): {$targetTime}" . PHP_EOL; - echo "Sleep until: {$sleepUntil}" . PHP_EOL; - echo "Now: {$currentTime}" . PHP_EOL; + echo "Failure" . PHP_EOL; + var_dump([ + "startTime" => $startTime, + "targetTime" => $targetTime, + "timeAfterSleep" => $timeAfterSleep, + "diff" => $timeAfterSleep - $targetTime, + "tolerance" => $tolerance, + "distanceFromTarget" => abs($timeAfterSleep - $targetTime), + ]); } ?> --EXPECT-- From 4712a299cdfa64c449823f3ed8d1132f849a98e3 Mon Sep 17 00:00:00 2001 From: divinity76 Date: Fri, 14 Mar 2025 09:56:59 +0100 Subject: [PATCH 5/5] CI MacOS and FreeBSD are slow --- ext/standard/tests/misc/time_sleep_until_basic.phpt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ext/standard/tests/misc/time_sleep_until_basic.phpt b/ext/standard/tests/misc/time_sleep_until_basic.phpt index 5dfb4dbe5279d..b02a96f7d1a31 100644 --- a/ext/standard/tests/misc/time_sleep_until_basic.phpt +++ b/ext/standard/tests/misc/time_sleep_until_basic.phpt @@ -44,9 +44,13 @@ if (stripos(PHP_OS, 'WIN') === 0) { // millisecond(.5 rounded up is 1 millisecond) $tolerance = 0.05; } elseif (stripos(PHP_OS, 'DARWIN') === 0) { - // macOS: time_sleep_until() may wake up slightly early for unknown reasons. Allow a larger tolerance. - $tolerance = 0.02; -} else { + // macOS: time_sleep_until() may wake up slightly early for unknown reasons. + // MacOS Github CI late by 106ms has been observed. + $tolerance = 0.2; +} elseif(PHP_OS === "FreeBSD"){ + // FreeBSD Github CI late by 74ms has been observed... + $tolerance = 0.2; +}else { // Default tolerance $tolerance = 0.01; }