use of com.linkedin.r2.message.stream.entitystream.DrainReader in project rest.li by linkedin.
the class TestServerTimeout method setup.
@BeforeClass
public void setup() throws IOException {
_clientFactory = new HttpClientFactory();
Map<String, Object> clientProperties = new HashMap<String, Object>();
clientProperties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(SERVER_IOHANDLER_TIMEOUT * 20));
clientProperties.put(HttpClientFactory.HTTP_POOL_MIN_SIZE, "1");
clientProperties.put(HttpClientFactory.HTTP_POOL_SIZE, "1");
_client = new TransportClientAdapter(_clientFactory.getClient(clientProperties), true);
final Map<URI, StreamRequestHandler> handlers = new HashMap<URI, StreamRequestHandler>();
handlers.put(BUGGY_SERVER_URI, new BuggyRequestHandler());
handlers.put(THROW_BUT_SHOULD_NOT_TIMEOUT_URI, new ThrowHandler());
handlers.put(BUGGY_FILTER_URI, new NormalHandler());
TransportDispatcher transportDispatcher = new TransportDispatcher() {
@Override
public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
throw new UnsupportedOperationException("This dispatcher only supports stream");
}
@Override
public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
StreamRequestHandler handler = handlers.get(req.getURI());
if (handler != null) {
handler.handleRequest(req, requestContext, new TransportCallbackAdapter<StreamResponse>(callback));
} else {
req.getEntityStream().setReader(new DrainReader());
callback.onResponse(TransportResponseImpl.<StreamResponse>error(new IllegalStateException("Handler not found for URI " + req.getURI())));
}
}
};
FilterChain filterChain = FilterChains.createStreamChain(new BuggyFilter());
_server = new HttpServerFactory(filterChain).createRAPServer(PORT, transportDispatcher, SERVER_IOHANDLER_TIMEOUT, true);
_server.start();
}
use of com.linkedin.r2.message.stream.entitystream.DrainReader in project rest.li by linkedin.
the class TestServerTimeoutAsyncEvent method setup.
@BeforeClass
public void setup() throws IOException {
_clientFactory = new HttpClientFactory();
Map<String, Object> clientProperties = new HashMap<String, Object>();
clientProperties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(ASYNC_EVENT_TIMEOUT * 20));
clientProperties.put(HttpClientFactory.HTTP_POOL_MIN_SIZE, "1");
clientProperties.put(HttpClientFactory.HTTP_POOL_SIZE, "1");
_client = new TransportClientAdapter(_clientFactory.getClient(clientProperties), true);
final Map<URI, StreamRequestHandler> handlers = new HashMap<URI, StreamRequestHandler>();
handlers.put(TIMEOUT_BEFORE_SENDING_RESPONSE_SERVER_URI, new TimeoutBeforeRespondingRequestHandler());
handlers.put(TIMEOUT_AFTER_SENDING_RESPONSE_SERVER_URI, new TimeoutAfterRespondingRequestHandler());
handlers.put(THROW_BUT_SHOULD_NOT_TIMEOUT_URI, new ThrowHandler());
handlers.put(BUGGY_FILTER_URI, new NormalHandler());
TransportDispatcher transportDispatcher = new TransportDispatcher() {
@Override
public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
throw new UnsupportedOperationException("This dispatcher only supports stream");
}
@Override
public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
StreamRequestHandler handler = handlers.get(req.getURI());
if (handler != null) {
handler.handleRequest(req, requestContext, new TransportCallbackAdapter<StreamResponse>(callback));
} else {
req.getEntityStream().setReader(new DrainReader());
callback.onResponse(TransportResponseImpl.<StreamResponse>error(new IllegalStateException("Handler not found for URI " + req.getURI())));
}
}
};
FilterChain filterChain = FilterChains.createStreamChain(new BuggyFilter());
_server = new HttpServerFactory(filterChain, HttpJettyServer.ServletType.ASYNC_EVENT).createServer(PORT, transportDispatcher, ASYNC_EVENT_TIMEOUT, true);
_server.start();
_asyncExecutor = Executors.newSingleThreadExecutor();
}
use of com.linkedin.r2.message.stream.entitystream.DrainReader in project rest.li by linkedin.
the class TestChannelPoolBehavior method testChannelReuse.
@Test
public void testChannelReuse() throws Exception {
_client2.streamRequest(new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, NOT_FOUND_URI)).build(EntityStreams.newEntityStream(new SlowWriter())), new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
if (e instanceof StreamException) {
StreamException streamException = (StreamException) e;
streamException.getResponse().getEntityStream().setReader(new CancelingReader());
}
throw new RuntimeException(e);
}
@Override
public void onSuccess(StreamResponse result) {
result.getEntityStream().setReader(new DrainReader());
}
});
Future<RestResponse> responseFuture = _client2.restRequest(new RestRequestBuilder(Bootstrap.createHttpURI(PORT, NORMAL_URI)).build());
RestResponse response = responseFuture.get(WRITER_DELAY * 1000, TimeUnit.MILLISECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
}
use of com.linkedin.r2.message.stream.entitystream.DrainReader in project rest.li by linkedin.
the class TestServerTimeoutAsyncEvent method getTransportDispatcher.
@Override
protected TransportDispatcher getTransportDispatcher() {
final Map<URI, StreamRequestHandler> handlers = new HashMap<>();
handlers.put(TIMEOUT_BEFORE_SENDING_RESPONSE_SERVER_URI, new TimeoutBeforeRespondingRequestHandler());
handlers.put(TIMEOUT_AFTER_SENDING_RESPONSE_SERVER_URI, new TimeoutAfterRespondingRequestHandler());
handlers.put(THROW_BUT_SHOULD_NOT_TIMEOUT_URI, new ThrowHandler());
handlers.put(BUGGY_FILTER_URI, new NormalHandler());
return new TransportDispatcher() {
@Override
public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
throw new UnsupportedOperationException("This dispatcher only supports stream");
}
@Override
public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
StreamRequestHandler handler = handlers.get(req.getURI());
if (handler != null) {
handler.handleRequest(req, requestContext, new TransportCallbackAdapter<>(callback));
} else {
req.getEntityStream().setReader(new DrainReader());
callback.onResponse(TransportResponseImpl.<StreamResponse>error(new IllegalStateException("Handler not found for URI " + req.getURI())));
}
}
};
}
use of com.linkedin.r2.message.stream.entitystream.DrainReader in project rest.li by linkedin.
the class TestHttp2NettyStreamClient method testChannelReusedAfterStreamingTimeout.
/**
* When a request fails due to {@link TimeoutException}, connection should not be destroyed.
* @throws Exception
*/
@Test(timeOut = TEST_TIMEOUT, retryAnalyzer = SingleRetry.class)
public void testChannelReusedAfterStreamingTimeout() throws Exception {
final HttpServerBuilder.HttpServerStatsProvider statsProvider = new HttpServerBuilder.HttpServerStatsProvider();
final HttpServerBuilder serverBuilder = new HttpServerBuilder();
final Server server = serverBuilder.serverStatsProvider(statsProvider).stopTimeout(0).build();
final HttpClientBuilder clientBuilder = new HttpClientBuilder(_eventLoop, _scheduler);
final Http2NettyStreamClient client = clientBuilder.setRequestTimeout(1000).buildHttp2StreamClient();
final TransportResponse<StreamResponse> response1;
final TransportResponse<StreamResponse> response2;
try {
server.start();
final StreamRequestBuilder builder1 = new StreamRequestBuilder(new URI(URL));
final StreamRequest request1 = builder1.setMethod(METHOD).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[REQUEST_SIZE]))));
final FutureTransportCallback<StreamResponse> callback1 = new FutureTransportCallback<>();
client.streamRequest(request1, new RequestContext(), new HashMap<>(), callback1);
response1 = callback1.get();
Assert.assertNotNull(response1);
Assert.assertFalse(response1.hasError());
response1.getResponse().getEntityStream().setReader(new TimeoutReader());
final StreamRequestBuilder builder2 = new StreamRequestBuilder(new URI(URL));
final StreamRequest request2 = builder2.setMethod(METHOD).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[REQUEST_SIZE]))));
final FutureTransportCallback<StreamResponse> callback2 = new FutureTransportCallback<>();
client.streamRequest(request2, new RequestContext(), new HashMap<>(), callback2);
response2 = callback2.get();
} finally {
server.stop();
}
// The 2nd request should succeed
Assert.assertNotNull(response2);
Assert.assertFalse(response2.hasError());
response2.getResponse().getEntityStream().setReader(new DrainReader());
// The server should have seen 3 requests but establishes only 1 connection with the client
Assert.assertEquals(statsProvider.requestCount(), 3);
Assert.assertEquals(statsProvider.clientConnections().size(), 1);
}
Aggregations