diff options
author | Victor Stinner <vstinner@python.org> | 2023-10-04 10:58:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-04 10:58:49 (GMT) |
commit | f9ac377626ff993569b532e693acf3f08a6c1f43 (patch) | |
tree | b2fbd6c74a2fb288a24bc807c882dc926a80b21d /Doc/library/test.rst | |
parent | 6c98c734c7bcef2eee870bf4e5b4326bb81b320c (diff) | |
download | cpython-f9ac377626ff993569b532e693acf3f08a6c1f43.zip cpython-f9ac377626ff993569b532e693acf3f08a6c1f43.tar.gz cpython-f9ac377626ff993569b532e693acf3f08a6c1f43.tar.bz2 |
[3.11] Add test.support.busy_retry() (#93770) (#110341)
Add test.support.busy_retry() (#93770)
Add busy_retry() and sleeping_retry() functions to test.support.
(cherry picked from commit 7e9eaad864349d2cfd4c9ffc4453aba03b2cbc16)
Diffstat (limited to 'Doc/library/test.rst')
-rw-r--r-- | Doc/library/test.rst | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/Doc/library/test.rst b/Doc/library/test.rst index 427953e..d3eb0ae 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -404,6 +404,51 @@ The :mod:`test.support` module defines the following constants: The :mod:`test.support` module defines the following functions: +.. function:: busy_retry(timeout, err_msg=None, /, *, error=True) + + Run the loop body until ``break`` stops the loop. + + After *timeout* seconds, raise an :exc:`AssertionError` if *error* is true, + or just stop the loop if *error* is false. + + Example:: + + for _ in support.busy_retry(support.SHORT_TIMEOUT): + if check(): + break + + Example of error=False usage:: + + for _ in support.busy_retry(support.SHORT_TIMEOUT, error=False): + if check(): + break + else: + raise RuntimeError('my custom error') + +.. function:: sleeping_retry(timeout, err_msg=None, /, *, init_delay=0.010, max_delay=1.0, error=True) + + Wait strategy that applies exponential backoff. + + Run the loop body until ``break`` stops the loop. Sleep at each loop + iteration, but not at the first iteration. The sleep delay is doubled at + each iteration (up to *max_delay* seconds). + + See :func:`busy_retry` documentation for the parameters usage. + + Example raising an exception after SHORT_TIMEOUT seconds:: + + for _ in support.sleeping_retry(support.SHORT_TIMEOUT): + if check(): + break + + Example of error=False usage:: + + for _ in support.sleeping_retry(support.SHORT_TIMEOUT, error=False): + if check(): + break + else: + raise RuntimeError('my custom error') + .. function:: is_resource_enabled(resource) Return ``True`` if *resource* is enabled and available. The list of |