class TestTransportLayerMessageHandling(unittest.TestCase): def setUp(self): self.tl = TransportLayer(1, 'localhost', None, 1) # The ok message should not trigger any callbacks def test_on_message_ok(self): self.tl.trigger_callbacks = mock.MagicMock( side_effect=AssertionError() ) self.tl._on_message(protocol.ok()) # Any non-ok message should cause trigger_callbacks to be called with # the type of message and the message object (dict) def test_on_message_not_ok(self): data = protocol.shout({}) self.tl.trigger_callbacks = mock.MagicMock() self.tl._on_message(data) self.tl.trigger_callbacks.assert_called_with(data['type'], data) # Invalid serialized messages should be dropped def test_on_raw_message_invalid(self): self.tl._init_peer = mock.MagicMock() self.tl._on_message = mock.MagicMock() self.tl._on_raw_message('invalid serialization') self.assertFalse(self.tl._init_peer.called) self.assertFalse(self.tl._on_message.called) # A hello message with no uri should not add a peer def test_on_raw_message_hello_no_uri(self): self.tl._on_raw_message([json.dumps(protocol.hello_request({}))]) self.assertEqual(0, len(self.tl.peers)) # A hello message with a uri should result in a new peer def test_on_raw_message_hello_with_uri(self): request = protocol.hello_request({ 'uri': 'tcp://localhost:12345' }) self.tl._on_raw_message([json.dumps(request)]) self.assertEqual(1, len(self.tl.peers))
class TestTransportLayerMessageHandling(unittest.TestCase): def setUp(self): self.tl = TransportLayer(1, 'localhost', None, 1) # The ok message should not trigger any callbacks def test_on_message_ok(self): self.tl.trigger_callbacks = mock.MagicMock( side_effect=AssertionError()) self.tl._on_message(protocol.ok()) # Any non-ok message should cause trigger_callbacks to be called with # the type of message and the message object (dict) def test_on_message_not_ok(self): data = protocol.shout({}) self.tl.trigger_callbacks = mock.MagicMock() self.tl._on_message(data) self.tl.trigger_callbacks.assert_called_with(data['type'], data) # Invalid serialized messages should be dropped def test_on_raw_message_invalid(self): self.tl._init_peer = mock.MagicMock() self.tl._on_message = mock.MagicMock() self.tl._on_raw_message('invalid serialization') self.assertFalse(self.tl._init_peer.called) self.assertFalse(self.tl._on_message.called) # A hello message with no uri should not add a peer def test_on_raw_message_hello_no_uri(self): self.tl._on_raw_message([json.dumps(protocol.hello_request({}))]) self.assertEqual(0, len(self.tl.peers)) # A hello message with a uri should result in a new peer def test_on_raw_message_hello_with_uri(self): request = protocol.hello_request({'uri': 'tcp://localhost:12345'}) self.tl._on_raw_message([json.dumps(request)]) self.assertEqual(1, len(self.tl.peers))