Comments (11)
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.
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.
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.
That seems reasonable to me. I would accept a pull request with that as a default-off parameter.
from freezegun.
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.
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.
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.
@jdunck that sounds good. I think it would actually make a lot of things easier.
from freezegun.
A freezegun.warp(seconds)
would be awesome! 👍
from freezegun.
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.
Going to close in favor of #142
from freezegun.
Related Issues (20)
- Add parameter to increase speed of time when tick=True
- `time_to_freeze.today()` returns wrong time and type if TZ is not 'UTC' in env vars HOT 2
- 'FakeDatetime' object cannot be interpreted as an integer
- Timezone America/Sao_Paulo triggers recursion errors in tests. HOT 3
- Maintenance plans HOT 3
- 1.3.0: `freezegun/__init__.py` is missing new version number HOT 1
- [Discussion] about freezing asyncio HOT 7
- ``PermissionError: [Errno 1] Operation not permitted`` appears when upgrading to ``1.3.0`` HOT 7
- Freezegun doesn't mock pointers to date functions HOT 1
- Tag for 1.2.2 is missing HOT 1
- Python 3.12: Deprecation warnings related to datetime.datetime.utcfromtimestamp HOT 2
- Time is not freezed when using `uvloop` event loop
- unsupported operand type(s) for +: 'FakeDatetime' and 'FakeDatetime'
- Datetime and Time packages do not have the same behavior when being frozen HOT 4
- timezone free datetime.datetime.fromtimestamp returns original datetime.datetime HOT 1
- Adding a return value to tick()
- Could have some problem with Django with TIME_ZONE setting
- Test regressions on Python 3.13.0b1
- `FakeDatetime`'s `timestamp()` is not consistent with `datetime().timestamp()` HOT 1
- consider overriding == on FakeDatetimeMeta HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from freezegun.