use of com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl in project rest.li by linkedin.
the class TestAsyncSharedPoolImpl method testMultipleGetItemSucceeds.
@Test
public void testMultipleGetItemSucceeds() throws Exception {
AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
pool.start();
final List<Object> items = new ArrayList<>(GET_COUNT);
for (int i = 0; i < GET_COUNT; i++) {
FutureCallback<Object> getCallback = new FutureCallback<>();
Cancellable cancellable = pool.get(getCallback);
// Operation should not be cancellable
Assert.assertNotNull(cancellable);
Assert.assertEquals(cancellable.cancel(), false);
Object item = getCallback.get(GET_TIMEOUT, TIME_UNIT);
Assert.assertNotNull(item);
items.add(item);
}
// All items should essentially be the same instance
Assert.assertEquals(items.size(), GET_COUNT);
items.stream().forEach(item -> Assert.assertSame(item, items.get(0)));
verifyStats(pool.getStats(), 1, GET_COUNT, 0, 0, 0, 0, 1, 0, 0);
// Put items back to the pool
IntStream.range(0, GET_COUNT).forEach(i -> pool.put(items.get(i)));
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 testGetOnErrorCallbackThrows.
@Test
public void testGetOnErrorCallbackThrows() throws Exception {
final LifecycleMock lifecycle = new LifecycleMock();
lifecycle.setCreateConsumer(callback -> callback.onError(new Throwable()));
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) {
}
@Override
public void onError(Throwable e) {
onSuccessLatch.countDown();
throw new RuntimeException();
}
});
if (!onSuccessLatch.await(GET_TIMEOUT, TIME_UNIT)) {
Assert.fail("Callback Error was not invoked");
}
}
use of com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl in project rest.li by linkedin.
the class TestAsyncSharedPoolImpl method testCancelWaiters.
@Test
public void testCancelWaiters() throws Exception {
final LifecycleMock lifecycle = new LifecycleMock();
final CountDownLatch latch = new CountDownLatch(1);
lifecycle.setCreateConsumer(callback -> {
try {
latch.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();
final CountDownLatch getLatch = new CountDownLatch(GET_COUNT - 1);
IntStream.range(0, GET_COUNT).forEach(i -> SCHEDULER.execute(() -> {
pool.get(new FutureCallback<>());
getLatch.countDown();
}));
if (!getLatch.await(GET_TIMEOUT, TIME_UNIT)) {
Assert.fail("Timed out awaiting for get");
}
Collection<Callback<Object>> waiters = pool.cancelWaiters();
Assert.assertNotNull(waiters);
Assert.assertEquals(waiters.size(), GET_COUNT);
verifyStats(pool.getStats(), 0, 0, 0, 0, 0, 0, 0, 0, 0);
latch.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 testShutdownWithMultiplePendingPut.
@Test
public void testShutdownWithMultiplePendingPut() throws Exception {
final AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
pool.start();
final CountDownLatch latch = new CountDownLatch(GET_COUNT);
final Collection<FutureCallback<Object>> getCallbacks = new ConcurrentLinkedQueue<>();
IntStream.range(0, GET_COUNT).forEach(i -> SCHEDULER.execute(() -> {
FutureCallback<Object> getCallback = new FutureCallback<>();
pool.get(getCallback);
getCallbacks.add(getCallback);
latch.countDown();
}));
if (!latch.await(OPERATION_TIMEOUT, TIME_UNIT)) {
Assert.fail("Timeout waiting for get calls");
}
final Collection<Object> items = new ConcurrentLinkedQueue<>();
getCallbacks.stream().forEach(callback -> {
try {
items.add(callback.get(GET_TIMEOUT, TIME_UNIT));
} catch (Exception e) {
}
});
Assert.assertEquals(items.size(), GET_COUNT);
verifyStats(pool.getStats(), 1, GET_COUNT, 0, 0, 0, 0, 1, 0, 0);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
pool.shutdown(shutdownCallback);
// Put items back to the pool
items.stream().forEach(item -> SCHEDULER.execute(() -> pool.put(item)));
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 testGetExceedMaxWaiters.
@Test
public void testGetExceedMaxWaiters() throws Exception {
final int maxWaiters = 5;
final CountDownLatch latch = new CountDownLatch(1);
final LifecycleMock lifecycle = new LifecycleMock();
lifecycle.setCreateConsumer(callback -> {
try {
latch.await();
callback.onSuccess(new Object());
} catch (Exception e) {
callback.onError(e);
}
});
AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, lifecycle, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, maxWaiters);
pool.start();
CountDownLatch getLatch = new CountDownLatch(maxWaiters - 1);
ConcurrentLinkedQueue<FutureCallback<Object>> callbacks = new ConcurrentLinkedQueue<>();
for (int i = 0; i < maxWaiters; i++) {
SCHEDULER.execute(() -> {
FutureCallback<Object> callback = new FutureCallback<>();
Cancellable cancellable = pool.get(callback);
Assert.assertNotNull(cancellable);
callbacks.add(callback);
getLatch.countDown();
});
}
getLatch.await(GET_TIMEOUT, TIME_UNIT);
FutureCallback<Object> waiterCallback = new FutureCallback<>();
Cancellable cancellable = pool.get(waiterCallback);
Assert.assertNotNull(cancellable);
Assert.assertFalse(cancellable.cancel());
try {
waiterCallback.get(GET_TIMEOUT, TIME_UNIT);
Assert.fail("Callback should fail but did not");
} catch (ExecutionException e) {
// Exception is recoverable and expected
}
latch.countDown();
callbacks.forEach(callback -> {
try {
Object item = callback.get();
Assert.assertNotNull(item);
pool.put(item);
} catch (Exception e) {
Assert.fail("Unexpected exception during #get()");
}
});
FutureCallback<None> shutdownCallback = new FutureCallback<>();
pool.shutdown(shutdownCallback);
shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Aggregations