def test_MRequest(self): msg = MRequest() msg.function = "foo" msg.arguments = [] self.assertEqual(msg.pack(), msgpack.packb([0, msg._msgid, "foo", []])) with self.assertRaises(InvalidMessageError): msg.arguments = None msg.pack() with self.assertRaises(InvalidMessageError): msg.function = None msg.arguments = [] msg.pack() pass
def test_send(self): rpc = MsgpackRpc() con_send_mock = mocks.rpc_connection_send(rpc) m1 = MRequest() with self.assertRaises(InvalidMessageError): rpc.send(m1) m1.function = "run" m1.arguments = [] rpc.send(m1) self.assertIsNone(rpc._response_callbacks.get(m1.get_msgid())) con_send_mock.assert_called_once_with(m1.pack()) con_send_mock.reset_mock() def r_cb(): pass rpc.send(m1, r_cb) self.assertEqual(rpc._response_callbacks[m1.get_msgid()], r_cb) con_send_mock.side_effect = BrokenPipeError() with self.assertRaises(BrokenPipeError): rpc.send(m1) with self.assertRaises(InvalidMessageError): rpc.send(None)
def test_message_callback(self): rpc = MsgpackRpc() mock_send = mocks.rpc_send(rpc) dispatch = mocks.rpc_dispatch(rpc, "run") m_req = MRequest() m_req.function = "run" m_req.arguments = [] dispatch.return_value = (None, []) rpc._message_callback(m_req.pack()) dispatch.assert_called_once_with(m_req) handle_response = mocks.rpc_handle_response(rpc) m_res = MResponse(1) m_res.response = [] rpc._message_callback(m_res.pack()) handle_response.assert_called_once_with(m_res) handle_notify = mocks.rpc_handle_notify(rpc) m_not = MNotify("test", []) rpc._message_callback(m_not.pack()) handle_notify.assert_called_once_with(m_not) handle_notify.side_effect = InvalidMessageError("not") handle_response.side_effect = InvalidMessageError("res") dispatch.side_effect = InvalidMessageError("req") rpc._message_callback(m_req.pack()) self.assertEqual(mock_send.call_args[0][0].error[1], "Could not handle request! req") rpc._message_callback(msgpack.packb(["hi"])) self.assertEqual(mock_send.call_args[0][0].error[0], 400) # handle unexpected exception dispatch.side_effect = TypeError() rpc._message_callback(m_req.pack()) self.assertEqual(mock_send.call_args[0][0].error[1], "Unexpected exception occurred!")