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.73 by jsr166, Fri Jun 6 21:11:10 2014 UTC

# Line 105 | Line 105 | public class CompletableFutureTest exten
105          assertTrue(f.toString().contains("[Completed exceptionally]"));
106      }
107  
108 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
109 <                                              CFException ex) {
108 >    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
109 >                                                      Throwable ex) {
110          try {
111              f.get(LONG_DELAY_MS, MILLISECONDS);
112              shouldThrow();
# Line 131 | Line 131 | public class CompletableFutureTest exten
131          } catch (ExecutionException success) {
132              assertSame(ex, success.getCause());
133          } catch (Throwable fail) { threadUnexpectedException(fail); }
134 +
135          assertTrue(f.isDone());
136          assertFalse(f.isCancelled());
137          assertTrue(f.toString().contains("[Completed exceptionally]"));
138      }
139  
140 +    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
141 +                                                Throwable ex) {
142 +        checkCompletedExceptionallyWithRootCause(f, ex);
143 +        try {
144 +            CompletableFuture<Throwable> spy = f.handle
145 +                ((U u, Throwable t) -> t);
146 +            assertTrue(spy.join() instanceof CompletionException);
147 +            assertSame(ex, spy.join().getCause());
148 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
149 +    }
150 +
151 +    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
152 +        checkCompletedExceptionallyWithRootCause(f, ex);
153 +        try {
154 +            CompletableFuture<Throwable> spy = f.handle
155 +                ((U u, Throwable t) -> t);
156 +            assertSame(ex, spy.join());
157 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
158 +    }
159 +
160      void checkCancelled(CompletableFuture<?> f) {
161          try {
162              f.get(LONG_DELAY_MS, MILLISECONDS);
# Line 218 | Line 239 | public class CompletableFutureTest exten
239       */
240      public void testCompleteExceptionally() {
241          CompletableFuture<Integer> f = new CompletableFuture<>();
242 +        CFException ex = new CFException();
243          checkIncomplete(f);
244 <        f.completeExceptionally(new CFException());
245 <        checkCompletedWithWrappedCFException(f);
244 >        f.completeExceptionally(ex);
245 >        checkCompletedExceptionally(f, ex);
246      }
247  
248      /**
# Line 261 | Line 283 | public class CompletableFutureTest exten
283       * obtrudeException forces completion with given exception
284       */
285      public void testObtrudeException() {
286 <        CompletableFuture<Integer> f = new CompletableFuture<>();
287 <        checkIncomplete(f);
288 <        f.complete(one);
289 <        checkCompletedNormally(f, one);
290 <        f.obtrudeException(new CFException());
269 <        checkCompletedWithWrappedCFException(f);
286 >        for (Integer v1 : new Integer[] { 1, null })
287 >    {
288 >        CFException ex;
289 >        CompletableFuture<Integer> f;
290 >
291          f = new CompletableFuture<>();
292 <        f.obtrudeException(new CFException());
293 <        checkCompletedWithWrappedCFException(f);
292 >        f.complete(v1);
293 >        for (int i = 0; i < 2; i++) {
294 >            f.obtrudeException(ex = new CFException());
295 >            checkCompletedExceptionally(f, ex);
296 >        }
297 >
298 >        f = new CompletableFuture<>();
299 >        for (int i = 0; i < 2; i++) {
300 >            f.obtrudeException(ex = new CFException());
301 >            checkCompletedExceptionally(f, ex);
302 >        }
303 >
304          f = new CompletableFuture<>();
305 +        f.completeExceptionally(ex = new CFException());
306 +        f.obtrudeValue(v1);
307 +        checkCompletedNormally(f, v1);
308 +        f.obtrudeException(ex = new CFException());
309 +        checkCompletedExceptionally(f, ex);
310          f.completeExceptionally(new CFException());
311 <        f.obtrudeValue(four);
312 <        checkCompletedNormally(f, four);
313 <        f.obtrudeException(new CFException());
314 <        checkCompletedWithWrappedCFException(f);
279 <    }
311 >        checkCompletedExceptionally(f, ex);
312 >        f.complete(v1);
313 >        checkCompletedExceptionally(f, ex);
314 >    }}
315  
316      /**
317       * getNumberOfDependents returns number of dependent tasks
# Line 320 | Line 355 | public class CompletableFutureTest exten
355          checkCompletedNormally(f, "test");
356      }
357  
358 <    // Choose non-commutative actions for better coverage
359 <
360 <    // A non-commutative function that handles and produces null values as well.
361 <    static Integer subtract(Integer x, Integer y) {
362 <        return (x == null && y == null) ? null :
363 <            ((x == null) ? 42 : x.intValue())
364 <            - ((y == null) ? 99 : y.intValue());
358 >    abstract class CheckedAction {
359 >        int invocationCount = 0;
360 >        final ExecutionMode m;
361 >        CheckedAction(ExecutionMode m) { this.m = m; }
362 >        void invoked() {
363 >            m.checkExecutionMode();
364 >            assertEquals(0, invocationCount++);
365 >        }
366 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
367 >        void assertInvoked() { assertEquals(1, invocationCount); }
368      }
369  
370 <    // A function that handles and produces null values as well.
371 <    static Integer inc(Integer x) {
372 <        return (x == null) ? null : x + 1;
370 >    abstract class CheckedIntegerAction extends CheckedAction {
371 >        Integer value;
372 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
373 >        void assertValue(Integer expected) {
374 >            assertInvoked();
375 >            assertEquals(expected, value);
376 >        }
377      }
378  
379 <    static final class IntegerSupplier implements Supplier<Integer> {
380 <        final ExecutionMode m;
381 <        int invocationCount = 0;
379 >    class IntegerSupplier extends CheckedAction
380 >        implements Supplier<Integer>
381 >    {
382          final Integer value;
383          IntegerSupplier(ExecutionMode m, Integer value) {
384 <            this.m = m;
384 >            super(m);
385              this.value = value;
386          }
387          public Integer get() {
388 <            m.checkExecutionMode();
347 <            invocationCount++;
388 >            invoked();
389              return value;
390          }
391      }
392 <        
393 <    static final class IncAction implements Consumer<Integer> {
394 <        int invocationCount = 0;
395 <        Integer value;
392 >
393 >    // A function that handles and produces null values as well.
394 >    static Integer inc(Integer x) {
395 >        return (x == null) ? null : x + 1;
396 >    }
397 >
398 >    class NoopConsumer extends CheckedIntegerAction
399 >        implements Consumer<Integer>
400 >    {
401 >        NoopConsumer(ExecutionMode m) { super(m); }
402          public void accept(Integer x) {
403 <            invocationCount++;
404 <            value = inc(x);
403 >            invoked();
404 >            value = x;
405          }
406      }
407 <    static final class IncFunction implements Function<Integer,Integer> {
408 <        final ExecutionMode m;
409 <        int invocationCount = 0;
410 <        Integer value;
411 <        IncFunction(ExecutionMode m) { this.m = m; }
407 >
408 >    class IncFunction extends CheckedIntegerAction
409 >        implements Function<Integer,Integer>
410 >    {
411 >        IncFunction(ExecutionMode m) { super(m); }
412          public Integer apply(Integer x) {
413 <            m.checkExecutionMode();
367 <            invocationCount++;
413 >            invoked();
414              return value = inc(x);
415          }
416      }
417 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
418 <        final ExecutionMode m;
419 <        int invocationCount = 0;
420 <        Integer value;
421 <        // Check this action was invoked exactly once when result is computed.
422 <        SubtractAction(ExecutionMode m) { this.m = m; }
417 >
418 >    // Choose non-commutative actions for better coverage
419 >    // A non-commutative function that handles and produces null values as well.
420 >    static Integer subtract(Integer x, Integer y) {
421 >        return (x == null && y == null) ? null :
422 >            ((x == null) ? 42 : x.intValue())
423 >            - ((y == null) ? 99 : y.intValue());
424 >    }
425 >
426 >    class SubtractAction extends CheckedIntegerAction
427 >        implements BiConsumer<Integer, Integer>
428 >    {
429 >        SubtractAction(ExecutionMode m) { super(m); }
430          public void accept(Integer x, Integer y) {
431 <            m.checkExecutionMode();
379 <            invocationCount++;
431 >            invoked();
432              value = subtract(x, y);
433          }
434      }
435 <    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
436 <        final ExecutionMode m;
437 <        int invocationCount = 0;
438 <        Integer value;
439 <        // Check this action was invoked exactly once when result is computed.
388 <        SubtractFunction(ExecutionMode m) { this.m = m; }
435 >
436 >    class SubtractFunction extends CheckedIntegerAction
437 >        implements BiFunction<Integer, Integer, Integer>
438 >    {
439 >        SubtractFunction(ExecutionMode m) { super(m); }
440          public Integer apply(Integer x, Integer y) {
441 <            m.checkExecutionMode();
391 <            invocationCount++;
441 >            invoked();
442              return value = subtract(x, y);
443          }
444      }
445 <    static final class Noop implements Runnable {
446 <        final ExecutionMode m;
447 <        int invocationCount = 0;
398 <        Noop(ExecutionMode m) { this.m = m; }
445 >
446 >    class Noop extends CheckedAction implements Runnable {
447 >        Noop(ExecutionMode m) { super(m); }
448          public void run() {
449 <            m.checkExecutionMode();
401 <            invocationCount++;
449 >            invoked();
450          }
451      }
452  
453 <    static final class FailingSupplier implements Supplier<Integer> {
454 <        final ExecutionMode m;
455 <        int invocationCount = 0;
456 <        FailingSupplier(ExecutionMode m) { this.m = m; }
453 >    class FailingSupplier extends CheckedAction
454 >        implements Supplier<Integer>
455 >    {
456 >        FailingSupplier(ExecutionMode m) { super(m); }
457          public Integer get() {
458 <            m.checkExecutionMode();
411 <            invocationCount++;
458 >            invoked();
459              throw new CFException();
460          }
461      }
462 <    static final class FailingConsumer implements Consumer<Integer> {
463 <        final ExecutionMode m;
464 <        int invocationCount = 0;
465 <        FailingConsumer(ExecutionMode m) { this.m = m; }
462 >
463 >    class FailingConsumer extends CheckedIntegerAction
464 >        implements Consumer<Integer>
465 >    {
466 >        FailingConsumer(ExecutionMode m) { super(m); }
467          public void accept(Integer x) {
468 <            m.checkExecutionMode();
469 <            invocationCount++;
468 >            invoked();
469 >            value = x;
470              throw new CFException();
471          }
472      }
473 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
474 <        final ExecutionMode m;
475 <        int invocationCount = 0;
476 <        FailingBiConsumer(ExecutionMode m) { this.m = m; }
473 >
474 >    class FailingBiConsumer extends CheckedIntegerAction
475 >        implements BiConsumer<Integer, Integer>
476 >    {
477 >        FailingBiConsumer(ExecutionMode m) { super(m); }
478          public void accept(Integer x, Integer y) {
479 <            m.checkExecutionMode();
480 <            invocationCount++;
479 >            invoked();
480 >            value = subtract(x, y);
481              throw new CFException();
482          }
483      }
484 <    static final class FailingFunction implements Function<Integer, Integer> {
485 <        final ExecutionMode m;
486 <        int invocationCount = 0;
487 <        FailingFunction(ExecutionMode m) { this.m = m; }
484 >
485 >    class FailingFunction extends CheckedIntegerAction
486 >        implements Function<Integer, Integer>
487 >    {
488 >        FailingFunction(ExecutionMode m) { super(m); }
489          public Integer apply(Integer x) {
490 <            m.checkExecutionMode();
491 <            invocationCount++;
490 >            invoked();
491 >            value = x;
492              throw new CFException();
493          }
494      }
495 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
496 <        final ExecutionMode m;
497 <        int invocationCount = 0;
498 <        FailingBiFunction(ExecutionMode m) { this.m = m; }
495 >
496 >    class FailingBiFunction extends CheckedIntegerAction
497 >        implements BiFunction<Integer, Integer, Integer>
498 >    {
499 >        FailingBiFunction(ExecutionMode m) { super(m); }
500          public Integer apply(Integer x, Integer y) {
501 <            m.checkExecutionMode();
502 <            invocationCount++;
501 >            invoked();
502 >            value = subtract(x, y);
503              throw new CFException();
504          }
505      }
506 <    static final class FailingRunnable implements Runnable {
507 <        final ExecutionMode m;
508 <        int invocationCount = 0;
458 <        FailingRunnable(ExecutionMode m) { this.m = m; }
506 >
507 >    class FailingRunnable extends CheckedAction implements Runnable {
508 >        FailingRunnable(ExecutionMode m) { super(m); }
509          public void run() {
510 <            m.checkExecutionMode();
461 <            invocationCount++;
510 >            invoked();
511              throw new CFException();
512          }
513      }
514  
515 <    static final class CompletableFutureInc
516 <        implements Function<Integer, CompletableFuture<Integer>> {
517 <        final ExecutionMode m;
518 <        int invocationCount = 0;
519 <        CompletableFutureInc(ExecutionMode m) { this.m = m; }
515 >
516 >    class CompletableFutureInc extends CheckedIntegerAction
517 >        implements Function<Integer, CompletableFuture<Integer>>
518 >    {
519 >        CompletableFutureInc(ExecutionMode m) { super(m); }
520          public CompletableFuture<Integer> apply(Integer x) {
521 <            m.checkExecutionMode();
522 <            invocationCount++;
521 >            invoked();
522 >            value = x;
523              CompletableFuture<Integer> f = new CompletableFuture<>();
524              f.complete(inc(x));
525              return f;
526          }
527      }
528  
529 <    static final class FailingCompletableFutureFunction
530 <        implements Function<Integer, CompletableFuture<Integer>> {
531 <        final ExecutionMode m;
532 <        int invocationCount = 0;
484 <        FailingCompletableFutureFunction(ExecutionMode m) { this.m = m; }
529 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
530 >        implements Function<Integer, CompletableFuture<Integer>>
531 >    {
532 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
533          public CompletableFuture<Integer> apply(Integer x) {
534 <            m.checkExecutionMode();
535 <            invocationCount++;
534 >            invoked();
535 >            value = x;
536              throw new CFException();
537          }
538      }
# Line 505 | Line 553 | public class CompletableFutureTest exten
553  
554      /**
555       * Permits the testing of parallel code for the 3 different
556 <     * execution modes without repeating all the testing code.
556 >     * execution modes without copy/pasting all the test methods.
557       */
558      enum ExecutionMode {
559          DEFAULT {
560              public void checkExecutionMode() {
561 +                assertFalse(ThreadExecutor.startedCurrentThread());
562                  assertNull(ForkJoinTask.getPool());
563              }
564              public CompletableFuture<Void> runAsync(Runnable a) {
# Line 838 | Line 887 | public class CompletableFutureTest exten
887              });
888          if (createIncomplete) f.completeExceptionally(ex1);
889  
890 <        checkCompletedWithWrappedCFException(g, ex2);
890 >        checkCompletedWithWrappedException(g, ex2);
891          assertEquals(1, a.get());
892      }}
893  
# Line 895 | Line 944 | public class CompletableFutureTest exten
944          if (createIncomplete) f.completeExceptionally(ex);
945  
946          checkCompletedNormally(g, v1);
947 <        checkCompletedWithWrappedCFException(f, ex);
947 >        checkCompletedExceptionally(f, ex);
948          assertEquals(1, a.get());
949      }}
950  
# Line 951 | Line 1000 | public class CompletableFutureTest exten
1000              });
1001          if (createIncomplete) f.completeExceptionally(ex1);
1002  
1003 <        checkCompletedWithWrappedCFException(g, ex2);
1004 <        checkCompletedWithWrappedCFException(f, ex1);
1003 >        checkCompletedWithWrappedException(g, ex2);
1004 >        checkCompletedExceptionally(f, ex1);
1005          assertEquals(1, a.get());
1006      }}
1007  
# Line 976 | Line 1025 | public class CompletableFutureTest exten
1025              });
1026          if (createIncomplete) f.complete(v1);
1027  
1028 <        checkCompletedWithWrappedCFException(g, ex);
1028 >        checkCompletedWithWrappedException(g, ex);
1029          checkCompletedNormally(f, v1);
1030          assertEquals(1, a.get());
1031      }}
# Line 995 | Line 1044 | public class CompletableFutureTest exten
1044          final CompletableFuture<Void> f = m.runAsync(r);
1045          assertNull(f.join());
1046          checkCompletedNormally(f, null);
1047 <        assertEquals(1, r.invocationCount);
1047 >        r.assertInvoked();
1048      }}
1049  
1050      /**
# Line 1011 | Line 1060 | public class CompletableFutureTest exten
1060          final FailingRunnable r = new FailingRunnable(m);
1061          final CompletableFuture<Void> f = m.runAsync(r);
1062          checkCompletedWithWrappedCFException(f);
1063 <        assertEquals(1, r.invocationCount);
1063 >        r.assertInvoked();
1064      }}
1065  
1066      /**
# Line 1029 | Line 1078 | public class CompletableFutureTest exten
1078          final CompletableFuture<Integer> f = m.supplyAsync(r);
1079          assertSame(v1, f.join());
1080          checkCompletedNormally(f, v1);
1081 <        assertEquals(1, r.invocationCount);
1081 >        r.assertInvoked();
1082      }}
1083  
1084      /**
# Line 1045 | Line 1094 | public class CompletableFutureTest exten
1094          FailingSupplier r = new FailingSupplier(m);
1095          CompletableFuture<Integer> f = m.supplyAsync(r);
1096          checkCompletedWithWrappedCFException(f);
1097 <        assertEquals(1, r.invocationCount);
1097 >        r.assertInvoked();
1098      }}
1099  
1100      // seq completion methods
# Line 1069 | Line 1118 | public class CompletableFutureTest exten
1118  
1119          checkCompletedNormally(g, null);
1120          checkCompletedNormally(f, v1);
1121 <        assertEquals(1, r.invocationCount);
1121 >        r.assertInvoked();
1122      }}
1123  
1124      /**
# Line 1090 | Line 1139 | public class CompletableFutureTest exten
1139              f.completeExceptionally(ex);
1140          }
1141  
1142 <        checkCompletedWithWrappedCFException(g, ex);
1143 <        checkCompletedWithWrappedCFException(f, ex);
1144 <        assertEquals(0, r.invocationCount);
1142 >        checkCompletedWithWrappedException(g, ex);
1143 >        checkCompletedExceptionally(f, ex);
1144 >        r.assertNotInvoked();
1145      }}
1146  
1147      /**
# Line 1114 | Line 1163 | public class CompletableFutureTest exten
1163  
1164          checkCompletedWithWrappedCancellationException(g);
1165          checkCancelled(f);
1166 <        assertEquals(0, r.invocationCount);
1166 >        r.assertNotInvoked();
1167      }}
1168  
1169      /**
# Line 1157 | Line 1206 | public class CompletableFutureTest exten
1206  
1207          checkCompletedNormally(g, inc(v1));
1208          checkCompletedNormally(f, v1);
1209 <        assertEquals(1, r.invocationCount);
1209 >        r.assertValue(inc(v1));
1210      }}
1211  
1212      /**
# Line 1178 | Line 1227 | public class CompletableFutureTest exten
1227              f.completeExceptionally(ex);
1228          }
1229  
1230 <        checkCompletedWithWrappedCFException(g, ex);
1231 <        checkCompletedWithWrappedCFException(f, ex);
1232 <        assertEquals(0, r.invocationCount);
1230 >        checkCompletedWithWrappedException(g, ex);
1231 >        checkCompletedExceptionally(f, ex);
1232 >        r.assertNotInvoked();
1233      }}
1234  
1235      /**
# Line 1202 | Line 1251 | public class CompletableFutureTest exten
1251  
1252          checkCompletedWithWrappedCancellationException(g);
1253          checkCancelled(f);
1254 <        assertEquals(0, r.invocationCount);
1254 >        r.assertNotInvoked();
1255      }}
1256  
1257      /**
# Line 1235 | Line 1284 | public class CompletableFutureTest exten
1284          for (Integer v1 : new Integer[] { 1, null })
1285      {
1286          final CompletableFuture<Integer> f = new CompletableFuture<>();
1287 <        final IncAction r = new IncAction();
1287 >        final NoopConsumer r = new NoopConsumer(m);
1288          if (!createIncomplete) f.complete(v1);
1289          final CompletableFuture<Void> g = m.thenAccept(f, r);
1290          if (createIncomplete) {
# Line 1244 | Line 1293 | public class CompletableFutureTest exten
1293          }
1294  
1295          checkCompletedNormally(g, null);
1296 +        r.assertValue(v1);
1297          checkCompletedNormally(f, v1);
1248        assertEquals(1, r.invocationCount);
1249        assertEquals(inc(v1), r.value);
1298      }}
1299  
1300      /**
# Line 1259 | Line 1307 | public class CompletableFutureTest exten
1307      {
1308          final CFException ex = new CFException();
1309          final CompletableFuture<Integer> f = new CompletableFuture<>();
1310 <        final IncAction r = new IncAction();
1310 >        final NoopConsumer r = new NoopConsumer(m);
1311          if (!createIncomplete) f.completeExceptionally(ex);
1312          final CompletableFuture<Void> g = m.thenAccept(f, r);
1313          if (createIncomplete) {
# Line 1267 | Line 1315 | public class CompletableFutureTest exten
1315              f.completeExceptionally(ex);
1316          }
1317  
1318 <        checkCompletedWithWrappedCFException(g, ex);
1319 <        checkCompletedWithWrappedCFException(f, ex);
1320 <        assertEquals(0, r.invocationCount);
1318 >        checkCompletedWithWrappedException(g, ex);
1319 >        checkCompletedExceptionally(f, ex);
1320 >        r.assertNotInvoked();
1321      }}
1322  
1323      /**
1324 <     * thenAccept result completes exceptionally if action does
1324 >     * thenAccept result completes exceptionally if source cancelled
1325       */
1326 <    public void testThenAccept_actionFailed() {
1326 >    public void testThenAccept_sourceCancelled() {
1327          for (ExecutionMode m : ExecutionMode.values())
1328          for (boolean createIncomplete : new boolean[] { true, false })
1329 <        for (Integer v1 : new Integer[] { 1, null })
1329 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1330      {
1331          final CompletableFuture<Integer> f = new CompletableFuture<>();
1332 <        final FailingConsumer r = new FailingConsumer(m);
1333 <        if (!createIncomplete) f.complete(v1);
1332 >        final NoopConsumer r = new NoopConsumer(m);
1333 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1334          final CompletableFuture<Void> g = m.thenAccept(f, r);
1335          if (createIncomplete) {
1336              checkIncomplete(g);
1337 <            f.complete(v1);
1337 >            assertTrue(f.cancel(mayInterruptIfRunning));
1338          }
1339  
1340 <        checkCompletedWithWrappedCFException(g);
1341 <        checkCompletedNormally(f, v1);
1340 >        checkCompletedWithWrappedCancellationException(g);
1341 >        checkCancelled(f);
1342 >        r.assertNotInvoked();
1343      }}
1344  
1345      /**
1346 <     * thenAccept result completes exceptionally if source cancelled
1346 >     * thenAccept result completes exceptionally if action does
1347       */
1348 <    public void testThenAccept_sourceCancelled() {
1348 >    public void testThenAccept_actionFailed() {
1349          for (ExecutionMode m : ExecutionMode.values())
1350          for (boolean createIncomplete : new boolean[] { true, false })
1351 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1351 >        for (Integer v1 : new Integer[] { 1, null })
1352      {
1353          final CompletableFuture<Integer> f = new CompletableFuture<>();
1354 <        final IncAction r = new IncAction();
1355 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1354 >        final FailingConsumer r = new FailingConsumer(m);
1355 >        if (!createIncomplete) f.complete(v1);
1356          final CompletableFuture<Void> g = m.thenAccept(f, r);
1357          if (createIncomplete) {
1358              checkIncomplete(g);
1359 <            assertTrue(f.cancel(mayInterruptIfRunning));
1359 >            f.complete(v1);
1360          }
1361  
1362 <        checkCompletedWithWrappedCancellationException(g);
1363 <        checkCancelled(f);
1315 <        assertEquals(0, r.invocationCount);
1362 >        checkCompletedWithWrappedCFException(g);
1363 >        checkCompletedNormally(f, v1);
1364      }}
1365  
1366      /**
# Line 1336 | Line 1384 | public class CompletableFutureTest exten
1384          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1385          if (createIncomplete) {
1386              checkIncomplete(h);
1387 <            assertEquals(0, r.invocationCount);
1387 >            r.assertNotInvoked();
1388              if (!fFirst) f.complete(v1); else g.complete(v2);
1389          }
1390  
1391          checkCompletedNormally(h, subtract(v1, v2));
1392          checkCompletedNormally(f, v1);
1393          checkCompletedNormally(g, v2);
1394 <        assertEquals(1, r.invocationCount);
1394 >        r.assertValue(subtract(v1, v2));
1395      }}
1396  
1397      /**
# Line 1370 | Line 1418 | public class CompletableFutureTest exten
1418              (!fFirst ? f : g).completeExceptionally(ex);
1419          }
1420  
1421 <        checkCompletedWithWrappedCFException(h, ex);
1422 <        assertEquals(0, r.invocationCount);
1421 >        checkCompletedWithWrappedException(h, ex);
1422 >        r.assertNotInvoked();
1423          checkCompletedNormally(fFirst ? f : g, v1);
1424 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1377 <    }}
1378 <
1379 <    /**
1380 <     * thenCombine result completes exceptionally if action does
1381 <     */
1382 <    public void testThenCombine_actionFailed() {
1383 <        for (ExecutionMode m : ExecutionMode.values())
1384 <        for (boolean fFirst : new boolean[] { true, false })
1385 <        for (Integer v1 : new Integer[] { 1, null })
1386 <        for (Integer v2 : new Integer[] { 2, null })
1387 <    {
1388 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1389 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1390 <        final FailingBiFunction r = new FailingBiFunction(m);
1391 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1392 <
1393 <        if (fFirst) {
1394 <            f.complete(v1);
1395 <            g.complete(v2);
1396 <        } else {
1397 <            g.complete(v2);
1398 <            f.complete(v1);
1399 <        }
1400 <
1401 <        checkCompletedWithWrappedCFException(h);
1402 <        checkCompletedNormally(f, v1);
1403 <        checkCompletedNormally(g, v2);
1424 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1425      }}
1426  
1427      /**
# Line 1428 | Line 1449 | public class CompletableFutureTest exten
1449  
1450          checkCompletedWithWrappedCancellationException(h);
1451          checkCancelled(!fFirst ? f : g);
1452 <        assertEquals(0, r.invocationCount);
1452 >        r.assertNotInvoked();
1453          checkCompletedNormally(fFirst ? f : g, v1);
1454      }}
1455  
1456      /**
1457 +     * thenCombine result completes exceptionally if action does
1458 +     */
1459 +    public void testThenCombine_actionFailed() {
1460 +        for (ExecutionMode m : ExecutionMode.values())
1461 +        for (boolean fFirst : new boolean[] { true, false })
1462 +        for (Integer v1 : new Integer[] { 1, null })
1463 +        for (Integer v2 : new Integer[] { 2, null })
1464 +    {
1465 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1466 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1467 +        final FailingBiFunction r = new FailingBiFunction(m);
1468 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1469 +
1470 +        if (fFirst) {
1471 +            f.complete(v1);
1472 +            g.complete(v2);
1473 +        } else {
1474 +            g.complete(v2);
1475 +            f.complete(v1);
1476 +        }
1477 +
1478 +        checkCompletedWithWrappedCFException(h);
1479 +        checkCompletedNormally(f, v1);
1480 +        checkCompletedNormally(g, v2);
1481 +    }}
1482 +
1483 +    /**
1484       * thenAcceptBoth result completes normally after normal
1485       * completion of sources
1486       */
# Line 1453 | Line 1501 | public class CompletableFutureTest exten
1501          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1502          if (createIncomplete) {
1503              checkIncomplete(h);
1504 <            assertEquals(0, r.invocationCount);
1504 >            r.assertNotInvoked();
1505              if (!fFirst) f.complete(v1); else g.complete(v2);
1506          }
1507  
1508          checkCompletedNormally(h, null);
1509 <        assertEquals(subtract(v1, v2), r.value);
1509 >        r.assertValue(subtract(v1, v2));
1510          checkCompletedNormally(f, v1);
1511          checkCompletedNormally(g, v2);
1512      }}
# Line 1487 | Line 1535 | public class CompletableFutureTest exten
1535              (!fFirst ? f : g).completeExceptionally(ex);
1536          }
1537  
1538 <        checkCompletedWithWrappedCFException(h, ex);
1539 <        assertEquals(0, r.invocationCount);
1538 >        checkCompletedWithWrappedException(h, ex);
1539 >        r.assertNotInvoked();
1540          checkCompletedNormally(fFirst ? f : g, v1);
1541 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1494 <    }}
1495 <
1496 <    /**
1497 <     * thenAcceptBoth result completes exceptionally if action does
1498 <     */
1499 <    public void testThenAcceptBoth_actionFailed() {
1500 <        for (ExecutionMode m : ExecutionMode.values())
1501 <        for (boolean fFirst : new boolean[] { true, false })
1502 <        for (Integer v1 : new Integer[] { 1, null })
1503 <        for (Integer v2 : new Integer[] { 2, null })
1504 <    {
1505 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1506 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1507 <        final FailingBiConsumer r = new FailingBiConsumer(m);
1508 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1509 <
1510 <        if (fFirst) {
1511 <            f.complete(v1);
1512 <            g.complete(v2);
1513 <        } else {
1514 <            g.complete(v2);
1515 <            f.complete(v1);
1516 <        }
1517 <
1518 <        checkCompletedWithWrappedCFException(h);
1519 <        checkCompletedNormally(f, v1);
1520 <        checkCompletedNormally(g, v2);
1541 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1542      }}
1543  
1544      /**
# Line 1545 | Line 1566 | public class CompletableFutureTest exten
1566  
1567          checkCompletedWithWrappedCancellationException(h);
1568          checkCancelled(!fFirst ? f : g);
1569 <        assertEquals(0, r.invocationCount);
1569 >        r.assertNotInvoked();
1570          checkCompletedNormally(fFirst ? f : g, v1);
1571      }}
1572  
1573      /**
1574 +     * thenAcceptBoth result completes exceptionally if action does
1575 +     */
1576 +    public void testThenAcceptBoth_actionFailed() {
1577 +        for (ExecutionMode m : ExecutionMode.values())
1578 +        for (boolean fFirst : new boolean[] { true, false })
1579 +        for (Integer v1 : new Integer[] { 1, null })
1580 +        for (Integer v2 : new Integer[] { 2, null })
1581 +    {
1582 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1583 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1584 +        final FailingBiConsumer r = new FailingBiConsumer(m);
1585 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1586 +
1587 +        if (fFirst) {
1588 +            f.complete(v1);
1589 +            g.complete(v2);
1590 +        } else {
1591 +            g.complete(v2);
1592 +            f.complete(v1);
1593 +        }
1594 +
1595 +        checkCompletedWithWrappedCFException(h);
1596 +        checkCompletedNormally(f, v1);
1597 +        checkCompletedNormally(g, v2);
1598 +    }}
1599 +
1600 +    /**
1601       * runAfterBoth result completes normally after normal
1602       * completion of sources
1603       */
# Line 1570 | Line 1618 | public class CompletableFutureTest exten
1618          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1619          if (createIncomplete) {
1620              checkIncomplete(h);
1621 <            assertEquals(0, r.invocationCount);
1621 >            r.assertNotInvoked();
1622              if (!fFirst) f.complete(v1); else g.complete(v2);
1623          }
1624  
1625          checkCompletedNormally(h, null);
1626 <        assertEquals(1, r.invocationCount);
1626 >        r.assertInvoked();
1627          checkCompletedNormally(f, v1);
1628          checkCompletedNormally(g, v2);
1629      }}
# Line 1604 | Line 1652 | public class CompletableFutureTest exten
1652              (!fFirst ? f : g).completeExceptionally(ex);
1653          }
1654  
1655 <        checkCompletedWithWrappedCFException(h, ex);
1656 <        assertEquals(0, r.invocationCount);
1655 >        checkCompletedWithWrappedException(h, ex);
1656 >        r.assertNotInvoked();
1657          checkCompletedNormally(fFirst ? f : g, v1);
1658 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1611 <    }}
1612 <
1613 <    /**
1614 <     * runAfterBoth result completes exceptionally if action does
1615 <     */
1616 <    public void testRunAfterBoth_actionFailed() {
1617 <        for (ExecutionMode m : ExecutionMode.values())
1618 <        for (boolean fFirst : new boolean[] { true, false })
1619 <        for (Integer v1 : new Integer[] { 1, null })
1620 <        for (Integer v2 : new Integer[] { 2, null })
1621 <    {
1622 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1623 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1624 <        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);
1658 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1659      }}
1660  
1661      /**
# Line 1665 | Line 1684 | public class CompletableFutureTest exten
1684  
1685          checkCompletedWithWrappedCancellationException(h);
1686          checkCancelled(!fFirst ? f : g);
1687 <        assertEquals(0, r.invocationCount);
1687 >        r.assertNotInvoked();
1688          checkCompletedNormally(fFirst ? f : g, v1);
1689      }}
1690  
1691      /**
1692 <     * applyToEither result completes normally after normal completion
1674 <     * of either source
1692 >     * runAfterBoth result completes exceptionally if action does
1693       */
1694 <    public void testApplyToEither_normalCompletion() {
1694 >    public void testRunAfterBoth_actionFailed() {
1695          for (ExecutionMode m : ExecutionMode.values())
1678        for (boolean createIncomplete : new boolean[] { true, false })
1696          for (boolean fFirst : new boolean[] { true, false })
1697          for (Integer v1 : new Integer[] { 1, null })
1698          for (Integer v2 : new Integer[] { 2, null })
1699      {
1700          final CompletableFuture<Integer> f = new CompletableFuture<>();
1701          final CompletableFuture<Integer> g = new CompletableFuture<>();
1702 <        final IncFunction r = new IncFunction(m);
1702 >        final FailingRunnable r1 = new FailingRunnable(m);
1703 >        final FailingRunnable r2 = new FailingRunnable(m);
1704  
1705 <        if (!createIncomplete)
1706 <            if (fFirst) f.complete(v1); else g.complete(v2);
1707 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1708 <        if (createIncomplete) {
1709 <            checkIncomplete(h);
1710 <            assertEquals(0, r.invocationCount);
1711 <            if (fFirst) f.complete(v1); else g.complete(v2);
1705 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1706 >        if (fFirst) {
1707 >            f.complete(v1);
1708 >            g.complete(v2);
1709 >        } else {
1710 >            g.complete(v2);
1711 >            f.complete(v1);
1712          }
1713 <        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1696 <        if (!fFirst) f.complete(v1); else g.complete(v2);
1713 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1714  
1715 +        checkCompletedWithWrappedCFException(h1);
1716 +        checkCompletedWithWrappedCFException(h2);
1717          checkCompletedNormally(f, v1);
1718          checkCompletedNormally(g, v2);
1700        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1719      }}
1720  
1721 <    public void testApplyToEither_normalCompletionBothAvailable() {
1721 >    /**
1722 >     * applyToEither result completes normally after normal completion
1723 >     * of either source
1724 >     */
1725 >    public void testApplyToEither_normalCompletion() {
1726          for (ExecutionMode m : ExecutionMode.values())
1705        for (boolean fFirst : new boolean[] { true, false })
1727          for (Integer v1 : new Integer[] { 1, null })
1728          for (Integer v2 : new Integer[] { 2, null })
1729      {
1730          final CompletableFuture<Integer> f = new CompletableFuture<>();
1731          final CompletableFuture<Integer> g = new CompletableFuture<>();
1732 <        final IncFunction r = new IncFunction(m);
1732 >        final IncFunction[] rs = new IncFunction[6];
1733 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1734  
1735 <        if (fFirst) {
1736 <            f.complete(v1);
1737 <            g.complete(v2);
1738 <        } else {
1739 <            g.complete(v2);
1740 <            f.complete(v1);
1741 <        }
1735 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1736 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1737 >        checkIncomplete(h0);
1738 >        checkIncomplete(h1);
1739 >        rs[0].assertNotInvoked();
1740 >        rs[1].assertNotInvoked();
1741 >        f.complete(v1);
1742 >        checkCompletedNormally(h0, inc(v1));
1743 >        checkCompletedNormally(h1, inc(v1));
1744 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1745 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1746 >        checkCompletedNormally(h2, inc(v1));
1747 >        checkCompletedNormally(h3, inc(v1));
1748 >        g.complete(v2);
1749  
1750 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1750 >        // unspecified behavior - both source completions available
1751 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1752 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1753 >        rs[4].assertValue(h4.join());
1754 >        rs[5].assertValue(h5.join());
1755 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
1756 >                   Objects.equals(inc(v2), h4.join()));
1757 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
1758 >                   Objects.equals(inc(v2), h5.join()));
1759  
1760          checkCompletedNormally(f, v1);
1761          checkCompletedNormally(g, v2);
1762 <
1763 <        // unspecified behavior
1764 <        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1765 <                   Objects.equals(h.join(), inc(v2)));
1766 <        assertEquals(1, r.invocationCount);
1762 >        checkCompletedNormally(h0, inc(v1));
1763 >        checkCompletedNormally(h1, inc(v1));
1764 >        checkCompletedNormally(h2, inc(v1));
1765 >        checkCompletedNormally(h3, inc(v1));
1766 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
1767      }}
1768  
1769      /**
1770       * applyToEither result completes exceptionally after exceptional
1771       * completion of either source
1772       */
1773 <    public void testApplyToEither_exceptionalCompletion1() {
1773 >    public void testApplyToEither_exceptionalCompletion() {
1774          for (ExecutionMode m : ExecutionMode.values())
1738        for (boolean createIncomplete : new boolean[] { true, false })
1739        for (boolean fFirst : new boolean[] { true, false })
1775          for (Integer v1 : new Integer[] { 1, null })
1776      {
1777          final CompletableFuture<Integer> f = new CompletableFuture<>();
1778          final CompletableFuture<Integer> g = new CompletableFuture<>();
1779          final CFException ex = new CFException();
1780 <        final IncFunction r = new IncFunction(m);
1780 >        final IncFunction[] rs = new IncFunction[6];
1781 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1782  
1783 <        if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex);
1784 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1785 <        if (createIncomplete) {
1786 <            checkIncomplete(h);
1787 <            assertEquals(0, r.invocationCount);
1788 <            (fFirst ? f : g).completeExceptionally(ex);
1789 <        }
1783 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1784 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1785 >        checkIncomplete(h0);
1786 >        checkIncomplete(h1);
1787 >        rs[0].assertNotInvoked();
1788 >        rs[1].assertNotInvoked();
1789 >        f.completeExceptionally(ex);
1790 >        checkCompletedWithWrappedException(h0, ex);
1791 >        checkCompletedWithWrappedException(h1, ex);
1792 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1793 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1794 >        checkCompletedWithWrappedException(h2, ex);
1795 >        checkCompletedWithWrappedException(h3, ex);
1796 >        g.complete(v1);
1797  
1798 <        checkCompletedWithWrappedCFException(h, ex);
1799 <        (!fFirst ? f : g).complete(v1);
1798 >        // unspecified behavior - both source completions available
1799 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1800 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1801 >        try {
1802 >            assertEquals(inc(v1), h4.join());
1803 >            rs[4].assertValue(inc(v1));
1804 >        } catch (CompletionException ok) {
1805 >            checkCompletedWithWrappedException(h4, ex);
1806 >            rs[4].assertNotInvoked();
1807 >        }
1808 >        try {
1809 >            assertEquals(inc(v1), h5.join());
1810 >            rs[5].assertValue(inc(v1));
1811 >        } catch (CompletionException ok) {
1812 >            checkCompletedWithWrappedException(h5, ex);
1813 >            rs[5].assertNotInvoked();
1814 >        }
1815  
1816 <        assertEquals(0, r.invocationCount);
1817 <        checkCompletedNormally(!fFirst ? f : g, v1);
1818 <        checkCompletedWithWrappedCFException(fFirst ? f : g, ex);
1819 <        checkCompletedWithWrappedCFException(h, ex);
1816 >        checkCompletedExceptionally(f, ex);
1817 >        checkCompletedNormally(g, v1);
1818 >        checkCompletedWithWrappedException(h0, ex);
1819 >        checkCompletedWithWrappedException(h1, ex);
1820 >        checkCompletedWithWrappedException(h2, ex);
1821 >        checkCompletedWithWrappedException(h3, ex);
1822 >        checkCompletedWithWrappedException(h4, ex);
1823 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1824      }}
1825  
1826      public void testApplyToEither_exceptionalCompletion2() {
1827          for (ExecutionMode m : ExecutionMode.values())
1766        for (boolean reverseArgs : new boolean[] { true, false })
1828          for (boolean fFirst : new boolean[] { true, false })
1829          for (Integer v1 : new Integer[] { 1, null })
1830      {
1831          final CompletableFuture<Integer> f = new CompletableFuture<>();
1832          final CompletableFuture<Integer> g = new CompletableFuture<>();
1772        final IncFunction r1 = new IncFunction(m);
1773        final IncFunction r2 = new IncFunction(m);
1833          final CFException ex = new CFException();
1834 <        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1835 <        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1836 <        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1834 >        final IncFunction[] rs = new IncFunction[6];
1835 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1836 >
1837 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1838 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1839          if (fFirst) {
1840              f.complete(v1);
1841              g.completeExceptionally(ex);
# Line 1782 | Line 1843 | public class CompletableFutureTest exten
1843              g.completeExceptionally(ex);
1844              f.complete(v1);
1845          }
1846 <        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1846 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1847 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1848  
1849 <        // unspecified behavior
1849 >        // unspecified behavior - both source completions available
1850 >        try {
1851 >            assertEquals(inc(v1), h0.join());
1852 >            rs[0].assertValue(inc(v1));
1853 >        } catch (CompletionException ok) {
1854 >            checkCompletedWithWrappedException(h0, ex);
1855 >            rs[0].assertNotInvoked();
1856 >        }
1857          try {
1858              assertEquals(inc(v1), h1.join());
1859 <            assertEquals(1, r1.invocationCount);
1859 >            rs[1].assertValue(inc(v1));
1860          } catch (CompletionException ok) {
1861 <            checkCompletedWithWrappedCFException(h1, ex);
1862 <            assertEquals(0, r1.invocationCount);
1861 >            checkCompletedWithWrappedException(h1, ex);
1862 >            rs[1].assertNotInvoked();
1863          }
1795
1864          try {
1865              assertEquals(inc(v1), h2.join());
1866 <            assertEquals(1, r2.invocationCount);
1866 >            rs[2].assertValue(inc(v1));
1867          } catch (CompletionException ok) {
1868 <            checkCompletedWithWrappedCFException(h2, ex);
1869 <            assertEquals(0, r2.invocationCount);
1868 >            checkCompletedWithWrappedException(h2, ex);
1869 >            rs[2].assertNotInvoked();
1870 >        }
1871 >        try {
1872 >            assertEquals(inc(v1), h3.join());
1873 >            rs[3].assertValue(inc(v1));
1874 >        } catch (CompletionException ok) {
1875 >            checkCompletedWithWrappedException(h3, ex);
1876 >            rs[3].assertNotInvoked();
1877          }
1878  
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);
1879          checkCompletedNormally(f, v1);
1880 <        checkCompletedNormally(g, v2);
1880 >        checkCompletedExceptionally(g, ex);
1881      }}
1882  
1883      /**
1884       * applyToEither result completes exceptionally if either source cancelled
1885       */
1886 <    public void testApplyToEither_sourceCancelled1() {
1886 >    public void testApplyToEither_sourceCancelled() {
1887          for (ExecutionMode m : ExecutionMode.values())
1888          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1851        for (boolean createIncomplete : new boolean[] { true, false })
1852        for (boolean fFirst : new boolean[] { true, false })
1889          for (Integer v1 : new Integer[] { 1, null })
1890      {
1891          final CompletableFuture<Integer> f = new CompletableFuture<>();
1892          final CompletableFuture<Integer> g = new CompletableFuture<>();
1893 <        final IncFunction r = new IncFunction(m);
1893 >        final IncFunction[] rs = new IncFunction[6];
1894 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1895  
1896 <        if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1897 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1898 <        if (createIncomplete) {
1899 <            checkIncomplete(h);
1900 <            assertEquals(0, r.invocationCount);
1901 <            assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1902 <        }
1896 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1897 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1898 >        checkIncomplete(h0);
1899 >        checkIncomplete(h1);
1900 >        rs[0].assertNotInvoked();
1901 >        rs[1].assertNotInvoked();
1902 >        f.cancel(mayInterruptIfRunning);
1903 >        checkCompletedWithWrappedCancellationException(h0);
1904 >        checkCompletedWithWrappedCancellationException(h1);
1905 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1906 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1907 >        checkCompletedWithWrappedCancellationException(h2);
1908 >        checkCompletedWithWrappedCancellationException(h3);
1909 >        g.complete(v1);
1910  
1911 <        checkCompletedWithWrappedCancellationException(h);
1912 <        (!fFirst ? f : g).complete(v1);
1911 >        // unspecified behavior - both source completions available
1912 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1913 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1914 >        try {
1915 >            assertEquals(inc(v1), h4.join());
1916 >            rs[4].assertValue(inc(v1));
1917 >        } catch (CompletionException ok) {
1918 >            checkCompletedWithWrappedCancellationException(h4);
1919 >            rs[4].assertNotInvoked();
1920 >        }
1921 >        try {
1922 >            assertEquals(inc(v1), h5.join());
1923 >            rs[5].assertValue(inc(v1));
1924 >        } catch (CompletionException ok) {
1925 >            checkCompletedWithWrappedCancellationException(h5);
1926 >            rs[5].assertNotInvoked();
1927 >        }
1928  
1929 <        assertEquals(0, r.invocationCount);
1930 <        checkCompletedNormally(!fFirst ? f : g, v1);
1931 <        checkCancelled(fFirst ? f : g);
1932 <        checkCompletedWithWrappedCancellationException(h);
1929 >        checkCancelled(f);
1930 >        checkCompletedNormally(g, v1);
1931 >        checkCompletedWithWrappedCancellationException(h0);
1932 >        checkCompletedWithWrappedCancellationException(h1);
1933 >        checkCompletedWithWrappedCancellationException(h2);
1934 >        checkCompletedWithWrappedCancellationException(h3);
1935 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1936      }}
1937  
1938      public void testApplyToEither_sourceCancelled2() {
1939          for (ExecutionMode m : ExecutionMode.values())
1940          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1879        for (boolean reverseArgs : new boolean[] { true, false })
1941          for (boolean fFirst : new boolean[] { true, false })
1942          for (Integer v1 : new Integer[] { 1, null })
1943      {
1944          final CompletableFuture<Integer> f = new CompletableFuture<>();
1945          final CompletableFuture<Integer> g = new CompletableFuture<>();
1946 <        final IncFunction r1 = new IncFunction(m);
1947 <        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);
1946 >        final IncFunction[] rs = new IncFunction[6];
1947 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1948  
1949 <        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1949 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1950 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1951          if (fFirst) {
1952              f.complete(v1);
1953 <            assertTrue(g.cancel(mayInterruptIfRunning));
1953 >            g.cancel(mayInterruptIfRunning);
1954          } else {
1955 <            assertTrue(g.cancel(mayInterruptIfRunning));
1955 >            g.cancel(mayInterruptIfRunning);
1956              f.complete(v1);
1957          }
1958 <        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1958 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1959 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1960  
1961 <        // unspecified behavior
1961 >        // unspecified behavior - both source completions available
1962 >        try {
1963 >            assertEquals(inc(v1), h0.join());
1964 >            rs[0].assertValue(inc(v1));
1965 >        } catch (CompletionException ok) {
1966 >            checkCompletedWithWrappedCancellationException(h0);
1967 >            rs[0].assertNotInvoked();
1968 >        }
1969          try {
1970              assertEquals(inc(v1), h1.join());
1971 <            assertEquals(1, r1.invocationCount);
1971 >            rs[1].assertValue(inc(v1));
1972          } catch (CompletionException ok) {
1973              checkCompletedWithWrappedCancellationException(h1);
1974 <            assertEquals(0, r1.invocationCount);
1974 >            rs[1].assertNotInvoked();
1975          }
1909
1976          try {
1977              assertEquals(inc(v1), h2.join());
1978 <            assertEquals(1, r2.invocationCount);
1978 >            rs[2].assertValue(inc(v1));
1979          } catch (CompletionException ok) {
1980              checkCompletedWithWrappedCancellationException(h2);
1981 <            assertEquals(0, r2.invocationCount);
1981 >            rs[2].assertNotInvoked();
1982 >        }
1983 >        try {
1984 >            assertEquals(inc(v1), h3.join());
1985 >            rs[3].assertValue(inc(v1));
1986 >        } catch (CompletionException ok) {
1987 >            checkCompletedWithWrappedCancellationException(h3);
1988 >            rs[3].assertNotInvoked();
1989          }
1990  
1918        checkCancelled(g);
1991          checkCompletedNormally(f, v1);
1992 +        checkCancelled(g);
1993      }}
1994  
1995      /**
1996 <     * acceptEither result completes normally after normal completion
1924 <     * of either source
1996 >     * applyToEither result completes exceptionally if action does
1997       */
1998 <    public void testAcceptEither_normalCompletion1() {
1998 >    public void testApplyToEither_actionFailed() {
1999          for (ExecutionMode m : ExecutionMode.values())
2000          for (Integer v1 : new Integer[] { 1, null })
2001          for (Integer v2 : new Integer[] { 2, null })
2002      {
2003          final CompletableFuture<Integer> f = new CompletableFuture<>();
2004          final CompletableFuture<Integer> g = new CompletableFuture<>();
2005 <        final IncAction r = new IncAction();
2006 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2005 >        final FailingFunction[] rs = new FailingFunction[6];
2006 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2007  
2008 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2009 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2010          f.complete(v1);
2011 <        checkCompletedNormally(h, null);
2012 <        assertEquals(inc(v1), r.value);
2013 <        g.complete(v2);
2014 <
2015 <        checkCompletedNormally(f, v1);
2016 <        checkCompletedNormally(g, v2);
2017 <        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);
2011 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2012 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2013 >        checkCompletedWithWrappedCFException(h0);
2014 >        checkCompletedWithWrappedCFException(h1);
2015 >        checkCompletedWithWrappedCFException(h2);
2016 >        checkCompletedWithWrappedCFException(h3);
2017 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2018  
2019          g.complete(v2);
2020 <        checkCompletedNormally(h, null);
2021 <        assertEquals(inc(v2), r.value);
2022 <        f.complete(v1);
2020 >
2021 >        // unspecified behavior - both source completions available
2022 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2023 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2024 >
2025 >        checkCompletedWithWrappedCFException(h4);
2026 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2027 >                   Objects.equals(v2, rs[4].value));
2028 >        checkCompletedWithWrappedCFException(h5);
2029 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2030 >                   Objects.equals(v2, rs[5].value));
2031  
2032          checkCompletedNormally(f, v1);
2033          checkCompletedNormally(g, v2);
1963        checkCompletedNormally(h, null);
2034      }}
2035  
2036 <    public void testAcceptEither_normalCompletion3() {
2036 >    /**
2037 >     * acceptEither result completes normally after normal completion
2038 >     * of either source
2039 >     */
2040 >    public void testAcceptEither_normalCompletion() {
2041          for (ExecutionMode m : ExecutionMode.values())
2042          for (Integer v1 : new Integer[] { 1, null })
2043          for (Integer v2 : new Integer[] { 2, null })
2044      {
2045          final CompletableFuture<Integer> f = new CompletableFuture<>();
2046          final CompletableFuture<Integer> g = new CompletableFuture<>();
2047 <        final IncAction r = new IncAction();
2047 >        final NoopConsumer[] rs = new NoopConsumer[6];
2048 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2049  
2050 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2051 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2052 +        checkIncomplete(h0);
2053 +        checkIncomplete(h1);
2054 +        rs[0].assertNotInvoked();
2055 +        rs[1].assertNotInvoked();
2056          f.complete(v1);
2057 +        checkCompletedNormally(h0, null);
2058 +        checkCompletedNormally(h1, null);
2059 +        rs[0].assertValue(v1);
2060 +        rs[1].assertValue(v1);
2061 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2062 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2063 +        checkCompletedNormally(h2, null);
2064 +        checkCompletedNormally(h3, null);
2065 +        rs[2].assertValue(v1);
2066 +        rs[3].assertValue(v1);
2067          g.complete(v2);
1977        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2068  
2069 <        checkCompletedNormally(h, null);
2069 >        // unspecified behavior - both source completions available
2070 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2071 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2072 >        checkCompletedNormally(h4, null);
2073 >        checkCompletedNormally(h5, null);
2074 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2075 >                   Objects.equals(v2, rs[4].value));
2076 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2077 >                   Objects.equals(v2, rs[5].value));
2078 >
2079          checkCompletedNormally(f, v1);
2080          checkCompletedNormally(g, v2);
2081 <
2082 <        // unspecified behavior
2083 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2084 <                   Objects.equals(r.value, inc(v2)));
2081 >        checkCompletedNormally(h0, null);
2082 >        checkCompletedNormally(h1, null);
2083 >        checkCompletedNormally(h2, null);
2084 >        checkCompletedNormally(h3, null);
2085 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2086      }}
2087  
2088      /**
2089       * acceptEither result completes exceptionally after exceptional
2090       * completion of either source
2091       */
2092 <    public void testAcceptEither_exceptionalCompletion1() {
2092 >    public void testAcceptEither_exceptionalCompletion() {
2093          for (ExecutionMode m : ExecutionMode.values())
2094          for (Integer v1 : new Integer[] { 1, null })
2095      {
2096          final CompletableFuture<Integer> f = new CompletableFuture<>();
2097          final CompletableFuture<Integer> g = new CompletableFuture<>();
1998        final IncAction r = new IncAction();
1999        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2098          final CFException ex = new CFException();
2099 +        final NoopConsumer[] rs = new NoopConsumer[6];
2100 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2101  
2102 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2103 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2104 +        checkIncomplete(h0);
2105 +        checkIncomplete(h1);
2106 +        rs[0].assertNotInvoked();
2107 +        rs[1].assertNotInvoked();
2108          f.completeExceptionally(ex);
2109 <        checkCompletedWithWrappedCFException(h, ex);
2109 >        checkCompletedWithWrappedException(h0, ex);
2110 >        checkCompletedWithWrappedException(h1, ex);
2111 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2112 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2113 >        checkCompletedWithWrappedException(h2, ex);
2114 >        checkCompletedWithWrappedException(h3, ex);
2115 >
2116          g.complete(v1);
2117  
2118 <        assertEquals(0, r.invocationCount);
2118 >        // unspecified behavior - both source completions available
2119 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2120 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2121 >        try {
2122 >            assertNull(h4.join());
2123 >            rs[4].assertValue(v1);
2124 >        } catch (CompletionException ok) {
2125 >            checkCompletedWithWrappedException(h4, ex);
2126 >            rs[4].assertNotInvoked();
2127 >        }
2128 >        try {
2129 >            assertNull(h5.join());
2130 >            rs[5].assertValue(v1);
2131 >        } catch (CompletionException ok) {
2132 >            checkCompletedWithWrappedException(h5, ex);
2133 >            rs[5].assertNotInvoked();
2134 >        }
2135 >
2136 >        checkCompletedExceptionally(f, ex);
2137          checkCompletedNormally(g, v1);
2138 <        checkCompletedWithWrappedCFException(f, ex);
2139 <        checkCompletedWithWrappedCFException(h, ex);
2138 >        checkCompletedWithWrappedException(h0, ex);
2139 >        checkCompletedWithWrappedException(h1, ex);
2140 >        checkCompletedWithWrappedException(h2, ex);
2141 >        checkCompletedWithWrappedException(h3, ex);
2142 >        checkCompletedWithWrappedException(h4, ex);
2143 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2144      }}
2145  
2146      public void testAcceptEither_exceptionalCompletion2() {
2147          for (ExecutionMode m : ExecutionMode.values())
2148 +        for (boolean fFirst : new boolean[] { true, false })
2149          for (Integer v1 : new Integer[] { 1, null })
2150      {
2151          final CompletableFuture<Integer> f = new CompletableFuture<>();
2152          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();
2153          final CFException ex = new CFException();
2154 +        final NoopConsumer[] rs = new NoopConsumer[6];
2155 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2156  
2157 <        g.completeExceptionally(ex);
2158 <        f.complete(v1);
2159 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2157 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2158 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2159 >        if (fFirst) {
2160 >            f.complete(v1);
2161 >            g.completeExceptionally(ex);
2162 >        } else {
2163 >            g.completeExceptionally(ex);
2164 >            f.complete(v1);
2165 >        }
2166 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2167 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2168  
2169 <        // unspecified behavior
2046 <        Integer v;
2169 >        // unspecified behavior - both source completions available
2170          try {
2171 <            assertNull(h.join());
2172 <            assertEquals(1, r.invocationCount);
2050 <            assertEquals(inc(v1), r.value);
2171 >            assertEquals(null, h0.join());
2172 >            rs[0].assertValue(v1);
2173          } catch (CompletionException ok) {
2174 <            checkCompletedWithWrappedCFException(h, ex);
2175 <            assertEquals(0, r.invocationCount);
2174 >            checkCompletedWithWrappedException(h0, ex);
2175 >            rs[0].assertNotInvoked();
2176          }
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;
2177          try {
2178 <            assertNull(h.join());
2179 <            assertEquals(1, r.invocationCount);
2078 <            assertEquals(inc(v1), r.value);
2178 >            assertEquals(null, h1.join());
2179 >            rs[1].assertValue(v1);
2180          } catch (CompletionException ok) {
2181 <            checkCompletedWithWrappedCFException(h, ex);
2182 <            assertEquals(0, r.invocationCount);
2181 >            checkCompletedWithWrappedException(h1, ex);
2182 >            rs[1].assertNotInvoked();
2183 >        }
2184 >        try {
2185 >            assertEquals(null, h2.join());
2186 >            rs[2].assertValue(v1);
2187 >        } catch (CompletionException ok) {
2188 >            checkCompletedWithWrappedException(h2, ex);
2189 >            rs[2].assertNotInvoked();
2190 >        }
2191 >        try {
2192 >            assertEquals(null, h3.join());
2193 >            rs[3].assertValue(v1);
2194 >        } catch (CompletionException ok) {
2195 >            checkCompletedWithWrappedException(h3, ex);
2196 >            rs[3].assertNotInvoked();
2197          }
2198  
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);
2199          checkCompletedNormally(f, v1);
2200 <        checkCompletedNormally(g, v2);
2200 >        checkCompletedExceptionally(g, ex);
2201      }}
2202  
2203      /**
2204       * acceptEither result completes exceptionally if either source cancelled
2205       */
2206 <    public void testAcceptEither_sourceCancelled1() {
2206 >    public void testAcceptEither_sourceCancelled() {
2207          for (ExecutionMode m : ExecutionMode.values())
2208          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2209          for (Integer v1 : new Integer[] { 1, null })
2210      {
2211          final CompletableFuture<Integer> f = new CompletableFuture<>();
2212          final CompletableFuture<Integer> g = new CompletableFuture<>();
2213 <        final IncAction r = new IncAction();
2214 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2213 >        final NoopConsumer[] rs = new NoopConsumer[6];
2214 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2215  
2216 <        assertTrue(f.cancel(mayInterruptIfRunning));
2217 <        checkCompletedWithWrappedCancellationException(h);
2218 <        g.complete(v1);
2219 <
2220 <        checkCancelled(f);
2221 <        assertEquals(0, r.invocationCount);
2222 <        checkCompletedNormally(g, v1);
2223 <        checkCompletedWithWrappedCancellationException(h);
2224 <    }}
2225 <
2226 <    public void testAcceptEither_sourceCancelled2() {
2227 <        for (ExecutionMode m : ExecutionMode.values())
2228 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2151 <        for (Integer v1 : new Integer[] { 1, null })
2152 <    {
2153 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2154 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2155 <        final IncAction r = new IncAction();
2156 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2157 <
2158 <        assertTrue(g.cancel(mayInterruptIfRunning));
2159 <        checkCompletedWithWrappedCancellationException(h);
2160 <        f.complete(v1);
2161 <
2162 <        checkCancelled(g);
2163 <        assertEquals(0, r.invocationCount);
2164 <        checkCompletedNormally(f, v1);
2165 <        checkCompletedWithWrappedCancellationException(h);
2166 <    }}
2216 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2217 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2218 >        checkIncomplete(h0);
2219 >        checkIncomplete(h1);
2220 >        rs[0].assertNotInvoked();
2221 >        rs[1].assertNotInvoked();
2222 >        f.cancel(mayInterruptIfRunning);
2223 >        checkCompletedWithWrappedCancellationException(h0);
2224 >        checkCompletedWithWrappedCancellationException(h1);
2225 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2226 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2227 >        checkCompletedWithWrappedCancellationException(h2);
2228 >        checkCompletedWithWrappedCancellationException(h3);
2229  
2230 <    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);
2230 >        g.complete(v1);
2231  
2232 <        // unspecified behavior
2233 <        Integer v;
2232 >        // unspecified behavior - both source completions available
2233 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2234 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2235          try {
2236 <            assertNull(h.join());
2237 <            assertEquals(1, r.invocationCount);
2186 <            assertEquals(inc(v1), r.value);
2236 >            assertNull(h4.join());
2237 >            rs[4].assertValue(v1);
2238          } catch (CompletionException ok) {
2239 <            checkCompletedWithWrappedCancellationException(h);
2240 <            assertEquals(0, r.invocationCount);
2239 >            checkCompletedWithWrappedCancellationException(h4);
2240 >            rs[4].assertNotInvoked();
2241          }
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;
2242          try {
2243 <            assertNull(h.join());
2244 <            assertEquals(1, r.invocationCount);
2214 <            assertEquals(inc(v1), r.value);
2243 >            assertNull(h5.join());
2244 >            rs[5].assertValue(v1);
2245          } catch (CompletionException ok) {
2246 <            checkCompletedWithWrappedCancellationException(h);
2247 <            assertEquals(0, r.invocationCount);
2246 >            checkCompletedWithWrappedCancellationException(h5);
2247 >            rs[5].assertNotInvoked();
2248          }
2249  
2250          checkCancelled(f);
2251          checkCompletedNormally(g, v1);
2252 +        checkCompletedWithWrappedCancellationException(h0);
2253 +        checkCompletedWithWrappedCancellationException(h1);
2254 +        checkCompletedWithWrappedCancellationException(h2);
2255 +        checkCompletedWithWrappedCancellationException(h3);
2256 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2257      }}
2258  
2259      /**
2260 <     * runAfterEither result completes normally after normal completion
2226 <     * of either source
2260 >     * acceptEither result completes exceptionally if action does
2261       */
2262 <    public void testRunAfterEither_normalCompletion1() {
2262 >    public void testAcceptEither_actionFailed() {
2263          for (ExecutionMode m : ExecutionMode.values())
2264          for (Integer v1 : new Integer[] { 1, null })
2265          for (Integer v2 : new Integer[] { 2, null })
2266      {
2267          final CompletableFuture<Integer> f = new CompletableFuture<>();
2268          final CompletableFuture<Integer> g = new CompletableFuture<>();
2269 <        final Noop r = new Noop(m);
2270 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2269 >        final FailingConsumer[] rs = new FailingConsumer[6];
2270 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2271  
2272 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2273 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2274          f.complete(v1);
2275 <        checkCompletedNormally(h, null);
2276 <        assertEquals(1, r.invocationCount);
2275 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2276 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2277 >        checkCompletedWithWrappedCFException(h0);
2278 >        checkCompletedWithWrappedCFException(h1);
2279 >        checkCompletedWithWrappedCFException(h2);
2280 >        checkCompletedWithWrappedCFException(h3);
2281 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2282 >
2283          g.complete(v2);
2284  
2285 +        // unspecified behavior - both source completions available
2286 +        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2287 +        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2288 +
2289 +        checkCompletedWithWrappedCFException(h4);
2290 +        assertTrue(Objects.equals(v1, rs[4].value) ||
2291 +                   Objects.equals(v2, rs[4].value));
2292 +        checkCompletedWithWrappedCFException(h5);
2293 +        assertTrue(Objects.equals(v1, rs[5].value) ||
2294 +                   Objects.equals(v2, rs[5].value));
2295 +
2296          checkCompletedNormally(f, v1);
2297          checkCompletedNormally(g, v2);
2245        checkCompletedNormally(h, null);
2246        assertEquals(1, r.invocationCount);
2298      }}
2299  
2300 <    public void testRunAfterEither_normalCompletion2() {
2300 >    /**
2301 >     * runAfterEither result completes normally after normal completion
2302 >     * of either source
2303 >     */
2304 >    public void testRunAfterEither_normalCompletion() {
2305          for (ExecutionMode m : ExecutionMode.values())
2306          for (Integer v1 : new Integer[] { 1, null })
2307          for (Integer v2 : new Integer[] { 2, null })
2308      {
2309          final CompletableFuture<Integer> f = new CompletableFuture<>();
2310          final CompletableFuture<Integer> g = new CompletableFuture<>();
2311 <        final Noop r = new Noop(m);
2312 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2311 >        final Noop[] rs = new Noop[6];
2312 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2313  
2314 <        g.complete(v2);
2315 <        checkCompletedNormally(h, null);
2316 <        assertEquals(1, r.invocationCount);
2314 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2315 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2316 >        checkIncomplete(h0);
2317 >        checkIncomplete(h1);
2318 >        rs[0].assertNotInvoked();
2319 >        rs[1].assertNotInvoked();
2320          f.complete(v1);
2321 +        checkCompletedNormally(h0, null);
2322 +        checkCompletedNormally(h1, null);
2323 +        rs[0].assertInvoked();
2324 +        rs[1].assertInvoked();
2325 +        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2326 +        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2327 +        checkCompletedNormally(h2, null);
2328 +        checkCompletedNormally(h3, null);
2329 +        rs[2].assertInvoked();
2330 +        rs[3].assertInvoked();
2331  
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);
2332          g.complete(v2);
2281        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2333  
2334 <        checkCompletedNormally(h, null);
2334 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2335 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2336 >
2337          checkCompletedNormally(f, v1);
2338          checkCompletedNormally(g, v2);
2339 <        assertEquals(1, r.invocationCount);
2339 >        checkCompletedNormally(h0, null);
2340 >        checkCompletedNormally(h1, null);
2341 >        checkCompletedNormally(h2, null);
2342 >        checkCompletedNormally(h3, null);
2343 >        checkCompletedNormally(h4, null);
2344 >        checkCompletedNormally(h5, null);
2345 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2346      }}
2347  
2348      /**
2349       * runAfterEither result completes exceptionally after exceptional
2350       * completion of either source
2351       */
2352 <    public void testRunAfterEither_exceptionalCompletion1() {
2352 >    public void testRunAfterEither_exceptionalCompletion() {
2353          for (ExecutionMode m : ExecutionMode.values())
2354          for (Integer v1 : new Integer[] { 1, null })
2355      {
2356          final CompletableFuture<Integer> f = new CompletableFuture<>();
2357          final CompletableFuture<Integer> g = new CompletableFuture<>();
2299        final Noop r = new Noop(m);
2300        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2358          final CFException ex = new CFException();
2359 +        final Noop[] rs = new Noop[6];
2360 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2361  
2362 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2363 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2364 +        checkIncomplete(h0);
2365 +        checkIncomplete(h1);
2366 +        rs[0].assertNotInvoked();
2367 +        rs[1].assertNotInvoked();
2368          f.completeExceptionally(ex);
2369 <        checkCompletedWithWrappedCFException(h, ex);
2369 >        checkCompletedWithWrappedException(h0, ex);
2370 >        checkCompletedWithWrappedException(h1, ex);
2371 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2372 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2373 >        checkCompletedWithWrappedException(h2, ex);
2374 >        checkCompletedWithWrappedException(h3, ex);
2375 >
2376          g.complete(v1);
2377  
2378 <        assertEquals(0, r.invocationCount);
2378 >        // unspecified behavior - both source completions available
2379 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2380 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2381 >        try {
2382 >            assertNull(h4.join());
2383 >            rs[4].assertInvoked();
2384 >        } catch (CompletionException ok) {
2385 >            checkCompletedWithWrappedException(h4, ex);
2386 >            rs[4].assertNotInvoked();
2387 >        }
2388 >        try {
2389 >            assertNull(h5.join());
2390 >            rs[5].assertInvoked();
2391 >        } catch (CompletionException ok) {
2392 >            checkCompletedWithWrappedException(h5, ex);
2393 >            rs[5].assertNotInvoked();
2394 >        }
2395 >
2396 >        checkCompletedExceptionally(f, ex);
2397          checkCompletedNormally(g, v1);
2398 <        checkCompletedWithWrappedCFException(f, ex);
2399 <        checkCompletedWithWrappedCFException(h, ex);
2398 >        checkCompletedWithWrappedException(h0, ex);
2399 >        checkCompletedWithWrappedException(h1, ex);
2400 >        checkCompletedWithWrappedException(h2, ex);
2401 >        checkCompletedWithWrappedException(h3, ex);
2402 >        checkCompletedWithWrappedException(h4, ex);
2403 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2404      }}
2405  
2406      public void testRunAfterEither_exceptionalCompletion2() {
2407          for (ExecutionMode m : ExecutionMode.values())
2408 +        for (boolean fFirst : new boolean[] { true, false })
2409          for (Integer v1 : new Integer[] { 1, null })
2410      {
2411          final CompletableFuture<Integer> f = new CompletableFuture<>();
2412          final CompletableFuture<Integer> g = new CompletableFuture<>();
2319        final Noop r = new Noop(m);
2320        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2321        final CFException ex = new CFException();
2322
2323        g.completeExceptionally(ex);
2324        checkCompletedWithWrappedCFException(h, ex);
2325        f.complete(v1);
2326
2327        assertEquals(0, r.invocationCount);
2328        checkCompletedNormally(f, v1);
2329        checkCompletedWithWrappedCFException(g, ex);
2330        checkCompletedWithWrappedCFException(h, ex);
2331    }}
2332
2333    public void testRunAfterEither_exceptionalCompletion3() {
2334        for (ExecutionMode m : ExecutionMode.values())
2335        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);
2413          final CFException ex = new CFException();
2414 +        final Noop[] rs = new Noop[6];
2415 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2416  
2417 <        g.completeExceptionally(ex);
2418 <        f.complete(v1);
2419 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2417 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2418 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2419 >        if (fFirst) {
2420 >            f.complete(v1);
2421 >            g.completeExceptionally(ex);
2422 >        } else {
2423 >            g.completeExceptionally(ex);
2424 >            f.complete(v1);
2425 >        }
2426 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2427 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2428  
2429 <        // unspecified behavior
2430 <        Integer v;
2429 >        // unspecified behavior - both source completions available
2430 >        try {
2431 >            assertEquals(null, h0.join());
2432 >            rs[0].assertInvoked();
2433 >        } catch (CompletionException ok) {
2434 >            checkCompletedWithWrappedException(h0, ex);
2435 >            rs[0].assertNotInvoked();
2436 >        }
2437 >        try {
2438 >            assertEquals(null, h1.join());
2439 >            rs[1].assertInvoked();
2440 >        } catch (CompletionException ok) {
2441 >            checkCompletedWithWrappedException(h1, ex);
2442 >            rs[1].assertNotInvoked();
2443 >        }
2444 >        try {
2445 >            assertEquals(null, h2.join());
2446 >            rs[2].assertInvoked();
2447 >        } catch (CompletionException ok) {
2448 >            checkCompletedWithWrappedException(h2, ex);
2449 >            rs[2].assertNotInvoked();
2450 >        }
2451          try {
2452 <            assertNull(h.join());
2453 <            assertEquals(1, r.invocationCount);
2452 >            assertEquals(null, h3.join());
2453 >            rs[3].assertInvoked();
2454          } catch (CompletionException ok) {
2455 <            checkCompletedWithWrappedCFException(h, ex);
2456 <            assertEquals(0, r.invocationCount);
2455 >            checkCompletedWithWrappedException(h3, ex);
2456 >            rs[3].assertNotInvoked();
2457          }
2458  
2356        checkCompletedWithWrappedCFException(g, ex);
2459          checkCompletedNormally(f, v1);
2460 +        checkCompletedExceptionally(g, ex);
2461      }}
2462  
2463 <    public void testRunAfterEither_exceptionalCompletion4() {
2463 >    /**
2464 >     * runAfterEither result completes exceptionally if either source cancelled
2465 >     */
2466 >    public void testRunAfterEither_sourceCancelled() {
2467          for (ExecutionMode m : ExecutionMode.values())
2468 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2469          for (Integer v1 : new Integer[] { 1, null })
2470      {
2471          final CompletableFuture<Integer> f = new CompletableFuture<>();
2472          final CompletableFuture<Integer> g = new CompletableFuture<>();
2473 <        final Noop r = new Noop(m);
2474 <        final CFException ex = new CFException();
2473 >        final Noop[] rs = new Noop[6];
2474 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2475 >
2476 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2477 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2478 >        checkIncomplete(h0);
2479 >        checkIncomplete(h1);
2480 >        rs[0].assertNotInvoked();
2481 >        rs[1].assertNotInvoked();
2482 >        f.cancel(mayInterruptIfRunning);
2483 >        checkCompletedWithWrappedCancellationException(h0);
2484 >        checkCompletedWithWrappedCancellationException(h1);
2485 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2486 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2487 >        checkCompletedWithWrappedCancellationException(h2);
2488 >        checkCompletedWithWrappedCancellationException(h3);
2489  
2369        f.completeExceptionally(ex);
2490          g.complete(v1);
2371        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2491  
2492 <        // unspecified behavior
2493 <        Integer v;
2492 >        // unspecified behavior - both source completions available
2493 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2494 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2495 >        try {
2496 >            assertNull(h4.join());
2497 >            rs[4].assertInvoked();
2498 >        } catch (CompletionException ok) {
2499 >            checkCompletedWithWrappedCancellationException(h4);
2500 >            rs[4].assertNotInvoked();
2501 >        }
2502          try {
2503 <            assertNull(h.join());
2504 <            assertEquals(1, r.invocationCount);
2503 >            assertNull(h5.join());
2504 >            rs[5].assertInvoked();
2505          } catch (CompletionException ok) {
2506 <            checkCompletedWithWrappedCFException(h, ex);
2507 <            assertEquals(0, r.invocationCount);
2506 >            checkCompletedWithWrappedCancellationException(h5);
2507 >            rs[5].assertNotInvoked();
2508          }
2509  
2510 <        checkCompletedWithWrappedCFException(f, ex);
2510 >        checkCancelled(f);
2511          checkCompletedNormally(g, v1);
2512 +        checkCompletedWithWrappedCancellationException(h0);
2513 +        checkCompletedWithWrappedCancellationException(h1);
2514 +        checkCompletedWithWrappedCancellationException(h2);
2515 +        checkCompletedWithWrappedCancellationException(h3);
2516 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2517      }}
2518  
2519      /**
2520       * runAfterEither result completes exceptionally if action does
2521       */
2522 <    public void testRunAfterEither_actionFailed1() {
2522 >    public void testRunAfterEither_actionFailed() {
2523          for (ExecutionMode m : ExecutionMode.values())
2524          for (Integer v1 : new Integer[] { 1, null })
2525          for (Integer v2 : new Integer[] { 2, null })
2526      {
2527          final CompletableFuture<Integer> f = new CompletableFuture<>();
2528          final CompletableFuture<Integer> g = new CompletableFuture<>();
2529 <        final FailingRunnable r = new FailingRunnable(m);
2530 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2529 >        final FailingRunnable[] rs = new FailingRunnable[6];
2530 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2531  
2532 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2533 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2534          f.complete(v1);
2535 <        checkCompletedWithWrappedCFException(h);
2535 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2536 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2537 >        checkCompletedWithWrappedCFException(h0);
2538 >        checkCompletedWithWrappedCFException(h1);
2539 >        checkCompletedWithWrappedCFException(h2);
2540 >        checkCompletedWithWrappedCFException(h3);
2541 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2542          g.complete(v2);
2543 <        checkCompletedNormally(f, v1);
2544 <        checkCompletedNormally(g, v2);
2545 <    }}
2546 <
2407 <    public void testRunAfterEither_actionFailed2() {
2408 <        for (ExecutionMode m : ExecutionMode.values())
2409 <        for (Integer v1 : new Integer[] { 1, null })
2410 <        for (Integer v2 : new Integer[] { 2, null })
2411 <    {
2412 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2413 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2414 <        final FailingRunnable r = new FailingRunnable(m);
2415 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2543 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2544 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2545 >        checkCompletedWithWrappedCFException(h4);
2546 >        checkCompletedWithWrappedCFException(h5);
2547  
2417        g.complete(v2);
2418        checkCompletedWithWrappedCFException(h);
2419        f.complete(v1);
2548          checkCompletedNormally(f, v1);
2549          checkCompletedNormally(g, v2);
2550 <    }}
2423 <
2424 <    /**
2425 <     * runAfterEither result completes exceptionally if either source cancelled
2426 <     */
2427 <    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() {
2448 <        for (ExecutionMode m : ExecutionMode.values())
2449 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2450 <        for (Integer v1 : new Integer[] { 1, null })
2451 <    {
2452 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2453 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2454 <        final Noop r = new Noop(m);
2455 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2456 <
2457 <        assertTrue(g.cancel(mayInterruptIfRunning));
2458 <        checkCompletedWithWrappedCancellationException(h);
2459 <        f.complete(v1);
2460 <
2461 <        checkCancelled(g);
2462 <        assertEquals(0, r.invocationCount);
2463 <        checkCompletedNormally(f, v1);
2464 <        checkCompletedWithWrappedCancellationException(h);
2465 <    }}
2466 <
2467 <    public void testRunAfterEither_sourceCancelled3() {
2468 <        for (ExecutionMode m : ExecutionMode.values())
2469 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2470 <        for (Integer v1 : new Integer[] { 1, null })
2471 <    {
2472 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2473 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2474 <        final Noop r = new Noop(m);
2475 <
2476 <        assertTrue(g.cancel(mayInterruptIfRunning));
2477 <        f.complete(v1);
2478 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2479 <
2480 <        // unspecified behavior
2481 <        Integer v;
2482 <        try {
2483 <            assertNull(h.join());
2484 <            assertEquals(1, r.invocationCount);
2485 <        } catch (CompletionException ok) {
2486 <            checkCompletedWithWrappedCancellationException(h);
2487 <            assertEquals(0, r.invocationCount);
2488 <        }
2489 <
2490 <        checkCancelled(g);
2491 <        checkCompletedNormally(f, v1);
2492 <    }}
2493 <
2494 <    public void testRunAfterEither_sourceCancelled4() {
2495 <        for (ExecutionMode m : ExecutionMode.values())
2496 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2497 <        for (Integer v1 : new Integer[] { 1, null })
2498 <    {
2499 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2500 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2501 <        final Noop r = new Noop(m);
2502 <
2503 <        assertTrue(f.cancel(mayInterruptIfRunning));
2504 <        g.complete(v1);
2505 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2506 <
2507 <        // unspecified behavior
2508 <        Integer v;
2509 <        try {
2510 <            assertNull(h.join());
2511 <            assertEquals(1, r.invocationCount);
2512 <        } catch (CompletionException ok) {
2513 <            checkCompletedWithWrappedCancellationException(h);
2514 <            assertEquals(0, r.invocationCount);
2515 <        }
2516 <
2517 <        checkCancelled(f);
2518 <        checkCompletedNormally(g, v1);
2550 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2551      }}
2552  
2553      /**
# Line 2534 | Line 2566 | public class CompletableFutureTest exten
2566  
2567          checkCompletedNormally(g, inc(v1));
2568          checkCompletedNormally(f, v1);
2569 <        assertEquals(1, r.invocationCount);
2569 >        r.assertValue(v1);
2570      }}
2571  
2572      /**
# Line 2552 | Line 2584 | public class CompletableFutureTest exten
2584          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2585          if (createIncomplete) f.completeExceptionally(ex);
2586  
2587 <        checkCompletedWithWrappedCFException(g, ex);
2588 <        checkCompletedWithWrappedCFException(f, ex);
2589 <        assertEquals(0, r.invocationCount);
2587 >        checkCompletedWithWrappedException(g, ex);
2588 >        checkCompletedExceptionally(f, ex);
2589 >        r.assertNotInvoked();
2590      }}
2591  
2592      /**
# Line 2614 | Line 2646 | public class CompletableFutureTest exten
2646       */
2647      public void testAllOf_normal() throws Exception {
2648          for (int k = 1; k < 20; ++k) {
2649 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2649 >            CompletableFuture<Integer>[] fs
2650 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2651              for (int i = 0; i < k; ++i)
2652                  fs[i] = new CompletableFuture<>();
2653              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
# Line 2628 | Line 2661 | public class CompletableFutureTest exten
2661          }
2662      }
2663  
2664 +    public void testAllOf_backwards() throws Exception {
2665 +        for (int k = 1; k < 20; ++k) {
2666 +            CompletableFuture<Integer>[] fs
2667 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2668 +            for (int i = 0; i < k; ++i)
2669 +                fs[i] = new CompletableFuture<>();
2670 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2671 +            for (int i = k - 1; i >= 0; i--) {
2672 +                checkIncomplete(f);
2673 +                checkIncomplete(CompletableFuture.allOf(fs));
2674 +                fs[i].complete(one);
2675 +            }
2676 +            checkCompletedNormally(f, null);
2677 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2678 +        }
2679 +    }
2680 +
2681      /**
2682       * anyOf(no component futures) returns an incomplete future
2683       */
# Line 2806 | Line 2856 | public class CompletableFutureTest exten
2856          final CompletableFuture<Integer> g = m.whenComplete
2857              (f,
2858               (Integer x, Throwable t) -> {
2859 +                m.checkExecutionMode();
2860                  threadAssertSame(x, v1);
2861                  threadAssertNull(t);
2862                  a.getAndIncrement();
# Line 2833 | Line 2884 | public class CompletableFutureTest exten
2884          final CompletableFuture<Integer> g = m.whenComplete
2885              (f,
2886               (Integer x, Throwable t) -> {
2887 +                m.checkExecutionMode();
2888                  threadAssertNull(x);
2889                  threadAssertSame(t, ex);
2890                  a.getAndIncrement();
2891              });
2892          if (createIncomplete) f.completeExceptionally(ex);
2893 <        checkCompletedWithWrappedCFException(f, ex);
2894 <        checkCompletedWithWrappedCFException(g, ex);
2893 >        checkCompletedExceptionally(f, ex);
2894 >        checkCompletedWithWrappedException(g, ex);
2895          assertEquals(1, a.get());
2896      }}
2897  
# Line 2858 | Line 2910 | public class CompletableFutureTest exten
2910          final CompletableFuture<Integer> g = m.whenComplete
2911              (f,
2912               (Integer x, Throwable t) -> {
2913 +                m.checkExecutionMode();
2914                  threadAssertNull(x);
2915                  threadAssertTrue(t instanceof CancellationException);
2916                  a.getAndIncrement();
2917              });
2918          if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2919  
2867        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2920          checkCompletedWithWrappedCancellationException(g);
2921          checkCancelled(f);
2922          assertEquals(1, a.get());
# Line 2886 | Line 2938 | public class CompletableFutureTest exten
2938          final CompletableFuture<Integer> g = m.whenComplete
2939              (f,
2940               (Integer x, Throwable t) -> {
2941 +                m.checkExecutionMode();
2942                  threadAssertSame(x, v1);
2943                  threadAssertNull(t);
2944                  a.getAndIncrement();
# Line 2893 | Line 2946 | public class CompletableFutureTest exten
2946              });
2947          if (createIncomplete) f.complete(v1);
2948          checkCompletedNormally(f, v1);
2949 <        checkCompletedWithWrappedCFException(g, ex);
2949 >        checkCompletedWithWrappedException(g, ex);
2950          assertEquals(1, a.get());
2951      }}
2952  
# Line 2916 | Line 2969 | public class CompletableFutureTest exten
2969          final CompletableFuture<Integer> g = m.whenComplete
2970              (f,
2971               (Integer x, Throwable t) -> {
2972 +                m.checkExecutionMode();
2973                  threadAssertSame(t, ex1);
2974                  threadAssertNull(x);
2975                  a.getAndIncrement();
# Line 2923 | Line 2977 | public class CompletableFutureTest exten
2977              });
2978          if (createIncomplete) f.completeExceptionally(ex1);
2979  
2980 <        checkCompletedWithWrappedCFException(f, ex1);
2981 <        checkCompletedWithWrappedCFException(g, ex1);
2980 >        checkCompletedExceptionally(f, ex1);
2981 >        checkCompletedWithWrappedException(g, ex1);
2982          assertEquals(1, a.get());
2983      }}
2984  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines