99
1010TEST_SESSION_PREFIX = "libtmux_"
1111RETRY_TIMEOUT_SECONDS = int (os .getenv ("RETRY_TIMEOUT_SECONDS" , 8 ))
12+ RETRY_INTERVAL_SECONDS = float (os .getenv ("RETRY_INTERVAL_SECONDS" , 0.05 ))
1213
1314namer = tempfile ._RandomNameSequence ()
1415current_dir = os .path .abspath (os .path .dirname (__file__ ))
1516example_dir = os .path .abspath (os .path .join (current_dir , ".." , "examples" ))
1617fixtures_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
5275def get_test_session_name (server , prefix = TEST_SESSION_PREFIX ):
0 commit comments