refactor: extract loopback Zeroconf fixtures and mock_incoming_msg helper#1758
Merged
Conversation
…lper The pattern `zc = Zeroconf(interfaces=["127.0.0.1"]); ...; zc.close()` appears 100+ times across the test suite, and the async variant roughly 30 times. Four files duplicate a byte-identical `mock_incoming_msg(records) -> DNSIncoming` helper that wraps records in a `DNSOutgoing(_FLAGS_QR_RESPONSE)` and parses the resulting wire bytes back through `DNSIncoming`. This PR: - Adds `mock_incoming_msg` to `tests/__init__.py`, replacing the four local copies in `tests/services/test_browser.py` and `tests/services/test_info.py`. - Adds `zc_loopback` (sync) and `aiozc_loopback` (async) fixtures to `tests/conftest.py` that yield a loopback-only Zeroconf instance and close it on teardown via try/finally. `close()` / `async_close()` remain idempotent, so tests are free to call them mid-test (e.g. to test post-close behavior) without conflicting with the fixture's cleanup. - Adopts `aiozc_loopback` in `tests/test_engine.py` to demonstrate the fixture's use — converts the two tests that previously had explicit try/finally + async_close blocks. The async fixture uses `@pytest_asyncio.fixture` so it works under pytest-asyncio's strict mode (the project default). Replaces and closes #1757.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1758 +/- ##
=======================================
Coverage 99.77% 99.77%
=======================================
Files 33 33
Lines 3500 3500
Branches 490 490
=======================================
Hits 3492 3492
Misses 5 5
Partials 3 3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Lifts the helper + fixtures sketched in #1757, removes the duplicated
mock_incoming_msgdefinitions that already exist in the suite, and adopts the new fixtures intests/test_engine.pyso the new infrastructure pays for itself within the same PR.Replaces and closes #1757.
Details
tests/__init__.py: addsmock_incoming_msg(records: Iterable[DNSRecord]) -> DNSIncoming, fully typed. The four local copies (one intests/services/test_browser.py, three intests/services/test_info.py) are removed and replaced with imports fromtests/__init__.py. The dedicated copy intests/test_core.pyis left in place since it has a different signature (takes aServiceStateChange).tests/conftest.py: addszc_loopbackandaiozc_loopbackfixtures returningGenerator[Zeroconf]andAsyncGenerator[AsyncZeroconf]respectively. Both yield a loopback-only instance and close it on teardown via try/finally.close()/async_close()are idempotent, so tests can still call them mid-test (e.g. to assert post-close behavior) without conflicting with fixture cleanup.aiozc_loopbackuses@pytest_asyncio.fixtureso it works under pytest-asyncio's strict mode (the project default; this is enforced byasyncio_mode = "strict"inpyproject.toml).tests/test_engine.py: convertstest_setup_releases_socket_ownershipandtest_async_close_propagates_outer_cancellationto useaiozc_loopback, removing the explicit try/finally +async_close()boilerplate from each.test_reaperandtest_reaper_aborts_when_donedeliberately callasync_close()mid-test to exercise post-close behavior, so they keep their inline setup; the fixture would still work there but the conversion would be less obviously useful and is left for a follow-up.Test plan
REQUIRE_CYTHON=1 poetry install --only=main,dev && poetry run pytest tests/ --no-cov --timeout=60: 384 passed, 3 skipped.SKIP_CYTHON=1 poetry run pytest tests/ --no-cov --timeout=60: 384 passed, 3 skipped.poetry run pre-commit runon touched files: ruff, ruff format, mypy, codespell, flake8, cython-lint all green.poetry run mypy tests/__init__.py tests/conftest.py tests/test_engine.py: clean. New helper and fixtures are fully typed.Follow-up opportunities
Once landed,
zc_loopback/aiozc_loopbackandmock_incoming_msgcan replace boilerplate in a long tail of tests (test_handlers.py,test_asyncio.py,tests/services/test_browser.py, etc.). Holding those off so this PR stays mechanically reviewable; subsequent PRs can each tackle one file at a time.