Skip to content

Commit

Permalink
Internal code restructure (no functional changes)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Oct 4, 2023
1 parent e26163c commit f15527b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
13 changes: 6 additions & 7 deletions src/engineio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
from . import payload

default_logger = logging.getLogger('engineio.client')
connected_clients = []


def signal_handler(sig, frame):
"""SIGINT handler.
Disconnect all active clients and then invoke the original signal handler.
"""
for client in connected_clients[:]:
for client in base_client.connected_clients[:]:
if not client.is_asyncio_based():
client.disconnect()
if callable(original_signal_handler):
Expand Down Expand Up @@ -163,7 +162,7 @@ def disconnect(self, abort=False):
self.read_loop_task.join()
self.state = 'disconnected'
try:
connected_clients.remove(self)
base_client.connected_clients.remove(self)
except ValueError: # pragma: no cover
pass
self._reset()
Expand Down Expand Up @@ -245,7 +244,7 @@ def _connect_polling(self, url, headers, engineio_path):
self.base_url += '&sid=' + self.sid

self.state = 'connected'
connected_clients.append(self)
base_client.connected_clients.append(self)
self._trigger_event('connect', run_async=False)

for pkt in p.packets[1:]:
Expand Down Expand Up @@ -414,7 +413,7 @@ def _connect_websocket(self, url, headers, engineio_path):
self.current_transport = 'websocket'

self.state = 'connected'
connected_clients.append(self)
base_client.connected_clients.append(self)
self._trigger_event('connect', run_async=False)
self.ws = ws
self.ws.settimeout(self.ping_interval + self.ping_timeout)
Expand Down Expand Up @@ -514,7 +513,7 @@ def _read_loop_polling(self):
if self.state == 'connected':
self._trigger_event('disconnect', run_async=False)
try:
connected_clients.remove(self)
base_client.connected_clients.remove(self)
except ValueError: # pragma: no cover
pass
self._reset()
Expand Down Expand Up @@ -556,7 +555,7 @@ def _read_loop_websocket(self):
if self.state == 'connected':
self._trigger_event('disconnect', run_async=False)
try:
connected_clients.remove(self)
base_client.connected_clients.remove(self)
except ValueError: # pragma: no cover
pass
self._reset()
Expand Down
37 changes: 19 additions & 18 deletions tests/common/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
import websocket

from engineio import base_client
from engineio import client
from engineio import exceptions
from engineio import json
Expand Down Expand Up @@ -197,7 +198,7 @@ def test_disconnect_not_connected(self):

def test_disconnect_polling(self):
c = client.Client()
client.connected_clients.append(c)
base_client.connected_clients.append(c)
c.state = 'connected'
c.current_transport = 'polling'
c.queue = mock.MagicMock()
Expand All @@ -207,12 +208,12 @@ def test_disconnect_polling(self):
c.disconnect()
c.read_loop_task.join.assert_called_once_with()
c.ws.mock.assert_not_called()
assert c not in client.connected_clients
assert c not in base_client.connected_clients
c._trigger_event.assert_called_once_with('disconnect', run_async=False)

def test_disconnect_websocket(self):
c = client.Client()
client.connected_clients.append(c)
base_client.connected_clients.append(c)
c.state = 'connected'
c.current_transport = 'websocket'
c.queue = mock.MagicMock()
Expand All @@ -222,12 +223,12 @@ def test_disconnect_websocket(self):
c.disconnect()
c.read_loop_task.join.assert_called_once_with()
c.ws.close.assert_called_once_with()
assert c not in client.connected_clients
assert c not in base_client.connected_clients
c._trigger_event.assert_called_once_with('disconnect', run_async=False)

def test_disconnect_polling_abort(self):
c = client.Client()
client.connected_clients.append(c)
base_client.connected_clients.append(c)
c.state = 'connected'
c.current_transport = 'polling'
c.queue = mock.MagicMock()
Expand All @@ -237,11 +238,11 @@ def test_disconnect_polling_abort(self):
c.queue.join.assert_not_called()
c.read_loop_task.join.assert_not_called()
c.ws.mock.assert_not_called()
assert c not in client.connected_clients
assert c not in base_client.connected_clients

