Skip to content

Commit 09d5bdb

Browse files
committed
test(retry): improve retry_until test reliability
- Add realistic sleep durations to simulate work - Add timing assertions with reasonable tolerances - Test both success and timeout scenarios - Test behavior with raises=False option - Improve test readability with clear timing expectations
1 parent 77cdce7 commit 09d5bdb

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

tests/test/test_retry.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from time import time
5+
from time import sleep, time
66

77
import pytest
88

@@ -17,62 +17,69 @@ def test_retry_three_times() -> None:
1717

1818
def call_me_three_times() -> bool:
1919
nonlocal value
20+
sleep(0.3) # Sleep for 0.3 seconds to simulate work
2021

2122
if value == 2:
2223
return True
2324

2425
value += 1
25-
2626
return False
2727

2828
retry_until(call_me_three_times, 1)
2929

3030
end = time()
3131

32-
assert abs((end - ini) - 1.0) > 0 < 0.1
32+
assert 0.9 <= (end - ini) <= 1.1 # Allow for small timing variations
3333

3434

3535
def test_function_times_out() -> None:
3636
"""Test time outs with retry_until()."""
3737
ini = time()
3838

3939
def never_true() -> bool:
40+
sleep(
41+
0.1,
42+
) # Sleep for 0.1 seconds to simulate work (called ~10 times in 1 second)
4043
return False
4144

4245
with pytest.raises(WaitTimeout):
4346
retry_until(never_true, 1)
4447

4548
end = time()
4649

47-
assert abs((end - ini) - 1.0) > 0 < 0.1
50+
assert 0.9 <= (end - ini) <= 1.1 # Allow for small timing variations
4851

4952

5053
def test_function_times_out_no_raise() -> None:
5154
"""Tests retry_until() with exception raising disabled."""
5255
ini = time()
5356

5457
def never_true() -> bool:
58+
sleep(
59+
0.1,
60+
) # Sleep for 0.1 seconds to simulate work (called ~10 times in 1 second)
5561
return False
5662

5763
retry_until(never_true, 1, raises=False)
5864

5965
end = time()
60-
61-
assert abs((end - ini) - 1.0) > 0 < 0.1
66+
assert 0.9 <= (end - ini) <= 1.1 # Allow for small timing variations
6267

6368

6469
def test_function_times_out_no_raise_assert() -> None:
6570
"""Tests retry_until() with exception raising disabled, returning False."""
6671
ini = time()
6772

6873
def never_true() -> bool:
74+
sleep(
75+
0.1,
76+
) # Sleep for 0.1 seconds to simulate work (called ~10 times in 1 second)
6977
return False
7078

7179
assert not retry_until(never_true, 1, raises=False)
7280

7381
end = time()
74-
75-
assert abs((end - ini) - 1.0) > 0 < 0.1
82+
assert 0.9 <= (end - ini) <= 1.1 # Allow for small timing variations
7683

7784

7885
def test_retry_three_times_no_raise_assert() -> None:
@@ -82,16 +89,17 @@ def test_retry_three_times_no_raise_assert() -> None:
8289

8390
def call_me_three_times() -> bool:
8491
nonlocal value
92+
sleep(
93+
0.3,
94+
) # Sleep for 0.3 seconds to simulate work (called 3 times in ~0.9 seconds)
8595

8696
if value == 2:
8797
return True
8898

8999
value += 1
90-
91100
return False
92101

93102
assert retry_until(call_me_three_times, 1, raises=False)
94103

95104
end = time()
96-
97-
assert abs((end - ini) - 1.0) > 0 < 0.1
105+
assert 0.9 <= (end - ini) <= 1.1 # Allow for small timing variations

0 commit comments

Comments
 (0)