Browse Source

async side

pull/1540/head
Miguel Grinberg 6 months ago
parent
commit
c49f05050d
Failed to extract signature
  1. 9
      src/socketio/async_pubsub_manager.py
  2. 100
      tests/async/test_pubsub_manager.py

9
src/socketio/async_pubsub_manager.py

@ -66,6 +66,10 @@ class AsyncPubSubManager(AsyncManager):
callback = (room, namespace, id)
else:
callback = None
if isinstance(data, tuple):
data = list(data)
else:
data = [data]
binary = Packet.data_is_binary(data)
if binary:
data, attachments = Packet.deconstruct_binary(data)
@ -155,6 +159,11 @@ class AsyncPubSubManager(AsyncManager):
if message.get('binary'):
attachments = [base64.b64decode(a) for a in data[1:]]
data = Packet.reconstruct_binary(data[0], attachments)
if isinstance(data, list):
if len(data) == 1:
data = data[0]
else:
data = tuple(data)
await super().emit(message['event'], data,
namespace=message.get('namespace'),
room=message.get('room'),

100
tests/async/test_pubsub_manager.py

@ -58,7 +58,7 @@ class TestAsyncPubSubManager:
'method': 'emit',
'event': 'foo',
'binary': False,
'data': 'bar',
'data': ['bar'],
'namespace': '/',
'room': None,
'skip_sid': None,
@ -74,7 +74,7 @@ class TestAsyncPubSubManager:
'method': 'emit',
'event': 'foo',
'binary': True,
'data': [{'_placeholder': True, 'num': 0}, 'YmFy'],
'data': [[{'_placeholder': True, 'num': 0}], 'YmFy'],
'namespace': '/',
'room': None,
'skip_sid': None,
@ -88,7 +88,7 @@ class TestAsyncPubSubManager:
'method': 'emit',
'event': 'foo',
'binary': True,
'data': [{'foo': {'_placeholder': True, 'num': 0}}, 'YmFy'],
'data': [[{'foo': {'_placeholder': True, 'num': 0}}], 'YmFy'],
'namespace': '/',
'room': None,
'skip_sid': None,
@ -104,7 +104,7 @@ class TestAsyncPubSubManager:
'method': 'emit',
'event': 'foo',
'binary': True,
'data': [{'_placeholder': True, 'num': 0}, 'YmFy'],
'data': [[{'_placeholder': True, 'num': 0}], 'YmFy'],
'namespace': '/',
'room': None,
'skip_sid': None,
@ -118,7 +118,87 @@ class TestAsyncPubSubManager:
'method': 'emit',
'event': 'foo',
'binary': True,
'data': [{'foo': {'_placeholder': True, 'num': 0}}, 'YmFy'],
'data': [[{'foo': {'_placeholder': True, 'num': 0}}], 'YmFy'],
'namespace': '/',
'room': None,
'skip_sid': None,
'callback': None,
'host_id': '123456',
}
)
async def test_emit_list(self):
await self.pm.emit('foo', [1, 'two'])
self.pm._publish.assert_awaited_once_with(
{
'method': 'emit',
'event': 'foo',
'binary': False,
'data': [[1, 'two']],
'namespace': '/',
'room': None,
'skip_sid': None,
'callback': None,
'host_id': '123456',
}
)
await self.pm.emit('foo', [1, b'two', 'three'])
self.pm._publish.assert_awaited_with(
{
'method': 'emit',
'event': 'foo',
'binary': True,
'data': [
[[1, {'_placeholder': True, 'num': 0}, 'three']], 'dHdv',
],
'namespace': '/',
'room': None,
'skip_sid': None,
'callback': None,
'host_id': '123456',
}
)
async def test_emit_no_arguments(self):
await self.pm.emit('foo', ())
self.pm._publish.assert_awaited_once_with(
{
'method': 'emit',
'event': 'foo',
'binary': False,
'data': [],
'namespace': '/',
'room': None,
'skip_sid': None,
'callback': None,
'host_id': '123456',
}
)
async def test_emit_multiple_arguments(self):
await self.pm.emit('foo', (1, 'two'))
self.pm._publish.assert_awaited_once_with(
{
'method': 'emit',
'event': 'foo',
'binary': False,
'data': [1, 'two'],
'namespace': '/',
'room': None,
'skip_sid': None,
'callback': None,
'host_id': '123456',
}
)
await self.pm.emit('foo', (1, b'two', 'three'))
self.pm._publish.assert_awaited_with(
{
'method': 'emit',
'event': 'foo',
'binary': True,
'data': [
[1, {'_placeholder': True, 'num': 0}, 'three'], 'dHdv',
],
'namespace': '/',
'room': None,
'skip_sid': None,
@ -135,7 +215,7 @@ class TestAsyncPubSubManager:
'method': 'emit',
'event': 'foo',
'binary': False,
'data': 'bar',
'data': ['bar'],
'namespace': '/',
'room': sid,
'skip_sid': None,
@ -151,7 +231,7 @@ class TestAsyncPubSubManager:
'method': 'emit',
'event': 'foo',
'binary': False,
'data': 'bar',
'data': ['bar'],
'namespace': '/baz',
'room': None,
'skip_sid': None,
@ -167,7 +247,7 @@ class TestAsyncPubSubManager:
'method': 'emit',
'event': 'foo',
'binary': False,
'data': 'bar',
'data': ['bar'],
'namespace': '/',
'room': 'baz',
'skip_sid': None,
@ -183,7 +263,7 @@ class TestAsyncPubSubManager:
'method': 'emit',
'event': 'foo',
'binary': False,
'data': 'bar',
'data': ['bar'],
'namespace': '/',
'room': None,
'skip_sid': 'baz',
@ -202,7 +282,7 @@ class TestAsyncPubSubManager:
'method': 'emit',
'event': 'foo',
'binary': False,
'data': 'bar',
'data': ['bar'],
'namespace': '/',
'room': 'baz',
'skip_sid': None,

Loading…
Cancel
Save