Skip to content

Commit dbdf527

Browse files
committed
Skip signal tests w/o PCNTL and skip timer tests on inaccurate platforms
The signal constants are only available when ext-pcntl is loaded. If it is not loaded, simply skip all tests that reference these constants. Before running a timer test and asserting its time interval, run a simple test to check the accuracy on the current platform. This is known to work on most common platforms, but will now avoid false negative test results on platforms that are known to be slow (Travis CI and others).
1 parent c795e50 commit dbdf527

File tree

6 files changed

+40
-22
lines changed

6 files changed

+40
-22
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
processIsolation="false"
1010
stopOnFailure="false"
1111
syntaxCheck="false"
12-
bootstrap="tests/bootstrap.php"
12+
bootstrap="vendor/autoload.php"
1313
>
1414
<testsuites>
1515
<testsuite name="React Test Suite">

tests/AbstractLoopTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,13 @@ function () {
491491

492492
public function testRemoveSignalNotRegisteredIsNoOp()
493493
{
494-
$this->loop->removeSignal(SIGINT, function () { });
494+
$this->loop->removeSignal(2, function () { });
495495
$this->assertTrue(true);
496496
}
497497

498+
/**
499+
* @requires extension pcntl
500+
*/
498501
public function testSignal()
499502
{
500503
if (!function_exists('posix_kill') || !function_exists('posix_getpid')) {
@@ -528,6 +531,9 @@ public function testSignal()
528531
$this->assertTrue($calledShouldNot);
529532
}
530533

534+
/**
535+
* @requires extension pcntl
536+
*/
531537
public function testSignalMultipleUsagesForTheSameListener()
532538
{
533539
$funcCallCount = 0;
@@ -552,6 +558,9 @@ public function testSignalMultipleUsagesForTheSameListener()
552558
$this->assertSame(1, $funcCallCount);
553559
}
554560

561+
/**
562+
* @requires extension pcntl
563+
*/
555564
public function testSignalsKeepTheLoopRunning()
556565
{
557566
$loop = $this->loop;
@@ -565,6 +574,9 @@ public function testSignalsKeepTheLoopRunning()
565574
$this->assertRunSlowerThan(1.5);
566575
}
567576

577+
/**
578+
* @requires extension pcntl
579+
*/
568580
public function testSignalsKeepTheLoopRunningAndRemovingItStopsTheLoop()
569581
{
570582
$loop = $this->loop;

tests/SignalsHandlerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
final class SignalsHandlerTest extends TestCase
88
{
9+
/**
10+
* @requires extension pcntl
11+
*/
912
public function testEmittedEventsAndCallHandling()
1013
{
1114
$callCount = 0;

tests/StreamSelectLoopTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,10 @@ public function signalProvider()
4949
/**
5050
* Test signal interrupt when no stream is attached to the loop
5151
* @dataProvider signalProvider
52+
* @requires extension pcntl
5253
*/
5354
public function testSignalInterruptNoStream($signal)
5455
{
55-
if (!extension_loaded('pcntl')) {
56-
$this->markTestSkipped('"pcntl" extension is required to run this test.');
57-
}
58-
5956
// dispatch signal handler every 10ms for 0.1s
6057
$check = $this->loop->addPeriodicTimer(0.01, function() {
6158
pcntl_signal_dispatch();
@@ -80,13 +77,10 @@ public function testSignalInterruptNoStream($signal)
8077
/**
8178
* Test signal interrupt when a stream is attached to the loop
8279
* @dataProvider signalProvider
80+
* @requires extension pcntl
8381
*/
8482
public function testSignalInterruptWithStream($signal)
8583
{
86-
if (!extension_loaded('pcntl')) {
87-
$this->markTestSkipped('"pcntl" extension is required to run this test.');
88-
}
89-
9084
// dispatch signal handler every 10ms
9185
$this->loop->addPeriodicTimer(0.01, function() {
9286
pcntl_signal_dispatch();

tests/Timer/AbstractTimerTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,28 @@ public function testAddTimerReturnsNonPeriodicTimerInstance()
2222
$this->assertFalse($timer->isPeriodic());
2323
}
2424

25+
/**
26+
* @depends testPlatformHasHighAccuracy
27+
*/
2528
public function testAddTimerWillBeInvokedOnceAndBlocksLoopWhenRunning()
2629
{
30+
// Make no strict assumptions about actual time interval. Common
31+
// environments usually provide millisecond accuracy (or better), but
32+
// Travis and other CI systems are slow.
33+
// We try to compensate for this by skipping accurate tests when the
34+
// current platform is known to be inaccurate. We test this by sleeping
35+
// 3x1ms and then measure the time for each iteration before running the
36+
// actual test.
37+
for ($i = 0; $i < 3; ++$i) {
38+
$start = microtime(true);
39+
usleep(1000);
40+
$time = microtime(true) - $start;
41+
42+
if ($time < 0.001 || $time > 0.002) {
43+
$this->markTestSkipped('Platform provides insufficient accuracy (' . $time . ' s)');
44+
}
45+
}
46+
2747
$loop = $this->createLoop();
2848

2949
$loop->addTimer(0.001, $this->expectCallableOnce());
@@ -32,10 +52,8 @@ public function testAddTimerWillBeInvokedOnceAndBlocksLoopWhenRunning()
3252
$loop->run();
3353
$end = microtime(true);
3454

35-
// make no strict assumptions about actual time interval.
36-
// must be at least 0.001s (1ms) and should not take longer than 0.1s
3755
$this->assertGreaterThanOrEqual(0.001, $end - $start);
38-
$this->assertLessThan(0.1, $end - $start);
56+
$this->assertLessThan(0.002, $end - $start);
3957
}
4058

4159
public function testAddPeriodicTimerReturnsPeriodicTimerInstance()

tests/bootstrap.php

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

0 commit comments

Comments
 (0)