Search in sources :

Example 51 with AsyncHttpClient

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

the class ConnectionPoolTest method testMaxTotalConnectionsException.

@Test(groups = "standalone", expectedExceptions = TooManyConnectionsException.class)
public void testMaxTotalConnectionsException() throws Throwable {
    try (AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(true).setMaxConnections(1))) {
        String url = getTargetUrl();
        List<ListenableFuture<Response>> futures = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            logger.info("{} requesting url [{}]...", i, url);
            futures.add(client.prepareGet(url).execute());
        }
        Exception exception = null;
        for (ListenableFuture<Response> future : futures) {
            try {
                future.get();
            } catch (Exception ex) {
                exception = ex;
                break;
            }
        }
        assertNotNull(exception);
        throw exception.getCause();
    }
}
Also used : Response(org.asynchttpclient.Response) ArrayList(java.util.ArrayList) ListenableFuture(org.asynchttpclient.ListenableFuture) 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 52 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient 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 53 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient 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 54 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient 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 55 with AsyncHttpClient

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

the class ConnectionPoolTest method nonPoolableConnectionReleaseSemaphoresTest.

@Test(groups = "standalone")
public void nonPoolableConnectionReleaseSemaphoresTest() throws Throwable {
    RequestBuilder request = get(getTargetUrl()).setHeader("Connection", "close");
    try (AsyncHttpClient client = asyncHttpClient(config().setMaxConnections(6).setMaxConnectionsPerHost(3))) {
        client.executeRequest(request).get();
        Thread.sleep(1000);
        client.executeRequest(request).get();
        Thread.sleep(1000);
        client.executeRequest(request).get();
        Thread.sleep(1000);
        client.executeRequest(request).get();
    }
}
Also used : RequestBuilder(org.asynchttpclient.RequestBuilder) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)146 Test (org.testng.annotations.Test)119 Response (org.asynchttpclient.Response)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)66 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 CountDownLatch (java.util.concurrent.CountDownLatch)31 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 RequestBuilder (org.asynchttpclient.RequestBuilder)16 IOException (java.io.IOException)14 RouteBuilder (org.apache.camel.builder.RouteBuilder)14 ExecutionException (java.util.concurrent.ExecutionException)13 Request (org.asynchttpclient.Request)13 WebSocket (org.asynchttpclient.ws.WebSocket)12 Test (org.junit.Test)11 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)10 File (java.io.File)9 WebSocketTextListener (org.asynchttpclient.ws.WebSocketTextListener)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7