Skip to content

Commit e34d7d1

Browse files
committed
#53: Optimize async socket timeout tests for faster execution
- Reduce UDP timeout from 2s to 0.2s in test 030 - Reduce TCP timeout from 1s to 0.2s in test 031 - Remove timing measurements and elapsed time checks - Simplify timeout validation to only check timed_out flag - Reduce client delay from 2s to 0.5s in TCP tes
1 parent 508f111 commit e34d7d1

File tree

2 files changed

+119
-10
lines changed

2 files changed

+119
-10
lines changed

tests/stream/030-udp_timeout_operations.phpt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,18 @@ $server = spawn(function() use (&$server_address, &$output) {
2626
$server_address = "udp://$address";
2727
$output['3'] = "Server: listening on $server_address";
2828

29-
// Set timeout to 2 seconds
30-
stream_set_timeout($socket, 2, 0);
31-
$output['4'] = "Server: set timeout to 2 seconds";
29+
// Set timeout to 0.2 seconds
30+
stream_set_timeout($socket, 0, 200000);
31+
$output['4'] = "Server: set timeout to 0.2 seconds";
3232

3333
// Try to receive data (should timeout)
3434
$output['5'] = "Server: waiting for UDP data (should timeout)";
35-
$start_time = microtime(true);
3635
$data = stream_socket_recvfrom($socket, 1024, 0, $peer);
37-
$end_time = microtime(true);
3836

3937
$meta = stream_get_meta_data($socket);
40-
$elapsed = round($end_time - $start_time, 1);
4138

4239
if ($meta['timed_out']) {
43-
$output['6'] = "Server: operation timed out after $elapsed seconds";
40+
$output['6'] = "Server: operation timed out";
4441
} else {
4542
$output['6'] = "Server: received data (unexpected): '$data'";
4643
}
@@ -62,11 +59,10 @@ foreach ($output as $line) {
6259

6360
?>
6461
--EXPECTF--
65-
Warning: stream_socket_recvfrom(): Socket operation timed out after 2.000000 seconds in %s on line %d
6662
Start UDP timeout operations test
6763
Server: creating UDP socket
6864
Server: listening on udp://127.0.0.1:%d
69-
Server: set timeout to 2 seconds
65+
Server: set timeout to 0.2 seconds
7066
Server: waiting for UDP data (should timeout)
71-
Server: operation timed out after %s seconds
67+
Server: operation timed out
7268
End UDP timeout operations test
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)