protected void awaitLatch(CountDownLatch latch, long seconds) { try { assertTrue(latch.await(seconds, TimeUnit.SECONDS)); } catch (InterruptedException e) { throw new RuntimeException(e); } }
@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(); } }
@Test public void registerWebSocketHandlerWithSockJS() throws Exception { WebSocketSession session = this.webSocketClient.doHandshake( new AbstractWebSocketHandler() {}, getWsBaseUrl() + "/sockjs/websocket").get(); TestHandler serverHandler = this.wac.getBean(TestHandler.class); assertTrue(serverHandler.connectLatch.await(2, TimeUnit.SECONDS)); session.close(); }
@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 getAndInterceptResponse() throws Exception { RequestInterceptor interceptor = new RequestInterceptor(); template.setInterceptors(Collections.singletonList(interceptor)); ListenableFuture<ResponseEntity<String>> future = template.getForEntity(baseUrl + "/get", String.class); interceptor.latch.await(5, TimeUnit.SECONDS); assertNotNull(interceptor.response); assertEquals(HttpStatus.OK, interceptor.response.getStatusCode()); assertNull(interceptor.exception); assertEquals(helloWorld, future.get().getBody()); }
@Test public final void testObserveOn() throws InterruptedException { final Scheduler scheduler = getScheduler(); Flowable<String> f = Flowable.fromArray("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"); ConcurrentObserverValidator<String> observer = new ConcurrentObserverValidator<String>(); f.observeOn(scheduler).subscribe(observer); if (!observer.completed.await(3000, TimeUnit.MILLISECONDS)) { fail("timed out"); } if (observer.error.get() != null) { observer.error.get().printStackTrace(); fail("Error: " + observer.error.get().getMessage()); } }
public static void main(String[] args) throws TimeoutException, InterruptedException { Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>( LongEvent.FACTORY, 16, DaemonThreadFactory.INSTANCE ); CountDownLatch shutdownLatch = new CountDownLatch(2); disruptor.handleEventsWith(new Handler(shutdownLatch)).then(new Handler(shutdownLatch)); disruptor.start(); long next = disruptor.getRingBuffer().next(); disruptor.getRingBuffer().get(next).set(next); disruptor.getRingBuffer().publish(next); disruptor.shutdown(10, TimeUnit.SECONDS); shutdownLatch.await(); System.out.println(value); } }
@Override public Void call() throws Exception { enterLatch.countDown(); new CountDownLatch(1).await(); // wait forever return null; } });
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"); }
private void block() { try { this.releaseLatch.set(new CountDownLatch(1)); this.releaseLatch.get().await(); } catch (InterruptedException e) { e.printStackTrace(); } }
@Test public void scheduleDirectDelayed() throws Exception { Scheduler s = getScheduler(); final CountDownLatch cdl = new CountDownLatch(1); s.scheduleDirect(new Runnable() { @Override public void run() { cdl.countDown(); } }, 50, TimeUnit.MILLISECONDS); assertTrue(cdl.await(5, TimeUnit.SECONDS)); }
@Test public void registerWebSocketHandler() throws Exception { WebSocketSession session = this.webSocketClient.doHandshake( new AbstractWebSocketHandler() {}, getWsBaseUrl() + "/ws").get(); TestHandler serverHandler = this.wac.getBean(TestHandler.class); assertTrue(serverHandler.connectLatch.await(2, TimeUnit.SECONDS)); session.close(); }
@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 getAndInterceptError() throws Exception { RequestInterceptor interceptor = new RequestInterceptor(); template.setInterceptors(Collections.singletonList(interceptor)); template.getForEntity(baseUrl + "/status/notfound", String.class); interceptor.latch.await(5, TimeUnit.SECONDS); assertNotNull(interceptor.response); assertEquals(HttpStatus.NOT_FOUND, interceptor.response.getStatusCode()); assertNull(interceptor.exception); }
@Test public void asyncProcessingApplied() throws InterruptedException { loadAsync(AsyncEventListener.class); String threadName = Thread.currentThread().getName(); AnotherTestEvent event = new AnotherTestEvent(this, threadName); AsyncEventListener listener = this.context.getBean(AsyncEventListener.class); this.eventCollector.assertNoEventReceived(listener); this.context.publishEvent(event); this.countDownLatch.await(2, TimeUnit.SECONDS); this.eventCollector.assertEvent(listener, event); this.eventCollector.assertTotalEventsCount(1); }
@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"); } };
protected void awaitLatch(CountDownLatch latch, long seconds) { try { assertTrue(latch.await(seconds, TimeUnit.SECONDS)); } catch (InterruptedException e) { throw new RuntimeException(e); } }
public void testAddAfterRun() throws Exception { // Run the previous test testRunOnPopulatedList(); // If it passed, then verify an Add will be executed without calling run CountDownLatch countDownLatch = new CountDownLatch(1); list.add(new MockRunnable(countDownLatch), Executors.newCachedThreadPool()); assertTrue(countDownLatch.await(1L, TimeUnit.SECONDS)); }