Browse Source

Correctly handle emits to multiple rooms in the async server (Fixes #1081)

pull/1082/head
Miguel Grinberg 2 years ago
parent
commit
232cef1f86
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 2
      src/socketio/asyncio_manager.py
  2. 23
      tests/asyncio/test_asyncio_manager.py

2
src/socketio/asyncio_manager.py

@ -15,7 +15,7 @@ class AsyncManager(BaseManager):
Note: this method is a coroutine. Note: this method is a coroutine.
""" """
if namespace not in self.rooms or room not in self.rooms[namespace]: if namespace not in self.rooms:
return return
tasks = [] tasks = []
if not isinstance(skip_sid, list): if not isinstance(skip_sid, list):

23
tests/asyncio/test_asyncio_manager.py

@ -244,6 +244,29 @@ class TestAsyncManager(unittest.TestCase):
'456', 'my event', {'foo': 'bar'}, '/foo', None '456', 'my event', {'foo': 'bar'}, '/foo', None
) )
def test_emit_to_rooms(self):
sid1 = self.bm.connect('123', '/foo')
self.bm.enter_room(sid1, '/foo', 'bar')
sid2 = self.bm.connect('456', '/foo')
self.bm.enter_room(sid2, '/foo', 'bar')
self.bm.enter_room(sid2, '/foo', 'baz')
sid3 = self.bm.connect('789', '/foo')
self.bm.enter_room(sid3, '/foo', 'baz')
_run(
self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo',
room=['bar', 'baz'])
)
assert self.bm.server._emit_internal.mock.call_count == 3
self.bm.server._emit_internal.mock.assert_any_call(
'123', 'my event', {'foo': 'bar'}, '/foo', None
)
self.bm.server._emit_internal.mock.assert_any_call(
'456', 'my event', {'foo': 'bar'}, '/foo', None
)
self.bm.server._emit_internal.mock.assert_any_call(
'789', 'my event', {'foo': 'bar'}, '/foo', None
)
def test_emit_to_all(self): def test_emit_to_all(self):
sid1 = self.bm.connect('123', '/foo') sid1 = self.bm.connect('123', '/foo')
self.bm.enter_room(sid1, '/foo', 'bar') self.bm.enter_room(sid1, '/foo', 'bar')

Loading…
Cancel
Save