|
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | 4 |
|
5 | | -""" Unit tests for zeroconf.core """ |
| 5 | +""" Unit tests for zeroconf._core """ |
6 | 6 |
|
7 | 7 | import itertools |
8 | 8 | import logging |
9 | 9 | import os |
| 10 | +import pytest |
10 | 11 | import socket |
11 | 12 | import time |
12 | 13 | import unittest |
13 | 14 | import unittest.mock |
14 | 15 | from typing import cast |
15 | 16 |
|
16 | 17 | import zeroconf as r |
17 | | -from zeroconf import _core |
18 | | -from zeroconf import const |
| 18 | +from zeroconf import _core, const, ServiceBrowser, Zeroconf |
19 | 19 |
|
20 | 20 | from . import has_working_ipv6, _inject_response |
21 | 21 |
|
@@ -234,3 +234,41 @@ def mock_split_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNS |
234 | 234 |
|
235 | 235 | finally: |
236 | 236 | zeroconf.close() |
| 237 | + |
| 238 | + |
| 239 | +def test_notify_listeners(): |
| 240 | + """Test adding and removing notify listeners.""" |
| 241 | + # instantiate a zeroconf instance |
| 242 | + zc = Zeroconf(interfaces=['127.0.0.1']) |
| 243 | + notify_called = 0 |
| 244 | + |
| 245 | + class TestNotifyListener(r.NotifyListener): |
| 246 | + def notify_all(self): |
| 247 | + nonlocal notify_called |
| 248 | + notify_called += 1 |
| 249 | + |
| 250 | + with pytest.raises(NotImplementedError): |
| 251 | + r.NotifyListener().notify_all() |
| 252 | + |
| 253 | + notify_listener = TestNotifyListener() |
| 254 | + |
| 255 | + zc.add_notify_listener(notify_listener) |
| 256 | + |
| 257 | + def on_service_state_change(zeroconf, service_type, state_change, name): |
| 258 | + """Dummy service callback.""" |
| 259 | + |
| 260 | + # start a browser |
| 261 | + browser = ServiceBrowser(zc, "_http._tcp.local.", [on_service_state_change]) |
| 262 | + browser.cancel() |
| 263 | + |
| 264 | + assert notify_called |
| 265 | + zc.remove_notify_listener(notify_listener) |
| 266 | + |
| 267 | + notify_called = 0 |
| 268 | + # start a browser |
| 269 | + browser = ServiceBrowser(zc, "_http._tcp.local.", [on_service_state_change]) |
| 270 | + browser.cancel() |
| 271 | + |
| 272 | + assert not notify_called |
| 273 | + |
| 274 | + zc.close() |
0 commit comments