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.41 by jsr166, Mon Jun 2 01:46:11 2014 UTC vs.
Revision 1.97 by jsr166, Wed Dec 31 16:44:01 2014 UTC

# Line 8 | Line 8
8   import junit.framework.*;
9   import java.util.concurrent.Callable;
10   import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
11   import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
12   import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
13   import java.util.concurrent.CompletableFuture;
14   import java.util.concurrent.CompletionException;
15   import java.util.concurrent.CompletionStage;
16 + import java.util.concurrent.ForkJoinPool;
17 + import java.util.concurrent.ForkJoinTask;
18   import java.util.concurrent.TimeoutException;
19   import java.util.concurrent.atomic.AtomicInteger;
20   import static java.util.concurrent.TimeUnit.MILLISECONDS;
# Line 55 | Line 53 | public class CompletableFutureTest exten
53      }
54  
55      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
56 <        try {
57 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
60 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
56 >        checkTimedGet(f, value);
57 >
58          try {
59              assertEquals(value, f.join());
60          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 74 | Line 71 | public class CompletableFutureTest exten
71      }
72  
73      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
74 +        long startTime = System.nanoTime();
75 +        long timeoutMillis = LONG_DELAY_MS;
76          try {
77 <            f.get(LONG_DELAY_MS, MILLISECONDS);
77 >            f.get(timeoutMillis, MILLISECONDS);
78              shouldThrow();
79          } catch (ExecutionException success) {
80              assertTrue(success.getCause() instanceof CFException);
81          } catch (Throwable fail) { threadUnexpectedException(fail); }
82 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
83 +
84          try {
85              f.join();
86              shouldThrow();
# Line 103 | Line 104 | public class CompletableFutureTest exten
104          assertTrue(f.toString().contains("[Completed exceptionally]"));
105      }
106  
107 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
108 <                                              CFException ex) {
107 >    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
108 >                                                      Throwable ex) {
109 >        long startTime = System.nanoTime();
110 >        long timeoutMillis = LONG_DELAY_MS;
111          try {
112 <            f.get(LONG_DELAY_MS, MILLISECONDS);
112 >            f.get(timeoutMillis, MILLISECONDS);
113              shouldThrow();
114          } catch (ExecutionException success) {
115              assertSame(ex, success.getCause());
116          } catch (Throwable fail) { threadUnexpectedException(fail); }
117 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
118 +
119          try {
120              f.join();
121              shouldThrow();
# Line 129 | Line 134 | public class CompletableFutureTest exten
134          } catch (ExecutionException success) {
135              assertSame(ex, success.getCause());
136          } catch (Throwable fail) { threadUnexpectedException(fail); }
137 +
138          assertTrue(f.isDone());
139          assertFalse(f.isCancelled());
140          assertTrue(f.toString().contains("[Completed exceptionally]"));
141      }
142  
143 +    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
144 +                                                Throwable ex) {
145 +        checkCompletedExceptionallyWithRootCause(f, ex);
146 +        try {
147 +            CompletableFuture<Throwable> spy = f.handle
148 +                ((U u, Throwable t) -> t);
149 +            assertTrue(spy.join() instanceof CompletionException);
150 +            assertSame(ex, spy.join().getCause());
151 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
152 +    }
153 +
154 +    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
155 +        checkCompletedExceptionallyWithRootCause(f, ex);
156 +        try {
157 +            CompletableFuture<Throwable> spy = f.handle
158 +                ((U u, Throwable t) -> t);
159 +            assertSame(ex, spy.join());
160 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
161 +    }
162 +
163      void checkCancelled(CompletableFuture<?> f) {
164 +        long startTime = System.nanoTime();
165 +        long timeoutMillis = LONG_DELAY_MS;
166          try {
167 <            f.get(LONG_DELAY_MS, MILLISECONDS);
167 >            f.get(timeoutMillis, MILLISECONDS);
168              shouldThrow();
169          } catch (CancellationException success) {
170          } catch (Throwable fail) { threadUnexpectedException(fail); }
171 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
172 +
173          try {
174              f.join();
175              shouldThrow();
# Line 160 | Line 190 | public class CompletableFutureTest exten
190      }
191  
192      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
193 +        long startTime = System.nanoTime();
194 +        long timeoutMillis = LONG_DELAY_MS;
195          try {
196 <            f.get(LONG_DELAY_MS, MILLISECONDS);
196 >            f.get(timeoutMillis, MILLISECONDS);
197              shouldThrow();
198          } catch (ExecutionException success) {
199              assertTrue(success.getCause() instanceof CancellationException);
200          } catch (Throwable fail) { threadUnexpectedException(fail); }
201 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
202 +
203          try {
204              f.join();
205              shouldThrow();
# Line 204 | Line 238 | public class CompletableFutureTest exten
238       * isCancelled, join, get, and getNow
239       */
240      public void testComplete() {
241 +        for (Integer v1 : new Integer[] { 1, null })
242 +    {
243          CompletableFuture<Integer> f = new CompletableFuture<>();
244          checkIncomplete(f);
245 <        f.complete(one);
246 <        checkCompletedNormally(f, one);
247 <    }
245 >        assertTrue(f.complete(v1));
246 >        assertFalse(f.complete(v1));
247 >        checkCompletedNormally(f, v1);
248 >    }}
249  
250      /**
251       * completeExceptionally completes exceptionally, as indicated by
# Line 216 | Line 253 | public class CompletableFutureTest exten
253       */
254      public void testCompleteExceptionally() {
255          CompletableFuture<Integer> f = new CompletableFuture<>();
256 +        CFException ex = new CFException();
257          checkIncomplete(f);
258 <        f.completeExceptionally(new CFException());
259 <        checkCompletedWithWrappedCFException(f);
258 >        f.completeExceptionally(ex);
259 >        checkCompletedExceptionally(f, ex);
260      }
261  
262      /**
# Line 226 | Line 264 | public class CompletableFutureTest exten
264       * methods isDone, isCancelled, join, get, and getNow
265       */
266      public void testCancel() {
267 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
268 +    {
269          CompletableFuture<Integer> f = new CompletableFuture<>();
270          checkIncomplete(f);
271          assertTrue(f.cancel(true));
272 +        assertTrue(f.cancel(true));
273          checkCancelled(f);
274 <    }
274 >    }}
275  
276      /**
277       * obtrudeValue forces completion with given value
# Line 238 | Line 279 | public class CompletableFutureTest exten
279      public void testObtrudeValue() {
280          CompletableFuture<Integer> f = new CompletableFuture<>();
281          checkIncomplete(f);
282 <        f.complete(one);
282 >        assertTrue(f.complete(one));
283          checkCompletedNormally(f, one);
284          f.obtrudeValue(three);
285          checkCompletedNormally(f, three);
# Line 247 | Line 288 | public class CompletableFutureTest exten
288          f = new CompletableFuture<>();
289          f.obtrudeValue(three);
290          checkCompletedNormally(f, three);
291 +        f.obtrudeValue(null);
292 +        checkCompletedNormally(f, null);
293          f = new CompletableFuture<>();
294          f.completeExceptionally(new CFException());
295          f.obtrudeValue(four);
# Line 257 | Line 300 | public class CompletableFutureTest exten
300       * obtrudeException forces completion with given exception
301       */
302      public void testObtrudeException() {
303 <        CompletableFuture<Integer> f = new CompletableFuture<>();
304 <        checkIncomplete(f);
305 <        f.complete(one);
306 <        checkCompletedNormally(f, one);
307 <        f.obtrudeException(new CFException());
265 <        checkCompletedWithWrappedCFException(f);
303 >        for (Integer v1 : new Integer[] { 1, null })
304 >    {
305 >        CFException ex;
306 >        CompletableFuture<Integer> f;
307 >
308          f = new CompletableFuture<>();
309 <        f.obtrudeException(new CFException());
310 <        checkCompletedWithWrappedCFException(f);
309 >        assertTrue(f.complete(v1));
310 >        for (int i = 0; i < 2; i++) {
311 >            f.obtrudeException(ex = new CFException());
312 >            checkCompletedExceptionally(f, ex);
313 >        }
314 >
315          f = new CompletableFuture<>();
316 +        for (int i = 0; i < 2; i++) {
317 +            f.obtrudeException(ex = new CFException());
318 +            checkCompletedExceptionally(f, ex);
319 +        }
320 +
321 +        f = new CompletableFuture<>();
322 +        f.completeExceptionally(ex = new CFException());
323 +        f.obtrudeValue(v1);
324 +        checkCompletedNormally(f, v1);
325 +        f.obtrudeException(ex = new CFException());
326 +        checkCompletedExceptionally(f, ex);
327          f.completeExceptionally(new CFException());
328 <        f.obtrudeValue(four);
329 <        checkCompletedNormally(f, four);
330 <        f.obtrudeException(new CFException());
331 <        checkCompletedWithWrappedCFException(f);
275 <    }
328 >        checkCompletedExceptionally(f, ex);
329 >        assertFalse(f.complete(v1));
330 >        checkCompletedExceptionally(f, ex);
331 >    }}
332  
333      /**
334       * getNumberOfDependents returns number of dependent tasks
335       */
336      public void testGetNumberOfDependents() {
337 +        for (ExecutionMode m : ExecutionMode.values())
338 +        for (Integer v1 : new Integer[] { 1, null })
339 +    {
340          CompletableFuture<Integer> f = new CompletableFuture<>();
341 <        assertEquals(f.getNumberOfDependents(), 0);
342 <        CompletableFuture g = f.thenRun(new Noop());
343 <        assertEquals(f.getNumberOfDependents(), 1);
344 <        assertEquals(g.getNumberOfDependents(), 0);
345 <        CompletableFuture h = f.thenRun(new Noop());
346 <        assertEquals(f.getNumberOfDependents(), 2);
347 <        f.complete(1);
341 >        assertEquals(0, f.getNumberOfDependents());
342 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
343 >        assertEquals(1, f.getNumberOfDependents());
344 >        assertEquals(0, g.getNumberOfDependents());
345 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
346 >        assertEquals(2, f.getNumberOfDependents());
347 >        assertEquals(0, h.getNumberOfDependents());
348 >        assertTrue(f.complete(v1));
349          checkCompletedNormally(g, null);
350 <        assertEquals(f.getNumberOfDependents(), 0);
351 <        assertEquals(g.getNumberOfDependents(), 0);
352 <    }
350 >        checkCompletedNormally(h, null);
351 >        assertEquals(0, f.getNumberOfDependents());
352 >        assertEquals(0, g.getNumberOfDependents());
353 >        assertEquals(0, h.getNumberOfDependents());
354 >    }}
355  
356      /**
357       * toString indicates current completion state
# Line 300 | Line 362 | public class CompletableFutureTest exten
362          f = new CompletableFuture<String>();
363          assertTrue(f.toString().contains("[Not completed]"));
364  
365 <        f.complete("foo");
365 >        assertTrue(f.complete("foo"));
366          assertTrue(f.toString().contains("[Completed normally]"));
367  
368          f = new CompletableFuture<String>();
369 <        f.completeExceptionally(new IndexOutOfBoundsException());
369 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
370          assertTrue(f.toString().contains("[Completed exceptionally]"));
371 +
372 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
373 +            f = new CompletableFuture<String>();
374 +            assertTrue(f.cancel(mayInterruptIfRunning));
375 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
376 +        }
377      }
378  
379      /**
# Line 316 | Line 384 | public class CompletableFutureTest exten
384          checkCompletedNormally(f, "test");
385      }
386  
387 <    // Choose non-commutative actions for better coverage
387 >    abstract class CheckedAction {
388 >        int invocationCount = 0;
389 >        final ExecutionMode m;
390 >        CheckedAction(ExecutionMode m) { this.m = m; }
391 >        void invoked() {
392 >            m.checkExecutionMode();
393 >            assertEquals(0, invocationCount++);
394 >        }
395 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
396 >        void assertInvoked() { assertEquals(1, invocationCount); }
397 >    }
398  
399 <    // A non-commutative function that handles and produces null values as well.
400 <    static Integer subtract(Integer x, Integer y) {
401 <        return (x == null && y == null) ? null :
402 <            ((x == null) ? 42 : x.intValue())
403 <            - ((y == null) ? 99 : y.intValue());
399 >    abstract class CheckedIntegerAction extends CheckedAction {
400 >        Integer value;
401 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
402 >        void assertValue(Integer expected) {
403 >            assertInvoked();
404 >            assertEquals(expected, value);
405 >        }
406 >    }
407 >
408 >    class IntegerSupplier extends CheckedAction
409 >        implements Supplier<Integer>
410 >    {
411 >        final Integer value;
412 >        IntegerSupplier(ExecutionMode m, Integer value) {
413 >            super(m);
414 >            this.value = value;
415 >        }
416 >        public Integer get() {
417 >            invoked();
418 >            return value;
419 >        }
420      }
421  
422      // A function that handles and produces null values as well.
# Line 330 | Line 424 | public class CompletableFutureTest exten
424          return (x == null) ? null : x + 1;
425      }
426  
427 <    static final Supplier<Integer> supplyOne =
428 <        () -> Integer.valueOf(1);
429 <    static final Function<Integer, Integer> inc =
430 <        (Integer x) -> Integer.valueOf(x.intValue() + 1);
337 <    static final BiFunction<Integer, Integer, Integer> subtract =
338 <        (Integer x, Integer y) -> subtract(x, y);
339 <    static final class IncAction implements Consumer<Integer> {
340 <        int invocationCount = 0;
341 <        Integer value;
342 <        public boolean ran() { return invocationCount == 1; }
427 >    class NoopConsumer extends CheckedIntegerAction
428 >        implements Consumer<Integer>
429 >    {
430 >        NoopConsumer(ExecutionMode m) { super(m); }
431          public void accept(Integer x) {
432 <            invocationCount++;
433 <            value = inc(x);
432 >            invoked();
433 >            value = x;
434          }
435      }
436 <    static final class IncFunction implements Function<Integer,Integer> {
437 <        int invocationCount = 0;
438 <        Integer value;
439 <        public boolean ran() { return invocationCount == 1; }
436 >
437 >    class IncFunction extends CheckedIntegerAction
438 >        implements Function<Integer,Integer>
439 >    {
440 >        IncFunction(ExecutionMode m) { super(m); }
441          public Integer apply(Integer x) {
442 <            invocationCount++;
442 >            invoked();
443              return value = inc(x);
444          }
445      }
446 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
447 <        int invocationCount = 0;
448 <        Integer value;
449 <        // Check this action was invoked exactly once when result is computed.
450 <        public boolean ran() { return invocationCount == 1; }
446 >
447 >    // Choose non-commutative actions for better coverage
448 >    // A non-commutative function that handles and produces null values as well.
449 >    static Integer subtract(Integer x, Integer y) {
450 >        return (x == null && y == null) ? null :
451 >            ((x == null) ? 42 : x.intValue())
452 >            - ((y == null) ? 99 : y.intValue());
453 >    }
454 >
455 >    class SubtractAction extends CheckedIntegerAction
456 >        implements BiConsumer<Integer, Integer>
457 >    {
458 >        SubtractAction(ExecutionMode m) { super(m); }
459          public void accept(Integer x, Integer y) {
460 <            invocationCount++;
460 >            invoked();
461              value = subtract(x, y);
462          }
463      }
464 <    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
465 <        int invocationCount = 0;
466 <        Integer value;
467 <        // Check this action was invoked exactly once when result is computed.
468 <        public boolean ran() { return invocationCount == 1; }
464 >
465 >    class SubtractFunction extends CheckedIntegerAction
466 >        implements BiFunction<Integer, Integer, Integer>
467 >    {
468 >        SubtractFunction(ExecutionMode m) { super(m); }
469          public Integer apply(Integer x, Integer y) {
470 <            invocationCount++;
470 >            invoked();
471              return value = subtract(x, y);
472          }
473      }
474 <    static final class Noop implements Runnable {
475 <        int invocationCount = 0;
476 <        boolean ran;
474 >
475 >    class Noop extends CheckedAction implements Runnable {
476 >        Noop(ExecutionMode m) { super(m); }
477          public void run() {
478 <            invocationCount++;
382 <            ran = true;
478 >            invoked();
479          }
480      }
481  
482 <    static final class FailingSupplier implements Supplier<Integer> {
483 <        boolean ran;
484 <        public Integer get() { ran = true; throw new CFException(); }
482 >    class FailingSupplier extends CheckedAction
483 >        implements Supplier<Integer>
484 >    {
485 >        FailingSupplier(ExecutionMode m) { super(m); }
486 >        public Integer get() {
487 >            invoked();
488 >            throw new CFException();
489 >        }
490      }
491 <    static final class FailingConsumer implements Consumer<Integer> {
492 <        boolean ran;
493 <        public void accept(Integer x) { ran = true; throw new CFException(); }
491 >
492 >    class FailingConsumer extends CheckedIntegerAction
493 >        implements Consumer<Integer>
494 >    {
495 >        FailingConsumer(ExecutionMode m) { super(m); }
496 >        public void accept(Integer x) {
497 >            invoked();
498 >            value = x;
499 >            throw new CFException();
500 >        }
501      }
502 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
503 <        boolean ran;
504 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
502 >
503 >    class FailingBiConsumer extends CheckedIntegerAction
504 >        implements BiConsumer<Integer, Integer>
505 >    {
506 >        FailingBiConsumer(ExecutionMode m) { super(m); }
507 >        public void accept(Integer x, Integer y) {
508 >            invoked();
509 >            value = subtract(x, y);
510 >            throw new CFException();
511 >        }
512      }
513 <    static final class FailingFunction implements Function<Integer, Integer> {
514 <        boolean ran;
515 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
513 >
514 >    class FailingFunction extends CheckedIntegerAction
515 >        implements Function<Integer, Integer>
516 >    {
517 >        FailingFunction(ExecutionMode m) { super(m); }
518 >        public Integer apply(Integer x) {
519 >            invoked();
520 >            value = x;
521 >            throw new CFException();
522 >        }
523      }
524 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
525 <        boolean ran;
526 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
524 >
525 >    class FailingBiFunction extends CheckedIntegerAction
526 >        implements BiFunction<Integer, Integer, Integer>
527 >    {
528 >        FailingBiFunction(ExecutionMode m) { super(m); }
529 >        public Integer apply(Integer x, Integer y) {
530 >            invoked();
531 >            value = subtract(x, y);
532 >            throw new CFException();
533 >        }
534      }
535 <    static final class FailingNoop implements Runnable {
536 <        boolean ran;
537 <        public void run() { ran = true; throw new CFException(); }
535 >
536 >    class FailingRunnable extends CheckedAction implements Runnable {
537 >        FailingRunnable(ExecutionMode m) { super(m); }
538 >        public void run() {
539 >            invoked();
540 >            throw new CFException();
541 >        }
542      }
543  
544 <    static final class CompletableFutureInc
545 <        implements Function<Integer, CompletableFuture<Integer>> {
546 <        boolean ran;
544 >
545 >    class CompletableFutureInc extends CheckedIntegerAction
546 >        implements Function<Integer, CompletableFuture<Integer>>
547 >    {
548 >        CompletableFutureInc(ExecutionMode m) { super(m); }
549          public CompletableFuture<Integer> apply(Integer x) {
550 <            ran = true;
550 >            invoked();
551 >            value = x;
552              CompletableFuture<Integer> f = new CompletableFuture<>();
553 <            f.complete(Integer.valueOf(x.intValue() + 1));
553 >            assertTrue(f.complete(inc(x)));
554              return f;
555          }
556      }
557  
558 <    static final class FailingCompletableFutureFunction
559 <        implements Function<Integer, CompletableFuture<Integer>> {
560 <        boolean ran;
558 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
559 >        implements Function<Integer, CompletableFuture<Integer>>
560 >    {
561 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
562          public CompletableFuture<Integer> apply(Integer x) {
563 <            ran = true; throw new CFException();
563 >            invoked();
564 >            value = x;
565 >            throw new CFException();
566          }
567      }
568  
569      // Used for explicit executor tests
570      static final class ThreadExecutor implements Executor {
571 <        AtomicInteger count = new AtomicInteger(0);
571 >        final AtomicInteger count = new AtomicInteger(0);
572 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
573 >        static boolean startedCurrentThread() {
574 >            return Thread.currentThread().getThreadGroup() == tg;
575 >        }
576  
577          public void execute(Runnable r) {
578              count.getAndIncrement();
579 <            new Thread(r).start();
579 >            new Thread(tg, r).start();
580          }
581      }
582  
583 <    static final class ExceptionToInteger implements Function<Throwable, Integer> {
584 <        public Integer apply(Throwable x) { return Integer.valueOf(3); }
442 <    }
443 <
444 <    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
445 <        boolean ran;
446 <        public Integer apply(Integer x, Throwable t) {
447 <            ran = true;
448 <            return (t == null) ? two : three;
449 <        }
450 <    }
583 >    static final boolean defaultExecutorIsCommonPool
584 >        = ForkJoinPool.getCommonPoolParallelism() > 1;
585  
586      /**
587       * Permits the testing of parallel code for the 3 different
588 <     * execution modes without repeating all the testing code.
588 >     * execution modes without copy/pasting all the test methods.
589       */
590      enum ExecutionMode {
591 <        DEFAULT {
591 >        SYNC {
592 >            public void checkExecutionMode() {
593 >                assertFalse(ThreadExecutor.startedCurrentThread());
594 >                assertNull(ForkJoinTask.getPool());
595 >            }
596 >            public CompletableFuture<Void> runAsync(Runnable a) {
597 >                throw new UnsupportedOperationException();
598 >            }
599 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
600 >                throw new UnsupportedOperationException();
601 >            }
602 >            public <T> CompletableFuture<Void> thenRun
603 >                (CompletableFuture<T> f, Runnable a) {
604 >                return f.thenRun(a);
605 >            }
606 >            public <T> CompletableFuture<Void> thenAccept
607 >                (CompletableFuture<T> f, Consumer<? super T> a) {
608 >                return f.thenAccept(a);
609 >            }
610 >            public <T,U> CompletableFuture<U> thenApply
611 >                (CompletableFuture<T> f, Function<? super T,U> a) {
612 >                return f.thenApply(a);
613 >            }
614 >            public <T,U> CompletableFuture<U> thenCompose
615 >                (CompletableFuture<T> f,
616 >                 Function<? super T,? extends CompletionStage<U>> a) {
617 >                return f.thenCompose(a);
618 >            }
619 >            public <T,U> CompletableFuture<U> handle
620 >                (CompletableFuture<T> f,
621 >                 BiFunction<? super T,Throwable,? extends U> a) {
622 >                return f.handle(a);
623 >            }
624 >            public <T> CompletableFuture<T> whenComplete
625 >                (CompletableFuture<T> f,
626 >                 BiConsumer<? super T,? super Throwable> a) {
627 >                return f.whenComplete(a);
628 >            }
629              public <T,U> CompletableFuture<Void> runAfterBoth
630                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
631                  return f.runAfterBoth(g, a);
# Line 471 | Line 642 | public class CompletableFutureTest exten
642                   BiFunction<? super T,? super U,? extends V> a) {
643                  return f.thenCombine(g, a);
644              }
645 <            public <T,U> CompletableFuture<U> applyToEither
645 >            public <T> CompletableFuture<Void> runAfterEither
646                  (CompletableFuture<T> f,
647 <                 CompletionStage<? extends T> g,
648 <                 Function<? super T,U> a) {
649 <                return f.applyToEither(g, a);
647 >                 CompletionStage<?> g,
648 >                 java.lang.Runnable a) {
649 >                return f.runAfterEither(g, a);
650              }
651              public <T> CompletableFuture<Void> acceptEither
652                  (CompletableFuture<T> f,
# Line 483 | Line 654 | public class CompletableFutureTest exten
654                   Consumer<? super T> a) {
655                  return f.acceptEither(g, a);
656              }
657 <            public <T> CompletableFuture<Void> runAfterEither
657 >            public <T,U> CompletableFuture<U> applyToEither
658                  (CompletableFuture<T> f,
659 <                 CompletionStage<?> g,
660 <                 java.lang.Runnable a) {
661 <                return f.runAfterEither(g, a);
659 >                 CompletionStage<? extends T> g,
660 >                 Function<? super T,U> a) {
661 >                return f.applyToEither(g, a);
662 >            }
663 >        },
664 >
665 >        ASYNC {
666 >            public void checkExecutionMode() {
667 >                assertEquals(defaultExecutorIsCommonPool,
668 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
669 >            }
670 >            public CompletableFuture<Void> runAsync(Runnable a) {
671 >                return CompletableFuture.runAsync(a);
672 >            }
673 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
674 >                return CompletableFuture.supplyAsync(a);
675 >            }
676 >            public <T> CompletableFuture<Void> thenRun
677 >                (CompletableFuture<T> f, Runnable a) {
678 >                return f.thenRunAsync(a);
679 >            }
680 >            public <T> CompletableFuture<Void> thenAccept
681 >                (CompletableFuture<T> f, Consumer<? super T> a) {
682 >                return f.thenAcceptAsync(a);
683 >            }
684 >            public <T,U> CompletableFuture<U> thenApply
685 >                (CompletableFuture<T> f, Function<? super T,U> a) {
686 >                return f.thenApplyAsync(a);
687              }
688              public <T,U> CompletableFuture<U> thenCompose
689                  (CompletableFuture<T> f,
690                   Function<? super T,? extends CompletionStage<U>> a) {
691 <                return f.thenCompose(a);
691 >                return f.thenComposeAsync(a);
692 >            }
693 >            public <T,U> CompletableFuture<U> handle
694 >                (CompletableFuture<T> f,
695 >                 BiFunction<? super T,Throwable,? extends U> a) {
696 >                return f.handleAsync(a);
697              }
698              public <T> CompletableFuture<T> whenComplete
699                  (CompletableFuture<T> f,
700                   BiConsumer<? super T,? super Throwable> a) {
701 <                return f.whenComplete(a);
701 >                return f.whenCompleteAsync(a);
702              }
502        },
503
504 //             /** Experimental way to do more testing */
505 //         REVERSE_DEFAULT {
506 //             public <T,U> CompletableFuture<Void> runAfterBoth
507 //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
508 //                 return g.runAfterBoth(f, a);
509 //             }
510 //             public <T,U> CompletableFuture<Void> thenAcceptBoth
511 //                 (CompletableFuture<T> f,
512 //                  CompletionStage<? extends U> g,
513 //                  BiConsumer<? super T,? super U> a) {
514 //                 return DEFAULT.thenAcceptBoth(f, g, a);
515 //             }
516 //         },
517
518        DEFAULT_ASYNC {
703              public <T,U> CompletableFuture<Void> runAfterBoth
704                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
705                  return f.runAfterBothAsync(g, a);
# Line 532 | Line 716 | public class CompletableFutureTest exten
716                   BiFunction<? super T,? super U,? extends V> a) {
717                  return f.thenCombineAsync(g, a);
718              }
719 <            public <T,U> CompletableFuture<U> applyToEither
719 >            public <T> CompletableFuture<Void> runAfterEither
720                  (CompletableFuture<T> f,
721 <                 CompletionStage<? extends T> g,
722 <                 Function<? super T,U> a) {
723 <                return f.applyToEitherAsync(g, a);
721 >                 CompletionStage<?> g,
722 >                 java.lang.Runnable a) {
723 >                return f.runAfterEitherAsync(g, a);
724              }
725              public <T> CompletableFuture<Void> acceptEither
726                  (CompletableFuture<T> f,
# Line 544 | Line 728 | public class CompletableFutureTest exten
728                   Consumer<? super T> a) {
729                  return f.acceptEitherAsync(g, a);
730              }
731 <            public <T> CompletableFuture<Void> runAfterEither
731 >            public <T,U> CompletableFuture<U> applyToEither
732                  (CompletableFuture<T> f,
733 <                 CompletionStage<?> g,
734 <                 java.lang.Runnable a) {
735 <                return f.runAfterEitherAsync(g, a);
733 >                 CompletionStage<? extends T> g,
734 >                 Function<? super T,U> a) {
735 >                return f.applyToEitherAsync(g, a);
736 >            }
737 >        },
738 >
739 >        EXECUTOR {
740 >            public void checkExecutionMode() {
741 >                assertTrue(ThreadExecutor.startedCurrentThread());
742 >            }
743 >            public CompletableFuture<Void> runAsync(Runnable a) {
744 >                return CompletableFuture.runAsync(a, new ThreadExecutor());
745 >            }
746 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
747 >                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
748 >            }
749 >            public <T> CompletableFuture<Void> thenRun
750 >                (CompletableFuture<T> f, Runnable a) {
751 >                return f.thenRunAsync(a, new ThreadExecutor());
752 >            }
753 >            public <T> CompletableFuture<Void> thenAccept
754 >                (CompletableFuture<T> f, Consumer<? super T> a) {
755 >                return f.thenAcceptAsync(a, new ThreadExecutor());
756 >            }
757 >            public <T,U> CompletableFuture<U> thenApply
758 >                (CompletableFuture<T> f, Function<? super T,U> a) {
759 >                return f.thenApplyAsync(a, new ThreadExecutor());
760              }
761              public <T,U> CompletableFuture<U> thenCompose
762                  (CompletableFuture<T> f,
763                   Function<? super T,? extends CompletionStage<U>> a) {
764 <                return f.thenComposeAsync(a);
764 >                return f.thenComposeAsync(a, new ThreadExecutor());
765 >            }
766 >            public <T,U> CompletableFuture<U> handle
767 >                (CompletableFuture<T> f,
768 >                 BiFunction<? super T,Throwable,? extends U> a) {
769 >                return f.handleAsync(a, new ThreadExecutor());
770              }
771              public <T> CompletableFuture<T> whenComplete
772                  (CompletableFuture<T> f,
773                   BiConsumer<? super T,? super Throwable> a) {
774 <                return f.whenCompleteAsync(a);
774 >                return f.whenCompleteAsync(a, new ThreadExecutor());
775              }
563        },
564
565 //         REVERSE_DEFAULT_ASYNC {
566 //             public <T,U> CompletableFuture<Void> runAfterBoth
567 //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
568 //                 return f.runAfterBothAsync(g, a);
569 //             }
570 //             public <T,U> CompletableFuture<Void> thenAcceptBoth
571 //                 (CompletableFuture<T> f,
572 //                  CompletionStage<? extends U> g,
573 //                  BiConsumer<? super T,? super U> a) {
574 //                 return DEFAULT_ASYNC.thenAcceptBoth(f, g, a);
575 //             }
576 //         },
577
578        EXECUTOR {
776              public <T,U> CompletableFuture<Void> runAfterBoth
777                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
778                  return f.runAfterBothAsync(g, a, new ThreadExecutor());
# Line 592 | Line 789 | public class CompletableFutureTest exten
789                   BiFunction<? super T,? super U,? extends V> a) {
790                  return f.thenCombineAsync(g, a, new ThreadExecutor());
791              }
595            public <T,U> CompletableFuture<U> applyToEither
596                (CompletableFuture<T> f,
597                 CompletionStage<? extends T> g,
598                 Function<? super T,U> a) {
599                return f.applyToEitherAsync(g, a, new ThreadExecutor());
600            }
601            public <T> CompletableFuture<Void> acceptEither
602                (CompletableFuture<T> f,
603                 CompletionStage<? extends T> g,
604                 Consumer<? super T> a) {
605                return f.acceptEitherAsync(g, a, new ThreadExecutor());
606            }
792              public <T> CompletableFuture<Void> runAfterEither
793                  (CompletableFuture<T> f,
794                   CompletionStage<?> g,
795                   java.lang.Runnable a) {
796                  return f.runAfterEitherAsync(g, a, new ThreadExecutor());
797              }
798 <            public <T,U> CompletableFuture<U> thenCompose
798 >            public <T> CompletableFuture<Void> acceptEither
799                  (CompletableFuture<T> f,
800 <                 Function<? super T,? extends CompletionStage<U>> a) {
801 <                return f.thenComposeAsync(a, new ThreadExecutor());
800 >                 CompletionStage<? extends T> g,
801 >                 Consumer<? super T> a) {
802 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
803              }
804 <            public <T> CompletableFuture<T> whenComplete
804 >            public <T,U> CompletableFuture<U> applyToEither
805                  (CompletableFuture<T> f,
806 <                 BiConsumer<? super T,? super Throwable> a) {
807 <                return f.whenCompleteAsync(a, new ThreadExecutor());
806 >                 CompletionStage<? extends T> g,
807 >                 Function<? super T,U> a) {
808 >                return f.applyToEitherAsync(g, a, new ThreadExecutor());
809              }
810          };
811  
812 +        public abstract void checkExecutionMode();
813 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
814 +        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
815 +        public abstract <T> CompletableFuture<Void> thenRun
816 +            (CompletableFuture<T> f, Runnable a);
817 +        public abstract <T> CompletableFuture<Void> thenAccept
818 +            (CompletableFuture<T> f, Consumer<? super T> a);
819 +        public abstract <T,U> CompletableFuture<U> thenApply
820 +            (CompletableFuture<T> f, Function<? super T,U> a);
821 +        public abstract <T,U> CompletableFuture<U> thenCompose
822 +            (CompletableFuture<T> f,
823 +             Function<? super T,? extends CompletionStage<U>> a);
824 +        public abstract <T,U> CompletableFuture<U> handle
825 +            (CompletableFuture<T> f,
826 +             BiFunction<? super T,Throwable,? extends U> a);
827 +        public abstract <T> CompletableFuture<T> whenComplete
828 +            (CompletableFuture<T> f,
829 +             BiConsumer<? super T,? super Throwable> a);
830          public abstract <T,U> CompletableFuture<Void> runAfterBoth
831              (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
832          public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
# Line 632 | Line 837 | public class CompletableFutureTest exten
837              (CompletableFuture<T> f,
838               CompletionStage<? extends U> g,
839               BiFunction<? super T,? super U,? extends V> a);
635        public abstract <T,U> CompletableFuture<U> applyToEither
636            (CompletableFuture<T> f,
637             CompletionStage<? extends T> g,
638             Function<? super T,U> a);
639        public abstract <T> CompletableFuture<Void> acceptEither
640            (CompletableFuture<T> f,
641             CompletionStage<? extends T> g,
642             Consumer<? super T> a);
840          public abstract <T> CompletableFuture<Void> runAfterEither
841              (CompletableFuture<T> f,
842               CompletionStage<?> g,
843               java.lang.Runnable a);
844 <        public abstract <T,U> CompletableFuture<U> thenCompose
844 >        public abstract <T> CompletableFuture<Void> acceptEither
845              (CompletableFuture<T> f,
846 <             Function<? super T,? extends CompletionStage<U>> a);
847 <        public abstract <T> CompletableFuture<T> whenComplete
846 >             CompletionStage<? extends T> g,
847 >             Consumer<? super T> a);
848 >        public abstract <T,U> CompletableFuture<U> applyToEither
849              (CompletableFuture<T> f,
850 <             BiConsumer<? super T,? super Throwable> a);
851 <
654 <
655 <    }
656 <
657 <    /**
658 <     * exceptionally action completes with function value on source
659 <     * exception; otherwise with source value
660 <     */
661 <    public void testExceptionally() {
662 <        CompletableFuture<Integer> f = new CompletableFuture<>();
663 <        ExceptionToInteger r = new ExceptionToInteger();
664 <        CompletableFuture<Integer> g = f.exceptionally(r);
665 <        f.completeExceptionally(new CFException());
666 <        checkCompletedNormally(g, three);
667 <
668 <        f = new CompletableFuture<>();
669 <        r = new ExceptionToInteger();
670 <        g = f.exceptionally(r);
671 <        f.complete(one);
672 <        checkCompletedNormally(g, one);
850 >             CompletionStage<? extends T> g,
851 >             Function<? super T,U> a);
852      }
853  
854      /**
855 <     * handle action completes normally with function value on either
856 <     * normal or exceptional completion of source
855 >     * exceptionally action is not invoked when source completes
856 >     * normally, and source result is propagated
857       */
858 <    public void testHandle() {
859 <        CompletableFuture<Integer> f, g;
860 <        IntegerHandler r;
861 <
862 <        f = new CompletableFuture<>();
863 <        f.completeExceptionally(new CFException());
864 <        g = f.handle(r = new IntegerHandler());
865 <        assertTrue(r.ran);
866 <        checkCompletedNormally(g, three);
867 <
868 <        f = new CompletableFuture<>();
869 <        g = f.handle(r = new IntegerHandler());
870 <        assertFalse(r.ran);
871 <        f.completeExceptionally(new CFException());
693 <        checkCompletedNormally(g, three);
694 <        assertTrue(r.ran);
695 <
696 <        f = new CompletableFuture<>();
697 <        f.complete(one);
698 <        g = f.handle(r = new IntegerHandler());
699 <        assertTrue(r.ran);
700 <        checkCompletedNormally(g, two);
858 >    public void testExceptionally_normalCompletion() {
859 >        for (boolean createIncomplete : new boolean[] { true, false })
860 >        for (Integer v1 : new Integer[] { 1, null })
861 >    {
862 >        final AtomicInteger a = new AtomicInteger(0);
863 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
864 >        if (!createIncomplete) assertTrue(f.complete(v1));
865 >        final CompletableFuture<Integer> g = f.exceptionally
866 >            ((Throwable t) -> {
867 >                // Should not be called
868 >                a.getAndIncrement();
869 >                throw new AssertionError();
870 >            });
871 >        if (createIncomplete) assertTrue(f.complete(v1));
872  
873 <        f = new CompletableFuture<>();
874 <        g = f.handle(r = new IntegerHandler());
875 <        assertFalse(r.ran);
876 <        f.complete(one);
706 <        assertTrue(r.ran);
707 <        checkCompletedNormally(g, two);
708 <    }
873 >        checkCompletedNormally(g, v1);
874 >        checkCompletedNormally(f, v1);
875 >        assertEquals(0, a.get());
876 >    }}
877  
878      /**
879 <     * runAsync completes after running Runnable
879 >     * exceptionally action completes with function value on source
880 >     * exception
881       */
882 <    public void testRunAsync() {
883 <        Noop r = new Noop();
884 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
885 <        assertNull(f.join());
886 <        assertTrue(r.ran);
887 <        checkCompletedNormally(f, null);
888 <    }
882 >    public void testExceptionally_exceptionalCompletion() {
883 >        for (boolean createIncomplete : new boolean[] { true, false })
884 >        for (Integer v1 : new Integer[] { 1, null })
885 >    {
886 >        final AtomicInteger a = new AtomicInteger(0);
887 >        final CFException ex = new CFException();
888 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
889 >        if (!createIncomplete) f.completeExceptionally(ex);
890 >        final CompletableFuture<Integer> g = f.exceptionally
891 >            ((Throwable t) -> {
892 >                ExecutionMode.SYNC.checkExecutionMode();
893 >                threadAssertSame(t, ex);
894 >                a.getAndIncrement();
895 >                return v1;
896 >            });
897 >        if (createIncomplete) f.completeExceptionally(ex);
898 >
899 >        checkCompletedNormally(g, v1);
900 >        assertEquals(1, a.get());
901 >    }}
902 >
903 >    public void testExceptionally_exceptionalCompletionActionFailed() {
904 >        for (boolean createIncomplete : new boolean[] { true, false })
905 >        for (Integer v1 : new Integer[] { 1, null })
906 >    {
907 >        final AtomicInteger a = new AtomicInteger(0);
908 >        final CFException ex1 = new CFException();
909 >        final CFException ex2 = new CFException();
910 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
911 >        if (!createIncomplete) f.completeExceptionally(ex1);
912 >        final CompletableFuture<Integer> g = f.exceptionally
913 >            ((Throwable t) -> {
914 >                ExecutionMode.SYNC.checkExecutionMode();
915 >                threadAssertSame(t, ex1);
916 >                a.getAndIncrement();
917 >                throw ex2;
918 >            });
919 >        if (createIncomplete) f.completeExceptionally(ex1);
920 >
921 >        checkCompletedWithWrappedException(g, ex2);
922 >        assertEquals(1, a.get());
923 >    }}
924  
925      /**
926 <     * runAsync with executor completes after running Runnable
926 >     * whenComplete action executes on normal completion, propagating
927 >     * source result.
928       */
929 <    public void testRunAsync2() {
930 <        Noop r = new Noop();
931 <        ThreadExecutor exec = new ThreadExecutor();
932 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
933 <        assertNull(f.join());
934 <        assertTrue(r.ran);
935 <        checkCompletedNormally(f, null);
936 <        assertEquals(1, exec.count.get());
937 <    }
929 >    public void testWhenComplete_normalCompletion1() {
930 >        for (ExecutionMode m : ExecutionMode.values())
931 >        for (boolean createIncomplete : new boolean[] { true, false })
932 >        for (Integer v1 : new Integer[] { 1, null })
933 >    {
934 >        final AtomicInteger a = new AtomicInteger(0);
935 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
936 >        if (!createIncomplete) assertTrue(f.complete(v1));
937 >        final CompletableFuture<Integer> g = m.whenComplete
938 >            (f,
939 >             (Integer x, Throwable t) -> {
940 >                m.checkExecutionMode();
941 >                threadAssertSame(x, v1);
942 >                threadAssertNull(t);
943 >                a.getAndIncrement();
944 >            });
945 >        if (createIncomplete) assertTrue(f.complete(v1));
946  
947 <    /**
948 <     * failing runAsync completes exceptionally after running Runnable
949 <     */
950 <    public void testRunAsync3() {
738 <        FailingNoop r = new FailingNoop();
739 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
740 <        checkCompletedWithWrappedCFException(f);
741 <        assertTrue(r.ran);
742 <    }
947 >        checkCompletedNormally(g, v1);
948 >        checkCompletedNormally(f, v1);
949 >        assertEquals(1, a.get());
950 >    }}
951  
952      /**
953 <     * supplyAsync completes with result of supplier
953 >     * whenComplete action executes on exceptional completion, propagating
954 >     * source result.
955       */
956 <    public void testSupplyAsync() {
957 <        CompletableFuture<Integer> f;
958 <        f = CompletableFuture.supplyAsync(supplyOne);
959 <        assertEquals(f.join(), one);
960 <        checkCompletedNormally(f, one);
961 <    }
956 >    public void testWhenComplete_exceptionalCompletion() {
957 >        for (ExecutionMode m : ExecutionMode.values())
958 >        for (boolean createIncomplete : new boolean[] { true, false })
959 >        for (Integer v1 : new Integer[] { 1, null })
960 >    {
961 >        final AtomicInteger a = new AtomicInteger(0);
962 >        final CFException ex = new CFException();
963 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
964 >        if (!createIncomplete) f.completeExceptionally(ex);
965 >        final CompletableFuture<Integer> g = m.whenComplete
966 >            (f,
967 >             (Integer x, Throwable t) -> {
968 >                m.checkExecutionMode();
969 >                threadAssertNull(x);
970 >                threadAssertSame(t, ex);
971 >                a.getAndIncrement();
972 >            });
973 >        if (createIncomplete) f.completeExceptionally(ex);
974 >
975 >        checkCompletedWithWrappedException(g, ex);
976 >        checkCompletedExceptionally(f, ex);
977 >        assertEquals(1, a.get());
978 >    }}
979 >
980 >    /**
981 >     * whenComplete action executes on cancelled source, propagating
982 >     * CancellationException.
983 >     */
984 >    public void testWhenComplete_sourceCancelled() {
985 >        for (ExecutionMode m : ExecutionMode.values())
986 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
987 >        for (boolean createIncomplete : new boolean[] { true, false })
988 >    {
989 >        final AtomicInteger a = new AtomicInteger(0);
990 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
991 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
992 >        final CompletableFuture<Integer> g = m.whenComplete
993 >            (f,
994 >             (Integer x, Throwable t) -> {
995 >                m.checkExecutionMode();
996 >                threadAssertNull(x);
997 >                threadAssertTrue(t instanceof CancellationException);
998 >                a.getAndIncrement();
999 >            });
1000 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1001  
1002 <    /**
1003 <     * supplyAsync with executor completes with result of supplier
1004 <     */
1005 <    public void testSupplyAsync2() {
758 <        CompletableFuture<Integer> f;
759 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
760 <        assertEquals(f.join(), one);
761 <        checkCompletedNormally(f, one);
762 <    }
1002 >        checkCompletedWithWrappedCancellationException(g);
1003 >        checkCancelled(f);
1004 >        assertEquals(1, a.get());
1005 >    }}
1006  
1007      /**
1008 <     * Failing supplyAsync completes exceptionally
1008 >     * If a whenComplete action throws an exception when triggered by
1009 >     * a normal completion, it completes exceptionally
1010       */
1011 <    public void testSupplyAsync3() {
1012 <        FailingSupplier r = new FailingSupplier();
1013 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1014 <        checkCompletedWithWrappedCFException(f);
1015 <        assertTrue(r.ran);
1016 <    }
1011 >    public void testWhenComplete_actionFailed() {
1012 >        for (boolean createIncomplete : new boolean[] { true, false })
1013 >        for (ExecutionMode m : ExecutionMode.values())
1014 >        for (Integer v1 : new Integer[] { 1, null })
1015 >    {
1016 >        final AtomicInteger a = new AtomicInteger(0);
1017 >        final CFException ex = new CFException();
1018 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1019 >        if (!createIncomplete) assertTrue(f.complete(v1));
1020 >        final CompletableFuture<Integer> g = m.whenComplete
1021 >            (f,
1022 >             (Integer x, Throwable t) -> {
1023 >                m.checkExecutionMode();
1024 >                threadAssertSame(x, v1);
1025 >                threadAssertNull(t);
1026 >                a.getAndIncrement();
1027 >                throw ex;
1028 >            });
1029 >        if (createIncomplete) assertTrue(f.complete(v1));
1030  
1031 <    // seq completion methods
1031 >        checkCompletedWithWrappedException(g, ex);
1032 >        checkCompletedNormally(f, v1);
1033 >        assertEquals(1, a.get());
1034 >    }}
1035  
1036      /**
1037 <     * thenRun result completes normally after normal completion of source
1037 >     * If a whenComplete action throws an exception when triggered by
1038 >     * a source completion that also throws an exception, the source
1039 >     * exception takes precedence.
1040       */
1041 <    public void testThenRun() {
1042 <        CompletableFuture<Integer> f;
1043 <        CompletableFuture<Void> g;
1044 <        Noop r;
1045 <
1046 <        f = new CompletableFuture<>();
1047 <        g = f.thenRun(r = new Noop());
1048 <        f.complete(null);
1049 <        checkCompletedNormally(g, null);
1050 <        assertTrue(r.ran);
1051 <
1052 <        f = new CompletableFuture<>();
1053 <        f.complete(null);
1054 <        g = f.thenRun(r = new Noop());
1055 <        checkCompletedNormally(g, null);
1056 <        assertTrue(r.ran);
1057 <    }
1041 >    public void testWhenComplete_actionFailedSourceFailed() {
1042 >        for (boolean createIncomplete : new boolean[] { true, false })
1043 >        for (ExecutionMode m : ExecutionMode.values())
1044 >        for (Integer v1 : new Integer[] { 1, null })
1045 >    {
1046 >        final AtomicInteger a = new AtomicInteger(0);
1047 >        final CFException ex1 = new CFException();
1048 >        final CFException ex2 = new CFException();
1049 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1050 >
1051 >        if (!createIncomplete) f.completeExceptionally(ex1);
1052 >        final CompletableFuture<Integer> g = m.whenComplete
1053 >            (f,
1054 >             (Integer x, Throwable t) -> {
1055 >                m.checkExecutionMode();
1056 >                threadAssertSame(t, ex1);
1057 >                threadAssertNull(x);
1058 >                a.getAndIncrement();
1059 >                throw ex2;
1060 >            });
1061 >        if (createIncomplete) f.completeExceptionally(ex1);
1062 >
1063 >        checkCompletedWithWrappedException(g, ex1);
1064 >        checkCompletedExceptionally(f, ex1);
1065 >        assertEquals(1, a.get());
1066 >    }}
1067  
1068      /**
1069 <     * thenRun result completes exceptionally after exceptional
1069 >     * handle action completes normally with function value on normal
1070       * completion of source
1071       */
1072 <    public void testThenRun2() {
1073 <        CompletableFuture<Integer> f;
1074 <        CompletableFuture<Void> g;
1075 <        Noop r;
1076 <
1077 <        f = new CompletableFuture<>();
1078 <        g = f.thenRun(r = new Noop());
1079 <        f.completeExceptionally(new CFException());
1080 <        checkCompletedWithWrappedCFException(g);
1081 <        assertFalse(r.ran);
1072 >    public void testHandle_normalCompletion() {
1073 >        for (ExecutionMode m : ExecutionMode.values())
1074 >        for (boolean createIncomplete : new boolean[] { true, false })
1075 >        for (Integer v1 : new Integer[] { 1, null })
1076 >    {
1077 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1078 >        final AtomicInteger a = new AtomicInteger(0);
1079 >        if (!createIncomplete) assertTrue(f.complete(v1));
1080 >        final CompletableFuture<Integer> g = m.handle
1081 >            (f,
1082 >             (Integer x, Throwable t) -> {
1083 >                m.checkExecutionMode();
1084 >                threadAssertSame(x, v1);
1085 >                threadAssertNull(t);
1086 >                a.getAndIncrement();
1087 >                return inc(v1);
1088 >            });
1089 >        if (createIncomplete) assertTrue(f.complete(v1));
1090  
1091 <        f = new CompletableFuture<>();
1092 <        f.completeExceptionally(new CFException());
1093 <        g = f.thenRun(r = new Noop());
1094 <        checkCompletedWithWrappedCFException(g);
816 <        assertFalse(r.ran);
817 <    }
1091 >        checkCompletedNormally(g, inc(v1));
1092 >        checkCompletedNormally(f, v1);
1093 >        assertEquals(1, a.get());
1094 >    }}
1095  
1096      /**
1097 <     * thenRun result completes exceptionally if action does
1097 >     * handle action completes normally with function value on
1098 >     * exceptional completion of source
1099       */
1100 <    public void testThenRun3() {
1101 <        CompletableFuture<Integer> f;
1102 <        CompletableFuture<Void> g;
1103 <        FailingNoop r;
1104 <
1105 <        f = new CompletableFuture<>();
1106 <        g = f.thenRun(r = new FailingNoop());
1107 <        f.complete(null);
1108 <        checkCompletedWithWrappedCFException(g);
1100 >    public void testHandle_exceptionalCompletion() {
1101 >        for (ExecutionMode m : ExecutionMode.values())
1102 >        for (boolean createIncomplete : new boolean[] { true, false })
1103 >        for (Integer v1 : new Integer[] { 1, null })
1104 >    {
1105 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1106 >        final AtomicInteger a = new AtomicInteger(0);
1107 >        final CFException ex = new CFException();
1108 >        if (!createIncomplete) f.completeExceptionally(ex);
1109 >        final CompletableFuture<Integer> g = m.handle
1110 >            (f,
1111 >             (Integer x, Throwable t) -> {
1112 >                m.checkExecutionMode();
1113 >                threadAssertNull(x);
1114 >                threadAssertSame(t, ex);
1115 >                a.getAndIncrement();
1116 >                return v1;
1117 >            });
1118 >        if (createIncomplete) f.completeExceptionally(ex);
1119  
1120 <        f = new CompletableFuture<>();
1121 <        f.complete(null);
1122 <        g = f.thenRun(r = new FailingNoop());
1123 <        checkCompletedWithWrappedCFException(g);
836 <    }
1120 >        checkCompletedNormally(g, v1);
1121 >        checkCompletedExceptionally(f, ex);
1122 >        assertEquals(1, a.get());
1123 >    }}
1124  
1125      /**
1126 <     * thenRun result completes exceptionally if source cancelled
1126 >     * handle action completes normally with function value on
1127 >     * cancelled source
1128       */
1129 <    public void testThenRun4() {
1130 <        CompletableFuture<Integer> f;
1131 <        CompletableFuture<Void> g;
1132 <        Noop r;
1133 <
1134 <        f = new CompletableFuture<>();
1135 <        g = f.thenRun(r = new Noop());
1136 <        assertTrue(f.cancel(true));
1137 <        checkCompletedWithWrappedCancellationException(g);
1138 <
1139 <        f = new CompletableFuture<>();
1140 <        assertTrue(f.cancel(true));
1141 <        g = f.thenRun(r = new Noop());
1142 <        checkCompletedWithWrappedCancellationException(g);
1143 <    }
1129 >    public void testHandle_sourceCancelled() {
1130 >        for (ExecutionMode m : ExecutionMode.values())
1131 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1132 >        for (boolean createIncomplete : new boolean[] { true, false })
1133 >        for (Integer v1 : new Integer[] { 1, null })
1134 >    {
1135 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1136 >        final AtomicInteger a = new AtomicInteger(0);
1137 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1138 >        final CompletableFuture<Integer> g = m.handle
1139 >            (f,
1140 >             (Integer x, Throwable t) -> {
1141 >                m.checkExecutionMode();
1142 >                threadAssertNull(x);
1143 >                threadAssertTrue(t instanceof CancellationException);
1144 >                a.getAndIncrement();
1145 >                return v1;
1146 >            });
1147 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1148  
1149 <    /**
1150 <     * thenApply result completes normally after normal completion of source
1151 <     */
1152 <    public void testThenApply() {
861 <        CompletableFuture<Integer> f = new CompletableFuture<>();
862 <        CompletableFuture<Integer> g = f.thenApply(inc);
863 <        f.complete(one);
864 <        checkCompletedNormally(g, two);
865 <    }
1149 >        checkCompletedNormally(g, v1);
1150 >        checkCancelled(f);
1151 >        assertEquals(1, a.get());
1152 >    }}
1153  
1154      /**
1155 <     * thenApply result completes exceptionally after exceptional
869 <     * completion of source
1155 >     * handle result completes exceptionally if action does
1156       */
1157 <    public void testThenApply2() {
1158 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1159 <        CompletableFuture<Integer> g = f.thenApply(inc);
1160 <        f.completeExceptionally(new CFException());
1161 <        checkCompletedWithWrappedCFException(g);
1162 <    }
1157 >    public void testHandle_sourceFailedActionFailed() {
1158 >        for (ExecutionMode m : ExecutionMode.values())
1159 >        for (boolean createIncomplete : new boolean[] { true, false })
1160 >    {
1161 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1162 >        final AtomicInteger a = new AtomicInteger(0);
1163 >        final CFException ex1 = new CFException();
1164 >        final CFException ex2 = new CFException();
1165 >        if (!createIncomplete) f.completeExceptionally(ex1);
1166 >        final CompletableFuture<Integer> g = m.handle
1167 >            (f,
1168 >             (Integer x, Throwable t) -> {
1169 >                m.checkExecutionMode();
1170 >                threadAssertNull(x);
1171 >                threadAssertSame(ex1, t);
1172 >                a.getAndIncrement();
1173 >                throw ex2;
1174 >            });
1175 >        if (createIncomplete) f.completeExceptionally(ex1);
1176 >
1177 >        checkCompletedWithWrappedException(g, ex2);
1178 >        checkCompletedExceptionally(f, ex1);
1179 >        assertEquals(1, a.get());
1180 >    }}
1181 >
1182 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1183 >        for (ExecutionMode m : ExecutionMode.values())
1184 >        for (boolean createIncomplete : new boolean[] { true, false })
1185 >        for (Integer v1 : new Integer[] { 1, null })
1186 >    {
1187 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1188 >        final AtomicInteger a = new AtomicInteger(0);
1189 >        final CFException ex = new CFException();
1190 >        if (!createIncomplete) assertTrue(f.complete(v1));
1191 >        final CompletableFuture<Integer> g = m.handle
1192 >            (f,
1193 >             (Integer x, Throwable t) -> {
1194 >                m.checkExecutionMode();
1195 >                threadAssertSame(x, v1);
1196 >                threadAssertNull(t);
1197 >                a.getAndIncrement();
1198 >                throw ex;
1199 >            });
1200 >        if (createIncomplete) assertTrue(f.complete(v1));
1201  
1202 <    /**
1203 <     * thenApply result completes exceptionally if action does
1204 <     */
1205 <    public void testThenApply3() {
882 <        CompletableFuture<Integer> f = new CompletableFuture<>();
883 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
884 <        f.complete(one);
885 <        checkCompletedWithWrappedCFException(g);
886 <    }
1202 >        checkCompletedWithWrappedException(g, ex);
1203 >        checkCompletedNormally(f, v1);
1204 >        assertEquals(1, a.get());
1205 >    }}
1206  
1207      /**
1208 <     * thenApply result completes exceptionally if source cancelled
1208 >     * runAsync completes after running Runnable
1209       */
1210 <    public void testThenApply4() {
1211 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1212 <        CompletableFuture<Integer> g = f.thenApply(inc);
1213 <        assertTrue(f.cancel(true));
1214 <        checkCompletedWithWrappedCancellationException(g);
1215 <    }
1210 >    public void testRunAsync_normalCompletion() {
1211 >        ExecutionMode[] executionModes = {
1212 >            ExecutionMode.ASYNC,
1213 >            ExecutionMode.EXECUTOR,
1214 >        };
1215 >        for (ExecutionMode m : executionModes)
1216 >    {
1217 >        final Noop r = new Noop(m);
1218 >        final CompletableFuture<Void> f = m.runAsync(r);
1219 >        assertNull(f.join());
1220 >        checkCompletedNormally(f, null);
1221 >        r.assertInvoked();
1222 >    }}
1223  
1224      /**
1225 <     * thenAccept result completes normally after normal completion of source
1225 >     * failing runAsync completes exceptionally after running Runnable
1226       */
1227 <    public void testThenAccept() {
1228 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1229 <        IncAction r = new IncAction();
1230 <        CompletableFuture<Void> g = f.thenAccept(r);
1231 <        f.complete(one);
1232 <        checkCompletedNormally(g, null);
1233 <        assertEquals(r.value, (Integer) 2);
1234 <    }
1227 >    public void testRunAsync_exceptionalCompletion() {
1228 >        ExecutionMode[] executionModes = {
1229 >            ExecutionMode.ASYNC,
1230 >            ExecutionMode.EXECUTOR,
1231 >        };
1232 >        for (ExecutionMode m : executionModes)
1233 >    {
1234 >        final FailingRunnable r = new FailingRunnable(m);
1235 >        final CompletableFuture<Void> f = m.runAsync(r);
1236 >        checkCompletedWithWrappedCFException(f);
1237 >        r.assertInvoked();
1238 >    }}
1239  
1240      /**
1241 <     * thenAccept result completes exceptionally after exceptional
912 <     * completion of source
1241 >     * supplyAsync completes with result of supplier
1242       */
1243 <    public void testThenAccept2() {
1244 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1245 <        IncAction r = new IncAction();
1246 <        CompletableFuture<Void> g = f.thenAccept(r);
1247 <        f.completeExceptionally(new CFException());
1248 <        checkCompletedWithWrappedCFException(g);
1249 <    }
1243 >    public void testSupplyAsync_normalCompletion() {
1244 >        ExecutionMode[] executionModes = {
1245 >            ExecutionMode.ASYNC,
1246 >            ExecutionMode.EXECUTOR,
1247 >        };
1248 >        for (ExecutionMode m : executionModes)
1249 >        for (Integer v1 : new Integer[] { 1, null })
1250 >    {
1251 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1252 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1253 >        assertSame(v1, f.join());
1254 >        checkCompletedNormally(f, v1);
1255 >        r.assertInvoked();
1256 >    }}
1257  
1258      /**
1259 <     * thenAccept result completes exceptionally if action does
1259 >     * Failing supplyAsync completes exceptionally
1260       */
1261 <    public void testThenAccept3() {
1262 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1263 <        FailingConsumer r = new FailingConsumer();
1264 <        CompletableFuture<Void> g = f.thenAccept(r);
1265 <        f.complete(one);
1266 <        checkCompletedWithWrappedCFException(g);
1267 <        assertTrue(r.ran);
1268 <    }
1261 >    public void testSupplyAsync_exceptionalCompletion() {
1262 >        ExecutionMode[] executionModes = {
1263 >            ExecutionMode.ASYNC,
1264 >            ExecutionMode.EXECUTOR,
1265 >        };
1266 >        for (ExecutionMode m : executionModes)
1267 >    {
1268 >        FailingSupplier r = new FailingSupplier(m);
1269 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1270 >        checkCompletedWithWrappedCFException(f);
1271 >        r.assertInvoked();
1272 >    }}
1273  
1274 <    /**
935 <     * thenAccept result completes exceptionally if source cancelled
936 <     */
937 <    public void testThenAccept4() {
938 <        CompletableFuture<Integer> f = new CompletableFuture<>();
939 <        IncAction r = new IncAction();
940 <        CompletableFuture<Void> g = f.thenAccept(r);
941 <        assertTrue(f.cancel(true));
942 <        checkCompletedWithWrappedCancellationException(g);
943 <    }
1274 >    // seq completion methods
1275  
1276      /**
1277 <     * thenCombine result completes normally after normal completion
947 <     * of sources
1277 >     * thenRun result completes normally after normal completion of source
1278       */
1279 <    public void testThenCombine_normalCompletion1() {
1279 >    public void testThenRun_normalCompletion() {
1280          for (ExecutionMode m : ExecutionMode.values())
1281          for (Integer v1 : new Integer[] { 1, null })
1282 <        for (Integer v2 : new Integer[] { 2, null }) {
953 <
1282 >    {
1283          final CompletableFuture<Integer> f = new CompletableFuture<>();
1284 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1285 <        final SubtractFunction r = new SubtractFunction();
957 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
958 <
959 <        f.complete(v1);
960 <        checkIncomplete(h);
961 <        assertEquals(r.invocationCount, 0);
962 <        g.complete(v2);
1284 >        final Noop[] rs = new Noop[6];
1285 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1286  
1287 <        checkCompletedNormally(h, subtract(v1, v2));
1287 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1288 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1289 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1290 >        checkIncomplete(h0);
1291 >        checkIncomplete(h1);
1292 >        checkIncomplete(h2);
1293 >        assertTrue(f.complete(v1));
1294 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1295 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1296 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1297 >
1298 >        checkCompletedNormally(h0, null);
1299 >        checkCompletedNormally(h1, null);
1300 >        checkCompletedNormally(h2, null);
1301 >        checkCompletedNormally(h3, null);
1302 >        checkCompletedNormally(h4, null);
1303 >        checkCompletedNormally(h5, null);
1304          checkCompletedNormally(f, v1);
1305 <        checkCompletedNormally(g, v2);
1306 <        assertEquals(r.invocationCount, 1);
968 <        }
969 <    }
1305 >        for (Noop r : rs) r.assertInvoked();
1306 >    }}
1307  
1308 <    public void testThenCombine_normalCompletion2() {
1308 >    /**
1309 >     * thenRun result completes exceptionally after exceptional
1310 >     * completion of source
1311 >     */
1312 >    public void testThenRun_exceptionalCompletion() {
1313          for (ExecutionMode m : ExecutionMode.values())
1314 <        for (Integer v1 : new Integer[] { 1, null })
1315 <        for (Integer v2 : new Integer[] { 2, null }) {
975 <
1314 >    {
1315 >        final CFException ex = new CFException();
1316          final CompletableFuture<Integer> f = new CompletableFuture<>();
1317 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1318 <        final SubtractFunction r = new SubtractFunction();
979 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1317 >        final Noop[] rs = new Noop[6];
1318 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1319  
1320 <        g.complete(v2);
1321 <        checkIncomplete(h);
1322 <        assertEquals(r.invocationCount, 0);
1323 <        f.complete(v1);
1320 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1321 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1322 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1323 >        checkIncomplete(h0);
1324 >        checkIncomplete(h1);
1325 >        checkIncomplete(h2);
1326 >        assertTrue(f.completeExceptionally(ex));
1327 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1328 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1329 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1330 >
1331 >        checkCompletedWithWrappedException(h0, ex);
1332 >        checkCompletedWithWrappedException(h1, ex);
1333 >        checkCompletedWithWrappedException(h2, ex);
1334 >        checkCompletedWithWrappedException(h3, ex);
1335 >        checkCompletedWithWrappedException(h4, ex);
1336 >        checkCompletedWithWrappedException(h5, ex);
1337 >        checkCompletedExceptionally(f, ex);
1338 >        for (Noop r : rs) r.assertNotInvoked();
1339 >    }}
1340  
1341 <        checkCompletedNormally(h, subtract(v1, v2));
1342 <        checkCompletedNormally(f, v1);
1343 <        checkCompletedNormally(g, v2);
1344 <        assertEquals(r.invocationCount, 1);
990 <        }
991 <    }
992 <
993 <    public void testThenCombine_normalCompletion3() {
1341 >    /**
1342 >     * thenRun result completes exceptionally if source cancelled
1343 >     */
1344 >    public void testThenRun_sourceCancelled() {
1345          for (ExecutionMode m : ExecutionMode.values())
1346 <        for (Integer v1 : new Integer[] { 1, null })
1347 <        for (Integer v2 : new Integer[] { 2, null }) {
997 <
1346 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1347 >    {
1348          final CompletableFuture<Integer> f = new CompletableFuture<>();
1349 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1350 <        final SubtractFunction r = new SubtractFunction();
1001 <
1002 <        g.complete(v2);
1003 <        f.complete(v1);
1004 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1349 >        final Noop[] rs = new Noop[6];
1350 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1351  
1352 <        checkCompletedNormally(h, subtract(v1, v2));
1353 <        checkCompletedNormally(f, v1);
1354 <        checkCompletedNormally(g, v2);
1355 <        assertEquals(r.invocationCount, 1);
1356 <        }
1357 <    }
1352 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1353 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1354 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1355 >        checkIncomplete(h0);
1356 >        checkIncomplete(h1);
1357 >        checkIncomplete(h2);
1358 >        assertTrue(f.cancel(mayInterruptIfRunning));
1359 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1360 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1361 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1362 >
1363 >        checkCompletedWithWrappedCancellationException(h0);
1364 >        checkCompletedWithWrappedCancellationException(h1);
1365 >        checkCompletedWithWrappedCancellationException(h2);
1366 >        checkCompletedWithWrappedCancellationException(h3);
1367 >        checkCompletedWithWrappedCancellationException(h4);
1368 >        checkCompletedWithWrappedCancellationException(h5);
1369 >        checkCancelled(f);
1370 >        for (Noop r : rs) r.assertNotInvoked();
1371 >    }}
1372  
1373 <    public void testThenCombine_normalCompletion4() {
1373 >    /**
1374 >     * thenRun result completes exceptionally if action does
1375 >     */
1376 >    public void testThenRun_actionFailed() {
1377          for (ExecutionMode m : ExecutionMode.values())
1378          for (Integer v1 : new Integer[] { 1, null })
1379 <        for (Integer v2 : new Integer[] { 2, null }) {
1017 <
1379 >    {
1380          final CompletableFuture<Integer> f = new CompletableFuture<>();
1381 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1382 <        final SubtractFunction r = new SubtractFunction();
1381 >        final FailingRunnable[] rs = new FailingRunnable[6];
1382 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1383  
1384 <        f.complete(v1);
1385 <        g.complete(v2);
1386 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1387 <
1388 <        checkCompletedNormally(h, subtract(v1, v2));
1384 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1385 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1386 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1387 >        assertTrue(f.complete(v1));
1388 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1389 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1390 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1391 >
1392 >        checkCompletedWithWrappedCFException(h0);
1393 >        checkCompletedWithWrappedCFException(h1);
1394 >        checkCompletedWithWrappedCFException(h2);
1395 >        checkCompletedWithWrappedCFException(h3);
1396 >        checkCompletedWithWrappedCFException(h4);
1397 >        checkCompletedWithWrappedCFException(h5);
1398          checkCompletedNormally(f, v1);
1399 <        checkCompletedNormally(g, v2);
1029 <        assertEquals(r.invocationCount, 1);
1030 <        }
1031 <    }
1399 >    }}
1400  
1401      /**
1402 <     * thenCombine result completes exceptionally after exceptional
1035 <     * completion of either source
1402 >     * thenApply result completes normally after normal completion of source
1403       */
1404 <    public void testThenCombine_exceptionalCompletion1() {
1038 <        for (ExecutionMode m : ExecutionMode.values())
1039 <        for (Integer v1 : new Integer[] { 1, null }) {
1040 <
1041 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1042 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1043 <        final SubtractFunction r = new SubtractFunction();
1044 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1045 <        final CFException ex = new CFException();
1046 <
1047 <        f.completeExceptionally(ex);
1048 <        checkIncomplete(h);
1049 <        g.complete(v1);
1050 <
1051 <        checkCompletedWithWrappedCFException(h, ex);
1052 <        checkCompletedWithWrappedCFException(f, ex);
1053 <        assertEquals(r.invocationCount, 0);
1054 <        checkCompletedNormally(g, v1);
1055 <        }
1056 <    }
1057 <
1058 <    public void testThenCombine_exceptionalCompletion2() {
1404 >    public void testThenApply_normalCompletion() {
1405          for (ExecutionMode m : ExecutionMode.values())
1406 <        for (Integer v1 : new Integer[] { 1, null }) {
1407 <
1406 >        for (Integer v1 : new Integer[] { 1, null })
1407 >    {
1408          final CompletableFuture<Integer> f = new CompletableFuture<>();
1409 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1410 <        final SubtractFunction r = new SubtractFunction();
1065 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1066 <        final CFException ex = new CFException();
1409 >        final IncFunction[] rs = new IncFunction[4];
1410 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1411  
1412 <        g.completeExceptionally(ex);
1413 <        checkIncomplete(h);
1414 <        f.complete(v1);
1412 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1413 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1414 >        checkIncomplete(h0);
1415 >        checkIncomplete(h1);
1416 >        assertTrue(f.complete(v1));
1417 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1418 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1419  
1420 <        checkCompletedWithWrappedCFException(h, ex);
1421 <        checkCompletedWithWrappedCFException(g, ex);
1422 <        assertEquals(r.invocationCount, 0);
1420 >        checkCompletedNormally(h0, inc(v1));
1421 >        checkCompletedNormally(h1, inc(v1));
1422 >        checkCompletedNormally(h2, inc(v1));
1423 >        checkCompletedNormally(h3, inc(v1));
1424          checkCompletedNormally(f, v1);
1425 <        }
1426 <    }
1425 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1426 >    }}
1427  
1428 <    public void testThenCombine_exceptionalCompletion3() {
1428 >    /**
1429 >     * thenApply result completes exceptionally after exceptional
1430 >     * completion of source
1431 >     */
1432 >    public void testThenApply_exceptionalCompletion() {
1433          for (ExecutionMode m : ExecutionMode.values())
1434 <        for (Integer v1 : new Integer[] { 1, null }) {
1082 <
1083 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1084 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1085 <        final SubtractFunction r = new SubtractFunction();
1434 >    {
1435          final CFException ex = new CFException();
1087
1088        g.completeExceptionally(ex);
1089        f.complete(v1);
1090        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1091
1092        checkCompletedWithWrappedCFException(h, ex);
1093        checkCompletedWithWrappedCFException(g, ex);
1094        assertEquals(r.invocationCount, 0);
1095        checkCompletedNormally(f, v1);
1096        }
1097    }
1098
1099    public void testThenCombine_exceptionalCompletion4() {
1100        for (ExecutionMode m : ExecutionMode.values())
1101        for (Integer v1 : new Integer[] { 1, null }) {
1102
1436          final CompletableFuture<Integer> f = new CompletableFuture<>();
1437 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1438 <        final SubtractFunction r = new SubtractFunction();
1106 <        final CFException ex = new CFException();
1437 >        final IncFunction[] rs = new IncFunction[4];
1438 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1439  
1440 <        f.completeExceptionally(ex);
1441 <        g.complete(v1);
1442 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1443 <
1444 <        checkCompletedWithWrappedCFException(h, ex);
1445 <        checkCompletedWithWrappedCFException(f, ex);
1446 <        assertEquals(r.invocationCount, 0);
1447 <        checkCompletedNormally(g, v1);
1448 <        }
1449 <    }
1440 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1441 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1442 >        assertTrue(f.completeExceptionally(ex));
1443 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1444 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1445 >
1446 >        checkCompletedWithWrappedException(h0, ex);
1447 >        checkCompletedWithWrappedException(h1, ex);
1448 >        checkCompletedWithWrappedException(h2, ex);
1449 >        checkCompletedWithWrappedException(h3, ex);
1450 >        checkCompletedExceptionally(f, ex);
1451 >        for (IncFunction r : rs) r.assertNotInvoked();
1452 >    }}
1453  
1454      /**
1455 <     * thenCombine result completes exceptionally if action does
1455 >     * thenApply result completes exceptionally if source cancelled
1456       */
1457 <    public void testThenCombine_actionFailed1() {
1457 >    public void testThenApply_sourceCancelled() {
1458          for (ExecutionMode m : ExecutionMode.values())
1459 <        for (Integer v1 : new Integer[] { 1, null })
1460 <        for (Integer v2 : new Integer[] { 2, null }) {
1126 <
1459 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1460 >    {
1461          final CompletableFuture<Integer> f = new CompletableFuture<>();
1462 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1463 <        final FailingBiFunction r = new FailingBiFunction();
1130 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1462 >        final IncFunction[] rs = new IncFunction[4];
1463 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1464  
1465 <        f.complete(v1);
1466 <        checkIncomplete(h);
1467 <        g.complete(v2);
1465 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1466 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1467 >        assertTrue(f.cancel(mayInterruptIfRunning));
1468 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1469 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1470  
1471 <        checkCompletedWithWrappedCFException(h);
1472 <        checkCompletedNormally(f, v1);
1473 <        checkCompletedNormally(g, v2);
1474 <        }
1475 <    }
1471 >        checkCompletedWithWrappedCancellationException(h0);
1472 >        checkCompletedWithWrappedCancellationException(h1);
1473 >        checkCompletedWithWrappedCancellationException(h2);
1474 >        checkCompletedWithWrappedCancellationException(h3);
1475 >        checkCancelled(f);
1476 >        for (IncFunction r : rs) r.assertNotInvoked();
1477 >    }}
1478  
1479 <    public void testThenCombine_actionFailed2() {
1479 >    /**
1480 >     * thenApply result completes exceptionally if action does
1481 >     */
1482 >    public void testThenApply_actionFailed() {
1483          for (ExecutionMode m : ExecutionMode.values())
1484          for (Integer v1 : new Integer[] { 1, null })
1485 <        for (Integer v2 : new Integer[] { 2, null }) {
1146 <
1485 >    {
1486          final CompletableFuture<Integer> f = new CompletableFuture<>();
1487 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1488 <        final FailingBiFunction r = new FailingBiFunction();
1150 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1487 >        final FailingFunction[] rs = new FailingFunction[4];
1488 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1489  
1490 <        g.complete(v2);
1491 <        checkIncomplete(h);
1492 <        f.complete(v1);
1490 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1491 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1492 >        assertTrue(f.complete(v1));
1493 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1494 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1495  
1496 <        checkCompletedWithWrappedCFException(h);
1496 >        checkCompletedWithWrappedCFException(h0);
1497 >        checkCompletedWithWrappedCFException(h1);
1498 >        checkCompletedWithWrappedCFException(h2);
1499 >        checkCompletedWithWrappedCFException(h3);
1500          checkCompletedNormally(f, v1);
1501 <        checkCompletedNormally(g, v2);
1159 <        }
1160 <    }
1501 >    }}
1502  
1503      /**
1504 <     * thenCombine result completes exceptionally if either source cancelled
1504 >     * thenAccept result completes normally after normal completion of source
1505       */
1506 <    public void testThenCombine_sourceCancelled1() {
1506 >    public void testThenAccept_normalCompletion() {
1507          for (ExecutionMode m : ExecutionMode.values())
1508 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1509 <        for (Integer v1 : new Integer[] { 1, null }) {
1169 <
1170 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1171 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1172 <        final SubtractFunction r = new SubtractFunction();
1173 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1174 <
1175 <        assertTrue(f.cancel(mayInterruptIfRunning));
1176 <        checkIncomplete(h);
1177 <        g.complete(v1);
1178 <
1179 <        checkCompletedWithWrappedCancellationException(h);
1180 <        checkCancelled(f);
1181 <        assertEquals(r.invocationCount, 0);
1182 <        checkCompletedNormally(g, v1);
1183 <        }
1184 <    }
1185 <
1186 <    public void testThenCombine_sourceCancelled2() {
1187 <        for (ExecutionMode m : ExecutionMode.values())
1188 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1189 <        for (Integer v1 : new Integer[] { 1, null }) {
1190 <
1508 >        for (Integer v1 : new Integer[] { 1, null })
1509 >    {
1510          final CompletableFuture<Integer> f = new CompletableFuture<>();
1511 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1512 <        final SubtractFunction r = new SubtractFunction();
1194 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1511 >        final NoopConsumer[] rs = new NoopConsumer[4];
1512 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1513  
1514 <        assertTrue(g.cancel(mayInterruptIfRunning));
1515 <        checkIncomplete(h);
1516 <        f.complete(v1);
1514 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1515 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1516 >        checkIncomplete(h0);
1517 >        checkIncomplete(h1);
1518 >        assertTrue(f.complete(v1));
1519 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1520 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1521  
1522 <        checkCompletedWithWrappedCancellationException(h);
1523 <        checkCancelled(g);
1524 <        assertEquals(r.invocationCount, 0);
1522 >        checkCompletedNormally(h0, null);
1523 >        checkCompletedNormally(h1, null);
1524 >        checkCompletedNormally(h2, null);
1525 >        checkCompletedNormally(h3, null);
1526          checkCompletedNormally(f, v1);
1527 <        }
1528 <    }
1527 >        for (NoopConsumer r : rs) r.assertValue(v1);
1528 >    }}
1529  
1530 <    public void testThenCombine_sourceCancelled3() {
1530 >    /**
1531 >     * thenAccept result completes exceptionally after exceptional
1532 >     * completion of source
1533 >     */
1534 >    public void testThenAccept_exceptionalCompletion() {
1535          for (ExecutionMode m : ExecutionMode.values())
1536 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1537 <        for (Integer v1 : new Integer[] { 1, null }) {
1211 <
1536 >    {
1537 >        final CFException ex = new CFException();
1538          final CompletableFuture<Integer> f = new CompletableFuture<>();
1539 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1540 <        final SubtractFunction r = new SubtractFunction();
1539 >        final NoopConsumer[] rs = new NoopConsumer[4];
1540 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1541  
1542 <        assertTrue(g.cancel(mayInterruptIfRunning));
1543 <        f.complete(v1);
1544 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1542 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1543 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1544 >        assertTrue(f.completeExceptionally(ex));
1545 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1546 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1547 >
1548 >        checkCompletedWithWrappedException(h0, ex);
1549 >        checkCompletedWithWrappedException(h1, ex);
1550 >        checkCompletedWithWrappedException(h2, ex);
1551 >        checkCompletedWithWrappedException(h3, ex);
1552 >        checkCompletedExceptionally(f, ex);
1553 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1554 >    }}
1555  
1556 <        checkCompletedWithWrappedCancellationException(h);
1557 <        checkCancelled(g);
1558 <        assertEquals(r.invocationCount, 0);
1559 <        checkCompletedNormally(f, v1);
1224 <        }
1225 <    }
1226 <
1227 <    public void testThenCombine_sourceCancelled4() {
1556 >    /**
1557 >     * thenAccept result completes exceptionally if source cancelled
1558 >     */
1559 >    public void testThenAccept_sourceCancelled() {
1560          for (ExecutionMode m : ExecutionMode.values())
1561          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1562 <        for (Integer v1 : new Integer[] { 1, null }) {
1231 <
1562 >    {
1563          final CompletableFuture<Integer> f = new CompletableFuture<>();
1564 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1565 <        final SubtractFunction r = new SubtractFunction();
1564 >        final NoopConsumer[] rs = new NoopConsumer[4];
1565 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1566  
1567 +        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1568 +        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1569          assertTrue(f.cancel(mayInterruptIfRunning));
1570 <        g.complete(v1);
1571 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1570 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1571 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1572  
1573 <        checkCompletedWithWrappedCancellationException(h);
1573 >        checkCompletedWithWrappedCancellationException(h0);
1574 >        checkCompletedWithWrappedCancellationException(h1);
1575 >        checkCompletedWithWrappedCancellationException(h2);
1576 >        checkCompletedWithWrappedCancellationException(h3);
1577          checkCancelled(f);
1578 <        assertEquals(r.invocationCount, 0);
1579 <        checkCompletedNormally(g, v1);
1244 <        }
1245 <    }
1578 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1579 >    }}
1580  
1581      /**
1582 <     * thenAcceptBoth result completes normally after normal
1249 <     * completion of sources
1582 >     * thenAccept result completes exceptionally if action does
1583       */
1584 <    public void testThenAcceptBoth_normalCompletion1() {
1584 >    public void testThenAccept_actionFailed() {
1585          for (ExecutionMode m : ExecutionMode.values())
1586          for (Integer v1 : new Integer[] { 1, null })
1587 <        for (Integer v2 : new Integer[] { 2, null }) {
1255 <
1587 >    {
1588          final CompletableFuture<Integer> f = new CompletableFuture<>();
1589 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1590 <        final SubtractAction r = new SubtractAction();
1259 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1589 >        final FailingConsumer[] rs = new FailingConsumer[4];
1590 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1591  
1592 <        f.complete(v1);
1593 <        checkIncomplete(h);
1594 <        assertFalse(r.ran());
1595 <        g.complete(v2);
1592 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1593 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1594 >        assertTrue(f.complete(v1));
1595 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1596 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1597  
1598 <        checkCompletedNormally(h, null);
1599 <        assertEquals(r.value, subtract(v1, v2));
1598 >        checkCompletedWithWrappedCFException(h0);
1599 >        checkCompletedWithWrappedCFException(h1);
1600 >        checkCompletedWithWrappedCFException(h2);
1601 >        checkCompletedWithWrappedCFException(h3);
1602          checkCompletedNormally(f, v1);
1603 <        checkCompletedNormally(g, v2);
1270 <        }
1271 <    }
1603 >    }}
1604  
1605 <    public void testThenAcceptBoth_normalCompletion2() {
1605 >    /**
1606 >     * thenCombine result completes normally after normal completion
1607 >     * of sources
1608 >     */
1609 >    public void testThenCombine_normalCompletion() {
1610          for (ExecutionMode m : ExecutionMode.values())
1611 +        for (boolean fFirst : new boolean[] { true, false })
1612          for (Integer v1 : new Integer[] { 1, null })
1613 <        for (Integer v2 : new Integer[] { 2, null }) {
1614 <
1613 >        for (Integer v2 : new Integer[] { 2, null })
1614 >    {
1615          final CompletableFuture<Integer> f = new CompletableFuture<>();
1616          final CompletableFuture<Integer> g = new CompletableFuture<>();
1617 <        final SubtractAction r = new SubtractAction();
1618 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1617 >        final SubtractFunction[] rs = new SubtractFunction[6];
1618 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1619 >
1620 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1621 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1622 >        final Integer w1 =  fFirst ? v1 : v2;
1623 >        final Integer w2 = !fFirst ? v1 : v2;
1624 >
1625 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1626 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1627 >        assertTrue(fst.complete(w1));
1628 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1629 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1630 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1631 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1632 >        checkCompletedNormally(h1, subtract(w1, w1));
1633 >        checkCompletedNormally(h3, subtract(w1, w1));
1634 >        rs[1].assertValue(subtract(w1, w1));
1635 >        rs[3].assertValue(subtract(w1, w1));
1636 >        assertTrue(snd.complete(w2));
1637 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1638 >
1639 >        checkCompletedNormally(h0, subtract(v1, v2));
1640 >        checkCompletedNormally(h2, subtract(v1, v2));
1641 >        checkCompletedNormally(h4, subtract(v1, v2));
1642 >        rs[0].assertValue(subtract(v1, v2));
1643 >        rs[2].assertValue(subtract(v1, v2));
1644 >        rs[4].assertValue(subtract(v1, v2));
1645  
1283        g.complete(v2);
1284        checkIncomplete(h);
1285        assertFalse(r.ran());
1286        f.complete(v1);
1287
1288        checkCompletedNormally(h, null);
1289        assertEquals(r.value, subtract(v1, v2));
1646          checkCompletedNormally(f, v1);
1647          checkCompletedNormally(g, v2);
1648 <        }
1293 <    }
1648 >    }}
1649  
1650 <    public void testThenAcceptBoth_normalCompletion3() {
1650 >    /**
1651 >     * thenCombine result completes exceptionally after exceptional
1652 >     * completion of either source
1653 >     */
1654 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1655          for (ExecutionMode m : ExecutionMode.values())
1656 +        for (boolean fFirst : new boolean[] { true, false })
1657 +        for (boolean failFirst : new boolean[] { true, false })
1658          for (Integer v1 : new Integer[] { 1, null })
1659 <        for (Integer v2 : new Integer[] { 2, null }) {
1299 <
1659 >    {
1660          final CompletableFuture<Integer> f = new CompletableFuture<>();
1661          final CompletableFuture<Integer> g = new CompletableFuture<>();
1662 <        final SubtractAction r = new SubtractAction();
1663 <
1664 <        g.complete(v2);
1665 <        f.complete(v1);
1666 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1667 <
1668 <        checkCompletedNormally(h, null);
1669 <        assertEquals(r.value, subtract(v1, v2));
1670 <        checkCompletedNormally(f, v1);
1671 <        checkCompletedNormally(g, v2);
1672 <        }
1673 <    }
1662 >        final CFException ex = new CFException();
1663 >        final SubtractFunction r1 = new SubtractFunction(m);
1664 >        final SubtractFunction r2 = new SubtractFunction(m);
1665 >        final SubtractFunction r3 = new SubtractFunction(m);
1666 >
1667 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1668 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1669 >        final Callable<Boolean> complete1 = failFirst ?
1670 >            () -> fst.completeExceptionally(ex) :
1671 >            () -> fst.complete(v1);
1672 >        final Callable<Boolean> complete2 = failFirst ?
1673 >            () -> snd.complete(v1) :
1674 >            () -> snd.completeExceptionally(ex);
1675 >
1676 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1677 >        assertTrue(complete1.call());
1678 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1679 >        checkIncomplete(h1);
1680 >        checkIncomplete(h2);
1681 >        assertTrue(complete2.call());
1682 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1683 >
1684 >        checkCompletedWithWrappedException(h1, ex);
1685 >        checkCompletedWithWrappedException(h2, ex);
1686 >        checkCompletedWithWrappedException(h3, ex);
1687 >        r1.assertNotInvoked();
1688 >        r2.assertNotInvoked();
1689 >        r3.assertNotInvoked();
1690 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1691 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1692 >    }}
1693  
1694 <    public void testThenAcceptBoth_normalCompletion4() {
1694 >    /**
1695 >     * thenCombine result completes exceptionally if either source cancelled
1696 >     */
1697 >    public void testThenCombine_sourceCancelled() throws Throwable {
1698          for (ExecutionMode m : ExecutionMode.values())
1699 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1700 +        for (boolean fFirst : new boolean[] { true, false })
1701 +        for (boolean failFirst : new boolean[] { true, false })
1702          for (Integer v1 : new Integer[] { 1, null })
1703 <        for (Integer v2 : new Integer[] { 2, null }) {
1319 <
1703 >    {
1704          final CompletableFuture<Integer> f = new CompletableFuture<>();
1705          final CompletableFuture<Integer> g = new CompletableFuture<>();
1706 <        final SubtractAction r = new SubtractAction();
1707 <
1708 <        f.complete(v1);
1709 <        g.complete(v2);
1710 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1711 <
1712 <        checkCompletedNormally(h, null);
1713 <        assertEquals(r.value, subtract(v1, v2));
1714 <        checkCompletedNormally(f, v1);
1715 <        checkCompletedNormally(g, v2);
1716 <        }
1717 <    }
1706 >        final SubtractFunction r1 = new SubtractFunction(m);
1707 >        final SubtractFunction r2 = new SubtractFunction(m);
1708 >        final SubtractFunction r3 = new SubtractFunction(m);
1709 >
1710 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1711 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1712 >        final Callable<Boolean> complete1 = failFirst ?
1713 >            () -> fst.cancel(mayInterruptIfRunning) :
1714 >            () -> fst.complete(v1);
1715 >        final Callable<Boolean> complete2 = failFirst ?
1716 >            () -> snd.complete(v1) :
1717 >            () -> snd.cancel(mayInterruptIfRunning);
1718 >
1719 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1720 >        assertTrue(complete1.call());
1721 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1722 >        checkIncomplete(h1);
1723 >        checkIncomplete(h2);
1724 >        assertTrue(complete2.call());
1725 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1726 >
1727 >        checkCompletedWithWrappedCancellationException(h1);
1728 >        checkCompletedWithWrappedCancellationException(h2);
1729 >        checkCompletedWithWrappedCancellationException(h3);
1730 >        r1.assertNotInvoked();
1731 >        r2.assertNotInvoked();
1732 >        r3.assertNotInvoked();
1733 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1734 >        checkCancelled(failFirst ? fst : snd);
1735 >    }}
1736  
1737      /**
1738 <     * thenAcceptBoth result completes exceptionally after exceptional
1337 <     * completion of either source
1738 >     * thenCombine result completes exceptionally if action does
1739       */
1740 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1740 >    public void testThenCombine_actionFailed() {
1741          for (ExecutionMode m : ExecutionMode.values())
1742 <        for (Integer v1 : new Integer[] { 1, null }) {
1743 <
1744 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1745 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1345 <        final SubtractAction r = new SubtractAction();
1346 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1347 <        final CFException ex = new CFException();
1348 <
1349 <        f.completeExceptionally(ex);
1350 <        checkIncomplete(h);
1351 <        g.complete(v1);
1352 <
1353 <        checkCompletedWithWrappedCFException(h, ex);
1354 <        checkCompletedWithWrappedCFException(f, ex);
1355 <        assertEquals(r.invocationCount, 0);
1356 <        checkCompletedNormally(g, v1);
1357 <        }
1358 <    }
1359 <
1360 <    public void testThenAcceptBoth_exceptionalCompletion2() {
1361 <        for (ExecutionMode m : ExecutionMode.values())
1362 <        for (Integer v1 : new Integer[] { 1, null }) {
1363 <
1742 >        for (boolean fFirst : new boolean[] { true, false })
1743 >        for (Integer v1 : new Integer[] { 1, null })
1744 >        for (Integer v2 : new Integer[] { 2, null })
1745 >    {
1746          final CompletableFuture<Integer> f = new CompletableFuture<>();
1747          final CompletableFuture<Integer> g = new CompletableFuture<>();
1748 <        final SubtractAction r = new SubtractAction();
1749 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1750 <        final CFException ex = new CFException();
1369 <
1370 <        g.completeExceptionally(ex);
1371 <        checkIncomplete(h);
1372 <        f.complete(v1);
1748 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1749 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1750 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1751  
1752 <        checkCompletedWithWrappedCFException(h, ex);
1753 <        checkCompletedWithWrappedCFException(g, ex);
1754 <        assertEquals(r.invocationCount, 0);
1755 <        checkCompletedNormally(f, v1);
1378 <        }
1379 <    }
1752 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1753 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1754 >        final Integer w1 =  fFirst ? v1 : v2;
1755 >        final Integer w2 = !fFirst ? v1 : v2;
1756  
1757 <    public void testThenAcceptBoth_exceptionalCompletion3() {
1758 <        for (ExecutionMode m : ExecutionMode.values())
1759 <        for (Integer v1 : new Integer[] { 1, null }) {
1757 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1758 >        assertTrue(fst.complete(w1));
1759 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1760 >        assertTrue(snd.complete(w2));
1761 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1762  
1763 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1764 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1765 <        final SubtractAction r = new SubtractAction();
1766 <        final CFException ex = new CFException();
1767 <
1768 <        g.completeExceptionally(ex);
1391 <        f.complete(v1);
1392 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1393 <
1394 <        checkCompletedWithWrappedCFException(h, ex);
1395 <        checkCompletedWithWrappedCFException(g, ex);
1396 <        assertEquals(r.invocationCount, 0);
1763 >        checkCompletedWithWrappedCFException(h1);
1764 >        checkCompletedWithWrappedCFException(h2);
1765 >        checkCompletedWithWrappedCFException(h3);
1766 >        r1.assertInvoked();
1767 >        r2.assertInvoked();
1768 >        r3.assertInvoked();
1769          checkCompletedNormally(f, v1);
1770 <        }
1771 <    }
1400 <
1401 <    public void testThenAcceptBoth_exceptionalCompletion4() {
1402 <        for (ExecutionMode m : ExecutionMode.values())
1403 <        for (Integer v1 : new Integer[] { 1, null }) {
1404 <
1405 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1406 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1407 <        final SubtractAction r = new SubtractAction();
1408 <        final CFException ex = new CFException();
1409 <
1410 <        f.completeExceptionally(ex);
1411 <        g.complete(v1);
1412 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1413 <
1414 <        checkCompletedWithWrappedCFException(h, ex);
1415 <        checkCompletedWithWrappedCFException(f, ex);
1416 <        assertFalse(r.ran());
1417 <        checkCompletedNormally(g, v1);
1418 <        }
1419 <    }
1770 >        checkCompletedNormally(g, v2);
1771 >    }}
1772  
1773      /**
1774 <     * thenAcceptBoth result completes exceptionally if action does
1774 >     * thenAcceptBoth result completes normally after normal
1775 >     * completion of sources
1776       */
1777 <    public void testThenAcceptBoth_actionFailed1() {
1777 >    public void testThenAcceptBoth_normalCompletion() {
1778          for (ExecutionMode m : ExecutionMode.values())
1779 +        for (boolean fFirst : new boolean[] { true, false })
1780          for (Integer v1 : new Integer[] { 1, null })
1781 <        for (Integer v2 : new Integer[] { 2, null }) {
1782 <
1781 >        for (Integer v2 : new Integer[] { 2, null })
1782 >    {
1783          final CompletableFuture<Integer> f = new CompletableFuture<>();
1784          final CompletableFuture<Integer> g = new CompletableFuture<>();
1785 <        final FailingBiConsumer r = new FailingBiConsumer();
1786 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1787 <
1788 <        f.complete(v1);
1789 <        checkIncomplete(h);
1790 <        g.complete(v2);
1791 <
1792 <        checkCompletedWithWrappedCFException(h);
1785 >        final SubtractAction r1 = new SubtractAction(m);
1786 >        final SubtractAction r2 = new SubtractAction(m);
1787 >        final SubtractAction r3 = new SubtractAction(m);
1788 >
1789 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1790 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1791 >        final Integer w1 =  fFirst ? v1 : v2;
1792 >        final Integer w2 = !fFirst ? v1 : v2;
1793 >
1794 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1795 >        assertTrue(fst.complete(w1));
1796 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1797 >        checkIncomplete(h1);
1798 >        checkIncomplete(h2);
1799 >        r1.assertNotInvoked();
1800 >        r2.assertNotInvoked();
1801 >        assertTrue(snd.complete(w2));
1802 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1803 >
1804 >        checkCompletedNormally(h1, null);
1805 >        checkCompletedNormally(h2, null);
1806 >        checkCompletedNormally(h3, null);
1807 >        r1.assertValue(subtract(v1, v2));
1808 >        r2.assertValue(subtract(v1, v2));
1809 >        r3.assertValue(subtract(v1, v2));
1810          checkCompletedNormally(f, v1);
1811          checkCompletedNormally(g, v2);
1812 <        }
1442 <    }
1812 >    }}
1813  
1814 <    public void testThenAcceptBoth_actionFailed2() {
1814 >    /**
1815 >     * thenAcceptBoth result completes exceptionally after exceptional
1816 >     * completion of either source
1817 >     */
1818 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1819          for (ExecutionMode m : ExecutionMode.values())
1820 +        for (boolean fFirst : new boolean[] { true, false })
1821 +        for (boolean failFirst : new boolean[] { true, false })
1822          for (Integer v1 : new Integer[] { 1, null })
1823 <        for (Integer v2 : new Integer[] { 2, null }) {
1448 <
1823 >    {
1824          final CompletableFuture<Integer> f = new CompletableFuture<>();
1825          final CompletableFuture<Integer> g = new CompletableFuture<>();
1826 <        final FailingBiConsumer r = new FailingBiConsumer();
1827 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1828 <
1829 <        g.complete(v2);
1830 <        checkIncomplete(h);
1831 <        f.complete(v1);
1832 <
1833 <        checkCompletedWithWrappedCFException(h);
1834 <        checkCompletedNormally(f, v1);
1835 <        checkCompletedNormally(g, v2);
1836 <        }
1837 <    }
1826 >        final CFException ex = new CFException();
1827 >        final SubtractAction r1 = new SubtractAction(m);
1828 >        final SubtractAction r2 = new SubtractAction(m);
1829 >        final SubtractAction r3 = new SubtractAction(m);
1830 >
1831 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1832 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1833 >        final Callable<Boolean> complete1 = failFirst ?
1834 >            () -> fst.completeExceptionally(ex) :
1835 >            () -> fst.complete(v1);
1836 >        final Callable<Boolean> complete2 = failFirst ?
1837 >            () -> snd.complete(v1) :
1838 >            () -> snd.completeExceptionally(ex);
1839 >
1840 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1841 >        assertTrue(complete1.call());
1842 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1843 >        checkIncomplete(h1);
1844 >        checkIncomplete(h2);
1845 >        assertTrue(complete2.call());
1846 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1847 >
1848 >        checkCompletedWithWrappedException(h1, ex);
1849 >        checkCompletedWithWrappedException(h2, ex);
1850 >        checkCompletedWithWrappedException(h3, ex);
1851 >        r1.assertNotInvoked();
1852 >        r2.assertNotInvoked();
1853 >        r3.assertNotInvoked();
1854 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1855 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1856 >    }}
1857  
1858      /**
1859       * thenAcceptBoth result completes exceptionally if either source cancelled
1860       */
1861 <    public void testThenAcceptBoth_sourceCancelled1() {
1468 <        for (ExecutionMode m : ExecutionMode.values())
1469 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1470 <        for (Integer v1 : new Integer[] { 1, null }) {
1471 <
1472 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1473 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1474 <        final SubtractAction r = new SubtractAction();
1475 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1476 <
1477 <        assertTrue(f.cancel(mayInterruptIfRunning));
1478 <        checkIncomplete(h);
1479 <        g.complete(v1);
1480 <
1481 <        checkCompletedWithWrappedCancellationException(h);
1482 <        checkCancelled(f);
1483 <        assertEquals(r.invocationCount, 0);
1484 <        checkCompletedNormally(g, v1);
1485 <        }
1486 <    }
1487 <
1488 <    public void testThenAcceptBoth_sourceCancelled2() {
1489 <        for (ExecutionMode m : ExecutionMode.values())
1490 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1491 <        for (Integer v1 : new Integer[] { 1, null }) {
1492 <
1493 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1494 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1495 <        final SubtractAction r = new SubtractAction();
1496 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1497 <
1498 <        assertTrue(g.cancel(mayInterruptIfRunning));
1499 <        checkIncomplete(h);
1500 <        f.complete(v1);
1501 <
1502 <        checkCompletedWithWrappedCancellationException(h);
1503 <        checkCancelled(g);
1504 <        assertEquals(r.invocationCount, 0);
1505 <        checkCompletedNormally(f, v1);
1506 <        }
1507 <    }
1508 <
1509 <    public void testThenAcceptBoth_sourceCancelled3() {
1510 <        for (ExecutionMode m : ExecutionMode.values())
1511 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1512 <        for (Integer v1 : new Integer[] { 1, null }) {
1513 <
1514 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1515 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1516 <        final SubtractAction r = new SubtractAction();
1517 <
1518 <        assertTrue(g.cancel(mayInterruptIfRunning));
1519 <        f.complete(v1);
1520 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1521 <
1522 <        checkCompletedWithWrappedCancellationException(h);
1523 <        checkCancelled(g);
1524 <        assertEquals(r.invocationCount, 0);
1525 <        checkCompletedNormally(f, v1);
1526 <        }
1527 <    }
1528 <
1529 <    public void testThenAcceptBoth_sourceCancelled4() {
1861 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1862          for (ExecutionMode m : ExecutionMode.values())
1863          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1864 <        for (Integer v1 : new Integer[] { 1, null }) {
1865 <
1864 >        for (boolean fFirst : new boolean[] { true, false })
1865 >        for (boolean failFirst : new boolean[] { true, false })
1866 >        for (Integer v1 : new Integer[] { 1, null })
1867 >    {
1868          final CompletableFuture<Integer> f = new CompletableFuture<>();
1869          final CompletableFuture<Integer> g = new CompletableFuture<>();
1870 <        final SubtractAction r = new SubtractAction();
1871 <
1872 <        assertTrue(f.cancel(mayInterruptIfRunning));
1873 <        g.complete(v1);
1874 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1875 <
1876 <        checkCompletedWithWrappedCancellationException(h);
1877 <        checkCancelled(f);
1878 <        assertEquals(r.invocationCount, 0);
1879 <        checkCompletedNormally(g, v1);
1880 <        }
1881 <    }
1870 >        final SubtractAction r1 = new SubtractAction(m);
1871 >        final SubtractAction r2 = new SubtractAction(m);
1872 >        final SubtractAction r3 = new SubtractAction(m);
1873 >
1874 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1875 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1876 >        final Callable<Boolean> complete1 = failFirst ?
1877 >            () -> fst.cancel(mayInterruptIfRunning) :
1878 >            () -> fst.complete(v1);
1879 >        final Callable<Boolean> complete2 = failFirst ?
1880 >            () -> snd.complete(v1) :
1881 >            () -> snd.cancel(mayInterruptIfRunning);
1882 >
1883 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1884 >        assertTrue(complete1.call());
1885 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1886 >        checkIncomplete(h1);
1887 >        checkIncomplete(h2);
1888 >        assertTrue(complete2.call());
1889 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1890 >
1891 >        checkCompletedWithWrappedCancellationException(h1);
1892 >        checkCompletedWithWrappedCancellationException(h2);
1893 >        checkCompletedWithWrappedCancellationException(h3);
1894 >        r1.assertNotInvoked();
1895 >        r2.assertNotInvoked();
1896 >        r3.assertNotInvoked();
1897 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1898 >        checkCancelled(failFirst ? fst : snd);
1899 >    }}
1900  
1901      /**
1902 <     * runAfterBoth result completes normally after normal
1551 <     * completion of sources
1902 >     * thenAcceptBoth result completes exceptionally if action does
1903       */
1904 <    public void testRunAfterBoth_normalCompletion1() {
1554 <        for (ExecutionMode m : ExecutionMode.values())
1555 <        for (Integer v1 : new Integer[] { 1, null })
1556 <        for (Integer v2 : new Integer[] { 2, null }) {
1557 <
1558 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1559 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1560 <        final Noop r = new Noop();
1561 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1562 <
1563 <        f.complete(v1);
1564 <        checkIncomplete(h);
1565 <        assertFalse(r.ran);
1566 <        g.complete(v2);
1567 <
1568 <        checkCompletedNormally(h, null);
1569 <        assertEquals(r.invocationCount, 1);
1570 <        checkCompletedNormally(f, v1);
1571 <        checkCompletedNormally(g, v2);
1572 <        }
1573 <    }
1574 <
1575 <    public void testRunAfterBoth_normalCompletion2() {
1904 >    public void testThenAcceptBoth_actionFailed() {
1905          for (ExecutionMode m : ExecutionMode.values())
1906 +        for (boolean fFirst : new boolean[] { true, false })
1907          for (Integer v1 : new Integer[] { 1, null })
1908 <        for (Integer v2 : new Integer[] { 2, null }) {
1909 <
1908 >        for (Integer v2 : new Integer[] { 2, null })
1909 >    {
1910          final CompletableFuture<Integer> f = new CompletableFuture<>();
1911          final CompletableFuture<Integer> g = new CompletableFuture<>();
1912 <        final Noop r = new Noop();
1913 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1912 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1913 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1914 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1915  
1916 <        g.complete(v2);
1917 <        checkIncomplete(h);
1918 <        assertFalse(r.ran);
1919 <        f.complete(v1);
1916 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1917 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1918 >        final Integer w1 =  fFirst ? v1 : v2;
1919 >        final Integer w2 = !fFirst ? v1 : v2;
1920  
1921 <        checkCompletedNormally(h, null);
1922 <        assertEquals(r.invocationCount, 1);
1923 <        checkCompletedNormally(f, v1);
1924 <        checkCompletedNormally(g, v2);
1925 <        }
1595 <    }
1921 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1922 >        assertTrue(fst.complete(w1));
1923 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1924 >        assertTrue(snd.complete(w2));
1925 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1926  
1927 <    public void testRunAfterBoth_normalCompletion3() {
1928 <        for (ExecutionMode m : ExecutionMode.values())
1929 <        for (Integer v1 : new Integer[] { 1, null })
1930 <        for (Integer v2 : new Integer[] { 2, null }) {
1931 <
1932 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1603 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1604 <        final Noop r = new Noop();
1605 <
1606 <        g.complete(v2);
1607 <        f.complete(v1);
1608 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1609 <
1610 <        checkCompletedNormally(h, null);
1611 <        assertTrue(r.ran);
1927 >        checkCompletedWithWrappedCFException(h1);
1928 >        checkCompletedWithWrappedCFException(h2);
1929 >        checkCompletedWithWrappedCFException(h3);
1930 >        r1.assertInvoked();
1931 >        r2.assertInvoked();
1932 >        r3.assertInvoked();
1933          checkCompletedNormally(f, v1);
1934          checkCompletedNormally(g, v2);
1935 <        }
1615 <    }
1935 >    }}
1936  
1937 <    public void testRunAfterBoth_normalCompletion4() {
1937 >    /**
1938 >     * runAfterBoth result completes normally after normal
1939 >     * completion of sources
1940 >     */
1941 >    public void testRunAfterBoth_normalCompletion() {
1942          for (ExecutionMode m : ExecutionMode.values())
1943 +        for (boolean fFirst : new boolean[] { true, false })
1944          for (Integer v1 : new Integer[] { 1, null })
1945 <        for (Integer v2 : new Integer[] { 2, null }) {
1946 <
1945 >        for (Integer v2 : new Integer[] { 2, null })
1946 >    {
1947          final CompletableFuture<Integer> f = new CompletableFuture<>();
1948          final CompletableFuture<Integer> g = new CompletableFuture<>();
1949 <        final Noop r = new Noop();
1950 <
1951 <        f.complete(v1);
1952 <        g.complete(v2);
1953 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1954 <
1955 <        checkCompletedNormally(h, null);
1956 <        assertEquals(r.invocationCount, 1);
1949 >        final Noop r1 = new Noop(m);
1950 >        final Noop r2 = new Noop(m);
1951 >        final Noop r3 = new Noop(m);
1952 >
1953 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1954 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1955 >        final Integer w1 =  fFirst ? v1 : v2;
1956 >        final Integer w2 = !fFirst ? v1 : v2;
1957 >
1958 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1959 >        assertTrue(fst.complete(w1));
1960 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1961 >        checkIncomplete(h1);
1962 >        checkIncomplete(h2);
1963 >        r1.assertNotInvoked();
1964 >        r2.assertNotInvoked();
1965 >        assertTrue(snd.complete(w2));
1966 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1967 >
1968 >        checkCompletedNormally(h1, null);
1969 >        checkCompletedNormally(h2, null);
1970 >        checkCompletedNormally(h3, null);
1971 >        r1.assertInvoked();
1972 >        r2.assertInvoked();
1973 >        r3.assertInvoked();
1974          checkCompletedNormally(f, v1);
1975          checkCompletedNormally(g, v2);
1976 <        }
1635 <    }
1976 >    }}
1977  
1978      /**
1979       * runAfterBoth result completes exceptionally after exceptional
1980       * completion of either source
1981       */
1982 <    public void testRunAfterBoth_exceptionalCompletion1() {
1642 <        for (ExecutionMode m : ExecutionMode.values())
1643 <        for (Integer v1 : new Integer[] { 1, null }) {
1644 <
1645 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1646 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1647 <        final Noop r = new Noop();
1648 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1649 <        final CFException ex = new CFException();
1650 <
1651 <        f.completeExceptionally(ex);
1652 <        checkIncomplete(h);
1653 <        g.complete(v1);
1654 <
1655 <        checkCompletedWithWrappedCFException(h, ex);
1656 <        checkCompletedWithWrappedCFException(f, ex);
1657 <        assertEquals(r.invocationCount, 0);
1658 <        checkCompletedNormally(g, v1);
1659 <        }
1660 <    }
1661 <
1662 <    public void testRunAfterBoth_exceptionalCompletion2() {
1663 <        for (ExecutionMode m : ExecutionMode.values())
1664 <        for (Integer v1 : new Integer[] { 1, null }) {
1665 <
1666 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1667 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1668 <        final Noop r = new Noop();
1669 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1670 <        final CFException ex = new CFException();
1671 <
1672 <        g.completeExceptionally(ex);
1673 <        checkIncomplete(h);
1674 <        f.complete(v1);
1675 <
1676 <        checkCompletedWithWrappedCFException(h, ex);
1677 <        checkCompletedWithWrappedCFException(g, ex);
1678 <        assertEquals(r.invocationCount, 0);
1679 <        checkCompletedNormally(f, v1);
1680 <        }
1681 <    }
1682 <
1683 <    public void testRunAfterBoth_exceptionalCompletion3() {
1982 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1983          for (ExecutionMode m : ExecutionMode.values())
1984 <        for (Integer v1 : new Integer[] { 1, null }) {
1985 <
1986 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1987 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1689 <        final Noop r = new Noop();
1690 <        final CFException ex = new CFException();
1691 <
1692 <        g.completeExceptionally(ex);
1693 <        f.complete(v1);
1694 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1695 <
1696 <        checkCompletedWithWrappedCFException(h, ex);
1697 <        checkCompletedWithWrappedCFException(g, ex);
1698 <        assertEquals(r.invocationCount, 0);
1699 <        checkCompletedNormally(f, v1);
1700 <        }
1701 <    }
1702 <
1703 <    public void testRunAfterBoth_exceptionalCompletion4() {
1704 <        for (ExecutionMode m : ExecutionMode.values())
1705 <        for (Integer v1 : new Integer[] { 1, null }) {
1706 <
1984 >        for (boolean fFirst : new boolean[] { true, false })
1985 >        for (boolean failFirst : new boolean[] { true, false })
1986 >        for (Integer v1 : new Integer[] { 1, null })
1987 >    {
1988          final CompletableFuture<Integer> f = new CompletableFuture<>();
1989          final CompletableFuture<Integer> g = new CompletableFuture<>();
1709        final Noop r = new Noop();
1990          final CFException ex = new CFException();
1991 <
1992 <        f.completeExceptionally(ex);
1993 <        g.complete(v1);
1994 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1995 <
1996 <        checkCompletedWithWrappedCFException(h, ex);
1997 <        checkCompletedWithWrappedCFException(f, ex);
1998 <        assertEquals(r.invocationCount, 0);
1999 <        checkCompletedNormally(g, v1);
2000 <        }
2001 <    }
1991 >        final Noop r1 = new Noop(m);
1992 >        final Noop r2 = new Noop(m);
1993 >        final Noop r3 = new Noop(m);
1994 >
1995 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1996 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1997 >        final Callable<Boolean> complete1 = failFirst ?
1998 >            () -> fst.completeExceptionally(ex) :
1999 >            () -> fst.complete(v1);
2000 >        final Callable<Boolean> complete2 = failFirst ?
2001 >            () -> snd.complete(v1) :
2002 >            () -> snd.completeExceptionally(ex);
2003 >
2004 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2005 >        assertTrue(complete1.call());
2006 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2007 >        checkIncomplete(h1);
2008 >        checkIncomplete(h2);
2009 >        assertTrue(complete2.call());
2010 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2011 >
2012 >        checkCompletedWithWrappedException(h1, ex);
2013 >        checkCompletedWithWrappedException(h2, ex);
2014 >        checkCompletedWithWrappedException(h3, ex);
2015 >        r1.assertNotInvoked();
2016 >        r2.assertNotInvoked();
2017 >        r3.assertNotInvoked();
2018 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2019 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2020 >    }}
2021  
2022      /**
2023 <     * runAfterBoth result completes exceptionally if action does
2023 >     * runAfterBoth result completes exceptionally if either source cancelled
2024       */
2025 <    public void testRunAfterBoth_actionFailed1() {
1727 <        for (ExecutionMode m : ExecutionMode.values())
1728 <        for (Integer v1 : new Integer[] { 1, null })
1729 <        for (Integer v2 : new Integer[] { 2, null }) {
1730 <
1731 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1732 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1733 <        final FailingNoop r = new FailingNoop();
1734 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1735 <
1736 <        f.complete(v1);
1737 <        checkIncomplete(h);
1738 <        g.complete(v2);
1739 <
1740 <        checkCompletedWithWrappedCFException(h);
1741 <        checkCompletedNormally(f, v1);
1742 <        checkCompletedNormally(g, v2);
1743 <        }
1744 <    }
1745 <
1746 <    public void testRunAfterBoth_actionFailed2() {
2025 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2026          for (ExecutionMode m : ExecutionMode.values())
2027 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2028 +        for (boolean fFirst : new boolean[] { true, false })
2029 +        for (boolean failFirst : new boolean[] { true, false })
2030          for (Integer v1 : new Integer[] { 1, null })
2031 <        for (Integer v2 : new Integer[] { 2, null }) {
1750 <
2031 >    {
2032          final CompletableFuture<Integer> f = new CompletableFuture<>();
2033          final CompletableFuture<Integer> g = new CompletableFuture<>();
2034 <        final FailingNoop r = new FailingNoop();
2035 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2036 <
2037 <        g.complete(v2);
2038 <        checkIncomplete(h);
2039 <        f.complete(v1);
2040 <
2041 <        checkCompletedWithWrappedCFException(h);
2042 <        checkCompletedNormally(f, v1);
2043 <        checkCompletedNormally(g, v2);
2044 <        }
2045 <    }
2034 >        final Noop r1 = new Noop(m);
2035 >        final Noop r2 = new Noop(m);
2036 >        final Noop r3 = new Noop(m);
2037 >
2038 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2039 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2040 >        final Callable<Boolean> complete1 = failFirst ?
2041 >            () -> fst.cancel(mayInterruptIfRunning) :
2042 >            () -> fst.complete(v1);
2043 >        final Callable<Boolean> complete2 = failFirst ?
2044 >            () -> snd.complete(v1) :
2045 >            () -> snd.cancel(mayInterruptIfRunning);
2046 >
2047 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2048 >        assertTrue(complete1.call());
2049 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2050 >        checkIncomplete(h1);
2051 >        checkIncomplete(h2);
2052 >        assertTrue(complete2.call());
2053 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2054 >
2055 >        checkCompletedWithWrappedCancellationException(h1);
2056 >        checkCompletedWithWrappedCancellationException(h2);
2057 >        checkCompletedWithWrappedCancellationException(h3);
2058 >        r1.assertNotInvoked();
2059 >        r2.assertNotInvoked();
2060 >        r3.assertNotInvoked();
2061 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2062 >        checkCancelled(failFirst ? fst : snd);
2063 >    }}
2064  
2065      /**
2066 <     * runAfterBoth result completes exceptionally if either source cancelled
2066 >     * runAfterBoth result completes exceptionally if action does
2067       */
2068 <    public void testRunAfterBoth_sourceCancelled1() {
2068 >    public void testRunAfterBoth_actionFailed() {
2069          for (ExecutionMode m : ExecutionMode.values())
2070 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2071 <        for (Integer v1 : new Integer[] { 1, null }) {
2072 <
2073 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1775 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1776 <        final Noop r = new Noop();
1777 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1778 <
1779 <        assertTrue(f.cancel(mayInterruptIfRunning));
1780 <        checkIncomplete(h);
1781 <        g.complete(v1);
1782 <
1783 <        checkCompletedWithWrappedCancellationException(h);
1784 <        checkCancelled(f);
1785 <        assertEquals(r.invocationCount, 0);
1786 <        checkCompletedNormally(g, v1);
1787 <        }
1788 <    }
1789 <
1790 <    public void testRunAfterBoth_sourceCancelled2() {
1791 <        for (ExecutionMode m : ExecutionMode.values())
1792 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1793 <        for (Integer v1 : new Integer[] { 1, null }) {
1794 <
2070 >        for (boolean fFirst : new boolean[] { true, false })
2071 >        for (Integer v1 : new Integer[] { 1, null })
2072 >        for (Integer v2 : new Integer[] { 2, null })
2073 >    {
2074          final CompletableFuture<Integer> f = new CompletableFuture<>();
2075          final CompletableFuture<Integer> g = new CompletableFuture<>();
2076 <        final Noop r = new Noop();
2077 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2078 <
1800 <        assertTrue(g.cancel(mayInterruptIfRunning));
1801 <        checkIncomplete(h);
1802 <        f.complete(v1);
1803 <
1804 <        checkCompletedWithWrappedCancellationException(h);
1805 <        checkCancelled(g);
1806 <        assertEquals(r.invocationCount, 0);
1807 <        checkCompletedNormally(f, v1);
1808 <        }
1809 <    }
1810 <
1811 <    public void testRunAfterBoth_sourceCancelled3() {
1812 <        for (ExecutionMode m : ExecutionMode.values())
1813 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1814 <        for (Integer v1 : new Integer[] { 1, null }) {
2076 >        final FailingRunnable r1 = new FailingRunnable(m);
2077 >        final FailingRunnable r2 = new FailingRunnable(m);
2078 >        final FailingRunnable r3 = new FailingRunnable(m);
2079  
2080 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2081 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2082 <        final Noop r = new Noop();
2080 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2081 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2082 >        final Integer w1 =  fFirst ? v1 : v2;
2083 >        final Integer w2 = !fFirst ? v1 : v2;
2084  
2085 <        assertTrue(g.cancel(mayInterruptIfRunning));
2086 <        f.complete(v1);
2087 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2085 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2086 >        assertTrue(fst.complete(w1));
2087 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2088 >        assertTrue(snd.complete(w2));
2089 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2090  
2091 <        checkCompletedWithWrappedCancellationException(h);
2092 <        checkCancelled(g);
2093 <        assertEquals(r.invocationCount, 0);
2091 >        checkCompletedWithWrappedCFException(h1);
2092 >        checkCompletedWithWrappedCFException(h2);
2093 >        checkCompletedWithWrappedCFException(h3);
2094 >        r1.assertInvoked();
2095 >        r2.assertInvoked();
2096 >        r3.assertInvoked();
2097          checkCompletedNormally(f, v1);
2098 <        }
2099 <    }
1830 <
1831 <    public void testRunAfterBoth_sourceCancelled4() {
1832 <        for (ExecutionMode m : ExecutionMode.values())
1833 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1834 <        for (Integer v1 : new Integer[] { 1, null }) {
1835 <
1836 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1837 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1838 <        final Noop r = new Noop();
1839 <
1840 <        assertTrue(f.cancel(mayInterruptIfRunning));
1841 <        g.complete(v1);
1842 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1843 <
1844 <        checkCompletedWithWrappedCancellationException(h);
1845 <        checkCancelled(f);
1846 <        assertEquals(r.invocationCount, 0);
1847 <        checkCompletedNormally(g, v1);
1848 <        }
1849 <    }
2098 >        checkCompletedNormally(g, v2);
2099 >    }}
2100  
2101      /**
2102       * applyToEither result completes normally after normal completion
2103       * of either source
2104       */
2105 <    public void testApplyToEither_normalCompletion1() {
2105 >    public void testApplyToEither_normalCompletion() {
2106          for (ExecutionMode m : ExecutionMode.values())
2107          for (Integer v1 : new Integer[] { 1, null })
2108 <        for (Integer v2 : new Integer[] { 2, null }) {
2109 <
2108 >        for (Integer v2 : new Integer[] { 2, null })
2109 >    {
2110          final CompletableFuture<Integer> f = new CompletableFuture<>();
2111          final CompletableFuture<Integer> g = new CompletableFuture<>();
2112 <        final IncFunction r = new IncFunction();
2113 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2112 >        final IncFunction[] rs = new IncFunction[6];
2113 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2114  
2115 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2116 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2117 +        checkIncomplete(h0);
2118 +        checkIncomplete(h1);
2119 +        rs[0].assertNotInvoked();
2120 +        rs[1].assertNotInvoked();
2121          f.complete(v1);
2122 <        checkCompletedNormally(h, inc(v1));
2123 <        g.complete(v2);
2124 <
2125 <        checkCompletedNormally(f, v1);
2126 <        checkCompletedNormally(g, v2);
2127 <        checkCompletedNormally(h, inc(v1));
1872 <        }
1873 <    }
1874 <
1875 <    public void testApplyToEither_normalCompletion2() {
1876 <        for (ExecutionMode m : ExecutionMode.values())
1877 <        for (Integer v1 : new Integer[] { 1, null })
1878 <        for (Integer v2 : new Integer[] { 2, null }) {
1879 <
1880 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1881 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1882 <        final IncFunction r = new IncFunction();
1883 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1884 <
2122 >        checkCompletedNormally(h0, inc(v1));
2123 >        checkCompletedNormally(h1, inc(v1));
2124 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2125 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2126 >        checkCompletedNormally(h2, inc(v1));
2127 >        checkCompletedNormally(h3, inc(v1));
2128          g.complete(v2);
1886        checkCompletedNormally(h, inc(v2));
1887        f.complete(v1);
2129  
2130 <        checkCompletedNormally(f, v1);
2131 <        checkCompletedNormally(g, v2);
2132 <        checkCompletedNormally(h, inc(v2));
2133 <        }
2134 <    }
2135 <    public void testApplyToEither_normalCompletion3() {
2136 <        for (ExecutionMode m : ExecutionMode.values())
2137 <        for (Integer v1 : new Integer[] { 1, null })
2138 <        for (Integer v2 : new Integer[] { 2, null }) {
1898 <
1899 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1900 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1901 <        final IncFunction r = new IncFunction();
1902 <
1903 <        f.complete(v1);
1904 <        g.complete(v2);
1905 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2130 >        // unspecified behavior - both source completions available
2131 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2132 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2133 >        rs[4].assertValue(h4.join());
2134 >        rs[5].assertValue(h5.join());
2135 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
2136 >                   Objects.equals(inc(v2), h4.join()));
2137 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
2138 >                   Objects.equals(inc(v2), h5.join()));
2139  
2140          checkCompletedNormally(f, v1);
2141          checkCompletedNormally(g, v2);
2142 <
2143 <        // unspecified behavior
2144 <        assertTrue(Objects.equals(h.join(), inc(v1)) ||
2145 <                   Objects.equals(h.join(), inc(v2)));
2146 <        assertEquals(r.invocationCount, 1);
2147 <        }
1915 <    }
2142 >        checkCompletedNormally(h0, inc(v1));
2143 >        checkCompletedNormally(h1, inc(v1));
2144 >        checkCompletedNormally(h2, inc(v1));
2145 >        checkCompletedNormally(h3, inc(v1));
2146 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
2147 >    }}
2148  
2149      /**
2150       * applyToEither result completes exceptionally after exceptional
2151       * completion of either source
2152       */
2153 <    public void testApplyToEither_exceptionalCompletion1() {
2153 >    public void testApplyToEither_exceptionalCompletion() {
2154          for (ExecutionMode m : ExecutionMode.values())
2155 <        for (Integer v1 : new Integer[] { 1, null }) {
2156 <
2155 >        for (Integer v1 : new Integer[] { 1, null })
2156 >    {
2157          final CompletableFuture<Integer> f = new CompletableFuture<>();
2158          final CompletableFuture<Integer> g = new CompletableFuture<>();
1927        final IncFunction r = new IncFunction();
1928        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2159          final CFException ex = new CFException();
2160 +        final IncFunction[] rs = new IncFunction[6];
2161 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2162  
2163 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2164 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2165 +        checkIncomplete(h0);
2166 +        checkIncomplete(h1);
2167 +        rs[0].assertNotInvoked();
2168 +        rs[1].assertNotInvoked();
2169          f.completeExceptionally(ex);
2170 <        checkCompletedWithWrappedCFException(h, ex);
2170 >        checkCompletedWithWrappedException(h0, ex);
2171 >        checkCompletedWithWrappedException(h1, ex);
2172 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2173 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2174 >        checkCompletedWithWrappedException(h2, ex);
2175 >        checkCompletedWithWrappedException(h3, ex);
2176          g.complete(v1);
2177  
2178 <        assertEquals(r.invocationCount, 0);
2179 <        checkCompletedNormally(g, v1);
2180 <        checkCompletedWithWrappedCFException(f, ex);
2181 <        checkCompletedWithWrappedCFException(h, ex);
2182 <        }
2183 <    }
2184 <
2185 <    public void testApplyToEither_exceptionalCompletion2() {
2186 <        for (ExecutionMode m : ExecutionMode.values())
1944 <        for (Integer v1 : new Integer[] { 1, null }) {
1945 <
1946 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1947 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1948 <        final IncFunction r = new IncFunction();
1949 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1950 <        final CFException ex = new CFException();
1951 <
1952 <        g.completeExceptionally(ex);
1953 <        checkCompletedWithWrappedCFException(h, ex);
1954 <        f.complete(v1);
1955 <
1956 <        assertEquals(r.invocationCount, 0);
1957 <        checkCompletedNormally(f, v1);
1958 <        checkCompletedWithWrappedCFException(g, ex);
1959 <        checkCompletedWithWrappedCFException(h, ex);
2178 >        // unspecified behavior - both source completions available
2179 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2180 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2181 >        try {
2182 >            assertEquals(inc(v1), h4.join());
2183 >            rs[4].assertValue(inc(v1));
2184 >        } catch (CompletionException ok) {
2185 >            checkCompletedWithWrappedException(h4, ex);
2186 >            rs[4].assertNotInvoked();
2187          }
1961    }
1962
1963    public void testApplyToEither_exceptionalCompletion3() {
1964        for (ExecutionMode m : ExecutionMode.values())
1965        for (Integer v1 : new Integer[] { 1, null }) {
1966
1967        final CompletableFuture<Integer> f = new CompletableFuture<>();
1968        final CompletableFuture<Integer> g = new CompletableFuture<>();
1969        final IncFunction r = new IncFunction();
1970        final CFException ex = new CFException();
1971
1972        g.completeExceptionally(ex);
1973        f.complete(v1);
1974        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1975
1976        // unspecified behavior
1977        Integer v;
2188          try {
2189 <            assertEquals(h.join(), inc(v1));
2190 <            assertEquals(r.invocationCount, 1);
2189 >            assertEquals(inc(v1), h5.join());
2190 >            rs[5].assertValue(inc(v1));
2191          } catch (CompletionException ok) {
2192 <            checkCompletedWithWrappedCFException(h, ex);
2193 <            assertEquals(r.invocationCount, 0);
2192 >            checkCompletedWithWrappedException(h5, ex);
2193 >            rs[5].assertNotInvoked();
2194          }
2195  
2196 <        checkCompletedWithWrappedCFException(g, ex);
2197 <        checkCompletedNormally(f, v1);
2198 <        }
2199 <    }
2196 >        checkCompletedExceptionally(f, ex);
2197 >        checkCompletedNormally(g, v1);
2198 >        checkCompletedWithWrappedException(h0, ex);
2199 >        checkCompletedWithWrappedException(h1, ex);
2200 >        checkCompletedWithWrappedException(h2, ex);
2201 >        checkCompletedWithWrappedException(h3, ex);
2202 >        checkCompletedWithWrappedException(h4, ex);
2203 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2204 >    }}
2205  
2206 <    public void testApplyToEither_exceptionalCompletion4() {
2206 >    public void testApplyToEither_exceptionalCompletion2() {
2207          for (ExecutionMode m : ExecutionMode.values())
2208 <        for (Integer v1 : new Integer[] { 1, null }) {
2209 <
2208 >        for (boolean fFirst : new boolean[] { true, false })
2209 >        for (Integer v1 : new Integer[] { 1, null })
2210 >    {
2211          final CompletableFuture<Integer> f = new CompletableFuture<>();
2212          final CompletableFuture<Integer> g = new CompletableFuture<>();
1997        final IncFunction r = new IncFunction();
2213          final CFException ex = new CFException();
2214 +        final IncFunction[] rs = new IncFunction[6];
2215 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2216  
2217 <        f.completeExceptionally(ex);
2218 <        g.complete(v1);
2219 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2217 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2218 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2219 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2220 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2221 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2222 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2223  
2224 <        // unspecified behavior
2005 <        Integer v;
2224 >        // unspecified behavior - both source completions available
2225          try {
2226 <            assertEquals(h.join(), inc(v1));
2227 <            assertEquals(r.invocationCount, 1);
2226 >            assertEquals(inc(v1), h0.join());
2227 >            rs[0].assertValue(inc(v1));
2228          } catch (CompletionException ok) {
2229 <            checkCompletedWithWrappedCFException(h, ex);
2230 <            assertEquals(r.invocationCount, 0);
2229 >            checkCompletedWithWrappedException(h0, ex);
2230 >            rs[0].assertNotInvoked();
2231          }
2232 <
2233 <        checkCompletedWithWrappedCFException(f, ex);
2234 <        checkCompletedNormally(g, v1);
2232 >        try {
2233 >            assertEquals(inc(v1), h1.join());
2234 >            rs[1].assertValue(inc(v1));
2235 >        } catch (CompletionException ok) {
2236 >            checkCompletedWithWrappedException(h1, ex);
2237 >            rs[1].assertNotInvoked();
2238          }
2239 <    }
2240 <
2241 <    /**
2242 <     * applyToEither result completes exceptionally if action does
2243 <     */
2244 <    public void testApplyToEither_actionFailed1() {
2245 <        for (ExecutionMode m : ExecutionMode.values())
2246 <        for (Integer v1 : new Integer[] { 1, null })
2247 <        for (Integer v2 : new Integer[] { 2, null }) {
2248 <
2249 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2250 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2251 <        final FailingFunction r = new FailingFunction();
2030 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2031 <
2032 <        f.complete(v1);
2033 <        checkCompletedWithWrappedCFException(h);
2034 <        g.complete(v2);
2035 <        checkCompletedNormally(f, v1);
2036 <        checkCompletedNormally(g, v2);
2239 >        try {
2240 >            assertEquals(inc(v1), h2.join());
2241 >            rs[2].assertValue(inc(v1));
2242 >        } catch (CompletionException ok) {
2243 >            checkCompletedWithWrappedException(h2, ex);
2244 >            rs[2].assertNotInvoked();
2245 >        }
2246 >        try {
2247 >            assertEquals(inc(v1), h3.join());
2248 >            rs[3].assertValue(inc(v1));
2249 >        } catch (CompletionException ok) {
2250 >            checkCompletedWithWrappedException(h3, ex);
2251 >            rs[3].assertNotInvoked();
2252          }
2038    }
2039
2040    public void testApplyToEither_actionFailed2() {
2041        for (ExecutionMode m : ExecutionMode.values())
2042        for (Integer v1 : new Integer[] { 1, null })
2043        for (Integer v2 : new Integer[] { 2, null }) {
2044
2045        final CompletableFuture<Integer> f = new CompletableFuture<>();
2046        final CompletableFuture<Integer> g = new CompletableFuture<>();
2047        final FailingFunction r = new FailingFunction();
2048        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2253  
2050        g.complete(v2);
2051        checkCompletedWithWrappedCFException(h);
2052        f.complete(v1);
2254          checkCompletedNormally(f, v1);
2255 <        checkCompletedNormally(g, v2);
2256 <        }
2056 <    }
2255 >        checkCompletedExceptionally(g, ex);
2256 >    }}
2257  
2258      /**
2259       * applyToEither result completes exceptionally if either source cancelled
2260       */
2261 <    public void testApplyToEither_sourceCancelled1() {
2261 >    public void testApplyToEither_sourceCancelled() {
2262          for (ExecutionMode m : ExecutionMode.values())
2263          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2264 <        for (Integer v1 : new Integer[] { 1, null }) {
2265 <
2264 >        for (Integer v1 : new Integer[] { 1, null })
2265 >    {
2266          final CompletableFuture<Integer> f = new CompletableFuture<>();
2267          final CompletableFuture<Integer> g = new CompletableFuture<>();
2268 <        final IncFunction r = new IncFunction();
2269 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2268 >        final IncFunction[] rs = new IncFunction[6];
2269 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2270  
2271 <        assertTrue(f.cancel(mayInterruptIfRunning));
2272 <        checkCompletedWithWrappedCancellationException(h);
2271 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2272 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2273 >        checkIncomplete(h0);
2274 >        checkIncomplete(h1);
2275 >        rs[0].assertNotInvoked();
2276 >        rs[1].assertNotInvoked();
2277 >        f.cancel(mayInterruptIfRunning);
2278 >        checkCompletedWithWrappedCancellationException(h0);
2279 >        checkCompletedWithWrappedCancellationException(h1);
2280 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2281 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2282 >        checkCompletedWithWrappedCancellationException(h2);
2283 >        checkCompletedWithWrappedCancellationException(h3);
2284          g.complete(v1);
2285  
2286 +        // unspecified behavior - both source completions available
2287 +        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2288 +        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2289 +        try {
2290 +            assertEquals(inc(v1), h4.join());
2291 +            rs[4].assertValue(inc(v1));
2292 +        } catch (CompletionException ok) {
2293 +            checkCompletedWithWrappedCancellationException(h4);
2294 +            rs[4].assertNotInvoked();
2295 +        }
2296 +        try {
2297 +            assertEquals(inc(v1), h5.join());
2298 +            rs[5].assertValue(inc(v1));
2299 +        } catch (CompletionException ok) {
2300 +            checkCompletedWithWrappedCancellationException(h5);
2301 +            rs[5].assertNotInvoked();
2302 +        }
2303 +
2304          checkCancelled(f);
2076        assertEquals(r.invocationCount, 0);
2305          checkCompletedNormally(g, v1);
2306 <        checkCompletedWithWrappedCancellationException(h);
2307 <        }
2308 <    }
2306 >        checkCompletedWithWrappedCancellationException(h0);
2307 >        checkCompletedWithWrappedCancellationException(h1);
2308 >        checkCompletedWithWrappedCancellationException(h2);
2309 >        checkCompletedWithWrappedCancellationException(h3);
2310 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2311 >    }}
2312  
2313      public void testApplyToEither_sourceCancelled2() {
2314          for (ExecutionMode m : ExecutionMode.values())
2315          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2316 <        for (Integer v1 : new Integer[] { 1, null }) {
2317 <
2316 >        for (boolean fFirst : new boolean[] { true, false })
2317 >        for (Integer v1 : new Integer[] { 1, null })
2318 >    {
2319          final CompletableFuture<Integer> f = new CompletableFuture<>();
2320          final CompletableFuture<Integer> g = new CompletableFuture<>();
2321 <        final IncFunction r = new IncFunction();
2322 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2321 >        final IncFunction[] rs = new IncFunction[6];
2322 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2323  
2324 <        assertTrue(g.cancel(mayInterruptIfRunning));
2325 <        checkCompletedWithWrappedCancellationException(h);
2326 <        f.complete(v1);
2324 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2325 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2326 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2327 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2328 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2329 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2330  
2331 <        checkCancelled(g);
2332 <        assertEquals(r.invocationCount, 0);
2333 <        checkCompletedNormally(f, v1);
2334 <        checkCompletedWithWrappedCancellationException(h);
2331 >        // unspecified behavior - both source completions available
2332 >        try {
2333 >            assertEquals(inc(v1), h0.join());
2334 >            rs[0].assertValue(inc(v1));
2335 >        } catch (CompletionException ok) {
2336 >            checkCompletedWithWrappedCancellationException(h0);
2337 >            rs[0].assertNotInvoked();
2338          }
2101    }
2102
2103    public void testApplyToEither_sourceCancelled3() {
2104        for (ExecutionMode m : ExecutionMode.values())
2105        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2106        for (Integer v1 : new Integer[] { 1, null }) {
2107
2108        final CompletableFuture<Integer> f = new CompletableFuture<>();
2109        final CompletableFuture<Integer> g = new CompletableFuture<>();
2110        final IncFunction r = new IncFunction();
2111
2112        assertTrue(g.cancel(mayInterruptIfRunning));
2113        f.complete(v1);
2114        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2115
2116        // unspecified behavior
2117        Integer v;
2339          try {
2340 <            assertEquals(h.join(), inc(v1));
2341 <            assertEquals(r.invocationCount, 1);
2340 >            assertEquals(inc(v1), h1.join());
2341 >            rs[1].assertValue(inc(v1));
2342          } catch (CompletionException ok) {
2343 <            checkCompletedWithWrappedCancellationException(h);
2344 <            assertEquals(r.invocationCount, 0);
2343 >            checkCompletedWithWrappedCancellationException(h1);
2344 >            rs[1].assertNotInvoked();
2345          }
2346 <
2347 <        checkCancelled(g);
2348 <        checkCompletedNormally(f, v1);
2346 >        try {
2347 >            assertEquals(inc(v1), h2.join());
2348 >            rs[2].assertValue(inc(v1));
2349 >        } catch (CompletionException ok) {
2350 >            checkCompletedWithWrappedCancellationException(h2);
2351 >            rs[2].assertNotInvoked();
2352          }
2129    }
2130
2131    public void testApplyToEither_sourceCancelled4() {
2132        for (ExecutionMode m : ExecutionMode.values())
2133        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2134        for (Integer v1 : new Integer[] { 1, null }) {
2135
2136        final CompletableFuture<Integer> f = new CompletableFuture<>();
2137        final CompletableFuture<Integer> g = new CompletableFuture<>();
2138        final IncFunction r = new IncFunction();
2139
2140        assertTrue(f.cancel(mayInterruptIfRunning));
2141        g.complete(v1);
2142        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2143
2144        // unspecified behavior
2145        Integer v;
2353          try {
2354 <            assertEquals(h.join(), inc(v1));
2355 <            assertEquals(r.invocationCount, 1);
2354 >            assertEquals(inc(v1), h3.join());
2355 >            rs[3].assertValue(inc(v1));
2356          } catch (CompletionException ok) {
2357 <            checkCompletedWithWrappedCancellationException(h);
2358 <            assertEquals(r.invocationCount, 0);
2357 >            checkCompletedWithWrappedCancellationException(h3);
2358 >            rs[3].assertNotInvoked();
2359          }
2360  
2361 <        checkCancelled(f);
2362 <        checkCompletedNormally(g, v1);
2363 <        }
2157 <    }
2361 >        checkCompletedNormally(f, v1);
2362 >        checkCancelled(g);
2363 >    }}
2364  
2365      /**
2366 <     * acceptEither result completes normally after normal completion
2161 <     * of either source
2366 >     * applyToEither result completes exceptionally if action does
2367       */
2368 <    public void testAcceptEither_normalCompletion1() {
2368 >    public void testApplyToEither_actionFailed() {
2369          for (ExecutionMode m : ExecutionMode.values())
2370          for (Integer v1 : new Integer[] { 1, null })
2371 <        for (Integer v2 : new Integer[] { 2, null }) {
2372 <
2371 >        for (Integer v2 : new Integer[] { 2, null })
2372 >    {
2373          final CompletableFuture<Integer> f = new CompletableFuture<>();
2374          final CompletableFuture<Integer> g = new CompletableFuture<>();
2375 <        final IncAction r = new IncAction();
2376 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2375 >        final FailingFunction[] rs = new FailingFunction[6];
2376 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2377  
2378 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2379 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2380          f.complete(v1);
2381 <        checkCompletedNormally(h, null);
2382 <        assertEquals(r.value, inc(v1));
2381 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2382 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2383 >        checkCompletedWithWrappedCFException(h0);
2384 >        checkCompletedWithWrappedCFException(h1);
2385 >        checkCompletedWithWrappedCFException(h2);
2386 >        checkCompletedWithWrappedCFException(h3);
2387 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2388 >
2389          g.complete(v2);
2390  
2391 <        checkCompletedNormally(f, v1);
2392 <        checkCompletedNormally(g, v2);
2393 <        checkCompletedNormally(h, null);
2181 <        }
2182 <    }
2391 >        // unspecified behavior - both source completions available
2392 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2393 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2394  
2395 <    public void testAcceptEither_normalCompletion2() {
2396 <        for (ExecutionMode m : ExecutionMode.values())
2397 <        for (Integer v1 : new Integer[] { 1, null })
2398 <        for (Integer v2 : new Integer[] { 2, null }) {
2399 <
2400 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2190 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2191 <        final IncAction r = new IncAction();
2192 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2193 <
2194 <        g.complete(v2);
2195 <        checkCompletedNormally(h, null);
2196 <        assertEquals(r.value, inc(v2));
2197 <        f.complete(v1);
2395 >        checkCompletedWithWrappedCFException(h4);
2396 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2397 >                   Objects.equals(v2, rs[4].value));
2398 >        checkCompletedWithWrappedCFException(h5);
2399 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2400 >                   Objects.equals(v2, rs[5].value));
2401  
2402          checkCompletedNormally(f, v1);
2403          checkCompletedNormally(g, v2);
2404 <        checkCompletedNormally(h, null);
2405 <        }
2406 <    }
2407 <    public void testAcceptEither_normalCompletion3() {
2404 >    }}
2405 >
2406 >    /**
2407 >     * acceptEither result completes normally after normal completion
2408 >     * of either source
2409 >     */
2410 >    public void testAcceptEither_normalCompletion() {
2411          for (ExecutionMode m : ExecutionMode.values())
2412          for (Integer v1 : new Integer[] { 1, null })
2413 <        for (Integer v2 : new Integer[] { 2, null }) {
2414 <
2413 >        for (Integer v2 : new Integer[] { 2, null })
2414 >    {
2415          final CompletableFuture<Integer> f = new CompletableFuture<>();
2416          final CompletableFuture<Integer> g = new CompletableFuture<>();
2417 <        final IncAction r = new IncAction();
2417 >        final NoopConsumer[] rs = new NoopConsumer[6];
2418 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2419 >
2420 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2421 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2422 >        checkIncomplete(h0);
2423 >        checkIncomplete(h1);
2424 >        rs[0].assertNotInvoked();
2425 >        rs[1].assertNotInvoked();
2426 >        f.complete(v1);
2427 >        checkCompletedNormally(h0, null);
2428 >        checkCompletedNormally(h1, null);
2429 >        rs[0].assertValue(v1);
2430 >        rs[1].assertValue(v1);
2431 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2432 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2433 >        checkCompletedNormally(h2, null);
2434 >        checkCompletedNormally(h3, null);
2435 >        rs[2].assertValue(v1);
2436 >        rs[3].assertValue(v1);
2437 >        g.complete(v2);
2438 >
2439 >        // unspecified behavior - both source completions available
2440 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2441 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2442 >        checkCompletedNormally(h4, null);
2443 >        checkCompletedNormally(h5, null);
2444 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2445 >                   Objects.equals(v2, rs[4].value));
2446 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2447 >                   Objects.equals(v2, rs[5].value));
2448  
2213        f.complete(v1);
2214        g.complete(v2);
2215        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2216
2217        checkCompletedNormally(h, null);
2449          checkCompletedNormally(f, v1);
2450          checkCompletedNormally(g, v2);
2451 <
2452 <        // unspecified behavior
2453 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2454 <                   Objects.equals(r.value, inc(v2)));
2455 <        }
2456 <    }
2451 >        checkCompletedNormally(h0, null);
2452 >        checkCompletedNormally(h1, null);
2453 >        checkCompletedNormally(h2, null);
2454 >        checkCompletedNormally(h3, null);
2455 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2456 >    }}
2457  
2458      /**
2459       * acceptEither result completes exceptionally after exceptional
2460       * completion of either source
2461       */
2462 <    public void testAcceptEither_exceptionalCompletion1() {
2462 >    public void testAcceptEither_exceptionalCompletion() {
2463          for (ExecutionMode m : ExecutionMode.values())
2464 <        for (Integer v1 : new Integer[] { 1, null }) {
2465 <
2464 >        for (Integer v1 : new Integer[] { 1, null })
2465 >    {
2466          final CompletableFuture<Integer> f = new CompletableFuture<>();
2467          final CompletableFuture<Integer> g = new CompletableFuture<>();
2237        final IncAction r = new IncAction();
2238        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2468          final CFException ex = new CFException();
2469 +        final NoopConsumer[] rs = new NoopConsumer[6];
2470 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2471  
2472 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2473 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2474 +        checkIncomplete(h0);
2475 +        checkIncomplete(h1);
2476 +        rs[0].assertNotInvoked();
2477 +        rs[1].assertNotInvoked();
2478          f.completeExceptionally(ex);
2479 <        checkCompletedWithWrappedCFException(h, ex);
2480 <        g.complete(v1);
2479 >        checkCompletedWithWrappedException(h0, ex);
2480 >        checkCompletedWithWrappedException(h1, ex);
2481 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2482 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2483 >        checkCompletedWithWrappedException(h2, ex);
2484 >        checkCompletedWithWrappedException(h3, ex);
2485  
2486 <        assertEquals(r.invocationCount, 0);
2246 <        checkCompletedNormally(g, v1);
2247 <        checkCompletedWithWrappedCFException(f, ex);
2248 <        checkCompletedWithWrappedCFException(h, ex);
2249 <        }
2250 <    }
2251 <
2252 <    public void testAcceptEither_exceptionalCompletion2() {
2253 <        for (ExecutionMode m : ExecutionMode.values())
2254 <        for (Integer v1 : new Integer[] { 1, null }) {
2255 <
2256 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2257 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2258 <        final IncAction r = new IncAction();
2259 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2260 <        final CFException ex = new CFException();
2261 <
2262 <        g.completeExceptionally(ex);
2263 <        checkCompletedWithWrappedCFException(h, ex);
2264 <        f.complete(v1);
2486 >        g.complete(v1);
2487  
2488 <        assertEquals(r.invocationCount, 0);
2489 <        checkCompletedNormally(f, v1);
2490 <        checkCompletedWithWrappedCFException(g, ex);
2491 <        checkCompletedWithWrappedCFException(h, ex);
2488 >        // unspecified behavior - both source completions available
2489 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2490 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2491 >        try {
2492 >            assertNull(h4.join());
2493 >            rs[4].assertValue(v1);
2494 >        } catch (CompletionException ok) {
2495 >            checkCompletedWithWrappedException(h4, ex);
2496 >            rs[4].assertNotInvoked();
2497          }
2271    }
2272
2273    public void testAcceptEither_exceptionalCompletion3() {
2274        for (ExecutionMode m : ExecutionMode.values())
2275        for (Integer v1 : new Integer[] { 1, null }) {
2276
2277        final CompletableFuture<Integer> f = new CompletableFuture<>();
2278        final CompletableFuture<Integer> g = new CompletableFuture<>();
2279        final IncAction r = new IncAction();
2280        final CFException ex = new CFException();
2281
2282        g.completeExceptionally(ex);
2283        f.complete(v1);
2284        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2285
2286        // unspecified behavior
2287        Integer v;
2498          try {
2499 <            assertEquals(h.join(), null);
2500 <            assertEquals(r.invocationCount, 1);
2291 <            assertEquals(inc(v1), r.value);
2499 >            assertNull(h5.join());
2500 >            rs[5].assertValue(v1);
2501          } catch (CompletionException ok) {
2502 <            checkCompletedWithWrappedCFException(h, ex);
2503 <            assertEquals(r.invocationCount, 0);
2502 >            checkCompletedWithWrappedException(h5, ex);
2503 >            rs[5].assertNotInvoked();
2504          }
2505  
2506 <        checkCompletedWithWrappedCFException(g, ex);
2507 <        checkCompletedNormally(f, v1);
2508 <        }
2509 <    }
2506 >        checkCompletedExceptionally(f, ex);
2507 >        checkCompletedNormally(g, v1);
2508 >        checkCompletedWithWrappedException(h0, ex);
2509 >        checkCompletedWithWrappedException(h1, ex);
2510 >        checkCompletedWithWrappedException(h2, ex);
2511 >        checkCompletedWithWrappedException(h3, ex);
2512 >        checkCompletedWithWrappedException(h4, ex);
2513 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2514 >    }}
2515  
2516 <    public void testAcceptEither_exceptionalCompletion4() {
2516 >    public void testAcceptEither_exceptionalCompletion2() {
2517          for (ExecutionMode m : ExecutionMode.values())
2518 <        for (Integer v1 : new Integer[] { 1, null }) {
2519 <
2518 >        for (boolean fFirst : new boolean[] { true, false })
2519 >        for (Integer v1 : new Integer[] { 1, null })
2520 >    {
2521          final CompletableFuture<Integer> f = new CompletableFuture<>();
2522          final CompletableFuture<Integer> g = new CompletableFuture<>();
2308        final IncAction r = new IncAction();
2523          final CFException ex = new CFException();
2524 +        final NoopConsumer[] rs = new NoopConsumer[6];
2525 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2526  
2527 <        f.completeExceptionally(ex);
2528 <        g.complete(v1);
2529 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2527 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2528 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2529 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2530 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2531 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2532 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2533  
2534 <        // unspecified behavior
2316 <        Integer v;
2534 >        // unspecified behavior - both source completions available
2535          try {
2536 <            assertEquals(h.join(), null);
2537 <            assertEquals(r.invocationCount, 1);
2320 <            assertEquals(inc(v1), r.value);
2536 >            assertEquals(null, h0.join());
2537 >            rs[0].assertValue(v1);
2538          } catch (CompletionException ok) {
2539 <            checkCompletedWithWrappedCFException(h, ex);
2540 <            assertEquals(r.invocationCount, 0);
2539 >            checkCompletedWithWrappedException(h0, ex);
2540 >            rs[0].assertNotInvoked();
2541          }
2542 <
2543 <        checkCompletedWithWrappedCFException(f, ex);
2544 <        checkCompletedNormally(g, v1);
2542 >        try {
2543 >            assertEquals(null, h1.join());
2544 >            rs[1].assertValue(v1);
2545 >        } catch (CompletionException ok) {
2546 >            checkCompletedWithWrappedException(h1, ex);
2547 >            rs[1].assertNotInvoked();
2548          }
2549 <    }
2550 <
2551 <    /**
2552 <     * acceptEither result completes exceptionally if action does
2553 <     */
2554 <    public void testAcceptEither_actionFailed1() {
2555 <        for (ExecutionMode m : ExecutionMode.values())
2556 <        for (Integer v1 : new Integer[] { 1, null })
2557 <        for (Integer v2 : new Integer[] { 2, null }) {
2558 <
2559 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2560 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2561 <        final FailingConsumer r = new FailingConsumer();
2342 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2343 <
2344 <        f.complete(v1);
2345 <        checkCompletedWithWrappedCFException(h);
2346 <        g.complete(v2);
2347 <        checkCompletedNormally(f, v1);
2348 <        checkCompletedNormally(g, v2);
2549 >        try {
2550 >            assertEquals(null, h2.join());
2551 >            rs[2].assertValue(v1);
2552 >        } catch (CompletionException ok) {
2553 >            checkCompletedWithWrappedException(h2, ex);
2554 >            rs[2].assertNotInvoked();
2555 >        }
2556 >        try {
2557 >            assertEquals(null, h3.join());
2558 >            rs[3].assertValue(v1);
2559 >        } catch (CompletionException ok) {
2560 >            checkCompletedWithWrappedException(h3, ex);
2561 >            rs[3].assertNotInvoked();
2562          }
2350    }
2351
2352    public void testAcceptEither_actionFailed2() {
2353        for (ExecutionMode m : ExecutionMode.values())
2354        for (Integer v1 : new Integer[] { 1, null })
2355        for (Integer v2 : new Integer[] { 2, null }) {
2356
2357        final CompletableFuture<Integer> f = new CompletableFuture<>();
2358        final CompletableFuture<Integer> g = new CompletableFuture<>();
2359        final FailingConsumer r = new FailingConsumer();
2360        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2563  
2362        g.complete(v2);
2363        checkCompletedWithWrappedCFException(h);
2364        f.complete(v1);
2564          checkCompletedNormally(f, v1);
2565 <        checkCompletedNormally(g, v2);
2566 <        }
2368 <    }
2565 >        checkCompletedExceptionally(g, ex);
2566 >    }}
2567  
2568      /**
2569       * acceptEither result completes exceptionally if either source cancelled
2570       */
2571 <    public void testAcceptEither_sourceCancelled1() {
2374 <        for (ExecutionMode m : ExecutionMode.values())
2375 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2376 <        for (Integer v1 : new Integer[] { 1, 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);
2382 <
2383 <        assertTrue(f.cancel(mayInterruptIfRunning));
2384 <        checkCompletedWithWrappedCancellationException(h);
2385 <        g.complete(v1);
2386 <
2387 <        checkCancelled(f);
2388 <        assertEquals(r.invocationCount, 0);
2389 <        checkCompletedNormally(g, v1);
2390 <        checkCompletedWithWrappedCancellationException(h);
2391 <        }
2392 <    }
2393 <
2394 <    public void testAcceptEither_sourceCancelled2() {
2571 >    public void testAcceptEither_sourceCancelled() {
2572          for (ExecutionMode m : ExecutionMode.values())
2573          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2574 <        for (Integer v1 : new Integer[] { 1, null }) {
2575 <
2574 >        for (Integer v1 : new Integer[] { 1, null })
2575 >    {
2576          final CompletableFuture<Integer> f = new CompletableFuture<>();
2577          final CompletableFuture<Integer> g = new CompletableFuture<>();
2578 <        final IncAction r = new IncAction();
2579 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2403 <
2404 <        assertTrue(g.cancel(mayInterruptIfRunning));
2405 <        checkCompletedWithWrappedCancellationException(h);
2406 <        f.complete(v1);
2578 >        final NoopConsumer[] rs = new NoopConsumer[6];
2579 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2580  
2581 <        checkCancelled(g);
2582 <        assertEquals(r.invocationCount, 0);
2583 <        checkCompletedNormally(f, v1);
2584 <        checkCompletedWithWrappedCancellationException(h);
2585 <        }
2586 <    }
2587 <
2588 <    public void testAcceptEither_sourceCancelled3() {
2589 <        for (ExecutionMode m : ExecutionMode.values())
2590 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2591 <        for (Integer v1 : new Integer[] { 1, null }) {
2581 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2582 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2583 >        checkIncomplete(h0);
2584 >        checkIncomplete(h1);
2585 >        rs[0].assertNotInvoked();
2586 >        rs[1].assertNotInvoked();
2587 >        f.cancel(mayInterruptIfRunning);
2588 >        checkCompletedWithWrappedCancellationException(h0);
2589 >        checkCompletedWithWrappedCancellationException(h1);
2590 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2591 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2592 >        checkCompletedWithWrappedCancellationException(h2);
2593 >        checkCompletedWithWrappedCancellationException(h3);
2594  
2595 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2421 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2422 <        final IncAction r = new IncAction();
2423 <
2424 <        assertTrue(g.cancel(mayInterruptIfRunning));
2425 <        f.complete(v1);
2426 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2595 >        g.complete(v1);
2596  
2597 <        // unspecified behavior
2598 <        Integer v;
2597 >        // unspecified behavior - both source completions available
2598 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2599 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2600          try {
2601 <            assertEquals(h.join(), null);
2602 <            assertEquals(r.invocationCount, 1);
2433 <            assertEquals(inc(v1), r.value);
2601 >            assertNull(h4.join());
2602 >            rs[4].assertValue(v1);
2603          } catch (CompletionException ok) {
2604 <            checkCompletedWithWrappedCancellationException(h);
2605 <            assertEquals(r.invocationCount, 0);
2437 <        }
2438 <
2439 <        checkCancelled(g);
2440 <        checkCompletedNormally(f, v1);
2604 >            checkCompletedWithWrappedCancellationException(h4);
2605 >            rs[4].assertNotInvoked();
2606          }
2442    }
2443
2444    public void testAcceptEither_sourceCancelled4() {
2445        for (ExecutionMode m : ExecutionMode.values())
2446        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2447        for (Integer v1 : new Integer[] { 1, null }) {
2448
2449        final CompletableFuture<Integer> f = new CompletableFuture<>();
2450        final CompletableFuture<Integer> g = new CompletableFuture<>();
2451        final IncAction r = new IncAction();
2452
2453        assertTrue(f.cancel(mayInterruptIfRunning));
2454        g.complete(v1);
2455        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2456
2457        // unspecified behavior
2458        Integer v;
2607          try {
2608 <            assertEquals(h.join(), null);
2609 <            assertEquals(r.invocationCount, 1);
2462 <            assertEquals(inc(v1), r.value);
2608 >            assertNull(h5.join());
2609 >            rs[5].assertValue(v1);
2610          } catch (CompletionException ok) {
2611 <            checkCompletedWithWrappedCancellationException(h);
2612 <            assertEquals(r.invocationCount, 0);
2611 >            checkCompletedWithWrappedCancellationException(h5);
2612 >            rs[5].assertNotInvoked();
2613          }
2614  
2615          checkCancelled(f);
2616          checkCompletedNormally(g, v1);
2617 <        }
2618 <    }
2617 >        checkCompletedWithWrappedCancellationException(h0);
2618 >        checkCompletedWithWrappedCancellationException(h1);
2619 >        checkCompletedWithWrappedCancellationException(h2);
2620 >        checkCompletedWithWrappedCancellationException(h3);
2621 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2622 >    }}
2623  
2624      /**
2625 <     * runAfterEither result completes normally after normal completion
2475 <     * of either source
2625 >     * acceptEither result completes exceptionally if action does
2626       */
2627 <    public void testRunAfterEither_normalCompletion1() {
2627 >    public void testAcceptEither_actionFailed() {
2628          for (ExecutionMode m : ExecutionMode.values())
2629          for (Integer v1 : new Integer[] { 1, null })
2630 <        for (Integer v2 : new Integer[] { 2, null }) {
2631 <
2630 >        for (Integer v2 : new Integer[] { 2, null })
2631 >    {
2632          final CompletableFuture<Integer> f = new CompletableFuture<>();
2633          final CompletableFuture<Integer> g = new CompletableFuture<>();
2634 <        final Noop r = new Noop();
2635 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2634 >        final FailingConsumer[] rs = new FailingConsumer[6];
2635 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2636  
2637 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2638 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2639          f.complete(v1);
2640 <        checkCompletedNormally(h, null);
2641 <        assertEquals(r.invocationCount, 1);
2642 <        g.complete(v2);
2640 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2641 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2642 >        checkCompletedWithWrappedCFException(h0);
2643 >        checkCompletedWithWrappedCFException(h1);
2644 >        checkCompletedWithWrappedCFException(h2);
2645 >        checkCompletedWithWrappedCFException(h3);
2646 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2647  
2648 <        checkCompletedNormally(f, v1);
2493 <        checkCompletedNormally(g, v2);
2494 <        checkCompletedNormally(h, null);
2495 <        assertEquals(r.invocationCount, 1);
2496 <        }
2497 <    }
2498 <
2499 <    public void testRunAfterEither_normalCompletion2() {
2500 <        for (ExecutionMode m : ExecutionMode.values())
2501 <        for (Integer v1 : new Integer[] { 1, null })
2502 <        for (Integer v2 : new Integer[] { 2, null }) {
2648 >        g.complete(v2);
2649  
2650 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2651 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2652 <        final Noop r = new Noop();
2507 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2650 >        // unspecified behavior - both source completions available
2651 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2652 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2653  
2654 <        g.complete(v2);
2655 <        checkCompletedNormally(h, null);
2656 <        assertEquals(r.invocationCount, 1);
2657 <        f.complete(v1);
2654 >        checkCompletedWithWrappedCFException(h4);
2655 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2656 >                   Objects.equals(v2, rs[4].value));
2657 >        checkCompletedWithWrappedCFException(h5);
2658 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2659 >                   Objects.equals(v2, rs[5].value));
2660  
2661          checkCompletedNormally(f, v1);
2662          checkCompletedNormally(g, v2);
2663 <        checkCompletedNormally(h, null);
2664 <        assertEquals(r.invocationCount, 1);
2665 <        }
2666 <    }
2667 <    public void testRunAfterEither_normalCompletion3() {
2663 >    }}
2664 >
2665 >    /**
2666 >     * runAfterEither result completes normally after normal completion
2667 >     * of either source
2668 >     */
2669 >    public void testRunAfterEither_normalCompletion() {
2670          for (ExecutionMode m : ExecutionMode.values())
2671          for (Integer v1 : new Integer[] { 1, null })
2672 <        for (Integer v2 : new Integer[] { 2, null }) {
2673 <
2672 >        for (Integer v2 : new Integer[] { 2, null })
2673 >    {
2674          final CompletableFuture<Integer> f = new CompletableFuture<>();
2675          final CompletableFuture<Integer> g = new CompletableFuture<>();
2676 <        final Noop r = new Noop();
2676 >        final Noop[] rs = new Noop[6];
2677 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2678  
2679 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2680 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2681 +        checkIncomplete(h0);
2682 +        checkIncomplete(h1);
2683 +        rs[0].assertNotInvoked();
2684 +        rs[1].assertNotInvoked();
2685          f.complete(v1);
2686 +        checkCompletedNormally(h0, null);
2687 +        checkCompletedNormally(h1, null);
2688 +        rs[0].assertInvoked();
2689 +        rs[1].assertInvoked();
2690 +        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2691 +        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2692 +        checkCompletedNormally(h2, null);
2693 +        checkCompletedNormally(h3, null);
2694 +        rs[2].assertInvoked();
2695 +        rs[3].assertInvoked();
2696 +
2697          g.complete(v2);
2531        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2698  
2699 <        checkCompletedNormally(h, null);
2699 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2700 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2701 >
2702          checkCompletedNormally(f, v1);
2703          checkCompletedNormally(g, v2);
2704 <        assertEquals(r.invocationCount, 1);
2705 <        }
2706 <    }
2704 >        checkCompletedNormally(h0, null);
2705 >        checkCompletedNormally(h1, null);
2706 >        checkCompletedNormally(h2, null);
2707 >        checkCompletedNormally(h3, null);
2708 >        checkCompletedNormally(h4, null);
2709 >        checkCompletedNormally(h5, null);
2710 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2711 >    }}
2712  
2713      /**
2714       * runAfterEither result completes exceptionally after exceptional
2715       * completion of either source
2716       */
2717 <    public void testRunAfterEither_exceptionalCompletion1() {
2717 >    public void testRunAfterEither_exceptionalCompletion() {
2718          for (ExecutionMode m : ExecutionMode.values())
2719 <        for (Integer v1 : new Integer[] { 1, null }) {
2720 <
2719 >        for (Integer v1 : new Integer[] { 1, null })
2720 >    {
2721          final CompletableFuture<Integer> f = new CompletableFuture<>();
2722          final CompletableFuture<Integer> g = new CompletableFuture<>();
2550        final Noop r = new Noop();
2551        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2723          final CFException ex = new CFException();
2724 +        final Noop[] rs = new Noop[6];
2725 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2726  
2727 <        f.completeExceptionally(ex);
2728 <        checkCompletedWithWrappedCFException(h, ex);
2729 <        g.complete(v1);
2730 <
2731 <        assertEquals(r.invocationCount, 0);
2732 <        checkCompletedNormally(g, v1);
2733 <        checkCompletedWithWrappedCFException(f, ex);
2734 <        checkCompletedWithWrappedCFException(h, ex);
2735 <        }
2736 <    }
2737 <
2738 <    public void testRunAfterEither_exceptionalCompletion2() {
2739 <        for (ExecutionMode m : ExecutionMode.values())
2567 <        for (Integer v1 : new Integer[] { 1, null }) {
2727 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2728 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2729 >        checkIncomplete(h0);
2730 >        checkIncomplete(h1);
2731 >        rs[0].assertNotInvoked();
2732 >        rs[1].assertNotInvoked();
2733 >        assertTrue(f.completeExceptionally(ex));
2734 >        checkCompletedWithWrappedException(h0, ex);
2735 >        checkCompletedWithWrappedException(h1, ex);
2736 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2737 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2738 >        checkCompletedWithWrappedException(h2, ex);
2739 >        checkCompletedWithWrappedException(h3, ex);
2740  
2741 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2570 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2571 <        final Noop r = new Noop();
2572 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2573 <        final CFException ex = new CFException();
2574 <
2575 <        g.completeExceptionally(ex);
2576 <        checkCompletedWithWrappedCFException(h, ex);
2577 <        f.complete(v1);
2741 >        assertTrue(g.complete(v1));
2742  
2743 <        assertEquals(r.invocationCount, 0);
2744 <        checkCompletedNormally(f, v1);
2745 <        checkCompletedWithWrappedCFException(g, ex);
2746 <        checkCompletedWithWrappedCFException(h, ex);
2743 >        // unspecified behavior - both source completions available
2744 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2745 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2746 >        try {
2747 >            assertNull(h4.join());
2748 >            rs[4].assertInvoked();
2749 >        } catch (CompletionException ok) {
2750 >            checkCompletedWithWrappedException(h4, ex);
2751 >            rs[4].assertNotInvoked();
2752          }
2584    }
2585
2586    public void testRunAfterEither_exceptionalCompletion3() {
2587        for (ExecutionMode m : ExecutionMode.values())
2588        for (Integer v1 : new Integer[] { 1, null }) {
2589
2590        final CompletableFuture<Integer> f = new CompletableFuture<>();
2591        final CompletableFuture<Integer> g = new CompletableFuture<>();
2592        final Noop r = new Noop();
2593        final CFException ex = new CFException();
2594
2595        g.completeExceptionally(ex);
2596        f.complete(v1);
2597        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2598
2599        // unspecified behavior
2600        Integer v;
2753          try {
2754 <            assertEquals(h.join(), null);
2755 <            assertEquals(r.invocationCount, 1);
2754 >            assertNull(h5.join());
2755 >            rs[5].assertInvoked();
2756          } catch (CompletionException ok) {
2757 <            checkCompletedWithWrappedCFException(h, ex);
2758 <            assertEquals(r.invocationCount, 0);
2757 >            checkCompletedWithWrappedException(h5, ex);
2758 >            rs[5].assertNotInvoked();
2759          }
2760  
2761 <        checkCompletedWithWrappedCFException(g, ex);
2762 <        checkCompletedNormally(f, v1);
2763 <        }
2764 <    }
2761 >        checkCompletedExceptionally(f, ex);
2762 >        checkCompletedNormally(g, v1);
2763 >        checkCompletedWithWrappedException(h0, ex);
2764 >        checkCompletedWithWrappedException(h1, ex);
2765 >        checkCompletedWithWrappedException(h2, ex);
2766 >        checkCompletedWithWrappedException(h3, ex);
2767 >        checkCompletedWithWrappedException(h4, ex);
2768 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2769 >    }}
2770  
2771 <    public void testRunAfterEither_exceptionalCompletion4() {
2771 >    public void testRunAfterEither_exceptionalCompletion2() {
2772          for (ExecutionMode m : ExecutionMode.values())
2773 <        for (Integer v1 : new Integer[] { 1, null }) {
2774 <
2773 >        for (boolean fFirst : new boolean[] { true, false })
2774 >        for (Integer v1 : new Integer[] { 1, null })
2775 >    {
2776          final CompletableFuture<Integer> f = new CompletableFuture<>();
2777          final CompletableFuture<Integer> g = new CompletableFuture<>();
2620        final Noop r = new Noop();
2778          final CFException ex = new CFException();
2779 +        final Noop[] rs = new Noop[6];
2780 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2781  
2782 <        f.completeExceptionally(ex);
2783 <        g.complete(v1);
2784 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2782 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2783 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2784 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2785 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2786 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2787 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2788  
2789 <        // unspecified behavior
2628 <        Integer v;
2789 >        // unspecified behavior - both source completions available
2790          try {
2791 <            assertEquals(h.join(), null);
2792 <            assertEquals(r.invocationCount, 1);
2791 >            assertEquals(null, h0.join());
2792 >            rs[0].assertInvoked();
2793          } catch (CompletionException ok) {
2794 <            checkCompletedWithWrappedCFException(h, ex);
2795 <            assertEquals(r.invocationCount, 0);
2794 >            checkCompletedWithWrappedException(h0, ex);
2795 >            rs[0].assertNotInvoked();
2796          }
2797 <
2798 <        checkCompletedWithWrappedCFException(f, ex);
2799 <        checkCompletedNormally(g, v1);
2797 >        try {
2798 >            assertEquals(null, h1.join());
2799 >            rs[1].assertInvoked();
2800 >        } catch (CompletionException ok) {
2801 >            checkCompletedWithWrappedException(h1, ex);
2802 >            rs[1].assertNotInvoked();
2803          }
2804 <    }
2805 <
2806 <    /**
2807 <     * runAfterEither result completes exceptionally if action does
2808 <     */
2809 <    public void testRunAfterEither_actionFailed1() {
2810 <        for (ExecutionMode m : ExecutionMode.values())
2811 <        for (Integer v1 : new Integer[] { 1, null })
2812 <        for (Integer v2 : new Integer[] { 2, null }) {
2813 <
2814 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2815 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2816 <        final FailingNoop r = new FailingNoop();
2653 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2654 <
2655 <        f.complete(v1);
2656 <        checkCompletedWithWrappedCFException(h);
2657 <        g.complete(v2);
2658 <        checkCompletedNormally(f, v1);
2659 <        checkCompletedNormally(g, v2);
2804 >        try {
2805 >            assertEquals(null, h2.join());
2806 >            rs[2].assertInvoked();
2807 >        } catch (CompletionException ok) {
2808 >            checkCompletedWithWrappedException(h2, ex);
2809 >            rs[2].assertNotInvoked();
2810 >        }
2811 >        try {
2812 >            assertEquals(null, h3.join());
2813 >            rs[3].assertInvoked();
2814 >        } catch (CompletionException ok) {
2815 >            checkCompletedWithWrappedException(h3, ex);
2816 >            rs[3].assertNotInvoked();
2817          }
2661    }
2662
2663    public void testRunAfterEither_actionFailed2() {
2664        for (ExecutionMode m : ExecutionMode.values())
2665        for (Integer v1 : new Integer[] { 1, null })
2666        for (Integer v2 : new Integer[] { 2, null }) {
2667
2668        final CompletableFuture<Integer> f = new CompletableFuture<>();
2669        final CompletableFuture<Integer> g = new CompletableFuture<>();
2670        final FailingNoop r = new FailingNoop();
2671        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2818  
2673        g.complete(v2);
2674        checkCompletedWithWrappedCFException(h);
2675        f.complete(v1);
2819          checkCompletedNormally(f, v1);
2820 <        checkCompletedNormally(g, v2);
2821 <        }
2679 <    }
2820 >        checkCompletedExceptionally(g, ex);
2821 >    }}
2822  
2823      /**
2824       * runAfterEither result completes exceptionally if either source cancelled
2825       */
2826 <    public void testRunAfterEither_sourceCancelled1() {
2826 >    public void testRunAfterEither_sourceCancelled() {
2827          for (ExecutionMode m : ExecutionMode.values())
2828          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2829 <        for (Integer v1 : new Integer[] { 1, null }) {
2830 <
2829 >        for (Integer v1 : new Integer[] { 1, null })
2830 >    {
2831          final CompletableFuture<Integer> f = new CompletableFuture<>();
2832          final CompletableFuture<Integer> g = new CompletableFuture<>();
2833 <        final Noop r = new Noop();
2834 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2833 >        final Noop[] rs = new Noop[6];
2834 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2835  
2836 <        assertTrue(f.cancel(mayInterruptIfRunning));
2837 <        checkCompletedWithWrappedCancellationException(h);
2838 <        g.complete(v1);
2836 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2837 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2838 >        checkIncomplete(h0);
2839 >        checkIncomplete(h1);
2840 >        rs[0].assertNotInvoked();
2841 >        rs[1].assertNotInvoked();
2842 >        f.cancel(mayInterruptIfRunning);
2843 >        checkCompletedWithWrappedCancellationException(h0);
2844 >        checkCompletedWithWrappedCancellationException(h1);
2845 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2846 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2847 >        checkCompletedWithWrappedCancellationException(h2);
2848 >        checkCompletedWithWrappedCancellationException(h3);
2849  
2850 <        checkCancelled(f);
2699 <        assertEquals(r.invocationCount, 0);
2700 <        checkCompletedNormally(g, v1);
2701 <        checkCompletedWithWrappedCancellationException(h);
2702 <        }
2703 <    }
2850 >        assertTrue(g.complete(v1));
2851  
2852 <    public void testRunAfterEither_sourceCancelled2() {
2853 <        for (ExecutionMode m : ExecutionMode.values())
2854 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2855 <        for (Integer v1 : new Integer[] { 1, null }) {
2856 <
2857 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2858 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2859 <        final Noop r = new Noop();
2860 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2714 <
2715 <        assertTrue(g.cancel(mayInterruptIfRunning));
2716 <        checkCompletedWithWrappedCancellationException(h);
2717 <        f.complete(v1);
2718 <
2719 <        checkCancelled(g);
2720 <        assertEquals(r.invocationCount, 0);
2721 <        checkCompletedNormally(f, v1);
2722 <        checkCompletedWithWrappedCancellationException(h);
2852 >        // unspecified behavior - both source completions available
2853 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2854 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2855 >        try {
2856 >            assertNull(h4.join());
2857 >            rs[4].assertInvoked();
2858 >        } catch (CompletionException ok) {
2859 >            checkCompletedWithWrappedCancellationException(h4);
2860 >            rs[4].assertNotInvoked();
2861          }
2724    }
2725
2726    public void testRunAfterEither_sourceCancelled3() {
2727        for (ExecutionMode m : ExecutionMode.values())
2728        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2729        for (Integer v1 : new Integer[] { 1, null }) {
2730
2731        final CompletableFuture<Integer> f = new CompletableFuture<>();
2732        final CompletableFuture<Integer> g = new CompletableFuture<>();
2733        final Noop r = new Noop();
2734
2735        assertTrue(g.cancel(mayInterruptIfRunning));
2736        f.complete(v1);
2737        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2738
2739        // unspecified behavior
2740        Integer v;
2862          try {
2863 <            assertEquals(h.join(), null);
2864 <            assertEquals(r.invocationCount, 1);
2863 >            assertNull(h5.join());
2864 >            rs[5].assertInvoked();
2865          } catch (CompletionException ok) {
2866 <            checkCompletedWithWrappedCancellationException(h);
2867 <            assertEquals(r.invocationCount, 0);
2866 >            checkCompletedWithWrappedCancellationException(h5);
2867 >            rs[5].assertNotInvoked();
2868          }
2869  
2870 <        checkCancelled(g);
2871 <        checkCompletedNormally(f, v1);
2872 <        }
2873 <    }
2870 >        checkCancelled(f);
2871 >        checkCompletedNormally(g, v1);
2872 >        checkCompletedWithWrappedCancellationException(h0);
2873 >        checkCompletedWithWrappedCancellationException(h1);
2874 >        checkCompletedWithWrappedCancellationException(h2);
2875 >        checkCompletedWithWrappedCancellationException(h3);
2876 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2877 >    }}
2878  
2879 <    public void testRunAfterEither_sourceCancelled4() {
2879 >    /**
2880 >     * runAfterEither result completes exceptionally if action does
2881 >     */
2882 >    public void testRunAfterEither_actionFailed() {
2883          for (ExecutionMode m : ExecutionMode.values())
2884 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2885 <        for (Integer v1 : new Integer[] { 1, null }) {
2886 <
2884 >        for (Integer v1 : new Integer[] { 1, null })
2885 >        for (Integer v2 : new Integer[] { 2, null })
2886 >    {
2887          final CompletableFuture<Integer> f = new CompletableFuture<>();
2888          final CompletableFuture<Integer> g = new CompletableFuture<>();
2889 <        final Noop r = new Noop();
2889 >        final FailingRunnable[] rs = new FailingRunnable[6];
2890 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2891  
2892 <        assertTrue(f.cancel(mayInterruptIfRunning));
2893 <        g.complete(v1);
2894 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2892 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2893 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2894 >        assertTrue(f.complete(v1));
2895 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2896 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2897 >        checkCompletedWithWrappedCFException(h0);
2898 >        checkCompletedWithWrappedCFException(h1);
2899 >        checkCompletedWithWrappedCFException(h2);
2900 >        checkCompletedWithWrappedCFException(h3);
2901 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2902 >        assertTrue(g.complete(v2));
2903 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2904 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2905 >        checkCompletedWithWrappedCFException(h4);
2906 >        checkCompletedWithWrappedCFException(h5);
2907  
2908 <        // unspecified behavior
2909 <        Integer v;
2910 <        try {
2911 <            assertEquals(h.join(), null);
2771 <            assertEquals(r.invocationCount, 1);
2772 <        } catch (CompletionException ok) {
2773 <            checkCompletedWithWrappedCancellationException(h);
2774 <            assertEquals(r.invocationCount, 0);
2775 <        }
2776 <
2777 <        checkCancelled(f);
2778 <        checkCompletedNormally(g, v1);
2779 <        }
2780 <    }
2908 >        checkCompletedNormally(f, v1);
2909 >        checkCompletedNormally(g, v2);
2910 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2911 >    }}
2912  
2913      /**
2914       * thenCompose result completes normally after normal completion of source
2915       */
2916 <    public void testThenCompose() {
2917 <        CompletableFuture<Integer> f, g;
2918 <        CompletableFutureInc r;
2919 <
2920 <        f = new CompletableFuture<>();
2921 <        g = f.thenCompose(r = new CompletableFutureInc());
2922 <        f.complete(one);
2923 <        checkCompletedNormally(g, two);
2924 <        assertTrue(r.ran);
2916 >    public void testThenCompose_normalCompletion() {
2917 >        for (ExecutionMode m : ExecutionMode.values())
2918 >        for (boolean createIncomplete : new boolean[] { true, false })
2919 >        for (Integer v1 : new Integer[] { 1, null })
2920 >    {
2921 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2922 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2923 >        if (!createIncomplete) assertTrue(f.complete(v1));
2924 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2925 >        if (createIncomplete) assertTrue(f.complete(v1));
2926  
2927 <        f = new CompletableFuture<>();
2928 <        f.complete(one);
2929 <        g = f.thenCompose(r = new CompletableFutureInc());
2930 <        checkCompletedNormally(g, two);
2799 <        assertTrue(r.ran);
2800 <    }
2927 >        checkCompletedNormally(g, inc(v1));
2928 >        checkCompletedNormally(f, v1);
2929 >        r.assertValue(v1);
2930 >    }}
2931  
2932      /**
2933       * thenCompose result completes exceptionally after exceptional
2934       * completion of source
2935       */
2936 <    public void testThenCompose2() {
2937 <        CompletableFuture<Integer> f, g;
2938 <        CompletableFutureInc r;
2939 <
2940 <        f = new CompletableFuture<>();
2941 <        g = f.thenCompose(r = new CompletableFutureInc());
2942 <        f.completeExceptionally(new CFException());
2943 <        checkCompletedWithWrappedCFException(g);
2936 >    public void testThenCompose_exceptionalCompletion() {
2937 >        for (ExecutionMode m : ExecutionMode.values())
2938 >        for (boolean createIncomplete : new boolean[] { true, false })
2939 >    {
2940 >        final CFException ex = new CFException();
2941 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2942 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2943 >        if (!createIncomplete) f.completeExceptionally(ex);
2944 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2945 >        if (createIncomplete) f.completeExceptionally(ex);
2946  
2947 <        f = new CompletableFuture<>();
2948 <        f.completeExceptionally(new CFException());
2949 <        g = f.thenCompose(r = new CompletableFutureInc());
2950 <        checkCompletedWithWrappedCFException(g);
2819 <    }
2947 >        checkCompletedWithWrappedException(g, ex);
2948 >        checkCompletedExceptionally(f, ex);
2949 >        r.assertNotInvoked();
2950 >    }}
2951  
2952      /**
2953       * thenCompose result completes exceptionally if action does
2954       */
2955 <    public void testThenCompose3() {
2956 <        CompletableFuture<Integer> f, g;
2957 <        FailingCompletableFutureFunction r;
2958 <
2959 <        f = new CompletableFuture<>();
2960 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2961 <        f.complete(one);
2962 <        checkCompletedWithWrappedCFException(g);
2955 >    public void testThenCompose_actionFailed() {
2956 >        for (ExecutionMode m : ExecutionMode.values())
2957 >        for (boolean createIncomplete : new boolean[] { true, false })
2958 >        for (Integer v1 : new Integer[] { 1, null })
2959 >    {
2960 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2961 >        final FailingCompletableFutureFunction r
2962 >            = new FailingCompletableFutureFunction(m);
2963 >        if (!createIncomplete) assertTrue(f.complete(v1));
2964 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2965 >        if (createIncomplete) assertTrue(f.complete(v1));
2966  
2833        f = new CompletableFuture<>();
2834        f.complete(one);
2835        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2967          checkCompletedWithWrappedCFException(g);
2968 <    }
2968 >        checkCompletedNormally(f, v1);
2969 >    }}
2970  
2971      /**
2972       * thenCompose result completes exceptionally if source cancelled
2973       */
2974 <    public void testThenCompose4() {
2975 <        CompletableFuture<Integer> f, g;
2976 <        CompletableFutureInc r;
2977 <
2978 <        f = new CompletableFuture<>();
2979 <        g = f.thenCompose(r = new CompletableFutureInc());
2980 <        assertTrue(f.cancel(true));
2981 <        checkCompletedWithWrappedCancellationException(g);
2982 <
2983 <        f = new CompletableFuture<>();
2984 <        assertTrue(f.cancel(true));
2985 <        g = f.thenCompose(r = new CompletableFutureInc());
2986 <        checkCompletedWithWrappedCancellationException(g);
2855 <    }
2856 <
2857 <    // asyncs
2858 <
2859 <    /**
2860 <     * thenRunAsync result completes normally after normal completion of source
2861 <     */
2862 <    public void testThenRunAsync() {
2863 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2864 <        Noop r = new Noop();
2865 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2866 <        f.complete(null);
2867 <        checkCompletedNormally(g, null);
2868 <
2869 <        // reordered version
2870 <        f = new CompletableFuture<>();
2871 <        f.complete(null);
2872 <        r = new Noop();
2873 <        g = f.thenRunAsync(r);
2874 <        checkCompletedNormally(g, null);
2875 <    }
2876 <
2877 <    /**
2878 <     * thenRunAsync result completes exceptionally after exceptional
2879 <     * completion of source
2880 <     */
2881 <    public void testThenRunAsync2() {
2882 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2883 <        Noop r = new Noop();
2884 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2885 <        f.completeExceptionally(new CFException());
2886 <        try {
2887 <            g.join();
2888 <            shouldThrow();
2889 <        } catch (CompletionException success) {}
2890 <        checkCompletedWithWrappedCFException(g);
2891 <    }
2892 <
2893 <    /**
2894 <     * thenRunAsync result completes exceptionally if action does
2895 <     */
2896 <    public void testThenRunAsync3() {
2897 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2898 <        FailingNoop r = new FailingNoop();
2899 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2900 <        f.complete(null);
2901 <        checkCompletedWithWrappedCFException(g);
2902 <    }
2903 <
2904 <    /**
2905 <     * thenRunAsync result completes exceptionally if source cancelled
2906 <     */
2907 <    public void testThenRunAsync4() {
2908 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2909 <        Noop r = new Noop();
2910 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2911 <        assertTrue(f.cancel(true));
2912 <        checkCompletedWithWrappedCancellationException(g);
2913 <    }
2914 <
2915 <    /**
2916 <     * thenApplyAsync result completes normally after normal completion of source
2917 <     */
2918 <    public void testThenApplyAsync() {
2919 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2920 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2921 <        f.complete(one);
2922 <        checkCompletedNormally(g, two);
2923 <    }
2924 <
2925 <    /**
2926 <     * thenApplyAsync result completes exceptionally after exceptional
2927 <     * completion of source
2928 <     */
2929 <    public void testThenApplyAsync2() {
2930 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2931 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2932 <        f.completeExceptionally(new CFException());
2933 <        checkCompletedWithWrappedCFException(g);
2934 <    }
2935 <
2936 <    /**
2937 <     * thenApplyAsync result completes exceptionally if action does
2938 <     */
2939 <    public void testThenApplyAsync3() {
2940 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2941 <        FailingFunction r = new FailingFunction();
2942 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
2943 <        f.complete(null);
2944 <        checkCompletedWithWrappedCFException(g);
2945 <    }
2946 <
2947 <    /**
2948 <     * thenApplyAsync result completes exceptionally if source cancelled
2949 <     */
2950 <    public void testThenApplyAsync4() {
2951 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2952 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2953 <        assertTrue(f.cancel(true));
2954 <        checkCompletedWithWrappedCancellationException(g);
2955 <    }
2956 <
2957 <    /**
2958 <     * thenAcceptAsync result completes normally after normal
2959 <     * completion of source
2960 <     */
2961 <    public void testThenAcceptAsync() {
2962 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2963 <        IncAction r = new IncAction();
2964 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2965 <        f.complete(one);
2966 <        checkCompletedNormally(g, null);
2967 <        assertEquals(r.value, (Integer) 2);
2968 <    }
2969 <
2970 <    /**
2971 <     * thenAcceptAsync result completes exceptionally after exceptional
2972 <     * completion of source
2973 <     */
2974 <    public void testThenAcceptAsync2() {
2975 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2976 <        IncAction r = new IncAction();
2977 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2978 <        f.completeExceptionally(new CFException());
2979 <        checkCompletedWithWrappedCFException(g);
2980 <    }
2981 <
2982 <    /**
2983 <     * thenAcceptAsync result completes exceptionally if action does
2984 <     */
2985 <    public void testThenAcceptAsync3() {
2986 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2987 <        FailingConsumer r = new FailingConsumer();
2988 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2989 <        f.complete(null);
2990 <        checkCompletedWithWrappedCFException(g);
2991 <    }
2992 <
2993 <    /**
2994 <     * thenAcceptAsync result completes exceptionally if source cancelled
2995 <     */
2996 <    public void testThenAcceptAsync4() {
2997 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2998 <        IncAction r = new IncAction();
2999 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3000 <        assertTrue(f.cancel(true));
3001 <        checkCompletedWithWrappedCancellationException(g);
3002 <    }
3003 <
3004 <    /**
3005 <     * thenComposeAsync result completes normally after normal
3006 <     * completion of source
3007 <     */
3008 <    public void testThenComposeAsync() {
3009 <        CompletableFuture<Integer> f, g;
3010 <        CompletableFutureInc r;
3011 <
3012 <        f = new CompletableFuture<>();
3013 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3014 <        f.complete(one);
3015 <        checkCompletedNormally(g, two);
3016 <
3017 <        f = new CompletableFuture<>();
3018 <        f.complete(one);
3019 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3020 <        checkCompletedNormally(g, two);
3021 <    }
3022 <
3023 <    /**
3024 <     * thenComposeAsync result completes exceptionally after
3025 <     * exceptional completion of source
3026 <     */
3027 <    public void testThenComposeAsync2() {
3028 <        CompletableFuture<Integer> f, g;
3029 <        CompletableFutureInc r;
3030 <
3031 <        f = new CompletableFuture<>();
3032 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3033 <        f.completeExceptionally(new CFException());
3034 <        checkCompletedWithWrappedCFException(g);
3035 <        assertFalse(r.ran);
3036 <
3037 <        f = new CompletableFuture<>();
3038 <        f.completeExceptionally(new CFException());
3039 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3040 <        checkCompletedWithWrappedCFException(g);
3041 <        assertFalse(r.ran);
3042 <    }
3043 <
3044 <    /**
3045 <     * thenComposeAsync result completes exceptionally if action does
3046 <     */
3047 <    public void testThenComposeAsync3() {
3048 <        CompletableFuture<Integer> f, g;
3049 <        FailingCompletableFutureFunction r;
3050 <
3051 <        f = new CompletableFuture<>();
3052 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
3053 <        f.complete(one);
3054 <        checkCompletedWithWrappedCFException(g);
3055 <
3056 <        f = new CompletableFuture<>();
3057 <        f.complete(one);
3058 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
3059 <        checkCompletedWithWrappedCFException(g);
3060 <    }
3061 <
3062 <    /**
3063 <     * thenComposeAsync result completes exceptionally if source cancelled
3064 <     */
3065 <    public void testThenComposeAsync4() {
3066 <        CompletableFuture<Integer> f, g;
3067 <        CompletableFutureInc r;
3068 <
3069 <        f = new CompletableFuture<>();
3070 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3071 <        assertTrue(f.cancel(true));
3072 <        checkCompletedWithWrappedCancellationException(g);
3073 <
3074 <        f = new CompletableFuture<>();
3075 <        assertTrue(f.cancel(true));
3076 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3077 <        checkCompletedWithWrappedCancellationException(g);
3078 <    }
3079 <
3080 <    // async with explicit executors
3081 <
3082 <    /**
3083 <     * thenRunAsync result completes normally after normal completion of source
3084 <     */
3085 <    public void testThenRunAsyncE() {
3086 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3087 <        Noop r = new Noop();
3088 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3089 <        f.complete(null);
3090 <        checkCompletedNormally(g, null);
3091 <
3092 <        // reordered version
3093 <        f = new CompletableFuture<>();
3094 <        f.complete(null);
3095 <        r = new Noop();
3096 <        g = f.thenRunAsync(r, new ThreadExecutor());
3097 <        checkCompletedNormally(g, null);
3098 <    }
3099 <
3100 <    /**
3101 <     * thenRunAsync result completes exceptionally after exceptional
3102 <     * completion of source
3103 <     */
3104 <    public void testThenRunAsync2E() {
3105 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3106 <        Noop r = new Noop();
3107 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3108 <        f.completeExceptionally(new CFException());
3109 <        try {
3110 <            g.join();
3111 <            shouldThrow();
3112 <        } catch (CompletionException success) {}
3113 <        checkCompletedWithWrappedCFException(g);
3114 <    }
3115 <
3116 <    /**
3117 <     * thenRunAsync result completes exceptionally if action does
3118 <     */
3119 <    public void testThenRunAsync3E() {
3120 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3121 <        FailingNoop r = new FailingNoop();
3122 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3123 <        f.complete(null);
3124 <        checkCompletedWithWrappedCFException(g);
3125 <    }
3126 <
3127 <    /**
3128 <     * thenRunAsync result completes exceptionally if source cancelled
3129 <     */
3130 <    public void testThenRunAsync4E() {
3131 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3132 <        Noop r = new Noop();
3133 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3134 <        assertTrue(f.cancel(true));
3135 <        checkCompletedWithWrappedCancellationException(g);
3136 <    }
3137 <
3138 <    /**
3139 <     * thenApplyAsync result completes normally after normal completion of source
3140 <     */
3141 <    public void testThenApplyAsyncE() {
3142 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3143 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3144 <        f.complete(one);
3145 <        checkCompletedNormally(g, two);
3146 <    }
3147 <
3148 <    /**
3149 <     * thenApplyAsync result completes exceptionally after exceptional
3150 <     * completion of source
3151 <     */
3152 <    public void testThenApplyAsync2E() {
3153 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3154 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3155 <        f.completeExceptionally(new CFException());
3156 <        checkCompletedWithWrappedCFException(g);
3157 <    }
3158 <
3159 <    /**
3160 <     * thenApplyAsync result completes exceptionally if action does
3161 <     */
3162 <    public void testThenApplyAsync3E() {
3163 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3164 <        FailingFunction r = new FailingFunction();
3165 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
3166 <        f.complete(null);
3167 <        checkCompletedWithWrappedCFException(g);
3168 <    }
3169 <
3170 <    /**
3171 <     * thenApplyAsync result completes exceptionally if source cancelled
3172 <     */
3173 <    public void testThenApplyAsync4E() {
3174 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3175 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3176 <        assertTrue(f.cancel(true));
3177 <        checkCompletedWithWrappedCancellationException(g);
3178 <    }
3179 <
3180 <    /**
3181 <     * thenAcceptAsync result completes normally after normal
3182 <     * completion of source
3183 <     */
3184 <    public void testThenAcceptAsyncE() {
3185 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3186 <        IncAction r = new IncAction();
3187 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3188 <        f.complete(one);
3189 <        checkCompletedNormally(g, null);
3190 <        assertEquals(r.value, (Integer) 2);
3191 <    }
3192 <
3193 <    /**
3194 <     * thenAcceptAsync result completes exceptionally after exceptional
3195 <     * completion of source
3196 <     */
3197 <    public void testThenAcceptAsync2E() {
3198 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3199 <        IncAction r = new IncAction();
3200 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3201 <        f.completeExceptionally(new CFException());
3202 <        checkCompletedWithWrappedCFException(g);
3203 <    }
3204 <
3205 <    /**
3206 <     * thenAcceptAsync result completes exceptionally if action does
3207 <     */
3208 <    public void testThenAcceptAsync3E() {
3209 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3210 <        FailingConsumer r = new FailingConsumer();
3211 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3212 <        f.complete(null);
3213 <        checkCompletedWithWrappedCFException(g);
3214 <    }
3215 <
3216 <    /**
3217 <     * thenAcceptAsync result completes exceptionally if source cancelled
3218 <     */
3219 <    public void testThenAcceptAsync4E() {
3220 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3221 <        IncAction r = new IncAction();
3222 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3223 <        assertTrue(f.cancel(true));
3224 <        checkCompletedWithWrappedCancellationException(g);
3225 <    }
3226 <
3227 <    /**
3228 <     * thenComposeAsync result completes normally after normal
3229 <     * completion of source
3230 <     */
3231 <    public void testThenComposeAsyncE() {
3232 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3233 <        CompletableFutureInc r = new CompletableFutureInc();
3234 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3235 <        f.complete(one);
3236 <        checkCompletedNormally(g, two);
3237 <    }
3238 <
3239 <    /**
3240 <     * thenComposeAsync result completes exceptionally after
3241 <     * exceptional completion of source
3242 <     */
3243 <    public void testThenComposeAsync2E() {
3244 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3245 <        CompletableFutureInc r = new CompletableFutureInc();
3246 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3247 <        f.completeExceptionally(new CFException());
3248 <        checkCompletedWithWrappedCFException(g);
3249 <    }
3250 <
3251 <    /**
3252 <     * thenComposeAsync result completes exceptionally if action does
3253 <     */
3254 <    public void testThenComposeAsync3E() {
3255 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3256 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
3257 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3258 <        f.complete(one);
3259 <        checkCompletedWithWrappedCFException(g);
3260 <    }
2974 >    public void testThenCompose_sourceCancelled() {
2975 >        for (ExecutionMode m : ExecutionMode.values())
2976 >        for (boolean createIncomplete : new boolean[] { true, false })
2977 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2978 >    {
2979 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2980 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2981 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2982 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2983 >        if (createIncomplete) {
2984 >            checkIncomplete(g);
2985 >            assertTrue(f.cancel(mayInterruptIfRunning));
2986 >        }
2987  
3262    /**
3263     * thenComposeAsync result completes exceptionally if source cancelled
3264     */
3265    public void testThenComposeAsync4E() {
3266        CompletableFuture<Integer> f = new CompletableFuture<>();
3267        CompletableFutureInc r = new CompletableFutureInc();
3268        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3269        assertTrue(f.cancel(true));
2988          checkCompletedWithWrappedCancellationException(g);
2989 <    }
2989 >        checkCancelled(f);
2990 >    }}
2991  
2992      // other static methods
2993  
# Line 3286 | Line 3005 | public class CompletableFutureTest exten
3005       * when all components complete normally
3006       */
3007      public void testAllOf_normal() throws Exception {
3008 <        for (int k = 1; k < 20; ++k) {
3009 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3010 <            for (int i = 0; i < k; ++i)
3008 >        for (int k = 1; k < 10; k++) {
3009 >            CompletableFuture<Integer>[] fs
3010 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3011 >            for (int i = 0; i < k; i++)
3012 >                fs[i] = new CompletableFuture<>();
3013 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3014 >            for (int i = 0; i < k; i++) {
3015 >                checkIncomplete(f);
3016 >                checkIncomplete(CompletableFuture.allOf(fs));
3017 >                fs[i].complete(one);
3018 >            }
3019 >            checkCompletedNormally(f, null);
3020 >            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3021 >        }
3022 >    }
3023 >
3024 >    public void testAllOf_backwards() throws Exception {
3025 >        for (int k = 1; k < 10; k++) {
3026 >            CompletableFuture<Integer>[] fs
3027 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3028 >            for (int i = 0; i < k; i++)
3029                  fs[i] = new CompletableFuture<>();
3030              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3031 <            for (int i = 0; i < k; ++i) {
3031 >            for (int i = k - 1; i >= 0; i--) {
3032                  checkIncomplete(f);
3033                  checkIncomplete(CompletableFuture.allOf(fs));
3034                  fs[i].complete(one);
# Line 3301 | Line 3038 | public class CompletableFutureTest exten
3038          }
3039      }
3040  
3041 +    public void testAllOf_exceptional() throws Exception {
3042 +        for (int k = 1; k < 10; k++) {
3043 +            CompletableFuture<Integer>[] fs
3044 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3045 +            CFException ex = new CFException();
3046 +            for (int i = 0; i < k; i++)
3047 +                fs[i] = new CompletableFuture<>();
3048 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3049 +            for (int i = 0; i < k; i++) {
3050 +                checkIncomplete(f);
3051 +                checkIncomplete(CompletableFuture.allOf(fs));
3052 +                if (i != k/2) {
3053 +                    fs[i].complete(i);
3054 +                    checkCompletedNormally(fs[i], i);
3055 +                } else {
3056 +                    fs[i].completeExceptionally(ex);
3057 +                    checkCompletedExceptionally(fs[i], ex);
3058 +                }
3059 +            }
3060 +            checkCompletedWithWrappedException(f, ex);
3061 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3062 +        }
3063 +    }
3064 +
3065      /**
3066       * anyOf(no component futures) returns an incomplete future
3067       */
3068      public void testAnyOf_empty() throws Exception {
3069 +        for (Integer v1 : new Integer[] { 1, null })
3070 +    {
3071          CompletableFuture<Object> f = CompletableFuture.anyOf();
3072          checkIncomplete(f);
3073 <    }
3073 >
3074 >        f.complete(v1);
3075 >        checkCompletedNormally(f, v1);
3076 >    }}
3077  
3078      /**
3079       * anyOf returns a future completed normally with a value when
3080       * a component future does
3081       */
3082      public void testAnyOf_normal() throws Exception {
3083 <        for (int k = 0; k < 10; ++k) {
3083 >        for (int k = 0; k < 10; k++) {
3084              CompletableFuture[] fs = new CompletableFuture[k];
3085 <            for (int i = 0; i < k; ++i)
3085 >            for (int i = 0; i < k; i++)
3086                  fs[i] = new CompletableFuture<>();
3087              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3088              checkIncomplete(f);
3089 <            for (int i = 0; i < k; ++i) {
3090 <                fs[i].complete(one);
3091 <                checkCompletedNormally(f, one);
3092 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3089 >            for (int i = 0; i < k; i++) {
3090 >                fs[i].complete(i);
3091 >                checkCompletedNormally(f, 0);
3092 >                int x = (int) CompletableFuture.anyOf(fs).join();
3093 >                assertTrue(0 <= x && x <= i);
3094 >            }
3095 >        }
3096 >    }
3097 >    public void testAnyOf_normal_backwards() throws Exception {
3098 >        for (int k = 0; k < 10; k++) {
3099 >            CompletableFuture[] fs = new CompletableFuture[k];
3100 >            for (int i = 0; i < k; i++)
3101 >                fs[i] = new CompletableFuture<>();
3102 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3103 >            checkIncomplete(f);
3104 >            for (int i = k - 1; i >= 0; i--) {
3105 >                fs[i].complete(i);
3106 >                checkCompletedNormally(f, k - 1);
3107 >                int x = (int) CompletableFuture.anyOf(fs).join();
3108 >                assertTrue(i <= x && x <= k - 1);
3109              }
3110          }
3111      }
# Line 3332 | Line 3114 | public class CompletableFutureTest exten
3114       * anyOf result completes exceptionally when any component does.
3115       */
3116      public void testAnyOf_exceptional() throws Exception {
3117 <        for (int k = 0; k < 10; ++k) {
3117 >        for (int k = 0; k < 10; k++) {
3118              CompletableFuture[] fs = new CompletableFuture[k];
3119 <            for (int i = 0; i < k; ++i)
3119 >            CFException[] exs = new CFException[k];
3120 >            for (int i = 0; i < k; i++) {
3121                  fs[i] = new CompletableFuture<>();
3122 +                exs[i] = new CFException();
3123 +            }
3124              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3125              checkIncomplete(f);
3126 <            for (int i = 0; i < k; ++i) {
3127 <                fs[i].completeExceptionally(new CFException());
3128 <                checkCompletedWithWrappedCFException(f);
3126 >            for (int i = 0; i < k; i++) {
3127 >                fs[i].completeExceptionally(exs[i]);
3128 >                checkCompletedWithWrappedException(f, exs[0]);
3129 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3130 >            }
3131 >        }
3132 >    }
3133 >
3134 >    public void testAnyOf_exceptional_backwards() throws Exception {
3135 >        for (int k = 0; k < 10; k++) {
3136 >            CompletableFuture[] fs = new CompletableFuture[k];
3137 >            CFException[] exs = new CFException[k];
3138 >            for (int i = 0; i < k; i++) {
3139 >                fs[i] = new CompletableFuture<>();
3140 >                exs[i] = new CFException();
3141 >            }
3142 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3143 >            checkIncomplete(f);
3144 >            for (int i = k - 1; i >= 0; i--) {
3145 >                fs[i].completeExceptionally(exs[i]);
3146 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3147                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3148              }
3149          }
# Line 3359 | Line 3162 | public class CompletableFutureTest exten
3162          Runnable[] throwingActions = {
3163              () -> CompletableFuture.supplyAsync(null),
3164              () -> CompletableFuture.supplyAsync(null, exec),
3165 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
3165 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3166  
3167              () -> CompletableFuture.runAsync(null),
3168              () -> CompletableFuture.runAsync(null, exec),
# Line 3432 | Line 3235 | public class CompletableFutureTest exten
3235  
3236              () -> f.thenCompose(null),
3237              () -> f.thenComposeAsync(null),
3238 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
3238 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
3239              () -> f.thenComposeAsync(null, exec),
3240  
3241              () -> f.exceptionally(null),
# Line 3464 | Line 3267 | public class CompletableFutureTest exten
3267          assertSame(f, f.toCompletableFuture());
3268      }
3269  
3270 <    /**
3468 <     * whenComplete action executes on normal completion, propagating
3469 <     * source result.
3470 <     */
3471 <    public void testWhenComplete1() {
3472 <        final AtomicInteger a = new AtomicInteger();
3473 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3474 <        CompletableFuture<Integer> g =
3475 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3476 <        f.complete(three);
3477 <        checkCompletedNormally(f, three);
3478 <        checkCompletedNormally(g, three);
3479 <        assertEquals(a.get(), 1);
3480 <    }
3481 <
3482 <    /**
3483 <     * whenComplete action executes on exceptional completion, propagating
3484 <     * source result.
3485 <     */
3486 <    public void testWhenComplete2() {
3487 <        final AtomicInteger a = new AtomicInteger();
3488 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3489 <        CompletableFuture<Integer> g =
3490 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3491 <        f.completeExceptionally(new CFException());
3492 <        assertTrue(f.isCompletedExceptionally());
3493 <        assertTrue(g.isCompletedExceptionally());
3494 <        assertEquals(a.get(), 1);
3495 <    }
3496 <
3497 <    /**
3498 <     * If a whenComplete action throws an exception when triggered by
3499 <     * a normal completion, it completes exceptionally
3500 <     */
3501 <    public void testWhenComplete3() {
3502 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3503 <        CompletableFuture<Integer> g =
3504 <            f.whenComplete((Integer x, Throwable t) ->
3505 <                           { throw new CFException(); } );
3506 <        f.complete(three);
3507 <        checkCompletedNormally(f, three);
3508 <        assertTrue(g.isCompletedExceptionally());
3509 <        checkCompletedWithWrappedCFException(g);
3510 <    }
3511 <
3512 <    /**
3513 <     * whenCompleteAsync action executes on normal completion, propagating
3514 <     * source result.
3515 <     */
3516 <    public void testWhenCompleteAsync1() {
3517 <        final AtomicInteger a = new AtomicInteger();
3518 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3519 <        CompletableFuture<Integer> g =
3520 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3521 <        f.complete(three);
3522 <        checkCompletedNormally(f, three);
3523 <        checkCompletedNormally(g, three);
3524 <        assertEquals(a.get(), 1);
3525 <    }
3270 >    //--- tests of implementation details; not part of official tck ---
3271  
3272 <    /**
3273 <     * whenCompleteAsync action executes on exceptional completion, propagating
3274 <     * source result.
3275 <     */
3276 <    public void testWhenCompleteAsync2() {
3277 <        final AtomicInteger a = new AtomicInteger();
3278 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3534 <        CompletableFuture<Integer> g =
3535 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3536 <        f.completeExceptionally(new CFException());
3537 <        checkCompletedWithWrappedCFException(f);
3538 <        checkCompletedWithWrappedCFException(g);
3539 <    }
3540 <
3541 <    /**
3542 <     * If a whenCompleteAsync action throws an exception when
3543 <     * triggered by a normal completion, it completes exceptionally
3544 <     */
3545 <    public void testWhenCompleteAsync3() {
3546 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3547 <        CompletableFuture<Integer> g =
3548 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3549 <                           { throw new CFException(); } );
3550 <        f.complete(three);
3551 <        checkCompletedNormally(f, three);
3552 <        checkCompletedWithWrappedCFException(g);
3553 <    }
3554 <
3555 <    /**
3556 <     * whenCompleteAsync action executes on normal completion, propagating
3557 <     * source result.
3558 <     */
3559 <    public void testWhenCompleteAsync1e() {
3560 <        final AtomicInteger a = new AtomicInteger();
3561 <        ThreadExecutor exec = new ThreadExecutor();
3562 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3563 <        CompletableFuture<Integer> g =
3564 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3565 <                                exec);
3566 <        f.complete(three);
3567 <        checkCompletedNormally(f, three);
3568 <        checkCompletedNormally(g, three);
3569 <        assertEquals(a.get(), 1);
3272 >    Object resultOf(CompletableFuture<?> f) {
3273 >        try {
3274 >            java.lang.reflect.Field resultField
3275 >                = CompletableFuture.class.getDeclaredField("result");
3276 >            resultField.setAccessible(true);
3277 >            return resultField.get(f);
3278 >        } catch (Throwable t) { throw new AssertionError(t); }
3279      }
3280  
3281 <    /**
3282 <     * whenCompleteAsync action executes on exceptional completion, propagating
3283 <     * source result.
3284 <     */
3285 <    public void testWhenCompleteAsync2e() {
3286 <        final AtomicInteger a = new AtomicInteger();
3287 <        ThreadExecutor exec = new ThreadExecutor();
3579 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3580 <        CompletableFuture<Integer> g =
3581 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3582 <                                exec);
3583 <        f.completeExceptionally(new CFException());
3584 <        checkCompletedWithWrappedCFException(f);
3585 <        checkCompletedWithWrappedCFException(g);
3586 <    }
3281 >    public void testExceptionPropagationReusesResultObject() {
3282 >        if (!testImplementationDetails) return;
3283 >        for (ExecutionMode m : ExecutionMode.values())
3284 >    {
3285 >        final CFException ex = new CFException();
3286 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3287 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3288  
3289 <    /**
3290 <     * If a whenCompleteAsync action throws an exception when triggered
3590 <     * by a normal completion, it completes exceptionally
3591 <     */
3592 <    public void testWhenCompleteAsync3e() {
3593 <        ThreadExecutor exec = new ThreadExecutor();
3594 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3595 <        CompletableFuture<Integer> g =
3596 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3597 <                                { throw new CFException(); },
3598 <                                exec);
3599 <        f.complete(three);
3600 <        checkCompletedNormally(f, three);
3601 <        checkCompletedWithWrappedCFException(g);
3602 <    }
3289 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3290 >            = new ArrayList<>();
3291  
3292 <    /**
3293 <     * handleAsync action completes normally with function value on
3294 <     * either normal or exceptional completion of source
3607 <     */
3608 <    public void testHandleAsync() {
3609 <        CompletableFuture<Integer> f, g;
3610 <        IntegerHandler r;
3292 >        funs.add((y) -> m.thenRun(y, new Noop(m)));
3293 >        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3294 >        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3295  
3296 <        f = new CompletableFuture<>();
3297 <        g = f.handleAsync(r = new IntegerHandler());
3298 <        assertFalse(r.ran);
3615 <        f.completeExceptionally(new CFException());
3616 <        checkCompletedWithWrappedCFException(f);
3617 <        checkCompletedNormally(g, three);
3618 <        assertTrue(r.ran);
3296 >        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3297 >        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3298 >        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3299  
3300 <        f = new CompletableFuture<>();
3301 <        g = f.handleAsync(r = new IntegerHandler());
3302 <        assertFalse(r.ran);
3623 <        f.completeExceptionally(new CFException());
3624 <        checkCompletedWithWrappedCFException(f);
3625 <        checkCompletedNormally(g, three);
3626 <        assertTrue(r.ran);
3300 >        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3301 >        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3302 >        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3303  
3304 <        f = new CompletableFuture<>();
3629 <        g = f.handleAsync(r = new IntegerHandler());
3630 <        assertFalse(r.ran);
3631 <        f.complete(one);
3632 <        checkCompletedNormally(f, one);
3633 <        checkCompletedNormally(g, two);
3634 <        assertTrue(r.ran);
3304 >        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3305  
3306 <        f = new CompletableFuture<>();
3637 <        g = f.handleAsync(r = new IntegerHandler());
3638 <        assertFalse(r.ran);
3639 <        f.complete(one);
3640 <        checkCompletedNormally(f, one);
3641 <        checkCompletedNormally(g, two);
3642 <        assertTrue(r.ran);
3643 <    }
3306 >        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3307  
3308 <    /**
3309 <     * handleAsync action with Executor completes normally with
3647 <     * function value on either normal or exceptional completion of
3648 <     * source
3649 <     */
3650 <    public void testHandleAsync2() {
3651 <        CompletableFuture<Integer> f, g;
3652 <        ThreadExecutor exec = new ThreadExecutor();
3653 <        IntegerHandler r;
3308 >        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3309 >        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3310  
3311 <        f = new CompletableFuture<>();
3312 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3313 <        assertFalse(r.ran);
3314 <        f.completeExceptionally(new CFException());
3315 <        checkCompletedWithWrappedCFException(f);
3316 <        checkCompletedNormally(g, three);
3317 <        assertTrue(r.ran);
3311 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3312 >                 fun : funs) {
3313 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3314 >            f.completeExceptionally(ex);
3315 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3316 >            checkCompletedWithWrappedException(src, ex);
3317 >            CompletableFuture<?> dep = fun.apply(src);
3318 >            checkCompletedWithWrappedException(dep, ex);
3319 >            assertSame(resultOf(src), resultOf(dep));
3320 >        }
3321  
3322 <        f = new CompletableFuture<>();
3323 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3324 <        assertFalse(r.ran);
3325 <        f.completeExceptionally(new CFException());
3326 <        checkCompletedWithWrappedCFException(f);
3327 <        checkCompletedNormally(g, three);
3328 <        assertTrue(r.ran);
3322 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3323 >                 fun : funs) {
3324 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3325 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3326 >            CompletableFuture<?> dep = fun.apply(src);
3327 >            f.completeExceptionally(ex);
3328 >            checkCompletedWithWrappedException(src, ex);
3329 >            checkCompletedWithWrappedException(dep, ex);
3330 >            assertSame(resultOf(src), resultOf(dep));
3331 >        }
3332  
3333 <        f = new CompletableFuture<>();
3334 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3335 <        assertFalse(r.ran);
3336 <        f.complete(one);
3337 <        checkCompletedNormally(f, one);
3338 <        checkCompletedNormally(g, two);
3339 <        assertTrue(r.ran);
3333 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3334 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3335 >                 fun : funs) {
3336 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3337 >            f.cancel(mayInterruptIfRunning);
3338 >            checkCancelled(f);
3339 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3340 >            checkCompletedWithWrappedCancellationException(src);
3341 >            CompletableFuture<?> dep = fun.apply(src);
3342 >            checkCompletedWithWrappedCancellationException(dep);
3343 >            assertSame(resultOf(src), resultOf(dep));
3344 >        }
3345  
3346 <        f = new CompletableFuture<>();
3347 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3348 <        assertFalse(r.ran);
3349 <        f.complete(one);
3350 <        checkCompletedNormally(f, one);
3351 <        checkCompletedNormally(g, two);
3352 <        assertTrue(r.ran);
3353 <    }
3346 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3347 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3348 >                 fun : funs) {
3349 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3350 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3351 >            CompletableFuture<?> dep = fun.apply(src);
3352 >            f.cancel(mayInterruptIfRunning);
3353 >            checkCancelled(f);
3354 >            checkCompletedWithWrappedCancellationException(src);
3355 >            checkCompletedWithWrappedCancellationException(dep);
3356 >            assertSame(resultOf(src), resultOf(dep));
3357 >        }
3358 >    }}
3359  
3360   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines