Skip to content

Commit b67dc84

Browse files
committed
Add test for stream_select with null timeout in coroutine context
1 parent 4344c20 commit b67dc84

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
--TEST--
2+
stream_select with null timeout (infinite wait with coroutine yield)
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/stream_helper.php';
7+
use function Async\spawn;
8+
use function Async\awaitAll;
9+
10+
echo "Testing stream_select with null timeout\n";
11+
12+
$sockets = create_socket_pair();
13+
if (!$sockets) {
14+
echo "Failed to create socket pair\n";
15+
exit(1);
16+
}
17+
18+
list($sock1, $sock2) = $sockets;
19+
20+
// Coroutine using stream_select with null timeout (infinite wait)
21+
$selector = spawn(function() use ($sock2) {
22+
echo "Selector: calling stream_select with null timeout\n";
23+
24+
$read = [$sock2];
25+
$write = null;
26+
$except = null;
27+
28+
$start_time = microtime(true);
29+
// This should YIELD the coroutine and allow writer to run
30+
$result = stream_select($read, $write, $except, null);
31+
$elapsed = round((microtime(true) - $start_time) * 1000, 2);
32+
33+
echo "Selector: stream_select returned after {$elapsed}ms\n";
34+
35+
if ($result > 0) {
36+
echo "Selector: data available\n";
37+
$data = fread($sock2, 1024);
38+
echo "Selector: read '$data'\n";
39+
} else {
40+
echo "Selector: ERROR - should have received data!\n";
41+
}
42+
43+
fclose($sock2);
44+
return "selector completed";
45+
});
46+
47+
// Writer coroutine - writes after a small delay
48+
$writer = spawn(function() use ($sock1) {
49+
// Small delay to ensure selector starts first
50+
\Async\delay(50);
51+
52+
echo "Writer: writing data\n";
53+
fwrite($sock1, "test data from writer");
54+
fflush($sock1);
55+
echo "Writer: data written\n";
56+
57+
fclose($sock1);
58+
return "writer completed";
59+
});
60+
61+
// Worker to demonstrate parallel execution
62+
$worker = spawn(function() {
63+
echo "Worker: executing during stream_select\n";
64+
\Async\delay(10);
65+
echo "Worker: finished\n";
66+
return "worker completed";
67+
});
68+
69+
list($results, $errors) = awaitAll([$selector, $writer, $worker]);
70+
71+
echo "Results:\n";
72+
foreach ($results as $i => $result) {
73+
echo " Coroutine $i: $result\n";
74+
}
75+
76+
echo "Test completed successfully\n";
77+
78+
?>
79+
--EXPECTF--
80+
Testing stream_select with null timeout
81+
Selector: calling stream_select with null timeout
82+
Worker: executing during stream_select
83+
Worker: finished
84+
Writer: writing data
85+
Writer: data written
86+
Selector: stream_select returned after %sms
87+
Selector: data available
88+
Selector: read 'test data from writer'
89+
Results:
90+
Coroutine 0: selector completed
91+
Coroutine 1: writer completed
92+
Coroutine 2: worker completed
93+
Test completed successfully

0 commit comments

Comments
 (0)