Skip to content

Commit 56c7db4

Browse files
authored
Merge pull request #56 from clue-labs/loop
Fix forward compatibility with upcoming EventLoop releases
2 parents 7d1f765 + 5cb958d commit 56c7db4

8 files changed

Lines changed: 98 additions & 87 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"require": {
77
"php": ">=5.3.0",
88
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
9-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
9+
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
1010
"react/stream": "^1.0 || ^0.7.6"
1111
},
1212
"require-dev": {

src/Process.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Evenement\EventEmitter;
66
use React\EventLoop\LoopInterface;
7-
use React\EventLoop\Timer\TimerInterface;
87
use React\Stream\ReadableResourceStream;
98
use React\Stream\WritableResourceStream;
109

@@ -116,7 +115,7 @@ public function start(LoopInterface $loop, $interval = 0.1)
116115
return;
117116
}
118117

119-
$loop->addPeriodicTimer($interval, function (TimerInterface $timer) use ($that, $loop) {
118+
$loop->addPeriodicTimer($interval, function ($timer) use ($that, $loop) {
120119
if (!$that->isRunning()) {
121120
$that->close();
122121
$loop->cancelTimer($timer);

tests/AbstractProcessTest.php

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace React\Tests\ChildProcess;
44

5-
use React\ChildProcess\Process;
6-
use React\EventLoop\Timer\Timer;
75
use PHPUnit\Framework\ExpectationFailedException;
86
use PHPUnit\Framework\TestCase;
7+
use React\ChildProcess\Process;
98
use SebastianBergmann\Environment\Runtime;
109

1110
abstract class AbstractProcessTest extends TestCase
@@ -152,15 +151,13 @@ public function testProcessWithDefaultCwdAndEnv()
152151
$cmd = $this->getPhpBinary() . ' -r ' . escapeshellarg('echo getcwd(), PHP_EOL, count($_SERVER), PHP_EOL;');
153152

154153
$loop = $this->createLoop();
154+
155155
$process = new Process($cmd);
156+
$process->start($loop);
156157

157158
$output = '';
158-
159-
$loop->addTimer(0.001, function(Timer $timer) use ($process, &$output) {
160-
$process->start($timer->getLoop());
161-
$process->stdout->on('data', function () use (&$output) {
162-
$output .= func_get_arg(0);
163-
});
159+
$process->stdout->on('data', function () use (&$output) {
160+
$output .= func_get_arg(0);
164161
});
165162

166163
$loop->run();
@@ -180,15 +177,13 @@ public function testProcessWithCwd()
180177
$cmd = $this->getPhpBinary() . ' -r ' . escapeshellarg('echo getcwd(), PHP_EOL;');
181178

182179
$loop = $this->createLoop();
180+
183181
$process = new Process($cmd, '/');
182+
$process->start($loop);
184183

185184
$output = '';
186-
187-
$loop->addTimer(0.001, function(Timer $timer) use ($process, &$output) {
188-
$process->start($timer->getLoop());
189-
$process->stdout->on('data', function () use (&$output) {
190-
$output .= func_get_arg(0);
191-
});
185+
$process->stdout->on('data', function () use (&$output) {
186+
$output .= func_get_arg(0);
192187
});
193188

194189
$loop->run();
@@ -205,15 +200,13 @@ public function testProcessWithEnv()
205200
$cmd = $this->getPhpBinary() . ' -r ' . escapeshellarg('echo getenv("foo"), PHP_EOL;');
206201

207202
$loop = $this->createLoop();
203+
208204
$process = new Process($cmd, null, array('foo' => 'bar'));
205+
$process->start($loop);
209206

210207
$output = '';
211-
212-
$loop->addTimer(0.001, function(Timer $timer) use ($process, &$output) {
213-
$process->start($timer->getLoop());
214-
$process->stdout->on('data', function () use (&$output) {
215-
$output .= func_get_arg(0);
216-
});
208+
$process->stdout->on('data', function () use (&$output) {
209+
$output .= func_get_arg(0);
217210
});
218211

219212
$loop->run();
@@ -236,9 +229,7 @@ public function testStartAndAllowProcessToExitSuccessfullyUsingEventLoop()
236229
$termSignal = func_get_arg(1);
237230
});
238231

239-
$loop->addTimer(0.001, function(Timer $timer) use ($process) {
240-
$process->start($timer->getLoop());
241-
});
232+
$process->start($loop);
242233

243234
$loop->run();
244235

@@ -257,15 +248,13 @@ public function testStartInvalidProcess()
257248
$cmd = tempnam(sys_get_temp_dir(), 'react');
258249

259250
$loop = $this->createLoop();
251+
260252
$process = new Process($cmd);
253+
$process->start($loop);
261254

262255
$output = '';
263-
264-
$loop->addTimer(0.001, function(Timer $timer) use ($process, &$output) {
265-
$process->start($timer->getLoop());
266-
$process->stderr->on('data', function () use (&$output) {
267-
$output .= func_get_arg(0);
268-
});
256+
$process->stderr->on('data', function () use (&$output) {
257+
$output .= func_get_arg(0);
269258
});
270259

271260
$loop->run();
@@ -338,10 +327,8 @@ public function testTerminateWithDefaultTermSignalUsingEventLoop()
338327
$termSignal = func_get_arg(1);
339328
});
340329

341-
$loop->addTimer(0.001, function(Timer $timer) use ($process) {
342-
$process->start($timer->getLoop());
343-
$process->terminate();
344-
});
330+
$process->start($loop);
331+
$process->terminate();
345332

346333
$loop->run();
347334

@@ -379,22 +366,20 @@ public function testTerminateWithStopAndContinueSignalsUsingEventLoop()
379366
});
380367

381368
$that = $this;
382-
$loop->addTimer(0.001, function(Timer $timer) use ($process, $that) {
383-
$process->start($timer->getLoop());
384-
$process->terminate(SIGSTOP);
385-
386-
$that->assertSoon(function() use ($process, $that) {
387-
$that->assertTrue($process->isStopped());
388-
$that->assertTrue($process->isRunning());
389-
$that->assertEquals(SIGSTOP, $process->getStopSignal());
390-
});
391-
392-
$process->terminate(SIGCONT);
393-
394-
$that->assertSoon(function() use ($process, $that) {
395-
$that->assertFalse($process->isStopped());
396-
$that->assertEquals(SIGSTOP, $process->getStopSignal());
397-
});
369+
$process->start($loop);
370+
$process->terminate(SIGSTOP);
371+
372+
$that->assertSoon(function () use ($process, $that) {
373+
$that->assertTrue($process->isStopped());
374+
$that->assertTrue($process->isRunning());
375+
$that->assertEquals(SIGSTOP, $process->getStopSignal());
376+
});
377+
378+
$process->terminate(SIGCONT);
379+
380+
$that->assertSoon(function () use ($process, $that) {
381+
$that->assertFalse($process->isStopped());
382+
$that->assertEquals(SIGSTOP, $process->getStopSignal());
398383
});
399384

400385
$loop->run();
@@ -444,7 +429,12 @@ function ($output) use (&$stdErr) {
444429
}
445430
);
446431

447-
$loop->tick();
432+
// tick loop once
433+
$loop->addTimer(0, function () use ($loop) {
434+
$loop->stop();
435+
});
436+
$loop->run();
437+
448438
sleep(1); // comment this line out and it works fine
449439

450440
$loop->run();

tests/ExtEventLoopProcessTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace React\Tests\ChildProcess;
4+
5+
use React\EventLoop\ExtEventLoop;
6+
7+
class ExtEventLoopProcessTest extends AbstractProcessTest
8+
{
9+
public function createLoop()
10+
{
11+
if (!extension_loaded('event')) {
12+
$this->markTestSkipped('ext-event is not installed.');
13+
}
14+
if (!class_exists('React\EventLoop\ExtEventLoop')) {
15+
$this->markTestSkipped('ext-event not supported by this legacy react/event-loop version');
16+
}
17+
18+
return new ExtEventLoop();
19+
}
20+
}

tests/ExtLibevLoopProcessTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace React\Tests\ChildProcess;
4+
5+
use React\EventLoop\ExtLibevLoop;
6+
use React\EventLoop\LibEvLoop;
7+
8+
class ExtLibevLoopProcessTest extends AbstractProcessTest
9+
{
10+
public function createLoop()
11+
{
12+
if (!class_exists('libev\EventLoop')) {
13+
$this->markTestSkipped('ext-libev is not installed.');
14+
}
15+
16+
return class_exists('React\EventLoop\ExtLibevLoop') ? new ExtLibevLoop() : new LibEvLoop();
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace React\Tests\ChildProcess;
4+
5+
use React\EventLoop\ExtLibeventLoop;
6+
use React\EventLoop\LibEventLoop;
7+
8+
class ExtLibeventLoopProcessTest extends AbstractProcessTest
9+
{
10+
public function createLoop()
11+
{
12+
if (!function_exists('event_base_new')) {
13+
$this->markTestSkipped('ext-libevent is not installed.');
14+
}
15+
16+
return class_exists('React\EventLoop\ExtLibeventLoop') ? new ExtLibeventLoop() : new LibEventLoop();
17+
}
18+
}

tests/LibEvLoopProcessTest.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/LibEventLoopProcessTest.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)