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 DataStreaming(self, request_iterator, context): print("connect to client...") with open('image_test/test.jpg', 'wb') as f: try: for i in request_iterator: f.write(i.imageData) audio_id = i.audio_id except Exception as e: print(e) f.close() path = "image_test/test.jpg" text = imageToCaption(path) text = text.replace('UNK', '') print(text) data = ttsModule.start(audio_id, text) for i in data: yield test_pb2.Response(audioData = i) for i in range(0,1): yield test_pb2.Response(resultText = text)
def TestMethod(self, controller, request, done): # Raise an exception if one has been passed in if self.exception: raise self.exception # Create a reply response = test_pb2.Response() response.str_data = self.response_str_data response.int_data = self.response_int_data # Simulate a user specified failure if self.failmsg: controller.handleError(-1, self.failmsg) # As per specification, call the run method of the done callback done.run(response)
def setUp(self): # Create a channel connected to a fake socket self.factory = FakeSocketFactory() self.channel = ch.SocketRpcChannel(socketFactory=self.factory) # Define a simple service request self.service_request = test_pb2.Request() self.service_request.str_data = 'The lord giveth' self.serialized_request = self.service_request.SerializeToString() # Define a service response self.service_response = test_pb2.Response() self.service_response.str_data = 'And the lord taketh away' self.serialized_response = \ self.service_response.SerializePartialToString() # Define an RPC request with the service request as payload self.rpc_request = rpc_pb2.Request() self.rpc_request.request_proto = self.serialized_request
def hello(self, request, context): return test_pb2.Response(data=request, statusMessage='OK', status=200)