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:com.vmware.photon.controller.api.client.resource.FlavorApiTest.java

@Test
public void testListAllAsyncForPagination() throws Exception {
    Flavor flavor1 = new Flavor();
    flavor1.setId("flavor1");
    flavor1.setKind("vm");

    Flavor flavor2 = new Flavor();
    flavor2.setId("flavor2");
    flavor2.setKind("vm");

    Flavor flavor3 = new Flavor();
    flavor3.setId("flavor3");
    flavor3.setKind("vm");

    String nextPageLink = "nextPageLink";

    final ResourceList<Flavor> flavorResourceList = new ResourceList<>(Arrays.asList(flavor1, flavor2),
            nextPageLink, null);/*from   w  w  w.j a  v a 2s.  com*/
    final ResourceList<Flavor> flavorResourceListNextPage = new ResourceList<>(Arrays.asList(flavor3));
    final ResourceList<Flavor> expectedFlavorResourceList = new ResourceList<>(
            Arrays.asList(flavor1, flavor2, flavor3));

    ObjectMapper mapper = new ObjectMapper();
    String serializedResponse = mapper.writeValueAsString(flavorResourceList);
    String serializedResponseNextPage = mapper.writeValueAsString(flavorResourceListNextPage);

    setupMocksForPagination(serializedResponse, serializedResponseNextPage, nextPageLink, HttpStatus.SC_OK);

    FlavorApi flavorApi = new FlavorApi(restClient);

    final CountDownLatch latch = new CountDownLatch(1);

    flavorApi.listAllAsync(new FutureCallback<ResourceList<Flavor>>() {
        @Override
        public void onSuccess(@Nullable ResourceList<Flavor> result) {
            assertTrue(result.getItems().containsAll(expectedFlavorResourceList.getItems()));
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
            fail(t.toString());
            latch.countDown();
        }
    });

    assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true));
}

From source file:com.vmware.photon.controller.api.client.resource.FlavorRestApiTest.java

@Test
public void testListAllAsyncForPagination() throws Exception {
    Flavor flavor1 = new Flavor();
    flavor1.setId("flavor1");
    flavor1.setKind("vm");

    Flavor flavor2 = new Flavor();
    flavor2.setId("flavor2");
    flavor2.setKind("vm");

    Flavor flavor3 = new Flavor();
    flavor3.setId("flavor3");
    flavor3.setKind("vm");

    String nextPageLink = "nextPageLink";

    final ResourceList<Flavor> flavorResourceList = new ResourceList<>(Arrays.asList(flavor1, flavor2),
            nextPageLink, null);/*from  w w  w .  ja v  a  2s .  co  m*/
    final ResourceList<Flavor> flavorResourceListNextPage = new ResourceList<>(Arrays.asList(flavor3));
    final ResourceList<Flavor> expectedFlavorResourceList = new ResourceList<>(
            Arrays.asList(flavor1, flavor2, flavor3));

    ObjectMapper mapper = new ObjectMapper();
    String serializedResponse = mapper.writeValueAsString(flavorResourceList);
    String serializedResponseNextPage = mapper.writeValueAsString(flavorResourceListNextPage);

    setupMocksForPagination(serializedResponse, serializedResponseNextPage, nextPageLink, HttpStatus.SC_OK);

    FlavorApi flavorApi = new FlavorRestApi(restClient);

    final CountDownLatch latch = new CountDownLatch(1);

    flavorApi.listAllAsync(new FutureCallback<ResourceList<Flavor>>() {
        @Override
        public void onSuccess(@Nullable ResourceList<Flavor> result) {
            assertTrue(result.getItems().containsAll(expectedFlavorResourceList.getItems()));
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
            fail(t.toString());
            latch.countDown();
        }
    });

    assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true));
}

From source file:interactivespaces.util.web.HttpClientHttpContentCopierTest.java

