Skip to content

refactor: extract loopback Zeroconf fixtures and mock_incoming_msg helper#1758

Merged
bdraco merged 1 commit into
masterfrom
refactor/test-fixtures-and-helper
May 20, 2026
Merged

refactor: extract loopback Zeroconf fixtures and mock_incoming_msg helper#1758
bdraco merged 1 commit into
masterfrom
refactor/test-fixtures-and-helper

Conversation

@bdraco
Copy link
Copy Markdown
Member

@bdraco bdraco commented May 20, 2026

Summary

Lifts the helper + fixtures sketched in #1757, removes the duplicated mock_incoming_msg definitions that already exist in the suite, and adopts the new fixtures in tests/test_engine.py so the new infrastructure pays for itself within the same PR.

Replaces and closes #1757.

Details

  • tests/__init__.py: adds mock_incoming_msg(records: Iterable[DNSRecord]) -> DNSIncoming, fully typed. The four local copies (one in tests/services/test_browser.py, three in tests/services/test_info.py) are removed and replaced with imports from tests/__init__.py. The dedicated copy in tests/test_core.py is left in place since it has a different signature (takes a ServiceStateChange).
  • tests/conftest.py: adds zc_loopback and aiozc_loopback fixtures returning Generator[Zeroconf] and AsyncGenerator[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_loopback uses @pytest_asyncio.fixture so it works under pytest-asyncio's strict mode (the project default; this is enforced by asyncio_mode = "strict" in pyproject.toml).
  • tests/test_engine.py: converts test_setup_releases_socket_ownership and test_async_close_propagates_outer_cancellation to use aiozc_loopback, removing the explicit try/finally + async_close() boilerplate from each. test_reaper and test_reaper_aborts_when_done deliberately call async_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 run on 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_loopback and mock_incoming_msg can 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.

…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.
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 20, 2026

Merging this PR will not alter performance

✅ 14 untouched benchmarks


Comparing refactor/test-fixtures-and-helper (5c45abe) with master (4ff6540)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (f4b5066) during the generation of this report, so 4ff6540 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.77%. Comparing base (4ff6540) to head (5c45abe).
⚠️ Report is 2 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bdraco bdraco marked this pull request as ready for review May 20, 2026 15:42
@bdraco bdraco merged commit cb0af4a into master May 20, 2026
33 checks passed
@bdraco bdraco deleted the refactor/test-fixtures-and-helper branch May 20, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant