Skip to content

Commit

Permalink
fix: retry client side requests timeout (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
cojenco committed Mar 23, 2022
1 parent a23f226 commit d0649c7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions google/resumable_media/requests/_request_helpers.py
Expand Up @@ -37,6 +37,7 @@
_CONNECTION_ERROR_CLASSES = (
requests.exceptions.ConnectionError,
requests.exceptions.ChunkedEncodingError,
requests.exceptions.Timeout,
urllib3.exceptions.ProtocolError,
ConnectionError, # Python 3.x only, superclass of ConnectionResetError.
)
Expand Down
34 changes: 33 additions & 1 deletion tests/unit/requests/test__helpers.py
Expand Up @@ -200,7 +200,8 @@ def test_success_with_retry_connection_error(self, randint_mock, sleep_mock):
def test_success_with_retry_chunked_encoding_error(self, randint_mock, sleep_mock):
randint_mock.side_effect = [125, 625, 375]

response = _make_response(http.client.NOT_FOUND)
status_code = int(http.client.OK)
response = _make_response(status_code)
responses = [
requests.exceptions.ChunkedEncodingError,
requests.exceptions.ChunkedEncodingError,
Expand All @@ -225,6 +226,37 @@ def test_success_with_retry_chunked_encoding_error(self, randint_mock, sleep_moc
sleep_mock.assert_any_call(1.125)
sleep_mock.assert_any_call(2.625)

@mock.patch(u"time.sleep")
@mock.patch(u"random.randint")
def test_success_with_retry_client_timeout(self, randint_mock, sleep_mock):
randint_mock.side_effect = [125, 625, 375]

status_code = int(http.client.OK)
response = _make_response(status_code)
responses = [
requests.exceptions.Timeout,
requests.exceptions.Timeout,
response,
]
func = mock.Mock(side_effect=responses, spec=[])

retry_strategy = common.RetryStrategy()
ret_val = _request_helpers.wait_and_retry(
func, _get_status_code, retry_strategy
)

assert ret_val == responses[-1]

assert func.call_count == 3
assert func.mock_calls == [mock.call()] * 3

assert randint_mock.call_count == 2
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 2

assert sleep_mock.call_count == 2
sleep_mock.assert_any_call(1.125)
sleep_mock.assert_any_call(2.625)

@mock.patch("time.sleep")
@mock.patch("random.randint")
def test_retry_exceeds_max_cumulative(self, randint_mock, sleep_mock):
Expand Down

0 comments on commit d0649c7

Please sign in to comment.