Search in sources :

Example 1 with ListenableFuture

use of org.asynchttpclient.ListenableFuture 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 2 with ListenableFuture

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

the class RetryNonBlockingIssue method testRetryNonBlockingAsyncConnect.

@Test(groups = "standalone")
public void testRetryNonBlockingAsyncConnect() throws IOException, InterruptedException, ExecutionException {
    AsyncHttpClientConfig config = //
    config().setKeepAlive(//
    true).setMaxConnections(//
    100).setConnectTimeout(//
    60000).setRequestTimeout(//
    30000).build();
    try (AsyncHttpClient client = asyncHttpClient(config)) {
        List<ListenableFuture<Response>> res = new ArrayList<>();
        for (int i = 0; i < 32; i++) {
            res.add(testMethodRequest(client, 3, "servlet", UUID.randomUUID().toString()));
        }
        StringBuilder b = new StringBuilder();
        for (ListenableFuture<Response> r : res) {
            Response theres = r.get();
            assertEquals(theres.getStatusCode(), 200);
            b.append("==============\r\n");
            b.append("Response Headers\r\n");
            HttpHeaders heads = theres.getHeaders();
            b.append(heads + "\r\n");
            b.append("==============\r\n");
        }
        System.out.println(b.toString());
        System.out.flush();
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) ArrayList(java.util.ArrayList) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) ListenableFuture(org.asynchttpclient.ListenableFuture) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 3 with ListenableFuture

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

the class RetryNonBlockingIssue method testRetryNonBlocking.

/**
     * Tests that a head request can be made
     * 
     * @throws IOException
     * @throws ExecutionException
     * @throws InterruptedException
     */
@Test(groups = "standalone")
public void testRetryNonBlocking() throws IOException, InterruptedException, ExecutionException {
    AsyncHttpClientConfig config = //
    config().setKeepAlive(//
    true).setMaxConnections(//
    100).setConnectTimeout(//
    60000).setRequestTimeout(//
    30000).build();
    try (AsyncHttpClient client = asyncHttpClient(config)) {
        List<ListenableFuture<Response>> res = new ArrayList<>();
        for (int i = 0; i < 32; i++) {
            res.add(testMethodRequest(client, 3, "servlet", UUID.randomUUID().toString()));
        }
        StringBuilder b = new StringBuilder();
        for (ListenableFuture<Response> r : res) {
            Response theres = r.get();
            assertEquals(200, theres.getStatusCode());
            b.append("==============\r\n");
            b.append("Response Headers\r\n");
            HttpHeaders heads = theres.getHeaders();
            b.append(heads + "\r\n");
            b.append("==============\r\n");
        }
        System.out.println(b.toString());
        System.out.flush();
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) ArrayList(java.util.ArrayList) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) ListenableFuture(org.asynchttpclient.ListenableFuture) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 4 with ListenableFuture

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

the class MaxTotalConnectionTest method testMaxTotalConnectionsExceedingException.

@Test(groups = "online")
public void testMaxTotalConnectionsExceedingException() throws IOException {
    String[] urls = new String[] { "http://google.com", "http://github.com/" };
    AsyncHttpClientConfig config = //
    config().setConnectTimeout(//
    1000).setRequestTimeout(//
    5000).setKeepAlive(//
    false).setMaxConnections(//
    1).setMaxConnectionsPerHost(//
    1).build();
    try (AsyncHttpClient client = asyncHttpClient(config)) {
        List<ListenableFuture<Response>> futures = new ArrayList<>();
        for (String url : urls) {
            futures.add(client.prepareGet(url).execute());
        }
        boolean caughtError = false;
        int i;
        for (i = 0; i < urls.length; i++) {
            try {
                futures.get(i).get();
            } catch (Exception e) {
                // assert that 2nd request fails, because
                // maxTotalConnections=1
                caughtError = true;
                break;
            }
        }
        Assert.assertEquals(1, i);
        Assert.assertTrue(caughtError);
    }
}
Also used : ArrayList(java.util.ArrayList) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) ListenableFuture(org.asynchttpclient.ListenableFuture) IOException(java.io.IOException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

ArrayList (java.util.ArrayList)4 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)4 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)4 ListenableFuture (org.asynchttpclient.ListenableFuture)4 Test (org.testng.annotations.Test)4 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)3 Response (org.asynchttpclient.Response)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)2 IOException (java.io.IOException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ExecutionException (java.util.concurrent.ExecutionException)1 TooManyConnectionsException (org.asynchttpclient.exception.TooManyConnectionsException)1