Skip to content

Commit 67d845d

Browse files
authored
Merge pull request #93 from kaduev13/master
StreamSelectLoop: Fix erroneous zero-time sleep (2)
2 parents a72b80d + 09cd0bc commit 67d845d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/StreamSelectLoop.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ public function run()
159159
if ($timeout < 0) {
160160
$timeout = 0;
161161
} else {
162-
$timeout *= self::MICROSECONDS_PER_SECOND;
162+
/*
163+
* round() needed to correct float error:
164+
* https://github.com/reactphp/event-loop/issues/48
165+
*/
166+
$timeout = round($timeout * self::MICROSECONDS_PER_SECOND);
163167
}
164168

165169
// The only possible event is stream activity, so wait forever ...

tests/StreamSelectLoopTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use React\EventLoop\LoopInterface;
66
use React\EventLoop\StreamSelectLoop;
7+
use React\EventLoop\Timer\Timer;
78

89
class StreamSelectLoopTest extends AbstractLoopTest
910
{
@@ -145,4 +146,34 @@ protected function forkSendSignal($signal)
145146
die();
146147
}
147148
}
149+
150+
/**
151+
* https://github.com/reactphp/event-loop/issues/48
152+
*
153+
* Tests that timer with very small interval uses at least 1 microsecond
154+
* timeout.
155+
*/
156+
public function testSmallTimerInterval()
157+
{
158+
/** @var StreamSelectLoop|\PHPUnit_Framework_MockObject_MockObject $loop */
159+
$loop = $this->getMock('React\EventLoop\StreamSelectLoop', ['streamSelect']);
160+
$loop
161+
->expects($this->at(0))
162+
->method('streamSelect')
163+
->with([], [], 1);
164+
$loop
165+
->expects($this->at(1))
166+
->method('streamSelect')
167+
->with([], [], 0);
168+
169+
$callsCount = 0;
170+
$loop->addPeriodicTimer(Timer::MIN_INTERVAL, function() use (&$loop, &$callsCount) {
171+
$callsCount++;
172+
if ($callsCount == 2) {
173+
$loop->stop();
174+
}
175+
});
176+
177+
$loop->run();
178+
}
148179
}

0 commit comments

Comments
 (0)