ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.61 by jsr166, Wed Jun 4 04:34:49 2014 UTC vs.
Revision 1.102 by jsr166, Sat Apr 25 04:55:30 2015 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import junit.framework.*;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 > import static java.util.concurrent.TimeUnit.SECONDS;
10 >
11 > import java.util.ArrayList;
12 > import java.util.List;
13 > import java.util.Objects;
14   import java.util.concurrent.Callable;
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
15   import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
16   import java.util.concurrent.CompletableFuture;
17   import java.util.concurrent.CompletionException;
18   import java.util.concurrent.CompletionStage;
19 + import java.util.concurrent.ExecutionException;
20 + import java.util.concurrent.Executor;
21   import java.util.concurrent.ForkJoinPool;
22   import java.util.concurrent.ForkJoinTask;
23   import java.util.concurrent.TimeoutException;
24   import java.util.concurrent.atomic.AtomicInteger;
24 import static java.util.concurrent.TimeUnit.MILLISECONDS;
25 import static java.util.concurrent.TimeUnit.SECONDS;
26 import java.util.*;
27 import java.util.function.Supplier;
28 import java.util.function.Consumer;
25   import java.util.function.BiConsumer;
30 import java.util.function.Function;
26   import java.util.function.BiFunction;
27 + import java.util.function.Consumer;
28 + import java.util.function.Function;
29 + import java.util.function.Supplier;
30 +
31 + import junit.framework.Test;
32 + import junit.framework.TestSuite;
33  
34   public class CompletableFutureTest extends JSR166TestCase {
35  
36      public static void main(String[] args) {
37 <        junit.textui.TestRunner.run(suite());
37 >        main(suite(), args);
38      }
39      public static Test suite() {
40          return new TestSuite(CompletableFutureTest.class);
# Line 57 | Line 58 | public class CompletableFutureTest exten
58      }
59  
60      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
61 <        try {
62 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
61 >        checkTimedGet(f, value);
62 >
63          try {
64              assertEquals(value, f.join());
65          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 76 | Line 76 | public class CompletableFutureTest exten
76      }
77  
78      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
79 +        long startTime = System.nanoTime();
80 +        long timeoutMillis = LONG_DELAY_MS;
81          try {
82 <            f.get(LONG_DELAY_MS, MILLISECONDS);
82 >            f.get(timeoutMillis, MILLISECONDS);
83              shouldThrow();
84          } catch (ExecutionException success) {
85              assertTrue(success.getCause() instanceof CFException);
86          } catch (Throwable fail) { threadUnexpectedException(fail); }
87 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
88 +
89          try {
90              f.join();
91              shouldThrow();
# Line 105 | Line 109 | public class CompletableFutureTest exten
109          assertTrue(f.toString().contains("[Completed exceptionally]"));
110      }
111  
112 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
113 <                                              CFException ex) {
112 >    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
113 >                                                      Throwable ex) {
114 >        long startTime = System.nanoTime();
115 >        long timeoutMillis = LONG_DELAY_MS;
116          try {
117 <            f.get(LONG_DELAY_MS, MILLISECONDS);
117 >            f.get(timeoutMillis, MILLISECONDS);
118              shouldThrow();
119          } catch (ExecutionException success) {
120              assertSame(ex, success.getCause());
121          } catch (Throwable fail) { threadUnexpectedException(fail); }
122 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
123 +
124          try {
125              f.join();
126              shouldThrow();
# Line 131 | Line 139 | public class CompletableFutureTest exten
139          } catch (ExecutionException success) {
140              assertSame(ex, success.getCause());
141          } catch (Throwable fail) { threadUnexpectedException(fail); }
142 +
143          assertTrue(f.isDone());
144          assertFalse(f.isCancelled());
145          assertTrue(f.toString().contains("[Completed exceptionally]"));
146      }
147  
148 +    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
149 +                                                Throwable ex) {
150 +        checkCompletedExceptionallyWithRootCause(f, ex);
151 +        try {
152 +            CompletableFuture<Throwable> spy = f.handle
153 +                ((U u, Throwable t) -> t);
154 +            assertTrue(spy.join() instanceof CompletionException);
155 +            assertSame(ex, spy.join().getCause());
156 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
157 +    }
158 +
159 +    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
160 +        checkCompletedExceptionallyWithRootCause(f, ex);
161 +        try {
162 +            CompletableFuture<Throwable> spy = f.handle
163 +                ((U u, Throwable t) -> t);
164 +            assertSame(ex, spy.join());
165 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
166 +    }
167 +
168      void checkCancelled(CompletableFuture<?> f) {
169 +        long startTime = System.nanoTime();
170 +        long timeoutMillis = LONG_DELAY_MS;
171          try {
172 <            f.get(LONG_DELAY_MS, MILLISECONDS);
172 >            f.get(timeoutMillis, MILLISECONDS);
173              shouldThrow();
174          } catch (CancellationException success) {
175          } catch (Throwable fail) { threadUnexpectedException(fail); }
176 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
177 +
178          try {
179              f.join();
180              shouldThrow();
# Line 162 | Line 195 | public class CompletableFutureTest exten
195      }
196  
197      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
198 +        long startTime = System.nanoTime();
199 +        long timeoutMillis = LONG_DELAY_MS;
200          try {
201 <            f.get(LONG_DELAY_MS, MILLISECONDS);
201 >            f.get(timeoutMillis, MILLISECONDS);
202              shouldThrow();
203          } catch (ExecutionException success) {
204              assertTrue(success.getCause() instanceof CancellationException);
205          } catch (Throwable fail) { threadUnexpectedException(fail); }
206 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
207 +
208          try {
209              f.join();
210              shouldThrow();
# Line 206 | Line 243 | public class CompletableFutureTest exten
243       * isCancelled, join, get, and getNow
244       */
245      public void testComplete() {
246 +        for (Integer v1 : new Integer[] { 1, null })
247 +    {
248          CompletableFuture<Integer> f = new CompletableFuture<>();
249          checkIncomplete(f);
250 <        f.complete(one);
251 <        checkCompletedNormally(f, one);
252 <    }
250 >        assertTrue(f.complete(v1));
251 >        assertFalse(f.complete(v1));
252 >        checkCompletedNormally(f, v1);
253 >    }}
254  
255      /**
256       * completeExceptionally completes exceptionally, as indicated by
# Line 218 | Line 258 | public class CompletableFutureTest exten
258       */
259      public void testCompleteExceptionally() {
260          CompletableFuture<Integer> f = new CompletableFuture<>();
261 +        CFException ex = new CFException();
262          checkIncomplete(f);
263 <        f.completeExceptionally(new CFException());
264 <        checkCompletedWithWrappedCFException(f);
263 >        f.completeExceptionally(ex);
264 >        checkCompletedExceptionally(f, ex);
265      }
266  
267      /**
# Line 228 | Line 269 | public class CompletableFutureTest exten
269       * methods isDone, isCancelled, join, get, and getNow
270       */
271      public void testCancel() {
272 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
273 +    {
274          CompletableFuture<Integer> f = new CompletableFuture<>();
275          checkIncomplete(f);
276 <        assertTrue(f.cancel(true));
276 >        assertTrue(f.cancel(mayInterruptIfRunning));
277 >        assertTrue(f.cancel(mayInterruptIfRunning));
278 >        assertTrue(f.cancel(!mayInterruptIfRunning));
279          checkCancelled(f);
280 <    }
280 >    }}
281  
282      /**
283       * obtrudeValue forces completion with given value
# Line 240 | Line 285 | public class CompletableFutureTest exten
285      public void testObtrudeValue() {
286          CompletableFuture<Integer> f = new CompletableFuture<>();
287          checkIncomplete(f);
288 <        f.complete(one);
288 >        assertTrue(f.complete(one));
289          checkCompletedNormally(f, one);
290          f.obtrudeValue(three);
291          checkCompletedNormally(f, three);
# Line 261 | Line 306 | public class CompletableFutureTest exten
306       * obtrudeException forces completion with given exception
307       */
308      public void testObtrudeException() {
309 <        CompletableFuture<Integer> f = new CompletableFuture<>();
310 <        checkIncomplete(f);
311 <        f.complete(one);
312 <        checkCompletedNormally(f, one);
313 <        f.obtrudeException(new CFException());
269 <        checkCompletedWithWrappedCFException(f);
309 >        for (Integer v1 : new Integer[] { 1, null })
310 >    {
311 >        CFException ex;
312 >        CompletableFuture<Integer> f;
313 >
314          f = new CompletableFuture<>();
315 <        f.obtrudeException(new CFException());
316 <        checkCompletedWithWrappedCFException(f);
315 >        assertTrue(f.complete(v1));
316 >        for (int i = 0; i < 2; i++) {
317 >            f.obtrudeException(ex = new CFException());
318 >            checkCompletedExceptionally(f, ex);
319 >        }
320 >
321          f = new CompletableFuture<>();
322 +        for (int i = 0; i < 2; i++) {
323 +            f.obtrudeException(ex = new CFException());
324 +            checkCompletedExceptionally(f, ex);
325 +        }
326 +
327 +        f = new CompletableFuture<>();
328 +        f.completeExceptionally(ex = new CFException());
329 +        f.obtrudeValue(v1);
330 +        checkCompletedNormally(f, v1);
331 +        f.obtrudeException(ex = new CFException());
332 +        checkCompletedExceptionally(f, ex);
333          f.completeExceptionally(new CFException());
334 <        f.obtrudeValue(four);
335 <        checkCompletedNormally(f, four);
336 <        f.obtrudeException(new CFException());
337 <        checkCompletedWithWrappedCFException(f);
279 <    }
334 >        checkCompletedExceptionally(f, ex);
335 >        assertFalse(f.complete(v1));
336 >        checkCompletedExceptionally(f, ex);
337 >    }}
338  
339      /**
340       * getNumberOfDependents returns number of dependent tasks
341       */
342      public void testGetNumberOfDependents() {
343 +        for (ExecutionMode m : ExecutionMode.values())
344 +        for (Integer v1 : new Integer[] { 1, null })
345 +    {
346          CompletableFuture<Integer> f = new CompletableFuture<>();
347          assertEquals(0, f.getNumberOfDependents());
348 <        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
348 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
349          assertEquals(1, f.getNumberOfDependents());
350          assertEquals(0, g.getNumberOfDependents());
351 <        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
351 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
352          assertEquals(2, f.getNumberOfDependents());
353 <        f.complete(1);
353 >        assertEquals(0, h.getNumberOfDependents());
354 >        assertTrue(f.complete(v1));
355          checkCompletedNormally(g, null);
356 +        checkCompletedNormally(h, null);
357          assertEquals(0, f.getNumberOfDependents());
358          assertEquals(0, g.getNumberOfDependents());
359 <    }
359 >        assertEquals(0, h.getNumberOfDependents());
360 >    }}
361  
362      /**
363       * toString indicates current completion state
# Line 304 | Line 368 | public class CompletableFutureTest exten
368          f = new CompletableFuture<String>();
369          assertTrue(f.toString().contains("[Not completed]"));
370  
371 <        f.complete("foo");
371 >        assertTrue(f.complete("foo"));
372          assertTrue(f.toString().contains("[Completed normally]"));
373  
374          f = new CompletableFuture<String>();
375 <        f.completeExceptionally(new IndexOutOfBoundsException());
375 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
376          assertTrue(f.toString().contains("[Completed exceptionally]"));
377 +
378 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
379 +            f = new CompletableFuture<String>();
380 +            assertTrue(f.cancel(mayInterruptIfRunning));
381 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
382 +        }
383      }
384  
385      /**
# Line 320 | Line 390 | public class CompletableFutureTest exten
390          checkCompletedNormally(f, "test");
391      }
392  
393 <    static final class IntegerSupplier implements Supplier<Integer> {
324 <        final ExecutionMode m;
393 >    abstract class CheckedAction {
394          int invocationCount = 0;
395 +        final ExecutionMode m;
396 +        CheckedAction(ExecutionMode m) { this.m = m; }
397 +        void invoked() {
398 +            m.checkExecutionMode();
399 +            assertEquals(0, invocationCount++);
400 +        }
401 +        void assertNotInvoked() { assertEquals(0, invocationCount); }
402 +        void assertInvoked() { assertEquals(1, invocationCount); }
403 +    }
404 +
405 +    abstract class CheckedIntegerAction extends CheckedAction {
406 +        Integer value;
407 +        CheckedIntegerAction(ExecutionMode m) { super(m); }
408 +        void assertValue(Integer expected) {
409 +            assertInvoked();
410 +            assertEquals(expected, value);
411 +        }
412 +    }
413 +
414 +    class IntegerSupplier extends CheckedAction
415 +        implements Supplier<Integer>
416 +    {
417          final Integer value;
418          IntegerSupplier(ExecutionMode m, Integer value) {
419 <            this.m = m;
419 >            super(m);
420              this.value = value;
421          }
422          public Integer get() {
423 <            m.checkExecutionMode();
333 <            invocationCount++;
423 >            invoked();
424              return value;
425          }
426      }
# Line 340 | Line 430 | public class CompletableFutureTest exten
430          return (x == null) ? null : x + 1;
431      }
432  
433 <    static final class IncAction implements Consumer<Integer> {
434 <        int invocationCount = 0;
435 <        Integer value;
433 >    class NoopConsumer extends CheckedIntegerAction
434 >        implements Consumer<Integer>
435 >    {
436 >        NoopConsumer(ExecutionMode m) { super(m); }
437          public void accept(Integer x) {
438 <            invocationCount++;
439 <            value = inc(x);
438 >            invoked();
439 >            value = x;
440          }
441      }
442 <    static final class IncFunction implements Function<Integer,Integer> {
443 <        final ExecutionMode m;
444 <        int invocationCount = 0;
445 <        Integer value;
446 <        IncFunction(ExecutionMode m) { this.m = m; }
442 >
443 >    class IncFunction extends CheckedIntegerAction
444 >        implements Function<Integer,Integer>
445 >    {
446 >        IncFunction(ExecutionMode m) { super(m); }
447          public Integer apply(Integer x) {
448 <            m.checkExecutionMode();
358 <            invocationCount++;
448 >            invoked();
449              return value = inc(x);
450          }
451      }
# Line 368 | Line 458 | public class CompletableFutureTest exten
458              - ((y == null) ? 99 : y.intValue());
459      }
460  
461 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
462 <        final ExecutionMode m;
463 <        int invocationCount = 0;
464 <        Integer value;
375 <        // Check this action was invoked exactly once when result is computed.
376 <        SubtractAction(ExecutionMode m) { this.m = m; }
461 >    class SubtractAction extends CheckedIntegerAction
462 >        implements BiConsumer<Integer, Integer>
463 >    {
464 >        SubtractAction(ExecutionMode m) { super(m); }
465          public void accept(Integer x, Integer y) {
466 <            m.checkExecutionMode();
379 <            invocationCount++;
466 >            invoked();
467              value = subtract(x, y);
468          }
469      }
470 <    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
471 <        final ExecutionMode m;
472 <        int invocationCount = 0;
473 <        Integer value;
474 <        // Check this action was invoked exactly once when result is computed.
388 <        SubtractFunction(ExecutionMode m) { this.m = m; }
470 >
471 >    class SubtractFunction extends CheckedIntegerAction
472 >        implements BiFunction<Integer, Integer, Integer>
473 >    {
474 >        SubtractFunction(ExecutionMode m) { super(m); }
475          public Integer apply(Integer x, Integer y) {
476 <            m.checkExecutionMode();
391 <            invocationCount++;
476 >            invoked();
477              return value = subtract(x, y);
478          }
479      }
480  
481 <    static final class Noop implements Runnable {
482 <        final ExecutionMode m;
398 <        int invocationCount = 0;
399 <        Noop(ExecutionMode m) { this.m = m; }
481 >    class Noop extends CheckedAction implements Runnable {
482 >        Noop(ExecutionMode m) { super(m); }
483          public void run() {
484 <            m.checkExecutionMode();
402 <            invocationCount++;
484 >            invoked();
485          }
486      }
487  
488 <    static final class FailingSupplier implements Supplier<Integer> {
489 <        final ExecutionMode m;
490 <        int invocationCount = 0;
491 <        FailingSupplier(ExecutionMode m) { this.m = m; }
488 >    class FailingSupplier extends CheckedAction
489 >        implements Supplier<Integer>
490 >    {
491 >        FailingSupplier(ExecutionMode m) { super(m); }
492          public Integer get() {
493 <            m.checkExecutionMode();
412 <            invocationCount++;
493 >            invoked();
494              throw new CFException();
495          }
496      }
497 <    static final class FailingConsumer implements Consumer<Integer> {
498 <        final ExecutionMode m;
499 <        int invocationCount = 0;
500 <        FailingConsumer(ExecutionMode m) { this.m = m; }
497 >
498 >    class FailingConsumer extends CheckedIntegerAction
499 >        implements Consumer<Integer>
500 >    {
501 >        FailingConsumer(ExecutionMode m) { super(m); }
502          public void accept(Integer x) {
503 <            m.checkExecutionMode();
504 <            invocationCount++;
503 >            invoked();
504 >            value = x;
505              throw new CFException();
506          }
507      }
508 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
509 <        final ExecutionMode m;
510 <        int invocationCount = 0;
511 <        FailingBiConsumer(ExecutionMode m) { this.m = m; }
508 >
509 >    class FailingBiConsumer extends CheckedIntegerAction
510 >        implements BiConsumer<Integer, Integer>
511 >    {
512 >        FailingBiConsumer(ExecutionMode m) { super(m); }
513          public void accept(Integer x, Integer y) {
514 <            m.checkExecutionMode();
515 <            invocationCount++;
514 >            invoked();
515 >            value = subtract(x, y);
516              throw new CFException();
517          }
518      }
519 <    static final class FailingFunction implements Function<Integer, Integer> {
520 <        final ExecutionMode m;
521 <        int invocationCount = 0;
522 <        FailingFunction(ExecutionMode m) { this.m = m; }
519 >
520 >    class FailingFunction extends CheckedIntegerAction
521 >        implements Function<Integer, Integer>
522 >    {
523 >        FailingFunction(ExecutionMode m) { super(m); }
524          public Integer apply(Integer x) {
525 <            m.checkExecutionMode();
526 <            invocationCount++;
525 >            invoked();
526 >            value = x;
527              throw new CFException();
528          }
529      }
530 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
531 <        final ExecutionMode m;
532 <        int invocationCount = 0;
533 <        FailingBiFunction(ExecutionMode m) { this.m = m; }
530 >
531 >    class FailingBiFunction extends CheckedIntegerAction
532 >        implements BiFunction<Integer, Integer, Integer>
533 >    {
534 >        FailingBiFunction(ExecutionMode m) { super(m); }
535          public Integer apply(Integer x, Integer y) {
536 <            m.checkExecutionMode();
537 <            invocationCount++;
536 >            invoked();
537 >            value = subtract(x, y);
538              throw new CFException();
539          }
540      }
541 <    static final class FailingRunnable implements Runnable {
542 <        final ExecutionMode m;
543 <        int invocationCount = 0;
459 <        FailingRunnable(ExecutionMode m) { this.m = m; }
541 >
542 >    class FailingRunnable extends CheckedAction implements Runnable {
543 >        FailingRunnable(ExecutionMode m) { super(m); }
544          public void run() {
545 <            m.checkExecutionMode();
462 <            invocationCount++;
545 >            invoked();
546              throw new CFException();
547          }
548      }
549  
550 <    static final class CompletableFutureInc
551 <        implements Function<Integer, CompletableFuture<Integer>> {
552 <        final ExecutionMode m;
553 <        int invocationCount = 0;
471 <        CompletableFutureInc(ExecutionMode m) { this.m = m; }
550 >    class CompletableFutureInc extends CheckedIntegerAction
551 >        implements Function<Integer, CompletableFuture<Integer>>
552 >    {
553 >        CompletableFutureInc(ExecutionMode m) { super(m); }
554          public CompletableFuture<Integer> apply(Integer x) {
555 <            m.checkExecutionMode();
556 <            invocationCount++;
555 >            invoked();
556 >            value = x;
557              CompletableFuture<Integer> f = new CompletableFuture<>();
558 <            f.complete(inc(x));
558 >            assertTrue(f.complete(inc(x)));
559              return f;
560          }
561      }
562  
563 <    static final class FailingCompletableFutureFunction
564 <        implements Function<Integer, CompletableFuture<Integer>> {
565 <        final ExecutionMode m;
566 <        int invocationCount = 0;
485 <        FailingCompletableFutureFunction(ExecutionMode m) { this.m = m; }
563 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
564 >        implements Function<Integer, CompletableFuture<Integer>>
565 >    {
566 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
567          public CompletableFuture<Integer> apply(Integer x) {
568 <            m.checkExecutionMode();
569 <            invocationCount++;
568 >            invoked();
569 >            value = x;
570              throw new CFException();
571          }
572      }
# Line 504 | Line 585 | public class CompletableFutureTest exten
585          }
586      }
587  
588 +    static final boolean defaultExecutorIsCommonPool
589 +        = ForkJoinPool.getCommonPoolParallelism() > 1;
590 +
591      /**
592       * Permits the testing of parallel code for the 3 different
593       * execution modes without copy/pasting all the test methods.
594       */
595      enum ExecutionMode {
596 <        DEFAULT {
596 >        SYNC {
597              public void checkExecutionMode() {
598                  assertFalse(ThreadExecutor.startedCurrentThread());
599                  assertNull(ForkJoinTask.getPool());
# Line 585 | Line 669 | public class CompletableFutureTest exten
669  
670          ASYNC {
671              public void checkExecutionMode() {
672 <                assertSame(ForkJoinPool.commonPool(),
673 <                           ForkJoinTask.getPool());
672 >                assertEquals(defaultExecutorIsCommonPool,
673 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
674              }
675              public CompletableFuture<Void> runAsync(Runnable a) {
676                  return CompletableFuture.runAsync(a);
# Line 782 | Line 866 | public class CompletableFutureTest exten
866      {
867          final AtomicInteger a = new AtomicInteger(0);
868          final CompletableFuture<Integer> f = new CompletableFuture<>();
869 <        if (!createIncomplete) f.complete(v1);
869 >        if (!createIncomplete) assertTrue(f.complete(v1));
870          final CompletableFuture<Integer> g = f.exceptionally
871              ((Throwable t) -> {
872                  // Should not be called
873                  a.getAndIncrement();
874                  throw new AssertionError();
875              });
876 <        if (createIncomplete) f.complete(v1);
876 >        if (createIncomplete) assertTrue(f.complete(v1));
877  
878          checkCompletedNormally(g, v1);
879          checkCompletedNormally(f, v1);
880          assertEquals(0, a.get());
881      }}
882  
799
883      /**
884       * exceptionally action completes with function value on source
885       * exception
# Line 811 | Line 894 | public class CompletableFutureTest exten
894          if (!createIncomplete) f.completeExceptionally(ex);
895          final CompletableFuture<Integer> g = f.exceptionally
896              ((Throwable t) -> {
897 <                ExecutionMode.DEFAULT.checkExecutionMode();
897 >                ExecutionMode.SYNC.checkExecutionMode();
898                  threadAssertSame(t, ex);
899                  a.getAndIncrement();
900                  return v1;
# Line 824 | Line 907 | public class CompletableFutureTest exten
907  
908      public void testExceptionally_exceptionalCompletionActionFailed() {
909          for (boolean createIncomplete : new boolean[] { true, false })
827        for (Integer v1 : new Integer[] { 1, null })
910      {
911          final AtomicInteger a = new AtomicInteger(0);
912          final CFException ex1 = new CFException();
# Line 833 | Line 915 | public class CompletableFutureTest exten
915          if (!createIncomplete) f.completeExceptionally(ex1);
916          final CompletableFuture<Integer> g = f.exceptionally
917              ((Throwable t) -> {
918 <                ExecutionMode.DEFAULT.checkExecutionMode();
918 >                ExecutionMode.SYNC.checkExecutionMode();
919 >                threadAssertSame(t, ex1);
920 >                a.getAndIncrement();
921 >                throw ex2;
922 >            });
923 >        if (createIncomplete) f.completeExceptionally(ex1);
924 >
925 >        checkCompletedWithWrappedException(g, ex2);
926 >        assertEquals(1, a.get());
927 >    }}
928 >
929 >    /**
930 >     * whenComplete action executes on normal completion, propagating
931 >     * source result.
932 >     */
933 >    public void testWhenComplete_normalCompletion1() {
934 >        for (ExecutionMode m : ExecutionMode.values())
935 >        for (boolean createIncomplete : new boolean[] { true, false })
936 >        for (Integer v1 : new Integer[] { 1, null })
937 >    {
938 >        final AtomicInteger a = new AtomicInteger(0);
939 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
940 >        if (!createIncomplete) assertTrue(f.complete(v1));
941 >        final CompletableFuture<Integer> g = m.whenComplete
942 >            (f,
943 >             (Integer x, Throwable t) -> {
944 >                m.checkExecutionMode();
945 >                threadAssertSame(x, v1);
946 >                threadAssertNull(t);
947 >                a.getAndIncrement();
948 >            });
949 >        if (createIncomplete) assertTrue(f.complete(v1));
950 >
951 >        checkCompletedNormally(g, v1);
952 >        checkCompletedNormally(f, v1);
953 >        assertEquals(1, a.get());
954 >    }}
955 >
956 >    /**
957 >     * whenComplete action executes on exceptional completion, propagating
958 >     * source result.
959 >     */
960 >    public void testWhenComplete_exceptionalCompletion() {
961 >        for (ExecutionMode m : ExecutionMode.values())
962 >        for (boolean createIncomplete : new boolean[] { true, false })
963 >    {
964 >        final AtomicInteger a = new AtomicInteger(0);
965 >        final CFException ex = new CFException();
966 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
967 >        if (!createIncomplete) f.completeExceptionally(ex);
968 >        final CompletableFuture<Integer> g = m.whenComplete
969 >            (f,
970 >             (Integer x, Throwable t) -> {
971 >                m.checkExecutionMode();
972 >                threadAssertNull(x);
973 >                threadAssertSame(t, ex);
974 >                a.getAndIncrement();
975 >            });
976 >        if (createIncomplete) f.completeExceptionally(ex);
977 >
978 >        checkCompletedWithWrappedException(g, ex);
979 >        checkCompletedExceptionally(f, ex);
980 >        assertEquals(1, a.get());
981 >    }}
982 >
983 >    /**
984 >     * whenComplete action executes on cancelled source, propagating
985 >     * CancellationException.
986 >     */
987 >    public void testWhenComplete_sourceCancelled() {
988 >        for (ExecutionMode m : ExecutionMode.values())
989 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
990 >        for (boolean createIncomplete : new boolean[] { true, false })
991 >    {
992 >        final AtomicInteger a = new AtomicInteger(0);
993 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
994 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
995 >        final CompletableFuture<Integer> g = m.whenComplete
996 >            (f,
997 >             (Integer x, Throwable t) -> {
998 >                m.checkExecutionMode();
999 >                threadAssertNull(x);
1000 >                threadAssertTrue(t instanceof CancellationException);
1001 >                a.getAndIncrement();
1002 >            });
1003 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1004 >
1005 >        checkCompletedWithWrappedCancellationException(g);
1006 >        checkCancelled(f);
1007 >        assertEquals(1, a.get());
1008 >    }}
1009 >
1010 >    /**
1011 >     * If a whenComplete action throws an exception when triggered by
1012 >     * a normal completion, it completes exceptionally
1013 >     */
1014 >    public void testWhenComplete_actionFailed() {
1015 >        for (boolean createIncomplete : new boolean[] { true, false })
1016 >        for (ExecutionMode m : ExecutionMode.values())
1017 >        for (Integer v1 : new Integer[] { 1, null })
1018 >    {
1019 >        final AtomicInteger a = new AtomicInteger(0);
1020 >        final CFException ex = new CFException();
1021 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1022 >        if (!createIncomplete) assertTrue(f.complete(v1));
1023 >        final CompletableFuture<Integer> g = m.whenComplete
1024 >            (f,
1025 >             (Integer x, Throwable t) -> {
1026 >                m.checkExecutionMode();
1027 >                threadAssertSame(x, v1);
1028 >                threadAssertNull(t);
1029 >                a.getAndIncrement();
1030 >                throw ex;
1031 >            });
1032 >        if (createIncomplete) assertTrue(f.complete(v1));
1033 >
1034 >        checkCompletedWithWrappedException(g, ex);
1035 >        checkCompletedNormally(f, v1);
1036 >        assertEquals(1, a.get());
1037 >    }}
1038 >
1039 >    /**
1040 >     * If a whenComplete action throws an exception when triggered by
1041 >     * a source completion that also throws an exception, the source
1042 >     * exception takes precedence.
1043 >     */
1044 >    public void testWhenComplete_actionFailedSourceFailed() {
1045 >        for (boolean createIncomplete : new boolean[] { true, false })
1046 >        for (ExecutionMode m : ExecutionMode.values())
1047 >    {
1048 >        final AtomicInteger a = new AtomicInteger(0);
1049 >        final CFException ex1 = new CFException();
1050 >        final CFException ex2 = new CFException();
1051 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1052 >
1053 >        if (!createIncomplete) f.completeExceptionally(ex1);
1054 >        final CompletableFuture<Integer> g = m.whenComplete
1055 >            (f,
1056 >             (Integer x, Throwable t) -> {
1057 >                m.checkExecutionMode();
1058                  threadAssertSame(t, ex1);
1059 +                threadAssertNull(x);
1060                  a.getAndIncrement();
1061                  throw ex2;
1062              });
1063          if (createIncomplete) f.completeExceptionally(ex1);
1064  
1065 <        checkCompletedWithWrappedCFException(g, ex2);
1065 >        checkCompletedWithWrappedException(g, ex1);
1066 >        checkCompletedExceptionally(f, ex1);
1067          assertEquals(1, a.get());
1068      }}
1069  
# Line 855 | Line 1078 | public class CompletableFutureTest exten
1078      {
1079          final CompletableFuture<Integer> f = new CompletableFuture<>();
1080          final AtomicInteger a = new AtomicInteger(0);
1081 <        if (!createIncomplete) f.complete(v1);
1081 >        if (!createIncomplete) assertTrue(f.complete(v1));
1082          final CompletableFuture<Integer> g = m.handle
1083              (f,
1084               (Integer x, Throwable t) -> {
# Line 865 | Line 1088 | public class CompletableFutureTest exten
1088                  a.getAndIncrement();
1089                  return inc(v1);
1090              });
1091 <        if (createIncomplete) f.complete(v1);
1091 >        if (createIncomplete) assertTrue(f.complete(v1));
1092  
1093          checkCompletedNormally(g, inc(v1));
1094          checkCompletedNormally(f, v1);
# Line 897 | Line 1120 | public class CompletableFutureTest exten
1120          if (createIncomplete) f.completeExceptionally(ex);
1121  
1122          checkCompletedNormally(g, v1);
1123 <        checkCompletedWithWrappedCFException(f, ex);
1123 >        checkCompletedExceptionally(f, ex);
1124          assertEquals(1, a.get());
1125      }}
1126  
# Line 953 | Line 1176 | public class CompletableFutureTest exten
1176              });
1177          if (createIncomplete) f.completeExceptionally(ex1);
1178  
1179 <        checkCompletedWithWrappedCFException(g, ex2);
1180 <        checkCompletedWithWrappedCFException(f, ex1);
1179 >        checkCompletedWithWrappedException(g, ex2);
1180 >        checkCompletedExceptionally(f, ex1);
1181          assertEquals(1, a.get());
1182      }}
1183  
# Line 966 | Line 1189 | public class CompletableFutureTest exten
1189          final CompletableFuture<Integer> f = new CompletableFuture<>();
1190          final AtomicInteger a = new AtomicInteger(0);
1191          final CFException ex = new CFException();
1192 <        if (!createIncomplete) f.complete(v1);
1192 >        if (!createIncomplete) assertTrue(f.complete(v1));
1193          final CompletableFuture<Integer> g = m.handle
1194              (f,
1195               (Integer x, Throwable t) -> {
# Line 976 | Line 1199 | public class CompletableFutureTest exten
1199                  a.getAndIncrement();
1200                  throw ex;
1201              });
1202 <        if (createIncomplete) f.complete(v1);
1202 >        if (createIncomplete) assertTrue(f.complete(v1));
1203  
1204 <        checkCompletedWithWrappedCFException(g, ex);
1204 >        checkCompletedWithWrappedException(g, ex);
1205          checkCompletedNormally(f, v1);
1206          assertEquals(1, a.get());
1207      }}
# Line 997 | Line 1220 | public class CompletableFutureTest exten
1220          final CompletableFuture<Void> f = m.runAsync(r);
1221          assertNull(f.join());
1222          checkCompletedNormally(f, null);
1223 <        assertEquals(1, r.invocationCount);
1223 >        r.assertInvoked();
1224      }}
1225  
1226      /**
# Line 1013 | Line 1236 | public class CompletableFutureTest exten
1236          final FailingRunnable r = new FailingRunnable(m);
1237          final CompletableFuture<Void> f = m.runAsync(r);
1238          checkCompletedWithWrappedCFException(f);
1239 <        assertEquals(1, r.invocationCount);
1239 >        r.assertInvoked();
1240      }}
1241  
1242      /**
# Line 1031 | Line 1254 | public class CompletableFutureTest exten
1254          final CompletableFuture<Integer> f = m.supplyAsync(r);
1255          assertSame(v1, f.join());
1256          checkCompletedNormally(f, v1);
1257 <        assertEquals(1, r.invocationCount);
1257 >        r.assertInvoked();
1258      }}
1259  
1260      /**
# Line 1047 | Line 1270 | public class CompletableFutureTest exten
1270          FailingSupplier r = new FailingSupplier(m);
1271          CompletableFuture<Integer> f = m.supplyAsync(r);
1272          checkCompletedWithWrappedCFException(f);
1273 <        assertEquals(1, r.invocationCount);
1273 >        r.assertInvoked();
1274      }}
1275  
1276      // seq completion methods
# Line 1057 | Line 1280 | public class CompletableFutureTest exten
1280       */
1281      public void testThenRun_normalCompletion() {
1282          for (ExecutionMode m : ExecutionMode.values())
1060        for (boolean createIncomplete : new boolean[] { true, false })
1283          for (Integer v1 : new Integer[] { 1, null })
1284      {
1285          final CompletableFuture<Integer> f = new CompletableFuture<>();
1286 <        final Noop r = new Noop(m);
1287 <        if (!createIncomplete) f.complete(v1);
1066 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1067 <        if (createIncomplete) {
1068 <            checkIncomplete(g);
1069 <            f.complete(v1);
1070 <        }
1286 >        final Noop[] rs = new Noop[6];
1287 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1288  
1289 <        checkCompletedNormally(g, null);
1289 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1290 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1291 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1292 >        checkIncomplete(h0);
1293 >        checkIncomplete(h1);
1294 >        checkIncomplete(h2);
1295 >        assertTrue(f.complete(v1));
1296 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1297 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1298 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1299 >
1300 >        checkCompletedNormally(h0, null);
1301 >        checkCompletedNormally(h1, null);
1302 >        checkCompletedNormally(h2, null);
1303 >        checkCompletedNormally(h3, null);
1304 >        checkCompletedNormally(h4, null);
1305 >        checkCompletedNormally(h5, null);
1306          checkCompletedNormally(f, v1);
1307 <        assertEquals(1, r.invocationCount);
1307 >        for (Noop r : rs) r.assertInvoked();
1308      }}
1309  
1310      /**
# Line 1080 | Line 1313 | public class CompletableFutureTest exten
1313       */
1314      public void testThenRun_exceptionalCompletion() {
1315          for (ExecutionMode m : ExecutionMode.values())
1083        for (boolean createIncomplete : new boolean[] { true, false })
1316      {
1317          final CFException ex = new CFException();
1318          final CompletableFuture<Integer> f = new CompletableFuture<>();
1319 <        final Noop r = new Noop(m);
1320 <        if (!createIncomplete) f.completeExceptionally(ex);
1089 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1090 <        if (createIncomplete) {
1091 <            checkIncomplete(g);
1092 <            f.completeExceptionally(ex);
1093 <        }
1319 >        final Noop[] rs = new Noop[6];
1320 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1321  
1322 <        checkCompletedWithWrappedCFException(g, ex);
1323 <        checkCompletedWithWrappedCFException(f, ex);
1324 <        assertEquals(0, r.invocationCount);
1322 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1323 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1324 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1325 >        checkIncomplete(h0);
1326 >        checkIncomplete(h1);
1327 >        checkIncomplete(h2);
1328 >        assertTrue(f.completeExceptionally(ex));
1329 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1330 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1331 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1332 >
1333 >        checkCompletedWithWrappedException(h0, ex);
1334 >        checkCompletedWithWrappedException(h1, ex);
1335 >        checkCompletedWithWrappedException(h2, ex);
1336 >        checkCompletedWithWrappedException(h3, ex);
1337 >        checkCompletedWithWrappedException(h4, ex);
1338 >        checkCompletedWithWrappedException(h5, ex);
1339 >        checkCompletedExceptionally(f, ex);
1340 >        for (Noop r : rs) r.assertNotInvoked();
1341      }}
1342  
1343      /**
# Line 1102 | Line 1345 | public class CompletableFutureTest exten
1345       */
1346      public void testThenRun_sourceCancelled() {
1347          for (ExecutionMode m : ExecutionMode.values())
1105        for (boolean createIncomplete : new boolean[] { true, false })
1348          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1349      {
1350          final CompletableFuture<Integer> f = new CompletableFuture<>();
1351 <        final Noop r = new Noop(m);
1352 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1111 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1112 <        if (createIncomplete) {
1113 <            checkIncomplete(g);
1114 <            assertTrue(f.cancel(mayInterruptIfRunning));
1115 <        }
1351 >        final Noop[] rs = new Noop[6];
1352 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1353  
1354 <        checkCompletedWithWrappedCancellationException(g);
1354 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1355 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1356 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1357 >        checkIncomplete(h0);
1358 >        checkIncomplete(h1);
1359 >        checkIncomplete(h2);
1360 >        assertTrue(f.cancel(mayInterruptIfRunning));
1361 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1362 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1363 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1364 >
1365 >        checkCompletedWithWrappedCancellationException(h0);
1366 >        checkCompletedWithWrappedCancellationException(h1);
1367 >        checkCompletedWithWrappedCancellationException(h2);
1368 >        checkCompletedWithWrappedCancellationException(h3);
1369 >        checkCompletedWithWrappedCancellationException(h4);
1370 >        checkCompletedWithWrappedCancellationException(h5);
1371          checkCancelled(f);
1372 <        assertEquals(0, r.invocationCount);
1372 >        for (Noop r : rs) r.assertNotInvoked();
1373      }}
1374  
1375      /**
# Line 1124 | Line 1377 | public class CompletableFutureTest exten
1377       */
1378      public void testThenRun_actionFailed() {
1379          for (ExecutionMode m : ExecutionMode.values())
1127        for (boolean createIncomplete : new boolean[] { true, false })
1380          for (Integer v1 : new Integer[] { 1, null })
1381      {
1382          final CompletableFuture<Integer> f = new CompletableFuture<>();
1383 <        final FailingRunnable r = new FailingRunnable(m);
1384 <        if (!createIncomplete) f.complete(v1);
1133 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1134 <        if (createIncomplete) {
1135 <            checkIncomplete(g);
1136 <            f.complete(v1);
1137 <        }
1383 >        final FailingRunnable[] rs = new FailingRunnable[6];
1384 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1385  
1386 <        checkCompletedWithWrappedCFException(g);
1386 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1387 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1388 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1389 >        assertTrue(f.complete(v1));
1390 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1391 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1392 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1393 >
1394 >        checkCompletedWithWrappedCFException(h0);
1395 >        checkCompletedWithWrappedCFException(h1);
1396 >        checkCompletedWithWrappedCFException(h2);
1397 >        checkCompletedWithWrappedCFException(h3);
1398 >        checkCompletedWithWrappedCFException(h4);
1399 >        checkCompletedWithWrappedCFException(h5);
1400          checkCompletedNormally(f, v1);
1401      }}
1402  
# Line 1145 | Line 1405 | public class CompletableFutureTest exten
1405       */
1406      public void testThenApply_normalCompletion() {
1407          for (ExecutionMode m : ExecutionMode.values())
1148        for (boolean createIncomplete : new boolean[] { true, false })
1408          for (Integer v1 : new Integer[] { 1, null })
1409      {
1410          final CompletableFuture<Integer> f = new CompletableFuture<>();
1411 <        final IncFunction r = new IncFunction(m);
1412 <        if (!createIncomplete) f.complete(v1);
1154 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1155 <        if (createIncomplete) {
1156 <            checkIncomplete(g);
1157 <            f.complete(v1);
1158 <        }
1411 >        final IncFunction[] rs = new IncFunction[4];
1412 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1413  
1414 <        checkCompletedNormally(g, inc(v1));
1414 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1415 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1416 >        checkIncomplete(h0);
1417 >        checkIncomplete(h1);
1418 >        assertTrue(f.complete(v1));
1419 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1420 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1421 >
1422 >        checkCompletedNormally(h0, inc(v1));
1423 >        checkCompletedNormally(h1, inc(v1));
1424 >        checkCompletedNormally(h2, inc(v1));
1425 >        checkCompletedNormally(h3, inc(v1));
1426          checkCompletedNormally(f, v1);
1427 <        assertEquals(1, r.invocationCount);
1427 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1428      }}
1429  
1430      /**
# Line 1168 | Line 1433 | public class CompletableFutureTest exten
1433       */
1434      public void testThenApply_exceptionalCompletion() {
1435          for (ExecutionMode m : ExecutionMode.values())
1171        for (boolean createIncomplete : new boolean[] { true, false })
1436      {
1437          final CFException ex = new CFException();
1438          final CompletableFuture<Integer> f = new CompletableFuture<>();
1439 <        final IncFunction r = new IncFunction(m);
1440 <        if (!createIncomplete) f.completeExceptionally(ex);
1177 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1178 <        if (createIncomplete) {
1179 <            checkIncomplete(g);
1180 <            f.completeExceptionally(ex);
1181 <        }
1439 >        final IncFunction[] rs = new IncFunction[4];
1440 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1441  
1442 <        checkCompletedWithWrappedCFException(g, ex);
1443 <        checkCompletedWithWrappedCFException(f, ex);
1444 <        assertEquals(0, r.invocationCount);
1442 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1443 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1444 >        assertTrue(f.completeExceptionally(ex));
1445 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1446 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1447 >
1448 >        checkCompletedWithWrappedException(h0, ex);
1449 >        checkCompletedWithWrappedException(h1, ex);
1450 >        checkCompletedWithWrappedException(h2, ex);
1451 >        checkCompletedWithWrappedException(h3, ex);
1452 >        checkCompletedExceptionally(f, ex);
1453 >        for (IncFunction r : rs) r.assertNotInvoked();
1454      }}
1455  
1456      /**
# Line 1190 | Line 1458 | public class CompletableFutureTest exten
1458       */
1459      public void testThenApply_sourceCancelled() {
1460          for (ExecutionMode m : ExecutionMode.values())
1193        for (boolean createIncomplete : new boolean[] { true, false })
1461          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1462      {
1463          final CompletableFuture<Integer> f = new CompletableFuture<>();
1464 <        final IncFunction r = new IncFunction(m);
1465 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1199 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1200 <        if (createIncomplete) {
1201 <            checkIncomplete(g);
1202 <            assertTrue(f.cancel(mayInterruptIfRunning));
1203 <        }
1464 >        final IncFunction[] rs = new IncFunction[4];
1465 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1466  
1467 <        checkCompletedWithWrappedCancellationException(g);
1467 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1468 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1469 >        assertTrue(f.cancel(mayInterruptIfRunning));
1470 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1471 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1472 >
1473 >        checkCompletedWithWrappedCancellationException(h0);
1474 >        checkCompletedWithWrappedCancellationException(h1);
1475 >        checkCompletedWithWrappedCancellationException(h2);
1476 >        checkCompletedWithWrappedCancellationException(h3);
1477          checkCancelled(f);
1478 <        assertEquals(0, r.invocationCount);
1478 >        for (IncFunction r : rs) r.assertNotInvoked();
1479      }}
1480  
1481      /**
# Line 1212 | Line 1483 | public class CompletableFutureTest exten
1483       */
1484      public void testThenApply_actionFailed() {
1485          for (ExecutionMode m : ExecutionMode.values())
1215        for (boolean createIncomplete : new boolean[] { true, false })
1486          for (Integer v1 : new Integer[] { 1, null })
1487      {
1488          final CompletableFuture<Integer> f = new CompletableFuture<>();
1489 <        final FailingFunction r = new FailingFunction(m);
1490 <        if (!createIncomplete) f.complete(v1);
1221 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1222 <        if (createIncomplete) {
1223 <            checkIncomplete(g);
1224 <            f.complete(v1);
1225 <        }
1489 >        final FailingFunction[] rs = new FailingFunction[4];
1490 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1491  
1492 <        checkCompletedWithWrappedCFException(g);
1492 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1493 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1494 >        assertTrue(f.complete(v1));
1495 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1496 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1497 >
1498 >        checkCompletedWithWrappedCFException(h0);
1499 >        checkCompletedWithWrappedCFException(h1);
1500 >        checkCompletedWithWrappedCFException(h2);
1501 >        checkCompletedWithWrappedCFException(h3);
1502          checkCompletedNormally(f, v1);
1503      }}
1504  
# Line 1233 | Line 1507 | public class CompletableFutureTest exten
1507       */
1508      public void testThenAccept_normalCompletion() {
1509          for (ExecutionMode m : ExecutionMode.values())
1236        for (boolean createIncomplete : new boolean[] { true, false })
1510          for (Integer v1 : new Integer[] { 1, null })
1511      {
1512          final CompletableFuture<Integer> f = new CompletableFuture<>();
1513 <        final IncAction r = new IncAction();
1514 <        if (!createIncomplete) f.complete(v1);
1242 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1243 <        if (createIncomplete) {
1244 <            checkIncomplete(g);
1245 <            f.complete(v1);
1246 <        }
1513 >        final NoopConsumer[] rs = new NoopConsumer[4];
1514 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1515  
1516 <        checkCompletedNormally(g, null);
1516 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1517 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1518 >        checkIncomplete(h0);
1519 >        checkIncomplete(h1);
1520 >        assertTrue(f.complete(v1));
1521 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1522 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1523 >
1524 >        checkCompletedNormally(h0, null);
1525 >        checkCompletedNormally(h1, null);
1526 >        checkCompletedNormally(h2, null);
1527 >        checkCompletedNormally(h3, null);
1528          checkCompletedNormally(f, v1);
1529 <        assertEquals(1, r.invocationCount);
1251 <        assertEquals(inc(v1), r.value);
1529 >        for (NoopConsumer r : rs) r.assertValue(v1);
1530      }}
1531  
1532      /**
# Line 1257 | Line 1535 | public class CompletableFutureTest exten
1535       */
1536      public void testThenAccept_exceptionalCompletion() {
1537          for (ExecutionMode m : ExecutionMode.values())
1260        for (boolean createIncomplete : new boolean[] { true, false })
1538      {
1539          final CFException ex = new CFException();
1540          final CompletableFuture<Integer> f = new CompletableFuture<>();
1541 <        final IncAction r = new IncAction();
1542 <        if (!createIncomplete) f.completeExceptionally(ex);
1266 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1267 <        if (createIncomplete) {
1268 <            checkIncomplete(g);
1269 <            f.completeExceptionally(ex);
1270 <        }
1541 >        final NoopConsumer[] rs = new NoopConsumer[4];
1542 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1543  
1544 <        checkCompletedWithWrappedCFException(g, ex);
1545 <        checkCompletedWithWrappedCFException(f, ex);
1546 <        assertEquals(0, r.invocationCount);
1544 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1545 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1546 >        assertTrue(f.completeExceptionally(ex));
1547 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1548 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1549 >
1550 >        checkCompletedWithWrappedException(h0, ex);
1551 >        checkCompletedWithWrappedException(h1, ex);
1552 >        checkCompletedWithWrappedException(h2, ex);
1553 >        checkCompletedWithWrappedException(h3, ex);
1554 >        checkCompletedExceptionally(f, ex);
1555 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1556      }}
1557  
1558      /**
# Line 1279 | Line 1560 | public class CompletableFutureTest exten
1560       */
1561      public void testThenAccept_sourceCancelled() {
1562          for (ExecutionMode m : ExecutionMode.values())
1282        for (boolean createIncomplete : new boolean[] { true, false })
1563          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1564      {
1565          final CompletableFuture<Integer> f = new CompletableFuture<>();
1566 <        final IncAction r = new IncAction();
1567 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1288 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1289 <        if (createIncomplete) {
1290 <            checkIncomplete(g);
1291 <            assertTrue(f.cancel(mayInterruptIfRunning));
1292 <        }
1566 >        final NoopConsumer[] rs = new NoopConsumer[4];
1567 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1568  
1569 <        checkCompletedWithWrappedCancellationException(g);
1569 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1570 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1571 >        assertTrue(f.cancel(mayInterruptIfRunning));
1572 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1573 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1574 >
1575 >        checkCompletedWithWrappedCancellationException(h0);
1576 >        checkCompletedWithWrappedCancellationException(h1);
1577 >        checkCompletedWithWrappedCancellationException(h2);
1578 >        checkCompletedWithWrappedCancellationException(h3);
1579          checkCancelled(f);
1580 <        assertEquals(0, r.invocationCount);
1580 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1581      }}
1582  
1583      /**
# Line 1301 | Line 1585 | public class CompletableFutureTest exten
1585       */
1586      public void testThenAccept_actionFailed() {
1587          for (ExecutionMode m : ExecutionMode.values())
1304        for (boolean createIncomplete : new boolean[] { true, false })
1588          for (Integer v1 : new Integer[] { 1, null })
1589      {
1590          final CompletableFuture<Integer> f = new CompletableFuture<>();
1591 <        final FailingConsumer r = new FailingConsumer(m);
1592 <        if (!createIncomplete) f.complete(v1);
1310 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1311 <        if (createIncomplete) {
1312 <            checkIncomplete(g);
1313 <            f.complete(v1);
1314 <        }
1591 >        final FailingConsumer[] rs = new FailingConsumer[4];
1592 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1593  
1594 <        checkCompletedWithWrappedCFException(g);
1594 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1595 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1596 >        assertTrue(f.complete(v1));
1597 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1598 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1599 >
1600 >        checkCompletedWithWrappedCFException(h0);
1601 >        checkCompletedWithWrappedCFException(h1);
1602 >        checkCompletedWithWrappedCFException(h2);
1603 >        checkCompletedWithWrappedCFException(h3);
1604          checkCompletedNormally(f, v1);
1605      }}
1606  
# Line 1323 | Line 1610 | public class CompletableFutureTest exten
1610       */
1611      public void testThenCombine_normalCompletion() {
1612          for (ExecutionMode m : ExecutionMode.values())
1326        for (boolean createIncomplete : new boolean[] { true, false })
1613          for (boolean fFirst : new boolean[] { true, false })
1614          for (Integer v1 : new Integer[] { 1, null })
1615          for (Integer v2 : new Integer[] { 2, null })
1616      {
1617          final CompletableFuture<Integer> f = new CompletableFuture<>();
1618          final CompletableFuture<Integer> g = new CompletableFuture<>();
1619 <        final SubtractFunction r = new SubtractFunction(m);
1619 >        final SubtractFunction[] rs = new SubtractFunction[6];
1620 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1621  
1622 <        if (fFirst) f.complete(v1); else g.complete(v2);
1623 <        if (!createIncomplete)
1624 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1625 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1626 <        if (createIncomplete) {
1627 <            checkIncomplete(h);
1628 <            assertEquals(0, r.invocationCount);
1629 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1630 <        }
1622 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1623 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1624 >        final Integer w1 =  fFirst ? v1 : v2;
1625 >        final Integer w2 = !fFirst ? v1 : v2;
1626 >
1627 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1628 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1629 >        assertTrue(fst.complete(w1));
1630 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1631 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1632 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1633 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1634 >        checkCompletedNormally(h1, subtract(w1, w1));
1635 >        checkCompletedNormally(h3, subtract(w1, w1));
1636 >        rs[1].assertValue(subtract(w1, w1));
1637 >        rs[3].assertValue(subtract(w1, w1));
1638 >        assertTrue(snd.complete(w2));
1639 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1640 >
1641 >        checkCompletedNormally(h0, subtract(v1, v2));
1642 >        checkCompletedNormally(h2, subtract(v1, v2));
1643 >        checkCompletedNormally(h4, subtract(v1, v2));
1644 >        rs[0].assertValue(subtract(v1, v2));
1645 >        rs[2].assertValue(subtract(v1, v2));
1646 >        rs[4].assertValue(subtract(v1, v2));
1647  
1345        checkCompletedNormally(h, subtract(v1, v2));
1648          checkCompletedNormally(f, v1);
1649          checkCompletedNormally(g, v2);
1348        assertEquals(1, r.invocationCount);
1650      }}
1651  
1652      /**
1653       * thenCombine result completes exceptionally after exceptional
1654       * completion of either source
1655       */
1656 <    public void testThenCombine_exceptionalCompletion() {
1656 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1657          for (ExecutionMode m : ExecutionMode.values())
1357        for (boolean createIncomplete : new boolean[] { true, false })
1658          for (boolean fFirst : new boolean[] { true, false })
1659 +        for (boolean failFirst : new boolean[] { true, false })
1660          for (Integer v1 : new Integer[] { 1, null })
1661      {
1662          final CompletableFuture<Integer> f = new CompletableFuture<>();
1663          final CompletableFuture<Integer> g = new CompletableFuture<>();
1664          final CFException ex = new CFException();
1665 <        final SubtractFunction r = new SubtractFunction(m);
1666 <
1667 <        (fFirst ? f : g).complete(v1);
1668 <        if (!createIncomplete)
1669 <            (!fFirst ? f : g).completeExceptionally(ex);
1670 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1671 <        if (createIncomplete) {
1672 <            checkIncomplete(h);
1673 <            (!fFirst ? f : g).completeExceptionally(ex);
1674 <        }
1675 <
1676 <        checkCompletedWithWrappedCFException(h, ex);
1677 <        assertEquals(0, r.invocationCount);
1678 <        checkCompletedNormally(fFirst ? f : g, v1);
1679 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1665 >        final SubtractFunction r1 = new SubtractFunction(m);
1666 >        final SubtractFunction r2 = new SubtractFunction(m);
1667 >        final SubtractFunction r3 = new SubtractFunction(m);
1668 >
1669 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1670 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1671 >        final Callable<Boolean> complete1 = failFirst ?
1672 >            () -> fst.completeExceptionally(ex) :
1673 >            () -> fst.complete(v1);
1674 >        final Callable<Boolean> complete2 = failFirst ?
1675 >            () -> snd.complete(v1) :
1676 >            () -> snd.completeExceptionally(ex);
1677 >
1678 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1679 >        assertTrue(complete1.call());
1680 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1681 >        checkIncomplete(h1);
1682 >        checkIncomplete(h2);
1683 >        assertTrue(complete2.call());
1684 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1685 >
1686 >        checkCompletedWithWrappedException(h1, ex);
1687 >        checkCompletedWithWrappedException(h2, ex);
1688 >        checkCompletedWithWrappedException(h3, ex);
1689 >        r1.assertNotInvoked();
1690 >        r2.assertNotInvoked();
1691 >        r3.assertNotInvoked();
1692 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1693 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1694      }}
1695  
1696      /**
1697       * thenCombine result completes exceptionally if either source cancelled
1698       */
1699 <    public void testThenCombine_sourceCancelled() {
1699 >    public void testThenCombine_sourceCancelled() throws Throwable {
1700          for (ExecutionMode m : ExecutionMode.values())
1701          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1387        for (boolean createIncomplete : new boolean[] { true, false })
1702          for (boolean fFirst : new boolean[] { true, false })
1703 +        for (boolean failFirst : new boolean[] { true, false })
1704          for (Integer v1 : new Integer[] { 1, null })
1705      {
1706          final CompletableFuture<Integer> f = new CompletableFuture<>();
1707          final CompletableFuture<Integer> g = new CompletableFuture<>();
1708 <        final SubtractFunction r = new SubtractFunction(m);
1709 <
1710 <        (fFirst ? f : g).complete(v1);
1711 <        if (!createIncomplete)
1712 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1713 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1714 <        if (createIncomplete) {
1715 <            checkIncomplete(h);
1716 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1717 <        }
1718 <
1719 <        checkCompletedWithWrappedCancellationException(h);
1720 <        checkCancelled(!fFirst ? f : g);
1721 <        assertEquals(0, r.invocationCount);
1722 <        checkCompletedNormally(fFirst ? f : g, v1);
1708 >        final SubtractFunction r1 = new SubtractFunction(m);
1709 >        final SubtractFunction r2 = new SubtractFunction(m);
1710 >        final SubtractFunction r3 = new SubtractFunction(m);
1711 >
1712 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1713 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1714 >        final Callable<Boolean> complete1 = failFirst ?
1715 >            () -> fst.cancel(mayInterruptIfRunning) :
1716 >            () -> fst.complete(v1);
1717 >        final Callable<Boolean> complete2 = failFirst ?
1718 >            () -> snd.complete(v1) :
1719 >            () -> snd.cancel(mayInterruptIfRunning);
1720 >
1721 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1722 >        assertTrue(complete1.call());
1723 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1724 >        checkIncomplete(h1);
1725 >        checkIncomplete(h2);
1726 >        assertTrue(complete2.call());
1727 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1728 >
1729 >        checkCompletedWithWrappedCancellationException(h1);
1730 >        checkCompletedWithWrappedCancellationException(h2);
1731 >        checkCompletedWithWrappedCancellationException(h3);
1732 >        r1.assertNotInvoked();
1733 >        r2.assertNotInvoked();
1734 >        r3.assertNotInvoked();
1735 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1736 >        checkCancelled(failFirst ? fst : snd);
1737      }}
1738  
1739      /**
# Line 1418 | Line 1747 | public class CompletableFutureTest exten
1747      {
1748          final CompletableFuture<Integer> f = new CompletableFuture<>();
1749          final CompletableFuture<Integer> g = new CompletableFuture<>();
1750 <        final FailingBiFunction r = new FailingBiFunction(m);
1751 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1750 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1751 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1752 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1753 >
1754 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1755 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1756 >        final Integer w1 =  fFirst ? v1 : v2;
1757 >        final Integer w2 = !fFirst ? v1 : v2;
1758 >
1759 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1760 >        assertTrue(fst.complete(w1));
1761 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1762 >        assertTrue(snd.complete(w2));
1763 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1764  
1765 <        if (fFirst) {
1766 <            f.complete(v1);
1767 <            g.complete(v2);
1768 <        } else {
1769 <            g.complete(v2);
1770 <            f.complete(v1);
1430 <        }
1431 <
1432 <        checkCompletedWithWrappedCFException(h);
1765 >        checkCompletedWithWrappedCFException(h1);
1766 >        checkCompletedWithWrappedCFException(h2);
1767 >        checkCompletedWithWrappedCFException(h3);
1768 >        r1.assertInvoked();
1769 >        r2.assertInvoked();
1770 >        r3.assertInvoked();
1771          checkCompletedNormally(f, v1);
1772          checkCompletedNormally(g, v2);
1773      }}
# Line 1440 | Line 1778 | public class CompletableFutureTest exten
1778       */
1779      public void testThenAcceptBoth_normalCompletion() {
1780          for (ExecutionMode m : ExecutionMode.values())
1443        for (boolean createIncomplete : new boolean[] { true, false })
1781          for (boolean fFirst : new boolean[] { true, false })
1782          for (Integer v1 : new Integer[] { 1, null })
1783          for (Integer v2 : new Integer[] { 2, null })
1784      {
1785          final CompletableFuture<Integer> f = new CompletableFuture<>();
1786          final CompletableFuture<Integer> g = new CompletableFuture<>();
1787 <        final SubtractAction r = new SubtractAction(m);
1788 <
1789 <        if (fFirst) f.complete(v1); else g.complete(v2);
1790 <        if (!createIncomplete)
1791 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1792 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1793 <        if (createIncomplete) {
1794 <            checkIncomplete(h);
1795 <            assertEquals(0, r.invocationCount);
1796 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1797 <        }
1798 <
1799 <        checkCompletedNormally(h, null);
1800 <        assertEquals(subtract(v1, v2), r.value);
1787 >        final SubtractAction r1 = new SubtractAction(m);
1788 >        final SubtractAction r2 = new SubtractAction(m);
1789 >        final SubtractAction r3 = new SubtractAction(m);
1790 >
1791 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1792 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1793 >        final Integer w1 =  fFirst ? v1 : v2;
1794 >        final Integer w2 = !fFirst ? v1 : v2;
1795 >
1796 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1797 >        assertTrue(fst.complete(w1));
1798 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1799 >        checkIncomplete(h1);
1800 >        checkIncomplete(h2);
1801 >        r1.assertNotInvoked();
1802 >        r2.assertNotInvoked();
1803 >        assertTrue(snd.complete(w2));
1804 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1805 >
1806 >        checkCompletedNormally(h1, null);
1807 >        checkCompletedNormally(h2, null);
1808 >        checkCompletedNormally(h3, null);
1809 >        r1.assertValue(subtract(v1, v2));
1810 >        r2.assertValue(subtract(v1, v2));
1811 >        r3.assertValue(subtract(v1, v2));
1812          checkCompletedNormally(f, v1);
1813          checkCompletedNormally(g, v2);
1814      }}
# Line 1469 | Line 1817 | public class CompletableFutureTest exten
1817       * thenAcceptBoth result completes exceptionally after exceptional
1818       * completion of either source
1819       */
1820 <    public void testThenAcceptBoth_exceptionalCompletion() {
1820 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1821          for (ExecutionMode m : ExecutionMode.values())
1474        for (boolean createIncomplete : new boolean[] { true, false })
1822          for (boolean fFirst : new boolean[] { true, false })
1823 +        for (boolean failFirst : new boolean[] { true, false })
1824          for (Integer v1 : new Integer[] { 1, null })
1825      {
1826          final CompletableFuture<Integer> f = new CompletableFuture<>();
1827          final CompletableFuture<Integer> g = new CompletableFuture<>();
1828          final CFException ex = new CFException();
1829 <        final SubtractAction r = new SubtractAction(m);
1830 <
1831 <        (fFirst ? f : g).complete(v1);
1832 <        if (!createIncomplete)
1833 <            (!fFirst ? f : g).completeExceptionally(ex);
1834 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1835 <        if (createIncomplete) {
1836 <            checkIncomplete(h);
1837 <            (!fFirst ? f : g).completeExceptionally(ex);
1838 <        }
1839 <
1840 <        checkCompletedWithWrappedCFException(h, ex);
1841 <        assertEquals(0, r.invocationCount);
1842 <        checkCompletedNormally(fFirst ? f : g, v1);
1843 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1829 >        final SubtractAction r1 = new SubtractAction(m);
1830 >        final SubtractAction r2 = new SubtractAction(m);
1831 >        final SubtractAction r3 = new SubtractAction(m);
1832 >
1833 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1834 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1835 >        final Callable<Boolean> complete1 = failFirst ?
1836 >            () -> fst.completeExceptionally(ex) :
1837 >            () -> fst.complete(v1);
1838 >        final Callable<Boolean> complete2 = failFirst ?
1839 >            () -> snd.complete(v1) :
1840 >            () -> snd.completeExceptionally(ex);
1841 >
1842 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1843 >        assertTrue(complete1.call());
1844 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1845 >        checkIncomplete(h1);
1846 >        checkIncomplete(h2);
1847 >        assertTrue(complete2.call());
1848 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1849 >
1850 >        checkCompletedWithWrappedException(h1, ex);
1851 >        checkCompletedWithWrappedException(h2, ex);
1852 >        checkCompletedWithWrappedException(h3, ex);
1853 >        r1.assertNotInvoked();
1854 >        r2.assertNotInvoked();
1855 >        r3.assertNotInvoked();
1856 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1857 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1858      }}
1859  
1860      /**
1861       * thenAcceptBoth result completes exceptionally if either source cancelled
1862       */
1863 <    public void testThenAcceptBoth_sourceCancelled() {
1863 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1864          for (ExecutionMode m : ExecutionMode.values())
1865          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1504        for (boolean createIncomplete : new boolean[] { true, false })
1866          for (boolean fFirst : new boolean[] { true, false })
1867 +        for (boolean failFirst : new boolean[] { true, false })
1868          for (Integer v1 : new Integer[] { 1, null })
1869      {
1870          final CompletableFuture<Integer> f = new CompletableFuture<>();
1871          final CompletableFuture<Integer> g = new CompletableFuture<>();
1872 <        final SubtractAction r = new SubtractAction(m);
1873 <
1874 <        (fFirst ? f : g).complete(v1);
1875 <        if (!createIncomplete)
1876 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1877 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1878 <        if (createIncomplete) {
1879 <            checkIncomplete(h);
1880 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1881 <        }
1882 <
1883 <        checkCompletedWithWrappedCancellationException(h);
1884 <        checkCancelled(!fFirst ? f : g);
1885 <        assertEquals(0, r.invocationCount);
1886 <        checkCompletedNormally(fFirst ? f : g, v1);
1872 >        final SubtractAction r1 = new SubtractAction(m);
1873 >        final SubtractAction r2 = new SubtractAction(m);
1874 >        final SubtractAction r3 = new SubtractAction(m);
1875 >
1876 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1877 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1878 >        final Callable<Boolean> complete1 = failFirst ?
1879 >            () -> fst.cancel(mayInterruptIfRunning) :
1880 >            () -> fst.complete(v1);
1881 >        final Callable<Boolean> complete2 = failFirst ?
1882 >            () -> snd.complete(v1) :
1883 >            () -> snd.cancel(mayInterruptIfRunning);
1884 >
1885 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1886 >        assertTrue(complete1.call());
1887 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1888 >        checkIncomplete(h1);
1889 >        checkIncomplete(h2);
1890 >        assertTrue(complete2.call());
1891 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1892 >
1893 >        checkCompletedWithWrappedCancellationException(h1);
1894 >        checkCompletedWithWrappedCancellationException(h2);
1895 >        checkCompletedWithWrappedCancellationException(h3);
1896 >        r1.assertNotInvoked();
1897 >        r2.assertNotInvoked();
1898 >        r3.assertNotInvoked();
1899 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1900 >        checkCancelled(failFirst ? fst : snd);
1901      }}
1902  
1903      /**
# Line 1535 | Line 1911 | public class CompletableFutureTest exten
1911      {
1912          final CompletableFuture<Integer> f = new CompletableFuture<>();
1913          final CompletableFuture<Integer> g = new CompletableFuture<>();
1914 <        final FailingBiConsumer r = new FailingBiConsumer(m);
1915 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1916 <
1917 <        if (fFirst) {
1918 <            f.complete(v1);
1919 <            g.complete(v2);
1920 <        } else {
1921 <            g.complete(v2);
1922 <            f.complete(v1);
1923 <        }
1914 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1915 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1916 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1917 >
1918 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1919 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1920 >        final Integer w1 =  fFirst ? v1 : v2;
1921 >        final Integer w2 = !fFirst ? v1 : v2;
1922 >
1923 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1924 >        assertTrue(fst.complete(w1));
1925 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1926 >        assertTrue(snd.complete(w2));
1927 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1928  
1929 <        checkCompletedWithWrappedCFException(h);
1929 >        checkCompletedWithWrappedCFException(h1);
1930 >        checkCompletedWithWrappedCFException(h2);
1931 >        checkCompletedWithWrappedCFException(h3);
1932 >        r1.assertInvoked();
1933 >        r2.assertInvoked();
1934 >        r3.assertInvoked();
1935          checkCompletedNormally(f, v1);
1936          checkCompletedNormally(g, v2);
1937      }}
# Line 1557 | Line 1942 | public class CompletableFutureTest exten
1942       */
1943      public void testRunAfterBoth_normalCompletion() {
1944          for (ExecutionMode m : ExecutionMode.values())
1560        for (boolean createIncomplete : new boolean[] { true, false })
1945          for (boolean fFirst : new boolean[] { true, false })
1946          for (Integer v1 : new Integer[] { 1, null })
1947          for (Integer v2 : new Integer[] { 2, null })
1948      {
1949          final CompletableFuture<Integer> f = new CompletableFuture<>();
1950          final CompletableFuture<Integer> g = new CompletableFuture<>();
1951 <        final Noop r = new Noop(m);
1952 <
1953 <        if (fFirst) f.complete(v1); else g.complete(v2);
1954 <        if (!createIncomplete)
1955 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1956 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1957 <        if (createIncomplete) {
1958 <            checkIncomplete(h);
1959 <            assertEquals(0, r.invocationCount);
1960 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1961 <        }
1962 <
1963 <        checkCompletedNormally(h, null);
1964 <        assertEquals(1, r.invocationCount);
1951 >        final Noop r1 = new Noop(m);
1952 >        final Noop r2 = new Noop(m);
1953 >        final Noop r3 = new Noop(m);
1954 >
1955 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1956 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1957 >        final Integer w1 =  fFirst ? v1 : v2;
1958 >        final Integer w2 = !fFirst ? v1 : v2;
1959 >
1960 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1961 >        assertTrue(fst.complete(w1));
1962 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1963 >        checkIncomplete(h1);
1964 >        checkIncomplete(h2);
1965 >        r1.assertNotInvoked();
1966 >        r2.assertNotInvoked();
1967 >        assertTrue(snd.complete(w2));
1968 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1969 >
1970 >        checkCompletedNormally(h1, null);
1971 >        checkCompletedNormally(h2, null);
1972 >        checkCompletedNormally(h3, null);
1973 >        r1.assertInvoked();
1974 >        r2.assertInvoked();
1975 >        r3.assertInvoked();
1976          checkCompletedNormally(f, v1);
1977          checkCompletedNormally(g, v2);
1978      }}
# Line 1586 | Line 1981 | public class CompletableFutureTest exten
1981       * runAfterBoth result completes exceptionally after exceptional
1982       * completion of either source
1983       */
1984 <    public void testRunAfterBoth_exceptionalCompletion() {
1984 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1985          for (ExecutionMode m : ExecutionMode.values())
1591        for (boolean createIncomplete : new boolean[] { true, false })
1986          for (boolean fFirst : new boolean[] { true, false })
1987 +        for (boolean failFirst : new boolean[] { true, false })
1988          for (Integer v1 : new Integer[] { 1, null })
1989      {
1990          final CompletableFuture<Integer> f = new CompletableFuture<>();
1991          final CompletableFuture<Integer> g = new CompletableFuture<>();
1992          final CFException ex = new CFException();
1993 <        final Noop r = new Noop(m);
1994 <
1995 <        (fFirst ? f : g).complete(v1);
1996 <        if (!createIncomplete)
1997 <            (!fFirst ? f : g).completeExceptionally(ex);
1998 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1999 <        if (createIncomplete) {
2000 <            checkIncomplete(h);
2001 <            (!fFirst ? f : g).completeExceptionally(ex);
2002 <        }
2003 <
2004 <        checkCompletedWithWrappedCFException(h, ex);
2005 <        assertEquals(0, r.invocationCount);
2006 <        checkCompletedNormally(fFirst ? f : g, v1);
2007 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1993 >        final Noop r1 = new Noop(m);
1994 >        final Noop r2 = new Noop(m);
1995 >        final Noop r3 = new Noop(m);
1996 >
1997 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1998 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1999 >        final Callable<Boolean> complete1 = failFirst ?
2000 >            () -> fst.completeExceptionally(ex) :
2001 >            () -> fst.complete(v1);
2002 >        final Callable<Boolean> complete2 = failFirst ?
2003 >            () -> snd.complete(v1) :
2004 >            () -> snd.completeExceptionally(ex);
2005 >
2006 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2007 >        assertTrue(complete1.call());
2008 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2009 >        checkIncomplete(h1);
2010 >        checkIncomplete(h2);
2011 >        assertTrue(complete2.call());
2012 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2013 >
2014 >        checkCompletedWithWrappedException(h1, ex);
2015 >        checkCompletedWithWrappedException(h2, ex);
2016 >        checkCompletedWithWrappedException(h3, ex);
2017 >        r1.assertNotInvoked();
2018 >        r2.assertNotInvoked();
2019 >        r3.assertNotInvoked();
2020 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2021 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2022      }}
2023  
2024      /**
2025       * runAfterBoth result completes exceptionally if either source cancelled
2026       */
2027 <    public void testRunAfterBoth_sourceCancelled() {
2027 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2028          for (ExecutionMode m : ExecutionMode.values())
2029          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1621        for (boolean createIncomplete : new boolean[] { true, false })
2030          for (boolean fFirst : new boolean[] { true, false })
2031 +        for (boolean failFirst : new boolean[] { true, false })
2032          for (Integer v1 : new Integer[] { 1, null })
2033      {
2034          final CompletableFuture<Integer> f = new CompletableFuture<>();
2035          final CompletableFuture<Integer> g = new CompletableFuture<>();
2036 <        final Noop r = new Noop(m);
2037 <
2038 <
2039 <        (fFirst ? f : g).complete(v1);
2040 <        if (!createIncomplete)
2041 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2042 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2043 <        if (createIncomplete) {
2044 <            checkIncomplete(h);
2045 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2046 <        }
2047 <
2048 <        checkCompletedWithWrappedCancellationException(h);
2049 <        checkCancelled(!fFirst ? f : g);
2050 <        assertEquals(0, r.invocationCount);
2051 <        checkCompletedNormally(fFirst ? f : g, v1);
2036 >        final Noop r1 = new Noop(m);
2037 >        final Noop r2 = new Noop(m);
2038 >        final Noop r3 = new Noop(m);
2039 >
2040 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2041 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2042 >        final Callable<Boolean> complete1 = failFirst ?
2043 >            () -> fst.cancel(mayInterruptIfRunning) :
2044 >            () -> fst.complete(v1);
2045 >        final Callable<Boolean> complete2 = failFirst ?
2046 >            () -> snd.complete(v1) :
2047 >            () -> snd.cancel(mayInterruptIfRunning);
2048 >
2049 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2050 >        assertTrue(complete1.call());
2051 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2052 >        checkIncomplete(h1);
2053 >        checkIncomplete(h2);
2054 >        assertTrue(complete2.call());
2055 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2056 >
2057 >        checkCompletedWithWrappedCancellationException(h1);
2058 >        checkCompletedWithWrappedCancellationException(h2);
2059 >        checkCompletedWithWrappedCancellationException(h3);
2060 >        r1.assertNotInvoked();
2061 >        r2.assertNotInvoked();
2062 >        r3.assertNotInvoked();
2063 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2064 >        checkCancelled(failFirst ? fst : snd);
2065      }}
2066  
2067      /**
# Line 1653 | Line 2075 | public class CompletableFutureTest exten
2075      {
2076          final CompletableFuture<Integer> f = new CompletableFuture<>();
2077          final CompletableFuture<Integer> g = new CompletableFuture<>();
2078 <        final FailingRunnable r = new FailingRunnable(m);
2079 <
2080 <        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r);
2081 <        if (fFirst) {
2082 <            f.complete(v1);
2083 <            g.complete(v2);
2084 <        } else {
2085 <            g.complete(v2);
2086 <            f.complete(v1);
2087 <        }
2088 <        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r);
2078 >        final FailingRunnable r1 = new FailingRunnable(m);
2079 >        final FailingRunnable r2 = new FailingRunnable(m);
2080 >        final FailingRunnable r3 = new FailingRunnable(m);
2081 >
2082 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2083 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2084 >        final Integer w1 =  fFirst ? v1 : v2;
2085 >        final Integer w2 = !fFirst ? v1 : v2;
2086 >
2087 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2088 >        assertTrue(fst.complete(w1));
2089 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2090 >        assertTrue(snd.complete(w2));
2091 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2092  
2093          checkCompletedWithWrappedCFException(h1);
2094          checkCompletedWithWrappedCFException(h2);
2095 +        checkCompletedWithWrappedCFException(h3);
2096 +        r1.assertInvoked();
2097 +        r2.assertInvoked();
2098 +        r3.assertInvoked();
2099          checkCompletedNormally(f, v1);
2100          checkCompletedNormally(g, v2);
2101      }}
# Line 1677 | Line 2106 | public class CompletableFutureTest exten
2106       */
2107      public void testApplyToEither_normalCompletion() {
2108          for (ExecutionMode m : ExecutionMode.values())
1680        for (boolean createIncomplete : new boolean[] { true, false })
1681        for (boolean fFirst : new boolean[] { true, false })
1682        for (Integer v1 : new Integer[] { 1, null })
1683        for (Integer v2 : new Integer[] { 2, null })
1684    {
1685        final CompletableFuture<Integer> f = new CompletableFuture<>();
1686        final CompletableFuture<Integer> g = new CompletableFuture<>();
1687        final IncFunction r = new IncFunction(m);
1688
1689        if (!createIncomplete)
1690            if (fFirst) f.complete(v1); else g.complete(v2);
1691        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1692        if (createIncomplete) {
1693            checkIncomplete(h);
1694            assertEquals(0, r.invocationCount);
1695            if (fFirst) f.complete(v1); else g.complete(v2);
1696        }
1697        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1698        if (!fFirst) f.complete(v1); else g.complete(v2);
1699
1700        checkCompletedNormally(f, v1);
1701        checkCompletedNormally(g, v2);
1702        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1703    }}
1704
1705    public void testApplyToEither_normalCompletionBothAvailable() {
1706        for (ExecutionMode m : ExecutionMode.values())
1707        for (boolean fFirst : new boolean[] { true, false })
2109          for (Integer v1 : new Integer[] { 1, null })
2110          for (Integer v2 : new Integer[] { 2, null })
2111      {
2112          final CompletableFuture<Integer> f = new CompletableFuture<>();
2113          final CompletableFuture<Integer> g = new CompletableFuture<>();
2114 <        final IncFunction r = new IncFunction(m);
2114 >        final IncFunction[] rs = new IncFunction[6];
2115 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2116  
2117 <        if (fFirst) {
2118 <            f.complete(v1);
2119 <            g.complete(v2);
2120 <        } else {
2121 <            g.complete(v2);
2122 <            f.complete(v1);
2123 <        }
2117 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2118 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2119 >        checkIncomplete(h0);
2120 >        checkIncomplete(h1);
2121 >        rs[0].assertNotInvoked();
2122 >        rs[1].assertNotInvoked();
2123 >        f.complete(v1);
2124 >        checkCompletedNormally(h0, inc(v1));
2125 >        checkCompletedNormally(h1, inc(v1));
2126 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2127 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2128 >        checkCompletedNormally(h2, inc(v1));
2129 >        checkCompletedNormally(h3, inc(v1));
2130 >        g.complete(v2);
2131  
2132 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2132 >        // unspecified behavior - both source completions available
2133 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2134 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2135 >        rs[4].assertValue(h4.join());
2136 >        rs[5].assertValue(h5.join());
2137 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
2138 >                   Objects.equals(inc(v2), h4.join()));
2139 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
2140 >                   Objects.equals(inc(v2), h5.join()));
2141  
2142          checkCompletedNormally(f, v1);
2143          checkCompletedNormally(g, v2);
2144 <
2145 <        // unspecified behavior
2146 <        assertTrue(Objects.equals(h.join(), inc(v1)) ||
2147 <                   Objects.equals(h.join(), inc(v2)));
2148 <        assertEquals(1, r.invocationCount);
2144 >        checkCompletedNormally(h0, inc(v1));
2145 >        checkCompletedNormally(h1, inc(v1));
2146 >        checkCompletedNormally(h2, inc(v1));
2147 >        checkCompletedNormally(h3, inc(v1));
2148 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
2149      }}
2150  
2151      /**
2152       * applyToEither result completes exceptionally after exceptional
2153       * completion of either source
2154       */
2155 <    public void testApplyToEither_exceptionalCompletion1() {
2155 >    public void testApplyToEither_exceptionalCompletion() {
2156          for (ExecutionMode m : ExecutionMode.values())
1740        for (boolean createIncomplete : new boolean[] { true, false })
1741        for (boolean fFirst : new boolean[] { true, false })
2157          for (Integer v1 : new Integer[] { 1, null })
2158      {
2159          final CompletableFuture<Integer> f = new CompletableFuture<>();
2160          final CompletableFuture<Integer> g = new CompletableFuture<>();
2161          final CFException ex = new CFException();
2162 <        final IncFunction r = new IncFunction(m);
2162 >        final IncFunction[] rs = new IncFunction[6];
2163 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2164  
2165 <        if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex);
2166 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2167 <        if (createIncomplete) {
2168 <            checkIncomplete(h);
2169 <            assertEquals(0, r.invocationCount);
2170 <            (fFirst ? f : g).completeExceptionally(ex);
2165 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2166 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2167 >        checkIncomplete(h0);
2168 >        checkIncomplete(h1);
2169 >        rs[0].assertNotInvoked();
2170 >        rs[1].assertNotInvoked();
2171 >        f.completeExceptionally(ex);
2172 >        checkCompletedWithWrappedException(h0, ex);
2173 >        checkCompletedWithWrappedException(h1, ex);
2174 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2175 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2176 >        checkCompletedWithWrappedException(h2, ex);
2177 >        checkCompletedWithWrappedException(h3, ex);
2178 >        g.complete(v1);
2179 >
2180 >        // unspecified behavior - both source completions available
2181 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2182 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2183 >        try {
2184 >            assertEquals(inc(v1), h4.join());
2185 >            rs[4].assertValue(inc(v1));
2186 >        } catch (CompletionException ok) {
2187 >            checkCompletedWithWrappedException(h4, ex);
2188 >            rs[4].assertNotInvoked();
2189 >        }
2190 >        try {
2191 >            assertEquals(inc(v1), h5.join());
2192 >            rs[5].assertValue(inc(v1));
2193 >        } catch (CompletionException ok) {
2194 >            checkCompletedWithWrappedException(h5, ex);
2195 >            rs[5].assertNotInvoked();
2196          }
2197  
2198 <        checkCompletedWithWrappedCFException(h, ex);
2199 <        (!fFirst ? f : g).complete(v1);
2200 <
2201 <        assertEquals(0, r.invocationCount);
2202 <        checkCompletedNormally(!fFirst ? f : g, v1);
2203 <        checkCompletedWithWrappedCFException(fFirst ? f : g, ex);
2204 <        checkCompletedWithWrappedCFException(h, ex);
2198 >        checkCompletedExceptionally(f, ex);
2199 >        checkCompletedNormally(g, v1);
2200 >        checkCompletedWithWrappedException(h0, ex);
2201 >        checkCompletedWithWrappedException(h1, ex);
2202 >        checkCompletedWithWrappedException(h2, ex);
2203 >        checkCompletedWithWrappedException(h3, ex);
2204 >        checkCompletedWithWrappedException(h4, ex);
2205 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2206      }}
2207  
2208      public void testApplyToEither_exceptionalCompletion2() {
2209          for (ExecutionMode m : ExecutionMode.values())
1768        for (boolean reverseArgs : new boolean[] { true, false })
2210          for (boolean fFirst : new boolean[] { true, false })
2211          for (Integer v1 : new Integer[] { 1, null })
2212      {
2213          final CompletableFuture<Integer> f = new CompletableFuture<>();
2214          final CompletableFuture<Integer> g = new CompletableFuture<>();
1774        final IncFunction r1 = new IncFunction(m);
1775        final IncFunction r2 = new IncFunction(m);
2215          final CFException ex = new CFException();
2216 <        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
2217 <        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1779 <        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1780 <        if (fFirst) {
1781 <            f.complete(v1);
1782 <            g.completeExceptionally(ex);
1783 <        } else {
1784 <            g.completeExceptionally(ex);
1785 <            f.complete(v1);
1786 <        }
1787 <        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
2216 >        final IncFunction[] rs = new IncFunction[6];
2217 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2218  
2219 <        // unspecified behavior
2219 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2220 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2221 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2222 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2223 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2224 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2225 >
2226 >        // unspecified behavior - both source completions available
2227 >        try {
2228 >            assertEquals(inc(v1), h0.join());
2229 >            rs[0].assertValue(inc(v1));
2230 >        } catch (CompletionException ok) {
2231 >            checkCompletedWithWrappedException(h0, ex);
2232 >            rs[0].assertNotInvoked();
2233 >        }
2234          try {
2235              assertEquals(inc(v1), h1.join());
2236 <            assertEquals(1, r1.invocationCount);
2236 >            rs[1].assertValue(inc(v1));
2237          } catch (CompletionException ok) {
2238 <            checkCompletedWithWrappedCFException(h1, ex);
2239 <            assertEquals(0, r1.invocationCount);
2238 >            checkCompletedWithWrappedException(h1, ex);
2239 >            rs[1].assertNotInvoked();
2240          }
1797
2241          try {
2242              assertEquals(inc(v1), h2.join());
2243 <            assertEquals(1, r2.invocationCount);
2243 >            rs[2].assertValue(inc(v1));
2244          } catch (CompletionException ok) {
2245 <            checkCompletedWithWrappedCFException(h2, ex);
2246 <            assertEquals(0, r2.invocationCount);
2245 >            checkCompletedWithWrappedException(h2, ex);
2246 >            rs[2].assertNotInvoked();
2247 >        }
2248 >        try {
2249 >            assertEquals(inc(v1), h3.join());
2250 >            rs[3].assertValue(inc(v1));
2251 >        } catch (CompletionException ok) {
2252 >            checkCompletedWithWrappedException(h3, ex);
2253 >            rs[3].assertNotInvoked();
2254          }
2255  
1806        checkCompletedWithWrappedCFException(g, ex);
1807        checkCompletedNormally(f, v1);
1808    }}
1809
1810    /**
1811     * applyToEither result completes exceptionally if action does
1812     */
1813    public void testApplyToEither_actionFailed1() {
1814        for (ExecutionMode m : ExecutionMode.values())
1815        for (Integer v1 : new Integer[] { 1, null })
1816        for (Integer v2 : new Integer[] { 2, null })
1817    {
1818        final CompletableFuture<Integer> f = new CompletableFuture<>();
1819        final CompletableFuture<Integer> g = new CompletableFuture<>();
1820        final FailingFunction r = new FailingFunction(m);
1821        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1822
1823        f.complete(v1);
1824        checkCompletedWithWrappedCFException(h);
1825        g.complete(v2);
1826        checkCompletedNormally(f, v1);
1827        checkCompletedNormally(g, v2);
1828    }}
1829
1830    public void testApplyToEither_actionFailed2() {
1831        for (ExecutionMode m : ExecutionMode.values())
1832        for (Integer v1 : new Integer[] { 1, null })
1833        for (Integer v2 : new Integer[] { 2, null })
1834    {
1835        final CompletableFuture<Integer> f = new CompletableFuture<>();
1836        final CompletableFuture<Integer> g = new CompletableFuture<>();
1837        final FailingFunction r = new FailingFunction(m);
1838        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1839
1840        g.complete(v2);
1841        checkCompletedWithWrappedCFException(h);
1842        f.complete(v1);
2256          checkCompletedNormally(f, v1);
2257 <        checkCompletedNormally(g, v2);
2257 >        checkCompletedExceptionally(g, ex);
2258      }}
2259  
2260      /**
2261       * applyToEither result completes exceptionally if either source cancelled
2262       */
2263 <    public void testApplyToEither_sourceCancelled1() {
2263 >    public void testApplyToEither_sourceCancelled() {
2264          for (ExecutionMode m : ExecutionMode.values())
2265          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1853        for (boolean createIncomplete : new boolean[] { true, false })
1854        for (boolean fFirst : new boolean[] { true, false })
2266          for (Integer v1 : new Integer[] { 1, null })
2267      {
2268          final CompletableFuture<Integer> f = new CompletableFuture<>();
2269          final CompletableFuture<Integer> g = new CompletableFuture<>();
2270 <        final IncFunction r = new IncFunction(m);
2270 >        final IncFunction[] rs = new IncFunction[6];
2271 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2272  
2273 <        if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
2274 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2275 <        if (createIncomplete) {
2276 <            checkIncomplete(h);
2277 <            assertEquals(0, r.invocationCount);
2278 <            assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
2273 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2274 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2275 >        checkIncomplete(h0);
2276 >        checkIncomplete(h1);
2277 >        rs[0].assertNotInvoked();
2278 >        rs[1].assertNotInvoked();
2279 >        f.cancel(mayInterruptIfRunning);
2280 >        checkCompletedWithWrappedCancellationException(h0);
2281 >        checkCompletedWithWrappedCancellationException(h1);
2282 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2283 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2284 >        checkCompletedWithWrappedCancellationException(h2);
2285 >        checkCompletedWithWrappedCancellationException(h3);
2286 >        g.complete(v1);
2287 >
2288 >        // unspecified behavior - both source completions available
2289 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2290 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2291 >        try {
2292 >            assertEquals(inc(v1), h4.join());
2293 >            rs[4].assertValue(inc(v1));
2294 >        } catch (CompletionException ok) {
2295 >            checkCompletedWithWrappedCancellationException(h4);
2296 >            rs[4].assertNotInvoked();
2297 >        }
2298 >        try {
2299 >            assertEquals(inc(v1), h5.join());
2300 >            rs[5].assertValue(inc(v1));
2301 >        } catch (CompletionException ok) {
2302 >            checkCompletedWithWrappedCancellationException(h5);
2303 >            rs[5].assertNotInvoked();
2304          }
2305  
2306 <        checkCompletedWithWrappedCancellationException(h);
2307 <        (!fFirst ? f : g).complete(v1);
2308 <
2309 <        assertEquals(0, r.invocationCount);
2310 <        checkCompletedNormally(!fFirst ? f : g, v1);
2311 <        checkCancelled(fFirst ? f : g);
2312 <        checkCompletedWithWrappedCancellationException(h);
2306 >        checkCancelled(f);
2307 >        checkCompletedNormally(g, v1);
2308 >        checkCompletedWithWrappedCancellationException(h0);
2309 >        checkCompletedWithWrappedCancellationException(h1);
2310 >        checkCompletedWithWrappedCancellationException(h2);
2311 >        checkCompletedWithWrappedCancellationException(h3);
2312 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2313      }}
2314  
2315      public void testApplyToEither_sourceCancelled2() {
2316          for (ExecutionMode m : ExecutionMode.values())
2317          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1881        for (boolean reverseArgs : new boolean[] { true, false })
2318          for (boolean fFirst : new boolean[] { true, false })
2319          for (Integer v1 : new Integer[] { 1, null })
2320      {
2321          final CompletableFuture<Integer> f = new CompletableFuture<>();
2322          final CompletableFuture<Integer> g = new CompletableFuture<>();
2323 <        final IncFunction r1 = new IncFunction(m);
2324 <        final IncFunction r2 = new IncFunction(m);
1889 <        final CFException ex = new CFException();
1890 <        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1891 <        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
2323 >        final IncFunction[] rs = new IncFunction[6];
2324 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2325  
2326 <        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
2327 <        if (fFirst) {
2328 <            f.complete(v1);
2329 <            assertTrue(g.cancel(mayInterruptIfRunning));
2330 <        } else {
2331 <            assertTrue(g.cancel(mayInterruptIfRunning));
1899 <            f.complete(v1);
1900 <        }
1901 <        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
2326 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2327 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2328 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2329 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2330 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2331 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2332  
2333 <        // unspecified behavior
2333 >        // unspecified behavior - both source completions available
2334 >        try {
2335 >            assertEquals(inc(v1), h0.join());
2336 >            rs[0].assertValue(inc(v1));
2337 >        } catch (CompletionException ok) {
2338 >            checkCompletedWithWrappedCancellationException(h0);
2339 >            rs[0].assertNotInvoked();
2340 >        }
2341          try {
2342              assertEquals(inc(v1), h1.join());
2343 <            assertEquals(1, r1.invocationCount);
2343 >            rs[1].assertValue(inc(v1));
2344          } catch (CompletionException ok) {
2345              checkCompletedWithWrappedCancellationException(h1);
2346 <            assertEquals(0, r1.invocationCount);
2346 >            rs[1].assertNotInvoked();
2347          }
1911
2348          try {
2349              assertEquals(inc(v1), h2.join());
2350 <            assertEquals(1, r2.invocationCount);
2350 >            rs[2].assertValue(inc(v1));
2351          } catch (CompletionException ok) {
2352              checkCompletedWithWrappedCancellationException(h2);
2353 <            assertEquals(0, r2.invocationCount);
2353 >            rs[2].assertNotInvoked();
2354 >        }
2355 >        try {
2356 >            assertEquals(inc(v1), h3.join());
2357 >            rs[3].assertValue(inc(v1));
2358 >        } catch (CompletionException ok) {
2359 >            checkCompletedWithWrappedCancellationException(h3);
2360 >            rs[3].assertNotInvoked();
2361          }
2362  
1920        checkCancelled(g);
2363          checkCompletedNormally(f, v1);
2364 +        checkCancelled(g);
2365      }}
2366  
2367      /**
2368 <     * acceptEither result completes normally after normal completion
1926 <     * of either source
2368 >     * applyToEither result completes exceptionally if action does
2369       */
2370 <    public void testAcceptEither_normalCompletion1() {
2370 >    public void testApplyToEither_actionFailed() {
2371          for (ExecutionMode m : ExecutionMode.values())
2372          for (Integer v1 : new Integer[] { 1, null })
2373          for (Integer v2 : new Integer[] { 2, null })
2374      {
2375          final CompletableFuture<Integer> f = new CompletableFuture<>();
2376          final CompletableFuture<Integer> g = new CompletableFuture<>();
2377 <        final IncAction r = new IncAction();
2378 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2377 >        final FailingFunction[] rs = new FailingFunction[6];
2378 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2379  
2380 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2381 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2382          f.complete(v1);
2383 <        checkCompletedNormally(h, null);
2384 <        assertEquals(inc(v1), r.value);
2385 <        g.complete(v2);
2386 <
2387 <        checkCompletedNormally(f, v1);
2388 <        checkCompletedNormally(g, v2);
2389 <        checkCompletedNormally(h, null);
1946 <    }}
1947 <
1948 <    public void testAcceptEither_normalCompletion2() {
1949 <        for (ExecutionMode m : ExecutionMode.values())
1950 <        for (Integer v1 : new Integer[] { 1, null })
1951 <        for (Integer v2 : new Integer[] { 2, null })
1952 <    {
1953 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1954 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1955 <        final IncAction r = new IncAction();
1956 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2383 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2384 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2385 >        checkCompletedWithWrappedCFException(h0);
2386 >        checkCompletedWithWrappedCFException(h1);
2387 >        checkCompletedWithWrappedCFException(h2);
2388 >        checkCompletedWithWrappedCFException(h3);
2389 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2390  
2391          g.complete(v2);
2392 <        checkCompletedNormally(h, null);
2393 <        assertEquals(inc(v2), r.value);
2394 <        f.complete(v1);
2392 >
2393 >        // unspecified behavior - both source completions available
2394 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2395 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2396 >
2397 >        checkCompletedWithWrappedCFException(h4);
2398 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2399 >                   Objects.equals(v2, rs[4].value));
2400 >        checkCompletedWithWrappedCFException(h5);
2401 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2402 >                   Objects.equals(v2, rs[5].value));
2403  
2404          checkCompletedNormally(f, v1);
2405          checkCompletedNormally(g, v2);
1965        checkCompletedNormally(h, null);
2406      }}
2407  
2408 <    public void testAcceptEither_normalCompletion3() {
2408 >    /**
2409 >     * acceptEither result completes normally after normal completion
2410 >     * of either source
2411 >     */
2412 >    public void testAcceptEither_normalCompletion() {
2413          for (ExecutionMode m : ExecutionMode.values())
2414          for (Integer v1 : new Integer[] { 1, null })
2415          for (Integer v2 : new Integer[] { 2, null })
2416      {
2417          final CompletableFuture<Integer> f = new CompletableFuture<>();
2418          final CompletableFuture<Integer> g = new CompletableFuture<>();
2419 <        final IncAction r = new IncAction();
2419 >        final NoopConsumer[] rs = new NoopConsumer[6];
2420 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2421  
2422 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2423 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2424 +        checkIncomplete(h0);
2425 +        checkIncomplete(h1);
2426 +        rs[0].assertNotInvoked();
2427 +        rs[1].assertNotInvoked();
2428          f.complete(v1);
2429 +        checkCompletedNormally(h0, null);
2430 +        checkCompletedNormally(h1, null);
2431 +        rs[0].assertValue(v1);
2432 +        rs[1].assertValue(v1);
2433 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2434 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2435 +        checkCompletedNormally(h2, null);
2436 +        checkCompletedNormally(h3, null);
2437 +        rs[2].assertValue(v1);
2438 +        rs[3].assertValue(v1);
2439          g.complete(v2);
1979        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2440  
2441 <        checkCompletedNormally(h, null);
2441 >        // unspecified behavior - both source completions available
2442 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2443 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2444 >        checkCompletedNormally(h4, null);
2445 >        checkCompletedNormally(h5, null);
2446 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2447 >                   Objects.equals(v2, rs[4].value));
2448 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2449 >                   Objects.equals(v2, rs[5].value));
2450 >
2451          checkCompletedNormally(f, v1);
2452          checkCompletedNormally(g, v2);
2453 <
2454 <        // unspecified behavior
2455 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2456 <                   Objects.equals(r.value, inc(v2)));
2453 >        checkCompletedNormally(h0, null);
2454 >        checkCompletedNormally(h1, null);
2455 >        checkCompletedNormally(h2, null);
2456 >        checkCompletedNormally(h3, null);
2457 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2458      }}
2459  
2460      /**
2461       * acceptEither result completes exceptionally after exceptional
2462       * completion of either source
2463       */
2464 <    public void testAcceptEither_exceptionalCompletion1() {
2464 >    public void testAcceptEither_exceptionalCompletion() {
2465          for (ExecutionMode m : ExecutionMode.values())
2466          for (Integer v1 : new Integer[] { 1, null })
2467      {
2468          final CompletableFuture<Integer> f = new CompletableFuture<>();
2469          final CompletableFuture<Integer> g = new CompletableFuture<>();
2000        final IncAction r = new IncAction();
2001        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2470          final CFException ex = new CFException();
2471 +        final NoopConsumer[] rs = new NoopConsumer[6];
2472 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2473  
2474 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2475 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2476 +        checkIncomplete(h0);
2477 +        checkIncomplete(h1);
2478 +        rs[0].assertNotInvoked();
2479 +        rs[1].assertNotInvoked();
2480          f.completeExceptionally(ex);
2481 <        checkCompletedWithWrappedCFException(h, ex);
2481 >        checkCompletedWithWrappedException(h0, ex);
2482 >        checkCompletedWithWrappedException(h1, ex);
2483 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2484 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2485 >        checkCompletedWithWrappedException(h2, ex);
2486 >        checkCompletedWithWrappedException(h3, ex);
2487 >
2488          g.complete(v1);
2489  
2490 <        assertEquals(0, r.invocationCount);
2490 >        // unspecified behavior - both source completions available
2491 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2492 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2493 >        try {
2494 >            assertNull(h4.join());
2495 >            rs[4].assertValue(v1);
2496 >        } catch (CompletionException ok) {
2497 >            checkCompletedWithWrappedException(h4, ex);
2498 >            rs[4].assertNotInvoked();
2499 >        }
2500 >        try {
2501 >            assertNull(h5.join());
2502 >            rs[5].assertValue(v1);
2503 >        } catch (CompletionException ok) {
2504 >            checkCompletedWithWrappedException(h5, ex);
2505 >            rs[5].assertNotInvoked();
2506 >        }
2507 >
2508 >        checkCompletedExceptionally(f, ex);
2509          checkCompletedNormally(g, v1);
2510 <        checkCompletedWithWrappedCFException(f, ex);
2511 <        checkCompletedWithWrappedCFException(h, ex);
2510 >        checkCompletedWithWrappedException(h0, ex);
2511 >        checkCompletedWithWrappedException(h1, ex);
2512 >        checkCompletedWithWrappedException(h2, ex);
2513 >        checkCompletedWithWrappedException(h3, ex);
2514 >        checkCompletedWithWrappedException(h4, ex);
2515 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2516      }}
2517  
2518      public void testAcceptEither_exceptionalCompletion2() {
2519          for (ExecutionMode m : ExecutionMode.values())
2520 +        for (boolean fFirst : new boolean[] { true, false })
2521          for (Integer v1 : new Integer[] { 1, null })
2522      {
2523          final CompletableFuture<Integer> f = new CompletableFuture<>();
2524          final CompletableFuture<Integer> g = new CompletableFuture<>();
2020        final IncAction r = new IncAction();
2021        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2022        final CFException ex = new CFException();
2023
2024        g.completeExceptionally(ex);
2025        checkCompletedWithWrappedCFException(h, ex);
2026        f.complete(v1);
2027
2028        assertEquals(0, r.invocationCount);
2029        checkCompletedNormally(f, v1);
2030        checkCompletedWithWrappedCFException(g, ex);
2031        checkCompletedWithWrappedCFException(h, ex);
2032    }}
2033
2034    public void testAcceptEither_exceptionalCompletion3() {
2035        for (ExecutionMode m : ExecutionMode.values())
2036        for (Integer v1 : new Integer[] { 1, null })
2037    {
2038        final CompletableFuture<Integer> f = new CompletableFuture<>();
2039        final CompletableFuture<Integer> g = new CompletableFuture<>();
2040        final IncAction r = new IncAction();
2525          final CFException ex = new CFException();
2526 +        final NoopConsumer[] rs = new NoopConsumer[6];
2527 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2528  
2529 <        g.completeExceptionally(ex);
2530 <        f.complete(v1);
2531 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2529 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2530 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2531 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2532 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2533 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2534 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2535  
2536 <        // unspecified behavior
2048 <        Integer v;
2536 >        // unspecified behavior - both source completions available
2537          try {
2538 <            assertNull(h.join());
2539 <            assertEquals(1, r.invocationCount);
2052 <            assertEquals(inc(v1), r.value);
2538 >            assertEquals(null, h0.join());
2539 >            rs[0].assertValue(v1);
2540          } catch (CompletionException ok) {
2541 <            checkCompletedWithWrappedCFException(h, ex);
2542 <            assertEquals(0, r.invocationCount);
2541 >            checkCompletedWithWrappedException(h0, ex);
2542 >            rs[0].assertNotInvoked();
2543          }
2057
2058        checkCompletedWithWrappedCFException(g, ex);
2059        checkCompletedNormally(f, v1);
2060    }}
2061
2062    public void testAcceptEither_exceptionalCompletion4() {
2063        for (ExecutionMode m : ExecutionMode.values())
2064        for (Integer v1 : new Integer[] { 1, null })
2065    {
2066        final CompletableFuture<Integer> f = new CompletableFuture<>();
2067        final CompletableFuture<Integer> g = new CompletableFuture<>();
2068        final IncAction r = new IncAction();
2069        final CFException ex = new CFException();
2070
2071        f.completeExceptionally(ex);
2072        g.complete(v1);
2073        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2074
2075        // unspecified behavior
2076        Integer v;
2544          try {
2545 <            assertNull(h.join());
2546 <            assertEquals(1, r.invocationCount);
2080 <            assertEquals(inc(v1), r.value);
2545 >            assertEquals(null, h1.join());
2546 >            rs[1].assertValue(v1);
2547          } catch (CompletionException ok) {
2548 <            checkCompletedWithWrappedCFException(h, ex);
2549 <            assertEquals(0, r.invocationCount);
2548 >            checkCompletedWithWrappedException(h1, ex);
2549 >            rs[1].assertNotInvoked();
2550 >        }
2551 >        try {
2552 >            assertEquals(null, h2.join());
2553 >            rs[2].assertValue(v1);
2554 >        } catch (CompletionException ok) {
2555 >            checkCompletedWithWrappedException(h2, ex);
2556 >            rs[2].assertNotInvoked();
2557 >        }
2558 >        try {
2559 >            assertEquals(null, h3.join());
2560 >            rs[3].assertValue(v1);
2561 >        } catch (CompletionException ok) {
2562 >            checkCompletedWithWrappedException(h3, ex);
2563 >            rs[3].assertNotInvoked();
2564          }
2565  
2086        checkCompletedWithWrappedCFException(f, ex);
2087        checkCompletedNormally(g, v1);
2088    }}
2089
2090    /**
2091     * acceptEither result completes exceptionally if action does
2092     */
2093    public void testAcceptEither_actionFailed1() {
2094        for (ExecutionMode m : ExecutionMode.values())
2095        for (Integer v1 : new Integer[] { 1, null })
2096        for (Integer v2 : new Integer[] { 2, null })
2097    {
2098        final CompletableFuture<Integer> f = new CompletableFuture<>();
2099        final CompletableFuture<Integer> g = new CompletableFuture<>();
2100        final FailingConsumer r = new FailingConsumer(m);
2101        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2102
2103        f.complete(v1);
2104        checkCompletedWithWrappedCFException(h);
2105        g.complete(v2);
2106        checkCompletedNormally(f, v1);
2107        checkCompletedNormally(g, v2);
2108    }}
2109
2110    public void testAcceptEither_actionFailed2() {
2111        for (ExecutionMode m : ExecutionMode.values())
2112        for (Integer v1 : new Integer[] { 1, null })
2113        for (Integer v2 : new Integer[] { 2, null })
2114    {
2115        final CompletableFuture<Integer> f = new CompletableFuture<>();
2116        final CompletableFuture<Integer> g = new CompletableFuture<>();
2117        final FailingConsumer r = new FailingConsumer(m);
2118        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2119
2120        g.complete(v2);
2121        checkCompletedWithWrappedCFException(h);
2122        f.complete(v1);
2566          checkCompletedNormally(f, v1);
2567 <        checkCompletedNormally(g, v2);
2567 >        checkCompletedExceptionally(g, ex);
2568      }}
2569  
2570      /**
2571       * acceptEither result completes exceptionally if either source cancelled
2572       */
2573 <    public void testAcceptEither_sourceCancelled1() {
2573 >    public void testAcceptEither_sourceCancelled() {
2574          for (ExecutionMode m : ExecutionMode.values())
2575          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2576          for (Integer v1 : new Integer[] { 1, null })
2577      {
2578          final CompletableFuture<Integer> f = new CompletableFuture<>();
2579          final CompletableFuture<Integer> g = new CompletableFuture<>();
2580 <        final IncAction r = new IncAction();
2581 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2139 <
2140 <        assertTrue(f.cancel(mayInterruptIfRunning));
2141 <        checkCompletedWithWrappedCancellationException(h);
2142 <        g.complete(v1);
2143 <
2144 <        checkCancelled(f);
2145 <        assertEquals(0, r.invocationCount);
2146 <        checkCompletedNormally(g, v1);
2147 <        checkCompletedWithWrappedCancellationException(h);
2148 <    }}
2580 >        final NoopConsumer[] rs = new NoopConsumer[6];
2581 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2582  
2583 <    public void testAcceptEither_sourceCancelled2() {
2584 <        for (ExecutionMode m : ExecutionMode.values())
2585 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2586 <        for (Integer v1 : new Integer[] { 1, null })
2587 <    {
2588 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2589 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2590 <        final IncAction r = new IncAction();
2591 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2583 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2584 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2585 >        checkIncomplete(h0);
2586 >        checkIncomplete(h1);
2587 >        rs[0].assertNotInvoked();
2588 >        rs[1].assertNotInvoked();
2589 >        f.cancel(mayInterruptIfRunning);
2590 >        checkCompletedWithWrappedCancellationException(h0);
2591 >        checkCompletedWithWrappedCancellationException(h1);
2592 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2593 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2594 >        checkCompletedWithWrappedCancellationException(h2);
2595 >        checkCompletedWithWrappedCancellationException(h3);
2596  
2597 <        assertTrue(g.cancel(mayInterruptIfRunning));
2161 <        checkCompletedWithWrappedCancellationException(h);
2162 <        f.complete(v1);
2163 <
2164 <        checkCancelled(g);
2165 <        assertEquals(0, r.invocationCount);
2166 <        checkCompletedNormally(f, v1);
2167 <        checkCompletedWithWrappedCancellationException(h);
2168 <    }}
2169 <
2170 <    public void testAcceptEither_sourceCancelled3() {
2171 <        for (ExecutionMode m : ExecutionMode.values())
2172 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2173 <        for (Integer v1 : new Integer[] { 1, null })
2174 <    {
2175 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2176 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2177 <        final IncAction r = new IncAction();
2178 <
2179 <        assertTrue(g.cancel(mayInterruptIfRunning));
2180 <        f.complete(v1);
2181 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2597 >        g.complete(v1);
2598  
2599 <        // unspecified behavior
2600 <        Integer v;
2599 >        // unspecified behavior - both source completions available
2600 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2601 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2602          try {
2603 <            assertNull(h.join());
2604 <            assertEquals(1, r.invocationCount);
2188 <            assertEquals(inc(v1), r.value);
2603 >            assertNull(h4.join());
2604 >            rs[4].assertValue(v1);
2605          } catch (CompletionException ok) {
2606 <            checkCompletedWithWrappedCancellationException(h);
2607 <            assertEquals(0, r.invocationCount);
2606 >            checkCompletedWithWrappedCancellationException(h4);
2607 >            rs[4].assertNotInvoked();
2608          }
2193
2194        checkCancelled(g);
2195        checkCompletedNormally(f, v1);
2196    }}
2197
2198    public void testAcceptEither_sourceCancelled4() {
2199        for (ExecutionMode m : ExecutionMode.values())
2200        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2201        for (Integer v1 : new Integer[] { 1, null })
2202    {
2203        final CompletableFuture<Integer> f = new CompletableFuture<>();
2204        final CompletableFuture<Integer> g = new CompletableFuture<>();
2205        final IncAction r = new IncAction();
2206
2207        assertTrue(f.cancel(mayInterruptIfRunning));
2208        g.complete(v1);
2209        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2210
2211        // unspecified behavior
2212        Integer v;
2609          try {
2610 <            assertNull(h.join());
2611 <            assertEquals(1, r.invocationCount);
2216 <            assertEquals(inc(v1), r.value);
2610 >            assertNull(h5.join());
2611 >            rs[5].assertValue(v1);
2612          } catch (CompletionException ok) {
2613 <            checkCompletedWithWrappedCancellationException(h);
2614 <            assertEquals(0, r.invocationCount);
2613 >            checkCompletedWithWrappedCancellationException(h5);
2614 >            rs[5].assertNotInvoked();
2615          }
2616  
2617          checkCancelled(f);
2618          checkCompletedNormally(g, v1);
2619 +        checkCompletedWithWrappedCancellationException(h0);
2620 +        checkCompletedWithWrappedCancellationException(h1);
2621 +        checkCompletedWithWrappedCancellationException(h2);
2622 +        checkCompletedWithWrappedCancellationException(h3);
2623 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2624      }}
2625  
2626      /**
2627 <     * runAfterEither result completes normally after normal completion
2228 <     * of either source
2627 >     * acceptEither result completes exceptionally if action does
2628       */
2629 <    public void testRunAfterEither_normalCompletion1() {
2629 >    public void testAcceptEither_actionFailed() {
2630          for (ExecutionMode m : ExecutionMode.values())
2631          for (Integer v1 : new Integer[] { 1, null })
2632          for (Integer v2 : new Integer[] { 2, null })
2633      {
2634          final CompletableFuture<Integer> f = new CompletableFuture<>();
2635          final CompletableFuture<Integer> g = new CompletableFuture<>();
2636 <        final Noop r = new Noop(m);
2637 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2636 >        final FailingConsumer[] rs = new FailingConsumer[6];
2637 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2638  
2639 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2640 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2641          f.complete(v1);
2642 <        checkCompletedNormally(h, null);
2643 <        assertEquals(1, r.invocationCount);
2642 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2643 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2644 >        checkCompletedWithWrappedCFException(h0);
2645 >        checkCompletedWithWrappedCFException(h1);
2646 >        checkCompletedWithWrappedCFException(h2);
2647 >        checkCompletedWithWrappedCFException(h3);
2648 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2649 >
2650          g.complete(v2);
2651  
2652 +        // unspecified behavior - both source completions available
2653 +        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2654 +        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2655 +
2656 +        checkCompletedWithWrappedCFException(h4);
2657 +        assertTrue(Objects.equals(v1, rs[4].value) ||
2658 +                   Objects.equals(v2, rs[4].value));
2659 +        checkCompletedWithWrappedCFException(h5);
2660 +        assertTrue(Objects.equals(v1, rs[5].value) ||
2661 +                   Objects.equals(v2, rs[5].value));
2662 +
2663          checkCompletedNormally(f, v1);
2664          checkCompletedNormally(g, v2);
2247        checkCompletedNormally(h, null);
2248        assertEquals(1, r.invocationCount);
2665      }}
2666  
2667 <    public void testRunAfterEither_normalCompletion2() {
2667 >    /**
2668 >     * runAfterEither result completes normally after normal completion
2669 >     * of either source
2670 >     */
2671 >    public void testRunAfterEither_normalCompletion() {
2672          for (ExecutionMode m : ExecutionMode.values())
2673          for (Integer v1 : new Integer[] { 1, null })
2674          for (Integer v2 : new Integer[] { 2, null })
2675      {
2676          final CompletableFuture<Integer> f = new CompletableFuture<>();
2677          final CompletableFuture<Integer> g = new CompletableFuture<>();
2678 <        final Noop r = new Noop(m);
2679 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2678 >        final Noop[] rs = new Noop[6];
2679 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2680  
2681 <        g.complete(v2);
2682 <        checkCompletedNormally(h, null);
2683 <        assertEquals(1, r.invocationCount);
2681 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2682 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2683 >        checkIncomplete(h0);
2684 >        checkIncomplete(h1);
2685 >        rs[0].assertNotInvoked();
2686 >        rs[1].assertNotInvoked();
2687          f.complete(v1);
2688 +        checkCompletedNormally(h0, null);
2689 +        checkCompletedNormally(h1, null);
2690 +        rs[0].assertInvoked();
2691 +        rs[1].assertInvoked();
2692 +        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2693 +        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2694 +        checkCompletedNormally(h2, null);
2695 +        checkCompletedNormally(h3, null);
2696 +        rs[2].assertInvoked();
2697 +        rs[3].assertInvoked();
2698  
2266        checkCompletedNormally(f, v1);
2267        checkCompletedNormally(g, v2);
2268        checkCompletedNormally(h, null);
2269        assertEquals(1, r.invocationCount);
2270        }}
2271
2272    public void testRunAfterEither_normalCompletion3() {
2273        for (ExecutionMode m : ExecutionMode.values())
2274        for (Integer v1 : new Integer[] { 1, null })
2275        for (Integer v2 : new Integer[] { 2, null })
2276    {
2277        final CompletableFuture<Integer> f = new CompletableFuture<>();
2278        final CompletableFuture<Integer> g = new CompletableFuture<>();
2279        final Noop r = new Noop(m);
2280
2281        f.complete(v1);
2699          g.complete(v2);
2283        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2700  
2701 <        checkCompletedNormally(h, null);
2701 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2702 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2703 >
2704          checkCompletedNormally(f, v1);
2705          checkCompletedNormally(g, v2);
2706 <        assertEquals(1, r.invocationCount);
2706 >        checkCompletedNormally(h0, null);
2707 >        checkCompletedNormally(h1, null);
2708 >        checkCompletedNormally(h2, null);
2709 >        checkCompletedNormally(h3, null);
2710 >        checkCompletedNormally(h4, null);
2711 >        checkCompletedNormally(h5, null);
2712 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2713      }}
2714  
2715      /**
2716       * runAfterEither result completes exceptionally after exceptional
2717       * completion of either source
2718       */
2719 <    public void testRunAfterEither_exceptionalCompletion1() {
2719 >    public void testRunAfterEither_exceptionalCompletion() {
2720          for (ExecutionMode m : ExecutionMode.values())
2721          for (Integer v1 : new Integer[] { 1, null })
2722      {
2723          final CompletableFuture<Integer> f = new CompletableFuture<>();
2724          final CompletableFuture<Integer> g = new CompletableFuture<>();
2301        final Noop r = new Noop(m);
2302        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2725          final CFException ex = new CFException();
2726 +        final Noop[] rs = new Noop[6];
2727 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2728  
2729 <        f.completeExceptionally(ex);
2730 <        checkCompletedWithWrappedCFException(h, ex);
2731 <        g.complete(v1);
2729 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2730 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2731 >        checkIncomplete(h0);
2732 >        checkIncomplete(h1);
2733 >        rs[0].assertNotInvoked();
2734 >        rs[1].assertNotInvoked();
2735 >        assertTrue(f.completeExceptionally(ex));
2736 >        checkCompletedWithWrappedException(h0, ex);
2737 >        checkCompletedWithWrappedException(h1, ex);
2738 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2739 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2740 >        checkCompletedWithWrappedException(h2, ex);
2741 >        checkCompletedWithWrappedException(h3, ex);
2742  
2743 <        assertEquals(0, r.invocationCount);
2310 <        checkCompletedNormally(g, v1);
2311 <        checkCompletedWithWrappedCFException(f, ex);
2312 <        checkCompletedWithWrappedCFException(h, ex);
2313 <    }}
2314 <
2315 <    public void testRunAfterEither_exceptionalCompletion2() {
2316 <        for (ExecutionMode m : ExecutionMode.values())
2317 <        for (Integer v1 : new Integer[] { 1, null })
2318 <    {
2319 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2320 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2321 <        final Noop r = new Noop(m);
2322 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2323 <        final CFException ex = new CFException();
2324 <
2325 <        g.completeExceptionally(ex);
2326 <        checkCompletedWithWrappedCFException(h, ex);
2327 <        f.complete(v1);
2328 <
2329 <        assertEquals(0, r.invocationCount);
2330 <        checkCompletedNormally(f, v1);
2331 <        checkCompletedWithWrappedCFException(g, ex);
2332 <        checkCompletedWithWrappedCFException(h, ex);
2333 <    }}
2743 >        assertTrue(g.complete(v1));
2744  
2745 <    public void testRunAfterEither_exceptionalCompletion3() {
2746 <        for (ExecutionMode m : ExecutionMode.values())
2747 <        for (Integer v1 : new Integer[] { 1, null })
2338 <    {
2339 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2340 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2341 <        final Noop r = new Noop(m);
2342 <        final CFException ex = new CFException();
2343 <
2344 <        g.completeExceptionally(ex);
2345 <        f.complete(v1);
2346 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2347 <
2348 <        // unspecified behavior
2349 <        Integer v;
2745 >        // unspecified behavior - both source completions available
2746 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2747 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2748          try {
2749 <            assertNull(h.join());
2750 <            assertEquals(1, r.invocationCount);
2749 >            assertNull(h4.join());
2750 >            rs[4].assertInvoked();
2751          } catch (CompletionException ok) {
2752 <            checkCompletedWithWrappedCFException(h, ex);
2753 <            assertEquals(0, r.invocationCount);
2752 >            checkCompletedWithWrappedException(h4, ex);
2753 >            rs[4].assertNotInvoked();
2754          }
2357
2358        checkCompletedWithWrappedCFException(g, ex);
2359        checkCompletedNormally(f, v1);
2360    }}
2361
2362    public void testRunAfterEither_exceptionalCompletion4() {
2363        for (ExecutionMode m : ExecutionMode.values())
2364        for (Integer v1 : new Integer[] { 1, null })
2365    {
2366        final CompletableFuture<Integer> f = new CompletableFuture<>();
2367        final CompletableFuture<Integer> g = new CompletableFuture<>();
2368        final Noop r = new Noop(m);
2369        final CFException ex = new CFException();
2370
2371        f.completeExceptionally(ex);
2372        g.complete(v1);
2373        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2374
2375        // unspecified behavior
2376        Integer v;
2755          try {
2756 <            assertNull(h.join());
2757 <            assertEquals(1, r.invocationCount);
2756 >            assertNull(h5.join());
2757 >            rs[5].assertInvoked();
2758          } catch (CompletionException ok) {
2759 <            checkCompletedWithWrappedCFException(h, ex);
2760 <            assertEquals(0, r.invocationCount);
2759 >            checkCompletedWithWrappedException(h5, ex);
2760 >            rs[5].assertNotInvoked();
2761          }
2762  
2763 <        checkCompletedWithWrappedCFException(f, ex);
2763 >        checkCompletedExceptionally(f, ex);
2764          checkCompletedNormally(g, v1);
2765 +        checkCompletedWithWrappedException(h0, ex);
2766 +        checkCompletedWithWrappedException(h1, ex);
2767 +        checkCompletedWithWrappedException(h2, ex);
2768 +        checkCompletedWithWrappedException(h3, ex);
2769 +        checkCompletedWithWrappedException(h4, ex);
2770 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2771      }}
2772  
2773 <    /**
2390 <     * runAfterEither result completes exceptionally if action does
2391 <     */
2392 <    public void testRunAfterEither_actionFailed1() {
2773 >    public void testRunAfterEither_exceptionalCompletion2() {
2774          for (ExecutionMode m : ExecutionMode.values())
2775 +        for (boolean fFirst : new boolean[] { true, false })
2776          for (Integer v1 : new Integer[] { 1, null })
2395        for (Integer v2 : new Integer[] { 2, null })
2777      {
2778          final CompletableFuture<Integer> f = new CompletableFuture<>();
2779          final CompletableFuture<Integer> g = new CompletableFuture<>();
2780 <        final FailingRunnable r = new FailingRunnable(m);
2781 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2780 >        final CFException ex = new CFException();
2781 >        final Noop[] rs = new Noop[6];
2782 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2783  
2784 <        f.complete(v1);
2785 <        checkCompletedWithWrappedCFException(h);
2786 <        g.complete(v2);
2787 <        checkCompletedNormally(f, v1);
2788 <        checkCompletedNormally(g, v2);
2789 <    }}
2784 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2785 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2786 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2787 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2788 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2789 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2790  
2791 <    public void testRunAfterEither_actionFailed2() {
2792 <        for (ExecutionMode m : ExecutionMode.values())
2793 <        for (Integer v1 : new Integer[] { 1, null })
2794 <        for (Integer v2 : new Integer[] { 2, null })
2795 <    {
2796 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2797 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2798 <        final FailingRunnable r = new FailingRunnable(m);
2799 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2791 >        // unspecified behavior - both source completions available
2792 >        try {
2793 >            assertEquals(null, h0.join());
2794 >            rs[0].assertInvoked();
2795 >        } catch (CompletionException ok) {
2796 >            checkCompletedWithWrappedException(h0, ex);
2797 >            rs[0].assertNotInvoked();
2798 >        }
2799 >        try {
2800 >            assertEquals(null, h1.join());
2801 >            rs[1].assertInvoked();
2802 >        } catch (CompletionException ok) {
2803 >            checkCompletedWithWrappedException(h1, ex);
2804 >            rs[1].assertNotInvoked();
2805 >        }
2806 >        try {
2807 >            assertEquals(null, h2.join());
2808 >            rs[2].assertInvoked();
2809 >        } catch (CompletionException ok) {
2810 >            checkCompletedWithWrappedException(h2, ex);
2811 >            rs[2].assertNotInvoked();
2812 >        }
2813 >        try {
2814 >            assertEquals(null, h3.join());
2815 >            rs[3].assertInvoked();
2816 >        } catch (CompletionException ok) {
2817 >            checkCompletedWithWrappedException(h3, ex);
2818 >            rs[3].assertNotInvoked();
2819 >        }
2820  
2419        g.complete(v2);
2420        checkCompletedWithWrappedCFException(h);
2421        f.complete(v1);
2821          checkCompletedNormally(f, v1);
2822 <        checkCompletedNormally(g, v2);
2822 >        checkCompletedExceptionally(g, ex);
2823      }}
2824  
2825      /**
2826       * runAfterEither result completes exceptionally if either source cancelled
2827       */
2828 <    public void testRunAfterEither_sourceCancelled1() {
2430 <        for (ExecutionMode m : ExecutionMode.values())
2431 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2432 <        for (Integer v1 : new Integer[] { 1, null })
2433 <    {
2434 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2435 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2436 <        final Noop r = new Noop(m);
2437 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2438 <
2439 <        assertTrue(f.cancel(mayInterruptIfRunning));
2440 <        checkCompletedWithWrappedCancellationException(h);
2441 <        g.complete(v1);
2442 <
2443 <        checkCancelled(f);
2444 <        assertEquals(0, r.invocationCount);
2445 <        checkCompletedNormally(g, v1);
2446 <        checkCompletedWithWrappedCancellationException(h);
2447 <    }}
2448 <
2449 <    public void testRunAfterEither_sourceCancelled2() {
2450 <        for (ExecutionMode m : ExecutionMode.values())
2451 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2452 <        for (Integer v1 : new Integer[] { 1, null })
2453 <    {
2454 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2455 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2456 <        final Noop r = new Noop(m);
2457 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2458 <
2459 <        assertTrue(g.cancel(mayInterruptIfRunning));
2460 <        checkCompletedWithWrappedCancellationException(h);
2461 <        f.complete(v1);
2462 <
2463 <        checkCancelled(g);
2464 <        assertEquals(0, r.invocationCount);
2465 <        checkCompletedNormally(f, v1);
2466 <        checkCompletedWithWrappedCancellationException(h);
2467 <    }}
2468 <
2469 <    public void testRunAfterEither_sourceCancelled3() {
2828 >    public void testRunAfterEither_sourceCancelled() {
2829          for (ExecutionMode m : ExecutionMode.values())
2830          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2831          for (Integer v1 : new Integer[] { 1, null })
2832      {
2833          final CompletableFuture<Integer> f = new CompletableFuture<>();
2834          final CompletableFuture<Integer> g = new CompletableFuture<>();
2835 <        final Noop r = new Noop(m);
2836 <
2478 <        assertTrue(g.cancel(mayInterruptIfRunning));
2479 <        f.complete(v1);
2480 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2835 >        final Noop[] rs = new Noop[6];
2836 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2837  
2838 <        // unspecified behavior
2839 <        Integer v;
2838 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2839 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2840 >        checkIncomplete(h0);
2841 >        checkIncomplete(h1);
2842 >        rs[0].assertNotInvoked();
2843 >        rs[1].assertNotInvoked();
2844 >        f.cancel(mayInterruptIfRunning);
2845 >        checkCompletedWithWrappedCancellationException(h0);
2846 >        checkCompletedWithWrappedCancellationException(h1);
2847 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2848 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2849 >        checkCompletedWithWrappedCancellationException(h2);
2850 >        checkCompletedWithWrappedCancellationException(h3);
2851 >
2852 >        assertTrue(g.complete(v1));
2853 >
2854 >        // unspecified behavior - both source completions available
2855 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2856 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2857 >        try {
2858 >            assertNull(h4.join());
2859 >            rs[4].assertInvoked();
2860 >        } catch (CompletionException ok) {
2861 >            checkCompletedWithWrappedCancellationException(h4);
2862 >            rs[4].assertNotInvoked();
2863 >        }
2864          try {
2865 <            assertNull(h.join());
2866 <            assertEquals(1, r.invocationCount);
2865 >            assertNull(h5.join());
2866 >            rs[5].assertInvoked();
2867          } catch (CompletionException ok) {
2868 <            checkCompletedWithWrappedCancellationException(h);
2869 <            assertEquals(0, r.invocationCount);
2868 >            checkCompletedWithWrappedCancellationException(h5);
2869 >            rs[5].assertNotInvoked();
2870          }
2871  
2872 <        checkCancelled(g);
2873 <        checkCompletedNormally(f, v1);
2872 >        checkCancelled(f);
2873 >        checkCompletedNormally(g, v1);
2874 >        checkCompletedWithWrappedCancellationException(h0);
2875 >        checkCompletedWithWrappedCancellationException(h1);
2876 >        checkCompletedWithWrappedCancellationException(h2);
2877 >        checkCompletedWithWrappedCancellationException(h3);
2878 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2879      }}
2880  
2881 <    public void testRunAfterEither_sourceCancelled4() {
2881 >    /**
2882 >     * runAfterEither result completes exceptionally if action does
2883 >     */
2884 >    public void testRunAfterEither_actionFailed() {
2885          for (ExecutionMode m : ExecutionMode.values())
2498        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2886          for (Integer v1 : new Integer[] { 1, null })
2887 +        for (Integer v2 : new Integer[] { 2, null })
2888      {
2889          final CompletableFuture<Integer> f = new CompletableFuture<>();
2890          final CompletableFuture<Integer> g = new CompletableFuture<>();
2891 <        final Noop r = new Noop(m);
2892 <
2505 <        assertTrue(f.cancel(mayInterruptIfRunning));
2506 <        g.complete(v1);
2507 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2891 >        final FailingRunnable[] rs = new FailingRunnable[6];
2892 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2893  
2894 <        // unspecified behavior
2895 <        Integer v;
2896 <        try {
2897 <            assertNull(h.join());
2898 <            assertEquals(1, r.invocationCount);
2899 <        } catch (CompletionException ok) {
2900 <            checkCompletedWithWrappedCancellationException(h);
2901 <            assertEquals(0, r.invocationCount);
2902 <        }
2894 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2895 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2896 >        assertTrue(f.complete(v1));
2897 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2898 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2899 >        checkCompletedWithWrappedCFException(h0);
2900 >        checkCompletedWithWrappedCFException(h1);
2901 >        checkCompletedWithWrappedCFException(h2);
2902 >        checkCompletedWithWrappedCFException(h3);
2903 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2904 >        assertTrue(g.complete(v2));
2905 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2906 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2907 >        checkCompletedWithWrappedCFException(h4);
2908 >        checkCompletedWithWrappedCFException(h5);
2909  
2910 <        checkCancelled(f);
2911 <        checkCompletedNormally(g, v1);
2910 >        checkCompletedNormally(f, v1);
2911 >        checkCompletedNormally(g, v2);
2912 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2913      }}
2914  
2915      /**
# Line 2530 | Line 2922 | public class CompletableFutureTest exten
2922      {
2923          final CompletableFuture<Integer> f = new CompletableFuture<>();
2924          final CompletableFutureInc r = new CompletableFutureInc(m);
2925 <        if (!createIncomplete) f.complete(v1);
2925 >        if (!createIncomplete) assertTrue(f.complete(v1));
2926          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2927 <        if (createIncomplete) f.complete(v1);
2927 >        if (createIncomplete) assertTrue(f.complete(v1));
2928  
2929          checkCompletedNormally(g, inc(v1));
2930          checkCompletedNormally(f, v1);
2931 <        assertEquals(1, r.invocationCount);
2931 >        r.assertValue(v1);
2932      }}
2933  
2934      /**
# Line 2554 | Line 2946 | public class CompletableFutureTest exten
2946          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2947          if (createIncomplete) f.completeExceptionally(ex);
2948  
2949 <        checkCompletedWithWrappedCFException(g, ex);
2950 <        checkCompletedWithWrappedCFException(f, ex);
2951 <        assertEquals(0, r.invocationCount);
2949 >        checkCompletedWithWrappedException(g, ex);
2950 >        checkCompletedExceptionally(f, ex);
2951 >        r.assertNotInvoked();
2952      }}
2953  
2954      /**
# Line 2570 | Line 2962 | public class CompletableFutureTest exten
2962          final CompletableFuture<Integer> f = new CompletableFuture<>();
2963          final FailingCompletableFutureFunction r
2964              = new FailingCompletableFutureFunction(m);
2965 <        if (!createIncomplete) f.complete(v1);
2965 >        if (!createIncomplete) assertTrue(f.complete(v1));
2966          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2967 <        if (createIncomplete) f.complete(v1);
2967 >        if (createIncomplete) assertTrue(f.complete(v1));
2968  
2969          checkCompletedWithWrappedCFException(g);
2970          checkCompletedNormally(f, v1);
# Line 2599 | Line 2991 | public class CompletableFutureTest exten
2991          checkCancelled(f);
2992      }}
2993  
2994 +    /**
2995 +     * thenCompose result completes exceptionally if the result of the action does
2996 +     */
2997 +    public void testThenCompose_actionReturnsFailingFuture() {
2998 +        for (ExecutionMode m : ExecutionMode.values())
2999 +        for (int order = 0; order < 6; order++)
3000 +        for (Integer v1 : new Integer[] { 1, null })
3001 +    {
3002 +        final CFException ex = new CFException();
3003 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3004 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
3005 +        final CompletableFuture<Integer> h;
3006 +        // Test all permutations of orders
3007 +        switch (order) {
3008 +        case 0:
3009 +            assertTrue(f.complete(v1));
3010 +            assertTrue(g.completeExceptionally(ex));
3011 +            h = m.thenCompose(f, (x -> g));
3012 +            break;
3013 +        case 1:
3014 +            assertTrue(f.complete(v1));
3015 +            h = m.thenCompose(f, (x -> g));
3016 +            assertTrue(g.completeExceptionally(ex));
3017 +            break;
3018 +        case 2:
3019 +            assertTrue(g.completeExceptionally(ex));
3020 +            assertTrue(f.complete(v1));
3021 +            h = m.thenCompose(f, (x -> g));
3022 +            break;
3023 +        case 3:
3024 +            assertTrue(g.completeExceptionally(ex));
3025 +            h = m.thenCompose(f, (x -> g));
3026 +            assertTrue(f.complete(v1));
3027 +            break;
3028 +        case 4:
3029 +            h = m.thenCompose(f, (x -> g));
3030 +            assertTrue(f.complete(v1));
3031 +            assertTrue(g.completeExceptionally(ex));
3032 +            break;
3033 +        case 5:
3034 +            h = m.thenCompose(f, (x -> g));
3035 +            assertTrue(f.complete(v1));
3036 +            assertTrue(g.completeExceptionally(ex));
3037 +            break;
3038 +        default: throw new AssertionError();
3039 +        }
3040 +
3041 +        checkCompletedExceptionally(g, ex);
3042 +        checkCompletedWithWrappedException(h, ex);
3043 +        checkCompletedNormally(f, v1);
3044 +    }}
3045 +
3046      // other static methods
3047  
3048      /**
# Line 2615 | Line 3059 | public class CompletableFutureTest exten
3059       * when all components complete normally
3060       */
3061      public void testAllOf_normal() throws Exception {
3062 <        for (int k = 1; k < 20; ++k) {
3062 >        for (int k = 1; k < 10; k++) {
3063              CompletableFuture<Integer>[] fs
3064                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3065 <            for (int i = 0; i < k; ++i)
3065 >            for (int i = 0; i < k; i++)
3066                  fs[i] = new CompletableFuture<>();
3067              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3068 <            for (int i = 0; i < k; ++i) {
3068 >            for (int i = 0; i < k; i++) {
3069                  checkIncomplete(f);
3070                  checkIncomplete(CompletableFuture.allOf(fs));
3071                  fs[i].complete(one);
# Line 2632 | Line 3076 | public class CompletableFutureTest exten
3076      }
3077  
3078      public void testAllOf_backwards() throws Exception {
3079 <        for (int k = 1; k < 20; ++k) {
3079 >        for (int k = 1; k < 10; k++) {
3080              CompletableFuture<Integer>[] fs
3081                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3082 <            for (int i = 0; i < k; ++i)
3082 >            for (int i = 0; i < k; i++)
3083                  fs[i] = new CompletableFuture<>();
3084              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3085              for (int i = k - 1; i >= 0; i--) {
# Line 2648 | Line 3092 | public class CompletableFutureTest exten
3092          }
3093      }
3094  
3095 +    public void testAllOf_exceptional() throws Exception {
3096 +        for (int k = 1; k < 10; k++) {
3097 +            CompletableFuture<Integer>[] fs
3098 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3099 +            CFException ex = new CFException();
3100 +            for (int i = 0; i < k; i++)
3101 +                fs[i] = new CompletableFuture<>();
3102 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3103 +            for (int i = 0; i < k; i++) {
3104 +                checkIncomplete(f);
3105 +                checkIncomplete(CompletableFuture.allOf(fs));
3106 +                if (i != k/2) {
3107 +                    fs[i].complete(i);
3108 +                    checkCompletedNormally(fs[i], i);
3109 +                } else {
3110 +                    fs[i].completeExceptionally(ex);
3111 +                    checkCompletedExceptionally(fs[i], ex);
3112 +                }
3113 +            }
3114 +            checkCompletedWithWrappedException(f, ex);
3115 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3116 +        }
3117 +    }
3118 +
3119      /**
3120       * anyOf(no component futures) returns an incomplete future
3121       */
3122      public void testAnyOf_empty() throws Exception {
3123 +        for (Integer v1 : new Integer[] { 1, null })
3124 +    {
3125          CompletableFuture<Object> f = CompletableFuture.anyOf();
3126          checkIncomplete(f);
3127 <    }
3127 >
3128 >        f.complete(v1);
3129 >        checkCompletedNormally(f, v1);
3130 >    }}
3131  
3132      /**
3133       * anyOf returns a future completed normally with a value when
3134       * a component future does
3135       */
3136      public void testAnyOf_normal() throws Exception {
3137 <        for (int k = 0; k < 10; ++k) {
3137 >        for (int k = 0; k < 10; k++) {
3138              CompletableFuture[] fs = new CompletableFuture[k];
3139 <            for (int i = 0; i < k; ++i)
3139 >            for (int i = 0; i < k; i++)
3140                  fs[i] = new CompletableFuture<>();
3141              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3142              checkIncomplete(f);
3143 <            for (int i = 0; i < k; ++i) {
3144 <                fs[i].complete(one);
3145 <                checkCompletedNormally(f, one);
3146 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3143 >            for (int i = 0; i < k; i++) {
3144 >                fs[i].complete(i);
3145 >                checkCompletedNormally(f, 0);
3146 >                int x = (int) CompletableFuture.anyOf(fs).join();
3147 >                assertTrue(0 <= x && x <= i);
3148 >            }
3149 >        }
3150 >    }
3151 >    public void testAnyOf_normal_backwards() throws Exception {
3152 >        for (int k = 0; k < 10; k++) {
3153 >            CompletableFuture[] fs = new CompletableFuture[k];
3154 >            for (int i = 0; i < k; i++)
3155 >                fs[i] = new CompletableFuture<>();
3156 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3157 >            checkIncomplete(f);
3158 >            for (int i = k - 1; i >= 0; i--) {
3159 >                fs[i].complete(i);
3160 >                checkCompletedNormally(f, k - 1);
3161 >                int x = (int) CompletableFuture.anyOf(fs).join();
3162 >                assertTrue(i <= x && x <= k - 1);
3163              }
3164          }
3165      }
# Line 2679 | Line 3168 | public class CompletableFutureTest exten
3168       * anyOf result completes exceptionally when any component does.
3169       */
3170      public void testAnyOf_exceptional() throws Exception {
3171 <        for (int k = 0; k < 10; ++k) {
3171 >        for (int k = 0; k < 10; k++) {
3172              CompletableFuture[] fs = new CompletableFuture[k];
3173 <            for (int i = 0; i < k; ++i)
3173 >            CFException[] exs = new CFException[k];
3174 >            for (int i = 0; i < k; i++) {
3175                  fs[i] = new CompletableFuture<>();
3176 +                exs[i] = new CFException();
3177 +            }
3178              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3179              checkIncomplete(f);
3180 <            for (int i = 0; i < k; ++i) {
3181 <                fs[i].completeExceptionally(new CFException());
3182 <                checkCompletedWithWrappedCFException(f);
3180 >            for (int i = 0; i < k; i++) {
3181 >                fs[i].completeExceptionally(exs[i]);
3182 >                checkCompletedWithWrappedException(f, exs[0]);
3183 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3184 >            }
3185 >        }
3186 >    }
3187 >
3188 >    public void testAnyOf_exceptional_backwards() throws Exception {
3189 >        for (int k = 0; k < 10; k++) {
3190 >            CompletableFuture[] fs = new CompletableFuture[k];
3191 >            CFException[] exs = new CFException[k];
3192 >            for (int i = 0; i < k; i++) {
3193 >                fs[i] = new CompletableFuture<>();
3194 >                exs[i] = new CFException();
3195 >            }
3196 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3197 >            checkIncomplete(f);
3198 >            for (int i = k - 1; i >= 0; i--) {
3199 >                fs[i].completeExceptionally(exs[i]);
3200 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3201                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3202              }
3203          }
# Line 2700 | Line 3210 | public class CompletableFutureTest exten
3210          CompletableFuture<Integer> f = new CompletableFuture<>();
3211          CompletableFuture<Integer> g = new CompletableFuture<>();
3212          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2703        CompletableFuture<?> h;
3213          ThreadExecutor exec = new ThreadExecutor();
3214  
3215          Runnable[] throwingActions = {
3216              () -> CompletableFuture.supplyAsync(null),
3217              () -> CompletableFuture.supplyAsync(null, exec),
3218 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3218 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3219  
3220              () -> CompletableFuture.runAsync(null),
3221              () -> CompletableFuture.runAsync(null, exec),
# Line 2811 | Line 3320 | public class CompletableFutureTest exten
3320          assertSame(f, f.toCompletableFuture());
3321      }
3322  
3323 <    /**
2815 <     * whenComplete action executes on normal completion, propagating
2816 <     * source result.
2817 <     */
2818 <    public void testWhenComplete_normalCompletion1() {
2819 <        for (ExecutionMode m : ExecutionMode.values())
2820 <        for (boolean createIncomplete : new boolean[] { true, false })
2821 <        for (Integer v1 : new Integer[] { 1, null })
2822 <    {
2823 <        final AtomicInteger a = new AtomicInteger(0);
2824 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2825 <        if (!createIncomplete) f.complete(v1);
2826 <        final CompletableFuture<Integer> g = m.whenComplete
2827 <            (f,
2828 <             (Integer x, Throwable t) -> {
2829 <                threadAssertSame(x, v1);
2830 <                threadAssertNull(t);
2831 <                a.getAndIncrement();
2832 <            });
2833 <        if (createIncomplete) f.complete(v1);
3323 >    //--- tests of implementation details; not part of official tck ---
3324  
3325 <        checkCompletedNormally(g, v1);
3326 <        checkCompletedNormally(f, v1);
3327 <        assertEquals(1, a.get());
3328 <    }}
3325 >    Object resultOf(CompletableFuture<?> f) {
3326 >        try {
3327 >            java.lang.reflect.Field resultField
3328 >                = CompletableFuture.class.getDeclaredField("result");
3329 >            resultField.setAccessible(true);
3330 >            return resultField.get(f);
3331 >        } catch (Throwable t) { throw new AssertionError(t); }
3332 >    }
3333  
3334 <    /**
3335 <     * whenComplete action executes on exceptional completion, propagating
2842 <     * source result.
2843 <     */
2844 <    public void testWhenComplete_exceptionalCompletion() {
3334 >    public void testExceptionPropagationReusesResultObject() {
3335 >        if (!testImplementationDetails) return;
3336          for (ExecutionMode m : ExecutionMode.values())
2846        for (boolean createIncomplete : new boolean[] { true, false })
2847        for (Integer v1 : new Integer[] { 1, null })
3337      {
2849        final AtomicInteger a = new AtomicInteger(0);
3338          final CFException ex = new CFException();
3339 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3340 <        if (!createIncomplete) f.completeExceptionally(ex);
2853 <        final CompletableFuture<Integer> g = m.whenComplete
2854 <            (f,
2855 <             (Integer x, Throwable t) -> {
2856 <                threadAssertNull(x);
2857 <                threadAssertSame(t, ex);
2858 <                a.getAndIncrement();
2859 <            });
2860 <        if (createIncomplete) f.completeExceptionally(ex);
2861 <        checkCompletedWithWrappedCFException(f, ex);
2862 <        checkCompletedWithWrappedCFException(g, ex);
2863 <        assertEquals(1, a.get());
2864 <    }}
3339 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3340 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3341  
3342 <    /**
3343 <     * whenComplete action executes on cancelled source, propagating
2868 <     * CancellationException.
2869 <     */
2870 <    public void testWhenComplete_sourceCancelled() {
2871 <        for (ExecutionMode m : ExecutionMode.values())
2872 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2873 <        for (boolean createIncomplete : new boolean[] { true, false })
2874 <    {
2875 <        final AtomicInteger a = new AtomicInteger(0);
2876 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2877 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2878 <        final CompletableFuture<Integer> g = m.whenComplete
2879 <            (f,
2880 <             (Integer x, Throwable t) -> {
2881 <                threadAssertNull(x);
2882 <                threadAssertTrue(t instanceof CancellationException);
2883 <                a.getAndIncrement();
2884 <            });
2885 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3342 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3343 >            = new ArrayList<>();
3344  
3345 <        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
3346 <        checkCompletedWithWrappedCancellationException(g);
3347 <        checkCancelled(f);
2890 <        assertEquals(1, a.get());
2891 <    }}
3345 >        funs.add((y) -> m.thenRun(y, new Noop(m)));
3346 >        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3347 >        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3348  
3349 <    /**
3350 <     * If a whenComplete action throws an exception when triggered by
3351 <     * a normal completion, it completes exceptionally
2896 <     */
2897 <    public void testWhenComplete_actionFailed() {
2898 <        for (boolean createIncomplete : new boolean[] { true, false })
2899 <        for (ExecutionMode m : ExecutionMode.values())
2900 <        for (Integer v1 : new Integer[] { 1, null })
2901 <    {
2902 <        final AtomicInteger a = new AtomicInteger(0);
2903 <        final CFException ex = new CFException();
2904 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2905 <        if (!createIncomplete) f.complete(v1);
2906 <        final CompletableFuture<Integer> g = m.whenComplete
2907 <            (f,
2908 <             (Integer x, Throwable t) -> {
2909 <                threadAssertSame(x, v1);
2910 <                threadAssertNull(t);
2911 <                a.getAndIncrement();
2912 <                throw ex;
2913 <            });
2914 <        if (createIncomplete) f.complete(v1);
2915 <        checkCompletedNormally(f, v1);
2916 <        checkCompletedWithWrappedCFException(g, ex);
2917 <        assertEquals(1, a.get());
2918 <    }}
3349 >        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3350 >        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3351 >        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3352  
3353 <    /**
3354 <     * If a whenComplete action throws an exception when triggered by
3355 <     * a source completion that also throws an exception, the source
2923 <     * exception takes precedence.
2924 <     */
2925 <    public void testWhenComplete_actionFailedSourceFailed() {
2926 <        for (boolean createIncomplete : new boolean[] { true, false })
2927 <        for (ExecutionMode m : ExecutionMode.values())
2928 <        for (Integer v1 : new Integer[] { 1, null })
2929 <    {
2930 <        final AtomicInteger a = new AtomicInteger(0);
2931 <        final CFException ex1 = new CFException();
2932 <        final CFException ex2 = new CFException();
2933 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3353 >        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3354 >        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3355 >        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3356  
3357 <        if (!createIncomplete) f.completeExceptionally(ex1);
2936 <        final CompletableFuture<Integer> g = m.whenComplete
2937 <            (f,
2938 <             (Integer x, Throwable t) -> {
2939 <                threadAssertSame(t, ex1);
2940 <                threadAssertNull(x);
2941 <                a.getAndIncrement();
2942 <                throw ex2;
2943 <            });
2944 <        if (createIncomplete) f.completeExceptionally(ex1);
3357 >        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3358  
3359 <        checkCompletedWithWrappedCFException(f, ex1);
3360 <        checkCompletedWithWrappedCFException(g, ex1);
3361 <        assertEquals(1, a.get());
3359 >        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3360 >
3361 >        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3362 >        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3363 >
3364 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3365 >                 fun : funs) {
3366 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3367 >            f.completeExceptionally(ex);
3368 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3369 >            checkCompletedWithWrappedException(src, ex);
3370 >            CompletableFuture<?> dep = fun.apply(src);
3371 >            checkCompletedWithWrappedException(dep, ex);
3372 >            assertSame(resultOf(src), resultOf(dep));
3373 >        }
3374 >
3375 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3376 >                 fun : funs) {
3377 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3378 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3379 >            CompletableFuture<?> dep = fun.apply(src);
3380 >            f.completeExceptionally(ex);
3381 >            checkCompletedWithWrappedException(src, ex);
3382 >            checkCompletedWithWrappedException(dep, ex);
3383 >            assertSame(resultOf(src), resultOf(dep));
3384 >        }
3385 >
3386 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3387 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3388 >                 fun : funs) {
3389 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3390 >            f.cancel(mayInterruptIfRunning);
3391 >            checkCancelled(f);
3392 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3393 >            checkCompletedWithWrappedCancellationException(src);
3394 >            CompletableFuture<?> dep = fun.apply(src);
3395 >            checkCompletedWithWrappedCancellationException(dep);
3396 >            assertSame(resultOf(src), resultOf(dep));
3397 >        }
3398 >
3399 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3400 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3401 >                 fun : funs) {
3402 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3403 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3404 >            CompletableFuture<?> dep = fun.apply(src);
3405 >            f.cancel(mayInterruptIfRunning);
3406 >            checkCancelled(f);
3407 >            checkCompletedWithWrappedCancellationException(src);
3408 >            checkCompletedWithWrappedCancellationException(dep);
3409 >            assertSame(resultOf(src), resultOf(dep));
3410 >        }
3411      }}
3412  
3413   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines