|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import threading |
6 | | -from collections.abc import AsyncGenerator, Generator |
| 6 | +from collections.abc import AsyncGenerator, Generator, Iterator |
7 | 7 | from unittest.mock import patch |
8 | 8 |
|
9 | 9 | import pytest |
|
15 | 15 | from zeroconf._services import info as service_info |
16 | 16 | from zeroconf.asyncio import AsyncZeroconf |
17 | 17 |
|
| 18 | +try: |
| 19 | + from blockbuster import BlockBuster, blockbuster_ctx |
| 20 | +except ImportError: # platforms without blockbuster (e.g. PyPy under QEMU) |
| 21 | + BlockBuster = None # type: ignore[assignment,misc] |
| 22 | + blockbuster_ctx = None # type: ignore[assignment] |
| 23 | + |
| 24 | +_BENCHMARKS_DIR = "tests/benchmarks" |
| 25 | + |
| 26 | +# Tests that perform sync IO inside the asyncio event loop and trip |
| 27 | +# blockbuster. Marked xfail so CI stays green; pop entries as they get |
| 28 | +# fixed so the underlying blocking call is gone for good. The single |
| 29 | +# entry below pins the deliberate sync-bootstrap path Zeroconf takes |
| 30 | +# when constructed with `use_asyncio=False` from inside a running loop |
| 31 | +# — that block on the loop-thread-ready event is the behavior the test |
| 32 | +# is documenting, so it stays xfail by design. |
| 33 | +_KNOWN_BLOCKING: frozenset[str] = frozenset( |
| 34 | + { |
| 35 | + "tests/test_core.py::Framework::test_use_asyncio_false_forces_thread_when_loop_running", |
| 36 | + } |
| 37 | +) |
| 38 | + |
| 39 | + |
| 40 | +def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None: |
| 41 | + """Mark known-blocking tests xfail so blockbuster doesn't fail the suite.""" |
| 42 | + if blockbuster_ctx is None: |
| 43 | + return |
| 44 | + marker = pytest.mark.xfail( |
| 45 | + reason="blockbuster: blocking call in asyncio path", |
| 46 | + strict=False, |
| 47 | + ) |
| 48 | + for item in items: |
| 49 | + if item.nodeid in _KNOWN_BLOCKING: |
| 50 | + item.add_marker(marker) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture(autouse=True) |
| 54 | +def blockbuster( |
| 55 | + request: pytest.FixtureRequest, |
| 56 | +) -> Iterator[BlockBuster | None]: |
| 57 | + """Fail any test that performs a blocking call inside the asyncio loop.""" |
| 58 | + if blockbuster_ctx is None or _BENCHMARKS_DIR in str(request.node.fspath): |
| 59 | + yield None |
| 60 | + return |
| 61 | + with blockbuster_ctx() as bb: |
| 62 | + yield bb |
| 63 | + |
18 | 64 |
|
19 | 65 | @pytest.fixture(autouse=True) |
20 | 66 | def verify_threads_ended(): |
|
0 commit comments