Git Product home page Git Product logo

Comments (11)

vartec avatar vartec commented on June 25, 2024 9

I've worked around the issue by doing this:

    @mock.patch('time.sleep')
    def test_timeout(self, mock_sleep):
        with freeze_time() as frozen_time:
            mock_sleep.side_effect = lambda seconds: frozen_time.tick(timedelta(seconds=seconds))
            ...

from freezegun.

spulec avatar spulec commented on June 25, 2024

I like the idea, but am a bit worried about backwards compatibility. Maybe a parameter to pass to the decorator.

My biggest concern is that it could cause harm to the 90% of users that wouldn't use the feature (imagine they are using some third party library that decides to call sleep()). With a parameter though, I think it would work.

from freezegun.

adamchainz avatar adamchainz commented on June 25, 2024

I'd like an option for time to generally keep ticking after freezing, so that two now() calls surrounding a long computation can get the actual difference rather than 0 seconds. I'm not using freezegun for tests though so much as re-running a computation as if it were a different day.

from freezegun.

spulec avatar spulec commented on June 25, 2024

That seems reasonable to me. I would accept a pull request with that as a default-off parameter.

from freezegun.

benkuhn avatar benkuhn commented on June 25, 2024

I would be interested in implementing a mock for sleep--we have some integration tests that I'd love to be able to run sleep during rather than routing our code around the sleep calls when under test.

Just want to check how to do this before I dive in--the right thing to do here is to create a FakeSleep class analogous to FakeStrfTime, have its call method move forward the time_to_freeze variable of the factory, and (if the parameter is supplied) patch it in everywhere the FakeStrfTime class is patched in?

from freezegun.

spulec avatar spulec commented on June 25, 2024

I don't think that should require any changes to freezegun to make work. If you do the Raw use, then patch sleep to increment the freezer.time_to_freeze.

from freezegun.

jdunck avatar jdunck commented on June 25, 2024

I would argue for a back-channel way to advance time. (Maybe also time.sleep should advance, but there should be a test-specific way to advance time as well.) As an example, freezegun.warp(seconds).

@spulec I started a local branch to support time.sleep/freezegun.warp, but immediately ran into the fact that FakeTime is constructed with an actual time_to_freeze rather than a reference to the possibly-nested times_to_freeze. What do you think about moving that FakeDateTime.times_to_freeze out to a thread-local stack that start/stop push and pop from?

from freezegun.

spulec avatar spulec commented on June 25, 2024

@jdunck that sounds good. I think it would actually make a lot of things easier.

from freezegun.

arnuschky avatar arnuschky commented on June 25, 2024

A freezegun.warp(seconds) would be awesome! 👍

from freezegun.

arnuschky avatar arnuschky commented on June 25, 2024

What I currently use as an intermediary (ugly) hack:

        now = datetime.now()
        # do something

        now += timedelta(seconds=0.3)
        with freeze_time(now):
             # do something 0.3 secs later

        now += timedelta(seconds=0.4)
        with freeze_time(now):
             # do something 0.4 secs later again

from freezegun.

spulec avatar spulec commented on June 25, 2024

Going to close in favor of #142

from freezegun.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.