diff --git a/src/socketio/admin.py b/src/socketio/admin.py index 81aa263..a628540 100644 --- a/src/socketio/admin.py +++ b/src/socketio/admin.py @@ -85,12 +85,14 @@ class InstrumentedServer: self.sio.manager.disconnect = self._disconnect # report join rooms - self.sio.manager.__enter_room = self.sio.manager.enter_room - self.sio.manager.enter_room = self._enter_room + self.sio.manager.__basic_enter_room = \ + self.sio.manager.basic_enter_room + self.sio.manager.basic_enter_room = self._basic_enter_room # report leave rooms - self.sio.manager.__leave_room = self.sio.manager.leave_room - self.sio.manager.leave_room = self._leave_room + self.sio.manager.__basic_leave_room = \ + self.sio.manager.basic_leave_room + self.sio.manager.basic_leave_room = self._basic_leave_room # report emit events self.sio.manager.__emit = self.sio.manager.emit @@ -121,8 +123,10 @@ class InstrumentedServer: if self.mode == 'development': self.sio.manager.connect = self.sio.manager.__connect self.sio.manager.disconnect = self.sio.manager.__disconnect - self.sio.manager.enter_room = self.sio.manager.__enter_room - self.sio.manager.leave_room = self.sio.manager.__leave_room + self.sio.manager.basic_enter_room = \ + self.sio.manager.__basic_enter_room + self.sio.manager.basic_leave_room = \ + self.sio.manager.__basic_leave_room self.sio.manager.emit = self.sio.manager.__emit self.sio._handle_event_internal = self.sio.__handle_event_internal self.sio.eio._ok = self.sio.eio.__ok @@ -234,8 +238,9 @@ class InstrumentedServer: except KeyError: pass - def _enter_room(self, sid, namespace, room, eio_sid=None): - ret = self.sio.manager.__enter_room(sid, namespace, room, eio_sid) + def _basic_enter_room(self, sid, namespace, room, eio_sid=None): + ret = self.sio.manager.__basic_enter_room(sid, namespace, room, + eio_sid) if room: self.sio.emit('room_joined', ( namespace, @@ -245,7 +250,7 @@ class InstrumentedServer: ), namespace=self.admin_namespace) return ret - def _leave_room(self, sid, namespace, room): + def _basic_leave_room(self, sid, namespace, room): if room: self.sio.emit('room_left', ( namespace, @@ -253,7 +258,7 @@ class InstrumentedServer: sid, datetime.utcnow().isoformat() + 'Z', ), namespace=self.admin_namespace) - return self.sio.manager.__leave_room(sid, namespace, room) + return self.sio.manager.__basic_leave_room(sid, namespace, room) def _emit(self, event, data, namespace, room=None, skip_sid=None, callback=None, **kwargs): diff --git a/src/socketio/asyncio_admin.py b/src/socketio/async_admin.py similarity index 92% rename from src/socketio/asyncio_admin.py rename to src/socketio/async_admin.py index f25a839..b43e0d7 100644 --- a/src/socketio/asyncio_admin.py +++ b/src/socketio/async_admin.py @@ -66,12 +66,14 @@ class InstrumentedAsyncServer: self.sio.manager.disconnect = self._disconnect # report join rooms - self.sio.manager.__enter_room = self.sio.manager.enter_room - self.sio.manager.enter_room = self._enter_room + self.sio.manager.__basic_enter_room = \ + self.sio.manager.basic_enter_room + self.sio.manager.basic_enter_room = self._basic_enter_room # report leave rooms - self.sio.manager.__leave_room = self.sio.manager.leave_room - self.sio.manager.leave_room = self._leave_room + self.sio.manager.__basic_leave_room = \ + self.sio.manager.basic_leave_room + self.sio.manager.basic_leave_room = self._basic_leave_room # report emit events self.sio.manager.__emit = self.sio.manager.emit @@ -102,8 +104,10 @@ class InstrumentedAsyncServer: if self.mode == 'development': self.sio.manager.connect = self.sio.manager.__connect self.sio.manager.disconnect = self.sio.manager.__disconnect - self.sio.manager.enter_room = self.sio.manager.__enter_room - self.sio.manager.leave_room = self.sio.manager.__leave_room + self.sio.manager.basic_enter_room = \ + self.sio.manager.__basic_enter_room + self.sio.manager.basic_leave_room = \ + self.sio.manager.__basic_leave_room self.sio.manager.emit = self.sio.manager.__emit self.sio._handle_event_internal = self.sio.__handle_event_internal self.sio.eio._ok = self.sio.eio.__ok @@ -160,15 +164,15 @@ class InstrumentedAsyncServer: async def admin_emit(self, _, namespace, room_filter, event, *data): await self.sio.emit(event, data, to=room_filter, namespace=namespace) - def admin_enter_room(self, _, namespace, room, room_filter=None): + async def admin_enter_room(self, _, namespace, room, room_filter=None): for sid, _ in self.sio.manager.get_participants( namespace, room_filter): - self.sio.enter_room(sid, room, namespace=namespace) + await self.sio.enter_room(sid, room, namespace=namespace) - def admin_leave_room(self, _, namespace, room, room_filter=None): + async def admin_leave_room(self, _, namespace, room, room_filter=None): for sid, _ in self.sio.manager.get_participants( namespace, room_filter): - self.sio.leave_room(sid, room, namespace=namespace) + await self.sio.leave_room(sid, room, namespace=namespace) async def admin_disconnect(self, _, namespace, close, room_filter=None): for sid, _ in self.sio.manager.get_participants( @@ -220,8 +224,9 @@ class InstrumentedAsyncServer: except KeyError: pass - def _enter_room(self, sid, namespace, room, eio_sid=None): - ret = self.sio.manager.__enter_room(sid, namespace, room, eio_sid) + def _basic_enter_room(self, sid, namespace, room, eio_sid=None): + ret = self.sio.manager.__basic_enter_room(sid, namespace, room, + eio_sid) if room: self.admin_queue.append(('room_joined', ( namespace, @@ -231,7 +236,7 @@ class InstrumentedAsyncServer: ))) return ret - def _leave_room(self, sid, namespace, room): + def _basic_leave_room(self, sid, namespace, room): if room: self.admin_queue.append(('room_left', ( namespace, @@ -239,7 +244,7 @@ class InstrumentedAsyncServer: sid, datetime.utcnow().isoformat() + 'Z', ))) - return self.sio.manager.__leave_room(sid, namespace, room) + return self.sio.manager.__basic_leave_room(sid, namespace, room) async def _emit(self, event, data, namespace, room=None, skip_sid=None, callback=None, **kwargs): diff --git a/src/socketio/async_server.py b/src/socketio/async_server.py index 07757f1..93fe676 100644 --- a/src/socketio/async_server.py +++ b/src/socketio/async_server.py @@ -496,7 +496,7 @@ class AsyncServer(base_server.BaseServer): :param namespace: The Socket.IO namespace to use for the admin interface. The default is ``/admin``. """ - from .asyncio_admin import InstrumentedAsyncServer + from .async_admin import InstrumentedAsyncServer return InstrumentedAsyncServer(self, auth=auth, mode=mode, read_only=read_only, server_id=server_id, diff --git a/src/socketio/base_manager.py b/src/socketio/base_manager.py index d1b0a08..ca4b0b9 100644 --- a/src/socketio/base_manager.py +++ b/src/socketio/base_manager.py @@ -126,7 +126,7 @@ class BaseManager: try: for sid, _ in self.get_participants(namespace, room): self.basic_leave_room(sid, namespace, room) - except KeyError: + except KeyError: # pragma: no cover pass def get_rooms(self, sid, namespace): diff --git a/tests/async/test_asyncio_admin.py b/tests/async/test_admin.py similarity index 98% rename from tests/async/test_asyncio_admin.py rename to tests/async/test_admin.py index d2686d3..2822fd1 100644 --- a/tests/async/test_asyncio_admin.py +++ b/tests/async/test_admin.py @@ -4,7 +4,10 @@ import time from unittest import mock import unittest import pytest -from engineio.asyncio_socket import AsyncSocket as EngineIOSocket +try: + from engineio.async_socket import AsyncSocket as EngineIOSocket +except ImportError: + from engineio.asyncio_socket import AsyncSocket as EngineIOSocket import socketio from socketio.exceptions import ConnectionError from tests.asyncio_web_server import SocketIOWebServer @@ -24,8 +27,8 @@ def with_instrumented_server(auth=False, **ikwargs): instrumented_server = sio.instrument(auth=auth, **ikwargs) @sio.event - def enter_room(sid, data): - sio.enter_room(sid, data) + async def enter_room(sid, data): + await sio.enter_room(sid, data) @sio.event async def emit(sid, event): diff --git a/tests/async/test_pubsub_manager.py b/tests/async/test_pubsub_manager.py index da0b86d..48a71aa 100644 --- a/tests/async/test_pubsub_manager.py +++ b/tests/async/test_pubsub_manager.py @@ -182,7 +182,7 @@ class TestAsyncPubSubManager(unittest.TestCase): assert self.pm.is_connected(sid, '/') is False def test_enter_room(self): - sid = self.pm.connect('123', '/') + sid = _run(self.pm.connect('123', '/')) _run(self.pm.enter_room(sid, '/', 'foo')) _run(self.pm.enter_room('456', '/', 'foo')) assert sid in self.pm.rooms['/']['foo'] @@ -193,7 +193,7 @@ class TestAsyncPubSubManager(unittest.TestCase): ) def test_leave_room(self): - sid = self.pm.connect('123', '/') + sid = _run(self.pm.connect('123', '/')) _run(self.pm.leave_room(sid, '/', 'foo')) _run(self.pm.leave_room('456', '/', 'foo')) assert 'foo' not in self.pm.rooms['/'] @@ -435,7 +435,7 @@ class TestAsyncPubSubManager(unittest.TestCase): ) def test_handle_enter_room(self): - sid = self.pm.connect('123', '/') + sid = _run(self.pm.connect('123', '/')) with mock.patch.object( async_manager.AsyncManager, 'enter_room', new=AsyncMock() ) as super_enter_room: @@ -456,7 +456,7 @@ class TestAsyncPubSubManager(unittest.TestCase): ) def test_handle_leave_room(self): - sid = self.pm.connect('123', '/') + sid = _run(self.pm.connect('123', '/')) with mock.patch.object( async_manager.AsyncManager, 'leave_room', new=AsyncMock() ) as super_leave_room: