Skip to content

Commit 0ff3c6b

Browse files
authored
test: shave ServiceBrowser first-query delay on loopback (#1720)
1 parent 9683fe6 commit 0ff3c6b

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

tests/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@
4545
# Timeout for ZeroconfServiceTypes.find() / AsyncZeroconfServiceTypes.async_find()
4646
# in loopback integration tests. `find()` is just `time.sleep(timeout)` —
4747
# it doesn't short-circuit on the first matching response — so the
48-
# timeout becomes a lower bound on the test runtime. On loopback the
49-
# registrar's response lands within a few ms; 200ms is ~50x headroom.
50-
LOOPBACK_FIND_TIMEOUT = 0.2
48+
# timeout becomes a lower bound on the test runtime. Callers MUST use
49+
# the `quick_timing` fixture, which shrinks the browser's first-query
50+
# delay from RFC 6762 §5.2's 20-120ms window to 1-5ms; with that shave
51+
# the registrar's response lands inside ~10ms and 75ms is ~7x headroom.
52+
LOOPBACK_FIND_TIMEOUT = 0.075
5153

5254

5355
class QuestionHistoryWithoutSuppression(QuestionHistory):

tests/conftest.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from zeroconf import _core, const
1212
from zeroconf._handlers import query_handler
13+
from zeroconf._services import browser as service_browser
1314
from zeroconf._services import info as service_info
1415

1516

@@ -46,18 +47,21 @@ def disable_duplicate_packet_suppression():
4647

4748
@pytest.fixture
4849
def quick_timing() -> Generator[None]:
49-
"""Shorten the probe/announce/goodbye intervals for tests on loopback.
50+
"""Shorten the probe/announce/goodbye/first-query intervals for tests on loopback.
5051
5152
The production values (_CHECK_TIME=500ms, _REGISTER_TIME=225ms,
52-
_UNREGISTER_TIME=125ms) exist for RFC 6762 interop on real
53-
networks. Tests on 127.0.0.1 do not need them and pay 1-2s per
54-
register/unregister cycle without this fixture. Opt in by adding
55-
`quick_timing` to a test's argument list.
53+
_UNREGISTER_TIME=125ms, _FIRST_QUERY_DELAY_RANDOM_INTERVAL=20-120ms)
54+
exist for RFC 6762 interop on real networks (§8.1 thundering-herd
55+
avoidance for probing, §5.2 for the initial-query delay). Tests on
56+
127.0.0.1 do not need them and pay 1-2s per register/unregister
57+
cycle and 20-120ms per ServiceBrowser startup without this fixture.
58+
Opt in by adding `quick_timing` to a test's argument list.
5659
"""
5760
with (
5861
patch.object(_core, "_CHECK_TIME", 10),
5962
patch.object(_core, "_REGISTER_TIME", 10),
6063
patch.object(_core, "_UNREGISTER_TIME", 10),
64+
patch.object(service_browser, "_FIRST_QUERY_DELAY_RANDOM_INTERVAL", (1, 5)),
6165
):
6266
yield
6367

tests/services/test_types.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def teardown_module():
2828
log.setLevel(original_logging_level)
2929

3030

31-
def test_integration_with_listener(disable_duplicate_packet_suppression):
31+
def test_integration_with_listener(quick_timing, disable_duplicate_packet_suppression):
3232
type_ = "_test-listen-type._tcp.local."
3333
name = "xxxyyy"
3434
registration_name = f"{name}.{type_}"
@@ -59,7 +59,7 @@ def test_integration_with_listener(disable_duplicate_packet_suppression):
5959

6060
@unittest.skipIf(not has_working_ipv6(), "Requires IPv6")
6161
@unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled")
62-
def test_integration_with_listener_v6_records(disable_duplicate_packet_suppression):
62+
def test_integration_with_listener_v6_records(quick_timing, disable_duplicate_packet_suppression):
6363
type_ = "_test-listenv6rec-type._tcp.local."
6464
name = "xxxyyy"
6565
registration_name = f"{name}.{type_}"
@@ -95,7 +95,7 @@ def test_integration_with_listener_v6_records(disable_duplicate_packet_suppressi
9595
sys.platform == "darwin" and os.environ.get("GITHUB_ACTIONS") == "true",
9696
"IPv6 multicast not working on macOS GitHub Actions",
9797
)
98-
def test_integration_with_listener_ipv6(disable_duplicate_packet_suppression):
98+
def test_integration_with_listener_ipv6(quick_timing, disable_duplicate_packet_suppression):
9999
type_ = "_test-listenv6ip-type._tcp.local."
100100
name = "xxxyyy"
101101
registration_name = f"{name}.{type_}"
@@ -127,7 +127,7 @@ def test_integration_with_listener_ipv6(disable_duplicate_packet_suppression):
127127
zeroconf_registrar.close()
128128

129129

130-
def test_integration_with_subtype_and_listener(disable_duplicate_packet_suppression):
130+
def test_integration_with_subtype_and_listener(quick_timing, disable_duplicate_packet_suppression):
131131
subtype_ = "_subtype._sub"
132132
type_ = "_listen._tcp.local."
133133
name = "xxxyyy"

0 commit comments

Comments
 (0)