@Override public Void call() throws Exception { enterLatch.countDown(); new CountDownLatch(1).await(); // wait forever return null; } });
@Test(timeout = 1000) public void testUnSubscribeObservableOfObservables() throws InterruptedException { final AtomicBoolean unsubscribed = new AtomicBoolean(); final CountDownLatch latch = new CountDownLatch(1); final AtomicInteger count = new AtomicInteger(); Observable.merge(source).take(6).blockingForEach(new Consumer<Long>() { latch.await(1000, TimeUnit.MILLISECONDS); System.out.println("unsubscribed: " + unsubscribed.get()); assertTrue(unsubscribed.get());
AtomicBoolean computationShouldWait = new AtomicBoolean(); CountDownLatch computationLatch = new CountDownLatch(1); QueuingRemovalListener<String, String> listener = queuingRemovalListener(); final LoadingCache<String, String> cache = expectedKeys.add(s); computationShouldWait.set(true); final AtomicInteger computedCount = new AtomicInteger(); ExecutorService threadPool = Executors.newFixedThreadPool(nThreads); final CountDownLatch tasksFinished = new CountDownLatch(nTasks); for (int i = 0; i < nTasks; i++) { final String s = "a" + i; @SuppressWarnings("unused") // go/futurereturn-lsc Future<?> possiblyIgnoredError = threadPool.submit( new Runnable() { @Override computationLatch.countDown(); while (computedCount.get() < nThreads) { Thread.yield(); tasksFinished.await();
private void unlockExclusiveAndTakeWriteLockMustBeAtomic() throws Exception CountDownLatch start = new CountDownLatch( threads ); AtomicBoolean stop = new AtomicBoolean(); OffHeapPageLock.tryExclusiveLock( lockAddr ); Runnable runnable = () -> while ( !stop.get() ) throw new RuntimeException( "I should not have gotten that lock" ); start.countDown(); for ( int i = 0; i < threads; i++ ) futures.add( executor.submit( runnable ) ); start.await(); OffHeapPageLock.unlockExclusiveAndTakeWriteLock( lockAddr ); stop.set( true ); for ( Future<?> future : futures )
final int numRetries = Flowable.bufferSize() * 2; int ncpu = Runtime.getRuntime().availableProcessors(); ExecutorService exec = Executors.newFixedThreadPool(Math.max(ncpu / 2, 2)); try { for (int r = 0; r < NUM_LOOPS; r++) { final AtomicInteger timeouts = new AtomicInteger(); final Map<Integer, List<String>> data = new ConcurrentHashMap<Integer, List<String>>(); final CountDownLatch cdl = new CountDownLatch(m); for (int i = 0; i < m; i++) { final int j = i; exec.execute(new Runnable() { @Override public void run() { cdl.await(); assertEquals(0, timeouts.get()); if (data.size() > 0) { fail("Data content mismatch: " + allSequenceFrequency(data)); exec.shutdown();
final AtomicBoolean functionCalled = new AtomicBoolean(); AsyncFunction<String, Integer> function = new AsyncFunction<String, Integer>() { ExecutorService executor = newSingleThreadExecutor(); ListenableFuture<Integer> future = transformAsync(inputFuture, function, executor); final CountDownLatch beforeFunction = new CountDownLatch(1); executor.execute( new Runnable() { @Override beforeFunction.countDown(); executor.shutdown(); assertTrue(executor.awaitTermination(5, SECONDS)); assertFalse(functionCalled.get());
final CountDownLatch queueShouldBeFullSignal = new CountDownLatch(capacity + 1); final CountDownLatch taskCompletedSignal = new CountDownLatch(nTasks); final CountDownLatch taskStartSignal = new CountDownLatch(1); final AtomicInteger producedCount = new AtomicInteger(); final AtomicInteger consumedCount = new AtomicInteger(); final ExecutorService producer = Executors.newSingleThreadExecutor( new ThreadFactoryBuilder().setNameFormat( "ExecsTest-Producer-%d" ).build() ); producer.submit( new Runnable() queueShouldBeFullSignal.await(); Assert.assertEquals(capacity + 1, producedCount.get()); taskStartSignal.countDown(); taskCompletedSignal.await(); Assert.assertEquals(nTasks, consumedCount.get()); blockingExecutor.shutdown(); producer.shutdown();
public void testMessageListenerWithConsumer() throws Exception { final AtomicInteger counter = new AtomicInteger(0); final CountDownLatch done = new CountDownLatch(1); // Receive a message with the JMS API connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQDestination destination = createDestination(session, destinationType); MessageConsumer consumer = session.createConsumer(destination); consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message m) { counter.incrementAndGet(); if (counter.get() == 4) { done.countDown(); } } }); // Send the messages sendMessages(session, destination, 4); assertTrue(done.await(1000, TimeUnit.MILLISECONDS)); Thread.sleep(200); // Make sure only 4 messages were delivered. assertEquals(4, counter.get()); }
int end = 1000; AtomicInteger index = new AtomicInteger(start); AtomicReference<Object> result = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); int expected = index.getAndIncrement(); Integer actual = (Integer) message.getHeaders().getOrDefault("seq", -1); if (actual != expected) { result.set("Expected: " + expected + ", but was: " + actual); latch.countDown(); return; result.set(ex.toString()); latch.countDown(); result.set("Done"); latch.countDown(); latch.await(10, TimeUnit.SECONDS); assertEquals("Done", result.get());
final CountDownLatch latch = new CountDownLatch(1); final AtomicInteger count = new AtomicInteger(); final AtomicReference<Throwable> error = new AtomicReference<Throwable>(); latch.await(); assertEquals(2, count.get()); assertNotNull(error.get()); if (!(error.get() instanceof NumberFormatException)) { fail("It should be a NumberFormatException");
ExecutorService exec = Executors.newFixedThreadPool(1); try { server.connectHandler(so -> { so.handler(buff -> { assertEquals(256, buff.length()); CountDownLatch latch = new CountDownLatch(1); exec.execute(() -> { latch.countDown(); so.write(expected); }); }); startServer(); AtomicInteger done = new AtomicInteger(); for (int i = 0;i < num;i++) { client.connect(testAddress, ar -> { assertEquals(expected, buff); so.close(); int val = done.incrementAndGet(); if (val == num) { testComplete(); exec.shutdown();
@Test public void testListenableFuture() throws Exception { final AtomicInteger statusCode = new AtomicInteger(500); try (AsyncHttpClient ahc = asyncHttpClient()) { final CountDownLatch latch = new CountDownLatch(1); final ListenableFuture<Response> future = ahc.prepareGet(getTargetUrl()).execute(); future.addListener(() -> { try { statusCode.set(future.get().getStatusCode()); latch.countDown(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } }, Executors.newFixedThreadPool(1)); latch.await(10, TimeUnit.SECONDS); assertEquals(statusCode.get(), 200); } }
final SerialDisposable task = new SerialDisposable(); try { final CountDownLatch finished = new CountDownLatch(1); final int COUNT = 30; final CountDownLatch timeHasPassed = new CountDownLatch(COUNT); final AtomicBoolean running = new AtomicBoolean(true); final AtomicInteger count = new AtomicInteger(0); final Flowable<Integer> obs = Flowable.unsafeCreate(new Publisher<Integer>() { timeHasPassed.await(8000, TimeUnit.MILLISECONDS); running.set(false); finished.await();
@Test public void concurrentSyslogMessageTest() throws Exception { SyslogMessageTask syslogMessage = null; ExecutorService service = Executors.newFixedThreadPool( numOfTasks ); for ( int i = 0; i < numOfTasks; i++ ) { syslogMessage = createSyslogMessageTask(); service.execute( syslogMessage ); } service.shutdown(); countDownLatch.countDown(); service.awaitTermination( 10000, TimeUnit.NANOSECONDS ); Assert.assertTrue( numOfErrors.get() == 0 ); }
@Test public void testServerConnectionHandler() throws Exception { AtomicInteger status = new AtomicInteger(); AtomicReference<HttpConnection> connRef = new AtomicReference<>(); server.connectionHandler(conn -> { assertEquals(0, status.getAndIncrement()); assertNull(connRef.getAndSet(conn)); }); server.requestHandler(req -> { assertEquals(1, status.getAndIncrement()); assertSame(connRef.get(), req.connection()); req.response().end(); }); CountDownLatch listenLatch = new CountDownLatch(1); server.listen(onSuccess(s -> listenLatch.countDown())); awaitLatch(listenLatch); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { testComplete(); }); await(); }
public void testStreamForEachConcurrentStressTest() throws Throwable { if (!impl.isConcurrent()) return; final Collection c = impl.emptyCollection(); final long testDurationMillis = timeoutMillis(); final AtomicBoolean done = new AtomicBoolean(false); final Object elt = impl.makeElement(1); final Future<?> f1, f2; final ExecutorService pool = Executors.newCachedThreadPool(); try (PoolCleaner cleaner = cleaner(pool, done)) { final CountDownLatch threadsStarted = new CountDownLatch(2); Runnable checkElt = () -> { threadsStarted.countDown(); while (!done.get()) c.stream().forEach(x -> assertSame(x, elt)); }; Runnable addRemove = () -> { threadsStarted.countDown(); while (!done.get()) { assertTrue(c.add(elt)); assertTrue(c.remove(elt)); }}; f1 = pool.submit(checkElt); f2 = pool.submit(addRemove); Thread.sleep(testDurationMillis); } assertNull(f1.get(0L, MILLISECONDS)); assertNull(f2.get(0L, MILLISECONDS)); }
String threadName = Thread.currentThread().getName(); try { producer = session.createProducer(destination); producer.setDeliveryMode(persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT); producer.setTimeToLive(msgTTL); while (running) { synchronized (this) { paused.await(); sentCount.incrementAndGet(); for (sentCount.set(0); sentCount.get() < messageCount && running; sentCount.incrementAndGet()) { synchronized (this) { paused.await(); } finally { if (finished != null) { finished.countDown();