From f2bb10680a5d4e363ff8e7762fef25ec75ef8b14 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 6 Oct 2022 23:42:32 -1000 Subject: [PATCH] fix: disconnect race in tests (#79) --- tests/test_disconnect.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tests/test_disconnect.py b/tests/test_disconnect.py index 5f32f6b..b8fa367 100644 --- a/tests/test_disconnect.py +++ b/tests/test_disconnect.py @@ -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()