Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix periodic timer with zero interval for ExtEvLoop and legacy ExtLibevLoop #243

Merged
merged 1 commit into from
Feb 20, 2022
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
2 changes: 1 addition & 1 deletion src/ExtEvLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/ExtLibevLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
20 changes: 20 additions & 0 deletions tests/Timer/AbstractTimerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down