2
2
3
3
from __future__ import annotations
4
4
5
- from time import time
5
+ from time import sleep , time
6
6
7
7
import pytest
8
8
@@ -17,62 +17,69 @@ def test_retry_three_times() -> None:
17
17
18
18
def call_me_three_times () -> bool :
19
19
nonlocal value
20
+ sleep (0.3 ) # Sleep for 0.3 seconds to simulate work
20
21
21
22
if value == 2 :
22
23
return True
23
24
24
25
value += 1
25
-
26
26
return False
27
27
28
28
retry_until (call_me_three_times , 1 )
29
29
30
30
end = time ()
31
31
32
- assert abs (( end - ini ) - 1.0 ) > 0 < 0.1
32
+ assert 0.9 <= ( end - ini ) <= 1.1 # Allow for small timing variations
33
33
34
34
35
35
def test_function_times_out () -> None :
36
36
"""Test time outs with retry_until()."""
37
37
ini = time ()
38
38
39
39
def never_true () -> bool :
40
+ sleep (
41
+ 0.1 ,
42
+ ) # Sleep for 0.1 seconds to simulate work (called ~10 times in 1 second)
40
43
return False
41
44
42
45
with pytest .raises (WaitTimeout ):
43
46
retry_until (never_true , 1 )
44
47
45
48
end = time ()
46
49
47
- assert abs (( end - ini ) - 1.0 ) > 0 < 0.1
50
+ assert 0.9 <= ( end - ini ) <= 1.1 # Allow for small timing variations
48
51
49
52
50
53
def test_function_times_out_no_raise () -> None :
51
54
"""Tests retry_until() with exception raising disabled."""
52
55
ini = time ()
53
56
54
57
def never_true () -> bool :
58
+ sleep (
59
+ 0.1 ,
60
+ ) # Sleep for 0.1 seconds to simulate work (called ~10 times in 1 second)
55
61
return False
56
62
57
63
retry_until (never_true , 1 , raises = False )
58
64
59
65
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
62
67
63
68
64
69
def test_function_times_out_no_raise_assert () -> None :
65
70
"""Tests retry_until() with exception raising disabled, returning False."""
66
71
ini = time ()
67
72
68
73
def never_true () -> bool :
74
+ sleep (
75
+ 0.1 ,
76
+ ) # Sleep for 0.1 seconds to simulate work (called ~10 times in 1 second)
69
77
return False
70
78
71
79
assert not retry_until (never_true , 1 , raises = False )
72
80
73
81
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
76
83
77
84
78
85
def test_retry_three_times_no_raise_assert () -> None :
@@ -82,16 +89,17 @@ def test_retry_three_times_no_raise_assert() -> None:
82
89
83
90
def call_me_three_times () -> bool :
84
91
nonlocal value
92
+ sleep (
93
+ 0.3 ,
94
+ ) # Sleep for 0.3 seconds to simulate work (called 3 times in ~0.9 seconds)
85
95
86
96
if value == 2 :
87
97
return True
88
98
89
99
value += 1
90
-
91
100
return False
92
101
93
102
assert retry_until (call_me_three_times , 1 , raises = False )
94
103
95
104
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