|
| 1 | +"""Pin the DEBUG-log breadcrumb on AsyncAdaptedConnection.close. |
| 2 | +
|
| 3 | +The narrow ``contextlib.suppress`` used to hide failed rollbacks |
| 4 | +(leader flip, transport timeout, etc.) silently. Replace with a |
| 5 | +try/except that DEBUG-logs before continuing to the close. Programming |
| 6 | +bugs (AttributeError, RuntimeError, etc.) still propagate. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import logging |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from dqliteclient.exceptions import DqliteConnectionError |
| 16 | +from dqlitedbapi.exceptions import OperationalError |
| 17 | +from sqlalchemydqlite.aio import AsyncAdaptedConnection |
| 18 | + |
| 19 | + |
| 20 | +class _FakeAsyncConn: |
| 21 | + """A synchronous stand-in: sqlalchemy.util.await_only accepts a plain |
| 22 | + callable's return when the adapter is driven by greenlet glue. For |
| 23 | + testing we short-circuit to a sync method and let ``await_only`` |
| 24 | + see the already-complete coroutine-ish value. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, rollback_exc: BaseException | None) -> None: |
| 28 | + self._rollback_exc = rollback_exc |
| 29 | + self.close_calls = 0 |
| 30 | + |
| 31 | + async def rollback(self) -> None: |
| 32 | + if self._rollback_exc is not None: |
| 33 | + raise self._rollback_exc |
| 34 | + |
| 35 | + async def close(self) -> None: |
| 36 | + self.close_calls += 1 |
| 37 | + |
| 38 | + |
| 39 | +def test_close_logs_rollback_failure(caplog: pytest.LogCaptureFixture) -> None: |
| 40 | + fake = _FakeAsyncConn(OperationalError("server gone")) |
| 41 | + adapter = AsyncAdaptedConnection(fake) # type: ignore[arg-type] |
| 42 | + |
| 43 | + # sqlalchemy.util.await_only drives the coroutine; using a live |
| 44 | + # engine is overkill for an observability test. Stub await_only. |
| 45 | + from sqlalchemydqlite import aio as aio_module |
| 46 | + |
| 47 | + def _fake_await_only(coro: object) -> object: |
| 48 | + # ``coro`` is a coroutine object; run it synchronously via send. |
| 49 | + import asyncio |
| 50 | + |
| 51 | + return asyncio.new_event_loop().run_until_complete(coro) # type: ignore[arg-type] |
| 52 | + |
| 53 | + orig = aio_module.await_only |
| 54 | + aio_module.await_only = _fake_await_only # type: ignore[assignment] |
| 55 | + try: |
| 56 | + with caplog.at_level(logging.DEBUG, logger="sqlalchemydqlite.aio"): |
| 57 | + adapter.close() |
| 58 | + finally: |
| 59 | + aio_module.await_only = orig # type: ignore[assignment] |
| 60 | + |
| 61 | + matching = [ |
| 62 | + r |
| 63 | + for r in caplog.records |
| 64 | + if r.levelno == logging.DEBUG and "rollback failed" in r.getMessage() |
| 65 | + ] |
| 66 | + assert matching, f"expected DEBUG 'rollback failed' record; got {caplog.records!r}" |
| 67 | + assert matching[0].exc_info is not None |
| 68 | + assert isinstance(matching[0].exc_info[1], OperationalError) |
| 69 | + assert fake.close_calls == 1 # close still ran |
| 70 | + |
| 71 | + |
| 72 | +def test_close_propagates_programming_bug() -> None: |
| 73 | + """RuntimeError / AttributeError / etc. are NOT suppressed.""" |
| 74 | + fake = _FakeAsyncConn(RuntimeError("programming bug")) |
| 75 | + adapter = AsyncAdaptedConnection(fake) # type: ignore[arg-type] |
| 76 | + |
| 77 | + from sqlalchemydqlite import aio as aio_module |
| 78 | + |
| 79 | + def _fake_await_only(coro: object) -> object: |
| 80 | + import asyncio |
| 81 | + |
| 82 | + return asyncio.new_event_loop().run_until_complete(coro) # type: ignore[arg-type] |
| 83 | + |
| 84 | + orig = aio_module.await_only |
| 85 | + aio_module.await_only = _fake_await_only # type: ignore[assignment] |
| 86 | + try: |
| 87 | + with pytest.raises(RuntimeError, match="programming bug"): |
| 88 | + adapter.close() |
| 89 | + finally: |
| 90 | + aio_module.await_only = orig # type: ignore[assignment] |
| 91 | + |
| 92 | + |
| 93 | +def test_close_with_also_failing_transport_errors(caplog: pytest.LogCaptureFixture) -> None: |
| 94 | + """The full category tuple (OSError, TimeoutError, ConnectionError, etc.) |
| 95 | + is all caught and DEBUG-logged. |
| 96 | + """ |
| 97 | + fake = _FakeAsyncConn(DqliteConnectionError("peer reset")) |
| 98 | + adapter = AsyncAdaptedConnection(fake) # type: ignore[arg-type] |
| 99 | + |
| 100 | + from sqlalchemydqlite import aio as aio_module |
| 101 | + |
| 102 | + def _fake_await_only(coro: object) -> object: |
| 103 | + import asyncio |
| 104 | + |
| 105 | + return asyncio.new_event_loop().run_until_complete(coro) # type: ignore[arg-type] |
| 106 | + |
| 107 | + orig = aio_module.await_only |
| 108 | + aio_module.await_only = _fake_await_only # type: ignore[assignment] |
| 109 | + try: |
| 110 | + with caplog.at_level(logging.DEBUG, logger="sqlalchemydqlite.aio"): |
| 111 | + adapter.close() |
| 112 | + finally: |
| 113 | + aio_module.await_only = orig # type: ignore[assignment] |
| 114 | + |
| 115 | + matching = [ |
| 116 | + r |
| 117 | + for r in caplog.records |
| 118 | + if r.levelno == logging.DEBUG and "rollback failed" in r.getMessage() |
| 119 | + ] |
| 120 | + assert matching |
| 121 | + assert fake.close_calls == 1 |
0 commit comments