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.51 by jsr166, Mon Jun 2 19:49:28 2014 UTC vs.
Revision 1.99 by jsr166, Wed Jan 7 07:59:20 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  
# 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));
277 +        assertTrue(f.cancel(true));
278          checkCancelled(f);
279 <    }
279 >    }}
280  
281      /**
282       * obtrudeValue forces completion with given value
# Line 240 | Line 284 | public class CompletableFutureTest exten
284      public void testObtrudeValue() {
285          CompletableFuture<Integer> f = new CompletableFuture<>();
286          checkIncomplete(f);
287 <        f.complete(one);
287 >        assertTrue(f.complete(one));
288          checkCompletedNormally(f, one);
289          f.obtrudeValue(three);
290          checkCompletedNormally(f, three);
# Line 261 | Line 305 | public class CompletableFutureTest exten
305       * obtrudeException forces completion with given exception
306       */
307      public void testObtrudeException() {
308 <        CompletableFuture<Integer> f = new CompletableFuture<>();
309 <        checkIncomplete(f);
310 <        f.complete(one);
311 <        checkCompletedNormally(f, one);
312 <        f.obtrudeException(new CFException());
269 <        checkCompletedWithWrappedCFException(f);
308 >        for (Integer v1 : new Integer[] { 1, null })
309 >    {
310 >        CFException ex;
311 >        CompletableFuture<Integer> f;
312 >
313          f = new CompletableFuture<>();
314 <        f.obtrudeException(new CFException());
315 <        checkCompletedWithWrappedCFException(f);
314 >        assertTrue(f.complete(v1));
315 >        for (int i = 0; i < 2; i++) {
316 >            f.obtrudeException(ex = new CFException());
317 >            checkCompletedExceptionally(f, ex);
318 >        }
319 >
320 >        f = new CompletableFuture<>();
321 >        for (int i = 0; i < 2; i++) {
322 >            f.obtrudeException(ex = new CFException());
323 >            checkCompletedExceptionally(f, ex);
324 >        }
325 >
326          f = new CompletableFuture<>();
327 +        f.completeExceptionally(ex = new CFException());
328 +        f.obtrudeValue(v1);
329 +        checkCompletedNormally(f, v1);
330 +        f.obtrudeException(ex = new CFException());
331 +        checkCompletedExceptionally(f, ex);
332          f.completeExceptionally(new CFException());
333 <        f.obtrudeValue(four);
334 <        checkCompletedNormally(f, four);
335 <        f.obtrudeException(new CFException());
336 <        checkCompletedWithWrappedCFException(f);
279 <    }
333 >        checkCompletedExceptionally(f, ex);
334 >        assertFalse(f.complete(v1));
335 >        checkCompletedExceptionally(f, ex);
336 >    }}
337  
338      /**
339       * getNumberOfDependents returns number of dependent tasks
340       */
341      public void testGetNumberOfDependents() {
342 +        for (ExecutionMode m : ExecutionMode.values())
343 +        for (Integer v1 : new Integer[] { 1, null })
344 +    {
345          CompletableFuture<Integer> f = new CompletableFuture<>();
346          assertEquals(0, f.getNumberOfDependents());
347 <        CompletableFuture g = f.thenRun(new Noop());
347 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
348          assertEquals(1, f.getNumberOfDependents());
349          assertEquals(0, g.getNumberOfDependents());
350 <        CompletableFuture h = f.thenRun(new Noop());
350 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
351          assertEquals(2, f.getNumberOfDependents());
352 <        f.complete(1);
352 >        assertEquals(0, h.getNumberOfDependents());
353 >        assertTrue(f.complete(v1));
354          checkCompletedNormally(g, null);
355 +        checkCompletedNormally(h, null);
356          assertEquals(0, f.getNumberOfDependents());
357          assertEquals(0, g.getNumberOfDependents());
358 <    }
358 >        assertEquals(0, h.getNumberOfDependents());
359 >    }}
360  
361      /**
362       * toString indicates current completion state
# Line 304 | Line 367 | public class CompletableFutureTest exten
367          f = new CompletableFuture<String>();
368          assertTrue(f.toString().contains("[Not completed]"));
369  
370 <        f.complete("foo");
370 >        assertTrue(f.complete("foo"));
371          assertTrue(f.toString().contains("[Completed normally]"));
372  
373          f = new CompletableFuture<String>();
374 <        f.completeExceptionally(new IndexOutOfBoundsException());
374 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
375          assertTrue(f.toString().contains("[Completed exceptionally]"));
376 +
377 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
378 +            f = new CompletableFuture<String>();
379 +            assertTrue(f.cancel(mayInterruptIfRunning));
380 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
381 +        }
382      }
383  
384      /**
# Line 320 | Line 389 | public class CompletableFutureTest exten
389          checkCompletedNormally(f, "test");
390      }
391  
392 <    // Choose non-commutative actions for better coverage
392 >    abstract class CheckedAction {
393 >        int invocationCount = 0;
394 >        final ExecutionMode m;
395 >        CheckedAction(ExecutionMode m) { this.m = m; }
396 >        void invoked() {
397 >            m.checkExecutionMode();
398 >            assertEquals(0, invocationCount++);
399 >        }
400 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
401 >        void assertInvoked() { assertEquals(1, invocationCount); }
402 >    }
403  
404 <    // A non-commutative function that handles and produces null values as well.
405 <    static Integer subtract(Integer x, Integer y) {
406 <        return (x == null && y == null) ? null :
407 <            ((x == null) ? 42 : x.intValue())
408 <            - ((y == null) ? 99 : y.intValue());
404 >    abstract class CheckedIntegerAction extends CheckedAction {
405 >        Integer value;
406 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
407 >        void assertValue(Integer expected) {
408 >            assertInvoked();
409 >            assertEquals(expected, value);
410 >        }
411 >    }
412 >
413 >    class IntegerSupplier extends CheckedAction
414 >        implements Supplier<Integer>
415 >    {
416 >        final Integer value;
417 >        IntegerSupplier(ExecutionMode m, Integer value) {
418 >            super(m);
419 >            this.value = value;
420 >        }
421 >        public Integer get() {
422 >            invoked();
423 >            return value;
424 >        }
425      }
426  
427      // A function that handles and produces null values as well.
# Line 334 | Line 429 | public class CompletableFutureTest exten
429          return (x == null) ? null : x + 1;
430      }
431  
432 <    static final Supplier<Integer> supplyOne =
433 <        () -> Integer.valueOf(1);
434 <    static final Function<Integer, Integer> inc =
435 <        (Integer x) -> Integer.valueOf(x.intValue() + 1);
341 <    static final BiFunction<Integer, Integer, Integer> subtract =
342 <        (Integer x, Integer y) -> subtract(x, y);
343 <    static final class IncAction implements Consumer<Integer> {
344 <        int invocationCount = 0;
345 <        Integer value;
432 >    class NoopConsumer extends CheckedIntegerAction
433 >        implements Consumer<Integer>
434 >    {
435 >        NoopConsumer(ExecutionMode m) { super(m); }
436          public void accept(Integer x) {
437 <            invocationCount++;
438 <            value = inc(x);
437 >            invoked();
438 >            value = x;
439          }
440      }
441 <    static final class IncFunction implements Function<Integer,Integer> {
442 <        int invocationCount = 0;
443 <        Integer value;
441 >
442 >    class IncFunction extends CheckedIntegerAction
443 >        implements Function<Integer,Integer>
444 >    {
445 >        IncFunction(ExecutionMode m) { super(m); }
446          public Integer apply(Integer x) {
447 <            invocationCount++;
447 >            invoked();
448              return value = inc(x);
449          }
450      }
451 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
452 <        int invocationCount = 0;
453 <        Integer value;
454 <        // Check this action was invoked exactly once when result is computed.
451 >
452 >    // Choose non-commutative actions for better coverage
453 >    // A non-commutative function that handles and produces null values as well.
454 >    static Integer subtract(Integer x, Integer y) {
455 >        return (x == null && y == null) ? null :
456 >            ((x == null) ? 42 : x.intValue())
457 >            - ((y == null) ? 99 : y.intValue());
458 >    }
459 >
460 >    class SubtractAction extends CheckedIntegerAction
461 >        implements BiConsumer<Integer, Integer>
462 >    {
463 >        SubtractAction(ExecutionMode m) { super(m); }
464          public void accept(Integer x, Integer y) {
465 <            invocationCount++;
465 >            invoked();
466              value = subtract(x, y);
467          }
468      }
469 <    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
470 <        int invocationCount = 0;
471 <        Integer value;
472 <        // Check this action was invoked exactly once when result is computed.
469 >
470 >    class SubtractFunction extends CheckedIntegerAction
471 >        implements BiFunction<Integer, Integer, Integer>
472 >    {
473 >        SubtractFunction(ExecutionMode m) { super(m); }
474          public Integer apply(Integer x, Integer y) {
475 <            invocationCount++;
475 >            invoked();
476              return value = subtract(x, y);
477          }
478      }
479 <    static final class Noop implements Runnable {
480 <        int invocationCount = 0;
479 >
480 >    class Noop extends CheckedAction implements Runnable {
481 >        Noop(ExecutionMode m) { super(m); }
482          public void run() {
483 <            invocationCount++;
483 >            invoked();
484          }
485      }
486  
487 <    static final class FailingSupplier implements Supplier<Integer> {
488 <        int invocationCount = 0;
487 >    class FailingSupplier extends CheckedAction
488 >        implements Supplier<Integer>
489 >    {
490 >        FailingSupplier(ExecutionMode m) { super(m); }
491          public Integer get() {
492 <            invocationCount++;
492 >            invoked();
493              throw new CFException();
494          }
495      }
496 <    static final class FailingConsumer implements Consumer<Integer> {
497 <        int invocationCount = 0;
496 >
497 >    class FailingConsumer extends CheckedIntegerAction
498 >        implements Consumer<Integer>
499 >    {
500 >        FailingConsumer(ExecutionMode m) { super(m); }
501          public void accept(Integer x) {
502 <            invocationCount++;
502 >            invoked();
503 >            value = x;
504              throw new CFException();
505          }
506      }
507 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
508 <        int invocationCount = 0;
507 >
508 >    class FailingBiConsumer extends CheckedIntegerAction
509 >        implements BiConsumer<Integer, Integer>
510 >    {
511 >        FailingBiConsumer(ExecutionMode m) { super(m); }
512          public void accept(Integer x, Integer y) {
513 <            invocationCount++;
513 >            invoked();
514 >            value = subtract(x, y);
515              throw new CFException();
516          }
517      }
518 <    static final class FailingFunction implements Function<Integer, Integer> {
519 <        int invocationCount = 0;
518 >
519 >    class FailingFunction extends CheckedIntegerAction
520 >        implements Function<Integer, Integer>
521 >    {
522 >        FailingFunction(ExecutionMode m) { super(m); }
523          public Integer apply(Integer x) {
524 <            invocationCount++;
524 >            invoked();
525 >            value = x;
526              throw new CFException();
527          }
528      }
529 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
530 <        int invocationCount = 0;
529 >
530 >    class FailingBiFunction extends CheckedIntegerAction
531 >        implements BiFunction<Integer, Integer, Integer>
532 >    {
533 >        FailingBiFunction(ExecutionMode m) { super(m); }
534          public Integer apply(Integer x, Integer y) {
535 <            invocationCount++;
535 >            invoked();
536 >            value = subtract(x, y);
537              throw new CFException();
538          }
539      }
540 <    static final class FailingRunnable implements Runnable {
541 <        int invocationCount = 0;
540 >
541 >    class FailingRunnable extends CheckedAction implements Runnable {
542 >        FailingRunnable(ExecutionMode m) { super(m); }
543          public void run() {
544 <            invocationCount++;
544 >            invoked();
545              throw new CFException();
546          }
547      }
548  
549 <    static final class CompletableFutureInc
550 <        implements Function<Integer, CompletableFuture<Integer>> {
551 <        int invocationCount = 0;
549 >
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 <            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 <        int invocationCount = 0;
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 <            invocationCount++;
568 >            invoked();
569 >            value = x;
570              throw new CFException();
571          }
572      }
573  
574      // Used for explicit executor tests
575      static final class ThreadExecutor implements Executor {
576 <        AtomicInteger count = new AtomicInteger(0);
576 >        final AtomicInteger count = new AtomicInteger(0);
577 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
578 >        static boolean startedCurrentThread() {
579 >            return Thread.currentThread().getThreadGroup() == tg;
580 >        }
581  
582          public void execute(Runnable r) {
583              count.getAndIncrement();
584 <            new Thread(r).start();
584 >            new Thread(tg, r).start();
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 repeating all the testing code.
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());
600              }
601 +            public CompletableFuture<Void> runAsync(Runnable a) {
602 +                throw new UnsupportedOperationException();
603 +            }
604 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
605 +                throw new UnsupportedOperationException();
606 +            }
607              public <T> CompletableFuture<Void> thenRun
608                  (CompletableFuture<T> f, Runnable a) {
609                  return f.thenRun(a);
# Line 528 | 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);
677 >            }
678 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
679 >                return CompletableFuture.supplyAsync(a);
680              }
681              public <T> CompletableFuture<Void> thenRun
682                  (CompletableFuture<T> f, Runnable a) {
# Line 596 | Line 743 | public class CompletableFutureTest exten
743  
744          EXECUTOR {
745              public void checkExecutionMode() {
746 <                //TODO
746 >                assertTrue(ThreadExecutor.startedCurrentThread());
747 >            }
748 >            public CompletableFuture<Void> runAsync(Runnable a) {
749 >                return CompletableFuture.runAsync(a, new ThreadExecutor());
750 >            }
751 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
752 >                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
753              }
754              public <T> CompletableFuture<Void> thenRun
755                  (CompletableFuture<T> f, Runnable a) {
# Line 662 | Line 815 | public class CompletableFutureTest exten
815          };
816  
817          public abstract void checkExecutionMode();
818 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
819 +        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
820          public abstract <T> CompletableFuture<Void> thenRun
821              (CompletableFuture<T> f, Runnable a);
822          public abstract <T> CompletableFuture<Void> thenAccept
# Line 711 | 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  
728
883      /**
884       * exceptionally action completes with function value on source
885       * exception
# Line 740 | Line 894 | public class CompletableFutureTest exten
894          if (!createIncomplete) f.completeExceptionally(ex);
895          final CompletableFuture<Integer> g = f.exceptionally
896              ((Throwable t) -> {
897 +                ExecutionMode.SYNC.checkExecutionMode();
898                  threadAssertSame(t, ex);
899                  a.getAndIncrement();
900                  return v1;
# Line 761 | Line 916 | public class CompletableFutureTest exten
916          if (!createIncomplete) f.completeExceptionally(ex1);
917          final CompletableFuture<Integer> g = f.exceptionally
918              ((Throwable t) -> {
919 +                ExecutionMode.SYNC.checkExecutionMode();
920                  threadAssertSame(t, ex1);
921                  a.getAndIncrement();
922                  throw ex2;
923              });
924          if (createIncomplete) f.completeExceptionally(ex1);
925  
926 <        checkCompletedWithWrappedCFException(g, ex2);
926 >        checkCompletedWithWrappedException(g, ex2);
927 >        assertEquals(1, a.get());
928 >    }}
929 >
930 >    /**
931 >     * whenComplete action executes on normal completion, propagating
932 >     * source result.
933 >     */
934 >    public void testWhenComplete_normalCompletion1() {
935 >        for (ExecutionMode m : ExecutionMode.values())
936 >        for (boolean createIncomplete : new boolean[] { true, false })
937 >        for (Integer v1 : new Integer[] { 1, null })
938 >    {
939 >        final AtomicInteger a = new AtomicInteger(0);
940 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
941 >        if (!createIncomplete) assertTrue(f.complete(v1));
942 >        final CompletableFuture<Integer> g = m.whenComplete
943 >            (f,
944 >             (Integer x, Throwable t) -> {
945 >                m.checkExecutionMode();
946 >                threadAssertSame(x, v1);
947 >                threadAssertNull(t);
948 >                a.getAndIncrement();
949 >            });
950 >        if (createIncomplete) assertTrue(f.complete(v1));
951 >
952 >        checkCompletedNormally(g, v1);
953 >        checkCompletedNormally(f, v1);
954 >        assertEquals(1, a.get());
955 >    }}
956 >
957 >    /**
958 >     * whenComplete action executes on exceptional completion, propagating
959 >     * source result.
960 >     */
961 >    public void testWhenComplete_exceptionalCompletion() {
962 >        for (ExecutionMode m : ExecutionMode.values())
963 >        for (boolean createIncomplete : new boolean[] { true, false })
964 >        for (Integer v1 : new Integer[] { 1, null })
965 >    {
966 >        final AtomicInteger a = new AtomicInteger(0);
967 >        final CFException ex = new CFException();
968 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
969 >        if (!createIncomplete) f.completeExceptionally(ex);
970 >        final CompletableFuture<Integer> g = m.whenComplete
971 >            (f,
972 >             (Integer x, Throwable t) -> {
973 >                m.checkExecutionMode();
974 >                threadAssertNull(x);
975 >                threadAssertSame(t, ex);
976 >                a.getAndIncrement();
977 >            });
978 >        if (createIncomplete) f.completeExceptionally(ex);
979 >
980 >        checkCompletedWithWrappedException(g, ex);
981 >        checkCompletedExceptionally(f, ex);
982 >        assertEquals(1, a.get());
983 >    }}
984 >
985 >    /**
986 >     * whenComplete action executes on cancelled source, propagating
987 >     * CancellationException.
988 >     */
989 >    public void testWhenComplete_sourceCancelled() {
990 >        for (ExecutionMode m : ExecutionMode.values())
991 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
992 >        for (boolean createIncomplete : new boolean[] { true, false })
993 >    {
994 >        final AtomicInteger a = new AtomicInteger(0);
995 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
996 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
997 >        final CompletableFuture<Integer> g = m.whenComplete
998 >            (f,
999 >             (Integer x, Throwable t) -> {
1000 >                m.checkExecutionMode();
1001 >                threadAssertNull(x);
1002 >                threadAssertTrue(t instanceof CancellationException);
1003 >                a.getAndIncrement();
1004 >            });
1005 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1006 >
1007 >        checkCompletedWithWrappedCancellationException(g);
1008 >        checkCancelled(f);
1009 >        assertEquals(1, a.get());
1010 >    }}
1011 >
1012 >    /**
1013 >     * If a whenComplete action throws an exception when triggered by
1014 >     * a normal completion, it completes exceptionally
1015 >     */
1016 >    public void testWhenComplete_actionFailed() {
1017 >        for (boolean createIncomplete : new boolean[] { true, false })
1018 >        for (ExecutionMode m : ExecutionMode.values())
1019 >        for (Integer v1 : new Integer[] { 1, null })
1020 >    {
1021 >        final AtomicInteger a = new AtomicInteger(0);
1022 >        final CFException ex = new CFException();
1023 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1024 >        if (!createIncomplete) assertTrue(f.complete(v1));
1025 >        final CompletableFuture<Integer> g = m.whenComplete
1026 >            (f,
1027 >             (Integer x, Throwable t) -> {
1028 >                m.checkExecutionMode();
1029 >                threadAssertSame(x, v1);
1030 >                threadAssertNull(t);
1031 >                a.getAndIncrement();
1032 >                throw ex;
1033 >            });
1034 >        if (createIncomplete) assertTrue(f.complete(v1));
1035 >
1036 >        checkCompletedWithWrappedException(g, ex);
1037 >        checkCompletedNormally(f, v1);
1038 >        assertEquals(1, a.get());
1039 >    }}
1040 >
1041 >    /**
1042 >     * If a whenComplete action throws an exception when triggered by
1043 >     * a source completion that also throws an exception, the source
1044 >     * exception takes precedence.
1045 >     */
1046 >    public void testWhenComplete_actionFailedSourceFailed() {
1047 >        for (boolean createIncomplete : new boolean[] { true, false })
1048 >        for (ExecutionMode m : ExecutionMode.values())
1049 >        for (Integer v1 : new Integer[] { 1, null })
1050 >    {
1051 >        final AtomicInteger a = new AtomicInteger(0);
1052 >        final CFException ex1 = new CFException();
1053 >        final CFException ex2 = new CFException();
1054 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1055 >
1056 >        if (!createIncomplete) f.completeExceptionally(ex1);
1057 >        final CompletableFuture<Integer> g = m.whenComplete
1058 >            (f,
1059 >             (Integer x, Throwable t) -> {
1060 >                m.checkExecutionMode();
1061 >                threadAssertSame(t, ex1);
1062 >                threadAssertNull(x);
1063 >                a.getAndIncrement();
1064 >                throw ex2;
1065 >            });
1066 >        if (createIncomplete) f.completeExceptionally(ex1);
1067 >
1068 >        checkCompletedWithWrappedException(g, ex1);
1069 >        checkCompletedExceptionally(f, ex1);
1070          assertEquals(1, a.get());
1071      }}
1072  
# Line 782 | Line 1081 | public class CompletableFutureTest exten
1081      {
1082          final CompletableFuture<Integer> f = new CompletableFuture<>();
1083          final AtomicInteger a = new AtomicInteger(0);
1084 <        if (!createIncomplete) f.complete(v1);
1084 >        if (!createIncomplete) assertTrue(f.complete(v1));
1085          final CompletableFuture<Integer> g = m.handle
1086              (f,
1087               (Integer x, Throwable t) -> {
1088 +                m.checkExecutionMode();
1089                  threadAssertSame(x, v1);
1090                  threadAssertNull(t);
1091                  a.getAndIncrement();
1092                  return inc(v1);
1093              });
1094 <        if (createIncomplete) f.complete(v1);
1094 >        if (createIncomplete) assertTrue(f.complete(v1));
1095  
1096          checkCompletedNormally(g, inc(v1));
1097          checkCompletedNormally(f, v1);
# Line 814 | Line 1114 | public class CompletableFutureTest exten
1114          final CompletableFuture<Integer> g = m.handle
1115              (f,
1116               (Integer x, Throwable t) -> {
1117 +                m.checkExecutionMode();
1118                  threadAssertNull(x);
1119                  threadAssertSame(t, ex);
1120                  a.getAndIncrement();
# Line 822 | Line 1123 | public class CompletableFutureTest exten
1123          if (createIncomplete) f.completeExceptionally(ex);
1124  
1125          checkCompletedNormally(g, v1);
1126 <        checkCompletedWithWrappedCFException(f, ex);
1126 >        checkCompletedExceptionally(f, ex);
1127          assertEquals(1, a.get());
1128      }}
1129  
# Line 842 | Line 1143 | public class CompletableFutureTest exten
1143          final CompletableFuture<Integer> g = m.handle
1144              (f,
1145               (Integer x, Throwable t) -> {
1146 +                m.checkExecutionMode();
1147                  threadAssertNull(x);
1148                  threadAssertTrue(t instanceof CancellationException);
1149                  a.getAndIncrement();
# Line 869 | Line 1171 | public class CompletableFutureTest exten
1171          final CompletableFuture<Integer> g = m.handle
1172              (f,
1173               (Integer x, Throwable t) -> {
1174 +                m.checkExecutionMode();
1175                  threadAssertNull(x);
1176                  threadAssertSame(ex1, t);
1177                  a.getAndIncrement();
# Line 876 | Line 1179 | public class CompletableFutureTest exten
1179              });
1180          if (createIncomplete) f.completeExceptionally(ex1);
1181  
1182 <        checkCompletedWithWrappedCFException(g, ex2);
1183 <        checkCompletedWithWrappedCFException(f, ex1);
1182 >        checkCompletedWithWrappedException(g, ex2);
1183 >        checkCompletedExceptionally(f, ex1);
1184          assertEquals(1, a.get());
1185      }}
1186  
# Line 889 | Line 1192 | public class CompletableFutureTest exten
1192          final CompletableFuture<Integer> f = new CompletableFuture<>();
1193          final AtomicInteger a = new AtomicInteger(0);
1194          final CFException ex = new CFException();
1195 <        if (!createIncomplete) f.complete(v1);
1195 >        if (!createIncomplete) assertTrue(f.complete(v1));
1196          final CompletableFuture<Integer> g = m.handle
1197              (f,
1198               (Integer x, Throwable t) -> {
1199 +                m.checkExecutionMode();
1200                  threadAssertSame(x, v1);
1201                  threadAssertNull(t);
1202                  a.getAndIncrement();
1203                  throw ex;
1204              });
1205 <        if (createIncomplete) f.complete(v1);
1205 >        if (createIncomplete) assertTrue(f.complete(v1));
1206  
1207 <        checkCompletedWithWrappedCFException(g, ex);
1207 >        checkCompletedWithWrappedException(g, ex);
1208          checkCompletedNormally(f, v1);
1209          assertEquals(1, a.get());
1210      }}
# Line 908 | Line 1212 | public class CompletableFutureTest exten
1212      /**
1213       * runAsync completes after running Runnable
1214       */
1215 <    public void testRunAsync() {
1216 <        Noop r = new Noop();
1217 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1218 <        assertNull(f.join());
1219 <        assertEquals(1, r.invocationCount);
1220 <        checkCompletedNormally(f, null);
1221 <    }
1222 <
1223 <    /**
920 <     * runAsync with executor completes after running Runnable
921 <     */
922 <    public void testRunAsync2() {
923 <        Noop r = new Noop();
924 <        ThreadExecutor exec = new ThreadExecutor();
925 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
1215 >    public void testRunAsync_normalCompletion() {
1216 >        ExecutionMode[] executionModes = {
1217 >            ExecutionMode.ASYNC,
1218 >            ExecutionMode.EXECUTOR,
1219 >        };
1220 >        for (ExecutionMode m : executionModes)
1221 >    {
1222 >        final Noop r = new Noop(m);
1223 >        final CompletableFuture<Void> f = m.runAsync(r);
1224          assertNull(f.join());
927        assertEquals(1, r.invocationCount);
1225          checkCompletedNormally(f, null);
1226 <        assertEquals(1, exec.count.get());
1227 <    }
1226 >        r.assertInvoked();
1227 >    }}
1228  
1229      /**
1230       * failing runAsync completes exceptionally after running Runnable
1231       */
1232 <    public void testRunAsync3() {
1233 <        FailingRunnable r = new FailingRunnable();
1234 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1232 >    public void testRunAsync_exceptionalCompletion() {
1233 >        ExecutionMode[] executionModes = {
1234 >            ExecutionMode.ASYNC,
1235 >            ExecutionMode.EXECUTOR,
1236 >        };
1237 >        for (ExecutionMode m : executionModes)
1238 >    {
1239 >        final FailingRunnable r = new FailingRunnable(m);
1240 >        final CompletableFuture<Void> f = m.runAsync(r);
1241          checkCompletedWithWrappedCFException(f);
1242 <        assertEquals(1, r.invocationCount);
1243 <    }
1242 >        r.assertInvoked();
1243 >    }}
1244  
1245      /**
1246       * supplyAsync completes with result of supplier
1247       */
1248 <    public void testSupplyAsync() {
1249 <        CompletableFuture<Integer> f;
1250 <        f = CompletableFuture.supplyAsync(supplyOne);
1251 <        assertEquals(f.join(), one);
1252 <        checkCompletedNormally(f, one);
1253 <    }
1254 <
1255 <    /**
1256 <     * supplyAsync with executor completes with result of supplier
1257 <     */
1258 <    public void testSupplyAsync2() {
1259 <        CompletableFuture<Integer> f;
1260 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
1261 <        assertEquals(f.join(), one);
959 <        checkCompletedNormally(f, one);
960 <    }
1248 >    public void testSupplyAsync_normalCompletion() {
1249 >        ExecutionMode[] executionModes = {
1250 >            ExecutionMode.ASYNC,
1251 >            ExecutionMode.EXECUTOR,
1252 >        };
1253 >        for (ExecutionMode m : executionModes)
1254 >        for (Integer v1 : new Integer[] { 1, null })
1255 >    {
1256 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1257 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1258 >        assertSame(v1, f.join());
1259 >        checkCompletedNormally(f, v1);
1260 >        r.assertInvoked();
1261 >    }}
1262  
1263      /**
1264       * Failing supplyAsync completes exceptionally
1265       */
1266 <    public void testSupplyAsync3() {
1267 <        FailingSupplier r = new FailingSupplier();
1268 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1266 >    public void testSupplyAsync_exceptionalCompletion() {
1267 >        ExecutionMode[] executionModes = {
1268 >            ExecutionMode.ASYNC,
1269 >            ExecutionMode.EXECUTOR,
1270 >        };
1271 >        for (ExecutionMode m : executionModes)
1272 >    {
1273 >        FailingSupplier r = new FailingSupplier(m);
1274 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1275          checkCompletedWithWrappedCFException(f);
1276 <        assertEquals(1, r.invocationCount);
1277 <    }
1276 >        r.assertInvoked();
1277 >    }}
1278  
1279      // seq completion methods
1280  
# Line 976 | Line 1283 | public class CompletableFutureTest exten
1283       */
1284      public void testThenRun_normalCompletion() {
1285          for (ExecutionMode m : ExecutionMode.values())
979        for (boolean createIncomplete : new boolean[] { true, false })
1286          for (Integer v1 : new Integer[] { 1, null })
1287      {
1288          final CompletableFuture<Integer> f = new CompletableFuture<>();
1289 <        final Noop r = new Noop();
1290 <        if (!createIncomplete) f.complete(v1);
985 <        final CompletableFuture<Void> g = m.thenRun(f, r);
986 <        if (createIncomplete) {
987 <            checkIncomplete(g);
988 <            f.complete(v1);
989 <        }
1289 >        final Noop[] rs = new Noop[6];
1290 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1291  
1292 <        checkCompletedNormally(g, null);
1292 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1293 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1294 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1295 >        checkIncomplete(h0);
1296 >        checkIncomplete(h1);
1297 >        checkIncomplete(h2);
1298 >        assertTrue(f.complete(v1));
1299 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1300 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1301 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1302 >
1303 >        checkCompletedNormally(h0, null);
1304 >        checkCompletedNormally(h1, null);
1305 >        checkCompletedNormally(h2, null);
1306 >        checkCompletedNormally(h3, null);
1307 >        checkCompletedNormally(h4, null);
1308 >        checkCompletedNormally(h5, null);
1309          checkCompletedNormally(f, v1);
1310 <        assertEquals(1, r.invocationCount);
1310 >        for (Noop r : rs) r.assertInvoked();
1311      }}
1312  
1313      /**
# Line 999 | Line 1316 | public class CompletableFutureTest exten
1316       */
1317      public void testThenRun_exceptionalCompletion() {
1318          for (ExecutionMode m : ExecutionMode.values())
1002        for (boolean createIncomplete : new boolean[] { true, false })
1319      {
1320          final CFException ex = new CFException();
1321          final CompletableFuture<Integer> f = new CompletableFuture<>();
1322 <        final Noop r = new Noop();
1323 <        if (!createIncomplete) f.completeExceptionally(ex);
1008 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1009 <        if (createIncomplete) {
1010 <            checkIncomplete(g);
1011 <            f.completeExceptionally(ex);
1012 <        }
1322 >        final Noop[] rs = new Noop[6];
1323 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1324  
1325 <        checkCompletedWithWrappedCFException(g, ex);
1326 <        checkCompletedWithWrappedCFException(f, ex);
1327 <        assertEquals(0, r.invocationCount);
1325 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1326 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1327 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1328 >        checkIncomplete(h0);
1329 >        checkIncomplete(h1);
1330 >        checkIncomplete(h2);
1331 >        assertTrue(f.completeExceptionally(ex));
1332 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1333 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1334 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1335 >
1336 >        checkCompletedWithWrappedException(h0, ex);
1337 >        checkCompletedWithWrappedException(h1, ex);
1338 >        checkCompletedWithWrappedException(h2, ex);
1339 >        checkCompletedWithWrappedException(h3, ex);
1340 >        checkCompletedWithWrappedException(h4, ex);
1341 >        checkCompletedWithWrappedException(h5, ex);
1342 >        checkCompletedExceptionally(f, ex);
1343 >        for (Noop r : rs) r.assertNotInvoked();
1344      }}
1345  
1346      /**
# Line 1021 | Line 1348 | public class CompletableFutureTest exten
1348       */
1349      public void testThenRun_sourceCancelled() {
1350          for (ExecutionMode m : ExecutionMode.values())
1024        for (boolean createIncomplete : new boolean[] { true, false })
1351          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1352      {
1353          final CompletableFuture<Integer> f = new CompletableFuture<>();
1354 <        final Noop r = new Noop();
1355 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1030 <        final CompletableFuture<Void> g = f.thenRun(r);
1031 <        if (createIncomplete) {
1032 <            checkIncomplete(g);
1033 <            assertTrue(f.cancel(mayInterruptIfRunning));
1034 <        }
1354 >        final Noop[] rs = new Noop[6];
1355 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1356  
1357 <        checkCompletedWithWrappedCancellationException(g);
1357 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1358 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1359 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1360 >        checkIncomplete(h0);
1361 >        checkIncomplete(h1);
1362 >        checkIncomplete(h2);
1363 >        assertTrue(f.cancel(mayInterruptIfRunning));
1364 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1365 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1366 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1367 >
1368 >        checkCompletedWithWrappedCancellationException(h0);
1369 >        checkCompletedWithWrappedCancellationException(h1);
1370 >        checkCompletedWithWrappedCancellationException(h2);
1371 >        checkCompletedWithWrappedCancellationException(h3);
1372 >        checkCompletedWithWrappedCancellationException(h4);
1373 >        checkCompletedWithWrappedCancellationException(h5);
1374          checkCancelled(f);
1375 <        assertEquals(0, r.invocationCount);
1375 >        for (Noop r : rs) r.assertNotInvoked();
1376      }}
1377  
1378      /**
# Line 1043 | Line 1380 | public class CompletableFutureTest exten
1380       */
1381      public void testThenRun_actionFailed() {
1382          for (ExecutionMode m : ExecutionMode.values())
1046        for (boolean createIncomplete : new boolean[] { true, false })
1383          for (Integer v1 : new Integer[] { 1, null })
1384      {
1385          final CompletableFuture<Integer> f = new CompletableFuture<>();
1386 <        final FailingRunnable r = new FailingRunnable();
1387 <        if (!createIncomplete) f.complete(v1);
1052 <        final CompletableFuture<Void> g = f.thenRun(r);
1053 <        if (createIncomplete) {
1054 <            checkIncomplete(g);
1055 <            f.complete(v1);
1056 <        }
1386 >        final FailingRunnable[] rs = new FailingRunnable[6];
1387 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1388  
1389 <        checkCompletedWithWrappedCFException(g);
1389 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1390 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1391 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1392 >        assertTrue(f.complete(v1));
1393 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1394 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1395 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1396 >
1397 >        checkCompletedWithWrappedCFException(h0);
1398 >        checkCompletedWithWrappedCFException(h1);
1399 >        checkCompletedWithWrappedCFException(h2);
1400 >        checkCompletedWithWrappedCFException(h3);
1401 >        checkCompletedWithWrappedCFException(h4);
1402 >        checkCompletedWithWrappedCFException(h5);
1403          checkCompletedNormally(f, v1);
1404      }}
1405  
# Line 1064 | Line 1408 | public class CompletableFutureTest exten
1408       */
1409      public void testThenApply_normalCompletion() {
1410          for (ExecutionMode m : ExecutionMode.values())
1067        for (boolean createIncomplete : new boolean[] { true, false })
1411          for (Integer v1 : new Integer[] { 1, null })
1412      {
1413          final CompletableFuture<Integer> f = new CompletableFuture<>();
1414 <        final IncFunction r = new IncFunction();
1415 <        if (!createIncomplete) f.complete(v1);
1073 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1074 <        if (createIncomplete) {
1075 <            checkIncomplete(g);
1076 <            f.complete(v1);
1077 <        }
1414 >        final IncFunction[] rs = new IncFunction[4];
1415 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1416  
1417 <        checkCompletedNormally(g, inc(v1));
1417 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1418 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1419 >        checkIncomplete(h0);
1420 >        checkIncomplete(h1);
1421 >        assertTrue(f.complete(v1));
1422 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1423 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1424 >
1425 >        checkCompletedNormally(h0, inc(v1));
1426 >        checkCompletedNormally(h1, inc(v1));
1427 >        checkCompletedNormally(h2, inc(v1));
1428 >        checkCompletedNormally(h3, inc(v1));
1429          checkCompletedNormally(f, v1);
1430 <        assertEquals(1, r.invocationCount);
1430 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1431      }}
1432  
1433      /**
# Line 1087 | Line 1436 | public class CompletableFutureTest exten
1436       */
1437      public void testThenApply_exceptionalCompletion() {
1438          for (ExecutionMode m : ExecutionMode.values())
1090        for (boolean createIncomplete : new boolean[] { true, false })
1439      {
1440          final CFException ex = new CFException();
1441          final CompletableFuture<Integer> f = new CompletableFuture<>();
1442 <        final IncFunction r = new IncFunction();
1443 <        if (!createIncomplete) f.completeExceptionally(ex);
1096 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1097 <        if (createIncomplete) {
1098 <            checkIncomplete(g);
1099 <            f.completeExceptionally(ex);
1100 <        }
1442 >        final IncFunction[] rs = new IncFunction[4];
1443 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1444  
1445 <        checkCompletedWithWrappedCFException(g, ex);
1446 <        checkCompletedWithWrappedCFException(f, ex);
1447 <        assertEquals(0, r.invocationCount);
1445 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1446 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1447 >        assertTrue(f.completeExceptionally(ex));
1448 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1449 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1450 >
1451 >        checkCompletedWithWrappedException(h0, ex);
1452 >        checkCompletedWithWrappedException(h1, ex);
1453 >        checkCompletedWithWrappedException(h2, ex);
1454 >        checkCompletedWithWrappedException(h3, ex);
1455 >        checkCompletedExceptionally(f, ex);
1456 >        for (IncFunction r : rs) r.assertNotInvoked();
1457      }}
1458  
1459      /**
# Line 1109 | Line 1461 | public class CompletableFutureTest exten
1461       */
1462      public void testThenApply_sourceCancelled() {
1463          for (ExecutionMode m : ExecutionMode.values())
1112        for (boolean createIncomplete : new boolean[] { true, false })
1464          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1465      {
1466          final CompletableFuture<Integer> f = new CompletableFuture<>();
1467 <        final IncFunction r = new IncFunction();
1468 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1118 <        final CompletableFuture<Integer> g = f.thenApply(r);
1119 <        if (createIncomplete) {
1120 <            checkIncomplete(g);
1121 <            assertTrue(f.cancel(mayInterruptIfRunning));
1122 <        }
1467 >        final IncFunction[] rs = new IncFunction[4];
1468 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1469  
1470 <        checkCompletedWithWrappedCancellationException(g);
1470 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1471 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1472 >        assertTrue(f.cancel(mayInterruptIfRunning));
1473 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1474 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1475 >
1476 >        checkCompletedWithWrappedCancellationException(h0);
1477 >        checkCompletedWithWrappedCancellationException(h1);
1478 >        checkCompletedWithWrappedCancellationException(h2);
1479 >        checkCompletedWithWrappedCancellationException(h3);
1480          checkCancelled(f);
1481 <        assertEquals(0, r.invocationCount);
1481 >        for (IncFunction r : rs) r.assertNotInvoked();
1482      }}
1483  
1484      /**
# Line 1131 | Line 1486 | public class CompletableFutureTest exten
1486       */
1487      public void testThenApply_actionFailed() {
1488          for (ExecutionMode m : ExecutionMode.values())
1134        for (boolean createIncomplete : new boolean[] { true, false })
1489          for (Integer v1 : new Integer[] { 1, null })
1490      {
1491          final CompletableFuture<Integer> f = new CompletableFuture<>();
1492 <        final FailingFunction r = new FailingFunction();
1493 <        if (!createIncomplete) f.complete(v1);
1140 <        final CompletableFuture<Integer> g = f.thenApply(r);
1141 <        if (createIncomplete) {
1142 <            checkIncomplete(g);
1143 <            f.complete(v1);
1144 <        }
1492 >        final FailingFunction[] rs = new FailingFunction[4];
1493 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1494  
1495 <        checkCompletedWithWrappedCFException(g);
1495 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1496 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1497 >        assertTrue(f.complete(v1));
1498 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1499 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1500 >
1501 >        checkCompletedWithWrappedCFException(h0);
1502 >        checkCompletedWithWrappedCFException(h1);
1503 >        checkCompletedWithWrappedCFException(h2);
1504 >        checkCompletedWithWrappedCFException(h3);
1505          checkCompletedNormally(f, v1);
1506      }}
1507  
# Line 1152 | Line 1510 | public class CompletableFutureTest exten
1510       */
1511      public void testThenAccept_normalCompletion() {
1512          for (ExecutionMode m : ExecutionMode.values())
1155        for (boolean createIncomplete : new boolean[] { true, false })
1513          for (Integer v1 : new Integer[] { 1, null })
1514      {
1515          final CompletableFuture<Integer> f = new CompletableFuture<>();
1516 <        final IncAction r = new IncAction();
1517 <        if (!createIncomplete) f.complete(v1);
1161 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1162 <        if (createIncomplete) {
1163 <            checkIncomplete(g);
1164 <            f.complete(v1);
1165 <        }
1516 >        final NoopConsumer[] rs = new NoopConsumer[4];
1517 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1518  
1519 <        checkCompletedNormally(g, null);
1519 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1520 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1521 >        checkIncomplete(h0);
1522 >        checkIncomplete(h1);
1523 >        assertTrue(f.complete(v1));
1524 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1525 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1526 >
1527 >        checkCompletedNormally(h0, null);
1528 >        checkCompletedNormally(h1, null);
1529 >        checkCompletedNormally(h2, null);
1530 >        checkCompletedNormally(h3, null);
1531          checkCompletedNormally(f, v1);
1532 <        assertEquals(1, r.invocationCount);
1170 <        assertEquals(inc(v1), r.value);
1532 >        for (NoopConsumer r : rs) r.assertValue(v1);
1533      }}
1534  
1535      /**
# Line 1176 | Line 1538 | public class CompletableFutureTest exten
1538       */
1539      public void testThenAccept_exceptionalCompletion() {
1540          for (ExecutionMode m : ExecutionMode.values())
1179        for (boolean createIncomplete : new boolean[] { true, false })
1541      {
1542          final CFException ex = new CFException();
1543          final CompletableFuture<Integer> f = new CompletableFuture<>();
1544 <        final IncAction r = new IncAction();
1545 <        if (!createIncomplete) f.completeExceptionally(ex);
1185 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1186 <        if (createIncomplete) {
1187 <            checkIncomplete(g);
1188 <            f.completeExceptionally(ex);
1189 <        }
1544 >        final NoopConsumer[] rs = new NoopConsumer[4];
1545 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1546  
1547 <        checkCompletedWithWrappedCFException(g, ex);
1548 <        checkCompletedWithWrappedCFException(f, ex);
1549 <        assertEquals(0, r.invocationCount);
1547 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1548 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1549 >        assertTrue(f.completeExceptionally(ex));
1550 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1551 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1552 >
1553 >        checkCompletedWithWrappedException(h0, ex);
1554 >        checkCompletedWithWrappedException(h1, ex);
1555 >        checkCompletedWithWrappedException(h2, ex);
1556 >        checkCompletedWithWrappedException(h3, ex);
1557 >        checkCompletedExceptionally(f, ex);
1558 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1559      }}
1560  
1561      /**
1562 <     * thenAccept result completes exceptionally if action does
1562 >     * thenAccept result completes exceptionally if source cancelled
1563       */
1564 <    public void testThenAccept_actionFailed() {
1564 >    public void testThenAccept_sourceCancelled() {
1565          for (ExecutionMode m : ExecutionMode.values())
1566 <        for (boolean createIncomplete : new boolean[] { true, false })
1202 <        for (Integer v1 : new Integer[] { 1, null })
1566 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1567      {
1568          final CompletableFuture<Integer> f = new CompletableFuture<>();
1569 <        final FailingConsumer r = new FailingConsumer();
1570 <        if (!createIncomplete) f.complete(v1);
1207 <        final CompletableFuture<Void> g = f.thenAccept(r);
1208 <        if (createIncomplete) {
1209 <            checkIncomplete(g);
1210 <            f.complete(v1);
1211 <        }
1569 >        final NoopConsumer[] rs = new NoopConsumer[4];
1570 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1571  
1572 <        checkCompletedWithWrappedCFException(g);
1573 <        checkCompletedNormally(f, v1);
1572 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1573 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1574 >        assertTrue(f.cancel(mayInterruptIfRunning));
1575 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1576 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1577 >
1578 >        checkCompletedWithWrappedCancellationException(h0);
1579 >        checkCompletedWithWrappedCancellationException(h1);
1580 >        checkCompletedWithWrappedCancellationException(h2);
1581 >        checkCompletedWithWrappedCancellationException(h3);
1582 >        checkCancelled(f);
1583 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1584      }}
1585  
1586      /**
1587 <     * thenAccept result completes exceptionally if source cancelled
1587 >     * thenAccept result completes exceptionally if action does
1588       */
1589 <    public void testThenAccept_sourceCancelled() {
1589 >    public void testThenAccept_actionFailed() {
1590          for (ExecutionMode m : ExecutionMode.values())
1591 <        for (boolean createIncomplete : new boolean[] { true, false })
1223 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1591 >        for (Integer v1 : new Integer[] { 1, null })
1592      {
1593          final CompletableFuture<Integer> f = new CompletableFuture<>();
1594 <        final IncAction r = new IncAction();
1595 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1228 <        final CompletableFuture<Void> g = f.thenAccept(r);
1229 <        if (createIncomplete) {
1230 <            checkIncomplete(g);
1231 <            assertTrue(f.cancel(mayInterruptIfRunning));
1232 <        }
1594 >        final FailingConsumer[] rs = new FailingConsumer[4];
1595 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1596  
1597 <        checkCompletedWithWrappedCancellationException(g);
1598 <        checkCancelled(f);
1599 <        assertEquals(0, r.invocationCount);
1597 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1598 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1599 >        assertTrue(f.complete(v1));
1600 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1601 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1602 >
1603 >        checkCompletedWithWrappedCFException(h0);
1604 >        checkCompletedWithWrappedCFException(h1);
1605 >        checkCompletedWithWrappedCFException(h2);
1606 >        checkCompletedWithWrappedCFException(h3);
1607 >        checkCompletedNormally(f, v1);
1608      }}
1609  
1610      /**
# Line 1242 | Line 1613 | public class CompletableFutureTest exten
1613       */
1614      public void testThenCombine_normalCompletion() {
1615          for (ExecutionMode m : ExecutionMode.values())
1245        for (boolean createIncomplete : new boolean[] { true, false })
1616          for (boolean fFirst : new boolean[] { true, false })
1617          for (Integer v1 : new Integer[] { 1, null })
1618          for (Integer v2 : new Integer[] { 2, null })
1619      {
1620          final CompletableFuture<Integer> f = new CompletableFuture<>();
1621          final CompletableFuture<Integer> g = new CompletableFuture<>();
1622 <        final SubtractFunction r = new SubtractFunction();
1622 >        final SubtractFunction[] rs = new SubtractFunction[6];
1623 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1624  
1625 <        if (fFirst) f.complete(v1); else g.complete(v2);
1626 <        if (!createIncomplete)
1627 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1628 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1629 <        if (createIncomplete) {
1630 <            checkIncomplete(h);
1631 <            assertEquals(0, r.invocationCount);
1632 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1633 <        }
1625 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1626 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1627 >        final Integer w1 =  fFirst ? v1 : v2;
1628 >        final Integer w2 = !fFirst ? v1 : v2;
1629 >
1630 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1631 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1632 >        assertTrue(fst.complete(w1));
1633 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1634 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1635 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1636 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1637 >        checkCompletedNormally(h1, subtract(w1, w1));
1638 >        checkCompletedNormally(h3, subtract(w1, w1));
1639 >        rs[1].assertValue(subtract(w1, w1));
1640 >        rs[3].assertValue(subtract(w1, w1));
1641 >        assertTrue(snd.complete(w2));
1642 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1643 >
1644 >        checkCompletedNormally(h0, subtract(v1, v2));
1645 >        checkCompletedNormally(h2, subtract(v1, v2));
1646 >        checkCompletedNormally(h4, subtract(v1, v2));
1647 >        rs[0].assertValue(subtract(v1, v2));
1648 >        rs[2].assertValue(subtract(v1, v2));
1649 >        rs[4].assertValue(subtract(v1, v2));
1650  
1264        checkCompletedNormally(h, subtract(v1, v2));
1651          checkCompletedNormally(f, v1);
1652          checkCompletedNormally(g, v2);
1267        assertEquals(1, r.invocationCount);
1653      }}
1654  
1655      /**
1656       * thenCombine result completes exceptionally after exceptional
1657       * completion of either source
1658       */
1659 <    public void testThenCombine_exceptionalCompletion1() {
1275 <        for (ExecutionMode m : ExecutionMode.values())
1276 <        for (Integer v1 : new Integer[] { 1, null })
1277 <    {
1278 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1279 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1280 <        final SubtractFunction r = new SubtractFunction();
1281 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1282 <        final CFException ex = new CFException();
1283 <
1284 <        f.completeExceptionally(ex);
1285 <        checkIncomplete(h);
1286 <        g.complete(v1);
1287 <
1288 <        checkCompletedWithWrappedCFException(h, ex);
1289 <        checkCompletedWithWrappedCFException(f, ex);
1290 <        assertEquals(0, r.invocationCount);
1291 <        checkCompletedNormally(g, v1);
1292 <    }}
1293 <
1294 <    public void testThenCombine_exceptionalCompletion2() {
1295 <        for (ExecutionMode m : ExecutionMode.values())
1296 <        for (Integer v1 : new Integer[] { 1, null })
1297 <    {
1298 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1299 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1300 <        final SubtractFunction r = new SubtractFunction();
1301 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1302 <        final CFException ex = new CFException();
1303 <
1304 <        g.completeExceptionally(ex);
1305 <        checkIncomplete(h);
1306 <        f.complete(v1);
1307 <
1308 <        checkCompletedWithWrappedCFException(h, ex);
1309 <        checkCompletedWithWrappedCFException(g, ex);
1310 <        assertEquals(0, r.invocationCount);
1311 <        checkCompletedNormally(f, v1);
1312 <    }}
1313 <
1314 <    public void testThenCombine_exceptionalCompletion3() {
1659 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1660          for (ExecutionMode m : ExecutionMode.values())
1661 +        for (boolean fFirst : new boolean[] { true, false })
1662 +        for (boolean failFirst : new boolean[] { true, false })
1663          for (Integer v1 : new Integer[] { 1, null })
1664      {
1665          final CompletableFuture<Integer> f = new CompletableFuture<>();
1666          final CompletableFuture<Integer> g = new CompletableFuture<>();
1320        final SubtractFunction r = new SubtractFunction();
1667          final CFException ex = new CFException();
1668 <
1669 <        g.completeExceptionally(ex);
1670 <        f.complete(v1);
1671 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1672 <
1673 <        checkCompletedWithWrappedCFException(h, ex);
1674 <        checkCompletedWithWrappedCFException(g, ex);
1675 <        assertEquals(0, r.invocationCount);
1676 <        checkCompletedNormally(f, v1);
1668 >        final SubtractFunction r1 = new SubtractFunction(m);
1669 >        final SubtractFunction r2 = new SubtractFunction(m);
1670 >        final SubtractFunction r3 = new SubtractFunction(m);
1671 >
1672 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1673 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1674 >        final Callable<Boolean> complete1 = failFirst ?
1675 >            () -> fst.completeExceptionally(ex) :
1676 >            () -> fst.complete(v1);
1677 >        final Callable<Boolean> complete2 = failFirst ?
1678 >            () -> snd.complete(v1) :
1679 >            () -> snd.completeExceptionally(ex);
1680 >
1681 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1682 >        assertTrue(complete1.call());
1683 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1684 >        checkIncomplete(h1);
1685 >        checkIncomplete(h2);
1686 >        assertTrue(complete2.call());
1687 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1688 >
1689 >        checkCompletedWithWrappedException(h1, ex);
1690 >        checkCompletedWithWrappedException(h2, ex);
1691 >        checkCompletedWithWrappedException(h3, ex);
1692 >        r1.assertNotInvoked();
1693 >        r2.assertNotInvoked();
1694 >        r3.assertNotInvoked();
1695 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1696 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1697      }}
1698  
1699 <    public void testThenCombine_exceptionalCompletion4() {
1699 >    /**
1700 >     * thenCombine result completes exceptionally if either source cancelled
1701 >     */
1702 >    public void testThenCombine_sourceCancelled() throws Throwable {
1703          for (ExecutionMode m : ExecutionMode.values())
1704 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1705 +        for (boolean fFirst : new boolean[] { true, false })
1706 +        for (boolean failFirst : new boolean[] { true, false })
1707          for (Integer v1 : new Integer[] { 1, null })
1708      {
1709          final CompletableFuture<Integer> f = new CompletableFuture<>();
1710          final CompletableFuture<Integer> g = new CompletableFuture<>();
1711 <        final SubtractFunction r = new SubtractFunction();
1712 <        final CFException ex = new CFException();
1713 <
1714 <        f.completeExceptionally(ex);
1715 <        g.complete(v1);
1716 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1717 <
1718 <        checkCompletedWithWrappedCFException(h, ex);
1719 <        checkCompletedWithWrappedCFException(f, ex);
1720 <        assertEquals(0, r.invocationCount);
1721 <        checkCompletedNormally(g, v1);
1711 >        final SubtractFunction r1 = new SubtractFunction(m);
1712 >        final SubtractFunction r2 = new SubtractFunction(m);
1713 >        final SubtractFunction r3 = new SubtractFunction(m);
1714 >
1715 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1716 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1717 >        final Callable<Boolean> complete1 = failFirst ?
1718 >            () -> fst.cancel(mayInterruptIfRunning) :
1719 >            () -> fst.complete(v1);
1720 >        final Callable<Boolean> complete2 = failFirst ?
1721 >            () -> snd.complete(v1) :
1722 >            () -> snd.cancel(mayInterruptIfRunning);
1723 >
1724 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1725 >        assertTrue(complete1.call());
1726 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1727 >        checkIncomplete(h1);
1728 >        checkIncomplete(h2);
1729 >        assertTrue(complete2.call());
1730 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1731 >
1732 >        checkCompletedWithWrappedCancellationException(h1);
1733 >        checkCompletedWithWrappedCancellationException(h2);
1734 >        checkCompletedWithWrappedCancellationException(h3);
1735 >        r1.assertNotInvoked();
1736 >        r2.assertNotInvoked();
1737 >        r3.assertNotInvoked();
1738 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1739 >        checkCancelled(failFirst ? fst : snd);
1740      }}
1741  
1742      /**
1743       * thenCombine result completes exceptionally if action does
1744       */
1745 <    public void testThenCombine_actionFailed1() {
1356 <        for (ExecutionMode m : ExecutionMode.values())
1357 <        for (Integer v1 : new Integer[] { 1, null })
1358 <        for (Integer v2 : new Integer[] { 2, null })
1359 <    {
1360 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1361 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1362 <        final FailingBiFunction r = new FailingBiFunction();
1363 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1364 <
1365 <        f.complete(v1);
1366 <        checkIncomplete(h);
1367 <        g.complete(v2);
1368 <
1369 <        checkCompletedWithWrappedCFException(h);
1370 <        checkCompletedNormally(f, v1);
1371 <        checkCompletedNormally(g, v2);
1372 <    }}
1373 <
1374 <    public void testThenCombine_actionFailed2() {
1745 >    public void testThenCombine_actionFailed() {
1746          for (ExecutionMode m : ExecutionMode.values())
1747 +        for (boolean fFirst : new boolean[] { true, false })
1748          for (Integer v1 : new Integer[] { 1, null })
1749          for (Integer v2 : new Integer[] { 2, null })
1750      {
1751          final CompletableFuture<Integer> f = new CompletableFuture<>();
1752          final CompletableFuture<Integer> g = new CompletableFuture<>();
1753 <        final FailingBiFunction r = new FailingBiFunction();
1754 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1755 <
1756 <        g.complete(v2);
1757 <        checkIncomplete(h);
1758 <        f.complete(v1);
1759 <
1760 <        checkCompletedWithWrappedCFException(h);
1753 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1754 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1755 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1756 >
1757 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1758 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1759 >        final Integer w1 =  fFirst ? v1 : v2;
1760 >        final Integer w2 = !fFirst ? v1 : v2;
1761 >
1762 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1763 >        assertTrue(fst.complete(w1));
1764 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1765 >        assertTrue(snd.complete(w2));
1766 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1767 >
1768 >        checkCompletedWithWrappedCFException(h1);
1769 >        checkCompletedWithWrappedCFException(h2);
1770 >        checkCompletedWithWrappedCFException(h3);
1771 >        r1.assertInvoked();
1772 >        r2.assertInvoked();
1773 >        r3.assertInvoked();
1774          checkCompletedNormally(f, v1);
1775          checkCompletedNormally(g, v2);
1776      }}
1777  
1778      /**
1394     * thenCombine result completes exceptionally if either source cancelled
1395     */
1396    public void testThenCombine_sourceCancelled1() {
1397        for (ExecutionMode m : ExecutionMode.values())
1398        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1399        for (Integer v1 : new Integer[] { 1, null })
1400    {
1401        final CompletableFuture<Integer> f = new CompletableFuture<>();
1402        final CompletableFuture<Integer> g = new CompletableFuture<>();
1403        final SubtractFunction r = new SubtractFunction();
1404        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1405
1406        assertTrue(f.cancel(mayInterruptIfRunning));
1407        checkIncomplete(h);
1408        g.complete(v1);
1409
1410        checkCompletedWithWrappedCancellationException(h);
1411        checkCancelled(f);
1412        assertEquals(0, r.invocationCount);
1413        checkCompletedNormally(g, v1);
1414    }}
1415
1416    public void testThenCombine_sourceCancelled2() {
1417        for (ExecutionMode m : ExecutionMode.values())
1418        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1419        for (Integer v1 : new Integer[] { 1, null })
1420    {
1421        final CompletableFuture<Integer> f = new CompletableFuture<>();
1422        final CompletableFuture<Integer> g = new CompletableFuture<>();
1423        final SubtractFunction r = new SubtractFunction();
1424        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1425
1426        assertTrue(g.cancel(mayInterruptIfRunning));
1427        checkIncomplete(h);
1428        f.complete(v1);
1429
1430        checkCompletedWithWrappedCancellationException(h);
1431        checkCancelled(g);
1432        assertEquals(0, r.invocationCount);
1433        checkCompletedNormally(f, v1);
1434    }}
1435
1436    public void testThenCombine_sourceCancelled3() {
1437        for (ExecutionMode m : ExecutionMode.values())
1438        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1439        for (Integer v1 : new Integer[] { 1, null })
1440    {
1441        final CompletableFuture<Integer> f = new CompletableFuture<>();
1442        final CompletableFuture<Integer> g = new CompletableFuture<>();
1443        final SubtractFunction r = new SubtractFunction();
1444
1445        assertTrue(g.cancel(mayInterruptIfRunning));
1446        f.complete(v1);
1447        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1448
1449        checkCompletedWithWrappedCancellationException(h);
1450        checkCancelled(g);
1451        assertEquals(0, r.invocationCount);
1452        checkCompletedNormally(f, v1);
1453    }}
1454
1455    public void testThenCombine_sourceCancelled4() {
1456        for (ExecutionMode m : ExecutionMode.values())
1457        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1458        for (Integer v1 : new Integer[] { 1, null })
1459    {
1460        final CompletableFuture<Integer> f = new CompletableFuture<>();
1461        final CompletableFuture<Integer> g = new CompletableFuture<>();
1462        final SubtractFunction r = new SubtractFunction();
1463
1464        assertTrue(f.cancel(mayInterruptIfRunning));
1465        g.complete(v1);
1466        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1467
1468        checkCompletedWithWrappedCancellationException(h);
1469        checkCancelled(f);
1470        assertEquals(0, r.invocationCount);
1471        checkCompletedNormally(g, v1);
1472    }}
1473
1474    /**
1779       * thenAcceptBoth result completes normally after normal
1780       * completion of sources
1781       */
1782 <    public void testThenAcceptBoth_normalCompletion1() {
1479 <        for (ExecutionMode m : ExecutionMode.values())
1480 <        for (Integer v1 : new Integer[] { 1, null })
1481 <        for (Integer v2 : new Integer[] { 2, null })
1482 <    {
1483 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1484 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1485 <        final SubtractAction r = new SubtractAction();
1486 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1487 <
1488 <        f.complete(v1);
1489 <        checkIncomplete(h);
1490 <        assertEquals(0, r.invocationCount);
1491 <        g.complete(v2);
1492 <
1493 <        checkCompletedNormally(h, null);
1494 <        assertEquals(subtract(v1, v2), r.value);
1495 <        checkCompletedNormally(f, v1);
1496 <        checkCompletedNormally(g, v2);
1497 <    }}
1498 <
1499 <    public void testThenAcceptBoth_normalCompletion2() {
1500 <        for (ExecutionMode m : ExecutionMode.values())
1501 <        for (Integer v1 : new Integer[] { 1, null })
1502 <        for (Integer v2 : new Integer[] { 2, null })
1503 <    {
1504 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1505 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1506 <        final SubtractAction r = new SubtractAction();
1507 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1508 <
1509 <        g.complete(v2);
1510 <        checkIncomplete(h);
1511 <        assertEquals(0, r.invocationCount);
1512 <        f.complete(v1);
1513 <
1514 <        checkCompletedNormally(h, null);
1515 <        assertEquals(subtract(v1, v2), r.value);
1516 <        checkCompletedNormally(f, v1);
1517 <        checkCompletedNormally(g, v2);
1518 <    }}
1519 <
1520 <    public void testThenAcceptBoth_normalCompletion3() {
1521 <        for (ExecutionMode m : ExecutionMode.values())
1522 <        for (Integer v1 : new Integer[] { 1, null })
1523 <        for (Integer v2 : new Integer[] { 2, null })
1524 <    {
1525 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1526 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1527 <        final SubtractAction r = new SubtractAction();
1528 <
1529 <        g.complete(v2);
1530 <        f.complete(v1);
1531 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1532 <
1533 <        checkCompletedNormally(h, null);
1534 <        assertEquals(subtract(v1, v2), r.value);
1535 <        checkCompletedNormally(f, v1);
1536 <        checkCompletedNormally(g, v2);
1537 <    }}
1538 <
1539 <    public void testThenAcceptBoth_normalCompletion4() {
1782 >    public void testThenAcceptBoth_normalCompletion() {
1783          for (ExecutionMode m : ExecutionMode.values())
1784 +        for (boolean fFirst : new boolean[] { true, false })
1785          for (Integer v1 : new Integer[] { 1, null })
1786          for (Integer v2 : new Integer[] { 2, null })
1787      {
1788          final CompletableFuture<Integer> f = new CompletableFuture<>();
1789          final CompletableFuture<Integer> g = new CompletableFuture<>();
1790 <        final SubtractAction r = new SubtractAction();
1791 <
1792 <        f.complete(v1);
1793 <        g.complete(v2);
1794 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1795 <
1796 <        checkCompletedNormally(h, null);
1797 <        assertEquals(subtract(v1, v2), r.value);
1790 >        final SubtractAction r1 = new SubtractAction(m);
1791 >        final SubtractAction r2 = new SubtractAction(m);
1792 >        final SubtractAction r3 = new SubtractAction(m);
1793 >
1794 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1795 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1796 >        final Integer w1 =  fFirst ? v1 : v2;
1797 >        final Integer w2 = !fFirst ? v1 : v2;
1798 >
1799 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1800 >        assertTrue(fst.complete(w1));
1801 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1802 >        checkIncomplete(h1);
1803 >        checkIncomplete(h2);
1804 >        r1.assertNotInvoked();
1805 >        r2.assertNotInvoked();
1806 >        assertTrue(snd.complete(w2));
1807 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1808 >
1809 >        checkCompletedNormally(h1, null);
1810 >        checkCompletedNormally(h2, null);
1811 >        checkCompletedNormally(h3, null);
1812 >        r1.assertValue(subtract(v1, v2));
1813 >        r2.assertValue(subtract(v1, v2));
1814 >        r3.assertValue(subtract(v1, v2));
1815          checkCompletedNormally(f, v1);
1816          checkCompletedNormally(g, v2);
1817      }}
# Line 1559 | Line 1820 | public class CompletableFutureTest exten
1820       * thenAcceptBoth result completes exceptionally after exceptional
1821       * completion of either source
1822       */
1823 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1563 <        for (ExecutionMode m : ExecutionMode.values())
1564 <        for (Integer v1 : new Integer[] { 1, null })
1565 <    {
1566 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1567 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1568 <        final SubtractAction r = new SubtractAction();
1569 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1570 <        final CFException ex = new CFException();
1571 <
1572 <        f.completeExceptionally(ex);
1573 <        checkIncomplete(h);
1574 <        g.complete(v1);
1575 <
1576 <        checkCompletedWithWrappedCFException(h, ex);
1577 <        checkCompletedWithWrappedCFException(f, ex);
1578 <        assertEquals(0, r.invocationCount);
1579 <        checkCompletedNormally(g, v1);
1580 <    }}
1581 <
1582 <    public void testThenAcceptBoth_exceptionalCompletion2() {
1583 <        for (ExecutionMode m : ExecutionMode.values())
1584 <        for (Integer v1 : new Integer[] { 1, null })
1585 <    {
1586 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1587 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1588 <        final SubtractAction r = new SubtractAction();
1589 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1590 <        final CFException ex = new CFException();
1591 <
1592 <        g.completeExceptionally(ex);
1593 <        checkIncomplete(h);
1594 <        f.complete(v1);
1595 <
1596 <        checkCompletedWithWrappedCFException(h, ex);
1597 <        checkCompletedWithWrappedCFException(g, ex);
1598 <        assertEquals(0, r.invocationCount);
1599 <        checkCompletedNormally(f, v1);
1600 <    }}
1601 <
1602 <    public void testThenAcceptBoth_exceptionalCompletion3() {
1823 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1824          for (ExecutionMode m : ExecutionMode.values())
1825 +        for (boolean fFirst : new boolean[] { true, false })
1826 +        for (boolean failFirst : new boolean[] { true, false })
1827          for (Integer v1 : new Integer[] { 1, null })
1828      {
1829          final CompletableFuture<Integer> f = new CompletableFuture<>();
1830          final CompletableFuture<Integer> g = new CompletableFuture<>();
1608        final SubtractAction r = new SubtractAction();
1831          final CFException ex = new CFException();
1832 <
1833 <        g.completeExceptionally(ex);
1834 <        f.complete(v1);
1835 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1836 <
1837 <        checkCompletedWithWrappedCFException(h, ex);
1838 <        checkCompletedWithWrappedCFException(g, ex);
1839 <        assertEquals(0, r.invocationCount);
1840 <        checkCompletedNormally(f, v1);
1832 >        final SubtractAction r1 = new SubtractAction(m);
1833 >        final SubtractAction r2 = new SubtractAction(m);
1834 >        final SubtractAction r3 = new SubtractAction(m);
1835 >
1836 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1837 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1838 >        final Callable<Boolean> complete1 = failFirst ?
1839 >            () -> fst.completeExceptionally(ex) :
1840 >            () -> fst.complete(v1);
1841 >        final Callable<Boolean> complete2 = failFirst ?
1842 >            () -> snd.complete(v1) :
1843 >            () -> snd.completeExceptionally(ex);
1844 >
1845 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1846 >        assertTrue(complete1.call());
1847 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1848 >        checkIncomplete(h1);
1849 >        checkIncomplete(h2);
1850 >        assertTrue(complete2.call());
1851 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1852 >
1853 >        checkCompletedWithWrappedException(h1, ex);
1854 >        checkCompletedWithWrappedException(h2, ex);
1855 >        checkCompletedWithWrappedException(h3, ex);
1856 >        r1.assertNotInvoked();
1857 >        r2.assertNotInvoked();
1858 >        r3.assertNotInvoked();
1859 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1860 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1861      }}
1862  
1863 <    public void testThenAcceptBoth_exceptionalCompletion4() {
1863 >    /**
1864 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1865 >     */
1866 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1867          for (ExecutionMode m : ExecutionMode.values())
1868 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1869 +        for (boolean fFirst : new boolean[] { true, false })
1870 +        for (boolean failFirst : new boolean[] { true, false })
1871          for (Integer v1 : new Integer[] { 1, null })
1872      {
1873          final CompletableFuture<Integer> f = new CompletableFuture<>();
1874          final CompletableFuture<Integer> g = new CompletableFuture<>();
1875 <        final SubtractAction r = new SubtractAction();
1876 <        final CFException ex = new CFException();
1877 <
1878 <        f.completeExceptionally(ex);
1879 <        g.complete(v1);
1880 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1881 <
1882 <        checkCompletedWithWrappedCFException(h, ex);
1883 <        checkCompletedWithWrappedCFException(f, ex);
1884 <        assertEquals(0, r.invocationCount);
1885 <        checkCompletedNormally(g, v1);
1875 >        final SubtractAction r1 = new SubtractAction(m);
1876 >        final SubtractAction r2 = new SubtractAction(m);
1877 >        final SubtractAction r3 = new SubtractAction(m);
1878 >
1879 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1880 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1881 >        final Callable<Boolean> complete1 = failFirst ?
1882 >            () -> fst.cancel(mayInterruptIfRunning) :
1883 >            () -> fst.complete(v1);
1884 >        final Callable<Boolean> complete2 = failFirst ?
1885 >            () -> snd.complete(v1) :
1886 >            () -> snd.cancel(mayInterruptIfRunning);
1887 >
1888 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1889 >        assertTrue(complete1.call());
1890 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1891 >        checkIncomplete(h1);
1892 >        checkIncomplete(h2);
1893 >        assertTrue(complete2.call());
1894 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1895 >
1896 >        checkCompletedWithWrappedCancellationException(h1);
1897 >        checkCompletedWithWrappedCancellationException(h2);
1898 >        checkCompletedWithWrappedCancellationException(h3);
1899 >        r1.assertNotInvoked();
1900 >        r2.assertNotInvoked();
1901 >        r3.assertNotInvoked();
1902 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1903 >        checkCancelled(failFirst ? fst : snd);
1904      }}
1905  
1906      /**
1907       * thenAcceptBoth result completes exceptionally if action does
1908       */
1909 <    public void testThenAcceptBoth_actionFailed1() {
1644 <        for (ExecutionMode m : ExecutionMode.values())
1645 <        for (Integer v1 : new Integer[] { 1, null })
1646 <        for (Integer v2 : new Integer[] { 2, null })
1647 <    {
1648 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1649 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1650 <        final FailingBiConsumer r = new FailingBiConsumer();
1651 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1652 <
1653 <        f.complete(v1);
1654 <        checkIncomplete(h);
1655 <        g.complete(v2);
1656 <
1657 <        checkCompletedWithWrappedCFException(h);
1658 <        checkCompletedNormally(f, v1);
1659 <        checkCompletedNormally(g, v2);
1660 <    }}
1661 <
1662 <    public void testThenAcceptBoth_actionFailed2() {
1909 >    public void testThenAcceptBoth_actionFailed() {
1910          for (ExecutionMode m : ExecutionMode.values())
1911 +        for (boolean fFirst : new boolean[] { true, false })
1912          for (Integer v1 : new Integer[] { 1, null })
1913          for (Integer v2 : new Integer[] { 2, null })
1914      {
1915          final CompletableFuture<Integer> f = new CompletableFuture<>();
1916          final CompletableFuture<Integer> g = new CompletableFuture<>();
1917 <        final FailingBiConsumer r = new FailingBiConsumer();
1918 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1919 <
1920 <        g.complete(v2);
1921 <        checkIncomplete(h);
1922 <        f.complete(v1);
1923 <
1924 <        checkCompletedWithWrappedCFException(h);
1917 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1918 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1919 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1920 >
1921 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1922 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1923 >        final Integer w1 =  fFirst ? v1 : v2;
1924 >        final Integer w2 = !fFirst ? v1 : v2;
1925 >
1926 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1927 >        assertTrue(fst.complete(w1));
1928 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1929 >        assertTrue(snd.complete(w2));
1930 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1931 >
1932 >        checkCompletedWithWrappedCFException(h1);
1933 >        checkCompletedWithWrappedCFException(h2);
1934 >        checkCompletedWithWrappedCFException(h3);
1935 >        r1.assertInvoked();
1936 >        r2.assertInvoked();
1937 >        r3.assertInvoked();
1938          checkCompletedNormally(f, v1);
1939          checkCompletedNormally(g, v2);
1940      }}
1941  
1942      /**
1682     * thenAcceptBoth result completes exceptionally if either source cancelled
1683     */
1684    public void testThenAcceptBoth_sourceCancelled1() {
1685        for (ExecutionMode m : ExecutionMode.values())
1686        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1687        for (Integer v1 : new Integer[] { 1, null })
1688    {
1689        final CompletableFuture<Integer> f = new CompletableFuture<>();
1690        final CompletableFuture<Integer> g = new CompletableFuture<>();
1691        final SubtractAction r = new SubtractAction();
1692        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1693
1694        assertTrue(f.cancel(mayInterruptIfRunning));
1695        checkIncomplete(h);
1696        g.complete(v1);
1697
1698        checkCompletedWithWrappedCancellationException(h);
1699        checkCancelled(f);
1700        assertEquals(0, r.invocationCount);
1701        checkCompletedNormally(g, v1);
1702    }}
1703
1704    public void testThenAcceptBoth_sourceCancelled2() {
1705        for (ExecutionMode m : ExecutionMode.values())
1706        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1707        for (Integer v1 : new Integer[] { 1, null })
1708    {
1709        final CompletableFuture<Integer> f = new CompletableFuture<>();
1710        final CompletableFuture<Integer> g = new CompletableFuture<>();
1711        final SubtractAction r = new SubtractAction();
1712        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1713
1714        assertTrue(g.cancel(mayInterruptIfRunning));
1715        checkIncomplete(h);
1716        f.complete(v1);
1717
1718        checkCompletedWithWrappedCancellationException(h);
1719        checkCancelled(g);
1720        assertEquals(0, r.invocationCount);
1721        checkCompletedNormally(f, v1);
1722    }}
1723
1724    public void testThenAcceptBoth_sourceCancelled3() {
1725        for (ExecutionMode m : ExecutionMode.values())
1726        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1727        for (Integer v1 : new Integer[] { 1, null })
1728    {
1729        final CompletableFuture<Integer> f = new CompletableFuture<>();
1730        final CompletableFuture<Integer> g = new CompletableFuture<>();
1731        final SubtractAction r = new SubtractAction();
1732
1733        assertTrue(g.cancel(mayInterruptIfRunning));
1734        f.complete(v1);
1735        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1736
1737        checkCompletedWithWrappedCancellationException(h);
1738        checkCancelled(g);
1739        assertEquals(0, r.invocationCount);
1740        checkCompletedNormally(f, v1);
1741    }}
1742
1743    public void testThenAcceptBoth_sourceCancelled4() {
1744        for (ExecutionMode m : ExecutionMode.values())
1745        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1746        for (Integer v1 : new Integer[] { 1, null })
1747    {
1748        final CompletableFuture<Integer> f = new CompletableFuture<>();
1749        final CompletableFuture<Integer> g = new CompletableFuture<>();
1750        final SubtractAction r = new SubtractAction();
1751
1752        assertTrue(f.cancel(mayInterruptIfRunning));
1753        g.complete(v1);
1754        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1755
1756        checkCompletedWithWrappedCancellationException(h);
1757        checkCancelled(f);
1758        assertEquals(0, r.invocationCount);
1759        checkCompletedNormally(g, v1);
1760    }}
1761
1762    /**
1943       * runAfterBoth result completes normally after normal
1944       * completion of sources
1945       */
1946 <    public void testRunAfterBoth_normalCompletion1() {
1767 <        for (ExecutionMode m : ExecutionMode.values())
1768 <        for (Integer v1 : new Integer[] { 1, null })
1769 <        for (Integer v2 : new Integer[] { 2, null })
1770 <    {
1771 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1772 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1773 <        final Noop r = new Noop();
1774 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1775 <
1776 <        f.complete(v1);
1777 <        checkIncomplete(h);
1778 <        assertEquals(0, r.invocationCount);
1779 <        g.complete(v2);
1780 <
1781 <        checkCompletedNormally(h, null);
1782 <        assertEquals(1, r.invocationCount);
1783 <        checkCompletedNormally(f, v1);
1784 <        checkCompletedNormally(g, v2);
1785 <    }}
1786 <
1787 <    public void testRunAfterBoth_normalCompletion2() {
1788 <        for (ExecutionMode m : ExecutionMode.values())
1789 <        for (Integer v1 : new Integer[] { 1, null })
1790 <        for (Integer v2 : new Integer[] { 2, null })
1791 <    {
1792 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1793 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1794 <        final Noop r = new Noop();
1795 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1796 <
1797 <        g.complete(v2);
1798 <        checkIncomplete(h);
1799 <        assertEquals(0, r.invocationCount);
1800 <        f.complete(v1);
1801 <
1802 <        checkCompletedNormally(h, null);
1803 <        assertEquals(1, r.invocationCount);
1804 <        checkCompletedNormally(f, v1);
1805 <        checkCompletedNormally(g, v2);
1806 <    }}
1807 <
1808 <    public void testRunAfterBoth_normalCompletion3() {
1809 <        for (ExecutionMode m : ExecutionMode.values())
1810 <        for (Integer v1 : new Integer[] { 1, null })
1811 <        for (Integer v2 : new Integer[] { 2, null })
1812 <    {
1813 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1814 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1815 <        final Noop r = new Noop();
1816 <
1817 <        g.complete(v2);
1818 <        f.complete(v1);
1819 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1820 <
1821 <        checkCompletedNormally(h, null);
1822 <        assertEquals(1, r.invocationCount);
1823 <        checkCompletedNormally(f, v1);
1824 <        checkCompletedNormally(g, v2);
1825 <    }}
1826 <
1827 <    public void testRunAfterBoth_normalCompletion4() {
1946 >    public void testRunAfterBoth_normalCompletion() {
1947          for (ExecutionMode m : ExecutionMode.values())
1948 +        for (boolean fFirst : new boolean[] { true, false })
1949          for (Integer v1 : new Integer[] { 1, null })
1950          for (Integer v2 : new Integer[] { 2, null })
1951      {
1952          final CompletableFuture<Integer> f = new CompletableFuture<>();
1953          final CompletableFuture<Integer> g = new CompletableFuture<>();
1954 <        final Noop r = new Noop();
1955 <
1956 <        f.complete(v1);
1957 <        g.complete(v2);
1958 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1959 <
1960 <        checkCompletedNormally(h, null);
1961 <        assertEquals(1, r.invocationCount);
1954 >        final Noop r1 = new Noop(m);
1955 >        final Noop r2 = new Noop(m);
1956 >        final Noop r3 = new Noop(m);
1957 >
1958 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1959 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1960 >        final Integer w1 =  fFirst ? v1 : v2;
1961 >        final Integer w2 = !fFirst ? v1 : v2;
1962 >
1963 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1964 >        assertTrue(fst.complete(w1));
1965 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1966 >        checkIncomplete(h1);
1967 >        checkIncomplete(h2);
1968 >        r1.assertNotInvoked();
1969 >        r2.assertNotInvoked();
1970 >        assertTrue(snd.complete(w2));
1971 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1972 >
1973 >        checkCompletedNormally(h1, null);
1974 >        checkCompletedNormally(h2, null);
1975 >        checkCompletedNormally(h3, null);
1976 >        r1.assertInvoked();
1977 >        r2.assertInvoked();
1978 >        r3.assertInvoked();
1979          checkCompletedNormally(f, v1);
1980          checkCompletedNormally(g, v2);
1981      }}
# Line 1847 | Line 1984 | public class CompletableFutureTest exten
1984       * runAfterBoth result completes exceptionally after exceptional
1985       * completion of either source
1986       */
1987 <    public void testRunAfterBoth_exceptionalCompletion1() {
1851 <        for (ExecutionMode m : ExecutionMode.values())
1852 <        for (Integer v1 : new Integer[] { 1, null })
1853 <    {
1854 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1855 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1856 <        final Noop r = new Noop();
1857 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1858 <        final CFException ex = new CFException();
1859 <
1860 <        f.completeExceptionally(ex);
1861 <        checkIncomplete(h);
1862 <        g.complete(v1);
1863 <
1864 <        checkCompletedWithWrappedCFException(h, ex);
1865 <        checkCompletedWithWrappedCFException(f, ex);
1866 <        assertEquals(0, r.invocationCount);
1867 <        checkCompletedNormally(g, v1);
1868 <    }}
1869 <
1870 <    public void testRunAfterBoth_exceptionalCompletion2() {
1987 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1988          for (ExecutionMode m : ExecutionMode.values())
1989 +        for (boolean fFirst : new boolean[] { true, false })
1990 +        for (boolean failFirst : new boolean[] { true, false })
1991          for (Integer v1 : new Integer[] { 1, null })
1992      {
1993          final CompletableFuture<Integer> f = new CompletableFuture<>();
1994          final CompletableFuture<Integer> g = new CompletableFuture<>();
1876        final Noop r = new Noop();
1877        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1995          final CFException ex = new CFException();
1996 <
1997 <        g.completeExceptionally(ex);
1998 <        checkIncomplete(h);
1999 <        f.complete(v1);
2000 <
2001 <        checkCompletedWithWrappedCFException(h, ex);
2002 <        checkCompletedWithWrappedCFException(g, ex);
2003 <        assertEquals(0, r.invocationCount);
2004 <        checkCompletedNormally(f, v1);
1996 >        final Noop r1 = new Noop(m);
1997 >        final Noop r2 = new Noop(m);
1998 >        final Noop r3 = new Noop(m);
1999 >
2000 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2001 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2002 >        final Callable<Boolean> complete1 = failFirst ?
2003 >            () -> fst.completeExceptionally(ex) :
2004 >            () -> fst.complete(v1);
2005 >        final Callable<Boolean> complete2 = failFirst ?
2006 >            () -> snd.complete(v1) :
2007 >            () -> snd.completeExceptionally(ex);
2008 >
2009 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2010 >        assertTrue(complete1.call());
2011 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2012 >        checkIncomplete(h1);
2013 >        checkIncomplete(h2);
2014 >        assertTrue(complete2.call());
2015 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2016 >
2017 >        checkCompletedWithWrappedException(h1, ex);
2018 >        checkCompletedWithWrappedException(h2, ex);
2019 >        checkCompletedWithWrappedException(h3, ex);
2020 >        r1.assertNotInvoked();
2021 >        r2.assertNotInvoked();
2022 >        r3.assertNotInvoked();
2023 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2024 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2025      }}
2026  
2027 <    public void testRunAfterBoth_exceptionalCompletion3() {
2028 <        for (ExecutionMode m : ExecutionMode.values())
2029 <        for (Integer v1 : new Integer[] { 1, null })
2030 <    {
1894 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1895 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1896 <        final Noop r = new Noop();
1897 <        final CFException ex = new CFException();
1898 <
1899 <        g.completeExceptionally(ex);
1900 <        f.complete(v1);
1901 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1902 <
1903 <        checkCompletedWithWrappedCFException(h, ex);
1904 <        checkCompletedWithWrappedCFException(g, ex);
1905 <        assertEquals(0, r.invocationCount);
1906 <        checkCompletedNormally(f, v1);
1907 <    }}
1908 <
1909 <    public void testRunAfterBoth_exceptionalCompletion4() {
2027 >    /**
2028 >     * runAfterBoth result completes exceptionally if either source cancelled
2029 >     */
2030 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2031          for (ExecutionMode m : ExecutionMode.values())
2032 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2033 +        for (boolean fFirst : new boolean[] { true, false })
2034 +        for (boolean failFirst : new boolean[] { true, false })
2035          for (Integer v1 : new Integer[] { 1, null })
2036      {
2037          final CompletableFuture<Integer> f = new CompletableFuture<>();
2038          final CompletableFuture<Integer> g = new CompletableFuture<>();
2039 <        final Noop r = new Noop();
2040 <        final CFException ex = new CFException();
2041 <
2042 <        f.completeExceptionally(ex);
2043 <        g.complete(v1);
2044 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2045 <
2046 <        checkCompletedWithWrappedCFException(h, ex);
2047 <        checkCompletedWithWrappedCFException(f, ex);
2048 <        assertEquals(0, r.invocationCount);
2049 <        checkCompletedNormally(g, v1);
2039 >        final Noop r1 = new Noop(m);
2040 >        final Noop r2 = new Noop(m);
2041 >        final Noop r3 = new Noop(m);
2042 >
2043 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2044 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2045 >        final Callable<Boolean> complete1 = failFirst ?
2046 >            () -> fst.cancel(mayInterruptIfRunning) :
2047 >            () -> fst.complete(v1);
2048 >        final Callable<Boolean> complete2 = failFirst ?
2049 >            () -> snd.complete(v1) :
2050 >            () -> snd.cancel(mayInterruptIfRunning);
2051 >
2052 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2053 >        assertTrue(complete1.call());
2054 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2055 >        checkIncomplete(h1);
2056 >        checkIncomplete(h2);
2057 >        assertTrue(complete2.call());
2058 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2059 >
2060 >        checkCompletedWithWrappedCancellationException(h1);
2061 >        checkCompletedWithWrappedCancellationException(h2);
2062 >        checkCompletedWithWrappedCancellationException(h3);
2063 >        r1.assertNotInvoked();
2064 >        r2.assertNotInvoked();
2065 >        r3.assertNotInvoked();
2066 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2067 >        checkCancelled(failFirst ? fst : snd);
2068      }}
2069  
2070      /**
2071       * runAfterBoth result completes exceptionally if action does
2072       */
2073 <    public void testRunAfterBoth_actionFailed1() {
1932 <        for (ExecutionMode m : ExecutionMode.values())
1933 <        for (Integer v1 : new Integer[] { 1, null })
1934 <        for (Integer v2 : new Integer[] { 2, null })
1935 <    {
1936 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1937 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1938 <        final FailingRunnable r = new FailingRunnable();
1939 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1940 <
1941 <        f.complete(v1);
1942 <        checkIncomplete(h);
1943 <        g.complete(v2);
1944 <
1945 <        checkCompletedWithWrappedCFException(h);
1946 <        checkCompletedNormally(f, v1);
1947 <        checkCompletedNormally(g, v2);
1948 <    }}
1949 <
1950 <    public void testRunAfterBoth_actionFailed2() {
2073 >    public void testRunAfterBoth_actionFailed() {
2074          for (ExecutionMode m : ExecutionMode.values())
2075 +        for (boolean fFirst : new boolean[] { true, false })
2076          for (Integer v1 : new Integer[] { 1, null })
2077          for (Integer v2 : new Integer[] { 2, null })
2078      {
2079          final CompletableFuture<Integer> f = new CompletableFuture<>();
2080          final CompletableFuture<Integer> g = new CompletableFuture<>();
2081 <        final FailingRunnable r = new FailingRunnable();
2082 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2083 <
2084 <        g.complete(v2);
2085 <        checkIncomplete(h);
2086 <        f.complete(v1);
2087 <
2088 <        checkCompletedWithWrappedCFException(h);
2081 >        final FailingRunnable r1 = new FailingRunnable(m);
2082 >        final FailingRunnable r2 = new FailingRunnable(m);
2083 >        final FailingRunnable r3 = new FailingRunnable(m);
2084 >
2085 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2086 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2087 >        final Integer w1 =  fFirst ? v1 : v2;
2088 >        final Integer w2 = !fFirst ? v1 : v2;
2089 >
2090 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2091 >        assertTrue(fst.complete(w1));
2092 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2093 >        assertTrue(snd.complete(w2));
2094 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2095 >
2096 >        checkCompletedWithWrappedCFException(h1);
2097 >        checkCompletedWithWrappedCFException(h2);
2098 >        checkCompletedWithWrappedCFException(h3);
2099 >        r1.assertInvoked();
2100 >        r2.assertInvoked();
2101 >        r3.assertInvoked();
2102          checkCompletedNormally(f, v1);
2103          checkCompletedNormally(g, v2);
2104      }}
2105  
2106      /**
1970     * runAfterBoth result completes exceptionally if either source cancelled
1971     */
1972    public void testRunAfterBoth_sourceCancelled1() {
1973        for (ExecutionMode m : ExecutionMode.values())
1974        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1975        for (Integer v1 : new Integer[] { 1, null })
1976    {
1977        final CompletableFuture<Integer> f = new CompletableFuture<>();
1978        final CompletableFuture<Integer> g = new CompletableFuture<>();
1979        final Noop r = new Noop();
1980        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1981
1982        assertTrue(f.cancel(mayInterruptIfRunning));
1983        checkIncomplete(h);
1984        g.complete(v1);
1985
1986        checkCompletedWithWrappedCancellationException(h);
1987        checkCancelled(f);
1988        assertEquals(0, r.invocationCount);
1989        checkCompletedNormally(g, v1);
1990    }}
1991
1992    public void testRunAfterBoth_sourceCancelled2() {
1993        for (ExecutionMode m : ExecutionMode.values())
1994        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1995        for (Integer v1 : new Integer[] { 1, null })
1996    {
1997        final CompletableFuture<Integer> f = new CompletableFuture<>();
1998        final CompletableFuture<Integer> g = new CompletableFuture<>();
1999        final Noop r = new Noop();
2000        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2001
2002        assertTrue(g.cancel(mayInterruptIfRunning));
2003        checkIncomplete(h);
2004        f.complete(v1);
2005
2006        checkCompletedWithWrappedCancellationException(h);
2007        checkCancelled(g);
2008        assertEquals(0, r.invocationCount);
2009        checkCompletedNormally(f, v1);
2010    }}
2011
2012    public void testRunAfterBoth_sourceCancelled3() {
2013        for (ExecutionMode m : ExecutionMode.values())
2014        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2015        for (Integer v1 : new Integer[] { 1, null })
2016    {
2017        final CompletableFuture<Integer> f = new CompletableFuture<>();
2018        final CompletableFuture<Integer> g = new CompletableFuture<>();
2019        final Noop r = new Noop();
2020
2021        assertTrue(g.cancel(mayInterruptIfRunning));
2022        f.complete(v1);
2023        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2024
2025        checkCompletedWithWrappedCancellationException(h);
2026        checkCancelled(g);
2027        assertEquals(0, r.invocationCount);
2028        checkCompletedNormally(f, v1);
2029    }}
2030
2031    public void testRunAfterBoth_sourceCancelled4() {
2032        for (ExecutionMode m : ExecutionMode.values())
2033        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2034        for (Integer v1 : new Integer[] { 1, null })
2035    {
2036        final CompletableFuture<Integer> f = new CompletableFuture<>();
2037        final CompletableFuture<Integer> g = new CompletableFuture<>();
2038        final Noop r = new Noop();
2039
2040        assertTrue(f.cancel(mayInterruptIfRunning));
2041        g.complete(v1);
2042        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2043
2044        checkCompletedWithWrappedCancellationException(h);
2045        checkCancelled(f);
2046        assertEquals(0, r.invocationCount);
2047        checkCompletedNormally(g, v1);
2048    }}
2049
2050    /**
2107       * applyToEither result completes normally after normal completion
2108       * of either source
2109       */
2110 <    public void testApplyToEither_normalCompletion1() {
2110 >    public void testApplyToEither_normalCompletion() {
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 IncFunction r = new IncFunction();
2118 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2117 >        final IncFunction[] rs = new IncFunction[6];
2118 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2119  
2120 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2121 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2122 +        checkIncomplete(h0);
2123 +        checkIncomplete(h1);
2124 +        rs[0].assertNotInvoked();
2125 +        rs[1].assertNotInvoked();
2126          f.complete(v1);
2127 <        checkCompletedNormally(h, inc(v1));
2127 >        checkCompletedNormally(h0, inc(v1));
2128 >        checkCompletedNormally(h1, inc(v1));
2129 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2130 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2131 >        checkCompletedNormally(h2, inc(v1));
2132 >        checkCompletedNormally(h3, inc(v1));
2133          g.complete(v2);
2134  
2135 <        checkCompletedNormally(f, v1);
2136 <        checkCompletedNormally(g, v2);
2137 <        checkCompletedNormally(h, inc(v1));
2138 <    }}
2139 <
2140 <    public void testApplyToEither_normalCompletion2() {
2141 <        for (ExecutionMode m : ExecutionMode.values())
2142 <        for (Integer v1 : new Integer[] { 1, null })
2143 <        for (Integer v2 : new Integer[] { 2, null })
2077 <    {
2078 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2079 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2080 <        final IncFunction r = new IncFunction();
2081 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2082 <
2083 <        g.complete(v2);
2084 <        checkCompletedNormally(h, inc(v2));
2085 <        f.complete(v1);
2086 <
2087 <        checkCompletedNormally(f, v1);
2088 <        checkCompletedNormally(g, v2);
2089 <        checkCompletedNormally(h, inc(v2));
2090 <        }}
2091 <
2092 <    public void testApplyToEither_normalCompletion3() {
2093 <        for (ExecutionMode m : ExecutionMode.values())
2094 <        for (Integer v1 : new Integer[] { 1, null })
2095 <        for (Integer v2 : new Integer[] { 2, null })
2096 <    {
2097 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2098 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2099 <        final IncFunction r = new IncFunction();
2100 <
2101 <        f.complete(v1);
2102 <        g.complete(v2);
2103 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2135 >        // unspecified behavior - both source completions available
2136 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2137 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2138 >        rs[4].assertValue(h4.join());
2139 >        rs[5].assertValue(h5.join());
2140 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
2141 >                   Objects.equals(inc(v2), h4.join()));
2142 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
2143 >                   Objects.equals(inc(v2), h5.join()));
2144  
2145          checkCompletedNormally(f, v1);
2146          checkCompletedNormally(g, v2);
2147 <
2148 <        // unspecified behavior
2149 <        assertTrue(Objects.equals(h.join(), inc(v1)) ||
2150 <                   Objects.equals(h.join(), inc(v2)));
2151 <        assertEquals(1, r.invocationCount);
2147 >        checkCompletedNormally(h0, inc(v1));
2148 >        checkCompletedNormally(h1, inc(v1));
2149 >        checkCompletedNormally(h2, inc(v1));
2150 >        checkCompletedNormally(h3, inc(v1));
2151 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
2152      }}
2153  
2154      /**
2155       * applyToEither result completes exceptionally after exceptional
2156       * completion of either source
2157       */
2158 <    public void testApplyToEither_exceptionalCompletion1() {
2158 >    public void testApplyToEither_exceptionalCompletion() {
2159          for (ExecutionMode m : ExecutionMode.values())
2160          for (Integer v1 : new Integer[] { 1, null })
2161      {
2162          final CompletableFuture<Integer> f = new CompletableFuture<>();
2163          final CompletableFuture<Integer> g = new CompletableFuture<>();
2124        final IncFunction r = new IncFunction();
2125        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2164          final CFException ex = new CFException();
2165 +        final IncFunction[] rs = new IncFunction[6];
2166 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2167  
2168 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2169 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2170 +        checkIncomplete(h0);
2171 +        checkIncomplete(h1);
2172 +        rs[0].assertNotInvoked();
2173 +        rs[1].assertNotInvoked();
2174          f.completeExceptionally(ex);
2175 <        checkCompletedWithWrappedCFException(h, ex);
2175 >        checkCompletedWithWrappedException(h0, ex);
2176 >        checkCompletedWithWrappedException(h1, ex);
2177 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2178 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2179 >        checkCompletedWithWrappedException(h2, ex);
2180 >        checkCompletedWithWrappedException(h3, ex);
2181          g.complete(v1);
2182  
2183 <        assertEquals(0, r.invocationCount);
2183 >        // unspecified behavior - both source completions available
2184 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2185 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2186 >        try {
2187 >            assertEquals(inc(v1), h4.join());
2188 >            rs[4].assertValue(inc(v1));
2189 >        } catch (CompletionException ok) {
2190 >            checkCompletedWithWrappedException(h4, ex);
2191 >            rs[4].assertNotInvoked();
2192 >        }
2193 >        try {
2194 >            assertEquals(inc(v1), h5.join());
2195 >            rs[5].assertValue(inc(v1));
2196 >        } catch (CompletionException ok) {
2197 >            checkCompletedWithWrappedException(h5, ex);
2198 >            rs[5].assertNotInvoked();
2199 >        }
2200 >
2201 >        checkCompletedExceptionally(f, ex);
2202          checkCompletedNormally(g, v1);
2203 <        checkCompletedWithWrappedCFException(f, ex);
2204 <        checkCompletedWithWrappedCFException(h, ex);
2203 >        checkCompletedWithWrappedException(h0, ex);
2204 >        checkCompletedWithWrappedException(h1, ex);
2205 >        checkCompletedWithWrappedException(h2, ex);
2206 >        checkCompletedWithWrappedException(h3, ex);
2207 >        checkCompletedWithWrappedException(h4, ex);
2208 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2209      }}
2210  
2211      public void testApplyToEither_exceptionalCompletion2() {
2212          for (ExecutionMode m : ExecutionMode.values())
2213 +        for (boolean fFirst : new boolean[] { true, false })
2214          for (Integer v1 : new Integer[] { 1, null })
2215      {
2216          final CompletableFuture<Integer> f = new CompletableFuture<>();
2217          final CompletableFuture<Integer> g = new CompletableFuture<>();
2144        final IncFunction r = new IncFunction();
2145        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2146        final CFException ex = new CFException();
2147
2148        g.completeExceptionally(ex);
2149        checkCompletedWithWrappedCFException(h, ex);
2150        f.complete(v1);
2151
2152        assertEquals(0, r.invocationCount);
2153        checkCompletedNormally(f, v1);
2154        checkCompletedWithWrappedCFException(g, ex);
2155        checkCompletedWithWrappedCFException(h, ex);
2156    }}
2157
2158    public void testApplyToEither_exceptionalCompletion3() {
2159        for (ExecutionMode m : ExecutionMode.values())
2160        for (Integer v1 : new Integer[] { 1, null })
2161    {
2162        final CompletableFuture<Integer> f = new CompletableFuture<>();
2163        final CompletableFuture<Integer> g = new CompletableFuture<>();
2164        final IncFunction r = new IncFunction();
2218          final CFException ex = new CFException();
2219 +        final IncFunction[] rs = new IncFunction[6];
2220 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2221  
2222 <        g.completeExceptionally(ex);
2223 <        f.complete(v1);
2224 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2222 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2223 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2224 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2225 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2226 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2227 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2228  
2229 <        // unspecified behavior
2172 <        Integer v;
2229 >        // unspecified behavior - both source completions available
2230          try {
2231 <            assertEquals(inc(v1), h.join());
2232 <            assertEquals(1, r.invocationCount);
2231 >            assertEquals(inc(v1), h0.join());
2232 >            rs[0].assertValue(inc(v1));
2233          } catch (CompletionException ok) {
2234 <            checkCompletedWithWrappedCFException(h, ex);
2235 <            assertEquals(0, r.invocationCount);
2234 >            checkCompletedWithWrappedException(h0, ex);
2235 >            rs[0].assertNotInvoked();
2236          }
2180
2181        checkCompletedWithWrappedCFException(g, ex);
2182        checkCompletedNormally(f, v1);
2183    }}
2184
2185    public void testApplyToEither_exceptionalCompletion4() {
2186        for (ExecutionMode m : ExecutionMode.values())
2187        for (Integer v1 : new Integer[] { 1, null })
2188    {
2189        final CompletableFuture<Integer> f = new CompletableFuture<>();
2190        final CompletableFuture<Integer> g = new CompletableFuture<>();
2191        final IncFunction r = new IncFunction();
2192        final CFException ex = new CFException();
2193
2194        f.completeExceptionally(ex);
2195        g.complete(v1);
2196        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2197
2198        // unspecified behavior
2199        Integer v;
2237          try {
2238 <            assertEquals(inc(v1), h.join());
2239 <            assertEquals(1, r.invocationCount);
2238 >            assertEquals(inc(v1), h1.join());
2239 >            rs[1].assertValue(inc(v1));
2240          } catch (CompletionException ok) {
2241 <            checkCompletedWithWrappedCFException(h, ex);
2242 <            assertEquals(0, r.invocationCount);
2241 >            checkCompletedWithWrappedException(h1, ex);
2242 >            rs[1].assertNotInvoked();
2243 >        }
2244 >        try {
2245 >            assertEquals(inc(v1), h2.join());
2246 >            rs[2].assertValue(inc(v1));
2247 >        } catch (CompletionException ok) {
2248 >            checkCompletedWithWrappedException(h2, ex);
2249 >            rs[2].assertNotInvoked();
2250 >        }
2251 >        try {
2252 >            assertEquals(inc(v1), h3.join());
2253 >            rs[3].assertValue(inc(v1));
2254 >        } catch (CompletionException ok) {
2255 >            checkCompletedWithWrappedException(h3, ex);
2256 >            rs[3].assertNotInvoked();
2257          }
2258  
2208        checkCompletedWithWrappedCFException(f, ex);
2209        checkCompletedNormally(g, v1);
2210    }}
2211
2212    /**
2213     * applyToEither result completes exceptionally if action does
2214     */
2215    public void testApplyToEither_actionFailed1() {
2216        for (ExecutionMode m : ExecutionMode.values())
2217        for (Integer v1 : new Integer[] { 1, null })
2218        for (Integer v2 : new Integer[] { 2, null })
2219    {
2220        final CompletableFuture<Integer> f = new CompletableFuture<>();
2221        final CompletableFuture<Integer> g = new CompletableFuture<>();
2222        final FailingFunction r = new FailingFunction();
2223        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2224
2225        f.complete(v1);
2226        checkCompletedWithWrappedCFException(h);
2227        g.complete(v2);
2259          checkCompletedNormally(f, v1);
2260 <        checkCompletedNormally(g, v2);
2230 <    }}
2231 <
2232 <    public void testApplyToEither_actionFailed2() {
2233 <        for (ExecutionMode m : ExecutionMode.values())
2234 <        for (Integer v1 : new Integer[] { 1, null })
2235 <        for (Integer v2 : new Integer[] { 2, null })
2236 <    {
2237 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2238 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2239 <        final FailingFunction r = new FailingFunction();
2240 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2241 <
2242 <        g.complete(v2);
2243 <        checkCompletedWithWrappedCFException(h);
2244 <        f.complete(v1);
2245 <        checkCompletedNormally(f, v1);
2246 <        checkCompletedNormally(g, v2);
2260 >        checkCompletedExceptionally(g, ex);
2261      }}
2262  
2263      /**
2264       * applyToEither result completes exceptionally if either source cancelled
2265       */
2266 <    public void testApplyToEither_sourceCancelled1() {
2266 >    public void testApplyToEither_sourceCancelled() {
2267          for (ExecutionMode m : ExecutionMode.values())
2268          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2269          for (Integer v1 : new Integer[] { 1, null })
2270      {
2271          final CompletableFuture<Integer> f = new CompletableFuture<>();
2272          final CompletableFuture<Integer> g = new CompletableFuture<>();
2273 <        final IncFunction r = new IncFunction();
2274 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2273 >        final IncFunction[] rs = new IncFunction[6];
2274 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2275  
2276 <        assertTrue(f.cancel(mayInterruptIfRunning));
2277 <        checkCompletedWithWrappedCancellationException(h);
2276 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2277 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2278 >        checkIncomplete(h0);
2279 >        checkIncomplete(h1);
2280 >        rs[0].assertNotInvoked();
2281 >        rs[1].assertNotInvoked();
2282 >        f.cancel(mayInterruptIfRunning);
2283 >        checkCompletedWithWrappedCancellationException(h0);
2284 >        checkCompletedWithWrappedCancellationException(h1);
2285 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2286 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2287 >        checkCompletedWithWrappedCancellationException(h2);
2288 >        checkCompletedWithWrappedCancellationException(h3);
2289          g.complete(v1);
2290  
2291 +        // unspecified behavior - both source completions available
2292 +        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2293 +        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2294 +        try {
2295 +            assertEquals(inc(v1), h4.join());
2296 +            rs[4].assertValue(inc(v1));
2297 +        } catch (CompletionException ok) {
2298 +            checkCompletedWithWrappedCancellationException(h4);
2299 +            rs[4].assertNotInvoked();
2300 +        }
2301 +        try {
2302 +            assertEquals(inc(v1), h5.join());
2303 +            rs[5].assertValue(inc(v1));
2304 +        } catch (CompletionException ok) {
2305 +            checkCompletedWithWrappedCancellationException(h5);
2306 +            rs[5].assertNotInvoked();
2307 +        }
2308 +
2309          checkCancelled(f);
2267        assertEquals(0, r.invocationCount);
2310          checkCompletedNormally(g, v1);
2311 <        checkCompletedWithWrappedCancellationException(h);
2311 >        checkCompletedWithWrappedCancellationException(h0);
2312 >        checkCompletedWithWrappedCancellationException(h1);
2313 >        checkCompletedWithWrappedCancellationException(h2);
2314 >        checkCompletedWithWrappedCancellationException(h3);
2315 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2316      }}
2317  
2318      public void testApplyToEither_sourceCancelled2() {
2319          for (ExecutionMode m : ExecutionMode.values())
2320          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2321 +        for (boolean fFirst : new boolean[] { true, false })
2322          for (Integer v1 : new Integer[] { 1, null })
2323      {
2324          final CompletableFuture<Integer> f = new CompletableFuture<>();
2325          final CompletableFuture<Integer> g = new CompletableFuture<>();
2326 <        final IncFunction r = new IncFunction();
2327 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2326 >        final IncFunction[] rs = new IncFunction[6];
2327 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2328  
2329 <        assertTrue(g.cancel(mayInterruptIfRunning));
2330 <        checkCompletedWithWrappedCancellationException(h);
2331 <        f.complete(v1);
2329 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2330 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2331 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2332 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2333 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2334 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2335  
2336 <        checkCancelled(g);
2287 <        assertEquals(0, r.invocationCount);
2288 <        checkCompletedNormally(f, v1);
2289 <        checkCompletedWithWrappedCancellationException(h);
2290 <    }}
2291 <
2292 <    public void testApplyToEither_sourceCancelled3() {
2293 <        for (ExecutionMode m : ExecutionMode.values())
2294 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2295 <        for (Integer v1 : new Integer[] { 1, null })
2296 <    {
2297 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2298 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2299 <        final IncFunction r = new IncFunction();
2300 <
2301 <        assertTrue(g.cancel(mayInterruptIfRunning));
2302 <        f.complete(v1);
2303 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2304 <
2305 <        // unspecified behavior
2306 <        Integer v;
2336 >        // unspecified behavior - both source completions available
2337          try {
2338 <            assertEquals(inc(v1), h.join());
2339 <            assertEquals(1, r.invocationCount);
2338 >            assertEquals(inc(v1), h0.join());
2339 >            rs[0].assertValue(inc(v1));
2340          } catch (CompletionException ok) {
2341 <            checkCompletedWithWrappedCancellationException(h);
2342 <            assertEquals(0, r.invocationCount);
2341 >            checkCompletedWithWrappedCancellationException(h0);
2342 >            rs[0].assertNotInvoked();
2343          }
2314
2315        checkCancelled(g);
2316        checkCompletedNormally(f, v1);
2317    }}
2318
2319    public void testApplyToEither_sourceCancelled4() {
2320        for (ExecutionMode m : ExecutionMode.values())
2321        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2322        for (Integer v1 : new Integer[] { 1, null })
2323    {
2324        final CompletableFuture<Integer> f = new CompletableFuture<>();
2325        final CompletableFuture<Integer> g = new CompletableFuture<>();
2326        final IncFunction r = new IncFunction();
2327
2328        assertTrue(f.cancel(mayInterruptIfRunning));
2329        g.complete(v1);
2330        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2331
2332        // unspecified behavior
2333        Integer v;
2344          try {
2345 <            assertEquals(inc(v1), h.join());
2346 <            assertEquals(1, r.invocationCount);
2345 >            assertEquals(inc(v1), h1.join());
2346 >            rs[1].assertValue(inc(v1));
2347          } catch (CompletionException ok) {
2348 <            checkCompletedWithWrappedCancellationException(h);
2349 <            assertEquals(0, r.invocationCount);
2348 >            checkCompletedWithWrappedCancellationException(h1);
2349 >            rs[1].assertNotInvoked();
2350 >        }
2351 >        try {
2352 >            assertEquals(inc(v1), h2.join());
2353 >            rs[2].assertValue(inc(v1));
2354 >        } catch (CompletionException ok) {
2355 >            checkCompletedWithWrappedCancellationException(h2);
2356 >            rs[2].assertNotInvoked();
2357 >        }
2358 >        try {
2359 >            assertEquals(inc(v1), h3.join());
2360 >            rs[3].assertValue(inc(v1));
2361 >        } catch (CompletionException ok) {
2362 >            checkCompletedWithWrappedCancellationException(h3);
2363 >            rs[3].assertNotInvoked();
2364          }
2365  
2366 <        checkCancelled(f);
2367 <        checkCompletedNormally(g, v1);
2366 >        checkCompletedNormally(f, v1);
2367 >        checkCancelled(g);
2368      }}
2369  
2370      /**
2371 <     * acceptEither result completes normally after normal completion
2348 <     * of either source
2371 >     * applyToEither result completes exceptionally if action does
2372       */
2373 <    public void testAcceptEither_normalCompletion1() {
2373 >    public void testApplyToEither_actionFailed() {
2374          for (ExecutionMode m : ExecutionMode.values())
2375          for (Integer v1 : new Integer[] { 1, null })
2376          for (Integer v2 : new Integer[] { 2, null })
2377      {
2378          final CompletableFuture<Integer> f = new CompletableFuture<>();
2379          final CompletableFuture<Integer> g = new CompletableFuture<>();
2380 <        final IncAction r = new IncAction();
2381 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2380 >        final FailingFunction[] rs = new FailingFunction[6];
2381 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2382  
2383 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2384 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2385          f.complete(v1);
2386 <        checkCompletedNormally(h, null);
2387 <        assertEquals(inc(v1), r.value);
2388 <        g.complete(v2);
2386 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2387 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2388 >        checkCompletedWithWrappedCFException(h0);
2389 >        checkCompletedWithWrappedCFException(h1);
2390 >        checkCompletedWithWrappedCFException(h2);
2391 >        checkCompletedWithWrappedCFException(h3);
2392 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2393  
2394 <        checkCompletedNormally(f, v1);
2366 <        checkCompletedNormally(g, v2);
2367 <        checkCompletedNormally(h, null);
2368 <    }}
2394 >        g.complete(v2);
2395  
2396 <    public void testAcceptEither_normalCompletion2() {
2397 <        for (ExecutionMode m : ExecutionMode.values())
2398 <        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);
2396 >        // unspecified behavior - both source completions available
2397 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2398 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2399  
2400 <        g.complete(v2);
2401 <        checkCompletedNormally(h, null);
2402 <        assertEquals(inc(v2), r.value);
2403 <        f.complete(v1);
2400 >        checkCompletedWithWrappedCFException(h4);
2401 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2402 >                   Objects.equals(v2, rs[4].value));
2403 >        checkCompletedWithWrappedCFException(h5);
2404 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2405 >                   Objects.equals(v2, rs[5].value));
2406  
2407          checkCompletedNormally(f, v1);
2408          checkCompletedNormally(g, v2);
2387        checkCompletedNormally(h, null);
2409      }}
2410  
2411 <    public void testAcceptEither_normalCompletion3() {
2411 >    /**
2412 >     * acceptEither result completes normally after normal completion
2413 >     * of either source
2414 >     */
2415 >    public void testAcceptEither_normalCompletion() {
2416          for (ExecutionMode m : ExecutionMode.values())
2417          for (Integer v1 : new Integer[] { 1, null })
2418          for (Integer v2 : new Integer[] { 2, null })
2419      {
2420          final CompletableFuture<Integer> f = new CompletableFuture<>();
2421          final CompletableFuture<Integer> g = new CompletableFuture<>();
2422 <        final IncAction r = new IncAction();
2422 >        final NoopConsumer[] rs = new NoopConsumer[6];
2423 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2424  
2425 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2426 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2427 +        checkIncomplete(h0);
2428 +        checkIncomplete(h1);
2429 +        rs[0].assertNotInvoked();
2430 +        rs[1].assertNotInvoked();
2431          f.complete(v1);
2432 +        checkCompletedNormally(h0, null);
2433 +        checkCompletedNormally(h1, null);
2434 +        rs[0].assertValue(v1);
2435 +        rs[1].assertValue(v1);
2436 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2437 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2438 +        checkCompletedNormally(h2, null);
2439 +        checkCompletedNormally(h3, null);
2440 +        rs[2].assertValue(v1);
2441 +        rs[3].assertValue(v1);
2442          g.complete(v2);
2401        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2443  
2444 <        checkCompletedNormally(h, null);
2444 >        // unspecified behavior - both source completions available
2445 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2446 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2447 >        checkCompletedNormally(h4, null);
2448 >        checkCompletedNormally(h5, null);
2449 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2450 >                   Objects.equals(v2, rs[4].value));
2451 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2452 >                   Objects.equals(v2, rs[5].value));
2453 >
2454          checkCompletedNormally(f, v1);
2455          checkCompletedNormally(g, v2);
2456 <
2457 <        // unspecified behavior
2458 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2459 <                   Objects.equals(r.value, inc(v2)));
2456 >        checkCompletedNormally(h0, null);
2457 >        checkCompletedNormally(h1, null);
2458 >        checkCompletedNormally(h2, null);
2459 >        checkCompletedNormally(h3, null);
2460 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2461      }}
2462  
2463      /**
2464       * acceptEither result completes exceptionally after exceptional
2465       * completion of either source
2466       */
2467 <    public void testAcceptEither_exceptionalCompletion1() {
2467 >    public void testAcceptEither_exceptionalCompletion() {
2468          for (ExecutionMode m : ExecutionMode.values())
2469          for (Integer v1 : new Integer[] { 1, null })
2470      {
2471          final CompletableFuture<Integer> f = new CompletableFuture<>();
2472          final CompletableFuture<Integer> g = new CompletableFuture<>();
2422        final IncAction r = new IncAction();
2423        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2473          final CFException ex = new CFException();
2474 +        final NoopConsumer[] rs = new NoopConsumer[6];
2475 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2476  
2477 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2478 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2479 +        checkIncomplete(h0);
2480 +        checkIncomplete(h1);
2481 +        rs[0].assertNotInvoked();
2482 +        rs[1].assertNotInvoked();
2483          f.completeExceptionally(ex);
2484 <        checkCompletedWithWrappedCFException(h, ex);
2484 >        checkCompletedWithWrappedException(h0, ex);
2485 >        checkCompletedWithWrappedException(h1, ex);
2486 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2487 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2488 >        checkCompletedWithWrappedException(h2, ex);
2489 >        checkCompletedWithWrappedException(h3, ex);
2490 >
2491          g.complete(v1);
2492  
2493 <        assertEquals(0, r.invocationCount);
2493 >        // unspecified behavior - both source completions available
2494 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2495 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2496 >        try {
2497 >            assertNull(h4.join());
2498 >            rs[4].assertValue(v1);
2499 >        } catch (CompletionException ok) {
2500 >            checkCompletedWithWrappedException(h4, ex);
2501 >            rs[4].assertNotInvoked();
2502 >        }
2503 >        try {
2504 >            assertNull(h5.join());
2505 >            rs[5].assertValue(v1);
2506 >        } catch (CompletionException ok) {
2507 >            checkCompletedWithWrappedException(h5, ex);
2508 >            rs[5].assertNotInvoked();
2509 >        }
2510 >
2511 >        checkCompletedExceptionally(f, ex);
2512          checkCompletedNormally(g, v1);
2513 <        checkCompletedWithWrappedCFException(f, ex);
2514 <        checkCompletedWithWrappedCFException(h, ex);
2513 >        checkCompletedWithWrappedException(h0, ex);
2514 >        checkCompletedWithWrappedException(h1, ex);
2515 >        checkCompletedWithWrappedException(h2, ex);
2516 >        checkCompletedWithWrappedException(h3, ex);
2517 >        checkCompletedWithWrappedException(h4, ex);
2518 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2519      }}
2520  
2521      public void testAcceptEither_exceptionalCompletion2() {
2522          for (ExecutionMode m : ExecutionMode.values())
2523 +        for (boolean fFirst : new boolean[] { true, false })
2524          for (Integer v1 : new Integer[] { 1, null })
2525      {
2526          final CompletableFuture<Integer> f = new CompletableFuture<>();
2527          final CompletableFuture<Integer> g = new CompletableFuture<>();
2442        final IncAction r = new IncAction();
2443        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2444        final CFException ex = new CFException();
2445
2446        g.completeExceptionally(ex);
2447        checkCompletedWithWrappedCFException(h, ex);
2448        f.complete(v1);
2449
2450        assertEquals(0, r.invocationCount);
2451        checkCompletedNormally(f, v1);
2452        checkCompletedWithWrappedCFException(g, ex);
2453        checkCompletedWithWrappedCFException(h, ex);
2454    }}
2455
2456    public void testAcceptEither_exceptionalCompletion3() {
2457        for (ExecutionMode m : ExecutionMode.values())
2458        for (Integer v1 : new Integer[] { 1, null })
2459    {
2460        final CompletableFuture<Integer> f = new CompletableFuture<>();
2461        final CompletableFuture<Integer> g = new CompletableFuture<>();
2462        final IncAction r = new IncAction();
2528          final CFException ex = new CFException();
2529 +        final NoopConsumer[] rs = new NoopConsumer[6];
2530 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2531  
2532 <        g.completeExceptionally(ex);
2533 <        f.complete(v1);
2534 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2532 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2533 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2534 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2535 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2536 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2537 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2538  
2539 <        // unspecified behavior
2470 <        Integer v;
2539 >        // unspecified behavior - both source completions available
2540          try {
2541 <            assertNull(h.join());
2542 <            assertEquals(1, r.invocationCount);
2474 <            assertEquals(inc(v1), r.value);
2541 >            assertEquals(null, h0.join());
2542 >            rs[0].assertValue(v1);
2543          } catch (CompletionException ok) {
2544 <            checkCompletedWithWrappedCFException(h, ex);
2545 <            assertEquals(0, r.invocationCount);
2544 >            checkCompletedWithWrappedException(h0, ex);
2545 >            rs[0].assertNotInvoked();
2546          }
2479
2480        checkCompletedWithWrappedCFException(g, ex);
2481        checkCompletedNormally(f, v1);
2482    }}
2483
2484    public void testAcceptEither_exceptionalCompletion4() {
2485        for (ExecutionMode m : ExecutionMode.values())
2486        for (Integer v1 : new Integer[] { 1, null })
2487    {
2488        final CompletableFuture<Integer> f = new CompletableFuture<>();
2489        final CompletableFuture<Integer> g = new CompletableFuture<>();
2490        final IncAction r = new IncAction();
2491        final CFException ex = new CFException();
2492
2493        f.completeExceptionally(ex);
2494        g.complete(v1);
2495        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2496
2497        // unspecified behavior
2498        Integer v;
2547          try {
2548 <            assertNull(h.join());
2549 <            assertEquals(1, r.invocationCount);
2502 <            assertEquals(inc(v1), r.value);
2548 >            assertEquals(null, h1.join());
2549 >            rs[1].assertValue(v1);
2550          } catch (CompletionException ok) {
2551 <            checkCompletedWithWrappedCFException(h, ex);
2552 <            assertEquals(0, r.invocationCount);
2551 >            checkCompletedWithWrappedException(h1, ex);
2552 >            rs[1].assertNotInvoked();
2553 >        }
2554 >        try {
2555 >            assertEquals(null, h2.join());
2556 >            rs[2].assertValue(v1);
2557 >        } catch (CompletionException ok) {
2558 >            checkCompletedWithWrappedException(h2, ex);
2559 >            rs[2].assertNotInvoked();
2560 >        }
2561 >        try {
2562 >            assertEquals(null, h3.join());
2563 >            rs[3].assertValue(v1);
2564 >        } catch (CompletionException ok) {
2565 >            checkCompletedWithWrappedException(h3, ex);
2566 >            rs[3].assertNotInvoked();
2567          }
2568  
2508        checkCompletedWithWrappedCFException(f, ex);
2509        checkCompletedNormally(g, v1);
2510    }}
2511
2512    /**
2513     * acceptEither result completes exceptionally if action does
2514     */
2515    public void testAcceptEither_actionFailed1() {
2516        for (ExecutionMode m : ExecutionMode.values())
2517        for (Integer v1 : new Integer[] { 1, null })
2518        for (Integer v2 : new Integer[] { 2, null })
2519    {
2520        final CompletableFuture<Integer> f = new CompletableFuture<>();
2521        final CompletableFuture<Integer> g = new CompletableFuture<>();
2522        final FailingConsumer r = new FailingConsumer();
2523        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2524
2525        f.complete(v1);
2526        checkCompletedWithWrappedCFException(h);
2527        g.complete(v2);
2528        checkCompletedNormally(f, v1);
2529        checkCompletedNormally(g, v2);
2530    }}
2531
2532    public void testAcceptEither_actionFailed2() {
2533        for (ExecutionMode m : ExecutionMode.values())
2534        for (Integer v1 : new Integer[] { 1, null })
2535        for (Integer v2 : new Integer[] { 2, null })
2536    {
2537        final CompletableFuture<Integer> f = new CompletableFuture<>();
2538        final CompletableFuture<Integer> g = new CompletableFuture<>();
2539        final FailingConsumer r = new FailingConsumer();
2540        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2541
2542        g.complete(v2);
2543        checkCompletedWithWrappedCFException(h);
2544        f.complete(v1);
2569          checkCompletedNormally(f, v1);
2570 <        checkCompletedNormally(g, v2);
2570 >        checkCompletedExceptionally(g, ex);
2571      }}
2572  
2573      /**
2574       * acceptEither result completes exceptionally if either source cancelled
2575       */
2576 <    public void testAcceptEither_sourceCancelled1() {
2576 >    public void testAcceptEither_sourceCancelled() {
2577          for (ExecutionMode m : ExecutionMode.values())
2578          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2579          for (Integer v1 : new Integer[] { 1, null })
2580      {
2581          final CompletableFuture<Integer> f = new CompletableFuture<>();
2582          final CompletableFuture<Integer> g = new CompletableFuture<>();
2583 <        final IncAction r = new IncAction();
2584 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2583 >        final NoopConsumer[] rs = new NoopConsumer[6];
2584 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2585  
2586 <        assertTrue(f.cancel(mayInterruptIfRunning));
2587 <        checkCompletedWithWrappedCancellationException(h);
2588 <        g.complete(v1);
2586 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2587 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2588 >        checkIncomplete(h0);
2589 >        checkIncomplete(h1);
2590 >        rs[0].assertNotInvoked();
2591 >        rs[1].assertNotInvoked();
2592 >        f.cancel(mayInterruptIfRunning);
2593 >        checkCompletedWithWrappedCancellationException(h0);
2594 >        checkCompletedWithWrappedCancellationException(h1);
2595 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2596 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2597 >        checkCompletedWithWrappedCancellationException(h2);
2598 >        checkCompletedWithWrappedCancellationException(h3);
2599  
2600 <        checkCancelled(f);
2567 <        assertEquals(0, r.invocationCount);
2568 <        checkCompletedNormally(g, v1);
2569 <        checkCompletedWithWrappedCancellationException(h);
2570 <    }}
2571 <
2572 <    public void testAcceptEither_sourceCancelled2() {
2573 <        for (ExecutionMode m : ExecutionMode.values())
2574 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2575 <        for (Integer v1 : new Integer[] { 1, null })
2576 <    {
2577 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2578 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2579 <        final IncAction r = new IncAction();
2580 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2581 <
2582 <        assertTrue(g.cancel(mayInterruptIfRunning));
2583 <        checkCompletedWithWrappedCancellationException(h);
2584 <        f.complete(v1);
2585 <
2586 <        checkCancelled(g);
2587 <        assertEquals(0, r.invocationCount);
2588 <        checkCompletedNormally(f, v1);
2589 <        checkCompletedWithWrappedCancellationException(h);
2590 <    }}
2591 <
2592 <    public void testAcceptEither_sourceCancelled3() {
2593 <        for (ExecutionMode m : ExecutionMode.values())
2594 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2595 <        for (Integer v1 : new Integer[] { 1, null })
2596 <    {
2597 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2598 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2599 <        final IncAction r = new IncAction();
2600 <
2601 <        assertTrue(g.cancel(mayInterruptIfRunning));
2602 <        f.complete(v1);
2603 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2600 >        g.complete(v1);
2601  
2602 <        // unspecified behavior
2603 <        Integer v;
2602 >        // unspecified behavior - both source completions available
2603 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2604 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2605          try {
2606 <            assertNull(h.join());
2607 <            assertEquals(1, r.invocationCount);
2610 <            assertEquals(inc(v1), r.value);
2606 >            assertNull(h4.join());
2607 >            rs[4].assertValue(v1);
2608          } catch (CompletionException ok) {
2609 <            checkCompletedWithWrappedCancellationException(h);
2610 <            assertEquals(0, r.invocationCount);
2609 >            checkCompletedWithWrappedCancellationException(h4);
2610 >            rs[4].assertNotInvoked();
2611          }
2615
2616        checkCancelled(g);
2617        checkCompletedNormally(f, v1);
2618    }}
2619
2620    public void testAcceptEither_sourceCancelled4() {
2621        for (ExecutionMode m : ExecutionMode.values())
2622        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2623        for (Integer v1 : new Integer[] { 1, null })
2624    {
2625        final CompletableFuture<Integer> f = new CompletableFuture<>();
2626        final CompletableFuture<Integer> g = new CompletableFuture<>();
2627        final IncAction r = new IncAction();
2628
2629        assertTrue(f.cancel(mayInterruptIfRunning));
2630        g.complete(v1);
2631        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2632
2633        // unspecified behavior
2634        Integer v;
2612          try {
2613 <            assertNull(h.join());
2614 <            assertEquals(1, r.invocationCount);
2638 <            assertEquals(inc(v1), r.value);
2613 >            assertNull(h5.join());
2614 >            rs[5].assertValue(v1);
2615          } catch (CompletionException ok) {
2616 <            checkCompletedWithWrappedCancellationException(h);
2617 <            assertEquals(0, r.invocationCount);
2616 >            checkCompletedWithWrappedCancellationException(h5);
2617 >            rs[5].assertNotInvoked();
2618          }
2619  
2620          checkCancelled(f);
2621          checkCompletedNormally(g, v1);
2622 +        checkCompletedWithWrappedCancellationException(h0);
2623 +        checkCompletedWithWrappedCancellationException(h1);
2624 +        checkCompletedWithWrappedCancellationException(h2);
2625 +        checkCompletedWithWrappedCancellationException(h3);
2626 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2627      }}
2628  
2629      /**
2630 <     * runAfterEither result completes normally after normal completion
2650 <     * of either source
2630 >     * acceptEither result completes exceptionally if action does
2631       */
2632 <    public void testRunAfterEither_normalCompletion1() {
2632 >    public void testAcceptEither_actionFailed() {
2633          for (ExecutionMode m : ExecutionMode.values())
2634          for (Integer v1 : new Integer[] { 1, null })
2635          for (Integer v2 : new Integer[] { 2, null })
2636      {
2637          final CompletableFuture<Integer> f = new CompletableFuture<>();
2638          final CompletableFuture<Integer> g = new CompletableFuture<>();
2639 <        final Noop r = new Noop();
2640 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2639 >        final FailingConsumer[] rs = new FailingConsumer[6];
2640 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2641  
2642 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2643 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2644          f.complete(v1);
2645 <        checkCompletedNormally(h, null);
2646 <        assertEquals(1, r.invocationCount);
2645 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2646 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2647 >        checkCompletedWithWrappedCFException(h0);
2648 >        checkCompletedWithWrappedCFException(h1);
2649 >        checkCompletedWithWrappedCFException(h2);
2650 >        checkCompletedWithWrappedCFException(h3);
2651 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2652 >
2653          g.complete(v2);
2654  
2655 <        checkCompletedNormally(f, v1);
2656 <        checkCompletedNormally(g, v2);
2657 <        checkCompletedNormally(h, null);
2670 <        assertEquals(1, r.invocationCount);
2671 <    }}
2655 >        // unspecified behavior - both source completions available
2656 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2657 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2658  
2659 <    public void testRunAfterEither_normalCompletion2() {
2660 <        for (ExecutionMode m : ExecutionMode.values())
2661 <        for (Integer v1 : new Integer[] { 1, null })
2662 <        for (Integer v2 : new Integer[] { 2, null })
2663 <    {
2664 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2679 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2680 <        final Noop r = new Noop();
2681 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2682 <
2683 <        g.complete(v2);
2684 <        checkCompletedNormally(h, null);
2685 <        assertEquals(1, r.invocationCount);
2686 <        f.complete(v1);
2659 >        checkCompletedWithWrappedCFException(h4);
2660 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2661 >                   Objects.equals(v2, rs[4].value));
2662 >        checkCompletedWithWrappedCFException(h5);
2663 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2664 >                   Objects.equals(v2, rs[5].value));
2665  
2666          checkCompletedNormally(f, v1);
2667          checkCompletedNormally(g, v2);
2668 <        checkCompletedNormally(h, null);
2691 <        assertEquals(1, r.invocationCount);
2692 <        }}
2668 >    }}
2669  
2670 <    public void testRunAfterEither_normalCompletion3() {
2670 >    /**
2671 >     * runAfterEither result completes normally after normal completion
2672 >     * of either source
2673 >     */
2674 >    public void testRunAfterEither_normalCompletion() {
2675          for (ExecutionMode m : ExecutionMode.values())
2676          for (Integer v1 : new Integer[] { 1, null })
2677          for (Integer v2 : new Integer[] { 2, null })
2678      {
2679          final CompletableFuture<Integer> f = new CompletableFuture<>();
2680          final CompletableFuture<Integer> g = new CompletableFuture<>();
2681 <        final Noop r = new Noop();
2681 >        final Noop[] rs = new Noop[6];
2682 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2683  
2684 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2685 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2686 +        checkIncomplete(h0);
2687 +        checkIncomplete(h1);
2688 +        rs[0].assertNotInvoked();
2689 +        rs[1].assertNotInvoked();
2690          f.complete(v1);
2691 +        checkCompletedNormally(h0, null);
2692 +        checkCompletedNormally(h1, null);
2693 +        rs[0].assertInvoked();
2694 +        rs[1].assertInvoked();
2695 +        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2696 +        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2697 +        checkCompletedNormally(h2, null);
2698 +        checkCompletedNormally(h3, null);
2699 +        rs[2].assertInvoked();
2700 +        rs[3].assertInvoked();
2701 +
2702          g.complete(v2);
2705        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2703  
2704 <        checkCompletedNormally(h, null);
2704 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2705 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2706 >
2707          checkCompletedNormally(f, v1);
2708          checkCompletedNormally(g, v2);
2709 <        assertEquals(1, r.invocationCount);
2709 >        checkCompletedNormally(h0, null);
2710 >        checkCompletedNormally(h1, null);
2711 >        checkCompletedNormally(h2, null);
2712 >        checkCompletedNormally(h3, null);
2713 >        checkCompletedNormally(h4, null);
2714 >        checkCompletedNormally(h5, null);
2715 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2716      }}
2717  
2718      /**
2719       * runAfterEither result completes exceptionally after exceptional
2720       * completion of either source
2721       */
2722 <    public void testRunAfterEither_exceptionalCompletion1() {
2718 <        for (ExecutionMode m : ExecutionMode.values())
2719 <        for (Integer v1 : new Integer[] { 1, null })
2720 <    {
2721 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2722 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2723 <        final Noop r = new Noop();
2724 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2725 <        final CFException ex = new CFException();
2726 <
2727 <        f.completeExceptionally(ex);
2728 <        checkCompletedWithWrappedCFException(h, ex);
2729 <        g.complete(v1);
2730 <
2731 <        assertEquals(0, r.invocationCount);
2732 <        checkCompletedNormally(g, v1);
2733 <        checkCompletedWithWrappedCFException(f, ex);
2734 <        checkCompletedWithWrappedCFException(h, ex);
2735 <    }}
2736 <
2737 <    public void testRunAfterEither_exceptionalCompletion2() {
2722 >    public void testRunAfterEither_exceptionalCompletion() {
2723          for (ExecutionMode m : ExecutionMode.values())
2724          for (Integer v1 : new Integer[] { 1, null })
2725      {
2726          final CompletableFuture<Integer> f = new CompletableFuture<>();
2727          final CompletableFuture<Integer> g = new CompletableFuture<>();
2743        final Noop r = new Noop();
2744        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2728          final CFException ex = new CFException();
2729 +        final Noop[] rs = new Noop[6];
2730 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2731  
2732 <        g.completeExceptionally(ex);
2733 <        checkCompletedWithWrappedCFException(h, ex);
2734 <        f.complete(v1);
2735 <
2736 <        assertEquals(0, r.invocationCount);
2737 <        checkCompletedNormally(f, v1);
2738 <        checkCompletedWithWrappedCFException(g, ex);
2739 <        checkCompletedWithWrappedCFException(h, ex);
2740 <    }}
2732 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2733 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2734 >        checkIncomplete(h0);
2735 >        checkIncomplete(h1);
2736 >        rs[0].assertNotInvoked();
2737 >        rs[1].assertNotInvoked();
2738 >        assertTrue(f.completeExceptionally(ex));
2739 >        checkCompletedWithWrappedException(h0, ex);
2740 >        checkCompletedWithWrappedException(h1, ex);
2741 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2742 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2743 >        checkCompletedWithWrappedException(h2, ex);
2744 >        checkCompletedWithWrappedException(h3, ex);
2745  
2746 <    public void testRunAfterEither_exceptionalCompletion3() {
2758 <        for (ExecutionMode m : ExecutionMode.values())
2759 <        for (Integer v1 : new Integer[] { 1, null })
2760 <    {
2761 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2762 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2763 <        final Noop r = new Noop();
2764 <        final CFException ex = new CFException();
2746 >        assertTrue(g.complete(v1));
2747  
2748 <        g.completeExceptionally(ex);
2749 <        f.complete(v1);
2750 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2769 <
2770 <        // unspecified behavior
2771 <        Integer v;
2748 >        // unspecified behavior - both source completions available
2749 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2750 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2751          try {
2752 <            assertNull(h.join());
2753 <            assertEquals(1, r.invocationCount);
2752 >            assertNull(h4.join());
2753 >            rs[4].assertInvoked();
2754          } catch (CompletionException ok) {
2755 <            checkCompletedWithWrappedCFException(h, ex);
2756 <            assertEquals(0, r.invocationCount);
2755 >            checkCompletedWithWrappedException(h4, ex);
2756 >            rs[4].assertNotInvoked();
2757          }
2779
2780        checkCompletedWithWrappedCFException(g, ex);
2781        checkCompletedNormally(f, v1);
2782    }}
2783
2784    public void testRunAfterEither_exceptionalCompletion4() {
2785        for (ExecutionMode m : ExecutionMode.values())
2786        for (Integer v1 : new Integer[] { 1, null })
2787    {
2788        final CompletableFuture<Integer> f = new CompletableFuture<>();
2789        final CompletableFuture<Integer> g = new CompletableFuture<>();
2790        final Noop r = new Noop();
2791        final CFException ex = new CFException();
2792
2793        f.completeExceptionally(ex);
2794        g.complete(v1);
2795        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2796
2797        // unspecified behavior
2798        Integer v;
2758          try {
2759 <            assertNull(h.join());
2760 <            assertEquals(1, r.invocationCount);
2759 >            assertNull(h5.join());
2760 >            rs[5].assertInvoked();
2761          } catch (CompletionException ok) {
2762 <            checkCompletedWithWrappedCFException(h, ex);
2763 <            assertEquals(0, r.invocationCount);
2762 >            checkCompletedWithWrappedException(h5, ex);
2763 >            rs[5].assertNotInvoked();
2764          }
2765  
2766 <        checkCompletedWithWrappedCFException(f, ex);
2766 >        checkCompletedExceptionally(f, ex);
2767          checkCompletedNormally(g, v1);
2768 +        checkCompletedWithWrappedException(h0, ex);
2769 +        checkCompletedWithWrappedException(h1, ex);
2770 +        checkCompletedWithWrappedException(h2, ex);
2771 +        checkCompletedWithWrappedException(h3, ex);
2772 +        checkCompletedWithWrappedException(h4, ex);
2773 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2774      }}
2775  
2776 <    /**
2812 <     * runAfterEither result completes exceptionally if action does
2813 <     */
2814 <    public void testRunAfterEither_actionFailed1() {
2776 >    public void testRunAfterEither_exceptionalCompletion2() {
2777          for (ExecutionMode m : ExecutionMode.values())
2778 +        for (boolean fFirst : new boolean[] { true, false })
2779          for (Integer v1 : new Integer[] { 1, null })
2817        for (Integer v2 : new Integer[] { 2, null })
2780      {
2781          final CompletableFuture<Integer> f = new CompletableFuture<>();
2782          final CompletableFuture<Integer> g = new CompletableFuture<>();
2783 <        final FailingRunnable r = new FailingRunnable();
2784 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2783 >        final CFException ex = new CFException();
2784 >        final Noop[] rs = new Noop[6];
2785 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2786  
2787 <        f.complete(v1);
2788 <        checkCompletedWithWrappedCFException(h);
2789 <        g.complete(v2);
2790 <        checkCompletedNormally(f, v1);
2791 <        checkCompletedNormally(g, v2);
2792 <    }}
2787 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2788 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2789 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2790 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2791 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2792 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2793  
2794 <    public void testRunAfterEither_actionFailed2() {
2795 <        for (ExecutionMode m : ExecutionMode.values())
2796 <        for (Integer v1 : new Integer[] { 1, null })
2797 <        for (Integer v2 : new Integer[] { 2, null })
2798 <    {
2799 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2800 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2801 <        final FailingRunnable r = new FailingRunnable();
2802 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2794 >        // unspecified behavior - both source completions available
2795 >        try {
2796 >            assertEquals(null, h0.join());
2797 >            rs[0].assertInvoked();
2798 >        } catch (CompletionException ok) {
2799 >            checkCompletedWithWrappedException(h0, ex);
2800 >            rs[0].assertNotInvoked();
2801 >        }
2802 >        try {
2803 >            assertEquals(null, h1.join());
2804 >            rs[1].assertInvoked();
2805 >        } catch (CompletionException ok) {
2806 >            checkCompletedWithWrappedException(h1, ex);
2807 >            rs[1].assertNotInvoked();
2808 >        }
2809 >        try {
2810 >            assertEquals(null, h2.join());
2811 >            rs[2].assertInvoked();
2812 >        } catch (CompletionException ok) {
2813 >            checkCompletedWithWrappedException(h2, ex);
2814 >            rs[2].assertNotInvoked();
2815 >        }
2816 >        try {
2817 >            assertEquals(null, h3.join());
2818 >            rs[3].assertInvoked();
2819 >        } catch (CompletionException ok) {
2820 >            checkCompletedWithWrappedException(h3, ex);
2821 >            rs[3].assertNotInvoked();
2822 >        }
2823  
2841        g.complete(v2);
2842        checkCompletedWithWrappedCFException(h);
2843        f.complete(v1);
2824          checkCompletedNormally(f, v1);
2825 <        checkCompletedNormally(g, v2);
2825 >        checkCompletedExceptionally(g, ex);
2826      }}
2827  
2828      /**
2829       * runAfterEither result completes exceptionally if either source cancelled
2830       */
2831 <    public void testRunAfterEither_sourceCancelled1() {
2852 <        for (ExecutionMode m : ExecutionMode.values())
2853 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2854 <        for (Integer v1 : new Integer[] { 1, null })
2855 <    {
2856 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2857 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2858 <        final Noop r = new Noop();
2859 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2860 <
2861 <        assertTrue(f.cancel(mayInterruptIfRunning));
2862 <        checkCompletedWithWrappedCancellationException(h);
2863 <        g.complete(v1);
2864 <
2865 <        checkCancelled(f);
2866 <        assertEquals(0, r.invocationCount);
2867 <        checkCompletedNormally(g, v1);
2868 <        checkCompletedWithWrappedCancellationException(h);
2869 <    }}
2870 <
2871 <    public void testRunAfterEither_sourceCancelled2() {
2831 >    public void testRunAfterEither_sourceCancelled() {
2832          for (ExecutionMode m : ExecutionMode.values())
2833          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2834          for (Integer v1 : new Integer[] { 1, null })
2835      {
2836          final CompletableFuture<Integer> f = new CompletableFuture<>();
2837          final CompletableFuture<Integer> g = new CompletableFuture<>();
2838 <        final Noop r = new Noop();
2839 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2838 >        final Noop[] rs = new Noop[6];
2839 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2840  
2841 <        assertTrue(g.cancel(mayInterruptIfRunning));
2842 <        checkCompletedWithWrappedCancellationException(h);
2843 <        f.complete(v1);
2841 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2842 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2843 >        checkIncomplete(h0);
2844 >        checkIncomplete(h1);
2845 >        rs[0].assertNotInvoked();
2846 >        rs[1].assertNotInvoked();
2847 >        f.cancel(mayInterruptIfRunning);
2848 >        checkCompletedWithWrappedCancellationException(h0);
2849 >        checkCompletedWithWrappedCancellationException(h1);
2850 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2851 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2852 >        checkCompletedWithWrappedCancellationException(h2);
2853 >        checkCompletedWithWrappedCancellationException(h3);
2854  
2855 <        checkCancelled(g);
2886 <        assertEquals(0, r.invocationCount);
2887 <        checkCompletedNormally(f, v1);
2888 <        checkCompletedWithWrappedCancellationException(h);
2889 <    }}
2855 >        assertTrue(g.complete(v1));
2856  
2857 <    public void testRunAfterEither_sourceCancelled3() {
2858 <        for (ExecutionMode m : ExecutionMode.values())
2859 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2894 <        for (Integer v1 : new Integer[] { 1, null })
2895 <    {
2896 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2897 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2898 <        final Noop r = new Noop();
2899 <
2900 <        assertTrue(g.cancel(mayInterruptIfRunning));
2901 <        f.complete(v1);
2902 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2903 <
2904 <        // unspecified behavior
2905 <        Integer v;
2857 >        // unspecified behavior - both source completions available
2858 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2859 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2860          try {
2861 <            assertNull(h.join());
2862 <            assertEquals(1, r.invocationCount);
2861 >            assertNull(h4.join());
2862 >            rs[4].assertInvoked();
2863          } catch (CompletionException ok) {
2864 <            checkCompletedWithWrappedCancellationException(h);
2865 <            assertEquals(0, r.invocationCount);
2864 >            checkCompletedWithWrappedCancellationException(h4);
2865 >            rs[4].assertNotInvoked();
2866 >        }
2867 >        try {
2868 >            assertNull(h5.join());
2869 >            rs[5].assertInvoked();
2870 >        } catch (CompletionException ok) {
2871 >            checkCompletedWithWrappedCancellationException(h5);
2872 >            rs[5].assertNotInvoked();
2873          }
2874  
2875 <        checkCancelled(g);
2876 <        checkCompletedNormally(f, v1);
2875 >        checkCancelled(f);
2876 >        checkCompletedNormally(g, v1);
2877 >        checkCompletedWithWrappedCancellationException(h0);
2878 >        checkCompletedWithWrappedCancellationException(h1);
2879 >        checkCompletedWithWrappedCancellationException(h2);
2880 >        checkCompletedWithWrappedCancellationException(h3);
2881 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2882      }}
2883  
2884 <    public void testRunAfterEither_sourceCancelled4() {
2884 >    /**
2885 >     * runAfterEither result completes exceptionally if action does
2886 >     */
2887 >    public void testRunAfterEither_actionFailed() {
2888          for (ExecutionMode m : ExecutionMode.values())
2920        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2889          for (Integer v1 : new Integer[] { 1, null })
2890 +        for (Integer v2 : new Integer[] { 2, null })
2891      {
2892          final CompletableFuture<Integer> f = new CompletableFuture<>();
2893          final CompletableFuture<Integer> g = new CompletableFuture<>();
2894 <        final Noop r = new Noop();
2894 >        final FailingRunnable[] rs = new FailingRunnable[6];
2895 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2896  
2897 <        assertTrue(f.cancel(mayInterruptIfRunning));
2898 <        g.complete(v1);
2899 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2897 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2898 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2899 >        assertTrue(f.complete(v1));
2900 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2901 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2902 >        checkCompletedWithWrappedCFException(h0);
2903 >        checkCompletedWithWrappedCFException(h1);
2904 >        checkCompletedWithWrappedCFException(h2);
2905 >        checkCompletedWithWrappedCFException(h3);
2906 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2907 >        assertTrue(g.complete(v2));
2908 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2909 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2910 >        checkCompletedWithWrappedCFException(h4);
2911 >        checkCompletedWithWrappedCFException(h5);
2912  
2913 <        // unspecified behavior
2914 <        Integer v;
2915 <        try {
2934 <            assertNull(h.join());
2935 <            assertEquals(1, r.invocationCount);
2936 <        } catch (CompletionException ok) {
2937 <            checkCompletedWithWrappedCancellationException(h);
2938 <            assertEquals(0, r.invocationCount);
2939 <        }
2940 <
2941 <        checkCancelled(f);
2942 <        checkCompletedNormally(g, v1);
2913 >        checkCompletedNormally(f, v1);
2914 >        checkCompletedNormally(g, v2);
2915 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2916      }}
2917  
2918      /**
# Line 2951 | Line 2924 | public class CompletableFutureTest exten
2924          for (Integer v1 : new Integer[] { 1, null })
2925      {
2926          final CompletableFuture<Integer> f = new CompletableFuture<>();
2927 <        final CompletableFutureInc r = new CompletableFutureInc();
2928 <        if (!createIncomplete) f.complete(v1);
2929 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2930 <        if (createIncomplete) f.complete(v1);
2927 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2928 >        if (!createIncomplete) assertTrue(f.complete(v1));
2929 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2930 >        if (createIncomplete) assertTrue(f.complete(v1));
2931  
2932          checkCompletedNormally(g, inc(v1));
2933          checkCompletedNormally(f, v1);
2934 <        assertEquals(1, r.invocationCount);
2934 >        r.assertValue(v1);
2935      }}
2936  
2937      /**
# Line 2970 | Line 2943 | public class CompletableFutureTest exten
2943          for (boolean createIncomplete : new boolean[] { true, false })
2944      {
2945          final CFException ex = new CFException();
2946 <        final CompletableFutureInc r = new CompletableFutureInc();
2946 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2947          final CompletableFuture<Integer> f = new CompletableFuture<>();
2948          if (!createIncomplete) f.completeExceptionally(ex);
2949 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2949 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2950          if (createIncomplete) f.completeExceptionally(ex);
2951  
2952 <        checkCompletedWithWrappedCFException(g, ex);
2953 <        checkCompletedWithWrappedCFException(f, ex);
2954 <        assertEquals(0, r.invocationCount);
2952 >        checkCompletedWithWrappedException(g, ex);
2953 >        checkCompletedExceptionally(f, ex);
2954 >        r.assertNotInvoked();
2955      }}
2956  
2957      /**
# Line 2991 | Line 2964 | public class CompletableFutureTest exten
2964      {
2965          final CompletableFuture<Integer> f = new CompletableFuture<>();
2966          final FailingCompletableFutureFunction r
2967 <            = new FailingCompletableFutureFunction();
2968 <        if (!createIncomplete) f.complete(v1);
2969 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2970 <        if (createIncomplete) f.complete(v1);
2967 >            = new FailingCompletableFutureFunction(m);
2968 >        if (!createIncomplete) assertTrue(f.complete(v1));
2969 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2970 >        if (createIncomplete) assertTrue(f.complete(v1));
2971  
2972          checkCompletedWithWrappedCFException(g);
2973          checkCompletedNormally(f, v1);
# Line 3009 | Line 2982 | public class CompletableFutureTest exten
2982          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2983      {
2984          final CompletableFuture<Integer> f = new CompletableFuture<>();
2985 <        final CompletableFutureInc r = new CompletableFutureInc();
2985 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2986          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2987 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2987 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2988          if (createIncomplete) {
2989              checkIncomplete(g);
2990              assertTrue(f.cancel(mayInterruptIfRunning));
# Line 3021 | Line 2994 | public class CompletableFutureTest exten
2994          checkCancelled(f);
2995      }}
2996  
2997 +    /**
2998 +     * thenCompose result completes exceptionally if the result of the action does
2999 +     */
3000 +    public void testThenCompose_actionReturnsFailingFuture() {
3001 +        for (ExecutionMode m : ExecutionMode.values())
3002 +        for (int order = 0; order < 6; order++)
3003 +        for (Integer v1 : new Integer[] { 1, null })
3004 +    {
3005 +        final CFException ex = new CFException();
3006 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3007 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
3008 +        final CompletableFuture<Integer> h;
3009 +        // Test all permutations of orders
3010 +        switch (order) {
3011 +        case 0:
3012 +            assertTrue(f.complete(v1));
3013 +            assertTrue(g.completeExceptionally(ex));
3014 +            h = m.thenCompose(f, (x -> g));
3015 +            break;
3016 +        case 1:
3017 +            assertTrue(f.complete(v1));
3018 +            h = m.thenCompose(f, (x -> g));
3019 +            assertTrue(g.completeExceptionally(ex));
3020 +            break;
3021 +        case 2:
3022 +            assertTrue(g.completeExceptionally(ex));
3023 +            assertTrue(f.complete(v1));
3024 +            h = m.thenCompose(f, (x -> g));
3025 +            break;
3026 +        case 3:
3027 +            assertTrue(g.completeExceptionally(ex));
3028 +            h = m.thenCompose(f, (x -> g));
3029 +            assertTrue(f.complete(v1));
3030 +            break;
3031 +        case 4:
3032 +            h = m.thenCompose(f, (x -> g));
3033 +            assertTrue(f.complete(v1));
3034 +            assertTrue(g.completeExceptionally(ex));
3035 +            break;
3036 +        case 5:
3037 +            h = m.thenCompose(f, (x -> g));
3038 +            assertTrue(f.complete(v1));
3039 +            assertTrue(g.completeExceptionally(ex));
3040 +            break;
3041 +        default: throw new AssertionError();
3042 +        }
3043 +
3044 +        checkCompletedExceptionally(g, ex);
3045 +        checkCompletedWithWrappedException(h, ex);
3046 +        checkCompletedNormally(f, v1);
3047 +    }}
3048 +
3049      // other static methods
3050  
3051      /**
# Line 3037 | Line 3062 | public class CompletableFutureTest exten
3062       * when all components complete normally
3063       */
3064      public void testAllOf_normal() throws Exception {
3065 <        for (int k = 1; k < 20; ++k) {
3066 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3067 <            for (int i = 0; i < k; ++i)
3065 >        for (int k = 1; k < 10; k++) {
3066 >            CompletableFuture<Integer>[] fs
3067 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3068 >            for (int i = 0; i < k; i++)
3069 >                fs[i] = new CompletableFuture<>();
3070 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3071 >            for (int i = 0; i < k; i++) {
3072 >                checkIncomplete(f);
3073 >                checkIncomplete(CompletableFuture.allOf(fs));
3074 >                fs[i].complete(one);
3075 >            }
3076 >            checkCompletedNormally(f, null);
3077 >            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3078 >        }
3079 >    }
3080 >
3081 >    public void testAllOf_backwards() throws Exception {
3082 >        for (int k = 1; k < 10; k++) {
3083 >            CompletableFuture<Integer>[] fs
3084 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3085 >            for (int i = 0; i < k; i++)
3086                  fs[i] = new CompletableFuture<>();
3087              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3088 <            for (int i = 0; i < k; ++i) {
3088 >            for (int i = k - 1; i >= 0; i--) {
3089                  checkIncomplete(f);
3090                  checkIncomplete(CompletableFuture.allOf(fs));
3091                  fs[i].complete(one);
# Line 3052 | Line 3095 | public class CompletableFutureTest exten
3095          }
3096      }
3097  
3098 +    public void testAllOf_exceptional() throws Exception {
3099 +        for (int k = 1; k < 10; k++) {
3100 +            CompletableFuture<Integer>[] fs
3101 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3102 +            CFException ex = new CFException();
3103 +            for (int i = 0; i < k; i++)
3104 +                fs[i] = new CompletableFuture<>();
3105 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3106 +            for (int i = 0; i < k; i++) {
3107 +                checkIncomplete(f);
3108 +                checkIncomplete(CompletableFuture.allOf(fs));
3109 +                if (i != k/2) {
3110 +                    fs[i].complete(i);
3111 +                    checkCompletedNormally(fs[i], i);
3112 +                } else {
3113 +                    fs[i].completeExceptionally(ex);
3114 +                    checkCompletedExceptionally(fs[i], ex);
3115 +                }
3116 +            }
3117 +            checkCompletedWithWrappedException(f, ex);
3118 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3119 +        }
3120 +    }
3121 +
3122      /**
3123       * anyOf(no component futures) returns an incomplete future
3124       */
3125      public void testAnyOf_empty() throws Exception {
3126 +        for (Integer v1 : new Integer[] { 1, null })
3127 +    {
3128          CompletableFuture<Object> f = CompletableFuture.anyOf();
3129          checkIncomplete(f);
3130 <    }
3130 >
3131 >        f.complete(v1);
3132 >        checkCompletedNormally(f, v1);
3133 >    }}
3134  
3135      /**
3136       * anyOf returns a future completed normally with a value when
3137       * a component future does
3138       */
3139      public void testAnyOf_normal() throws Exception {
3140 <        for (int k = 0; k < 10; ++k) {
3140 >        for (int k = 0; k < 10; k++) {
3141              CompletableFuture[] fs = new CompletableFuture[k];
3142 <            for (int i = 0; i < k; ++i)
3142 >            for (int i = 0; i < k; i++)
3143                  fs[i] = new CompletableFuture<>();
3144              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3145              checkIncomplete(f);
3146 <            for (int i = 0; i < k; ++i) {
3147 <                fs[i].complete(one);
3148 <                checkCompletedNormally(f, one);
3149 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3146 >            for (int i = 0; i < k; i++) {
3147 >                fs[i].complete(i);
3148 >                checkCompletedNormally(f, 0);
3149 >                int x = (int) CompletableFuture.anyOf(fs).join();
3150 >                assertTrue(0 <= x && x <= i);
3151 >            }
3152 >        }
3153 >    }
3154 >    public void testAnyOf_normal_backwards() throws Exception {
3155 >        for (int k = 0; k < 10; k++) {
3156 >            CompletableFuture[] fs = new CompletableFuture[k];
3157 >            for (int i = 0; i < k; i++)
3158 >                fs[i] = new CompletableFuture<>();
3159 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3160 >            checkIncomplete(f);
3161 >            for (int i = k - 1; i >= 0; i--) {
3162 >                fs[i].complete(i);
3163 >                checkCompletedNormally(f, k - 1);
3164 >                int x = (int) CompletableFuture.anyOf(fs).join();
3165 >                assertTrue(i <= x && x <= k - 1);
3166              }
3167          }
3168      }
# Line 3083 | Line 3171 | public class CompletableFutureTest exten
3171       * anyOf result completes exceptionally when any component does.
3172       */
3173      public void testAnyOf_exceptional() throws Exception {
3174 <        for (int k = 0; k < 10; ++k) {
3174 >        for (int k = 0; k < 10; k++) {
3175              CompletableFuture[] fs = new CompletableFuture[k];
3176 <            for (int i = 0; i < k; ++i)
3176 >            CFException[] exs = new CFException[k];
3177 >            for (int i = 0; i < k; i++) {
3178                  fs[i] = new CompletableFuture<>();
3179 +                exs[i] = new CFException();
3180 +            }
3181              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3182              checkIncomplete(f);
3183 <            for (int i = 0; i < k; ++i) {
3184 <                fs[i].completeExceptionally(new CFException());
3185 <                checkCompletedWithWrappedCFException(f);
3183 >            for (int i = 0; i < k; i++) {
3184 >                fs[i].completeExceptionally(exs[i]);
3185 >                checkCompletedWithWrappedException(f, exs[0]);
3186 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3187 >            }
3188 >        }
3189 >    }
3190 >
3191 >    public void testAnyOf_exceptional_backwards() throws Exception {
3192 >        for (int k = 0; k < 10; k++) {
3193 >            CompletableFuture[] fs = new CompletableFuture[k];
3194 >            CFException[] exs = new CFException[k];
3195 >            for (int i = 0; i < k; i++) {
3196 >                fs[i] = new CompletableFuture<>();
3197 >                exs[i] = new CFException();
3198 >            }
3199 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3200 >            checkIncomplete(f);
3201 >            for (int i = k - 1; i >= 0; i--) {
3202 >                fs[i].completeExceptionally(exs[i]);
3203 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3204                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3205              }
3206          }
# Line 3110 | Line 3219 | public class CompletableFutureTest exten
3219          Runnable[] throwingActions = {
3220              () -> CompletableFuture.supplyAsync(null),
3221              () -> CompletableFuture.supplyAsync(null, exec),
3222 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
3222 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3223  
3224              () -> CompletableFuture.runAsync(null),
3225              () -> CompletableFuture.runAsync(null, exec),
# Line 3183 | Line 3292 | public class CompletableFutureTest exten
3292  
3293              () -> f.thenCompose(null),
3294              () -> f.thenComposeAsync(null),
3295 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
3295 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
3296              () -> f.thenComposeAsync(null, exec),
3297  
3298              () -> f.exceptionally(null),
# Line 3215 | Line 3324 | public class CompletableFutureTest exten
3324          assertSame(f, f.toCompletableFuture());
3325      }
3326  
3327 <    /**
3219 <     * whenComplete action executes on normal completion, propagating
3220 <     * source result.
3221 <     */
3222 <    public void testWhenComplete_normalCompletion1() {
3223 <        for (ExecutionMode m : ExecutionMode.values())
3224 <        for (boolean createIncomplete : new boolean[] { true, false })
3225 <        for (Integer v1 : new Integer[] { 1, null })
3226 <    {
3227 <        final AtomicInteger a = new AtomicInteger(0);
3228 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3229 <        if (!createIncomplete) f.complete(v1);
3230 <        final CompletableFuture<Integer> g = m.whenComplete
3231 <            (f,
3232 <             (Integer x, Throwable t) -> {
3233 <                threadAssertSame(x, v1);
3234 <                threadAssertNull(t);
3235 <                a.getAndIncrement();
3236 <            });
3237 <        if (createIncomplete) f.complete(v1);
3327 >    //--- tests of implementation details; not part of official tck ---
3328  
3329 <        checkCompletedNormally(g, v1);
3330 <        checkCompletedNormally(f, v1);
3331 <        assertEquals(1, a.get());
3332 <    }}
3329 >    Object resultOf(CompletableFuture<?> f) {
3330 >        try {
3331 >            java.lang.reflect.Field resultField
3332 >                = CompletableFuture.class.getDeclaredField("result");
3333 >            resultField.setAccessible(true);
3334 >            return resultField.get(f);
3335 >        } catch (Throwable t) { throw new AssertionError(t); }
3336 >    }
3337  
3338 <    /**
3339 <     * whenComplete action executes on exceptional completion, propagating
3246 <     * source result.
3247 <     */
3248 <    public void testWhenComplete_exceptionalCompletion() {
3338 >    public void testExceptionPropagationReusesResultObject() {
3339 >        if (!testImplementationDetails) return;
3340          for (ExecutionMode m : ExecutionMode.values())
3250        for (boolean createIncomplete : new boolean[] { true, false })
3251        for (Integer v1 : new Integer[] { 1, null })
3341      {
3253        final AtomicInteger a = new AtomicInteger(0);
3342          final CFException ex = new CFException();
3343 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3344 <        if (!createIncomplete) f.completeExceptionally(ex);
3257 <        final CompletableFuture<Integer> g = m.whenComplete
3258 <            (f,
3259 <             (Integer x, Throwable t) -> {
3260 <                threadAssertNull(x);
3261 <                threadAssertSame(t, ex);
3262 <                a.getAndIncrement();
3263 <            });
3264 <        if (createIncomplete) f.completeExceptionally(ex);
3265 <        checkCompletedWithWrappedCFException(f, ex);
3266 <        checkCompletedWithWrappedCFException(g, ex);
3267 <        assertEquals(1, a.get());
3268 <    }}
3343 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3344 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3345  
3346 <    /**
3347 <     * whenComplete action executes on cancelled source, propagating
3272 <     * CancellationException.
3273 <     */
3274 <    public void testWhenComplete_sourceCancelled() {
3275 <        for (ExecutionMode m : ExecutionMode.values())
3276 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3277 <        for (boolean createIncomplete : new boolean[] { true, false })
3278 <    {
3279 <        final AtomicInteger a = new AtomicInteger(0);
3280 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3281 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3282 <        final CompletableFuture<Integer> g = m.whenComplete
3283 <            (f,
3284 <             (Integer x, Throwable t) -> {
3285 <                threadAssertNull(x);
3286 <                threadAssertTrue(t instanceof CancellationException);
3287 <                a.getAndIncrement();
3288 <            });
3289 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3346 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3347 >            = new ArrayList<>();
3348  
3349 <        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
3350 <        checkCompletedWithWrappedCancellationException(g);
3351 <        checkCancelled(f);
3294 <        assertEquals(1, a.get());
3295 <    }}
3349 >        funs.add((y) -> m.thenRun(y, new Noop(m)));
3350 >        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3351 >        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3352  
3353 <    /**
3354 <     * If a whenComplete action throws an exception when triggered by
3355 <     * a normal completion, it completes exceptionally
3300 <     */
3301 <    public void testWhenComplete_actionFailed() {
3302 <        for (boolean createIncomplete : new boolean[] { true, false })
3303 <        for (ExecutionMode m : ExecutionMode.values())
3304 <        for (Integer v1 : new Integer[] { 1, null })
3305 <    {
3306 <        final AtomicInteger a = new AtomicInteger(0);
3307 <        final CFException ex = new CFException();
3308 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3309 <        if (!createIncomplete) f.complete(v1);
3310 <        final CompletableFuture<Integer> g = m.whenComplete
3311 <            (f,
3312 <             (Integer x, Throwable t) -> {
3313 <                threadAssertSame(x, v1);
3314 <                threadAssertNull(t);
3315 <                a.getAndIncrement();
3316 <                throw ex;
3317 <            });
3318 <        if (createIncomplete) f.complete(v1);
3319 <        checkCompletedNormally(f, v1);
3320 <        checkCompletedWithWrappedCFException(g, ex);
3321 <        assertEquals(1, a.get());
3322 <    }}
3353 >        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3354 >        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3355 >        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3356  
3357 <    /**
3358 <     * If a whenComplete action throws an exception when triggered by
3359 <     * a source completion that also throws an exception, the source
3327 <     * exception takes precedence.
3328 <     */
3329 <    public void testWhenComplete_actionFailedSourceFailed() {
3330 <        for (boolean createIncomplete : new boolean[] { true, false })
3331 <        for (ExecutionMode m : ExecutionMode.values())
3332 <        for (Integer v1 : new Integer[] { 1, null })
3333 <    {
3334 <        final AtomicInteger a = new AtomicInteger(0);
3335 <        final CFException ex1 = new CFException();
3336 <        final CFException ex2 = new CFException();
3337 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3357 >        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3358 >        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3359 >        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3360  
3361 <        if (!createIncomplete) f.completeExceptionally(ex1);
3340 <        final CompletableFuture<Integer> g = m.whenComplete
3341 <            (f,
3342 <             (Integer x, Throwable t) -> {
3343 <                threadAssertSame(t, ex1);
3344 <                threadAssertNull(x);
3345 <                a.getAndIncrement();
3346 <                throw ex2;
3347 <            });
3348 <        if (createIncomplete) f.completeExceptionally(ex1);
3361 >        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3362  
3363 <        checkCompletedWithWrappedCFException(f, ex1);
3364 <        checkCompletedWithWrappedCFException(g, ex1);
3365 <        assertEquals(1, a.get());
3363 >        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3364 >
3365 >        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3366 >        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3367 >
3368 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3369 >                 fun : funs) {
3370 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3371 >            f.completeExceptionally(ex);
3372 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3373 >            checkCompletedWithWrappedException(src, ex);
3374 >            CompletableFuture<?> dep = fun.apply(src);
3375 >            checkCompletedWithWrappedException(dep, ex);
3376 >            assertSame(resultOf(src), resultOf(dep));
3377 >        }
3378 >
3379 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3380 >                 fun : funs) {
3381 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3382 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3383 >            CompletableFuture<?> dep = fun.apply(src);
3384 >            f.completeExceptionally(ex);
3385 >            checkCompletedWithWrappedException(src, ex);
3386 >            checkCompletedWithWrappedException(dep, ex);
3387 >            assertSame(resultOf(src), resultOf(dep));
3388 >        }
3389 >
3390 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3391 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3392 >                 fun : funs) {
3393 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3394 >            f.cancel(mayInterruptIfRunning);
3395 >            checkCancelled(f);
3396 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3397 >            checkCompletedWithWrappedCancellationException(src);
3398 >            CompletableFuture<?> dep = fun.apply(src);
3399 >            checkCompletedWithWrappedCancellationException(dep);
3400 >            assertSame(resultOf(src), resultOf(dep));
3401 >        }
3402 >
3403 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3404 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3405 >                 fun : funs) {
3406 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3407 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3408 >            CompletableFuture<?> dep = fun.apply(src);
3409 >            f.cancel(mayInterruptIfRunning);
3410 >            checkCancelled(f);
3411 >            checkCompletedWithWrappedCancellationException(src);
3412 >            checkCompletedWithWrappedCancellationException(dep);
3413 >            assertSame(resultOf(src), resultOf(dep));
3414 >        }
3415      }}
3416  
3417   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines