[Process] fix waitUntil+add tests

This commit is contained in:
Nicolas Grekas 2018-10-23 10:23:47 +02:00
parent 9d9bd2d162
commit 946d278d7d
2 changed files with 17 additions and 8 deletions

View File

@ -450,27 +450,28 @@ class Process implements \IteratorAggregate
}
$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;
}
}
/**

View File

@ -141,7 +141,7 @@ class ProcessTest extends TestCase
$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;
@ -150,6 +150,7 @@ class ProcessTest extends TestCase
return false;
});
$p->stop();
$this->assertTrue($result);
if ('\\' === \DIRECTORY_SEPARATOR) {
// Windows is slower
@ -160,6 +161,13 @@ class ProcessTest extends TestCase
$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