1+ --TEST--
2+ All sleep functions async comparison: sleep(), usleep(), time_nanosleep(), time_sleep_until()
3+ --SKIPIF--
4+ <?php
5+ if (!function_exists ("sleep " ) || !function_exists ("usleep " ) ||
6+ !function_exists ("time_nanosleep " ) || !function_exists ("time_sleep_until " )) {
7+ echo "skip one or more sleep functions are not available " ;
8+ }
9+ ?>
10+ --FILE--
11+ <?php
12+
13+ use function Async \spawn ;
14+
15+ echo "Starting full sleep functions test \n" ;
16+
17+ spawn (function () {
18+ echo "Testing sleep() async \n" ;
19+ $ start = microtime (true );
20+ $ result = sleep (1 );
21+ $ elapsed = microtime (true ) - $ start ;
22+ echo "sleep(1) result: $ result, elapsed: " . round ($ elapsed , 1 ) . "s \n" ;
23+ });
24+
25+ spawn (function () {
26+ echo "Testing usleep() async \n" ;
27+ $ start = microtime (true );
28+ usleep (200000 ); // 0.2s
29+ $ elapsed = microtime (true ) - $ start ;
30+ echo "usleep(200000) elapsed: " . round ($ elapsed , 1 ) . "s \n" ;
31+ });
32+
33+ spawn (function () {
34+ echo "Testing time_nanosleep() async \n" ;
35+ $ start = microtime (true );
36+ $ result = time_nanosleep (0 , 100000000 ); // 0.1s
37+ $ elapsed = microtime (true ) - $ start ;
38+ echo "time_nanosleep(0, 100000000) result: " . ($ result ? "true " : "false " ) . ", elapsed: " . round ($ elapsed , 1 ) . "s \n" ;
39+ });
40+
41+ spawn (function () {
42+ echo "Testing time_sleep_until() async \n" ;
43+ $ start = microtime (true );
44+ $ target = microtime (true ) + 0.15 ;
45+ $ result = time_sleep_until ($ target );
46+ $ elapsed = microtime (true ) - $ start ;
47+ echo "time_sleep_until() result: " . ($ result ? "true " : "false " ) . ", elapsed: " . round ($ elapsed , 1 ) . "s \n" ;
48+ });
49+
50+ spawn (function () {
51+ echo "Background task running \n" ;
52+ });
53+
54+ echo "Full sleep functions test completed \n" ;
55+ ?>
56+ --EXPECT--
57+ Starting full sleep functions test
58+ Full sleep functions test completed
59+ Testing sleep() async
60+ Testing usleep() async
61+ Testing time_nanosleep() async
62+ Testing time_sleep_until() async
63+ Background task running
64+ time_nanosleep(0, 100000000) result: true, elapsed: 0.1s
65+ time_sleep_until() result: true, elapsed: 0.2s
66+ usleep(200000) elapsed: 0.2s
67+ sleep(1) result: 0, elapsed: 1.0s
0 commit comments