@@ -309,67 +309,84 @@ def test_parse_params_mixed() -> None:
309309 }
310310
311311
312- RETRYABLE_TRANSPORT_ERRORS = (
313- # Impit raises a bare `HTTPError` for a body that ends mid-chunk, so even the generic base class is transient.
314- impit .HTTPError ,
315- impit .TimeoutException ,
316- impit .NetworkError ,
317- impit .RemoteProtocolError ,
318- impit .DecodingError ,
319- # A proxy rejecting the tunnel is often transient, e.g. one that is overloaded or rate-limiting.
320- impit .ProxyError ,
321- )
322- """Transport errors that must stay retryable."""
323-
324- NON_RETRYABLE_TRANSPORT_ERRORS = (
325- impit .UnsupportedProtocol ,
326- impit .LocalProtocolError ,
327- impit .TooManyRedirects ,
328- # No built-in client ever raises this one, because `_make_request` decides on status codes from the response.
329- impit .HTTPStatusError ,
312+ @pytest .mark .parametrize (
313+ 'exc' ,
314+ [
315+ # Impit wraps a failure its internal HTTP library did not classify in a bare `HTTPError`, so even the
316+ # generic base class is transient.
317+ pytest .param (impit .HTTPError ('unclassified failure' ), id = 'bare HTTPError' ),
318+ pytest .param (impit .TimeoutException ('timeout' ), id = 'TimeoutException' ),
319+ pytest .param (impit .NetworkError ('network error' ), id = 'NetworkError' ),
320+ pytest .param (impit .RemoteProtocolError ('remote protocol error' ), id = 'RemoteProtocolError' ),
321+ pytest .param (impit .DecodingError ('decoding error' ), id = 'DecodingError' ),
322+ # One `ProxyError` covers both a proxy rejecting the CONNECT tunnel and a 407, so a transient case cannot
323+ # be told from a permanent one - retrying is the safer default.
324+ pytest .param (impit .ProxyError ('proxy error' ), id = 'ProxyError' ),
325+ ],
330326)
331- """Transport errors that a retry cannot fix, so they must fail on the first attempt."""
327+ def test_is_retryable_error (exc : Exception ) -> None :
328+ """A transient transport failure is retried."""
329+ assert _is_retryable_error (exc )
332330
333331
334- def test_is_retryable_error () -> None :
335- """Transient transport failures are retried, and the ones a retry cannot fix are not."""
336- for error_class in RETRYABLE_TRANSPORT_ERRORS :
337- assert _is_retryable_error (error_class ('test' )), error_class .__name__
338-
339- for error_class in NON_RETRYABLE_TRANSPORT_ERRORS :
340- assert not _is_retryable_error (error_class ('test' )), error_class .__name__
341-
342- # `InvalidResponseBodyError` is raised by a resource client once `call` has returned, never inside the retry loop.
343- assert not _is_retryable_error (InvalidResponseBodyError (Mock ()))
344- assert not _is_retryable_error (ValueError ('test' ))
345- assert not _is_retryable_error (RuntimeError ('test' ))
346- assert not _is_retryable_error (Exception ('test' ))
332+ @pytest .mark .parametrize (
333+ 'exc' ,
334+ [
335+ pytest .param (impit .LocalProtocolError ('invalid header value' ), id = 'LocalProtocolError' ),
336+ pytest .param (impit .UnsupportedProtocol ('unsupported scheme' ), id = 'UnsupportedProtocol' ),
337+ pytest .param (impit .TooManyRedirects ('too many redirects' ), id = 'TooManyRedirects' ),
338+ pytest .param (impit .HTTPStatusError ('status error' ), id = 'HTTPStatusError' ),
339+ # Impit reports a bad URL outside the `impit.HTTPError` tree entirely.
340+ pytest .param (impit .InvalidURL ('unsupported scheme' ), id = 'InvalidURL' ),
341+ # `InvalidResponseBodyError` is raised by a resource client once `call` has returned, never in the retry loop.
342+ pytest .param (InvalidResponseBodyError (Mock ()), id = 'InvalidResponseBodyError' ),
343+ pytest .param (ValueError ('value error' ), id = 'ValueError' ),
344+ pytest .param (RuntimeError ('runtime error' ), id = 'RuntimeError' ),
345+ pytest .param (Exception ('generic exception' ), id = 'Exception' ),
346+ ],
347+ )
348+ def test_is_not_retryable_error (exc : Exception ) -> None :
349+ """A transport failure a retry cannot fix, and anything outside Impit's hierarchy, is not retried."""
350+ assert not _is_retryable_error (exc )
347351
348352
349353def test_permanent_transport_error_is_not_retried () -> None :
350354 """A transport error a retry cannot fix fails on the first attempt instead of burning the whole backoff."""
351355 client = ImpitHttpClient (token = 'test_token' , min_delay_between_retries = timedelta (0 ))
352- request = Mock (side_effect = impit .UnsupportedProtocol ( 'unsupported scheme ' ))
356+ request = Mock (side_effect = impit .LocalProtocolError ( 'invalid header value ' ))
353357 client ._impit_client = Mock (request = request )
354358
355- with pytest .raises (impit .UnsupportedProtocol ):
356- client .call (method = 'GET' , url = 'ftp ://api.test.com/endpoint' )
359+ with pytest .raises (impit .LocalProtocolError ):
360+ client .call (method = 'GET' , url = 'https ://api.test.com/endpoint' )
357361
358362 request .assert_called_once ()
359363
360364
361365async def test_permanent_transport_error_is_not_retried_async () -> None :
362366 """The async client applies the same policy, failing on the first attempt."""
363367 client = ImpitHttpClientAsync (token = 'test_token' , min_delay_between_retries = timedelta (0 ))
364- request = AsyncMock (side_effect = impit .UnsupportedProtocol ( 'unsupported scheme ' ))
368+ request = AsyncMock (side_effect = impit .LocalProtocolError ( 'invalid header value ' ))
365369 client ._impit_async_client = Mock (request = request )
366370
367- with pytest .raises (impit .UnsupportedProtocol ):
368- await client .call (method = 'GET' , url = 'ftp ://api.test.com/endpoint' )
371+ with pytest .raises (impit .LocalProtocolError ):
372+ await client .call (method = 'GET' , url = 'https ://api.test.com/endpoint' )
369373
370374 request .assert_awaited_once ()
371375
372376
377+ def test_transient_transport_error_is_retried () -> None :
378+ """A transient transport failure keeps being retried until the attempts run out."""
379+ client = ImpitHttpClient (token = 'test_token' , max_retries = 2 , min_delay_between_retries = timedelta (0 ))
380+ request = Mock (side_effect = impit .TimeoutException ('timeout' ))
381+ client ._impit_client = Mock (request = request )
382+
383+ with pytest .raises (impit .TimeoutException ):
384+ client .call (method = 'GET' , url = 'https://api.test.com/endpoint' )
385+
386+ # `max_retries` attempts inside the backoff loop, plus the final one it makes after the last delay.
387+ assert request .call_count == 3
388+
389+
373390@pytest .fixture (
374391 params = [
375392 pytest .param ((GzipHttpCompressor (), 'gzip' , gzip .decompress ), id = 'gzip' ),
0 commit comments