Skip to content

Commit 09361f6

Browse files
committed
missed one
1 parent d49a267 commit 09361f6

2 files changed

Lines changed: 110 additions & 115 deletions

File tree

tests/services/test_browser.py

Lines changed: 109 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,22 @@
66

77
import logging
88
import socket
9-
import threading
109
import time
1110
import os
1211
import unittest
1312
from threading import Event
14-
from typing import List
1513

1614
import pytest
1715

1816
import zeroconf as r
19-
from zeroconf import DNSAddress, DNSPointer, DNSQuestion, const, current_time_millis
17+
from zeroconf import DNSPointer, DNSQuestion, const, current_time_millis
2018
import zeroconf._services.browser as _services_browser
2119
from zeroconf import Zeroconf
2220
from zeroconf._services import ServiceStateChange
2321
from zeroconf._services.browser import ServiceBrowser
2422
from zeroconf._services.info import ServiceInfo
25-
from zeroconf.aio import AsyncZeroconf
2623

27-
from . import has_working_ipv6, _clear_cache, _inject_response
24+
from .. import has_working_ipv6, _inject_response
2825

2926

3027
log = logging.getLogger('zeroconf')
@@ -237,6 +234,113 @@ def mock_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNSIncomi
237234
zeroconf.close()
238235

239236

237+
class TestServiceBrowserMultipleTypes(unittest.TestCase):
238+
def test_update_record(self):
239+
240+
service_names = ['name2._type2._tcp.local.', 'name._type._tcp.local.', 'name._type._udp.local']
241+
service_types = ['_type2._tcp.local.', '_type._tcp.local.', '_type._udp.local.']
242+
243+
service_added_count = 0
244+
service_removed_count = 0
245+
service_add_event = Event()
246+
service_removed_event = Event()
247+
248+
class MyServiceListener(r.ServiceListener):
249+
def add_service(self, zc, type_, name) -> None:
250+
nonlocal service_added_count
251+
service_added_count += 1
252+
if service_added_count == 3:
253+
service_add_event.set()
254+
255+
def remove_service(self, zc, type_, name) -> None:
256+
nonlocal service_removed_count
257+
service_removed_count += 1
258+
if service_removed_count == 3:
259+
service_removed_event.set()
260+
261+
def mock_incoming_msg(
262+
service_state_change: r.ServiceStateChange, service_type: str, service_name: str, ttl: int
263+
) -> r.DNSIncoming:
264+
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
265+
generated.add_answer_at_time(
266+
r.DNSPointer(service_type, const._TYPE_PTR, const._CLASS_IN, ttl, service_name), 0
267+
)
268+
return r.DNSIncoming(generated.packets()[0])
269+
270+
zeroconf = r.Zeroconf(interfaces=['127.0.0.1'])
271+
service_browser = r.ServiceBrowser(zeroconf, service_types, listener=MyServiceListener())
272+
273+
try:
274+
wait_time = 3
275+
276+
# all three services added
277+
_inject_response(
278+
zeroconf,
279+
mock_incoming_msg(r.ServiceStateChange.Added, service_types[0], service_names[0], 120),
280+
)
281+
_inject_response(
282+
zeroconf,
283+
mock_incoming_msg(r.ServiceStateChange.Added, service_types[1], service_names[1], 120),
284+
)
285+
zeroconf.wait(100)
286+
287+
called_with_refresh_time_check = False
288+
289+
def _mock_get_expiration_time(self, percent):
290+
nonlocal called_with_refresh_time_check
291+
if percent == const._EXPIRE_REFRESH_TIME_PERCENT:
292+
called_with_refresh_time_check = True
293+
return 0
294+
return self.created + (percent * self.ttl * 10)
295+
296+
# Set an expire time that will force a refresh
297+
with unittest.mock.patch("zeroconf.DNSRecord.get_expiration_time", new=_mock_get_expiration_time):
298+
_inject_response(
299+
zeroconf,
300+
mock_incoming_msg(r.ServiceStateChange.Added, service_types[0], service_names[0], 120),
301+
)
302+
# Add the last record after updating the first one
303+
# to ensure the service_add_event only gets set
304+
# after the update
305+
_inject_response(
306+
zeroconf,
307+
mock_incoming_msg(r.ServiceStateChange.Added, service_types[2], service_names[2], 120),
308+
)
309+
service_add_event.wait(wait_time)
310+
assert called_with_refresh_time_check is True
311+
assert service_added_count == 3
312+
assert service_removed_count == 0
313+
314+
_inject_response(
315+
zeroconf,
316+
mock_incoming_msg(r.ServiceStateChange.Updated, service_types[0], service_names[0], 0),
317+
)
318+
319+
# all three services removed
320+
_inject_response(
321+
zeroconf,
322+
mock_incoming_msg(r.ServiceStateChange.Removed, service_types[0], service_names[0], 0),
323+
)
324+
_inject_response(
325+
zeroconf,
326+
mock_incoming_msg(r.ServiceStateChange.Removed, service_types[1], service_names[1], 0),
327+
)
328+
_inject_response(
329+
zeroconf,
330+
mock_incoming_msg(r.ServiceStateChange.Removed, service_types[2], service_names[2], 0),
331+
)
332+
service_removed_event.wait(wait_time)
333+
assert service_added_count == 3
334+
assert service_removed_count == 3
335+
336+
finally:
337+
assert len(zeroconf.listeners) == 1
338+
service_browser.cancel()
339+
assert len(zeroconf.listeners) == 0
340+
zeroconf.remove_all_service_listeners()
341+
zeroconf.close()
342+
343+
240344
def test_backoff():
241345
got_query = Event()
242346

