use of com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl in project rest.li by linkedin.
the class TestAsyncSharedPoolImpl method testGetWithNoWaiter.
@Test(expectedExceptions = ExecutionException.class)
public void testGetWithNoWaiter() throws Exception {
AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, NO_WAITER);
pool.start();
FutureCallback<Object> callback = new FutureCallback<>();
Cancellable cancellable = pool.get(callback);
Assert.assertNotNull(cancellable);
Assert.assertFalse(cancellable.cancel());
callback.get(GET_TIMEOUT, TIME_UNIT);
}
use of com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl in project rest.li by linkedin.
the class TestAsyncSharedPoolImpl method testMaximumConcurrency.
@Test(dataProvider = "lifecycles")
public void testMaximumConcurrency(AsyncPool.Lifecycle<Object> lifecycle) throws Exception {
final AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, lifecycle, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
pool.start();
CountDownLatch latch = new CountDownLatch(NUMBER_OF_THREADS);
IntStream.range(0, NUMBER_OF_THREADS).forEach(i -> SCHEDULER.execute(() -> {
try {
FutureCallback<Object> callback = new FutureCallback<>();
Assert.assertNotNull(pool.get(callback));
Object item = callback.get(GET_TIMEOUT, TIME_UNIT);
Assert.assertNotNull(item);
pool.put(item);
} catch (Exception e) {
} finally {
latch.countDown();
}
}));
if (!latch.await(OPERATION_TIMEOUT, TIME_UNIT)) {
Assert.fail("Timed out before tasks finish");
}
PoolStats stats = pool.getStats();
System.err.println("Total Created: " + stats.getTotalCreated());
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 testShutdownWithMultiplePendingPutValidationFails.
@Test
public void testShutdownWithMultiplePendingPutValidationFails() throws Exception {
final LifecycleMock lifecycleMock = new LifecycleMock();
lifecycleMock.setValidatePutSupplier(() -> false);
final AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, lifecycleMock, 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");
}
Assert.assertEquals(getCallbacks.size(), GET_COUNT);
final Collection<Object> items = new ConcurrentLinkedQueue<>();
getCallbacks.stream().forEach(callback -> {
try {
items.add(callback.get(GET_TIMEOUT, TIME_UNIT));
} catch (Exception e) {
e.printStackTrace();
}
});
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 testGetStats.
@Test
public void testGetStats() {
AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
PoolStats stats = pool.getStats();
Assert.assertNotNull(stats);
Assert.assertEquals(stats.getMaxPoolSize(), 1);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getIdleCount(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyed(), 0);
Assert.assertEquals(stats.getTotalTimedOut(), 0);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalBadDestroyed(), 0);
Assert.assertEquals(stats.getCheckedOut(), 0);
Assert.assertEquals(stats.getTotalCreated(), 0);
Assert.assertEquals(stats.getPoolSize(), 0);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), 0);
Assert.assertEquals(stats.getSampleMaxPoolSize(), 0);
Assert.assertEquals(stats.getWaitTime50Pct(), 0);
Assert.assertEquals(stats.getWaitTime95Pct(), 0);
Assert.assertEquals(stats.getWaitTime99Pct(), 0);
Assert.assertEquals(stats.getWaitTimeAvg(), 0.0);
}
use of com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl in project rest.li by linkedin.
the class TestAsyncSharedPoolImpl method testMixedPutAndDisposeItemSucceeds.
@Test
public void testMixedPutAndDisposeItemSucceeds() 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);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
pool.shutdown(shutdownCallback);
// Put items back to the pool
IntStream.range(0, GET_COUNT).forEach(i -> {
if (i % 2 == 0) {
pool.put(items.get(i));
} else {
pool.dispose(items.get(i));
}
});
shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Aggregations