Fast-forward asyncio event loop time (in tests)
Project description
Fast-forward asyncio event loop time (in tests)
What is this?
Fake the flow of time in asyncio event loops. The effects of time removal can be seen from both sides:
-
From the event loop's (i.e. your tests') point of view, all external activities, such as synchronous executor calls (thread pools) and i/o with sockets, servers, files, happen in zero amount of the loop time — even if it takes some real time. This hides the code overhead and network latencies from the time measurements, making the loop time sharply and predictably advancing in configured steps.
-
From the observer's (i.e. your personal) point of view, all activities of the event loop, such as sleeps, events/conditions waits, timeouts, "later" callbacks, happen in near-zero amount of the real time (due to the usual code execution overhead). This speeds up the execution of tests without breaking the tests' time-based design, even if they are designed to run in seconds or minutes.
For the latter case, there are a few exceptions when the event loop's activities are synced with the true-time external activities, such as thread pools or i/o, so that they spend the real time above the usual code overhead (if configured).
The library was originally developed for Kopf, a framework for Kubernetes Operators in Python, which actively uses asyncio tests in pytest (≈7000 unit-tests in ≈2 minutes). You can see how this library changes and simplifies the tests in Kopf's PR #881.
What is it not?
It speeds up tests based on the flow of time, in particular various kinds of timers, timeouts, sleeps, delays, rate limiters — both in tests and in the system under test.
It does NOT speed up tests that are simply slow with no explicit delays, such as those involving the local/loopback network communication, heavy algorithmical compute, slow data moving or processing, etc. These activities take their fair time and cannot be time-compacted.
It does NOT speed up time-based tests using the synchronous primitives
and the wall-clock time; looptime compacts only the asyncio time.
Examples
Here, we assume that the async tests are supported. For example,
use pytest-asyncio:
pip install pytest-asyncio
pip install looptime
Nothing is needed to make async tests run with the fake time, it just works:
import asyncio
import pytest
@pytest.mark.asyncio
async def test_me():
await asyncio.sleep(100)
assert asyncio.get_running_loop().time() == 100
pytest --looptime
The test will be executed in approximately 0.01 seconds, while the event loop believes it is 100 seconds old.
If the command line or ini-file options for all tests is not desirable, individual tests can be marked for fast time forwarding explicitly:
import asyncio
import pytest
@pytest.mark.asyncio
@pytest.mark.looptime
async def test_me():
await asyncio.sleep(100)
assert asyncio.get_running_loop().time() == 100
pytest
Under the hood, the library solves some nuanced situations with time in tests. See "Nuances" below for more complicated (and nuanced) examples.
Documentation
For more tricks and options, see the full documentation.
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
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 looptime-0.7.tar.gz.
File metadata
- Download URL: looptime-0.7.tar.gz
- Upload date:
- Size: 38.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b32eab62d2f11af9d2322a0d120054a38b9eb9380383a427a68155cf34963c4
|
|
| MD5 |
bb016f57df77a1786684b5b5f211ae17
|
|
| BLAKE2b-256 |
d63e74b54612606b87eedd21820517d206c370888fea8c66d93c05f1f4d4388e
|
Provenance
The following attestation bundles were made for looptime-0.7.tar.gz:
Publisher:
publish.yaml on nolar/looptime
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
looptime-0.7.tar.gz -
Subject digest:
6b32eab62d2f11af9d2322a0d120054a38b9eb9380383a427a68155cf34963c4 - Sigstore transparency entry: 789560369
- Sigstore integration time:
-
Permalink:
nolar/looptime@7d5430281544b02ea4a27d962efb128cb7eea180 -
Branch / Tag:
refs/tags/0.7 - Owner: https://github.com/nolar
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@7d5430281544b02ea4a27d962efb128cb7eea180 -
Trigger Event:
release
-
Statement type:
File details
Details for the file looptime-0.7-py3-none-any.whl.
File metadata
- Download URL: looptime-0.7-py3-none-any.whl
- Upload date:
- Size: 21.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efb3916196dbbe943a7f2853d79647e4476a5dd149306ce2736607d814930327
|
|
| MD5 |
2c00958a04a4285998c9c942d865bfce
|
|
| BLAKE2b-256 |
243c2bec447bf2486dfe8ed9c99d01892d57388b3620883d2374b975f36f1963
|
Provenance
The following attestation bundles were made for looptime-0.7-py3-none-any.whl:
Publisher:
publish.yaml on nolar/looptime
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
looptime-0.7-py3-none-any.whl -
Subject digest:
efb3916196dbbe943a7f2853d79647e4476a5dd149306ce2736607d814930327 - Sigstore transparency entry: 789560371
- Sigstore integration time:
-
Permalink:
nolar/looptime@7d5430281544b02ea4a27d962efb128cb7eea180 -
Branch / Tag:
refs/tags/0.7 - Owner: https://github.com/nolar
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@7d5430281544b02ea4a27d962efb128cb7eea180 -
Trigger Event:
release
-
Statement type: