You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be useful if test code could check the current frozen time configuration (e.g. is time currently frozen, what packages are ignored, is real_asyncio set or not). Currently it can only be done by introspecting internal details.
For example, I just implemented a assert_with_retry helper function which waits for a condition to become true (when a background asyncio task is performing some operation). It took me a long time to realize freeze_time was causing asyncio.sleep to lock up, causing tests to freeze up. I wanted to avoid the pain from other developers, so I started looking at how can I detect frozen time without real_asyncio=True option. I ended up writing the functions below (which are way less than ideal) and having assert_with_retry raise an exception with clear instructions in case it was called with frozen time without real_asyncio. It would have been great to have a stable API from which to check them.
importasyncioimportfreezegundefis_time_frozen() ->bool:
"""Check whether time is currently frozen."""returnlen(freezegun.api.freeze_factories) >0_cached_event_loop_class: type[asyncio.AbstractEventLoop] |None=Nonedefis_time_frozen_with_real_asyncio() ->bool:
global_cached_event_loop_class# noqa: PLW0603if_cached_event_loop_classisNone:
event_loop=asyncio.new_event_loop()
event_loop.close()
_cached_event_loop_class=type(event_loop)
s=repr(_cached_event_loop_class.time)
# Freezegun sets the event loop .time method to a lambda function which uses# the real tick method. This is detected based on the method name "freeze_time"# being in the function repr. The file test_freezegun_helper.py includes tests# so this does not break during a library upgrade.return"freeze_time"ins
The text was updated successfully, but these errors were encountered:
It would be useful if test code could check the current frozen time configuration (e.g. is time currently frozen, what packages are ignored, is
real_asyncio
set or not). Currently it can only be done by introspecting internal details.For example, I just implemented a
assert_with_retry
helper function which waits for a condition to become true (when a background asyncio task is performing some operation). It took me a long time to realizefreeze_time
was causingasyncio.sleep
to lock up, causing tests to freeze up. I wanted to avoid the pain from other developers, so I started looking at how can I detect frozen time withoutreal_asyncio=True
option. I ended up writing the functions below (which are way less than ideal) and havingassert_with_retry
raise an exception with clear instructions in case it was called with frozen time withoutreal_asyncio
. It would have been great to have a stable API from which to check them.The text was updated successfully, but these errors were encountered: