public void testShutdownAndAwaitTermination_immediateShutdown() throws Exception { ExecutorService service = Executors.newSingleThreadExecutor(); assertTrue(shutdownAndAwaitTermination(service, 1L, SECONDS)); assertTrue(service.isTerminated()); }
public void testShutdownAndAwaitTermination_forcedShutDownInternal() throws Exception { ExecutorService service = mock(ExecutorService.class); when(service.awaitTermination(HALF_SECOND_NANOS, NANOSECONDS)) .thenReturn(false) .thenReturn(true); when(service.isTerminated()).thenReturn(true); assertTrue(shutdownAndAwaitTermination(service, 1L, SECONDS)); verify(service).shutdown(); verify(service, times(2)).awaitTermination(HALF_SECOND_NANOS, NANOSECONDS); verify(service).shutdownNow(); }
public void testShutdownAndAwaitTermination_immediateShutdownInternal() throws Exception { ExecutorService service = mock(ExecutorService.class); when(service.awaitTermination(HALF_SECOND_NANOS, NANOSECONDS)).thenReturn(true); when(service.isTerminated()).thenReturn(true); assertTrue(shutdownAndAwaitTermination(service, 1L, SECONDS)); verify(service).shutdown(); verify(service).awaitTermination(HALF_SECOND_NANOS, NANOSECONDS); }
public void testDirectExecutorService_shutdownNow() { ExecutorService executor = newDirectExecutorService(); assertEquals(ImmutableList.of(), executor.shutdownNow()); assertTrue(executor.isShutdown()); }
public void testShutdownAndAwaitTermination_interruptedInternal() throws Exception { final ExecutorService service = mock(ExecutorService.class); when(service.awaitTermination(HALF_SECOND_NANOS, NANOSECONDS)) .thenThrow(new InterruptedException()); final AtomicBoolean terminated = new AtomicBoolean(); // we need to keep this in a flag because t.isInterrupted() returns false after t.join() final AtomicBoolean interrupted = new AtomicBoolean(); // we need to use another thread because it will be interrupted and thus using // the current one, owned by JUnit, would make the test fail Thread thread = new Thread( new Runnable() { @Override public void run() { terminated.set(shutdownAndAwaitTermination(service, 1L, SECONDS)); interrupted.set(Thread.currentThread().isInterrupted()); } }); thread.start(); thread.join(); verify(service).shutdown(); verify(service).awaitTermination(HALF_SECOND_NANOS, NANOSECONDS); verify(service).shutdownNow(); assertTrue(interrupted.get()); assertFalse(terminated.get()); } }
public void testGetExitingExecutorService_executorSetToUseDaemonThreads() { TestApplication application = new TestApplication(); ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1)); assertNotNull(application.getExitingExecutorService(executor)); assertTrue(executor.getThreadFactory().newThread(EMPTY_RUNNABLE).isDaemon()); }
public void testGetExitingScheduledExecutorService_executorSetToUseDaemonThreads() { TestApplication application = new TestApplication(); ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); assertNotNull(application.getExitingScheduledExecutorService(executor)); assertTrue(executor.getThreadFactory().newThread(EMPTY_RUNNABLE).isDaemon()); }
public void testDirectExecutorServiceInvokeAll() throws Exception { final ExecutorService executor = newDirectExecutorService(); final ThreadLocal<Integer> threadLocalCount = new ThreadLocal<Integer>() { @Override protected Integer initialValue() { return 0; } }; final Callable<Integer> incrementTask = new Callable<Integer>() { @Override public Integer call() { int i = threadLocalCount.get(); threadLocalCount.set(i + 1); return i; } }; List<Future<Integer>> futures = executor.invokeAll(Collections.nCopies(10, incrementTask)); for (int i = 0; i < 10; i++) { Future<Integer> future = futures.get(i); assertTrue("Task should have been run before being returned", future.isDone()); assertEquals(i, future.get().intValue()); } assertEquals(10, threadLocalCount.get().intValue()); }
public void testListeningDecorator_cancelled() throws Exception { ScheduledThreadPoolExecutor delegate = new ScheduledThreadPoolExecutor(1); BlockingQueue<?> delegateQueue = delegate.getQueue(); ListeningScheduledExecutorService service = listeningDecorator(delegate); ListenableFuture<?> future; ScheduledFuture<?> delegateFuture; Runnable runnable = new Runnable() { @Override public void run() {} }; future = service.schedule(runnable, 5, TimeUnit.MINUTES); future.cancel(true); assertTrue(future.isCancelled()); delegateFuture = (ScheduledFuture<?>) delegateQueue.element(); assertTrue(delegateFuture.isCancelled()); delegateQueue.clear(); future = service.scheduleAtFixedRate(runnable, 5, 5, TimeUnit.MINUTES); future.cancel(true); assertTrue(future.isCancelled()); delegateFuture = (ScheduledFuture<?>) delegateQueue.element(); assertTrue(delegateFuture.isCancelled()); delegateQueue.clear(); future = service.scheduleWithFixedDelay(runnable, 5, 5, TimeUnit.MINUTES); future.cancel(true); assertTrue(future.isCancelled()); delegateFuture = (ScheduledFuture<?>) delegateQueue.element(); assertTrue(delegateFuture.isCancelled()); }
assertTrue(executor.isShutdown()); try { executor.submit(doNothingRunnable); assertTrue(executor.awaitTermination(1, TimeUnit.SECONDS)); assertTrue(executor.awaitTermination(0, TimeUnit.SECONDS)); assertTrue(executor.isShutdown()); try { executor.submit(doNothingRunnable); assertTrue(executor.isTerminated());
assertTrue(future.isDone()); assertListenerRunImmediately(future); assertEquals(1, threadLocalCount.get().intValue());
public void testListeningDecorator_scheduleSuccess() throws Exception { final CountDownLatch completed = new CountDownLatch(1); ScheduledThreadPoolExecutor delegate = new ScheduledThreadPoolExecutor(1) { @Override protected void afterExecute(Runnable r, Throwable t) { completed.countDown(); } }; ListeningScheduledExecutorService service = listeningDecorator(delegate); ListenableFuture<Integer> future = service.schedule(Callables.returning(42), 1, TimeUnit.MILLISECONDS); /* * Wait not just until the Future's value is set (as in future.get()) but * also until ListeningScheduledExecutorService's wrapper task is done * executing listeners, as detected by yielding control to afterExecute. */ completed.await(); assertTrue(future.isDone()); assertThat(future.get()).isEqualTo(42); assertListenerRunImmediately(future); assertEquals(0, delegate.getQueue().size()); }
public void testShutdownAndAwaitTermination_immediateShutdown() throws Exception { ExecutorService service = Executors.newSingleThreadExecutor(); assertTrue(shutdownAndAwaitTermination(service, 1L, SECONDS)); assertTrue(service.isTerminated()); }
public void testShutdownAndAwaitTermination_immediateShutdownInternal() throws Exception { ExecutorService service = mock(ExecutorService.class); when(service.awaitTermination(HALF_SECOND_NANOS, NANOSECONDS)).thenReturn(true); when(service.isTerminated()).thenReturn(true); assertTrue(shutdownAndAwaitTermination(service, 1L, SECONDS)); verify(service).shutdown(); verify(service).awaitTermination(HALF_SECOND_NANOS, NANOSECONDS); }
public void testShutdownAndAwaitTermination_forcedShutDownInternal() throws Exception { ExecutorService service = mock(ExecutorService.class); when(service.awaitTermination(HALF_SECOND_NANOS, NANOSECONDS)) .thenReturn(false).thenReturn(true); when(service.isTerminated()).thenReturn(true); assertTrue(shutdownAndAwaitTermination(service, 1L, SECONDS)); verify(service).shutdown(); verify(service, times(2)).awaitTermination(HALF_SECOND_NANOS, NANOSECONDS); verify(service).shutdownNow(); }
public void testDirectExecutorService_shutdownNow() { ExecutorService executor = newDirectExecutorService(); assertEquals(ImmutableList.of(), executor.shutdownNow()); assertTrue(executor.isShutdown()); }
public void testShutdownAndAwaitTermination_interruptedInternal() throws Exception { final ExecutorService service = mock(ExecutorService.class); when(service.awaitTermination(HALF_SECOND_NANOS, NANOSECONDS)) .thenThrow(new InterruptedException()); final AtomicBoolean terminated = new AtomicBoolean(); // we need to keep this in a flag because t.isInterrupted() returns false after t.join() final AtomicBoolean interrupted = new AtomicBoolean(); // we need to use another thread because it will be interrupted and thus using // the current one, owned by JUnit, would make the test fail Thread thread = new Thread(new Runnable() { @Override public void run() { terminated.set(shutdownAndAwaitTermination(service, 1L, SECONDS)); interrupted.set(Thread.currentThread().isInterrupted()); } }); thread.start(); thread.join(); verify(service).shutdown(); verify(service).awaitTermination(HALF_SECOND_NANOS, NANOSECONDS); verify(service).shutdownNow(); assertTrue(interrupted.get()); assertFalse(terminated.get()); } }
public void testGetExitingExecutorService_executorSetToUseDaemonThreads() { TestApplication application = new TestApplication(); ThreadPoolExecutor executor = new ThreadPoolExecutor( 1, 2, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1)); assertNotNull(application.getExitingExecutorService(executor)); assertTrue(executor.getThreadFactory().newThread(EMPTY_RUNNABLE).isDaemon()); }
public void testGetExitingScheduledExecutorService_executorSetToUseDaemonThreads() { TestApplication application = new TestApplication(); ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); assertNotNull(application.getExitingScheduledExecutorService(executor)); assertTrue(executor.getThreadFactory().newThread(EMPTY_RUNNABLE).isDaemon()); }
public void testListeningDecorator_scheduleSuccess() throws Exception { final CountDownLatch completed = new CountDownLatch(1); ScheduledThreadPoolExecutor delegate = new ScheduledThreadPoolExecutor(1) { @Override protected void afterExecute(Runnable r, Throwable t) { completed.countDown(); } }; ListeningScheduledExecutorService service = listeningDecorator(delegate); ListenableFuture<Integer> future = service.schedule(Callables.returning(42), 1, TimeUnit.MILLISECONDS); /* * Wait not just until the Future's value is set (as in future.get()) but * also until ListeningScheduledExecutorService's wrapper task is done * executing listeners, as detected by yielding control to afterExecute. */ completed.await(); assertTrue(future.isDone()); assertThat(future.get()).isEqualTo(42); assertListenerRunImmediately(future); assertEquals(0, delegate.getQueue().size()); }