Skip to content

Commit 92cbd92

Browse files
committed
test: Cover read failures on the async and non-retryable status paths
1 parent fbab73a commit 92cbd92

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎src/apify_client/http_clients/_impit.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ def _make_request(
252252
logger.debug('Status code is not retryable', extra={'status_code': response.status_code})
253253
stop_retrying()
254254

255-
# Read the response in case it is a stream, so we can raise the error properly. A read that fails is a
256-
# transport failure like any other, so it goes through the same classification as a failed send.
255+
# Read the response in case it is a stream, so we can raise the error properly. A failed read goes through
256+
# the same classification as a failed send.
257257
try:
258258
response.read()
259259
except Exception as exc:
@@ -521,8 +521,8 @@ async def _make_request(
521521
logger.debug('Status code is not retryable', extra={'status_code': response.status_code})
522522
stop_retrying()
523523

524-
# Read the response in case it is a stream, so we can raise the error properly. A read that fails is a
525-
# transport failure like any other, so it goes through the same classification as a failed send.
524+
# Read the response in case it is a stream, so we can raise the error properly. A failed read goes through
525+
# the same classification as a failed send.
526526
try:
527527
await response.aread()
528528
except Exception as exc:

‎tests/unit/test_http_clients.py‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ async def test_error_response_read_failure_is_retried_and_closed_async() -> None
353353
with pytest.raises(impit.ReadError):
354354
await client.call(method='GET', url='https://api.test.com/endpoint', stream=True)
355355

356-
assert request.call_count == 2
356+
assert request.await_count == 2
357357
for response in responses:
358358
response.aclose.assert_awaited_once()
359359

@@ -372,6 +372,34 @@ def test_non_retryable_error_response_read_failure_stops_retrying() -> None:
372372
response.close.assert_called_once()
373373

374374

375+
async def test_non_retryable_error_response_read_failure_stops_retrying_async() -> None:
376+
"""The async client also stops retrying when a read failure is not a transport error."""
377+
client = ImpitHttpClientAsync(token='test_token', min_delay_between_retries=timedelta(0))
378+
response = Mock(status_code=500, aread=AsyncMock(side_effect=ValueError('broken response')), aclose=AsyncMock())
379+
request = AsyncMock(return_value=response)
380+
client._impit_async_client = Mock(request=request)
381+
382+
with pytest.raises(ValueError, match='broken response'):
383+
await client.call(method='GET', url='https://api.test.com/endpoint', stream=True)
384+
385+
request.assert_awaited_once()
386+
response.aclose.assert_awaited_once()
387+
388+
389+
def test_error_response_read_failure_on_non_retryable_status_is_not_retried() -> None:
390+
"""A transient read failure on a status that is not retryable surfaces immediately instead of being retried."""
391+
client = ImpitHttpClient(token='test_token', min_delay_between_retries=timedelta(0))
392+
response = Mock(status_code=404, read=Mock(side_effect=impit.ReadError('truncated')), close=Mock())
393+
request = Mock(return_value=response)
394+
client._impit_client = Mock(request=request)
395+
396+
with pytest.raises(impit.ReadError):
397+
client.call(method='GET', url='https://api.test.com/endpoint', stream=True)
398+
399+
request.assert_called_once()
400+
response.close.assert_called_once()
401+
402+
375403
@pytest.fixture(
376404
params=[
377405
pytest.param((GzipHttpCompressor(), 'gzip', gzip.decompress), id='gzip'),

0 commit comments

Comments
 (0)