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.nsxclient.apis.DhcpServiceApiTest.java

@Test
public void testCreateDhcpRelayService() throws IOException, InterruptedException {
    final DhcpRelayService mockResponse = new DhcpRelayService();
    mockResponse.setId("id");
    mockResponse.setResourceType(LogicalServiceResourceType.DHCP_RELAY_SERVICE);
    setupMocks(objectMapper.writeValueAsString(mockResponse), HttpStatus.SC_CREATED);

    DhcpServiceApi client = new DhcpServiceApi(restClient);
    final CountDownLatch latch = new CountDownLatch(1);
    client.createDhcpRelayService(new DhcpRelayServiceCreateSpec(),
            new com.google.common.util.concurrent.FutureCallback<DhcpRelayService>() {
                @Override/*from w  w w  .ja  v a  2  s  . c  o m*/
                public void onSuccess(DhcpRelayService 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 testGetFabricNode() throws IOException, InterruptedException {
    final FabricNode mockResponse = new FabricNode();
    mockResponse.setId("id");
    setupMocks(objectMapper.writeValueAsString(mockResponse), HttpStatus.SC_OK);

    FabricApi client = new FabricApi(restClient);
    final CountDownLatch latch = new CountDownLatch(1);
    client.getFabricNode("nodeId", new com.google.common.util.concurrent.FutureCallback<FabricNode>() {
        @Override/*ww w. j ava 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:com.vmware.photon.controller.nsxclient.apis.FabricApiTest.java

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

    FabricApi client = new FabricApi(restClient);
    final CountDownLatch latch = new CountDownLatch(1);
    client.getTransportNode("id", new com.google.common.util.concurrent.FutureCallback<TransportNode>() {
        @Override//  w  ww.  ja v  a2  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 testGetTransportZone() throws IOException, InterruptedException {
    final TransportZone mockResponse = new TransportZone();
    mockResponse.setId("id");
    setupMocks(objectMapper.writeValueAsString(mockResponse), HttpStatus.SC_OK);

    FabricApi client = new FabricApi(restClient);
    final CountDownLatch latch = new CountDownLatch(1);
    client.getTransportZone("id", new com.google.common.util.concurrent.FutureCallback<TransportZone>() {
        @Override/*from w  w w  . j av  a2  s . c o 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));
}

From source file:com.hazelcast.hibernate.app.Executor.java

public void execute() throws Exception {
    CountDownLatch latch = new CountDownLatch(1000);
    int count;/*  w w w  .j  a v a2  s . c  o  m*/

    Session session = sessionFactory.openSession();
    try {
        Criteria criteria = session.createCriteria(DummyEntity.class);
        criteria.setProjection(Projections.rowCount());
        count = ((Long) criteria.uniqueResult()).intValue();
    } finally {
        session.close();
    }

    if (count == 0) {
        count = 200000;
        insertDummyEntities(count, 100);
    }

    try {
        for (int i = 0; i < latch.getCount(); i++) {
            executorService.submit(new Task(i, sessionFactory, 1000, latch));
        }

        latch.await(1, TimeUnit.DAYS);
    } finally {
        executorService.shutdown();
    }
}

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

@Test
public void testPauseSystemAsync() throws IOException, InterruptedException {
    final Task responseTask = getExpectedTaskResponse();
    DeploymentApi deploymentApi = new DeploymentApi(restClient);

    final CountDownLatch latch = new CountDownLatch(1);

    deploymentApi.pauseSystemAsync("deploymentId1", new FutureCallback<Task>() {
        @Override/* w ww . j  ava 2s . com*/
        public void onSuccess(Task result) {
            assertEquals(result, responseTask);
            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.DeploymentRestApiTest.java

@Test
public void testPauseSystemAsync() throws IOException, InterruptedException {
    final Task responseTask = getExpectedTaskResponse();
    DeploymentApi deploymentApi = new DeploymentRestApi(restClient);

    final CountDownLatch latch = new CountDownLatch(1);

    deploymentApi.pauseSystemAsync("deploymentId1", new FutureCallback<Task>() {
        @Override//from  www  .j a  v a2s.c o  m
        public void onSuccess(Task result) {
            assertEquals(result, responseTask);
            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.DeploymentApiTest.java

@Test
public void testResumeSystemAsync() throws Exception {
    Task responseTask = getExpectedTaskResponse();
    DeploymentApi deploymentApi = new DeploymentApi(restClient);

    final CountDownLatch latch = new CountDownLatch(1);

    deploymentApi.resumeSystemAsync("deploymentId1", new FutureCallback<Task>() {
        @Override/*from ww w . j a va2s. c  om*/
        public void onSuccess(Task result) {
            assertEquals(result, responseTask);
            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.DeploymentRestApiTest.java

@Test
public void testResumeSystemAsync() throws Exception {
    Task responseTask = getExpectedTaskResponse();
    DeploymentApi deploymentApi = new DeploymentRestApi(restClient);

    final CountDownLatch latch = new CountDownLatch(1);

    deploymentApi.resumeSystemAsync("deploymentId1", new FutureCallback<Task>() {
        @Override//from ww w  .  ja  v a 2  s  .c  om
        public void onSuccess(Task result) {
            assertEquals(result, responseTask);
            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.datatorrent.contrib.hdht.HDHTWriterTest.java

@Test
public void testWriteError() throws Exception {
    File file = new File(testInfo.getDir());
    FileUtils.deleteDirectory(file);/*from  www. java 2 s. c  o m*/

    final RuntimeException writeError = new RuntimeException("failure simulation");
    final CountDownLatch endWindowComplete = new CountDownLatch(1);
    final CountDownLatch writerActive = new CountDownLatch(1);

    FileAccessFSImpl fa = new MockFileAccess() {
        @Override
        public FileWriter getWriter(long bucketKey, String fileName) throws IOException {
            writerActive.countDown();
            try {
                if (endWindowComplete.await(10, TimeUnit.SECONDS)) {
                    throw writeError;
                }
            } catch (InterruptedException e) {
                //Do nothing
            }
            return super.getWriter(bucketKey, fileName);
        }
    };
    fa.setBasePath(file.getAbsolutePath());
    HDHTWriter hds = new HDHTWriter();
    hds.setFileStore(fa);
    hds.setFlushIntervalCount(0); // flush after every window

    long BUCKETKEY = 1;

    hds.setup(new OperatorContextTestHelper.TestIdOperatorContext(0, new DefaultAttributeMap()));
    //hds.writeExecutor = new ScheduledThreadPoolExecutor(1);

    hds.beginWindow(1);
    long[] seqArray = { 5L, 1L, 3L, 4L, 2L };
    for (long seq : seqArray) {
        Slice key = newKey(BUCKETKEY, seq);
        hds.put(BUCKETKEY, key, ("data" + seq).getBytes());
    }
    hds.endWindow();
    hds.checkpointed(1);
    hds.committed(1);
    endWindowComplete.countDown();

    try {
        Assert.assertTrue(writerActive.await(10, TimeUnit.SECONDS));
        hds.writeExecutor.shutdown();
        hds.writeExecutor.awaitTermination(10, TimeUnit.SECONDS);
        hds.beginWindow(2);
        hds.endWindow();
        Assert.fail("exception not raised");
    } catch (Exception e) {
        Assert.assertSame(writeError, e.getCause());
    }

    hds.teardown();
}