/**
 * Test a file upload from an input stream.
 *//*from  w  w w.  ja  va 2 s  .c om*/
@Test
public void testFileUploadInputStream() throws Exception {
    final File destination = getTempFile();

    InputStream source = new ByteArrayInputStream(TEST_CONTENT.getBytes());

    final AtomicReference<Map<String, String>> receivedParameters = new AtomicReference<Map<String, String>>();
    final CountDownLatch latch = new CountDownLatch(1);

    webServer.setHttpFileUploadListener(new HttpFileUploadListener() {
        @Override
        public void handleHttpFileUpload(HttpFileUpload fileUpload) {
            fileUpload.moveTo(destination);
            receivedParameters.set(fileUpload.getParameters());
            latch.countDown();
        }
    });

    String sourceParameterName = "myfile";
    Map<String, String> expectedParameters = Maps.newHashMap();
    expectedParameters.put("foo", "bar");
    expectedParameters.put("bletch", "spam");

    copier.copyTo(getUrlPrefix(), source, "foo.txt", sourceParameterName, expectedParameters);

    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));

    Assert.assertEquals(TEST_CONTENT, Files.readFile(destination).trim());
    Assert.assertEquals(expectedParameters, receivedParameters.get());
}

From source file:com.vmware.photon.controller.nsxclient.apis.FabricApiTest.java

@Test
public void testGetTransportNodeState() throws IOException, InterruptedException {
    final TransportNodeState mockResponse = new TransportNodeState();
    mockResponse.setState(com.vmware.photon.controller.nsxclient.datatypes.TransportNodeState.SUCCESS);
    setupMocks(objectMapper.writeValueAsString(mockResponse), HttpStatus.SC_OK);

    FabricApi client = new FabricApi(restClient);
    final CountDownLatch latch = new CountDownLatch(1);
    client.getTransportNodeState("id",
            new com.google.common.util.concurrent.FutureCallback<TransportNodeState>() {
                @Override/*from  w ww . j a  va 2 s  .co m*/
                public void onSuccess(TransportNodeState result) {
                    assertEquals(result, mockResponse);
                    latch.countDown();
                }

                @Override
                public void onFailure(Throwable t) {
                    fail(t.toString());
                    latch.countDown();
                }
            });

    assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true));
}

From source file:com.vmware.photon.controller.nsxclient.apis.FabricApiTest.java

@Test
public void testRegisterFabricNode() throws IOException, InterruptedException {
    final FabricNode mockResponse = new FabricNode();
    mockResponse.setId("id");
    mockResponse.setExternalId("externalId");
    setupMocks(objectMapper.writeValueAsString(mockResponse), HttpStatus.SC_CREATED);

    FabricApi client = new FabricApi(restClient);
    final CountDownLatch latch = new CountDownLatch(1);
    client.registerFabricNode(new FabricNodeCreateSpec(),
            new com.google.common.util.concurrent.FutureCallback<FabricNode>() {
                @Override/*from  w  ww  . j a  va  2s  . c  o  m*/
                public void onSuccess(FabricNode result) {
                    assertEquals(result, mockResponse);
                    latch.countDown();
                }

                @Override
                public void onFailure(Throwable t) {
                    fail(t.toString());
                    latch.countDown();
                }
            });

    assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true));
}

From source file:reactor.ipc.netty.tcp.TcpServerTests.java

@Test
public void testIssue462() throws InterruptedException {

    final CountDownLatch countDownLatch = new CountDownLatch(1);

    NettyContext server = TcpServer.create(0).newHandler((in, out) -> {
        in.receive().log("channel").subscribe(trip -> {
            countDownLatch.countDown();//www .  j  a va  2  s  .  c o m
        });
        return Flux.never();
    }).block(Duration.ofSeconds(30));

    System.out.println("PORT +" + server.address().getPort());

    NettyContext client = TcpClient.create(server.address().getPort())
            .newHandler((in, out) -> out.sendString(Flux.just("test"))).block(Duration.ofSeconds(30));

    client.dispose();
    server.dispose();

    assertThat("countDownLatch counted down", countDownLatch.await(5, TimeUnit.SECONDS));
}

