Skip to content

Commit 48b32b3

Browse files
committed
Modify retry() to take a callable and enforce the timeout
1 parent 8a85ee7 commit 48b32b3

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

libtmux/test.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
def retry(seconds=RETRY_TIMEOUT_SECONDS):
2020
"""
21-
Retry a block of code until a time limit or ``break``.
21+
Retry a function until a condition meets or the specified time passes.
2222
2323
Parameters
2424
----------
2525
seconds : int
26-
Seconds to retry, defaults to ``RETRY_TIMEOUT_SECONDS``, which is
27-
configurable via environmental variables.
26+
Seconds to retry, defaults to ``8``, which is configurable via
27+
``RETRY_TIMEOUT_SECONDS`` environmental variables.
2828
2929
Returns
3030
-------
@@ -34,13 +34,19 @@ def retry(seconds=RETRY_TIMEOUT_SECONDS):
3434
Examples
3535
--------
3636
37-
>>> while retry():
37+
>>> def f():
3838
... p = w.attached_pane
3939
... p.server._update_panes()
40-
... if p.current_path == pane_path:
41-
... break
40+
... return p.current_path == pane_path
41+
...
42+
... retry(f)
4243
"""
43-
return (lambda: time.time() < time.time() + seconds)()
44+
ini = time.time()
45+
46+
while not fun():
47+
end = time.time()
48+
if end - ini >= secs:
49+
raise Exception('Function timed out without meeting condition')
4450

4551

4652
def get_test_session_name(server, prefix=TEST_SESSION_PREFIX):

0 commit comments

Comments
 (0)