Skip to content

Commit

Permalink
Merge pull request #4
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikola committed Jul 31, 2014
2 parents 6e2578c + cbb0ead commit 36f4b29
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
"keywords": ["process"],
"license": "MIT",
"require": {
"php": ">=5.4.0",
"evenement/evenement": "~2.0",
"react/event-loop": "0.4.*",
"react/stream": "0.4.*"
"php": ">=5.3.0",
"evenement/evenement": "~1.0",
"react/event-loop": "0.3.*",
"react/stream": "0.3.*"
},
"require-dev": {
"sebastian/environment": "~1.0"
},
"autoload": {
"psr-4": { "React\\ChildProcess\\": "src" }
Expand Down
9 changes: 5 additions & 4 deletions src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ public function start(LoopInterface $loop, $interval = 0.1)
stream_set_blocking($pipe, 0);
}

$loop->addPeriodicTimer($interval, function (Timer $timer) {
if (!$this->isRunning()) {
$this->close();
$that = $this;
$loop->addPeriodicTimer($interval, function (Timer $timer) use ($that) {
if (!$that->isRunning()) {
$that->close();
$timer->cancel();
$this->emit('exit', array($this->getExitCode(), $this->getTermSignal()));
$that->emit('exit', array($that->getExitCode(), $that->getTermSignal()));
}
});
}
Expand Down
31 changes: 20 additions & 11 deletions tests/AbstractProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use React\ChildProcess\Process;
use React\EventLoop\Timer\Timer;
use SebastianBergmann\Environment\Runtime;

abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -67,7 +68,7 @@ public function testGetTermSignalWhenRunning($process)

public function testProcessWithDefaultCwdAndEnv()
{
$cmd = PHP_BINARY . ' -r ' . escapeshellarg('echo getcwd(), PHP_EOL, count($_SERVER), PHP_EOL;');
$cmd = $this->getPhpBinary() . ' -r ' . escapeshellarg('echo getcwd(), PHP_EOL, count($_SERVER), PHP_EOL;');

$loop = $this->createLoop();
$process = new Process($cmd);
Expand Down Expand Up @@ -95,7 +96,7 @@ public function testProcessWithDefaultCwdAndEnv()

public function testProcessWithCwd()
{
$cmd = PHP_BINARY . ' -r ' . escapeshellarg('echo getcwd(), PHP_EOL;');
$cmd = $this->getPhpBinary() . ' -r ' . escapeshellarg('echo getcwd(), PHP_EOL;');

$loop = $this->createLoop();
$process = new Process($cmd, '/');
Expand All @@ -120,7 +121,7 @@ public function testProcessWithEnv()
$this->markTestSkipped('Cannot execute PHP processes with custom environments on Travis CI.');
}

$cmd = PHP_BINARY . ' -r ' . escapeshellarg('echo getenv("foo"), PHP_EOL;');
$cmd = $this->getPhpBinary() . ' -r ' . escapeshellarg('echo getenv("foo"), PHP_EOL;');

$loop = $this->createLoop();
$process = new Process($cmd, null, array('foo' => 'bar'));
Expand Down Expand Up @@ -267,21 +268,22 @@ public function testTerminateWithStopAndContinueSignalsUsingEventLoop()
$termSignal = func_get_arg(1);
});

$loop->addTimer(0.001, function(Timer $timer) use ($process) {
$that = $this;
$loop->addTimer(0.001, function(Timer $timer) use ($process, $that) {
$process->start($timer->getLoop());
$process->terminate(SIGSTOP);

$this->assertSoon(function() use ($process) {
$this->assertTrue($process->isStopped());
$this->assertTrue($process->isRunning());
$this->assertEquals(SIGSTOP, $process->getStopSignal());
$that->assertSoon(function() use ($process, $that) {
$that->assertTrue($process->isStopped());
$that->assertTrue($process->isRunning());
$that->assertEquals(SIGSTOP, $process->getStopSignal());
});

$process->terminate(SIGCONT);

$this->assertSoon(function() use ($process) {
$this->assertFalse($process->isStopped());
$this->assertEquals(SIGSTOP, $process->getStopSignal());
$that->assertSoon(function() use ($process, $that) {
$that->assertFalse($process->isStopped());
$that->assertEquals(SIGSTOP, $process->getStopSignal());
});
});

Expand Down Expand Up @@ -325,4 +327,11 @@ public function assertSoon(\Closure $callback, $timeout = 20000, $interval = 200
usleep($interval);
}
}

private function getPhpBinary()
{
$runtime = new Runtime();

return $runtime->getBinary();
}
}

0 comments on commit 36f4b29

Please sign in to comment.