Skip to content

Commit 858b18a

Browse files
committed
Fix failing test cases due to inaccurate timers
1 parent f331f95 commit 858b18a

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

tests/Timer/AbstractTimerTest.php

Lines changed: 15 additions & 32 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()
@@ -90,17 +73,17 @@ public function testAddPeriodicTimerWillBeInvokedWithMaximumAccuracyUntilItIsCan
9073
++$i;
9174
});
9275

93-
$loop->addTimer(0.02, function () use ($loop, $periodic) {
76+
$loop->addTimer(0.1, function () use ($loop, $periodic) {
9477
$loop->cancelTimer($periodic);
9578
});
9679

9780
$loop->run();
9881

9982
// make no strict assumptions about number of invocations.
100-
// we know it must be no more than 20 times and should at least be
101-
// invoked twice for really slow loops
102-
$this->assertLessThanOrEqual(20, $i);
103-
$this->assertGreaterThan(2, $i);
83+
// we know it must be no more than 100 times and should at least be
84+
// invoked 4 times for really slow loops
85+
$this->assertLessThanOrEqual(100, $i);
86+
$this->assertGreaterThanOrEqual(4, $i);
10487
}
10588

10689
public function testAddPeriodicTimerCancelsItself()
@@ -122,11 +105,11 @@ 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);
129-
$this->assertLessThan(0.1, $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.2s for slower loops.
111+
$this->assertGreaterThanOrEqual(0.004, $end - $start);
112+
$this->assertLessThan(0.2, $end - $start);
130113
}
131114

132115
public function testMinimumIntervalOneMicrosecond()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy