|
| 1 | +"""Pin: ``AsyncConnection.force_close_transport`` is a public, |
| 2 | +synchronous, idempotent, never-raising last-resort cleanup hook. |
| 3 | +
|
| 4 | +The SA dialect's async adapter calls this from its non-greenlet |
| 5 | +finalize path (GC sweep with no event loop). Walking the |
| 6 | +underlying client connection's private ``_protocol._writer`` |
| 7 | +chain from outside this package broke silently when the chain |
| 8 | +shape changed; this hook is the single supported access boundary. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from unittest.mock import MagicMock |
| 14 | + |
| 15 | +from dqlitedbapi.aio.connection import AsyncConnection |
| 16 | + |
| 17 | + |
| 18 | +def test_force_close_transport_calls_writer_close() -> None: |
| 19 | + """The hook walks _async_conn → _protocol → _writer and calls |
| 20 | + writer.close().""" |
| 21 | + conn = AsyncConnection("localhost:9001", database="x") |
| 22 | + inner = MagicMock() |
| 23 | + proto = MagicMock() |
| 24 | + writer = MagicMock() |
| 25 | + proto._writer = writer |
| 26 | + inner._protocol = proto |
| 27 | + conn._async_conn = inner |
| 28 | + |
| 29 | + conn.force_close_transport() |
| 30 | + |
| 31 | + writer.close.assert_called_once_with() |
| 32 | + |
| 33 | + |
| 34 | +def test_force_close_transport_is_idempotent() -> None: |
| 35 | + """Multiple invocations are safe; the writer's close() may be |
| 36 | + called repeatedly.""" |
| 37 | + conn = AsyncConnection("localhost:9001", database="x") |
| 38 | + inner = MagicMock() |
| 39 | + proto = MagicMock() |
| 40 | + writer = MagicMock() |
| 41 | + proto._writer = writer |
| 42 | + inner._protocol = proto |
| 43 | + conn._async_conn = inner |
| 44 | + |
| 45 | + conn.force_close_transport() |
| 46 | + conn.force_close_transport() |
| 47 | + conn.force_close_transport() |
| 48 | + |
| 49 | + assert writer.close.call_count == 3 |
| 50 | + |
| 51 | + |
| 52 | +def test_force_close_transport_handles_missing_async_conn() -> None: |
| 53 | + """A connection that was never opened (or already closed and |
| 54 | + nulled) absorbs the call without raising.""" |
| 55 | + conn = AsyncConnection("localhost:9001", database="x") |
| 56 | + assert conn._async_conn is None # never connected |
| 57 | + conn.force_close_transport() # must not raise |
| 58 | + |
| 59 | + |
| 60 | +def test_force_close_transport_handles_missing_protocol() -> None: |
| 61 | + """An inner connection without ``_protocol`` (mid-construction |
| 62 | + or already torn down) absorbs the call.""" |
| 63 | + conn = AsyncConnection("localhost:9001", database="x") |
| 64 | + inner = MagicMock(spec=[]) # no attributes |
| 65 | + conn._async_conn = inner |
| 66 | + conn.force_close_transport() # must not raise |
| 67 | + |
| 68 | + |
| 69 | +def test_force_close_transport_swallows_writer_close_exception() -> None: |
| 70 | + """``writer.close()`` raising must not propagate — last-resort |
| 71 | + cleanup must always finish.""" |
| 72 | + conn = AsyncConnection("localhost:9001", database="x") |
| 73 | + inner = MagicMock() |
| 74 | + proto = MagicMock() |
| 75 | + writer = MagicMock() |
| 76 | + writer.close.side_effect = OSError("transport already closed") |
| 77 | + proto._writer = writer |
| 78 | + inner._protocol = proto |
| 79 | + conn._async_conn = inner |
| 80 | + |
| 81 | + conn.force_close_transport() # must not raise |
| 82 | + writer.close.assert_called_once_with() |
0 commit comments