forked from mtusi2026/mtusi2026lab2
Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6 and RustFS configuration, migrations, hooks, evidence and
scenario scripts and the CI job from the lab 4 solution commit d2744bc,
merge the lab 4 Makefile targets into the lab 5 skeleton Makefile
without touching its targets, and append the lab 4 solution variables
to the skeleton .env.example. The package and the application follow
the contract version 1.5.0, and the image and the throwaway test
containers are tagged lab5 so they do not collide with a lab 4 stand on
the same host.
This is the starting point of the lab 5 cycle: lint, own tests and the
offline skeleton check pass before any lab 5 change.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
98 lines
3.7 KiB
Python
98 lines
3.7 KiB
Python
import asyncio
|
|
from collections.abc import AsyncIterator, Callable
|
|
from contextlib import asynccontextmanager
|
|
|
|
from httpx import AsyncClient
|
|
|
|
from conftest import keyed, make_chat, register, resolve, running_app
|
|
from messenger.messaging.application import handlers
|
|
from messenger.messaging.domain.events import MessagePosted
|
|
from messenger.shared.application.messagebus import EventBus
|
|
|
|
MARKER = "текст-который-не-должен-утечь-при-таймауте"
|
|
|
|
|
|
@asynccontextmanager
|
|
async def slow_post(delay: float) -> AsyncIterator[None]:
|
|
original: Callable = handlers.PostMessageHandler.__call__
|
|
|
|
async def stalled(self, command):
|
|
await asyncio.sleep(delay)
|
|
return await original(self, command)
|
|
|
|
handlers.PostMessageHandler.__call__ = stalled
|
|
try:
|
|
yield
|
|
finally:
|
|
handlers.PostMessageHandler.__call__ = original
|
|
|
|
|
|
async def post(client: AsyncClient, chat: dict, sender: dict, text: str):
|
|
return await client.post(
|
|
f"/api/v1/chats/{chat['id']}/messages", json={"text": text}, headers=keyed(sender)
|
|
)
|
|
|
|
|
|
async def prepared(client: AsyncClient) -> tuple[dict, dict]:
|
|
alice = await register(client, "alice")
|
|
return alice, await make_chat(client, alice)
|
|
|
|
|
|
async def test_timeout_returns_unavailable_error_body() -> None:
|
|
async with running_app(request_timeout_seconds=0.15) as client:
|
|
alice, chat = await prepared(client)
|
|
async with slow_post(1.0):
|
|
response = await post(client, chat, alice, "медленная запись")
|
|
assert response.status_code == 503
|
|
body = response.json()
|
|
assert body["error"]["code"] == "unavailable"
|
|
assert body["error"]["request_id"] == response.headers["X-Request-Id"]
|
|
|
|
|
|
async def test_timeout_does_not_break_following_requests() -> None:
|
|
async with running_app(request_timeout_seconds=0.15) as client:
|
|
alice, chat = await prepared(client)
|
|
async with slow_post(1.0):
|
|
assert (await post(client, chat, alice, "зависшая запись")).status_code == 503
|
|
assert (await post(client, chat, alice, "нормальная запись")).status_code == 201
|
|
assert (await client.get("/health/ready")).status_code == 200
|
|
|
|
|
|
async def test_timeout_is_logged_without_message_text(capsys) -> None:
|
|
async with running_app(request_timeout_seconds=0.15, log_level="INFO") as client:
|
|
alice, chat = await prepared(client)
|
|
async with slow_post(1.0):
|
|
assert (await post(client, chat, alice, MARKER)).status_code == 503
|
|
captured = capsys.readouterr()
|
|
assert "request.timeout" in captured.out
|
|
assert MARKER not in captured.out
|
|
assert MARKER not in captured.err
|
|
|
|
|
|
async def test_normal_request_is_not_affected_by_timeout() -> None:
|
|
async with running_app(request_timeout_seconds=5.0) as client:
|
|
alice, chat = await prepared(client)
|
|
assert (await post(client, chat, alice, "обычная запись")).status_code == 201
|
|
|
|
|
|
async def test_slow_event_handler_does_not_trigger_timeout() -> None:
|
|
async with running_app(request_timeout_seconds=0.3) as client:
|
|
alice, chat = await prepared(client)
|
|
blocked = asyncio.Event()
|
|
bus = await resolve(client, EventBus)
|
|
|
|
async def stall(_event: MessagePosted) -> None:
|
|
await blocked.wait()
|
|
|
|
bus.subscribe(MessagePosted, stall)
|
|
response = await post(client, chat, alice, "ответ не ждёт обработчик")
|
|
blocked.set()
|
|
assert response.status_code == 201
|
|
|
|
|
|
async def test_healthcheck_stays_fast(client: AsyncClient) -> None:
|
|
loop = asyncio.get_running_loop()
|
|
started = loop.time()
|
|
assert (await client.get("/health/live")).status_code == 200
|
|
assert loop.time() - started < 1.0
|