|
| 1 | +"""Retry policy per spec 002 §6 (conformance C07/C08/C09). |
| 2 | +
|
| 3 | +Retryable: network errors, 408, 429, 5xx. Permanent client errors (other |
| 4 | +4xx) are dropped after the first attempt. A Retry-After header overrides the |
| 5 | +computed backoff delay. |
| 6 | +""" |
| 7 | + |
| 8 | +from unittest.mock import Mock |
| 9 | + |
| 10 | +import pytest |
| 11 | +import requests |
| 12 | + |
| 13 | +from logtide_sdk import ClientOptions, LogTideClient |
| 14 | +from logtide_sdk.models import LogEntry |
| 15 | +from logtide_sdk.enums import LogLevel |
| 16 | + |
| 17 | + |
| 18 | +def http_error(status: int, headers: dict | None = None) -> requests.HTTPError: |
| 19 | + response = Mock(spec=requests.Response) |
| 20 | + response.status_code = status |
| 21 | + response.headers = headers or {} |
| 22 | + return requests.HTTPError(f"HTTP {status}", response=response) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def client(): |
| 27 | + c = LogTideClient( |
| 28 | + ClientOptions( |
| 29 | + api_url="http://localhost:8080", |
| 30 | + api_key="lp_k", |
| 31 | + service="svc", |
| 32 | + retry_delay_ms=1, |
| 33 | + ) |
| 34 | + ) |
| 35 | + yield c |
| 36 | + c._closed = True |
| 37 | + |
| 38 | + |
| 39 | +def entry() -> LogEntry: |
| 40 | + return LogEntry(service="svc", level=LogLevel.INFO, message="m") |
| 41 | + |
| 42 | + |
| 43 | +def test_no_retry_on_permanent_4xx(client, mocker): |
| 44 | + send = mocker.patch.object(client, "_send_logs", side_effect=http_error(400)) |
| 45 | + sleep = mocker.patch("logtide_sdk.client.time.sleep") |
| 46 | + |
| 47 | + client._send_logs_with_retry([entry()]) |
| 48 | + |
| 49 | + assert send.call_count == 1, "400 must not be retried" |
| 50 | + sleep.assert_not_called() |
| 51 | + metrics = client.get_metrics() |
| 52 | + assert metrics.logs_dropped == 1 |
| 53 | + assert metrics.retries == 0 |
| 54 | + |
| 55 | + |
| 56 | +def test_no_retry_on_401(client, mocker): |
| 57 | + send = mocker.patch.object(client, "_send_logs", side_effect=http_error(401)) |
| 58 | + client._send_logs_with_retry([entry()]) |
| 59 | + assert send.call_count == 1 |
| 60 | + |
| 61 | + |
| 62 | +def test_retries_on_5xx_then_succeeds(client, mocker): |
| 63 | + send = mocker.patch.object( |
| 64 | + client, "_send_logs", side_effect=[http_error(500), None] |
| 65 | + ) |
| 66 | + mocker.patch("logtide_sdk.client.time.sleep") |
| 67 | + |
| 68 | + client._send_logs_with_retry([entry()]) |
| 69 | + |
| 70 | + assert send.call_count == 2 |
| 71 | + metrics = client.get_metrics() |
| 72 | + assert metrics.logs_sent == 1 |
| 73 | + assert metrics.retries == 1 |
| 74 | + |
| 75 | + |
| 76 | +def test_retries_on_408_and_429(client, mocker): |
| 77 | + send = mocker.patch.object( |
| 78 | + client, "_send_logs", side_effect=[http_error(408), http_error(429), None] |
| 79 | + ) |
| 80 | + mocker.patch("logtide_sdk.client.time.sleep") |
| 81 | + client._send_logs_with_retry([entry()]) |
| 82 | + assert send.call_count == 3 |
| 83 | + |
| 84 | + |
| 85 | +def test_retries_on_network_error(client, mocker): |
| 86 | + send = mocker.patch.object( |
| 87 | + client, |
| 88 | + "_send_logs", |
| 89 | + side_effect=[requests.ConnectionError("refused"), None], |
| 90 | + ) |
| 91 | + mocker.patch("logtide_sdk.client.time.sleep") |
| 92 | + client._send_logs_with_retry([entry()]) |
| 93 | + assert send.call_count == 2 |
| 94 | + |
| 95 | + |
| 96 | +def test_retry_after_overrides_backoff(client, mocker): |
| 97 | + mocker.patch.object( |
| 98 | + client, |
| 99 | + "_send_logs", |
| 100 | + side_effect=[http_error(429, {"Retry-After": "7"}), None], |
| 101 | + ) |
| 102 | + sleep = mocker.patch("logtide_sdk.client.time.sleep") |
| 103 | + |
| 104 | + client._send_logs_with_retry([entry()]) |
| 105 | + |
| 106 | + sleep.assert_called_once_with(7.0) |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.asyncio |
| 110 | +async def test_async_no_retry_on_permanent_4xx(mocker): |
| 111 | + import aiohttp |
| 112 | + |
| 113 | + from logtide_sdk.async_client import AsyncLogTideClient |
| 114 | + |
| 115 | + client = AsyncLogTideClient( |
| 116 | + ClientOptions(api_url="http://localhost:8080", api_key="lp_k", service="svc") |
| 117 | + ) |
| 118 | + try: |
| 119 | + error = aiohttp.ClientResponseError( |
| 120 | + request_info=Mock(), history=(), status=403, headers={} |
| 121 | + ) |
| 122 | + send = mocker.patch.object(client, "_send_logs", side_effect=error) |
| 123 | + await client._send_logs_with_retry([entry()]) |
| 124 | + assert send.call_count == 1, "403 must not be retried" |
| 125 | + finally: |
| 126 | + client._closed = True |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.asyncio |
| 130 | +async def test_async_retry_after_overrides_backoff(mocker): |
| 131 | + import aiohttp |
| 132 | + |
| 133 | + from logtide_sdk.async_client import AsyncLogTideClient |
| 134 | + |
| 135 | + client = AsyncLogTideClient( |
| 136 | + ClientOptions( |
| 137 | + api_url="http://localhost:8080", api_key="lp_k", service="svc", retry_delay_ms=1 |
| 138 | + ) |
| 139 | + ) |
| 140 | + try: |
| 141 | + error = aiohttp.ClientResponseError( |
| 142 | + request_info=Mock(), history=(), status=429, headers={"Retry-After": "5"} |
| 143 | + ) |
| 144 | + mocker.patch.object(client, "_send_logs", side_effect=[error, None]) |
| 145 | + sleep = mocker.patch("logtide_sdk.async_client.asyncio.sleep") |
| 146 | + await client._send_logs_with_retry([entry()]) |
| 147 | + sleep.assert_called_once_with(5.0) |
| 148 | + finally: |
| 149 | + client._closed = True |
0 commit comments