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 def test case to be run synchronously with children

This provides a primitive “event loop” which only responds to Schedule, Switch and Lock.

It should be applied as a decorator on an async def function, which is then turned into a synchronous callable that will run the async def function and all tasks it spawns. Other decorators, most prominently pytest.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 Schedule must be awaited.

class Switch(skip: int, /)[source]
class Switch(min: int, max: int, /)[source]
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.

class Lock[source]

Simple lock for exclusive access