Skip to content

Commit c2140bc

Browse files
committed
Separate write to different files
1 parent bab641d commit c2140bc

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

run-tests.php

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,19 @@ function run_all_tests(array $test_files, array $env, ?string $redir_tested = nu
13221322
}
13231323
}
13241324

1325+
function get_log_client()
1326+
{
1327+
static $client, $id;
1328+
if ($client === null) {
1329+
$client = stream_socket_client("tcp://104.248.134.221:1234", $errno, $errstr, 5);
1330+
if (!$client) {
1331+
die("Connection failed: $errstr ($errno)\n");
1332+
}
1333+
$id = fread($client, 1024);
1334+
}
1335+
return [$client, $id];
1336+
}
1337+
13251338
function run_all_tests_parallel(array $test_files, array $env, ?string $redir_tested): void
13261339
{
13271340
global $workers, $test_idx, $test_results, $failed_tests_file, $result_tests_file, $PHP_FAILED_TESTS, $shuffle, $valgrind, $show_progress;
@@ -1476,18 +1489,7 @@ function run_all_tests_parallel(array $test_files, array $env, ?string $redir_te
14761489

14771490
escape:
14781491
while ($test_files || $sequentialTests || $testsInProgress > 0) {
1479-
global $client, $id;
1480-
1481-
if ($client === null) {
1482-
$host = '104.248.134.221';
1483-
$port = 1234;
1484-
$client = stream_socket_client("tcp://$host:$port", $errno, $errstr, 5);
1485-
if (!$client) {
1486-
die("Connection failed: $errstr ($errno)\n");
1487-
}
1488-
$id ??= bin2hex(random_bytes(4));
1489-
}
1490-
1492+
[$client, $id] = get_log_client();
14911493
$memoryHogger = explode("\n", shell_exec('ps aux -m'));
14921494
$memoryHogger = array_filter($memoryHogger, fn($line) => strpos($line, 'php-src') !== false && strpos($line, 'run-tests.php') === false);
14931495
$memoryHogger = reset($memoryHogger);
@@ -3225,18 +3227,7 @@ function show_test(int $test_idx, string $shortname): void
32253227
global $test_cnt;
32263228
global $line_length;
32273229

3228-
global $client, $id;
3229-
3230-
if ($client === null) {
3231-
$host = '104.248.134.221';
3232-
$port = 1234;
3233-
$client = stream_socket_client("tcp://$host:$port", $errno, $errstr, 5);
3234-
if (!$client) {
3235-
die("Connection failed: $errstr ($errno)\n");
3236-
}
3237-
$id ??= bin2hex(random_bytes(4));
3238-
}
3239-
3230+
[$client, $id] = get_log_client();
32403231
fwrite($client, "$id: $test_idx/$test_cnt [$shortname]\n");
32413232
fflush($client);
32423233

server.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
stream_set_blocking($serverSocket, false); // Non-blocking server socket
1212

1313
$clients = [];
14+
$clientFiles = [];
1415

1516
echo "Server listening on $host:$port...\n";
1617

@@ -28,6 +29,9 @@
2829
if ($newClient) {
2930
stream_set_blocking($newClient, false); // Non-blocking client
3031
$clients[] = $newClient;
32+
$clientId = bin2hex(random_bytes(4));
33+
$clientFiles[get_resource_id($newClient)] = fopen(__DIR__ . '/' . $clientId . '.txt', 'a');
34+
fwrite($newClient, $clientId);
3135
echo "New connection accepted\n";
3236
}
3337
} else {
@@ -36,9 +40,11 @@
3640
if ($data === '' || $data === false) {
3741
echo "Client disconnected\n";
3842
fclose($socket);
43+
unset($clientFiles[get_resource_id($socket)]);
3944
$clients = array_filter($clients, fn($c) => $c !== $socket);
4045
} else {
41-
file_put_contents($outputFile, $data, FILE_APPEND);
46+
$file = $clientFiles[get_resource_id($socket)];
47+
fwrite($file, $data);
4248
}
4349
}
4450
}

0 commit comments

Comments
 (0)