use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class TestServerTimeout method testServerTimeout.
@Test
public void testServerTimeout() throws Exception {
final StreamRequest request = new StreamRequestBuilder(getHttpUri(BUGGY_SERVER_URI)).build(EntityStreams.emptyStream());
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger status = new AtomicInteger(-1);
_client.streamRequest(request, new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
status.set(result.getStatus());
result.getEntityStream().setReader(new Reader() {
private ReadHandle _rh;
@Override
public void onInit(ReadHandle rh) {
_rh = rh;
_rh.request(Integer.MAX_VALUE);
}
@Override
public void onDataAvailable(ByteString data) {
// do nothing
}
@Override
public void onDone() {
// server would close the connection if TimeoutException, and netty would end the chunked transferring
// with an empty chunk
latch.countDown();
}
@Override
public void onError(Throwable e) {
latch.countDown();
}
});
}
});
// server should timeout so await should return true
Assert.assertTrue(latch.await(SERVER_IOHANDLER_TIMEOUT * 2, TimeUnit.MILLISECONDS));
Assert.assertEquals(status.get(), RestStatus.OK);
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class TestServerTimeoutAsyncEvent method testServerTimeoutAfterResponding.
@Test
public void testServerTimeoutAfterResponding() throws Exception {
Future<RestResponse> futureResponse = _client.restRequest(new RestRequestBuilder(getHttpUri(TIMEOUT_AFTER_SENDING_RESPONSE_SERVER_URI)).build());
// server should timeout so get should succeed
RestResponse response = futureResponse.get(ASYNC_EVENT_TIMEOUT * 2, TimeUnit.MILLISECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
Assert.assertEquals(response.getEntity().length(), RESPONSE_SIZE_WRITTEN_SO_FAR);
}
use of com.linkedin.r2.util.Timeout 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.util.Timeout 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.util.Timeout 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);
}
}
Aggregations