tests/test_services.py

Lines changed: 1 addition & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
import pytest
1717

1818
import zeroconf as r
19-
from zeroconf import DNSAddress, DNSPointer, DNSQuestion, const, current_time_millis
20-
import zeroconf._services.browser as _services_browser
19+
from zeroconf import DNSAddress, const
2120
from zeroconf import Zeroconf
22-
from zeroconf._services import ServiceStateChange
2321
from zeroconf._services.browser import ServiceBrowser
2422
from zeroconf._services.info import ServiceInfo
2523
from zeroconf.aio import AsyncZeroconf
@@ -436,113 +434,6 @@ def get_service_info_helper(zc, type, name):
436434
zc.close()
437435

438436

439-
class TestServiceBrowserMultipleTypes(unittest.TestCase):
440-
def test_update_record(self):
441-
442-
service_names = ['name2._type2._tcp.local.', 'name._type._tcp.local.', 'name._type._udp.local']
443-
service_types = ['_type2._tcp.local.', '_type._tcp.local.', '_type._udp.local.']
444-
445-
service_added_count = 0
446-
service_removed_count = 0
447-
service_add_event = Event()
448-
service_removed_event = Event()
449-
450-
class MyServiceListener(r.ServiceListener):
451-
def add_service(self, zc, type_, name) -> None:
452-
nonlocal service_added_count
453-
service_added_count += 1
454-
if service_added_count == 3:
455-
service_add_event.set()
456-
457-
def remove_service(self, zc, type_, name) -> None:
458-
nonlocal service_removed_count
459-
service_removed_count += 1
460-
if service_removed_count == 3:
461-
service_removed_event.set()
462-
463-
def mock_incoming_msg(
464-
service_state_change: r.ServiceStateChange, service_type: str, service_name: str, ttl: int
465-
) -> r.DNSIncoming:
466-
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
467-
generated.add_answer_at_time(
468-
r.DNSPointer(service_type, const._TYPE_PTR, const._CLASS_IN, ttl, service_name), 0
469-
)
470-
return r.DNSIncoming(generated.packets()[0])
471-
472-
zeroconf = r.Zeroconf(interfaces=['127.0.0.1'])
473-
service_browser = r.ServiceBrowser(zeroconf, service_types, listener=MyServiceListener())
474-
475-
try:
476-
wait_time = 3
477-
478-
# all three services added
479-
_inject_response(
480-
zeroconf,
481-
mock_incoming_msg(r.ServiceStateChange.Added, service_types[0], service_names[0], 120),
482-
)
483-
_inject_response(
484-
zeroconf,
485-
mock_incoming_msg(r.ServiceStateChange.Added, service_types[1], service_names[1], 120),
486-
)
487-
zeroconf.wait(100)
488-
489-
called_with_refresh_time_check = False
490-
491-
def _mock_get_expiration_time(self, percent):
492-
nonlocal called_with_refresh_time_check
493-
if percent == const._EXPIRE_REFRESH_TIME_PERCENT:
494-
called_with_refresh_time_check = True
495-
return 0
496-
return self.created + (percent * self.ttl * 10)
497-
498-
# Set an expire time that will force a refresh
499-
with unittest.mock.patch("zeroconf.DNSRecord.get_expiration_time", new=_mock_get_expiration_time):
500-
_inject_response(
501-
zeroconf,
502-
mock_incoming_msg(r.ServiceStateChange.Added, service_types[0], service_names[0], 120),
503-
)
504-
# Add the last record after updating the first one
505-
# to ensure the service_add_event only gets set
506-
# after the update
507-
_inject_response(
508-
zeroconf,
509-
mock_incoming_msg(r.ServiceStateChange.Added, service_types[2], service_names[2], 120),
510-
)
511-
service_add_event.wait(wait_time)
512-
assert called_with_refresh_time_check is True
513-
assert service_added_count == 3
514-
assert service_removed_count == 0
515-
516-
_inject_response(
517-
zeroconf,
518-
mock_incoming_msg(r.ServiceStateChange.Updated, service_types[0], service_names[0], 0),
519-
)
520-
521-
# all three services removed
522-
_inject_response(
523-
zeroconf,
524-
mock_incoming_msg(r.ServiceStateChange.Removed, service_types[0], service_names[0], 0),
525-
)
526-
_inject_response(
527-
zeroconf,
528-
mock_incoming_msg(r.ServiceStateChange.Removed, service_types[1], service_names[1], 0),
529-
)
530-
_inject_response(
531-
zeroconf,
532-
mock_incoming_msg(r.ServiceStateChange.Removed, service_types[2], service_names[2], 0),
533-
)
534-
service_removed_event.wait(wait_time)
535-
assert service_added_count == 3
536-
assert service_removed_count == 3
537-
538-
finally:
539-
assert len(zeroconf.listeners) == 1
540-
service_browser.cancel()
541-
assert len(zeroconf.listeners) == 0
542-
zeroconf.remove_all_service_listeners()
543-
zeroconf.close()
544-
545-
546437
class ListenerTest(unittest.TestCase):
547438
def test_integration_with_listener_class(self):
548439

0 commit comments

Comments
 (0)