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();