Skip to content

Commit

Permalink
Modify retry() to take a callable and enforce the timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
categulario committed May 17, 2022
1 parent 8a85ee7 commit 48b32b3
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions libtmux/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

def retry(seconds=RETRY_TIMEOUT_SECONDS):
"""
Retry a block of code until a time limit or ``break``.
Retry a function until a condition meets or the specified time passes.
Parameters
----------
seconds : int
Seconds to retry, defaults to ``RETRY_TIMEOUT_SECONDS``, which is
configurable via environmental variables.
Seconds to retry, defaults to ``8``, which is configurable via
``RETRY_TIMEOUT_SECONDS`` environmental variables.
Returns
-------
Expand All @@ -34,13 +34,19 @@ def retry(seconds=RETRY_TIMEOUT_SECONDS):
Examples
--------
>>> while retry():
>>> def f():
... p = w.attached_pane
... p.server._update_panes()
... if p.current_path == pane_path:
... break
... return p.current_path == pane_path
...
... retry(f)
"""
return (lambda: time.time() < time.time() + seconds)()
ini = time.time()

while not fun():
end = time.time()
if end - ini >= secs:
raise Exception('Function timed out without meeting condition')


def get_test_session_name(server, prefix=TEST_SESSION_PREFIX):
Expand Down

0 comments on commit 48b32b3

Please sign in to comment.