Skip to content
Open
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
44 changes: 44 additions & 0 deletions tests/test_fetcher_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,50 @@ def test_http_error(self) -> None:
self.fetcher.fetch(self.url)
self.assertEqual(cm.exception.status_code, 404)

@patch.object(urllib3.HTTPConnectionPool, "_make_request")
def test_http_error_retry(self, mock_request: Mock) -> None:
for status in (502, 503, 504):
with self.subTest(status=status):
mock_request.reset_mock()
mock_request.side_effect = [
urllib3.HTTPResponse(status=status, body=io.BytesIO()),
urllib3.HTTPResponse(
status=200,
body=io.BytesIO(self.file_contents),
preload_content=False,
),
]
data = self.fetcher.download_bytes(self.url, self.file_length)
self.assertEqual(data, self.file_contents)
self.assertEqual(mock_request.call_count, 2)

@patch.object(urllib3.HTTPConnectionPool, "_make_request")
def test_http_error_retry_exhausted(self, mock_request: Mock) -> None:
for status in (502, 503, 504):
with self.subTest(status=status):
mock_request.reset_mock()
mock_request.side_effect = [
urllib3.HTTPResponse(status=status, body=io.BytesIO())
for _ in range(4)
]
with self.assertRaises(exceptions.DownloadHTTPError) as cm:
self.fetcher.fetch(self.url)
self.assertEqual(cm.exception.status_code, status)
self.assertEqual(mock_request.call_count, 4)

@patch.object(urllib3.HTTPConnectionPool, "_make_request")
def test_http_error_no_retry(self, mock_request: Mock) -> None:
for status in (400, 403, 404, 500):
with self.subTest(status=status):
mock_request.reset_mock()
mock_request.return_value = urllib3.HTTPResponse(
status=status, body=io.BytesIO()
)
with self.assertRaises(exceptions.DownloadHTTPError) as cm:
self.fetcher.fetch(self.url)
self.assertEqual(cm.exception.status_code, status)
mock_request.assert_called_once()

# Response read timeout error
@patch.object(urllib3.PoolManager, "request")
def test_response_read_timeout(self, mock_session_get: Mock) -> None:
Expand Down
11 changes: 10 additions & 1 deletion tuf/ngclient/urllib3_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
class Urllib3Fetcher(FetcherInterface):
"""An implementation of ``FetcherInterface`` based on the urllib3 library.

Requests are retried up to three times on HTTP 502, 503 and 504 errors.

Attributes:
socket_timeout: Timeout in seconds, used for both initial connection
delay and the maximum delay between bytes received.
Expand All @@ -50,7 +52,14 @@ def __init__(
if app_user_agent is not None:
ua = f"{app_user_agent} {ua}"

self._proxy_env = ProxyEnvironment(headers={"User-Agent": ua})
self._proxy_env = ProxyEnvironment(
headers={"User-Agent": ua},
retries=urllib3.Retry(
total=3,
status_forcelist=(502, 503, 504),
raise_on_status=False,
),
)

def _fetch(self, url: str) -> Iterator[bytes]:
"""Fetch the contents of HTTP/HTTPS url from a remote server.
Expand Down