lucasnetau · GitHub

The test looks fine but I think GitHub actions can't handle the speed of this interval (maybe only executes it once every millisecond, thus only increasing $i once)

The thing is: If I add an 0 as $interval for my periodicTimer and increase a value every time (like in your test) I would expect that in an ideal execution this value would hit a number near infinity (theoretically). What I want to say with this is that you can't say what number exactly will come out in the end, you can only guess the range and it depends on the system you're executing the code. This makes testing this actually pretty hard and unstable.

It's definitely the wrong behavior when passing a 0 as an interval and only getting one increase out. We may can't guess the exact number that will come out but we know it has to be higher than 1. With that said I wrote a little test myself:

public function testAddPeriodicTimerWithZeroIntervalWillExecuteCallbackFunctionAtLeastTwice()
    {
        $loop = $this->createLoop();
        $i = 0;
        $periodic = $loop->addPeriodicTimer(0, function ($periodic) use (&$i, $loop) {
            ++$i;
            if ($i === 2) {
                $loop->cancelTimer($periodic);
            }
        });
        $loop->run();
        $this->assertEquals(2, $i);
    }

I tested this and it worked for ExtEvLoop but failed for ExtLibevLoop (because old PHP uses ExtLibev). That means you have to add the same fix into the addPeriodicTimer() function inside ExtLibevLoop.php.

Read the original on github.com ↗