Skip to content

Commit accf6ff

Browse files
authored
Fixed the retry tests failing (#500)
1 parent fc9da22 commit accf6ff

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

tests/e2e/common/retry_test_mixins.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ class PySQLRetryTestsMixin:
121121
# For testing purposes
122122
_retry_policy = {
123123
"_retry_delay_min": 0.1,
124-
"_retry_delay_max": 5,
124+
"_retry_delay_max": 3,
125125
"_retry_stop_after_attempts_count": 5,
126-
"_retry_stop_after_attempts_duration": 10,
126+
"_retry_stop_after_attempts_duration": 30,
127127
"_retry_delay_default": 0.5,
128128
}
129129

@@ -135,7 +135,7 @@ def test_retry_urllib3_settings_are_honored(self):
135135
urllib3_config = {"connect": 10, "read": 11, "redirect": 12}
136136
rp = DatabricksRetryPolicy(
137137
delay_min=0.1,
138-
delay_max=10.0,
138+
delay_max=3,
139139
stop_after_attempts_count=10,
140140
stop_after_attempts_duration=10.0,
141141
delay_default=1.0,
@@ -174,14 +174,14 @@ def test_retry_max_count_not_exceeded(self):
174174
def test_retry_exponential_backoff(self):
175175
"""GIVEN the retry policy is configured for reasonable exponential backoff
176176
WHEN the server sends nothing but 429 responses with retry-afters
177-
THEN the connector will use those retry-afters values as delay
177+
THEN the connector will use those retry-afters values as floor
178178
"""
179179
retry_policy = self._retry_policy.copy()
180180
retry_policy["_retry_delay_min"] = 1
181181

182182
time_start = time.time()
183183
with mocked_server_response(
184-
status=429, headers={"Retry-After": "3"}
184+
status=429, headers={"Retry-After": "8"}
185185
) as mock_obj:
186186
with pytest.raises(RequestError) as cm:
187187
with self.connection(extra_params=retry_policy) as conn:
@@ -191,14 +191,14 @@ def test_retry_exponential_backoff(self):
191191
assert isinstance(cm.value.args[1], MaxRetryDurationError)
192192

193193
# With setting delay_min to 1, the expected retry delays should be:
194-
# 3, 3, 3, 3
194+
# 8, 8, 8, 8
195195
# The first 3 retries are allowed, the 4th retry puts the total duration over the limit
196-
# of 10 seconds
196+
# of 30 seconds
197197
assert mock_obj.return_value.getresponse.call_count == 4
198-
assert duration > 6
198+
assert duration > 24
199199

200-
# Should be less than 7, but this is a safe margin for CI/CD slowness
201-
assert duration < 10
200+
# Should be less than 26, but this is a safe margin for CI/CD slowness
201+
assert duration < 30
202202

203203
def test_retry_max_duration_not_exceeded(self):
204204
"""GIVEN the max attempt duration of 10 seconds

tests/unit/test_retry.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ def test_sleep__no_retry_after(self, t_mock, retry_policy, error_history):
3434
retry_policy.history = [error_history, error_history]
3535
retry_policy.sleep(HTTPResponse(status=503))
3636

37-
expected_backoff_time = self.calculate_backoff_time(
38-
0, retry_policy.delay_min, retry_policy.delay_max
37+
expected_backoff_time = max(
38+
self.calculate_backoff_time(
39+
0, retry_policy.delay_min, retry_policy.delay_max
40+
),
41+
retry_policy.delay_max,
3942
)
4043
t_mock.assert_called_with(expected_backoff_time)
4144

@@ -54,8 +57,11 @@ def test_sleep__no_retry_after_header__multiple_retries(self, t_mock, retry_poli
5457
expected_backoff_times = []
5558
for attempt in range(num_attempts):
5659
expected_backoff_times.append(
57-
self.calculate_backoff_time(
58-
attempt, retry_policy.delay_min, retry_policy.delay_max
60+
max(
61+
self.calculate_backoff_time(
62+
attempt, retry_policy.delay_min, retry_policy.delay_max
63+
),
64+
retry_policy.delay_max,
5965
)
6066
)
6167

@@ -77,10 +83,3 @@ def test_excessive_retry_attempts_error(self, t_mock, retry_policy):
7783
retry_policy.sleep(HTTPResponse(status=503))
7884
# Internally urllib3 calls the increment function generating a new instance for every retry
7985
retry_policy = retry_policy.increment()
80-
81-
@patch("time.sleep")
82-
def test_sleep__retry_after_present(self, t_mock, retry_policy, error_history):
83-
retry_policy._retry_start_time = time.time()
84-
retry_policy.history = [error_history, error_history, error_history]
85-
retry_policy.sleep(HTTPResponse(status=503, headers={"Retry-After": "3"}))
86-
t_mock.assert_called_with(3)

0 commit comments

Comments
 (0)