From 7b1515b8ad48b9106b6fc0d36f641d01ad8e10f3 Mon Sep 17 00:00:00 2001 From: Hal Blackburn Date: Sun, 2 Feb 2025 02:33:17 +0000 Subject: [PATCH 1/2] chore: treat warnings in tests as errors --- pyproject.toml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 8290502..dc4f043 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -105,6 +105,13 @@ convention = "numpy" [tool.pytest.ini_options] addopts = ["--doctest-modules"] asyncio_default_fixture_loop_scope = "function" +filterwarnings = [ + # Treat warnings emitted during tests as errors. (This way we can explicitly + # ignore warnings we know to be OK.) + "error", + # Triggered by the fairly old version of protobuf we use + 'ignore:_SixMetaPathImporter\.find_spec\(\)\ not\ found;\ falling\ back\ to\ find_module\(\):ImportWarning:importlib._bootstrap' +] [tool.coverage.run] omit = [ From c80e60d34426e4f61ec77c657f4a3942bed3abc7 Mon Sep 17 00:00:00 2001 From: Hal Blackburn Date: Sun, 2 Feb 2025 03:23:28 +0000 Subject: [PATCH 2/2] test: fix resource leak warnings in tests One of the tests was not stopping an event loop it created, which caused a resource warning later during an unrelated test. --- test/test_kv.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/test/test_kv.py b/test/test_kv.py index 67e61cf..83d9a0f 100644 --- a/test/test_kv.py +++ b/test/test_kv.py @@ -404,6 +404,23 @@ def db(create_db: partial[Kv]) -> Kv: return create_db() +@pytest.fixture +def stopped_loop() -> Generator[asyncio.AbstractEventLoop]: + """Get an asyncio loop that is not actively running anything.""" + try: + loop = asyncio.new_event_loop() + yield loop + assert not loop.is_running() + finally: + + async def close() -> None: + await asyncio.wait_for(loop.shutdown_asyncgens(), timeout=0.1) + await asyncio.wait_for(loop.shutdown_default_executor(), timeout=0.1) + + loop.run_until_complete(close()) + loop.close() + + def pack_kv_entry( key: AnyKvKey, value: bytes, versionstamp: int = 1 ) -> ProtobufKvEntry: @@ -1020,8 +1037,10 @@ def use_kv_and_finalize() -> None: assert session.closed -def test_close_via_finalizer__loop_not_running() -> None: - loop = asyncio.new_event_loop() +def test_close_via_finalizer__loop_not_running( + stopped_loop: asyncio.AbstractEventLoop, +) -> None: + loop = stopped_loop authenticator = Mock() async def create_session() -> aiohttp.ClientSession: