8000 moved signal handling tests to AbstractLoopTest · cebe/event-loop@7cf320a · GitHub
[go: up one dir, main page]

Skip to content

Commit 7cf320a

Browse files
committed
moved signal handling tests to AbstractLoopTest
do not merge yet, this currently fails on various combinations of php versions, I'd like to fix it after reactphp#45 is merged to have tests run against all systems.
1 parent e10c3d1 commit 7cf320a

File tree

3 files changed

+121
-119
lines changed

3 files changed

+121
-119
lines changed

src/ExtEventLoop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ private function unsubscribeStreamEvent($stream, $flag)
294294
*/
295295
private function createTimerCallback()
296296
{
297-
$this->timerCallback = function ($_, $_, $timer) {
297+
$this->timerCallback = function ($_, $__, $timer) {
298298
call_user_func($timer->getCallback(), $timer);
299299

300300
if (!$timer->isPeriodic() && $this->isTimerActive($timer)) {

tests/AbstractLoopTest.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace React\Tests\EventLoop;
44

5+
use React\EventLoop\LoopInterface;
6+
57
abstract class AbstractLoopTest extends TestCase
68
{
79
/**
@@ -11,9 +13,18 @@ abstract class AbstractLoopTest extends TestCase
1113

1214
public function setUp()
1315
{
16+
parent::setUp();
1417
$this->loop = $this->createLoop();
1518
}
1619

20+
protected function tearDown()
21+
{
22+
parent::tearDown();
23+
if (strncmp($this->getName(false), 'testSignal', 10) === 0 && extension_loaded('pcntl')) {
24+
$this->resetSignalHandlers();
25+
}
26+
}
27+
1728
abstract public function createLoop();
1829

1930
public function createStream()
@@ -495,4 +506,113 @@ private function assertRunFasterThan($maxInterval)
495506

496507
$this->assertLessThan($maxInterval, $interval);
497508
}
509+
510+
public function signalProvider()
511+
{
512+
return [
513+
['SIGUSR1', SIGUSR1],
514+
['SIGHUP', SIGHUP],
515+
['SIGTERM', SIGTERM],
516+
];
517+
}
518+
519+
private $_signalHandled = false;
520+
521+
/**
522+
* Test signal interrupt when no stream is attached to the loop
523+
* @dataProvider signalProvider
524+
*/
525+
public function testSignalInterruptNoStream($sigName, $signal)
526+
{
527+
if (!extension_loaded('pcntl')) {
528+
$this->markTestSkipped('"pcntl" extension is required to run this test.');
529+
}
530+
531+
// dispatch signal handler once before signal is sent and once after
532+
$this->loop->addTimer(0.01, function() { pcntl_signal_dispatch(); });
533+
$this->loop->addTimer(0.03, function() { pcntl_signal_dispatch(); });
534+
if (defined('HHVM_VERSION')) {
535+
// hhvm startup is slow so we need to add another handler much later
536+
$this->loop->addTimer(0.5, function() { pcntl_signal_dispatch(); });
537+
}
538+
539+
$this->setUpSignalHandler($signal);
540+
541+
// spawn external process to send signal to current process id
542+
$this->forkSendSignal($signal);
543+
$this->loop->run();
544+
$this->assertTrue($this->_signalHandled);
545+
}
546+
547+
/**
548+
* Test signal interrupt when a stream is attached to the loop
549+
* @dataProvider signalProvider
550+
*/
551+
public function testSignalInterruptWithStream($sigName, $signal)
552+
{
553+
if (!extension_loaded('pcntl')) {
554+
$this->markTestSkipped('"pcntl" extension is required to run this test.');
555+
}
556+
557+
// dispatch signal handler every 10ms
558+
$this->loop->addPeriodicTimer(0.01, function() { pcntl_signal_dispatch(); });
559+
560+
// add stream to the loop
561+
list($writeStream, $readStream) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
562+
$this->loop->addReadStream($readStream, function($stream, $loop) {
563+
/** @var $loop LoopInterface */
564+
$read = fgets($stream);
565+
if ($read === "end loop\n") {
566+
$loop->stop();
567+
}
568+
});
569+
$this->loop->addTimer(0.05, function() use ($writeStream) {
570+
fwrite($writeStream, "end loop\n");
571+
});
572+
573+
$this->setUpSignalHandler($signal);
574+
575+
// spawn external process to send signal to current process id
576+
$this->forkSendSignal($signal);
577+
578+
$this->loop->run();
579+
580+
$this->assertTrue($this->_signalHandled);
581+
}
582+
583+
/**
584+
* add signal handler for signal
585+
*/
586+
protected function setUpSignalHandler($signal)
587+
{
588+
$this->_signalHandled = false;
589+
$this->assertTrue(pcntl_signal($signal, function() { $this->_signalHandled = true; }));
590+
}
591+
592+
/**
593+
* reset all signal handlers to default
594+
*/
595+
protected function resetSignalHandlers()
596+
{
597+
foreach($this->signalProvider() as $signal) {
598+
pcntl_signal($signal[1], SIG_DFL);
599+
}
600+
}
601+
602+
/**
603+
* fork child process to send signal to current process id
604+
*/
605+
protected function forkSendSignal($signal)
606+
{
607+
$currentPid = posix_getpid();
608+
$childPid = pcntl_fork();
609+
if ($childPid == -1) {
610+
$this->fail("Failed to fork child process!");
611+
} else if ($childPid === 0) {
612+
// this is executed in the child process
613+
usleep(20000);
614+
posix_kill($currentPid, $signal);
615+
die();
616+
}
617+
}
498618
}

tests/StreamSelectLoopTest.php

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,10 @@
22

33
namespace React\Tests\EventLoop;
44

5-
use React\EventLoop\LoopInterface;
65
use React\EventLoop\StreamSelectLoop;
76

87
class StreamSelectLoopTest extends AbstractLoopTest
98
{
10-
protected function tearDown()
11-
{
12-
parent::tearDown();
13-
if (strncmp($this->getName(false), 'testSignal', 10) === 0 && extension_loaded('pcntl')) {
14-
$this->resetSignalHandlers();
15-
}
16-
}
17-
189
public function createLoop()
1910
{
2011
return new StreamSelectLoop();
@@ -36,113 +27,4 @@ public function testStreamSelectTimeoutEmulation()
3627

3728
$this->assertGreaterThan(0.04, $interval);
3829
}
39-
40-
public function signalProvider()
41-
{
42-
return [
43-
['SIGUSR1', SIGUSR1],
44-
['SIGHUP', SIGHUP],
45-
['SIGTERM', SIGTERM],
46-
];
47-
}
48-
49-
private $_signalHandled = false;
50-
51-
/**
52-
* Test signal interrupt when no stream is attached to the loop
53-
* @dataProvider signalProvider
54-
*/
55-
public function testSignalInterruptNoStream($sigName, $signal)
56-
{
57-
if (!extension_loaded('pcntl')) {
58-
$this->markTestSkipped('"pcntl" extension is required to run this test.');
59-
}
60-
61-
// dispatch signal handler once before signal is sent and once after
62-
$this->loop->addTimer(0.01, function() { pcntl_signal_dispatch(); });
63-
$this->loop->addTimer(0.03, function() { pcntl_signal_dispatch(); });
64-
if (defined('HHVM_VERSION')) {
65-
// hhvm startup is slow so we need to add another handler much later
66-
$this->loop->addTimer(0.5, function() { pcntl_signal_dispatch(); });
67-
}
68-
69-
$this->setUpSignalHandler($signal);
70-
71-
// spawn external process to send signal to current process id
72-
$this->forkSendSignal($signal);
73-
$this->loop->run();
74-
$this->assertTrue($this->_signalHandled);
75-
}
76-
77-
/**
78-
* Test signal interrupt when a stream is attached to the loop
79-
* @dataProvider signalProvider
80-
*/
81-
public function testSignalInterruptWithStream($sigName, $signal)
82-
{
83-
if (!extension_loaded('pcntl')) {
84-
$this->markTestSkipped('"pcntl" extension is required to run this test.');
85-
}
86-
87-
// dispatch signal handler every 10ms
88-
$this->loop->addPeriodicTimer(0.01, function() { pcntl_signal_dispatch(); });
89-
90-
// add stream to the loop
91-
list($writeStream, $readStream) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
92-
$this->loop->addReadStream($readStream, function($stream, $loop) {
93-
/** @var $loop LoopInterface */
94-
$read = fgets($stream);
95-
if ($read === "end loop\n") {
96-
$loop->stop();
97-
}
98-
});
99-
$this->loop->addTimer(0.05, function() use ($writeStream) {
100-
fwrite($writeStream, "end loop\n");
101-
});
102-
103-
$this->setUpSignalHandler($signal);
104-
105-
// spawn external process to send signal to current process id
106-
$this->forkSendSignal($signal);
107-
108-
$this->loop->run();
109-
110-
$this->assertTrue($this->_signalHandled);
111-
}
112-
113-
/**
114-
* add signal handler for signal
115-
*/
116-
protected function setUpSignalHandler($signal)
117-
{
118-
$this->_signalHandled = false;
119-
$this->assertTrue(pcntl_signal($signal, function() { $this->_signalHandled = true; }));
120-
}
121-
122-
/**
123-
* reset all signal handlers to default
124-
*/
125-
protected function resetSignalHandlers()
126-
{
127-
foreach($this->signalProvider() as $signal) {
128-
pcntl_signal($signal[1], SIG_DFL);
129-
}
130-
}
131-
132-
/**
133-
* fork child process to send signal to current process id
134-
*/
135-
protected function forkSendSignal($signal)
136-
{
137-
$currentPid = posix_getpid();
138-
$childPid = pcntl_fork();
139-
if ($childPid == -1) {
140-
$this->fail("Failed to fork child process!");
141-
} else if ($childPid === 0) {
142-
// this is executed in the child process
143-
usleep(20000);
144-
posix_kill($currentPid, $signal);
145-
die();
146-
}
147-
}
14830
}

0 commit comments

Comments
 (0)
0