fix: disconnect race in tests (#79)

This commit is contained in:
J. Nick Koston 2022-10-06 23:42:32 -10:00 committed by GitHub
parent c015d8a219
commit f2bb10680a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,4 @@
import functools
import os
import asyncio
from unittest.mock import patch
import pytest
@ -35,16 +34,27 @@ async def test_bus_disconnect_before_reply(event_loop):
assert bus._disconnected
assert not bus.connected
assert (await bus.wait_for_disconnect()) is None
# Let the exception propagate to the event loop
# so the next test does not fail
await asyncio.sleep(0)
await asyncio.sleep(0)
@pytest.mark.asyncio
async def test_unexpected_disconnect(event_loop):
bus = MessageBus()
class FakeSocket:
def send(self, *args, **kwargs):
raise OSError
assert not bus.connected
await bus.connect()
assert bus.connected
with patch.object(bus._writer, "_write_without_remove_writer"):
with patch.object(bus._writer, "_write_without_remove_writer"), patch.object(
bus._writer, "sock", FakeSocket()
):
ping = bus.call(
Message(
destination="org.freedesktop.DBus",
@ -54,8 +64,6 @@ async def test_unexpected_disconnect(event_loop):
)
)
event_loop.call_soon(functools.partial(os.close, bus._fd))
with pytest.raises(OSError):
await ping
@ -64,3 +72,7 @@ async def test_unexpected_disconnect(event_loop):
with pytest.raises(OSError):
await bus.wait_for_disconnect()
bus.disconnect()
with pytest.raises(OSError):
await bus.wait_for_disconnect()