"""Настоящая единица работы: граница транзакции, соединение на шаг, срок блока, отказ 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 ( DatabaseWarnings, 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_APPLICATION_NAME, DB_CLOSE_TIMEOUT_SECONDS, ) pytestmark = pytest.mark.anyio SECONDS_MARGIN = 0.7 # сервер замечает закрытый сокет не сразу SESSIONS_SETTLE_SECONDS = 3.0 @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: DatabaseWarnings, ) -> 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: DatabaseWarnings, ) -> 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_child_task_under_deadline_keeps_block_timer(engine) -> None: """Общий срок снимает таймер только у своей задачи: дочернюю его отмена не достанет, а копия контекста у неё та же.""" uow = make_uow(make_factory(engine), timeout_seconds=0.3) async def slow_block() -> None: async with uow: await uow._current_session().execute(text("SELECT pg_sleep(4)")) started = time.monotonic() async with scope.deadline_scope(timeout_seconds=30): with pytest.raises(exceptions.ServiceUnavailable): await asyncio.create_task(slow_block()) assert time.monotonic() - started < 0.3 + DB_CLOSE_TIMEOUT_SECONDS async def _app_sessions(connection) -> int: """Сессии приложения во временной базе; соединение теста — без его имени.""" return await connection.fetchval( "SELECT count(*) FROM pg_stat_activity " "WHERE datname = current_database() AND application_name = $1", DB_APPLICATION_NAME, ) async def _settled_sessions(connection, *, limit: int) -> int: """Сессии, когда их стало не больше `limit` или вышел срок ожидания.""" deadline = time.monotonic() + SESSIONS_SETTLE_SECONDS while True: sessions = await _app_sessions(connection) if sessions <= limit or time.monotonic() > deadline: return sessions await asyncio.sleep(0.1) async def test_no_sessions_over_pool_after_freeze(proxy_target) -> None: """Сценарий «После заморозки PostgreSQL сессий сверх пула нет»: заморозка на проверке соединения при выдаче (транзакции нет, и срок простоя транзакции сессию не снимет), три раза подряд.""" pool_size = 2 connection = await admin() try: baseline = await _app_sessions(connection) async with TcpProxy(*proxy_target) as proxy: engine = make_engine( host="127.0.0.1", port=proxy.port, pool_size=pool_size, ) factory = make_factory(engine) async def step() -> None: uow = make_uow(factory, timeout_seconds=0.3) async with uow: await uow._current_session().execute( text("SELECT pg_sleep(0.05)"), ) for _ in range(3): # прогрев: открыты все соединения пула, запросы идут вместе await asyncio.gather(*(step() for _ in range(pool_size))) proxy.freeze() results = await asyncio.gather( *(step() for _ in range(pool_size)), return_exceptions=True, ) assert all( isinstance(result, exceptions.ServiceUnavailable) for result in results ), results proxy.thaw() # соединения этой пачки сброшены, в пуле их нет assert engine.pool.checkedout() == 0 sessions = await _settled_sessions(connection, limit=baseline) assert sessions <= baseline, f"{sessions} > {baseline}" # пул рабочий: новые соединения вместо сброшенных await step() await engine.dispose() finally: await connection.close() async def test_soft_close_is_graceful(proxy_target) -> None: """Мягкое закрытие без отказа: сервер получил Terminate и закрыл сессию сам, обрыв транспорта после него ничего не меняет.""" connection = await admin() try: baseline = await _app_sessions(connection) 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 assert await _app_sessions(connection) == baseline + 1 before = proxy.client_writes await engine.dispose() sessions = await _settled_sessions(connection, limit=baseline) assert sessions == baseline # единственная порция клиента после проверки — Terminate assert proxy.client_writes == before + 1 finally: await connection.close() async def test_constraint_violation( uow, engine, warnings: DatabaseWarnings, ) -> 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'"), ("count_unfinished", "'processing'"), ("count_unfinished", "'queued'"), ("queue_snapshot", "'processing'"), ("queue_snapshot", "'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 == "count_unfinished": await uow.jobs.count_unfinished(limit=1) elif method == "queue_snapshot": await uow.jobs.queue_snapshot(now=NOW) 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