Skip to content

Commit 4fe4263

Browse files
authored
Merge pull request #353 from tony/retrying
consolidate retry into retry wrapped lambda
2 parents ad9ee05 + 99b6550 commit 4fe4263

File tree

1 file changed

+36
-45
lines changed

1 file changed

+36
-45
lines changed

tests/test_workspacebuilder.py

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,42 @@
99

1010
import kaptan
1111
import pytest
12-
13-
from . import fixtures_dir
1412
from libtmux import Window
1513
from libtmux.common import has_gte_version
1614
from libtmux.test import temp_session
15+
1716
from tmuxp import config, exc
1817
from tmuxp._compat import text_type
1918
from tmuxp.workspacebuilder import WorkspaceBuilder
2019

21-
from . import example_dir
20+
from . import example_dir, fixtures_dir
2221
from .fixtures._util import loadfixture
2322

24-
2523
RETRY_TIMEOUT_SECONDS = int(os.getenv('RETRY_TIMEOUT_SECONDS', 8))
2624

2725

26+
def retry(seconds=RETRY_TIMEOUT_SECONDS):
27+
"""Retry a block of code until a time limit or ``break``.
28+
29+
.. code-block:: python
30+
31+
while retry():
32+
p = w.attached_pane
33+
p.server._update_panes()
34+
if p.current_path == pane_path:
35+
break
36+
37+
38+
:param seconds: Seconds to retry, defaults to ``RETRY_TIMEOUT_SECONDS``,
39+
which is configurable via environmental variables.
40+
:type seconds: int
41+
:rtype: void
42+
43+
:todo: Move to libtmux.test
44+
"""
45+
return (lambda: time.time() < time.time() + seconds)()
46+
47+
2848
def test_split_windows(session):
2949
yaml_config = loadfixture("workspacebuilder/two_pane.yaml")
3050
s = session
@@ -110,15 +130,11 @@ def test_focus_pane_index(session):
110130

111131
pane_path = '/usr'
112132

113-
timeout = time.time() + RETRY_TIMEOUT_SECONDS # seconds timeout
114-
115-
while True:
133+
while retry():
116134
p = w.attached_pane
117135
p.server._update_panes()
118136
if p.current_path == pane_path:
119137
break
120-
elif time.time() > timeout:
121-
break
122138

123139
assert p.current_path == pane_path
124140

@@ -131,14 +147,12 @@ def test_focus_pane_index(session):
131147

132148
p = None
133149
pane_path = '/'
134-
timeout = time.time() + RETRY_TIMEOUT_SECONDS # seconds timeout
135-
while True:
150+
151+
while retry():
136152
p = window3.attached_pane
137153
p.server._update_panes()
138154
if p.current_path == pane_path:
139155
break
140-
elif time.time() > timeout:
141-
break
142156

143157
assert p.current_path == pane_path
144158

@@ -302,17 +316,14 @@ def test_window_options_after(session):
302316

303317
def assert_last_line(p, s):
304318
correct = False
305-
timeout = time.time() + RETRY_TIMEOUT_SECONDS # seconds timeout
306319

307-
while True:
320+
while retry():
308321
pane_out = p.cmd('capture-pane', '-p', '-J').stdout
309322
while not pane_out[-1].strip(): # delete trailing lines tmux 1.8
310323
pane_out.pop()
311324
if len(pane_out) > 1 and pane_out[-2].strip() == s:
312325
correct = True
313326
break
314-
elif time.time() > timeout:
315-
break
316327

317328
# Print output for easier debugging if assertion fails
318329
if not correct:
@@ -351,13 +362,11 @@ def test_window_shell(session):
351362
for w, wconf in builder.iter_create_windows(s):
352363
if 'window_shell' in wconf:
353364
assert wconf['window_shell'] == text_type('top')
354-
timeout = time.time() + RETRY_TIMEOUT_SECONDS # seconds timeout
355-
while True:
365+
366+
while retry():
356367
session.server._update_windows()
357368
if w['window_name'] != 'top':
358369
break
359-
elif time.time() > timeout:
360-
break
361370

362371
assert w.name != text_type('top')
363372

@@ -403,38 +412,29 @@ def test_automatic_rename_option(session):
403412
assert s.name != 'tmuxp'
404413
w = s.windows[0]
405414

406-
timeout = time.time() + RETRY_TIMEOUT_SECONDS # seconds timeout
407-
while True:
415+
while retry():
408416
session.server._update_windows()
409417
if w.name != 'sh':
410418
break
411-
elif time.time() > timeout:
412-
break
413419

414420
assert w.name != 'sh'
415421

416422
pane_base_index = w.show_window_option('pane-base-index', g=True)
417423
w.select_pane(pane_base_index)
418424

419-
timeout = time.time() + RETRY_TIMEOUT_SECONDS # seconds timeout
420-
while True:
425+
while retry():
421426
session.server._update_windows()
422427
if w.name == 'sh':
423428
break
424-
elif time.time() > timeout:
425-
break
426429

427430
assert w.name == text_type('sh')
428431

429432
w.select_pane('-D')
430433

431-
timeout = time.time() + RETRY_TIMEOUT_SECONDS # seconds timeout
432-
while True:
434+
while retry():
433435
session.server._update_windows()
434436
if w['window_name'] != 'sh':
435437
break
436-
elif time.time() > timeout:
437-
break
438438

439439
assert w.name != text_type('sh')
440440

@@ -490,8 +490,7 @@ def test_start_directory(session, tmpdir):
490490

491491
for path, window in zip(dirs, session.windows):
492492
for p in window.panes:
493-
timeout = time.time() + RETRY_TIMEOUT_SECONDS # seconds timeout
494-
while True:
493+
while retry():
495494
p.server._update_panes()
496495
pane_path = p.current_path
497496
if pane_path is None:
@@ -505,8 +504,6 @@ def test_start_directory(session, tmpdir):
505504
path in pane_path
506505
)
507506
break
508-
elif time.time() > timeout:
509-
break
510507

511508
# handle case with OS X adding /private/ to /tmp/ paths
512509
assert result
@@ -558,8 +555,7 @@ def test_start_directory_relative(session, tmpdir):
558555

559556
for path, window in zip(dirs, session.windows):
560557
for p in window.panes:
561-
timeout = time.time() + RETRY_TIMEOUT_SECONDS # seconds timeout
562-
while True:
558+
while retry():
563559
p.server._update_panes()
564560
# Handle case where directories resolve to /private/ in OSX
565561
pane_path = p.current_path
@@ -574,8 +570,6 @@ def test_start_directory_relative(session, tmpdir):
574570
path in pane_path
575571
)
576572
break
577-
elif time.time() > timeout:
578-
break
579573

580574
assert result
581575

@@ -629,13 +623,10 @@ def test_pane_order(session):
629623
# at 0 since python list.
630624
pane_path = pane_paths[p_index - pane_base_index]
631625

632-
timeout = time.time() + RETRY_TIMEOUT_SECONDS # seconds timeout
633-
while True:
626+
while retry():
634627
p.server._update_panes()
635628
if p.current_path == pane_path:
636629
break
637-
elif time.time() > timeout:
638-
break
639630

640631
assert p.current_path, pane_path
641632

0 commit comments

Comments
 (0)