Skip to content

Commit b3fc842

Browse files
properly handle callbacks in multi-host configurations for asyncio
1 parent 8d7059a commit b3fc842

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

socketio/asyncio_pubsub_manager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ async def emit(self, event, data, namespace=None, room=None, skip_sid=None,
6565
callback = None
6666
await self._publish({'method': 'emit', 'event': event, 'data': data,
6767
'namespace': namespace, 'room': room,
68-
'skip_sid': skip_sid, 'callback': callback})
68+
'skip_sid': skip_sid, 'callback': callback,
69+
'host_id': self.host_id})
6970

7071
async def close_room(self, room, namespace=None):
7172
await self._publish({'method': 'close_room', 'room': room,
@@ -95,8 +96,9 @@ async def _handle_emit(self, message):
9596
# Here in the receiving end we set up a local callback that preserves
9697
# the callback host and id from the sender
9798
remote_callback = message.get('callback')
99+
remote_host_id = message.get('host_id')
98100
if remote_callback is not None and len(remote_callback) == 3:
99-
callback = partial(self._return_callback, self.host_id,
101+
callback = partial(self._return_callback, remote_host_id,
100102
*remote_callback)
101103
else:
102104
callback = None

tests/test_asyncio_pubsub_manager.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ def setUp(self):
4444
self.pm = asyncio_pubsub_manager.AsyncPubSubManager()
4545
self.pm._publish = AsyncMock()
4646
self.pm.set_server(mock_server)
47+
self.pm.host_id = '123456'
4748
self.pm.initialize()
4849

4950
def test_default_init(self):
5051
self.assertEqual(self.pm.channel, 'socketio')
51-
self.assertEqual(len(self.pm.host_id), 32)
5252
self.pm.server.start_background_task.assert_called_once_with(
5353
self.pm._thread)
5454

@@ -71,28 +71,28 @@ def test_emit(self):
7171
self.pm._publish.mock.assert_called_once_with(
7272
{'method': 'emit', 'event': 'foo', 'data': 'bar',
7373
'namespace': '/', 'room': None, 'skip_sid': None,
74-
'callback': None})
74+
'callback': None, 'host_id': '123456'})
7575

7676
def test_emit_with_namespace(self):
7777
_run(self.pm.emit('foo', 'bar', namespace='/baz'))
7878
self.pm._publish.mock.assert_called_once_with(
7979
{'method': 'emit', 'event': 'foo', 'data': 'bar',
8080
'namespace': '/baz', 'room': None, 'skip_sid': None,
81-
'callback': None})
81+
'callback': None, 'host_id': '123456'})
8282

8383
def test_emit_with_room(self):
8484
_run(self.pm.emit('foo', 'bar', room='baz'))
8585
self.pm._publish.mock.assert_called_once_with(
8686
{'method': 'emit', 'event': 'foo', 'data': 'bar',
8787
'namespace': '/', 'room': 'baz', 'skip_sid': None,
88-
'callback': None})
88+
'callback': None, 'host_id': '123456'})
8989

9090
def test_emit_with_skip_sid(self):
9191
_run(self.pm.emit('foo', 'bar', skip_sid='baz'))
9292
self.pm._publish.mock.assert_called_once_with(
9393
{'method': 'emit', 'event': 'foo', 'data': 'bar',
9494
'namespace': '/', 'room': None, 'skip_sid': 'baz',
95-
'callback': None})
95+
'callback': None, 'host_id': '123456'})
9696

9797
def test_emit_with_callback(self):
9898
with mock.patch.object(self.pm, '_generate_ack_id',
@@ -101,7 +101,7 @@ def test_emit_with_callback(self):
101101
self.pm._publish.mock.assert_called_once_with(
102102
{'method': 'emit', 'event': 'foo', 'data': 'bar',
103103
'namespace': '/', 'room': 'baz', 'skip_sid': None,
104-
'callback': ('baz', '/', '123')})
104+
'callback': ('baz', '/', '123'), 'host_id': '123456'})
105105

106106
def test_emit_with_callback_without_server(self):
107107
standalone_pm = asyncio_pubsub_manager.AsyncPubSubManager()
@@ -173,7 +173,8 @@ def test_handle_emit_with_callback(self):
173173
new=AsyncMock()) as super_emit:
174174
_run(self.pm._handle_emit({'event': 'foo', 'data': 'bar',
175175
'namespace': '/baz',
176-
'callback': ('sid', '/baz', 123)}))
176+
'callback': ('sid', '/baz', 123),
177+
'host_id': '123456'}))
177178
self.assertEqual(super_emit.mock.call_count, 1)
178179
self.assertEqual(super_emit.mock.call_args[0],
179180
(self.pm, 'foo', 'bar'))

0 commit comments

Comments
 (0)