Search in sources :

Example 1 with HttpServerBuilder

use of com.linkedin.r2.testutils.server.HttpServerBuilder in project rest.li by linkedin.

the class TestHttpClientFactory method testShutdownAfterClients.

@Test
public void testShutdownAfterClients() throws Exception {
    NioEventLoopGroup eventLoop = new NioEventLoopGroup();
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    HttpClientFactory factory = getHttpClientFactory(eventLoop, true, scheduler, true);
    Server server = new HttpServerBuilder().build();
    try {
        server.start();
        List<Client> clients = new ArrayList<Client>();
        for (int i = 0; i < 1; i++) {
            clients.add(new TransportClientAdapter(factory.getClient(Collections.<String, String>emptyMap()), true));
        }
        for (Client c : clients) {
            RestRequest r = new RestRequestBuilder(new URI(URI)).build();
            FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
            c.restRequest(r, futureCallback);
            futureCallback.get(30, TimeUnit.SECONDS);
        }
        for (Client c : clients) {
            FutureCallback<None> callback = new FutureCallback<None>();
            c.shutdown(callback);
            callback.get(30, TimeUnit.SECONDS);
        }
        FutureCallback<None> factoryShutdown = new FutureCallback<None>();
        factory.shutdown(factoryShutdown);
        factoryShutdown.get(30, TimeUnit.SECONDS);
        Assert.assertTrue(eventLoop.awaitTermination(30, TimeUnit.SECONDS), "Failed to shut down event-loop");
        Assert.assertTrue(scheduler.awaitTermination(30, TimeUnit.SECONDS), "Failed to shut down scheduler");
    } finally {
        server.stop();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Server(org.eclipse.jetty.server.Server) RestResponse(com.linkedin.r2.message.rest.RestResponse) ArrayList(java.util.ArrayList) URI(java.net.URI) 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 2 with HttpServerBuilder

use of com.linkedin.r2.testutils.server.HttpServerBuilder in project rest.li by linkedin.

the class TestChannelPoolManagerFactorySharingConnection method makeRequestsWithClients.

/**
 * Helper function that creates clientFactory, makes request sequentially, checks the result and shutdowns everything
 */
public void makeRequestsWithClients(boolean shareConnection, ClientGenerator clientGenerator, int expectedConnectionsWithSharing) throws Exception {
    HttpClientFactory clientFactory = new HttpClientFactory.Builder().setShareConnection(shareConnection).build();
    HttpServerBuilder.HttpServerStatsProvider httpServerStatsProvider = getHttpServerStatsProviderIgnoringOptions();
    Server server = new HttpServerBuilder().serverStatsProvider(httpServerStatsProvider).build();
    try {
        server.start();
        List<Client> clients = new ArrayList<>();
        for (int i = 0; i < TRIALS; i++) {
            clientGenerator.populate(clients, clientFactory);
        }
        for (Client c : clients) {
            RestRequest r = new RestRequestBuilder(new URI(TestHttpClientFactory.URI)).build();
            c.restRequest(r).get(30, TimeUnit.SECONDS);
            FutureCallback<None> shutdownCallback = new FutureCallback<>();
            c.shutdown(shutdownCallback);
            shutdownCallback.get(20, TimeUnit.SECONDS);
        }
        Assert.assertEquals(httpServerStatsProvider.requestCount(), NUMBER_OF_REQUESTS);
        int expectedOpenedConnections = shareConnection ? expectedConnectionsWithSharing : OPENED_CONNECTIONS_WITHOUT_SHARING;
        Assert.assertEquals(httpServerStatsProvider.clientConnections().size(), expectedOpenedConnections);
    } finally {
        server.stop();
    }
    // shutdown the client factory which will trigger the ChannelPoolManagerFactorySharingConnecion shutdown
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    clientFactory.shutdown(shutdownCallback);
    shutdownCallback.get(10, TimeUnit.SECONDS);
}
Also used : Server(org.eclipse.jetty.server.Server) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) ArrayList(java.util.ArrayList) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Client(com.linkedin.r2.transport.common.Client) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback)

Example 3 with HttpServerBuilder

use of com.linkedin.r2.testutils.server.HttpServerBuilder 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 4 with HttpServerBuilder

use of com.linkedin.r2.testutils.server.HttpServerBuilder 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 5 with HttpServerBuilder

use of com.linkedin.r2.testutils.server.HttpServerBuilder 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)

Aggregations

Server (org.eclipse.jetty.server.Server)20 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)19 URI (java.net.URI)19 Test (org.testng.annotations.Test)16 FutureCallback (com.linkedin.common.callback.FutureCallback)15 RequestContext (com.linkedin.r2.message.RequestContext)12 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)12 None (com.linkedin.common.util.None)11 RestRequest (com.linkedin.r2.message.rest.RestRequest)11 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)7 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)7 Client (com.linkedin.r2.transport.common.Client)7 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)6 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)6 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)6 ArrayList (java.util.ArrayList)6 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)6