Search in sources :

Example 16 with ConditionCheck

use of com.linkedin.databus2.test.ConditionCheck in project databus by linkedin.

the class TestHttpResponseProcessor method testRequestError.

@Test
public void testRequestError() throws DatabusException {
    Logger log = Logger.getLogger("GenericHttpResponseHandler.testRequestError");
    TestHttpResponseProcessor respProcessor = new TestHttpResponseProcessor(log);
    TestConnectListener connectListener = new TestConnectListener(log);
    TestSendRequestListener requestListener = new TestSendRequestListener(log);
    TestCloseListener closeListener = new TestCloseListener(log);
    //Need this call to set respProcessor without triggering erroneous check
    final GenericHttpResponseHandler responseHandler = new GenericHttpResponseHandler(respProcessor, KeepAliveType.KEEP_ALIVE);
    responseHandler.getLog().setLevel(_logLevel);
    responseHandler.setRequestListener(requestListener);
    responseHandler.setConnectionListener(connectListener);
    responseHandler.setCloseListener(closeListener);
    Channel channel = createClientBootstrap(responseHandler);
    SocketAddress clientAddr = channel.getLocalAddress();
    try {
        HttpResponse resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
        resp.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
        sendServerResponse(clientAddr, resp, 2000);
        channel.write(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/test"));
        final List<String> callbacks = respProcessor.getCallbacks();
        final List<String> connectCallbacks = connectListener.getCallbacks();
        final List<String> requestCallbacks = requestListener.getCallbacks();
        final List<String> closeCallbacks = closeListener.getCallbacks();
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return 1 == closeCallbacks.size();
            }
        }, "waiting for close channel callback", 1000, null);
        //make sure that no new callbacks have showed up
        stateSanityCheck(connectCallbacks, requestCallbacks, callbacks, closeCallbacks);
        Assert.assertEquals(connectCallbacks.get(0), "onConnectSuccess");
        Assert.assertEquals(requestCallbacks.size(), 1);
        Assert.assertEquals(requestCallbacks.get(0), "onSendRequestFailure");
        Assert.assertEquals(callbacks.size(), 0);
        Assert.assertEquals(closeCallbacks.size(), 1);
        Assert.assertEquals(closeCallbacks.get(0), "onChannelClose");
    } finally {
        channel.close();
    }
}
Also used : ConditionCheck(com.linkedin.databus2.test.ConditionCheck) Channel(org.jboss.netty.channel.Channel) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Logger(org.apache.log4j.Logger) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.testng.annotations.Test)

Example 17 with ConditionCheck

use of com.linkedin.databus2.test.ConditionCheck in project databus by linkedin.

the class TestHttpResponseProcessor method testHappyPathWithCloseChunking.

@Test
public void testHappyPathWithCloseChunking() throws DatabusException {
    Logger log = Logger.getLogger("GenericHttpResponseHandler.testHappyPathWithCloseChunking");
    final GenericHttpResponseHandler responseHandler = new GenericHttpResponseHandler(KeepAliveType.KEEP_ALIVE);
    responseHandler.getLog().setLevel(_logLevel);
    TestHttpResponseProcessor respProcessor = new TestHttpResponseProcessor(log);
    TestConnectListener connectListener = new TestConnectListener(log);
    TestSendRequestListener requestListener = new TestSendRequestListener(log);
    TestCloseListener closeListener = new TestCloseListener(log);
    responseHandler.setConnectionListener(connectListener);
    Channel channel = createClientBootstrap(responseHandler);
    SocketAddress clientAddr = channel.getLocalAddress();
    try {
        setListeners(responseHandler, respProcessor, requestListener, closeListener);
        channel.write(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/test"));
        //It seems that there is a race condition between the writeFuture succeeding
        //and the writeComplete message getting to the handler. Make sure that the
        //writeComplete has got to the handler before we do anything else with
        //the channel.
        final GenericHttpResponseHandler handler = getResponseHandler(channel);
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return handler._messageState.hasSentRequest();
            }
        }, "request sent", 1000, log);
        HttpResponse resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        resp.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
        sendServerResponse(clientAddr, resp, 1000);
        HttpChunk chunk1 = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer("chunk1".getBytes(Charset.defaultCharset())));
        sendServerResponse(clientAddr, chunk1, 1000);
        HttpChunk chunk2 = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer("chunk2".getBytes(Charset.defaultCharset())));
        sendServerResponse(clientAddr, chunk2, 1000);
        sendServerResponse(clientAddr, new DefaultHttpChunkTrailer(), 1000);
        TestUtil.sleep(200);
        sendServerClose(clientAddr, -1);
        final List<String> callbacks = respProcessor.getCallbacks();
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return 5 == callbacks.size();
            }
        }, "waiting for response processed", 1000, null);
        final List<String> connectCallbacks = connectListener.getCallbacks();
        final List<String> requestCallbacks = requestListener.getCallbacks();
        final List<String> closeCallbacks = closeListener.getCallbacks();
        stateSanityCheck(connectCallbacks, requestCallbacks, callbacks, closeCallbacks);
        Assert.assertEquals(callbacks.get(0), "startResponse");
        Assert.assertEquals(callbacks.get(1), "addChunk");
        Assert.assertEquals(callbacks.get(2), "addChunk");
        Assert.assertEquals(callbacks.get(3), "addTrailer");
        Assert.assertEquals(callbacks.get(4), "finishResponse");
        //make sure that no new callbacks have showed up
        Assert.assertEquals(callbacks.size(), 5);
    } finally {
        channel.close();
    }
}
Also used : ConditionCheck(com.linkedin.databus2.test.ConditionCheck) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) Channel(org.jboss.netty.channel.Channel) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Logger(org.apache.log4j.Logger) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpChunkTrailer(org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk) Test(org.testng.annotations.Test)

