List of usage examples for java.util.concurrent CountDownLatch CountDownLatch
public CountDownLatch(int count)
From source file:uk.co.sdev.undertow.rx.services.offers.OffersServiceTest.java
@Test public void shouldReturnObservableOfOffers() throws Exception { OffersService service = new OffersService(mapper, client, "http://localhost:9101/offers"); stubGet("/offers?count=2", Arrays.asList(new Offer("offer1", "description1", "offer1.jpg"), new Offer("offer2", "description2", "offer2.jpg"))); List<Offer> offers = new ArrayList<>(); List<Throwable> throwables = new ArrayList<>(); CountDownLatch finished = new CountDownLatch(1); Observable<Offer> offer = service.offers(2); new Thread(() -> offer.subscribe(o -> offers.add(o), t -> throwables.add(t), () -> finished.countDown())) .start();//from w w w .j a v a2 s. c o m finished.await(5, TimeUnit.SECONDS); assertThat(offers.size(), is(2)); assertThat(offers.get(0).getTitle(), is("offer1")); assertThat(offers.get(1).getTitle(), is("offer2")); }
From source file:com.microsoft.office.core.ContactsAsyncTestCase.java
@Test(timeout = 60000) public void readTest() throws Exception { // create contact first prepareContact();//from w ww .ja v a2 s . c om counter = new CountDownLatch(1); Futures.addCallback(Me.flushAsync(), new FutureCallback<Void>() { @Override public void onFailure(Throwable t) { reportError(t); counter.countDown(); } @Override public void onSuccess(Void result) { try { readAndCheck(); // clean-up removeContact(); } catch (Throwable t) { reportError(t); } counter.countDown(); } }); counter.await(); }
From source file:org.springframework.cloud.stream.FluxTest.java
@Test public void testSimpleSubscriber() throws Exception { final CountDownLatch latch = new CountDownLatch(10); emitter.subscribe(s -> {/* w w w .ja v a2 s. c o m*/ System.out.println(s); latch.countDown(); }); latch.await(); }
From source file:com.signicat.hystrix.servlet.AsyncWrapperServletTest.java
@Test public void require_That_Servlet_Timeout_Kicks_In_But_Hystrix_Timeout_Does_Not_Kick_In() throws Exception { CountDownLatch servletTimeout = new CountDownLatch(1); final AsyncTestServlet servlet = new AsyncTestServlet(new CountDownLatch(1), servletTimeout, new CountDownLatch(1), new CountDownLatch(1), new CountDownLatch(1), new CountDownLatch(1), null, new TimeoutServlet(servletTimeout), 1000L); try (TestServer server = new TestServer(0, servlet)) { server.start();/*from w ww .ja va 2 s. co m*/ try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet("http://localhost:" + server.getPort() + "/bananarama"); try (CloseableHttpResponse httpResponse = httpclient.execute(httpGet)) { StatusLine statusLine = httpResponse.getStatusLine(); assertThat(statusLine.getStatusCode(), equalTo(504)); assertThat(statusLine.getReasonPhrase(), equalTo("Timeout from async listener")); EntityUtils.consume(httpResponse.getEntity()); assertThat(servlet.servletTimeout.await(60, TimeUnit.SECONDS), is(true)); assertThat(servlet.servletComplete.await(60, TimeUnit.SECONDS), is(true)); assertThat(servlet.servletError.getCount(), equalTo(1L)); assertThat(servlet.hystrixError.await(60, TimeUnit.SECONDS), is(true)); assertThat(servlet.hystrixCompleted.getCount(), equalTo(1L)); assertThat(servlet.hystrixNext.getCount(), equalTo(1L)); } } } }
From source file:com.microsoft.office.integration.test.ContactsAsyncTestCase.java
public void testRead() { // create contact first prepareContact();//from w ww . j av a 2s .c om counter = new CountDownLatch(1); Futures.addCallback(Me.flushAsync(), new FutureCallback<Void>() { public void onFailure(Throwable t) { reportError(t); counter.countDown(); } public void onSuccess(Void result) { try { readAndCheck(); // clean-up removeContact(); } catch (Throwable t) { reportError(t); } counter.countDown(); } }); try { if (!counter.await(60000, TimeUnit.MILLISECONDS)) { fail("testRead() timed out"); } } catch (InterruptedException e) { fail("testRead() has been interrupted"); } }
From source file:com.couchbase.client.internal.HttpFutureTest.java
@Test public void testCancellation() throws Exception { CountDownLatch latch = new CountDownLatch(1); long timeout = 1000; HttpFuture<CancellableOperation> future = new HttpFuture<CancellableOperation>(latch, timeout); HttpOperation op = new CancellableOperation(); latch.countDown();/*from w w w.j a v a2 s . co m*/ future.setOperation(op); future.cancel(true); try { future.get(); assertTrue("Future did not throw ExecutionException", false); } catch (ExecutionException e) { assertTrue(e.getCause() instanceof CancellationException); assertEquals("Cancelled", e.getCause().getMessage()); } catch (Exception e) { assertTrue(e.getMessage(), false); } }
From source file:com.microsoft.office.core.FolderAsyncTestCase.java
@Test(timeout = 60000) public void readTest() throws Exception { prepareFolder();/*www .ja va 2 s. c om*/ counter = new CountDownLatch(1); Futures.addCallback(Me.flushAsync(), new FutureCallback<Void>() { @Override public void onFailure(Throwable t) { reportError(t); counter.countDown(); } @Override public void onSuccess(Void result) { try { FolderAsyncTestCase.this.readAndCheck(); FolderAsyncTestCase.this.removeFolder(); } catch (Throwable t) { reportError(t); } counter.countDown(); } }); counter.await(); }
From source file:it.pronetics.madstore.repository.index.impl.IndexManagerMultiThreadTest.java
public void testIndexManagerSharedByMultipleThreads() throws Exception { final CountDownLatch firstStepLatch = new CountDownLatch(1); final CountDownLatch secondStepLatch = new CountDownLatch(1); final CountDownLatch thirdStepLatch = new CountDownLatch(1); final CountDownLatch stopLatch = new CountDownLatch(4); final List<String> terms = new ArrayList<String>(); final Element entry1 = Utils.getDoc("luceneentry1.xml").getDocumentElement(); final Element entry2 = Utils.getDoc("luceneentry2.xml").getDocumentElement(); terms.add("bla"); Thread t1 = threadFactory.newThread(new Runnable() { public void run() { try { indexManager.index(COLLECTION_KEY, "entry1", entry1); firstStepLatch.countDown(); } finally { stopLatch.countDown();//from w w w .j a v a2 s . c om } } }); t1.start(); Thread t2 = threadFactory.newThread(new Runnable() { public void run() { try { firstStepLatch.await(5, TimeUnit.SECONDS); List<SearchResult> entryKeyPair = indexManager.searchCollectionByFullText(COLLECTION_KEY, terms, 0, 0); assertEquals(1, entryKeyPair.size()); secondStepLatch.countDown(); } catch (InterruptedException ex) { fail(ex.getMessage()); } finally { stopLatch.countDown(); } } }); t2.start(); Thread t3 = threadFactory.newThread(new Runnable() { public void run() { try { secondStepLatch.await(5, TimeUnit.SECONDS); indexManager.index(COLLECTION_KEY, "entry2", entry2); thirdStepLatch.countDown(); } catch (InterruptedException ex) { fail(ex.getMessage()); } finally { stopLatch.countDown(); } } }); t3.start(); Thread t4 = threadFactory.newThread(new Runnable() { public void run() { try { thirdStepLatch.await(5, TimeUnit.SECONDS); List<SearchResult> entryKeyPair = indexManager.searchCollectionByFullText(COLLECTION_KEY, terms, 0, 0); assertEquals(2, entryKeyPair.size()); } catch (InterruptedException ex) { fail(ex.getMessage()); } finally { stopLatch.countDown(); } } }); t4.start(); stopLatch.await(); threadFactory.verifyThreads(); threadFactory.resetThreads(); }
From source file:com.netflix.curator.framework.imps.TestFrameworkBackground.java
@Test public void testRetries() throws Exception { final int SLEEP = 1000; final int TIMES = 5; Timing timing = new Timing(); CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryNTimes(TIMES, SLEEP)); try {/*from w ww . j a va2s. c o m*/ client.start(); client.getZookeeperClient().blockUntilConnectedOrTimedOut(); final CountDownLatch latch = new CountDownLatch(TIMES); final List<Long> times = Lists.newArrayList(); final AtomicLong start = new AtomicLong(System.currentTimeMillis()); ((CuratorFrameworkImpl) client).debugListener = new CuratorFrameworkImpl.DebugBackgroundListener() { @Override public void listen(OperationAndData<?> data) { if (data.getOperation().getClass().getName().contains("CreateBuilderImpl")) { long now = System.currentTimeMillis(); times.add(now - start.get()); start.set(now); latch.countDown(); } } }; server.stop(); client.create().inBackground().forPath("/one"); latch.await(); for (long elapsed : times.subList(1, times.size())) // first one isn't a retry { Assert.assertTrue(elapsed >= SLEEP, elapsed + ": " + times); } } finally { IOUtils.closeQuietly(client); } }
From source file:com.guang.eunormia.common.dislock.ZKClient.java
public void init() throws Exception { connectedSignal = new CountDownLatch(1); zookeeper = new ZooKeeper(zkAddress, timeout, this); if (connectedSignal.await(6000, TimeUnit.MILLISECONDS)) { logger.warn("- the conect to zookeeper server success ..."); } else {//from w ww. j a v a2s . c om logger.error("- try to establish connection to zookeeper timeout ..."); throw new Exception("- try to establish connection to zookeeper timeout ..."); } }