Search in sources :

Example 1 with DoneCallback

use of org.jdeferred.DoneCallback 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)

Aggregations

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