Skip to content

Commit

Permalink
Add is_closed() method to SSHClientConnection() and SSHServerConnecti…
Browse files Browse the repository at this point in the history
…on()

This commit adds a new is_closed() method to test whether an existing
SSHClientConnection or SSHServerConnection object's network connection
is closed or not.
  • Loading branch information
ronf committed Jan 11, 2024
1 parent 8b55fd9 commit 2d99f8e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions asyncssh/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,11 @@ def is_server(self) -> bool:

return self._server

def is_closed(self):
"""Return whether the connection is closed"""

return self._close_event.is_set()

def get_owner(self) -> Optional[Union[SSHClient, SSHServer]]:
"""Return the SSHClient or SSHServer which owns this connection"""

Expand Down
2 changes: 2 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ SSHClientConnection
.. automethod:: set_keepalive
.. automethod:: get_server_host_key
.. automethod:: send_debug
.. automethod:: is_closed
=================================== =

====================================================================================================================================================== =
Expand Down Expand Up @@ -407,6 +408,7 @@ SSHServerConnection
.. automethod:: set_extra_info
.. automethod:: set_keepalive
.. automethod:: send_debug
.. automethod:: is_closed
============================== =

============================================ =
Expand Down
4 changes: 3 additions & 1 deletion tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,11 @@ async def _check_version(self, *args, **kwargs):
async def test_connect(self):
"""Test connecting with async context manager"""

async with self.connect():
async with self.connect() as conn:
pass

self.assertTrue(conn.is_closed())

@asynctest
async def test_connect_sock(self):
"""Test connecting using an already-connected socket"""
Expand Down

0 comments on commit 2d99f8e

Please sign in to comment.