From 49e6fe379a6a6e578246b37bfe51d0dc8dcb62e1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 5 Oct 2023 10:36:20 -0500 Subject: [PATCH 1/5] fix: ensure ServiceInfo cache is cleared when adding to the registry --- src/zeroconf/_services/info.py | 8 ++++++++ src/zeroconf/_services/registry.py | 1 + 2 files changed, 9 insertions(+) diff --git a/src/zeroconf/_services/info.py b/src/zeroconf/_services/info.py index 0600d5d34..ee033c823 100644 --- a/src/zeroconf/_services/info.py +++ b/src/zeroconf/_services/info.py @@ -273,6 +273,14 @@ def properties(self) -> Dict[Union[str, bytes], Optional[Union[str, bytes]]]: assert self._properties is not None return self._properties + def async_clear_cache(self) -> None: + """Clear the cache for this service info.""" + self._dns_address_cache = None + self._dns_pointer_cache = None + self._dns_service_cache = None + self._dns_text_cache = None + self._get_address_and_nsec_records_cache = None + async def async_wait(self, timeout: float, loop: Optional[asyncio.AbstractEventLoop] = None) -> None: """Calling task waits for a given number of milliseconds or until notified.""" if not self._new_records_futures: diff --git a/src/zeroconf/_services/registry.py b/src/zeroconf/_services/registry.py index 12051275e..e9dc4a62b 100644 --- a/src/zeroconf/_services/registry.py +++ b/src/zeroconf/_services/registry.py @@ -91,6 +91,7 @@ def _add(self, info: ServiceInfo) -> None: if info.key in self._services: raise ServiceNameAlreadyRegistered + info.async_clear_cache() self._services[info.key] = info self.types.setdefault(info.type.lower(), []).append(info.key) self.servers.setdefault(info.server_key, []).append(info.key) From 9dc0258efe98ed0d6144039a3e2a1f2b96674006 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 5 Oct 2023 10:39:16 -0500 Subject: [PATCH 2/5] fix: make sure async_update_service clears the internal service info cache --- tests/test_asyncio.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_asyncio.py b/tests/test_asyncio.py index d77e7e832..2b8fe1134 100644 --- a/tests/test_asyncio.py +++ b/tests/test_asyncio.py @@ -171,6 +171,12 @@ def update_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: ) task = await aiozc.async_update_service(new_info) await task + assert new_info.dns_service().server_key == "ash-2.local." + new_info.server = "ash-3.local." + task = await aiozc.async_update_service(new_info) + await task + assert new_info.dns_service().server_key == "ash-3.local." + task = await aiozc.async_unregister_service(new_info) await task await aiozc.async_close() From 82bd80b4bef42460678f3ad03ccb1fa7eb7baf88 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 5 Oct 2023 10:43:01 -0500 Subject: [PATCH 3/5] fix: tests --- tests/conftest.py | 7 +++++++ tests/test_asyncio.py | 1 + 2 files changed, 8 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 5cdff18e0..b43b7090d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -11,6 +11,13 @@ from zeroconf import _core, _listener, const +@pytest.fixture(autouse=True) +def fast_register(): + """Make the register call faster.""" + with patch.object(_core, "_REGISTER_TIME", 0.0), patch.object(const, "_REGISTER_TIME", 0.0): + yield + + @pytest.fixture(autouse=True) def verify_threads_ended(): """Verify that the threads are not running after the test.""" diff --git a/tests/test_asyncio.py b/tests/test_asyncio.py index 2b8fe1134..25dd4681d 100644 --- a/tests/test_asyncio.py +++ b/tests/test_asyncio.py @@ -184,6 +184,7 @@ def update_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: assert calls == [ ('add', type_, registration_name), ('update', type_, registration_name), + ('update', type_, registration_name), ('remove', type_, registration_name), ] From a1e6cde05b3a8b9c93cc58f240a1ccb0dd940e3c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 5 Oct 2023 10:44:39 -0500 Subject: [PATCH 4/5] fix: tests --- src/zeroconf/_services/info.pxd | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/zeroconf/_services/info.pxd b/src/zeroconf/_services/info.pxd index 223883165..dcfc3a8fe 100644 --- a/src/zeroconf/_services/info.pxd +++ b/src/zeroconf/_services/info.pxd @@ -107,3 +107,5 @@ cdef class ServiceInfo(RecordUpdateListener): @cython.locals(cacheable=cython.bint) cdef cython.set _get_address_and_nsec_records(self, object override_ttl) + + cpdef async_clear_cache(self) From ab40705570c9f91b89f414647960ddd09202b86e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 5 Oct 2023 10:50:21 -0500 Subject: [PATCH 5/5] too fast --- tests/conftest.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b43b7090d..5cdff18e0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -11,13 +11,6 @@ from zeroconf import _core, _listener, const -@pytest.fixture(autouse=True) -def fast_register(): - """Make the register call faster.""" - with patch.object(_core, "_REGISTER_TIME", 0.0), patch.object(const, "_REGISTER_TIME", 0.0): - yield - - @pytest.fixture(autouse=True) def verify_threads_ended(): """Verify that the threads are not running after the test."""