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

Add parameter to increase speed of time when tick=True #512

Open
Samreay opened this issue Sep 18, 2023 · 0 comments
Open

Add parameter to increase speed of time when tick=True #512

Samreay opened this issue Sep 18, 2023 · 0 comments

Comments

@Samreay
Copy link

Samreay commented Sep 18, 2023

Hi all,

I've got a fun use case to see if there's appetitate for me to raise a PR about. When using tick in my tests, I would love to be able to scroll through time at a faster rate as I have some mocked services which are set to emit events over the course of a day, and I'd love to not mock those services and simply have time progress at a faster rate when invoking datetime.now()

I believe the change itself would be fairly simple in the form of a new kwarg like tick_speed=1.0 when invoking freezegun, and then updating:

class TickingDateTimeFactory:
    def __init__(self, time_to_freeze, start, tick_speed):
        self.time_to_freeze = time_to_freeze
        self.start = start
        self.tick_speed = tick_speed

    def __call__(self):
        return self.time_to_freeze + (real_datetime.now() - self.start) * self.tick_speed

Are there any subtle caveats though which may make this not viable, before I throw some code together? The most glaring one I can think of is doing this may mean wanting to also mock time.sleep and its asyncio version, which may not play nice with various use cases like sleeping while something starts or initialises, but given the auto_tick_increment parameter effectively allows some scrolling through in, I feel it may be acceptable to say that this "does not cover time.sleep" and other timers (like thread.Timer)

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