Skip to content

Commit 73032f3

Browse files
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 ------- 946d278 [Process] fix waitUntil+add tests
2 parents 9d9bd2d + 946d278 commit 73032f3

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -450,27 +450,28 @@ public function waitUntil(callable $callback): bool
450450
}
451451
$callback = $this->buildCallback($callback);
452452

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

459459
foreach ($output as $type => $data) {
460460
if (3 !== $type) {
461-
$wait = !$callback(self::STDOUT === $type ? self::OUT : self::ERR, $data);
461+
$ready = $ready || $callback(self::STDOUT === $type ? self::OUT : self::ERR, $data);
462462
} elseif (!isset($this->fallbackStatus['signaled'])) {
463463
$this->fallbackStatus['exitcode'] = (int) $data;
464464
}
465465
}
466-
if ($wait && !$this->isRunning()) {
466+
if ($ready) {
467+
return true;
468+
}
469+
if (!$running) {
467470
return false;
468471
}
469472

470473
usleep(1000);
471-
} while ($wait);
472-
473-
return true;
474+
}
474475
}
475476

476477
/**

src/Symfony/Component/Process/Tests/ProcessTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function testWaitUntilSpecificOutput()
141141
$start = microtime(true);
142142

143143
$completeOutput = '';
144-
$p->waitUntil(function ($type, $output) use (&$completeOutput) {
144+
$result = $p->waitUntil(function ($type, $output) use (&$completeOutput) {
145145
$completeOutput .= $output;
146146
if (false !== strpos($output, 'One more')) {
147147
return true;
@@ -150,6 +150,7 @@ public function testWaitUntilSpecificOutput()
150150
return false;
151151
});
152152
$p->stop();
153+
$this->assertTrue($result);
153154

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

164+
public function testWaitUntilCanReturnFalse()
165+
{
166+
$p = $this->getProcess('echo foo');
167+
$p->start();
168+
$this->assertFalse($p->waitUntil(function () { return false; }));
169+
}
170+
163171
public function testAllOutputIsActuallyReadOnTermination()
164172
{
165173
// this code will result in a maximum of 2 reads of 8192 bytes by calling

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy