51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
"""Готовность при завершении и ограничение времени проверки зависимостей."""
|
|
|
|
import asyncio
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from app.domain import health
|
|
from app.main import create_app
|
|
from conftest import close_app, make_client
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clean_db() -> None:
|
|
"""Эти проверки не обращаются к хранилищам."""
|
|
|
|
|
|
async def test_draining_keeps_liveness_but_rejects_new_work(make_settings):
|
|
app = create_app(make_settings())
|
|
app.state.lifecycle.start_draining()
|
|
try:
|
|
async with make_client(app) as client:
|
|
assert (await client.get("/health/live")).status_code == 200
|
|
assert (await client.get("/health/ready")).status_code == 503
|
|
assert (await client.get("/api/v1/users/me")).status_code == 503
|
|
finally:
|
|
await close_app(app)
|
|
|
|
|
|
async def test_readiness_timeout_cancels_dependency_checks(make_settings, monkeypatch):
|
|
cancelled = []
|
|
|
|
async def stalled(_):
|
|
try:
|
|
await asyncio.sleep(30)
|
|
finally:
|
|
cancelled.append(True)
|
|
|
|
monkeypatch.setattr(health, "check_database", stalled)
|
|
monkeypatch.setattr(health, "check_redis", stalled)
|
|
app = create_app(make_settings(readiness_timeout_seconds=0.03))
|
|
try:
|
|
async with make_client(app) as client:
|
|
started = time.monotonic()
|
|
response = await client.get("/health/ready")
|
|
assert response.status_code == 503
|
|
assert time.monotonic() - started < 1
|
|
assert len(cancelled) == 2
|
|
finally:
|
|
await close_app(app)
|