@@ -644,6 +644,8 @@ def remove_service(self, zeroconf, type, name):
644
644
def test_integration ():
645
645
service_added = Event ()
646
646
service_removed = Event ()
647
+ unexpected_ttl = Event ()
648
+ got_query = Event ()
647
649
648
650
type_ = "_http._tcp.local."
649
651
registration_name = "xxxyyy.%s" % type_
@@ -656,6 +658,42 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
656
658
service_removed .set ()
657
659
658
660
zeroconf_browser = Zeroconf (interfaces = ['127.0.0.1' ])
661
+
662
+ # we are going to monkey patch the zeroconf send to check packet sizes
663
+ old_send = zeroconf_browser .send
664
+
665
+ time_offset = 0
666
+
667
+ def current_time_millis ():
668
+ """Current system time in milliseconds"""
669
+ return time .time () * 1000 + time_offset * 1000
670
+
671
+ expected_ttl = r ._DNS_TTL
672
+
673
+ # needs to be a list so that we can modify it in our phony send
674
+ nbr_queries = [0 , None ]
675
+
676
+ def send (out , addr = r ._MDNS_ADDR , port = r ._MDNS_PORT ):
677
+ """Sends an outgoing packet."""
678
+ pout = r .DNSIncoming (out .packet ())
679
+
680
+ for answer in pout .answers :
681
+ nbr_queries [0 ] += 1
682
+ if not answer .ttl > expected_ttl / 2 :
683
+ unexpected_ttl .set ()
684
+
685
+ got_query .set ()
686
+ old_send (out , addr = addr , port = port )
687
+
688
+ # monkey patch the zeroconf send
689
+ zeroconf_browser .send = send
690
+
691
+ # monkey patch the zeroconf current_time_millis
692
+ r .current_time_millis = current_time_millis
693
+
694
+ service_added = Event ()
695
+ service_removed = Event ()
696
+
659
697
browser = ServiceBrowser (zeroconf_browser , type_ , [on_service_state_change ])
660
698
661
699
zeroconf_registrar = Zeroconf (interfaces = ['127.0.0.1' ])
@@ -669,6 +707,16 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
669
707
try :
670
708
service_added .wait (1 )
671
709
assert service_added .is_set ()
710
+
711
+ sleep_count = 0
712
+ while nbr_queries [0 ] < 50 :
713
+ time_offset += expected_ttl / 4
714
+ zeroconf_browser .notify_all ()
715
+ sleep_count += 1
716
+ got_query .wait (1 )
717
+ got_query .clear ()
718
+ assert not unexpected_ttl .is_set ()
719
+
672
720
# Don't remove service, allow close() to cleanup
673
721
674
722
finally :
0 commit comments