Skip to content

Commit 9bd8c3b

Browse files
committed
test new retry() function and add parameters
1 parent bb8ddf0 commit 9bd8c3b

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

libtmux/test.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,41 @@
99

1010
TEST_SESSION_PREFIX = "libtmux_"
1111
RETRY_TIMEOUT_SECONDS = int(os.getenv("RETRY_TIMEOUT_SECONDS", 8))
12+
RETRY_INTERVAL_SECONDS = float(os.getenv("RETRY_INTERVAL_SECONDS", 0.05))
1213

1314
namer = tempfile._RandomNameSequence()
1415
current_dir = os.path.abspath(os.path.dirname(__file__))
1516
example_dir = os.path.abspath(os.path.join(current_dir, "..", "examples"))
1617
fixtures_dir = os.path.realpath(os.path.join(current_dir, "fixtures"))
1718

1819

19-
def retry(fun, seconds=RETRY_TIMEOUT_SECONDS):
20+
class WaitTimeout(Exception):
21+
'''Function timed out without meeting condition'''
22+
23+
24+
def retry(
25+
fun,
26+
seconds=RETRY_TIMEOUT_SECONDS,
27+
*,
28+
interval=RETRY_INTERVAL_SECONDS,
29+
raises=True,
30+
):
2031
"""
2132
Retry a function until a condition meets or the specified time passes.
2233
2334
Parameters
2435
----------
36+
fun : callable
37+
A function that will be called repeatedly until it returns ``True`` or
38+
the specified time passes.
2539
seconds : int
26-
Seconds to retry, defaults to ``8``, which is configurable via
27-
``RETRY_TIMEOUT_SECONDS`` environmental variables.
40+
Seconds to retry. Defaults to ``8``, which is configurable via
41+
``RETRY_TIMEOUT_SECONDS`` environment variables.
42+
interval : float
43+
Time in seconds to wait between calls. Defaults to ``0.05`` and is
44+
configurable via ``RETRY_INTERVAL_SECONDS`` environment variable.
45+
raises : bool
46+
Wether or not to raise an exception on timeout. Defaults to ``True``.
2847
2948
Returns
3049
-------
@@ -46,7 +65,11 @@ def retry(fun, seconds=RETRY_TIMEOUT_SECONDS):
4665
while not fun():
4766
end = time.time()
4867
if end - ini >= seconds:
49-
raise Exception('Function timed out without meeting condition')
68+
if raises:
69+
raise WaitTimeout()
70+
else:
71+
break
72+
time.sleep(interval)
5073

5174

5275
def get_test_session_name(server, prefix=TEST_SESSION_PREFIX):

tests/test_test.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from time import time
2+
import pytest
3+
4+
from libtmux.test import retry, WaitTimeout
5+
6+
7+
def test_retry_three_times():
8+
ini = time()
9+
value = 0
10+
11+
def call_me_three_times():
12+
nonlocal value
13+
14+
if value == 2:
15+
return True
16+
17+
value += 1
18+
19+
return False
20+
21+
retry(call_me_three_times, 1)
22+
23+
end = time()
24+
25+
assert abs((end - ini) - 0.1) < 0.01
26+
27+
28+
def test_function_times_out():
29+
ini = time()
30+
31+
def never_true():
32+
return False
33+
34+
with pytest.raises(WaitTimeout):
35+
retry(never_true, 1)
36+
37+
end = time()
38+
39+
assert abs((end - ini) - 1.0) < 0.01
40+
41+
42+
def test_function_times_out_no_rise():
43+
ini = time()
44+
45+
def never_true():
46+
return False
47+
48+
retry(never_true, 1, raises=False)
49+
50+
end = time()
51+
52+
assert abs((end - ini) - 1.0) < 0.01

0 commit comments

Comments
 (0)