Build a deadline-aware retry simulator
Implement simulate_retry(outcomes, deadline, max_attempts=3, base=1.0, idempotent=True). Each outcome is a (duration, status) pair. Return (terminal_status, tuple_of_start_times, elapsed_time) without real waiting.
Your task
- Accept nonnegative finite deadline, base, and durations; require positive integer max_attempts excluding booleans, boolean idempotent, and status ok, transient, or permanent. Raise ValueError for invalid inputs.
- An attempt starts only strictly before the deadline. A result arriving exactly at the deadline can succeed. A duration exceeding remaining time ends at the deadline with status deadline.
- Stop on ok or permanent. Stop on the first transient outcome with status unsafe if idempotent is false.
- Between retryable attempts wait base * 2 ** (attempt_number - 1). If the next start would be at or after the deadline, return deadline with elapsed_time equal to the deadline.
- When the attempt limit or supplied outcomes are exhausted after a transient result, return exhausted without an extra wait. No outcomes returns exhausted at time zero unless the deadline is zero, which returns deadline.
- Do not sleep or call a network. This is a deterministic policy simulator; real jitter and receiver deduplication are outside its contract.
Examples
EXAMPLE 1
Inputsimulate_retry([(1, "transient"), (1, "ok")], 5)
Output("ok", (0.0, 2.0), 3.0)
Implement the function, then run the tests.
Ctrl / ⌘ + Enter to test · Shift + Tab leaves editor