| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from anthropic.lib._retry import is_fatal_status_error |
| 6 |
from anthropic.lib.environments._poller import _jitter, _backoff |
| 7 |
|
| 8 |
from .test_poller_method import _api_status_error |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.parametrize( |
| 12 |
("description", "attempt", "want"), |
| 13 |
[ |
| 14 |
("first failure backs off two seconds", 1, 2.0), |
| 15 |
("second failure doubles to four seconds", 2, 4.0), |
| 16 |
("very large attempt is capped at sixty seconds", 100, 60.0), |
| 17 |
], |
| 18 |
) |
| 19 |
def test_backoff(description: str, attempt: int, want: float) -> None: |
| 20 |
assert _backoff(attempt) == want, description |
| 21 |
|
| 22 |
|
| 23 |
def test_jitter_within_bounds() -> None: |
| 24 |
for _ in range(100): |
| 25 |
v = _jitter(1.0, 3.0) |
| 26 |
assert 1.0 <= v < 3.0 |
| 27 |
|
| 28 |
|
| 29 |
@pytest.mark.parametrize( |
| 30 |
("status", "fatal"), |
| 31 |
[ |
| 32 |
(400, True), |
| 33 |
(401, True), |
| 34 |
(403, True), |
| 35 |
(404, True), |
| 36 |
(408, False), |
| 37 |
(409, False), |
| 38 |
(412, True), |
| 39 |
(422, True), |
| 40 |
(429, False), |
| 41 |
(500, False), |
| 42 |
], |
| 43 |
) |
| 44 |
def test_is_fatal_status_error(status: int, fatal: bool) -> None: |
| 45 |
assert is_fatal_status_error(_api_status_error(status)) is fatal |
| 46 |
|
| 47 |
|
| 48 |
def test_is_fatal_status_error_ignores_non_api_errors() -> None: |
| 49 |
assert is_fatal_status_error(KeyError("bug")) is False |