Search in sources :

Example 1 with HttpProgress

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

the class AsyncHttpTest method testPromiseAdapter.

public void testPromiseAdapter() throws IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger successCount = new AtomicInteger();
    final AtomicInteger progressCount = new AtomicInteger();
    try (AsyncHttpClient client = asyncHttpClient()) {
        Promise<Response, Throwable, HttpProgress> p1 = AsyncHttpDeferredObject.promise(client.prepareGet("http://gatling.io"));
        p1.done(new DoneCallback<Response>() {

            @Override
            public void onDone(Response response) {
                try {
                    assertEquals(response.getStatusCode(), 200);
                    successCount.incrementAndGet();
                } finally {
                    latch.countDown();
                }
            }
        }).progress(new ProgressCallback<HttpProgress>() {

            @Override
            public void onProgress(HttpProgress progress) {
                progressCount.incrementAndGet();
            }
        });
        latch.await();
        assertTrue(progressCount.get() > 0);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}
Also used : Response(org.asynchttpclient.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DoneCallback(org.jdeferred.DoneCallback) HttpProgress(org.asynchttpclient.extras.jdeferred.HttpProgress) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 2 with HttpProgress

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

the class AsyncHttpTest method testMultiplePromiseAdapter.

public void testMultiplePromiseAdapter() throws IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger successCount = new AtomicInteger();
    try (AsyncHttpClient client = asyncHttpClient()) {
        Promise<Response, Throwable, HttpProgress> p1 = AsyncHttpDeferredObject.promise(client.prepareGet("http://gatling.io"));
        Promise<Response, Throwable, HttpProgress> p2 = AsyncHttpDeferredObject.promise(client.prepareGet("http://www.google.com"));
        AsyncHttpDeferredObject deferredRequest = new AsyncHttpDeferredObject(client.prepareGet("http://jdeferred.org"));
        deferredManager.when(p1, p2, deferredRequest).then(new DoneCallback<MultipleResults>() {

            @Override
            public void onDone(MultipleResults result) {
                try {
                    assertEquals(result.size(), 3);
                    assertEquals(Response.class.cast(result.get(0).getResult()).getStatusCode(), 200);
                    assertEquals(Response.class.cast(result.get(1).getResult()).getStatusCode(), 200);
                    assertEquals(Response.class.cast(result.get(2).getResult()).getStatusCode(), 200);
                    successCount.incrementAndGet();
                } finally {
                    latch.countDown();
                }
            }
        });
        latch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}
Also used : Response(org.asynchttpclient.Response) AsyncHttpDeferredObject(org.asynchttpclient.extras.jdeferred.AsyncHttpDeferredObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpProgress(org.asynchttpclient.extras.jdeferred.HttpProgress) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) MultipleResults(org.jdeferred.multiple.MultipleResults)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)2 Response (org.asynchttpclient.Response)2 HttpProgress (org.asynchttpclient.extras.jdeferred.HttpProgress)2 AsyncHttpDeferredObject (org.asynchttpclient.extras.jdeferred.AsyncHttpDeferredObject)1 DoneCallback (org.jdeferred.DoneCallback)1 MultipleResults (org.jdeferred.multiple.MultipleResults)1