9
9
10
10
TEST_SESSION_PREFIX = "libtmux_"
11
11
RETRY_TIMEOUT_SECONDS = int (os .getenv ("RETRY_TIMEOUT_SECONDS" , 8 ))
12
+ RETRY_INTERVAL_SECONDS = float (os .getenv ("RETRY_INTERVAL_SECONDS" , 0.05 ))
12
13
13
14
namer = tempfile ._RandomNameSequence ()
14
15
current_dir = os .path .abspath (os .path .dirname (__file__ ))
15
16
example_dir = os .path .abspath (os .path .join (current_dir , ".." , "examples" ))
16
17
fixtures_dir = os .path .realpath (os .path .join (current_dir , "fixtures" ))
17
18
18
19
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
+ ):
20
31
"""
21
32
Retry a function until a condition meets or the specified time passes.
22
33
23
34
Parameters
24
35
----------
36
+ fun : callable
37
+ A function that will be called repeatedly until it returns ``True`` or
38
+ the specified time passes.
25
39
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``.
28
47
29
48
Returns
30
49
-------
@@ -46,7 +65,11 @@ def retry(fun, seconds=RETRY_TIMEOUT_SECONDS):
46
65
while not fun ():
47
66
end = time .time ()
48
67
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 )
50
73
51
74
52
75
def get_test_session_name (server , prefix = TEST_SESSION_PREFIX ):
0 commit comments