Skip to content

Commit 772cd90

Browse files
authored
test: prevent random test runner deadlock with one job (#10729)
* fix: prevent random test runner deadlock with one job * test: move random runner regression coverage to AutoReview * test: make random runner regression portable * style: align random runner test assignments
1 parent 40f64d0 commit 772cd90

2 files changed

Lines changed: 122 additions & 1 deletion

File tree

‎.github/scripts/run-random-tests.sh‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,10 @@ cleanup_finished_pids() {
665665
active+=("$pid")
666666
fi
667667
done
668-
bg_pids=("${active[@]:-}")
668+
bg_pids=()
669+
if [[ ${#active[@]} -gt 0 ]]; then
670+
bg_pids=("${active[@]}")
671+
fi
669672
}
670673

671674
spawn_limited_job() {
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\AutoReview;
15+
16+
use FilesystemIterator;
17+
use PHPUnit\Framework\Attributes\CoversNothing;
18+
use PHPUnit\Framework\Attributes\DataProvider;
19+
use PHPUnit\Framework\Attributes\Group;
20+
use PHPUnit\Framework\TestCase;
21+
use RecursiveDirectoryIterator;
22+
use RecursiveIteratorIterator;
23+
24+
/**
25+
* @internal
26+
*/
27+
#[CoversNothing]
28+
#[Group('AutoReview')]
29+
final class RunRandomTestsTest extends TestCase
30+
{
31+
private string $project;
32+
private string $timeoutCommand;
33+
34+
protected function setUp(): void
35+
{
36+
parent::setUp();
37+
38+
if (is_windows()) {
39+
$this->markTestSkipped('The random test runner requires Bash and GNU coreutils.');
40+
}
41+
42+
exec('command -v timeout || command -v gtimeout', $output, $exitCode);
43+
44+
if ($exitCode !== 0) {
45+
$this->markTestSkipped('The random test runner test requires timeout or gtimeout.');
46+
}
47+
48+
$this->timeoutCommand = $output[0];
49+
$this->project = sys_get_temp_dir() . '/ci4-random-tests-' . bin2hex(random_bytes(4));
50+
51+
foreach (['.github/scripts', 'vendor/bin', 'tests/system/First', 'tests/system/Second'] as $directory) {
52+
mkdir($this->project . '/' . $directory, 0777, true);
53+
}
54+
55+
copy(ROOTPATH . '.github/scripts/run-random-tests.sh', $this->project . '/.github/scripts/run-random-tests.sh');
56+
file_put_contents($this->project . '/.github/scripts/random-tests-config.txt', "First\nSecond\n");
57+
file_put_contents($this->project . '/vendor/bin/phpunit', <<<'BASH'
58+
#!/usr/bin/env bash
59+
printf '%s\n' "$1" >> calls.log
60+
sleep 0.05
61+
62+
BASH);
63+
chmod($this->project . '/vendor/bin/phpunit', 0755);
64+
}
65+
66+
protected function tearDown(): void
67+
{
68+
if (isset($this->project)) {
69+
$files = new RecursiveIteratorIterator(
70+
new RecursiveDirectoryIterator($this->project, FilesystemIterator::SKIP_DOTS),
71+
RecursiveIteratorIterator::CHILD_FIRST,
72+
);
73+
74+
foreach ($files as $file) {
75+
$file->isDir() ? rmdir($file->getPathname()) : unlink($file->getPathname());
76+
}
77+
78+
rmdir($this->project);
79+
}
80+
81+
parent::tearDown();
82+
}
83+
84+
#[DataProvider('provideRunsEveryComponentOnEachRepetition')]
85+
public function testRunsEveryComponentOnEachRepetition(int $maxJobs): void
86+
{
87+
// Bound the command so a scheduler regression fails instead of hanging PHPUnit.
88+
exec(sprintf(
89+
'%s --kill-after=1s 5s bash %s --max-jobs %d --repeat 2 --timeout 0 2>&1',
90+
escapeshellarg($this->timeoutCommand),
91+
escapeshellarg($this->project . '/.github/scripts/run-random-tests.sh'),
92+
$maxJobs,
93+
), $output, $exitCode);
94+
$output = implode("\n", $output);
95+
96+
$this->assertSame(0, $exitCode, $output);
97+
$this->assertStringContainsString('All components passed random execution tests!', $output);
98+
99+
$calls = file($this->project . '/calls.log', FILE_IGNORE_NEW_LINES);
100+
101+
$this->assertIsArray($calls);
102+
$this->assertCount(4, $calls);
103+
104+
foreach (['First', 'Second'] as $component) {
105+
$this->assertCount(2, array_keys($calls, 'tests/system/' . $component, true));
106+
}
107+
}
108+
109+
/**
110+
* @return iterable<string, array{int}>
111+
*/
112+
public static function provideRunsEveryComponentOnEachRepetition(): iterable
113+
{
114+
yield 'one job' => [1];
115+
116+
yield 'two jobs' => [2];
117+
}
118+
}

0 commit comments

Comments
 (0)