From c29941dc4687cca845834fc7df917330e623a15c Mon Sep 17 00:00:00 2001 From: Ivan Kalita Date: Mon, 3 Apr 2017 22:47:39 +0300 Subject: [PATCH 1/2] Add StreamSelectLoop test for timer with small interval. --- tests/StreamSelectLoopTest.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/StreamSelectLoopTest.php b/tests/StreamSelectLoopTest.php index 5cb957b7..d2e3e078 100644 --- a/tests/StreamSelectLoopTest.php +++ b/tests/StreamSelectLoopTest.php @@ -4,6 +4,7 @@ use React\EventLoop\LoopInterface; use React\EventLoop\StreamSelectLoop; +use React\EventLoop\Timer\Timer; class StreamSelectLoopTest extends AbstractLoopTest { @@ -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(); + } } From 7d5fb96b32b2cf942b9e9680edda17e791fabd33 Mon Sep 17 00:00:00 2001 From: Ivan Kalita Date: Mon, 3 Apr 2017 23:10:33 +0300 Subject: [PATCH 2/2] Add StreamSelectLoop timeout rounding. --- src/StreamSelectLoop.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/StreamSelectLoop.php b/src/StreamSelectLoop.php index e51a27f8..696b05ef 100644 --- a/src/StreamSelectLoop.php +++ b/src/StreamSelectLoop.php @@ -186,7 +186,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 ... 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