use of java.util.concurrent.TimeoutException in project rest.li by linkedin.
the class TestHttpNettyClient method testNoChannelTimeout.
@Test
public void testNoChannelTimeout() throws InterruptedException {
HttpNettyClient client = new HttpNettyClient(new NoCreations(_scheduler), _scheduler, 500, 500, 1024 * 1024 * 2);
RestRequest r = new RestRequestBuilder(URI.create("http://localhost/")).build();
FutureCallback<RestResponse> cb = new FutureCallback<RestResponse>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(cb);
client.restRequest(r, new RequestContext(), new HashMap<String, String>(), 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 java.util.concurrent.TimeoutException in project rest.li by linkedin.
the class TestHttpNettyClient method testShutdownStuckInPool.
@Test
public void testShutdownStuckInPool() throws InterruptedException, ExecutionException, TimeoutException {
// Test that shutdown works when the outstanding request is stuck in the pool waiting for a channel
HttpNettyClient client = new HttpNettyClient(new NoCreations(_scheduler), _scheduler, 60000, 1, 1024 * 1024 * 2);
RestRequest r = new RestRequestBuilder(URI.create("http://some.host/")).build();
FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
client.restRequest(r, new RequestContext(), new HashMap<String, String>(), new TransportCallbackAdapter<RestResponse>(futureCallback));
FutureCallback<None> shutdownCallback = new FutureCallback<None>();
client.shutdown(shutdownCallback);
shutdownCallback.get(30, TimeUnit.SECONDS);
try {
futureCallback.get(30, TimeUnit.SECONDS);
Assert.fail("get should have thrown exception");
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
}
}
use of java.util.concurrent.TimeoutException 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<StreamResponse>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), 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 java.util.concurrent.TimeoutException in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testSlowReaderTimeout.
/**
* Tests slow EntityStream {@link Reader} implementation should be subject to streaming timeout even
* if the entire response entity can be buffered in memory.
*
* @throws Exception
*/
@Test(dataProvider = "slowReaderTimeoutClientProvider")
public void testSlowReaderTimeout(AbstractNettyStreamClient client) throws Exception {
// Sets the response size to be greater than zero but smaller than the in-memory buffer for HTTP/1.1
// and smaller than the receiving window size for HTTP/2 so the receiver will not block sender
Server server = new HttpServerBuilder().responseSize(R2Constants.DEFAULT_DATA_CHUNK_SIZE).build();
StreamRequest request = new StreamRequestBuilder(new URI(URL)).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.emptyStream());
final CountDownLatch responseLatch = new CountDownLatch(1);
final CountDownLatch streamLatch = new CountDownLatch(1);
final AtomicReference<TransportResponse<StreamResponse>> atomicTransportResponse = new AtomicReference<>();
final AtomicReference<Throwable> atomicThrowable = new AtomicReference<>();
try {
server.start();
client.streamRequest(request, new RequestContext(), new HashMap<>(), response -> {
atomicTransportResponse.set(response);
responseLatch.countDown();
response.getResponse().getEntityStream().setReader(new Reader() {
@Override
public void onInit(ReadHandle rh) {
}
@Override
public void onDataAvailable(ByteString data) {
}
@Override
public void onDone() {
}
@Override
public void onError(Throwable e) {
atomicThrowable.set(e);
streamLatch.countDown();
}
});
});
} finally {
responseLatch.await(5, TimeUnit.SECONDS);
streamLatch.await(5, TimeUnit.SECONDS);
server.stop();
}
TransportResponse<StreamResponse> transportResponse = atomicTransportResponse.get();
Assert.assertNotNull(transportResponse, "Expected to receive a response");
Assert.assertFalse(transportResponse.hasError(), "Expected to receive a response without error");
Assert.assertNotNull(transportResponse.getResponse());
Assert.assertNotNull(transportResponse.getResponse().getEntityStream());
Throwable throwable = atomicThrowable.get();
Assert.assertNotNull(throwable, "Expected onError invoked with TimeoutException");
Assert.assertTrue(throwable instanceof RemoteInvocationException);
Assert.assertNotNull(throwable.getCause());
Assert.assertTrue(throwable.getCause() instanceof TimeoutException);
}
use of java.util.concurrent.TimeoutException in project rest.li by linkedin.
the class TestHttpNettyClient method testShutdownRequestOutstanding.
private void testShutdownRequestOutstanding(int shutdownTimeout, int requestTimeout, Class<?>... causeChain) throws InterruptedException, IOException, ExecutionException, TimeoutException {
TestServer testServer = new TestServer();
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(requestTimeout).setShutdownTimeout(shutdownTimeout).buildRest();
RestRequest r = new RestRequestBuilder(testServer.getNoResponseURI()).build();
FutureCallback<RestResponse> cb = new FutureCallback<RestResponse>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(cb);
client.restRequest(r, new RequestContext(), new HashMap<String, String>(), callback);
FutureCallback<None> shutdownCallback = new FutureCallback<None>();
client.shutdown(shutdownCallback);
shutdownCallback.get(30, TimeUnit.SECONDS);
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("Get timed out, should have thrown ExecutionException", e);
} catch (ExecutionException e) {
verifyCauseChain(e, causeChain);
}
testServer.shutdown();
}
Aggregations