Services depend only on src/core/ports: async with self.uow is one transaction, commit is explicit, leaving without it rolls back. A connection is held per step, the JSON routers keep a shared request deadline, and rollback runs under the block deadline. Adds service-tests with fakes, a repo contract run on both the fake and SQL, and make service_test / service_test_db. API, schema and settings are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
408 lines
14 KiB
Python
408 lines
14 KiB
Python
"""Настоящая единица работы: граница транзакции, соединение на шаг, срок блока,
|
|
отказ PostgreSQL и обрыв соединения с дешёвой проверкой при выдаче (спек
|
|
`unit-of-work`, design, решения 2, 4, 7)."""
|
|
|
|
import asyncio
|
|
import time
|
|
|
|
import pytest
|
|
from sqlalchemy import event, exc, text
|
|
|
|
from builders import NOW, PASSWORD_HASH, attachment_dto, user_dto
|
|
from conftest import (
|
|
TcpProxy,
|
|
admin,
|
|
make_engine,
|
|
make_factory,
|
|
make_uow,
|
|
truncate_all,
|
|
)
|
|
from src.core import exceptions
|
|
from src.core.ports.uow import UnitOfWorkError
|
|
from src.infra.db import scope
|
|
from src.infra.db.engine import _ping_once
|
|
from src.infra.db.uow import SqlAlchemyUnitOfWork
|
|
from src.schemas.enums import AttachmentStatus
|
|
from src.utils.constants import DB_CLOSE_TIMEOUT_SECONDS
|
|
|
|
pytestmark = pytest.mark.anyio
|
|
|
|
SECONDS_MARGIN = 0.7
|
|
|
|
|
|
class _Warnings:
|
|
"""Записи логгера `database`: сколько раз назван отказ."""
|
|
|
|
def __init__(self) -> None:
|
|
self.events: list[str] = []
|
|
|
|
def warning(self, event_name: str, **_: object) -> None:
|
|
self.events.append(event_name)
|
|
|
|
|
|
@pytest.fixture
|
|
def warnings(monkeypatch: pytest.MonkeyPatch) -> _Warnings:
|
|
recorder = _Warnings()
|
|
monkeypatch.setattr(scope, "logger", recorder)
|
|
return recorder
|
|
|
|
|
|
@pytest.fixture
|
|
async def engine():
|
|
await truncate_all()
|
|
created = make_engine(pool_size=1)
|
|
yield created
|
|
await created.dispose()
|
|
|
|
|
|
@pytest.fixture
|
|
def uow(engine) -> SqlAlchemyUnitOfWork:
|
|
return make_uow(make_factory(engine))
|
|
|
|
|
|
def _other(engine) -> SqlAlchemyUnitOfWork:
|
|
return make_uow(make_factory(engine))
|
|
|
|
|
|
async def _pid(uow: SqlAlchemyUnitOfWork) -> int:
|
|
result = await uow._current_session().execute(
|
|
text("SELECT pg_backend_pid()"),
|
|
)
|
|
return result.scalar_one()
|
|
|
|
|
|
async def _user_visible(engine, username: str) -> bool:
|
|
async with _other(engine) as other:
|
|
found = await other.users.get_user_credentials(username=username)
|
|
return found is not None
|
|
|
|
|
|
async def test_exit_without_commit(uow, engine) -> None:
|
|
"""Сценарий «Выход без фиксации»."""
|
|
user = user_dto()
|
|
async with uow:
|
|
await uow.users.insert_user(user=user, password_hash=PASSWORD_HASH)
|
|
assert not await _user_visible(engine, user.username)
|
|
|
|
|
|
async def test_exception_inside_block(uow, engine) -> None:
|
|
"""Сценарий «Исключение внутри блока»: то же исключение, запись откачена."""
|
|
user = user_dto()
|
|
with pytest.raises(exceptions.UsernameTaken):
|
|
async with uow:
|
|
await uow.users.insert_user(user=user, password_hash=PASSWORD_HASH)
|
|
raise exceptions.UsernameTaken
|
|
assert not await _user_visible(engine, user.username)
|
|
|
|
|
|
async def test_commit_visible_to_other_connection(uow, engine) -> None:
|
|
"""Сценарий «Фиксация»."""
|
|
user = user_dto()
|
|
async with uow:
|
|
await uow.users.insert_user(user=user, password_hash=PASSWORD_HASH)
|
|
await uow.commit()
|
|
assert await _user_visible(engine, user.username)
|
|
|
|
|
|
async def test_work_after_commit_is_an_error(uow) -> None:
|
|
"""Сценарий «Работа после фиксации»: не молчаливая вторая транзакция."""
|
|
async with uow:
|
|
await uow.commit()
|
|
with pytest.raises(UnitOfWorkError):
|
|
_ = uow.users
|
|
with pytest.raises(UnitOfWorkError):
|
|
await uow.commit()
|
|
|
|
|
|
async def test_nested_block_without_second_connection(uow, engine) -> None:
|
|
"""Сценарий «Вложенный блок»."""
|
|
async with uow:
|
|
await uow.users.get_user_credentials(username="nobody")
|
|
with pytest.raises(UnitOfWorkError):
|
|
await uow.__aenter__()
|
|
assert engine.pool.checkedout() == 1
|
|
|
|
|
|
async def test_repo_outside_block_no_query(uow, engine) -> None:
|
|
"""Сценарий «Repo вне блока»: запроса нет."""
|
|
with pytest.raises(UnitOfWorkError):
|
|
_ = uow.users
|
|
assert engine.pool.checkedout() == 0
|
|
|
|
|
|
async def test_blocks_opened(uow) -> None:
|
|
for _ in range(3):
|
|
async with uow:
|
|
pass
|
|
assert uow.blocks_opened == 3
|
|
|
|
|
|
async def test_connection_taken_only_inside_block(uow, engine) -> None:
|
|
pool = engine.pool
|
|
assert pool.checkedout() == 0
|
|
async with uow:
|
|
# выдача ленивая: сессия без выражения соединения не берёт
|
|
assert pool.checkedout() == 0
|
|
await uow.users.get_user_credentials(username="nobody")
|
|
assert pool.checkedout() == 1
|
|
assert pool.checkedout() == 0
|
|
|
|
|
|
async def test_commit_returns_connection_after_exit(uow, engine) -> None:
|
|
async with uow:
|
|
await uow.users.get_user_credentials(username="nobody")
|
|
await uow.commit()
|
|
assert engine.pool.checkedout() == 0
|
|
|
|
|
|
async def test_statement_longer_than_block(
|
|
engine,
|
|
warnings: _Warnings,
|
|
) -> None:
|
|
"""Сценарий «Запрос дольше срока»."""
|
|
uow = make_uow(make_factory(engine), timeout_seconds=0.3)
|
|
async with uow:
|
|
first_pid = await _pid(uow)
|
|
started = time.monotonic()
|
|
with pytest.raises(exceptions.ServiceUnavailable):
|
|
async with uow:
|
|
await uow._current_session().execute(
|
|
text("SELECT pg_sleep(4)"),
|
|
)
|
|
assert time.monotonic() - started < 0.3 + DB_CLOSE_TIMEOUT_SECONDS
|
|
assert warnings.events == ["Database is unavailable"]
|
|
assert engine.pool.checkedout() == 0
|
|
# следующий блок — рабочее соединение, а не прежнее
|
|
async with uow:
|
|
assert await _pid(uow) != first_pid
|
|
|
|
|
|
async def test_frozen_on_rollback(proxy_target, warnings: _Warnings) -> None:
|
|
"""Сценарий «PostgreSQL заморожен при откате»."""
|
|
async with TcpProxy(*proxy_target) as proxy:
|
|
engine = make_engine(host="127.0.0.1", port=proxy.port, pool_size=1)
|
|
uow = make_uow(make_factory(engine), timeout_seconds=0.5)
|
|
started = None
|
|
with pytest.raises(exceptions.ServiceUnavailable):
|
|
async with uow:
|
|
await uow.users.get_user_credentials(username="nobody")
|
|
proxy.freeze()
|
|
started = time.monotonic()
|
|
elapsed = time.monotonic() - started
|
|
assert elapsed < 0.5 + DB_CLOSE_TIMEOUT_SECONDS + SECONDS_MARGIN
|
|
assert warnings.events == ["Database is unavailable"]
|
|
assert engine.pool.checkedout() == 0
|
|
proxy.thaw()
|
|
await engine.dispose()
|
|
|
|
|
|
async def test_frozen_at_exception_in_block(proxy_target) -> None:
|
|
"""Исключение при замороженном сервере: откат под сроком блока, отказ БД
|
|
заменяет доменное исключение, а не висит до разморозки."""
|
|
async with TcpProxy(*proxy_target) as proxy:
|
|
engine = make_engine(host="127.0.0.1", port=proxy.port, pool_size=1)
|
|
uow = make_uow(make_factory(engine), timeout_seconds=0.5)
|
|
started = time.monotonic()
|
|
with pytest.raises(exceptions.ServiceUnavailable):
|
|
async with uow:
|
|
await uow.users.get_user_credentials(username="nobody")
|
|
proxy.freeze()
|
|
started = time.monotonic()
|
|
raise exceptions.ChatNotFound
|
|
elapsed = time.monotonic() - started
|
|
assert elapsed < 0.5 + DB_CLOSE_TIMEOUT_SECONDS + SECONDS_MARGIN
|
|
proxy.thaw()
|
|
await engine.dispose()
|
|
|
|
|
|
async def test_cancel_with_open_transaction(proxy_target) -> None:
|
|
"""Сценарий «Отмена при открытой транзакции»."""
|
|
async with TcpProxy(*proxy_target) as proxy:
|
|
engine = make_engine(host="127.0.0.1", port=proxy.port, pool_size=1)
|
|
uow = make_uow(make_factory(engine), timeout_seconds=30)
|
|
inside = asyncio.Event()
|
|
|
|
async def block() -> None:
|
|
async with uow:
|
|
await uow.users.get_user_credentials(username="nobody")
|
|
inside.set()
|
|
await asyncio.sleep(60)
|
|
|
|
task = asyncio.create_task(block())
|
|
await inside.wait()
|
|
accepted = proxy.accepted
|
|
proxy.freeze()
|
|
started = time.monotonic()
|
|
task.cancel()
|
|
with pytest.raises(asyncio.CancelledError):
|
|
await task
|
|
elapsed = time.monotonic() - started
|
|
assert elapsed < DB_CLOSE_TIMEOUT_SECONDS + SECONDS_MARGIN
|
|
assert engine.pool.checkedout() == 0
|
|
proxy.thaw()
|
|
# соединение в пул не вернулось: следующий блок открывает новое
|
|
async with uow:
|
|
await uow.users.get_user_credentials(username="nobody")
|
|
assert proxy.accepted == accepted + 1
|
|
await engine.dispose()
|
|
|
|
|
|
async def test_constraint_violation(uow, engine, warnings: _Warnings) -> None:
|
|
"""Сценарий «Нарушение ограничения»."""
|
|
async with uow:
|
|
first_pid = await _pid(uow)
|
|
with pytest.raises(exc.IntegrityError):
|
|
async with uow:
|
|
# owner не участник: внешний ключ на chat_members
|
|
await uow.attachments.insert_attachment(
|
|
attachment=attachment_dto(
|
|
chat_id=user_dto().id,
|
|
owner_id=user_dto().id,
|
|
),
|
|
)
|
|
assert warnings.events == []
|
|
assert engine.pool.checkedout() == 0
|
|
async with uow:
|
|
assert await _pid(uow) == first_pid
|
|
|
|
|
|
async def test_terminated_connection_replaced(uow) -> None:
|
|
"""`pg_terminate_backend`: следующий блок получает новое без ошибки."""
|
|
async with uow:
|
|
pid = await _pid(uow)
|
|
connection = await admin()
|
|
try:
|
|
await connection.fetchval("SELECT pg_terminate_backend($1)", pid)
|
|
finally:
|
|
await connection.close()
|
|
async with uow:
|
|
assert await _pid(uow) != pid
|
|
|
|
|
|
async def test_ping_is_replaced(engine) -> None:
|
|
assert engine.dialect.do_ping is _ping_once
|
|
|
|
|
|
async def test_ping_detects_dropped_connection_through_proxy(
|
|
proxy_target,
|
|
) -> None:
|
|
"""Обрыв при подменённой проверке: `_connection`, `fetchrow` и
|
|
`_handle_exception` адаптера настоящие. Смена их имён ломает разбор обрыва, и
|
|
следующий блок падает ошибкой вместо нового соединения (design, решение 7)."""
|
|
async with TcpProxy(*proxy_target) as proxy:
|
|
engine = make_engine(host="127.0.0.1", port=proxy.port, pool_size=1)
|
|
pings: list[bool] = []
|
|
real_ping = engine.dialect.do_ping
|
|
|
|
def spy(connection) -> bool:
|
|
result = False
|
|
try:
|
|
result = real_ping(connection)
|
|
finally:
|
|
pings.append(result)
|
|
return result
|
|
|
|
engine.dialect.do_ping = spy
|
|
uow = make_uow(make_factory(engine))
|
|
async with uow:
|
|
first = await _pid(uow)
|
|
accepted = proxy.accepted
|
|
proxy.drop()
|
|
await asyncio.sleep(0)
|
|
async with uow:
|
|
second = await _pid(uow)
|
|
# проверка выполнялась и нашла обрыв; новое соединение через прокси
|
|
assert pings == [False]
|
|
assert second != first
|
|
assert proxy.accepted == accepted + 1
|
|
await engine.dispose()
|
|
|
|
|
|
async def test_ping_is_one_exchange(proxy_target) -> None:
|
|
"""Проверка при выдаче — один обмен, а не `BEGIN`, `;`, `ROLLBACK`."""
|
|
async with TcpProxy(*proxy_target) as proxy:
|
|
engine = make_engine(host="127.0.0.1", port=proxy.port, pool_size=1)
|
|
async with engine.connect():
|
|
pass
|
|
# первая проверка готовит выражение, вторая — повторная
|
|
async with engine.connect():
|
|
pass
|
|
before = proxy.client_writes
|
|
async with engine.connect():
|
|
pass
|
|
assert proxy.client_writes - before == 1
|
|
await engine.dispose()
|
|
|
|
|
|
async def test_ping_with_real_postgres_rejected_connection(engine) -> None:
|
|
"""Живое соединение: проверка не падает и возвращает пул в рабочий вид."""
|
|
async with engine.connect() as connection:
|
|
await connection.execute(text("SELECT 1"))
|
|
assert engine.pool.checkedout() == 0
|
|
|
|
|
|
async def _statements(engine, action) -> list[str]:
|
|
captured: list[str] = []
|
|
|
|
def before_cursor_execute(_c, _cursor, statement, _p, _ctx, _many) -> None:
|
|
captured.append(statement)
|
|
|
|
event.listen(
|
|
engine.sync_engine,
|
|
"before_cursor_execute",
|
|
before_cursor_execute,
|
|
)
|
|
try:
|
|
await action()
|
|
finally:
|
|
event.remove(
|
|
engine.sync_engine,
|
|
"before_cursor_execute",
|
|
before_cursor_execute,
|
|
)
|
|
return captured
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("method", "constant"),
|
|
[
|
|
("lock_due_jobs", "'queued'"),
|
|
("count_published", "'queued'"),
|
|
("lock_stale_pending", "'pending'"),
|
|
("get_staging_candidates", "'ready'"),
|
|
],
|
|
)
|
|
async def test_status_is_a_constant_in_statement(
|
|
uow,
|
|
engine,
|
|
method: str,
|
|
constant: str,
|
|
) -> None:
|
|
async def action() -> None:
|
|
async with uow:
|
|
if method == "lock_due_jobs":
|
|
await uow.jobs.lock_due_jobs(now=NOW, limit=1)
|
|
elif method == "count_published":
|
|
await uow.jobs.count_published()
|
|
elif method == "lock_stale_pending":
|
|
await uow.attachments.lock_stale_pending(
|
|
attachment_id=user_dto().id,
|
|
older_than=NOW,
|
|
)
|
|
else:
|
|
await uow.attachments.get_staging_candidates(
|
|
status=AttachmentStatus.READY,
|
|
updated_before=None,
|
|
after=None,
|
|
limit=1,
|
|
)
|
|
|
|
statements = await _statements(engine, action)
|
|
relevant = [
|
|
item
|
|
for item in statements
|
|
if "attachment" in item.lower() and "select" in item.lower()
|
|
]
|
|
assert relevant
|
|
assert all(constant in item for item in relevant), relevant
|