diff --git a/tests/test_core.py b/tests/test_core.py index a99519cb..6d7467ab 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -2,11 +2,12 @@ # -*- coding: utf-8 -*- -""" Unit tests for zeroconf.core """ +""" Unit tests for zeroconf._core """ import itertools import logging import os +import pytest import socket import time import unittest @@ -14,8 +15,7 @@ from typing import cast import zeroconf as r -from zeroconf import _core -from zeroconf import const +from zeroconf import _core, const, ServiceBrowser, Zeroconf from . import has_working_ipv6, _inject_response @@ -234,3 +234,41 @@ def mock_split_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNS finally: zeroconf.close() + + +def test_notify_listeners(): + """Test adding and removing notify listeners.""" + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=['127.0.0.1']) + notify_called = 0 + + class TestNotifyListener(r.NotifyListener): + def notify_all(self): + nonlocal notify_called + notify_called += 1 + + with pytest.raises(NotImplementedError): + r.NotifyListener().notify_all() + + notify_listener = TestNotifyListener() + + zc.add_notify_listener(notify_listener) + + def on_service_state_change(zeroconf, service_type, state_change, name): + """Dummy service callback.""" + + # start a browser + browser = ServiceBrowser(zc, "_http._tcp.local.", [on_service_state_change]) + browser.cancel() + + assert notify_called + zc.remove_notify_listener(notify_listener) + + notify_called = 0 + # start a browser + browser = ServiceBrowser(zc, "_http._tcp.local.", [on_service_state_change]) + browser.cancel() + + assert not notify_called + + zc.close() diff --git a/tests/test_init.py b/tests/test_init.py index 48fff674..4710a994 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -11,8 +11,6 @@ import unittest.mock from typing import Optional # noqa # used in type hints -import pytest - import zeroconf as r from zeroconf import ServiceBrowser, ServiceInfo, Zeroconf, const @@ -246,41 +244,3 @@ def generate_host(zc, host_name, type_): 0, ) zc.send(out) - - -def test_notify_listeners(): - """Test adding and removing notify listeners.""" - # instantiate a zeroconf instance - zc = Zeroconf(interfaces=['127.0.0.1']) - notify_called = 0 - - class TestNotifyListener(r.NotifyListener): - def notify_all(self): - nonlocal notify_called - notify_called += 1 - - with pytest.raises(NotImplementedError): - r.NotifyListener().notify_all() - - notify_listener = TestNotifyListener() - - zc.add_notify_listener(notify_listener) - - def on_service_state_change(zeroconf, service_type, state_change, name): - """Dummy service callback.""" - - # start a browser - browser = ServiceBrowser(zc, "_http._tcp.local.", [on_service_state_change]) - browser.cancel() - - assert notify_called - zc.remove_notify_listener(notify_listener) - - notify_called = 0 - # start a browser - browser = ServiceBrowser(zc, "_http._tcp.local.", [on_service_state_change]) - browser.cancel() - - assert not notify_called - - zc.close()