use of com.linkedin.r2.transport.http.client.HttpClientBuilder in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testUnsupportedRestRequest.
@Test(expectedExceptions = UnsupportedOperationException.class)
public void testUnsupportedRestRequest() throws UnsupportedOperationException {
TransportClient client = new HttpClientBuilder(_eventLoop, _scheduler).buildStreamClient();
client.restRequest(null, new RequestContext(), new HashMap<>(), null);
Assert.fail("The Http Stream clients should throw UnsupportedOperationException when streamRequest is called");
}
use of com.linkedin.r2.transport.http.client.HttpClientBuilder in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testUnsupportedRestRequestHttp2.
@Test(expectedExceptions = UnsupportedOperationException.class)
public void testUnsupportedRestRequestHttp2() throws UnsupportedOperationException {
TransportClient client = new HttpClientBuilder(_eventLoop, _scheduler).buildHttp2StreamClient();
client.restRequest(null, new RequestContext(), new HashMap<>(), null);
Assert.fail("The Http Stream clients should throw UnsupportedOperationException when streamRequest is called");
}
use of com.linkedin.r2.transport.http.client.HttpClientBuilder in project rest.li by linkedin.
the class TestHttpNettyClient method testNoResponseTimeout.
@Test
public void testNoResponseTimeout() throws InterruptedException, IOException {
TestServer testServer = new TestServer();
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(500).setIdleTimeout(10000).setShutdownTimeout(500).buildRestClient();
RestRequest r = new RestRequestBuilder(testServer.getNoResponseURI()).build();
FutureCallback<RestResponse> cb = new FutureCallback<>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
client.restRequest(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);
}
testServer.shutdown();
}
use of com.linkedin.r2.transport.http.client.HttpClientBuilder in project rest.li by linkedin.
the class TestHttpNettyClient method testBadAddress.
@Test
public void testBadAddress() throws InterruptedException, IOException, TimeoutException {
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(30000).setIdleTimeout(10000).setShutdownTimeout(500).buildRestClient();
RestRequest r = new RestRequestBuilder(URI.create("http://this.host.does.not.exist.linkedin.com")).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);
Assert.fail("Get was supposed to fail");
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, UnknownHostException.class);
}
}
use of com.linkedin.r2.transport.http.client.HttpClientBuilder in project rest.li by linkedin.
the class TestHttp2NettyStreamClient method testRequestTimeout.
/**
* Tests the condition that when a client request times out before the request is processed
* by the server, the servlet implementation throws when attempting to read the request entity.
*/
@Test(enabled = false)
public void testRequestTimeout() throws Exception {
final AtomicInteger serverIOExceptions = new AtomicInteger(0);
final CountDownLatch exceptionLatch = new CountDownLatch(1);
final CountDownLatch responseLatch = new CountDownLatch(1);
final CountDownLatch serverLatch = new CountDownLatch(1);
final HttpServerBuilder serverBuilder = new HttpServerBuilder();
final Server server = serverBuilder.exceptionListener(throwable -> {
if (throwable instanceof IOException) {
serverIOExceptions.incrementAndGet();
exceptionLatch.countDown();
}
}).responseLatch(serverLatch).build();
final HttpClientBuilder clientBuilder = new HttpClientBuilder(_eventLoop, _scheduler);
final Http2NettyStreamClient client = clientBuilder.setRequestTimeout(500).buildHttp2StreamClient();
try {
server.start();
// Sends the stream request
final StreamRequestBuilder builder = new StreamRequestBuilder(new URI(URL));
final StreamRequest request = builder.setMethod(METHOD).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[REQUEST_SIZE]))));
client.streamRequest(request, new RequestContext(), new HashMap<>(), response -> responseLatch.countDown());
// Waits for request to timeout
Thread.sleep(1000);
// Allows server to process request
serverLatch.countDown();
} finally {
if (!responseLatch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS)) {
Assert.fail("Timeout waiting for response latch");
}
if (!exceptionLatch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS)) {
Assert.fail("Timeout waiting for exception latch");
}
server.stop();
}
// Expects two IOExceptions thrown by the server. One for the initial OPTIONS upgrade request and one for
// the actual GET request.
Assert.assertEquals(serverIOExceptions.get(), 2);
}
Aggregations