From c49f05050deb34150d885e658f89fcb4f4c06bac Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sun, 14 Dec 2025 18:50:22 +0000 Subject: [PATCH] async side --- src/socketio/async_pubsub_manager.py | 9 +++ tests/async/test_pubsub_manager.py | 100 ++++++++++++++++++++++++--- 2 files changed, 99 insertions(+), 10 deletions(-) diff --git a/src/socketio/async_pubsub_manager.py b/src/socketio/async_pubsub_manager.py index 1bfcf9d..6d63164 100644 --- a/src/socketio/async_pubsub_manager.py +++ b/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'), diff --git a/tests/async/test_pubsub_manager.py b/tests/async/test_pubsub_manager.py index f9cfb7e..0da08d2 100644 --- a/tests/async/test_pubsub_manager.py +++ b/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,