use of com.linkedin.r2.transport.http.client.NoopRateLimiter in project rest.li by linkedin.
the class TestAsyncPool method testGetStats.
@Test(retryAnalyzer = SingleRetry.class)
public void testGetStats() throws Exception {
final int POOL_SIZE = 25;
final int MIN_SIZE = 0;
final int MAX_WAITER_SIZE = Integer.MAX_VALUE;
final SettableClock clock = new SettableClock();
final LongTracking waitTimeTracker = new LongTracking();
final int GET = 20;
final int PUT_GOOD = 2;
final int PUT_BAD = 3;
final int DISPOSE = 4;
final int TIMEOUT = 100;
final int WAITER_TIMEOUT = 200;
final int DELAY = 1200;
final UnreliableLifecycle lifecycle = new UnreliableLifecycle();
final AsyncPool<AtomicBoolean> pool = new AsyncPoolImpl<>("object pool", lifecycle, POOL_SIZE, TIMEOUT, WAITER_TIMEOUT, _executor, MAX_WAITER_SIZE, AsyncPoolImpl.Strategy.MRU, MIN_SIZE, new NoopRateLimiter(), clock, waitTimeTracker);
PoolStats stats;
final List<AtomicBoolean> objects = new ArrayList<>();
pool.start();
// test values at initialization
stats = pool.getStats();
Assert.assertEquals(stats.getTotalCreated(), 0);
Assert.assertEquals(stats.getTotalDestroyed(), 0);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getCheckedOut(), 0);
Assert.assertEquals(stats.getTotalTimedOut(), 0);
Assert.assertEquals(stats.getTotalWaiterTimedOut(), 0);
Assert.assertEquals(stats.getTotalBadDestroyed(), 0);
Assert.assertEquals(stats.getMaxPoolSize(), POOL_SIZE);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getPoolSize(), 0);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), 0);
Assert.assertEquals(stats.getSampleMaxPoolSize(), 0);
// do a few gets
for (int i = 0; i < GET; i++) {
FutureCallback<AtomicBoolean> cb = new FutureCallback<>();
pool.get(cb);
AtomicBoolean obj = cb.get();
objects.add(obj);
}
clock.addDuration(SAMPLING_DURATION_INCREMENT);
stats = pool.getStats();
Assert.assertEquals(stats.getTotalCreated(), GET);
Assert.assertEquals(stats.getTotalDestroyed(), 0);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getCheckedOut(), GET);
Assert.assertEquals(stats.getTotalTimedOut(), 0);
Assert.assertEquals(stats.getTotalBadDestroyed(), 0);
Assert.assertEquals(stats.getMaxPoolSize(), POOL_SIZE);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getPoolSize(), GET);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), GET);
Assert.assertEquals(stats.getSampleMaxPoolSize(), GET);
// do some puts with good objects
for (int i = 0; i < PUT_GOOD; i++) {
AtomicBoolean obj = objects.remove(objects.size() - 1);
pool.put(obj);
}
clock.addDuration(SAMPLING_DURATION_INCREMENT);
stats = pool.getStats();
Assert.assertEquals(stats.getTotalCreated(), GET);
Assert.assertEquals(stats.getTotalDestroyed(), 0);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getCheckedOut(), GET - PUT_GOOD);
Assert.assertEquals(stats.getTotalTimedOut(), 0);
Assert.assertEquals(stats.getTotalBadDestroyed(), 0);
Assert.assertEquals(stats.getMaxPoolSize(), POOL_SIZE);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getPoolSize(), GET);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), GET);
Assert.assertEquals(stats.getSampleMaxPoolSize(), GET);
// do some puts with bad objects
for (int i = 0; i < PUT_BAD; i++) {
AtomicBoolean obj = objects.remove(objects.size() - 1);
// invalidate the object
obj.set(false);
pool.put(obj);
}
clock.addDuration(SAMPLING_DURATION_INCREMENT);
stats = pool.getStats();
Assert.assertEquals(stats.getTotalCreated(), GET);
Assert.assertEquals(stats.getTotalDestroyed(), PUT_BAD);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getCheckedOut(), GET - PUT_GOOD - PUT_BAD);
Assert.assertEquals(stats.getTotalTimedOut(), 0);
Assert.assertEquals(stats.getTotalBadDestroyed(), PUT_BAD);
Assert.assertEquals(stats.getMaxPoolSize(), POOL_SIZE);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getPoolSize(), GET - PUT_BAD);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), GET - PUT_GOOD);
Assert.assertEquals(stats.getSampleMaxPoolSize(), GET);
// do some disposes
for (int i = 0; i < DISPOSE; i++) {
AtomicBoolean obj = objects.remove(objects.size() - 1);
pool.dispose(obj);
}
clock.addDuration(SAMPLING_DURATION_INCREMENT);
stats = pool.getStats();
Assert.assertEquals(stats.getTotalCreated(), GET);
Assert.assertEquals(stats.getTotalDestroyed(), PUT_BAD + DISPOSE);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getCheckedOut(), GET - PUT_GOOD - PUT_BAD - DISPOSE);
Assert.assertEquals(stats.getTotalTimedOut(), 0);
Assert.assertEquals(stats.getTotalBadDestroyed(), PUT_BAD + DISPOSE);
Assert.assertEquals(stats.getMaxPoolSize(), POOL_SIZE);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getPoolSize(), GET - PUT_BAD - DISPOSE);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), GET - PUT_GOOD - PUT_BAD);
Assert.assertEquals(stats.getSampleMaxPoolSize(), GET - PUT_BAD);
// wait for a reap -- should destroy the PUT_GOOD objects
Thread.sleep(DELAY);
clock.addDuration(SAMPLING_DURATION_INCREMENT);
stats = pool.getStats();
Assert.assertEquals(stats.getTotalCreated(), GET);
Assert.assertEquals(stats.getTotalDestroyed(), PUT_GOOD + PUT_BAD + DISPOSE);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getCheckedOut(), GET - PUT_GOOD - PUT_BAD - DISPOSE);
Assert.assertEquals(stats.getTotalTimedOut(), PUT_GOOD);
Assert.assertEquals(stats.getTotalBadDestroyed(), PUT_BAD + DISPOSE);
Assert.assertEquals(stats.getMaxPoolSize(), POOL_SIZE);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getPoolSize(), GET - PUT_GOOD - PUT_BAD - DISPOSE);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), GET - PUT_GOOD - PUT_BAD - DISPOSE);
Assert.assertEquals(stats.getSampleMaxPoolSize(), GET - PUT_BAD - DISPOSE);
}
Aggregations