Search in sources :

Example 16 with AsyncSharedPoolImpl

use of com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl in project rest.li by linkedin.

the class TestAsyncSharedPoolImpl method testGetItemCancelled.

@Test
public void testGetItemCancelled() throws Exception {
    final LifecycleMock lifecycle = new LifecycleMock();
    final CountDownLatch createLatch = new CountDownLatch(1);
    lifecycle.setCreateConsumer(callback -> {
        try {
            createLatch.await();
            callback.onSuccess(ITEM);
        } catch (Exception e) {
            callback.onError(e);
        }
    });
    AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, lifecycle, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
    pool.start();
    // Only one thread will perform the actual item creation task and the rest
    // will return immediately. Therefore we wait for GET_COUNT - 1 threads to complete.
    final CountDownLatch getLatch = new CountDownLatch(GET_COUNT - 1);
    final ConcurrentLinkedQueue<Cancellable> cancellables = new ConcurrentLinkedQueue<>();
    for (int i = 0; i < GET_COUNT; i++) {
        SCHEDULER.execute(() -> {
            cancellables.add(pool.get(new FutureCallback<>()));
            getLatch.countDown();
        });
    }
    if (!getLatch.await(GET_TIMEOUT, TIME_UNIT)) {
        Assert.fail("Timed out awaiting for get");
    }
    Assert.assertEquals(cancellables.size(), GET_COUNT - 1);
    // Cancelling waiters should all succeed
    cancellables.stream().forEach(cancellable -> Assert.assertTrue(cancellable.cancel()));
    // Cancel the last waiter blocking item creation
    Assert.assertEquals(pool.cancelWaiters().size(), 1);
    createLatch.countDown();
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    pool.shutdown(shutdownCallback);
    shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Also used : Cancellable(com.linkedin.r2.util.Cancellable) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 17 with AsyncSharedPoolImpl

use of com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl in project rest.li by linkedin.

the class TestAsyncSharedPoolImpl method testGetOnSuccessCallbackThrows.

@Test
public void testGetOnSuccessCallbackThrows() throws Exception {
    AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
    pool.start();
    CountDownLatch onSuccessLatch = new CountDownLatch(1);
    pool.get(new Callback<Object>() {

        @Override
        public void onSuccess(Object result) {
            onSuccessLatch.countDown();
            throw new RuntimeException();
        }

        @Override
        public void onError(Throwable e) {
        }
    });
    if (!onSuccessLatch.await(GET_TIMEOUT, TIME_UNIT)) {
        Assert.fail("Callback onSuccess was not invoked");
    }
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Aggregations

AsyncSharedPoolImpl (com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl)17 Test (org.testng.annotations.Test)17 FutureCallback (com.linkedin.common.callback.FutureCallback)14 None (com.linkedin.common.util.None)12 CountDownLatch (java.util.concurrent.CountDownLatch)9 Cancellable (com.linkedin.r2.util.Cancellable)8 ExecutionException (java.util.concurrent.ExecutionException)8 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)5 ArrayList (java.util.ArrayList)3 PoolStats (com.linkedin.r2.transport.http.client.PoolStats)2 Callback (com.linkedin.common.callback.Callback)1