Skip to content

Commit

Permalink
feat: add support to retry known connection errors (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
cojenco authored Apr 21, 2023
1 parent 1aff914 commit 147e845
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
7 changes: 7 additions & 0 deletions google/resumable_media/requests/_request_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
This utilities are explicitly catered to ``requests``-like transports.
"""

import http.client
import requests.exceptions
import urllib3.exceptions # type: ignore

Expand All @@ -35,10 +36,16 @@
_DEFAULT_READ_TIMEOUT = 60

_CONNECTION_ERROR_CLASSES = (
http.client.BadStatusLine,
http.client.IncompleteRead,
http.client.ResponseNotReady,
requests.exceptions.ConnectionError,
requests.exceptions.ChunkedEncodingError,
requests.exceptions.Timeout,
urllib3.exceptions.PoolError,
urllib3.exceptions.ProtocolError,
urllib3.exceptions.SSLError,
urllib3.exceptions.TimeoutError,
ConnectionError, # Python 3.x only, superclass of ConnectionResetError.
)

Expand Down
80 changes: 42 additions & 38 deletions tests/unit/requests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,18 @@ def raise_response():

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

response = _make_response(http.client.NOT_FOUND)
status_code = int(http.client.OK)
response = _make_response(status_code)
responses = [
ConnectionResetError, # Subclass of ConnectionError
urllib3.exceptions.ConnectionError,
requests.exceptions.ConnectionError,
http.client.BadStatusLine(""),
http.client.IncompleteRead(""),
http.client.ResponseNotReady,
ConnectionError,
response,
]
func = mock.Mock(side_effect=responses, spec=[])
Expand All @@ -183,28 +187,29 @@ def test_success_with_retry_connection_error(self, randint_mock, sleep_mock):
)

assert ret_val == responses[-1]

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

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

assert sleep_mock.call_count == 3
assert func.call_count == 5
assert func.mock_calls == [mock.call()] * 5
assert randint_mock.call_count == 4
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 4
assert sleep_mock.call_count == 4
sleep_mock.assert_any_call(1.125)
sleep_mock.assert_any_call(2.625)
sleep_mock.assert_any_call(4.375)
sleep_mock.assert_any_call(4.500)
sleep_mock.assert_any_call(8.875)

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

status_code = int(http.client.OK)
response = _make_response(status_code)
responses = [
requests.exceptions.ConnectionError,
requests.exceptions.ChunkedEncodingError,
requests.exceptions.ChunkedEncodingError,
requests.exceptions.Timeout,
response,
]
func = mock.Mock(side_effect=responses, spec=[])
Expand All @@ -215,27 +220,27 @@ def test_success_with_retry_chunked_encoding_error(self, randint_mock, sleep_moc
)

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
assert func.call_count == 4
assert func.mock_calls == [mock.call()] * 4
assert randint_mock.call_count == 3
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 3
assert sleep_mock.call_count == 3
sleep_mock.assert_any_call(1.125)
sleep_mock.assert_any_call(2.625)
sleep_mock.assert_any_call(4.500)

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

status_code = int(http.client.OK)
response = _make_response(status_code)
responses = [
requests.exceptions.Timeout,
requests.exceptions.Timeout,
urllib3.exceptions.PoolError(None, ""),
urllib3.exceptions.ProtocolError,
urllib3.exceptions.SSLError,
urllib3.exceptions.TimeoutError,
response,
]
func = mock.Mock(side_effect=responses, spec=[])
Expand All @@ -246,16 +251,15 @@ def test_success_with_retry_client_timeout(self, randint_mock, sleep_mock):
)

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
assert func.call_count == 5
assert func.mock_calls == [mock.call()] * 5
assert randint_mock.call_count == 4
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 4
assert sleep_mock.call_count == 4
sleep_mock.assert_any_call(1.125)
sleep_mock.assert_any_call(2.625)
sleep_mock.assert_any_call(4.500)
sleep_mock.assert_any_call(8.875)

@mock.patch("time.sleep")
@mock.patch("random.randint")
Expand Down

0 comments on commit 147e845

Please sign in to comment.