|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import timedelta |
| 4 | +from typing import TYPE_CHECKING |
| 5 | +from unittest.mock import AsyncMock, Mock |
| 6 | + |
| 7 | +import impit |
| 8 | +import pytest |
| 9 | + |
| 10 | +from apify_client.http_clients import ImpitHttpClient, ImpitHttpClientAsync |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from _pytest.monkeypatch import MonkeyPatch |
| 14 | + |
| 15 | + |
| 16 | +def successful_response() -> Mock: |
| 17 | + return Mock(status_code=200) |
| 18 | + |
| 19 | + |
| 20 | +def test_generic_http_error_is_retried(monkeypatch: MonkeyPatch) -> None: |
| 21 | + """A bare Impit HTTPError from a truncated body remains retryable.""" |
| 22 | + client = ImpitHttpClient(min_delay_between_retries=timedelta(0)) |
| 23 | + send_request = Mock( |
| 24 | + side_effect=[impit.HTTPError('unexpected EOF'), impit.HTTPError('unexpected EOF'), successful_response()] |
| 25 | + ) |
| 26 | + monkeypatch.setattr(client, 'send_request', send_request) |
| 27 | + |
| 28 | + response = client.call(method='GET', url='https://example.com') |
| 29 | + |
| 30 | + assert response.status_code == 200 |
| 31 | + assert send_request.call_count == 3 |
| 32 | + |
| 33 | + |
| 34 | +async def test_generic_http_error_is_retried_async(monkeypatch: MonkeyPatch) -> None: |
| 35 | + """The async Impit adapter treats a bare HTTPError from a truncated body the same way.""" |
| 36 | + client = ImpitHttpClientAsync(min_delay_between_retries=timedelta(0)) |
| 37 | + send_request = AsyncMock( |
| 38 | + side_effect=[impit.HTTPError('unexpected EOF'), impit.HTTPError('unexpected EOF'), successful_response()] |
| 39 | + ) |
| 40 | + monkeypatch.setattr(client, 'send_request', send_request) |
| 41 | + |
| 42 | + response = await client.call(method='GET', url='https://example.com') |
| 43 | + |
| 44 | + assert response.status_code == 200 |
| 45 | + assert send_request.call_count == 3 |
| 46 | + |
| 47 | + |
| 48 | +def test_permanent_transport_error_is_not_retried(monkeypatch: MonkeyPatch) -> None: |
| 49 | + """A transport error a retry cannot fix must fail on the first attempt instead of burning the whole backoff.""" |
| 50 | + client = ImpitHttpClient(min_delay_between_retries=timedelta(0)) |
| 51 | + send_request = Mock(side_effect=impit.UnsupportedProtocol('unsupported scheme')) |
| 52 | + monkeypatch.setattr(client, 'send_request', send_request) |
| 53 | + |
| 54 | + with pytest.raises(impit.UnsupportedProtocol): |
| 55 | + client.call(method='GET', url='https://example.com') |
| 56 | + |
| 57 | + assert send_request.call_count == 1 |
| 58 | + |
| 59 | + |
| 60 | +def test_error_response_read_failure_uses_transport_retry_policy(monkeypatch: MonkeyPatch) -> None: |
| 61 | + """Failures while reading a streamed 5xx error body are classified and retried like send failures.""" |
| 62 | + client = ImpitHttpClient(max_retries=1, min_delay_between_retries=timedelta(0)) |
| 63 | + responses = [ |
| 64 | + Mock(status_code=500, read=Mock(side_effect=impit.ReadError('truncated')), close=Mock()) for _ in range(2) |
| 65 | + ] |
| 66 | + send_request = Mock(side_effect=responses) |
| 67 | + monkeypatch.setattr(client, 'send_request', send_request) |
| 68 | + |
| 69 | + with pytest.raises(impit.ReadError): |
| 70 | + client.call(method='GET', url='https://example.com', stream=True) |
| 71 | + |
| 72 | + assert send_request.call_count == 2 |
| 73 | + for response in responses: |
| 74 | + response.close.assert_called_once() |
| 75 | + |
| 76 | + |
| 77 | +async def test_error_response_read_failure_uses_transport_retry_policy_async(monkeypatch: MonkeyPatch) -> None: |
| 78 | + """The async base also classifies failures while buffering streamed error responses.""" |
| 79 | + client = ImpitHttpClientAsync(max_retries=1, min_delay_between_retries=timedelta(0)) |
| 80 | + responses = [ |
| 81 | + Mock(status_code=500, aread=AsyncMock(side_effect=impit.ReadError('truncated')), aclose=AsyncMock()) |
| 82 | + for _ in range(2) |
| 83 | + ] |
| 84 | + send_request = AsyncMock(side_effect=responses) |
| 85 | + monkeypatch.setattr(client, 'send_request', send_request) |
| 86 | + |
| 87 | + with pytest.raises(impit.ReadError): |
| 88 | + await client.call(method='GET', url='https://example.com', stream=True) |
| 89 | + |
| 90 | + assert send_request.call_count == 2 |
| 91 | + for response in responses: |
| 92 | + response.aclose.assert_awaited_once() |
0 commit comments