Search in sources :

Example 56 with AsyncHttpClient

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

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

Example 58 with AsyncHttpClient

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

the class FilterTest method replayHeaderResponseFilterTest.

@Test(groups = "standalone")
public void replayHeaderResponseFilterTest() throws Exception {
    final AtomicBoolean replay = new AtomicBoolean(true);
    ResponseFilter responseFilter = new ResponseFilter() {

        public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException {
            if (ctx.getResponseHeaders() != null && ctx.getResponseHeaders().getHeaders().get("Ping").equals("Pong") && replay.getAndSet(false)) {
                Request request = new RequestBuilder(ctx.getRequest()).addHeader("Ping", "Pong").build();
                return new FilterContext.FilterContextBuilder<T>().asyncHandler(ctx.getAsyncHandler()).request(request).replayRequest(true).build();
            }
            return ctx;
        }
    };
    try (AsyncHttpClient c = asyncHttpClient(config().addResponseFilter(responseFilter))) {
        Response response = c.preparePost(getTargetUrl()).addHeader("Ping", "Pong").execute().get();
        assertNotNull(response);
        assertEquals(response.getStatusCode(), 200);
        assertEquals(response.getHeader("Ping"), "Pong");
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RequestBuilder(org.asynchttpclient.RequestBuilder) Request(org.asynchttpclient.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 59 with AsyncHttpClient

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

the class FilterTest method maxConnectionsText.

@Test(groups = "standalone")
public void maxConnectionsText() throws Exception {
    try (AsyncHttpClient c = asyncHttpClient(config().addRequestFilter(new ThrottleRequestFilter(0, 1000)))) {
        c.preparePost(getTargetUrl()).execute().get();
        fail("Should have timed out");
    } catch (ExecutionException ex) {
        assertTrue(ex.getCause() instanceof FilterException);
    }
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 60 with AsyncHttpClient

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

the class BodyDeferringAsyncHandlerTest method deferredInputStreamTrickWithFailure.

@Test(groups = "standalone", expectedExceptions = RemotelyClosedException.class)
public void deferredInputStreamTrickWithFailure() throws Throwable {
    try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
        BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredInputStreamTrickWithFailure").addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
        PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream pis = new PipedInputStream(pos);
        BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos);
        Future<Response> f = r.execute(bdah);
        BodyDeferringInputStream is = new BodyDeferringInputStream(f, bdah, pis);
        Response resp = is.getAsapResponse();
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
        // "consume" the body, but our code needs input stream
        CountingOutputStream cos = new CountingOutputStream();
        try {
            try {
                copy(is, cos);
            } finally {
                is.close();
                cos.close();
            }
        } catch (IOException e) {
            throw e.getCause();
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) PipedOutputStream(java.io.PipedOutputStream) BodyDeferringInputStream(org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) 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