|
| 1 | +import inspect |
| 2 | +import time |
| 3 | +from collections.abc import Iterable |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from extapi.http.abc import AbstractExecutor, Addon |
| 9 | +from extapi.http.addons.auth import BearerAuthAddon |
| 10 | +from extapi.http.addons.retry import Retry5xxAddon |
| 11 | +from extapi.http.executors.retry import RetryableExecutor |
| 12 | +from extapi.http.types import ExecuteError, HttpExecuteError, RequestData, Response |
| 13 | + |
| 14 | + |
| 15 | +class _DummyExecutor(AbstractExecutor[Any]): |
| 16 | + def __init__(self, responses: Iterable[int | type[Exception] | Exception] = (200,)): |
| 17 | + self.call_count = 0 |
| 18 | + self._responses: list[int | type[Exception] | Exception] = list(responses) |
| 19 | + self._current_response_index = 0 |
| 20 | + |
| 21 | + async def execute(self, request: RequestData) -> Response: |
| 22 | + self.call_count += 1 |
| 23 | + if self._current_response_index < len(self._responses): |
| 24 | + response = self._responses[self._current_response_index] |
| 25 | + self._current_response_index += 1 |
| 26 | + else: # pragma: no cover |
| 27 | + response = self._responses[-1] # Return the last response if we've run out |
| 28 | + |
| 29 | + if isinstance(response, Exception) or ( |
| 30 | + inspect.isclass(response) and issubclass(response, Exception) |
| 31 | + ): |
| 32 | + raise response |
| 33 | + |
| 34 | + if isinstance(response, int): |
| 35 | + return Response(status=response, backend_response="heyo", url=request.url) |
| 36 | + |
| 37 | + raise BaseException( |
| 38 | + f"unexpected response type: {type(response)}" |
| 39 | + ) # pragma: no cover |
| 40 | + |
| 41 | + |
1 | 42 | class TestRetryExecutor: |
2 | | - def test(self): |
3 | | - pass |
| 43 | + async def test_basic(self, request_simple: RequestData): |
| 44 | + base = _DummyExecutor() |
| 45 | + executor = RetryableExecutor(base, retry_sleep_timeout=0) |
| 46 | + |
| 47 | + response = await executor.execute(request_simple) |
| 48 | + |
| 49 | + assert response.status == 200 |
| 50 | + assert response.backend_response == "heyo" |
| 51 | + assert response.url == request_simple.url |
| 52 | + |
| 53 | + async def test_retry_500_no_addon(self, request_simple: RequestData): |
| 54 | + base = _DummyExecutor(responses=[500, 200]) |
| 55 | + executor = RetryableExecutor(base, max_retries=2, retry_sleep_timeout=0) |
| 56 | + |
| 57 | + response = await executor.execute(request_simple) |
| 58 | + |
| 59 | + assert response.status == 500 |
| 60 | + assert base.call_count == 1 |
| 61 | + |
| 62 | + async def test_retry_500_with_addon(self, request_simple: RequestData): |
| 63 | + base = _DummyExecutor(responses=[500, 200]) |
| 64 | + executor = RetryableExecutor( |
| 65 | + base, |
| 66 | + max_retries=2, |
| 67 | + retry_sleep_timeout=0, |
| 68 | + addons=[ |
| 69 | + Retry5xxAddon(), |
| 70 | + ], |
| 71 | + ) |
| 72 | + |
| 73 | + response = await executor.execute(request_simple) |
| 74 | + |
| 75 | + assert response.status == 200 |
| 76 | + assert base.call_count == 2 |
| 77 | + |
| 78 | + async def test_max_retries_exceeded(self, request_simple: RequestData): |
| 79 | + base = _DummyExecutor(responses=[500, 500, 500]) |
| 80 | + executor = RetryableExecutor( |
| 81 | + base, |
| 82 | + max_retries=3, |
| 83 | + retry_sleep_timeout=0, |
| 84 | + addons=[ |
| 85 | + Retry5xxAddon(), |
| 86 | + ], |
| 87 | + ) |
| 88 | + |
| 89 | + response = await executor.execute(request_simple) |
| 90 | + |
| 91 | + assert response.status == 500 |
| 92 | + assert base.call_count == 3 |
| 93 | + |
| 94 | + async def test_non_retryable_status(self, request_simple: RequestData): |
| 95 | + base = _DummyExecutor(responses=[400]) |
| 96 | + executor = RetryableExecutor( |
| 97 | + base, |
| 98 | + max_retries=2, |
| 99 | + retry_sleep_timeout=0, |
| 100 | + addons=[ |
| 101 | + Retry5xxAddon(), |
| 102 | + ], |
| 103 | + ) |
| 104 | + |
| 105 | + response = await executor.execute(request_simple) |
| 106 | + |
| 107 | + assert response.status == 400 |
| 108 | + assert base.call_count == 1 |
| 109 | + |
| 110 | + async def test_retry_with_bearer_auth_error(self, request_simple: RequestData): |
| 111 | + base = _DummyExecutor(responses=[401, 401, 200]) |
| 112 | + executor = RetryableExecutor( |
| 113 | + base, |
| 114 | + max_retries=2, |
| 115 | + retry_sleep_timeout=0, |
| 116 | + addons=[BearerAuthAddon(lambda: "token")], |
| 117 | + ) |
| 118 | + |
| 119 | + response = await executor.execute(request_simple) |
| 120 | + |
| 121 | + assert response.status == 401 |
| 122 | + assert base.call_count == 2 |
| 123 | + |
| 124 | + async def test_retry_with_bearer_auth_success(self, request_simple: RequestData): |
| 125 | + base = _DummyExecutor(responses=[401, 401, 200]) |
| 126 | + executor = RetryableExecutor( |
| 127 | + base, |
| 128 | + max_retries=3, |
| 129 | + retry_sleep_timeout=0, |
| 130 | + addons=[BearerAuthAddon(lambda: "token")], |
| 131 | + ) |
| 132 | + |
| 133 | + response = await executor.execute(request_simple) |
| 134 | + |
| 135 | + assert response.status == 200 |
| 136 | + assert base.call_count == 3 |
| 137 | + |
| 138 | + async def test_custom_retry_timeout(self, request_simple: RequestData): |
| 139 | + base = _DummyExecutor(responses=[500, 200]) |
| 140 | + executor = RetryableExecutor( |
| 141 | + base, |
| 142 | + max_retries=2, |
| 143 | + retry_sleep_timeout=0, |
| 144 | + addons=[ |
| 145 | + Retry5xxAddon(retry_timeout=1), |
| 146 | + ], |
| 147 | + ) |
| 148 | + |
| 149 | + started_at = time.monotonic() |
| 150 | + response = await executor.execute(request_simple) |
| 151 | + elapsed = time.monotonic() - started_at |
| 152 | + |
| 153 | + assert elapsed >= 1 |
| 154 | + |
| 155 | + assert response.status == 200 |
| 156 | + assert base.call_count == 2 |
| 157 | + |
| 158 | + async def test_timeout_error(self, request_simple: RequestData): |
| 159 | + base = _DummyExecutor(responses=[TimeoutError, TimeoutError(), 200]) |
| 160 | + executor = RetryableExecutor(base, max_retries=3, retry_sleep_timeout=0) |
| 161 | + |
| 162 | + response = await executor.execute(request_simple) |
| 163 | + |
| 164 | + assert response.status == 200 |
| 165 | + assert base.call_count == 3 |
| 166 | + |
| 167 | + async def test_timeout_error_propagate(self, request_simple: RequestData): |
| 168 | + base = _DummyExecutor(responses=[TimeoutError, 200]) |
| 169 | + executor = RetryableExecutor(base, max_retries=1, retry_sleep_timeout=0) |
| 170 | + |
| 171 | + with pytest.raises(ExecuteError) as err: |
| 172 | + await executor.execute(request_simple) |
| 173 | + |
| 174 | + assert str(err.value) == "request failed after 1 retries: TimeoutError()" |
| 175 | + |
| 176 | + async def test_exception_success(self, request_simple: RequestData): |
| 177 | + base = _DummyExecutor(responses=[Exception("some error"), 200]) |
| 178 | + executor = RetryableExecutor(base, max_retries=3, retry_sleep_timeout=0) |
| 179 | + |
| 180 | + response = await executor.execute(request_simple) |
| 181 | + |
| 182 | + assert response.status == 200 |
| 183 | + assert base.call_count == 2 |
| 184 | + |
| 185 | + async def test_exception_propagate(self, request_simple: RequestData): |
| 186 | + base = _DummyExecutor(responses=[Exception("some error"), 200]) |
| 187 | + executor = RetryableExecutor(base, max_retries=1, retry_sleep_timeout=0) |
| 188 | + |
| 189 | + with pytest.raises(Exception) as err: |
| 190 | + await executor.execute(request_simple) |
| 191 | + |
| 192 | + assert str(err.value) == "request failed after 1 retries: Exception(some error)" |
| 193 | + |
| 194 | + async def test_propagate_execute_error(self, request_simple: RequestData): |
| 195 | + resp = Response(status=404, backend_response=None, url=request_simple.url) |
| 196 | + base = _DummyExecutor(responses=[HttpExecuteError(resp), 200]) |
| 197 | + executor = RetryableExecutor(base, max_retries=1, retry_sleep_timeout=0) |
| 198 | + |
| 199 | + with pytest.raises(ExecuteError) as err: |
| 200 | + await executor.execute(request_simple) |
| 201 | + |
| 202 | + assert str(err.value) == "HTTPExecuteError(url=https://example.com, status=404)" |
| 203 | + |
| 204 | + async def test_with_process_error_addon(self, request_simple: RequestData): |
| 205 | + class _Addon(Addon): |
| 206 | + async def process_error( |
| 207 | + self, request: RequestData, error: Exception |
| 208 | + ) -> None: |
| 209 | + raise Exception("error processing exception") |
| 210 | + |
| 211 | + base = _DummyExecutor(responses=[Exception("some error"), 200]) |
| 212 | + executor = RetryableExecutor( |
| 213 | + base, max_retries=1, retry_sleep_timeout=0, addons=[_Addon()] |
| 214 | + ) |
| 215 | + |
| 216 | + with pytest.raises(ExecuteError) as err: |
| 217 | + await executor.execute(request_simple) |
| 218 | + |
| 219 | + assert str(err.value) == "request failed after 1 retries: Exception(some error)" |
| 220 | + |
| 221 | + async def test_with_process_error_addon_success(self, request_simple: RequestData): |
| 222 | + class _Addon(Addon): |
| 223 | + async def process_error( |
| 224 | + self, request: RequestData, error: Exception |
| 225 | + ) -> None: |
| 226 | + raise Exception("error processing exception") |
| 227 | + |
| 228 | + base = _DummyExecutor(responses=[Exception("some error"), 200]) |
| 229 | + executor = RetryableExecutor( |
| 230 | + base, max_retries=2, retry_sleep_timeout=0, addons=[_Addon()] |
| 231 | + ) |
| 232 | + |
| 233 | + response = await executor.execute(request_simple) |
| 234 | + |
| 235 | + assert response.status == 200 |
| 236 | + assert base.call_count == 2 |
0 commit comments