Search in sources :

Example 1 with HttpNettyClient

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

Example 2 with HttpNettyClient

use of com.linkedin.r2.transport.http.client.rest.HttpNettyClient 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();
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) Server(org.eclipse.jetty.server.Server) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) 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) Test(org.testng.annotations.Test)

Example 3 with HttpNettyClient

use of com.linkedin.r2.transport.http.client.rest.HttpNettyClient 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);
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) HttpNettyStreamClient(com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient) 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) Test(org.testng.annotations.Test)

Example 4 with HttpNettyClient

use of com.linkedin.r2.transport.http.client.rest.HttpNettyClient in project rest.li by linkedin.

the class TestHttpNettyClient method testResponseSize.

public void testResponseSize(int responseSize, int expectedResult) throws InterruptedException, IOException, TimeoutException {
    TestServer testServer = new TestServer();
    HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(50000).setIdleTimeout(10000).setShutdownTimeout(500).setMaxResponseSize(TEST_MAX_RESPONSE_SIZE).buildRestClient();
    RestRequest r = new RestRequestBuilder(testServer.getResponseOfSizeURI(responseSize)).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);
        if (expectedResult == TOO_LARGE) {
            Assert.fail("Max response size exceeded, expected exception. ");
        }
    } catch (ExecutionException e) {
        if (expectedResult == RESPONSE_OK) {
            Assert.fail("Unexpected ExecutionException, response was <= max response size.");
        }
        verifyCauseChain(e, RemoteInvocationException.class, TooLongFrameException.class);
    }
    testServer.shutdown();
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) 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) FutureCallback(com.linkedin.common.callback.FutureCallback)

Example 5 with HttpNettyClient

use of com.linkedin.r2.transport.http.client.rest.HttpNettyClient in project rest.li by linkedin.

the class TestHttpNettyClient method testShutdown.

@Test
public void testShutdown() throws ExecutionException, TimeoutException, InterruptedException {
    HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(500).setIdleTimeout(10000).setShutdownTimeout(500).buildRestClient();
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    client.shutdown(shutdownCallback);
    shutdownCallback.get(30, TimeUnit.SECONDS);
    // Now verify a new request will also fail
    RestRequest r = new RestRequestBuilder(URI.create("http://no.such.host.linkedin.com")).build();
    FutureCallback<RestResponse> callback = new FutureCallback<>();
    client.restRequest(r, new RequestContext(), new HashMap<>(), new TransportCallbackAdapter<>(callback));
    try {
        callback.get(30, TimeUnit.SECONDS);
    } catch (ExecutionException e) {
    // Expected
    }
}
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) ExecutionException(java.util.concurrent.ExecutionException) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) 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