def testBadResponse(self): '''Test bad response from server.''' # Fake socket with prepared response socket = FakeSocket() socket.withInputBytes("bad response") socketFactory = FakeSocketFactory() socketFactory.setSocket(socket) # Create channel channel = ch.SocketRpcChannel("host", -1, socketFactory) controller = channel.newController() # Create the service service = test_pb2.TestService_Stub(channel) # Call RPC method callback = FakeCallback() service.TestMethod(controller,self.service_request,callback) # Verify request was sent and bad response received self.assertFalse(callback.invoked,'Callback invoked') self.assertEquals(self.serialized_request, socket.getRequest().request_proto, 'Request protocol serialisation') self.assertTrue(controller.failed(),'Controller failed') self.assertEquals(rpc_pb2.BAD_RESPONSE_PROTO, controller.reason, 'Controller reason')
def testBadResponse(self): '''Test bad response from server.''' # Fake socket with prepared response socket = FakeSocket() socket.withInputBytes("bad response") socketFactory = FakeSocketFactory() socketFactory.setSocket(socket) # Create channel channel = ch.SocketRpcChannel("host", -1, socketFactory) # Create the service service = test_pb2.TestService_Stub(channel) # Call RPC method controller = channel.newController() callback = FakeCallback() service.TestMethod(controller, self.service_request, callback) # Verify request was sent and bad response received self.assertTrue(callback.invoked, 'Callback invoked') self.assertEquals(self.serialized_request, socket.getRequest().request_proto, 'Request protocol serialisation') self.assertTrue(controller.failed(), 'Controller failed') self.assertTrue(callback.response is None, 'Response') self.assertEquals(rpc_pb2.BAD_RESPONSE_PROTO, controller.reason, 'Controller reason') # Call Blocking RPC controller = channel.newController() try: service.TestMethod(controller, self.service_request, None) self.fail('Should have thrown error') except RpcError: self.assertTrue(controller.failed()) self.assertEquals(rpc_pb2.BAD_RESPONSE_PROTO, controller.reason, 'Error reason')
class Test__LifeCycle(unittest.TestCase): '''Unit tests for the protobuf.channel._Lifecycle class.''' def setUp(self): # Create a channel connected to a fake socket self.factory = FakeSocketFactory() self.socket = FakeSocket() self.channel = ch.SocketRpcChannel(socketFactory = self.factory) self.controller = self.channel.newController() self.lc = ch._LifeCycle(self.controller, self.channel) self.factory.setSocket(self.socket) # 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 an RPC request with the service request as payload self.rpc_request = rpc_pb2.Request() self.rpc_request.request_proto = self.serialized_request def tearDown(self): pass def test___init__(self): '''Test _LifeCycle constructor.''' self.assertEqual(self.lc.controller, self.controller, "Attribute 'controller' incorrectly initialized") self.assertEqual(self.lc.channel, self.channel, "Attribute 'channel' incorrectly initialized") self.assertEqual(self.lc.sock, None, "Attribute 'sock' incorrectly initialized") self.assertEqual(self.lc.byte_stream, None, "Attribute 'byte_stream' incorrectly initialized") self.assertEqual(self.lc.rpcResponse, None, "Attribute 'rpcResponse' incorrectly initialized") self.assertEqual(self.lc.serviceResponse, None, "Attribute 'serviceResponse' incorrectly initialized") def test_tryToValidateRequest(self): '''Test tryToValidateRequest - normal usage.''' self.assertEquals(self.lc.tryToValidateRequest(self.rpc_request), None, "tryToValidateRequest - valid request") def test_tryToValidateRequest_con_error(self): '''Test tryToValidateRequest - controller in error state.''' self.controller.success = False self.assertEquals(self.lc.tryToValidateRequest(self.rpc_request), None, "tryToValidateRequest - controller in error state") def test_tryToValidateRequest_BAD_REQUEST_PROTO(self): '''Test tryToValidateRequest - BadRequestProto error thrown.''' # A request with mandatory fields missing self.rpc_request = rpc_pb2.Request() self.lc.tryToValidateRequest(self.rpc_request) self.assertEquals(self.controller.reason, rpc_pb2.BAD_REQUEST_PROTO, "tryToValidateRequest - invalid request") self.assertEquals(self.controller.failed(), True, "tryToValidateRequest - invalid request") def test_tryToOpenSocket(self): '''Test tryToOpenSocket - normal usage.''' self.lc.tryToOpenSocket() self.assert_(self.lc.sock) def test_tryToOpenSocket_con_error(self): '''Test tryToOpenSocket - controller in error state.''' self.controller.error = True self.lc.tryToOpenSocket() self.assertEquals(self.lc.sock, None, "tryToOpenSocket - controller in error state") def test_tryToOpenSocket_UNKNOWN_HOST(self): '''Test tryToOpenSocket - UnknownHost error thrown.''' self.socket.throwUnknownHostException() self.lc.tryToOpenSocket() self.assertEquals(self.lc.sock, None, "tryToOpenSocket - UNKNOWN_HOST error") self.assertEquals(self.controller.reason, rpc_pb2.UNKNOWN_HOST, "tryToOpenSocket - UNKNOWN_HOST error") self.assertEquals(self.controller.failed(), True, "tryToOpenSocket - UNKNOWN_HOST error") def test_tryToOpenSocket_IO_ERROR(self): '''Test tryToOpenSocket - IOError error thrown.''' self.socket.throwIOErrorException() self.lc.tryToOpenSocket() self.assertEquals(self.lc.sock, None, "tryToOpenSocket - IO_ERROR error") self.assertEquals(self.controller.reason, rpc_pb2.IO_ERROR, "tryToOpenSocket - IO_ERROR error") self.assertEquals(self.controller.failed(), True, "tryToOpenSocket - IO_ERROR error") def test_tryToSendRpcRequest(self): '''Test tryToSendRpcRequest - normal usage.''' # Instantiate the test service, and get a reference to the method method_name = 'TestMethod' service = TestServiceImpl() method = service.DESCRIPTOR.FindMethodByName(method_name) # Set the service and method names of the RPC request self.rpc_request.service_name = service.DESCRIPTOR.full_name self.rpc_request.method_name = method_name # Add the socket instance to the lifecycle object self.lc.sock = self.socket self.assertEquals(self.lc.tryToSendRpcRequest(method, self.rpc_request), None, "tryToSendRpcRequest - normal return") def test_tryToSendRpcRequest_IO_ERROR(self): '''Test tryToSendRpcRequest - IOError error thrown.''' # Instantiate the test service, and get a reference to the method method_name = 'TestMethod' service = TestServiceImpl() method = service.DESCRIPTOR.FindMethodByName(method_name) # Set the service and method names of the RPC request self.rpc_request.service_name = service.DESCRIPTOR.full_name self.rpc_request.method_name = method_name # Set the exception, and add the socket instance to the lifecycle object self.socket.throwIOErrorException() self.lc.sock = self.socket self.assertEquals(self.lc.tryToSendRpcRequest(method, self.rpc_request), None, "tryToSendRpcRequest - IO_ERROR") self.assertEquals(self.controller.reason, rpc_pb2.IO_ERROR, "tryToSendRpcRequest - IO_ERROR error") self.assertEquals(self.controller.failed(), True, "tryToSendRpcRequest - IO_ERROR error") def test_tryToReceiveReply(self): '''Test tryToReceiveReply - normal usage.''' # Add some data to the socket msg = 'Message from server' self.socket.withInputBytes(msg) self.lc.sock = self.socket self.assertEquals(self.lc.tryToReceiveReply(), None, "tryToReceiveReply - normal usage") # Verify the socket has been closed self.assert_(self.socket.input_stream.closed, "tryToReceiveReply - normal usage") def test_tryToReceiveReply_IOError(self): '''Test tryToReceiveReply - IOError thrown.''' # Add some data to the socket msg = 'Message from server' self.socket.withInputBytes(msg) self.socket.throwIOErrorException() self.lc.sock = self.socket self.assertEquals(self.lc.tryToReceiveReply(), None, "tryToReceiveReply - IO_ERROR error") self.assertEquals(self.controller.reason, rpc_pb2.IO_ERROR, "tryToReceiveReply - IO_ERROR error") self.assertEquals(self.controller.failed(), True, "tryToReceiveReply - IO_ERROR error") # Verify the socket has been closed self.assert_(self.socket.input_stream.closed, "tryToReceiveReply - IO_ERROR error") def test_tryToParseReply(self): '''Test tryToParseReply - normal usage.''' resp_class = rpc_pb2.Response expected_response = resp_class() self.lc.byte_stream = expected_response.SerializeToString() self.assertEquals(self.lc.tryToParseReply(), None, "tryToParseReply - normal usage") def test_tryToParseReply_BAD_RESPONSE_PROTO(self): '''Test tryToParseReply - BadResponseProto error thrown.''' # Setup an arbitrary and broken bytestream self.lc.byte_stream = 'ABCD' self.assertEquals(self.lc.tryToParseReply(), None, "tryToParseReply - BAD_RESPONSE_PROTO error") self.assertEquals(self.controller.reason, rpc_pb2.BAD_RESPONSE_PROTO, "tryToParseReply - BAD_RESPONSE_PROTO error") self.assertEquals(self.controller.failed(), True, "tryToParseReply - BAD_RESPONSE_PROTO error") def test_tryToRetrieveServiceResponse(self): '''Test tryToRetrieveServiceResponse - normal usage.''' resp_class = rpc_pb2.Response expected_response = resp_class() self.lc.byte_stream = expected_response.SerializeToString() self.lc.rpcResponse = expected_response self.assertEquals(self.lc.tryToRetrieveServiceResponse(resp_class), None, "tryToRetrieveServiceResponse - normal usage") def test_tryToRetrieveServiceResponse_BAD_RESPONSE_PROTO(self): '''tryToRetrieveServiceResponse - BadResponseProto This error can never trigger, since all fields of an RPC Response() object are optional!''' pass 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") self.assert_(self.controller.success, "tryToRunCallback - contoller success flag set")
class Test__LifeCycle(unittest.TestCase): '''Unit tests for the protobuf.channel._Lifecycle class.''' def setUp(self): # Create a channel connected to a fake socket self.factory = FakeSocketFactory() self.socket = FakeSocket() self.channel = ch.SocketRpcChannel(socketFactory=self.factory) self.controller = self.channel.newController() self.lc = ch._LifeCycle(self.controller, self.channel) self.factory.setSocket(self.socket) # 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 an RPC request with the service request as payload self.rpc_request = rpc_pb2.Request() self.rpc_request.request_proto = self.serialized_request def tearDown(self): pass def test___init__(self): '''Test _LifeCycle constructor.''' self.assertEqual(self.lc.controller, self.controller, "Attribute 'controller' incorrectly initialized") self.assertEqual(self.lc.channel, self.channel, "Attribute 'channel' incorrectly initialized") self.assertEqual(self.lc.sock, None, "Attribute 'sock' incorrectly initialized") self.assertEqual(self.lc.byte_stream, None, "Attribute 'byte_stream' incorrectly initialized") self.assertEqual(self.lc.rpcResponse, None, "Attribute 'rpcResponse' incorrectly initialized") self.assertEqual(self.lc.serviceResponse, None, "Attribute 'serviceResponse' incorrectly initialized") def test_tryToValidateRequest(self): '''Test tryToValidateRequest - normal usage.''' self.assertEquals(self.lc.tryToValidateRequest(self.rpc_request), None, "tryToValidateRequest - valid request") def test_tryToValidateRequest_con_error(self): '''Test tryToValidateRequest - controller in error state.''' self.assertEquals(self.lc.tryToValidateRequest(self.rpc_request), None, "tryToValidateRequest - controller in error state") def test_tryToValidateRequest_BAD_REQUEST_PROTO(self): '''Test tryToValidateRequest - BadRequestProto error thrown.''' # A request with mandatory fields missing self.rpc_request = rpc_pb2.Request() self.lc.tryToValidateRequest(self.rpc_request) self.assertEquals(self.controller.reason, rpc_pb2.BAD_REQUEST_PROTO, "tryToValidateRequest - invalid request") self.assertEquals(self.controller.failed(), True, "tryToValidateRequest - invalid request") def test_tryToOpenSocket(self): '''Test tryToOpenSocket - normal usage.''' self.lc.tryToOpenSocket() self.assert_(self.lc.sock) def test_tryToOpenSocket_con_error(self): '''Test tryToOpenSocket - controller in error state.''' self.controller._fail = True self.lc.tryToOpenSocket() self.assertEquals(self.lc.sock, None, "tryToOpenSocket - controller in error state") def test_tryToOpenSocket_UNKNOWN_HOST(self): '''Test tryToOpenSocket - UnknownHost error thrown.''' self.socket.throwUnknownHostException() self.lc.tryToOpenSocket() self.assertEquals(self.lc.sock, None, "tryToOpenSocket - UNKNOWN_HOST error") self.assertEquals(self.controller.reason, rpc_pb2.UNKNOWN_HOST, "tryToOpenSocket - UNKNOWN_HOST error") self.assertEquals(self.controller.failed(), True, "tryToOpenSocket - UNKNOWN_HOST error") def test_tryToOpenSocket_IO_ERROR(self): '''Test tryToOpenSocket - IOError error thrown.''' self.socket.throwIOErrorException() self.lc.tryToOpenSocket() self.assertEquals(self.lc.sock, None, "tryToOpenSocket - IO_ERROR error") self.assertEquals(self.controller.reason, rpc_pb2.IO_ERROR, "tryToOpenSocket - IO_ERROR error") self.assertEquals(self.controller.failed(), True, "tryToOpenSocket - IO_ERROR error") def test_tryToSendRpcRequest(self): '''Test tryToSendRpcRequest - normal usage.''' # Instantiate the test service, and get a reference to the method method_name = 'TestMethod' service = TestServiceImpl() method = service.DESCRIPTOR.FindMethodByName(method_name) # Set the service and method names of the RPC request self.rpc_request.service_name = service.DESCRIPTOR.full_name self.rpc_request.method_name = method_name # Add the socket instance to the lifecycle object self.lc.sock = self.socket self.assertEquals( self.lc.tryToSendRpcRequest(method, self.rpc_request), None, "tryToSendRpcRequest - normal return") def test_tryToSendRpcRequest_IO_ERROR(self): '''Test tryToSendRpcRequest - IOError error thrown.''' # Instantiate the test service, and get a reference to the method method_name = 'TestMethod' service = TestServiceImpl() method = service.DESCRIPTOR.FindMethodByName(method_name) # Set the service and method names of the RPC request self.rpc_request.service_name = service.DESCRIPTOR.full_name self.rpc_request.method_name = method_name # Set the exception, and add the socket instance to the # lifecycle object self.socket.throwIOErrorException() self.lc.sock = self.socket self.assertEquals( self.lc.tryToSendRpcRequest(method, self.rpc_request), None, "tryToSendRpcRequest - IO_ERROR") self.assertEquals(self.controller.reason, rpc_pb2.IO_ERROR, "tryToSendRpcRequest - IO_ERROR error") self.assertEquals(self.controller.failed(), True, "tryToSendRpcRequest - IO_ERROR error") def test_tryToReceiveReply(self): '''Test tryToReceiveReply - normal usage.''' # Add some data to the socket msg = 'Message from server' self.socket.withInputBytes(msg) self.lc.sock = self.socket self.assertEquals(self.lc.tryToReceiveReply(), None, "tryToReceiveReply - normal usage") # Verify the socket has been closed self.assert_(self.socket.input_stream.closed, "tryToReceiveReply - normal usage") def test_tryToReceiveReply_IOError(self): '''Test tryToReceiveReply - IOError thrown.''' # Add some data to the socket msg = 'Message from server' self.socket.withInputBytes(msg) self.socket.throwIOErrorException() self.lc.sock = self.socket self.assertEquals(self.lc.tryToReceiveReply(), None, "tryToReceiveReply - IO_ERROR error") self.assertEquals(self.controller.reason, rpc_pb2.IO_ERROR, "tryToReceiveReply - IO_ERROR error") self.assertEquals(self.controller.failed(), True, "tryToReceiveReply - IO_ERROR error") # Verify the socket has been closed self.assert_(self.socket.input_stream.closed, "tryToReceiveReply - IO_ERROR error") def test_tryToParseReply(self): '''Test tryToParseReply - normal usage.''' resp_class = rpc_pb2.Response expected_response = resp_class() self.lc.byte_stream = expected_response.SerializeToString() self.assertEquals(self.lc.tryToParseReply(), None, "tryToParseReply - normal usage") def test_tryToParseReply_BAD_RESPONSE_PROTO(self): '''Test tryToParseReply - BadResponseProto error thrown.''' # Setup an arbitrary and broken bytestream self.lc.byte_stream = 'ABCD' self.assertEquals(self.lc.tryToParseReply(), None, "tryToParseReply - BAD_RESPONSE_PROTO error") self.assertEquals(self.controller.reason, rpc_pb2.BAD_RESPONSE_PROTO, "tryToParseReply - BAD_RESPONSE_PROTO error") self.assertEquals(self.controller.failed(), True, "tryToParseReply - BAD_RESPONSE_PROTO error") def test_tryToRetrieveServiceResponse(self): '''Test tryToRetrieveServiceResponse - normal usage.''' resp_class = rpc_pb2.Response expected_response = resp_class() self.lc.byte_stream = expected_response.SerializeToString() self.lc.rpcResponse = expected_response self.assertEquals(self.lc.tryToRetrieveServiceResponse(resp_class), None, "tryToRetrieveServiceResponse - normal usage") def test_tryToRetrieveServiceResponse_BAD_RESPONSE_PROTO(self): '''tryToRetrieveServiceResponse - BadResponseProto This error can never trigger, since all fields of an RPC Response() object are optional!''' pass 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")