def test_disconnect_websocket_abort(self):
c = client.Client()
client.connected_clients.append(c)
base_client.connected_clients.append(c)
c.state = 'connected'
c.current_transport = 'websocket'
c.queue = mock.MagicMock()
Expand All @@ -251,7 +252,7 @@ def test_disconnect_websocket_abort(self):
c.queue.join.assert_not_called()
c.read_loop_task.join.assert_not_called()
c.ws.mock.assert_not_called()
assert c not in client.connected_clients
assert c not in base_client.connected_clients

def test_current_transport(self):
c = client.Client()
Expand Down Expand Up @@ -388,7 +389,7 @@ def test_polling_connection_successful(self, _send_request):
c._read_loop_websocket.assert_not_called()
c._write_loop.assert_called_once_with()
on_connect.assert_called_once_with()
assert c in client.connected_clients
assert c in base_client.connected_clients
assert (
c.base_url
== 'http://foo/engine.io/?transport=polling&EIO=4&sid=123'
Expand Down Expand Up @@ -428,7 +429,7 @@ def test_polling_https_noverify_connection_successful(self, _send_request):
c._read_loop_websocket.assert_not_called()
c._write_loop.assert_called_once_with()
on_connect.assert_called_once_with()
assert c in client.connected_clients
assert c in base_client.connected_clients
assert (
c.base_url
== 'https://foo/engine.io/?transport=polling&EIO=4&sid=123'
Expand Down Expand Up @@ -497,7 +498,7 @@ def test_polling_connection_upgraded(self, _send_request):
'http://foo', {}, 'engine.io'
)
on_connect.assert_called_once_with()
assert c in client.connected_clients
assert c in base_client.connected_clients
assert (
c.base_url
== 'http://foo/engine.io/?transport=polling&EIO=4&sid=123'
Expand Down Expand Up @@ -540,7 +541,7 @@ def test_polling_connection_not_upgraded(self, _send_request):
c._read_loop_websocket.assert_not_called()
c._write_loop.assert_called_once_with()
on_connect.assert_called_once_with()
assert c in client.connected_clients
assert c in base_client.connected_clients

@mock.patch('engineio.client.time.time', return_value=123.456)
@mock.patch(
Expand Down Expand Up @@ -654,7 +655,7 @@ def test_websocket_connection_successful(self, create_connection):
c._read_loop_websocket.assert_called_once_with()
c._write_loop.assert_called_once_with()
on_connect.assert_called_once_with()
assert c in client.connected_clients
assert c in base_client.connected_clients
assert c.base_url == 'ws://foo/engine.io/?transport=websocket&EIO=4'
assert c.sid == '123'
assert c.ping_interval == 1
Expand Down Expand Up @@ -696,7 +697,7 @@ def test_websocket_https_noverify_connection_successful(
c._read_loop_websocket.assert_called_once_with()
c._write_loop.assert_called_once_with()
on_connect.assert_called_once_with()
assert c in client.connected_clients
assert c in base_client.connected_clients
assert c.base_url == 'wss://foo/engine.io/?transport=websocket&EIO=4'
assert c.sid == '123'
assert c.ping_interval == 1
Expand Down Expand Up @@ -1152,7 +1153,7 @@ def test_websocket_upgrade_successful(self, create_connection):
c._read_loop_websocket.assert_called_once_with()
c._write_loop.assert_called_once_with()
on_connect.assert_not_called() # was called by polling
assert c not in client.connected_clients # was added by polling
assert c not in base_client.connected_clients # was added by polling
assert c.base_url == 'http://foo' # not changed
assert c.sid == '123' # not changed
assert c.transport() == 'websocket'
Expand Down Expand Up @@ -1717,9 +1718,9 @@ def test_write_loop_websocket_bad_connection(self):
@mock.patch('engineio.client.original_signal_handler')
def test_signal_handler(self, original_handler):
clients = [mock.MagicMock(), mock.MagicMock()]
client.connected_clients = clients[:]
client.connected_clients[0].is_asyncio_based.return_value = False
client.connected_clients[1].is_asyncio_based.return_value = True
base_client.connected_clients = clients[:]
base_client.connected_clients[0].is_asyncio_based.return_value = False
base_client.connected_clients[1].is_asyncio_based.return_value = True
client.signal_handler('sig', 'frame')
clients[0].disconnect.assert_called_once_with()
clients[1].disconnect.assert_not_called()

0 comments on commit f15527b

Please sign in to comment.