@Test public void testRacingRegistrations() throws InterruptedException { for (int i = 0; i < ITERATIONS; i++) { startLatch = new CountDownLatch(THREAD_COUNT); registeredLatch = new CountDownLatch(THREAD_COUNT); canUnregisterLatch = new CountDownLatch(1); unregisteredLatch = new CountDownLatch(THREAD_COUNT); List<SubscriberThread> threads = startThreads(); registeredLatch.await(); eventBus.post("42"); canUnregisterLatch.countDown(); for (int t = 0; t < THREAD_COUNT; t++) { int eventCount = threads.get(t).eventCount; if (eventCount != 1) { fail("Failed in iteration " + i + ": thread #" + t + " has event count of " + eventCount); } } // Wait for threads to be done unregisteredLatch.await(); } }
@Override public Void call() throws Exception { enterLatch.countDown(); new CountDownLatch(1).await(); // wait forever return null; } });
private void block() { try { this.releaseLatch.set(new CountDownLatch(1)); this.releaseLatch.get().await(); } catch (InterruptedException e) { e.printStackTrace(); } }
@Test public void testLongTimeAction() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); LongTimeAction action = new LongTimeAction(latch); Observable.just(1).buffer(10, TimeUnit.MILLISECONDS, 10) .subscribe(action); latch.await(); assertFalse(action.fail); }
@Test public void testStartAsync() throws InterruptedException { Observable<String> os = ASYNC_OBSERVABLE_OF_INFINITE_INTEGERS(new CountDownLatch(1)) .zipWith(ASYNC_OBSERVABLE_OF_INFINITE_INTEGERS(new CountDownLatch(1)), new BiFunction<Integer, Integer, String>() { @Override public String apply(Integer a, Integer b) { return a + "-" + b; } }).take(5); TestObserver<String> to = new TestObserver<String>(); os.subscribe(to); to.awaitTerminalEvent(); to.assertNoErrors(); assertEquals(5, to.valueCount()); assertEquals("1-1", to.values().get(0)); assertEquals("2-2", to.values().get(1)); assertEquals("5-5", to.values().get(4)); }
@Test public void getAsyncResultSuccess() { this.mvcResult.setAsyncResult("Foo"); this.mvcResult.setAsyncDispatchLatch(new CountDownLatch(0)); this.mvcResult.getAsyncResult(); }
@Test public void interrupted() { CountDownLatch cdl = new CountDownLatch(1); Disposable d = Disposables.empty(); Thread.currentThread().interrupt(); try { BlockingHelper.awaitForComplete(cdl, d); } catch (IllegalStateException ex) { // expected } assertTrue(d.isDisposed()); assertTrue(Thread.interrupted()); }
@Test public void testSharedServersRoundRobinWithOtherServerRunningOnDifferentPort() throws Exception { // Have a server running on a different port to make sure it doesn't interact CountDownLatch latch = new CountDownLatch(1); HttpServer theServer = vertx.createHttpServer(new HttpServerOptions().setPort(8081)); theServer.requestHandler(req -> { fail("Should not process request"); }).listen(onSuccess(s -> latch.countDown())); awaitLatch(latch); testSharedServersRoundRobin(); }
private void runThreadsSingleEventType(int threadCount) throws InterruptedException { int iterations = COUNT / threadCount; eventBus.register(this); CountDownLatch latch = new CountDownLatch(threadCount + 1); List<PosterThread> threads = startThreads(latch, threadCount, iterations, "Hello"); long time = triggerAndWaitForThreads(threads, latch); log(threadCount + " threads posted " + iterations + " events each in " + time + "ms"); waitForEventCount(COUNT * 2, 5000); assertEquals("Hello", lastStringEvent); int expectedCount = threadCount * iterations; assertEquals(expectedCount, countStringEvent.intValue()); assertEquals(expectedCount, countObjectEvent.intValue()); }
protected void close(Vertx vertx) throws InterruptedException { if (vertx == null) { return; } CountDownLatch latch = new CountDownLatch(1); vertx.close(ar -> { latch.countDown(); }); awaitLatch(latch); }
private void assertRunnableDecorated(Runnable scheduleCall) throws InterruptedException { try { final CountDownLatch decoratedCalled = new CountDownLatch(1); RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() { @Override public Runnable apply(final Runnable actual) throws Exception { return new Runnable() { @Override public void run() { decoratedCalled.countDown(); actual.run(); } }; } }); scheduleCall.run(); assertTrue(decoratedCalled.await(5, TimeUnit.SECONDS)); } finally { RxJavaPlugins.reset(); } }
@Test public void scheduleDirect() throws Exception { Scheduler s = getScheduler(); final CountDownLatch cdl = new CountDownLatch(1); s.scheduleDirect(new Runnable() { @Override public void run() { cdl.countDown(); } }); assertTrue(cdl.await(5, TimeUnit.SECONDS)); }
@Test public void testLongTimeAction() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); LongTimeAction action = new LongTimeAction(latch); Flowable.just(1).buffer(10, TimeUnit.MILLISECONDS, 10) .subscribe(action); latch.await(); assertFalse(action.fail); }
@Test public void testStartAsync() throws InterruptedException { Flowable<String> os = ASYNC_OBSERVABLE_OF_INFINITE_INTEGERS(new CountDownLatch(1)).onBackpressureBuffer() .zipWith(ASYNC_OBSERVABLE_OF_INFINITE_INTEGERS(new CountDownLatch(1)).onBackpressureBuffer(), new BiFunction<Integer, Integer, String>() { @Override public String apply(Integer a, Integer b) { return a + "-" + b; } }).take(5); TestSubscriber<String> ts = new TestSubscriber<String>(); os.subscribe(ts); ts.awaitTerminalEvent(); ts.assertNoErrors(); assertEquals(5, ts.valueCount()); assertEquals("1-1", ts.values().get(0)); assertEquals("2-2", ts.values().get(1)); assertEquals("5-5", ts.values().get(4)); }
@Test public void unwrapScheduleDirectTaskAfterDispose() { Scheduler scheduler = getScheduler(); final CountDownLatch cdl = new CountDownLatch(1); Runnable countDownRunnable = new Runnable() { @Override public void run() { cdl.countDown(); } }; Disposable disposable = scheduler.scheduleDirect(countDownRunnable, 100, TimeUnit.MILLISECONDS); SchedulerRunnableIntrospection wrapper = (SchedulerRunnableIntrospection) disposable; assertSame(countDownRunnable, wrapper.getWrappedRunnable()); disposable.dispose(); assertSame(Functions.EMPTY_RUNNABLE, wrapper.getWrappedRunnable()); }
@Override public ListenableFuture<String> call() throws Exception { inFunction.countDown(); try { new CountDownLatch(1).await(); // wait for interrupt } catch (InterruptedException expected) { gotException.countDown(); throw expected; } return immediateFuture("a"); } };
@Test public void testReplyFailureNoHandlers() throws Exception { CountDownLatch latch = new CountDownLatch(1); EventBus eb = vertx.eventBus(); eb.send(ADDRESS1, "bar", new DeliveryOptions().setSendTimeout(10), ar -> { assertTrue(ar.failed()); latch.countDown(); }); awaitLatch(latch); FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(eb); assertEquals(Collections.singletonList(ADDRESS1), metrics.getReplyFailureAddresses()); assertEquals(Collections.singletonList(ReplyFailure.NO_HANDLERS), metrics.getReplyFailures()); }
private void runThreadsSingleEventType(int threadCount) throws InterruptedException { int iterations = COUNT / threadCount; eventBus.register(this); CountDownLatch latch = new CountDownLatch(threadCount + 1); List<PosterThread> threads = startThreads(latch, threadCount, iterations, "Hello"); long time = triggerAndWaitForThreads(threads, latch); log(threadCount + " threads posted " + iterations + " events each in " + time + "ms"); waitForEventCount(COUNT * 2, 5000); assertEquals("Hello", lastStringEvent); int expectedCount = threadCount * iterations; assertEquals(expectedCount, countStringEvent.intValue()); assertEquals(expectedCount, countObjectEvent.intValue()); }
protected void close(Vertx vertx) throws Exception { CountDownLatch latch = new CountDownLatch(1); vertx.close(ar -> { latch.countDown(); }); awaitLatch(latch); } }
private void stopActiveMqBrokerAndAwait() throws Exception { logger.debug("Stopping ActiveMQ broker and will await shutdown"); if (!this.activeMQBroker.isStarted()) { logger.debug("Broker not running"); return; } final CountDownLatch latch = new CountDownLatch(1); this.activeMQBroker.addShutdownHook(new Runnable() { public void run() { latch.countDown(); } }); this.activeMQBroker.stop(); assertTrue("Broker did not stop", latch.await(5, TimeUnit.SECONDS)); logger.debug("Broker stopped"); }