Browse Source

Changes following internal refactorings

pull/1164/head
Miguel Grinberg 2 years ago
parent
commit
e3fc32d949
Failed to extract signature
  1. 25
      src/socketio/admin.py
  2. 33
      src/socketio/async_admin.py
  3. 2
      src/socketio/async_server.py
  4. 2
      src/socketio/base_manager.py
  5. 9
      tests/async/test_admin.py
  6. 8
      tests/async/test_pubsub_manager.py

25
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):

33
src/socketio/asyncio_admin.py → 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):

2
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,

2
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):

9
tests/async/test_asyncio_admin.py → 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):

8
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:

Loading…
Cancel
Save