From 45d880cf905f05e1c9966fbe46ed53236bda2851 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Tue, 1 Jan 2019 07:09:59 +0000 Subject: [PATCH] correct handling of disconnects fixes #227 --- socketio/asyncio_client.py | 1 + socketio/client.py | 1 + tests/test_asyncio_client.py | 3 +++ tests/test_client.py | 2 ++ 4 files changed, 7 insertions(+) diff --git a/socketio/asyncio_client.py b/socketio/asyncio_client.py index f5fae64..2f78a52 100644 --- a/socketio/asyncio_client.py +++ b/socketio/asyncio_client.py @@ -197,6 +197,7 @@ class AsyncClient(client.Client): await self._trigger_event('disconnect', namespace='/') await self._send_packet(packet.Packet( packet.DISCONNECT, namespace='/')) + await self.eio.disconnect(abort=True) def start_background_task(self, target, *args, **kwargs): """Start a background task using the appropriate async model. diff --git a/socketio/client.py b/socketio/client.py index 484cb12..f56493f 100644 --- a/socketio/client.py +++ b/socketio/client.py @@ -293,6 +293,7 @@ class Client(object): self._trigger_event('disconnect', namespace='/') self._send_packet(packet.Packet( packet.DISCONNECT, namespace='/')) + self.eio.disconnect(abort=True) def transport(self): """Return the name of the transport used by the client. diff --git a/tests/test_asyncio_client.py b/tests/test_asyncio_client.py index a09b838..85dc735 100644 --- a/tests/test_asyncio_client.py +++ b/tests/test_asyncio_client.py @@ -262,6 +262,8 @@ class TestAsyncClient(unittest.TestCase): c = asyncio_client.AsyncClient() c._trigger_event = AsyncMock() c._send_packet = AsyncMock() + c.eio = mock.MagicMock() + c.eio.disconnect = AsyncMock() _run(c.disconnect()) c._trigger_event.mock.assert_called_once_with( 'disconnect', namespace='/') @@ -269,6 +271,7 @@ class TestAsyncClient(unittest.TestCase): expected_packet = packet.Packet(packet.DISCONNECT, namespace='/') self.assertEqual(c._send_packet.mock.call_args_list[0][0][0].encode(), expected_packet.encode()) + c.eio.disconnect.mock.assert_called_once_with(abort=True) def test_disconnect_namespaces(self): c = asyncio_client.AsyncClient() diff --git a/tests/test_client.py b/tests/test_client.py index 7458b5c..02d5ed0 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -336,12 +336,14 @@ class TestClient(unittest.TestCase): c = client.Client() c._trigger_event = mock.MagicMock() c._send_packet = mock.MagicMock() + c.eio = mock.MagicMock() c.disconnect() c._trigger_event.assert_called_once_with('disconnect', namespace='/') self.assertEqual(c._send_packet.call_count, 1) expected_packet = packet.Packet(packet.DISCONNECT, namespace='/') self.assertEqual(c._send_packet.call_args_list[0][0][0].encode(), expected_packet.encode()) + c.eio.disconnect.assert_called_once_with(abort=True) def test_disconnect_namespaces(self): c = client.Client()