Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/socketio/async_pubsub_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ async def emit(self, event, data, namespace=None, room=None, skip_sid=None,
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)
Expand Down Expand Up @@ -155,6 +159,11 @@ async def _handle_emit(self, message):
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'),
Expand Down
9 changes: 9 additions & 0 deletions src/socketio/pubsub_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def emit(self, event, data, namespace=None, room=None, skip_sid=None,
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)
Expand Down Expand Up @@ -151,6 +155,11 @@ def _handle_emit(self, message):
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)
super().emit(message['event'], data,
namespace=message.get('namespace'),
room=message.get('room'),
Expand Down
217 changes: 207 additions & 10 deletions tests/async/test_pubsub_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def test_emit(self):
'method': 'emit',
'event': 'foo',
'binary': False,
'data': 'bar',
'data': ['bar'],
'namespace': '/',
'room': None,
'skip_sid': None,
Expand All @@ -74,7 +74,7 @@ async def test_emit_binary(self):
'method': 'emit',
'event': 'foo',
'binary': True,
'data': [{'_placeholder': True, 'num': 0}, 'YmFy'],
'data': [[{'_placeholder': True, 'num': 0}], 'YmFy'],
'namespace': '/',
'room': None,
'skip_sid': None,
Expand All @@ -88,7 +88,7 @@ async def test_emit_binary(self):
'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,
Expand All @@ -104,7 +104,7 @@ async def test_emit_bytearray(self):
'method': 'emit',
'event': 'foo',
'binary': True,
'data': [{'_placeholder': True, 'num': 0}, 'YmFy'],
'data': [[{'_placeholder': True, 'num': 0}], 'YmFy'],
'namespace': '/',
'room': None,
'skip_sid': None,
Expand All @@ -118,7 +118,87 @@ async def test_emit_bytearray(self):
'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,
Expand All @@ -135,7 +215,7 @@ async def test_emit_with_to(self):
'method': 'emit',
'event': 'foo',
'binary': False,
'data': 'bar',
'data': ['bar'],
'namespace': '/',
'room': sid,
'skip_sid': None,
Expand All @@ -151,7 +231,7 @@ async def test_emit_with_namespace(self):
'method': 'emit',
'event': 'foo',
'binary': False,
'data': 'bar',
'data': ['bar'],
'namespace': '/baz',
'room': None,
'skip_sid': None,
Expand All @@ -167,7 +247,7 @@ async def test_emit_with_room(self):
'method': 'emit',
'event': 'foo',
'binary': False,
'data': 'bar',
'data': ['bar'],
'namespace': '/',
'room': 'baz',
'skip_sid': None,
Expand All @@ -183,7 +263,7 @@ async def test_emit_with_skip_sid(self):
'method': 'emit',
'event': 'foo',
'binary': False,
'data': 'bar',
'data': ['bar'],
'namespace': '/',
'room': None,
'skip_sid': 'baz',
Expand All @@ -202,7 +282,7 @@ async def test_emit_with_callback(self):
'method': 'emit',
'event': 'foo',
'binary': False,
'data': 'bar',
'data': ['bar'],
'namespace': '/',
'room': 'baz',
'skip_sid': None,
Expand Down Expand Up @@ -294,6 +374,20 @@ async def test_close_room_with_namespace(self):
)

async def test_handle_emit(self):
with mock.patch.object(
async_manager.AsyncManager, 'emit'
) as super_emit:
await self.pm._handle_emit({'event': 'foo', 'data': ['bar']})
super_emit.assert_awaited_once_with(
'foo',
'bar',
namespace=None,
room=None,
skip_sid=None,
callback=None,
)

async def test_handle_legacy_emit(self):
with mock.patch.object(
async_manager.AsyncManager, 'emit'
) as super_emit:
Expand All @@ -308,6 +402,37 @@ async def test_handle_emit(self):
)

async def test_handle_emit_binary(self):
with mock.patch.object(
async_manager.AsyncManager, 'emit'
) as super_emit:
await self.pm._handle_emit({
'event': 'foo',
'binary': True,
'data': [[{'_placeholder': True, 'num': 0}], 'YmFy'],
})
super_emit.assert_awaited_once_with(
'foo',
b'bar',
namespace=None,
room=None,
skip_sid=None,
callback=None,
)
await self.pm._handle_emit({
'event': 'foo',
'binary': True,
'data': [[{'foo': {'_placeholder': True, 'num': 0}}], 'YmFy'],
})
super_emit.assert_awaited_with(
'foo',
{'foo': b'bar'},
namespace=None,
room=None,
skip_sid=None,
callback=None,
)

async def test_handle_legacy_emit_binary(self):
with mock.patch.object(
async_manager.AsyncManager, 'emit'
) as super_emit:
Expand Down Expand Up @@ -338,6 +463,78 @@ async def test_handle_emit_binary(self):
callback=None,
)

async def test_handle_emit_list(self):
with mock.patch.object(
async_manager.AsyncManager, 'emit'
) as super_emit:
await self.pm._handle_emit({'event': 'foo', 'data': [[1, 'two']]})
super_emit.assert_awaited_once_with(
'foo',
[1, 'two'],
namespace=None,
room=None,
skip_sid=None,
callback=None,
)
await self.pm._handle_emit({
'event': 'foo',
'binary': True,
'data': [
[[1, {'_placeholder': True, 'num': 0}, 'three']], 'dHdv'
]
})
super_emit.assert_awaited_with(
'foo',
[1, b'two', 'three'],
namespace=None,
room=None,
skip_sid=None,
callback=None,
)

async def test_handle_emit_no_arguments(self):
with mock.patch.object(
async_manager.AsyncManager, 'emit'
) as super_emit:
await self.pm._handle_emit({'event': 'foo', 'data': []})
super_emit.assert_awaited_once_with(
'foo',
(),
namespace=None,
room=None,
skip_sid=None,
callback=None,
)

async def test_handle_emit_multiple_arguments(self):
with mock.patch.object(
async_manager.AsyncManager, 'emit'
) as super_emit:
await self.pm._handle_emit({'event': 'foo', 'data': [1, 'two']})
super_emit.assert_awaited_once_with(
'foo',
(1, 'two'),
namespace=None,
room=None,
skip_sid=None,
callback=None,
)
await self.pm._handle_emit({
'event': 'foo',
'binary': True,
'data': [
[1, {'_placeholder': True, 'num': 0}, 'three'], 'dHdv'
]
})
super_emit.assert_awaited_with(
'foo',
(1, b'two', 'three'),
namespace=None,
room=None,
skip_sid=None,
callback=None,
)

async def test_handle_emit_with_namespace(self):
with mock.patch.object(
async_manager.AsyncManager, 'emit'
Expand Down
Loading
Loading