Skip to content

Commit 0b9d36f

Browse files
authored
chore: fix handler tests (#1296)
1 parent 0060f79 commit 0b9d36f

3 files changed

Lines changed: 52 additions & 32 deletions

File tree

src/zeroconf/_handlers/multicast_outgoing_queue.pxd

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ cdef object RAND_INT
1212
cdef class MulticastOutgoingQueue:
1313

1414
cdef object zc
15-
cdef object queue
16-
cdef cython.uint additional_delay
17-
cdef cython.uint aggregation_delay
15+
cdef public object queue
16+
cdef public object _multicast_delay_random_min
17+
cdef public object _multicast_delay_random_max
18+
cdef object _additional_delay
19+
cdef object _aggregation_delay
1820

19-
@cython.locals(last_group=AnswerGroup, random_int=cython.uint, random_delay=float, send_after=float, send_before=float)
21+
@cython.locals(last_group=AnswerGroup, random_int=cython.uint)
2022
cpdef async_add(self, float now, cython.dict answers)
2123

2224
@cython.locals(pending=AnswerGroup)

src/zeroconf/_handlers/multicast_outgoing_queue.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,41 @@
3838
from .._core import Zeroconf
3939

4040
_float = float
41+
_int = int
4142

4243

4344
class MulticastOutgoingQueue:
4445
"""An outgoing queue used to aggregate multicast responses."""
4546

46-
__slots__ = ("zc", "queue", "additional_delay", "aggregation_delay")
47+
__slots__ = (
48+
"zc",
49+
"queue",
50+
"_multicast_delay_random_min",
51+
"_multicast_delay_random_max",
52+
"_additional_delay",
53+
"_aggregation_delay",
54+
)
4755

48-
def __init__(self, zeroconf: 'Zeroconf', additional_delay: int, max_aggregation_delay: int) -> None:
56+
def __init__(self, zeroconf: 'Zeroconf', additional_delay: _int, max_aggregation_delay: _int) -> None:
4957
self.zc = zeroconf
5058
self.queue: deque[AnswerGroup] = deque()
5159
# Additional delay is used to implement
5260
# Protect the network against excessive packet flooding
5361
# https://datatracker.ietf.org/doc/html/rfc6762#section-14
54-
self.additional_delay = additional_delay
55-
self.aggregation_delay = max_aggregation_delay
62+
self._multicast_delay_random_min = MULTICAST_DELAY_RANDOM_INTERVAL[0]
63+
self._multicast_delay_random_max = MULTICAST_DELAY_RANDOM_INTERVAL[1]
64+
self._additional_delay = additional_delay
65+
self._aggregation_delay = max_aggregation_delay
5666

5767
def async_add(self, now: _float, answers: _AnswerWithAdditionalsType) -> None:
5868
"""Add a group of answers with additionals to the outgoing queue."""
5969
loop = self.zc.loop
6070
if TYPE_CHECKING:
6171
assert loop is not None
62-
random_int = RAND_INT(*MULTICAST_DELAY_RANDOM_INTERVAL)
63-
random_delay = random_int + self.additional_delay
72+
random_int = RAND_INT(self._multicast_delay_random_min, self._multicast_delay_random_max)
73+
random_delay = random_int + self._additional_delay
6474
send_after = now + random_delay
65-
send_before = now + self.aggregation_delay + self.additional_delay
75+
send_before = now + self._aggregation_delay + self._additional_delay
6676
if len(self.queue):
6777
# If we calculate a random delay for the send after time
6878
# that is less than the last group scheduled to go out,

tests/test_handlers.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import unittest
1212
import unittest.mock
1313
from typing import List, cast
14+
from unittest.mock import patch
1415

1516
import pytest
1617

1718
import zeroconf as r
1819
from zeroconf import ServiceInfo, Zeroconf, const, current_time_millis
19-
from zeroconf._handlers import multicast_outgoing_queue
2020
from zeroconf._handlers.multicast_outgoing_queue import (
2121
MulticastOutgoingQueue,
2222
construct_outgoing_multicast_answers,
@@ -1413,7 +1413,7 @@ async def test_response_aggregation_timings(run_isolated):
14131413
zc = aiozc.zeroconf
14141414
protocol = zc.engine.protocols[0]
14151415

1416-
with unittest.mock.patch.object(aiozc.zeroconf, "async_send") as send_mock:
1416+
with patch.object(aiozc.zeroconf, "async_send") as send_mock:
14171417
protocol.datagram_received(query.packets()[0], ('127.0.0.1', const._MDNS_PORT))
14181418
protocol.datagram_received(query2.packets()[0], ('127.0.0.1', const._MDNS_PORT))
14191419
protocol.datagram_received(query.packets()[0], ('127.0.0.1', const._MDNS_PORT))
@@ -1492,7 +1492,7 @@ async def test_response_aggregation_timings_multiple(run_isolated, disable_dupli
14921492
zc = aiozc.zeroconf
14931493
protocol = zc.engine.protocols[0]
14941494

1495-
with unittest.mock.patch.object(aiozc.zeroconf, "async_send") as send_mock:
1495+
with patch.object(aiozc.zeroconf, "async_send") as send_mock:
14961496
send_mock.reset_mock()
14971497
protocol.datagram_received(query2.packets()[0], ('127.0.0.1', const._MDNS_PORT))
14981498
protocol.last_time = 0 # manually reset the last time to avoid duplicate packet suppression
@@ -1581,16 +1581,19 @@ async def test_response_aggregation_random_delay():
15811581
outgoing_queue = MulticastOutgoingQueue(mocked_zc, 0, 500)
15821582

15831583
now = current_time_millis()
1584-
with unittest.mock.patch.object(multicast_outgoing_queue, "MULTICAST_DELAY_RANDOM_INTERVAL", (500, 600)):
1585-
outgoing_queue.async_add(now, {info.dns_pointer(): set()})
1584+
outgoing_queue._multicast_delay_random_min = 500
1585+
outgoing_queue._multicast_delay_random_max = 600
1586+
outgoing_queue.async_add(now, {info.dns_pointer(): set()})
15861587

15871588
# The second group should always be coalesced into first group since it will always come before
1588-
with unittest.mock.patch.object(multicast_outgoing_queue, "MULTICAST_DELAY_RANDOM_INTERVAL", (300, 400)):
1589-
outgoing_queue.async_add(now, {info2.dns_pointer(): set()})
1589+
outgoing_queue._multicast_delay_random_min = 300
1590+
outgoing_queue._multicast_delay_random_max = 400
1591+
outgoing_queue.async_add(now, {info2.dns_pointer(): set()})
15901592

15911593
# The third group should always be coalesced into first group since it will always come before
1592-
with unittest.mock.patch.object(multicast_outgoing_queue, "MULTICAST_DELAY_RANDOM_INTERVAL", (100, 200)):
1593-
outgoing_queue.async_add(now, {info3.dns_pointer(): set(), info4.dns_pointer(): set()})
1594+
outgoing_queue._multicast_delay_random_min = 100
1595+
outgoing_queue._multicast_delay_random_max = 200
1596+
outgoing_queue.async_add(now, {info3.dns_pointer(): set(), info4.dns_pointer(): set()})
15941597

15951598
assert len(outgoing_queue.queue) == 1
15961599
assert info.dns_pointer() in outgoing_queue.queue[0].answers
@@ -1599,8 +1602,9 @@ async def test_response_aggregation_random_delay():
15991602
assert info4.dns_pointer() in outgoing_queue.queue[0].answers
16001603

16011604
# The forth group should not be coalesced because its scheduled after the last group in the queue
1602-
with unittest.mock.patch.object(multicast_outgoing_queue, "MULTICAST_DELAY_RANDOM_INTERVAL", (700, 800)):
1603-
outgoing_queue.async_add(now, {info5.dns_pointer(): set()})
1605+
outgoing_queue._multicast_delay_random_min = 700
1606+
outgoing_queue._multicast_delay_random_max = 800
1607+
outgoing_queue.async_add(now, {info5.dns_pointer(): set()})
16041608

16051609
assert len(outgoing_queue.queue) == 2
16061610
assert info.dns_pointer() not in outgoing_queue.queue[1].answers
@@ -1630,21 +1634,22 @@ async def test_future_answers_are_removed_on_send():
16301634
outgoing_queue = MulticastOutgoingQueue(mocked_zc, 0, 0)
16311635

16321636
now = current_time_millis()
1633-
with unittest.mock.patch.object(multicast_outgoing_queue, "MULTICAST_DELAY_RANDOM_INTERVAL", (1, 1)):
1634-
outgoing_queue.async_add(now, {info.dns_pointer(): set()})
1637+
outgoing_queue._multicast_delay_random_min = 1
1638+
outgoing_queue._multicast_delay_random_max = 1
1639+
outgoing_queue.async_add(now, {info.dns_pointer(): set()})
16351640

16361641
assert len(outgoing_queue.queue) == 1
16371642

1638-
with unittest.mock.patch.object(multicast_outgoing_queue, "MULTICAST_DELAY_RANDOM_INTERVAL", (2, 2)):
1639-
outgoing_queue.async_add(now, {info.dns_pointer(): set()})
1643+
outgoing_queue._multicast_delay_random_min = 2
1644+
outgoing_queue._multicast_delay_random_max = 2
1645+
outgoing_queue.async_add(now, {info.dns_pointer(): set()})
16401646

16411647
assert len(outgoing_queue.queue) == 2
16421648

1643-
with unittest.mock.patch.object(
1644-
multicast_outgoing_queue, "MULTICAST_DELAY_RANDOM_INTERVAL", (1000, 1000)
1645-
):
1646-
outgoing_queue.async_add(now, {info2.dns_pointer(): set()})
1647-
outgoing_queue.async_add(now, {info.dns_pointer(): set()})
1649+
outgoing_queue._multicast_delay_random_min = 1000
1650+
outgoing_queue._multicast_delay_random_max = 1000
1651+
outgoing_queue.async_add(now, {info2.dns_pointer(): set()})
1652+
outgoing_queue.async_add(now, {info.dns_pointer(): set()})
16481653

16491654
assert len(outgoing_queue.queue) == 3
16501655

@@ -1676,6 +1681,9 @@ def async_update_records(self, zc: 'Zeroconf', now: float, records: List[r.Recor
16761681

16771682
zc.add_listener(MyListener(), None) # type: ignore[arg-type]
16781683
await asyncio.sleep(0) # flush out any call soons
1679-
assert "listeners passed to async_add_listener must inherit from RecordUpdateListener" in caplog.text
1684+
assert (
1685+
"listeners passed to async_add_listener must inherit from RecordUpdateListener" in caplog.text
1686+
or "TypeError: Argument \'listener\' has incorrect type" in caplog.text
1687+
)
16801688

16811689
await aiozc.async_close()

0 commit comments

Comments
 (0)