Retry Utilities¶
Retry helper functions for libtmux test utilities. These utilities help manage testing operations that may require multiple attempts before succeeding.
Basic Retry Functionality¶
Retry helpers for libtmux and downstream libtmux libraries.
-
-
Retry a function until a condition meets or the specified time passes.
- Parameters:
fun (
callable) – A function that will be called repeatedly until it returnsTrueor the specified time passes. Called withargsunpacked, so it accepts no arguments unlessargsis given.seconds (
float) – Seconds to retry. Defaults to8, which is configurable viaRETRY_TIMEOUT_SECONDSenvironment variables.args (
tuple) – Positional arguments passed tofunon every call. Pass the subject being waited on here instead of closing over it, which keeps predicates written inside a loop free of a loop-variable binding.interval (
float) – Time in seconds to wait between calls. Defaults to0.05and is configurable viaRETRY_INTERVAL_SECONDSenvironment variable.raises (
bool) – Whether or not to raise an exception on timeout. Defaults toTrue.
Notes
The budget is measured with
time.monotonic(), which only ever moves forward. A wall clock does not: an NTP correction, a container clock sync, or a VM resume steps it, and a step landing inside the wait would end it early or stretch it by the size of the step.Examples
>>> def fn(): ... p = session.active_window.active_pane ... return p.pane_current_path is not None
>>> retry_until(fn) True
In pytest:
>>> assert retry_until(fn, raises=False)
Waiting on something from a loop takes
argsrather than a closure:>>> window = session.new_window(window_name='retry_until_args') >>> panes = [window.active_pane, window.split()] >>> for pane in panes: ... pane.send_keys('echo ready')
Compare whole lines.
capture_panereturns the command tmux echoed onto the pane, so'ready' in linematches that echo and is already true before the shell has run anything:>>> for pane in panes: ... assert retry_until( ... lambda p: any(line.strip() == 'ready' for line in p.capture_pane()), ... args=(pane,), ... )