Skip to content

Commit 64d143d

Browse files
authored
test: drop ZeroconfServiceTypes.find() timeouts from 500ms to 200ms on loopback (#1710)
1 parent 963d3d7 commit 64d143d

3 files changed

Lines changed: 29 additions & 13 deletions

File tree

tests/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
# or two queries.
4343
QUICK_REQUEST_TIMEOUT_MS = 50
4444

45+
# Timeout for ZeroconfServiceTypes.find() / AsyncZeroconfServiceTypes.async_find()
46+
# in loopback integration tests. `find()` is just `time.sleep(timeout)` —
47+
# 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
51+
4552

4653
class QuestionHistoryWithoutSuppression(QuestionHistory):
4754
def suppresses(self, question: DNSQuestion, now: float, known_answers: set[DNSRecord]) -> bool:

tests/services/test_types.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import zeroconf as r
1212
from zeroconf import ServiceInfo, Zeroconf, ZeroconfServiceTypes
1313

14-
from .. import _clear_cache, has_working_ipv6
14+
from .. import LOOPBACK_FIND_TIMEOUT, _clear_cache, has_working_ipv6
1515

1616
log = logging.getLogger("zeroconf")
1717
original_logging_level = logging.NOTSET
@@ -47,10 +47,10 @@ def test_integration_with_listener(disable_duplicate_packet_suppression):
4747
)
4848
zeroconf_registrar.registry.async_add(info)
4949
try:
50-
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=0.5)
50+
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT)
5151
assert type_ in service_types
5252
_clear_cache(zeroconf_registrar)
53-
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
53+
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT)
5454
assert type_ in service_types
5555

5656
finally:
@@ -79,10 +79,10 @@ def test_integration_with_listener_v6_records(disable_duplicate_packet_suppressi
7979
)
8080
zeroconf_registrar.registry.async_add(info)
8181
try:
82-
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=0.5)
82+
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT)
8383
assert type_ in service_types
8484
_clear_cache(zeroconf_registrar)
85-
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
85+
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT)
8686
assert type_ in service_types
8787

8888
finally:
@@ -115,10 +115,12 @@ def test_integration_with_listener_ipv6(disable_duplicate_packet_suppression):
115115
)
116116
zeroconf_registrar.registry.async_add(info)
117117
try:
118-
service_types = ZeroconfServiceTypes.find(ip_version=r.IPVersion.V6Only, timeout=0.5)
118+
service_types = ZeroconfServiceTypes.find(
119+
ip_version=r.IPVersion.V6Only, timeout=LOOPBACK_FIND_TIMEOUT
120+
)
119121
assert type_ in service_types
120122
_clear_cache(zeroconf_registrar)
121-
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
123+
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT)
122124
assert type_ in service_types
123125

124126
finally:
@@ -147,10 +149,10 @@ def test_integration_with_subtype_and_listener(disable_duplicate_packet_suppress
147149
)
148150
zeroconf_registrar.registry.async_add(info)
149151
try:
150-
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=0.5)
152+
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT)
151153
assert discovery_type in service_types
152154
_clear_cache(zeroconf_registrar)
153-
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
155+
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT)
154156
assert discovery_type in service_types
155157

156158
finally:

tests/test_asyncio.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from zeroconf.const import _LISTENER_TIME
4444

4545
from . import (
46+
LOOPBACK_FIND_TIMEOUT,
4647
QUICK_REQUEST_TIMEOUT_MS,
4748
QuestionHistoryWithoutSuppression,
4849
_clear_cache,
@@ -919,14 +920,20 @@ async def test_async_zeroconf_service_types(quick_timing: None) -> None:
919920
)
920921
task = await zeroconf_registrar.async_register_service(info)
921922
await task
922-
# Ensure we do not clear the cache until after the last broadcast is processed
923-
await asyncio.sleep(0.2)
923+
# Wait for the last announce broadcast before clearing. With
924+
# `quick_timing` the broadcasts use _REGISTER_TIME=10ms apart so
925+
# 50ms is plenty.
926+
await asyncio.sleep(0.05)
924927
_clear_cache(zeroconf_registrar.zeroconf)
925928
try:
926-
service_types = await AsyncZeroconfServiceTypes.async_find(interfaces=["127.0.0.1"], timeout=0.5)
929+
service_types = await AsyncZeroconfServiceTypes.async_find(
930+
interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT
931+
)
927932
assert type_ in service_types
928933
_clear_cache(zeroconf_registrar.zeroconf)
929-
service_types = await AsyncZeroconfServiceTypes.async_find(aiozc=zeroconf_registrar, timeout=0.5)
934+
service_types = await AsyncZeroconfServiceTypes.async_find(
935+
aiozc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT
936+
)
930937
assert type_ in service_types
931938

932939
finally:

0 commit comments

Comments
 (0)