Skip to content

Commit a24857f

Browse files
committed
refactor!(test[retry]) Move from test to test.retry
1 parent 6445c20 commit a24857f

File tree

4 files changed

+76
-51
lines changed

4 files changed

+76
-51
lines changed

src/libtmux/test/__init__.py

Lines changed: 0 additions & 49 deletions
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

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

tests/test_pane.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)