def test_to_msgpack(self): """ Simple good case. """ result = to_msgpack(self.mock_message) unpacked = msgpack.unpackb(result) for k in ['uuid', 'key', 'value', 'timestamp', 'public_key', 'name', 'meta', 'sig', 'version', 'message']: self.assertIn(k, unpacked.keys()) self.assertEqual(unpacked[k], getattr(self, k))
def test_send_message(self): """ Ensure the message passed into the protocol is turned into a netstring version of a msgpacked message. """ # Create a simple Ping message uuid = str(uuid4()) version = get_version() msg = Pong(uuid, self.node_id, version) # Send it down the wire... self.protocol.sendMessage(msg) # Check it's as expected. expected = self._to_netstring(to_msgpack(msg)) actual = self.transport.value() self.assertEqual(expected, actual)
def test_string_received_good_message(self): """ Ensures the correct method on the local node object is called with the expected arguments. """ # Mock self.node.message_received = MagicMock(return_value=True) # Create a simple Ping message uuid = str(uuid4()) version = get_version() msg = Pong(uuid, self.node_id, version) # Receive it... raw = to_msgpack(msg) self.protocol.stringReceived(raw) # Check it results in a call to the node's message_received method. self.node.message_received.assert_called_once_with(msg, self.protocol)
def test_from_msgpack_value(self): """ Ensures a valid value message is correctly parsed. """ mock_message = to_msgpack(self.mock_message) result = from_msgpack(mock_message) self.assertIsInstance(result, Value) self.assertEqual(result.uuid, self.uuid) self.assertEqual(result.node, self.node) self.assertEqual(result.key, self.key) self.assertEqual(result.value, self.value) self.assertEqual(result.timestamp, self.timestamp) self.assertEqual(result.public_key, self.public_key) self.assertEqual(result.name, self.name) self.assertEqual(result.meta, self.meta) self.assertEqual(result.sig, self.sig) self.assertEqual(result.version, self.version)
def test_send_message_with_lose_connection(self): """ This check ensures the loseConnection method has been called on the protocol's transport when the appropriate flag has been passed in. """ # Mock self.transport.loseConnection = MagicMock() # Create a simple Ping message uuid = str(uuid4()) version = get_version() msg = Pong(uuid, self.node_id, version) # Send it down the wire with the loseConnection flag set to True self.protocol.sendMessage(msg, True) # Check it's as expected. expected = self._to_netstring(to_msgpack(msg)) actual = self.transport.value() self.assertEqual(expected, actual) # Ensure the loseConnection method was also called. self.transport.loseConnection.assert_called_once_with()
def test_send_message_no_lose_connection(self): """ Ensures that a the default for sending a message does NOT result in the connection being lost. """ # Mock self.transport.loseConnection = MagicMock() # Create a simple Ping message uuid = str(uuid4()) version = get_version() msg = Pong(uuid, self.node_id, version) # Send it down the wire... self.protocol.sendMessage(msg) # Check it's as expected. expected = self._to_netstring(to_msgpack(msg)) actual = self.transport.value() self.assertEqual(expected, actual) # Ensure the loseConnection method was not called. self.assertEqual(0, len(self.transport.loseConnection.mock_calls))