public void testExecuteAfterShutdown() { ExecutorService executor = newDirectExecutorService(); executor.shutdown(); try { executor.execute(EMPTY_RUNNABLE); fail(); } catch (RejectedExecutionException expected) { } }
private static void assertExecutionException(Future<?> future, Exception expectedCause) throws Exception { try { future.get(); fail("Expected ExecutionException"); } catch (ExecutionException e) { assertThat(e).hasCauseThat().isSameAs(expectedCause); } }
/** 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); } }
/** * 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()"); } }
/** invokeAny(c) throws NPE if c has null elements */ public void testInvokeAnyImpl_nullElement() throws Exception { ListeningExecutorService e = newDirectExecutorService(); List<Callable<Integer>> l = new ArrayList<>(); l.add( new Callable<Integer>() { @Override public Integer call() { throw new ArithmeticException("/ by zero"); } }); l.add(null); try { invokeAnyImpl(e, l, false, 0, TimeUnit.NANOSECONDS); fail(); } catch (NullPointerException success) { } finally { joinPool(e); } }
/** invokeAny(null) throws NPE */ public void testInvokeAnyImpl_nullTasks() throws Exception { ListeningExecutorService e = newDirectExecutorService(); try { invokeAnyImpl(e, null, false, 0, TimeUnit.NANOSECONDS); fail(); } catch (NullPointerException success) { } finally { joinPool(e); } }
/** invokeAny(c) throws ExecutionException if no task in c completes */ public void testInvokeAnyImpl_noTaskCompletes() throws Exception { ListeningExecutorService e = newDirectExecutorService(); List<Callable<String>> l = new ArrayList<>(); l.add(new NPETask()); try { invokeAnyImpl(e, l, false, 0, TimeUnit.NANOSECONDS); fail(); } catch (ExecutionException success) { assertThat(success).hasCauseThat().isInstanceOf(NullPointerException.class); } finally { joinPool(e); } }
public void testExecuteAfterShutdown() { ExecutorService executor = newDirectExecutorService(); executor.shutdown(); try { executor.execute(EMPTY_RUNNABLE); fail(); } catch (RejectedExecutionException expected) {} }
private static void assertExecutionException(Future<?> future, Exception expectedCause) throws Exception { try { future.get(); fail("Expected ExecutionException"); } catch (ExecutionException e) { assertSame(expectedCause, e.getCause()); } }
/** * 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); } }
/** * invokeAny(c) throws NPE if c has null elements */ public void testInvokeAnyImpl_nullElement() throws Exception { ListeningExecutorService e = newDirectExecutorService(); List<Callable<Integer>> l = new ArrayList<>(); l.add(new Callable<Integer>() { @Override public Integer call() { throw new ArithmeticException("/ by zero"); } }); l.add(null); try { invokeAnyImpl(e, l, false, 0, TimeUnit.NANOSECONDS); fail(); } catch (NullPointerException success) { } 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()"); } }
/** * invokeAny(c) throws ExecutionException if no task in c completes */ public void testInvokeAnyImpl_noTaskCompletes() throws Exception { ListeningExecutorService e = newDirectExecutorService(); List<Callable<String>> l = new ArrayList<>(); l.add(new NPETask()); try { invokeAnyImpl(e, l, false, 0, TimeUnit.NANOSECONDS); fail(); } catch (ExecutionException success) { assertThat(success.getCause()).isInstanceOf(NullPointerException.class); } finally { joinPool(e); } }
/** * invokeAny(null) throws NPE */ public void testInvokeAnyImpl_nullTasks() throws Exception { ListeningExecutorService e = newDirectExecutorService(); try { invokeAnyImpl(e, null, false, 0, TimeUnit.NANOSECONDS); fail(); } catch (NullPointerException success) { } finally { joinPool(e); } }