@@ -163,13 +163,15 @@ def test_can_disconnect(self):
163
163
assert _run (self .pm .can_disconnect (sid , '/' )) is True
164
164
_run (self .pm .can_disconnect (sid , '/foo' ))
165
165
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' }
167
168
)
168
169
169
170
def test_disconnect (self ):
170
171
_run (self .pm .disconnect ('foo' , '/' ))
171
172
self .pm ._publish .mock .assert_called_once_with (
172
- {'method' : 'disconnect' , 'sid' : 'foo' , 'namespace' : '/' }
173
+ {'method' : 'disconnect' , 'sid' : 'foo' , 'namespace' : '/' ,
174
+ 'host_id' : '123456' }
173
175
)
174
176
175
177
def test_disconnect_ignore_queue (self ):
@@ -182,13 +184,15 @@ def test_disconnect_ignore_queue(self):
182
184
def test_close_room (self ):
183
185
_run (self .pm .close_room ('foo' ))
184
186
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' }
186
189
)
187
190
188
191
def test_close_room_with_namespace (self ):
189
192
_run (self .pm .close_room ('foo' , '/bar' ))
190
193
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' }
192
196
)
193
197
194
198
def test_handle_emit (self ):
@@ -263,8 +267,7 @@ def test_handle_emit_with_skip_sid(self):
263
267
callback = None ,
264
268
)
265
269
266
- def test_handle_emit_with_callback (self ):
267
- host_id = self .pm .host_id
270
+ def test_handle_emit_with_remote_callback (self ):
268
271
with mock .patch .object (
269
272
asyncio_manager .AsyncManager , 'emit' , new = AsyncMock ()
270
273
) as super_emit :
@@ -275,7 +278,7 @@ def test_handle_emit_with_callback(self):
275
278
'data' : 'bar' ,
276
279
'namespace' : '/baz' ,
277
280
'callback' : ('sid' , '/baz' , 123 ),
278
- 'host_id' : '123456 ' ,
281
+ 'host_id' : 'x ' ,
279
282
}
280
283
)
281
284
)
@@ -291,14 +294,40 @@ def test_handle_emit_with_callback(self):
291
294
self .pm ._publish .mock .assert_called_once_with (
292
295
{
293
296
'method' : 'callback' ,
294
- 'host_id' : host_id ,
297
+ 'host_id' : 'x' ,
295
298
'sid' : 'sid' ,
296
299
'namespace' : '/baz' ,
297
300
'id' : 123 ,
298
301
'args' : ('one' , 2 , 'three' ),
299
302
}
300
303
)
301
304
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
+
302
331
def test_handle_callback (self ):
303
332
host_id = self .pm .host_id
304
333
with mock .patch .object (
@@ -419,50 +448,66 @@ def test_background_thread(self):
419
448
self .pm ._handle_callback = AsyncMock ()
420
449
self .pm ._handle_disconnect = AsyncMock ()
421
450
self .pm ._handle_close_room = AsyncMock ()
451
+ host_id = self .pm .host_id
422
452
423
453
async def messages ():
424
454
import pickle
425
455
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' })
432
464
yield 'bad json'
433
465
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 })
434
475
raise asyncio .CancelledError () # force the thread to exit
435
476
436
477
self .pm ._listen = messages
437
478
_run (self .pm ._thread ())
438
479
439
480
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' }
441
485
)
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 }
444
488
)
445
489
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' }
447
492
)
448
493
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' }
450
495
)
451
496
452
497
def test_background_thread_exception (self ):
453
498
self .pm ._handle_emit = AsyncMock (side_effect = [ValueError (),
454
499
asyncio .CancelledError ])
455
500
456
501
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' }
459
504
460
505
self .pm ._listen = messages
461
506
_run (self .pm ._thread ())
462
507
463
508
self .pm ._handle_emit .mock .assert_any_call (
464
- {'method' : 'emit' , 'value' : 'foo' }
509
+ {'method' : 'emit' , 'value' : 'foo' , 'host_id' : 'x' }
465
510
)
466
511
self .pm ._handle_emit .mock .assert_called_with (
467
- {'method' : 'emit' , 'value' : 'bar' }
512
+ {'method' : 'emit' , 'value' : 'bar' , 'host_id' : 'x' }
468
513
)
0 commit comments