Skip to content

Commit

Permalink
fix: disconnect race in tests (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 7, 2022
1 parent c015d8a commit f2bb106
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions tests/test_disconnect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import functools
import os
import asyncio
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -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",
Expand All @@ -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

Expand All @@ -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()

0 comments on commit f2bb106

Please sign in to comment.