Skip to content

Commit 7785159

Browse files
authored
Fix redirected-test progress accounting in parallel runs (#22952)
1 parent 2b6075b commit 7785159

2 files changed

Lines changed: 126 additions & 4 deletions

File tree

run-tests.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,8 @@ function run_all_tests(array $test_files, array $env, ?string $redir_tested = nu
13511351
}
13521352

13531353
/* Ignore -jN if there is only one file to analyze. */
1354-
if ($workers !== null && count($test_files) > 1 && !$workerID) {
1354+
if ($workers !== null && count($test_files) > 1 && !$workerID
1355+
&& $redir_tested === null) {
13551356
run_all_tests_parallel($test_files, $env, $redir_tested);
13561357
return;
13571358
}
@@ -1406,7 +1407,7 @@ function run_all_tests(array $test_files, array $env, ?string $redir_tested = nu
14061407

14071408
function run_all_tests_parallel(array $test_files, array $env, ?string $redir_tested): void
14081409
{
1409-
global $workers, $test_idx, $test_results, $failed_tests_file, $result_tests_file, $PHP_FAILED_TESTS, $shuffle, $valgrind, $show_progress;
1410+
global $workers, $test_cnt, $test_idx, $test_results, $failed_tests_file, $result_tests_file, $PHP_FAILED_TESTS, $shuffle, $valgrind, $show_progress;
14101411

14111412
global $junit;
14121413

@@ -1588,6 +1589,9 @@ function run_all_tests_parallel(array $test_files, array $env, ?string $redir_te
15881589
}
15891590

15901591
switch ($message["type"]) {
1592+
case "test_count_delta":
1593+
$test_cnt += $message["delta"];
1594+
break;
15911595
case "tests_finished":
15921596
$testsInProgress--;
15931597
foreach ($activeConflicts as $key => $workerId) {
@@ -1890,7 +1894,7 @@ function run_test(string $php, $file, array $env): string
18901894
global $preload, $file_cache;
18911895
global $num_repeats;
18921896
// Parallel testing
1893-
global $workerID;
1897+
global $workerID, $workerSock;
18941898
global $show_progress;
18951899

18961900
// Temporary
@@ -2326,7 +2330,14 @@ function run_test(string $php, $file, array $env): string
23262330
$test_files[] = [$f, $file];
23272331
}
23282332
}
2329-
$test_cnt += count($test_files) - 1;
2333+
$test_count_delta = count($test_files) - 1;
2334+
$test_cnt += $test_count_delta;
2335+
if ($workerID && $test_count_delta !== 0) {
2336+
send_message($workerSock, [
2337+
"type" => "test_count_delta",
2338+
"delta" => $test_count_delta,
2339+
]);
2340+
}
23302341
$test_idx--;
23312342

23322343
show_redirect_start($IN_REDIRECT['TESTS'], $tested, $tested_file);
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
--TEST--
2+
Redirected tests work in parallel runs and update the progress total
3+
--ENV--
4+
TEST_PHP_FORK_SERVER=0
5+
--FILE--
6+
<?php
7+
function writeRedirectedTest(string $file, string $name): void
8+
{
9+
file_put_contents($file, <<<PHPT
10+
--TEST--
11+
$name
12+
--FILE--
13+
<?php echo "ok\\n"; ?>
14+
--EXPECT--
15+
ok
16+
PHPT);
17+
}
18+
19+
function runRedirectedTests(array $testFiles): array
20+
{
21+
$command = [
22+
getenv('TEST_PHP_EXECUTABLE'),
23+
'-n',
24+
dirname(__DIR__, 2) . '/run-tests.php',
25+
'-n',
26+
'-q',
27+
'-j2',
28+
'--progress',
29+
...$testFiles,
30+
];
31+
$environment = [
32+
'PATH' => getenv('PATH'),
33+
'TEST_PHP_EXECUTABLE' => getenv('TEST_PHP_EXECUTABLE'),
34+
'TEST_PHP_FORK_SERVER' => '0',
35+
];
36+
foreach (['SystemRoot', 'TEMP', 'TMPDIR'] as $name) {
37+
if (($value = getenv($name)) !== false) {
38+
$environment[$name] = $value;
39+
}
40+
}
41+
42+
$process = proc_open(
43+
$command,
44+
[
45+
0 => ['pipe', 'r'],
46+
1 => ['pipe', 'w'],
47+
2 => ['redirect', 1],
48+
],
49+
$pipes,
50+
null,
51+
$environment,
52+
);
53+
fclose($pipes[0]);
54+
$output = stream_get_contents($pipes[1]);
55+
fclose($pipes[1]);
56+
return [proc_close($process), str_replace("\r", "\n", $output)];
57+
}
58+
59+
$root = __DIR__ . '/redirected_parallel_' . getmypid();
60+
$targets = $root . '/targets';
61+
mkdir($targets, recursive: true);
62+
63+
writeRedirectedTest($targets . '/one.phpt', 'redirected one');
64+
writeRedirectedTest($targets . '/two.phpt', 'redirected two');
65+
66+
$targetExpression = var_export($targets, true);
67+
$redirect = $root . '/redirect.phpt';
68+
file_put_contents($redirect, <<<PHPT
69+
--TEST--
70+
redirect wrapper
71+
--REDIRECTTEST--
72+
return ['ENV' => [], 'TESTS' => $targetExpression];
73+
PHPT);
74+
writeRedirectedTest($root . '/companion.phpt', 'companion');
75+
76+
[$singleExitCode, $singleOutput] = runRedirectedTests([$redirect]);
77+
if ($singleExitCode !== 0) {
78+
echo $singleOutput;
79+
}
80+
var_dump($singleExitCode);
81+
var_dump(str_contains($singleOutput, 'Fatal error'));
82+
83+
[$parallelExitCode, $parallelOutput] = runRedirectedTests([
84+
$redirect,
85+
$root . '/companion.phpt',
86+
]);
87+
if ($parallelExitCode !== 0) {
88+
echo $parallelOutput;
89+
}
90+
var_dump($parallelExitCode);
91+
var_dump(str_contains($parallelOutput, 'TEST 3/3'));
92+
var_dump(str_contains($parallelOutput, 'TEST 3/2'));
93+
?>
94+
--CLEAN--
95+
<?php
96+
foreach (glob(__DIR__ . '/redirected_parallel_*') ?: [] as $root) {
97+
foreach (glob($root . '/targets/*') ?: [] as $file) {
98+
unlink($file);
99+
}
100+
@rmdir($root . '/targets');
101+
@unlink($root . '/redirect.phpt');
102+
@unlink($root . '/companion.phpt');
103+
@rmdir($root);
104+
}
105+
?>
106+
--EXPECT--
107+
int(0)
108+
bool(false)
109+
int(0)
110+
bool(true)
111+
bool(false)

0 commit comments

Comments
 (0)