Source: mtusi2026lab5-safronov, solution/bvt2301-safronov, 046ec9e. Contracts, public tests and evidence of lab 5 are not carried over. The compose project, image and CI prefix are renamed to lab6; the first broker start defect is recorded in KNOWN_ISSUES.md; PLAN.md outlines the OpenSpec changes of lab 6. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
458 lines
16 KiB
Python
458 lines
16 KiB
Python
"""Приложение в процессе на «тесном» API: соединение на шаг и общий срок
|
|
JSON-запроса (спек `unit-of-work`). S3 и кэш — фейки: рабочий стенд, кроме его
|
|
PostgreSQL как сервера, не затрагивается."""
|
|
|
|
import asyncio
|
|
import logging
|
|
import time
|
|
from collections.abc import AsyncIterator
|
|
from pathlib import Path
|
|
from uuid import uuid4
|
|
|
|
import httpx
|
|
import pytest
|
|
from sqlalchemy import event
|
|
|
|
from builders import attachment_dto
|
|
from conftest import (
|
|
BLOCK_SECONDS,
|
|
DatabaseWarnings,
|
|
TcpProxy,
|
|
admin,
|
|
truncate_all,
|
|
)
|
|
from fakes import FakeCache, FakeDatabase, FakeObjectStorage
|
|
from src.api import dependencies
|
|
from src.core.repos import chat_cache
|
|
from src.core.services import ChatService
|
|
from src.core.services import session as session_module
|
|
from src.infra.db import create_engine, create_session_factory
|
|
from src.infra.db.session import engine, session_factory
|
|
from src.infra.db.uow import SqlAlchemyUnitOfWork
|
|
from src.main import app
|
|
from src.schemas.dataclasses import ChatFullDTO
|
|
from src.schemas.enums import AttachmentStatus
|
|
from src.utils.constants import DB_CLOSE_TIMEOUT_SECONDS
|
|
from src.utils.utc import utcnow
|
|
|
|
pytestmark = pytest.mark.anyio
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
SAMPLE = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes()
|
|
# запас на планировщик: срок проверяется сверху, не точно
|
|
MARGIN = 0.6
|
|
PASSWORD = "password-1234"
|
|
|
|
|
|
class _Recording:
|
|
"""Обёртка ASGI: сколько соединений занято в начале ответа."""
|
|
|
|
def __init__(self) -> None:
|
|
self.at_response_start: list[int] = []
|
|
|
|
async def __call__(self, scope, receive, send) -> None:
|
|
async def recording_send(message) -> None:
|
|
if message["type"] == "http.response.start":
|
|
self.at_response_start.append(engine.pool.checkedout())
|
|
await send(message)
|
|
|
|
await app(scope, receive, recording_send)
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_clients(monkeypatch: pytest.MonkeyPatch) -> tuple[FakeCache, object]:
|
|
journal = FakeDatabase()
|
|
cache = FakeCache(journal)
|
|
storage = FakeObjectStorage(journal)
|
|
monkeypatch.setattr(dependencies, "cache", cache)
|
|
monkeypatch.setattr(dependencies, "object_storage", storage)
|
|
return cache, storage
|
|
|
|
|
|
@pytest.fixture
|
|
async def recording(fake_clients) -> AsyncIterator[_Recording]:
|
|
await truncate_all()
|
|
await engine.dispose()
|
|
yield _Recording()
|
|
await engine.dispose()
|
|
|
|
|
|
@pytest.fixture
|
|
async def client(recording: _Recording) -> AsyncIterator[httpx.AsyncClient]:
|
|
transport = httpx.ASGITransport(
|
|
app=recording,
|
|
raise_app_exceptions=False,
|
|
)
|
|
async with httpx.AsyncClient(
|
|
transport=transport,
|
|
base_url="http://test/api/v1",
|
|
timeout=30,
|
|
) as created:
|
|
yield created
|
|
|
|
|
|
class _Checkouts:
|
|
def __init__(self) -> None:
|
|
self.count = 0
|
|
|
|
def __call__(self, *_: object) -> None:
|
|
self.count += 1
|
|
|
|
def __enter__(self) -> "_Checkouts":
|
|
event.listen(engine.sync_engine, "checkout", self)
|
|
return self
|
|
|
|
def __exit__(self, *_: object) -> None:
|
|
event.remove(engine.sync_engine, "checkout", self)
|
|
|
|
|
|
async def _register_login(
|
|
client: httpx.AsyncClient,
|
|
) -> tuple[dict[str, str], str]:
|
|
username = f"u{uuid4().hex[:12]}"
|
|
response = await client.post(
|
|
"/users",
|
|
json={
|
|
"username": username,
|
|
"display_name": "User",
|
|
"password": PASSWORD,
|
|
},
|
|
)
|
|
assert response.status_code == 201, response.text
|
|
user_id = response.json()["id"]
|
|
response = await client.post("/auth/sessions", auth=(username, PASSWORD))
|
|
assert response.status_code == 201, response.text
|
|
token = response.json()["token"]
|
|
return {"Authorization": f"Bearer {token}"}, user_id
|
|
|
|
|
|
async def _chat(
|
|
client: httpx.AsyncClient,
|
|
headers: dict[str, str],
|
|
user_id: str,
|
|
) -> str:
|
|
response = await client.post(
|
|
"/chats",
|
|
headers=headers,
|
|
json={"title": "T", "member_ids": [user_id]},
|
|
)
|
|
assert response.status_code == 201, response.text
|
|
return response.json()["id"]
|
|
|
|
|
|
async def test_response_without_held_connection(
|
|
client: httpx.AsyncClient,
|
|
recording: _Recording,
|
|
) -> None:
|
|
"""Сценарий «Ответ без занятого соединения»."""
|
|
headers, user_id = await _register_login(client)
|
|
chat_id = await _chat(client, headers, user_id)
|
|
recording.at_response_start.clear()
|
|
with _Checkouts() as checkouts:
|
|
response = await client.get(
|
|
f"/chats/{chat_id}/messages",
|
|
headers=headers,
|
|
)
|
|
assert response.status_code == 200
|
|
assert checkouts.count == 2
|
|
assert recording.at_response_start == [0]
|
|
|
|
|
|
async def test_pool_of_one_connection(client: httpx.AsyncClient) -> None:
|
|
"""Сценарий «Пул из одного соединения»."""
|
|
headers, user_id = await _register_login(client)
|
|
chat_id = await _chat(client, headers, user_id)
|
|
assert engine.pool.size() == 1
|
|
responses = await asyncio.gather(
|
|
*(
|
|
client.get(f"/chats/{chat_id}/messages", headers=headers)
|
|
for _ in range(2)
|
|
),
|
|
)
|
|
assert [item.status_code for item in responses] == [200, 200]
|
|
|
|
|
|
async def test_login_does_not_hold_connection_on_hash(
|
|
client: httpx.AsyncClient,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Сценарий «Вход не держит соединение на хэш»."""
|
|
headers, _ = await _register_login(client)
|
|
username = (await client.get("/users/me", headers=headers)).json()[
|
|
"username"
|
|
]
|
|
verifying = asyncio.Event()
|
|
release = asyncio.Event()
|
|
|
|
async def verify_password(*, password: str, password_hash: str | None):
|
|
verifying.set()
|
|
await release.wait()
|
|
return True
|
|
|
|
monkeypatch.setattr(session_module, "verify_password", verify_password)
|
|
login = asyncio.create_task(
|
|
client.post("/auth/sessions", auth=(username, PASSWORD)),
|
|
)
|
|
await verifying.wait()
|
|
assert engine.pool.checkedout() == 0
|
|
other = await client.get("/users/me", headers=headers)
|
|
assert other.status_code == 200
|
|
release.set()
|
|
assert (await login).status_code == 201
|
|
|
|
|
|
async def test_block_without_postgres_takes_no_connection(
|
|
client: httpx.AsyncClient,
|
|
fake_clients,
|
|
) -> None:
|
|
"""Сценарий «Блок без обращения к PostgreSQL»: только вход выдаёт."""
|
|
cache, _ = fake_clients
|
|
headers, user_id = await _register_login(client)
|
|
chat_id = await _chat(client, headers, user_id)
|
|
# карточка уже в кэше после создания
|
|
found = await chat_cache.get_chat(cache, chat_id=chat_id)
|
|
assert isinstance(found, ChatFullDTO)
|
|
with _Checkouts() as checkouts:
|
|
response = await client.get(f"/chats/{chat_id}", headers=headers)
|
|
assert response.status_code == 200
|
|
assert checkouts.count == 1
|
|
|
|
|
|
async def test_two_slow_steps_fit_single_deadline(
|
|
client: httpx.AsyncClient,
|
|
) -> None:
|
|
"""Сценарий «Два медленных шага»: 503 через ~T, а не через сумму сроков."""
|
|
headers, user_id = await _register_login(client)
|
|
chat_id = await _chat(client, headers, user_id)
|
|
sessions_lock = await admin()
|
|
messages_lock = await admin()
|
|
sessions_tx = sessions_lock.transaction()
|
|
messages_tx = messages_lock.transaction()
|
|
await sessions_tx.start()
|
|
await messages_tx.start()
|
|
try:
|
|
await sessions_lock.execute(
|
|
"LOCK TABLE sessions IN ACCESS EXCLUSIVE MODE",
|
|
)
|
|
await messages_lock.execute(
|
|
"LOCK TABLE messages IN ACCESS EXCLUSIVE MODE",
|
|
)
|
|
started = time.monotonic()
|
|
request = asyncio.create_task(
|
|
client.get(f"/chats/{chat_id}/messages", headers=headers),
|
|
)
|
|
# проверка токена занимает больше половины срока
|
|
await asyncio.sleep(BLOCK_SECONDS * 0.6)
|
|
await sessions_tx.rollback()
|
|
response = await request
|
|
elapsed = time.monotonic() - started
|
|
finally:
|
|
await messages_tx.rollback()
|
|
await sessions_lock.close()
|
|
await messages_lock.close()
|
|
assert response.status_code == 503
|
|
assert response.json()["error"]["code"] == "unavailable"
|
|
assert elapsed < BLOCK_SECONDS + MARGIN
|
|
|
|
|
|
async def test_hash_queue_gets_deadline(client: httpx.AsyncClient) -> None:
|
|
"""Сценарий «Очередь хэшей»: не успевшие — 503 через ~T."""
|
|
headers, _ = await _register_login(client)
|
|
username = (await client.get("/users/me", headers=headers)).json()[
|
|
"username"
|
|
]
|
|
|
|
async def login() -> tuple[int, float]:
|
|
started = time.monotonic()
|
|
response = await client.post(
|
|
"/auth/sessions",
|
|
auth=(username, PASSWORD),
|
|
)
|
|
return response.status_code, time.monotonic() - started
|
|
|
|
results = await asyncio.gather(*(login() for _ in range(200)))
|
|
statuses = {status for status, _ in results}
|
|
assert 503 in statuses
|
|
assert statuses <= {201, 503}
|
|
assert max(elapsed for _, elapsed in results) < BLOCK_SECONDS + MARGIN
|
|
|
|
|
|
class _PoolRecords(logging.Handler):
|
|
"""Записи пула: только текст, `exc_info` с кадрами не хранится."""
|
|
|
|
def __init__(self) -> None:
|
|
super().__init__(level=logging.WARNING)
|
|
self.messages: list[str] = []
|
|
|
|
def emit(self, record: logging.LogRecord) -> None:
|
|
self.messages.append(record.getMessage())
|
|
|
|
|
|
async def test_deadline_in_second_block_single_record(
|
|
client: httpx.AsyncClient,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
proxy_target: tuple[str, int],
|
|
warnings: DatabaseWarnings,
|
|
) -> None:
|
|
"""Сценарий «Срок запроса истёк во втором блоке: одна запись, без ERROR
|
|
пула»: заморозка между входом и операцией, зазор сроков — настоящая
|
|
длительность блока входа."""
|
|
headers, _ = await _register_login(client)
|
|
records = _PoolRecords()
|
|
pool_logger = logging.getLogger("sqlalchemy.pool")
|
|
pool_logger.addHandler(records)
|
|
async with TcpProxy(*proxy_target) as proxy:
|
|
proxied = create_engine(
|
|
dependencies.settings.database.model_copy(
|
|
update={"host": "127.0.0.1", "port": proxy.port},
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
dependencies,
|
|
"session_factory",
|
|
create_session_factory(proxied),
|
|
)
|
|
real_list = ChatService.list_user_chats
|
|
|
|
async def frozen_list(self, **kwargs):
|
|
proxy.freeze()
|
|
return await real_list(self, **kwargs)
|
|
|
|
monkeypatch.setattr(ChatService, "list_user_chats", frozen_list)
|
|
try:
|
|
started = time.monotonic()
|
|
response = await client.get("/chats", headers=headers)
|
|
elapsed = time.monotonic() - started
|
|
checked_out = proxied.pool.checkedout()
|
|
finally:
|
|
pool_logger.removeHandler(records)
|
|
proxy.thaw()
|
|
await proxied.dispose()
|
|
assert response.status_code == 503
|
|
assert response.json()["error"]["code"] == "unavailable"
|
|
assert elapsed < BLOCK_SECONDS + DB_CLOSE_TIMEOUT_SECONDS + MARGIN
|
|
assert warnings.events == ["Database is unavailable"]
|
|
assert records.messages == []
|
|
assert checked_out == 0
|
|
|
|
|
|
async def test_upload_longer_than_block(
|
|
client: httpx.AsyncClient,
|
|
fake_clients,
|
|
) -> None:
|
|
"""Сценарий «Вложение дольше срока блока»: загрузка."""
|
|
_, storage = fake_clients
|
|
headers, user_id = await _register_login(client)
|
|
chat_id = await _chat(client, headers, user_id)
|
|
|
|
async def slow_body() -> AsyncIterator[bytes]:
|
|
yield SAMPLE[:100]
|
|
await asyncio.sleep(BLOCK_SECONDS + 0.3)
|
|
yield SAMPLE[100:]
|
|
|
|
started = time.monotonic()
|
|
response = await client.post(
|
|
f"/chats/{chat_id}/attachments",
|
|
params={"filename": "a.png"},
|
|
headers={**headers, "Content-Type": "image/png"},
|
|
content=slow_body(),
|
|
)
|
|
assert response.status_code == 202, response.text
|
|
assert time.monotonic() - started > BLOCK_SECONDS
|
|
assert storage.objects
|
|
|
|
|
|
async def test_download_longer_than_block(
|
|
client: httpx.AsyncClient,
|
|
fake_clients,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Тот же сценарий: скачивание потоком."""
|
|
_, storage = fake_clients
|
|
headers, user_id = await _register_login(client)
|
|
chat_id = await _chat(client, headers, user_id)
|
|
row = attachment_dto(
|
|
chat_id=chat_id,
|
|
owner_id=user_id,
|
|
status=AttachmentStatus.READY,
|
|
created_at=utcnow(),
|
|
namespace=storage.namespace,
|
|
)
|
|
async with SqlAlchemyUnitOfWork(
|
|
session_factory,
|
|
timeout_seconds=BLOCK_SECONDS,
|
|
) as uow:
|
|
await uow.attachments.insert_attachment(attachment=row)
|
|
await uow.commit()
|
|
storage.seed(row.object_key, b"content", content_type="image/png")
|
|
real_open = storage.open
|
|
|
|
class _Slow:
|
|
def __init__(self, inner) -> None:
|
|
self.inner = inner
|
|
|
|
async def __aenter__(self):
|
|
stored = await self.inner.__aenter__()
|
|
real_chunks = stored.iter_chunks
|
|
|
|
async def iter_chunks(chunk_bytes: int = 1024):
|
|
await asyncio.sleep(BLOCK_SECONDS + 0.3)
|
|
async for chunk in real_chunks(chunk_bytes):
|
|
yield chunk
|
|
|
|
stored.iter_chunks = iter_chunks
|
|
return stored
|
|
|
|
async def __aexit__(self, *exc_info: object):
|
|
return await self.inner.__aexit__(*exc_info)
|
|
|
|
monkeypatch.setattr(storage, "open", lambda key: _Slow(real_open(key)))
|
|
started = time.monotonic()
|
|
response = await client.get(
|
|
f"/attachments/{row.id}/content",
|
|
headers=headers,
|
|
)
|
|
assert response.status_code == 200
|
|
assert response.content == b"content"
|
|
assert time.monotonic() - started > BLOCK_SECONDS
|
|
|
|
|
|
async def test_code_order_with_postgres_down(
|
|
client: httpx.AsyncClient,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Вход идёт раньше валидации, битый JSON — раньше входа; отказ БД на входе —
|
|
503 до 400 (design, Context, п. 8)."""
|
|
headers, user_id = await _register_login(client)
|
|
chat_id = await _chat(client, headers, user_id)
|
|
down_settings = dependencies.settings.database.model_copy(
|
|
update={"port": 1, "connect_timeout_seconds": 1},
|
|
)
|
|
down = create_engine(down_settings)
|
|
monkeypatch.setattr(
|
|
dependencies,
|
|
"session_factory",
|
|
create_session_factory(down),
|
|
)
|
|
url = f"/chats/{chat_id}/messages"
|
|
try:
|
|
no_token = await client.post(url, json={"text": "x"})
|
|
assert no_token.status_code == 401
|
|
broken = await client.post(
|
|
url,
|
|
headers={**headers, "Content-Type": "application/json"},
|
|
content=b"{not json",
|
|
)
|
|
assert broken.status_code == 400
|
|
extra = await client.post(
|
|
url,
|
|
headers={**headers, "Idempotency-Key": "k"},
|
|
json={"text": "x", "extra": 1},
|
|
)
|
|
assert extra.status_code == 503
|
|
assert extra.json()["error"]["code"] == "unavailable"
|
|
history = await client.get(url, headers=headers)
|
|
assert history.status_code == 503
|
|
finally:
|
|
await down.dispose()
|