From source file:com.vmware.photon.controller.nsxclient.apis.FabricApiTest.java

@Test
public void testGetTransportZoneSummary() throws IOException, InterruptedException {
    final TransportZoneSummary mockResponse = new TransportZoneSummary();
    mockResponse.setNumTransportNodes(5);
    setupMocks(objectMapper.writeValueAsString(mockResponse), HttpStatus.SC_OK);

    FabricApi client = new FabricApi(restClient);
    final CountDownLatch latch = new CountDownLatch(1);
    client.getTransportZoneSummary("id",
            new com.google.common.util.concurrent.FutureCallback<TransportZoneSummary>() {
                @Override/*ww w  . j  av  a2 s. c  o m*/
                public void onSuccess(TransportZoneSummary result) {
                    assertEquals(result, mockResponse);
                    latch.countDown();
                }

                @Override
                public void onFailure(Throwable t) {
                    fail(t.toString());
                    latch.countDown();
                }
            });

    assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true));
}

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

@Test
public void testTwoCookies() throws InterruptedException, IOException {
    Router router = prepareServer();/*  w ww .ja  va  2  s  . co m*/

    // Prepare the router with a controller
    Controller controller = new DefaultController() {
        @SuppressWarnings("unused")
        public Result index() {
            return ok("Alright")
                    .with(Cookie.builder("my-cookie", context().parameter("id")).setMaxAge(1000).build())
                    .with(Cookie.builder("my-cookie-2", context().parameter("id")).setMaxAge(1000).build());
        }

        @SuppressWarnings("unused")
        public Result logged() {
            String id = context().cookieValue("my-cookie");
            String id2 = context().cookie("my-cookie-2").value();
            if (id == null || id2 == 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
    doneSignal.await(30, TimeUnit.SECONDS); // wait for all to finish

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

}

From source file:com.vmware.photon.controller.nsxclient.apis.FabricApiTest.java

@Test
public void testCreateTransportNode() throws IOException, InterruptedException {
    final TransportNode mockResponse = new TransportNode();
    mockResponse.setId("id");
    setupMocks(objectMapper.writeValueAsString(mockResponse), HttpStatus.SC_CREATED);

    FabricApi client = new FabricApi(restClient);
    final CountDownLatch latch = new CountDownLatch(1);
    client.createTransportNode(new TransportNodeCreateSpec(),
            new com.google.common.util.concurrent.FutureCallback<TransportNode>() {
                @Override/*w w w. j  av a  2 s.c o m*/
                public void onSuccess(TransportNode result) {
                    assertEquals(result, mockResponse);
                    latch.countDown();

                }

                @Override
                public void onFailure(Throwable t) {
                    fail(t.toString());
                    latch.countDown();
                }
            });

    assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true));
}

From source file:com.vmware.photon.controller.nsxclient.apis.FabricApiTest.java

@Test
public void testCreateTransportZone() throws IOException, InterruptedException {
    final TransportZone mockResponse = new TransportZone();
    mockResponse.setId("id");
    setupMocks(objectMapper.writeValueAsString(mockResponse), HttpStatus.SC_CREATED);

    FabricApi client = new FabricApi(restClient);
    final CountDownLatch latch = new CountDownLatch(1);
    client.createTransportZone(new TransportZoneCreateSpec(),
            new com.google.common.util.concurrent.FutureCallback<TransportZone>() {
                @Override/*  www .ja  va2 s  .co m*/
                public void onSuccess(TransportZone result) {
                    assertEquals(result, mockResponse);
                    latch.countDown();

                }

                @Override
                public void onFailure(Throwable t) {
                    fail(t.toString());
                    latch.countDown();
                }
            });

    assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true));
}