Skip to content

Commit ef55d41

Browse files
committed
refactor!(test[retry]) Move from test to test.retry
1 parent 8751eb2 commit ef55d41

File tree

4 files changed

+73
-51
lines changed

4 files changed

+73
-51
lines changed

src/libtmux/test/__init__.py

-49
Original file line numberDiff line numberDiff line change
@@ -39,52 +39,3 @@
3939
current_dir = pathlib.Path(__file__)
4040
example_dir = current_dir.parent / "examples"
4141
fixtures_dir = current_dir / "fixtures"
42-
43-
44-
def retry_until(
45-
fun: Callable[[], bool],
46-
seconds: float = RETRY_TIMEOUT_SECONDS,
47-
*,
48-
interval: float = RETRY_INTERVAL_SECONDS,
49-
raises: bool | None = True,
50-
) -> bool:
51-
"""
52-
Retry a function until a condition meets or the specified time passes.
53-
54-
Parameters
55-
----------
56-
fun : callable
57-
A function that will be called repeatedly until it returns ``True`` or
58-
the specified time passes.
59-
seconds : float
60-
Seconds to retry. Defaults to ``8``, which is configurable via
61-
``RETRY_TIMEOUT_SECONDS`` environment variables.
62-
interval : float
63-
Time in seconds to wait between calls. Defaults to ``0.05`` and is
64-
configurable via ``RETRY_INTERVAL_SECONDS`` environment variable.
65-
raises : bool
66-
Whether or not to raise an exception on timeout. Defaults to ``True``.
67-
68-
Examples
69-
--------
70-
>>> def fn():
71-
... p = session.active_window.active_pane
72-
... return p.pane_current_path is not None
73-
74-
>>> retry_until(fn)
75-
True
76-
77-
In pytest:
78-
79-
>>> assert retry_until(fn, raises=False)
80-
"""
81-
ini = time.time()
82-
83-
while not fun():
84-
end = time.time()
85-
if end - ini >= seconds:
86-
if raises:
87-
raise WaitTimeout
88-
return False
89-
time.sleep(interval)
90-
return True

src/libtmux/test/retry.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Retry helpers for libtmux and downstream libtmux libraries."""
2+
3+
from __future__ import annotations
4+
5+
import logging
6+
import time
7+
import typing as t
8+
9+
from libtmux.exc import WaitTimeout
10+
from libtmux.test.constants import (
11+
RETRY_INTERVAL_SECONDS,
12+
RETRY_TIMEOUT_SECONDS,
13+
)
14+
15+
logger = logging.getLogger(__name__)
16+
17+
if t.TYPE_CHECKING:
18+
import sys
19+
from collections.abc import Callable
20+
21+
if sys.version_info >= (3, 11):
22+
pass
23+
24+
25+
def retry_until(
26+
fun: Callable[[], bool],
27+
seconds: float = RETRY_TIMEOUT_SECONDS,
28+
*,
29+
interval: float = RETRY_INTERVAL_SECONDS,
30+
raises: bool | None = True,
31+
) -> bool:
32+
"""
33+
Retry a function until a condition meets or the specified time passes.
34+
35+
Parameters
36+
----------
37+
fun : callable
38+
A function that will be called repeatedly until it returns ``True`` or
39+
the specified time passes.
40+
seconds : float
41+
Seconds to retry. Defaults to ``8``, which is configurable via
42+
``RETRY_TIMEOUT_SECONDS`` environment variables.
43+
interval : float
44+
Time in seconds to wait between calls. Defaults to ``0.05`` and is
45+
configurable via ``RETRY_INTERVAL_SECONDS`` environment variable.
46+
raises : bool
47+
Whether or not to raise an exception on timeout. Defaults to ``True``.
48+
49+
Examples
50+
--------
51+
>>> def fn():
52+
... p = session.active_window.active_pane
53+
... return p.pane_current_path is not None
54+
55+
>>> retry_until(fn)
56+
True
57+
58+
In pytest:
59+
60+
>>> assert retry_until(fn, raises=False)
61+
"""
62+
ini = time.time()
63+
64+
while not fun():
65+
end = time.time()
66+
if end - ini >= seconds:
67+
if raises:
68+
raise WaitTimeout
69+
return False
70+
time.sleep(interval)
71+
return True

tests/test_pane.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from libtmux.common import has_gte_version, has_lt_version, has_lte_version
1212
from libtmux.constants import PaneDirection, ResizeAdjustmentDirection
13-
from libtmux.test import retry_until
13+
from libtmux.test.retry import retry_until
1414

1515
if t.TYPE_CHECKING:
1616
from libtmux.session import Session

tests/test_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88

99
from libtmux.exc import WaitTimeout
10-
from libtmux.test import retry_until
10+
from libtmux.test.retry import retry_until
1111

1212

1313
def test_retry_three_times() -> None:

0 commit comments

Comments
 (0)