Skip to content

Commit b6c206d

Browse files
committed
#19: Fix race conditions and conflicts when running async tests with multiple workers:
- Replace basic uniqid() with PID + microtime for unique temp files - Ensure each worker gets isolated temporary files and directories - Maintain cross-platform compatibility (Windows + Unix) - Preserve existing API - no breaking changes to test files This resolves "Server did not output startup message" failures when tests run in parallel with -j option.
1 parent de8b026 commit b6c206d

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

tests/common/http_server.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ function async_test_server_start(?string $router = null): AsyncTestServerInfo {
2727

2828
$cmd = [$php_executable, '-t', $doc_root, '-n', '-S', 'localhost:0', $router];
2929

30-
$output_file = tempnam(sys_get_temp_dir(), 'async_test_server_output');
30+
// Use unique temp file with PID and microtime for parallel workers
31+
$unique_id = getmypid() . '_' . microtime(true);
32+
$output_file = tempnam(sys_get_temp_dir(), "async_test_server_output_{$unique_id}_");
3133
$output_file_fd = fopen($output_file, 'ab');
3234
if ($output_file_fd === false) {
3335
die(sprintf("Failed opening output file %s\n", $output_file));
@@ -43,7 +45,7 @@ function async_test_server_start(?string $router = null): AsyncTestServerInfo {
4345
2 => $output_file_fd,
4446
);
4547
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
46-
48+
4749
// Wait for the server to start
4850
$bound = null;
4951
for ($i = 0; $i < 60; $i++) {
@@ -52,6 +54,7 @@ function async_test_server_start(?string $router = null): AsyncTestServerInfo {
5254
if (empty($status['running'])) {
5355
echo "Server failed to start\n";
5456
printf("Server output:\n%s\n", file_get_contents($output_file));
57+
fclose($output_file_fd);
5558
proc_terminate($handle);
5659
exit(1);
5760
}
@@ -66,6 +69,7 @@ function async_test_server_start(?string $router = null): AsyncTestServerInfo {
6669
if ($bound === null) {
6770
echo "Server did not output startup message\n";
6871
printf("Server output:\n%s\n", file_get_contents($output_file));
72+
fclose($output_file_fd);
6973
proc_terminate($handle);
7074
exit(1);
7175
}
@@ -91,12 +95,13 @@ function async_test_server_start(?string $router = null): AsyncTestServerInfo {
9195

9296
if ($error) {
9397
echo $error;
98+
fclose($output_file_fd);
9499
proc_terminate($handle);
95100
exit(1);
96101
}
97102

98103
register_shutdown_function(
99-
function($handle) use($doc_root, $output_file) {
104+
function($handle) use($doc_root, $output_file, $output_file_fd) {
100105
if (is_resource($handle) && get_resource_type($handle) === 'process') {
101106
$status = proc_get_status($handle);
102107
if ($status !== false && $status['running']) {
@@ -110,6 +115,9 @@ function($handle) use($doc_root, $output_file) {
110115
printf("Server output:\n%s\n", file_get_contents($output_file));
111116
}
112117
}
118+
if (is_resource($output_file_fd)) {
119+
fclose($output_file_fd);
120+
}
113121
@unlink($output_file);
114122
remove_directory($doc_root);
115123
},

0 commit comments

Comments
 (0)