bug #28958 [Process] fix waitUntil+add tests (nicolas-grekas)
This PR was merged into the 4.2-dev branch.
Discussion
----------
[Process] fix waitUntil+add tests
| Q | A
| ------------- | ---
| Branch? | 4.2
| Bug fix? | yes
| New feature? |
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Commits
-------
946d278d7d
[Process] fix waitUntil+add tests
This commit is contained in:
commit
73032f3445
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user