Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56a7517

Browse files
committed
minor #11565 [Process] Added process synchronization to the incremental output tests (webmozart)
This PR was merged into the 2.3 branch. Discussion ---------- [Process] Added process synchronization to the incremental output tests | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - The tests currently fail from time to time if the executing machine is under heavy load. This leads to false negatives on Travis CI. A side effect of the change is that the tests are much faster now. Commits ------- 6dd3946 [Process] Added process synchronization to the incremental output tests
2 parents 976a1cc + 6dd3946 commit 56a7517

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,32 +259,52 @@ public function testGetErrorOutput()
259259

260260
public function testGetIncrementalErrorOutput()
261261
{
262-
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { usleep(100000); file_put_contents(\'php://stderr\', \'ERROR\'); $n++; }')));
262+
// use a lock file to toggle between writing ("W") and reading ("R") the
263+
// error stream
264+
$lock = tempnam(sys_get_temp_dir(), get_class($this).'Lock');
265+
file_put_contents($lock, 'W');
266+
267+
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { if (\'W\' === file_get_contents('.var_export($lock, true).')) { file_put_contents(\'php://stderr\', \'ERROR\'); $n++; file_put_contents('.var_export($lock, true).', \'R\'); } usleep(100); }')));
263268

264269
$p->start();
265270
while ($p->isRunning()) {
266-
$this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getIncrementalErrorOutput(), $matches));
267-
usleep(20000);
271+
if ('R' === file_get_contents($lock)) {
272+
$this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getIncrementalErrorOutput(), $matches));
273+
file_put_contents($lock, 'W');
274+
}
275+
usleep(100);
268276
}
277+
278+
unlink($lock);
269279
}
270280

271281
public function testGetOutput()
272282
{
273-
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) {echo \' foo \';$n++; usleep(500); }')));
283+
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { echo \' foo \'; $n++; }')));
274284

275285
$p->run();
276286
$this->assertEquals(3, preg_match_all('/foo/', $p->getOutput(), $matches));
277287
}
278288

279289
public function testGetIncrementalOutput()
280290
{
281-
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) { echo \' foo \'; usleep(50000); $n++; }')));
291+
// use a lock file to toggle between writing ("W") and reading ("R") the
292+
// output stream
293+
$lock = tempnam(sys_get_temp_dir(), get_class($this).'Lock');
294+
file_put_contents($lock, 'W');
295+
296+
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { if (\'W\' === file_get_contents('.var_export($lock, true).')) { echo \' foo \'; $n++; file_put_contents('.var_export($lock, true).', \'R\'); } usleep(100); }')));
282297

283298
$p->start();
284299
while ($p->isRunning()) {
285-
$this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getIncrementalOutput(), $matches));
286-
usleep(20000);
300+
if ('R' === file_get_contents($lock)) {
301+
$this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getIncrementalOutput(), $matches));
302+
file_put_contents($lock, 'W');
303+
}
304+
usleep(100);
287305
}
306+
307+
unlink($lock);
288308
}
289309

290310
public function testZeroAsOutput()

0 commit comments

Comments
 (0)