Repository navigation
fix: prevent random test runner deadlock with one job #10729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\AutoReview; | ||
|
|
||
| use FilesystemIterator; | ||
| use PHPUnit\Framework\Attributes\CoversNothing; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| use PHPUnit\Framework\TestCase; | ||
| use RecursiveDirectoryIterator; | ||
| use RecursiveIteratorIterator; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| #[CoversNothing] | ||
| #[Group('AutoReview')] | ||
| final class RunRandomTestsTest extends TestCase | ||
| { | ||
| private string $project; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| if (PHP_OS_FAMILY === 'Windows') { | ||
| $this->markTestSkipped('The random test runner requires Bash and GNU coreutils.'); | ||
| } | ||
|
|
||
| $this->project = sys_get_temp_dir() . '/ci4-random-tests-' . bin2hex(random_bytes(4)); | ||
|
|
||
| foreach (['.github/scripts', 'vendor/bin', 'tests/system/First', 'tests/system/Second'] as $directory) { | ||
| mkdir($this->project . '/' . $directory, 0777, true); | ||
| } | ||
|
|
||
| copy(ROOTPATH . '.github/scripts/run-random-tests.sh', $this->project . '/.github/scripts/run-random-tests.sh'); | ||
| file_put_contents($this->project . '/.github/scripts/random-tests-config.txt', "First\nSecond\n"); | ||
| file_put_contents($this->project . '/vendor/bin/phpunit', <<<'BASH' | ||
| #!/usr/bin/env bash | ||
| printf '%s\n' "$1" >> calls.log | ||
| sleep 0.05 | ||
|
|
||
| BASH); | ||
| chmod($this->project . '/vendor/bin/phpunit', 0755); | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| if (isset($this->project)) { | ||
| $files = new RecursiveIteratorIterator( | ||
| new RecursiveDirectoryIterator($this->project, FilesystemIterator::SKIP_DOTS), | ||
| RecursiveIteratorIterator::CHILD_FIRST, | ||
| ); | ||
|
|
||
| foreach ($files as $file) { | ||
| $file->isDir() ? rmdir($file->getPathname()) : unlink($file->getPathname()); | ||
| } | ||
|
|
||
| rmdir($this->project); | ||
| } | ||
|
|
||
| parent::tearDown(); | ||
| } | ||
|
|
||
| #[DataProvider('provideRunsEveryComponentOnEachRepetition')] | ||
| public function testRunsEveryComponentOnEachRepetition(int $maxJobs): void | ||
| { | ||
| // Bound the command so a scheduler regression fails instead of hanging PHPUnit. | ||
| exec(sprintf( | ||
| 'GITHUB_ACTIONS=true timeout --kill-after=1s 5s bash %s --max-jobs %d --repeat 2 --timeout 0 2>&1', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you need to set
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| escapeshellarg($this->project . '/.github/scripts/run-random-tests.sh'), | ||
| $maxJobs, | ||
| ), $output, $exitCode); | ||
| $output = implode("\n", $output); | ||
|
|
||
| $this->assertSame(0, $exitCode, $output); | ||
| $this->assertStringContainsString('All components passed random execution tests!', $output); | ||
|
|
||
| $calls = file($this->project . '/calls.log', FILE_IGNORE_NEW_LINES); | ||
|
|
||
| $this->assertIsArray($calls); | ||
| $this->assertCount(4, $calls); | ||
|
|
||
| foreach (['First', 'Second'] as $component) { | ||
| $this->assertCount(2, array_keys($calls, 'tests/system/' . $component, true)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<string, array{int}> | ||
| */ | ||
| public static function provideRunsEveryComponentOnEachRepetition(): iterable | ||
| { | ||
| yield 'one job' => [1]; | ||
|
|
||
| yield 'two jobs' => [2]; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can use
is_windows()