Skip to content

Commit 787baa7

Browse files
authored
fix: retry client side requests timeout (#319)
1 parent 3ff3f42 commit 787baa7

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/google-resumable-media/google/resumable_media/requests/_request_helpers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
_CONNECTION_ERROR_CLASSES = (
3838
requests.exceptions.ConnectionError,
3939
requests.exceptions.ChunkedEncodingError,
40+
requests.exceptions.Timeout,
4041
urllib3.exceptions.ProtocolError,
4142
ConnectionError, # Python 3.x only, superclass of ConnectionResetError.
4243
)

packages/google-resumable-media/tests/unit/requests/test__helpers.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ def test_success_with_retry_connection_error(self, randint_mock, sleep_mock):
200200
def test_success_with_retry_chunked_encoding_error(self, randint_mock, sleep_mock):
201201
randint_mock.side_effect = [125, 625, 375]
202202

203-
response = _make_response(http.client.NOT_FOUND)
203+
status_code = int(http.client.OK)
204+
response = _make_response(status_code)
204205
responses = [
205206
requests.exceptions.ChunkedEncodingError,
206207
requests.exceptions.ChunkedEncodingError,
@@ -225,6 +226,37 @@ def test_success_with_retry_chunked_encoding_error(self, randint_mock, sleep_moc
225226
sleep_mock.assert_any_call(1.125)
226227
sleep_mock.assert_any_call(2.625)
227228

229+
@mock.patch(u"time.sleep")
230+
@mock.patch(u"random.randint")
231+
def test_success_with_retry_client_timeout(self, randint_mock, sleep_mock):
232+
randint_mock.side_effect = [125, 625, 375]
233+
234+
status_code = int(http.client.OK)
235+
response = _make_response(status_code)
236+
responses = [
237+
requests.exceptions.Timeout,
238+
requests.exceptions.Timeout,
239+
response,
240+
]
241+
func = mock.Mock(side_effect=responses, spec=[])
242+
243+
retry_strategy = common.RetryStrategy()
244+
ret_val = _request_helpers.wait_and_retry(
245+
func, _get_status_code, retry_strategy
246+
)
247+
248+
assert ret_val == responses[-1]
249+
250+
assert func.call_count == 3
251+
assert func.mock_calls == [mock.call()] * 3
252+
253+
assert randint_mock.call_count == 2
254+
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 2
255+
256+
assert sleep_mock.call_count == 2
257+
sleep_mock.assert_any_call(1.125)
258+
sleep_mock.assert_any_call(2.625)
259+
228260
@mock.patch("time.sleep")
229261
@mock.patch("random.randint")
230262
def test_retry_exceeds_max_cumulative(self, randint_mock, sleep_mock):

0 commit comments

Comments
 (0)