Skip to content

Commit

Permalink
[Process] Added process synchronization to the incremental output tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
webmozart committed Aug 6, 2014
1 parent 678f772 commit 6dd3946
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/Symfony/Component/Process/Tests/AbstractProcessTest.php
Expand Up @@ -259,32 +259,52 @@ public function testGetErrorOutput()

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

$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); }')));

$p->start();
while ($p->isRunning()) {
$this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getIncrementalErrorOutput(), $matches));
usleep(20000);
if ('R' === file_get_contents($lock)) {
$this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getIncrementalErrorOutput(), $matches));
file_put_contents($lock, 'W');
}
usleep(100);
}

unlink($lock);
}

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

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

public function testGetIncrementalOutput()
{
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) { echo \' foo \'; usleep(50000); $n++; }')));
// use a lock file to toggle between writing ("W") and reading ("R") the
// output stream
$lock = tempnam(sys_get_temp_dir(), get_class($this).'Lock');
file_put_contents($lock, 'W');

$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); }')));

$p->start();
while ($p->isRunning()) {
$this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getIncrementalOutput(), $matches));
usleep(20000);
if ('R' === file_get_contents($lock)) {
$this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getIncrementalOutput(), $matches));
file_put_contents($lock, 'W');
}
usleep(100);
}

unlink($lock);
}

public function testZeroAsOutput()
Expand Down

0 comments on commit 6dd3946

Please sign in to comment.