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
45 changes: 45 additions & 0 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import time
import unittest
import unittest.mock
from typing import List

import zeroconf as r
from zeroconf import ServiceInfo, Zeroconf, current_time_millis
Expand Down Expand Up @@ -915,3 +916,47 @@ async def test_cache_flush_bit():
assert loaded_info.addresses == info.addresses

await aiozc.async_close()


# This test uses asyncio because it needs to access the cache directly
# which is not threadsafe
@pytest.mark.asyncio
async def test_record_update_manager_add_listener_callsback_existing_records():
"""Test that the RecordUpdateManager will callback existing records."""

aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
zc: Zeroconf = aiozc.zeroconf
updated = []

class MyListener(r.RecordUpdateListener):
"""A RecordUpdateListener that does not implement update_records."""

def async_update_records(self, zc: 'Zeroconf', now: float, records: List[r.DNSRecord]) -> None:
"""Update multiple records in one shot."""
updated.extend(records)

type_ = "_cacheflush._tcp.local."
name = "knownname"
registration_name = "%s.%s" % (name, type_)
desc = {'path': '/~paulsm/'}
server_name = "server-uu1.local."
info = ServiceInfo(
type_, registration_name, 80, 0, 0, desc, server_name, addresses=[socket.inet_aton("10.0.1.2")]
)
a_record = info.dns_addresses()[0]
ptr_record = info.dns_pointer()
zc.cache.async_add_records([ptr_record, a_record, info.dns_text(), info.dns_service()])

listener = MyListener()

zc.add_listener(
listener,
[
r.DNSQuestion(type_, const._TYPE_PTR, const._CLASS_IN),
r.DNSQuestion(server_name, const._TYPE_A, const._CLASS_IN),
],
)
await asyncio.sleep(0) # flush out the call_soon_threadsafe

assert set(updated) == set([ptr_record, a_record])
await aiozc.async_close()
2 changes: 1 addition & 1 deletion zeroconf/_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@ def add_listener(
self.listeners.append(listener)

if question is None:
self.zc.notify_all()
return

questions = [question] if isinstance(question, DNSQuestion) else question
Expand All @@ -406,6 +405,7 @@ def _async_update_matching_records(
for record in self.cache.async_entries_with_name(question.name):
if not record.is_expired(now) and question.answered_by(record):
records.append(record)

if not records:
return
listener.async_update_records(self.zc, now, records)
Expand Down