assertFalse(executor.isShutdown()); assertFalse(executor.isTerminated()); assertTrue(executor.isShutdown()); try { executor.submit(doNothingRunnable); fail("Should have encountered RejectedExecutionException"); } catch (RejectedExecutionException ex) { assertFalse(executor.isTerminated()); assertFalse(executor.awaitTermination(20, TimeUnit.MILLISECONDS)); assertTrue(executor.awaitTermination(1, TimeUnit.SECONDS)); assertTrue(executor.awaitTermination(0, TimeUnit.SECONDS)); assertTrue(executor.isShutdown()); try { executor.submit(doNothingRunnable); fail("Should have encountered RejectedExecutionException"); } catch (RejectedExecutionException ex) { assertTrue(executor.isTerminated()); assertEquals(Thread.State.TERMINATED, otherThread.getState()); Throwable throwable = throwableFromOtherThread.get(); assertNull( "Throwable from other thread: " + (throwable == null ? null : Throwables.getStackTraceAsString(throwable)),
public void testListeningDecorator_scheduleFailure() throws Exception { ScheduledThreadPoolExecutor delegate = new ScheduledThreadPoolExecutor(1); ListeningScheduledExecutorService service = listeningDecorator(delegate); RuntimeException ex = new RuntimeException(); ListenableFuture<?> future = service.schedule(new ThrowingRunnable(0, ex), 1, TimeUnit.MILLISECONDS); assertExecutionException(future, ex); assertEquals(0, delegate.getQueue().size()); }
assertTrue(future.isDone()); assertListenerRunImmediately(future); assertEquals(1, threadLocalCount.get().intValue()); otherThread.join(1000); assertEquals(Thread.State.TERMINATED, otherThread.getState()); Throwable throwable = throwableFromOtherThread.get(); assertNull( "Throwable from other thread: " + (throwable == null ? null : Throwables.getStackTraceAsString(throwable)),
public void testDirectExecutorService_shutdownNow() { ExecutorService executor = newDirectExecutorService(); assertEquals(ImmutableList.of(), executor.shutdownNow()); assertTrue(executor.isShutdown()); }
public void testPlatformThreadFactory_default() { ThreadFactory factory = MoreExecutors.platformThreadFactory(); assertNotNull(factory); // Executors#defaultThreadFactory() may return a new instance each time. assertEquals(factory.getClass(), Executors.defaultThreadFactory().getClass()); }
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 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 testShutdownAndAwaitTermination_immediateShutdown() throws Exception { ExecutorService service = Executors.newSingleThreadExecutor(); assertTrue(shutdownAndAwaitTermination(service, 1L, SECONDS)); assertTrue(service.isTerminated()); }
/** invokeAny(c) returns result of some task in c if at least one completes */ public void testInvokeAnyImpl() throws Exception { ListeningExecutorService e = newDirectExecutorService(); try { List<Callable<String>> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); String result = invokeAnyImpl(e, l, false, 0, TimeUnit.NANOSECONDS); assertSame(TEST_STRING, result); } finally { joinPool(e); } }
/** * Test for a bug where threads weren't getting signaled when shutdown was called, only when tasks * completed. */ public void testDirectExecutorService_awaitTermination_missedSignal() { final ExecutorService service = MoreExecutors.newDirectExecutorService(); Thread waiter = new Thread() { @Override public void run() { try { service.awaitTermination(1, TimeUnit.DAYS); } catch (InterruptedException e) { return; } } }; waiter.start(); awaitTimedWaiting(waiter); service.shutdown(); Uninterruptibles.joinUninterruptibly(waiter, 10, TimeUnit.SECONDS); if (waiter.isAlive()) { waiter.interrupt(); fail("awaitTermination failed to trigger after shutdown()"); } }
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()); } }
private static void assertExecutionException(Future<?> future, Exception expectedCause) throws Exception { try { future.get(); fail("Expected ExecutionException"); } catch (ExecutionException e) { assertSame(expectedCause, e.getCause()); } }
public void testThreadRenaming() { Executor renamingExecutor = renamingDecorator(newDirectExecutorService(), Suppliers.ofInstance("FooBar")); String oldName = Thread.currentThread().getName(); renamingExecutor.execute( new Runnable() { @Override public void run() { assertEquals("FooBar", Thread.currentThread().getName()); } }); assertEquals(oldName, Thread.currentThread().getName()); }
public void testExecuteAfterShutdown() { ExecutorService executor = newDirectExecutorService(); executor.shutdown(); try { executor.execute(EMPTY_RUNNABLE); fail(); } catch (RejectedExecutionException expected) { } }
public void testShutdownAndAwaitTermination_nonTerminationInternal() throws Exception { ExecutorService service = mock(ExecutorService.class); when(service.awaitTermination(HALF_SECOND_NANOS, NANOSECONDS)) .thenReturn(false) .thenReturn(false); assertFalse(shutdownAndAwaitTermination(service, 1L, SECONDS)); verify(service).shutdown(); verify(service, times(2)).awaitTermination(HALF_SECOND_NANOS, NANOSECONDS); verify(service).shutdownNow(); }
public void testListeningDecorator() throws Exception { ListeningExecutorService service = listeningDecorator(newDirectExecutorService()); assertSame(service, listeningDecorator(service)); List<Callable<String>> callables = ImmutableList.of(Callables.returning("x")); List<Future<String>> results; results = service.invokeAll(callables); assertThat(getOnlyElement(results)).isInstanceOf(TrustedListenableFutureTask.class); results = service.invokeAll(callables, 1, SECONDS); assertThat(getOnlyElement(results)).isInstanceOf(TrustedListenableFutureTask.class); /* * TODO(cpovirk): move ForwardingTestCase somewhere common, and use it to * test the forwarded methods */ }
/** invokeAny(empty collection) throws IAE */ public void testInvokeAnyImpl_emptyTasks() throws Exception { ListeningExecutorService e = newDirectExecutorService(); try { invokeAnyImpl(e, new ArrayList<Callable<String>>(), false, 0, TimeUnit.NANOSECONDS); fail(); } catch (IllegalArgumentException success) { } finally { joinPool(e); } }
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 testGetExitingScheduledExecutorService_executorSetToUseDaemonThreads() { TestApplication application = new TestApplication(); ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); assertNotNull(application.getExitingScheduledExecutorService(executor)); assertTrue(executor.getThreadFactory().newThread(EMPTY_RUNNABLE).isDaemon()); }