Browse Source

Avoid double calls to client disconnect handlers

Fixes #261
pull/269/head
Miguel Grinberg 6 years ago
parent
commit
f752312884
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 4
      socketio/asyncio_client.py
  2. 6
      socketio/client.py
  3. 15
      tests/asyncio/test_asyncio_client.py
  4. 15
      tests/common/test_client.py

4
socketio/asyncio_client.py

@ -235,11 +235,11 @@ class AsyncClient(client.Client):
Note: this method is a coroutine.
"""
# here we just request the disconnection
# later in _handle_eio_disconnect we invoke the disconnect handler
for n in self.namespaces:
await self._trigger_event('disconnect', namespace=n)
await self._send_packet(packet.Packet(packet.DISCONNECT,
namespace=n))
await self._trigger_event('disconnect', namespace='/')
await self._send_packet(packet.Packet(
packet.DISCONNECT, namespace='/'))
await self.eio.disconnect(abort=True)

6
socketio/client.py

@ -317,7 +317,7 @@ class Client(object):
def event_callback(*args):
callback_args.append(args)
callback_event.set()
self.emit(event, data=data, namespace=namespace,
callback=event_callback)
if not callback_event.wait(timeout=timeout):
@ -328,10 +328,10 @@ class Client(object):
def disconnect(self):
"""Disconnect from the server."""
# here we just request the disconnection
# later in _handle_eio_disconnect we invoke the disconnect handler
for n in self.namespaces:
self._trigger_event('disconnect', namespace=n)
self._send_packet(packet.Packet(packet.DISCONNECT, namespace=n))
self._trigger_event('disconnect', namespace='/')
self._send_packet(packet.Packet(
packet.DISCONNECT, namespace='/'))
self.eio.disconnect(abort=True)

15
tests/asyncio/test_asyncio_client.py

@ -291,9 +291,9 @@ class TestAsyncClient(unittest.TestCase):
c._send_packet = AsyncMock()
c.eio = mock.MagicMock()
c.eio.disconnect = AsyncMock()
c.eio.state = 'connected'
_run(c.disconnect())
c._trigger_event.mock.assert_called_once_with(
'disconnect', namespace='/')
self.assertEqual(c._trigger_event.mock.call_count, 0)
self.assertEqual(c._send_packet.mock.call_count, 1)
expected_packet = packet.Packet(packet.DISCONNECT, namespace='/')
self.assertEqual(c._send_packet.mock.call_args_list[0][0][0].encode(),
@ -305,12 +305,11 @@ class TestAsyncClient(unittest.TestCase):
c.namespaces = ['/foo', '/bar']
c._trigger_event = AsyncMock()
c._send_packet = AsyncMock()
c.eio = mock.MagicMock()
c.eio.disconnect = AsyncMock()
c.eio.state = 'connected'
_run(c.disconnect())
self.assertEqual(c._trigger_event.mock.call_args_list, [
mock.call('disconnect', namespace='/foo'),
mock.call('disconnect', namespace='/bar'),
mock.call('disconnect', namespace='/')
])
self.assertEqual(c._trigger_event.mock.call_count, 0)
self.assertEqual(c._send_packet.mock.call_count, 3)
expected_packet = packet.Packet(packet.DISCONNECT, namespace='/foo')
self.assertEqual(c._send_packet.mock.call_args_list[0][0][0].encode(),
@ -632,6 +631,7 @@ class TestAsyncClient(unittest.TestCase):
def test_eio_disconnect(self):
c = asyncio_client.AsyncClient()
c._trigger_event = AsyncMock()
c.eio.state = 'connected'
_run(c._handle_eio_disconnect())
c._trigger_event.mock.assert_called_once_with(
'disconnect', namespace='/')
@ -640,6 +640,7 @@ class TestAsyncClient(unittest.TestCase):
c = asyncio_client.AsyncClient()
c.namespaces = ['/foo', '/bar']
c._trigger_event = AsyncMock()
c.eio.state = 'connected'
_run(c._handle_eio_disconnect())
c._trigger_event.mock.assert_any_call('disconnect', namespace='/foo')
c._trigger_event.mock.assert_any_call('disconnect', namespace='/bar')

15
tests/common/test_client.py

@ -376,8 +376,9 @@ class TestClient(unittest.TestCase):
c._trigger_event = mock.MagicMock()
c._send_packet = mock.MagicMock()
c.eio = mock.MagicMock()
c.eio.state = 'connected'
c.disconnect()
c._trigger_event.assert_called_once_with('disconnect', namespace='/')
self.assertEqual(c._trigger_event.call_count, 0)
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(),
@ -389,12 +390,10 @@ class TestClient(unittest.TestCase):
c.namespaces = ['/foo', '/bar']
c._trigger_event = mock.MagicMock()
c._send_packet = mock.MagicMock()
c.eio = mock.MagicMock()
c.eio.state = 'connected'
c.disconnect()
self.assertEqual(c._trigger_event.call_args_list, [
mock.call('disconnect', namespace='/foo'),
mock.call('disconnect', namespace='/bar'),
mock.call('disconnect', namespace='/')
])
self.assertEqual(c._trigger_event.call_count, 0)
self.assertEqual(c._send_packet.call_count, 3)
expected_packet = packet.Packet(packet.DISCONNECT, namespace='/foo')
self.assertEqual(c._send_packet.call_args_list[0][0][0].encode(),
@ -731,6 +730,8 @@ class TestClient(unittest.TestCase):
def test_eio_disconnect(self):
c = client.Client()
c._trigger_event = mock.MagicMock()
c.start_background_task = mock.MagicMock()
c.eio.state = 'connected'
c._handle_eio_disconnect()
c._trigger_event.assert_called_once_with('disconnect', namespace='/')
@ -738,6 +739,8 @@ class TestClient(unittest.TestCase):
c = client.Client()
c.namespaces = ['/foo', '/bar']
c._trigger_event = mock.MagicMock()
c.start_background_task = mock.MagicMock()
c.eio.state = 'connected'
c._handle_eio_disconnect()
c._trigger_event.assert_any_call('disconnect', namespace='/foo')
c._trigger_event.assert_any_call('disconnect', namespace='/bar')

Loading…
Cancel
Save