use of com.linkedin.r2.message.stream.StreamException in project rest.li by linkedin.
the class TestServerTimeout method testFilterNotCancelButShouldNotTimeout.
@Test
public void testFilterNotCancelButShouldNotTimeout() throws Exception {
RestRequest request = new RestRequestBuilder(Bootstrap.createHttpURI(PORT, STREAM_EXCEPTION_FILTER_URI)).setEntity(new byte[10240]).build();
_client.restRequest(request);
Future<RestResponse> futureResponse = _client.restRequest(request);
// if server times out, our second request would fail with TimeoutException because it's blocked by first one
try {
futureResponse.get(SERVER_IOHANDLER_TIMEOUT / 2, TimeUnit.MILLISECONDS);
Assert.fail("Should fail with ExecutionException");
} catch (ExecutionException ex) {
Assert.assertTrue(ex.getCause() instanceof RestException);
RestException restException = (RestException) ex.getCause();
Assert.assertTrue(restException.getResponse().getEntity().asString("UTF8").contains("StreamException in filter."));
}
}
use of com.linkedin.r2.message.stream.StreamException in project rest.li by linkedin.
the class TestStreamRequest method expectErrorCallback.
private static Callback<StreamResponse> expectErrorCallback(final CountDownLatch latch, final AtomicInteger status) {
return new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
if (e instanceof StreamException) {
StreamResponse errorResponse = ((StreamException) e).getResponse();
status.set(errorResponse.getStatus());
}
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
latch.countDown();
throw new RuntimeException("Should have failed with 404");
}
};
}
use of com.linkedin.r2.message.stream.StreamException in project rest.li by linkedin.
the class TestStreamFilterAdapters method testResponseFilterAdapterChangeError.
@Test
public void testResponseFilterAdapterChangeError() {
FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
}
@Override
public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
if (ex instanceof RestException) {
RestResponse res = ((RestException) ex).getResponse();
String newEntityStr = res.getEntity().asString("UTF8").replace('1', '0');
nextFilter.onError(new RestException((res.builder().setEntity(newEntityStr.getBytes()).build())), requestContext, wireAttrs);
} else {
nextFilter.onError(new IllegalStateException(), requestContext, wireAttrs);
}
}
});
fc.onStreamError(simpleStreamException("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
Throwable capturedEx = _beforeFilter.getThrowable();
Assert.assertTrue(capturedEx instanceof StreamException);
((StreamException) capturedEx).getResponse().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");
}
}));
fc.onStreamError(new IllegalArgumentException(), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
capturedEx = _beforeFilter.getThrowable();
Assert.assertTrue(capturedEx instanceof IllegalStateException);
}
use of com.linkedin.r2.message.stream.StreamException in project rest.li by linkedin.
the class TestStreamFilterAdapters method testResponseFilterAdapterPassThrough.
@Test
public void testResponseFilterAdapterPassThrough() {
FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onResponse(res, requestContext, wireAttrs);
}
@Override
public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onError(ex, requestContext, wireAttrs);
}
});
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"), "12345");
}
}));
fc.onStreamError(simpleStreamException("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
Throwable capturedEx = _beforeFilter.getThrowable();
Assert.assertTrue(capturedEx instanceof StreamException);
((StreamException) capturedEx).getResponse().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.r2.message.stream.StreamException in project rest.li by linkedin.
the class TestMessages method testToRestTransportCallbackRestException.
@Test
public void testToRestTransportCallbackRestException() {
TransportCallback<StreamResponse> streamCallback = response -> {
Assert.assertTrue(response.hasError());
Assert.assertNotNull(response.getError());
Assert.assertTrue(response.getError() instanceof StreamException);
Assert.assertNotNull(response.getWireAttributes());
Assert.assertEquals(response.getWireAttributes(), WIRE_ATTR);
};
TransportCallback<RestResponse> restCallback = Messages.toRestTransportCallback(streamCallback);
RestResponseBuilder builder = new RestResponseBuilder();
builder.setEntity(DATA);
RestResponse restResponse = builder.build();
restCallback.onResponse(TransportResponseImpl.error(new RestException(restResponse, new IllegalStateException()), WIRE_ATTR));
}
Aggregations