Skip to content

[Process] fix waitUntil+add tests #28958

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

Merged
merged 1 commit into from
Oct 23, 2018
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
15 changes: 8 additions & 7 deletions src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,27 +450,28 @@ public function waitUntil(callable $callback): bool
}
$callback = $this->buildCallback($callback);

$wait = true;
do {
$ready = false;
while (true) {
$this->checkTimeout();
$running = '\\' === \DIRECTORY_SEPARATOR ? $this->isRunning() : $this->processPipes->areOpen();
$output = $this->processPipes->readAndWrite($running, '\\' !== \DIRECTORY_SEPARATOR || !$running);

foreach ($output as $type => $data) {
if (3 !== $type) {
$wait = !$callback(self::STDOUT === $type ? self::OUT : self::ERR, $data);
$ready = $ready || $callback(self::STDOUT === $type ? self::OUT : self::ERR, $data);
} elseif (!isset($this->fallbackStatus['signaled'])) {
$this->fallbackStatus['exitcode'] = (int) $data;
}
}
if ($wait && !$this->isRunning()) {
if ($ready) {
return true;
}
if (!$running) {
return false;
}

usleep(1000);
} while ($wait);

return true;
}
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function testWaitUntilSpecificOutput()
$start = microtime(true);

$completeOutput = '';
$p->waitUntil(function ($type, $output) use (&$completeOutput) {
$result = $p->waitUntil(function ($type, $output) use (&$completeOutput) {
$completeOutput .= $output;
if (false !== strpos($output, 'One more')) {
return true;
Expand All @@ -150,6 +150,7 @@ public function testWaitUntilSpecificOutput()
return false;
});
$p->stop();
$this->assertTrue($result);

if ('\\' === \DIRECTORY_SEPARATOR) {
// Windows is slower
Expand All @@ -160,6 +161,13 @@ public function testWaitUntilSpecificOutput()
$this->assertEquals("First iteration output\nSecond iteration output\nOne more iteration output\n", $completeOutput);
}

public function testWaitUntilCanReturnFalse()
{
$p = $this->getProcess('echo foo');
$p->start();
$this->assertFalse($p->waitUntil(function () { return false; }));
}

public function testAllOutputIsActuallyReadOnTermination()
{
// this code will result in a maximum of 2 reads of 8192 bytes by calling
Expand Down