Search in sources :

Example 11 with HttpNettyClient

use of com.linkedin.r2.transport.http.client.rest.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();
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) RestResponse(com.linkedin.r2.message.rest.RestResponse) HttpNettyClient(com.linkedin.r2.transport.http.client.rest.HttpNettyClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) ExecutionException(java.util.concurrent.ExecutionException) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException)

Example 12 with HttpNettyClient

use of com.linkedin.r2.transport.http.client.rest.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");
}
Also used : RequestContext(com.linkedin.r2.message.RequestContext) HttpNettyClient(com.linkedin.r2.transport.http.client.rest.HttpNettyClient) Test(org.testng.annotations.Test)

Example 13 with HttpNettyClient

use of com.linkedin.r2.transport.http.client.rest.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);
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) HttpProtocolVersion(com.linkedin.r2.transport.http.common.HttpProtocolVersion) RestResponse(com.linkedin.r2.message.rest.RestResponse) HttpNettyClient(com.linkedin.r2.transport.http.client.rest.HttpNettyClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 14 with HttpNettyClient

use of com.linkedin.r2.transport.http.client.rest.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);
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) HttpNettyClient(com.linkedin.r2.transport.http.client.rest.HttpNettyClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 15 with HttpNettyClient

use of com.linkedin.r2.transport.http.client.rest.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();
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) RestResponse(com.linkedin.r2.message.rest.RestResponse) HttpNettyClient(com.linkedin.r2.transport.http.client.rest.HttpNettyClient) EncoderException(io.netty.handler.codec.EncoderException) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) ChannelException(io.netty.channel.ChannelException) Test(org.testng.annotations.Test)

Aggregations

FutureCallback (com.linkedin.common.callback.FutureCallback)18 RequestContext (com.linkedin.r2.message.RequestContext)18 RestRequest (com.linkedin.r2.message.rest.RestRequest)17 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)17 HttpNettyClient (com.linkedin.r2.transport.http.client.rest.HttpNettyClient)16 ExecutionException (java.util.concurrent.ExecutionException)15 Test (org.testng.annotations.Test)15 RestResponse (com.linkedin.r2.message.rest.RestResponse)14 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)14 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)12 TimeoutException (java.util.concurrent.TimeoutException)11 None (com.linkedin.common.util.None)5 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)3 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)2 HttpNettyStreamClient (com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient)2 HttpProtocolVersion (com.linkedin.r2.transport.http.common.HttpProtocolVersion)2 ChannelException (io.netty.channel.ChannelException)2 EncoderException (io.netty.handler.codec.EncoderException)2