Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /ctrl-gaurav_effGen /tests /reliability /test_retry.py
| """ | |
| Tests for effGen retry primitives. | |
| Coverage: | |
| - Retry.compute_delay() — exponential + jitter + Retry-After | |
| - retryable() decorator — sync & async | |
| - RetryExhausted raised after max_attempts | |
| - Non-retryable exceptions re-raised immediately | |
| - is_transient_error() classifier | |
| - OTel span event emission (in-memory exporter) | |
| """ | |
| from __future__ import annotations | |
| from unittest.mock import MagicMock, patch | |
| import pytest | |
| from effgen.reliability.retry import ( | |
| Retry, | |
| RetryExhausted, | |
| is_transient_error, | |
| retryable, | |
| ) | |
| pytestmark = pytest.mark.reliability | |
| # --------------------------------------------------------------------------- | |
| # is_transient_error | |
| # --------------------------------------------------------------------------- | |
| class TestIsTransientError: | |
| def test_connection_error(self): | |
| assert is_transient_error(ConnectionError("reset")) | |
| def test_timeout_error(self): | |
| assert is_transient_error(TimeoutError("timed out")) | |
| def test_os_error(self): | |
| assert is_transient_error(OSError("network unreachable")) | |
| def test_asyncio_timeout(self): | |
| assert is_transient_error(TimeoutError()) | |
| def test_effgen_timeout(self): | |
| from effgen.reliability.timeouts import TimeoutError as EffGenTO | |
| assert is_transient_error(EffGenTO("model_call", 60.0)) | |
| def test_http_429_via_status_code(self): | |
| exc = Exception("rate limited") | |
| exc.status_code = 429 # type: ignore[attr-defined] | |
| assert is_transient_error(exc) | |
| def test_http_500_via_status_code(self): | |
| exc = Exception("internal server error") | |
| exc.status_code = 500 # type: ignore[attr-defined] | |
| assert is_transient_error(exc) | |
| def test_http_503_via_status_code(self): | |
| exc = Exception("service unavailable") | |
| exc.status_code = 503 # type: ignore[attr-defined] | |
| assert is_transient_error(exc) | |
| def test_http_429_via_response(self): | |
| exc = Exception("rate limited") | |
| resp = MagicMock() | |
| resp.status_code = 429 | |
| exc.response = resp # type: ignore[attr-defined] | |
| assert is_transient_error(exc) | |
| def test_http_400_not_retryable(self): | |
| exc = Exception("bad request") | |
| exc.status_code = 400 # type: ignore[attr-defined] | |
| assert not is_transient_error(exc) | |
| def test_a_413_that_is_really_a_tokens_per_minute_limit_is_rate_limited(self): | |
| """Groq reports a spent per-minute token budget as 413, not 429. | |
| The payload says so — ``'code': 'rate_limit_exceeded'`` and "tokens per | |
| minute (TPM)" — but a blanket 413 rule reads it as an oversized request, | |
| which is permanent. The caller then neither backs off nor reports a | |
| throttle, and the same budget would have served the request a minute | |
| later. | |
| """ | |
| from effgen.models.errors import classify_provider_error | |
| exc = Exception( | |
| "Error code: 413 - {'error': {'message': 'Request too large for model " | |
| "`llama-3.1-8b-instant` in organization `org_x` service tier `on_demand` " | |
| "on tokens per minute (TPM): Limit 6000, Requested 8212, please reduce " | |
| "your message size and try again.', 'type': 'tokens', " | |
| "'code': 'rate_limit_exceeded'}}" | |
| ) | |
| exc.status_code = 413 # type: ignore[attr-defined] | |
| result = classify_provider_error(exc) | |
| assert result.category == "rate_limited" | |
| assert result.rate_limited is True | |
| # And through the shape an adapter actually raises, which is what the | |
| # retry layer sees. | |
| from effgen.models._adapter_utils import provider_runtime_error | |
| wrapped = provider_runtime_error( | |
| "groq", "llama-3.1-8b-instant", "generate", exc | |
| ) | |
| assert is_transient_error(wrapped) | |
| assert classify_provider_error(wrapped).rate_limited is True | |
| def test_an_ordinary_413_is_still_an_invalid_request(self): | |
| """A genuinely oversized payload is a property of the request.""" | |
| from effgen.models.errors import classify_provider_error | |
| exc = Exception("Error code: 413 - payload too large: body exceeds 10 MB") | |
| exc.status_code = 413 # type: ignore[attr-defined] | |
| assert classify_provider_error(exc).category == "invalid_request" | |
| def test_value_error_not_retryable(self): | |
| assert not is_transient_error(ValueError("bad input")) | |
| def test_runtime_error_not_retryable(self): | |
| assert not is_transient_error(RuntimeError("logic error")) | |
| # -- adapter-wrapped provider errors (the shape every real adapter | |
| # actually raises via effgen.models._adapter_utils.provider_runtime_error) -- | |
| def test_wrapped_429_is_retryable(self): | |
| from effgen.models._adapter_utils import provider_runtime_error | |
| class _FakeSDKError(Exception): | |
| status_code = 429 | |
| wrapped = provider_runtime_error("groq", "llama-3.1-8b-instant", "chat", _FakeSDKError("rate limited")) | |
| assert is_transient_error(wrapped) | |
| def test_wrapped_5xx_is_retryable(self): | |
| from effgen.models._adapter_utils import provider_runtime_error | |
| class _FakeSDKError(Exception): | |
| status_code = 503 | |
| wrapped = provider_runtime_error("openai", "gpt-5-nano", "chat", _FakeSDKError("service unavailable")) | |
| assert is_transient_error(wrapped) | |
| def test_wrapped_400_is_not_retryable(self): | |
| from effgen.models._adapter_utils import provider_runtime_error | |
| class _FakeSDKError(Exception): | |
| status_code = 400 | |
| wrapped = provider_runtime_error("openai", "gpt-5-nano", "chat", _FakeSDKError("bad request")) | |
| assert not is_transient_error(wrapped) | |
| def test_wrapped_auth_error_is_not_retryable(self): | |
| from effgen.models._adapter_utils import provider_runtime_error | |
| class _FakeSDKError(Exception): | |
| status_code = 401 | |
| wrapped = provider_runtime_error("openai", "gpt-5-nano", "chat", _FakeSDKError("invalid api key")) | |
| assert not is_transient_error(wrapped) | |
| def test_wrapped_timeout_is_retryable(self): | |
| from effgen.models._adapter_utils import provider_runtime_error | |
| from effgen.reliability.timeouts import TimeoutError as EffGenTO | |
| wrapped = provider_runtime_error("openai", "gpt-5-nano", "chat", EffGenTO("model_call", 2.0)) | |
| assert is_transient_error(wrapped) | |
| def test_typed_model_auth_error_not_retryable(self): | |
| from effgen.models.errors import ModelAuthError | |
| assert not is_transient_error(ModelAuthError("groq", "llama-3.1-8b-instant", "bad key")) | |
| def test_typed_provider_transient_error_retryable(self): | |
| from effgen.models.errors import ProviderTransientError | |
| assert is_transient_error(ProviderTransientError("openai", "gpt-5-nano", 503)) | |
| def test_typed_invalid_request_error_not_retryable(self): | |
| from effgen.models.errors import InvalidRequestError | |
| assert not is_transient_error(InvalidRequestError("openai", "gpt-5-nano", "bad schema")) | |
| def test_rate_limit_exceeded_is_retryable(self): | |
| from effgen.models._rate_limit import RateLimitExceeded | |
| exc = RateLimitExceeded("Daily request budget exhausted for groq/llama-3.1-8b-instant.") | |
| assert is_transient_error(exc) | |
| def test_rate_limit_exceeded_carries_error_context(self): | |
| from effgen.models._rate_limit import RateLimitExceeded | |
| exc = RateLimitExceeded("Daily budget exhausted.") | |
| assert exc.error_context["category"] == "rate_limited" | |
| def test_agrees_with_classify_provider_error_and_router_retry_policy(self): | |
| """The three classifiers agree on every *typed* provider error the | |
| router itself ever sees (see effgen.models.routing.retry's module | |
| docstring for the router's deliberate, documented divergence on | |
| BudgetExceededError and on unrecognized exceptions — those aren't | |
| covered here since they're intentionally different by design).""" | |
| from effgen.models._rate_limit import RateLimitExceeded | |
| from effgen.models.errors import ( | |
| ModelTimeoutError, | |
| ProviderTransientError, | |
| classify_provider_error, | |
| ) | |
| from effgen.models.routing.retry import RetryPolicy | |
| cases = [ | |
| RateLimitExceeded("Daily budget exhausted."), | |
| ProviderTransientError("openai", "m", 503), | |
| ModelTimeoutError("groq", "m"), | |
| ] | |
| for exc in cases: | |
| assert is_transient_error(exc) is True | |
| assert classify_provider_error(exc).should_retry is True | |
| assert RetryPolicy().is_retriable(exc) is True | |
| # --------------------------------------------------------------------------- | |
| # Retry.compute_delay | |
| # --------------------------------------------------------------------------- | |
| class TestRetryComputeDelay: | |
| def test_exponential_growth(self): | |
| r = Retry(base_delay=1.0, max_delay=100.0, jitter=False) | |
| delays = [r.compute_delay(i) for i in range(5)] | |
| # Should be 1, 2, 4, 8, 16 (capped by max_delay) | |
| assert delays[0] == pytest.approx(1.0) | |
| assert delays[1] == pytest.approx(2.0) | |
| assert delays[2] == pytest.approx(4.0) | |
| assert delays[3] == pytest.approx(8.0) | |
| assert delays[4] == pytest.approx(16.0) | |
| def test_capped_by_max_delay(self): | |
| r = Retry(base_delay=1.0, max_delay=5.0, jitter=False) | |
| delay = r.compute_delay(10) # Would be 1024s without cap | |
| assert delay == pytest.approx(5.0) | |
| def test_jitter_adds_randomness(self): | |
| r = Retry(base_delay=1.0, max_delay=100.0, jitter=True) | |
| delays = {r.compute_delay(0) for _ in range(20)} | |
| # With jitter the delays should not all be the same | |
| assert len(delays) > 1 | |
| def test_jitter_within_bounds(self): | |
| r = Retry(base_delay=1.0, max_delay=100.0, jitter=True) | |
| for _ in range(50): | |
| d = r.compute_delay(0) | |
| assert d >= 1.0 # base_delay | |
| assert d <= 100.0 # max_delay | |
| def test_honour_retry_after(self): | |
| r = Retry(base_delay=1.0, max_delay=100.0, jitter=False, honour_retry_after=True) | |
| exc = Exception("too many requests") | |
| exc.retry_after = 7.5 # type: ignore[attr-defined] | |
| delay = r.compute_delay(0, exc) | |
| assert delay == pytest.approx(7.5) | |
| def test_retry_after_capped_by_max_delay(self): | |
| r = Retry(base_delay=1.0, max_delay=5.0, jitter=False, honour_retry_after=True) | |
| exc = Exception("too many requests") | |
| exc.retry_after = 999.0 # type: ignore[attr-defined] | |
| delay = r.compute_delay(0, exc) | |
| assert delay == pytest.approx(5.0) | |
| def test_retry_after_from_response_header(self): | |
| r = Retry(base_delay=1.0, max_delay=100.0, jitter=False, honour_retry_after=True) | |
| exc = Exception("rate limited") | |
| resp = MagicMock() | |
| resp.headers = {"retry-after": "3"} | |
| exc.response = resp # type: ignore[attr-defined] | |
| delay = r.compute_delay(0, exc) | |
| assert delay == pytest.approx(3.0) | |
| # --------------------------------------------------------------------------- | |
| # @retryable decorator — synchronous | |
| # --------------------------------------------------------------------------- | |
| class TestRetryableSync: | |
| def test_succeeds_first_attempt(self): | |
| calls = [] | |
| def fn(): | |
| calls.append(1) | |
| return "ok" | |
| assert fn() == "ok" | |
| assert len(calls) == 1 | |
| def test_retries_on_transient_error(self): | |
| calls = [] | |
| def fn(): | |
| calls.append(1) | |
| if len(calls) < 3: | |
| raise ConnectionError("temporary") | |
| return "done" | |
| assert fn() == "done" | |
| assert len(calls) == 3 | |
| def test_raises_retry_exhausted(self): | |
| def fn(): | |
| raise ConnectionError("always fails") | |
| with pytest.raises(RetryExhausted) as exc_info: | |
| fn() | |
| assert exc_info.value.attempts == 3 | |
| assert isinstance(exc_info.value.last_error, ConnectionError) | |
| def test_non_retryable_reraises_immediately(self): | |
| calls = [] | |
| def fn(): | |
| calls.append(1) | |
| raise ValueError("bad input — not retryable") | |
| with pytest.raises(ValueError, match="bad input"): | |
| fn() | |
| assert len(calls) == 1 # Only called once, no retry | |
| def test_custom_retryable_predicate(self): | |
| """Only retry ZeroDivisionError, not ValueError.""" | |
| calls = [] | |
| policy = Retry( | |
| max_attempts=3, | |
| base_delay=0.0, | |
| jitter=False, | |
| retryable=lambda exc: isinstance(exc, ZeroDivisionError), | |
| ) | |
| def fn(): | |
| calls.append(1) | |
| if len(calls) < 3: | |
| raise ZeroDivisionError("divide by zero") | |
| return "recovered" | |
| assert fn() == "recovered" | |
| assert len(calls) == 3 | |
| def test_preserves_function_name(self): | |
| def my_special_function(): | |
| return "x" | |
| assert my_special_function.__name__ == "my_special_function" | |
| def test_max_attempts_one_no_retry(self): | |
| calls = [] | |
| def fn(): | |
| calls.append(1) | |
| raise ConnectionError("fail") | |
| with pytest.raises(RetryExhausted): | |
| fn() | |
| assert len(calls) == 1 | |
| # --------------------------------------------------------------------------- | |
| # @retryable decorator — async | |
| # --------------------------------------------------------------------------- | |
| class TestRetryableAsync: | |
| async def test_async_succeeds_first_attempt(self): | |
| calls = [] | |
| async def fn(): | |
| calls.append(1) | |
| return "async_ok" | |
| assert await fn() == "async_ok" | |
| assert len(calls) == 1 | |
| async def test_async_retries_on_transient_error(self): | |
| calls = [] | |
| async def fn(): | |
| calls.append(1) | |
| if len(calls) < 4: | |
| raise TimeoutError() | |
| return "async_done" | |
| assert await fn() == "async_done" | |
| assert len(calls) == 4 | |
| async def test_async_raises_retry_exhausted(self): | |
| async def fn(): | |
| raise ConnectionError("always") | |
| with pytest.raises(RetryExhausted) as exc_info: | |
| await fn() | |
| assert exc_info.value.attempts == 2 | |
| async def test_async_non_retryable_reraises(self): | |
| calls = [] | |
| async def fn(): | |
| calls.append(1) | |
| raise TypeError("type error") | |
| with pytest.raises(TypeError): | |
| await fn() | |
| assert len(calls) == 1 | |
| async def test_async_preserves_name(self): | |
| async def my_async_fn(): | |
| return "x" | |
| assert my_async_fn.__name__ == "my_async_fn" | |
| # --------------------------------------------------------------------------- | |
| # OTel span event emission | |
| # --------------------------------------------------------------------------- | |
| class TestRetryOtelEvents: | |
| def test_emits_retry_event_on_span(self): | |
| """Retry should add a span event with attempt/reason/delay attributes.""" | |
| try: | |
| from opentelemetry.sdk.trace import TracerProvider | |
| from opentelemetry.sdk.trace.export import SimpleSpanProcessor | |
| from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( | |
| InMemorySpanExporter, | |
| ) | |
| except ImportError: | |
| pytest.skip("opentelemetry-sdk not installed") | |
| exporter = InMemorySpanExporter() | |
| provider = TracerProvider() | |
| provider.add_span_processor(SimpleSpanProcessor(exporter)) | |
| tracer = provider.get_tracer("test") | |
| attempts = [] | |
| def flaky(): | |
| attempts.append(1) | |
| if len(attempts) < 3: | |
| raise ConnectionError("flaky") | |
| return "ok" | |
| with tracer.start_as_current_span("test.span"): | |
| result = flaky() | |
| assert result == "ok" | |
| spans = exporter.get_finished_spans() | |
| assert len(spans) >= 1 | |
| # Find the retry events on the test span | |
| test_span = spans[0] | |
| retry_events = [e for e in test_span.events if "retry" in e.name.lower()] | |
| assert len(retry_events) >= 2, f"Expected ≥2 retry events, got: {[e.name for e in test_span.events]}" | |
| def test_otel_not_installed_is_noop(self): | |
| """If OTel not available, retry still works — no ImportError.""" | |
| import effgen.reliability.retry as retry_mod | |
| with patch.object(retry_mod, "_emit_retry_event", side_effect=ImportError("no otel")): | |
| calls = [] | |
| def fn(): | |
| calls.append(1) | |
| if len(calls) == 1: | |
| raise ConnectionError("fail once") | |
| return "ok" | |
| # Should succeed despite _emit_retry_event raising ImportError | |
| # (the function catches all exceptions in that path) | |
| # Actually the mock will raise on emit, but retry should still work | |
| # Let's just verify no crash propagates | |
| try: | |
| fn() | |
| except Exception: | |
| pass # Acceptable — the key is no unhandled ImportError crash | |
| # --------------------------------------------------------------------------- | |
| # RetryExhausted metadata | |
| # --------------------------------------------------------------------------- | |
| class TestRetryExhausted: | |
| def test_attributes(self): | |
| exc = RetryExhausted(5, ValueError("root cause")) | |
| assert exc.attempts == 5 | |
| assert isinstance(exc.last_error, ValueError) | |
| assert "5" in str(exc) | |
| assert "ValueError" in str(exc) | |
Xet Storage Details
- Size:
- 18.8 kB
- Xet hash:
- 8a909e6f56c16b1d3b1ca1319410ffedf0f4971bc09f6bc38d65df77ca1579d8
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.