Async and parallel tests
Async Tests
Asynchronous tests are natively supported. All asynchronous tests get ASYNC flag set in flags.
✋ Note that the top level test must not be asynchronous.
If you try to run an asynchronous test as the top level test, you will get an error:
error: top level test was not started in main thread
Inline Async Tests
An inline asynchronous test can be defined using async with statement as follows.
1 | from testflows.core import * |
Decorated Async Tests
A decorated asynchronous test can be defined in a similar way as a non-asynchronous test.
The only difference is that the decorated function must be asynchronous
and be defined using async def keyword just like any other asynchronous function.
1 | from testflows.core import * |
✋ See asyncio module to learn more about asynchronous programming in Python.
Parallel Tests
Running Parallel Tests
Tests can be executed in parallel either using threads or an asynchronous executor defined using ThreadPool class or AsyncPool class respectively.
In order to run a test in parallel, it must either have PARALLEL flag
set or parallel=True specified during the test definition.
A parallel executor can be specified using executor parameter. If no executor
is explicitly specified, then a default executor is created for the
test of the type that is needed to execute a test.
✋ Note that the default executor does not have a limit on the number of parallel tests because the pool size is not limited.
Here is an example when executor is not specified.
1 | import time |
Using join()
The join() function can be used to join any currently running parallel tests. For example,
1 |
|
Parallel Executors
Parallel executors can be used to gain fine grained control over how many tests are executed in parallel.
✋ You should not share a single pool executor between different tests as it can lead to a deadlock given that a situation might arise when a parent test can be left waiting for the child test to complete, and a child test will not be able to complete due to the shared pool having no available workers.
If you want to share a pool between different tests, you must use either
SharedThreadPool class or SharedAsyncPool class for normal or asynchronous tests,
respectively. These classes ensure that a deadlock between a parent and child test is avoided
by blocking and waiting for the completion of any task that is submitted when no idle workers
are available.
Thread Pool
A thread pool executor is defined by creating an object of Pool class which is a short form for defining a ThreadPool class and will run a test in another thread.
The maximum number of threads can be controlled by setting max_workers
parameter, and by default is set to 16. If max_workers is set to None
then the pool size is not limited.
If there are more tasks submitted to the pool than there are currently available threads, then any extra tasks will block until a worker in the pool is freed up.
1 | with Pool(5) as pool: |
Async Pool
An asynchronous pool executor is defined by creating an object of AsyncPool class
and will run an asynchronous test using a new loop running in another thread unless
loop parameter is explicitly specified during executor object creation.
The maximum number of concurrent asynchronous tasks can be controlled by setting max_workers
parameter, and by default is set to 1024. If max_workers is set to None
then the pool size is not limited.
If there are more tasks submitted to the pool than there are currently available threads, then any extra tasks will block until a worker in the pool is freed up.
1 | with AsyncPool(5) as pool: |