|
2 | 2 |
|
3 | 3 | """ Unit tests for zeroconf.py """ |
4 | 4 |
|
5 | | -import zeroconf as r |
| 5 | +import socket |
6 | 6 | import struct |
7 | 7 | import unittest |
| 8 | +from time import sleep |
8 | 9 |
|
9 | | -from zeroconf import byte_ord, xrange |
| 10 | +import zeroconf as r |
| 11 | +from zeroconf import byte_ord, ServiceBrowser, ServiceInfo, xrange, Zeroconf |
10 | 12 |
|
11 | 13 |
|
12 | 14 | class PacketGeneration(unittest.TestCase): |
@@ -121,3 +123,45 @@ class Framework(unittest.TestCase): |
121 | 123 | def testLaunchAndClose(self): |
122 | 124 | rv = r.Zeroconf() |
123 | 125 | rv.close() |
| 126 | + |
| 127 | + |
| 128 | +def test_integration(): |
| 129 | + |
| 130 | + services = set() |
| 131 | + |
| 132 | + class Listener(object): |
| 133 | + |
| 134 | + def removeService(self, zeroconf, type_, name): |
| 135 | + print('remove') |
| 136 | + services.remove((type_, name)) |
| 137 | + |
| 138 | + def addService(self, zeroconf, type_, name): |
| 139 | + print('add') |
| 140 | + services.add((type_, name)) |
| 141 | + |
| 142 | + type_ = "_http._tcp.local." |
| 143 | + |
| 144 | + zeroconf_browser = Zeroconf() |
| 145 | + listener = Listener() |
| 146 | + browser = ServiceBrowser(zeroconf_browser, type_, listener) |
| 147 | + |
| 148 | + zeroconf_registrar = Zeroconf() |
| 149 | + desc = {'path': '/~paulsm/'} |
| 150 | + name = "xxxyyy.%s" % type_ |
| 151 | + info = ServiceInfo( |
| 152 | + type_, name, |
| 153 | + socket.inet_aton("10.0.1.2"), 80, 0, 0, |
| 154 | + desc, "ash-2.local.") |
| 155 | + zeroconf_registrar.registerService(info) |
| 156 | + |
| 157 | + # TODO replace those blind sleeps with events |
| 158 | + sleep(2) |
| 159 | + try: |
| 160 | + assert (type_, name) in services, services |
| 161 | + zeroconf_registrar.unregisterService(info) |
| 162 | + sleep(2) |
| 163 | + assert (type_, name) not in services, services |
| 164 | + finally: |
| 165 | + zeroconf_registrar.close() |
| 166 | + browser.cancel() |
| 167 | + zeroconf_browser.close() |
0 commit comments