/** Test the method isDone() */ @Test public void testIsDoneMethod() throws Exception { Callable<String> task = new BasicTestTask(); IExecutorService executor = createSingleNodeExecutorService("isDoneMethod"); Future future = executor.submit(task); if (future.isDone()) { assertTrue(future.isDone()); } assertEquals(future.get(), BasicTestTask.RESULT); assertTrue(future.isDone()); }
/** Test for the issue 129. Repeatedly runs tasks and check for isDone() status after get(). */ @Test public void testIsDoneMethod2() throws Exception { ExecutorService executor = createSingleNodeExecutorService("isDoneMethod2"); for (int i = 0; i < COUNT; i++) { Callable<String> task1 = new BasicTestTask(); Callable<String> task2 = new BasicTestTask(); Future future1 = executor.submit(task1); Future future2 = executor.submit(task2); assertEquals(future2.get(), BasicTestTask.RESULT); assertTrue(future2.isDone()); assertEquals(future1.get(), BasicTestTask.RESULT); assertTrue(future1.isDone()); } }
/** Test multiple Future.get() invocation */ @Test public void testMultipleFutureGets() throws Exception { Callable<String> task = new BasicTestTask(); ExecutorService executor = createSingleNodeExecutorService("isTwoGetFromFuture"); Future<String> future = executor.submit(task); String s1 = future.get(); assertEquals(s1, BasicTestTask.RESULT); assertTrue(future.isDone()); String s2 = future.get(); assertEquals(s2, BasicTestTask.RESULT); assertTrue(future.isDone()); String s3 = future.get(); assertEquals(s3, BasicTestTask.RESULT); assertTrue(future.isDone()); String s4 = future.get(); assertEquals(s4, BasicTestTask.RESULT); assertTrue(future.isDone()); }
@Test public void testCancellationAwareTask2() { Callable task1 = new CancellationAwareTask(5000); ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask", 1); executor.submit(task1); Callable task2 = new BasicTestTask(); Future future = executor.submit(task2); assertFalse(future.isDone()); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); try { future.get(); fail("Should not complete the task successfully"); } catch (CancellationException expected) { } catch (Exception e) { fail("Unexpected exception " + e); } }
@Test public void testCancellationAwareTask() throws ExecutionException, InterruptedException { CancellationAwareTask task = new CancellationAwareTask(5000); ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask"); Future future = executor.submit(task); try { future.get(2, TimeUnit.SECONDS); fail("Should throw TimeoutException!"); } catch (TimeoutException expected) { } assertFalse(future.isDone()); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); try { future.get(); fail("Should not complete the task successfully"); } catch (CancellationException expected) { } catch (Exception e) { fail("Unexpected exception " + e); } }