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.58 by jsr166, Mon Jun 2 23:10:08 2014 UTC vs.
Revision 1.107 by jsr166, Thu Sep 3 17:06:18 2015 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import junit.framework.*;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 > import static java.util.concurrent.TimeUnit.SECONDS;
10 >
11 > import java.util.ArrayList;
12 > import java.util.List;
13 > import java.util.Objects;
14   import java.util.concurrent.Callable;
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
15   import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
16   import java.util.concurrent.CompletableFuture;
17   import java.util.concurrent.CompletionException;
18   import java.util.concurrent.CompletionStage;
19 + import java.util.concurrent.ExecutionException;
20 + import java.util.concurrent.Executor;
21   import java.util.concurrent.ForkJoinPool;
22   import java.util.concurrent.ForkJoinTask;
23   import java.util.concurrent.TimeoutException;
24 + import java.util.concurrent.TimeUnit;
25   import java.util.concurrent.atomic.AtomicInteger;
26 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
25 < import static java.util.concurrent.TimeUnit.SECONDS;
26 < import java.util.*;
27 < import java.util.function.Supplier;
28 < import java.util.function.Consumer;
26 > import java.util.concurrent.atomic.AtomicReference;
27   import java.util.function.BiConsumer;
30 import java.util.function.Function;
28   import java.util.function.BiFunction;
29 + import java.util.function.Consumer;
30 + import java.util.function.Function;
31 + import java.util.function.Supplier;
32 +
33 + import junit.framework.Test;
34 + import junit.framework.TestSuite;
35  
36   public class CompletableFutureTest extends JSR166TestCase {
37  
38      public static void main(String[] args) {
39 <        junit.textui.TestRunner.run(suite());
39 >        main(suite(), args);
40      }
41      public static Test suite() {
42          return new TestSuite(CompletableFutureTest.class);
# Line 44 | Line 47 | public class CompletableFutureTest exten
47      void checkIncomplete(CompletableFuture<?> f) {
48          assertFalse(f.isDone());
49          assertFalse(f.isCancelled());
50 <        assertTrue(f.toString().contains("[Not completed]"));
50 >        assertTrue(f.toString().contains("Not completed"));
51          try {
52              assertNull(f.getNow(null));
53          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 57 | Line 60 | public class CompletableFutureTest exten
60      }
61  
62      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
63 <        try {
64 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
63 >        checkTimedGet(f, value);
64 >
65          try {
66              assertEquals(value, f.join());
67          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 76 | Line 78 | public class CompletableFutureTest exten
78      }
79  
80      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
81 +        long startTime = System.nanoTime();
82 +        long timeoutMillis = LONG_DELAY_MS;
83          try {
84 <            f.get(LONG_DELAY_MS, MILLISECONDS);
84 >            f.get(timeoutMillis, MILLISECONDS);
85              shouldThrow();
86          } catch (ExecutionException success) {
87              assertTrue(success.getCause() instanceof CFException);
88          } catch (Throwable fail) { threadUnexpectedException(fail); }
89 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
90 +
91          try {
92              f.join();
93              shouldThrow();
# Line 105 | Line 111 | public class CompletableFutureTest exten
111          assertTrue(f.toString().contains("[Completed exceptionally]"));
112      }
113  
114 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
115 <                                              CFException ex) {
114 >    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
115 >                                                      Throwable ex) {
116 >        long startTime = System.nanoTime();
117 >        long timeoutMillis = LONG_DELAY_MS;
118          try {
119 <            f.get(LONG_DELAY_MS, MILLISECONDS);
119 >            f.get(timeoutMillis, MILLISECONDS);
120              shouldThrow();
121          } catch (ExecutionException success) {
122              assertSame(ex, success.getCause());
123          } catch (Throwable fail) { threadUnexpectedException(fail); }
124 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
125 +
126          try {
127              f.join();
128              shouldThrow();
# Line 131 | Line 141 | public class CompletableFutureTest exten
141          } catch (ExecutionException success) {
142              assertSame(ex, success.getCause());
143          } catch (Throwable fail) { threadUnexpectedException(fail); }
144 +
145 +        assertTrue(f.isDone());
146 +        assertFalse(f.isCancelled());
147 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
148 +    }
149 +
150 +    <U> void checkCompletedExceptionallyWithTimeout(CompletableFuture<U> f) {
151 +        long startTime = System.nanoTime();
152 +        long timeoutMillis = LONG_DELAY_MS;
153 +        try {
154 +            f.get(timeoutMillis, MILLISECONDS);
155 +            shouldThrow();
156 +        } catch (ExecutionException ex) {
157 +            assertTrue(ex.getCause() instanceof TimeoutException);
158 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
159 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
160 +
161 +        try {
162 +            f.join();
163 +            shouldThrow();
164 +        } catch (Throwable ex) {
165 +            assertTrue(ex.getCause() instanceof TimeoutException);
166 +        }
167 +
168 +        try {
169 +            f.getNow(null);
170 +            shouldThrow();
171 +        } catch (Throwable ex) {
172 +            assertTrue(ex.getCause() instanceof TimeoutException);
173 +        }
174 +
175 +        try {
176 +            f.get();
177 +            shouldThrow();
178 +        } catch (ExecutionException ex) {
179 +            assertTrue(ex.getCause() instanceof TimeoutException);
180 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
181 +
182          assertTrue(f.isDone());
183          assertFalse(f.isCancelled());
184          assertTrue(f.toString().contains("[Completed exceptionally]"));
185      }
186  
187 +    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
188 +                                                Throwable ex) {
189 +        checkCompletedExceptionallyWithRootCause(f, ex);
190 +        try {
191 +            CompletableFuture<Throwable> spy = f.handle
192 +                ((U u, Throwable t) -> t);
193 +            assertTrue(spy.join() instanceof CompletionException);
194 +            assertSame(ex, spy.join().getCause());
195 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
196 +    }
197 +
198 +    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
199 +        checkCompletedExceptionallyWithRootCause(f, ex);
200 +        try {
201 +            CompletableFuture<Throwable> spy = f.handle
202 +                ((U u, Throwable t) -> t);
203 +            assertSame(ex, spy.join());
204 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
205 +    }
206 +
207      void checkCancelled(CompletableFuture<?> f) {
208 +        long startTime = System.nanoTime();
209 +        long timeoutMillis = LONG_DELAY_MS;
210          try {
211 <            f.get(LONG_DELAY_MS, MILLISECONDS);
211 >            f.get(timeoutMillis, MILLISECONDS);
212              shouldThrow();
213          } catch (CancellationException success) {
214          } catch (Throwable fail) { threadUnexpectedException(fail); }
215 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
216 +
217          try {
218              f.join();
219              shouldThrow();
# Line 162 | Line 234 | public class CompletableFutureTest exten
234      }
235  
236      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
237 +        long startTime = System.nanoTime();
238 +        long timeoutMillis = LONG_DELAY_MS;
239          try {
240 <            f.get(LONG_DELAY_MS, MILLISECONDS);
240 >            f.get(timeoutMillis, MILLISECONDS);
241              shouldThrow();
242          } catch (ExecutionException success) {
243              assertTrue(success.getCause() instanceof CancellationException);
244          } catch (Throwable fail) { threadUnexpectedException(fail); }
245 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
246 +
247          try {
248              f.join();
249              shouldThrow();
# Line 206 | Line 282 | public class CompletableFutureTest exten
282       * isCancelled, join, get, and getNow
283       */
284      public void testComplete() {
285 +        for (Integer v1 : new Integer[] { 1, null })
286 +    {
287          CompletableFuture<Integer> f = new CompletableFuture<>();
288          checkIncomplete(f);
289 <        f.complete(one);
290 <        checkCompletedNormally(f, one);
291 <    }
289 >        assertTrue(f.complete(v1));
290 >        assertFalse(f.complete(v1));
291 >        checkCompletedNormally(f, v1);
292 >    }}
293  
294      /**
295       * completeExceptionally completes exceptionally, as indicated by
# Line 218 | Line 297 | public class CompletableFutureTest exten
297       */
298      public void testCompleteExceptionally() {
299          CompletableFuture<Integer> f = new CompletableFuture<>();
300 +        CFException ex = new CFException();
301          checkIncomplete(f);
302 <        f.completeExceptionally(new CFException());
303 <        checkCompletedWithWrappedCFException(f);
302 >        f.completeExceptionally(ex);
303 >        checkCompletedExceptionally(f, ex);
304      }
305  
306      /**
# Line 228 | Line 308 | public class CompletableFutureTest exten
308       * methods isDone, isCancelled, join, get, and getNow
309       */
310      public void testCancel() {
311 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
312 +    {
313          CompletableFuture<Integer> f = new CompletableFuture<>();
314          checkIncomplete(f);
315 <        assertTrue(f.cancel(true));
315 >        assertTrue(f.cancel(mayInterruptIfRunning));
316 >        assertTrue(f.cancel(mayInterruptIfRunning));
317 >        assertTrue(f.cancel(!mayInterruptIfRunning));
318          checkCancelled(f);
319 <    }
319 >    }}
320  
321      /**
322       * obtrudeValue forces completion with given value
# Line 240 | Line 324 | public class CompletableFutureTest exten
324      public void testObtrudeValue() {
325          CompletableFuture<Integer> f = new CompletableFuture<>();
326          checkIncomplete(f);
327 <        f.complete(one);
327 >        assertTrue(f.complete(one));
328          checkCompletedNormally(f, one);
329          f.obtrudeValue(three);
330          checkCompletedNormally(f, three);
# Line 261 | Line 345 | public class CompletableFutureTest exten
345       * obtrudeException forces completion with given exception
346       */
347      public void testObtrudeException() {
348 <        CompletableFuture<Integer> f = new CompletableFuture<>();
349 <        checkIncomplete(f);
350 <        f.complete(one);
351 <        checkCompletedNormally(f, one);
352 <        f.obtrudeException(new CFException());
269 <        checkCompletedWithWrappedCFException(f);
348 >        for (Integer v1 : new Integer[] { 1, null })
349 >    {
350 >        CFException ex;
351 >        CompletableFuture<Integer> f;
352 >
353          f = new CompletableFuture<>();
354 <        f.obtrudeException(new CFException());
355 <        checkCompletedWithWrappedCFException(f);
354 >        assertTrue(f.complete(v1));
355 >        for (int i = 0; i < 2; i++) {
356 >            f.obtrudeException(ex = new CFException());
357 >            checkCompletedExceptionally(f, ex);
358 >        }
359 >
360 >        f = new CompletableFuture<>();
361 >        for (int i = 0; i < 2; i++) {
362 >            f.obtrudeException(ex = new CFException());
363 >            checkCompletedExceptionally(f, ex);
364 >        }
365 >
366          f = new CompletableFuture<>();
367 +        f.completeExceptionally(ex = new CFException());
368 +        f.obtrudeValue(v1);
369 +        checkCompletedNormally(f, v1);
370 +        f.obtrudeException(ex = new CFException());
371 +        checkCompletedExceptionally(f, ex);
372          f.completeExceptionally(new CFException());
373 <        f.obtrudeValue(four);
374 <        checkCompletedNormally(f, four);
375 <        f.obtrudeException(new CFException());
376 <        checkCompletedWithWrappedCFException(f);
279 <    }
373 >        checkCompletedExceptionally(f, ex);
374 >        assertFalse(f.complete(v1));
375 >        checkCompletedExceptionally(f, ex);
376 >    }}
377  
378      /**
379       * getNumberOfDependents returns number of dependent tasks
380       */
381      public void testGetNumberOfDependents() {
382 +        for (ExecutionMode m : ExecutionMode.values())
383 +        for (Integer v1 : new Integer[] { 1, null })
384 +    {
385          CompletableFuture<Integer> f = new CompletableFuture<>();
386          assertEquals(0, f.getNumberOfDependents());
387 <        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
387 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
388          assertEquals(1, f.getNumberOfDependents());
389          assertEquals(0, g.getNumberOfDependents());
390 <        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
390 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
391          assertEquals(2, f.getNumberOfDependents());
392 <        f.complete(1);
392 >        assertEquals(0, h.getNumberOfDependents());
393 >        assertTrue(f.complete(v1));
394          checkCompletedNormally(g, null);
395 +        checkCompletedNormally(h, null);
396          assertEquals(0, f.getNumberOfDependents());
397          assertEquals(0, g.getNumberOfDependents());
398 <    }
398 >        assertEquals(0, h.getNumberOfDependents());
399 >    }}
400  
401      /**
402       * toString indicates current completion state
# Line 304 | Line 407 | public class CompletableFutureTest exten
407          f = new CompletableFuture<String>();
408          assertTrue(f.toString().contains("[Not completed]"));
409  
410 <        f.complete("foo");
410 >        assertTrue(f.complete("foo"));
411          assertTrue(f.toString().contains("[Completed normally]"));
412  
413          f = new CompletableFuture<String>();
414 <        f.completeExceptionally(new IndexOutOfBoundsException());
414 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
415          assertTrue(f.toString().contains("[Completed exceptionally]"));
416 +
417 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
418 +            f = new CompletableFuture<String>();
419 +            assertTrue(f.cancel(mayInterruptIfRunning));
420 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
421 +        }
422      }
423  
424      /**
# Line 320 | Line 429 | public class CompletableFutureTest exten
429          checkCompletedNormally(f, "test");
430      }
431  
432 <    // Choose non-commutative actions for better coverage
433 <
434 <    // A non-commutative function that handles and produces null values as well.
435 <    static Integer subtract(Integer x, Integer y) {
436 <        return (x == null && y == null) ? null :
437 <            ((x == null) ? 42 : x.intValue())
438 <            - ((y == null) ? 99 : y.intValue());
432 >    abstract class CheckedAction {
433 >        int invocationCount = 0;
434 >        final ExecutionMode m;
435 >        CheckedAction(ExecutionMode m) { this.m = m; }
436 >        void invoked() {
437 >            m.checkExecutionMode();
438 >            assertEquals(0, invocationCount++);
439 >        }
440 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
441 >        void assertInvoked() { assertEquals(1, invocationCount); }
442      }
443  
444 <    // A function that handles and produces null values as well.
445 <    static Integer inc(Integer x) {
446 <        return (x == null) ? null : x + 1;
444 >    abstract class CheckedIntegerAction extends CheckedAction {
445 >        Integer value;
446 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
447 >        void assertValue(Integer expected) {
448 >            assertInvoked();
449 >            assertEquals(expected, value);
450 >        }
451      }
452  
453 <    static final class IntegerSupplier implements Supplier<Integer> {
454 <        final ExecutionMode m;
455 <        int invocationCount = 0;
453 >    class IntegerSupplier extends CheckedAction
454 >        implements Supplier<Integer>
455 >    {
456          final Integer value;
457          IntegerSupplier(ExecutionMode m, Integer value) {
458 <            this.m = m;
458 >            super(m);
459              this.value = value;
460          }
461          public Integer get() {
462 <            m.checkExecutionMode();
347 <            invocationCount++;
462 >            invoked();
463              return value;
464          }
465      }
466 <        
467 <    static final class IncAction implements Consumer<Integer> {
468 <        int invocationCount = 0;
469 <        Integer value;
466 >
467 >    // A function that handles and produces null values as well.
468 >    static Integer inc(Integer x) {
469 >        return (x == null) ? null : x + 1;
470 >    }
471 >
472 >    class NoopConsumer extends CheckedIntegerAction
473 >        implements Consumer<Integer>
474 >    {
475 >        NoopConsumer(ExecutionMode m) { super(m); }
476          public void accept(Integer x) {
477 <            invocationCount++;
478 <            value = inc(x);
477 >            invoked();
478 >            value = x;
479          }
480      }
481 <    static final class IncFunction implements Function<Integer,Integer> {
482 <        final ExecutionMode m;
483 <        int invocationCount = 0;
484 <        Integer value;
485 <        IncFunction(ExecutionMode m) { this.m = m; }
481 >
482 >    class IncFunction extends CheckedIntegerAction
483 >        implements Function<Integer,Integer>
484 >    {
485 >        IncFunction(ExecutionMode m) { super(m); }
486          public Integer apply(Integer x) {
487 <            m.checkExecutionMode();
367 <            invocationCount++;
487 >            invoked();
488              return value = inc(x);
489          }
490      }
491 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
492 <        final ExecutionMode m;
493 <        int invocationCount = 0;
494 <        Integer value;
495 <        // Check this action was invoked exactly once when result is computed.
496 <        SubtractAction(ExecutionMode m) { this.m = m; }
491 >
492 >    // Choose non-commutative actions for better coverage
493 >    // A non-commutative function that handles and produces null values as well.
494 >    static Integer subtract(Integer x, Integer y) {
495 >        return (x == null && y == null) ? null :
496 >            ((x == null) ? 42 : x.intValue())
497 >            - ((y == null) ? 99 : y.intValue());
498 >    }
499 >
500 >    class SubtractAction extends CheckedIntegerAction
501 >        implements BiConsumer<Integer, Integer>
502 >    {
503 >        SubtractAction(ExecutionMode m) { super(m); }
504          public void accept(Integer x, Integer y) {
505 <            m.checkExecutionMode();
379 <            invocationCount++;
505 >            invoked();
506              value = subtract(x, y);
507          }
508      }
509 <    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
510 <        final ExecutionMode m;
511 <        int invocationCount = 0;
512 <        Integer value;
513 <        // Check this action was invoked exactly once when result is computed.
388 <        SubtractFunction(ExecutionMode m) { this.m = m; }
509 >
510 >    class SubtractFunction extends CheckedIntegerAction
511 >        implements BiFunction<Integer, Integer, Integer>
512 >    {
513 >        SubtractFunction(ExecutionMode m) { super(m); }
514          public Integer apply(Integer x, Integer y) {
515 <            m.checkExecutionMode();
391 <            invocationCount++;
515 >            invoked();
516              return value = subtract(x, y);
517          }
518      }
519 <    static final class Noop implements Runnable {
520 <        final ExecutionMode m;
521 <        int invocationCount = 0;
398 <        Noop(ExecutionMode m) { this.m = m; }
519 >
520 >    class Noop extends CheckedAction implements Runnable {
521 >        Noop(ExecutionMode m) { super(m); }
522          public void run() {
523 <            m.checkExecutionMode();
401 <            invocationCount++;
523 >            invoked();
524          }
525      }
526  
527 <    static final class FailingSupplier implements Supplier<Integer> {
528 <        final ExecutionMode m;
529 <        int invocationCount = 0;
530 <        FailingSupplier(ExecutionMode m) { this.m = m; }
527 >    class FailingSupplier extends CheckedAction
528 >        implements Supplier<Integer>
529 >    {
530 >        FailingSupplier(ExecutionMode m) { super(m); }
531          public Integer get() {
532 <            m.checkExecutionMode();
411 <            invocationCount++;
532 >            invoked();
533              throw new CFException();
534          }
535      }
536 <    static final class FailingConsumer implements Consumer<Integer> {
537 <        final ExecutionMode m;
538 <        int invocationCount = 0;
539 <        FailingConsumer(ExecutionMode m) { this.m = m; }
536 >
537 >    class FailingConsumer extends CheckedIntegerAction
538 >        implements Consumer<Integer>
539 >    {
540 >        FailingConsumer(ExecutionMode m) { super(m); }
541          public void accept(Integer x) {
542 <            m.checkExecutionMode();
543 <            invocationCount++;
542 >            invoked();
543 >            value = x;
544              throw new CFException();
545          }
546      }
547 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
548 <        final ExecutionMode m;
549 <        int invocationCount = 0;
550 <        FailingBiConsumer(ExecutionMode m) { this.m = m; }
547 >
548 >    class FailingBiConsumer extends CheckedIntegerAction
549 >        implements BiConsumer<Integer, Integer>
550 >    {
551 >        FailingBiConsumer(ExecutionMode m) { super(m); }
552          public void accept(Integer x, Integer y) {
553 <            m.checkExecutionMode();
554 <            invocationCount++;
553 >            invoked();
554 >            value = subtract(x, y);
555              throw new CFException();
556          }
557      }
558 <    static final class FailingFunction implements Function<Integer, Integer> {
559 <        final ExecutionMode m;
560 <        int invocationCount = 0;
561 <        FailingFunction(ExecutionMode m) { this.m = m; }
558 >
559 >    class FailingFunction extends CheckedIntegerAction
560 >        implements Function<Integer, Integer>
561 >    {
562 >        FailingFunction(ExecutionMode m) { super(m); }
563          public Integer apply(Integer x) {
564 <            m.checkExecutionMode();
565 <            invocationCount++;
564 >            invoked();
565 >            value = x;
566              throw new CFException();
567          }
568      }
569 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
570 <        final ExecutionMode m;
571 <        int invocationCount = 0;
572 <        FailingBiFunction(ExecutionMode m) { this.m = m; }
569 >
570 >    class FailingBiFunction extends CheckedIntegerAction
571 >        implements BiFunction<Integer, Integer, Integer>
572 >    {
573 >        FailingBiFunction(ExecutionMode m) { super(m); }
574          public Integer apply(Integer x, Integer y) {
575 <            m.checkExecutionMode();
576 <            invocationCount++;
575 >            invoked();
576 >            value = subtract(x, y);
577              throw new CFException();
578          }
579      }
580 <    static final class FailingRunnable implements Runnable {
581 <        final ExecutionMode m;
582 <        int invocationCount = 0;
458 <        FailingRunnable(ExecutionMode m) { this.m = m; }
580 >
581 >    class FailingRunnable extends CheckedAction implements Runnable {
582 >        FailingRunnable(ExecutionMode m) { super(m); }
583          public void run() {
584 <            m.checkExecutionMode();
461 <            invocationCount++;
584 >            invoked();
585              throw new CFException();
586          }
587      }
588  
589 <    static final class CompletableFutureInc
590 <        implements Function<Integer, CompletableFuture<Integer>> {
591 <        final ExecutionMode m;
592 <        int invocationCount = 0;
470 <        CompletableFutureInc(ExecutionMode m) { this.m = m; }
589 >    class CompletableFutureInc extends CheckedIntegerAction
590 >        implements Function<Integer, CompletableFuture<Integer>>
591 >    {
592 >        CompletableFutureInc(ExecutionMode m) { super(m); }
593          public CompletableFuture<Integer> apply(Integer x) {
594 <            m.checkExecutionMode();
595 <            invocationCount++;
594 >            invoked();
595 >            value = x;
596              CompletableFuture<Integer> f = new CompletableFuture<>();
597 <            f.complete(inc(x));
597 >            assertTrue(f.complete(inc(x)));
598              return f;
599          }
600      }
601  
602 <    static final class FailingCompletableFutureFunction
603 <        implements Function<Integer, CompletableFuture<Integer>> {
604 <        final ExecutionMode m;
605 <        int invocationCount = 0;
484 <        FailingCompletableFutureFunction(ExecutionMode m) { this.m = m; }
602 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
603 >        implements Function<Integer, CompletableFuture<Integer>>
604 >    {
605 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
606          public CompletableFuture<Integer> apply(Integer x) {
607 <            m.checkExecutionMode();
608 <            invocationCount++;
607 >            invoked();
608 >            value = x;
609              throw new CFException();
610          }
611      }
# Line 503 | Line 624 | public class CompletableFutureTest exten
624          }
625      }
626  
627 +    static final boolean defaultExecutorIsCommonPool
628 +        = ForkJoinPool.getCommonPoolParallelism() > 1;
629 +
630      /**
631       * Permits the testing of parallel code for the 3 different
632 <     * execution modes without repeating all the testing code.
632 >     * execution modes without copy/pasting all the test methods.
633       */
634      enum ExecutionMode {
635 <        DEFAULT {
635 >        SYNC {
636              public void checkExecutionMode() {
637 +                assertFalse(ThreadExecutor.startedCurrentThread());
638                  assertNull(ForkJoinTask.getPool());
639              }
640              public CompletableFuture<Void> runAsync(Runnable a) {
# Line 583 | Line 708 | public class CompletableFutureTest exten
708  
709          ASYNC {
710              public void checkExecutionMode() {
711 <                assertSame(ForkJoinPool.commonPool(),
712 <                           ForkJoinTask.getPool());
711 >                assertEquals(defaultExecutorIsCommonPool,
712 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
713              }
714              public CompletableFuture<Void> runAsync(Runnable a) {
715                  return CompletableFuture.runAsync(a);
# Line 780 | Line 905 | public class CompletableFutureTest exten
905      {
906          final AtomicInteger a = new AtomicInteger(0);
907          final CompletableFuture<Integer> f = new CompletableFuture<>();
908 <        if (!createIncomplete) f.complete(v1);
908 >        if (!createIncomplete) assertTrue(f.complete(v1));
909          final CompletableFuture<Integer> g = f.exceptionally
910              ((Throwable t) -> {
911                  // Should not be called
912                  a.getAndIncrement();
913                  throw new AssertionError();
914              });
915 <        if (createIncomplete) f.complete(v1);
915 >        if (createIncomplete) assertTrue(f.complete(v1));
916  
917          checkCompletedNormally(g, v1);
918          checkCompletedNormally(f, v1);
919          assertEquals(0, a.get());
920      }}
921  
797
922      /**
923       * exceptionally action completes with function value on source
924       * exception
# Line 809 | Line 933 | public class CompletableFutureTest exten
933          if (!createIncomplete) f.completeExceptionally(ex);
934          final CompletableFuture<Integer> g = f.exceptionally
935              ((Throwable t) -> {
936 <                ExecutionMode.DEFAULT.checkExecutionMode();
936 >                ExecutionMode.SYNC.checkExecutionMode();
937                  threadAssertSame(t, ex);
938                  a.getAndIncrement();
939                  return v1;
# Line 822 | Line 946 | public class CompletableFutureTest exten
946  
947      public void testExceptionally_exceptionalCompletionActionFailed() {
948          for (boolean createIncomplete : new boolean[] { true, false })
825        for (Integer v1 : new Integer[] { 1, null })
949      {
950          final AtomicInteger a = new AtomicInteger(0);
951          final CFException ex1 = new CFException();
# Line 831 | Line 954 | public class CompletableFutureTest exten
954          if (!createIncomplete) f.completeExceptionally(ex1);
955          final CompletableFuture<Integer> g = f.exceptionally
956              ((Throwable t) -> {
957 <                ExecutionMode.DEFAULT.checkExecutionMode();
957 >                ExecutionMode.SYNC.checkExecutionMode();
958 >                threadAssertSame(t, ex1);
959 >                a.getAndIncrement();
960 >                throw ex2;
961 >            });
962 >        if (createIncomplete) f.completeExceptionally(ex1);
963 >
964 >        checkCompletedWithWrappedException(g, ex2);
965 >        assertEquals(1, a.get());
966 >    }}
967 >
968 >    /**
969 >     * whenComplete action executes on normal completion, propagating
970 >     * source result.
971 >     */
972 >    public void testWhenComplete_normalCompletion1() {
973 >        for (ExecutionMode m : ExecutionMode.values())
974 >        for (boolean createIncomplete : new boolean[] { true, false })
975 >        for (Integer v1 : new Integer[] { 1, null })
976 >    {
977 >        final AtomicInteger a = new AtomicInteger(0);
978 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
979 >        if (!createIncomplete) assertTrue(f.complete(v1));
980 >        final CompletableFuture<Integer> g = m.whenComplete
981 >            (f,
982 >             (Integer x, Throwable t) -> {
983 >                m.checkExecutionMode();
984 >                threadAssertSame(x, v1);
985 >                threadAssertNull(t);
986 >                a.getAndIncrement();
987 >            });
988 >        if (createIncomplete) assertTrue(f.complete(v1));
989 >
990 >        checkCompletedNormally(g, v1);
991 >        checkCompletedNormally(f, v1);
992 >        assertEquals(1, a.get());
993 >    }}
994 >
995 >    /**
996 >     * whenComplete action executes on exceptional completion, propagating
997 >     * source result.
998 >     */
999 >    public void testWhenComplete_exceptionalCompletion() {
1000 >        for (ExecutionMode m : ExecutionMode.values())
1001 >        for (boolean createIncomplete : new boolean[] { true, false })
1002 >    {
1003 >        final AtomicInteger a = new AtomicInteger(0);
1004 >        final CFException ex = new CFException();
1005 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1006 >        if (!createIncomplete) f.completeExceptionally(ex);
1007 >        final CompletableFuture<Integer> g = m.whenComplete
1008 >            (f,
1009 >             (Integer x, Throwable t) -> {
1010 >                m.checkExecutionMode();
1011 >                threadAssertNull(x);
1012 >                threadAssertSame(t, ex);
1013 >                a.getAndIncrement();
1014 >            });
1015 >        if (createIncomplete) f.completeExceptionally(ex);
1016 >
1017 >        checkCompletedWithWrappedException(g, ex);
1018 >        checkCompletedExceptionally(f, ex);
1019 >        assertEquals(1, a.get());
1020 >    }}
1021 >
1022 >    /**
1023 >     * whenComplete action executes on cancelled source, propagating
1024 >     * CancellationException.
1025 >     */
1026 >    public void testWhenComplete_sourceCancelled() {
1027 >        for (ExecutionMode m : ExecutionMode.values())
1028 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1029 >        for (boolean createIncomplete : new boolean[] { true, false })
1030 >    {
1031 >        final AtomicInteger a = new AtomicInteger(0);
1032 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1033 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1034 >        final CompletableFuture<Integer> g = m.whenComplete
1035 >            (f,
1036 >             (Integer x, Throwable t) -> {
1037 >                m.checkExecutionMode();
1038 >                threadAssertNull(x);
1039 >                threadAssertTrue(t instanceof CancellationException);
1040 >                a.getAndIncrement();
1041 >            });
1042 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1043 >
1044 >        checkCompletedWithWrappedCancellationException(g);
1045 >        checkCancelled(f);
1046 >        assertEquals(1, a.get());
1047 >    }}
1048 >
1049 >    /**
1050 >     * If a whenComplete action throws an exception when triggered by
1051 >     * a normal completion, it completes exceptionally
1052 >     */
1053 >    public void testWhenComplete_actionFailed() {
1054 >        for (boolean createIncomplete : new boolean[] { true, false })
1055 >        for (ExecutionMode m : ExecutionMode.values())
1056 >        for (Integer v1 : new Integer[] { 1, null })
1057 >    {
1058 >        final AtomicInteger a = new AtomicInteger(0);
1059 >        final CFException ex = new CFException();
1060 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1061 >        if (!createIncomplete) assertTrue(f.complete(v1));
1062 >        final CompletableFuture<Integer> g = m.whenComplete
1063 >            (f,
1064 >             (Integer x, Throwable t) -> {
1065 >                m.checkExecutionMode();
1066 >                threadAssertSame(x, v1);
1067 >                threadAssertNull(t);
1068 >                a.getAndIncrement();
1069 >                throw ex;
1070 >            });
1071 >        if (createIncomplete) assertTrue(f.complete(v1));
1072 >
1073 >        checkCompletedWithWrappedException(g, ex);
1074 >        checkCompletedNormally(f, v1);
1075 >        assertEquals(1, a.get());
1076 >    }}
1077 >
1078 >    /**
1079 >     * If a whenComplete action throws an exception when triggered by
1080 >     * a source completion that also throws an exception, the source
1081 >     * exception takes precedence.
1082 >     */
1083 >    public void testWhenComplete_actionFailedSourceFailed() {
1084 >        for (boolean createIncomplete : new boolean[] { true, false })
1085 >        for (ExecutionMode m : ExecutionMode.values())
1086 >    {
1087 >        final AtomicInteger a = new AtomicInteger(0);
1088 >        final CFException ex1 = new CFException();
1089 >        final CFException ex2 = new CFException();
1090 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1091 >
1092 >        if (!createIncomplete) f.completeExceptionally(ex1);
1093 >        final CompletableFuture<Integer> g = m.whenComplete
1094 >            (f,
1095 >             (Integer x, Throwable t) -> {
1096 >                m.checkExecutionMode();
1097                  threadAssertSame(t, ex1);
1098 +                threadAssertNull(x);
1099                  a.getAndIncrement();
1100                  throw ex2;
1101              });
1102          if (createIncomplete) f.completeExceptionally(ex1);
1103  
1104 <        checkCompletedWithWrappedCFException(g, ex2);
1104 >        checkCompletedWithWrappedException(g, ex1);
1105 >        checkCompletedExceptionally(f, ex1);
1106          assertEquals(1, a.get());
1107      }}
1108  
# Line 853 | Line 1117 | public class CompletableFutureTest exten
1117      {
1118          final CompletableFuture<Integer> f = new CompletableFuture<>();
1119          final AtomicInteger a = new AtomicInteger(0);
1120 <        if (!createIncomplete) f.complete(v1);
1120 >        if (!createIncomplete) assertTrue(f.complete(v1));
1121          final CompletableFuture<Integer> g = m.handle
1122              (f,
1123               (Integer x, Throwable t) -> {
# Line 863 | Line 1127 | public class CompletableFutureTest exten
1127                  a.getAndIncrement();
1128                  return inc(v1);
1129              });
1130 <        if (createIncomplete) f.complete(v1);
1130 >        if (createIncomplete) assertTrue(f.complete(v1));
1131  
1132          checkCompletedNormally(g, inc(v1));
1133          checkCompletedNormally(f, v1);
# Line 895 | Line 1159 | public class CompletableFutureTest exten
1159          if (createIncomplete) f.completeExceptionally(ex);
1160  
1161          checkCompletedNormally(g, v1);
1162 <        checkCompletedWithWrappedCFException(f, ex);
1162 >        checkCompletedExceptionally(f, ex);
1163          assertEquals(1, a.get());
1164      }}
1165  
# Line 951 | Line 1215 | public class CompletableFutureTest exten
1215              });
1216          if (createIncomplete) f.completeExceptionally(ex1);
1217  
1218 <        checkCompletedWithWrappedCFException(g, ex2);
1219 <        checkCompletedWithWrappedCFException(f, ex1);
1218 >        checkCompletedWithWrappedException(g, ex2);
1219 >        checkCompletedExceptionally(f, ex1);
1220          assertEquals(1, a.get());
1221      }}
1222  
# Line 964 | Line 1228 | public class CompletableFutureTest exten
1228          final CompletableFuture<Integer> f = new CompletableFuture<>();
1229          final AtomicInteger a = new AtomicInteger(0);
1230          final CFException ex = new CFException();
1231 <        if (!createIncomplete) f.complete(v1);
1231 >        if (!createIncomplete) assertTrue(f.complete(v1));
1232          final CompletableFuture<Integer> g = m.handle
1233              (f,
1234               (Integer x, Throwable t) -> {
# Line 974 | Line 1238 | public class CompletableFutureTest exten
1238                  a.getAndIncrement();
1239                  throw ex;
1240              });
1241 <        if (createIncomplete) f.complete(v1);
1241 >        if (createIncomplete) assertTrue(f.complete(v1));
1242  
1243 <        checkCompletedWithWrappedCFException(g, ex);
1243 >        checkCompletedWithWrappedException(g, ex);
1244          checkCompletedNormally(f, v1);
1245          assertEquals(1, a.get());
1246      }}
# Line 995 | Line 1259 | public class CompletableFutureTest exten
1259          final CompletableFuture<Void> f = m.runAsync(r);
1260          assertNull(f.join());
1261          checkCompletedNormally(f, null);
1262 <        assertEquals(1, r.invocationCount);
1262 >        r.assertInvoked();
1263      }}
1264  
1265      /**
# Line 1011 | Line 1275 | public class CompletableFutureTest exten
1275          final FailingRunnable r = new FailingRunnable(m);
1276          final CompletableFuture<Void> f = m.runAsync(r);
1277          checkCompletedWithWrappedCFException(f);
1278 <        assertEquals(1, r.invocationCount);
1278 >        r.assertInvoked();
1279      }}
1280  
1281      /**
# Line 1029 | Line 1293 | public class CompletableFutureTest exten
1293          final CompletableFuture<Integer> f = m.supplyAsync(r);
1294          assertSame(v1, f.join());
1295          checkCompletedNormally(f, v1);
1296 <        assertEquals(1, r.invocationCount);
1296 >        r.assertInvoked();
1297      }}
1298  
1299      /**
# Line 1045 | Line 1309 | public class CompletableFutureTest exten
1309          FailingSupplier r = new FailingSupplier(m);
1310          CompletableFuture<Integer> f = m.supplyAsync(r);
1311          checkCompletedWithWrappedCFException(f);
1312 <        assertEquals(1, r.invocationCount);
1312 >        r.assertInvoked();
1313      }}
1314  
1315      // seq completion methods
# Line 1055 | Line 1319 | public class CompletableFutureTest exten
1319       */
1320      public void testThenRun_normalCompletion() {
1321          for (ExecutionMode m : ExecutionMode.values())
1058        for (boolean createIncomplete : new boolean[] { true, false })
1322          for (Integer v1 : new Integer[] { 1, null })
1323      {
1324          final CompletableFuture<Integer> f = new CompletableFuture<>();
1325 <        final Noop r = new Noop(m);
1326 <        if (!createIncomplete) f.complete(v1);
1064 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1065 <        if (createIncomplete) {
1066 <            checkIncomplete(g);
1067 <            f.complete(v1);
1068 <        }
1325 >        final Noop[] rs = new Noop[6];
1326 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1327  
1328 <        checkCompletedNormally(g, null);
1328 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1329 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1330 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1331 >        checkIncomplete(h0);
1332 >        checkIncomplete(h1);
1333 >        checkIncomplete(h2);
1334 >        assertTrue(f.complete(v1));
1335 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1336 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1337 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1338 >
1339 >        checkCompletedNormally(h0, null);
1340 >        checkCompletedNormally(h1, null);
1341 >        checkCompletedNormally(h2, null);
1342 >        checkCompletedNormally(h3, null);
1343 >        checkCompletedNormally(h4, null);
1344 >        checkCompletedNormally(h5, null);
1345          checkCompletedNormally(f, v1);
1346 <        assertEquals(1, r.invocationCount);
1346 >        for (Noop r : rs) r.assertInvoked();
1347      }}
1348  
1349      /**
# Line 1078 | Line 1352 | public class CompletableFutureTest exten
1352       */
1353      public void testThenRun_exceptionalCompletion() {
1354          for (ExecutionMode m : ExecutionMode.values())
1081        for (boolean createIncomplete : new boolean[] { true, false })
1355      {
1356          final CFException ex = new CFException();
1357          final CompletableFuture<Integer> f = new CompletableFuture<>();
1358 <        final Noop r = new Noop(m);
1359 <        if (!createIncomplete) f.completeExceptionally(ex);
1087 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1088 <        if (createIncomplete) {
1089 <            checkIncomplete(g);
1090 <            f.completeExceptionally(ex);
1091 <        }
1358 >        final Noop[] rs = new Noop[6];
1359 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1360  
1361 <        checkCompletedWithWrappedCFException(g, ex);
1362 <        checkCompletedWithWrappedCFException(f, ex);
1363 <        assertEquals(0, r.invocationCount);
1361 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1362 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1363 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1364 >        checkIncomplete(h0);
1365 >        checkIncomplete(h1);
1366 >        checkIncomplete(h2);
1367 >        assertTrue(f.completeExceptionally(ex));
1368 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1369 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1370 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1371 >
1372 >        checkCompletedWithWrappedException(h0, ex);
1373 >        checkCompletedWithWrappedException(h1, ex);
1374 >        checkCompletedWithWrappedException(h2, ex);
1375 >        checkCompletedWithWrappedException(h3, ex);
1376 >        checkCompletedWithWrappedException(h4, ex);
1377 >        checkCompletedWithWrappedException(h5, ex);
1378 >        checkCompletedExceptionally(f, ex);
1379 >        for (Noop r : rs) r.assertNotInvoked();
1380      }}
1381  
1382      /**
# Line 1100 | Line 1384 | public class CompletableFutureTest exten
1384       */
1385      public void testThenRun_sourceCancelled() {
1386          for (ExecutionMode m : ExecutionMode.values())
1103        for (boolean createIncomplete : new boolean[] { true, false })
1387          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1388      {
1389          final CompletableFuture<Integer> f = new CompletableFuture<>();
1390 <        final Noop r = new Noop(m);
1391 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1109 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1110 <        if (createIncomplete) {
1111 <            checkIncomplete(g);
1112 <            assertTrue(f.cancel(mayInterruptIfRunning));
1113 <        }
1390 >        final Noop[] rs = new Noop[6];
1391 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1392  
1393 <        checkCompletedWithWrappedCancellationException(g);
1393 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1394 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1395 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1396 >        checkIncomplete(h0);
1397 >        checkIncomplete(h1);
1398 >        checkIncomplete(h2);
1399 >        assertTrue(f.cancel(mayInterruptIfRunning));
1400 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1401 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1402 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1403 >
1404 >        checkCompletedWithWrappedCancellationException(h0);
1405 >        checkCompletedWithWrappedCancellationException(h1);
1406 >        checkCompletedWithWrappedCancellationException(h2);
1407 >        checkCompletedWithWrappedCancellationException(h3);
1408 >        checkCompletedWithWrappedCancellationException(h4);
1409 >        checkCompletedWithWrappedCancellationException(h5);
1410          checkCancelled(f);
1411 <        assertEquals(0, r.invocationCount);
1411 >        for (Noop r : rs) r.assertNotInvoked();
1412      }}
1413  
1414      /**
# Line 1122 | Line 1416 | public class CompletableFutureTest exten
1416       */
1417      public void testThenRun_actionFailed() {
1418          for (ExecutionMode m : ExecutionMode.values())
1125        for (boolean createIncomplete : new boolean[] { true, false })
1419          for (Integer v1 : new Integer[] { 1, null })
1420      {
1421          final CompletableFuture<Integer> f = new CompletableFuture<>();
1422 <        final FailingRunnable r = new FailingRunnable(m);
1423 <        if (!createIncomplete) f.complete(v1);
1131 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1132 <        if (createIncomplete) {
1133 <            checkIncomplete(g);
1134 <            f.complete(v1);
1135 <        }
1422 >        final FailingRunnable[] rs = new FailingRunnable[6];
1423 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1424  
1425 <        checkCompletedWithWrappedCFException(g);
1425 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1426 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1427 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1428 >        assertTrue(f.complete(v1));
1429 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1430 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1431 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1432 >
1433 >        checkCompletedWithWrappedCFException(h0);
1434 >        checkCompletedWithWrappedCFException(h1);
1435 >        checkCompletedWithWrappedCFException(h2);
1436 >        checkCompletedWithWrappedCFException(h3);
1437 >        checkCompletedWithWrappedCFException(h4);
1438 >        checkCompletedWithWrappedCFException(h5);
1439          checkCompletedNormally(f, v1);
1440      }}
1441  
# Line 1143 | Line 1444 | public class CompletableFutureTest exten
1444       */
1445      public void testThenApply_normalCompletion() {
1446          for (ExecutionMode m : ExecutionMode.values())
1146        for (boolean createIncomplete : new boolean[] { true, false })
1447          for (Integer v1 : new Integer[] { 1, null })
1448      {
1449          final CompletableFuture<Integer> f = new CompletableFuture<>();
1450 <        final IncFunction r = new IncFunction(m);
1451 <        if (!createIncomplete) f.complete(v1);
1152 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1153 <        if (createIncomplete) {
1154 <            checkIncomplete(g);
1155 <            f.complete(v1);
1156 <        }
1450 >        final IncFunction[] rs = new IncFunction[4];
1451 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1452  
1453 <        checkCompletedNormally(g, inc(v1));
1453 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1454 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1455 >        checkIncomplete(h0);
1456 >        checkIncomplete(h1);
1457 >        assertTrue(f.complete(v1));
1458 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1459 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1460 >
1461 >        checkCompletedNormally(h0, inc(v1));
1462 >        checkCompletedNormally(h1, inc(v1));
1463 >        checkCompletedNormally(h2, inc(v1));
1464 >        checkCompletedNormally(h3, inc(v1));
1465          checkCompletedNormally(f, v1);
1466 <        assertEquals(1, r.invocationCount);
1466 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1467      }}
1468  
1469      /**
# Line 1166 | Line 1472 | public class CompletableFutureTest exten
1472       */
1473      public void testThenApply_exceptionalCompletion() {
1474          for (ExecutionMode m : ExecutionMode.values())
1169        for (boolean createIncomplete : new boolean[] { true, false })
1475      {
1476          final CFException ex = new CFException();
1477          final CompletableFuture<Integer> f = new CompletableFuture<>();
1478 <        final IncFunction r = new IncFunction(m);
1479 <        if (!createIncomplete) f.completeExceptionally(ex);
1175 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1176 <        if (createIncomplete) {
1177 <            checkIncomplete(g);
1178 <            f.completeExceptionally(ex);
1179 <        }
1478 >        final IncFunction[] rs = new IncFunction[4];
1479 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1480  
1481 <        checkCompletedWithWrappedCFException(g, ex);
1482 <        checkCompletedWithWrappedCFException(f, ex);
1483 <        assertEquals(0, r.invocationCount);
1481 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1482 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1483 >        assertTrue(f.completeExceptionally(ex));
1484 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1485 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1486 >
1487 >        checkCompletedWithWrappedException(h0, ex);
1488 >        checkCompletedWithWrappedException(h1, ex);
1489 >        checkCompletedWithWrappedException(h2, ex);
1490 >        checkCompletedWithWrappedException(h3, ex);
1491 >        checkCompletedExceptionally(f, ex);
1492 >        for (IncFunction r : rs) r.assertNotInvoked();
1493      }}
1494  
1495      /**
# Line 1188 | Line 1497 | public class CompletableFutureTest exten
1497       */
1498      public void testThenApply_sourceCancelled() {
1499          for (ExecutionMode m : ExecutionMode.values())
1191        for (boolean createIncomplete : new boolean[] { true, false })
1500          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1501      {
1502          final CompletableFuture<Integer> f = new CompletableFuture<>();
1503 <        final IncFunction r = new IncFunction(m);
1504 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1197 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1198 <        if (createIncomplete) {
1199 <            checkIncomplete(g);
1200 <            assertTrue(f.cancel(mayInterruptIfRunning));
1201 <        }
1503 >        final IncFunction[] rs = new IncFunction[4];
1504 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1505  
1506 <        checkCompletedWithWrappedCancellationException(g);
1506 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1507 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1508 >        assertTrue(f.cancel(mayInterruptIfRunning));
1509 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1510 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1511 >
1512 >        checkCompletedWithWrappedCancellationException(h0);
1513 >        checkCompletedWithWrappedCancellationException(h1);
1514 >        checkCompletedWithWrappedCancellationException(h2);
1515 >        checkCompletedWithWrappedCancellationException(h3);
1516          checkCancelled(f);
1517 <        assertEquals(0, r.invocationCount);
1517 >        for (IncFunction r : rs) r.assertNotInvoked();
1518      }}
1519  
1520      /**
# Line 1210 | Line 1522 | public class CompletableFutureTest exten
1522       */
1523      public void testThenApply_actionFailed() {
1524          for (ExecutionMode m : ExecutionMode.values())
1213        for (boolean createIncomplete : new boolean[] { true, false })
1525          for (Integer v1 : new Integer[] { 1, null })
1526      {
1527          final CompletableFuture<Integer> f = new CompletableFuture<>();
1528 <        final FailingFunction r = new FailingFunction(m);
1529 <        if (!createIncomplete) f.complete(v1);
1219 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1220 <        if (createIncomplete) {
1221 <            checkIncomplete(g);
1222 <            f.complete(v1);
1223 <        }
1528 >        final FailingFunction[] rs = new FailingFunction[4];
1529 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1530  
1531 <        checkCompletedWithWrappedCFException(g);
1531 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1532 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1533 >        assertTrue(f.complete(v1));
1534 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1535 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1536 >
1537 >        checkCompletedWithWrappedCFException(h0);
1538 >        checkCompletedWithWrappedCFException(h1);
1539 >        checkCompletedWithWrappedCFException(h2);
1540 >        checkCompletedWithWrappedCFException(h3);
1541          checkCompletedNormally(f, v1);
1542      }}
1543  
# Line 1231 | Line 1546 | public class CompletableFutureTest exten
1546       */
1547      public void testThenAccept_normalCompletion() {
1548          for (ExecutionMode m : ExecutionMode.values())
1234        for (boolean createIncomplete : new boolean[] { true, false })
1549          for (Integer v1 : new Integer[] { 1, null })
1550      {
1551          final CompletableFuture<Integer> f = new CompletableFuture<>();
1552 <        final IncAction r = new IncAction();
1553 <        if (!createIncomplete) f.complete(v1);
1240 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1241 <        if (createIncomplete) {
1242 <            checkIncomplete(g);
1243 <            f.complete(v1);
1244 <        }
1552 >        final NoopConsumer[] rs = new NoopConsumer[4];
1553 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1554  
1555 <        checkCompletedNormally(g, null);
1555 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1556 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1557 >        checkIncomplete(h0);
1558 >        checkIncomplete(h1);
1559 >        assertTrue(f.complete(v1));
1560 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1561 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1562 >
1563 >        checkCompletedNormally(h0, null);
1564 >        checkCompletedNormally(h1, null);
1565 >        checkCompletedNormally(h2, null);
1566 >        checkCompletedNormally(h3, null);
1567          checkCompletedNormally(f, v1);
1568 <        assertEquals(1, r.invocationCount);
1249 <        assertEquals(inc(v1), r.value);
1568 >        for (NoopConsumer r : rs) r.assertValue(v1);
1569      }}
1570  
1571      /**
# Line 1255 | Line 1574 | public class CompletableFutureTest exten
1574       */
1575      public void testThenAccept_exceptionalCompletion() {
1576          for (ExecutionMode m : ExecutionMode.values())
1258        for (boolean createIncomplete : new boolean[] { true, false })
1577      {
1578          final CFException ex = new CFException();
1579          final CompletableFuture<Integer> f = new CompletableFuture<>();
1580 <        final IncAction r = new IncAction();
1581 <        if (!createIncomplete) f.completeExceptionally(ex);
1264 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1265 <        if (createIncomplete) {
1266 <            checkIncomplete(g);
1267 <            f.completeExceptionally(ex);
1268 <        }
1580 >        final NoopConsumer[] rs = new NoopConsumer[4];
1581 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1582  
1583 <        checkCompletedWithWrappedCFException(g, ex);
1584 <        checkCompletedWithWrappedCFException(f, ex);
1585 <        assertEquals(0, r.invocationCount);
1583 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1584 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1585 >        assertTrue(f.completeExceptionally(ex));
1586 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1587 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1588 >
1589 >        checkCompletedWithWrappedException(h0, ex);
1590 >        checkCompletedWithWrappedException(h1, ex);
1591 >        checkCompletedWithWrappedException(h2, ex);
1592 >        checkCompletedWithWrappedException(h3, ex);
1593 >        checkCompletedExceptionally(f, ex);
1594 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1595      }}
1596  
1597      /**
1598 <     * thenAccept result completes exceptionally if action does
1598 >     * thenAccept result completes exceptionally if source cancelled
1599       */
1600 <    public void testThenAccept_actionFailed() {
1600 >    public void testThenAccept_sourceCancelled() {
1601          for (ExecutionMode m : ExecutionMode.values())
1602 <        for (boolean createIncomplete : new boolean[] { true, false })
1281 <        for (Integer v1 : new Integer[] { 1, null })
1602 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1603      {
1604          final CompletableFuture<Integer> f = new CompletableFuture<>();
1605 <        final FailingConsumer r = new FailingConsumer(m);
1606 <        if (!createIncomplete) f.complete(v1);
1286 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1287 <        if (createIncomplete) {
1288 <            checkIncomplete(g);
1289 <            f.complete(v1);
1290 <        }
1605 >        final NoopConsumer[] rs = new NoopConsumer[4];
1606 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1607  
1608 <        checkCompletedWithWrappedCFException(g);
1609 <        checkCompletedNormally(f, v1);
1608 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1609 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1610 >        assertTrue(f.cancel(mayInterruptIfRunning));
1611 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1612 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1613 >
1614 >        checkCompletedWithWrappedCancellationException(h0);
1615 >        checkCompletedWithWrappedCancellationException(h1);
1616 >        checkCompletedWithWrappedCancellationException(h2);
1617 >        checkCompletedWithWrappedCancellationException(h3);
1618 >        checkCancelled(f);
1619 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1620      }}
1621  
1622      /**
1623 <     * thenAccept result completes exceptionally if source cancelled
1623 >     * thenAccept result completes exceptionally if action does
1624       */
1625 <    public void testThenAccept_sourceCancelled() {
1625 >    public void testThenAccept_actionFailed() {
1626          for (ExecutionMode m : ExecutionMode.values())
1627 <        for (boolean createIncomplete : new boolean[] { true, false })
1302 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1627 >        for (Integer v1 : new Integer[] { 1, null })
1628      {
1629          final CompletableFuture<Integer> f = new CompletableFuture<>();
1630 <        final IncAction r = new IncAction();
1631 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1307 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1308 <        if (createIncomplete) {
1309 <            checkIncomplete(g);
1310 <            assertTrue(f.cancel(mayInterruptIfRunning));
1311 <        }
1630 >        final FailingConsumer[] rs = new FailingConsumer[4];
1631 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1632  
1633 <        checkCompletedWithWrappedCancellationException(g);
1634 <        checkCancelled(f);
1635 <        assertEquals(0, r.invocationCount);
1633 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1634 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1635 >        assertTrue(f.complete(v1));
1636 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1637 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1638 >
1639 >        checkCompletedWithWrappedCFException(h0);
1640 >        checkCompletedWithWrappedCFException(h1);
1641 >        checkCompletedWithWrappedCFException(h2);
1642 >        checkCompletedWithWrappedCFException(h3);
1643 >        checkCompletedNormally(f, v1);
1644      }}
1645  
1646      /**
# Line 1321 | Line 1649 | public class CompletableFutureTest exten
1649       */
1650      public void testThenCombine_normalCompletion() {
1651          for (ExecutionMode m : ExecutionMode.values())
1324        for (boolean createIncomplete : new boolean[] { true, false })
1652          for (boolean fFirst : new boolean[] { true, false })
1653          for (Integer v1 : new Integer[] { 1, null })
1654          for (Integer v2 : new Integer[] { 2, null })
1655      {
1656          final CompletableFuture<Integer> f = new CompletableFuture<>();
1657          final CompletableFuture<Integer> g = new CompletableFuture<>();
1658 <        final SubtractFunction r = new SubtractFunction(m);
1658 >        final SubtractFunction[] rs = new SubtractFunction[6];
1659 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1660  
1661 <        if (fFirst) f.complete(v1); else g.complete(v2);
1662 <        if (!createIncomplete)
1663 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1664 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1665 <        if (createIncomplete) {
1666 <            checkIncomplete(h);
1667 <            assertEquals(0, r.invocationCount);
1668 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1669 <        }
1661 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1662 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1663 >        final Integer w1 =  fFirst ? v1 : v2;
1664 >        final Integer w2 = !fFirst ? v1 : v2;
1665 >
1666 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1667 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1668 >        assertTrue(fst.complete(w1));
1669 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1670 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1671 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1672 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1673 >        checkCompletedNormally(h1, subtract(w1, w1));
1674 >        checkCompletedNormally(h3, subtract(w1, w1));
1675 >        rs[1].assertValue(subtract(w1, w1));
1676 >        rs[3].assertValue(subtract(w1, w1));
1677 >        assertTrue(snd.complete(w2));
1678 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1679 >
1680 >        checkCompletedNormally(h0, subtract(v1, v2));
1681 >        checkCompletedNormally(h2, subtract(v1, v2));
1682 >        checkCompletedNormally(h4, subtract(v1, v2));
1683 >        rs[0].assertValue(subtract(v1, v2));
1684 >        rs[2].assertValue(subtract(v1, v2));
1685 >        rs[4].assertValue(subtract(v1, v2));
1686  
1343        checkCompletedNormally(h, subtract(v1, v2));
1687          checkCompletedNormally(f, v1);
1688          checkCompletedNormally(g, v2);
1346        assertEquals(1, r.invocationCount);
1689      }}
1690  
1691      /**
1692       * thenCombine result completes exceptionally after exceptional
1693       * completion of either source
1694       */
1695 <    public void testThenCombine_exceptionalCompletion() {
1695 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1696          for (ExecutionMode m : ExecutionMode.values())
1355        for (boolean createIncomplete : new boolean[] { true, false })
1697          for (boolean fFirst : new boolean[] { true, false })
1698 +        for (boolean failFirst : new boolean[] { true, false })
1699          for (Integer v1 : new Integer[] { 1, null })
1700      {
1701          final CompletableFuture<Integer> f = new CompletableFuture<>();
1702          final CompletableFuture<Integer> g = new CompletableFuture<>();
1703          final CFException ex = new CFException();
1704 <        final SubtractFunction r = new SubtractFunction(m);
1705 <
1706 <        (fFirst ? f : g).complete(v1);
1707 <        if (!createIncomplete)
1708 <            (!fFirst ? f : g).completeExceptionally(ex);
1709 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1710 <        if (createIncomplete) {
1711 <            checkIncomplete(h);
1712 <            (!fFirst ? f : g).completeExceptionally(ex);
1713 <        }
1714 <
1715 <        checkCompletedWithWrappedCFException(h, ex);
1716 <        assertEquals(0, r.invocationCount);
1717 <        checkCompletedNormally(fFirst ? f : g, v1);
1718 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1704 >        final SubtractFunction r1 = new SubtractFunction(m);
1705 >        final SubtractFunction r2 = new SubtractFunction(m);
1706 >        final SubtractFunction r3 = new SubtractFunction(m);
1707 >
1708 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1709 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1710 >        final Callable<Boolean> complete1 = failFirst ?
1711 >            () -> fst.completeExceptionally(ex) :
1712 >            () -> fst.complete(v1);
1713 >        final Callable<Boolean> complete2 = failFirst ?
1714 >            () -> snd.complete(v1) :
1715 >            () -> snd.completeExceptionally(ex);
1716 >
1717 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1718 >        assertTrue(complete1.call());
1719 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1720 >        checkIncomplete(h1);
1721 >        checkIncomplete(h2);
1722 >        assertTrue(complete2.call());
1723 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1724 >
1725 >        checkCompletedWithWrappedException(h1, ex);
1726 >        checkCompletedWithWrappedException(h2, ex);
1727 >        checkCompletedWithWrappedException(h3, ex);
1728 >        r1.assertNotInvoked();
1729 >        r2.assertNotInvoked();
1730 >        r3.assertNotInvoked();
1731 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1732 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1733      }}
1734  
1735      /**
1736 <     * thenCombine result completes exceptionally if action does
1736 >     * thenCombine result completes exceptionally if either source cancelled
1737       */
1738 <    public void testThenCombine_actionFailed() {
1738 >    public void testThenCombine_sourceCancelled() throws Throwable {
1739          for (ExecutionMode m : ExecutionMode.values())
1740 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1741          for (boolean fFirst : new boolean[] { true, false })
1742 +        for (boolean failFirst : new boolean[] { true, false })
1743          for (Integer v1 : new Integer[] { 1, null })
1386        for (Integer v2 : new Integer[] { 2, null })
1744      {
1745          final CompletableFuture<Integer> f = new CompletableFuture<>();
1746          final CompletableFuture<Integer> g = new CompletableFuture<>();
1747 <        final FailingBiFunction r = new FailingBiFunction(m);
1748 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1749 <
1750 <        if (fFirst) {
1751 <            f.complete(v1);
1752 <            g.complete(v2);
1753 <        } else {
1754 <            g.complete(v2);
1755 <            f.complete(v1);
1756 <        }
1757 <
1758 <        checkCompletedWithWrappedCFException(h);
1759 <        checkCompletedNormally(f, v1);
1760 <        checkCompletedNormally(g, v2);
1747 >        final SubtractFunction r1 = new SubtractFunction(m);
1748 >        final SubtractFunction r2 = new SubtractFunction(m);
1749 >        final SubtractFunction r3 = new SubtractFunction(m);
1750 >
1751 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1752 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1753 >        final Callable<Boolean> complete1 = failFirst ?
1754 >            () -> fst.cancel(mayInterruptIfRunning) :
1755 >            () -> fst.complete(v1);
1756 >        final Callable<Boolean> complete2 = failFirst ?
1757 >            () -> snd.complete(v1) :
1758 >            () -> snd.cancel(mayInterruptIfRunning);
1759 >
1760 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1761 >        assertTrue(complete1.call());
1762 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1763 >        checkIncomplete(h1);
1764 >        checkIncomplete(h2);
1765 >        assertTrue(complete2.call());
1766 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1767 >
1768 >        checkCompletedWithWrappedCancellationException(h1);
1769 >        checkCompletedWithWrappedCancellationException(h2);
1770 >        checkCompletedWithWrappedCancellationException(h3);
1771 >        r1.assertNotInvoked();
1772 >        r2.assertNotInvoked();
1773 >        r3.assertNotInvoked();
1774 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1775 >        checkCancelled(failFirst ? fst : snd);
1776      }}
1777  
1778      /**
1779 <     * thenCombine result completes exceptionally if either source cancelled
1779 >     * thenCombine result completes exceptionally if action does
1780       */
1781 <    public void testThenCombine_sourceCancelled() {
1781 >    public void testThenCombine_actionFailed() {
1782          for (ExecutionMode m : ExecutionMode.values())
1411        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1412        for (boolean createIncomplete : new boolean[] { true, false })
1783          for (boolean fFirst : new boolean[] { true, false })
1784          for (Integer v1 : new Integer[] { 1, null })
1785 +        for (Integer v2 : new Integer[] { 2, null })
1786      {
1787          final CompletableFuture<Integer> f = new CompletableFuture<>();
1788          final CompletableFuture<Integer> g = new CompletableFuture<>();
1789 <        final SubtractFunction r = new SubtractFunction(m);
1789 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1790 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1791 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1792 >
1793 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1794 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1795 >        final Integer w1 =  fFirst ? v1 : v2;
1796 >        final Integer w2 = !fFirst ? v1 : v2;
1797 >
1798 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1799 >        assertTrue(fst.complete(w1));
1800 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1801 >        assertTrue(snd.complete(w2));
1802 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1803  
1804 <        (fFirst ? f : g).complete(v1);
1805 <        if (!createIncomplete)
1806 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1807 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1808 <        if (createIncomplete) {
1809 <            checkIncomplete(h);
1810 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1811 <        }
1428 <
1429 <        checkCompletedWithWrappedCancellationException(h);
1430 <        checkCancelled(!fFirst ? f : g);
1431 <        assertEquals(0, r.invocationCount);
1432 <        checkCompletedNormally(fFirst ? f : g, v1);
1804 >        checkCompletedWithWrappedCFException(h1);
1805 >        checkCompletedWithWrappedCFException(h2);
1806 >        checkCompletedWithWrappedCFException(h3);
1807 >        r1.assertInvoked();
1808 >        r2.assertInvoked();
1809 >        r3.assertInvoked();
1810 >        checkCompletedNormally(f, v1);
1811 >        checkCompletedNormally(g, v2);
1812      }}
1813  
1814      /**
# Line 1438 | Line 1817 | public class CompletableFutureTest exten
1817       */
1818      public void testThenAcceptBoth_normalCompletion() {
1819          for (ExecutionMode m : ExecutionMode.values())
1441        for (boolean createIncomplete : new boolean[] { true, false })
1820          for (boolean fFirst : new boolean[] { true, false })
1821          for (Integer v1 : new Integer[] { 1, null })
1822          for (Integer v2 : new Integer[] { 2, null })
1823      {
1824          final CompletableFuture<Integer> f = new CompletableFuture<>();
1825          final CompletableFuture<Integer> g = new CompletableFuture<>();
1826 <        final SubtractAction r = new SubtractAction(m);
1827 <
1828 <        if (fFirst) f.complete(v1); else g.complete(v2);
1829 <        if (!createIncomplete)
1830 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1831 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1832 <        if (createIncomplete) {
1833 <            checkIncomplete(h);
1834 <            assertEquals(0, r.invocationCount);
1835 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1836 <        }
1837 <
1838 <        checkCompletedNormally(h, null);
1839 <        assertEquals(subtract(v1, v2), r.value);
1826 >        final SubtractAction r1 = new SubtractAction(m);
1827 >        final SubtractAction r2 = new SubtractAction(m);
1828 >        final SubtractAction r3 = new SubtractAction(m);
1829 >
1830 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1831 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1832 >        final Integer w1 =  fFirst ? v1 : v2;
1833 >        final Integer w2 = !fFirst ? v1 : v2;
1834 >
1835 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1836 >        assertTrue(fst.complete(w1));
1837 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1838 >        checkIncomplete(h1);
1839 >        checkIncomplete(h2);
1840 >        r1.assertNotInvoked();
1841 >        r2.assertNotInvoked();
1842 >        assertTrue(snd.complete(w2));
1843 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1844 >
1845 >        checkCompletedNormally(h1, null);
1846 >        checkCompletedNormally(h2, null);
1847 >        checkCompletedNormally(h3, null);
1848 >        r1.assertValue(subtract(v1, v2));
1849 >        r2.assertValue(subtract(v1, v2));
1850 >        r3.assertValue(subtract(v1, v2));
1851          checkCompletedNormally(f, v1);
1852          checkCompletedNormally(g, v2);
1853      }}
# Line 1467 | Line 1856 | public class CompletableFutureTest exten
1856       * thenAcceptBoth result completes exceptionally after exceptional
1857       * completion of either source
1858       */
1859 <    public void testThenAcceptBoth_exceptionalCompletion() {
1859 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1860          for (ExecutionMode m : ExecutionMode.values())
1472        for (boolean createIncomplete : new boolean[] { true, false })
1861          for (boolean fFirst : new boolean[] { true, false })
1862 +        for (boolean failFirst : new boolean[] { true, false })
1863          for (Integer v1 : new Integer[] { 1, null })
1864      {
1865          final CompletableFuture<Integer> f = new CompletableFuture<>();
1866          final CompletableFuture<Integer> g = new CompletableFuture<>();
1867          final CFException ex = new CFException();
1868 <        final SubtractAction r = new SubtractAction(m);
1869 <
1870 <        (fFirst ? f : g).complete(v1);
1871 <        if (!createIncomplete)
1872 <            (!fFirst ? f : g).completeExceptionally(ex);
1873 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1874 <        if (createIncomplete) {
1875 <            checkIncomplete(h);
1876 <            (!fFirst ? f : g).completeExceptionally(ex);
1877 <        }
1878 <
1879 <        checkCompletedWithWrappedCFException(h, ex);
1880 <        assertEquals(0, r.invocationCount);
1881 <        checkCompletedNormally(fFirst ? f : g, v1);
1882 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1868 >        final SubtractAction r1 = new SubtractAction(m);
1869 >        final SubtractAction r2 = new SubtractAction(m);
1870 >        final SubtractAction r3 = new SubtractAction(m);
1871 >
1872 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1873 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1874 >        final Callable<Boolean> complete1 = failFirst ?
1875 >            () -> fst.completeExceptionally(ex) :
1876 >            () -> fst.complete(v1);
1877 >        final Callable<Boolean> complete2 = failFirst ?
1878 >            () -> snd.complete(v1) :
1879 >            () -> snd.completeExceptionally(ex);
1880 >
1881 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1882 >        assertTrue(complete1.call());
1883 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1884 >        checkIncomplete(h1);
1885 >        checkIncomplete(h2);
1886 >        assertTrue(complete2.call());
1887 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1888 >
1889 >        checkCompletedWithWrappedException(h1, ex);
1890 >        checkCompletedWithWrappedException(h2, ex);
1891 >        checkCompletedWithWrappedException(h3, ex);
1892 >        r1.assertNotInvoked();
1893 >        r2.assertNotInvoked();
1894 >        r3.assertNotInvoked();
1895 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1896 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1897      }}
1898  
1899      /**
1900 <     * thenAcceptBoth result completes exceptionally if action does
1900 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1901       */
1902 <    public void testThenAcceptBoth_actionFailed() {
1902 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1903          for (ExecutionMode m : ExecutionMode.values())
1904 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1905          for (boolean fFirst : new boolean[] { true, false })
1906 +        for (boolean failFirst : new boolean[] { true, false })
1907          for (Integer v1 : new Integer[] { 1, null })
1503        for (Integer v2 : new Integer[] { 2, null })
1908      {
1909          final CompletableFuture<Integer> f = new CompletableFuture<>();
1910          final CompletableFuture<Integer> g = new CompletableFuture<>();
1911 <        final FailingBiConsumer r = new FailingBiConsumer(m);
1912 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1913 <
1914 <        if (fFirst) {
1915 <            f.complete(v1);
1916 <            g.complete(v2);
1917 <        } else {
1918 <            g.complete(v2);
1919 <            f.complete(v1);
1920 <        }
1921 <
1922 <        checkCompletedWithWrappedCFException(h);
1923 <        checkCompletedNormally(f, v1);
1924 <        checkCompletedNormally(g, v2);
1911 >        final SubtractAction r1 = new SubtractAction(m);
1912 >        final SubtractAction r2 = new SubtractAction(m);
1913 >        final SubtractAction r3 = new SubtractAction(m);
1914 >
1915 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1916 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1917 >        final Callable<Boolean> complete1 = failFirst ?
1918 >            () -> fst.cancel(mayInterruptIfRunning) :
1919 >            () -> fst.complete(v1);
1920 >        final Callable<Boolean> complete2 = failFirst ?
1921 >            () -> snd.complete(v1) :
1922 >            () -> snd.cancel(mayInterruptIfRunning);
1923 >
1924 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1925 >        assertTrue(complete1.call());
1926 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1927 >        checkIncomplete(h1);
1928 >        checkIncomplete(h2);
1929 >        assertTrue(complete2.call());
1930 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1931 >
1932 >        checkCompletedWithWrappedCancellationException(h1);
1933 >        checkCompletedWithWrappedCancellationException(h2);
1934 >        checkCompletedWithWrappedCancellationException(h3);
1935 >        r1.assertNotInvoked();
1936 >        r2.assertNotInvoked();
1937 >        r3.assertNotInvoked();
1938 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1939 >        checkCancelled(failFirst ? fst : snd);
1940      }}
1941  
1942      /**
1943 <     * thenAcceptBoth result completes exceptionally if either source cancelled
1943 >     * thenAcceptBoth result completes exceptionally if action does
1944       */
1945 <    public void testThenAcceptBoth_sourceCancelled() {
1945 >    public void testThenAcceptBoth_actionFailed() {
1946          for (ExecutionMode m : ExecutionMode.values())
1528        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1529        for (boolean createIncomplete : new boolean[] { true, false })
1947          for (boolean fFirst : new boolean[] { true, false })
1948          for (Integer v1 : new Integer[] { 1, null })
1949 +        for (Integer v2 : new Integer[] { 2, null })
1950      {
1951          final CompletableFuture<Integer> f = new CompletableFuture<>();
1952          final CompletableFuture<Integer> g = new CompletableFuture<>();
1953 <        final SubtractAction r = new SubtractAction(m);
1953 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1954 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1955 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1956 >
1957 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1958 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1959 >        final Integer w1 =  fFirst ? v1 : v2;
1960 >        final Integer w2 = !fFirst ? v1 : v2;
1961 >
1962 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1963 >        assertTrue(fst.complete(w1));
1964 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1965 >        assertTrue(snd.complete(w2));
1966 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1967  
1968 <        (fFirst ? f : g).complete(v1);
1969 <        if (!createIncomplete)
1970 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1971 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1972 <        if (createIncomplete) {
1973 <            checkIncomplete(h);
1974 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1975 <        }
1545 <
1546 <        checkCompletedWithWrappedCancellationException(h);
1547 <        checkCancelled(!fFirst ? f : g);
1548 <        assertEquals(0, r.invocationCount);
1549 <        checkCompletedNormally(fFirst ? f : g, v1);
1968 >        checkCompletedWithWrappedCFException(h1);
1969 >        checkCompletedWithWrappedCFException(h2);
1970 >        checkCompletedWithWrappedCFException(h3);
1971 >        r1.assertInvoked();
1972 >        r2.assertInvoked();
1973 >        r3.assertInvoked();
1974 >        checkCompletedNormally(f, v1);
1975 >        checkCompletedNormally(g, v2);
1976      }}
1977  
1978      /**
# Line 1555 | Line 1981 | public class CompletableFutureTest exten
1981       */
1982      public void testRunAfterBoth_normalCompletion() {
1983          for (ExecutionMode m : ExecutionMode.values())
1558        for (boolean createIncomplete : new boolean[] { true, false })
1984          for (boolean fFirst : new boolean[] { true, false })
1985          for (Integer v1 : new Integer[] { 1, null })
1986          for (Integer v2 : new Integer[] { 2, null })
1987      {
1988          final CompletableFuture<Integer> f = new CompletableFuture<>();
1989          final CompletableFuture<Integer> g = new CompletableFuture<>();
1990 <        final Noop r = new Noop(m);
1991 <
1992 <        if (fFirst) f.complete(v1); else g.complete(v2);
1993 <        if (!createIncomplete)
1994 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1995 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1996 <        if (createIncomplete) {
1997 <            checkIncomplete(h);
1998 <            assertEquals(0, r.invocationCount);
1999 <            if (!fFirst) f.complete(v1); else g.complete(v2);
2000 <        }
2001 <
2002 <        checkCompletedNormally(h, null);
2003 <        assertEquals(1, r.invocationCount);
1990 >        final Noop r1 = new Noop(m);
1991 >        final Noop r2 = new Noop(m);
1992 >        final Noop r3 = new Noop(m);
1993 >
1994 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1995 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1996 >        final Integer w1 =  fFirst ? v1 : v2;
1997 >        final Integer w2 = !fFirst ? v1 : v2;
1998 >
1999 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2000 >        assertTrue(fst.complete(w1));
2001 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2002 >        checkIncomplete(h1);
2003 >        checkIncomplete(h2);
2004 >        r1.assertNotInvoked();
2005 >        r2.assertNotInvoked();
2006 >        assertTrue(snd.complete(w2));
2007 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2008 >
2009 >        checkCompletedNormally(h1, null);
2010 >        checkCompletedNormally(h2, null);
2011 >        checkCompletedNormally(h3, null);
2012 >        r1.assertInvoked();
2013 >        r2.assertInvoked();
2014 >        r3.assertInvoked();
2015          checkCompletedNormally(f, v1);
2016          checkCompletedNormally(g, v2);
2017      }}
# Line 1584 | Line 2020 | public class CompletableFutureTest exten
2020       * runAfterBoth result completes exceptionally after exceptional
2021       * completion of either source
2022       */
2023 <    public void testRunAfterBoth_exceptionalCompletion() {
2023 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
2024          for (ExecutionMode m : ExecutionMode.values())
1589        for (boolean createIncomplete : new boolean[] { true, false })
2025          for (boolean fFirst : new boolean[] { true, false })
2026 +        for (boolean failFirst : new boolean[] { true, false })
2027          for (Integer v1 : new Integer[] { 1, null })
2028      {
2029          final CompletableFuture<Integer> f = new CompletableFuture<>();
2030          final CompletableFuture<Integer> g = new CompletableFuture<>();
2031          final CFException ex = new CFException();
2032 <        final Noop r = new Noop(m);
2033 <
2034 <        (fFirst ? f : g).complete(v1);
2035 <        if (!createIncomplete)
2036 <            (!fFirst ? f : g).completeExceptionally(ex);
2037 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2038 <        if (createIncomplete) {
2039 <            checkIncomplete(h);
2040 <            (!fFirst ? f : g).completeExceptionally(ex);
2041 <        }
2042 <
2043 <        checkCompletedWithWrappedCFException(h, ex);
2044 <        assertEquals(0, r.invocationCount);
2045 <        checkCompletedNormally(fFirst ? f : g, v1);
2046 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
2047 <    }}
2048 <
2049 <    /**
2050 <     * runAfterBoth result completes exceptionally if action does
2051 <     */
2052 <    public void testRunAfterBoth_actionFailed() {
2053 <        for (ExecutionMode m : ExecutionMode.values())
2054 <        for (boolean fFirst : new boolean[] { true, false })
2055 <        for (Integer v1 : new Integer[] { 1, null })
2056 <        for (Integer v2 : new Integer[] { 2, null })
2057 <    {
2058 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2059 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2060 <        final FailingRunnable r = new FailingRunnable(m);
1625 <
1626 <        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r);
1627 <        if (fFirst) {
1628 <            f.complete(v1);
1629 <            g.complete(v2);
1630 <        } else {
1631 <            g.complete(v2);
1632 <            f.complete(v1);
1633 <        }
1634 <        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r);
1635 <
1636 <        checkCompletedWithWrappedCFException(h1);
1637 <        checkCompletedWithWrappedCFException(h2);
1638 <        checkCompletedNormally(f, v1);
1639 <        checkCompletedNormally(g, v2);
2032 >        final Noop r1 = new Noop(m);
2033 >        final Noop r2 = new Noop(m);
2034 >        final Noop r3 = new Noop(m);
2035 >
2036 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2037 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2038 >        final Callable<Boolean> complete1 = failFirst ?
2039 >            () -> fst.completeExceptionally(ex) :
2040 >            () -> fst.complete(v1);
2041 >        final Callable<Boolean> complete2 = failFirst ?
2042 >            () -> snd.complete(v1) :
2043 >            () -> snd.completeExceptionally(ex);
2044 >
2045 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2046 >        assertTrue(complete1.call());
2047 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2048 >        checkIncomplete(h1);
2049 >        checkIncomplete(h2);
2050 >        assertTrue(complete2.call());
2051 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2052 >
2053 >        checkCompletedWithWrappedException(h1, ex);
2054 >        checkCompletedWithWrappedException(h2, ex);
2055 >        checkCompletedWithWrappedException(h3, ex);
2056 >        r1.assertNotInvoked();
2057 >        r2.assertNotInvoked();
2058 >        r3.assertNotInvoked();
2059 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2060 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2061      }}
2062  
2063      /**
2064       * runAfterBoth result completes exceptionally if either source cancelled
2065       */
2066 <    public void testRunAfterBoth_sourceCancelled() {
2066 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2067          for (ExecutionMode m : ExecutionMode.values())
2068          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1648        for (boolean createIncomplete : new boolean[] { true, false })
2069          for (boolean fFirst : new boolean[] { true, false })
2070 +        for (boolean failFirst : new boolean[] { true, false })
2071          for (Integer v1 : new Integer[] { 1, null })
2072      {
2073          final CompletableFuture<Integer> f = new CompletableFuture<>();
2074          final CompletableFuture<Integer> g = new CompletableFuture<>();
2075 <        final Noop r = new Noop(m);
2076 <
2077 <
2078 <        (fFirst ? f : g).complete(v1);
2079 <        if (!createIncomplete)
2080 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2081 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2082 <        if (createIncomplete) {
2083 <            checkIncomplete(h);
2084 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2085 <        }
2086 <
2087 <        checkCompletedWithWrappedCancellationException(h);
2088 <        checkCancelled(!fFirst ? f : g);
2089 <        assertEquals(0, r.invocationCount);
2090 <        checkCompletedNormally(fFirst ? f : g, v1);
2075 >        final Noop r1 = new Noop(m);
2076 >        final Noop r2 = new Noop(m);
2077 >        final Noop r3 = new Noop(m);
2078 >
2079 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2080 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2081 >        final Callable<Boolean> complete1 = failFirst ?
2082 >            () -> fst.cancel(mayInterruptIfRunning) :
2083 >            () -> fst.complete(v1);
2084 >        final Callable<Boolean> complete2 = failFirst ?
2085 >            () -> snd.complete(v1) :
2086 >            () -> snd.cancel(mayInterruptIfRunning);
2087 >
2088 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2089 >        assertTrue(complete1.call());
2090 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2091 >        checkIncomplete(h1);
2092 >        checkIncomplete(h2);
2093 >        assertTrue(complete2.call());
2094 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2095 >
2096 >        checkCompletedWithWrappedCancellationException(h1);
2097 >        checkCompletedWithWrappedCancellationException(h2);
2098 >        checkCompletedWithWrappedCancellationException(h3);
2099 >        r1.assertNotInvoked();
2100 >        r2.assertNotInvoked();
2101 >        r3.assertNotInvoked();
2102 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2103 >        checkCancelled(failFirst ? fst : snd);
2104      }}
2105  
2106      /**
2107 <     * applyToEither result completes normally after normal completion
1674 <     * of either source
2107 >     * runAfterBoth result completes exceptionally if action does
2108       */
2109 <    public void testApplyToEither_normalCompletion() {
2109 >    public void testRunAfterBoth_actionFailed() {
2110          for (ExecutionMode m : ExecutionMode.values())
1678        for (boolean createIncomplete : new boolean[] { true, false })
2111          for (boolean fFirst : new boolean[] { true, false })
2112          for (Integer v1 : new Integer[] { 1, null })
2113          for (Integer v2 : new Integer[] { 2, null })
2114      {
2115          final CompletableFuture<Integer> f = new CompletableFuture<>();
2116          final CompletableFuture<Integer> g = new CompletableFuture<>();
2117 <        final IncFunction r = new IncFunction(m);
2118 <
2119 <        if (!createIncomplete)
2120 <            if (fFirst) f.complete(v1); else g.complete(v2);
2121 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2122 <        if (createIncomplete) {
2123 <            checkIncomplete(h);
2124 <            assertEquals(0, r.invocationCount);
2125 <            if (fFirst) f.complete(v1); else g.complete(v2);
2126 <        }
2127 <        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
2128 <        if (!fFirst) f.complete(v1); else g.complete(v2);
2117 >        final FailingRunnable r1 = new FailingRunnable(m);
2118 >        final FailingRunnable r2 = new FailingRunnable(m);
2119 >        final FailingRunnable r3 = new FailingRunnable(m);
2120 >
2121 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2122 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2123 >        final Integer w1 =  fFirst ? v1 : v2;
2124 >        final Integer w2 = !fFirst ? v1 : v2;
2125 >
2126 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2127 >        assertTrue(fst.complete(w1));
2128 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2129 >        assertTrue(snd.complete(w2));
2130 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2131  
2132 +        checkCompletedWithWrappedCFException(h1);
2133 +        checkCompletedWithWrappedCFException(h2);
2134 +        checkCompletedWithWrappedCFException(h3);
2135 +        r1.assertInvoked();
2136 +        r2.assertInvoked();
2137 +        r3.assertInvoked();
2138          checkCompletedNormally(f, v1);
2139          checkCompletedNormally(g, v2);
1700        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
2140      }}
2141  
2142 <    public void testApplyToEither_normalCompletionBothAvailable() {
2142 >    /**
2143 >     * applyToEither result completes normally after normal completion
2144 >     * of either source
2145 >     */
2146 >    public void testApplyToEither_normalCompletion() {
2147          for (ExecutionMode m : ExecutionMode.values())
1705        for (boolean fFirst : new boolean[] { true, false })
2148          for (Integer v1 : new Integer[] { 1, null })
2149          for (Integer v2 : new Integer[] { 2, null })
2150      {
2151          final CompletableFuture<Integer> f = new CompletableFuture<>();
2152          final CompletableFuture<Integer> g = new CompletableFuture<>();
2153 <        final IncFunction r = new IncFunction(m);
2153 >        final IncFunction[] rs = new IncFunction[6];
2154 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2155  
2156 <        if (fFirst) {
2157 <            f.complete(v1);
2158 <            g.complete(v2);
2159 <        } else {
2160 <            g.complete(v2);
2161 <            f.complete(v1);
2162 <        }
2156 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2157 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2158 >        checkIncomplete(h0);
2159 >        checkIncomplete(h1);
2160 >        rs[0].assertNotInvoked();
2161 >        rs[1].assertNotInvoked();
2162 >        f.complete(v1);
2163 >        checkCompletedNormally(h0, inc(v1));
2164 >        checkCompletedNormally(h1, inc(v1));
2165 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2166 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2167 >        checkCompletedNormally(h2, inc(v1));
2168 >        checkCompletedNormally(h3, inc(v1));
2169 >        g.complete(v2);
2170  
2171 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2171 >        // unspecified behavior - both source completions available
2172 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2173 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2174 >        rs[4].assertValue(h4.join());
2175 >        rs[5].assertValue(h5.join());
2176 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
2177 >                   Objects.equals(inc(v2), h4.join()));
2178 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
2179 >                   Objects.equals(inc(v2), h5.join()));
2180  
2181          checkCompletedNormally(f, v1);
2182          checkCompletedNormally(g, v2);
2183 <
2184 <        // unspecified behavior
2185 <        assertTrue(Objects.equals(h.join(), inc(v1)) ||
2186 <                   Objects.equals(h.join(), inc(v2)));
2187 <        assertEquals(1, r.invocationCount);
2183 >        checkCompletedNormally(h0, inc(v1));
2184 >        checkCompletedNormally(h1, inc(v1));
2185 >        checkCompletedNormally(h2, inc(v1));
2186 >        checkCompletedNormally(h3, inc(v1));
2187 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
2188      }}
2189  
2190      /**
2191       * applyToEither result completes exceptionally after exceptional
2192       * completion of either source
2193       */
2194 <    public void testApplyToEither_exceptionalCompletion1() {
2194 >    public void testApplyToEither_exceptionalCompletion() {
2195          for (ExecutionMode m : ExecutionMode.values())
1738        for (boolean createIncomplete : new boolean[] { true, false })
1739        for (boolean fFirst : new boolean[] { true, false })
2196          for (Integer v1 : new Integer[] { 1, null })
2197      {
2198          final CompletableFuture<Integer> f = new CompletableFuture<>();
2199          final CompletableFuture<Integer> g = new CompletableFuture<>();
2200          final CFException ex = new CFException();
2201 <        final IncFunction r = new IncFunction(m);
2201 >        final IncFunction[] rs = new IncFunction[6];
2202 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2203  
2204 <        if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex);
2205 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2206 <        if (createIncomplete) {
2207 <            checkIncomplete(h);
2208 <            assertEquals(0, r.invocationCount);
2209 <            (fFirst ? f : g).completeExceptionally(ex);
2204 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2205 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2206 >        checkIncomplete(h0);
2207 >        checkIncomplete(h1);
2208 >        rs[0].assertNotInvoked();
2209 >        rs[1].assertNotInvoked();
2210 >        f.completeExceptionally(ex);
2211 >        checkCompletedWithWrappedException(h0, ex);
2212 >        checkCompletedWithWrappedException(h1, ex);
2213 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2214 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2215 >        checkCompletedWithWrappedException(h2, ex);
2216 >        checkCompletedWithWrappedException(h3, ex);
2217 >        g.complete(v1);
2218 >
2219 >        // unspecified behavior - both source completions available
2220 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2221 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2222 >        try {
2223 >            assertEquals(inc(v1), h4.join());
2224 >            rs[4].assertValue(inc(v1));
2225 >        } catch (CompletionException ok) {
2226 >            checkCompletedWithWrappedException(h4, ex);
2227 >            rs[4].assertNotInvoked();
2228 >        }
2229 >        try {
2230 >            assertEquals(inc(v1), h5.join());
2231 >            rs[5].assertValue(inc(v1));
2232 >        } catch (CompletionException ok) {
2233 >            checkCompletedWithWrappedException(h5, ex);
2234 >            rs[5].assertNotInvoked();
2235          }
2236  
2237 <        checkCompletedWithWrappedCFException(h, ex);
2238 <        (!fFirst ? f : g).complete(v1);
2239 <
2240 <        assertEquals(0, r.invocationCount);
2241 <        checkCompletedNormally(!fFirst ? f : g, v1);
2242 <        checkCompletedWithWrappedCFException(fFirst ? f : g, ex);
2243 <        checkCompletedWithWrappedCFException(h, ex);
2237 >        checkCompletedExceptionally(f, ex);
2238 >        checkCompletedNormally(g, v1);
2239 >        checkCompletedWithWrappedException(h0, ex);
2240 >        checkCompletedWithWrappedException(h1, ex);
2241 >        checkCompletedWithWrappedException(h2, ex);
2242 >        checkCompletedWithWrappedException(h3, ex);
2243 >        checkCompletedWithWrappedException(h4, ex);
2244 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2245      }}
2246  
2247      public void testApplyToEither_exceptionalCompletion2() {
2248          for (ExecutionMode m : ExecutionMode.values())
1766        for (boolean reverseArgs : new boolean[] { true, false })
2249          for (boolean fFirst : new boolean[] { true, false })
2250          for (Integer v1 : new Integer[] { 1, null })
2251      {
2252          final CompletableFuture<Integer> f = new CompletableFuture<>();
2253          final CompletableFuture<Integer> g = new CompletableFuture<>();
1772        final IncFunction r1 = new IncFunction(m);
1773        final IncFunction r2 = new IncFunction(m);
2254          final CFException ex = new CFException();
2255 <        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
2256 <        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1777 <        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1778 <        if (fFirst) {
1779 <            f.complete(v1);
1780 <            g.completeExceptionally(ex);
1781 <        } else {
1782 <            g.completeExceptionally(ex);
1783 <            f.complete(v1);
1784 <        }
1785 <        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
2255 >        final IncFunction[] rs = new IncFunction[6];
2256 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2257  
2258 <        // unspecified behavior
2258 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2259 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2260 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2261 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2262 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2263 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2264 >
2265 >        // unspecified behavior - both source completions available
2266 >        try {
2267 >            assertEquals(inc(v1), h0.join());
2268 >            rs[0].assertValue(inc(v1));
2269 >        } catch (CompletionException ok) {
2270 >            checkCompletedWithWrappedException(h0, ex);
2271 >            rs[0].assertNotInvoked();
2272 >        }
2273          try {
2274              assertEquals(inc(v1), h1.join());
2275 <            assertEquals(1, r1.invocationCount);
2275 >            rs[1].assertValue(inc(v1));
2276          } catch (CompletionException ok) {
2277 <            checkCompletedWithWrappedCFException(h1, ex);
2278 <            assertEquals(0, r1.invocationCount);
2277 >            checkCompletedWithWrappedException(h1, ex);
2278 >            rs[1].assertNotInvoked();
2279          }
1795
2280          try {
2281              assertEquals(inc(v1), h2.join());
2282 <            assertEquals(1, r2.invocationCount);
2282 >            rs[2].assertValue(inc(v1));
2283          } catch (CompletionException ok) {
2284 <            checkCompletedWithWrappedCFException(h2, ex);
2285 <            assertEquals(0, r2.invocationCount);
2284 >            checkCompletedWithWrappedException(h2, ex);
2285 >            rs[2].assertNotInvoked();
2286 >        }
2287 >        try {
2288 >            assertEquals(inc(v1), h3.join());
2289 >            rs[3].assertValue(inc(v1));
2290 >        } catch (CompletionException ok) {
2291 >            checkCompletedWithWrappedException(h3, ex);
2292 >            rs[3].assertNotInvoked();
2293          }
2294  
1804        checkCompletedWithWrappedCFException(g, ex);
1805        checkCompletedNormally(f, v1);
1806    }}
1807
1808    /**
1809     * applyToEither result completes exceptionally if action does
1810     */
1811    public void testApplyToEither_actionFailed1() {
1812        for (ExecutionMode m : ExecutionMode.values())
1813        for (Integer v1 : new Integer[] { 1, null })
1814        for (Integer v2 : new Integer[] { 2, null })
1815    {
1816        final CompletableFuture<Integer> f = new CompletableFuture<>();
1817        final CompletableFuture<Integer> g = new CompletableFuture<>();
1818        final FailingFunction r = new FailingFunction(m);
1819        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1820
1821        f.complete(v1);
1822        checkCompletedWithWrappedCFException(h);
1823        g.complete(v2);
1824        checkCompletedNormally(f, v1);
1825        checkCompletedNormally(g, v2);
1826    }}
1827
1828    public void testApplyToEither_actionFailed2() {
1829        for (ExecutionMode m : ExecutionMode.values())
1830        for (Integer v1 : new Integer[] { 1, null })
1831        for (Integer v2 : new Integer[] { 2, null })
1832    {
1833        final CompletableFuture<Integer> f = new CompletableFuture<>();
1834        final CompletableFuture<Integer> g = new CompletableFuture<>();
1835        final FailingFunction r = new FailingFunction(m);
1836        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1837
1838        g.complete(v2);
1839        checkCompletedWithWrappedCFException(h);
1840        f.complete(v1);
2295          checkCompletedNormally(f, v1);
2296 <        checkCompletedNormally(g, v2);
2296 >        checkCompletedExceptionally(g, ex);
2297      }}
2298  
2299      /**
2300       * applyToEither result completes exceptionally if either source cancelled
2301       */
2302 <    public void testApplyToEither_sourceCancelled1() {
2302 >    public void testApplyToEither_sourceCancelled() {
2303          for (ExecutionMode m : ExecutionMode.values())
2304          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1851        for (boolean createIncomplete : new boolean[] { true, false })
1852        for (boolean fFirst : new boolean[] { true, false })
2305          for (Integer v1 : new Integer[] { 1, null })
2306      {
2307          final CompletableFuture<Integer> f = new CompletableFuture<>();
2308          final CompletableFuture<Integer> g = new CompletableFuture<>();
2309 <        final IncFunction r = new IncFunction(m);
2309 >        final IncFunction[] rs = new IncFunction[6];
2310 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2311  
2312 <        if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
2313 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2314 <        if (createIncomplete) {
2315 <            checkIncomplete(h);
2316 <            assertEquals(0, r.invocationCount);
2317 <            assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
2312 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2313 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2314 >        checkIncomplete(h0);
2315 >        checkIncomplete(h1);
2316 >        rs[0].assertNotInvoked();
2317 >        rs[1].assertNotInvoked();
2318 >        f.cancel(mayInterruptIfRunning);
2319 >        checkCompletedWithWrappedCancellationException(h0);
2320 >        checkCompletedWithWrappedCancellationException(h1);
2321 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2322 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2323 >        checkCompletedWithWrappedCancellationException(h2);
2324 >        checkCompletedWithWrappedCancellationException(h3);
2325 >        g.complete(v1);
2326 >
2327 >        // unspecified behavior - both source completions available
2328 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2329 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2330 >        try {
2331 >            assertEquals(inc(v1), h4.join());
2332 >            rs[4].assertValue(inc(v1));
2333 >        } catch (CompletionException ok) {
2334 >            checkCompletedWithWrappedCancellationException(h4);
2335 >            rs[4].assertNotInvoked();
2336 >        }
2337 >        try {
2338 >            assertEquals(inc(v1), h5.join());
2339 >            rs[5].assertValue(inc(v1));
2340 >        } catch (CompletionException ok) {
2341 >            checkCompletedWithWrappedCancellationException(h5);
2342 >            rs[5].assertNotInvoked();
2343          }
2344  
2345 <        checkCompletedWithWrappedCancellationException(h);
2346 <        (!fFirst ? f : g).complete(v1);
2347 <
2348 <        assertEquals(0, r.invocationCount);
2349 <        checkCompletedNormally(!fFirst ? f : g, v1);
2350 <        checkCancelled(fFirst ? f : g);
2351 <        checkCompletedWithWrappedCancellationException(h);
2345 >        checkCancelled(f);
2346 >        checkCompletedNormally(g, v1);
2347 >        checkCompletedWithWrappedCancellationException(h0);
2348 >        checkCompletedWithWrappedCancellationException(h1);
2349 >        checkCompletedWithWrappedCancellationException(h2);
2350 >        checkCompletedWithWrappedCancellationException(h3);
2351 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2352      }}
2353  
2354      public void testApplyToEither_sourceCancelled2() {
2355          for (ExecutionMode m : ExecutionMode.values())
2356          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1879        for (boolean reverseArgs : new boolean[] { true, false })
2357          for (boolean fFirst : new boolean[] { true, false })
2358          for (Integer v1 : new Integer[] { 1, null })
2359      {
2360          final CompletableFuture<Integer> f = new CompletableFuture<>();
2361          final CompletableFuture<Integer> g = new CompletableFuture<>();
2362 <        final IncFunction r1 = new IncFunction(m);
2363 <        final IncFunction r2 = new IncFunction(m);
1887 <        final CFException ex = new CFException();
1888 <        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1889 <        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
2362 >        final IncFunction[] rs = new IncFunction[6];
2363 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2364  
2365 <        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
2366 <        if (fFirst) {
2367 <            f.complete(v1);
2368 <            assertTrue(g.cancel(mayInterruptIfRunning));
2369 <        } else {
2370 <            assertTrue(g.cancel(mayInterruptIfRunning));
1897 <            f.complete(v1);
1898 <        }
1899 <        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
2365 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2366 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2367 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2368 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2369 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2370 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2371  
2372 <        // unspecified behavior
2372 >        // unspecified behavior - both source completions available
2373 >        try {
2374 >            assertEquals(inc(v1), h0.join());
2375 >            rs[0].assertValue(inc(v1));
2376 >        } catch (CompletionException ok) {
2377 >            checkCompletedWithWrappedCancellationException(h0);
2378 >            rs[0].assertNotInvoked();
2379 >        }
2380          try {
2381              assertEquals(inc(v1), h1.join());
2382 <            assertEquals(1, r1.invocationCount);
2382 >            rs[1].assertValue(inc(v1));
2383          } catch (CompletionException ok) {
2384              checkCompletedWithWrappedCancellationException(h1);
2385 <            assertEquals(0, r1.invocationCount);
2385 >            rs[1].assertNotInvoked();
2386          }
1909
2387          try {
2388              assertEquals(inc(v1), h2.join());
2389 <            assertEquals(1, r2.invocationCount);
2389 >            rs[2].assertValue(inc(v1));
2390          } catch (CompletionException ok) {
2391              checkCompletedWithWrappedCancellationException(h2);
2392 <            assertEquals(0, r2.invocationCount);
2392 >            rs[2].assertNotInvoked();
2393 >        }
2394 >        try {
2395 >            assertEquals(inc(v1), h3.join());
2396 >            rs[3].assertValue(inc(v1));
2397 >        } catch (CompletionException ok) {
2398 >            checkCompletedWithWrappedCancellationException(h3);
2399 >            rs[3].assertNotInvoked();
2400          }
2401  
1918        checkCancelled(g);
2402          checkCompletedNormally(f, v1);
2403 +        checkCancelled(g);
2404      }}
2405  
2406      /**
2407 <     * acceptEither result completes normally after normal completion
1924 <     * of either source
2407 >     * applyToEither result completes exceptionally if action does
2408       */
2409 <    public void testAcceptEither_normalCompletion1() {
2409 >    public void testApplyToEither_actionFailed() {
2410          for (ExecutionMode m : ExecutionMode.values())
2411          for (Integer v1 : new Integer[] { 1, null })
2412          for (Integer v2 : new Integer[] { 2, null })
2413      {
2414          final CompletableFuture<Integer> f = new CompletableFuture<>();
2415          final CompletableFuture<Integer> g = new CompletableFuture<>();
2416 <        final IncAction r = new IncAction();
2417 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2416 >        final FailingFunction[] rs = new FailingFunction[6];
2417 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2418  
2419 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2420 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2421          f.complete(v1);
2422 <        checkCompletedNormally(h, null);
2423 <        assertEquals(inc(v1), r.value);
2424 <        g.complete(v2);
2425 <
2426 <        checkCompletedNormally(f, v1);
2427 <        checkCompletedNormally(g, v2);
2428 <        checkCompletedNormally(h, null);
1944 <    }}
1945 <
1946 <    public void testAcceptEither_normalCompletion2() {
1947 <        for (ExecutionMode m : ExecutionMode.values())
1948 <        for (Integer v1 : new Integer[] { 1, null })
1949 <        for (Integer v2 : new Integer[] { 2, null })
1950 <    {
1951 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1952 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1953 <        final IncAction r = new IncAction();
1954 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2422 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2423 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2424 >        checkCompletedWithWrappedCFException(h0);
2425 >        checkCompletedWithWrappedCFException(h1);
2426 >        checkCompletedWithWrappedCFException(h2);
2427 >        checkCompletedWithWrappedCFException(h3);
2428 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2429  
2430          g.complete(v2);
2431 <        checkCompletedNormally(h, null);
2432 <        assertEquals(inc(v2), r.value);
2433 <        f.complete(v1);
2431 >
2432 >        // unspecified behavior - both source completions available
2433 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2434 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2435 >
2436 >        checkCompletedWithWrappedCFException(h4);
2437 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2438 >                   Objects.equals(v2, rs[4].value));
2439 >        checkCompletedWithWrappedCFException(h5);
2440 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2441 >                   Objects.equals(v2, rs[5].value));
2442  
2443          checkCompletedNormally(f, v1);
2444          checkCompletedNormally(g, v2);
1963        checkCompletedNormally(h, null);
2445      }}
2446  
2447 <    public void testAcceptEither_normalCompletion3() {
2447 >    /**
2448 >     * acceptEither result completes normally after normal completion
2449 >     * of either source
2450 >     */
2451 >    public void testAcceptEither_normalCompletion() {
2452          for (ExecutionMode m : ExecutionMode.values())
2453          for (Integer v1 : new Integer[] { 1, null })
2454          for (Integer v2 : new Integer[] { 2, null })
2455      {
2456          final CompletableFuture<Integer> f = new CompletableFuture<>();
2457          final CompletableFuture<Integer> g = new CompletableFuture<>();
2458 <        final IncAction r = new IncAction();
2458 >        final NoopConsumer[] rs = new NoopConsumer[6];
2459 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2460  
2461 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2462 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2463 +        checkIncomplete(h0);
2464 +        checkIncomplete(h1);
2465 +        rs[0].assertNotInvoked();
2466 +        rs[1].assertNotInvoked();
2467          f.complete(v1);
2468 +        checkCompletedNormally(h0, null);
2469 +        checkCompletedNormally(h1, null);
2470 +        rs[0].assertValue(v1);
2471 +        rs[1].assertValue(v1);
2472 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2473 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2474 +        checkCompletedNormally(h2, null);
2475 +        checkCompletedNormally(h3, null);
2476 +        rs[2].assertValue(v1);
2477 +        rs[3].assertValue(v1);
2478          g.complete(v2);
1977        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2479  
2480 <        checkCompletedNormally(h, null);
2480 >        // unspecified behavior - both source completions available
2481 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2482 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2483 >        checkCompletedNormally(h4, null);
2484 >        checkCompletedNormally(h5, null);
2485 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2486 >                   Objects.equals(v2, rs[4].value));
2487 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2488 >                   Objects.equals(v2, rs[5].value));
2489 >
2490          checkCompletedNormally(f, v1);
2491          checkCompletedNormally(g, v2);
2492 <
2493 <        // unspecified behavior
2494 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2495 <                   Objects.equals(r.value, inc(v2)));
2492 >        checkCompletedNormally(h0, null);
2493 >        checkCompletedNormally(h1, null);
2494 >        checkCompletedNormally(h2, null);
2495 >        checkCompletedNormally(h3, null);
2496 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2497      }}
2498  
2499      /**
2500       * acceptEither result completes exceptionally after exceptional
2501       * completion of either source
2502       */
2503 <    public void testAcceptEither_exceptionalCompletion1() {
2503 >    public void testAcceptEither_exceptionalCompletion() {
2504          for (ExecutionMode m : ExecutionMode.values())
2505          for (Integer v1 : new Integer[] { 1, null })
2506      {
2507          final CompletableFuture<Integer> f = new CompletableFuture<>();
2508          final CompletableFuture<Integer> g = new CompletableFuture<>();
1998        final IncAction r = new IncAction();
1999        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2509          final CFException ex = new CFException();
2510 +        final NoopConsumer[] rs = new NoopConsumer[6];
2511 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2512  
2513 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2514 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2515 +        checkIncomplete(h0);
2516 +        checkIncomplete(h1);
2517 +        rs[0].assertNotInvoked();
2518 +        rs[1].assertNotInvoked();
2519          f.completeExceptionally(ex);
2520 <        checkCompletedWithWrappedCFException(h, ex);
2520 >        checkCompletedWithWrappedException(h0, ex);
2521 >        checkCompletedWithWrappedException(h1, ex);
2522 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2523 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2524 >        checkCompletedWithWrappedException(h2, ex);
2525 >        checkCompletedWithWrappedException(h3, ex);
2526 >
2527          g.complete(v1);
2528  
2529 <        assertEquals(0, r.invocationCount);
2529 >        // unspecified behavior - both source completions available
2530 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2531 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2532 >        try {
2533 >            assertNull(h4.join());
2534 >            rs[4].assertValue(v1);
2535 >        } catch (CompletionException ok) {
2536 >            checkCompletedWithWrappedException(h4, ex);
2537 >            rs[4].assertNotInvoked();
2538 >        }
2539 >        try {
2540 >            assertNull(h5.join());
2541 >            rs[5].assertValue(v1);
2542 >        } catch (CompletionException ok) {
2543 >            checkCompletedWithWrappedException(h5, ex);
2544 >            rs[5].assertNotInvoked();
2545 >        }
2546 >
2547 >        checkCompletedExceptionally(f, ex);
2548          checkCompletedNormally(g, v1);
2549 <        checkCompletedWithWrappedCFException(f, ex);
2550 <        checkCompletedWithWrappedCFException(h, ex);
2549 >        checkCompletedWithWrappedException(h0, ex);
2550 >        checkCompletedWithWrappedException(h1, ex);
2551 >        checkCompletedWithWrappedException(h2, ex);
2552 >        checkCompletedWithWrappedException(h3, ex);
2553 >        checkCompletedWithWrappedException(h4, ex);
2554 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2555      }}
2556  
2557      public void testAcceptEither_exceptionalCompletion2() {
2558          for (ExecutionMode m : ExecutionMode.values())
2559 +        for (boolean fFirst : new boolean[] { true, false })
2560          for (Integer v1 : new Integer[] { 1, null })
2561      {
2562          final CompletableFuture<Integer> f = new CompletableFuture<>();
2563          final CompletableFuture<Integer> g = new CompletableFuture<>();
2018        final IncAction r = new IncAction();
2019        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2020        final CFException ex = new CFException();
2021
2022        g.completeExceptionally(ex);
2023        checkCompletedWithWrappedCFException(h, ex);
2024        f.complete(v1);
2025
2026        assertEquals(0, r.invocationCount);
2027        checkCompletedNormally(f, v1);
2028        checkCompletedWithWrappedCFException(g, ex);
2029        checkCompletedWithWrappedCFException(h, ex);
2030    }}
2031
2032    public void testAcceptEither_exceptionalCompletion3() {
2033        for (ExecutionMode m : ExecutionMode.values())
2034        for (Integer v1 : new Integer[] { 1, null })
2035    {
2036        final CompletableFuture<Integer> f = new CompletableFuture<>();
2037        final CompletableFuture<Integer> g = new CompletableFuture<>();
2038        final IncAction r = new IncAction();
2564          final CFException ex = new CFException();
2565 +        final NoopConsumer[] rs = new NoopConsumer[6];
2566 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2567  
2568 <        g.completeExceptionally(ex);
2569 <        f.complete(v1);
2570 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2568 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2569 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2570 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2571 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2572 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2573 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2574  
2575 <        // unspecified behavior
2046 <        Integer v;
2575 >        // unspecified behavior - both source completions available
2576          try {
2577 <            assertNull(h.join());
2578 <            assertEquals(1, r.invocationCount);
2050 <            assertEquals(inc(v1), r.value);
2577 >            assertEquals(null, h0.join());
2578 >            rs[0].assertValue(v1);
2579          } catch (CompletionException ok) {
2580 <            checkCompletedWithWrappedCFException(h, ex);
2581 <            assertEquals(0, r.invocationCount);
2580 >            checkCompletedWithWrappedException(h0, ex);
2581 >            rs[0].assertNotInvoked();
2582          }
2055
2056        checkCompletedWithWrappedCFException(g, ex);
2057        checkCompletedNormally(f, v1);
2058    }}
2059
2060    public void testAcceptEither_exceptionalCompletion4() {
2061        for (ExecutionMode m : ExecutionMode.values())
2062        for (Integer v1 : new Integer[] { 1, null })
2063    {
2064        final CompletableFuture<Integer> f = new CompletableFuture<>();
2065        final CompletableFuture<Integer> g = new CompletableFuture<>();
2066        final IncAction r = new IncAction();
2067        final CFException ex = new CFException();
2068
2069        f.completeExceptionally(ex);
2070        g.complete(v1);
2071        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2072
2073        // unspecified behavior
2074        Integer v;
2583          try {
2584 <            assertNull(h.join());
2585 <            assertEquals(1, r.invocationCount);
2078 <            assertEquals(inc(v1), r.value);
2584 >            assertEquals(null, h1.join());
2585 >            rs[1].assertValue(v1);
2586          } catch (CompletionException ok) {
2587 <            checkCompletedWithWrappedCFException(h, ex);
2588 <            assertEquals(0, r.invocationCount);
2587 >            checkCompletedWithWrappedException(h1, ex);
2588 >            rs[1].assertNotInvoked();
2589 >        }
2590 >        try {
2591 >            assertEquals(null, h2.join());
2592 >            rs[2].assertValue(v1);
2593 >        } catch (CompletionException ok) {
2594 >            checkCompletedWithWrappedException(h2, ex);
2595 >            rs[2].assertNotInvoked();
2596 >        }
2597 >        try {
2598 >            assertEquals(null, h3.join());
2599 >            rs[3].assertValue(v1);
2600 >        } catch (CompletionException ok) {
2601 >            checkCompletedWithWrappedException(h3, ex);
2602 >            rs[3].assertNotInvoked();
2603          }
2604  
2084        checkCompletedWithWrappedCFException(f, ex);
2085        checkCompletedNormally(g, v1);
2086    }}
2087
2088    /**
2089     * acceptEither result completes exceptionally if action does
2090     */
2091    public void testAcceptEither_actionFailed1() {
2092        for (ExecutionMode m : ExecutionMode.values())
2093        for (Integer v1 : new Integer[] { 1, null })
2094        for (Integer v2 : new Integer[] { 2, null })
2095    {
2096        final CompletableFuture<Integer> f = new CompletableFuture<>();
2097        final CompletableFuture<Integer> g = new CompletableFuture<>();
2098        final FailingConsumer r = new FailingConsumer(m);
2099        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2100
2101        f.complete(v1);
2102        checkCompletedWithWrappedCFException(h);
2103        g.complete(v2);
2104        checkCompletedNormally(f, v1);
2105        checkCompletedNormally(g, v2);
2106    }}
2107
2108    public void testAcceptEither_actionFailed2() {
2109        for (ExecutionMode m : ExecutionMode.values())
2110        for (Integer v1 : new Integer[] { 1, null })
2111        for (Integer v2 : new Integer[] { 2, null })
2112    {
2113        final CompletableFuture<Integer> f = new CompletableFuture<>();
2114        final CompletableFuture<Integer> g = new CompletableFuture<>();
2115        final FailingConsumer r = new FailingConsumer(m);
2116        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2117
2118        g.complete(v2);
2119        checkCompletedWithWrappedCFException(h);
2120        f.complete(v1);
2605          checkCompletedNormally(f, v1);
2606 <        checkCompletedNormally(g, v2);
2606 >        checkCompletedExceptionally(g, ex);
2607      }}
2608  
2609      /**
2610       * acceptEither result completes exceptionally if either source cancelled
2611       */
2612 <    public void testAcceptEither_sourceCancelled1() {
2129 <        for (ExecutionMode m : ExecutionMode.values())
2130 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2131 <        for (Integer v1 : new Integer[] { 1, null })
2132 <    {
2133 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2134 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2135 <        final IncAction r = new IncAction();
2136 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2137 <
2138 <        assertTrue(f.cancel(mayInterruptIfRunning));
2139 <        checkCompletedWithWrappedCancellationException(h);
2140 <        g.complete(v1);
2141 <
2142 <        checkCancelled(f);
2143 <        assertEquals(0, r.invocationCount);
2144 <        checkCompletedNormally(g, v1);
2145 <        checkCompletedWithWrappedCancellationException(h);
2146 <    }}
2147 <
2148 <    public void testAcceptEither_sourceCancelled2() {
2612 >    public void testAcceptEither_sourceCancelled() {
2613          for (ExecutionMode m : ExecutionMode.values())
2614          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2615          for (Integer v1 : new Integer[] { 1, null })
2616      {
2617          final CompletableFuture<Integer> f = new CompletableFuture<>();
2618          final CompletableFuture<Integer> g = new CompletableFuture<>();
2619 <        final IncAction r = new IncAction();
2620 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2157 <
2158 <        assertTrue(g.cancel(mayInterruptIfRunning));
2159 <        checkCompletedWithWrappedCancellationException(h);
2160 <        f.complete(v1);
2619 >        final NoopConsumer[] rs = new NoopConsumer[6];
2620 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2621  
2622 <        checkCancelled(g);
2623 <        assertEquals(0, r.invocationCount);
2624 <        checkCompletedNormally(f, v1);
2625 <        checkCompletedWithWrappedCancellationException(h);
2626 <    }}
2622 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2623 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2624 >        checkIncomplete(h0);
2625 >        checkIncomplete(h1);
2626 >        rs[0].assertNotInvoked();
2627 >        rs[1].assertNotInvoked();
2628 >        f.cancel(mayInterruptIfRunning);
2629 >        checkCompletedWithWrappedCancellationException(h0);
2630 >        checkCompletedWithWrappedCancellationException(h1);
2631 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2632 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2633 >        checkCompletedWithWrappedCancellationException(h2);
2634 >        checkCompletedWithWrappedCancellationException(h3);
2635  
2636 <    public void testAcceptEither_sourceCancelled3() {
2169 <        for (ExecutionMode m : ExecutionMode.values())
2170 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2171 <        for (Integer v1 : new Integer[] { 1, null })
2172 <    {
2173 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2174 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2175 <        final IncAction r = new IncAction();
2176 <
2177 <        assertTrue(g.cancel(mayInterruptIfRunning));
2178 <        f.complete(v1);
2179 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2636 >        g.complete(v1);
2637  
2638 <        // unspecified behavior
2639 <        Integer v;
2638 >        // unspecified behavior - both source completions available
2639 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2640 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2641          try {
2642 <            assertNull(h.join());
2643 <            assertEquals(1, r.invocationCount);
2186 <            assertEquals(inc(v1), r.value);
2642 >            assertNull(h4.join());
2643 >            rs[4].assertValue(v1);
2644          } catch (CompletionException ok) {
2645 <            checkCompletedWithWrappedCancellationException(h);
2646 <            assertEquals(0, r.invocationCount);
2645 >            checkCompletedWithWrappedCancellationException(h4);
2646 >            rs[4].assertNotInvoked();
2647          }
2191
2192        checkCancelled(g);
2193        checkCompletedNormally(f, v1);
2194    }}
2195
2196    public void testAcceptEither_sourceCancelled4() {
2197        for (ExecutionMode m : ExecutionMode.values())
2198        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2199        for (Integer v1 : new Integer[] { 1, null })
2200    {
2201        final CompletableFuture<Integer> f = new CompletableFuture<>();
2202        final CompletableFuture<Integer> g = new CompletableFuture<>();
2203        final IncAction r = new IncAction();
2204
2205        assertTrue(f.cancel(mayInterruptIfRunning));
2206        g.complete(v1);
2207        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2208
2209        // unspecified behavior
2210        Integer v;
2648          try {
2649 <            assertNull(h.join());
2650 <            assertEquals(1, r.invocationCount);
2214 <            assertEquals(inc(v1), r.value);
2649 >            assertNull(h5.join());
2650 >            rs[5].assertValue(v1);
2651          } catch (CompletionException ok) {
2652 <            checkCompletedWithWrappedCancellationException(h);
2653 <            assertEquals(0, r.invocationCount);
2652 >            checkCompletedWithWrappedCancellationException(h5);
2653 >            rs[5].assertNotInvoked();
2654          }
2655  
2656          checkCancelled(f);
2657          checkCompletedNormally(g, v1);
2658 +        checkCompletedWithWrappedCancellationException(h0);
2659 +        checkCompletedWithWrappedCancellationException(h1);
2660 +        checkCompletedWithWrappedCancellationException(h2);
2661 +        checkCompletedWithWrappedCancellationException(h3);
2662 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2663      }}
2664  
2665      /**
2666 <     * runAfterEither result completes normally after normal completion
2226 <     * of either source
2666 >     * acceptEither result completes exceptionally if action does
2667       */
2668 <    public void testRunAfterEither_normalCompletion1() {
2668 >    public void testAcceptEither_actionFailed() {
2669          for (ExecutionMode m : ExecutionMode.values())
2670          for (Integer v1 : new Integer[] { 1, null })
2671          for (Integer v2 : new Integer[] { 2, null })
2672      {
2673          final CompletableFuture<Integer> f = new CompletableFuture<>();
2674          final CompletableFuture<Integer> g = new CompletableFuture<>();
2675 <        final Noop r = new Noop(m);
2676 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2675 >        final FailingConsumer[] rs = new FailingConsumer[6];
2676 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2677  
2678 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2679 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2680          f.complete(v1);
2681 <        checkCompletedNormally(h, null);
2682 <        assertEquals(1, r.invocationCount);
2681 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2682 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2683 >        checkCompletedWithWrappedCFException(h0);
2684 >        checkCompletedWithWrappedCFException(h1);
2685 >        checkCompletedWithWrappedCFException(h2);
2686 >        checkCompletedWithWrappedCFException(h3);
2687 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2688 >
2689          g.complete(v2);
2690  
2691 +        // unspecified behavior - both source completions available
2692 +        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2693 +        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2694 +
2695 +        checkCompletedWithWrappedCFException(h4);
2696 +        assertTrue(Objects.equals(v1, rs[4].value) ||
2697 +                   Objects.equals(v2, rs[4].value));
2698 +        checkCompletedWithWrappedCFException(h5);
2699 +        assertTrue(Objects.equals(v1, rs[5].value) ||
2700 +                   Objects.equals(v2, rs[5].value));
2701 +
2702          checkCompletedNormally(f, v1);
2703          checkCompletedNormally(g, v2);
2245        checkCompletedNormally(h, null);
2246        assertEquals(1, r.invocationCount);
2704      }}
2705  
2706 <    public void testRunAfterEither_normalCompletion2() {
2706 >    /**
2707 >     * runAfterEither result completes normally after normal completion
2708 >     * of either source
2709 >     */
2710 >    public void testRunAfterEither_normalCompletion() {
2711          for (ExecutionMode m : ExecutionMode.values())
2712          for (Integer v1 : new Integer[] { 1, null })
2713          for (Integer v2 : new Integer[] { 2, null })
2714      {
2715          final CompletableFuture<Integer> f = new CompletableFuture<>();
2716          final CompletableFuture<Integer> g = new CompletableFuture<>();
2717 <        final Noop r = new Noop(m);
2718 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2717 >        final Noop[] rs = new Noop[6];
2718 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2719  
2720 <        g.complete(v2);
2721 <        checkCompletedNormally(h, null);
2722 <        assertEquals(1, r.invocationCount);
2720 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2721 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2722 >        checkIncomplete(h0);
2723 >        checkIncomplete(h1);
2724 >        rs[0].assertNotInvoked();
2725 >        rs[1].assertNotInvoked();
2726          f.complete(v1);
2727 +        checkCompletedNormally(h0, null);
2728 +        checkCompletedNormally(h1, null);
2729 +        rs[0].assertInvoked();
2730 +        rs[1].assertInvoked();
2731 +        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2732 +        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2733 +        checkCompletedNormally(h2, null);
2734 +        checkCompletedNormally(h3, null);
2735 +        rs[2].assertInvoked();
2736 +        rs[3].assertInvoked();
2737  
2264        checkCompletedNormally(f, v1);
2265        checkCompletedNormally(g, v2);
2266        checkCompletedNormally(h, null);
2267        assertEquals(1, r.invocationCount);
2268        }}
2269
2270    public void testRunAfterEither_normalCompletion3() {
2271        for (ExecutionMode m : ExecutionMode.values())
2272        for (Integer v1 : new Integer[] { 1, null })
2273        for (Integer v2 : new Integer[] { 2, null })
2274    {
2275        final CompletableFuture<Integer> f = new CompletableFuture<>();
2276        final CompletableFuture<Integer> g = new CompletableFuture<>();
2277        final Noop r = new Noop(m);
2278
2279        f.complete(v1);
2738          g.complete(v2);
2281        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2739  
2740 <        checkCompletedNormally(h, null);
2740 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2741 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2742 >
2743          checkCompletedNormally(f, v1);
2744          checkCompletedNormally(g, v2);
2745 <        assertEquals(1, r.invocationCount);
2745 >        checkCompletedNormally(h0, null);
2746 >        checkCompletedNormally(h1, null);
2747 >        checkCompletedNormally(h2, null);
2748 >        checkCompletedNormally(h3, null);
2749 >        checkCompletedNormally(h4, null);
2750 >        checkCompletedNormally(h5, null);
2751 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2752      }}
2753  
2754      /**
2755       * runAfterEither result completes exceptionally after exceptional
2756       * completion of either source
2757       */
2758 <    public void testRunAfterEither_exceptionalCompletion1() {
2294 <        for (ExecutionMode m : ExecutionMode.values())
2295 <        for (Integer v1 : new Integer[] { 1, null })
2296 <    {
2297 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2298 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2299 <        final Noop r = new Noop(m);
2300 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2301 <        final CFException ex = new CFException();
2302 <
2303 <        f.completeExceptionally(ex);
2304 <        checkCompletedWithWrappedCFException(h, ex);
2305 <        g.complete(v1);
2306 <
2307 <        assertEquals(0, r.invocationCount);
2308 <        checkCompletedNormally(g, v1);
2309 <        checkCompletedWithWrappedCFException(f, ex);
2310 <        checkCompletedWithWrappedCFException(h, ex);
2311 <    }}
2312 <
2313 <    public void testRunAfterEither_exceptionalCompletion2() {
2758 >    public void testRunAfterEither_exceptionalCompletion() {
2759          for (ExecutionMode m : ExecutionMode.values())
2760          for (Integer v1 : new Integer[] { 1, null })
2761      {
2762          final CompletableFuture<Integer> f = new CompletableFuture<>();
2763          final CompletableFuture<Integer> g = new CompletableFuture<>();
2319        final Noop r = new Noop(m);
2320        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2764          final CFException ex = new CFException();
2765 +        final Noop[] rs = new Noop[6];
2766 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2767  
2768 <        g.completeExceptionally(ex);
2769 <        checkCompletedWithWrappedCFException(h, ex);
2770 <        f.complete(v1);
2768 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2769 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2770 >        checkIncomplete(h0);
2771 >        checkIncomplete(h1);
2772 >        rs[0].assertNotInvoked();
2773 >        rs[1].assertNotInvoked();
2774 >        assertTrue(f.completeExceptionally(ex));
2775 >        checkCompletedWithWrappedException(h0, ex);
2776 >        checkCompletedWithWrappedException(h1, ex);
2777 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2778 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2779 >        checkCompletedWithWrappedException(h2, ex);
2780 >        checkCompletedWithWrappedException(h3, ex);
2781  
2782 <        assertEquals(0, r.invocationCount);
2328 <        checkCompletedNormally(f, v1);
2329 <        checkCompletedWithWrappedCFException(g, ex);
2330 <        checkCompletedWithWrappedCFException(h, ex);
2331 <    }}
2782 >        assertTrue(g.complete(v1));
2783  
2784 <    public void testRunAfterEither_exceptionalCompletion3() {
2785 <        for (ExecutionMode m : ExecutionMode.values())
2786 <        for (Integer v1 : new Integer[] { 1, null })
2336 <    {
2337 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2338 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2339 <        final Noop r = new Noop(m);
2340 <        final CFException ex = new CFException();
2341 <
2342 <        g.completeExceptionally(ex);
2343 <        f.complete(v1);
2344 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2345 <
2346 <        // unspecified behavior
2347 <        Integer v;
2784 >        // unspecified behavior - both source completions available
2785 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2786 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2787          try {
2788 <            assertNull(h.join());
2789 <            assertEquals(1, r.invocationCount);
2788 >            assertNull(h4.join());
2789 >            rs[4].assertInvoked();
2790          } catch (CompletionException ok) {
2791 <            checkCompletedWithWrappedCFException(h, ex);
2792 <            assertEquals(0, r.invocationCount);
2791 >            checkCompletedWithWrappedException(h4, ex);
2792 >            rs[4].assertNotInvoked();
2793          }
2355
2356        checkCompletedWithWrappedCFException(g, ex);
2357        checkCompletedNormally(f, v1);
2358    }}
2359
2360    public void testRunAfterEither_exceptionalCompletion4() {
2361        for (ExecutionMode m : ExecutionMode.values())
2362        for (Integer v1 : new Integer[] { 1, null })
2363    {
2364        final CompletableFuture<Integer> f = new CompletableFuture<>();
2365        final CompletableFuture<Integer> g = new CompletableFuture<>();
2366        final Noop r = new Noop(m);
2367        final CFException ex = new CFException();
2368
2369        f.completeExceptionally(ex);
2370        g.complete(v1);
2371        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2372
2373        // unspecified behavior
2374        Integer v;
2794          try {
2795 <            assertNull(h.join());
2796 <            assertEquals(1, r.invocationCount);
2795 >            assertNull(h5.join());
2796 >            rs[5].assertInvoked();
2797          } catch (CompletionException ok) {
2798 <            checkCompletedWithWrappedCFException(h, ex);
2799 <            assertEquals(0, r.invocationCount);
2798 >            checkCompletedWithWrappedException(h5, ex);
2799 >            rs[5].assertNotInvoked();
2800          }
2801  
2802 <        checkCompletedWithWrappedCFException(f, ex);
2802 >        checkCompletedExceptionally(f, ex);
2803          checkCompletedNormally(g, v1);
2804 +        checkCompletedWithWrappedException(h0, ex);
2805 +        checkCompletedWithWrappedException(h1, ex);
2806 +        checkCompletedWithWrappedException(h2, ex);
2807 +        checkCompletedWithWrappedException(h3, ex);
2808 +        checkCompletedWithWrappedException(h4, ex);
2809 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2810      }}
2811  
2812 <    /**
2388 <     * runAfterEither result completes exceptionally if action does
2389 <     */
2390 <    public void testRunAfterEither_actionFailed1() {
2812 >    public void testRunAfterEither_exceptionalCompletion2() {
2813          for (ExecutionMode m : ExecutionMode.values())
2814 +        for (boolean fFirst : new boolean[] { true, false })
2815          for (Integer v1 : new Integer[] { 1, null })
2393        for (Integer v2 : new Integer[] { 2, null })
2816      {
2817          final CompletableFuture<Integer> f = new CompletableFuture<>();
2818          final CompletableFuture<Integer> g = new CompletableFuture<>();
2819 <        final FailingRunnable r = new FailingRunnable(m);
2820 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2819 >        final CFException ex = new CFException();
2820 >        final Noop[] rs = new Noop[6];
2821 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2822  
2823 <        f.complete(v1);
2824 <        checkCompletedWithWrappedCFException(h);
2825 <        g.complete(v2);
2826 <        checkCompletedNormally(f, v1);
2827 <        checkCompletedNormally(g, v2);
2828 <    }}
2823 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2824 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2825 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2826 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2827 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2828 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2829  
2830 <    public void testRunAfterEither_actionFailed2() {
2831 <        for (ExecutionMode m : ExecutionMode.values())
2832 <        for (Integer v1 : new Integer[] { 1, null })
2833 <        for (Integer v2 : new Integer[] { 2, null })
2834 <    {
2835 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2836 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2837 <        final FailingRunnable r = new FailingRunnable(m);
2838 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2830 >        // unspecified behavior - both source completions available
2831 >        try {
2832 >            assertEquals(null, h0.join());
2833 >            rs[0].assertInvoked();
2834 >        } catch (CompletionException ok) {
2835 >            checkCompletedWithWrappedException(h0, ex);
2836 >            rs[0].assertNotInvoked();
2837 >        }
2838 >        try {
2839 >            assertEquals(null, h1.join());
2840 >            rs[1].assertInvoked();
2841 >        } catch (CompletionException ok) {
2842 >            checkCompletedWithWrappedException(h1, ex);
2843 >            rs[1].assertNotInvoked();
2844 >        }
2845 >        try {
2846 >            assertEquals(null, h2.join());
2847 >            rs[2].assertInvoked();
2848 >        } catch (CompletionException ok) {
2849 >            checkCompletedWithWrappedException(h2, ex);
2850 >            rs[2].assertNotInvoked();
2851 >        }
2852 >        try {
2853 >            assertEquals(null, h3.join());
2854 >            rs[3].assertInvoked();
2855 >        } catch (CompletionException ok) {
2856 >            checkCompletedWithWrappedException(h3, ex);
2857 >            rs[3].assertNotInvoked();
2858 >        }
2859  
2417        g.complete(v2);
2418        checkCompletedWithWrappedCFException(h);
2419        f.complete(v1);
2860          checkCompletedNormally(f, v1);
2861 <        checkCompletedNormally(g, v2);
2861 >        checkCompletedExceptionally(g, ex);
2862      }}
2863  
2864      /**
2865       * runAfterEither result completes exceptionally if either source cancelled
2866       */
2867 <    public void testRunAfterEither_sourceCancelled1() {
2428 <        for (ExecutionMode m : ExecutionMode.values())
2429 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2430 <        for (Integer v1 : new Integer[] { 1, null })
2431 <    {
2432 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2433 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2434 <        final Noop r = new Noop(m);
2435 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2436 <
2437 <        assertTrue(f.cancel(mayInterruptIfRunning));
2438 <        checkCompletedWithWrappedCancellationException(h);
2439 <        g.complete(v1);
2440 <
2441 <        checkCancelled(f);
2442 <        assertEquals(0, r.invocationCount);
2443 <        checkCompletedNormally(g, v1);
2444 <        checkCompletedWithWrappedCancellationException(h);
2445 <    }}
2446 <
2447 <    public void testRunAfterEither_sourceCancelled2() {
2867 >    public void testRunAfterEither_sourceCancelled() {
2868          for (ExecutionMode m : ExecutionMode.values())
2869          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2870          for (Integer v1 : new Integer[] { 1, null })
2871      {
2872          final CompletableFuture<Integer> f = new CompletableFuture<>();
2873          final CompletableFuture<Integer> g = new CompletableFuture<>();
2874 <        final Noop r = new Noop(m);
2875 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2456 <
2457 <        assertTrue(g.cancel(mayInterruptIfRunning));
2458 <        checkCompletedWithWrappedCancellationException(h);
2459 <        f.complete(v1);
2874 >        final Noop[] rs = new Noop[6];
2875 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2876  
2877 <        checkCancelled(g);
2878 <        assertEquals(0, r.invocationCount);
2879 <        checkCompletedNormally(f, v1);
2880 <        checkCompletedWithWrappedCancellationException(h);
2881 <    }}
2882 <
2883 <    public void testRunAfterEither_sourceCancelled3() {
2884 <        for (ExecutionMode m : ExecutionMode.values())
2885 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2886 <        for (Integer v1 : new Integer[] { 1, null })
2887 <    {
2888 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2889 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2890 <        final Noop r = new Noop(m);
2891 <
2892 <        assertTrue(g.cancel(mayInterruptIfRunning));
2893 <        f.complete(v1);
2894 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2895 <
2480 <        // unspecified behavior
2481 <        Integer v;
2877 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2878 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2879 >        checkIncomplete(h0);
2880 >        checkIncomplete(h1);
2881 >        rs[0].assertNotInvoked();
2882 >        rs[1].assertNotInvoked();
2883 >        f.cancel(mayInterruptIfRunning);
2884 >        checkCompletedWithWrappedCancellationException(h0);
2885 >        checkCompletedWithWrappedCancellationException(h1);
2886 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2887 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2888 >        checkCompletedWithWrappedCancellationException(h2);
2889 >        checkCompletedWithWrappedCancellationException(h3);
2890 >
2891 >        assertTrue(g.complete(v1));
2892 >
2893 >        // unspecified behavior - both source completions available
2894 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2895 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2896          try {
2897 <            assertNull(h.join());
2898 <            assertEquals(1, r.invocationCount);
2897 >            assertNull(h4.join());
2898 >            rs[4].assertInvoked();
2899          } catch (CompletionException ok) {
2900 <            checkCompletedWithWrappedCancellationException(h);
2901 <            assertEquals(0, r.invocationCount);
2900 >            checkCompletedWithWrappedCancellationException(h4);
2901 >            rs[4].assertNotInvoked();
2902 >        }
2903 >        try {
2904 >            assertNull(h5.join());
2905 >            rs[5].assertInvoked();
2906 >        } catch (CompletionException ok) {
2907 >            checkCompletedWithWrappedCancellationException(h5);
2908 >            rs[5].assertNotInvoked();
2909          }
2910  
2911 <        checkCancelled(g);
2912 <        checkCompletedNormally(f, v1);
2911 >        checkCancelled(f);
2912 >        checkCompletedNormally(g, v1);
2913 >        checkCompletedWithWrappedCancellationException(h0);
2914 >        checkCompletedWithWrappedCancellationException(h1);
2915 >        checkCompletedWithWrappedCancellationException(h2);
2916 >        checkCompletedWithWrappedCancellationException(h3);
2917 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2918      }}
2919  
2920 <    public void testRunAfterEither_sourceCancelled4() {
2920 >    /**
2921 >     * runAfterEither result completes exceptionally if action does
2922 >     */
2923 >    public void testRunAfterEither_actionFailed() {
2924          for (ExecutionMode m : ExecutionMode.values())
2496        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2925          for (Integer v1 : new Integer[] { 1, null })
2926 +        for (Integer v2 : new Integer[] { 2, null })
2927      {
2928          final CompletableFuture<Integer> f = new CompletableFuture<>();
2929          final CompletableFuture<Integer> g = new CompletableFuture<>();
2930 <        final Noop r = new Noop(m);
2931 <
2503 <        assertTrue(f.cancel(mayInterruptIfRunning));
2504 <        g.complete(v1);
2505 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2930 >        final FailingRunnable[] rs = new FailingRunnable[6];
2931 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2932  
2933 <        // unspecified behavior
2934 <        Integer v;
2935 <        try {
2936 <            assertNull(h.join());
2937 <            assertEquals(1, r.invocationCount);
2938 <        } catch (CompletionException ok) {
2939 <            checkCompletedWithWrappedCancellationException(h);
2940 <            assertEquals(0, r.invocationCount);
2941 <        }
2933 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2934 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2935 >        assertTrue(f.complete(v1));
2936 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2937 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2938 >        checkCompletedWithWrappedCFException(h0);
2939 >        checkCompletedWithWrappedCFException(h1);
2940 >        checkCompletedWithWrappedCFException(h2);
2941 >        checkCompletedWithWrappedCFException(h3);
2942 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2943 >        assertTrue(g.complete(v2));
2944 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2945 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2946 >        checkCompletedWithWrappedCFException(h4);
2947 >        checkCompletedWithWrappedCFException(h5);
2948  
2949 <        checkCancelled(f);
2950 <        checkCompletedNormally(g, v1);
2949 >        checkCompletedNormally(f, v1);
2950 >        checkCompletedNormally(g, v2);
2951 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2952      }}
2953  
2954      /**
# Line 2528 | Line 2961 | public class CompletableFutureTest exten
2961      {
2962          final CompletableFuture<Integer> f = new CompletableFuture<>();
2963          final CompletableFutureInc r = new CompletableFutureInc(m);
2964 <        if (!createIncomplete) f.complete(v1);
2964 >        if (!createIncomplete) assertTrue(f.complete(v1));
2965          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2966 <        if (createIncomplete) f.complete(v1);
2966 >        if (createIncomplete) assertTrue(f.complete(v1));
2967  
2968          checkCompletedNormally(g, inc(v1));
2969          checkCompletedNormally(f, v1);
2970 <        assertEquals(1, r.invocationCount);
2970 >        r.assertValue(v1);
2971      }}
2972  
2973      /**
# Line 2552 | Line 2985 | public class CompletableFutureTest exten
2985          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2986          if (createIncomplete) f.completeExceptionally(ex);
2987  
2988 <        checkCompletedWithWrappedCFException(g, ex);
2989 <        checkCompletedWithWrappedCFException(f, ex);
2990 <        assertEquals(0, r.invocationCount);
2988 >        checkCompletedWithWrappedException(g, ex);
2989 >        checkCompletedExceptionally(f, ex);
2990 >        r.assertNotInvoked();
2991      }}
2992  
2993      /**
# Line 2568 | Line 3001 | public class CompletableFutureTest exten
3001          final CompletableFuture<Integer> f = new CompletableFuture<>();
3002          final FailingCompletableFutureFunction r
3003              = new FailingCompletableFutureFunction(m);
3004 <        if (!createIncomplete) f.complete(v1);
3004 >        if (!createIncomplete) assertTrue(f.complete(v1));
3005          final CompletableFuture<Integer> g = m.thenCompose(f, r);
3006 <        if (createIncomplete) f.complete(v1);
3006 >        if (createIncomplete) assertTrue(f.complete(v1));
3007  
3008          checkCompletedWithWrappedCFException(g);
3009          checkCompletedNormally(f, v1);
# Line 2597 | Line 3030 | public class CompletableFutureTest exten
3030          checkCancelled(f);
3031      }}
3032  
3033 +    /**
3034 +     * thenCompose result completes exceptionally if the result of the action does
3035 +     */
3036 +    public void testThenCompose_actionReturnsFailingFuture() {
3037 +        for (ExecutionMode m : ExecutionMode.values())
3038 +        for (int order = 0; order < 6; order++)
3039 +        for (Integer v1 : new Integer[] { 1, null })
3040 +    {
3041 +        final CFException ex = new CFException();
3042 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3043 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
3044 +        final CompletableFuture<Integer> h;
3045 +        // Test all permutations of orders
3046 +        switch (order) {
3047 +        case 0:
3048 +            assertTrue(f.complete(v1));
3049 +            assertTrue(g.completeExceptionally(ex));
3050 +            h = m.thenCompose(f, (x -> g));
3051 +            break;
3052 +        case 1:
3053 +            assertTrue(f.complete(v1));
3054 +            h = m.thenCompose(f, (x -> g));
3055 +            assertTrue(g.completeExceptionally(ex));
3056 +            break;
3057 +        case 2:
3058 +            assertTrue(g.completeExceptionally(ex));
3059 +            assertTrue(f.complete(v1));
3060 +            h = m.thenCompose(f, (x -> g));
3061 +            break;
3062 +        case 3:
3063 +            assertTrue(g.completeExceptionally(ex));
3064 +            h = m.thenCompose(f, (x -> g));
3065 +            assertTrue(f.complete(v1));
3066 +            break;
3067 +        case 4:
3068 +            h = m.thenCompose(f, (x -> g));
3069 +            assertTrue(f.complete(v1));
3070 +            assertTrue(g.completeExceptionally(ex));
3071 +            break;
3072 +        case 5:
3073 +            h = m.thenCompose(f, (x -> g));
3074 +            assertTrue(f.complete(v1));
3075 +            assertTrue(g.completeExceptionally(ex));
3076 +            break;
3077 +        default: throw new AssertionError();
3078 +        }
3079 +
3080 +        checkCompletedExceptionally(g, ex);
3081 +        checkCompletedWithWrappedException(h, ex);
3082 +        checkCompletedNormally(f, v1);
3083 +    }}
3084 +
3085      // other static methods
3086  
3087      /**
# Line 2613 | Line 3098 | public class CompletableFutureTest exten
3098       * when all components complete normally
3099       */
3100      public void testAllOf_normal() throws Exception {
3101 <        for (int k = 1; k < 20; ++k) {
3102 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3103 <            for (int i = 0; i < k; ++i)
3101 >        for (int k = 1; k < 10; k++) {
3102 >            CompletableFuture<Integer>[] fs
3103 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3104 >            for (int i = 0; i < k; i++)
3105 >                fs[i] = new CompletableFuture<>();
3106 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3107 >            for (int i = 0; i < k; i++) {
3108 >                checkIncomplete(f);
3109 >                checkIncomplete(CompletableFuture.allOf(fs));
3110 >                fs[i].complete(one);
3111 >            }
3112 >            checkCompletedNormally(f, null);
3113 >            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3114 >        }
3115 >    }
3116 >
3117 >    public void testAllOf_backwards() throws Exception {
3118 >        for (int k = 1; k < 10; k++) {
3119 >            CompletableFuture<Integer>[] fs
3120 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3121 >            for (int i = 0; i < k; i++)
3122                  fs[i] = new CompletableFuture<>();
3123              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3124 <            for (int i = 0; i < k; ++i) {
3124 >            for (int i = k - 1; i >= 0; i--) {
3125                  checkIncomplete(f);
3126                  checkIncomplete(CompletableFuture.allOf(fs));
3127                  fs[i].complete(one);
# Line 2628 | Line 3131 | public class CompletableFutureTest exten
3131          }
3132      }
3133  
3134 +    public void testAllOf_exceptional() throws Exception {
3135 +        for (int k = 1; k < 10; k++) {
3136 +            CompletableFuture<Integer>[] fs
3137 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3138 +            CFException ex = new CFException();
3139 +            for (int i = 0; i < k; i++)
3140 +                fs[i] = new CompletableFuture<>();
3141 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3142 +            for (int i = 0; i < k; i++) {
3143 +                checkIncomplete(f);
3144 +                checkIncomplete(CompletableFuture.allOf(fs));
3145 +                if (i != k/2) {
3146 +                    fs[i].complete(i);
3147 +                    checkCompletedNormally(fs[i], i);
3148 +                } else {
3149 +                    fs[i].completeExceptionally(ex);
3150 +                    checkCompletedExceptionally(fs[i], ex);
3151 +                }
3152 +            }
3153 +            checkCompletedWithWrappedException(f, ex);
3154 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3155 +        }
3156 +    }
3157 +
3158      /**
3159       * anyOf(no component futures) returns an incomplete future
3160       */
3161      public void testAnyOf_empty() throws Exception {
3162 +        for (Integer v1 : new Integer[] { 1, null })
3163 +    {
3164          CompletableFuture<Object> f = CompletableFuture.anyOf();
3165          checkIncomplete(f);
3166 <    }
3166 >
3167 >        f.complete(v1);
3168 >        checkCompletedNormally(f, v1);
3169 >    }}
3170  
3171      /**
3172       * anyOf returns a future completed normally with a value when
3173       * a component future does
3174       */
3175      public void testAnyOf_normal() throws Exception {
3176 <        for (int k = 0; k < 10; ++k) {
3176 >        for (int k = 0; k < 10; k++) {
3177              CompletableFuture[] fs = new CompletableFuture[k];
3178 <            for (int i = 0; i < k; ++i)
3178 >            for (int i = 0; i < k; i++)
3179                  fs[i] = new CompletableFuture<>();
3180              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3181              checkIncomplete(f);
3182 <            for (int i = 0; i < k; ++i) {
3183 <                fs[i].complete(one);
3184 <                checkCompletedNormally(f, one);
3185 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3182 >            for (int i = 0; i < k; i++) {
3183 >                fs[i].complete(i);
3184 >                checkCompletedNormally(f, 0);
3185 >                int x = (int) CompletableFuture.anyOf(fs).join();
3186 >                assertTrue(0 <= x && x <= i);
3187 >            }
3188 >        }
3189 >    }
3190 >    public void testAnyOf_normal_backwards() throws Exception {
3191 >        for (int k = 0; k < 10; k++) {
3192 >            CompletableFuture[] fs = new CompletableFuture[k];
3193 >            for (int i = 0; i < k; i++)
3194 >                fs[i] = new CompletableFuture<>();
3195 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3196 >            checkIncomplete(f);
3197 >            for (int i = k - 1; i >= 0; i--) {
3198 >                fs[i].complete(i);
3199 >                checkCompletedNormally(f, k - 1);
3200 >                int x = (int) CompletableFuture.anyOf(fs).join();
3201 >                assertTrue(i <= x && x <= k - 1);
3202              }
3203          }
3204      }
# Line 2659 | Line 3207 | public class CompletableFutureTest exten
3207       * anyOf result completes exceptionally when any component does.
3208       */
3209      public void testAnyOf_exceptional() throws Exception {
3210 <        for (int k = 0; k < 10; ++k) {
3210 >        for (int k = 0; k < 10; k++) {
3211              CompletableFuture[] fs = new CompletableFuture[k];
3212 <            for (int i = 0; i < k; ++i)
3212 >            CFException[] exs = new CFException[k];
3213 >            for (int i = 0; i < k; i++) {
3214                  fs[i] = new CompletableFuture<>();
3215 +                exs[i] = new CFException();
3216 +            }
3217              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3218              checkIncomplete(f);
3219 <            for (int i = 0; i < k; ++i) {
3220 <                fs[i].completeExceptionally(new CFException());
3221 <                checkCompletedWithWrappedCFException(f);
3219 >            for (int i = 0; i < k; i++) {
3220 >                fs[i].completeExceptionally(exs[i]);
3221 >                checkCompletedWithWrappedException(f, exs[0]);
3222 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3223 >            }
3224 >        }
3225 >    }
3226 >
3227 >    public void testAnyOf_exceptional_backwards() throws Exception {
3228 >        for (int k = 0; k < 10; k++) {
3229 >            CompletableFuture[] fs = new CompletableFuture[k];
3230 >            CFException[] exs = new CFException[k];
3231 >            for (int i = 0; i < k; i++) {
3232 >                fs[i] = new CompletableFuture<>();
3233 >                exs[i] = new CFException();
3234 >            }
3235 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3236 >            checkIncomplete(f);
3237 >            for (int i = k - 1; i >= 0; i--) {
3238 >                fs[i].completeExceptionally(exs[i]);
3239 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3240                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3241              }
3242          }
# Line 2680 | Line 3249 | public class CompletableFutureTest exten
3249          CompletableFuture<Integer> f = new CompletableFuture<>();
3250          CompletableFuture<Integer> g = new CompletableFuture<>();
3251          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2683        CompletableFuture<?> h;
3252          ThreadExecutor exec = new ThreadExecutor();
3253  
3254          Runnable[] throwingActions = {
3255              () -> CompletableFuture.supplyAsync(null),
3256              () -> CompletableFuture.supplyAsync(null, exec),
3257 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3257 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3258  
3259              () -> CompletableFuture.runAsync(null),
3260              () -> CompletableFuture.runAsync(null, exec),
# Line 2791 | Line 3359 | public class CompletableFutureTest exten
3359          assertSame(f, f.toCompletableFuture());
3360      }
3361  
3362 +    // jdk9
3363 +
3364      /**
3365 <     * whenComplete action executes on normal completion, propagating
2796 <     * source result.
3365 >     * newIncompleteFuture returns an incomplete CompletableFuture
3366       */
3367 <    public void testWhenComplete_normalCompletion1() {
3368 <        for (ExecutionMode m : ExecutionMode.values())
3369 <        for (boolean createIncomplete : new boolean[] { true, false })
3370 <        for (Integer v1 : new Integer[] { 1, null })
3371 <    {
3372 <        final AtomicInteger a = new AtomicInteger(0);
2804 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2805 <        if (!createIncomplete) f.complete(v1);
2806 <        final CompletableFuture<Integer> g = m.whenComplete
2807 <            (f,
2808 <             (Integer x, Throwable t) -> {
2809 <                threadAssertSame(x, v1);
2810 <                threadAssertNull(t);
2811 <                a.getAndIncrement();
2812 <            });
2813 <        if (createIncomplete) f.complete(v1);
3367 >    public void testNewIncompleteFuture() {
3368 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3369 >        CompletableFuture<Integer> g = f.newIncompleteFuture();
3370 >        checkIncomplete(f);
3371 >        checkIncomplete(g);
3372 >    }
3373  
3374 <        checkCompletedNormally(g, v1);
3375 <        checkCompletedNormally(f, v1);
3376 <        assertEquals(1, a.get());
3377 <    }}
3374 >    /**
3375 >     * completedStage returns a completed CompletionStage
3376 >     */
3377 >    public void testCompletedStage() {
3378 >        AtomicInteger x = new AtomicInteger();
3379 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3380 >        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3381 >        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3382 >        assertEquals(x.get(), 1);
3383 >        assertNull(r.get());
3384 >    }
3385  
3386      /**
3387 <     * whenComplete action executes on exceptional completion, propagating
3388 <     * source result.
3387 >     * defaultExecutor by default returns the commonPool if
3388 >     * it supports at least one thread.
3389       */
3390 <    public void testWhenComplete_exceptionalCompletion() {
3391 <        for (ExecutionMode m : ExecutionMode.values())
3392 <        for (boolean createIncomplete : new boolean[] { true, false })
3393 <        for (Integer v1 : new Integer[] { 1, null })
3394 <    {
3395 <        final AtomicInteger a = new AtomicInteger(0);
3396 <        final CFException ex = new CFException();
2831 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2832 <        if (!createIncomplete) f.completeExceptionally(ex);
2833 <        final CompletableFuture<Integer> g = m.whenComplete
2834 <            (f,
2835 <             (Integer x, Throwable t) -> {
2836 <                threadAssertNull(x);
2837 <                threadAssertSame(t, ex);
2838 <                a.getAndIncrement();
2839 <            });
2840 <        if (createIncomplete) f.completeExceptionally(ex);
2841 <        checkCompletedWithWrappedCFException(f, ex);
2842 <        checkCompletedWithWrappedCFException(g, ex);
2843 <        assertEquals(1, a.get());
2844 <    }}
3390 >    public void testDefaultExecutor() {
3391 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3392 >        Executor e = f.defaultExecutor();
3393 >        Executor c =  ForkJoinPool.commonPool();
3394 >        if (ForkJoinPool.getCommonPoolParallelism() > 1)
3395 >            assertSame(e, c);
3396 >    }
3397  
3398      /**
3399 <     * whenComplete action executes on cancelled source, propagating
3400 <     * CancellationException.
3399 >     * failedFuture returns a CompletableFuture completed
3400 >     * exceptionally with the given Exception
3401       */
3402 <    public void testWhenComplete_sourceCancelled() {
3403 <        for (ExecutionMode m : ExecutionMode.values())
3404 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3405 <        for (boolean createIncomplete : new boolean[] { true, false })
3406 <    {
2855 <        final AtomicInteger a = new AtomicInteger(0);
2856 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2857 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2858 <        final CompletableFuture<Integer> g = m.whenComplete
2859 <            (f,
2860 <             (Integer x, Throwable t) -> {
2861 <                threadAssertNull(x);
2862 <                threadAssertTrue(t instanceof CancellationException);
2863 <                a.getAndIncrement();
2864 <            });
2865 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3402 >    public void testFailedFuture() {
3403 >        CFException ex = new CFException();
3404 >        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3405 >        checkCompletedExceptionallyWithRootCause(f, ex);
3406 >    }
3407  
3408 <        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
3409 <        checkCompletedWithWrappedCancellationException(g);
3410 <        checkCancelled(f);
3411 <        assertEquals(1, a.get());
3412 <    }}
3408 >    /**
3409 >     * failedFuture(null) throws NPE
3410 >     */
3411 >    public void testFailedFuture2() {
3412 >        try {
3413 >            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3414 >            shouldThrow();
3415 >        } catch (NullPointerException success) {}
3416 >    }
3417  
3418      /**
3419 <     * If a whenComplete action throws an exception when triggered by
3420 <     * a normal completion, it completes exceptionally
3419 >     * copy returns a CompletableFuture that is completed normally,
3420 >     * with the same value, when source is.
3421       */
3422 <    public void testWhenComplete_actionFailed() {
3423 <        for (boolean createIncomplete : new boolean[] { true, false })
3424 <        for (ExecutionMode m : ExecutionMode.values())
3425 <        for (Integer v1 : new Integer[] { 1, null })
3426 <    {
3427 <        final AtomicInteger a = new AtomicInteger(0);
3428 <        final CFException ex = new CFException();
3429 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3430 <        if (!createIncomplete) f.complete(v1);
2886 <        final CompletableFuture<Integer> g = m.whenComplete
2887 <            (f,
2888 <             (Integer x, Throwable t) -> {
2889 <                threadAssertSame(x, v1);
2890 <                threadAssertNull(t);
2891 <                a.getAndIncrement();
2892 <                throw ex;
2893 <            });
2894 <        if (createIncomplete) f.complete(v1);
2895 <        checkCompletedNormally(f, v1);
2896 <        checkCompletedWithWrappedCFException(g, ex);
2897 <        assertEquals(1, a.get());
2898 <    }}
3422 >    public void testCopy() {
3423 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3424 >        CompletableFuture<Integer> g = f.copy();
3425 >        checkIncomplete(f);
3426 >        checkIncomplete(g);
3427 >        f.complete(1);
3428 >        checkCompletedNormally(f, 1);
3429 >        checkCompletedNormally(g, 1);
3430 >    }
3431  
3432      /**
3433 <     * If a whenComplete action throws an exception when triggered by
3434 <     * a source completion that also throws an exception, the source
2903 <     * exception takes precedence.
3433 >     * copy returns a CompletableFuture that is completed exceptionally
3434 >     * when source is.
3435       */
3436 <    public void testWhenComplete_actionFailedSourceFailed() {
3437 <        for (boolean createIncomplete : new boolean[] { true, false })
3436 >    public void testCopy2() {
3437 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3438 >        CompletableFuture<Integer> g = f.copy();
3439 >        checkIncomplete(f);
3440 >        checkIncomplete(g);
3441 >        CFException ex = new CFException();
3442 >        f.completeExceptionally(ex);
3443 >        checkCompletedExceptionally(f, ex);
3444 >        checkCompletedWithWrappedCFException(g);
3445 >    }
3446 >
3447 >    /**
3448 >     * minimalCompletionStage returns a CompletableFuture that is
3449 >     * completed normally, with the same value, when source is.
3450 >     */
3451 >    public void testMinimalCompletionStage() {
3452 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3453 >        CompletionStage<Integer> g = f.minimalCompletionStage();
3454 >        AtomicInteger x = new AtomicInteger();
3455 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3456 >        checkIncomplete(f);
3457 >        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3458 >        f.complete(1);
3459 >        checkCompletedNormally(f, 1);
3460 >        assertEquals(x.get(), 1);
3461 >        assertNull(r.get());
3462 >    }
3463 >
3464 >    /**
3465 >     * minimalCompletionStage returns a CompletableFuture that is
3466 >     * completed exceptionally when source is.
3467 >     */
3468 >    public void testMinimalCompletionStage2() {
3469 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3470 >        CompletionStage<Integer> g = f.minimalCompletionStage();
3471 >        AtomicInteger x = new AtomicInteger();
3472 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3473 >        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3474 >        checkIncomplete(f);
3475 >        CFException ex = new CFException();
3476 >        f.completeExceptionally(ex);
3477 >        checkCompletedExceptionally(f, ex);
3478 >        assertEquals(x.get(), 0);
3479 >        assertEquals(r.get().getCause(), ex);
3480 >    }
3481 >
3482 >    /**
3483 >     * failedStage returns a Completionstage completed
3484 >     * exceptionally with the given Exception
3485 >     */
3486 >    public void testFailedStage() {
3487 >        CFException ex = new CFException();
3488 >        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3489 >        AtomicInteger x = new AtomicInteger();
3490 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3491 >        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3492 >        assertEquals(x.get(), 0);
3493 >        assertEquals(r.get().getCause(), ex);
3494 >    }
3495 >
3496 >    /**
3497 >     * completeAsync completes with value of given supplier
3498 >     */
3499 >    public void testCompleteAsync() {
3500 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3501 >        f.completeAsync(() -> 1);
3502 >        f.join();
3503 >        checkCompletedNormally(f, 1);
3504 >    }
3505 >
3506 >    /**
3507 >     * completeAsync completes exceptionally if given supplier throws
3508 >     */
3509 >    public void testCompleteAsync2() {
3510 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3511 >        CFException ex = new CFException();
3512 >        f.completeAsync(() -> {if (true) throw ex; return 1;});
3513 >        try {
3514 >            f.join();
3515 >            shouldThrow();
3516 >        } catch (Exception success) {}
3517 >        checkCompletedWithWrappedCFException(f);
3518 >    }
3519 >
3520 >    /**
3521 >     * completeAsync with given executor completes with value of given supplier
3522 >     */
3523 >    public void testCompleteAsync3() {
3524 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3525 >        f.completeAsync(() -> 1, new ThreadExecutor());
3526 >        f.join();
3527 >        checkCompletedNormally(f, 1);
3528 >    }
3529 >
3530 >    /**
3531 >     * completeAsync with given executor completes exceptionally if
3532 >     * given supplier throws
3533 >     */
3534 >    public void testCompleteAsync4() {
3535 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3536 >        CFException ex = new CFException();
3537 >        f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor());
3538 >        try {
3539 >            f.join();
3540 >            shouldThrow();
3541 >        } catch (Exception success) {}
3542 >        checkCompletedWithWrappedCFException(f);
3543 >    }
3544 >
3545 >    /**
3546 >     * orTimeout completes with TimeoutException if not complete
3547 >     */
3548 >    public void testOrTimeout() {
3549 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3550 >        f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
3551 >        checkCompletedExceptionallyWithTimeout(f);
3552 >    }
3553 >
3554 >    /**
3555 >     * orTimeout completes normally if completed before timeout
3556 >     */
3557 >    public void testOrTimeout2() {
3558 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3559 >        f.complete(1);
3560 >        f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
3561 >        checkCompletedNormally(f, 1);
3562 >    }
3563 >
3564 >    /**
3565 >     * completeOnTimeout completes with given value if not complete
3566 >     */
3567 >    public void testCompleteOnTimeout() {
3568 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3569 >        f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
3570 >        f.join();
3571 >        checkCompletedNormally(f, -1);
3572 >    }
3573 >
3574 >    /**
3575 >     * completeOnTimeout has no effect if completed within timeout
3576 >     */
3577 >    public void testCompleteOnTimeout2() {
3578 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3579 >        f.complete(1);
3580 >        f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
3581 >        checkCompletedNormally(f, 1);
3582 >    }
3583 >
3584 >    /**
3585 >     * delayedExecutor returns an executor that delays submission
3586 >     */
3587 >    public void testDelayedExecutor() {
3588 >        long timeoutMillis = SMALL_DELAY_MS;
3589 >        Executor d = CompletableFuture.delayedExecutor(timeoutMillis,
3590 >                                                       MILLISECONDS);
3591 >        long startTime = System.nanoTime();
3592 >        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d);
3593 >        assertNull(f.getNow(null));
3594 >        try {
3595 >            f.get(LONG_DELAY_MS, MILLISECONDS);
3596 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
3597 >        assertTrue(millisElapsedSince(startTime) > timeoutMillis/2);
3598 >        checkCompletedNormally(f, 1);
3599 >    }
3600 >
3601 >    /**
3602 >     * delayedExecutor for a given executor returns an executor that
3603 >     * delays submission
3604 >     */
3605 >    public void testDelayedExecutor2() {
3606 >        long timeoutMillis = SMALL_DELAY_MS;
3607 >        Executor d = CompletableFuture.delayedExecutor(timeoutMillis,
3608 >                                                       MILLISECONDS,
3609 >                                                       new ThreadExecutor());
3610 >        long startTime = System.nanoTime();
3611 >        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d);
3612 >        assertNull(f.getNow(null));
3613 >        try {
3614 >            f.get(LONG_DELAY_MS, MILLISECONDS);
3615 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
3616 >        assertTrue(millisElapsedSince(startTime) > timeoutMillis/2);
3617 >        checkCompletedNormally(f, 1);
3618 >    }
3619 >
3620 >    //--- tests of implementation details; not part of official tck ---
3621 >
3622 >    Object resultOf(CompletableFuture<?> f) {
3623 >        try {
3624 >            java.lang.reflect.Field resultField
3625 >                = CompletableFuture.class.getDeclaredField("result");
3626 >            resultField.setAccessible(true);
3627 >            return resultField.get(f);
3628 >        } catch (Throwable t) { throw new AssertionError(t); }
3629 >    }
3630 >
3631 >    public void testExceptionPropagationReusesResultObject() {
3632 >        if (!testImplementationDetails) return;
3633          for (ExecutionMode m : ExecutionMode.values())
2908        for (Integer v1 : new Integer[] { 1, null })
3634      {
3635 <        final AtomicInteger a = new AtomicInteger(0);
3636 <        final CFException ex1 = new CFException();
3637 <        final CFException ex2 = new CFException();
2913 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3635 >        final CFException ex = new CFException();
3636 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3637 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3638  
3639 <        if (!createIncomplete) f.completeExceptionally(ex1);
3640 <        final CompletableFuture<Integer> g = m.whenComplete
2917 <            (f,
2918 <             (Integer x, Throwable t) -> {
2919 <                threadAssertSame(t, ex1);
2920 <                threadAssertNull(x);
2921 <                a.getAndIncrement();
2922 <                throw ex2;
2923 <            });
2924 <        if (createIncomplete) f.completeExceptionally(ex1);
3639 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3640 >            = new ArrayList<>();
3641  
3642 <        checkCompletedWithWrappedCFException(f, ex1);
3643 <        checkCompletedWithWrappedCFException(g, ex1);
3644 <        assertEquals(1, a.get());
3642 >        funs.add((y) -> m.thenRun(y, new Noop(m)));
3643 >        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3644 >        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3645 >
3646 >        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3647 >        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3648 >        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3649 >
3650 >        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3651 >        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3652 >        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3653 >
3654 >        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3655 >
3656 >        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3657 >
3658 >        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3659 >        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3660 >
3661 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3662 >                 fun : funs) {
3663 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3664 >            f.completeExceptionally(ex);
3665 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3666 >            checkCompletedWithWrappedException(src, ex);
3667 >            CompletableFuture<?> dep = fun.apply(src);
3668 >            checkCompletedWithWrappedException(dep, ex);
3669 >            assertSame(resultOf(src), resultOf(dep));
3670 >        }
3671 >
3672 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3673 >                 fun : funs) {
3674 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3675 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3676 >            CompletableFuture<?> dep = fun.apply(src);
3677 >            f.completeExceptionally(ex);
3678 >            checkCompletedWithWrappedException(src, ex);
3679 >            checkCompletedWithWrappedException(dep, ex);
3680 >            assertSame(resultOf(src), resultOf(dep));
3681 >        }
3682 >
3683 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3684 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3685 >                 fun : funs) {
3686 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3687 >            f.cancel(mayInterruptIfRunning);
3688 >            checkCancelled(f);
3689 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3690 >            checkCompletedWithWrappedCancellationException(src);
3691 >            CompletableFuture<?> dep = fun.apply(src);
3692 >            checkCompletedWithWrappedCancellationException(dep);
3693 >            assertSame(resultOf(src), resultOf(dep));
3694 >        }
3695 >
3696 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3697 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3698 >                 fun : funs) {
3699 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3700 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3701 >            CompletableFuture<?> dep = fun.apply(src);
3702 >            f.cancel(mayInterruptIfRunning);
3703 >            checkCancelled(f);
3704 >            checkCompletedWithWrappedCancellationException(src);
3705 >            checkCompletedWithWrappedCancellationException(dep);
3706 >            assertSame(resultOf(src), resultOf(dep));
3707 >        }
3708      }}
3709  
3710   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines