1212from typing import Optional # noqa # used in type hints
1313
1414import zeroconf as r
15- from zeroconf import ServiceBrowser , ServiceInfo , Zeroconf , const
15+ from zeroconf import DNSOutgoing , ServiceBrowser , ServiceInfo , Zeroconf , const
1616
1717from . import _inject_response
1818
@@ -69,8 +69,7 @@ def test_same_name(self):
6969 generated .add_question (question )
7070 r .DNSIncoming (generated .packets ()[0 ])
7171
72- def test_lots_of_names (self ):
73-
72+ def test_verify_name_change_with_lots_of_names (self ):
7473 # instantiate a zeroconf instance
7574 zc = Zeroconf (interfaces = ['127.0.0.1' ])
7675
@@ -83,64 +82,21 @@ def test_lots_of_names(self):
8382 # verify that name changing works
8483 self .verify_name_change (zc , type_ , name , server_count )
8584
86- # we are going to patch the zeroconf send to check packet sizes
87- old_send = zc .async_send
88-
89- longest_packet_len = 0
90- longest_packet = None # type: Optional[r.DNSOutgoing]
91-
92- def send (out , addr = const ._MDNS_ADDR , port = const ._MDNS_PORT ):
93- """Sends an outgoing packet."""
94- for packet in out .packets ():
95- nonlocal longest_packet_len , longest_packet
96- if longest_packet_len < len (packet ):
97- longest_packet_len = len (packet )
98- longest_packet = out
99- old_send (out , addr = addr , port = port )
100-
101- # patch the zeroconf send
102- with unittest .mock .patch .object (zc , "async_send" , send ):
103-
104- # dummy service callback
105- def on_service_state_change (zeroconf , service_type , state_change , name ):
106- pass
107-
108- # start a browser
109- browser = ServiceBrowser (zc , type_ , [on_service_state_change ])
110-
111- # wait until the browse request packet has maxed out in size
112- sleep_count = 0
113- # we will never get to this large of a packet given the application-layer
114- # splitting of packets, but we still want to track the longest_packet_len
115- # for the debug message below
116- while sleep_count < 100 and longest_packet_len < const ._MAX_MSG_ABSOLUTE - 100 :
117- sleep_count += 1
118- time .sleep (0.1 )
119-
120- browser .cancel ()
121- time .sleep (0.5 )
122-
123- import zeroconf
124-
125- zeroconf .log .debug ('sleep_count %d, sized %d' , sleep_count , longest_packet_len )
126-
127- # now the browser has sent at least one request, verify the size
128- assert longest_packet_len <= const ._MAX_MSG_TYPICAL
129- assert longest_packet_len >= const ._MAX_MSG_TYPICAL - 100
85+ zc .close ()
13086
131- # mock zeroconf's logger warning() and debug()
132- from unittest . mock import patch
87+ def test_large_packet_exception_log_handling ( self ):
88+ """Verify we downgrade debug after warning."""
13389
134- patch_warn = patch ('zeroconf._logger.log.warning' )
135- patch_debug = patch ('zeroconf._logger.log.debug' )
136- mocked_log_warn = patch_warn .start ()
137- mocked_log_debug = patch_debug .start ()
90+ # instantiate a zeroconf instance
91+ zc = Zeroconf (interfaces = ['127.0.0.1' ])
13892
93+ with unittest .mock .patch ('zeroconf._logger.log.warning' ) as mocked_log_warn , unittest .mock .patch (
94+ 'zeroconf._logger.log.debug'
95+ ) as mocked_log_debug :
13996 # now that we have a long packet in our possession, let's verify the
14097 # exception handling.
141- out = longest_packet
142- assert out is not None
143- out .data .append (b'\0 ' * 1000 )
98+ out = r .DNSOutgoing (const ._FLAGS_QR_RESPONSE | const ._FLAGS_AA )
99+ out .data .append (b'\0 ' * 10000 )
144100
145101 # mock the zeroconf logger and check for the correct logging backoff
146102 call_counts = mocked_log_warn .call_count , mocked_log_debug .call_count
@@ -156,7 +112,7 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
156112 zc .send (out , const ._MDNS_ADDR , const ._MDNS_PORT )
157113 zc .send (out , const ._MDNS_ADDR , const ._MDNS_PORT )
158114 time .sleep (0.3 )
159- zeroconf .log .debug (
115+ r .log .debug (
160116 'warn %d debug %d was %s' ,
161117 mocked_log_warn .call_count ,
162118 mocked_log_debug .call_count ,
0 commit comments