|
1 | 1 | import functools
|
2 | 2 | import logging
|
| 3 | +import pickle |
| 4 | +import json |
| 5 | +import marshal |
3 | 6 | import unittest
|
4 | 7 | from unittest import mock
|
5 | 8 |
|
@@ -365,8 +368,6 @@ def test_background_thread(self):
|
365 | 368 | self.pm._handle_close_room = mock.MagicMock()
|
366 | 369 |
|
367 | 370 | def messages():
|
368 |
| - import pickle |
369 |
| - |
370 | 371 | yield {'method': 'emit', 'value': 'foo'}
|
371 | 372 | yield {'missing': 'method'}
|
372 | 373 | yield '{"method": "callback", "value": "bar"}'
|
@@ -394,3 +395,46 @@ def messages():
|
394 | 395 | self.pm._handle_close_room.assert_called_once_with(
|
395 | 396 | {'method': 'close_room', 'value': 'baz'}
|
396 | 397 | )
|
| 398 | + |
| 399 | + def test_background_thread_with_encoder(self): |
| 400 | + mock_server = mock.MagicMock() |
| 401 | + pm = pubsub_manager.PubSubManager(encoder=marshal) |
| 402 | + pm.set_server(mock_server) |
| 403 | + pm._publish = mock.MagicMock() |
| 404 | + pm._handle_emit = mock.MagicMock() |
| 405 | + pm._handle_callback = mock.MagicMock() |
| 406 | + pm._handle_disconnect = mock.MagicMock() |
| 407 | + pm._handle_close_room = mock.MagicMock() |
| 408 | + |
| 409 | + pm.initialize() |
| 410 | + |
| 411 | + def messages(): |
| 412 | + yield {'method': 'emit', 'value': 'foo'} |
| 413 | + yield marshal.dumps({'method': 'callback', 'value': 'bar'}) |
| 414 | + yield json.dumps( |
| 415 | + {'method': 'disconnect', 'sid': '123', 'namespace': '/foo'} |
| 416 | + ) |
| 417 | + yield pickle.dumps({'method': 'close_room', 'value': 'baz'}) |
| 418 | + yield {'method': 'bogus'} |
| 419 | + yield 'bad json' |
| 420 | + yield b'bad encoding' |
| 421 | + |
| 422 | + pm._listen = mock.MagicMock(side_effect=messages) |
| 423 | + |
| 424 | + try: |
| 425 | + pm._thread() |
| 426 | + except StopIteration: |
| 427 | + pass |
| 428 | + |
| 429 | + pm._handle_emit.assert_called_once_with( |
| 430 | + {'method': 'emit', 'value': 'foo'} |
| 431 | + ) |
| 432 | + pm._handle_callback.assert_called_once_with( |
| 433 | + {'method': 'callback', 'value': 'bar'} |
| 434 | + ) |
| 435 | + pm._handle_disconnect.assert_called_once_with( |
| 436 | + {'method': 'disconnect', 'sid': '123', 'namespace': '/foo'} |
| 437 | + ) |
| 438 | + pm._handle_close_room.assert_called_once_with( |
| 439 | + {'method': 'close_room', 'value': 'baz'} |
| 440 | + ) |
0 commit comments