Skip to content

Commit

Permalink
PHPUnit process is blocked when there's a lot of output and a test wi…
Browse files Browse the repository at this point in the history
…th separate process
  • Loading branch information
staabm committed Oct 19, 2024
1 parent 7c58ed5 commit af9d538
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
67 changes: 59 additions & 8 deletions src/Util/PHP/DefaultPhpProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
use function array_merge;
use function fclose;
use function file_put_contents;
use function fread;
use function fwrite;
use function is_array;
use function is_resource;
use function proc_close;
use function proc_open;
use function stream_get_contents;
use function proc_terminate;
use function sprintf;
use function stream_select;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;
Expand Down Expand Up @@ -112,16 +115,64 @@ protected function runProcess(string $job, array $settings): array

$stderr = $stdout = '';

if (isset($pipes[1])) {
$stdout = stream_get_contents($pipes[1]);
unset($pipes[0]);
$timeout = 5;

fclose($pipes[1]);
}
while (true) {
$r = $pipes;
$w = null;
$e = null;

$n = @stream_select($r, $w, $e, $timeout);

if ($n === false) {
break;
}

if ($n === 0) {
proc_terminate($process, 9);

throw new PhpProcessException(
sprintf(
'Job execution aborted after %d seconds',
$timeout,
),
);
}

if (isset($pipes[2])) {
$stderr = stream_get_contents($pipes[2]);
if ($n > 0) {
foreach ($r as $pipe) {
$pipeOffset = 0;

fclose($pipes[2]);
foreach ($pipes as $i => $origPipe) {
if ($pipe === $origPipe) {
$pipeOffset = $i;

break;
}
}

if (!$pipeOffset) {
break;
}

$line = fread($pipe, 8192);

if ($line === '' || $line === false) {
fclose($pipes[$pipeOffset]);

unset($pipes[$pipeOffset]);
} elseif ($pipeOffset === 1) {
$stdout .= $line;
} else {
$stderr .= $line;
}
}

if (empty($pipes)) {
break;
}
}
}

proc_close($process);
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/regression/5993/Issue5993Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
--SKIPIF--
<?php
for ($i = 0; $i < 390; $i++) {
trigger_error("error $i");
\trigger_error("error {$i}");
}
?>
--FILE--
Expand Down

0 comments on commit af9d538

Please sign in to comment.