Skip to content

Commit a3f4759

Browse files
author
Tony Crisci
committed
Add connected ivar to MessageBus
fixes #74
1 parent de8ed30 commit a3f4759

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

dbus_next/aio/message_bus.py

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ class MessageBus(BaseMessageBus):
110110
:ivar unique_name: The unique name of the message bus connection. It will
111111
be :class:`None` until the message bus connects.
112112
:vartype unique_name: str
113+
:ivar connected: True if this message bus is expected to be able to send
114+
and receive messages.
115+
:vartype connected: bool
113116
"""
114117
def __init__(self,
115118
bus_address: str = None,

dbus_next/glib/message_bus.py

+3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ class MessageBus(BaseMessageBus):
139139
:class:`AuthExternal <dbus_next.auth.AuthExternal>`.
140140
:type auth: :class:`Authenticator <dbus_next.auth.Authenticator>`
141141
142+
:ivar connected: True if this message bus is expected to be able to send
143+
and receive messages.
144+
:vartype connected: bool
142145
:ivar unique_name: The unique name of the message bus connection. It will
143146
be :class:`None` until the message bus connects.
144147
:vartype unique_name: str

dbus_next/message_bus.py

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class BaseMessageBus:
4444
:ivar unique_name: The unique name of the message bus connection. It will
4545
be :class:`None` until the message bus connects.
4646
:vartype unique_name: str
47+
:ivar connected: True if this message bus is expected to be able to send
48+
and receive messages.
49+
:vartype connected: bool
4750
"""
4851
def __init__(self,
4952
bus_address: Optional[str] = None,
@@ -82,6 +85,12 @@ def __init__(self,
8285

8386
self._setup_socket()
8487

88+
@property
89+
def connected(self):
90+
if self.unique_name is None or self._disconnected or self._user_disconnect:
91+
return False
92+
return True
93+
8594
def export(self, path: str, interface: ServiceInterface):
8695
"""Export the service interface on this message bus to make it available
8796
to other clients.

test/test_disconnect.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
async def test_bus_disconnect_before_reply(event_loop):
1111
'''In this test, the bus disconnects before the reply comes in. Make sure
1212
the caller receives a reply with the error instead of hanging.'''
13-
14-
bus = await MessageBus().connect()
13+
bus = MessageBus()
14+
assert not bus.connected
15+
await bus.connect()
16+
assert bus.connected
1517

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

2729
assert bus._disconnected
30+
assert not bus.connected
2831
assert (await bus.wait_for_disconnect()) is None
2932

3033

3134
@pytest.mark.asyncio
3235
async def test_unexpected_disconnect(event_loop):
33-
bus = await MessageBus().connect()
36+
bus = MessageBus()
37+
assert not bus.connected
38+
await bus.connect()
39+
assert bus.connected
3440

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

4652
assert bus._disconnected
53+
assert not bus.connected
4754

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

0 commit comments

Comments
 (0)