Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
# or two queries.
QUICK_REQUEST_TIMEOUT_MS = 50

# Timeout for ZeroconfServiceTypes.find() / AsyncZeroconfServiceTypes.async_find()
# in loopback integration tests. `find()` is just `time.sleep(timeout)` —
# it doesn't short-circuit on the first matching response — so the
# timeout becomes a lower bound on the test runtime. On loopback the
# registrar's response lands within a few ms; 200ms is ~50x headroom.
LOOPBACK_FIND_TIMEOUT = 0.2


class QuestionHistoryWithoutSuppression(QuestionHistory):
def suppresses(self, question: DNSQuestion, now: float, known_answers: set[DNSRecord]) -> bool:
Expand Down
20 changes: 11 additions & 9 deletions tests/services/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import zeroconf as r
from zeroconf import ServiceInfo, Zeroconf, ZeroconfServiceTypes

from .. import _clear_cache, has_working_ipv6
from .. import LOOPBACK_FIND_TIMEOUT, _clear_cache, has_working_ipv6

log = logging.getLogger("zeroconf")
original_logging_level = logging.NOTSET
Expand Down Expand Up @@ -47,10 +47,10 @@ def test_integration_with_listener(disable_duplicate_packet_suppression):
)
zeroconf_registrar.registry.async_add(info)
try:
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=0.5)
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT)
assert type_ in service_types
_clear_cache(zeroconf_registrar)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT)
assert type_ in service_types

finally:
Expand Down Expand Up @@ -79,10 +79,10 @@ def test_integration_with_listener_v6_records(disable_duplicate_packet_suppressi
)
zeroconf_registrar.registry.async_add(info)
try:
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=0.5)
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT)
assert type_ in service_types
_clear_cache(zeroconf_registrar)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT)
assert type_ in service_types

finally:
Expand Down Expand Up @@ -115,10 +115,12 @@ def test_integration_with_listener_ipv6(disable_duplicate_packet_suppression):
)
zeroconf_registrar.registry.async_add(info)
try:
service_types = ZeroconfServiceTypes.find(ip_version=r.IPVersion.V6Only, timeout=0.5)
service_types = ZeroconfServiceTypes.find(
ip_version=r.IPVersion.V6Only, timeout=LOOPBACK_FIND_TIMEOUT
)
assert type_ in service_types
_clear_cache(zeroconf_registrar)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT)
assert type_ in service_types

finally:
Expand Down Expand Up @@ -147,10 +149,10 @@ def test_integration_with_subtype_and_listener(disable_duplicate_packet_suppress
)
zeroconf_registrar.registry.async_add(info)
try:
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=0.5)
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT)
assert discovery_type in service_types
_clear_cache(zeroconf_registrar)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT)
assert discovery_type in service_types

finally:
Expand Down
15 changes: 11 additions & 4 deletions tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from zeroconf.const import _LISTENER_TIME

from . import (
LOOPBACK_FIND_TIMEOUT,
QUICK_REQUEST_TIMEOUT_MS,
QuestionHistoryWithoutSuppression,
_clear_cache,
Expand Down Expand Up @@ -919,14 +920,20 @@ async def test_async_zeroconf_service_types(quick_timing: None) -> None:
)
task = await zeroconf_registrar.async_register_service(info)
await task
# Ensure we do not clear the cache until after the last broadcast is processed
await asyncio.sleep(0.2)
# Wait for the last announce broadcast before clearing. With
# `quick_timing` the broadcasts use _REGISTER_TIME=10ms apart so
# 50ms is plenty.
await asyncio.sleep(0.05)
_clear_cache(zeroconf_registrar.zeroconf)
try:
service_types = await AsyncZeroconfServiceTypes.async_find(interfaces=["127.0.0.1"], timeout=0.5)
service_types = await AsyncZeroconfServiceTypes.async_find(
interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT
)
assert type_ in service_types
_clear_cache(zeroconf_registrar.zeroconf)
service_types = await AsyncZeroconfServiceTypes.async_find(aiozc=zeroconf_registrar, timeout=0.5)
service_types = await AsyncZeroconfServiceTypes.async_find(
aiozc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT
)
assert type_ in service_types

finally:
Expand Down