Skip to content

Commit 9921747

Browse files
committed
document libtmux test functions, add retry function, update api docs
1 parent 658c5fd commit 9921747

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

doc/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ Exceptions
108108
Test tools
109109
----------
110110

111+
.. automethod:: libtmux.test.retry
112+
111113
.. automethod:: libtmux.test.get_test_session_name
112114

113115
.. automethod:: libtmux.test.get_test_window_name

libtmux/test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,50 @@
77
import logging
88
import os
99
import tempfile
10+
import time
1011

1112
logger = logging.getLogger(__name__)
1213

1314
TEST_SESSION_PREFIX = 'libtmux_'
15+
RETRY_TIMEOUT_SECONDS = int(os.getenv('RETRY_TIMEOUT_SECONDS', 8))
1416

1517
namer = tempfile._RandomNameSequence()
1618
current_dir = os.path.abspath(os.path.dirname(__file__))
1719
example_dir = os.path.abspath(os.path.join(current_dir, '..', 'examples'))
1820
fixtures_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+
2143
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+
"""
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

2961
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+
"""
3072
while True:
3173
window_name = prefix + next(namer)
3274
if not session.find_where(window_name=window_name):

0 commit comments

Comments
 (0)