Skip to content

Commit

Permalink
minor #11565 [Process] Added process synchronization to the increment…
Browse files Browse the repository at this point in the history
…al 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
  • Loading branch information
fabpot committed Aug 7, 2014
2 parents 976a1cc + 6dd3946 commit 56a7517
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 56a7517

Please sign in to comment.