From 8e6d223553d4a3170695d2859a07b62a1d078139 Mon Sep 17 00:00:00 2001 From: James Lucas Date: Tue, 15 Feb 2022 22:07:59 +1100 Subject: [PATCH] When initialising a periodic EvTimer, the after and repeat parameters should take the adjusted interval value from Timer. This takes into account any MIN_INTERVAL bounds are applied when initialising the Timer --- src/ExtEvLoop.php | 2 +- src/ExtLibevLoop.php | 2 +- tests/Timer/AbstractTimerTest.php | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/ExtEvLoop.php b/src/ExtEvLoop.php index 7fcc29af..e823c37e 100644 --- a/src/ExtEvLoop.php +++ b/src/ExtEvLoop.php @@ -162,7 +162,7 @@ public function addPeriodicTimer($interval, $callback) \call_user_func($timer->getCallback(), $timer); }; - $event = $this->loop->timer($interval, $interval, $callback); + $event = $this->loop->timer($timer->getInterval(), $timer->getInterval(), $callback); $this->timers->attach($timer, $event); return $timer; diff --git a/src/ExtLibevLoop.php b/src/ExtLibevLoop.php index 2cf1ad54..c303fdd5 100644 --- a/src/ExtLibevLoop.php +++ b/src/ExtLibevLoop.php @@ -132,7 +132,7 @@ public function addPeriodicTimer($interval, $callback) \call_user_func($timer->getCallback(), $timer); }; - $event = new TimerEvent($callback, $interval, $interval); + $event = new TimerEvent($callback, $timer->getInterval(), $timer->getInterval()); $this->timerEvents->attach($timer, $event); $this->loop->add($event); diff --git a/tests/Timer/AbstractTimerTest.php b/tests/Timer/AbstractTimerTest.php index c5198385..bbea46f8 100644 --- a/tests/Timer/AbstractTimerTest.php +++ b/tests/Timer/AbstractTimerTest.php @@ -121,6 +121,26 @@ public function testMinimumIntervalOneMicrosecond() $this->assertEquals(0.000001, $timer->getInterval()); } + public function testAddPeriodicTimerWithZeroIntervalWillExecuteCallbackFunctionAtLeastTwice() + { + $loop = $this->createLoop(); + + $timeout = $loop->addTimer(2, $this->expectCallableNever()); //Timeout the test after two seconds if the periodic timer hasn't fired twice + + $i = 0; + $loop->addPeriodicTimer(0, function ($timer) use (&$i, $loop, $timeout) { + ++$i; + if ($i === 2) { + $loop->cancelTimer($timer); + $loop->cancelTimer($timeout); + } + }); + + $loop->run(); + + $this->assertEquals(2, $i); + } + public function testTimerIntervalBelowZeroRunsImmediately() { $loop = $this->createLoop();