Skip to content

Commit 8f00cfc

Browse files
authored
Replace select loop with asyncio loop (#504)
1 parent 9a32db8 commit 8f00cfc

6 files changed

Lines changed: 154 additions & 213 deletions

File tree

tests/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
USA
2121
"""
2222

23+
import asyncio
2324
import socket
2425
from functools import lru_cache
2526

@@ -32,7 +33,12 @@
3233

3334
def _inject_response(zc: Zeroconf, msg: DNSIncoming) -> None:
3435
"""Inject a DNSIncoming response."""
35-
zc.handle_response(msg)
36+
assert zc.loop is not None
37+
38+
async def _wait_for_response():
39+
zc.handle_response(msg)
40+
41+
asyncio.run_coroutine_threadsafe(_wait_for_response(), zc.loop).result()
3642

3743

3844
@lru_cache(maxsize=None)

tests/test_asyncio.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

tests/test_core.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ def test_reaper(self):
4646
zeroconf.cache.add(record_with_1s_ttl)
4747
entries_with_cache = list(itertools.chain(*[cache.entries_with_name(name) for name in cache.names()]))
4848
time.sleep(1)
49-
with zeroconf.engine.condition:
50-
zeroconf.engine._notify()
49+
zeroconf.notify_all()
5150
time.sleep(0.1)
5251
entries = list(itertools.chain(*[cache.entries_with_name(name) for name in cache.names()]))
5352
zeroconf.close()

tests/test_init.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,11 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
148148
zc.send(out)
149149
assert mocked_log_warn.call_count == call_counts[0]
150150

151-
# force a receive of a packet
152-
packet = out.packets()[0]
153-
s = zc._respond_sockets[0]
154-
155151
# mock the zeroconf logger and check for the correct logging backoff
156152
call_counts = mocked_log_warn.call_count, mocked_log_debug.call_count
157153
# force receive on oversized packet
158-
s.sendto(packet, 0, (const._MDNS_ADDR, const._MDNS_PORT))
159-
s.sendto(packet, 0, (const._MDNS_ADDR, const._MDNS_PORT))
154+
zc.send(out, const._MDNS_ADDR, const._MDNS_PORT)
155+
zc.send(out, const._MDNS_ADDR, const._MDNS_PORT)
160156
time.sleep(2.0)
161157
zeroconf.log.debug(
162158
'warn %d debug %d was %s', mocked_log_warn.call_count, mocked_log_debug.call_count, call_counts
@@ -166,28 +162,6 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
166162
# close our zeroconf which will close the sockets
167163
zc.close()
168164

169-
# pop the big chunk off the end of the data and send on a closed socket
170-
out.data.pop()
171-
zc._GLOBAL_DONE = False
172-
173-
# mock the zeroconf logger and check for the correct logging backoff
174-
call_counts = mocked_log_warn.call_count, mocked_log_debug.call_count
175-
# send on a closed socket (force a socket error)
176-
zc.send(out)
177-
zeroconf.log.debug(
178-
'warn %d debug %d was %s', mocked_log_warn.call_count, mocked_log_debug.call_count, call_counts
179-
)
180-
assert mocked_log_warn.call_count > call_counts[0]
181-
assert mocked_log_debug.call_count > call_counts[0]
182-
zc.send(out)
183-
zeroconf.log.debug(
184-
'warn %d debug %d was %s', mocked_log_warn.call_count, mocked_log_debug.call_count, call_counts
185-
)
186-
assert mocked_log_debug.call_count > call_counts[0] + 2
187-
188-
mocked_log_warn.stop()
189-
mocked_log_debug.stop()
190-
191165
def verify_name_change(self, zc, type_, name, number_hosts):
192166
desc = {'path': '/~paulsm/'}
193167
info_service = ServiceInfo(

0 commit comments

Comments
 (0)