Test helper for controlling the asyncio event loop's internal clock
Project description
aiotime
An asyncio test helper that allows you to deterministically control the event loop's internal clock in your tests, affecting the behavior of the following functions:
asyncio.sleeploop.call_atloop.call_later
Note about behavior
If you enter the aiotime.FastForward context manager (as in the with block in the examples below), then the loop supplied to its constructor will STOP triggering scheduled events, tasks or callbacks. Once you __enter__ the context manager, you MUST call the returned object or the event loop will be stuck in time. Only when you __exit__ the context manager will the loop return to normal behavior.
(Using the same event loop with and without aiotime control is not supported; there may be unexpected effects with scheduling at the margins.)
Getting started
# TODO Add to pypi
Controlling asyncio.sleep
loop = asyncio.get_event_loop()
# Try sleeping with normal loop behavior
start = dt.datetime.now()
sleep_task = asyncio.create_task(asyncio.sleep(0.25))
await sleep_task
assert dt.datetime.now() - start > dt.timedelta(seconds=0.25)
with aiotime.FastForward(loop) as ff:
# Try fast-forwarding through the sleep
start = dt.datetime.now()
sleep_task = asyncio.create_task(asyncio.sleep(0.25))
await ff(1.5) # ff more than necessary
await sleep_task
assert dt.datetime.now() - start < dt.timedelta(seconds=0.05)
Controlling loop.call_later
loop = asyncio.get_event_loop()
with aiotime.FastForward(loop) as ff:
# Try call_later() with fast-forwarding
start = dt.datetime.now()
event = asyncio.Event()
def test():
event.set()
loop.call_later(0.25, test)
await ff(1.5) # ff more than necessary
await asyncio.wait_for(event.wait(), 2) # timeout just in case
assert dt.datetime.now() - start < dt.timedelta(seconds=0.05)
# call_later() with normal loop behavior now, after context manager exits
start = dt.datetime.now()
event = asyncio.Event()
def test():
event.set()
loop.call_later(0.25, test)
await asyncio.wait_for(event.wait(), 1) # timeout just in case
assert dt.datetime.now() - start > dt.timedelta(seconds=0.25)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiotime-0.1.0.tar.gz.
File metadata
- Download URL: aiotime-0.1.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35b260a63bf05d32733215b7c348511968d6ac8264d0ca2a05a00a3d277d1cfc
|
|
| MD5 |
f63aa8b6fcdbae77844aa1f0ca22e4ac
|
|
| BLAKE2b-256 |
6884df8ceeb6bc114899550b79307bb4f8805d626e696a39d6a933a38b6f8d98
|
File details
Details for the file aiotime-0.1.0-py3-none-any.whl.
File metadata
- Download URL: aiotime-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45e491eb484c9de44064b882988dda161aadd825d0f8ac6a42425fe987a0fe91
|
|
| MD5 |
7f45dfac33eef2d45873d01d626e6ffa
|
|
| BLAKE2b-256 |
495f7cb0f8fed2c32d59bc3a807c01766a019436eb68ebcbf66ca1af33056ea9
|