Skip to content

CallbackClock returns value at the time of the previous generation

If callable in the CallbackClock is a generator and current time/context dependent, you can see that the generation happens on the previous iteration

How to reproduce:

<?php

use Arokettu\Clock\CallbackClock;

$clock = new CallbackClock(function () {
    while (true) {
        yield new DateTimeImmutable();
    }
});

for ($i = 0; $i < 10; $i++) {
    echo
        $clock->now()->format('i:s.u'),
        "\t",
        (new DateTimeImmutable())->format('i:s.u'),
        PHP_EOL;
    sleep(2);
}

Expected example:

10:36.024583    10:36.024599
10:38.024780    10:38.024833
10:40.024997    10:40.025050
10:42.025338    10:42.025393

Actual example:

00:44.544147    00:44.544178
00:44.544157    00:46.544385
00:46.544362    00:48.544637
00:48.544608    00:50.544878

Writing it here just so I won't explain in the changelog