Skip to content

Commit 72e709b

Browse files
authored
Add async_unregister_all_services to AsyncZeroconf (#649)
1 parent 79e39c0 commit 72e709b

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

tests/test_aio.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from zeroconf._services import ServiceInfo, ServiceListener
1919
from zeroconf._utils.time import current_time_millis
2020

21+
from . import _clear_cache
22+
2123

2224
from . import _clear_cache
2325

@@ -498,3 +500,61 @@ async def test_async_context_manager() -> None:
498500
await task
499501
aiosinfo = await aiozc.async_get_service_info(type_, registration_name)
500502
assert aiosinfo is not None
503+
504+
505+
@pytest.mark.asyncio
506+
async def test_async_unregister_all_services() -> None:
507+
"""Test unregistering all services."""
508+
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
509+
type_ = "_test1-srvc-type._tcp.local."
510+
name = "xxxyyy"
511+
name2 = "abc"
512+
registration_name = "%s.%s" % (name, type_)
513+
registration_name2 = "%s.%s" % (name2, type_)
514+
515+
desc = {'path': '/~paulsm/'}
516+
info = ServiceInfo(
517+
type_,
518+
registration_name,
519+
80,
520+
0,
521+
0,
522+
desc,
523+
"ash-1.local.",
524+
addresses=[socket.inet_aton("10.0.1.2")],
525+
)
526+
info2 = ServiceInfo(
527+
type_,
528+
registration_name2,
529+
80,
530+
0,
531+
0,
532+
desc,
533+
"ash-5.local.",
534+
addresses=[socket.inet_aton("10.0.1.5")],
535+
)
536+
tasks = []
537+
tasks.append(await aiozc.async_register_service(info))
538+
tasks.append(await aiozc.async_register_service(info2))
539+
await asyncio.gather(*tasks)
540+
541+
tasks = []
542+
tasks.append(aiozc.async_get_service_info(type_, registration_name))
543+
tasks.append(aiozc.async_get_service_info(type_, registration_name2))
544+
results = await asyncio.gather(*tasks)
545+
assert results[0] is not None
546+
assert results[1] is not None
547+
548+
await aiozc.async_unregister_all_services()
549+
550+
tasks = []
551+
tasks.append(aiozc.async_get_service_info(type_, registration_name))
552+
tasks.append(aiozc.async_get_service_info(type_, registration_name2))
553+
results = await asyncio.gather(*tasks)
554+
assert results[0] is None
555+
assert results[1] is None
556+
557+
# Verify we can call again
558+
await aiozc.async_unregister_all_services()
559+
560+
await aiozc.async_close()

zeroconf/aio.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,21 @@ async def async_register_service(
227227
self.zeroconf.registry.add(info)
228228
return asyncio.ensure_future(self._async_broadcast_service(info, _REGISTER_TIME, None))
229229

230+
async def async_unregister_all_services(self) -> None:
231+
"""Unregister all registered services.
232+
233+
Unlike async_register_service and async_unregister_service, this
234+
method does not return a future and is always expected to be
235+
awaited since its only called at shutdown.
236+
"""
237+
out = self.zeroconf.generate_unregister_all_services()
238+
if not out:
239+
return
240+
for i in range(3):
241+
if i != 0:
242+
await asyncio.sleep(millis_to_seconds(_UNREGISTER_TIME))
243+
self.zeroconf.async_send(out)
244+
230245
async def async_check_service(self, info: ServiceInfo, cooperating_responders: bool = False) -> None:
231246
"""Checks the network for a unique service name."""
232247
instance_name_from_service_info(info)

0 commit comments

Comments
 (0)