Skip to content

Commit d911428

Browse files
committed
Fix race condition in async cURL error handling test
The test was spawning three async coroutines that executed in parallel, causing non-deterministic output order. This led to sporadic test failures when the actual output order differed from the expected order. Solution: - Functions now return arrays with numbered keys (1-11) instead of echoing - After await_all(), all outputs are merged and sorted by key (ksort) - Output is then printed in deterministic order This preserves the async nature of the test while ensuring stable output. The test still validates concurrent cURL operations with proper error handling.
1 parent 5436dd3 commit d911428

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

tests/curl/005-error_handling.phpt

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,62 +12,65 @@ use function Async\await_all;
1212
$server = async_test_server_start();
1313

1414
function test_connection_error() {
15-
echo "Testing connection error\n";
16-
15+
$output = [];
16+
$output[1] = "Testing connection error";
17+
1718
$ch = curl_init();
1819
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1:99991/nonexistent"); // Wrong port
1920
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2021
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
2122
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
22-
23+
2324
$response = curl_exec($ch);
2425
$error = curl_error($ch);
2526
$errno = curl_errno($ch);
26-
27-
28-
echo "Connection failed as expected\n";
29-
echo "Error present: " . (!empty($error) ? "yes" : "no") . "\n";
30-
echo "Error number: $errno\n";
31-
32-
return $response;
27+
28+
29+
$output[4] = "Connection failed as expected";
30+
$output[5] = "Error present: " . (!empty($error) ? "yes" : "no");
31+
$output[6] = "Error number: $errno";
32+
33+
return $output;
3334
}
3435

3536
function test_server_error($server) {
36-
echo "Testing server error\n";
37-
37+
$output = [];
38+
$output[2] = "Testing server error";
39+
3840
$ch = curl_init();
3941
curl_setopt($ch, CURLOPT_URL, "http://localhost:{$server->port}/error");
4042
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4143
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
42-
44+
4345
$response = curl_exec($ch);
4446
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
4547
$error = curl_error($ch);
46-
47-
48-
echo "HTTP Code: $http_code\n";
49-
echo "Error: " . ($error ?: "none") . "\n";
50-
echo "Response length: " . strlen($response) . "\n";
51-
52-
return $response;
48+
49+
50+
$output[7] = "HTTP Code: $http_code";
51+
$output[8] = "Error: " . ($error ?: "none");
52+
$output[9] = "Response length: " . strlen($response);
53+
54+
return $output;
5355
}
5456

5557
function test_not_found($server) {
56-
echo "Testing 404 error\n";
57-
58+
$output = [];
59+
$output[3] = "Testing 404 error";
60+
5861
$ch = curl_init();
5962
curl_setopt($ch, CURLOPT_URL, "http://localhost:{$server->port}/missing.html");
6063
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6164
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
62-
65+
6366
$response = curl_exec($ch);
6467
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
65-
66-
67-
echo "HTTP Code: $http_code\n";
68-
echo "Response length: " . strlen($response) . "\n";
69-
70-
return $response;
68+
69+
70+
$output[10] = "HTTP Code: $http_code";
71+
$output[11] = "Response length: " . strlen($response);
72+
73+
return $output;
7174
}
7275

7376
echo "Test start\n";
@@ -80,6 +83,18 @@ $coroutines = [
8083

8184
$results = await_all($coroutines);
8285

86+
// Merge all outputs and sort by key to ensure deterministic order
87+
$all_output = [];
88+
foreach ($results as $output) {
89+
$all_output = array_merge($all_output, $output);
90+
}
91+
ksort($all_output);
92+
93+
// Print in sorted order
94+
foreach ($all_output as $line) {
95+
echo "$line\n";
96+
}
97+
8398
echo "Test end\n";
8499

85100
async_test_server_stop($server);

0 commit comments

Comments
 (0)