@@ -81,7 +81,7 @@ def test_lots_of_names(self):
8181 # verify that name changing works
8282 self .verify_name_change (zc , type_ , name , server_count )
8383
84- # we are going to monkey patch the zeroconf send to check packet sizes
84+ # we are going to patch the zeroconf send to check packet sizes
8585 old_send = zc .send
8686
8787 longest_packet_len = 0
@@ -96,71 +96,74 @@ def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT):
9696 longest_packet = out
9797 old_send (out , addr = addr , port = port )
9898
99- # monkey patch the zeroconf send
100- setattr (zc , "send" , send )
101-
102- # dummy service callback
103- def on_service_state_change (zeroconf , service_type , state_change , name ):
104- pass
105-
106- # start a browser
107- browser = ServiceBrowser (zc , type_ , [on_service_state_change ])
108-
109- # wait until the browse request packet has maxed out in size
110- sleep_count = 0
111- # we will never get to this large of a packet given the application-layer
112- # splitting of packets, but we still want to track the longest_packet_len
113- # for the debug message below
114- while sleep_count < 100 and longest_packet_len < const ._MAX_MSG_ABSOLUTE - 100 :
115- sleep_count += 1
116- time .sleep (0.1 )
117-
118- browser .cancel ()
119- time .sleep (0.5 )
120-
121- import zeroconf
122-
123- zeroconf .log .debug ('sleep_count %d, sized %d' , sleep_count , longest_packet_len )
124-
125- # now the browser has sent at least one request, verify the size
126- assert longest_packet_len <= const ._MAX_MSG_TYPICAL
127- assert longest_packet_len >= const ._MAX_MSG_TYPICAL - 100
128-
129- # mock zeroconf's logger warning() and debug()
130- from unittest .mock import patch
131-
132- patch_warn = patch ('zeroconf._logger.log.warning' )
133- patch_debug = patch ('zeroconf._logger.log.debug' )
134- mocked_log_warn = patch_warn .start ()
135- mocked_log_debug = patch_debug .start ()
136-
137- # now that we have a long packet in our possession, let's verify the
138- # exception handling.
139- out = longest_packet
140- assert out is not None
141- out .data .append (b'\0 ' * 1000 )
142-
143- # mock the zeroconf logger and check for the correct logging backoff
144- call_counts = mocked_log_warn .call_count , mocked_log_debug .call_count
145- # try to send an oversized packet
146- zc .send (out )
147- assert mocked_log_warn .call_count == call_counts [0 ]
148- zc .send (out )
149- assert mocked_log_warn .call_count == call_counts [0 ]
150-
151- # mock the zeroconf logger and check for the correct logging backoff
152- call_counts = mocked_log_warn .call_count , mocked_log_debug .call_count
153- # force receive on oversized packet
154- zc .send (out , const ._MDNS_ADDR , const ._MDNS_PORT )
155- zc .send (out , const ._MDNS_ADDR , const ._MDNS_PORT )
156- time .sleep (2.0 )
157- zeroconf .log .debug (
158- 'warn %d debug %d was %s' , mocked_log_warn .call_count , mocked_log_debug .call_count , call_counts
159- )
160- assert mocked_log_debug .call_count > call_counts [0 ]
161-
162- # close our zeroconf which will close the sockets
163- zc .close ()
99+ # patch the zeroconf send
100+ with unittest .mock .patch .object (zc , "send" , send ):
101+
102+ # dummy service callback
103+ def on_service_state_change (zeroconf , service_type , state_change , name ):
104+ pass
105+
106+ # start a browser
107+ browser = ServiceBrowser (zc , type_ , [on_service_state_change ])
108+
109+ # wait until the browse request packet has maxed out in size
110+ sleep_count = 0
111+ # we will never get to this large of a packet given the application-layer
112+ # splitting of packets, but we still want to track the longest_packet_len
113+ # for the debug message below
114+ while sleep_count < 100 and longest_packet_len < const ._MAX_MSG_ABSOLUTE - 100 :
115+ sleep_count += 1
116+ time .sleep (0.1 )
117+
118+ browser .cancel ()
119+ time .sleep (0.5 )
120+
121+ import zeroconf
122+
123+ zeroconf .log .debug ('sleep_count %d, sized %d' , sleep_count , longest_packet_len )
124+
125+ # now the browser has sent at least one request, verify the size
126+ assert longest_packet_len <= const ._MAX_MSG_TYPICAL
127+ assert longest_packet_len >= const ._MAX_MSG_TYPICAL - 100
128+
129+ # mock zeroconf's logger warning() and debug()
130+ from unittest .mock import patch
131+
132+ patch_warn = patch ('zeroconf._logger.log.warning' )
133+ patch_debug = patch ('zeroconf._logger.log.debug' )
134+ mocked_log_warn = patch_warn .start ()
135+ mocked_log_debug = patch_debug .start ()
136+
137+ # now that we have a long packet in our possession, let's verify the
138+ # exception handling.
139+ out = longest_packet
140+ assert out is not None
141+ out .data .append (b'\0 ' * 1000 )
142+
143+ # mock the zeroconf logger and check for the correct logging backoff
144+ call_counts = mocked_log_warn .call_count , mocked_log_debug .call_count
145+ # try to send an oversized packet
146+ zc .send (out )
147+ assert mocked_log_warn .call_count == call_counts [0 ]
148+ zc .send (out )
149+ assert mocked_log_warn .call_count == call_counts [0 ]
150+
151+ # mock the zeroconf logger and check for the correct logging backoff
152+ call_counts = mocked_log_warn .call_count , mocked_log_debug .call_count
153+ # force receive on oversized packet
154+ zc .send (out , const ._MDNS_ADDR , const ._MDNS_PORT )
155+ zc .send (out , const ._MDNS_ADDR , const ._MDNS_PORT )
156+ time .sleep (2.0 )
157+ zeroconf .log .debug (
158+ 'warn %d debug %d was %s' ,
159+ mocked_log_warn .call_count ,
160+ mocked_log_debug .call_count ,
161+ call_counts ,
162+ )
163+ assert mocked_log_debug .call_count > call_counts [0 ]
164+
165+ # close our zeroconf which will close the sockets
166+ zc .close ()
164167
165168 def verify_name_change (self , zc , type_ , name , number_hosts ):
166169 desc = {'path' : '/~paulsm/' }
0 commit comments