use of com.linkedin.common.callback.Callback 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.common.callback.Callback in project rest.li by linkedin.
the class TestStreamFilterAdapters method testRequestFilterAdapterPassThrough.
@Test
public void testRequestFilterAdapterPassThrough() {
FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onRequest(req, requestContext, wireAttrs);
}
});
fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
StreamRequest capturedReq = _afterFilter.getRequest();
Assert.assertEquals(capturedReq.getURI(), SIMPLE_URI);
capturedReq.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
Assert.fail("shouldn't have error");
}
@Override
public void onSuccess(ByteString result) {
Assert.assertEquals(result.asString("UTF8"), "12345");
}
}));
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestStreamFilterAdapters method testRequestFilterAdapterCallsOnError.
@Test
public void testRequestFilterAdapterCallsOnError() {
final Exception runTimeException = new RuntimeException();
FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onError(runTimeException, requestContext, wireAttrs);
}
});
fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
Throwable ex = _beforeFilter.getThrowable();
Assert.assertSame(ex, runTimeException);
fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onError(simpleRestException(req.getEntity().asString("UTF8")), requestContext, wireAttrs);
}
});
fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
ex = _beforeFilter.getThrowable();
Assert.assertTrue(ex instanceof StreamException);
StreamResponse errorResponse = ((StreamException) ex).getResponse();
errorResponse.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
Assert.fail("should not happen");
}
@Override
public void onSuccess(ByteString result) {
Assert.assertEquals(result.asString("UTF8"), "12345");
}
}));
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestStreamFilterAdapters method testRequestFilterAdapterChangeRequest.
@Test
public void testRequestFilterAdapterChangeRequest() {
FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onRequest(req.builder().setEntity(req.getEntity().asString("UTF8").replace('1', '0').getBytes()).build(), requestContext, wireAttrs);
}
});
fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
StreamRequest capturedReq = _afterFilter.getRequest();
Assert.assertEquals(capturedReq.getURI(), SIMPLE_URI);
capturedReq.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
Assert.fail("shouldn't have error");
}
@Override
public void onSuccess(ByteString result) {
Assert.assertEquals(result.asString("UTF8"), "02345");
}
}));
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestStreamFilterAdapters method testResponseFilterAdapterChangeResponse.
@Test
public void testResponseFilterAdapterChangeResponse() {
FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onResponse(res.builder().setEntity(res.getEntity().asString("UTF8").replace('1', '0').getBytes()).build(), requestContext, wireAttrs);
}
@Override
public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
}
});
fc.onStreamResponse(simpleStreamResponse("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
StreamResponse capturedResponse = _beforeFilter.getResponse();
capturedResponse.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
Assert.fail("should not happen");
}
@Override
public void onSuccess(ByteString result) {
Assert.assertEquals(result.asString("UTF8"), "02345");
}
}));
}
Aggregations