File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff line change 1010async 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
3235async 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 ()
You can’t perform that action at this time.
0 commit comments