Search in sources :

Example 71 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestHttpClientFactory method testShutdownIOThread.

@Test(dataProvider = "configs")
public void testShutdownIOThread(boolean restOverStream, String protocolVersion) throws Exception {
    EventLoopGroup eventLoop = new NioEventLoopGroup();
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    ExecutorService callbackExecutor = Executors.newFixedThreadPool(1);
    HttpClientFactory factory = getHttpClientFactory(eventLoop, true, scheduler, true, callbackExecutor, false);
    CountDownLatch responseLatch = new CountDownLatch(1);
    Server server = new HttpServerBuilder().responseLatch(responseLatch).build();
    try {
        server.start();
        HashMap<String, String> properties = new HashMap<>();
        properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
        Client client = new TransportClientAdapter(factory.getClient(properties), restOverStream);
        URI uri = new URI(URI);
        Future<RestResponse> responseFuture = client.restRequest(new RestRequestBuilder(uri).build());
        FutureCallback<None> factoryShutdown = new FutureCallback<>();
        factory.shutdown(factoryShutdown);
        FutureCallback<None> clientShutdown = new FutureCallback<>();
        client.shutdown(clientShutdown);
        // Client and factory shutdowns are now pending.  When we release the latch, the response will
        // be returned, which causes the shutdowns to complete on the Netty IO thread that received the
        // response.
        responseLatch.countDown();
        clientShutdown.get(60, TimeUnit.SECONDS);
        factoryShutdown.get(60, TimeUnit.SECONDS);
    } finally {
        server.stop();
    }
    Assert.assertTrue(eventLoop.awaitTermination(30, TimeUnit.SECONDS), "Failed to shut down event-loop");
    Assert.assertTrue(scheduler.awaitTermination(60, TimeUnit.SECONDS), "Failed to shut down scheduler");
    callbackExecutor.shutdown();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) Client(com.linkedin.r2.transport.common.Client) None(com.linkedin.common.util.None) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 72 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestHttpClientFactory method testSuccessfulRequest.

@Test(dataProvider = "configsExpectedRequestCount")
public void testSuccessfulRequest(boolean restOverStream, String protocolVersion, int expectedRequests) throws Exception {
    EventLoopGroup eventLoop = new NioEventLoopGroup();
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    HttpClientFactory factory = getHttpClientFactory(eventLoop, true, scheduler, true);
    HttpServerBuilder.HttpServerStatsProvider httpServerStatsProvider = new HttpServerBuilder.HttpServerStatsProvider();
    Server server = new HttpServerBuilder().serverStatsProvider(httpServerStatsProvider).build();
    try {
        server.start();
        List<Client> clients = new ArrayList<>();
        int savedTimingKeyCount = TimingKey.getCount();
        for (int i = 0; i < 100; i++) {
            HashMap<String, String> properties = new HashMap<>();
            properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
            clients.add(new TransportClientAdapter(factory.getClient(properties), restOverStream));
        }
        int addedTimingKeyCount = TimingKey.getCount() - savedTimingKeyCount;
        // In current implementation, one client can have around 30 TimingKeys by default.
        Assert.assertTrue(addedTimingKeyCount >= 30 * clients.size());
        for (Client c : clients) {
            RestRequest r = new RestRequestBuilder(new URI(URI)).build();
            c.restRequest(r).get(30, TimeUnit.SECONDS);
        }
        Assert.assertEquals(httpServerStatsProvider.requestCount(), expectedRequests);
        savedTimingKeyCount = TimingKey.getCount();
        for (Client c : clients) {
            FutureCallback<None> callback = new FutureCallback<>();
            c.shutdown(callback);
            callback.get(30, TimeUnit.SECONDS);
        }
        FutureCallback<None> factoryShutdown = new FutureCallback<>();
        factory.shutdown(factoryShutdown);
        factoryShutdown.get(30, TimeUnit.SECONDS);
        int removedTimingKeyCount = savedTimingKeyCount - TimingKey.getCount();
        Assert.assertEquals(addedTimingKeyCount, removedTimingKeyCount);
    } finally {
        server.stop();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) ArrayList(java.util.ArrayList) URI(java.net.URI) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) RestRequest(com.linkedin.r2.message.rest.RestRequest) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) Client(com.linkedin.r2.transport.common.Client) None(com.linkedin.common.util.None) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 73 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder 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 74 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testMakingOutboundHttpsRequest.

@Test(enabled = false)
public void testMakingOutboundHttpsRequest() throws NoSuchAlgorithmException, InterruptedException, ExecutionException, TimeoutException {
    SSLContext context = SSLContext.getDefault();
    SSLParameters sslParameters = context.getDefaultSSLParameters();
    HttpNettyStreamClient client = new HttpClientBuilder(_eventLoop, _scheduler).setSSLContext(context).setSSLParameters(sslParameters).buildStreamClient();
    RestRequest r = new RestRequestBuilder(URI.create("https://www.howsmyssl.com/a/check")).build();
    FutureCallback<StreamResponse> cb = new FutureCallback<>();
    TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
    client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
    cb.get(30, TimeUnit.SECONDS);
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) SSLContext(javax.net.ssl.SSLContext) HttpNettyStreamClient(com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) SSLParameters(javax.net.ssl.SSLParameters) 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 75 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestNettyRequestAdapter method testRestToNettyRequestWithMultipleCookies.

@Test
public void testRestToNettyRequestWithMultipleCookies() throws Exception {
    RestRequestBuilder restRequestBuilder = new RestRequestBuilder(new URI(ANY_URI));
    restRequestBuilder.setCookies(ANY_COOKIES);
    RestRequest restRequest = restRequestBuilder.build();
    HttpRequest nettyRequest = NettyRequestAdapter.toNettyRequest(restRequest);
    Assert.assertEquals(nettyRequest.headers().get("Cookie"), ENCODED_COOKIES_HEADER_VALUE);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Aggregations

RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)336 RestRequest (com.linkedin.r2.message.rest.RestRequest)290 Test (org.testng.annotations.Test)267 URI (java.net.URI)220 RestResponse (com.linkedin.r2.message.rest.RestResponse)192 RequestContext (com.linkedin.r2.message.RequestContext)155 ExecutionException (java.util.concurrent.ExecutionException)55 ByteString (com.linkedin.data.ByteString)46 FutureCallback (com.linkedin.common.callback.FutureCallback)43 RestException (com.linkedin.r2.message.rest.RestException)42 HashMap (java.util.HashMap)36 TimeoutException (java.util.concurrent.TimeoutException)29 AfterTest (org.testng.annotations.AfterTest)26 BeforeTest (org.testng.annotations.BeforeTest)26 Callback (com.linkedin.common.callback.Callback)25 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)25 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)21 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)20