/** * Returns true if the task has had an error. * * Only true if calling {@link #get()} will throw an exception when it completes (including cancel). * Implementations may set this true before completion if they have that insight, or * (the default) they may compute it lazily after completion (returning false before completion). */ @Override public boolean isError() { if (!isDone()) return false; if (isCancelled()) return true; try { get(); return false; } catch (Throwable t) { return true; } }
@Test public void fieldsSetForSimpleBasicTask() throws Exception { final CountDownLatch signalStarted = new CountDownLatch(1); final CountDownLatch allowCompletion = new CountDownLatch(1); BasicTask<Integer> t = new BasicTask<Integer>(new Callable<Integer>() { @Override public Integer call() throws Exception { signalStarted.countDown(); allowCompletion.await(); return 42; }}); assertEquals(null, t.getSubmittedByTask()); assertEquals(-1, t.submitTimeUtc); assertNull(t.getInternalFuture()); em.submit(MutableMap.of("tag", "A"), t); assertTrue(signalStarted.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); assertTrue(t.submitTimeUtc > 0); assertTrue(t.startTimeUtc >= t.submitTimeUtc); assertNotNull(t.getInternalFuture()); assertEquals(-1, t.endTimeUtc); assertEquals(false, t.isCancelled()); allowCompletion.countDown(); assertEquals(t.get(), (Integer)42); assertTrue(t.endTimeUtc >= t.startTimeUtc); log.debug("BasicTask duration (millis): {}", (t.endTimeUtc - t.submitTimeUtc)); }
}}); em.submit(MutableMap.of("tag", "A"), t); assertFalse(t.isCancelled()); assertFalse(t.isDone()); assertFalse(t.isError()); t.cancel(true); assertTrue(t.isCancelled()); assertTrue(t.isError()); try { assertTrue(t.isCancelled()); assertTrue(t.isDone()); assertTrue(t.isError());
@Test public void cancelAfterRun() throws Exception { BasicTask<Integer> t = new BasicTask<Integer>(Callables.returning(42)); em.submit(MutableMap.of("tag", "A"), t); assertEquals(t.get(), (Integer)42); t.cancel(true); assertFalse(t.isCancelled()); assertFalse(t.isError()); assertTrue(t.isDone()); }
@Test public void errorDuringRun() throws Exception { BasicTask<Void> t = new BasicTask<Void>(new Callable<Void>() { @Override public Void call() throws Exception { throw new IllegalStateException("Simulating failure in errorDuringRun"); }}); em.submit(MutableMap.of("tag", "A"), t); try { t.get(); fail("get should have failed due to error"); } catch (Exception eo) { Throwable e = Throwables.getRootCause(eo); assertEquals("Simulating failure in errorDuringRun", e.getMessage()); } assertFalse(t.isCancelled()); assertTrue(t.isError()); assertTrue(t.isDone()); log.debug("errorDuringRun status: {}", t.getStatusDetail(false)); assertTrue(t.getStatusDetail(false).contains("Simulating failure in errorDuringRun"), "details="+t.getStatusDetail(false)); }
@Test public void cancelBeforeRun() throws Exception { final CountDownLatch blockForever = new CountDownLatch(1); BasicTask<Integer> t = new BasicTask<Integer>(new Callable<Integer>() { @Override public Integer call() throws Exception { blockForever.await(); return 42; }}); t.cancel(true); assertTrue(t.isCancelled()); assertTrue(t.isDone()); assertTrue(t.isError()); em.submit(MutableMap.of("tag", "A"), t); try { t.get(); fail("get should have failed due to cancel"); } catch (CancellationException e) { // expected } assertTrue(t.isCancelled()); assertTrue(t.isDone()); assertTrue(t.isError()); log.debug("cancelBeforeRun status: {}", t.getStatusDetail(false)); assertTrue(t.getStatusDetail(false).toLowerCase().contains("cancel")); }
else if (!isCancelled() && startTimeUtc <= 0) { rv = "Submitted for execution"; if (verbosity>0) { long elapsed = endTimeUtc - submitTimeUtc; String duration = Time.makeTimeStringRounded(elapsed); if (isCancelled()) { rv = "Cancelled"; if (verbosity >= 1) rv+=" after "+duration;