Skip to content

Commit

Permalink
Add connected ivar to MessageBus
Browse files Browse the repository at this point in the history
fixes #74
  • Loading branch information
Tony Crisci committed Dec 6, 2020
1 parent de8ed30 commit a3f4759
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions dbus_next/aio/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class MessageBus(BaseMessageBus):
:ivar unique_name: The unique name of the message bus connection. It will
be :class:`None` until the message bus connects.
:vartype unique_name: str
:ivar connected: True if this message bus is expected to be able to send
and receive messages.
:vartype connected: bool
"""
def __init__(self,
bus_address: str = None,
Expand Down
3 changes: 3 additions & 0 deletions dbus_next/glib/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ class MessageBus(BaseMessageBus):
:class:`AuthExternal <dbus_next.auth.AuthExternal>`.
:type auth: :class:`Authenticator <dbus_next.auth.Authenticator>`
:ivar connected: True if this message bus is expected to be able to send
and receive messages.
:vartype connected: bool
:ivar unique_name: The unique name of the message bus connection. It will
be :class:`None` until the message bus connects.
:vartype unique_name: str
Expand Down
9 changes: 9 additions & 0 deletions dbus_next/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class BaseMessageBus:
:ivar unique_name: The unique name of the message bus connection. It will
be :class:`None` until the message bus connects.
:vartype unique_name: str
:ivar connected: True if this message bus is expected to be able to send
and receive messages.
:vartype connected: bool
"""
def __init__(self,
bus_address: Optional[str] = None,
Expand Down Expand Up @@ -82,6 +85,12 @@ def __init__(self,

self._setup_socket()

@property
def connected(self):
if self.unique_name is None or self._disconnected or self._user_disconnect:
return False
return True

def export(self, path: str, interface: ServiceInterface):
"""Export the service interface on this message bus to make it available
to other clients.
Expand Down
13 changes: 10 additions & 3 deletions test/test_disconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
async def test_bus_disconnect_before_reply(event_loop):
'''In this test, the bus disconnects before the reply comes in. Make sure
the caller receives a reply with the error instead of hanging.'''

bus = await MessageBus().connect()
bus = MessageBus()
assert not bus.connected
await bus.connect()
assert bus.connected

ping = bus.call(
Message(destination='org.freedesktop.DBus',
Expand All @@ -25,12 +27,16 @@ async def test_bus_disconnect_before_reply(event_loop):
await ping

assert bus._disconnected
assert not bus.connected
assert (await bus.wait_for_disconnect()) is None


@pytest.mark.asyncio
async def test_unexpected_disconnect(event_loop):
bus = await MessageBus().connect()
bus = MessageBus()
assert not bus.connected
await bus.connect()
assert bus.connected

ping = bus.call(
Message(destination='org.freedesktop.DBus',
Expand All @@ -44,6 +50,7 @@ async def test_unexpected_disconnect(event_loop):
await ping

assert bus._disconnected
assert not bus.connected

with pytest.raises(OSError):
await bus.wait_for_disconnect()

0 comments on commit a3f4759

Please sign in to comment.