1--TEST--
2time_sleep_until() function - basic test for time_sleep_until()
3--SKIPIF--
4<?php
5if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
6if (!function_exists("time_sleep_until")) die('skip time_sleep_until() not available');
7?>
8--CREDITS--
9Manuel Baldassarri mb@ideato.it
10Michele Orselli mo@ideato.it
11#PHPTestFest Cesena Italia on 2009-06-20
12--FILE--
13<?php
14$time = microtime(true) + 2;
15$sleepUntil = (int) $time;
16var_dump(time_sleep_until($sleepUntil));
17$now = microtime(true);
18if (substr(PHP_OS, 0, 3) == 'WIN') {
19    // on windows, time_sleep_until has millisecond accuracy while microtime() is accurate
20    // to 10th of a second. this means there can be up to a .9 millisecond difference
21    // which will fail this test. this test randomly fails on Windows and this is the cause.
22    //
23    // fix: round to nearest millisecond
24    // passes for up to .5 milliseconds less, fails for more than .5 milliseconds
25    // should be fine since time_sleep_until() on Windows is accurate to the
26    // 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;
30}
31
32// Add some tolerance for early wake on macos. Reason unknown.
33if ($now + 0.002 >= $sleepUntil) {
34    echo "Success\n";
35} else {
36    echo "Sleep until (before truncation): ", $time, "\n";
37    echo "Sleep until: ", $sleepUntil, "\n";
38    echo "Now: ", $now, "\n";
39}
40
41?>
42--EXPECT--
43bool(true)
44Success
45