def test_validateAndExecuteRequest(self): '''Test a request for an existing service and method.''' # Add a test service service = self.registerTestService() # Define an RPC request for an existing service and method self.rpc_request.service_name = service.DESCRIPTOR.full_name self.rpc_request.method_name = "TestMethod" # Serialize the request (bytestream, sock) = self.serializeRpcRequestToSocket() # Construct the expected response from the service expected_response = test_pb2.Response() expected_response.str_data = service.response_str_data expected_response.int_data = service.response_int_data serialized_payload = expected_response.SerializeToString() expected_rpc = rpc_pb2.Response() expected_rpc.callback = True expected_rpc.response_proto = serialized_payload # Run the method on the server handler = server.SocketHandler(sock, self.client_addr, self.server_addr, self.testserver) received_rpc = handler.validateAndExecuteRequest(bytestream) # Check the response message error code self.assertEqual(received_rpc, expected_rpc, 'Normal response')
def test_callMethod(self): ''' Test normal return for callMethod ''' # Get the service and method service = self.registerTestService() method = service.DESCRIPTOR.FindMethodByName("TestMethod") # Define an RPC request for an existing service and method self.rpc_request.service_name = service.DESCRIPTOR.full_name self.rpc_request.method_name = "TestMethod" # Serialize the request (bytestream, sock) = self.serializeRpcRequestToSocket() # Construct the expected response from the service expected_response = test_pb2.Response() expected_response.str_data = service.response_str_data expected_response.int_data = service.response_int_data serialized_payload = expected_response.SerializeToString() # Wrap the expected response in an RPC Response message expected_rpc = rpc_pb2.Response() expected_rpc.callback = True expected_rpc.response_proto = serialized_payload handler = server.SocketHandler(sock, self.client_addr, self.server_addr, self.testserver) response = handler.callMethod(service, method, self.service_request) self.assertEquals(response, expected_rpc, 'Normal response')
def test_tryToRunCallback(self): '''Test tryToRunCallback - normal usage.''' callback = FakeCallback() self.lc.rpcResponse = rpc_pb2.Response() self.assertEquals(self.lc.tryToRunCallback(callback), None, "tryToRunCallback - normal usage")
def handleError(self, e): '''Produce an RPC response to convey a server error to the client.''' msg = "%d : %s" % (e.rpc_error_code, e.message) log.error(msg) # Create error reply response = rpc_pb.Response() response.error_reason = e.rpc_error_code response.error = e.message return response
def callMethod(self, service, method, proto_request): '''Execute a service method request.''' log.debug('Calling service %s' % service) log.debug('Calling method %s' % method) # Create the controller (initialised to success) and callback controller = SocketRpcController() callback = Callback() try: service.CallMethod(method, controller, proto_request, callback) except Exception, e: raise error.RpcError(unicode(e)) # Return an RPC response, with payload defined in the callback response = rpc_pb.Response() if callback.response: response.callback = True response.response_proto = callback.response.SerializeToString() else: response.callback = callback.invoked # Check to see if controller has been set to not success by user. if controller.failed(): response.error = controller.error() response.error_reason = rpc_pb.RPC_FAILED return response def handleError(self, e): '''Produce an RPC response to convey a server error to the client.'''
def withNoResponse(self, callback): rpcResponse = rpc_pb2.Response() rpcResponse.callback = callback self.input_stream.stream_data = rpcResponse.SerializeToString()
def withResponseProto(self, responseproto): rpcResponse = rpc_pb2.Response() rpcResponse.callback = True rpcResponse.response_proto = responseproto.SerializeToString() self.input_stream.stream_data = rpcResponse.SerializeToString()