from __future__ import annotations import unittest from collections.abc import Callable from typing import ParamSpec, TypeVar P = ParamSpec("P") T = TypeVar("T") def retry( operation: Callable[P, T], *args: P.args, max_attempts: int = 3, initial_delay: float = 0.5, backoff_factor: float = 2.0, retry_on: tuple[type[Exception], ...] = (Exception,), sleep: Callable[[float], None], **kwargs: P.kwargs, ) -> T: """Run an operation, retrying failures with exponential backoff.""" if max_attempts < 1: raise ValueError("max_attempts must be at least 1") if initial_delay < 0: raise ValueError("initial_delay cannot be negative") if backoff_factor < 1: raise ValueError("backoff_factor must be at least 1") delay = initial_delay for attempt in range(1, max_attempts + 1): try: return operation(*args, **kwargs) except retry_on: if attempt == max_attempts: raise # Delay grows exponentially after each failed attempt. sleep(delay) delay *= backoff_factor raise RuntimeError("unreachable") class RetryTests(unittest.TestCase): def test_retries_with_exponential_backoff(self) -> None: attempts = 0 delays: list[float] = [] def flaky() -> str: nonlocal attempts attempts += 1 if attempts < 3: raise ValueError("temporary failure") return "success" result = retry( flaky, max_attempts=4, initial_delay=0.25, backoff_factor=2, sleep=delays.append, ) self.assertEqual(result, "success") self.assertEqual(attempts, 3) self.assertEqual(delays, [0.25, 0.5]) def test_raises_after_attempts_are_exhausted(self) -> None: attempts = 0 def always_fails() -> None: nonlocal attempts attempts += 1 raise RuntimeError("still failing") with self.assertRaisesRegex(RuntimeError, "still failing"): retry(always_fails, max_attempts=3, sleep=lambda _: None) self.assertEqual(attempts, 3) if __name__ == "__main__": unittest.main()