Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Parallel/Application/ParallelFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function process(
));

++$systemErrorsCount;
$reachedSystemErrorsCountLimit = true;
$reachedSystemErrorsCountLimit = $systemErrorsCount >= self::SYSTEM_ERROR_LIMIT;
$this->processPool->quitAll();

// give the child processes time to actually die, they can still write to cache meanwhile
Expand Down
3 changes: 3 additions & 0 deletions tests/Bin/Fixture/ParallelTimeout/Timeout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

pause();
39 changes: 39 additions & 0 deletions tests/Bin/Fixture/TimeoutRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Bin\Fixture;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;

final class TimeoutRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Exceed the worker timeout on pause()', [new CodeSample('', '')]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if ($this->isName($node, 'pause')) {
sleep(10);
}

return null;
}
}
54 changes: 54 additions & 0 deletions tests/Bin/RectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,58 @@ public function testConsoleOutput(string $command, string $expectedOutput): void
$process->getErrorOutput()
));
}

/**
* @return Iterator<string, array{int, string[]}>
*/
public static function provideParallelTimeouts(): Iterator
{
yield 'one timeout below the limit' => [0, [
'Child process timed out after 1 seconds',
'Some parallel jobs have not finished, results for 1 of 1 files are missing',
]];

yield '49 errors followed by a timeout reach the limit' => [49, [
...array_fill(0, 49, "Syntax error, unexpected ';'"),
'Child process timed out after 1 seconds',
'Reached system errors count limit of 50, exiting...',
'Some parallel jobs have not finished, results for 1 of 50 files are missing',
]];
}

/**
* @param string[] $expectedMessages
*/
#[DataProvider('provideParallelTimeouts')]
public function testParallelTimeoutReportsErrorLimitOnlyWhenReached(
int $previousErrorCount,
array $expectedMessages
): void {
$filePaths = [];
for ($i = 5; $i < 5 + $previousErrorCount; ++$i) {
$filePaths[] = __DIR__ . sprintf('/../../e2e/parallel-system-errors-limit/src/File%02d.php', $i);
}

// The syntax-error files are larger, so the single worker processes them before the timeout file.
$filePaths[] = __DIR__ . '/Fixture/ParallelTimeout/Timeout.php';

$process = new Process([
PHP_BINARY,
'bin/rector',
'--config',
__DIR__ . '/config/parallel-worker-timeout.php',
'--dry-run',
'--clear-cache',
'--no-progress-bar',
'--output-format=json',
...$filePaths,
]);
$process->setTimeout(20);
$process->run();

$this->assertSame(1, $process->getExitCode(), $process->getOutput() . $process->getErrorOutput());
$result = json_decode($process->getOutput(), true, flags: JSON_THROW_ON_ERROR);
$this->assertSame(count($expectedMessages), $result['totals']['errors']);
$this->assertSame($expectedMessages, array_column($result['errors'], 'message'));
}
}
14 changes: 14 additions & 0 deletions tests/Bin/config/parallel-worker-timeout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Tests\Bin\Fixture\TimeoutRector;

require_once __DIR__ . '/../Fixture/TimeoutRector.php';

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([__DIR__ . '/../Fixture/ParallelTimeout']);
$rectorConfig->parallel(processTimeout: 1, maxNumberOfProcess: 1, jobSize: 1);
$rectorConfig->rules([TimeoutRector::class]);
};
Loading