Fake the time.sleep/asyncio.sleep function during tests.
Project description
⏰ SleepFake
SleepFake
is a compact Python package (under 100 lines) that provides a context manager to simulate the time.sleep
and asyncio.sleep
functions during tests. This is useful for testing time-dependent code without the need to actually wait for time to pass. The real magic behind this package comes from freezegun. 🎩✨
Installation
pip install sleepfake
🚀 Usage
import asyncio
import time
from sleepfake import SleepFake
def test_example():
real_start = time.time()
with SleepFake():
start = time.time()
time.sleep(10)
end = time.time()
assert end - start == 10
real_end = time.time()
assert real_end - real_start < 1
async def test_async_example():
real_start = asyncio.get_event_loop().time()
with SleepFake():
start = asyncio.get_event_loop().time()
await asyncio.gather(asyncio.sleep(5), asyncio.sleep(5), asyncio.sleep(5))
end = asyncio.get_event_loop().time()
assert end - start <= 5.5 # almost 5 seconds # noqa: PLR2004
assert end - start >= 5 # almost 5 seconds # noqa: PLR2004
real_end = asyncio.get_event_loop().time()
assert real_end - real_start < 1 # almost 0 seconds
Local Development
Prerequisites
Install rye
curl -sSf https://rye-up.com/get | bash
Install dep
rye sync
Run tests
rye run test
Run linter
rye run lint
Acknowledgments 🙏
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sleepfake-1.0.0.tar.gz
(6.1 kB
view hashes)
Built Distribution
Close
Hashes for sleepfake-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f76ac63869b92b715191d06a4c8da3c8bb37a717af73cd69f485be0718bd19af |
|
MD5 | 67f8fd7ee01bebbca357e4a1783e8409 |
|
BLAKE2b-256 | 66426b6e8198b75f1bde22c950d96c0db2b826c4586d1f3a49a9d698856b1a57 |