Example 18 with ConditionCheck

use of com.linkedin.databus2.test.ConditionCheck in project databus by linkedin.

the class TestHttpResponseProcessor method testHappyPathWithCloseNoChunking.

@Test
public void testHappyPathWithCloseNoChunking() throws DatabusException {
    Logger log = Logger.getLogger("GenericHttpResponseHandler.testHappyPathWithCloseNoChunking");
    final GenericHttpResponseHandler responseHandler = new GenericHttpResponseHandler(KeepAliveType.KEEP_ALIVE);
    responseHandler.getLog().setLevel(_logLevel);
    TestHttpResponseProcessor respProcessor = new TestHttpResponseProcessor(log);
    TestConnectListener connectListener = new TestConnectListener(log);
    TestSendRequestListener requestListener = new TestSendRequestListener(log);
    TestCloseListener closeListener = new TestCloseListener(log);
    responseHandler.setConnectionListener(connectListener);
    Channel channel = createClientBootstrap(responseHandler);
    SocketAddress clientAddr = channel.getLocalAddress();
    try {
        setListeners(responseHandler, respProcessor, requestListener, closeListener);
        channel.write(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/test"));
        //It seems that there is a race condition between the writeFuture succeeding
        //and the writeComplete message getting to the handler. Make sure that the
        //writeComplete has got to the handler before we do anything else with
        //the channel.
        final GenericHttpResponseHandler handler = getResponseHandler(channel);
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return handler._messageState.hasSentRequest();
            }
        }, "request sent", 1000, log);
        HttpResponse resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        resp.setContent(null);
        resp.setHeader(HttpHeaders.Names.CONTENT_LENGTH, 0);
        sendServerResponse(clientAddr, resp, 1000);
        TestUtil.sleep(200);
        sendServerClose(clientAddr, -1);
        final List<String> callbacks = respProcessor.getCallbacks();
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return 2 == callbacks.size();
            }
        }, "waiting for response processed", 1000, null);
        final List<String> connectCallbacks = connectListener.getCallbacks();
        final List<String> requestCallbacks = requestListener.getCallbacks();
        final List<String> closeCallbacks = closeListener.getCallbacks();
        stateSanityCheck(connectCallbacks, requestCallbacks, callbacks, closeCallbacks);
        Assert.assertEquals(callbacks.get(0), "startResponse");
        Assert.assertEquals(callbacks.get(1), "finishResponse");
        //make sure that no new callbacks have showed up
        Assert.assertEquals(callbacks.size(), 2);
    } finally {
        channel.close();
    }
}
Also used : ConditionCheck(com.linkedin.databus2.test.ConditionCheck) Channel(org.jboss.netty.channel.Channel) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Logger(org.apache.log4j.Logger) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.testng.annotations.Test)

Example 19 with ConditionCheck

use of com.linkedin.databus2.test.ConditionCheck in project databus by linkedin.

the class TestHttpResponseProcessor method testRepeatedReadSuccess.

@Test
public void testRepeatedReadSuccess() throws DatabusException {
    Logger log = Logger.getLogger("GenericHttpResponseHandler.testRestRepeatedReadSuccess");
    //global responseHandler;
    final GenericHttpResponseHandler responseHandler = new GenericHttpResponseHandler(KeepAliveType.KEEP_ALIVE);
    responseHandler.getLog().setLevel(_logLevel);
    TestConnectListener connectListener = new TestConnectListener(log);
    responseHandler.setConnectionListener(connectListener);
    Channel channel = createClientBootstrap(responseHandler);
    SocketAddress clientAddr = channel.getLocalAddress();
    try {
        for (int i = 0; i < 2; ++i) {
            TestHttpResponseProcessor respProcessor = new TestHttpResponseProcessor(log);
            TestSendRequestListener requestListener = new TestSendRequestListener(log);
            TestCloseListener closeListener = new TestCloseListener(log);
            setListeners(responseHandler, respProcessor, requestListener, closeListener);
            channel.write(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/test"));
            TestUtil.sleep(1000);
            HttpResponse resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
            resp.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
            sendServerResponse(clientAddr, resp, 2000);
            HttpChunk chunk1 = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer("chunk1".getBytes(Charset.defaultCharset())));
            sendServerResponse(clientAddr, chunk1, 1000);
            sendServerResponse(clientAddr, new DefaultHttpChunkTrailer(), 1000);
            final List<String> callbacks = respProcessor.getCallbacks();
            final List<String> connectCallbacks = connectListener.getCallbacks();
            final List<String> requestCallbacks = requestListener.getCallbacks();
            final List<String> closeCallbacks = closeListener.getCallbacks();
            TestUtil.sleep(500);
            stateSanityCheck(connectCallbacks, requestCallbacks, callbacks, closeCallbacks);
            Assert.assertEquals(1, connectCallbacks.size());
            Assert.assertEquals(connectCallbacks.get(0), "onConnectSuccess");
            Assert.assertEquals(requestCallbacks.size(), 1);
            Assert.assertEquals(requestCallbacks.get(0), "onSendRequestSuccess");
            TestUtil.assertWithBackoff(new ConditionCheck() {

                @Override
                public boolean check() {
                    return callbacks.size() == 4;
                }
            }, "waiting for response processed", 2000, null);
            Assert.assertEquals(callbacks.get(0), "startResponse");
            Assert.assertEquals(callbacks.get(1), "addChunk");
            Assert.assertEquals(callbacks.get(2), "addTrailer");
            Assert.assertEquals(callbacks.get(3), "finishResponse");
        }
    } finally {
        channel.close();
    }
}
Also used : ConditionCheck(com.linkedin.databus2.test.ConditionCheck) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) Channel(org.jboss.netty.channel.Channel) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Logger(org.apache.log4j.Logger) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpChunkTrailer(org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk) Test(org.testng.annotations.Test)

Example 20 with ConditionCheck

use of com.linkedin.databus2.test.ConditionCheck in project databus by linkedin.

the class TestHttpResponseProcessor method createClientBootstrap.

static Channel createClientBootstrap(final GenericHttpResponseHandler responseHandler, int port) {
    final ChannelFuture connectFuture = createChannelFuture(responseHandler, port);
    TestUtil.assertWithBackoff(new ConditionCheck() {

        @Override
        public boolean check() {
            return (connectFuture.isDone() && connectFuture.isSuccess() && responseHandler.getMessageState() == MessageState.REQUEST_WAIT);
        }
    }, "waiting for connect success", 2000, null);
    Assert.assertTrue(connectFuture.isDone() && connectFuture.isSuccess());
    return connectFuture.getChannel();
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ConditionCheck(com.linkedin.databus2.test.ConditionCheck)

Aggregations

ConditionCheck (com.linkedin.databus2.test.ConditionCheck)47 Test (org.testng.annotations.Test)40 Logger (org.apache.log4j.Logger)36 Channel (org.jboss.netty.channel.Channel)22 InetSocketAddress (java.net.InetSocketAddress)21 Checkpoint (com.linkedin.databus.core.Checkpoint)20 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)20 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)20 SocketAddress (java.net.SocketAddress)19 RegisterResponseEntry (com.linkedin.databus2.core.container.request.RegisterResponseEntry)15 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)15 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)15 DbusEventBuffer (com.linkedin.databus.core.DbusEventBuffer)12 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)11 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)11 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)11 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)10 List (java.util.List)10 IdNamePair (com.linkedin.databus.core.util.IdNamePair)9