Skip to content

Commit 7343dce

Browse files
Support multiple arguments via pubsub emits
1 parent 91553c1 commit 7343dce

File tree

2 files changed

+179
-10
lines changed

2 files changed

+179
-10
lines changed

src/socketio/pubsub_manager.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def emit(self, event, data, namespace=None, room=None, skip_sid=None,
6363
callback = (room, namespace, id)
6464
else:
6565
callback = None
66+
if isinstance(data, tuple):
67+
data = list(data)
68+
else:
69+
data = [data]
6670
binary = Packet.data_is_binary(data)
6771
if binary:
6872
data, attachments = Packet.deconstruct_binary(data)
@@ -151,6 +155,11 @@ def _handle_emit(self, message):
151155
if message.get('binary'):
152156
attachments = [base64.b64decode(a) for a in data[1:]]
153157
data = Packet.reconstruct_binary(data[0], attachments)
158+
if isinstance(data, list):
159+
if len(data) == 1:
160+
data = data[0]
161+
else:
162+
data = tuple(data)
154163
super().emit(message['event'], data,
155164
namespace=message.get('namespace'),
156165
room=message.get('room'),

tests/common/test_pubsub_manager.py

Lines changed: 170 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_emit(self):
7070
'method': 'emit',
7171
'event': 'foo',
7272
'binary': False,
73-
'data': 'bar',
73+
'data': ['bar'],
7474
'namespace': '/',
7575
'room': None,
7676
'skip_sid': None,
@@ -86,7 +86,7 @@ def test_emit_binary(self):
8686
'method': 'emit',
8787
'event': 'foo',
8888
'binary': True,
89-
'data': [{'_placeholder': True, 'num': 0}, 'YmFy'],
89+
'data': [[{'_placeholder': True, 'num': 0}], 'YmFy'],
9090
'namespace': '/',
9191
'room': None,
9292
'skip_sid': None,
@@ -100,7 +100,7 @@ def test_emit_binary(self):
100100
'method': 'emit',
101101
'event': 'foo',
102102
'binary': True,
103-
'data': [{'foo': {'_placeholder': True, 'num': 0}}, 'YmFy'],
103+
'data': [[{'foo': {'_placeholder': True, 'num': 0}}], 'YmFy'],
104104
'namespace': '/',
105105
'room': None,
106106
'skip_sid': None,
@@ -116,7 +116,7 @@ def test_emit_bytearray(self):
116116
'method': 'emit',
117117
'event': 'foo',
118118
'binary': True,
119-
'data': [{'_placeholder': True, 'num': 0}, 'YmFy'],
119+
'data': [[{'_placeholder': True, 'num': 0}], 'YmFy'],
120120
'namespace': '/',
121121
'room': None,
122122
'skip_sid': None,
@@ -130,7 +130,87 @@ def test_emit_bytearray(self):
130130
'method': 'emit',
131131
'event': 'foo',
132132
'binary': True,
133-
'data': [{'foo': {'_placeholder': True, 'num': 0}}, 'YmFy'],
133+
'data': [[{'foo': {'_placeholder': True, 'num': 0}}], 'YmFy'],
134+
'namespace': '/',
135+
'room': None,
136+
'skip_sid': None,
137+
'callback': None,
138+
'host_id': '123456',
139+
}
140+
)
141+
142+
def test_emit_list(self):
143+
self.pm.emit('foo', [1, 'two'])
144+
self.pm._publish.assert_called_once_with(
145+
{
146+
'method': 'emit',
147+
'event': 'foo',
148+
'binary': False,
149+
'data': [[1, 'two']],
150+
'namespace': '/',
151+
'room': None,
152+
'skip_sid': None,
153+
'callback': None,
154+
'host_id': '123456',
155+
}
156+
)
157+
self.pm.emit('foo', [1, b'two', 'three'])
158+
self.pm._publish.assert_called_with(
159+
{
160+
'method': 'emit',
161+
'event': 'foo',
162+
'binary': True,
163+
'data': [
164+
[[1, {'_placeholder': True, 'num': 0}, 'three']], 'dHdv',
165+
],
166+
'namespace': '/',
167+
'room': None,
168+
'skip_sid': None,
169+
'callback': None,
170+
'host_id': '123456',
171+
}
172+
)
173+
174+
def test_emit_no_arguments(self):
175+
self.pm.emit('foo', ())
176+
self.pm._publish.assert_called_once_with(
177+
{
178+
'method': 'emit',
179+
'event': 'foo',
180+
'binary': False,
181+
'data': [],
182+
'namespace': '/',
183+
'room': None,
184+
'skip_sid': None,
185+
'callback': None,
186+
'host_id': '123456',
187+
}
188+
)
189+
190+
def test_emit_multiple_arguments(self):
191+
self.pm.emit('foo', (1, 'two'))
192+
self.pm._publish.assert_called_once_with(
193+
{
194+
'method': 'emit',
195+
'event': 'foo',
196+
'binary': False,
197+
'data': [1, 'two'],
198+
'namespace': '/',
199+
'room': None,
200+
'skip_sid': None,
201+
'callback': None,
202+
'host_id': '123456',
203+
}
204+
)
205+
self.pm.emit('foo', (1, b'two', 'three'))
206+
self.pm._publish.assert_called_with(
207+
{
208+
'method': 'emit',
209+
'event': 'foo',
210+
'binary': True,
211+
'data': [
212+
[1, {'_placeholder': True, 'num': 0}, 'three'], 'dHdv',
213+
],
134214
'namespace': '/',
135215
'room': None,
136216
'skip_sid': None,
@@ -147,7 +227,7 @@ def test_emit_with_to(self):
147227
'method': 'emit',
148228
'event': 'foo',
149229
'binary': False,
150-
'data': 'bar',
230+
'data': ['bar'],
151231
'namespace': '/',
152232
'room': sid,
153233
'skip_sid': None,
@@ -163,7 +243,7 @@ def test_emit_with_namespace(self):
163243
'method': 'emit',
164244
'event': 'foo',
165245
'binary': False,
166-
'data': 'bar',
246+
'data': ['bar'],
167247
'namespace': '/baz',
168248
'room': None,
169249
'skip_sid': None,
@@ -179,7 +259,7 @@ def test_emit_with_room(self):
179259
'method': 'emit',
180260
'event': 'foo',
181261
'binary': False,
182-
'data': 'bar',
262+
'data': ['bar'],
183263
'namespace': '/',
184264
'room': 'baz',
185265
'skip_sid': None,
@@ -195,7 +275,7 @@ def test_emit_with_skip_sid(self):
195275
'method': 'emit',
196276
'event': 'foo',
197277
'binary': False,
198-
'data': 'bar',
278+
'data': ['bar'],
199279
'namespace': '/',
200280
'room': None,
201281
'skip_sid': 'baz',
@@ -214,7 +294,7 @@ def test_emit_with_callback(self):
214294
'method': 'emit',
215295
'event': 'foo',
216296
'binary': False,
217-
'data': 'bar',
297+
'data': ['bar'],
218298
'namespace': '/',
219299
'room': 'baz',
220300
'skip_sid': None,
@@ -305,6 +385,18 @@ def test_close_room_with_namespace(self):
305385
)
306386

307387
def test_handle_emit(self):
388+
with mock.patch.object(manager.Manager, 'emit') as super_emit:
389+
self.pm._handle_emit({'event': 'foo', 'data': ['bar']})
390+
super_emit.assert_called_once_with(
391+
'foo',
392+
'bar',
393+
namespace=None,
394+
room=None,
395+
skip_sid=None,
396+
callback=None,
397+
)
398+
399+
def test_handle_legacy_emit(self):
308400
with mock.patch.object(manager.Manager, 'emit') as super_emit:
309401
self.pm._handle_emit({'event': 'foo', 'data': 'bar'})
310402
super_emit.assert_called_once_with(
@@ -317,6 +409,35 @@ def test_handle_emit(self):
317409
)
318410

319411
def test_handle_emit_binary(self):
412+
with mock.patch.object(manager.Manager, 'emit') as super_emit:
413+
self.pm._handle_emit({
414+
'event': 'foo',
415+
'binary': True,
416+
'data': [[{'_placeholder': True, 'num': 0}], 'YmFy'],
417+
})
418+
super_emit.assert_called_once_with(
419+
'foo',
420+
b'bar',
421+
namespace=None,
422+
room=None,
423+
skip_sid=None,
424+
callback=None,
425+
)
426+
self.pm._handle_emit({
427+
'event': 'foo',
428+
'binary': True,
429+
'data': [[{'foo': {'_placeholder': True, 'num': 0}}], 'YmFy'],
430+
})
431+
super_emit.assert_called_with(
432+
'foo',
433+
{'foo': b'bar'},
434+
namespace=None,
435+
room=None,
436+
skip_sid=None,
437+
callback=None,
438+
)
439+
440+
def test_handle_legacy_emit_binary(self):
320441
with mock.patch.object(manager.Manager, 'emit') as super_emit:
321442
self.pm._handle_emit({
322443
'event': 'foo',
@@ -345,6 +466,45 @@ def test_handle_emit_binary(self):
345466
callback=None,
346467
)
347468

469+
def test_handle_emit_no_arguments(self):
470+
with mock.patch.object(manager.Manager, 'emit') as super_emit:
471+
self.pm._handle_emit({'event': 'foo', 'data': []})
472+
super_emit.assert_called_once_with(
473+
'foo',
474+
(),
475+
namespace=None,
476+
room=None,
477+
skip_sid=None,
478+
callback=None,
479+
)
480+
481+
def test_handle_emit_multiple_arguments(self):
482+
with mock.patch.object(manager.Manager, 'emit') as super_emit:
483+
self.pm._handle_emit({'event': 'foo', 'data': [1, 'two']})
484+
super_emit.assert_called_once_with(
485+
'foo',
486+
(1, 'two'),
487+
namespace=None,
488+
room=None,
489+
skip_sid=None,
490+
callback=None,
491+
)
492+
self.pm._handle_emit({
493+
'event': 'foo',
494+
'binary': True,
495+
'data': [
496+
[1, {'_placeholder': True, 'num': 0}, 'three'], 'dHdv'
497+
],
498+
})
499+
super_emit.assert_called_with(
500+
'foo',
501+
(1, b'two', 'three'),
502+
namespace=None,
503+
room=None,
504+
skip_sid=None,
505+
callback=None,
506+
)
507+
348508
def test_handle_emit_with_namespace(self):
349509
with mock.patch.object(manager.Manager, 'emit') as super_emit:
350510
self.pm._handle_emit(

0 commit comments

Comments
 (0)