Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/StreamSelectLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ public function run()
if ($timeout < 0) {
$timeout = 0;
} else {
$timeout *= self::MICROSECONDS_PER_SECOND;
/*
* round() needed to correct float error:
* https://github.com/reactphp/event-loop/issues/48
*/
$timeout = round($timeout * self::MICROSECONDS_PER_SECOND);
}

// The only possible event is stream activity, so wait forever ...
Expand Down
31 changes: 31 additions & 0 deletions tests/StreamSelectLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use React\EventLoop\LoopInterface;
use React\EventLoop\StreamSelectLoop;
use React\EventLoop\Timer\Timer;

class StreamSelectLoopTest extends AbstractLoopTest
{
Expand Down Expand Up @@ -145,4 +146,34 @@ protected function forkSendSignal($signal)
die();
}
}

/**
* https://github.com/reactphp/event-loop/issues/48
*
* Tests that timer with very small interval uses at least 1 microsecond
* timeout.
*/
public function testSmallTimerInterval()
{
/** @var StreamSelectLoop|\PHPUnit_Framework_MockObject_MockObject $loop */
$loop = $this->getMock('React\EventLoop\StreamSelectLoop', ['streamSelect']);
$loop
->expects($this->at(0))
->method('streamSelect')
->with([], [], 1);
$loop
->expects($this->at(1))
->method('streamSelect')
->with([], [], 0);

$callsCount = 0;
$loop->addPeriodicTimer(Timer::MIN_INTERVAL, function() use (&$loop, &$callsCount) {
$callsCount++;
if ($callsCount == 2) {
$loop->stop();
}
});

$loop->run();
}
}