Skip to content

Commit d83c6bb

Browse files
Yield+close in connected_connection fixture for leak-hygiene parity
The fixture used a plain return shape, leaving the conn unclosed across the test boundary. Convert to an async generator with a try/yield/finally close so the suite's leak-hygiene contract holds under stricter pytest configs. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8e9517c commit d83c6bb

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

tests/conftest.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Pytest configuration for dqlite-client tests."""
22

3+
import contextlib
34
import sys
5+
from collections.abc import AsyncIterator
46
from pathlib import Path
57
from unittest.mock import AsyncMock, MagicMock, patch
68

@@ -92,14 +94,20 @@ async def connected_connection(
9294
mock_writer: MagicMock,
9395
welcome_response: bytes,
9496
db_response: bytes,
95-
) -> tuple[DqliteConnection, AsyncMock, MagicMock]:
97+
) -> AsyncIterator[tuple[DqliteConnection, AsyncMock, MagicMock]]:
9698
"""A DqliteConnection that is already connected with mocked transport.
9799
98-
Returns (conn, mock_reader, mock_writer) so tests can configure
99-
additional response data on mock_reader.
100+
Yields (conn, mock_reader, mock_writer) so tests can configure
101+
additional response data on mock_reader. Closes the connection on
102+
teardown so the suite-wide leak-hygiene contract holds (no
103+
ResourceWarning under stricter pytest configs).
100104
"""
101105
mock_reader.read.side_effect = [welcome_response, db_response]
102106
conn = DqliteConnection("localhost:9001")
103107
with patch("asyncio.open_connection", return_value=(mock_reader, mock_writer)):
104108
await conn.connect()
105-
return conn, mock_reader, mock_writer
109+
try:
110+
yield conn, mock_reader, mock_writer
111+
finally:
112+
with contextlib.suppress(Exception):
113+
await conn.close()

0 commit comments

Comments
 (0)