Example usage for java.util.concurrent CountDownLatch await

List of usage examples for java.util.concurrent CountDownLatch await

Introduction

In this page you can find the example usage for java.util.concurrent CountDownLatch await.

Prototype

public boolean await(long timeout, TimeUnit unit) throws InterruptedException 

Source Link

Document

Causes the current thread to wait until the latch has counted down to zero, unless the thread is Thread#interrupt interrupted , or the specified waiting time elapses.

Usage

From source file:org.wisdom.framework.vertx.CookiesTest.java

@Test
public void testFlash() throws InterruptedException, IOException {
    Router router = prepareServer();/*from  ww  w .  ja va2s. co  m*/

    // Prepare the router with a controller
    Controller controller = new DefaultController() {
        @SuppressWarnings("unused")
        public Result index() {
            context().flash().put("id", context().parameter("id"));
            return ok("Alright");
        }

        @SuppressWarnings("unused")
        public Result logged() {
            String id = context().flash().get("id");
            if (id == null) {
                return badRequest("no flash");
            } else {
                return ok(id);
            }
        }
    };
    final Route route1 = new RouteBuilder().route(HttpMethod.GET).on("/").to(controller, "index");
    final Route route2 = new RouteBuilder().route(HttpMethod.GET).on("/logged").to(controller, "logged");
    configureRouter(router, route1, route2);

    server.start();
    waitForStart(server);

    // Now start bunch of clients
    int num = NUMBER_OF_CLIENTS;
    CountDownLatch startSignal = new CountDownLatch(1);
    CountDownLatch doneSignal = new CountDownLatch(num);

    int port = server.httpPort();

    for (int i = 0; i < num; ++i) { // create and start threads
        executor.submit(new LoggedClient(startSignal, doneSignal, port, i, true));
    }

    startSignal.countDown(); // let all threads proceed
    doneSignal.await(30, TimeUnit.SECONDS); // wait for all to finish

    assertThat(failure).isEmpty();
    assertThat(success).hasSize(num);

}

From source file:io.druid.server.initialization.JettyTest.java

@Test
public void testThreadNotStuckOnException() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    Executors.newSingleThreadExecutor().execute(new Runnable() {
        @Override//from w w  w. j a  v  a 2s .  c  o m
        public void run() {
            try {
                ListenableFuture<InputStream> go = client.go(
                        new Request(HttpMethod.GET,
                                new URL("http://localhost:" + port + "/exception/exception")),
                        new InputStreamResponseHandler());
                StringWriter writer = new StringWriter();
                IOUtils.copy(go.get(), writer, "utf-8");
            } catch (IOException e) {
                // Expected.
            } catch (Throwable t) {
                Throwables.propagate(t);
            }
            latch.countDown();
        }
    });

    latch.await(5, TimeUnit.SECONDS);
}

From source file:org.wisdom.framework.vertx.CookiesTest.java

@Test
public void testCookie() throws InterruptedException, IOException {
    Router router = prepareServer();//from ww  w. ja v  a 2s .c o m

    // Prepare the router with a controller
    Controller controller = new DefaultController() {
        @SuppressWarnings("unused")
        public Result index() {
            if (context().parameter("id") == null) {
                System.err.println("No param: " + context().parameters());
            }
            return ok("Alright")
                    .with(Cookie.builder("my-cookie", context().parameter("id")).setMaxAge(1000).build());
        }

        @SuppressWarnings("unused")
        public Result logged() {
            String id = context().cookieValue("my-cookie");
            if (id == null) {
                return badRequest("no cookie");
            } else {
                return ok(id);
            }
        }
    };
    final Route route1 = new RouteBuilder().route(HttpMethod.GET).on("/").to(controller, "index");
    final Route route2 = new RouteBuilder().route(HttpMethod.GET).on("/logged").to(controller, "logged");
    configureRouter(router, route1, route2);

    server.start();
    waitForStart(server);

    // Now start bunch of clients
    int num = NUMBER_OF_CLIENTS;
    CountDownLatch startSignal = new CountDownLatch(1);
    CountDownLatch doneSignal = new CountDownLatch(num);

    int port = server.httpPort();

    for (int i = 0; i < num; ++i) {// create and start threads
        executor.submit(new LoggedClient(startSignal, doneSignal, port, i, false));
    }

    startSignal.countDown(); // let all threads proceed
    assertThat(doneSignal.await(60, TimeUnit.SECONDS)).isTrue(); // wait for all to finish

    assertThat(failure).isEmpty();
    assertThat(success).hasSize(num);

}

From source file:interactivespaces.activity.component.ActivityComponentContextTest.java

/**
 * Test that there is a processing handler that never exits and the wait has
 * to exit./*from w  w w  . j  a  v  a2s .  co  m*/
 */
@Test
public void testWaitMultithreadedProcessingHandlers() throws Exception {
    final CountDownLatch latch = new CountDownLatch(2);
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            context.enterHandler();
            latch.countDown();
            InteractiveSpacesUtilities.delay(1000);
            context.exitHandler();
        }
    };

    executor.execute(runnable);
    executor.execute(runnable);

    // Make sure they have both entered before starting the wait.
    Assert.assertTrue(latch.await(500, TimeUnit.MILLISECONDS));

    Assert.assertTrue(context.waitOnNoProcessingHandlings(500, 4000));
}