@Test public void testInvokeAllTimeoutCancelled() throws Exception { ExecutorService executor = createSingleNodeExecutorService("testInvokeAll"); assertFalse(executor.isShutdown()); // Only one task ArrayList<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>(); tasks.add(new CancellationAwareTask(0)); List<Future<Boolean>> futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS); assertEquals(futures.size(), 1); assertEquals(futures.get(0).get(), Boolean.TRUE); // More tasks tasks.clear(); for (int i = 0; i < COUNT; i++) { tasks.add(new CancellationAwareTask(i < 2 ? 0 : 20000)); } futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS); assertEquals(futures.size(), COUNT); for (int i = 0; i < COUNT; i++) { if (i < 2) { assertEquals(futures.get(i).get(), Boolean.TRUE); } else { boolean excepted = false; try { futures.get(i).get(); } catch (CancellationException e) { excepted = true; } assertTrue(excepted); } } }
/** Shutting down the cluster should act as the ExecutorService shutdown */ @Test(expected = RejectedExecutionException.class) public void testClusterShutdown() throws Exception { ExecutorService executor = createSingleNodeExecutorService("testClusterShutdown"); shutdownNodeFactory(); Thread.sleep(2000); assertNotNull(executor); assertTrue(executor.isShutdown()); assertTrue(executor.isTerminated()); // New tasks must be rejected Callable<String> task = new BasicTestTask(); executor.submit(task); }
/** Shutdown-related method behaviour when the cluster is running */ @Test public void testShutdownBehaviour() throws Exception { ExecutorService executor = createSingleNodeExecutorService("testShutdownBehaviour"); // Fresh instance, is not shutting down assertFalse(executor.isShutdown()); assertFalse(executor.isTerminated()); executor.shutdown(); assertTrue(executor.isShutdown()); assertTrue(executor.isTerminated()); // shutdownNow() should return an empty list and be ignored List<Runnable> pending = executor.shutdownNow(); assertTrue(pending.isEmpty()); assertTrue(executor.isShutdown()); assertTrue(executor.isTerminated()); // awaitTermination() should return immediately false try { boolean terminated = executor.awaitTermination(60L, TimeUnit.SECONDS); assertFalse(terminated); } catch (InterruptedException ie) { fail("InterruptedException"); } assertTrue(executor.isShutdown()); assertTrue(executor.isTerminated()); }
@Test public void testInvokeAllTimeoutSuccess() throws Exception { ExecutorService executor = createSingleNodeExecutorService("testInvokeAll"); assertFalse(executor.isShutdown()); // Only one task ArrayList<Callable<String>> tasks = new ArrayList<Callable<String>>(); tasks.add(new BasicTestTask()); List<Future<String>> futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS); assertEquals(futures.size(), 1); assertEquals(futures.get(0).get(), BasicTestTask.RESULT); // More tasks tasks.clear(); for (int i = 0; i < COUNT; i++) { tasks.add(new BasicTestTask()); } futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS); assertEquals(futures.size(), COUNT); for (int i = 0; i < COUNT; i++) { assertEquals(futures.get(i).get(), BasicTestTask.RESULT); } }