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);
}
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");
}
}
Aggregations