Search in sources :

Example 1 with AsyncSharedPoolImpl

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

the class TestAsyncSharedPoolImpl method testStartShutdownSucceeds.

@Test
public void testStartShutdownSucceeds() throws Exception {
    AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
    pool.start();
    verifyStats(pool.getStats(), 0, 0, 0, 0, 0, 0, 0, 0, 0);
    FutureCallback<None> callback = new FutureCallback<>();
    pool.shutdown(callback);
    None none = callback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
    Assert.assertNotNull(none);
    Assert.assertSame(none, None.none());
    verifyStats(pool.getStats(), 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 2 with AsyncSharedPoolImpl

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

the class TestAsyncSharedPoolImpl method testSingleGetItemSucceeds.

@Test
public void testSingleGetItemSucceeds() throws Exception {
    AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
    pool.start();
    FutureCallback<Object> getCallback = new FutureCallback<>();
    Cancellable cancellable = pool.get(getCallback);
    Assert.assertNotNull(cancellable);
    Object item = getCallback.get(GET_TIMEOUT, TIME_UNIT);
    Assert.assertNotNull(item);
    verifyStats(pool.getStats(), 1, 1, 0, 0, 0, 0, 1, 0, 0);
    pool.put(item);
    verifyStats(pool.getStats(), 1, 0, 1, 0, 0, 0, 1, 0, 0);
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    pool.shutdown(shutdownCallback);
    shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) Cancellable(com.linkedin.r2.util.Cancellable) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 3 with AsyncSharedPoolImpl

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

the class TestAsyncSharedPoolImpl method testMultipleDisposeItemSucceeds.

@Test
public void testMultipleDisposeItemSucceeds() 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.dispose(items.get(i)));
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    pool.shutdown(shutdownCallback);
    shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) Cancellable(com.linkedin.r2.util.Cancellable) ArrayList(java.util.ArrayList) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 4 with AsyncSharedPoolImpl

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

the class TestAsyncSharedPoolImpl method testShutdownWithMultiplePendingDispose.

@Test
public void testShutdownWithMultiplePendingDispose() 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.dispose(item)));
    shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 5 with AsyncSharedPoolImpl

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

the class TestAsyncSharedPoolImpl method testGetItemCreateFails.

@Test(expectedExceptions = ExecutionException.class)
public void testGetItemCreateFails() throws Exception {
    final LifecycleMock lifecycle = new LifecycleMock();
    lifecycle.setCreateConsumer(callback -> callback.onError(new Exception("Simulated create failure")));
    AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, lifecycle, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
    pool.start();
    FutureCallback<Object> getCallback = new FutureCallback<>();
    Cancellable cancellable = pool.get(getCallback);
    Assert.assertNotNull(cancellable);
    verifyStats(pool.getStats(), 0, 0, 0, 0, 0, 0, 0, 1, 0);
    getCallback.get(GET_TIMEOUT, TIME_UNIT);
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) Cancellable(com.linkedin.r2.util.Cancellable) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) 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