Search in sources :

Example 11 with HttpClientFactory

use of com.linkedin.r2.transport.http.client.HttpClientFactory 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 12 with HttpClientFactory

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

the class TestHttpClientFactory method testShutdownIOThread.

@Test(dataProvider = "configs")
public void testShutdownIOThread(boolean restOverStream, String protocolVersion) throws Exception {
    NioEventLoopGroup 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<None>();
        factory.shutdown(factoryShutdown);
        FutureCallback<None> clientShutdown = new FutureCallback<None>();
        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) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) 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 13 with HttpClientFactory

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

the class TestStreamResponse method testErrorWhileStreaming.

@Test
public void testErrorWhileStreaming() throws Exception {
    HttpClientFactory clientFactory = new HttpClientFactory();
    Map<String, String> clientProperties = new HashMap<String, String>();
    clientProperties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, "1000");
    Client client = new TransportClientAdapter(_clientFactory.getClient(clientProperties), true);
    StreamRequestBuilder builder = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, SERVER_ERROR_URI));
    StreamRequest request = builder.build(EntityStreams.emptyStream());
    final AtomicInteger status = new AtomicInteger(-1);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    final Callback<None> readerCallback = getReaderCallback(latch, error);
    final BytesReader reader = new BytesReader(BYTE, readerCallback);
    Callback<StreamResponse> callback = getCallback(status, readerCallback, reader);
    client.streamRequest(request, callback);
    latch.await(2000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(status.get(), RestStatus.OK);
    Throwable throwable = error.get();
    Assert.assertNotNull(throwable);
    final FutureCallback<None> clientShutdownCallback = new FutureCallback<None>();
    client.shutdown(clientShutdownCallback);
    clientShutdownCallback.get();
    final FutureCallback<None> factoryShutdownCallback = new FutureCallback<None>();
    clientFactory.shutdown(factoryShutdownCallback);
    factoryShutdownCallback.get();
}
Also used : HashMap(java.util.HashMap) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) Client(com.linkedin.r2.transport.common.Client) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 14 with HttpClientFactory

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

the class TestHttpClientFactory method testShutdownNoTimeout.

@Test(dataProvider = "configs")
public void testShutdownNoTimeout(boolean restOverStream, String protocolVersion) 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 < 100; i++) {
            HashMap<String, String> properties = new HashMap<>();
            properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
            clients.add(new TransportClientAdapter(factory.getClient(properties), restOverStream));
        }
        for (Client c : clients) {
            RestRequest r = new RestRequestBuilder(new URI(URI)).build();
            c.restRequest(r).get(30, TimeUnit.SECONDS);
        }
    } finally {
        server.stop();
    }
    FutureCallback<None> factoryShutdown = new FutureCallback<None>();
    factory.shutdown(factoryShutdown);
    try {
        factoryShutdown.get(1, TimeUnit.SECONDS);
        Assert.fail("Factory shutdown should have timed out");
    } catch (TimeoutException e) {
    // Expected
    }
    Assert.assertFalse(eventLoop.isShutdown(), "Boss should not be shut down");
    Assert.assertFalse(scheduler.isShutdown(), "Scheduler should not be shut down");
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) 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) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 15 with HttpClientFactory

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

the class TestHttpClientFactory method testClientShutdownBeingCalledMultipleTimes.

@Test
public void testClientShutdownBeingCalledMultipleTimes() throws InterruptedException, ExecutionException, TimeoutException {
    HttpClientFactory factory = new HttpClientFactory();
    TransportClient client = factory.getClient(Collections.<String, Object>emptyMap());
    // first shutdown call
    FutureCallback<None> clientShutdown = new FutureCallback<None>();
    client.shutdown(clientShutdown);
    clientShutdown.get(30, TimeUnit.SECONDS);
    // second shutdown call
    clientShutdown = new FutureCallback<None>();
    client.shutdown(clientShutdown);
    try {
        clientShutdown.get(30, TimeUnit.SECONDS);
        Assert.fail("should have thrown exception on the second shutdown call.");
    } catch (ExecutionException ex) {
        Assert.assertTrue(ex.getCause() instanceof IllegalStateException);
    }
    FutureCallback<None> shutdownCallback = new FutureCallback<None>();
    factory.shutdown(shutdownCallback);
    shutdownCallback.get(30, TimeUnit.SECONDS);
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) ExecutionException(java.util.concurrent.ExecutionException) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Aggregations

HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)23 HashMap (java.util.HashMap)23 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)18 None (com.linkedin.common.util.None)15 Test (org.testng.annotations.Test)14 FutureCallback (com.linkedin.common.callback.FutureCallback)13 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)11 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)11 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)10 URI (java.net.URI)9 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)8 LoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory)8 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)8 RestRequest (com.linkedin.r2.message.rest.RestRequest)8 Client (com.linkedin.r2.transport.common.Client)8 RequestContext (com.linkedin.r2.message.RequestContext)7 HttpServerFactory (com.linkedin.r2.transport.http.server.HttpServerFactory)7 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)7 ArrayList (java.util.ArrayList)7 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)6