Search in sources :

Example 1 with AsyncCompletionHandler

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

the class ZeroCopyFileTest method zeroCopyPostTest.

@Test(groups = "standalone")
public void zeroCopyPostTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
    try (AsyncHttpClient client = asyncHttpClient()) {
        final AtomicBoolean headerSent = new AtomicBoolean(false);
        final AtomicBoolean operationCompleted = new AtomicBoolean(false);
        Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncCompletionHandler<Response>() {

            public State onHeadersWritten() {
                headerSent.set(true);
                return State.CONTINUE;
            }

            public State onContentWritten() {
                operationCompleted.set(true);
                return State.CONTINUE;
            }

            @Override
            public Response onCompleted(Response response) throws Exception {
                return response;
            }
        }).get();
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
        assertTrue(operationCompleted.get());
        assertTrue(headerSent.get());
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncCompletionHandler(org.asynchttpclient.AsyncCompletionHandler) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) BasicHttpsTest(org.asynchttpclient.BasicHttpsTest) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 2 with AsyncCompletionHandler

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

the class NettyRequestThrottleTimeoutTest method testRequestTimeout.

@Test(groups = "standalone")
public void testRequestTimeout() throws IOException {
    final Semaphore requestThrottle = new Semaphore(1);
    int samples = 10;
    try (AsyncHttpClient client = asyncHttpClient(config().setMaxConnections(1))) {
        final CountDownLatch latch = new CountDownLatch(samples);
        final List<Exception> tooManyConnections = Collections.synchronizedList(new ArrayList<>(2));
        for (int i = 0; i < samples; i++) {
            new Thread(new Runnable() {

                public void run() {
                    try {
                        requestThrottle.acquire();
                        Future<Response> responseFuture = null;
                        try {
                            responseFuture = client.prepareGet(getTargetUrl()).setRequestTimeout(SLEEPTIME_MS / 2).execute(new AsyncCompletionHandler<Response>() {

                                @Override
                                public Response onCompleted(Response response) throws Exception {
                                    return response;
                                }

                                @Override
                                public void onThrowable(Throwable t) {
                                    logger.error("onThrowable got an error", t);
                                    try {
                                        Thread.sleep(100);
                                    } catch (InterruptedException e) {
                                    }
                                    requestThrottle.release();
                                }
                            });
                        } catch (Exception e) {
                            tooManyConnections.add(e);
                        }
                        if (responseFuture != null)
                            responseFuture.get();
                    } catch (Exception e) {
                    } finally {
                        latch.countDown();
                    }
                }
            }).start();
        }
        try {
            latch.await(30, TimeUnit.SECONDS);
        } catch (Exception e) {
            fail("failed to wait for requests to complete");
        }
        for (Exception e : tooManyConnections) logger.error("Exception while calling execute", e);
        assertTrue(tooManyConnections.isEmpty(), "Should not have any connection errors where too many connections have been attempted");
    }
}
Also used : Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AsyncCompletionHandler(org.asynchttpclient.AsyncCompletionHandler) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)2 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)2 AsyncCompletionHandler (org.asynchttpclient.AsyncCompletionHandler)2 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)2 Response (org.asynchttpclient.Response)2 Test (org.testng.annotations.Test)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ServletException (javax.servlet.ServletException)1 BasicHttpsTest (org.asynchttpclient.BasicHttpsTest)1