7
7
import logging
8
8
import os
9
9
import tempfile
10
+ import time
10
11
11
12
logger = logging .getLogger (__name__ )
12
13
13
14
TEST_SESSION_PREFIX = 'libtmux_'
15
+ RETRY_TIMEOUT_SECONDS = int (os .getenv ('RETRY_TIMEOUT_SECONDS' , 8 ))
14
16
15
17
namer = tempfile ._RandomNameSequence ()
16
18
current_dir = os .path .abspath (os .path .dirname (__file__ ))
17
19
example_dir = os .path .abspath (os .path .join (current_dir , '..' , 'examples' ))
18
20
fixtures_dir = os .path .realpath (os .path .join (current_dir , 'fixtures' ))
19
21
20
22
23
+ def retry (seconds = RETRY_TIMEOUT_SECONDS ):
24
+ """Retry a block of code until a time limit or ``break``.
25
+
26
+ .. code-block:: python
27
+
28
+ while retry():
29
+ p = w.attached_pane
30
+ p.server._update_panes()
31
+ if p.current_path == pane_path:
32
+ break
33
+
34
+
35
+ :param seconds: Seconds to retry, defaults to ``RETRY_TIMEOUT_SECONDS``,
36
+ which is configurable via environmental variables.
37
+ :type seconds: int
38
+ :rtype: void
39
+ """
40
+ return (lambda : time .time () < time .time () + seconds )()
41
+
42
+
21
43
def get_test_session_name (server , prefix = TEST_SESSION_PREFIX ):
44
+ """Faker to create a session name that doesn't exist.
45
+
46
+ :param server: libtmux server
47
+ :type server: :class:`libtmux.Server`
48
+ :param prefix: prefix for sessions (e.g. libtmux_). Defaults to
49
+ ``TEST_SESSION_PREFIX``.
50
+ :type prefix: string
51
+ :rtype: string
52
+ :returns: Random session name guaranteed to not collide with current ones
53
+ """
22
54
while True :
23
55
session_name = prefix + next (namer )
24
56
if not server .has_session (session_name ):
@@ -27,6 +59,16 @@ def get_test_session_name(server, prefix=TEST_SESSION_PREFIX):
27
59
28
60
29
61
def get_test_window_name (session , prefix = TEST_SESSION_PREFIX ):
62
+ """Faker to create a window name that doesn't exist.
63
+
64
+ :param session: libtmux session
65
+ :type session: :class:`libtmux.Session`
66
+ :param prefix: prefix for sessions (e.g. libtmux_). Defaults to
67
+ ``TEST_SESSION_PREFIX``. ATM we reuse the test session prefix here.
68
+ :type prefix: string
69
+ :rtype: string
70
+ :returns: Random window name guaranteed to not collide with current ones
71
+ """
30
72
while True :
31
73
window_name = prefix + next (namer )
32
74
if not session .find_where (window_name = window_name ):
0 commit comments