Skip to content

nitpick time_sleep_until_basic.phpt #18021

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
60 changes: 45 additions & 15 deletions ext/standard/tests/misc/time_sleep_until_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,29 @@ Michele Orselli mo@ideato.it
#PHPTestFest Cesena Italia on 2009-06-20
--FILE--
<?php
$time = microtime(true) + 2;
$sleepUntil = (int) $time;
var_dump(time_sleep_until($sleepUntil));
$now = microtime(true);
if (substr(PHP_OS, 0, 3) == 'WIN') {
/**
* Checks if a value is within a specified tolerance of a target value.
*
* @param float $val The value to test.
* @param float $target The target value.
* @param float $tolerance The allowed tolerance.
*
* @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
{
return abs($val - $target) <= $tolerance;
}

$startTime = microtime(true);
// Calculate the target sleep time (2 seconds from now)
$targetTime = $startTime + 2;
// Sleep until the target time
$dump = time_sleep_until($targetTime);
// Capture the current time immediately after sleep
$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
// which will fail this test. this test randomly fails on Windows and this is the cause.
Expand All @@ -24,20 +42,32 @@ if (substr(PHP_OS, 0, 3) == 'WIN') {
// passes for up to .5 milliseconds less, fails for more than .5 milliseconds
// should be fine since time_sleep_until() on Windows is accurate to the
// millisecond(.5 rounded up is 1 millisecond)
// In practice, on slower machines even that can fail, so giving yet 50ms or more.
$tmp = round($now, 3);
$now = $tmp >= (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.
// 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;
}

// Add some tolerance for early wake on macos. Reason unknown.
if ($now + 0.002 >= $sleepUntil) {
echo "Success\n";
if (1 && isWithinTolerance($timeAfterSleep, $targetTime, $tolerance)) {
echo "Success" . PHP_EOL;
} else {
echo "Sleep until (before truncation): ", $time, "\n";
echo "Sleep until: ", $sleepUntil, "\n";
echo "Now: ", $now, "\n";
echo "Failure" . PHP_EOL;
var_dump([
"startTime" => $startTime,
"targetTime" => $targetTime,
"timeAfterSleep" => $timeAfterSleep,
"diff" => $timeAfterSleep - $targetTime,
"tolerance" => $tolerance,
"distanceFromTarget" => abs($timeAfterSleep - $targetTime),
]);
}

?>
--EXPECT--
bool(true)
Expand Down
Loading