From a3f4759150d4f3fdf9531357c881782da90b70fd Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sun, 6 Dec 2020 13:58:52 -0500 Subject: [PATCH] Add connected ivar to MessageBus fixes #74 --- dbus_next/aio/message_bus.py | 3 +++ dbus_next/glib/message_bus.py | 3 +++ dbus_next/message_bus.py | 9 +++++++++ test/test_disconnect.py | 13 ++++++++++--- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/dbus_next/aio/message_bus.py b/dbus_next/aio/message_bus.py index 3e75639..5b017bd 100644 --- a/dbus_next/aio/message_bus.py +++ b/dbus_next/aio/message_bus.py @@ -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, diff --git a/dbus_next/glib/message_bus.py b/dbus_next/glib/message_bus.py index 0936109..668e9ec 100644 --- a/dbus_next/glib/message_bus.py +++ b/dbus_next/glib/message_bus.py @@ -139,6 +139,9 @@ class MessageBus(BaseMessageBus): :class:`AuthExternal `. :type auth: :class:`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 diff --git a/dbus_next/message_bus.py b/dbus_next/message_bus.py index d847371..5d2a623 100644 --- a/dbus_next/message_bus.py +++ b/dbus_next/message_bus.py @@ -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, @@ -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. diff --git a/test/test_disconnect.py b/test/test_disconnect.py index d5318d9..d04d996 100644 --- a/test/test_disconnect.py +++ b/test/test_disconnect.py @@ -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', @@ -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', @@ -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()