use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class HttpServerFactory method createRAPServer.
public HttpServer createRAPServer(int port, TransportDispatcher transportDispatcher, int timeout, boolean restOverStream) {
final TransportDispatcher filterDispatcher = new FilterChainDispatcher(transportDispatcher, _filters);
HttpServlet httpServlet = restOverStream ? new RAPStreamServlet(filterDispatcher, timeout, DEFAULT_LOG_SERVLET_EXCEPTIONS) : new RAPServlet(filterDispatcher);
return new HttpJettyServer(port, httpServlet);
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class TestAsyncPool method testGetStatsWithErrors.
@Test
public void testGetStatsWithErrors() throws Exception {
final int POOL_SIZE = 25;
final int GET = 20;
final int PUT_BAD = 5;
final int DISPOSE = 7;
final int CREATE_BAD = 9;
final int TIMEOUT = 100;
final UnreliableLifecycle lifecycle = new UnreliableLifecycle();
final AsyncPool<AtomicBoolean> pool = new AsyncPoolImpl<>("object pool", lifecycle, POOL_SIZE, TIMEOUT, _executor);
PoolStats stats;
final List<AtomicBoolean> objects = new ArrayList<>();
pool.start();
// 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);
}
// put and destroy some, with errors
lifecycle.setFail(true);
for (int i = 0; i < PUT_BAD; i++) {
AtomicBoolean obj = objects.remove(objects.size() - 1);
obj.set(false);
pool.put(obj);
}
for (int i = 0; i < DISPOSE; i++) {
AtomicBoolean obj = objects.remove(objects.size() - 1);
pool.dispose(obj);
}
stats = pool.getStats();
Assert.assertEquals(stats.getTotalDestroyed(), 0);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), PUT_BAD + DISPOSE);
Assert.assertEquals(stats.getTotalBadDestroyed(), PUT_BAD + DISPOSE);
// create some with errors
for (int i = 0; i < CREATE_BAD; i++) {
FutureCallback<AtomicBoolean> cb = new FutureCallback<>();
try {
pool.get(cb);
} catch (Exception e) {
// this error is expected
}
}
stats = pool.getStats();
Assert.assertEquals(stats.getCheckedOut(), GET - PUT_BAD - DISPOSE);
Assert.assertEquals(stats.getTotalCreateErrors(), CREATE_BAD);
}
use of com.linkedin.r2.util.Timeout 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.util.Timeout in project rest.li by linkedin.
the class TestDisruptor method testRestNoDisrupt.
@Test
public void testRestNoDisrupt() throws Exception {
System.out.println(_serverProvider);
final RequestContext requestContext = new RequestContext();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean success = new AtomicBoolean(false);
_client.restRequest(new RestRequestBuilder(getHttpURI()).build(), requestContext, new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse result) {
success.set(true);
latch.countDown();
}
@Override
public void onError(Throwable e) {
success.set(false);
latch.countDown();
}
});
Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
Assert.assertTrue(success.get(), "Unexpected transport response");
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class TestDisruptor method testRestLatencyDisrupt.
@Test
public void testRestLatencyDisrupt() throws Exception {
final RequestContext requestContext = new RequestContext();
requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.delay(REQUEST_LATENCY));
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean success = new AtomicBoolean(false);
_client.restRequest(new RestRequestBuilder(getHttpURI()).build(), requestContext, new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse result) {
success.set(true);
latch.countDown();
}
@Override
public void onError(Throwable e) {
success.set(false);
latch.countDown();
}
});
Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
Assert.assertTrue(success.get(), "Unexpected transport response");
}
Aggregations