Skip to content

Commit 3db142e

Browse files
committed
+ sleep functions
1 parent 5ea7c6d commit 3db142e

7 files changed

+337
-0
lines changed

tests/sleep/001-sleep_basic.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
sleep() async basic functionality
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("sleep")) echo "skip sleep() is not available";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
use function Async\spawn;
11+
12+
echo "Main thread start\n";
13+
14+
$start_time = microtime(true);
15+
16+
spawn(function () use ($start_time) {
17+
echo "Starting async sleep test\n";
18+
19+
$result = sleep(1);
20+
21+
$elapsed = microtime(true) - $start_time;
22+
echo "Sleep returned: $result\n";
23+
echo "Elapsed time >= 1s: " . ($elapsed >= 1.0 ? "yes" : "no") . "\n";
24+
echo "Sleep test completed\n";
25+
});
26+
27+
spawn(function() {
28+
echo "Other async task executing\n";
29+
});
30+
31+
echo "Main thread end\n";
32+
?>
33+
--EXPECT--
34+
Main thread start
35+
Main thread end
36+
Starting async sleep test
37+
Other async task executing
38+
Sleep returned: 0
39+
Elapsed time >= 1s: yes
40+
Sleep test completed

tests/sleep/002-usleep_basic.phpt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
usleep() async basic functionality
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("usleep")) echo "skip usleep() is not available";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
use function Async\spawn;
11+
12+
echo "Main thread start\n";
13+
14+
$start_time = microtime(true);
15+
16+
spawn(function () use ($start_time) {
17+
echo "Starting async usleep test\n";
18+
19+
usleep(500000); // 0.5 seconds
20+
21+
$elapsed = microtime(true) - $start_time;
22+
echo "Elapsed time >= 0.5s: " . ($elapsed >= 0.5 ? "yes" : "no") . "\n";
23+
echo "Usleep test completed\n";
24+
});
25+
26+
spawn(function() {
27+
echo "Other async task executing\n";
28+
});
29+
30+
echo "Main thread end\n";
31+
?>
32+
--EXPECT--
33+
Main thread start
34+
Main thread end
35+
Starting async usleep test
36+
Other async task executing
37+
Elapsed time >= 0.5s: yes
38+
Usleep test completed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
time_nanosleep() async basic functionality
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("time_nanosleep")) echo "skip time_nanosleep() is not available";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
use function Async\spawn;
11+
12+
echo "Main thread start\n";
13+
14+
$start_time = microtime(true);
15+
16+
spawn(function () use ($start_time) {
17+
echo "Starting async time_nanosleep test\n";
18+
19+
$result = time_nanosleep(0, 300000000); // 0.3 seconds
20+
21+
$elapsed = microtime(true) - $start_time;
22+
echo "time_nanosleep returned: " . ($result === true ? "true" : "false") . "\n";
23+
echo "Elapsed time >= 0.3s: " . ($elapsed >= 0.3 ? "yes" : "no") . "\n";
24+
echo "time_nanosleep test completed\n";
25+
});
26+
27+
spawn(function() {
28+
echo "Other async task executing\n";
29+
});
30+
31+
echo "Main thread end\n";
32+
?>
33+
--EXPECT--
34+
Main thread start
35+
Main thread end
36+
Starting async time_nanosleep test
37+
Other async task executing
38+
time_nanosleep returned: true
39+
Elapsed time >= 0.3s: yes
40+
time_nanosleep test completed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
time_sleep_until() async basic functionality
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("time_sleep_until")) echo "skip time_sleep_until() is not available";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
use function Async\spawn;
11+
12+
echo "Main thread start\n";
13+
14+
$start_time = microtime(true);
15+
16+
spawn(function () use ($start_time) {
17+
echo "Starting async time_sleep_until test\n";
18+
19+
$target_time = microtime(true) + 0.4; // Sleep for 0.4 seconds
20+
$result = time_sleep_until($target_time);
21+
22+
$elapsed = microtime(true) - $start_time;
23+
echo "time_sleep_until returned: " . ($result === true ? "true" : "false") . "\n";
24+
echo "Elapsed time >= 0.4s: " . ($elapsed >= 0.4 ? "yes" : "no") . "\n";
25+
echo "time_sleep_until test completed\n";
26+
});
27+
28+
spawn(function() {
29+
echo "Other async task executing\n";
30+
});
31+
32+
echo "Main thread end\n";
33+
?>
34+
--EXPECT--
35+
Main thread start
36+
Main thread end
37+
Starting async time_sleep_until test
38+
Other async task executing
39+
time_sleep_until returned: true
40+
Elapsed time >= 0.4s: yes
41+
time_sleep_until test completed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
--TEST--
2+
Multiple concurrent async sleep operations
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("sleep") || !function_exists("usleep")) {
6+
echo "skip sleep functions are not available";
7+
}
8+
?>
9+
--FILE--
10+
<?php
11+
12+
use function Async\spawn;
13+
14+
$results = [];
15+
$start_time = microtime(true);
16+
17+
echo "Main start\n";
18+
19+
spawn(function () use (&$results, $start_time) {
20+
echo "Sleep 1 starting (1s)\n";
21+
sleep(1);
22+
$elapsed = microtime(true) - $start_time;
23+
$results[] = "Sleep 1: " . round($elapsed, 1) . "s";
24+
echo "Sleep 1 completed\n";
25+
});
26+
27+
spawn(function () use (&$results, $start_time) {
28+
echo "Sleep 2 starting (0.5s)\n";
29+
usleep(500000);
30+
$elapsed = microtime(true) - $start_time;
31+
$results[] = "Sleep 2: " . round($elapsed, 1) . "s";
32+
echo "Sleep 2 completed\n";
33+
});
34+
35+
spawn(function () use (&$results, $start_time) {
36+
echo "Sleep 3 starting (0.3s)\n";
37+
time_nanosleep(0, 300000000);
38+
$elapsed = microtime(true) - $start_time;
39+
$results[] = "Sleep 3: " . round($elapsed, 1) . "s";
40+
echo "Sleep 3 completed\n";
41+
});
42+
43+
spawn(function() {
44+
echo "Background task running\n";
45+
});
46+
47+
echo "Main end\n";
48+
?>
49+
--EXPECT--
50+
Main start
51+
Main end
52+
Sleep 1 starting (1s)
53+
Sleep 2 starting (0.5s)
54+
Sleep 3 starting (0.3s)
55+
Background task running
56+
Sleep 3 completed
57+
Sleep 2 completed
58+
Sleep 1 completed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
Sleep functions precision and timing accuracy
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("sleep") || !function_exists("usleep") || !function_exists("time_nanosleep")) {
6+
echo "skip sleep functions are not available";
7+
}
8+
?>
9+
--FILE--
10+
<?php
11+
12+
use function Async\spawn;
13+
14+
echo "Starting precision test\n";
15+
16+
spawn(function () {
17+
echo "Testing short sleep precision\n";
18+
19+
$start = microtime(true);
20+
usleep(50000); // 50ms
21+
$elapsed = microtime(true) - $start;
22+
23+
echo "usleep(50000) - Expected: ~0.05s, Actual: " . round($elapsed, 3) . "s\n";
24+
echo "Precision acceptable: " . (abs($elapsed - 0.05) < 0.01 ? "yes" : "no") . "\n";
25+
});
26+
27+
spawn(function () {
28+
echo "Testing nanosleep precision\n";
29+
30+
$start = microtime(true);
31+
time_nanosleep(0, 75000000); // 75ms
32+
$elapsed = microtime(true) - $start;
33+
34+
echo "time_nanosleep(0, 75000000) - Expected: ~0.075s, Actual: " . round($elapsed, 3) . "s\n";
35+
echo "Precision acceptable: " . (abs($elapsed - 0.075) < 0.01 ? "yes" : "no") . "\n";
36+
});
37+
38+
spawn(function() {
39+
echo "Background task running\n";
40+
});
41+
42+
echo "Precision test completed\n";
43+
?>
44+
--EXPECT--
45+
Starting precision test
46+
Precision test completed
47+
Testing short sleep precision
48+
Testing nanosleep precision
49+
Background task running
50+
usleep(50000) - Expected: ~0.05s, Actual: 0.050s
51+
Precision acceptable: yes
52+
time_nanosleep(0, 75000000) - Expected: ~0.075s, Actual: 0.075s
53+
Precision acceptable: yes

0 commit comments

Comments
 (0)