Files
2026-11-12 19:38:03 +03:00

54 lines
1.6 KiB
Python

"""Изолированная проверка HTTP-бюджета readiness при медленной отмене драйвера."""
import asyncio
import time
from unittest.mock import patch
from src.core import readiness
from src.core.errors import ApiError
from src.core.settings import settings
async def main():
cancelled = asyncio.Event()
release = asyncio.Event()
calls = 0
async def stalled_driver():
nonlocal calls
calls += 1
try:
await asyncio.Event().wait()
except asyncio.CancelledError:
cancelled.set()
await release.wait()
with (
patch.object(settings, "healthcheck_timeout_seconds", 0.03),
patch.object(readiness, "_probe", stalled_driver),
):
for _ in range(3):
began = time.monotonic()
try:
await readiness.check()
except ApiError as error:
assert error.status_code == 503
else:
raise AssertionError("Stalled dependency was declared ready")
assert time.monotonic() - began < 0.15, "HTTP waited for slow driver cancellation"
assert cancelled.is_set() and calls == 1, "Readiness spawned accumulating probes"
release.set()
await asyncio.sleep(0)
await readiness.close()
async def healthy():
pass
with patch.object(readiness, "_probe", healthy):
await readiness.check()
await readiness.close()
print("PASS: bounded readiness response, one pending probe, recovery after cancellation")
asyncio.run(main())