Skip to content

Commit 6577e3b

Browse files
committed
Fix failing test cases due to inaccurate timers
1 parent f331f95 commit 6577e3b

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

tests/Timer/AbstractTimerTest.php

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

25-
/**
26-
* @depends testPlatformHasHighAccuracy
27-
*/
2825
public function testAddTimerWillBeInvokedOnceAndBlocksLoopWhenRunning()
2926
{
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-
4727
$loop = $this->createLoop();
4828

49-
$loop->addTimer(0.001, $this->expectCallableOnce());
29+
$loop->addTimer(0.002, $this->expectCallableOnce());
5030

5131
$start = microtime(true);
5232
$loop->run();
5333
$end = microtime(true);
5434

35+
// 1 invocation should take 2ms (± 1ms due to timer inaccuracies)
36+
// make no strict assumptions about time interval, must at least take 1ms
37+
// and should not take longer than 0.1s for slower loops.
5538
$this->assertGreaterThanOrEqual(0.001, $end - $start);
56-
$this->assertLessThan(0.002, $end - $start);
39+
$this->assertLessThan(0.1, $end - $start);
5740
}
5841

5942
public function testAddPeriodicTimerReturnsPeriodicTimerInstance()
@@ -122,10 +105,10 @@ public function testAddPeriodicTimerCancelsItself()
122105

123106
$this->assertEquals(5, $i);
124107

125-
// make no strict assumptions about time interval.
126-
// 5 invocations must take at least 0.005s (5ms) and should not take
127-
// longer than 0.1s for slower loops.
128-
$this->assertGreaterThanOrEqual(0.005, $end - $start);
108+
// 5 invocations should take 5ms (± 1ms due to timer inaccuracies)
109+
// make no strict assumptions about time interval, must at least take 4ms
110+
// and should not take longer than 0.1s for slower loops.
111+
$this->assertGreaterThanOrEqual(0.004, $end - $start);
129112
$this->assertLessThan(0.1, $end - $start);
130113
}
131114

0 commit comments

Comments
 (0)