|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import httpx |
| 4 | +import pytest |
| 5 | + |
| 6 | +from durable_workflow.retry_policy import RetryPolicy |
| 7 | + |
| 8 | + |
| 9 | +class TestRetryPolicy: |
| 10 | + def test_should_retry_connection_error(self) -> None: |
| 11 | + policy = RetryPolicy(max_attempts=3) |
| 12 | + exc = httpx.ConnectError("connection failed") |
| 13 | + assert policy.should_retry(exc, attempt=0) is True |
| 14 | + assert policy.should_retry(exc, attempt=1) is True |
| 15 | + assert policy.should_retry(exc, attempt=2) is True |
| 16 | + assert policy.should_retry(exc, attempt=3) is False # max_attempts reached |
| 17 | + |
| 18 | + def test_should_retry_timeout(self) -> None: |
| 19 | + policy = RetryPolicy(max_attempts=3) |
| 20 | + exc = httpx.TimeoutException("timeout") |
| 21 | + assert policy.should_retry(exc, attempt=0) is True |
| 22 | + |
| 23 | + def test_should_retry_network_error(self) -> None: |
| 24 | + policy = RetryPolicy(max_attempts=3) |
| 25 | + exc = httpx.NetworkError("network error") |
| 26 | + assert policy.should_retry(exc, attempt=0) is True |
| 27 | + |
| 28 | + def test_should_retry_5xx_server_error(self) -> None: |
| 29 | + policy = RetryPolicy(max_attempts=3) |
| 30 | + response = httpx.Response(status_code=500, request=httpx.Request("GET", "http://test")) |
| 31 | + exc = httpx.HTTPStatusError("server error", request=response.request, response=response) |
| 32 | + assert policy.should_retry(exc, attempt=0) is True |
| 33 | + |
| 34 | + def test_should_retry_429_rate_limit(self) -> None: |
| 35 | + policy = RetryPolicy(max_attempts=3) |
| 36 | + response = httpx.Response(status_code=429, request=httpx.Request("GET", "http://test")) |
| 37 | + exc = httpx.HTTPStatusError("rate limited", request=response.request, response=response) |
| 38 | + assert policy.should_retry(exc, attempt=0) is True |
| 39 | + |
| 40 | + def test_should_not_retry_4xx_client_error(self) -> None: |
| 41 | + policy = RetryPolicy(max_attempts=3) |
| 42 | + response = httpx.Response(status_code=404, request=httpx.Request("GET", "http://test")) |
| 43 | + exc = httpx.HTTPStatusError("not found", request=response.request, response=response) |
| 44 | + assert policy.should_retry(exc, attempt=0) is False |
| 45 | + |
| 46 | + def test_should_not_retry_400_bad_request(self) -> None: |
| 47 | + policy = RetryPolicy(max_attempts=3) |
| 48 | + response = httpx.Response(status_code=400, request=httpx.Request("GET", "http://test")) |
| 49 | + exc = httpx.HTTPStatusError("bad request", request=response.request, response=response) |
| 50 | + assert policy.should_retry(exc, attempt=0) is False |
| 51 | + |
| 52 | + def test_should_not_retry_other_exceptions(self) -> None: |
| 53 | + policy = RetryPolicy(max_attempts=3) |
| 54 | + exc = ValueError("not a network error") |
| 55 | + assert policy.should_retry(exc, attempt=0) is False |
| 56 | + |
| 57 | + def test_backoff_calculation(self) -> None: |
| 58 | + policy = RetryPolicy( |
| 59 | + initial_backoff_seconds=0.1, |
| 60 | + max_backoff_seconds=5.0, |
| 61 | + backoff_multiplier=2.0, |
| 62 | + jitter=False, |
| 63 | + ) |
| 64 | + assert policy.backoff_seconds(0) == 0.1 |
| 65 | + assert policy.backoff_seconds(1) == 0.2 |
| 66 | + assert policy.backoff_seconds(2) == 0.4 |
| 67 | + assert policy.backoff_seconds(10) == 5.0 # capped at max |
| 68 | + |
| 69 | + def test_backoff_with_jitter(self) -> None: |
| 70 | + policy = RetryPolicy(initial_backoff_seconds=1.0, jitter=True) |
| 71 | + # Jitter should give us ±25% |
| 72 | + backoff = policy.backoff_seconds(0) |
| 73 | + assert 0.75 <= backoff <= 1.25 |
| 74 | + |
| 75 | + @pytest.mark.asyncio |
| 76 | + async def test_execute_success_on_first_try(self) -> None: |
| 77 | + policy = RetryPolicy(max_attempts=3) |
| 78 | + call_count = 0 |
| 79 | + |
| 80 | + async def fn() -> str: |
| 81 | + nonlocal call_count |
| 82 | + call_count += 1 |
| 83 | + return "success" |
| 84 | + |
| 85 | + result = await policy.execute(fn) |
| 86 | + assert result == "success" |
| 87 | + assert call_count == 1 |
| 88 | + |
| 89 | + @pytest.mark.asyncio |
| 90 | + async def test_execute_success_after_retry(self) -> None: |
| 91 | + policy = RetryPolicy(max_attempts=3, initial_backoff_seconds=0.01, jitter=False) |
| 92 | + call_count = 0 |
| 93 | + |
| 94 | + async def fn() -> str: |
| 95 | + nonlocal call_count |
| 96 | + call_count += 1 |
| 97 | + if call_count < 3: |
| 98 | + raise httpx.ConnectError("connection failed") |
| 99 | + return "success" |
| 100 | + |
| 101 | + result = await policy.execute(fn) |
| 102 | + assert result == "success" |
| 103 | + assert call_count == 3 |
| 104 | + |
| 105 | + @pytest.mark.asyncio |
| 106 | + async def test_execute_exhausted_retries(self) -> None: |
| 107 | + policy = RetryPolicy(max_attempts=3, initial_backoff_seconds=0.01, jitter=False) |
| 108 | + call_count = 0 |
| 109 | + |
| 110 | + async def fn() -> str: |
| 111 | + nonlocal call_count |
| 112 | + call_count += 1 |
| 113 | + raise httpx.ConnectError("connection failed") |
| 114 | + |
| 115 | + with pytest.raises(httpx.ConnectError): |
| 116 | + await policy.execute(fn) |
| 117 | + |
| 118 | + assert call_count == 3 |
| 119 | + |
| 120 | + @pytest.mark.asyncio |
| 121 | + async def test_execute_non_retryable_error(self) -> None: |
| 122 | + policy = RetryPolicy(max_attempts=3) |
| 123 | + call_count = 0 |
| 124 | + |
| 125 | + async def fn() -> str: |
| 126 | + nonlocal call_count |
| 127 | + call_count += 1 |
| 128 | + response = httpx.Response(status_code=400, request=httpx.Request("GET", "http://test")) |
| 129 | + raise httpx.HTTPStatusError("bad request", request=response.request, response=response) |
| 130 | + |
| 131 | + with pytest.raises(httpx.HTTPStatusError): |
| 132 | + await policy.execute(fn) |
| 133 | + |
| 134 | + assert call_count == 1 # Should not retry 400 errors |
| 135 | + |
| 136 | + @pytest.mark.asyncio |
| 137 | + async def test_execute_retries_500_but_not_400(self) -> None: |
| 138 | + policy = RetryPolicy(max_attempts=3, initial_backoff_seconds=0.01, jitter=False) |
| 139 | + call_count = 0 |
| 140 | + |
| 141 | + async def fn() -> str: |
| 142 | + nonlocal call_count |
| 143 | + call_count += 1 |
| 144 | + if call_count == 1: |
| 145 | + response = httpx.Response(status_code=500, request=httpx.Request("GET", "http://test")) |
| 146 | + raise httpx.HTTPStatusError("server error", request=response.request, response=response) |
| 147 | + return "success" |
| 148 | + |
| 149 | + result = await policy.execute(fn) |
| 150 | + assert result == "success" |
| 151 | + assert call_count == 2 # First attempt 500, second attempt success |
0 commit comments