fix: disconnect race in tests (#79)
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import functools
|
import asyncio
|
||||||
import os
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -35,16 +34,27 @@ async def test_bus_disconnect_before_reply(event_loop):
|
|||||||
assert bus._disconnected
|
assert bus._disconnected
|
||||||
assert not bus.connected
|
assert not bus.connected
|
||||||
assert (await bus.wait_for_disconnect()) is None
|
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
|
@pytest.mark.asyncio
|
||||||
async def test_unexpected_disconnect(event_loop):
|
async def test_unexpected_disconnect(event_loop):
|
||||||
bus = MessageBus()
|
bus = MessageBus()
|
||||||
|
|
||||||
|
class FakeSocket:
|
||||||
|
def send(self, *args, **kwargs):
|
||||||
|
raise OSError
|
||||||
|
|
||||||
assert not bus.connected
|
assert not bus.connected
|
||||||
await bus.connect()
|
await bus.connect()
|
||||||
assert bus.connected
|
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(
|
ping = bus.call(
|
||||||
Message(
|
Message(
|
||||||
destination="org.freedesktop.DBus",
|
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):
|
with pytest.raises(OSError):
|
||||||
await ping
|
await ping
|
||||||
|
|
||||||
@@ -64,3 +72,7 @@ async def test_unexpected_disconnect(event_loop):
|
|||||||
|
|
||||||
with pytest.raises(OSError):
|
with pytest.raises(OSError):
|
||||||
await bus.wait_for_disconnect()
|
await bus.wait_for_disconnect()
|
||||||
|
|
||||||
|
bus.disconnect()
|
||||||
|
with pytest.raises(OSError):
|
||||||
|
await bus.wait_for_disconnect()
|
||||||
|
|||||||
Reference in New Issue
Block a user