Browse Source

Enable emitting to single client in managers with to=... (#1374)

* Enable emitting to single client in AsyncPubSubManager

* Handle `to` in async manager and sync versions

* Name tests consistently

* Rm extra blank line in test_pubsub_manager
pull/1381/head
Pavieł Michalkievič 8 months ago
committed by GitHub
parent
commit
f1476041e5
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 3
      src/socketio/async_manager.py
  2. 3
      src/socketio/async_pubsub_manager.py
  3. 3
      src/socketio/manager.py
  4. 3
      src/socketio/pubsub_manager.py
  5. 2
      tests/async/test_manager.py
  6. 16
      tests/async/test_pubsub_manager.py
  7. 2
      tests/common/test_manager.py
  8. 16
      tests/common/test_pubsub_manager.py

3
src/socketio/async_manager.py

@ -11,12 +11,13 @@ class AsyncManager(BaseManager):
return self.is_connected(sid, namespace)
async def emit(self, event, data, namespace, room=None, skip_sid=None,
callback=None, **kwargs):
callback=None, to=None, **kwargs):
"""Emit a message to a single client, a room, or all the clients
connected to the namespace.
Note: this method is a coroutine.
"""
room = to or room
if namespace not in self.rooms:
return
if isinstance(data, tuple):

3
src/socketio/async_pubsub_manager.py

@ -38,7 +38,7 @@ class AsyncPubSubManager(AsyncManager):
self._get_logger().info(self.name + ' backend initialized.')
async def emit(self, event, data, namespace=None, room=None, skip_sid=None,
callback=None, **kwargs):
callback=None, to=None, **kwargs):
"""Emit a message to a single client, a room, or all the clients
connected to the namespace.
@ -49,6 +49,7 @@ class AsyncPubSubManager(AsyncManager):
Note: this method is a coroutine.
"""
room = to or room
if kwargs.get('ignore_queue'):
return await super().emit(
event, data, namespace=namespace, room=room, skip_sid=skip_sid,

3
src/socketio/manager.py

@ -20,9 +20,10 @@ class Manager(base_manager.BaseManager):
return self.is_connected(sid, namespace)
def emit(self, event, data, namespace, room=None, skip_sid=None,
callback=None, **kwargs):
callback=None, to=None, **kwargs):
"""Emit a message to a single client, a room, or all the clients
connected to the namespace."""
room = to or room
if namespace not in self.rooms:
return
if isinstance(data, tuple):

3
src/socketio/pubsub_manager.py

@ -37,7 +37,7 @@ class PubSubManager(Manager):
self._get_logger().info(self.name + ' backend initialized.')
def emit(self, event, data, namespace=None, room=None, skip_sid=None,
callback=None, **kwargs):
callback=None, to=None, **kwargs):
"""Emit a message to a single client, a room, or all the clients
connected to the namespace.
@ -46,6 +46,7 @@ class PubSubManager(Manager):
The parameters are the same as in :meth:`.Server.emit`.
"""
room = to or room
if kwargs.get('ignore_queue'):
return super().emit(
event, data, namespace=namespace, room=room, skip_sid=skip_sid,

2
tests/async/test_manager.py

@ -205,7 +205,7 @@ class TestAsyncManager(unittest.TestCase):
_run(self.bm.connect('456', '/foo'))
_run(
self.bm.emit(
'my event', {'foo': 'bar'}, namespace='/foo', room=sid
'my event', {'foo': 'bar'}, namespace='/foo', to=sid
)
)
assert self.bm.server._send_eio_packet.mock.call_count == 1

16
tests/async/test_pubsub_manager.py

@ -67,6 +67,22 @@ class TestAsyncPubSubManager(unittest.TestCase):
}
)
def test_emit_with_to(self):
sid = 'room-mate'
_run(self.pm.emit('foo', 'bar', to=sid))
self.pm._publish.mock.assert_called_once_with(
{
'method': 'emit',
'event': 'foo',
'data': 'bar',
'namespace': '/',
'room': sid,
'skip_sid': None,
'callback': None,
'host_id': '123456',
}
)
def test_emit_with_namespace(self):
_run(self.pm.emit('foo', 'bar', namespace='/baz'))
self.pm._publish.mock.assert_called_once_with(

2
tests/common/test_manager.py

@ -206,7 +206,7 @@ class TestBaseManager(unittest.TestCase):
def test_emit_to_sid(self):
sid = self.bm.connect('123', '/foo')
self.bm.connect('456', '/foo')
self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo', room=sid)
self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo', to=sid)
assert self.bm.server._send_eio_packet.call_count == 1
assert self.bm.server._send_eio_packet.call_args_list[0][0][0] == '123'
pkt = self.bm.server._send_eio_packet.call_args_list[0][0][1]

16
tests/common/test_pubsub_manager.py

@ -78,6 +78,22 @@ class TestPubSubManager(unittest.TestCase):
}
)
def test_emit_with_to(self):
sid = "ferris"
self.pm.emit('foo', 'bar', to=sid)
self.pm._publish.assert_called_once_with(
{
'method': 'emit',
'event': 'foo',
'data': 'bar',
'namespace': '/',
'room': sid,
'skip_sid': None,
'callback': None,
'host_id': '123456',
}
)
def test_emit_with_namespace(self):
self.pm.emit('foo', 'bar', namespace='/baz')
self.pm._publish.assert_called_once_with(

Loading…
Cancel
Save