Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
# -*- 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
import unittest.mock
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

Expand Down Expand Up @@ -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()
40 changes: 0 additions & 40 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()