Пример #1
0
    def testNoCallBack(self):
        '''Test RPC failing to invoke callback.'''

        # Fake socket with callback set to false
        socket = FakeSocket()
        socket.withNoResponse(False)
        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)

        self.assertFalse(callback.invoked, 'Callback invoked')
        self.assertEquals(self.serialized_request,
                          socket.getRequest().request_proto,
                          'Request protocol serialisation')
        self.assertEquals(service.DESCRIPTOR.full_name,
                          socket.getRequest().service_name, 'Service name')
        self.assertEquals(service.DESCRIPTOR.methods[0].name,
                          socket.getRequest().method_name, 'Method name')

        # Call Blocking RPC
        controller = channel.newController()
        response = service.TestMethod(controller, self.service_request, None)
        self.assertFalse(controller.failed())
        self.assertTrue(response is None, 'Response');
Пример #2
0
    def testIOErrorWhileCreatingSocket(self):
        '''Test Error while creating socket.'''

        # Fake socket primed to throw an unknown host exception
        socket = FakeSocket()
        socket.throwIOErrorException()
        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)

        self.assertTrue(callback.invoked, 'Callback invoked')
        self.assertTrue(callback.response is None, 'Response')
        self.assertTrue(controller.failed())
        self.assertEquals(rpc_pb2.IO_ERROR, controller.reason, 'Error 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.IO_ERROR, controller.reason, 
                              'Error reason')
Пример #3
0
    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
Пример #4
0
    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
Пример #5
0
    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')
Пример #6
0
    def testGoodRpc(self):
        '''Test a good RPC call.'''

        # Fake socket with prepared response
        socket = FakeSocket()
        socket.withResponseProto(self.service_response)
        socketFactory = FakeSocketFactory()
        socketFactory.setSocket(socket)

        # Create channel
        channel = ch.SocketRpcChannel("host", -1, socketFactory)

        # Create the service
        service = test_pb2.TestService_Stub(channel)

        def VerifyGoodRpc(response):
            self.assertEquals(self.service_response.str_data,
                              response.str_data, 'Response message')
            self.assertEquals(self.serialized_request,
                              socket.getRequest().request_proto,
                              'Request protocol serialisation')
            self.assertEquals(service.DESCRIPTOR.full_name,
                              socket.getRequest().service_name, 'Service name')
            self.assertEquals(service.DESCRIPTOR.methods[0].name,
                              socket.getRequest().method_name, 'Method name')
            
        # Call RPC method
        controller = channel.newController()
        callback = FakeCallback()
        service.TestMethod(controller, self.service_request, callback)

        self.assertTrue(callback.invoked, 'Callback invoked')
        VerifyGoodRpc(callback.response)
        
        # Call Blocking RPC
        controller = channel.newController()
        response = service.TestMethod(controller, self.service_request, None)
        VerifyGoodRpc(response)
Пример #7
0
    def testIncompleteRequest(self):
        '''Test calling RPC with incomplete request.'''

        # Create data
        service_request = test_pb2.Request()

        # Fake socket with prepared response
        socket = FakeSocket()
        socket.withResponseProto(self.service_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, service_request, callback)

        self.assertTrue(callback.invoked, 'Callback invoked')
        self.assertTrue(callback.response is None, 'Response')
        self.assertEquals(rpc_pb2.BAD_REQUEST_PROTO, controller.reason)
        self.assertTrue(controller.failed())

        # Call Blocking RPC
        controller = channel.newController()
        try:
            service.TestMethod(controller, service_request, None)
            self.fail('Should have thrown error')
        except RpcError:
            self.assertTrue(controller.failed())
            self.assertEquals(rpc_pb2.BAD_REQUEST_PROTO, controller.reason, 
                              'Error reason')