Skip to content

Commit ee1ecab

Browse files
committed
* fix test stability
1 parent 3abceb4 commit ee1ecab

10 files changed

+229
-128
lines changed

tests/dns/004-concurrent_dns_lookups.phpt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,38 @@ for ($i = 0; $i < 5; $i++) {
1515
$coroutines[] = spawn(function() use ($i) {
1616
$hostname = $i % 2 == 0 ? 'localhost' : '127.0.0.1';
1717
$ip = gethostbyname($hostname);
18-
echo "Coroutine $i: $hostname -> $ip\n";
19-
return $ip;
18+
return ['coroutine' => $i, 'input' => $hostname, 'output' => $ip];
2019
});
2120
}
2221

2322
// Test concurrent gethostbyaddr
2423
for ($i = 5; $i < 8; $i++) {
2524
$coroutines[] = spawn(function() use ($i) {
2625
$hostname = gethostbyaddr('127.0.0.1');
27-
echo "Coroutine $i: 127.0.0.1 -> $hostname\n";
28-
return $hostname;
26+
return ['coroutine' => $i, 'input' => '127.0.0.1', 'output' => $hostname];
2927
});
3028
}
3129

3230
[$results, $exceptions] = awaitAll($coroutines);
31+
32+
// Print results in deterministic order
33+
foreach ($results as $result) {
34+
echo "Coroutine {$result['coroutine']}: {$result['input']} -> {$result['output']}\n";
35+
}
36+
3337
echo "All DNS lookups completed\n";
3438
echo "Total results: " . count($results) . "\n";
3539

3640
?>
3741
--EXPECTF--
3842
Starting concurrent DNS lookups
39-
Coroutine %d: %s -> %s
40-
Coroutine %d: %s -> %s
41-
Coroutine %d: %s -> %s
42-
Coroutine %d: %s -> %s
43-
Coroutine %d: %s -> %s
44-
Coroutine %d: 127.0.0.1 -> %s
45-
Coroutine %d: 127.0.0.1 -> %s
46-
Coroutine %d: 127.0.0.1 -> %s
43+
Coroutine 0: localhost -> %s
44+
Coroutine 1: 127.0.0.1 -> %s
45+
Coroutine 2: localhost -> %s
46+
Coroutine 3: 127.0.0.1 -> %s
47+
Coroutine 4: localhost -> %s
48+
Coroutine 5: 127.0.0.1 -> %s
49+
Coroutine 6: 127.0.0.1 -> %s
50+
Coroutine 7: 127.0.0.1 -> %s
4751
All DNS lookups completed
4852
Total results: 8

tests/socket_ext/001-socket_read_async.phpt

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,77 +15,87 @@ use function Async\delay;
1515

1616
echo "Start\n";
1717

18+
// Array to collect output from spawn functions
19+
$output = [];
20+
1821
// Shared variable for server port
1922
$port = null;
2023

