77import logging
88import os
99import tempfile
10+ import time
1011
1112logger = logging .getLogger (__name__ )
1213
1314TEST_SESSION_PREFIX = 'libtmux_'
15+ RETRY_TIMEOUT_SECONDS = int (os .getenv ('RETRY_TIMEOUT_SECONDS' , 8 ))
1416
1517namer = tempfile ._RandomNameSequence ()
1618current_dir = os .path .abspath (os .path .dirname (__file__ ))
1719example_dir = os .path .abspath (os .path .join (current_dir , '..' , 'examples' ))
1820fixtures_dir = os .path .realpath (os .path .join (current_dir , 'fixtures' ))
1921
2022
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+
2143def 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+ """
2254 while True :
2355 session_name = prefix + next (namer )
2456 if not server .has_session (session_name ):
@@ -27,6 +59,16 @@ def get_test_session_name(server, prefix=TEST_SESSION_PREFIX):
2759
2860
2961def 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+ """
3072 while True :
3173 window_name = prefix + next (namer )
3274 if not session .find_where (window_name = window_name ):
0 commit comments