File size: 1,584 Bytes
ae5413a b9e710f ae5413a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
from unittest.mock import AsyncMock, MagicMock
import pytest
from src.middleware.retry import RetryMiddleware
pytestmark = pytest.mark.unit
@pytest.mark.asyncio
async def test_retry_middleware_succeeds_first_try():
"""RetryMiddleware should pass through on success."""
middleware = RetryMiddleware(max_attempts=3)
context = MagicMock()
next_fn = AsyncMock()
await middleware.process(context, next_fn)
next_fn.assert_called_once_with(context)
@pytest.mark.asyncio
async def test_retry_middleware_retries_on_429():
"""RetryMiddleware should retry on 429 rate limit."""
middleware = RetryMiddleware(max_attempts=3, min_wait=0.01)
context = MagicMock()
# First two calls fail with 429, third succeeds
call_count = 0
async def mock_next(ctx):
nonlocal call_count
call_count += 1
if call_count < 3:
error = Exception("Rate limited")
error.response = MagicMock(status_code=429)
raise error
await middleware.process(context, mock_next)
assert call_count == 3
@pytest.mark.asyncio
async def test_retry_middleware_raises_after_max_attempts():
"""RetryMiddleware should raise after max attempts exhausted."""
middleware = RetryMiddleware(max_attempts=2, min_wait=0.01)
context = MagicMock()
async def always_fails(ctx):
error = Exception("Always fails")
error.response = MagicMock(status_code=500)
raise error
with pytest.raises(Exception, match="Always fails"):
await middleware.process(context, always_fails)
|