@@ -163,13 +163,15 @@ def test_can_disconnect(self):
163163 assert _run (self .pm .can_disconnect (sid , '/' )) is True
164164 _run (self .pm .can_disconnect (sid , '/foo' ))
165165 self .pm ._publish .mock .assert_called_once_with (
166- {'method' : 'disconnect' , 'sid' : sid , 'namespace' : '/foo' }
166+ {'method' : 'disconnect' , 'sid' : sid , 'namespace' : '/foo' ,
167+ 'host_id' : '123456' }
167168 )
168169
169170 def test_disconnect (self ):
170171 _run (self .pm .disconnect ('foo' , '/' ))
171172 self .pm ._publish .mock .assert_called_once_with (
172- {'method' : 'disconnect' , 'sid' : 'foo' , 'namespace' : '/' }
173+ {'method' : 'disconnect' , 'sid' : 'foo' , 'namespace' : '/' ,
174+ 'host_id' : '123456' }
173175 )
174176
175177 def test_disconnect_ignore_queue (self ):
@@ -182,13 +184,15 @@ def test_disconnect_ignore_queue(self):
182184 def test_close_room (self ):
183185 _run (self .pm .close_room ('foo' ))
184186 self .pm ._publish .mock .assert_called_once_with (
185- {'method' : 'close_room' , 'room' : 'foo' , 'namespace' : '/' }
187+ {'method' : 'close_room' , 'room' : 'foo' , 'namespace' : '/' ,
188+ 'host_id' : '123456' }
186189 )
187190
188191 def test_close_room_with_namespace (self ):
189192 _run (self .pm .close_room ('foo' , '/bar' ))
190193 self .pm ._publish .mock .assert_called_once_with (
191- {'method' : 'close_room' , 'room' : 'foo' , 'namespace' : '/bar' }
194+ {'method' : 'close_room' , 'room' : 'foo' , 'namespace' : '/bar' ,
195+ 'host_id' : '123456' }
192196 )
193197
194198 def test_handle_emit (self ):
@@ -263,8 +267,7 @@ def test_handle_emit_with_skip_sid(self):
263267 callback = None ,
264268 )
265269
266- def test_handle_emit_with_callback (self ):
267- host_id = self .pm .host_id
270+ def test_handle_emit_with_remote_callback (self ):
268271 with mock .patch .object (
269272 asyncio_manager .AsyncManager , 'emit' , new = AsyncMock ()
270273 ) as super_emit :
@@ -275,7 +278,7 @@ def test_handle_emit_with_callback(self):
275278 'data' : 'bar' ,
276279 'namespace' : '/baz' ,
277280 'callback' : ('sid' , '/baz' , 123 ),
278- 'host_id' : '123456 ' ,
281+ 'host_id' : 'x ' ,
279282 }
280283 )
281284 )
@@ -291,14 +294,40 @@ def test_handle_emit_with_callback(self):
291294 self .pm ._publish .mock .assert_called_once_with (
292295 {
293296 'method' : 'callback' ,
294- 'host_id' : host_id ,
297+ 'host_id' : 'x' ,
295298 'sid' : 'sid' ,
296299 'namespace' : '/baz' ,
297300 'id' : 123 ,
298301 'args' : ('one' , 2 , 'three' ),
299302 }
300303 )
301304
305+ def test_handle_emit_with_local_callback (self ):
306+ with mock .patch .object (
307+ asyncio_manager .AsyncManager , 'emit' , new = AsyncMock ()
308+ ) as super_emit :
309+ _run (
310+ self .pm ._handle_emit (
311+ {
312+ 'event' : 'foo' ,
313+ 'data' : 'bar' ,
314+ 'namespace' : '/baz' ,
315+ 'callback' : ('sid' , '/baz' , 123 ),
316+ 'host_id' : self .pm .host_id ,
317+ }
318+ )
319+ )
320+ assert super_emit .mock .call_count == 1
321+ assert super_emit .mock .call_args [0 ] == (self .pm , 'foo' , 'bar' )
322+ assert super_emit .mock .call_args [1 ]['namespace' ] == '/baz'
323+ assert super_emit .mock .call_args [1 ]['room' ] is None
324+ assert super_emit .mock .call_args [1 ]['skip_sid' ] is None
325+ assert isinstance (
326+ super_emit .mock .call_args [1 ]['callback' ], functools .partial
327+ )
328+ _run (super_emit .mock .call_args [1 ]['callback' ]('one' , 2 , 'three' ))
329+ self .pm ._publish .mock .assert_not_called ()
330+
302331 def test_handle_callback (self ):
303332 host_id = self .pm .host_id
304333 with mock .patch .object (
@@ -419,50 +448,66 @@ def test_background_thread(self):
419448 self .pm ._handle_callback = AsyncMock ()
420449 self .pm ._handle_disconnect = AsyncMock ()
421450 self .pm ._handle_close_room = AsyncMock ()
451+ host_id = self .pm .host_id
422452
423453 async def messages ():
424454 import pickle
425455
426- yield {'method' : 'emit' , 'value' : 'foo' }
427- yield {'missing' : 'method' }
428- yield '{"method": "callback", "value": "bar"}'
429- yield {'method' : 'disconnect' , 'sid' : '123' , 'namespace' : '/foo' }
430- yield {'method' : 'bogus' }
431- yield pickle .dumps ({'method' : 'close_room' , 'value' : 'baz' })
456+ yield {'method' : 'emit' , 'value' : 'foo' , 'host_id' : 'x' }
457+ yield {'missing' : 'method' , 'host_id' : 'x' }
458+ yield '{"method": "callback", "value": "bar", "host_id": "x"}'
459+ yield {'method' : 'disconnect' , 'sid' : '123' , 'namespace' : '/foo' ,
460+ 'host_id' : 'x' }
461+ yield {'method' : 'bogus' , 'host_id' : 'x' }
462+ yield pickle .dumps ({'method' : 'close_room' , 'value' : 'baz' ,
463+ 'host_id' : 'x' })
432464 yield 'bad json'
433465 yield b'bad pickled'
466+
467+ # these should not publish anything on the queue, as they come from
468+ # the same host
469+ yield {'method' : 'emit' , 'value' : 'foo' , 'host_id' : host_id }
470+ yield {'method' : 'callback' , 'value' : 'bar' , 'host_id' : host_id }
471+ yield {'method' : 'disconnect' , 'sid' : '123' , 'namespace' : '/foo' ,
472+ 'host_id' : host_id }
473+ yield pickle .dumps ({'method' : 'close_room' , 'value' : 'baz' ,
474+ 'host_id' : host_id })
434475 raise asyncio .CancelledError () # force the thread to exit
435476
436477 self .pm ._listen = messages
437478 _run (self .pm ._thread ())
438479
439480 self .pm ._handle_emit .mock .assert_called_once_with (
440- {'method' : 'emit' , 'value' : 'foo' }
481+ {'method' : 'emit' , 'value' : 'foo' , 'host_id' : 'x' }
482+ )
483+ self .pm ._handle_callback .mock .assert_any_call (
484+ {'method' : 'callback' , 'value' : 'bar' , 'host_id' : 'x' }
441485 )
442- self .pm ._handle_callback .mock .assert_called_once_with (
443- {'method' : 'callback' , 'value' : 'bar' }
486+ self .pm ._handle_callback .mock .assert_any_call (
487+ {'method' : 'callback' , 'value' : 'bar' , 'host_id' : host_id }
444488 )
445489 self .pm ._handle_disconnect .mock .assert_called_once_with (
446- {'method' : 'disconnect' , 'sid' : '123' , 'namespace' : '/foo' }
490+ {'method' : 'disconnect' , 'sid' : '123' , 'namespace' : '/foo' ,
491+ 'host_id' : 'x' }
447492 )
448493 self .pm ._handle_close_room .mock .assert_called_once_with (
449- {'method' : 'close_room' , 'value' : 'baz' }
494+ {'method' : 'close_room' , 'value' : 'baz' , 'host_id' : 'x' }
450495 )
451496
452497 def test_background_thread_exception (self ):
453498 self .pm ._handle_emit = AsyncMock (side_effect = [ValueError (),
454499 asyncio .CancelledError ])
455500
456501 async def messages ():
457- yield {'method' : 'emit' , 'value' : 'foo' }
458- yield {'method' : 'emit' , 'value' : 'bar' }
502+ yield {'method' : 'emit' , 'value' : 'foo' , 'host_id' : 'x' }
503+ yield {'method' : 'emit' , 'value' : 'bar' , 'host_id' : 'x' }
459504
460505 self .pm ._listen = messages
461506 _run (self .pm ._thread ())
462507
463508 self .pm ._handle_emit .mock .assert_any_call (
464- {'method' : 'emit' , 'value' : 'foo' }
509+ {'method' : 'emit' , 'value' : 'foo' , 'host_id' : 'x' }
465510 )
466511 self .pm ._handle_emit .mock .assert_called_with (
467- {'method' : 'emit' , 'value' : 'bar' }
512+ {'method' : 'emit' , 'value' : 'bar' , 'host_id' : 'x' }
468513 )
0 commit comments