/** * Adds a future to this compound future. * * @param fut Future to add. */ public final void add(IgniteInternalFuture<T> fut) { assert fut != null; synchronized (this) { if (futs == null) futs = fut; else if (futs instanceof IgniteInternalFuture) { Collection<IgniteInternalFuture> futs0 = new ArrayList<>(4); futs0.add((IgniteInternalFuture)futs); futs0.add(fut); futs = futs0; } else ((Collection<IgniteInternalFuture>)futs).add(fut); } fut.listen(this); if (isCancelled()) { try { fut.cancel(); } catch (IgniteCheckedException e) { onDone(e); } } }
/** {@inheritDoc} */ @Override public String toString() { return S.toString(GridCompoundFuture.class, this, "done", isDone(), "cancelled", isCancelled(), "err", error(), "futs", F.viewReadOnly(futures(), new C1<IgniteInternalFuture<T>, String>() { @Override public String apply(IgniteInternalFuture<T> f) { return Boolean.toString(f.isDone()); } }) ); } }
/** * @throws Exception If failed. */ @Test public void testCompleteOnReducer() throws Exception { GridCompoundFuture<Boolean, Boolean> fut = new GridCompoundFuture<>(CU.boolReducer()); List<GridFutureAdapter<Boolean>> futs = new ArrayList<>(5); for (int i = 0; i < 5; i++) { GridFutureAdapter<Boolean> part = new GridFutureAdapter<>(); fut.add(part); futs.add(part); } fut.markInitialized(); assertFalse(fut.isDone()); assertFalse(fut.isCancelled()); for (int i = 0; i < 3; i++) { futs.get(i).onDone(true); assertFalse(fut.isDone()); } futs.get(3).onDone(false); assertTrue(fut.isDone()); }
/** * @throws Exception If failed. */ @Test public void testCompleteOnException() throws Exception { GridCompoundFuture<Boolean, Boolean> fut = new GridCompoundFuture<>(CU.boolReducer()); List<GridFutureAdapter<Boolean>> futs = new ArrayList<>(5); for (int i = 0; i < 5; i++) { GridFutureAdapter<Boolean> part = new GridFutureAdapter<>(); fut.add(part); futs.add(part); } fut.markInitialized(); assertFalse(fut.isDone()); assertFalse(fut.isCancelled()); for (int i = 0; i < 3; i++) { futs.get(i).onDone(true); assertFalse(fut.isDone()); } futs.get(3).onDone(new IgniteCheckedException("Test message")); assertTrue(fut.isDone()); }
/** * @throws Exception If failed. */ @Test public void testMarkInitialized() throws Exception { GridCompoundFuture<Boolean, Boolean> fut = new GridCompoundFuture<>(); for (int i = 0; i < 5; i++) { IgniteInternalFuture<Boolean> part = new GridFinishedFuture<>(true); fut.add(part); } assertFalse(fut.isDone()); assertFalse(fut.isCancelled()); fut.markInitialized(); assertTrue(fut.isDone()); }
/** * Adds a future to this compound future. * * @param fut Future to add. */ @SuppressWarnings("unchecked") public final void add(IgniteInternalFuture<T> fut) { assert fut != null; synchronized (this) { if (futs == null) futs = fut; else if (futs instanceof IgniteInternalFuture) { Collection<IgniteInternalFuture> futs0 = new ArrayList<>(4); futs0.add((IgniteInternalFuture)futs); futs0.add(fut); futs = futs0; } else ((Collection<IgniteInternalFuture>)futs).add(fut); } fut.listen(this); if (isCancelled()) { try { fut.cancel(); } catch (IgniteCheckedException e) { onDone(e); } } }
/** {@inheritDoc} */ @Override public String toString() { return S.toString(GridCompoundFuture.class, this, "done", isDone(), "cancelled", isCancelled(), "err", error(), "futs", F.viewReadOnly(futures(), new C1<IgniteInternalFuture<T>, String>() { @Override public String apply(IgniteInternalFuture<T> f) { return Boolean.toString(f.isDone()); } }) ); } }