1111import unittest
1212import unittest .mock
1313from typing import List , cast
14+ from unittest .mock import patch
1415
1516import pytest
1617
1718import zeroconf as r
1819from zeroconf import ServiceInfo , Zeroconf , const , current_time_millis
19- from zeroconf ._handlers import multicast_outgoing_queue
2020from 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