Test Event Loop¶
To facilitate event loop agnostic features, asyncstdlib includes
its own custom event loop implementation for testing.
This is provided as a simple decorator that is compatible with pytest,
as well as a number of async commands specific to the event loop.
Event Loops¶
The test event loop is available via a decorator that should be directly applied
to an async def test case.
- sync(test_case: (...) -> (await) None) -> (...) None[source]¶
Mark an
async deftest case to be run synchronously with childrenThis provides a primitive “event loop” which only responds to
Schedule,SwitchandLock.It should be applied as a decorator on an
async deffunction, which is then turned into a synchronous callable that will run theasync deffunction and all tasks it spawns. Other decorators, most prominentlypytest.mark.parametrize(), can be applied around it.
Async commands¶
- class Schedule(*await Any)[source]¶
Signal to the event loop to adopt and run new coroutines
- Parameters:
coros – The coroutines to start running
In order to communicate with the event loop and start the coroutines, the
Schedulemust be awaited.
- class Switch[source]¶
Signal to the event loop to run another coroutine
Pauses the coroutine but immediately continues after all other runnable coroutines of the event loop. This is similar to the common
sleep(0)function of regular event loop frameworks.If a single argument is given, this specifies how many turns should be skipped. The default corresponds to 0. If two arguments are given, this is interpreted as an inclusive interval to randomly select the skip count.