spulec / freezegun

Let your Python tests travel through time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add parameter to increase speed of time when tick=True

Samreay opened this issue · comments

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)