diff options
author | Victor Stinner <vstinner@python.org> | 2022-06-15 09:42:10 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-15 09:42:10 (GMT) |
commit | 7e9eaad864349d2cfd4c9ffc4453aba03b2cbc16 (patch) | |
tree | 7bc4498a4c5ad027208a714ff9281d2f4639e301 /Doc | |
parent | 4e9fa71d7e8e2f322f0b81b315ddc921f57384c0 (diff) | |
download | cpython-7e9eaad864349d2cfd4c9ffc4453aba03b2cbc16.zip cpython-7e9eaad864349d2cfd4c9ffc4453aba03b2cbc16.tar.gz cpython-7e9eaad864349d2cfd4c9ffc4453aba03b2cbc16.tar.bz2 |
Add test.support.busy_retry() (#93770)
Add busy_retry() and sleeping_retry() functions to test.support.
Diffstat (limited to 'Doc')
-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 5c458bf..e255952 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -413,6 +413,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 |