Skip to content

Commit ee071a1

Browse files
authored
Log an error when listeners are added that do not inherit from RecordUpdateListener (#1034)
1 parent 28938d2 commit ee071a1

5 files changed

Lines changed: 43 additions & 2 deletions

File tree

tests/test_handlers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,3 +1538,25 @@ async def test_future_answers_are_removed_on_send():
15381538

15391539
# But the one we have not sent yet shoudl still go out later
15401540
assert info2.dns_pointer() in outgoing_queue.queue[0].answers
1541+
1542+
1543+
@pytest.mark.asyncio
1544+
async def test_add_listener_warns_when_not_using_record_update_listener(caplog):
1545+
"""Log when a listener is added that is not using RecordUpdateListener as a base class."""
1546+
1547+
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
1548+
zc: Zeroconf = aiozc.zeroconf
1549+
updated = []
1550+
1551+
class MyListener:
1552+
"""A RecordUpdateListener that does not implement update_records."""
1553+
1554+
def async_update_records(self, zc: 'Zeroconf', now: float, records: List[r.RecordUpdate]) -> None:
1555+
"""Update multiple records in one shot."""
1556+
updated.extend(records)
1557+
1558+
zc.add_listener(MyListener(), None)
1559+
await asyncio.sleep(0) # flush out any call soons
1560+
assert "listeners passed to async_add_listener must inherit from RecordUpdateListener" in caplog.text
1561+
1562+
await aiozc.async_close()

zeroconf/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
)
6464
from ._services.registry import ServiceRegistry # noqa # import needed for backwards compat
6565
from ._services.types import ZeroconfServiceTypes
66-
from ._updates import RecordUpdate, RecordUpdateListener # noqa # import needed for backwards compat
66+
from ._updates import RecordUpdate, RecordUpdateListener
6767
from ._utils.name import service_type_name # noqa # import needed for backwards compat
6868
from ._utils.net import ( # noqa # import needed for backwards compat
6969
add_multicast_member,
@@ -94,6 +94,9 @@
9494
"ServiceStateChange",
9595
"IPVersion",
9696
"ZeroconfServiceTypes",
97+
"RecordUpdate",
98+
"RecordUpdateListener",
99+
"current_time_millis",
97100
# Exceptions
98101
"Error",
99102
"AbstractMethodException",

zeroconf/_handlers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,12 @@ def async_add_listener(
492492
493493
This function is not threadsafe and must be called in the eventloop.
494494
"""
495+
if not isinstance(listener, RecordUpdateListener):
496+
log.error(
497+
"listeners passed to async_add_listener must inherit from RecordUpdateListener;"
498+
" In the future this will fail"
499+
)
500+
495501
self.listeners.append(listener)
496502

497503
if question is None:

zeroconf/_updates.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ class RecordUpdate(NamedTuple):
3636

3737

3838
class RecordUpdateListener:
39+
"""Base call for all record listeners.
40+
41+
All listeners passed to async_add_listener should use RecordUpdateListener
42+
as a base class. In the future it will be required.
43+
"""
44+
3945
def update_record( # pylint: disable=no-self-use
4046
self, zc: 'Zeroconf', now: float, record: DNSRecord
4147
) -> None:

zeroconf/_utils/time.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525

2626

2727
def current_time_millis() -> float:
28-
"""Current system time in milliseconds"""
28+
"""Current time in milliseconds.
29+
30+
The current implemention uses `time.monotonic`
31+
but may change in the future.
32+
"""
2933
return time.monotonic() * 1000
3034

3135

0 commit comments

Comments
 (0)