Files
wehub-resource-sync 6ede33ccdb
Build and Push Docker Images / create_manifest (web, surfsense-web, , cpu) (push) Has been cancelled
Build and Push Docker Images / finalize_release (push) Has been cancelled
Obsidian Plugin Lint / lint (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-24.04-arm, linux/arm64, arm64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-latest, linux/amd64, amd64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / compute_version (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / verify_digests (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, , cpu) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda, cuda) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda126, cuda126) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:33:44 +08:00

75 lines
2.1 KiB
Python

"""Lock the ``with_retries`` policy: budget, recovery, exhaustion, timeout, backoff.
Tests with ``backoff="none"`` to keep wall-clock time zero. Backoff sleep
values themselves are observed by monkeypatching ``asyncio.sleep`` so we
don't introduce flakiness via real timing.
"""
from __future__ import annotations
import pytest
from app.automations.runtime.retries import with_retries
pytestmark = pytest.mark.unit
async def test_with_retries_returns_result_and_attempts_one_on_first_success() -> None:
"""A coroutine that succeeds on the first call returns its result
paired with ``attempts=1`` — no retry consumed."""
calls = 0
async def succeed() -> str:
nonlocal calls
calls += 1
return "ok"
result, attempts = await with_retries(
succeed, max_retries=2, backoff="none", timeout=None
)
assert result == "ok"
assert attempts == 1
assert calls == 1
async def test_with_retries_returns_attempt_count_when_succeeding_after_failures() -> (
None
):
"""A coroutine that fails twice then succeeds returns ``attempts=3``
(the actual attempt that produced the result). Locks the contract
that the caller can distinguish first-try success from a recovery."""
calls = 0
async def flaky() -> str:
nonlocal calls
calls += 1
if calls < 3:
raise RuntimeError("transient")
return "ok"
result, attempts = await with_retries(
flaky, max_retries=5, backoff="none", timeout=None
)
assert result == "ok"
assert attempts == 3
assert calls == 3
async def test_with_retries_reraises_after_exhausting_the_budget() -> None:
"""When the coroutine raises on every attempt within
``1 + max_retries`` tries, the last exception propagates and the
handler is called exactly ``1 + max_retries`` times."""
calls = 0
async def always_fails() -> str:
nonlocal calls
calls += 1
raise RuntimeError(f"boom-{calls}")
with pytest.raises(RuntimeError, match="boom-3"):
await with_retries(always_fails, max_retries=2, backoff="none", timeout=None)
assert calls == 3 # 1 initial + 2 retries