Files
payment-processing-test/tests/unit/test_retry.py
T

52 lines
1.1 KiB
Python

import pytest
from app.processing import retry_async
async def test_retry_uses_exponential_delay_and_returns_success() -> None:
calls = 0
delays: list[float] = []
async def operation() -> str:
nonlocal calls
calls += 1
if calls < 3:
raise RuntimeError("temporary failure")
return "processed"
async def record_sleep(delay: float) -> None:
delays.append(delay)
result = await retry_async(
operation,
attempts=3,
base_delay=1,
sleep=record_sleep,
)
assert result == "processed"
assert calls == 3
assert delays == [1, 2]
async def test_retry_raises_last_error_after_three_attempts() -> None:
calls = 0
async def operation() -> None:
nonlocal calls
calls += 1
raise RuntimeError(f"failure {calls}")
async def no_sleep(_: float) -> None:
pass
with pytest.raises(RuntimeError, match="failure 3"):
await retry_async(
operation,
attempts=3,
base_delay=1,
sleep=no_sleep,
)
assert calls == 3