Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retry client side requests timeout #319

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions google/resumable_media/requests/_request_helpers.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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