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

FG incorrectly causes monotonic time to go back #556

Open
christopherlb opened this issue Aug 13, 2024 · 0 comments
Open

FG incorrectly causes monotonic time to go back #556

christopherlb opened this issue Aug 13, 2024 · 0 comments

Comments

@christopherlb
Copy link

The Python time.monotonic() clock is, by definition, a clock that cannot go back ( https://docs.python.org/3/library/time.html#time.monotonic ).

In other words, monotonic time can move forwards or stand still but never decrease. This is different to a wall clock (datetime.now()), which can go back due to manual adjustment or daylight savings etc.

Freezegun violates this concept, as causing the clock to travel back in time causes monotonic time to decrease.

This is illustrated by a failure in the following test:

    def test_time_monotonic(self) -> None:
        freezer = freeze_time("2012")
        freezer.start()
        start = time.monotonic()
        freezer.stop()

        # Simulate the system (wallclock) being set back
        freezer = freeze_time("2011")
        freezer.start()
        later = time.monotonic()
        freezer.stop()

        # Despite the system clock being moved back, monotonic time MUST never decrease.
        # It can "move forward" by 0 seconds, or more, but never back.
        assert later >= start

This test also fails if you freezetime.tick(negative_number), or seemingly any function that relies on this.

Essentially, freezegun needs to maintain two clock sources. It seems to only maintain one (time_to_freeze).

As the wall clock time (returned by e.g datetime.now()) is allowed to vary, both forwards and back, the clock source for time.monotonic() should discard negative updates. It's a design decision as to whether the test-developer needs to move both times independently with two freezegun function calls, or whether this is handled in the library.

The ability to fast forward monotonic time is an essential ability, as this is the preferred way of implementing timers (as opposed to alarms). If you can't fast forward this clock, you would need to sit and wait in test.

@christopherlb christopherlb changed the title FG allows monotonic time to go back FG incorrectly causes monotonic time to go back Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant