Search in sources :

Example 26 with Response

use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.

the class ConnectionPoolTest method asyncHandlerOnThrowableTest.

@Test(groups = "standalone")
public void asyncHandlerOnThrowableTest() throws Exception {
    try (AsyncHttpClient client = asyncHttpClient()) {
        final AtomicInteger count = new AtomicInteger();
        final String THIS_IS_NOT_FOR_YOU = "This is not for you";
        final CountDownLatch latch = new CountDownLatch(16);
        for (int i = 0; i < 16; i++) {
            client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerBase() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    throw new Exception(THIS_IS_NOT_FOR_YOU);
                }
            });
            client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerBase() {

                @Override
                public void onThrowable(Throwable t) {
                    if (t.getMessage() != null && t.getMessage().equalsIgnoreCase(THIS_IS_NOT_FOR_YOU)) {
                        count.incrementAndGet();
                    }
                }

                @Override
                public Response onCompleted(Response response) throws Exception {
                    latch.countDown();
                    return response;
                }
            });
        }
        latch.await(TIMEOUT, TimeUnit.SECONDS);
        assertEquals(count.get(), 0);
    }
}
Also used : Response(org.asynchttpclient.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncCompletionHandlerBase(org.asynchttpclient.AsyncCompletionHandlerBase) CountDownLatch(java.util.concurrent.CountDownLatch) TooManyConnectionsException(org.asynchttpclient.exception.TooManyConnectionsException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 27 with Response

use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.

the class ConnectionPoolTest method multipleMaxConnectionOpenTest.

@Test(groups = "standalone", expectedExceptions = TooManyConnectionsException.class)
public void multipleMaxConnectionOpenTest() throws Throwable {
    try (AsyncHttpClient c = asyncHttpClient(config().setKeepAlive(true).setConnectTimeout(5000).setMaxConnections(1))) {
        String body = "hello there";
        // once
        Response response = c.preparePost(getTargetUrl()).setBody(body).execute().get(TIMEOUT, TimeUnit.SECONDS);
        assertEquals(response.getResponseBody(), body);
        // twice
        Exception exception = null;
        try {
            c.preparePost(String.format("http://localhost:%d/foo/test", port2)).setBody(body).execute().get(TIMEOUT, TimeUnit.SECONDS);
            fail("Should throw exception. Too many connections issued.");
        } catch (Exception ex) {
            ex.printStackTrace();
            exception = ex;
        }
        assertNotNull(exception);
        throw exception.getCause();
    }
}
Also used : Response(org.asynchttpclient.Response) TooManyConnectionsException(org.asynchttpclient.exception.TooManyConnectionsException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 28 with Response

use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.

the class ConnectionPoolTest method win7DisconnectTest.

/**
     * This test just make sure the hack used to catch disconnected channel under win7 doesn't throw any exception. The onComplete method must be only called once.
     * 
     * @throws Exception if something wrong happens.
     */
@Test(groups = "standalone")
public void win7DisconnectTest() throws Exception {
    final AtomicInteger count = new AtomicInteger(0);
    try (AsyncHttpClient client = asyncHttpClient()) {
        AsyncCompletionHandler<Response> handler = new AsyncCompletionHandlerAdapter() {

            @Override
            public Response onCompleted(Response response) throws Exception {
                count.incrementAndGet();
                StackTraceElement e = new StackTraceElement("sun.nio.ch.SocketDispatcher", "read0", null, -1);
                IOException t = new IOException();
                t.setStackTrace(new StackTraceElement[] { e });
                throw t;
            }
        };
        try {
            client.prepareGet(getTargetUrl()).execute(handler).get();
            fail("Must have received an exception");
        } catch (ExecutionException ex) {
            assertNotNull(ex);
            assertNotNull(ex.getCause());
            assertEquals(ex.getCause().getClass(), IOException.class);
            assertEquals(count.get(), 1);
        }
    }
}
Also used : Response(org.asynchttpclient.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 29 with Response

use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.

the class ConnectionPoolTest method testMaxTotalConnections.

@Test(groups = "standalone")
public void testMaxTotalConnections() throws Exception {
    try (AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(true).setMaxConnections(1))) {
        String url = getTargetUrl();
        int i;
        Exception exception = null;
        for (i = 0; i < 3; i++) {
            try {
                logger.info("{} requesting url [{}]...", i, url);
                Response response = client.prepareGet(url).execute().get();
                logger.info("{} response [{}].", i, response);
            } catch (Exception ex) {
                exception = ex;
            }
        }
        assertNull(exception);
    }
}
Also used : Response(org.asynchttpclient.Response) TooManyConnectionsException(org.asynchttpclient.exception.TooManyConnectionsException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 30 with Response

use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.

the class MaxConnectionsInThreads method testMaxConnectionsWithinThreads.

@Test(groups = "standalone")
public void testMaxConnectionsWithinThreads() throws Exception {
    String[] urls = new String[] { getTargetUrl(), getTargetUrl() };
    AsyncHttpClientConfig config = //
    config().setConnectTimeout(//
    1000).setRequestTimeout(//
    5000).setKeepAlive(//
    true).setMaxConnections(//
    1).setMaxConnectionsPerHost(//
    1).build();
    final CountDownLatch inThreadsLatch = new CountDownLatch(2);
    final AtomicInteger failedCount = new AtomicInteger();
    try (AsyncHttpClient client = asyncHttpClient(config)) {
        for (final String url : urls) {
            Thread t = new Thread() {

                public void run() {
                    client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {

                        @Override
                        public Response onCompleted(Response response) throws Exception {
                            Response r = super.onCompleted(response);
                            inThreadsLatch.countDown();
                            return r;
                        }

                        @Override
                        public void onThrowable(Throwable t) {
                            super.onThrowable(t);
                            failedCount.incrementAndGet();
                            inThreadsLatch.countDown();
                        }
                    });
                }
            };
            t.start();
        }
        inThreadsLatch.await();
        assertEquals(failedCount.get(), 1, "Max Connections should have been reached when launching from concurrent threads");
        final CountDownLatch notInThreadsLatch = new CountDownLatch(2);
        failedCount.set(0);
        for (final String url : urls) {
            client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    Response r = super.onCompleted(response);
                    notInThreadsLatch.countDown();
                    return r;
                }

                @Override
                public void onThrowable(Throwable t) {
                    super.onThrowable(t);
                    failedCount.incrementAndGet();
                    notInThreadsLatch.countDown();
                }
            });
        }
        notInThreadsLatch.await();
        assertEquals(failedCount.get(), 1, "Max Connections should have been reached when launching from main thread");
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) AsyncCompletionHandlerBase(org.asynchttpclient.AsyncCompletionHandlerBase) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

Response (org.asynchttpclient.Response)90 Test (org.testng.annotations.Test)78 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)69 HttpServletResponse (javax.servlet.http.HttpServletResponse)42 Request (org.asynchttpclient.Request)14 RequestBuilder (org.asynchttpclient.RequestBuilder)14 IOException (java.io.IOException)12 ExecutionException (java.util.concurrent.ExecutionException)12 File (java.io.File)10 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 InputStreamBodyGenerator (org.asynchttpclient.request.body.generator.InputStreamBodyGenerator)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 TestSubscriber (rx.observers.TestSubscriber)6 ArrayList (java.util.ArrayList)5