2124
// Server coroutine
22-
$server = spawn(function() use (&$port) {
23-
echo "Server: creating socket\n";
25+
$server = spawn(function() use (&$port, &$output) {
26+
$output[] = "Server: creating socket";
2427
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
2528
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
2629
socket_bind($socket, '127.0.0.1', 0);
2730
socket_listen($socket);
2831

2932
$addr = '';
3033
socket_getsockname($socket, $addr, $port);
31-
echo "Server: listening on port $port\n";
34+
$output[] = "Server: listening on port $port";
3235

33-
echo "Server: waiting for connection\n";
36+
$output[] = "Server: waiting for connection";
3437
$client = socket_accept($socket);
35-
echo "Server: client connected\n";
38+
$output[] = "Server: client connected";
3639

3740
$data = socket_read($client, 1024);
38-
echo "Server: received '$data'\n";
41+
$output[] = "Server: received '$data'";
3942

4043
socket_write($client, "Hello from server");
41-
echo "Server: response sent\n";
44+
$output[] = "Server: response sent";
4245

4346
socket_close($client);
4447
socket_close($socket);
4548
});
4649

4750
// Client coroutine
48-
$client = spawn(function() use (&$port) {
51+
$client = spawn(function() use (&$port, &$output) {
4952
// Wait for the server to set the port
5053
while ($port === null) {
5154
delay(1);
5255
}
5356

54-
echo "Client: connecting\n";
57+
$output[] = "Client: connecting";
5558
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
5659
if (!socket_connect($socket, '127.0.0.1', $port)) {
57-
echo "Client: failed to connect\n";
60+
$output[] = "Client: failed to connect";
5861
return;
5962
}
6063

6164
socket_write($socket, "Hello from client");
62-
echo "Client: sent request\n";
65+
$output[] = "Client: sent request";
6366

6467
$response = socket_read($socket, 1024);
65-
echo "Client: received '$response'\n";
68+
$output[] = "Client: received '$response'";
6669

6770
socket_close($socket);
6871
});
6972

7073
// Worker coroutine for parallel execution
71-
$worker = spawn(function() {
72-
echo "Worker: doing work while server waits\n";
74+
$worker = spawn(function() use (&$output) {
75+
$output[] = "Worker: doing work while server waits";
7376
});
7477

7578
awaitAll([$server, $client, $worker]);
79+
80+
// Sort and output results
81+
sort($output);
82+
foreach ($output as $line) {
83+
echo $line . "\n";
84+
}
85+
7686
echo "End\n";
7787

7888
?>
7989
--EXPECTF--
8090
Start
81-
Server: creating socket
82-
Server: listening on port %d
83-
Server: waiting for connection
8491
Client: connecting
85-
Worker: doing work while server waits
86-
Server: client connected
92+
Client: received 'Hello from server'
8793
Client: sent request
94+
Server: client connected
95+
Server: creating socket
96+
Server: listening on port %d
8897
Server: received 'Hello from client'
8998
Server: response sent
90-
Client: received 'Hello from server'
99+
Server: waiting for connection
100+
Worker: doing work while server waits
91101
End

tests/socket_ext/004-socket_connect_async.phpt

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ use function Async\delay;
1616
echo "Start\n";
1717

1818
$port = null;
19+
$output = [];
1920

2021
// Server coroutine
21-
$server = spawn(function() use (&$port) {
22+
$server = spawn(function() use (&$port, &$output) {
2223
echo "Server: creating socket\n";
2324
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
2425
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
@@ -31,10 +32,10 @@ $server = spawn(function() use (&$port) {
3132

3233
echo "Server: accepting connections\n";
3334
$client1 = socket_accept($socket);
34-
echo "Server: client1 connected\n";
35+
$output[] = "Server: client1 connected";
3536

3637
$client2 = socket_accept($socket);
37-
echo "Server: client2 connected\n";
38+
$output[] = "Server: client2 connected";
3839

3940
socket_write($client1, "Hello client1");
4041
socket_write($client2, "Hello client2");
@@ -45,45 +46,52 @@ $server = spawn(function() use (&$port) {
4546
});
4647

4748
// Client coroutines
48-
$client1 = spawn(function() use (&$port) {
49+
$client1 = spawn(function() use (&$port, &$output) {
4950
while ($port === null) {
5051
delay(1);
5152
}
5253

53-
echo "Client1: connecting\n";
54+
$output[] = "Client1: connecting";
5455
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
5556

5657
if (socket_connect($socket, '127.0.0.1', $port)) {
57-
echo "Client1: connected successfully\n";
58+
$output[] = "Client1: connected successfully";
5859
$data = socket_read($socket, 1024);
59-
echo "Client1: received '$data'\n";
60+
$output[] = "Client1: received '$data'";
6061
} else {
61-
echo "Client1: connection failed\n";
62+
$output[] = "Client1: connection failed";
6263
}
6364

6465
socket_close($socket);
6566
});
6667

67-
$client2 = spawn(function() use (&$port) {
68+
$client2 = spawn(function() use (&$port, &$output) {
6869
while ($port === null) {
6970
delay(1);
7071
}
7172

72-
echo "Client2: connecting\n";
73+
$output[] = "Client2: connecting";
7374
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
7475

7576
if (socket_connect($socket, '127.0.0.1', $port)) {
76-
echo "Client2: connected successfully\n";
77+
$output[] = "Client2: connected successfully";
7778
$data = socket_read($socket, 1024);
78-
echo "Client2: received '$data'\n";
79+
$output[] = "Client2: received '$data'";
7980
} else {
80-
echo "Client2: connection failed\n";
81+
$output[] = "Client2: connection failed";
8182
}
8283

8384
socket_close($socket);
8485
});
8586

8687
awaitAll([$server, $client1, $client2]);
88+
89+
// Sort output for deterministic results
90+
sort($output);
91+
foreach ($output as $message) {
92+
echo $message . "\n";
93+
}
94+
8795
echo "End\n";
8896

8997
?>
@@ -92,8 +100,12 @@ Start
92100
Server: creating socket
93101
Server: listening on port %d
94102
Server: accepting connections
103+
Client1: connected successfully
95104
Client1: connecting
105+
Client1: received 'Hello client1'
106+
Client2: connected successfully
96107
Client2: connecting
108+
Client2: received 'Hello client2'
97109
Server: client1 connected
98-
Client1: connected successfully
99-
%a
110+
Server: client2 connected
111+
End

tests/socket_ext/005-socket_accept_multiple.phpt

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,30 @@ use function Async\delay;
1515

1616
echo "Start\n";
1717

18+
// Array to collect output from spawn functions
19+
$output = [];
20+
1821
$port = null;
1922

2023
// Server coroutine
21-
$server = spawn(function() use (&$port) {
22-
echo "Server: creating socket\n";
24+
$server = spawn(function() use (&$port, &$output) {
25+
$output[] = "Server: creating socket";
2326
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
2427
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
2528
socket_bind($socket, '127.0.0.1', 0);
2629
socket_listen($socket, 5);
2730

2831
$addr = '';
2932
socket_getsockname($socket, $addr, $port);
30-
echo "Server: listening on port $port\n";
33+
$output[] = "Server: listening on port $port";
3134

3235
$clients = [];
3336

3437
// Accept 3 clients
3538
for ($i = 1; $i <= 3; $i++) {
36-
echo "Server: waiting for client $i\n";
39+
$output[] = "Server: waiting for client $i";
3740
$client = socket_accept($socket);
38-
echo "Server: client $i connected\n";
41+
$output[] = "Server: client $i connected";
3942
$clients[] = $client;
4043
}
4144

@@ -52,35 +55,55 @@ $server = spawn(function() use (&$port) {
5255
// Multiple client coroutines
5356
$clients = [];
5457
for ($i = 1; $i <= 3; $i++) {
55-
$clients[] = spawn(function() use (&$port, $i) {
58+
$clients[] = spawn(function() use (&$port, $i, &$output) {
5659
while ($port === null) {
5760
delay(1);
5861
}
5962

6063
// Small delay to stagger connections
6164
delay($i);
6265

63-
echo "Client$i: connecting\n";
66+
$output[] = "Client$i: connecting";
6467
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
6568

6669
if (socket_connect($socket, '127.0.0.1', $port)) {
67-
echo "Client$i: connected\n";
70+
$output[] = "Client$i: connected";
6871
$data = socket_read($socket, 1024);
69-
echo "Client$i: received '$data'\n";
72+
$output[] = "Client$i: received '$data'";
7073
}
7174

7275
socket_close($socket);
7376
});
7477
}
7578

7679
awaitAll(array_merge([$server], $clients));
80+
81+
// Sort and output results
82+
sort($output);
83+
foreach ($output as $line) {
84+
echo $line . "\n";
85+
}
86+
7787
echo "End\n";
7888

7989
?>
8090
--EXPECTF--
8191
Start
92+
Client1: connected
93+
Client1: connecting
94+
Client1: received 'Response to client 1'
95+
Client2: connected
96+
Client2: connecting
97+
Client2: received 'Response to client 2'
98+
Client3: connected
99+
Client3: connecting
100+
Client3: received 'Response to client 3'
101+
Server: client 1 connected
102+
Server: client 2 connected
103+
Server: client 3 connected
82104
Server: creating socket
83105
Server: listening on port %d
84106
Server: waiting for client 1
85-
Client1: connecting
86-
%a
107+
Server: waiting for client 2
108+
Server: waiting for client 3
109+
End

0 commit comments

Comments
 (0)