forked from kenzuya/unshackle
30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
"""RnetSession.request logs "Retrying" only when a retry follows."""
|
|
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from unshackle.core import session as session_module
|
|
from unshackle.core.session import MaxRetriesError, RnetSession
|
|
|
|
|
|
@pytest.fixture
|
|
def rate_limited(monkeypatch):
|
|
s = RnetSession()
|
|
monkeypatch.setattr(s, "ensure_client", lambda: SimpleNamespace(request=lambda *a, **k: None))
|
|
monkeypatch.setattr(s, "get_sleep_time", lambda *a: 0)
|
|
monkeypatch.setattr(
|
|
session_module,
|
|
"RnetResponse",
|
|
lambda raw: SimpleNamespace(status_code=429, reason="Too Many Requests", ok=False),
|
|
)
|
|
return s
|
|
|
|
|
|
@pytest.mark.parametrize(("max_retries", "attempts"), [(0, ""), (2, " after 3 attempts")])
|
|
def test_spent_budget_names_the_attempts_and_host_not_the_url(rate_limited, max_retries, attempts):
|
|
with pytest.raises(MaxRetriesError) as raised:
|
|
rate_limited.get("https://example.invalid/x?hmac=secret", max_retries=max_retries)
|
|
|
|
assert str(raised.value) == f"GET example.invalid failed{attempts}: 429 Too Many Requests"
|