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 void await() 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 .

Usage

From source file:com.greplin.zookeeper.RobustZooKeeper.java

public void sync() throws IOException, InterruptedException {

    log.info("Called sync() on client " + clientNumber);
    final CountDownLatch waitForSync = new CountDownLatch(1);
    final ZooKeeper c = getClient();
    assert c.getState().isAlive();

    c.sync("/", new AsyncCallback.VoidCallback() {
        @Override/*from w  ww.  ja  va2s. c om*/
        public void processResult(int rc, String path, Object ctx) {
            log.info("Sync callback triggered on client " + clientNumber);
            waitForSync.countDown();
        }
    }, null);

    log.info("Waitng for sync callback on client " + clientNumber);
    waitForSync.await();

    log.info("sync() finished on " + clientNumber);
    return;
}

From source file:info.archinnov.achilles.test.integration.tests.AsyncEventInterceptorIT.java

@Test
public void should_apply_post_delete_interceptors() throws Exception {

    CompleteBean entity = builder().randomId().name("DuyHai").label("label").buid();

    final CountDownLatch latch = new CountDownLatch(1);
    manager2.delete(entity, withAsyncListeners(new FutureCallback<Object>() {
        @Override/*from   w w  w .ja  v  a  2  s . co m*/
        public void onSuccess(Object result) {
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {

        }
    }));

    latch.await();

    assertThat(entity.getLabel()).isEqualTo("postRemove");
}

From source file:com.microsoft.office.integration.test.EventsAsyncTestCase.java

private void deleteAndCheck() throws Exception {
    removeEvent();//from  w  w  w. jav a  2  s .c o m
    final CountDownLatch cdl = new CountDownLatch(1);
    Futures.addCallback(Me.getEvents().getAsync(event.getId()), new FutureCallback<IEvent>() {
        public void onFailure(Throwable t) {
            reportError(t);
            cdl.countDown();
        }

        public void onSuccess(IEvent result) {
            try {
                assertNull(result);
            } catch (Throwable t) {
                reportError(t);
            }

            cdl.countDown();
        }
    });
    cdl.await();
}

From source file:info.archinnov.achilles.test.integration.tests.AsyncEventInterceptorIT.java

@Test
public void should_apply_persist_interceptors() throws Exception {

    CompleteBean entity = builder().randomId().name("DuyHai").label("label").version(incr(2L)).buid();

    final CountDownLatch latch = new CountDownLatch(1);
    asyncManager.insert(entity, withAsyncListeners(new FutureCallback<Object>() {
        @Override// www . j  a v a  2s  .c om
        public void onSuccess(Object result) {
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {

        }
    }));

    latch.await();

    assertThat(entity.getName()).isEqualTo("prePersist");
    assertThat(entity.getLabel()).isEqualTo("postPersist : label");

    Row row = session.execute("select name,label from CompleteBean where id = " + entity.getId()).one();

    assertThat(row.getString("name")).isEqualTo("prePersist");
    assertThat(row.getString("label")).isEqualTo("label");

}

From source file:io.druid.server.namespace.cache.NamespaceExtractionCacheManagerExecutorsTest.java

@Test(timeout = 50_000)
public void testConcurrentDelete() throws ExecutionException, InterruptedException {
    final int threads = 5;
    ListeningExecutorService executorService = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(threads));
    final CountDownLatch latch = new CountDownLatch(threads);
    Collection<ListenableFuture<?>> futures = new ArrayList<>();
    for (int i = 0; i < threads; ++i) {
        final int loopNum = i;
        ListenableFuture<?> future = executorService.submit(new Runnable() {
            @Override/*ww w  .  j a v a2  s  . c  o  m*/
            public void run() {
                try {
                    latch.countDown();
                    latch.await();
                    for (int j = 0; j < 10; ++j) {
                        testDelete(String.format("ns-%d", loopNum));
                    }
                } catch (InterruptedException e) {
                    throw Throwables.propagate(e);
                }
            }
        });
    }
    Futures.allAsList(futures).get();
    executorService.shutdown();
}

From source file:org.kurento.repository.OneRecordingServer.java

private void prepareToDownloadVideo(RepositoryItem repositoryItem) throws InterruptedException {
    RepositoryHttpPlayer player = repositoryItem.createRepositoryHttpPlayer("video-download");
    log.info("The video can be downloaded with GET from the URL: " + player.getURL());

    player.setAutoTerminationTimeout(30 * 60 * 1000);
    log.info("The player will be auto-terminated 30 min after the last downloading of content (http GET)");

    final CountDownLatch terminatedLatch = new CountDownLatch(1);

    player.addSessionStartedListener(new RepositoryHttpEventListener<HttpSessionStartedEvent>() {
        @Override// w  ww  . j a  va 2  s.c o  m
        public void onEvent(HttpSessionStartedEvent event) {
            log.info("Downloading started");
        }
    });

    player.addSessionTerminatedListener(new RepositoryHttpEventListener<HttpSessionTerminatedEvent>() {
        @Override
        public void onEvent(HttpSessionTerminatedEvent event) {
            log.info("Downloading terminated");
            terminatedLatch.countDown();
        }
    });

    try {
        terminatedLatch.await();
    } catch (InterruptedException e) {
    }
}

From source file:com.microsoft.office.core.ContactsAsyncTestCase.java

private void deleteAndCheck() throws Exception {
    removeContact();/*from  w  w w . j  a  va 2s. c o  m*/
    final CountDownLatch cdl = new CountDownLatch(1);
    Futures.addCallback(Me.getContacts().getAsync(contact.getId()), new FutureCallback<IContact>() {
        @Override
        public void onFailure(Throwable t) {
            reportError(t);
            cdl.countDown();
        }

        @Override
        public void onSuccess(IContact result) {
            try {
                assertNull(result);
            } catch (Throwable t) {
                reportError(t);
            }
            cdl.countDown();
        }
    });
    cdl.await();
}

From source file:com.twitter.common.zookeeper.GroupTest.java

@Test
public void testNodeDeleteTriggersOnLoseMembership() throws Exception {
    final CountDownLatch lostMembership = new CountDownLatch(1);
    Command onLoseMembership = new Command() {
        @Override/*from w  ww  . j a v a 2s .c o  m*/
        public void execute() throws RuntimeException {
            lostMembership.countDown();
        }
    };
    assertEmptyMembershipObserved();

    Membership membership = group.join(onLoseMembership);
    assertMembershipObserved(membership.getMemberId());
    membership.cancel();

    lostMembership.await(); // Will hang this test if onLoseMembership event is not propagated.
}

From source file:com.bt.aloha.dialog.DialogConcurrentUpdateBlockTest.java

@Test
public void testSequenceNumberReleasedOnFailedUpdate() {
    // setup//  w  ww.j  a  v  a  2 s.  c o  m
    final CountDownLatch firstWriterRead = new CountDownLatch(1);
    final CountDownLatch secondWriterWrote = new CountDownLatch(1);

    DialogConcurrentUpdateBlock dialogConcurrentUpdateBlock = new DialogConcurrentUpdateBlock(
            dialogBeanHelper) {
        public void execute() {
            DialogInfo dialogInfo = dialogCollection.get("id");
            firstWriterRead.countDown();
            assignSequenceNumber(dialogInfo, Request.INVITE);
            log.debug("Waiting for second writer to write");
            try {
                secondWriterWrote.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
            dialogCollection.replace(dialogInfo);
        }

        public String getResourceId() {
            return "id";
        }
    };

    // act
    new Thread(new CompetingWriter(firstWriterRead, secondWriterWrote)).start();
    concurrentUpdateManager.executeConcurrentUpdate(dialogConcurrentUpdateBlock);

    // assert
    assertEquals(5, dialogCollection.get("id").getSequenceNumber());
    assertEquals(1, releasedSequenceNumbers.size());
    assertEquals(4, releasedSequenceNumbers.get(0));
    assertEquals(5, lastSequenceNumber);
}

From source file:com.microsoft.office.core.ContactsAsyncTestCase.java

private void createAndCheck() throws Exception {
    prepareContact();/* w  w w .ja v a  2  s .  com*/
    final CountDownLatch cdl = new CountDownLatch(1);
    Futures.addCallback(Me.flushAsync(), new FutureCallback<Void>() {
        @Override
        public void onFailure(Throwable t) {
            reportError(t);
            cdl.countDown();
        }

        @Override
        public void onSuccess(Void result) {
            try {
                assertTrue(StringUtils.isNotEmpty(contact.getId()));
            } catch (Throwable t) {
                reportError(t);
            }

            cdl.countDown();
        }
    });

    cdl.await();
}