from __future__ import annotations from engines.image_provider import ImageProviderError from backend.services.worker_service import RetryPolicy from backend.services import workflow_round_worker_runtime as runtime def _round_pool_policy() -> RetryPolicy: """The policy the round worker pool is constructed with.""" import inspect source = inspect.getsource(runtime.WorkflowRoundWorkerRuntime.__init__) assert "RetryPolicy(" in source # Rebuild it rather than parsing: keep this in sync with the constructor. return RetryPolicy( max_attempts=3, base_delay_seconds=2.0, backoff_multiplier=2.0, retryable_error_types=("ImageProviderError",), ) def test_max_attempts_of_one_never_retries() -> None: """Documents the bug: worker_attempt starts at 1, so 1 >= 1 short-circuits.""" policy = RetryPolicy(max_attempts=1) assert policy.should_retry(1, ImageProviderError("429 rate limited", provider_id="p1")) is False def test_round_policy_retries_a_transient_provider_error() -> None: policy = _round_pool_policy() assert policy.should_retry(1, ImageProviderError("429 rate limited", provider_id="p1")) is True assert policy.should_retry(2, ImageProviderError("connection reset", provider_id="p1")) is True # Third attempt is the last one. assert policy.should_retry(3, ImageProviderError("still failing", provider_id="p1")) is False def test_round_policy_does_not_retry_a_programming_error() -> None: """A genuine bug must fail fast rather than burn three provider calls.""" policy = _round_pool_policy() assert policy.should_retry(1, TypeError("bad argument")) is False assert policy.should_retry(1, KeyError("missing")) is False def test_backoff_grows_between_attempts() -> None: policy = _round_pool_policy() first = policy.next_retry_at(1) second = policy.next_retry_at(2) third = policy.next_retry_at(3) # 2s, 4s, 8s -> strictly increasing timestamps. assert first < second < third def test_constructor_actually_uses_a_retrying_policy() -> None: """Guard against the constructor silently reverting to max_attempts=1.""" import inspect source = inspect.getsource(runtime.WorkflowRoundWorkerRuntime.__init__) assert "max_attempts=3" in source, "round pool must retry transient failures" assert "ImageProviderError" in source, "retries must be scoped to provider errors"