use of com.linkedin.r2.netty.client.HttpNettyClient 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).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);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
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();
}
use of com.linkedin.r2.netty.client.HttpNettyClient in project rest.li by linkedin.
the class TestHttpNettyClient method testUnsupportedStreamRequest.
@Test(expectedExceptions = UnsupportedOperationException.class)
public void testUnsupportedStreamRequest() throws UnsupportedOperationException {
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).buildRestClient();
client.streamRequest(null, new RequestContext(), new HashMap<>(), null);
Assert.fail("The Http Rest client should throw UnsupportedOperationException when streamRequest is called");
}
use of com.linkedin.r2.netty.client.HttpNettyClient in project rest.li by linkedin.
the class TestHttpNettyClient method testRequestContextAttributes.
@Test
public void testRequestContextAttributes() throws InterruptedException, IOException, TimeoutException {
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).buildRestClient();
RestRequest r = new RestRequestBuilder(URI.create("http://localhost")).build();
FutureCallback<RestResponse> cb = new FutureCallback<>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
RequestContext requestContext = new RequestContext();
client.restRequest(r, requestContext, new HashMap<>(), callback);
final String actualRemoteAddress = (String) requestContext.getLocalAttr(R2Constants.REMOTE_SERVER_ADDR);
final int actualRemotePort = (int) requestContext.getLocalAttr(R2Constants.REMOTE_SERVER_PORT);
final HttpProtocolVersion actualProtocolVersion = (HttpProtocolVersion) requestContext.getLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION);
Assert.assertTrue("127.0.0.1".equals(actualRemoteAddress) || "0:0:0:0:0:0:0:1".equals(actualRemoteAddress), "Actual remote client address is not expected. " + "The local attribute field must be IP address in string type");
Assert.assertEquals(actualRemotePort, 80);
Assert.assertEquals(actualProtocolVersion, HttpProtocolVersion.HTTP_1_1);
}
use of com.linkedin.r2.netty.client.HttpNettyClient 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);
RestRequest r = new RestRequestBuilder(URI.create("http://some.host/")).build();
FutureCallback<RestResponse> futureCallback = new FutureCallback<>();
client.restRequest(r, new RequestContext(), new HashMap<>(), new TransportCallbackAdapter<>(futureCallback));
FutureCallback<None> shutdownCallback = new FutureCallback<>();
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 com.linkedin.r2.netty.client.HttpNettyClient in project rest.li by linkedin.
the class TestHttpNettyClient method testSendBadHeader.
@Test
public void testSendBadHeader() throws Exception {
TestServer testServer = new TestServer();
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(10000).setIdleTimeout(10000).setShutdownTimeout(500).buildRestClient();
RestRequestBuilder rb = new RestRequestBuilder(testServer.getRequestURI());
rb.setHeader("x", "makenettyunhappy\u000Bblah");
RestRequest request = rb.build();
FutureCallback<RestResponse> cb = new FutureCallback<>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
client.restRequest(request, new RequestContext(), new HashMap<>(), callback);
try {
cb.get(30, TimeUnit.SECONDS);
Assert.fail("Should fail sending request");
} catch (TimeoutException ex) {
Assert.fail("Unexpected TimeoutException, should have been ExecutionException", ex);
} catch (ExecutionException ex) {
verifyCauseChain(ex, RemoteInvocationException.class, ChannelException.class, EncoderException.class, IllegalArgumentException.class);
}
testServer.shutdown();
}
Aggregations