use of com.linkedin.r2.netty.client.HttpNettyClient in project rest.li by linkedin.
the class TestHttpNettyClientCommon method testPerRequestTimeout.
/**
* Testing making request with custom-perRequest timeout, higher and lower than request timeout,
* d2 or http requests and check it is working
*/
@SuppressWarnings("unchecked")
@Test(dataProvider = "isStreamAndHigher")
public void testPerRequestTimeout(boolean isStream, boolean isHigherThanDefault) throws InterruptedException, IOException {
TestServer testServer = new TestServer();
int defaultRequestTimeout = 300;
int requestTimeoutPerRequest = isHigherThanDefault ? defaultRequestTimeout + 200 : defaultRequestTimeout - 200;
HttpClientBuilder clientBuilder = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(defaultRequestTimeout);
AbstractNettyClient<?, ?> client = isStream ? clientBuilder.buildStreamClient() : clientBuilder.buildRestClient();
RestRequest r = new RestRequestBuilder(testServer.getNoResponseURI()).build();
RequestContext requestContext = new RequestContext();
requestContext.putLocalAttr(R2Constants.REQUEST_TIMEOUT, requestTimeoutPerRequest);
long startTime = System.currentTimeMillis();
FutureCallback<?> cb = new FutureCallback<>();
if (isStream) {
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>((FutureCallback<StreamResponse>) cb);
client.streamRequest(Messages.toStreamRequest(r), requestContext, new HashMap<>(), callback);
} else {
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>((FutureCallback<RestResponse>) cb);
client.restRequest(r, requestContext, new HashMap<>(), callback);
}
try {
// This timeout needs to be significantly larger than the getTimeout of the netty client;
// we're testing that the client will generate its own timeout
cb.get(10, TimeUnit.SECONDS);
Assert.fail("Get was supposed to time out");
} catch (TimeoutException e) {
// TimeoutException means the timeout for Future.get() elapsed and nothing happened.
// Instead, we are expecting our callback to be invoked before the future timeout
// with a timeout generated by the HttpNettyClient.
Assert.fail("Unexpected TimeoutException, should have been ExecutionException", e);
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
long endTime = System.currentTimeMillis();
Assert.assertEquals((endTime - startTime) > defaultRequestTimeout, isHigherThanDefault, "The request timed out after " + (endTime - startTime) + "ms but it was supposed to be about " + (isHigherThanDefault ? "higher" : "lower") + " than " + defaultRequestTimeout + "ms");
// 150 ms of accuracy
Assert.assertTrue(// 150 ms of accuracy
(endTime - startTime) - requestTimeoutPerRequest < 150, "The request timed out after " + (endTime - startTime) + "ms but it was supposed to be about " + requestTimeoutPerRequest + "ms");
}
testServer.shutdown();
}
use of com.linkedin.r2.netty.client.HttpNettyClient in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testNoResponseTimeout.
@Test(dataProvider = "noResponseClients")
public void testNoResponseTimeout(AbstractNettyStreamClient client) throws Exception {
CountDownLatch responseLatch = new CountDownLatch(1);
Server server = new HttpServerBuilder().responseLatch(responseLatch).build();
try {
server.start();
RestRequest r = new RestRequestBuilder(new URI(URL)).build();
FutureCallback<StreamResponse> cb = new FutureCallback<>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
// This timeout needs to be significantly larger than the getTimeout of the netty client;
// we're testing that the client will generate its own timeout
cb.get(30, TimeUnit.SECONDS);
Assert.fail("Get was supposed to time out");
} catch (TimeoutException e) {
// TimeoutException means the timeout for Future.get() elapsed and nothing happened.
// Instead, we are expecting our callback to be invoked before the future timeout
// with a timeout generated by the HttpNettyClient.
Assert.fail("Unexpected TimeoutException, should have been ExecutionException", e);
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
} finally {
responseLatch.countDown();
server.stop();
}
}
use of com.linkedin.r2.netty.client.HttpNettyClient in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testNoChannelTimeout.
@Test
public void testNoChannelTimeout() throws InterruptedException {
HttpNettyStreamClient client = new HttpNettyStreamClient(new NoCreations(_scheduler), _scheduler, 500, 500);
RestRequest r = new RestRequestBuilder(URI.create("http://localhost/")).build();
FutureCallback<StreamResponse> cb = new FutureCallback<>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
try {
// This timeout needs to be significantly larger than the getTimeout of the netty client;
// we're testing that the client will generate its own timeout
cb.get(30, TimeUnit.SECONDS);
Assert.fail("Get was supposed to time out");
} catch (TimeoutException e) {
// TimeoutException means the timeout for Future.get() elapsed and nothing happened.
// Instead, we are expecting our callback to be invoked before the future timeout
// with a timeout generated by the HttpNettyClient.
Assert.fail("Unexpected TimeoutException, should have been ExecutionException", e);
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
}
}
use of com.linkedin.r2.netty.client.HttpNettyClient in project rest.li by linkedin.
the class TestHttpNettyClient method testResponseSize.
public void testResponseSize(int responseSize, int expectedResult) throws InterruptedException, IOException, TimeoutException {
TestServer testServer = new TestServer();
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(50000).setIdleTimeout(10000).setShutdownTimeout(500).setMaxResponseSize(TEST_MAX_RESPONSE_SIZE).buildRestClient();
RestRequest r = new RestRequestBuilder(testServer.getResponseOfSizeURI(responseSize)).build();
FutureCallback<RestResponse> cb = new FutureCallback<>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
client.restRequest(r, new RequestContext(), new HashMap<>(), callback);
try {
cb.get(30, TimeUnit.SECONDS);
if (expectedResult == TOO_LARGE) {
Assert.fail("Max response size exceeded, expected exception. ");
}
} catch (ExecutionException e) {
if (expectedResult == RESPONSE_OK) {
Assert.fail("Unexpected ExecutionException, response was <= max response size.");
}
verifyCauseChain(e, RemoteInvocationException.class, TooLongFrameException.class);
}
testServer.shutdown();
}
use of com.linkedin.r2.netty.client.HttpNettyClient in project rest.li by linkedin.
the class TestHttpNettyClient method testShutdown.
@Test
public void testShutdown() throws ExecutionException, TimeoutException, InterruptedException {
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(500).setIdleTimeout(10000).setShutdownTimeout(500).buildRestClient();
FutureCallback<None> shutdownCallback = new FutureCallback<>();
client.shutdown(shutdownCallback);
shutdownCallback.get(30, TimeUnit.SECONDS);
// Now verify a new request will also fail
RestRequest r = new RestRequestBuilder(URI.create("http://no.such.host.linkedin.com")).build();
FutureCallback<RestResponse> callback = new FutureCallback<>();
client.restRequest(r, new RequestContext(), new HashMap<>(), new TransportCallbackAdapter<>(callback));
try {
callback.get(30, TimeUnit.SECONDS);
} catch (ExecutionException e) {
// Expected
}
}
Aggregations