Skip to content

test_update_record is flakey #469

@bdraco

Description

@bdraco
self = <zeroconf.test.TestServiceBrowserMultipleTypes testMethod=test_update_record>

    def test_update_record(self):
    
        service_names = ['name2._type2._tcp.local.', 'name._type._tcp.local.', 'name._type._udp.local']
        service_types = ['_type2._tcp.local.', '_type._tcp.local.', '_type._udp.local.']
    
        service_added_count = 0
        service_removed_count = 0
        service_add_event = Event()
        service_removed_event = Event()
    
        class MyServiceListener(r.ServiceListener):
            def add_service(self, zc, type_, name) -> None:
                nonlocal service_added_count
                service_added_count += 1
                if service_added_count == 3:
                    service_add_event.set()
    
            def remove_service(self, zc, type_, name) -> None:
                nonlocal service_removed_count
                service_removed_count += 1
                if service_removed_count == 3:
                    service_removed_event.set()
    
        def mock_incoming_msg(
            service_state_change: r.ServiceStateChange, service_type: str, service_name: str, ttl: int
        ) -> r.DNSIncoming:
            generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE)
            generated.add_answer_at_time(
                r.DNSPointer(service_type, r._TYPE_PTR, r._CLASS_IN, ttl, service_name), 0
            )
            return r.DNSIncoming(generated.packet())
    
        zeroconf = r.Zeroconf(interfaces=['127.0.0.1'])
        service_browser = r.ServiceBrowser(zeroconf, service_types, listener=MyServiceListener())
    
        try:
            wait_time = 3
    
            # all three services added
            zeroconf.handle_response(
                mock_incoming_msg(r.ServiceStateChange.Added, service_types[0], service_names[0], 120)
            )
            zeroconf.handle_response(
                mock_incoming_msg(r.ServiceStateChange.Added, service_types[1], service_names[1], 120)
            )
            zeroconf.handle_response(
                mock_incoming_msg(r.ServiceStateChange.Added, service_types[2], service_names[2], 120)
            )
    
            called_with_refresh_time_check = False
    
            def _mock_get_expiration_time(self, percent):
                nonlocal called_with_refresh_time_check
                if percent == _EXPIRE_REFRESH_TIME_PERCENT:
                    called_with_refresh_time_check = True
                    return 0
                return self.created + (percent * self.ttl * 10)
    
            # Set an expire time that will force a refresh
            with unittest.mock.patch("zeroconf.DNSRecord.get_expiration_time", new=_mock_get_expiration_time):
                zeroconf.handle_response(
                    mock_incoming_msg(r.ServiceStateChange.Added, service_types[2], service_names[2], 120)
                )
                service_add_event.wait(wait_time)
>           assert called_with_refresh_time_check is True
E           AssertionError: assert False is True

zeroconf\test.py:2042: AssertionError

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions