Skip to content

Commit

Permalink
Merge branch 'next-release'
Browse files Browse the repository at this point in the history
  • Loading branch information
cboden committed Aug 1, 2016
2 parents 92b2937 + 472edad commit 3ab4f83
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm
- hhvm-nightly

matrix:
allow_failures:
- php: hhvm-nightly

before_script:
- composer install --dev --prefer-source
Expand Down
5 changes: 0 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@
},
"autoload": {
"psr-4": { "React\\ChildProcess\\": "src" }
},
"extra": {
"branch-alias": {
"dev-master": "0.5-dev"
}
}
}
31 changes: 22 additions & 9 deletions src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
use React\EventLoop\Timer\Timer;
use React\EventLoop\Timer\TimerInterface;
use React\Stream\Stream;

/**
Expand Down Expand Up @@ -101,18 +101,31 @@ public function start(LoopInterface $loop, $interval = 0.1)
throw new \RuntimeException('Unable to launch a new process.');
}

$closeCount = 0;

$streamCloseHandler = function (Stream $stream) use (&$closeCount, $loop, $interval) {
$closeCount++;

if ($closeCount < 2) {
return;
}

$loop->addPeriodicTimer($interval, function (TimerInterface $timer) {
if (!$this->isRunning()) {
$this->close();
$timer->cancel();
$this->emit('exit', array($this->getExitCode(), $this->getTermSignal()));
}
});

};

$this->stdin = new Stream($this->pipes[0], $loop);
$this->stdin->pause();
$this->stdout = new Stream($this->pipes[1], $loop);
$this->stdout->on('close', $streamCloseHandler);
$this->stderr = new Stream($this->pipes[2], $loop);

$loop->addPeriodicTimer($interval, function (Timer $timer) {
if (!$this->isRunning()) {
$this->close();
$timer->cancel();
$this->emit('exit', array($this->getExitCode(), $this->getTermSignal()));
}
});
$this->stderr->on('close', $streamCloseHandler);
}

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/AbstractProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,46 @@ public function testTerminateWithStopAndContinueSignalsUsingEventLoop()
$this->assertFalse($process->isTerminated());
}

public function testIssue18() {
$loop = $this->createLoop();

$testString = 'x';

$process = new Process($this->getPhpBinary() . " -r 'echo \"$testString\";'");

$stdOut = '';
$stdErr = '';

$process->on(
'exit',
function ($exitCode) use (&$stdOut, &$stdErr, $testString) {
$this->assertEquals(0, $exitCode, "Exit code is 0");

$this->assertEquals($testString, $stdOut);
}
);

$process->start($loop);

$process->stdout->on(
'data',
function ($output) use (&$stdOut) {
$stdOut .= $output;
}
);
$process->stderr->on(
'data',
function ($output) use (&$stdErr) {
$stdErr .= $output;
}
);

$loop->tick();
sleep(1); // comment this line out and it works fine

$loop->run();
}

/**
* Execute a callback at regular intervals until it returns successfully or
* a timeout is reached.
Expand Down

0 comments on commit 3ab4f83

Please sign in to comment.