Skip to content

Commit f84ddde

Browse files
authored
Merge pull request #243 from lucasnetau/master
Fix periodic timer with zero interval for `ExtEvLoop` and legacy `ExtLibevLoop`
2 parents e69a8bd + 8e6d223 commit f84ddde

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/ExtEvLoop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function addPeriodicTimer($interval, $callback)
162162
\call_user_func($timer->getCallback(), $timer);
163163
};
164164

165-
$event = $this->loop->timer($interval, $interval, $callback);
165+
$event = $this->loop->timer($timer->getInterval(), $timer->getInterval(), $callback);
166166
$this->timers->attach($timer, $event);
167167

168168
return $timer;

src/ExtLibevLoop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function addPeriodicTimer($interval, $callback)
132132
\call_user_func($timer->getCallback(), $timer);
133133
};
134134

135-
$event = new TimerEvent($callback, $interval, $interval);
135+
$event = new TimerEvent($callback, $timer->getInterval(), $timer->getInterval());
136136
$this->timerEvents->attach($timer, $event);
137137
$this->loop->add($event);
138138

tests/Timer/AbstractTimerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,26 @@ public function testMinimumIntervalOneMicrosecond()
121121
$this->assertEquals(0.000001, $timer->getInterval());
122122
}
123123

124+
public function testAddPeriodicTimerWithZeroIntervalWillExecuteCallbackFunctionAtLeastTwice()
125+
{
126+
$loop = $this->createLoop();
127+
128+
$timeout = $loop->addTimer(2, $this->expectCallableNever()); //Timeout the test after two seconds if the periodic timer hasn't fired twice
129+
130+
$i = 0;
131+
$loop->addPeriodicTimer(0, function ($timer) use (&$i, $loop, $timeout) {
132+
++$i;
133+
if ($i === 2) {
134+
$loop->cancelTimer($timer);
135+
$loop->cancelTimer($timeout);
136+
}
137+
});
138+
139+
$loop->run();
140+
141+
$this->assertEquals(2, $i);
142+
}
143+
124144
public function testTimerIntervalBelowZeroRunsImmediately()
125145
{
126146
$loop = $this->createLoop();

0 commit comments

Comments
 (0)