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');
    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')
    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 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')
    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)
    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')