@Override public T getUnchecked() { try { return get(); } catch (Exception e) { throw Exceptions.propagate(e); } }
@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 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)); }
assertEquals( 46, tb.get() ); assertEquals( t, em.getTasksWithTag("A").iterator().next() ); assertNull( t.getSubmittedByTask() );
@Override public T getUnchecked(Duration duration) { try { return get(duration); } catch (Exception e) { throw Exceptions.propagate(e); } }
assertEquals("a", t.get());
@Override public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return get(new Duration(timeout, unit)); }
@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")); }
/** * 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 runSimpleBasicTask() throws Exception { BasicTask<Object> t = new BasicTask<Object>(newPutCallable(1, "b")); data.put(1, "a"); Task<Object> t2 = em.submit(MutableMap.of("tag", "A"), t); assertEquals("a", t.get()); assertEquals("a", t2.get()); assertEquals("b", data.get(1)); }
@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()); }
assertTrue(t.isError()); try { t.get(); fail("get should have failed due to cancel"); } catch (CancellationException e) {