1+ --TEST--
2+ TCP timeout operations with fread/fwrite in async context
3+ --FILE--
4+ <?php
5+
6+ use function Async \spawn ;
7+ use function Async \awaitAll ;
8+ use function Async \delay ;
9+
10+ $ output = [];
11+
12+ $ output ['1 ' ] = "Start TCP timeout operations test " ;
13+
14+ $ server_address = null ;
15+
16+ // Server coroutine that tests timeout
17+ $ server = spawn (function () use (&$ server_address , &$ output ) {
18+ $ output ['2 ' ] = "Server: creating TCP socket " ;
19+ $ socket = stream_socket_server ("tcp://127.0.0.1:0 " , $ errno , $ errstr );
20+ if (!$ socket ) {
21+ $ output ['2a ' ] = "Server: failed to create socket: $ errstr " ;
22+ return ;
23+ }
24+
25+ $ address = stream_socket_get_name ($ socket , false );
26+ $ server_address = "tcp:// $ address " ;
27+ $ output ['3 ' ] = "Server: listening on $ server_address " ;
28+
29+ // Accept client connection
30+ $ output ['4 ' ] = "Server: waiting for client connection " ;
31+ $ client = stream_socket_accept ($ socket );
32+ if (!$ client ) {
33+ $ output ['4a ' ] = "Server: failed to accept client " ;
34+ fclose ($ socket );
35+ return ;
36+ }
37+
38+ $ output ['5 ' ] = "Server: client connected " ;
39+
40+ // Set timeout to 0.2 seconds on client socket
41+ stream_set_timeout ($ client , 0 , 200000 );
42+ $ output ['6 ' ] = "Server: set read timeout to 0.2 seconds " ;
43+
44+ // Try to read data (should timeout)
45+ $ output ['7 ' ] = "Server: reading from client (should timeout) " ;
46+ $ data = fread ($ client , 1024 );
47+
48+ $ meta = stream_get_meta_data ($ client );
49+
50+ if ($ meta ['timed_out ' ]) {
51+ $ output ['8 ' ] = "Server: read operation timed out " ;
52+ } else {
53+ $ output ['8 ' ] = "Server: received data (unexpected): ' $ data' " ;
54+ }
55+
56+ fclose ($ client );
57+ fclose ($ socket );
58+ return $ server_address ;
59+ });
60+
61+ // Client coroutine that connects but doesn't send data immediately
62+ $ client = spawn (function () use (&$ server_address , &$ output ) {
63+ // Wait for server to start
64+ for ($ attempts = 0 ; $ attempts < 3 ; $ attempts ++) {
65+ delay (10 );
66+ if ($ server_address ) {
67+ break ;
68+ }
69+ }
70+
71+ if (!$ server_address ) {
72+ throw new Exception ("Client: failed to get server address after 10 attempts " );
73+ }
74+
75+ $ output ['4b ' ] = "Client: connecting to $ server_address " ;
76+ $ socket = stream_socket_client ($ server_address , $ errno , $ errstr );
77+ if (!$ socket ) {
78+ $ output ['4c ' ] = "Client: failed to connect: $ errstr " ;
79+ return ;
80+ }
81+
82+ $ output ['5b ' ] = "Client: connected to server " ;
83+
84+ // Wait for server to timeout (don't send data)
85+ delay (500 );
86+
87+ fclose ($ socket );
88+ });
89+
90+ awaitAll ([$ server , $ client ]);
91+ $ output ['z ' ] = "End TCP timeout operations test " ;
92+
93+ // Sort output by keys to ensure deterministic test results
94+ ksort ($ output );
95+
96+ // Output sorted results
97+ foreach ($ output as $ line ) {
98+ echo $ line . "\n" ;
99+ }
100+
101+ ?>
102+ --EXPECTF--
103+ Start TCP timeout operations test
104+ Server: creating TCP socket
105+ Server: listening on tcp://127.0.0.1:%d
106+ Server: waiting for client connection
107+ Client: connecting to tcp://127.0.0.1:%d
108+ Server: client connected
109+ Client: connected to server
110+ Server: set read timeout to 0.2 seconds
111+ Server: reading from client (should timeout)
112+ Server: read operation timed out
113+ End TCP timeout operations test
0 commit comments