Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
fixing forking test for Travis
Browse files Browse the repository at this point in the history
  • Loading branch information
fvovan committed Apr 14, 2015
1 parent 6a3bbe1 commit 048a2a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
2 changes: 2 additions & 0 deletions library/CM/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class CM_Process {

const FORKING_DELAY = 200;
const RESPAWN_TIMEOUT = 10;

/** @var Closure|null */
Expand Down Expand Up @@ -165,6 +166,7 @@ private function _fork(Closure $workload, $sequence) {
throw new CM_Exception('Cannot open stream socket pair');
}
$pid = pcntl_fork();
usleep(self::FORKING_DELAY);
if ($pid === -1) {
throw new CM_Exception('Could not spawn child process');
}
Expand Down
47 changes: 26 additions & 21 deletions tests/library/CM/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,43 @@ public function testFork() {
*/
public function testForkAndWaitForChildren() {
$process = CM_Process::getInstance();
$parentOutput = [];
for ($i = 1; $i <= 4; $i++) {
CM_ProcessTest::writeln("Child $i forked.");
$parentOutput[] = "Child $i forked.";
$process->fork(function () use ($i) {
$ms = 100 * $i;
usleep($ms * 1000);
CM_ProcessTest::writeln("Child $i terminated after $ms ms.");
});
}
CM_ProcessTest::writeln('Parent waiting for 250 ms...');
$parentOutput[] = 'Parent waiting for 250 ms...';
usleep(250000);
CM_ProcessTest::writeln('Parent listening to children...');
$process->waitForChildren(null, function () {
CM_ProcessTest::writeln('All children terminated.');
$parentOutput[] = 'Parent listening to children...';
$process->waitForChildren(null, function () use (&$parentOutput) {
$parentOutput[] = 'All children terminated.';
});
CM_ProcessTest::writeln('Parent terminated.');
$parentOutput[] = 'Parent terminated.';

$outputFileExpected = 'Child 1 forked.
Child 2 forked.
Child 3 forked.
Child 4 forked.
Parent waiting for 250 ms...
Child 1 terminated after 100 ms.
Child 2 terminated after 200 ms.
Parent listening to children...
Child 3 terminated after 300 ms.
Child 4 terminated after 400 ms.
All children terminated.
Parent terminated.
';
rewind(self::$_file);
$outputFileActual = fread(self::$_file, 8192);
$this->assertEquals($outputFileExpected, $outputFileActual);
$childrenOutput = explode("\n", fread(self::$_file, 8192));

$this->assertSame([
'Child 1 forked.',
'Child 2 forked.',
'Child 3 forked.',
'Child 4 forked.',
'Parent waiting for 250 ms...',
'Parent listening to children...',
'All children terminated.',
'Parent terminated.'
], $parentOutput);

$this->assertContainsAll([
'Child 2 terminated after 200 ms.',
'Child 1 terminated after 100 ms.',
'Child 3 terminated after 300 ms.',
'Child 4 terminated after 400 ms.',
], $childrenOutput);
}

/**
Expand Down

0 comments on commit 048a2a0

Please sign in to comment.