Example usage for io.netty.channel.nio NioEventLoopGroup awaitTermination

List of usage examples for io.netty.channel.nio NioEventLoopGroup awaitTermination

Introduction

In this page you can find the example usage for io.netty.channel.nio NioEventLoopGroup awaitTermination.

Prototype

@Override
    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException 

Source Link

Usage

From source file:com.linkedin.r2.transport.http.client.TestHttpClientFactory.java

License:Apache License

@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 {//from w  ww .j a  va2 s .c o m
        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();
    }
}

From source file:com.linkedin.r2.transport.http.client.TestHttpClientFactory.java

License:Apache License

@Test(dataProvider = "configs")
public void testShutdownBeforeClients(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 {//from   w  w  w.j  a  v  a 2  s  .  c o m
        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);
        }

        FutureCallback<None> factoryShutdown = new FutureCallback<None>();
        factory.shutdown(factoryShutdown);

        for (Client c : clients) {
            FutureCallback<None> callback = new FutureCallback<None>();
            c.shutdown(callback);
            callback.get(30, TimeUnit.SECONDS);
        }

        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();
    }
}

From source file:com.linkedin.r2.transport.http.client.TestHttpClientFactory.java

License:Apache License

@Test(dataProvider = "configs")
public void testShutdownTimeout(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 {/* ww  w  .  java 2  s  . c o  m*/
        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);
        }

        FutureCallback<None> factoryShutdown = new FutureCallback<None>();
        factory.shutdown(factoryShutdown, 1, TimeUnit.SECONDS);

        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();
    }
}

From source file:com.linkedin.r2.transport.http.client.TestHttpClientFactory.java

License:Apache License

@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 {//from   www  .j av a 2s .co m
        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();
}

From source file:com.linkedin.r2.transport.http.client.TestHttpClientFactory.java

License:Apache License

/**
 * Tests that even when the factory is shutdown with a long timeout, it does not occupy
 * any executors with tasks that might prevent them shutting down properly.
 * @throws InterruptedException/*from  ww  w  . j a va 2s  . co  m*/
 * @throws ExecutionException
 * @throws TimeoutException
 */
@Test
public void testShutdownTimeoutDoesNotOccupyExecutors()
        throws InterruptedException, ExecutionException, TimeoutException {
    NioEventLoopGroup eventLoop = new NioEventLoopGroup();
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    HttpClientFactory factory = getHttpClientFactory(eventLoop, false, scheduler, false);

    FutureCallback<None> callback = new FutureCallback<None>();
    factory.shutdown(callback, 60, TimeUnit.MINUTES);
    callback.get(60, TimeUnit.SECONDS);
    scheduler.shutdown();
    eventLoop.shutdownGracefully();
    Assert.assertTrue(scheduler.awaitTermination(60, TimeUnit.SECONDS));
    Assert.assertTrue(eventLoop.awaitTermination(60, TimeUnit.SECONDS));
}