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.34 by jsr166, Sun Jun 1 21:17:05 2014 UTC vs.
Revision 1.37 by jsr166, Sun Jun 1 23:20:19 2014 UTC

# Line 16 | Line 16 | import java.util.concurrent.ExecutionExc
16   import java.util.concurrent.Future;
17   import java.util.concurrent.CompletableFuture;
18   import java.util.concurrent.CompletionException;
19 + import java.util.concurrent.CompletionStage;
20   import java.util.concurrent.TimeoutException;
21   import java.util.concurrent.atomic.AtomicInteger;
22   import static java.util.concurrent.TimeUnit.MILLISECONDS;
# Line 317 | Line 318 | public class CompletableFutureTest exten
318  
319      // Choose non-commutative actions for better coverage
320  
321 +    // A non-commutative function that handles null values as well,
322 +    // and produces null values occasionally.
323 +    public static Integer subtract(Integer x, Integer y) {
324 +        return (x == null && y == null) ? null :
325 +            ((x == null) ? 42 : x.intValue())
326 +            - ((y == null) ? 99 : y.intValue());
327 +    }
328 +
329      static final Supplier<Integer> supplyOne =
330          () -> Integer.valueOf(1);
331      static final Function<Integer, Integer> inc =
332          (Integer x) -> Integer.valueOf(x.intValue() + 1);
333      static final BiFunction<Integer, Integer, Integer> subtract =
334 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
334 >        (Integer x, Integer y) -> subtract(x, y);
335      static final class IncAction implements Consumer<Integer> {
336          int value;
337          public void accept(Integer x) { value = x.intValue() + 1; }
338      }
339      static final class SubtractAction implements BiConsumer<Integer, Integer> {
340 <        int value;
340 >        int invocationCount = 0;
341 >        Integer value;
342 >        // Check this action was invoked exactly once when result is computed.
343 >        public boolean ran() { return invocationCount == 1; }
344          public void accept(Integer x, Integer y) {
345 <            value = x.intValue() - y.intValue();
345 >            invocationCount++;
346 >            value = subtract(x, y);
347 >        }
348 >    }
349 >    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
350 >        int invocationCount = 0;
351 >        Integer value;
352 >        // Check this action was invoked exactly once when result is computed.
353 >        public boolean ran() { return invocationCount == 1; }
354 >        public Integer apply(Integer x, Integer y) {
355 >            invocationCount++;
356 >            return value = subtract(x, y);
357          }
358      }
359      static final class Noop implements Runnable {
# Line 405 | Line 428 | public class CompletableFutureTest exten
428      }
429  
430      /**
431 +     * Permits the testing of parallel code for the 3 different
432 +     * execution modes without repeating all the testing code.
433 +     */
434 +    enum ExecutionMode {
435 +        DEFAULT {
436 +            public <T,U> CompletableFuture<Void> runAfterBoth
437 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
438 +                return f.runAfterBoth(g, a);
439 +            }
440 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
441 +                (CompletableFuture<T> f,
442 +                 CompletionStage<? extends U> g,
443 +                 BiConsumer<? super T,? super U> a) {
444 +                return f.thenAcceptBoth(g, a);
445 +            }
446 +            public <T,U,V> CompletableFuture<V> thenCombine
447 +                (CompletableFuture<T> f,
448 +                 CompletionStage<? extends U> g,
449 +                 BiFunction<? super T,? super U,? extends V> a) {
450 +                return f.thenCombine(g, a);
451 +            }
452 +        },
453 +
454 + //             /** Experimental way to do more testing */
455 + //         REVERSE_DEFAULT {
456 + //             public <T,U> CompletableFuture<Void> runAfterBoth
457 + //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
458 + //                 return g.runAfterBoth(f, a);
459 + //             }
460 + //             public <T,U> CompletableFuture<Void> thenAcceptBoth
461 + //                 (CompletableFuture<T> f,
462 + //                  CompletionStage<? extends U> g,
463 + //                  BiConsumer<? super T,? super U> a) {
464 + //                 return DEFAULT.thenAcceptBoth(f, g, a);
465 + //             }
466 + //         },
467 +
468 +        DEFAULT_ASYNC {
469 +            public <T,U> CompletableFuture<Void> runAfterBoth
470 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
471 +                return f.runAfterBothAsync(g, a);
472 +            }
473 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
474 +                (CompletableFuture<T> f,
475 +                 CompletionStage<? extends U> g,
476 +                 BiConsumer<? super T,? super U> a) {
477 +                return f.thenAcceptBothAsync(g, a);
478 +            }
479 +            public <T,U,V> CompletableFuture<V> thenCombine
480 +                (CompletableFuture<T> f,
481 +                 CompletionStage<? extends U> g,
482 +                 BiFunction<? super T,? super U,? extends V> a) {
483 +                return f.thenCombineAsync(g, a);
484 +            }
485 +        },
486 +
487 + //         REVERSE_DEFAULT_ASYNC {
488 + //             public <T,U> CompletableFuture<Void> runAfterBoth
489 + //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
490 + //                 return f.runAfterBothAsync(g, a);
491 + //             }
492 + //             public <T,U> CompletableFuture<Void> thenAcceptBoth
493 + //                 (CompletableFuture<T> f,
494 + //                  CompletionStage<? extends U> g,
495 + //                  BiConsumer<? super T,? super U> a) {
496 + //                 return DEFAULT_ASYNC.thenAcceptBoth(f, g, a);
497 + //             }
498 + //         },
499 +
500 +        EXECUTOR {
501 +            public <T,U> CompletableFuture<Void> runAfterBoth
502 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
503 +                return f.runAfterBothAsync(g, a, new ThreadExecutor());
504 +            }
505 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
506 +                (CompletableFuture<T> f,
507 +                 CompletionStage<? extends U> g,
508 +                 BiConsumer<? super T,? super U> a) {
509 +                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
510 +            }
511 +            public <T,U,V> CompletableFuture<V> thenCombine
512 +                (CompletableFuture<T> f,
513 +                 CompletionStage<? extends U> g,
514 +                 BiFunction<? super T,? super U,? extends V> a) {
515 +                return f.thenCombineAsync(g, a, new ThreadExecutor());
516 +            }
517 +        };
518 +
519 +        public abstract <T,U> CompletableFuture<Void> runAfterBoth
520 +            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
521 +        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
522 +            (CompletableFuture<T> f,
523 +             CompletionStage<? extends U> g,
524 +             BiConsumer<? super T,? super U> a);
525 +        public abstract <T,U,V> CompletableFuture<V> thenCombine
526 +            (CompletableFuture<T> f,
527 +             CompletionStage<? extends U> g,
528 +             BiFunction<? super T,? super U,? extends V> a);
529 +    }
530 +
531 +    /**
532       * exceptionally action completes with function value on source
533       * exception; otherwise with source value
534       */
# Line 696 | Line 820 | public class CompletableFutureTest exten
820       * thenCombine result completes normally after normal completion
821       * of sources
822       */
823 <    public void testThenCombine() {
824 <        CompletableFuture<Integer> f, g, h;
823 >    public void testThenCombine_normalCompletion1() {
824 >        for (ExecutionMode m : ExecutionMode.values())
825 >        for (Integer v1 : new Integer[] { 1, null })
826 >        for (Integer v2 : new Integer[] { 2, null }) {
827  
828 <        f = new CompletableFuture<>();
829 <        g = new CompletableFuture<>();
830 <        h = f.thenCombine(g, subtract);
831 <        f.complete(3);
828 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
829 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
830 >        final SubtractFunction r = new SubtractFunction();
831 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
832 >
833 >        f.complete(v1);
834          checkIncomplete(h);
835 <        g.complete(1);
836 <        checkCompletedNormally(h, 2);
835 >        assertFalse(r.ran());
836 >        g.complete(v2);
837  
838 <        f = new CompletableFuture<>();
839 <        g = new CompletableFuture<>();
840 <        h = f.thenCombine(g, subtract);
841 <        g.complete(1);
838 >        checkCompletedNormally(h, subtract(v1, v2));
839 >        checkCompletedNormally(f, v1);
840 >        checkCompletedNormally(g, v2);
841 >        }
842 >    }
843 >
844 >    public void testThenCombine_normalCompletion2() {
845 >        for (ExecutionMode m : ExecutionMode.values())
846 >        for (Integer v1 : new Integer[] { 1, null })
847 >        for (Integer v2 : new Integer[] { 2, null }) {
848 >
849 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
850 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
851 >        final SubtractFunction r = new SubtractFunction();
852 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
853 >
854 >        g.complete(v2);
855          checkIncomplete(h);
856 <        f.complete(3);
857 <        checkCompletedNormally(h, 2);
856 >        assertFalse(r.ran());
857 >        f.complete(v1);
858  
859 <        f = new CompletableFuture<>();
860 <        g = new CompletableFuture<>();
861 <        g.complete(1);
862 <        f.complete(3);
863 <        h = f.thenCombine(g, subtract);
864 <        checkCompletedNormally(h, 2);
859 >        checkCompletedNormally(h, subtract(v1, v2));
860 >        checkCompletedNormally(f, v1);
861 >        checkCompletedNormally(g, v2);
862 >        }
863 >    }
864 >
865 >    public void testThenCombine_normalCompletion3() {
866 >        for (ExecutionMode m : ExecutionMode.values())
867 >        for (Integer v1 : new Integer[] { 1, null })
868 >        for (Integer v2 : new Integer[] { 2, null }) {
869 >
870 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
871 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
872 >        final SubtractFunction r = new SubtractFunction();
873 >
874 >        g.complete(v2);
875 >        f.complete(v1);
876 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
877 >
878 >        checkCompletedNormally(h, subtract(v1, v2));
879 >        checkCompletedNormally(f, v1);
880 >        checkCompletedNormally(g, v2);
881 >        }
882 >    }
883 >
884 >    public void testThenCombine_normalCompletion4() {
885 >        for (ExecutionMode m : ExecutionMode.values())
886 >        for (Integer v1 : new Integer[] { 1, null })
887 >        for (Integer v2 : new Integer[] { 2, null }) {
888 >
889 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
890 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
891 >        final SubtractFunction r = new SubtractFunction();
892 >
893 >        f.complete(v1);
894 >        g.complete(v2);
895 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
896 >
897 >        checkCompletedNormally(h, subtract(v1, v2));
898 >        checkCompletedNormally(f, v1);
899 >        checkCompletedNormally(g, v2);
900 >        }
901      }
902  
903      /**
904       * thenCombine result completes exceptionally after exceptional
905       * completion of either source
906       */
907 <    public void testThenCombine2() {
908 <        CompletableFuture<Integer> f, g, h;
907 >    public void testThenCombine_exceptionalCompletion1() {
908 >        for (ExecutionMode m : ExecutionMode.values())
909 >        for (Integer v1 : new Integer[] { 1, null }) {
910  
911 <        f = new CompletableFuture<>();
912 <        g = new CompletableFuture<>();
913 <        h = f.thenCombine(g, subtract);
914 <        f.completeExceptionally(new CFException());
911 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
912 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
913 >        final SubtractFunction r = new SubtractFunction();
914 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
915 >        final CFException ex = new CFException();
916 >
917 >        f.completeExceptionally(ex);
918          checkIncomplete(h);
919 <        g.complete(1);
739 <        checkCompletedWithWrappedCFException(h);
919 >        g.complete(v1);
920  
921 <        f = new CompletableFuture<>();
922 <        g = new CompletableFuture<>();
923 <        h = f.thenCombine(g, subtract);
924 <        g.completeExceptionally(new CFException());
921 >        checkCompletedWithWrappedCFException(h, ex);
922 >        checkCompletedWithWrappedCFException(f, ex);
923 >        assertFalse(r.ran());
924 >        checkCompletedNormally(g, v1);
925 >        }
926 >    }
927 >
928 >    public void testThenCombine_exceptionalCompletion2() {
929 >        for (ExecutionMode m : ExecutionMode.values())
930 >        for (Integer v1 : new Integer[] { 1, null }) {
931 >
932 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
933 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
934 >        final SubtractFunction r = new SubtractFunction();
935 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
936 >        final CFException ex = new CFException();
937 >
938 >        g.completeExceptionally(ex);
939          checkIncomplete(h);
940 <        f.complete(3);
747 <        checkCompletedWithWrappedCFException(h);
940 >        f.complete(v1);
941  
942 <        f = new CompletableFuture<>();
943 <        g = new CompletableFuture<>();
944 <        f.complete(3);
945 <        g.completeExceptionally(new CFException());
946 <        h = f.thenCombine(g, subtract);
947 <        checkCompletedWithWrappedCFException(h);
942 >        checkCompletedWithWrappedCFException(h, ex);
943 >        checkCompletedWithWrappedCFException(g, ex);
944 >        assertFalse(r.ran());
945 >        checkCompletedNormally(f, v1);
946 >        }
947 >    }
948  
949 <        f = new CompletableFuture<>();
950 <        g = new CompletableFuture<>();
951 <        f.completeExceptionally(new CFException());
952 <        g.complete(3);
953 <        h = f.thenCombine(g, subtract);
954 <        checkCompletedWithWrappedCFException(h);
949 >    public void testThenCombine_exceptionalCompletion3() {
950 >        for (ExecutionMode m : ExecutionMode.values())
951 >        for (Integer v1 : new Integer[] { 1, null }) {
952 >
953 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
954 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
955 >        final SubtractFunction r = new SubtractFunction();
956 >        final CFException ex = new CFException();
957 >
958 >        g.completeExceptionally(ex);
959 >        f.complete(v1);
960 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
961 >
962 >        checkCompletedWithWrappedCFException(h, ex);
963 >        checkCompletedWithWrappedCFException(g, ex);
964 >        assertFalse(r.ran());
965 >        checkCompletedNormally(f, v1);
966 >        }
967 >    }
968 >
969 >    public void testThenCombine_exceptionalCompletion4() {
970 >        for (ExecutionMode m : ExecutionMode.values())
971 >        for (Integer v1 : new Integer[] { 1, null }) {
972 >
973 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
974 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
975 >        final SubtractFunction r = new SubtractFunction();
976 >        final CFException ex = new CFException();
977 >
978 >        f.completeExceptionally(ex);
979 >        g.complete(v1);
980 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
981 >
982 >        checkCompletedWithWrappedCFException(h, ex);
983 >        checkCompletedWithWrappedCFException(f, ex);
984 >        assertFalse(r.ran());
985 >        checkCompletedNormally(g, v1);
986 >        }
987      }
988  
989      /**
990       * thenCombine result completes exceptionally if action does
991       */
992 <    public void testThenCombine3() {
993 <        CompletableFuture<Integer> f = new CompletableFuture<>();
994 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
995 <        FailingBiFunction r = new FailingBiFunction();
996 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
997 <        f.complete(one);
998 <        checkIncomplete(g);
999 <        assertFalse(r.ran);
1000 <        f2.complete(two);
1001 <        checkCompletedWithWrappedCFException(g);
1002 <        assertTrue(r.ran);
992 >    public void testThenCombine_actionFailed1() {
993 >        for (ExecutionMode m : ExecutionMode.values())
994 >        for (Integer v1 : new Integer[] { 1, null })
995 >        for (Integer v2 : new Integer[] { 2, null }) {
996 >
997 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
998 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
999 >        final FailingBiFunction r = new FailingBiFunction();
1000 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1001 >
1002 >        f.complete(v1);
1003 >        checkIncomplete(h);
1004 >        g.complete(v2);
1005 >
1006 >        checkCompletedWithWrappedCFException(h);
1007 >        checkCompletedNormally(f, v1);
1008 >        checkCompletedNormally(g, v2);
1009 >        }
1010 >    }
1011 >
1012 >    public void testThenCombine_actionFailed2() {
1013 >        for (ExecutionMode m : ExecutionMode.values())
1014 >        for (Integer v1 : new Integer[] { 1, null })
1015 >        for (Integer v2 : new Integer[] { 2, null }) {
1016 >
1017 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1018 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1019 >        final FailingBiFunction r = new FailingBiFunction();
1020 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1021 >
1022 >        g.complete(v2);
1023 >        checkIncomplete(h);
1024 >        f.complete(v1);
1025 >
1026 >        checkCompletedWithWrappedCFException(h);
1027 >        checkCompletedNormally(f, v1);
1028 >        checkCompletedNormally(g, v2);
1029 >        }
1030      }
1031  
1032      /**
1033       * thenCombine result completes exceptionally if either source cancelled
1034       */
1035 <    public void testThenCombine4() {
1036 <        CompletableFuture<Integer> f, g, h;
1035 >    public void testThenCombine_sourceCancelled1() {
1036 >        for (ExecutionMode m : ExecutionMode.values())
1037 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1038 >        for (Integer v1 : new Integer[] { 1, null }) {
1039  
1040 <        f = new CompletableFuture<>();
1041 <        g = new CompletableFuture<>();
1042 <        h = f.thenCombine(g, subtract);
1043 <        assertTrue(f.cancel(true));
1040 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1041 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1042 >        final SubtractFunction r = new SubtractFunction();
1043 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1044 >
1045 >        assertTrue(f.cancel(mayInterruptIfRunning));
1046          checkIncomplete(h);
1047 <        g.complete(1);
1047 >        g.complete(v1);
1048 >
1049          checkCompletedWithWrappedCancellationException(h);
1050 +        checkCancelled(f);
1051 +        assertFalse(r.ran());
1052 +        checkCompletedNormally(g, v1);
1053 +        }
1054 +    }
1055  
1056 <        f = new CompletableFuture<>();
1057 <        g = new CompletableFuture<>();
1058 <        h = f.thenCombine(g, subtract);
1059 <        assertTrue(g.cancel(true));
1056 >    public void testThenCombine_sourceCancelled2() {
1057 >        for (ExecutionMode m : ExecutionMode.values())
1058 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1059 >        for (Integer v1 : new Integer[] { 1, null }) {
1060 >
1061 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1062 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1063 >        final SubtractFunction r = new SubtractFunction();
1064 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1065 >
1066 >        assertTrue(g.cancel(mayInterruptIfRunning));
1067          checkIncomplete(h);
1068 <        f.complete(3);
1068 >        f.complete(v1);
1069 >
1070          checkCompletedWithWrappedCancellationException(h);
1071 +        checkCancelled(g);
1072 +        assertFalse(r.ran());
1073 +        checkCompletedNormally(f, v1);
1074 +        }
1075 +    }
1076 +
1077 +    public void testThenCombine_sourceCancelled3() {
1078 +        for (ExecutionMode m : ExecutionMode.values())
1079 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1080 +        for (Integer v1 : new Integer[] { 1, null }) {
1081 +
1082 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1083 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1084 +        final SubtractFunction r = new SubtractFunction();
1085 +
1086 +        assertTrue(g.cancel(mayInterruptIfRunning));
1087 +        f.complete(v1);
1088 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1089 +
1090 +        checkCompletedWithWrappedCancellationException(h);
1091 +        checkCancelled(g);
1092 +        assertFalse(r.ran());
1093 +        checkCompletedNormally(f, v1);
1094 +        }
1095 +    }
1096 +
1097 +    public void testThenCombine_sourceCancelled4() {
1098 +        for (ExecutionMode m : ExecutionMode.values())
1099 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1100 +        for (Integer v1 : new Integer[] { 1, null }) {
1101 +
1102 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1103 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1104 +        final SubtractFunction r = new SubtractFunction();
1105 +
1106 +        assertTrue(f.cancel(mayInterruptIfRunning));
1107 +        g.complete(v1);
1108 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1109  
802        f = new CompletableFuture<>();
803        g = new CompletableFuture<>();
804        assertTrue(f.cancel(true));
805        assertTrue(g.cancel(true));
806        h = f.thenCombine(g, subtract);
1110          checkCompletedWithWrappedCancellationException(h);
1111 +        checkCancelled(f);
1112 +        assertFalse(r.ran());
1113 +        checkCompletedNormally(g, v1);
1114 +        }
1115      }
1116  
1117      /**
1118       * thenAcceptBoth result completes normally after normal
1119       * completion of sources
1120       */
1121 <    public void testThenAcceptBoth() {
1122 <        CompletableFuture<Integer> f, g;
1123 <        CompletableFuture<Void> h;
1124 <        SubtractAction r;
1121 >    public void testThenAcceptBoth_normalCompletion1() {
1122 >        for (ExecutionMode m : ExecutionMode.values())
1123 >        for (Integer v1 : new Integer[] { 1, null })
1124 >        for (Integer v2 : new Integer[] { 2, null }) {
1125  
1126 <        f = new CompletableFuture<>();
1127 <        g = new CompletableFuture<>();
1128 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1129 <        f.complete(3);
1126 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1127 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1128 >        final SubtractAction r = new SubtractAction();
1129 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1130 >
1131 >        f.complete(v1);
1132          checkIncomplete(h);
1133 <        g.complete(1);
1133 >        assertFalse(r.ran());
1134 >        g.complete(v2);
1135 >
1136          checkCompletedNormally(h, null);
1137 <        assertEquals(r.value, 2);
1137 >        assertEquals(r.value, subtract(v1, v2));
1138 >        checkCompletedNormally(f, v1);
1139 >        checkCompletedNormally(g, v2);
1140 >        }
1141 >    }
1142  
1143 <        f = new CompletableFuture<>();
1144 <        g = new CompletableFuture<>();
1145 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1146 <        g.complete(1);
1143 >    public void testThenAcceptBoth_normalCompletion2() {
1144 >        for (ExecutionMode m : ExecutionMode.values())
1145 >        for (Integer v1 : new Integer[] { 1, null })
1146 >        for (Integer v2 : new Integer[] { 2, null }) {
1147 >
1148 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1149 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1150 >        final SubtractAction r = new SubtractAction();
1151 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1152 >
1153 >        g.complete(v2);
1154          checkIncomplete(h);
1155 <        f.complete(3);
1155 >        assertFalse(r.ran());
1156 >        f.complete(v1);
1157 >
1158          checkCompletedNormally(h, null);
1159 <        assertEquals(r.value, 2);
1159 >        assertEquals(r.value, subtract(v1, v2));
1160 >        checkCompletedNormally(f, v1);
1161 >        checkCompletedNormally(g, v2);
1162 >        }
1163 >    }
1164 >
1165 >    public void testThenAcceptBoth_normalCompletion3() {
1166 >        for (ExecutionMode m : ExecutionMode.values())
1167 >        for (Integer v1 : new Integer[] { 1, null })
1168 >        for (Integer v2 : new Integer[] { 2, null }) {
1169 >
1170 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1171 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1172 >        final SubtractAction r = new SubtractAction();
1173 >
1174 >        g.complete(v2);
1175 >        f.complete(v1);
1176 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1177  
837        f = new CompletableFuture<>();
838        g = new CompletableFuture<>();
839        g.complete(1);
840        f.complete(3);
841        h = f.thenAcceptBoth(g, r = new SubtractAction());
1178          checkCompletedNormally(h, null);
1179 <        assertEquals(r.value, 2);
1179 >        assertEquals(r.value, subtract(v1, v2));
1180 >        checkCompletedNormally(f, v1);
1181 >        checkCompletedNormally(g, v2);
1182 >        }
1183 >    }
1184 >
1185 >    public void testThenAcceptBoth_normalCompletion4() {
1186 >        for (ExecutionMode m : ExecutionMode.values())
1187 >        for (Integer v1 : new Integer[] { 1, null })
1188 >        for (Integer v2 : new Integer[] { 2, null }) {
1189 >
1190 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1191 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1192 >        final SubtractAction r = new SubtractAction();
1193 >
1194 >        f.complete(v1);
1195 >        g.complete(v2);
1196 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1197 >
1198 >        checkCompletedNormally(h, null);
1199 >        assertEquals(r.value, subtract(v1, v2));
1200 >        checkCompletedNormally(f, v1);
1201 >        checkCompletedNormally(g, v2);
1202 >        }
1203      }
1204  
1205      /**
1206       * thenAcceptBoth result completes exceptionally after exceptional
1207       * completion of either source
1208       */
1209 <    public void testThenAcceptBoth2() {
1210 <        CompletableFuture<Integer> f, g;
1211 <        CompletableFuture<Void> h;
853 <        SubtractAction r;
1209 >    public void testThenAcceptBoth_exceptionalCompletion1() {
1210 >        for (ExecutionMode m : ExecutionMode.values())
1211 >        for (Integer v1 : new Integer[] { 1, null }) {
1212  
1213 <        f = new CompletableFuture<>();
1214 <        g = new CompletableFuture<>();
1215 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1216 <        f.completeExceptionally(new CFException());
1213 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1214 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1215 >        final SubtractAction r = new SubtractAction();
1216 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1217 >        final CFException ex = new CFException();
1218 >
1219 >        f.completeExceptionally(ex);
1220          checkIncomplete(h);
1221 <        g.complete(1);
861 <        checkCompletedWithWrappedCFException(h);
1221 >        g.complete(v1);
1222  
1223 <        f = new CompletableFuture<>();
1224 <        g = new CompletableFuture<>();
1225 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1226 <        g.completeExceptionally(new CFException());
1223 >        checkCompletedWithWrappedCFException(h, ex);
1224 >        checkCompletedWithWrappedCFException(f, ex);
1225 >        assertFalse(r.ran());
1226 >        checkCompletedNormally(g, v1);
1227 >        }
1228 >    }
1229 >
1230 >    public void testThenAcceptBoth_exceptionalCompletion2() {
1231 >        for (ExecutionMode m : ExecutionMode.values())
1232 >        for (Integer v1 : new Integer[] { 1, null }) {
1233 >
1234 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1235 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1236 >        final SubtractAction r = new SubtractAction();
1237 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1238 >        final CFException ex = new CFException();
1239 >
1240 >        g.completeExceptionally(ex);
1241          checkIncomplete(h);
1242 <        f.complete(3);
869 <        checkCompletedWithWrappedCFException(h);
1242 >        f.complete(v1);
1243  
1244 <        f = new CompletableFuture<>();
1245 <        g = new CompletableFuture<>();
1246 <        f.complete(3);
1247 <        g.completeExceptionally(new CFException());
1248 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1249 <        checkCompletedWithWrappedCFException(h);
1244 >        checkCompletedWithWrappedCFException(h, ex);
1245 >        checkCompletedWithWrappedCFException(g, ex);
1246 >        assertFalse(r.ran());
1247 >        checkCompletedNormally(f, v1);
1248 >        }
1249 >    }
1250  
1251 <        f = new CompletableFuture<>();
1252 <        g = new CompletableFuture<>();
1253 <        f.completeExceptionally(new CFException());
1254 <        g.complete(3);
1255 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1256 <        checkCompletedWithWrappedCFException(h);
1251 >    public void testThenAcceptBoth_exceptionalCompletion3() {
1252 >        for (ExecutionMode m : ExecutionMode.values())
1253 >        for (Integer v1 : new Integer[] { 1, null }) {
1254 >
1255 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1256 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1257 >        final SubtractAction r = new SubtractAction();
1258 >        final CFException ex = new CFException();
1259 >
1260 >        g.completeExceptionally(ex);
1261 >        f.complete(v1);
1262 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1263 >
1264 >        checkCompletedWithWrappedCFException(h, ex);
1265 >        checkCompletedWithWrappedCFException(g, ex);
1266 >        assertFalse(r.ran());
1267 >        checkCompletedNormally(f, v1);
1268 >        }
1269 >    }
1270 >
1271 >    public void testThenAcceptBoth_exceptionalCompletion4() {
1272 >        for (ExecutionMode m : ExecutionMode.values())
1273 >        for (Integer v1 : new Integer[] { 1, null }) {
1274 >
1275 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1276 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1277 >        final SubtractAction r = new SubtractAction();
1278 >        final CFException ex = new CFException();
1279 >
1280 >        f.completeExceptionally(ex);
1281 >        g.complete(v1);
1282 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1283 >
1284 >        checkCompletedWithWrappedCFException(h, ex);
1285 >        checkCompletedWithWrappedCFException(f, ex);
1286 >        assertFalse(r.ran());
1287 >        checkCompletedNormally(g, v1);
1288 >        }
1289      }
1290  
1291      /**
1292       * thenAcceptBoth result completes exceptionally if action does
1293       */
1294 <    public void testThenAcceptBoth3() {
1295 <        CompletableFuture<Integer> f, g;
1296 <        CompletableFuture<Void> h;
1297 <        FailingBiConsumer r;
1294 >    public void testThenAcceptBoth_actionFailed1() {
1295 >        for (ExecutionMode m : ExecutionMode.values())
1296 >        for (Integer v1 : new Integer[] { 1, null })
1297 >        for (Integer v2 : new Integer[] { 2, null }) {
1298  
1299 <        f = new CompletableFuture<>();
1300 <        g = new CompletableFuture<>();
1301 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1302 <        f.complete(3);
1299 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1300 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1301 >        final FailingBiConsumer r = new FailingBiConsumer();
1302 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1303 >
1304 >        f.complete(v1);
1305          checkIncomplete(h);
1306 <        g.complete(1);
1306 >        g.complete(v2);
1307 >
1308          checkCompletedWithWrappedCFException(h);
1309 +        checkCompletedNormally(f, v1);
1310 +        checkCompletedNormally(g, v2);
1311 +        }
1312 +    }
1313 +
1314 +    public void testThenAcceptBoth_actionFailed2() {
1315 +        for (ExecutionMode m : ExecutionMode.values())
1316 +        for (Integer v1 : new Integer[] { 1, null })
1317 +        for (Integer v2 : new Integer[] { 2, null }) {
1318 +
1319 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1320 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1321 +        final FailingBiConsumer r = new FailingBiConsumer();
1322 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1323 +
1324 +        g.complete(v2);
1325 +        checkIncomplete(h);
1326 +        f.complete(v1);
1327  
902        f = new CompletableFuture<>();
903        g = new CompletableFuture<>();
904        f.complete(3);
905        g.complete(1);
906        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1328          checkCompletedWithWrappedCFException(h);
1329 +        checkCompletedNormally(f, v1);
1330 +        checkCompletedNormally(g, v2);
1331 +        }
1332      }
1333  
1334      /**
1335       * thenAcceptBoth result completes exceptionally if either source cancelled
1336       */
1337 <    public void testThenAcceptBoth4() {
1338 <        CompletableFuture<Integer> f, g;
1339 <        CompletableFuture<Void> h;
1340 <        SubtractAction r;
1337 >    public void testThenAcceptBoth_sourceCancelled1() {
1338 >        for (ExecutionMode m : ExecutionMode.values())
1339 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1340 >        for (Integer v1 : new Integer[] { 1, null }) {
1341  
1342 <        f = new CompletableFuture<>();
1343 <        g = new CompletableFuture<>();
1344 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1345 <        assertTrue(f.cancel(true));
1342 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1343 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1344 >        final SubtractAction r = new SubtractAction();
1345 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1346 >
1347 >        assertTrue(f.cancel(mayInterruptIfRunning));
1348          checkIncomplete(h);
1349 <        g.complete(1);
1349 >        g.complete(v1);
1350 >
1351          checkCompletedWithWrappedCancellationException(h);
1352 +        checkCancelled(f);
1353 +        assertFalse(r.ran());
1354 +        checkCompletedNormally(g, v1);
1355 +        }
1356 +    }
1357  
1358 <        f = new CompletableFuture<>();
1359 <        g = new CompletableFuture<>();
1360 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1361 <        assertTrue(g.cancel(true));
1358 >    public void testThenAcceptBoth_sourceCancelled2() {
1359 >        for (ExecutionMode m : ExecutionMode.values())
1360 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1361 >        for (Integer v1 : new Integer[] { 1, null }) {
1362 >
1363 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1364 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1365 >        final SubtractAction r = new SubtractAction();
1366 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1367 >
1368 >        assertTrue(g.cancel(mayInterruptIfRunning));
1369          checkIncomplete(h);
1370 <        f.complete(3);
932 <        checkCompletedWithWrappedCancellationException(h);
1370 >        f.complete(v1);
1371  
934        f = new CompletableFuture<>();
935        g = new CompletableFuture<>();
936        f.complete(3);
937        assertTrue(g.cancel(true));
938        h = f.thenAcceptBoth(g, r = new SubtractAction());
1372          checkCompletedWithWrappedCancellationException(h);
1373 +        checkCancelled(g);
1374 +        assertFalse(r.ran());
1375 +        checkCompletedNormally(f, v1);
1376 +        }
1377 +    }
1378 +
1379 +    public void testThenAcceptBoth_sourceCancelled3() {
1380 +        for (ExecutionMode m : ExecutionMode.values())
1381 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1382 +        for (Integer v1 : new Integer[] { 1, null }) {
1383 +
1384 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1385 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1386 +        final SubtractAction r = new SubtractAction();
1387 +
1388 +        assertTrue(g.cancel(mayInterruptIfRunning));
1389 +        f.complete(v1);
1390 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1391  
941        f = new CompletableFuture<>();
942        g = new CompletableFuture<>();
943        assertTrue(f.cancel(true));
944        g.complete(3);
945        h = f.thenAcceptBoth(g, r = new SubtractAction());
1392          checkCompletedWithWrappedCancellationException(h);
1393 +        checkCancelled(g);
1394 +        assertFalse(r.ran());
1395 +        checkCompletedNormally(f, v1);
1396 +        }
1397      }
1398  
1399 <    /**
1400 <     * Permits the testing of parallel code for the 3 different
1401 <     * execution modes without repeating all the testing code.
1402 <     */
953 <    enum ExecutionMode {
954 <        DEFAULT {
955 <            public <T,U> CompletableFuture<Void> runAfterBoth
956 <                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) {
957 <                return f.runAfterBoth(g, r);
958 <            }
959 <        },
1399 >    public void testThenAcceptBoth_sourceCancelled4() {
1400 >        for (ExecutionMode m : ExecutionMode.values())
1401 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1402 >        for (Integer v1 : new Integer[] { 1, null }) {
1403  
1404 <        DEFAULT_ASYNC {
1405 <            public <T,U> CompletableFuture<Void> runAfterBoth
1406 <                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) {
964 <                return f.runAfterBothAsync(g, r);
965 <            }
966 <        },
1404 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1405 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1406 >        final SubtractAction r = new SubtractAction();
1407  
1408 <        EXECUTOR {
1409 <            public <T,U> CompletableFuture<Void> runAfterBoth
1410 <                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) {
971 <                return f.runAfterBothAsync(g, r, new ThreadExecutor());
972 <            }
973 <        };
1408 >        assertTrue(f.cancel(mayInterruptIfRunning));
1409 >        g.complete(v1);
1410 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1411  
1412 <        public abstract <T,U> CompletableFuture<Void> runAfterBoth
1413 <            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r);
1412 >        checkCompletedWithWrappedCancellationException(h);
1413 >        checkCancelled(f);
1414 >        assertFalse(r.ran());
1415 >        checkCompletedNormally(g, v1);
1416 >        }
1417      }
1418  
1419      /**
# Line 1713 | Line 2153 | public class CompletableFutureTest exten
2153      }
2154  
2155      /**
1716     * thenCombineAsync result completes normally after normal
1717     * completion of sources
1718     */
1719    public void testThenCombineAsync() {
1720        CompletableFuture<Integer> f, g, h;
1721
1722        f = new CompletableFuture<>();
1723        g = new CompletableFuture<>();
1724        h = f.thenCombineAsync(g, subtract);
1725        f.complete(3);
1726        checkIncomplete(h);
1727        g.complete(1);
1728        checkCompletedNormally(h, 2);
1729
1730        f = new CompletableFuture<>();
1731        g = new CompletableFuture<>();
1732        h = f.thenCombineAsync(g, subtract);
1733        g.complete(1);
1734        checkIncomplete(h);
1735        f.complete(3);
1736        checkCompletedNormally(h, 2);
1737
1738        f = new CompletableFuture<>();
1739        g = new CompletableFuture<>();
1740        g.complete(1);
1741        f.complete(3);
1742        h = f.thenCombineAsync(g, subtract);
1743        checkCompletedNormally(h, 2);
1744    }
1745
1746    /**
1747     * thenCombineAsync result completes exceptionally after exceptional
1748     * completion of either source
1749     */
1750    public void testThenCombineAsync2() {
1751        CompletableFuture<Integer> f, g, h;
1752
1753        f = new CompletableFuture<>();
1754        g = new CompletableFuture<>();
1755        h = f.thenCombineAsync(g, subtract);
1756        f.completeExceptionally(new CFException());
1757        checkIncomplete(h);
1758        g.complete(1);
1759        checkCompletedWithWrappedCFException(h);
1760
1761        f = new CompletableFuture<>();
1762        g = new CompletableFuture<>();
1763        h = f.thenCombineAsync(g, subtract);
1764        g.completeExceptionally(new CFException());
1765        checkIncomplete(h);
1766        f.complete(3);
1767        checkCompletedWithWrappedCFException(h);
1768
1769        f = new CompletableFuture<>();
1770        g = new CompletableFuture<>();
1771        g.completeExceptionally(new CFException());
1772        f.complete(3);
1773        h = f.thenCombineAsync(g, subtract);
1774        checkCompletedWithWrappedCFException(h);
1775    }
1776
1777    /**
1778     * thenCombineAsync result completes exceptionally if action does
1779     */
1780    public void testThenCombineAsync3() {
1781        CompletableFuture<Integer> f = new CompletableFuture<>();
1782        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1783        FailingBiFunction r = new FailingBiFunction();
1784        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1785        f.complete(one);
1786        checkIncomplete(g);
1787        assertFalse(r.ran);
1788        f2.complete(two);
1789        checkCompletedWithWrappedCFException(g);
1790        assertTrue(r.ran);
1791    }
1792
1793    /**
1794     * thenCombineAsync result completes exceptionally if either source cancelled
1795     */
1796    public void testThenCombineAsync4() {
1797        CompletableFuture<Integer> f, g, h;
1798
1799        f = new CompletableFuture<>();
1800        g = new CompletableFuture<>();
1801        h = f.thenCombineAsync(g, subtract);
1802        assertTrue(f.cancel(true));
1803        checkIncomplete(h);
1804        g.complete(1);
1805        checkCompletedWithWrappedCancellationException(h);
1806
1807        f = new CompletableFuture<>();
1808        g = new CompletableFuture<>();
1809        h = f.thenCombineAsync(g, subtract);
1810        assertTrue(g.cancel(true));
1811        checkIncomplete(h);
1812        f.complete(3);
1813        checkCompletedWithWrappedCancellationException(h);
1814
1815        f = new CompletableFuture<>();
1816        g = new CompletableFuture<>();
1817        g.complete(3);
1818        assertTrue(f.cancel(true));
1819        h = f.thenCombineAsync(g, subtract);
1820        checkCompletedWithWrappedCancellationException(h);
1821
1822        f = new CompletableFuture<>();
1823        g = new CompletableFuture<>();
1824        f.complete(3);
1825        assertTrue(g.cancel(true));
1826        h = f.thenCombineAsync(g, subtract);
1827        checkCompletedWithWrappedCancellationException(h);
1828    }
1829
1830    /**
1831     * thenAcceptBothAsync result completes normally after normal
1832     * completion of sources
1833     */
1834    public void testThenAcceptBothAsync() {
1835        CompletableFuture<Integer> f, g;
1836        CompletableFuture<Void> h;
1837        SubtractAction r;
1838
1839        f = new CompletableFuture<>();
1840        g = new CompletableFuture<>();
1841        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1842        f.complete(3);
1843        checkIncomplete(h);
1844        g.complete(1);
1845        checkCompletedNormally(h, null);
1846        assertEquals(r.value, 2);
1847
1848        f = new CompletableFuture<>();
1849        g = new CompletableFuture<>();
1850        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1851        g.complete(1);
1852        checkIncomplete(h);
1853        f.complete(3);
1854        checkCompletedNormally(h, null);
1855        assertEquals(r.value, 2);
1856
1857        f = new CompletableFuture<>();
1858        g = new CompletableFuture<>();
1859        g.complete(1);
1860        f.complete(3);
1861        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1862        checkCompletedNormally(h, null);
1863        assertEquals(r.value, 2);
1864    }
1865
1866    /**
1867     * thenAcceptBothAsync result completes exceptionally after exceptional
1868     * completion of source
1869     */
1870    public void testThenAcceptBothAsync2() {
1871        CompletableFuture<Integer> f, g;
1872        CompletableFuture<Void> h;
1873        SubtractAction r;
1874
1875        f = new CompletableFuture<>();
1876        g = new CompletableFuture<>();
1877        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1878        f.completeExceptionally(new CFException());
1879        checkIncomplete(h);
1880        g.complete(1);
1881        checkCompletedWithWrappedCFException(h);
1882
1883        f = new CompletableFuture<>();
1884        g = new CompletableFuture<>();
1885        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1886        g.completeExceptionally(new CFException());
1887        checkIncomplete(h);
1888        f.complete(3);
1889        checkCompletedWithWrappedCFException(h);
1890
1891        f = new CompletableFuture<>();
1892        g = new CompletableFuture<>();
1893        f.complete(3);
1894        g.completeExceptionally(new CFException());
1895        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1896        checkCompletedWithWrappedCFException(h);
1897
1898        f = new CompletableFuture<>();
1899        g = new CompletableFuture<>();
1900        f.completeExceptionally(new CFException());
1901        g.complete(3);
1902        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1903        checkCompletedWithWrappedCFException(h);
1904    }
1905
1906    /**
1907     * thenAcceptBothAsync result completes exceptionally if action does
1908     */
1909    public void testThenAcceptBothAsync3() {
1910        CompletableFuture<Integer> f, g;
1911        CompletableFuture<Void> h;
1912        FailingBiConsumer r;
1913
1914        f = new CompletableFuture<>();
1915        g = new CompletableFuture<>();
1916        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1917        f.complete(3);
1918        checkIncomplete(h);
1919        g.complete(1);
1920        checkCompletedWithWrappedCFException(h);
1921
1922        f = new CompletableFuture<>();
1923        g = new CompletableFuture<>();
1924        f.complete(3);
1925        g.complete(1);
1926        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1927        checkCompletedWithWrappedCFException(h);
1928    }
1929
1930    /**
1931     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1932     */
1933    public void testThenAcceptBothAsync4() {
1934        CompletableFuture<Integer> f, g;
1935        CompletableFuture<Void> h;
1936        SubtractAction r;
1937
1938        f = new CompletableFuture<>();
1939        g = new CompletableFuture<>();
1940        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1941        assertTrue(f.cancel(true));
1942        checkIncomplete(h);
1943        g.complete(1);
1944        checkCompletedWithWrappedCancellationException(h);
1945
1946        f = new CompletableFuture<>();
1947        g = new CompletableFuture<>();
1948        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1949        assertTrue(g.cancel(true));
1950        checkIncomplete(h);
1951        f.complete(3);
1952        checkCompletedWithWrappedCancellationException(h);
1953
1954        f = new CompletableFuture<>();
1955        g = new CompletableFuture<>();
1956        f.complete(3);
1957        assertTrue(g.cancel(true));
1958        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1959        checkCompletedWithWrappedCancellationException(h);
1960
1961        f = new CompletableFuture<>();
1962        g = new CompletableFuture<>();
1963        assertTrue(f.cancel(true));
1964        g.complete(3);
1965        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1966        checkCompletedWithWrappedCancellationException(h);
1967    }
1968
1969    /**
2156       * applyToEitherAsync result completes normally after normal
2157       * completion of sources
2158       */
# Line 2406 | Line 2592 | public class CompletableFutureTest exten
2592      }
2593  
2594      /**
2409     * thenCombineAsync result completes normally after normal
2410     * completion of sources
2411     */
2412    public void testThenCombineAsyncE() {
2413        CompletableFuture<Integer> f, g, h;
2414        ThreadExecutor e = new ThreadExecutor();
2415        int count = 0;
2416
2417        f = new CompletableFuture<>();
2418        g = new CompletableFuture<>();
2419        h = f.thenCombineAsync(g, subtract, e);
2420        f.complete(3);
2421        checkIncomplete(h);
2422        g.complete(1);
2423        checkCompletedNormally(h, 2);
2424        assertEquals(++count, e.count.get());
2425
2426        f = new CompletableFuture<>();
2427        g = new CompletableFuture<>();
2428        h = f.thenCombineAsync(g, subtract, e);
2429        g.complete(1);
2430        checkIncomplete(h);
2431        f.complete(3);
2432        checkCompletedNormally(h, 2);
2433        assertEquals(++count, e.count.get());
2434
2435        f = new CompletableFuture<>();
2436        g = new CompletableFuture<>();
2437        g.complete(1);
2438        f.complete(3);
2439        h = f.thenCombineAsync(g, subtract, e);
2440        checkCompletedNormally(h, 2);
2441        assertEquals(++count, e.count.get());
2442    }
2443
2444    /**
2445     * thenCombineAsync result completes exceptionally after exceptional
2446     * completion of either source
2447     */
2448    public void testThenCombineAsync2E() {
2449        CompletableFuture<Integer> f, g, h;
2450        ThreadExecutor e = new ThreadExecutor();
2451        int count = 0;
2452
2453        f = new CompletableFuture<>();
2454        g = new CompletableFuture<>();
2455        h = f.thenCombineAsync(g, subtract, e);
2456        f.completeExceptionally(new CFException());
2457        checkIncomplete(h);
2458        g.complete(1);
2459        checkCompletedWithWrappedCFException(h);
2460
2461        f = new CompletableFuture<>();
2462        g = new CompletableFuture<>();
2463        h = f.thenCombineAsync(g, subtract, e);
2464        g.completeExceptionally(new CFException());
2465        checkIncomplete(h);
2466        f.complete(3);
2467        checkCompletedWithWrappedCFException(h);
2468
2469        f = new CompletableFuture<>();
2470        g = new CompletableFuture<>();
2471        g.completeExceptionally(new CFException());
2472        h = f.thenCombineAsync(g, subtract, e);
2473        checkIncomplete(h);
2474        f.complete(3);
2475        checkCompletedWithWrappedCFException(h);
2476
2477        assertEquals(0, e.count.get());
2478    }
2479
2480    /**
2481     * thenCombineAsync result completes exceptionally if action does
2482     */
2483    public void testThenCombineAsync3E() {
2484        CompletableFuture<Integer> f = new CompletableFuture<>();
2485        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2486        FailingBiFunction r = new FailingBiFunction();
2487        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2488        f.complete(one);
2489        checkIncomplete(g);
2490        assertFalse(r.ran);
2491        f2.complete(two);
2492        checkCompletedWithWrappedCFException(g);
2493        assertTrue(r.ran);
2494    }
2495
2496    /**
2497     * thenCombineAsync result completes exceptionally if either source cancelled
2498     */
2499    public void testThenCombineAsync4E() {
2500        CompletableFuture<Integer> f, g, h;
2501        ThreadExecutor e = new ThreadExecutor();
2502
2503        f = new CompletableFuture<>();
2504        g = new CompletableFuture<>();
2505        h = f.thenCombineAsync(g, subtract, e);
2506        assertTrue(f.cancel(true));
2507        checkIncomplete(h);
2508        g.complete(1);
2509        checkCompletedWithWrappedCancellationException(h);
2510
2511        f = new CompletableFuture<>();
2512        g = new CompletableFuture<>();
2513        h = f.thenCombineAsync(g, subtract, e);
2514        assertTrue(g.cancel(true));
2515        checkIncomplete(h);
2516        f.complete(3);
2517        checkCompletedWithWrappedCancellationException(h);
2518
2519        f = new CompletableFuture<>();
2520        g = new CompletableFuture<>();
2521        assertTrue(g.cancel(true));
2522        h = f.thenCombineAsync(g, subtract, e);
2523        checkIncomplete(h);
2524        f.complete(3);
2525        checkCompletedWithWrappedCancellationException(h);
2526
2527        f = new CompletableFuture<>();
2528        g = new CompletableFuture<>();
2529        assertTrue(f.cancel(true));
2530        assertTrue(g.cancel(true));
2531        h = f.thenCombineAsync(g, subtract, e);
2532        checkCompletedWithWrappedCancellationException(h);
2533
2534        assertEquals(0, e.count.get());
2535    }
2536
2537    /**
2538     * thenAcceptBothAsync result completes normally after normal
2539     * completion of sources
2540     */
2541    public void testThenAcceptBothAsyncE() {
2542        CompletableFuture<Integer> f, g;
2543        CompletableFuture<Void> h;
2544        SubtractAction r;
2545        ThreadExecutor e = new ThreadExecutor();
2546
2547        f = new CompletableFuture<>();
2548        g = new CompletableFuture<>();
2549        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2550        f.complete(3);
2551        checkIncomplete(h);
2552        g.complete(1);
2553        checkCompletedNormally(h, null);
2554        assertEquals(r.value, 2);
2555
2556        f = new CompletableFuture<>();
2557        g = new CompletableFuture<>();
2558        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2559        g.complete(1);
2560        checkIncomplete(h);
2561        f.complete(3);
2562        checkCompletedNormally(h, null);
2563        assertEquals(r.value, 2);
2564
2565        f = new CompletableFuture<>();
2566        g = new CompletableFuture<>();
2567        g.complete(1);
2568        f.complete(3);
2569        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2570        checkCompletedNormally(h, null);
2571        assertEquals(r.value, 2);
2572
2573        assertEquals(3, e.count.get());
2574    }
2575
2576    /**
2577     * thenAcceptBothAsync result completes exceptionally after exceptional
2578     * completion of source
2579     */
2580    public void testThenAcceptBothAsync2E() {
2581        CompletableFuture<Integer> f, g;
2582        CompletableFuture<Void> h;
2583        SubtractAction r;
2584        ThreadExecutor e = new ThreadExecutor();
2585
2586        f = new CompletableFuture<>();
2587        g = new CompletableFuture<>();
2588        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2589        f.completeExceptionally(new CFException());
2590        checkIncomplete(h);
2591        g.complete(1);
2592        checkCompletedWithWrappedCFException(h);
2593
2594        f = new CompletableFuture<>();
2595        g = new CompletableFuture<>();
2596        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2597        g.completeExceptionally(new CFException());
2598        checkIncomplete(h);
2599        f.complete(3);
2600        checkCompletedWithWrappedCFException(h);
2601
2602        f = new CompletableFuture<>();
2603        g = new CompletableFuture<>();
2604        f.complete(3);
2605        g.completeExceptionally(new CFException());
2606        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2607        checkCompletedWithWrappedCFException(h);
2608
2609        f = new CompletableFuture<>();
2610        g = new CompletableFuture<>();
2611        f.completeExceptionally(new CFException());
2612        g.complete(3);
2613        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2614        checkCompletedWithWrappedCFException(h);
2615
2616        assertEquals(0, e.count.get());
2617    }
2618
2619    /**
2620     * thenAcceptBothAsync result completes exceptionally if action does
2621     */
2622    public void testThenAcceptBothAsync3E() {
2623        CompletableFuture<Integer> f, g;
2624        CompletableFuture<Void> h;
2625        FailingBiConsumer r;
2626        ThreadExecutor e = new ThreadExecutor();
2627
2628        f = new CompletableFuture<>();
2629        g = new CompletableFuture<>();
2630        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2631        f.complete(3);
2632        checkIncomplete(h);
2633        g.complete(1);
2634        checkCompletedWithWrappedCFException(h);
2635
2636        f = new CompletableFuture<>();
2637        g = new CompletableFuture<>();
2638        f.complete(3);
2639        g.complete(1);
2640        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2641        checkCompletedWithWrappedCFException(h);
2642
2643        assertEquals(2, e.count.get());
2644    }
2645
2646    /**
2647     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2648     */
2649    public void testThenAcceptBothAsync4E() {
2650        CompletableFuture<Integer> f, g;
2651        CompletableFuture<Void> h;
2652        SubtractAction r;
2653        ThreadExecutor e = new ThreadExecutor();
2654
2655        f = new CompletableFuture<>();
2656        g = new CompletableFuture<>();
2657        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2658        assertTrue(f.cancel(true));
2659        checkIncomplete(h);
2660        g.complete(1);
2661        checkCompletedWithWrappedCancellationException(h);
2662
2663        f = new CompletableFuture<>();
2664        g = new CompletableFuture<>();
2665        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2666        assertTrue(g.cancel(true));
2667        checkIncomplete(h);
2668        f.complete(3);
2669        checkCompletedWithWrappedCancellationException(h);
2670
2671        f = new CompletableFuture<>();
2672        g = new CompletableFuture<>();
2673        f.complete(3);
2674        assertTrue(g.cancel(true));
2675        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2676        checkCompletedWithWrappedCancellationException(h);
2677
2678        f = new CompletableFuture<>();
2679        g = new CompletableFuture<>();
2680        assertTrue(f.cancel(true));
2681        g.complete(3);
2682        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2683        checkCompletedWithWrappedCancellationException(h);
2684
2685        assertEquals(0, e.count.get());
2686    }
2687
2688    /**
2595       * applyToEitherAsync result completes normally after normal
2596       * completion of sources
2597       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines