Browse Source

Invoke the disconnect handler when the client initiates a disconnection (Fixes #594)

pull/602/head
Miguel Grinberg 4 years ago
parent
commit
3349b024d5
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 1
      socketio/asyncio_client.py
  2. 1
      socketio/client.py
  3. 2
      tests/asyncio/test_asyncio_client.py
  4. 2
      tests/common/test_client.py

1
socketio/asyncio_client.py

@ -255,7 +255,6 @@ class AsyncClient(client.Client):
for n in self.namespaces: for n in self.namespaces:
await self._send_packet(packet.Packet(packet.DISCONNECT, await self._send_packet(packet.Packet(packet.DISCONNECT,
namespace=n)) namespace=n))
self.connected = False
await self.eio.disconnect(abort=True) await self.eio.disconnect(abort=True)
def start_background_task(self, target, *args, **kwargs): def start_background_task(self, target, *args, **kwargs):

1
socketio/client.py

@ -408,7 +408,6 @@ class Client(object):
# later in _handle_eio_disconnect we invoke the disconnect handler # later in _handle_eio_disconnect we invoke the disconnect handler
for n in self.namespaces: for n in self.namespaces:
self._send_packet(packet.Packet(packet.DISCONNECT, namespace=n)) self._send_packet(packet.Packet(packet.DISCONNECT, namespace=n))
self.connected = False
self.eio.disconnect(abort=True) self.eio.disconnect(abort=True)
def get_sid(self, namespace=None): def get_sid(self, namespace=None):

2
tests/asyncio/test_asyncio_client.py

@ -406,6 +406,7 @@ class TestAsyncClient(unittest.TestCase):
def test_disconnect(self): def test_disconnect(self):
c = asyncio_client.AsyncClient() c = asyncio_client.AsyncClient()
c.connected = True
c.namespaces = {'/': '1'} c.namespaces = {'/': '1'}
c._trigger_event = AsyncMock() c._trigger_event = AsyncMock()
c._send_packet = AsyncMock() c._send_packet = AsyncMock()
@ -413,6 +414,7 @@ class TestAsyncClient(unittest.TestCase):
c.eio.disconnect = AsyncMock() c.eio.disconnect = AsyncMock()
c.eio.state = 'connected' c.eio.state = 'connected'
_run(c.disconnect()) _run(c.disconnect())
assert c.connected
assert c._trigger_event.mock.call_count == 0 assert c._trigger_event.mock.call_count == 0
assert c._send_packet.mock.call_count == 1 assert c._send_packet.mock.call_count == 1
expected_packet = packet.Packet(packet.DISCONNECT, namespace='/') expected_packet = packet.Packet(packet.DISCONNECT, namespace='/')

2
tests/common/test_client.py

@ -508,12 +508,14 @@ class TestClient(unittest.TestCase):
def test_disconnect(self): def test_disconnect(self):
c = client.Client() c = client.Client()
c.connected = True
c.namespaces = {'/': '1'} c.namespaces = {'/': '1'}
c._trigger_event = mock.MagicMock() c._trigger_event = mock.MagicMock()
c._send_packet = mock.MagicMock() c._send_packet = mock.MagicMock()
c.eio = mock.MagicMock() c.eio = mock.MagicMock()
c.eio.state = 'connected' c.eio.state = 'connected'
c.disconnect() c.disconnect()
assert c.connected
assert c._trigger_event.call_count == 0 assert c._trigger_event.call_count == 0
assert c._send_packet.call_count == 1 assert c._send_packet.call_count == 1
expected_packet = packet.Packet(packet.DISCONNECT, namespace='/') expected_packet = packet.Packet(packet.DISCONNECT, namespace='/')

Loading…
Cancel
Save