10000 Merge pull request #94 from jsor-labs/backport-93 · reactphp/event-loop@47fe726 · GitHub
[go: up one dir, main page]

Skip to content

Commit 47fe726

Browse files
authored
Merge pull request #94 from jsor-labs/backport-93
StreamSelectLoop: Fix erroneous zero-time sleep (backport to 0.4)
2 parents 9504651 + 7d5fb96 commit 47fe726

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
@@ -186,7 +186,11 @@ public function run()
186186
if ($timeout < 0) {
187187
$timeout = 0;
188188
} else {
189-
$timeout *= self::MICROSECONDS_PER_SECOND;
189+
/*
190+
* round() needed to correct float error:
191+
* https://github.com/reactphp/event-loop/issues/48
192+
*/
193+
$timeout = round($timeout * self::MICROSECONDS_PER_SECOND);
190194
}
191195

192196
// 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)
0