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.36 by jsr166, Sun Jun 1 23:12:45 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 +    public static int subtract(Integer x, Integer y) {
323 +        return ((x == null) ? 42 : x.intValue())
324 +            - ((y == null) ? 99 : y.intValue());
325 +    }
326 +
327      static final Supplier<Integer> supplyOne =
328          () -> Integer.valueOf(1);
329      static final Function<Integer, Integer> inc =
330          (Integer x) -> Integer.valueOf(x.intValue() + 1);
331      static final BiFunction<Integer, Integer, Integer> subtract =
332 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
332 >        (Integer x, Integer y) -> subtract(x, y);
333      static final class IncAction implements Consumer<Integer> {
334          int value;
335          public void accept(Integer x) { value = x.intValue() + 1; }
336      }
337      static final class SubtractAction implements BiConsumer<Integer, Integer> {
338 +        volatile int invocationCount = 0;
339          int value;
340 +        // Check this action was invoked exactly once when result is computed.
341 +        public boolean ran() { return invocationCount == 1; }
342          public void accept(Integer x, Integer y) {
343 <            value = x.intValue() - y.intValue();
343 >            invocationCount++;
344 >            value = subtract(x, y);
345 >        }
346 >    }
347 >    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
348 >        volatile int invocationCount = 0;
349 >        int value;
350 >        // Check this action was invoked exactly once when result is computed.
351 >        public boolean ran() { return invocationCount == 1; }
352 >        public Integer apply(Integer x, Integer y) {
353 >            invocationCount++;
354 >            return subtract(x, y);
355          }
356      }
357      static final class Noop implements Runnable {
# Line 405 | Line 426 | public class CompletableFutureTest exten
426      }
427  
428      /**
429 +     * Permits the testing of parallel code for the 3 different
430 +     * execution modes without repeating all the testing code.
431 +     */
432 +    enum ExecutionMode {
433 +        DEFAULT {
434 +            public <T,U> CompletableFuture<Void> runAfterBoth
435 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
436 +                return f.runAfterBoth(g, a);
437 +            }
438 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
439 +                (CompletableFuture<T> f,
440 +                 CompletionStage<? extends U> g,
441 +                 BiConsumer<? super T,? super U> a) {
442 +                return f.thenAcceptBoth(g, a);
443 +            }
444 +            public <T,U,V> CompletableFuture<V> thenCombine
445 +                (CompletableFuture<T> f,
446 +                 CompletionStage<? extends U> g,
447 +                 BiFunction<? super T,? super U,? extends V> a) {
448 +                return f.thenCombine(g, a);
449 +            }
450 +        },
451 +
452 + //             /** Experimental way to do more testing */
453 + //         REVERSE_DEFAULT {
454 + //             public <T,U> CompletableFuture<Void> runAfterBoth
455 + //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
456 + //                 return g.runAfterBoth(f, a);
457 + //             }
458 + //             public <T,U> CompletableFuture<Void> thenAcceptBoth
459 + //                 (CompletableFuture<T> f,
460 + //                  CompletionStage<? extends U> g,
461 + //                  BiConsumer<? super T,? super U> a) {
462 + //                 return DEFAULT.thenAcceptBoth(f, g, a);
463 + //             }
464 + //         },
465 +
466 +        DEFAULT_ASYNC {
467 +            public <T,U> CompletableFuture<Void> runAfterBoth
468 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
469 +                return f.runAfterBothAsync(g, a);
470 +            }
471 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
472 +                (CompletableFuture<T> f,
473 +                 CompletionStage<? extends U> g,
474 +                 BiConsumer<? super T,? super U> a) {
475 +                return f.thenAcceptBothAsync(g, a);
476 +            }
477 +            public <T,U,V> CompletableFuture<V> thenCombine
478 +                (CompletableFuture<T> f,
479 +                 CompletionStage<? extends U> g,
480 +                 BiFunction<? super T,? super U,? extends V> a) {
481 +                return f.thenCombineAsync(g, a);
482 +            }
483 +        },
484 +
485 + //         REVERSE_DEFAULT_ASYNC {
486 + //             public <T,U> CompletableFuture<Void> runAfterBoth
487 + //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
488 + //                 return f.runAfterBothAsync(g, a);
489 + //             }
490 + //             public <T,U> CompletableFuture<Void> thenAcceptBoth
491 + //                 (CompletableFuture<T> f,
492 + //                  CompletionStage<? extends U> g,
493 + //                  BiConsumer<? super T,? super U> a) {
494 + //                 return DEFAULT_ASYNC.thenAcceptBoth(f, g, a);
495 + //             }
496 + //         },
497 +
498 +        EXECUTOR {
499 +            public <T,U> CompletableFuture<Void> runAfterBoth
500 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
501 +                return f.runAfterBothAsync(g, a, new ThreadExecutor());
502 +            }
503 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
504 +                (CompletableFuture<T> f,
505 +                 CompletionStage<? extends U> g,
506 +                 BiConsumer<? super T,? super U> a) {
507 +                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
508 +            }
509 +            public <T,U,V> CompletableFuture<V> thenCombine
510 +                (CompletableFuture<T> f,
511 +                 CompletionStage<? extends U> g,
512 +                 BiFunction<? super T,? super U,? extends V> a) {
513 +                return f.thenCombineAsync(g, a, new ThreadExecutor());
514 +            }
515 +        };
516 +
517 +        public abstract <T,U> CompletableFuture<Void> runAfterBoth
518 +            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
519 +        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
520 +            (CompletableFuture<T> f,
521 +             CompletionStage<? extends U> g,
522 +             BiConsumer<? super T,? super U> a);
523 +        public abstract <T,U,V> CompletableFuture<V> thenCombine
524 +            (CompletableFuture<T> f,
525 +             CompletionStage<? extends U> g,
526 +             BiFunction<? super T,? super U,? extends V> a);
527 +    }
528 +
529 +    /**
530       * exceptionally action completes with function value on source
531       * exception; otherwise with source value
532       */
# Line 696 | Line 818 | public class CompletableFutureTest exten
818       * thenCombine result completes normally after normal completion
819       * of sources
820       */
821 <    public void testThenCombine() {
822 <        CompletableFuture<Integer> f, g, h;
821 >    public void testThenCombine_normalCompletion1() {
822 >        for (ExecutionMode m : ExecutionMode.values())
823 >        for (Integer v1 : new Integer[] { 1, null })
824 >        for (Integer v2 : new Integer[] { 2, null }) {
825  
826 <        f = new CompletableFuture<>();
827 <        g = new CompletableFuture<>();
828 <        h = f.thenCombine(g, subtract);
829 <        f.complete(3);
826 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
827 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
828 >        final SubtractFunction r = new SubtractFunction();
829 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
830 >
831 >        f.complete(v1);
832          checkIncomplete(h);
833 <        g.complete(1);
834 <        checkCompletedNormally(h, 2);
833 >        assertFalse(r.ran());
834 >        g.complete(v2);
835  
836 <        f = new CompletableFuture<>();
837 <        g = new CompletableFuture<>();
838 <        h = f.thenCombine(g, subtract);
839 <        g.complete(1);
836 >        checkCompletedNormally(h, subtract(v1, v2));
837 >        checkCompletedNormally(f, v1);
838 >        checkCompletedNormally(g, v2);
839 >        }
840 >    }
841 >
842 >    public void testThenCombine_normalCompletion2() {
843 >        for (ExecutionMode m : ExecutionMode.values())
844 >        for (Integer v1 : new Integer[] { 1, null })
845 >        for (Integer v2 : new Integer[] { 2, null }) {
846 >
847 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
848 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
849 >        final SubtractFunction r = new SubtractFunction();
850 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
851 >
852 >        g.complete(v2);
853          checkIncomplete(h);
854 <        f.complete(3);
855 <        checkCompletedNormally(h, 2);
854 >        assertFalse(r.ran());
855 >        f.complete(v1);
856  
857 <        f = new CompletableFuture<>();
858 <        g = new CompletableFuture<>();
859 <        g.complete(1);
860 <        f.complete(3);
861 <        h = f.thenCombine(g, subtract);
862 <        checkCompletedNormally(h, 2);
857 >        checkCompletedNormally(h, subtract(v1, v2));
858 >        checkCompletedNormally(f, v1);
859 >        checkCompletedNormally(g, v2);
860 >        }
861 >    }
862 >
863 >    public void testThenCombine_normalCompletion3() {
864 >        for (ExecutionMode m : ExecutionMode.values())
865 >        for (Integer v1 : new Integer[] { 1, null })
866 >        for (Integer v2 : new Integer[] { 2, null }) {
867 >
868 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
869 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
870 >        final SubtractFunction r = new SubtractFunction();
871 >
872 >        g.complete(v2);
873 >        f.complete(v1);
874 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
875 >
876 >        checkCompletedNormally(h, subtract(v1, v2));
877 >        checkCompletedNormally(f, v1);
878 >        checkCompletedNormally(g, v2);
879 >        }
880 >    }
881 >
882 >    public void testThenCombine_normalCompletion4() {
883 >        for (ExecutionMode m : ExecutionMode.values())
884 >        for (Integer v1 : new Integer[] { 1, null })
885 >        for (Integer v2 : new Integer[] { 2, null }) {
886 >
887 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
888 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
889 >        final SubtractFunction r = new SubtractFunction();
890 >
891 >        f.complete(v1);
892 >        g.complete(v2);
893 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
894 >
895 >        checkCompletedNormally(h, subtract(v1, v2));
896 >        checkCompletedNormally(f, v1);
897 >        checkCompletedNormally(g, v2);
898 >        }
899      }
900  
901      /**
902       * thenCombine result completes exceptionally after exceptional
903       * completion of either source
904       */
905 <    public void testThenCombine2() {
906 <        CompletableFuture<Integer> f, g, h;
905 >    public void testThenCombine_exceptionalCompletion1() {
906 >        for (ExecutionMode m : ExecutionMode.values())
907 >        for (Integer v1 : new Integer[] { 1, null }) {
908  
909 <        f = new CompletableFuture<>();
910 <        g = new CompletableFuture<>();
911 <        h = f.thenCombine(g, subtract);
912 <        f.completeExceptionally(new CFException());
909 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
910 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
911 >        final SubtractFunction r = new SubtractFunction();
912 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
913 >        final CFException ex = new CFException();
914 >
915 >        f.completeExceptionally(ex);
916          checkIncomplete(h);
917 <        g.complete(1);
739 <        checkCompletedWithWrappedCFException(h);
917 >        g.complete(v1);
918  
919 <        f = new CompletableFuture<>();
920 <        g = new CompletableFuture<>();
921 <        h = f.thenCombine(g, subtract);
922 <        g.completeExceptionally(new CFException());
919 >        checkCompletedWithWrappedCFException(h, ex);
920 >        checkCompletedWithWrappedCFException(f, ex);
921 >        assertFalse(r.ran());
922 >        checkCompletedNormally(g, v1);
923 >        }
924 >    }
925 >
926 >    public void testThenCombine_exceptionalCompletion2() {
927 >        for (ExecutionMode m : ExecutionMode.values())
928 >        for (Integer v1 : new Integer[] { 1, null }) {
929 >
930 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
931 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
932 >        final SubtractFunction r = new SubtractFunction();
933 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract);
934 >        final CFException ex = new CFException();
935 >
936 >        g.completeExceptionally(ex);
937          checkIncomplete(h);
938 <        f.complete(3);
747 <        checkCompletedWithWrappedCFException(h);
938 >        f.complete(v1);
939  
940 <        f = new CompletableFuture<>();
941 <        g = new CompletableFuture<>();
942 <        f.complete(3);
943 <        g.completeExceptionally(new CFException());
944 <        h = f.thenCombine(g, subtract);
945 <        checkCompletedWithWrappedCFException(h);
940 >        checkCompletedWithWrappedCFException(h, ex);
941 >        checkCompletedWithWrappedCFException(g, ex);
942 >        assertFalse(r.ran());
943 >        checkCompletedNormally(f, v1);
944 >        }
945 >    }
946  
947 <        f = new CompletableFuture<>();
948 <        g = new CompletableFuture<>();
949 <        f.completeExceptionally(new CFException());
950 <        g.complete(3);
951 <        h = f.thenCombine(g, subtract);
952 <        checkCompletedWithWrappedCFException(h);
947 >    public void testThenCombine_exceptionalCompletion3() {
948 >        for (ExecutionMode m : ExecutionMode.values())
949 >        for (Integer v1 : new Integer[] { 1, null }) {
950 >
951 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
952 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
953 >        final SubtractFunction r = new SubtractFunction();
954 >        final CFException ex = new CFException();
955 >
956 >        g.completeExceptionally(ex);
957 >        f.complete(v1);
958 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
959 >
960 >        checkCompletedWithWrappedCFException(h, ex);
961 >        checkCompletedWithWrappedCFException(g, ex);
962 >        assertFalse(r.ran());
963 >        checkCompletedNormally(f, v1);
964 >        }
965 >    }
966 >
967 >    public void testThenCombine_exceptionalCompletion4() {
968 >        for (ExecutionMode m : ExecutionMode.values())
969 >        for (Integer v1 : new Integer[] { 1, null }) {
970 >
971 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
972 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
973 >        final SubtractFunction r = new SubtractFunction();
974 >        final CFException ex = new CFException();
975 >
976 >        f.completeExceptionally(ex);
977 >        g.complete(v1);
978 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract);
979 >
980 >        checkCompletedWithWrappedCFException(h, ex);
981 >        checkCompletedWithWrappedCFException(f, ex);
982 >        assertFalse(r.ran());
983 >        checkCompletedNormally(g, v1);
984 >        }
985      }
986  
987      /**
988       * thenCombine result completes exceptionally if action does
989       */
990 <    public void testThenCombine3() {
991 <        CompletableFuture<Integer> f = new CompletableFuture<>();
992 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
993 <        FailingBiFunction r = new FailingBiFunction();
994 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
995 <        f.complete(one);
996 <        checkIncomplete(g);
997 <        assertFalse(r.ran);
998 <        f2.complete(two);
999 <        checkCompletedWithWrappedCFException(g);
1000 <        assertTrue(r.ran);
990 >    public void testThenCombine_actionFailed1() {
991 >        for (ExecutionMode m : ExecutionMode.values())
992 >        for (Integer v1 : new Integer[] { 1, null })
993 >        for (Integer v2 : new Integer[] { 2, null }) {
994 >
995 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
996 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
997 >        final FailingBiFunction r = new FailingBiFunction();
998 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
999 >
1000 >        f.complete(v1);
1001 >        checkIncomplete(h);
1002 >        g.complete(v2);
1003 >
1004 >        checkCompletedWithWrappedCFException(h);
1005 >        checkCompletedNormally(f, v1);
1006 >        checkCompletedNormally(g, v2);
1007 >        }
1008 >    }
1009 >
1010 >    public void testThenCombine_actionFailed2() {
1011 >        for (ExecutionMode m : ExecutionMode.values())
1012 >        for (Integer v1 : new Integer[] { 1, null })
1013 >        for (Integer v2 : new Integer[] { 2, null }) {
1014 >
1015 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1016 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1017 >        final FailingBiFunction r = new FailingBiFunction();
1018 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1019 >
1020 >        g.complete(v2);
1021 >        checkIncomplete(h);
1022 >        f.complete(v1);
1023 >
1024 >        checkCompletedWithWrappedCFException(h);
1025 >        checkCompletedNormally(f, v1);
1026 >        checkCompletedNormally(g, v2);
1027 >        }
1028      }
1029  
1030      /**
1031       * thenCombine result completes exceptionally if either source cancelled
1032       */
1033 <    public void testThenCombine4() {
1034 <        CompletableFuture<Integer> f, g, h;
1033 >    public void testThenCombine_sourceCancelled1() {
1034 >        for (ExecutionMode m : ExecutionMode.values())
1035 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1036 >        for (Integer v1 : new Integer[] { 1, null }) {
1037  
1038 <        f = new CompletableFuture<>();
1039 <        g = new CompletableFuture<>();
1040 <        h = f.thenCombine(g, subtract);
1041 <        assertTrue(f.cancel(true));
1038 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1039 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1040 >        final SubtractFunction r = new SubtractFunction();
1041 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1042 >
1043 >        assertTrue(f.cancel(mayInterruptIfRunning));
1044          checkIncomplete(h);
1045 <        g.complete(1);
1045 >        g.complete(v1);
1046 >
1047          checkCompletedWithWrappedCancellationException(h);
1048 +        checkCancelled(f);
1049 +        assertFalse(r.ran());
1050 +        checkCompletedNormally(g, v1);
1051 +        }
1052 +    }
1053  
1054 <        f = new CompletableFuture<>();
1055 <        g = new CompletableFuture<>();
1056 <        h = f.thenCombine(g, subtract);
1057 <        assertTrue(g.cancel(true));
1054 >    public void testThenCombine_sourceCancelled2() {
1055 >        for (ExecutionMode m : ExecutionMode.values())
1056 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1057 >        for (Integer v1 : new Integer[] { 1, null }) {
1058 >
1059 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1060 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1061 >        final SubtractFunction r = new SubtractFunction();
1062 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1063 >
1064 >        assertTrue(g.cancel(mayInterruptIfRunning));
1065          checkIncomplete(h);
1066 <        f.complete(3);
1066 >        f.complete(v1);
1067 >
1068          checkCompletedWithWrappedCancellationException(h);
1069 +        checkCancelled(g);
1070 +        assertFalse(r.ran());
1071 +        checkCompletedNormally(f, v1);
1072 +        }
1073 +    }
1074 +
1075 +    public void testThenCombine_sourceCancelled3() {
1076 +        for (ExecutionMode m : ExecutionMode.values())
1077 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1078 +        for (Integer v1 : new Integer[] { 1, null }) {
1079 +
1080 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1081 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1082 +        final SubtractFunction r = new SubtractFunction();
1083 +
1084 +        assertTrue(g.cancel(mayInterruptIfRunning));
1085 +        f.complete(v1);
1086 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1087 +
1088 +        checkCompletedWithWrappedCancellationException(h);
1089 +        checkCancelled(g);
1090 +        assertFalse(r.ran());
1091 +        checkCompletedNormally(f, v1);
1092 +        }
1093 +    }
1094 +
1095 +    public void testThenCombine_sourceCancelled4() {
1096 +        for (ExecutionMode m : ExecutionMode.values())
1097 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1098 +        for (Integer v1 : new Integer[] { 1, null }) {
1099 +
1100 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1101 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1102 +        final SubtractFunction r = new SubtractFunction();
1103 +
1104 +        assertTrue(f.cancel(mayInterruptIfRunning));
1105 +        g.complete(v1);
1106 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1107  
802        f = new CompletableFuture<>();
803        g = new CompletableFuture<>();
804        assertTrue(f.cancel(true));
805        assertTrue(g.cancel(true));
806        h = f.thenCombine(g, subtract);
1108          checkCompletedWithWrappedCancellationException(h);
1109 +        checkCancelled(f);
1110 +        assertFalse(r.ran());
1111 +        checkCompletedNormally(g, v1);
1112 +        }
1113      }
1114  
1115      /**
1116       * thenAcceptBoth result completes normally after normal
1117       * completion of sources
1118       */
1119 <    public void testThenAcceptBoth() {
1120 <        CompletableFuture<Integer> f, g;
1121 <        CompletableFuture<Void> h;
1122 <        SubtractAction r;
1119 >    public void testThenAcceptBoth_normalCompletion1() {
1120 >        for (ExecutionMode m : ExecutionMode.values())
1121 >        for (Integer v1 : new Integer[] { 1, null })
1122 >        for (Integer v2 : new Integer[] { 2, null }) {
1123  
1124 <        f = new CompletableFuture<>();
1125 <        g = new CompletableFuture<>();
1126 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1127 <        f.complete(3);
1124 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1125 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1126 >        final SubtractAction r = new SubtractAction();
1127 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1128 >
1129 >        f.complete(v1);
1130          checkIncomplete(h);
1131 <        g.complete(1);
1131 >        assertEquals(r.value, 0);
1132 >        g.complete(v2);
1133 >
1134          checkCompletedNormally(h, null);
1135 <        assertEquals(r.value, 2);
1135 >        assertEquals(r.value, subtract(v1, v2));
1136 >        checkCompletedNormally(f, v1);
1137 >        checkCompletedNormally(g, v2);
1138 >        }
1139 >    }
1140  
1141 <        f = new CompletableFuture<>();
1142 <        g = new CompletableFuture<>();
1143 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1144 <        g.complete(1);
1141 >    public void testThenAcceptBoth_normalCompletion2() {
1142 >        for (ExecutionMode m : ExecutionMode.values())
1143 >        for (Integer v1 : new Integer[] { 1, null })
1144 >        for (Integer v2 : new Integer[] { 2, null }) {
1145 >
1146 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1147 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1148 >        final SubtractAction r = new SubtractAction();
1149 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1150 >
1151 >        g.complete(v2);
1152          checkIncomplete(h);
1153 <        f.complete(3);
1153 >        assertEquals(r.value, 0);
1154 >        f.complete(v1);
1155 >
1156          checkCompletedNormally(h, null);
1157 <        assertEquals(r.value, 2);
1157 >        assertEquals(r.value, subtract(v1, v2));
1158 >        checkCompletedNormally(f, v1);
1159 >        checkCompletedNormally(g, v2);
1160 >        }
1161 >    }
1162 >
1163 >    public void testThenAcceptBoth_normalCompletion3() {
1164 >        for (ExecutionMode m : ExecutionMode.values())
1165 >        for (Integer v1 : new Integer[] { 1, null })
1166 >        for (Integer v2 : new Integer[] { 2, null }) {
1167 >
1168 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1169 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1170 >        final SubtractAction r = new SubtractAction();
1171 >
1172 >        g.complete(v2);
1173 >        f.complete(v1);
1174 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1175  
837        f = new CompletableFuture<>();
838        g = new CompletableFuture<>();
839        g.complete(1);
840        f.complete(3);
841        h = f.thenAcceptBoth(g, r = new SubtractAction());
1176          checkCompletedNormally(h, null);
1177 <        assertEquals(r.value, 2);
1177 >        assertEquals(r.value, subtract(v1, v2));
1178 >        checkCompletedNormally(f, v1);
1179 >        checkCompletedNormally(g, v2);
1180 >        }
1181 >    }
1182 >
1183 >    public void testThenAcceptBoth_normalCompletion4() {
1184 >        for (ExecutionMode m : ExecutionMode.values())
1185 >        for (Integer v1 : new Integer[] { 1, null })
1186 >        for (Integer v2 : new Integer[] { 2, null }) {
1187 >
1188 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1189 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1190 >        final SubtractAction r = new SubtractAction();
1191 >
1192 >        f.complete(v1);
1193 >        g.complete(v2);
1194 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1195 >
1196 >        checkCompletedNormally(h, null);
1197 >        assertEquals(r.value, subtract(v1, v2));
1198 >        checkCompletedNormally(f, v1);
1199 >        checkCompletedNormally(g, v2);
1200 >        }
1201      }
1202  
1203      /**
1204       * thenAcceptBoth result completes exceptionally after exceptional
1205       * completion of either source
1206       */
1207 <    public void testThenAcceptBoth2() {
1208 <        CompletableFuture<Integer> f, g;
1209 <        CompletableFuture<Void> h;
853 <        SubtractAction r;
1207 >    public void testThenAcceptBoth_exceptionalCompletion1() {
1208 >        for (ExecutionMode m : ExecutionMode.values())
1209 >        for (Integer v1 : new Integer[] { 1, null }) {
1210  
1211 <        f = new CompletableFuture<>();
1212 <        g = new CompletableFuture<>();
1213 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1214 <        f.completeExceptionally(new CFException());
1211 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1212 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1213 >        final SubtractAction r = new SubtractAction();
1214 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1215 >        final CFException ex = new CFException();
1216 >
1217 >        f.completeExceptionally(ex);
1218          checkIncomplete(h);
1219 <        g.complete(1);
861 <        checkCompletedWithWrappedCFException(h);
1219 >        g.complete(v1);
1220  
1221 <        f = new CompletableFuture<>();
1222 <        g = new CompletableFuture<>();
1223 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1224 <        g.completeExceptionally(new CFException());
1221 >        checkCompletedWithWrappedCFException(h, ex);
1222 >        checkCompletedWithWrappedCFException(f, ex);
1223 >        assertFalse(r.ran());
1224 >        checkCompletedNormally(g, v1);
1225 >        }
1226 >    }
1227 >
1228 >    public void testThenAcceptBoth_exceptionalCompletion2() {
1229 >        for (ExecutionMode m : ExecutionMode.values())
1230 >        for (Integer v1 : new Integer[] { 1, null }) {
1231 >
1232 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1233 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1234 >        final SubtractAction r = new SubtractAction();
1235 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1236 >        final CFException ex = new CFException();
1237 >
1238 >        g.completeExceptionally(ex);
1239          checkIncomplete(h);
1240 <        f.complete(3);
869 <        checkCompletedWithWrappedCFException(h);
1240 >        f.complete(v1);
1241  
1242 <        f = new CompletableFuture<>();
1243 <        g = new CompletableFuture<>();
1244 <        f.complete(3);
1245 <        g.completeExceptionally(new CFException());
1246 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1247 <        checkCompletedWithWrappedCFException(h);
1242 >        checkCompletedWithWrappedCFException(h, ex);
1243 >        checkCompletedWithWrappedCFException(g, ex);
1244 >        assertFalse(r.ran());
1245 >        checkCompletedNormally(f, v1);
1246 >        }
1247 >    }
1248  
1249 <        f = new CompletableFuture<>();
1250 <        g = new CompletableFuture<>();
1251 <        f.completeExceptionally(new CFException());
1252 <        g.complete(3);
1253 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1254 <        checkCompletedWithWrappedCFException(h);
1249 >    public void testThenAcceptBoth_exceptionalCompletion3() {
1250 >        for (ExecutionMode m : ExecutionMode.values())
1251 >        for (Integer v1 : new Integer[] { 1, null }) {
1252 >
1253 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1254 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1255 >        final SubtractAction r = new SubtractAction();
1256 >        final CFException ex = new CFException();
1257 >
1258 >        g.completeExceptionally(ex);
1259 >        f.complete(v1);
1260 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1261 >
1262 >        checkCompletedWithWrappedCFException(h, ex);
1263 >        checkCompletedWithWrappedCFException(g, ex);
1264 >        assertFalse(r.ran());
1265 >        checkCompletedNormally(f, v1);
1266 >        }
1267 >    }
1268 >
1269 >    public void testThenAcceptBoth_exceptionalCompletion4() {
1270 >        for (ExecutionMode m : ExecutionMode.values())
1271 >        for (Integer v1 : new Integer[] { 1, null }) {
1272 >
1273 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1274 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1275 >        final SubtractAction r = new SubtractAction();
1276 >        final CFException ex = new CFException();
1277 >
1278 >        f.completeExceptionally(ex);
1279 >        g.complete(v1);
1280 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1281 >
1282 >        checkCompletedWithWrappedCFException(h, ex);
1283 >        checkCompletedWithWrappedCFException(f, ex);
1284 >        assertFalse(r.ran());
1285 >        checkCompletedNormally(g, v1);
1286 >        }
1287      }
1288  
1289      /**
1290       * thenAcceptBoth result completes exceptionally if action does
1291       */
1292 <    public void testThenAcceptBoth3() {
1293 <        CompletableFuture<Integer> f, g;
1294 <        CompletableFuture<Void> h;
1295 <        FailingBiConsumer r;
1292 >    public void testThenAcceptBoth_actionFailed1() {
1293 >        for (ExecutionMode m : ExecutionMode.values())
1294 >        for (Integer v1 : new Integer[] { 1, null })
1295 >        for (Integer v2 : new Integer[] { 2, null }) {
1296  
1297 <        f = new CompletableFuture<>();
1298 <        g = new CompletableFuture<>();
1299 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1300 <        f.complete(3);
1297 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1298 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1299 >        final FailingBiConsumer r = new FailingBiConsumer();
1300 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1301 >
1302 >        f.complete(v1);
1303          checkIncomplete(h);
1304 <        g.complete(1);
1304 >        g.complete(v2);
1305 >
1306          checkCompletedWithWrappedCFException(h);
1307 +        checkCompletedNormally(f, v1);
1308 +        checkCompletedNormally(g, v2);
1309 +        }
1310 +    }
1311 +
1312 +    public void testThenAcceptBoth_actionFailed2() {
1313 +        for (ExecutionMode m : ExecutionMode.values())
1314 +        for (Integer v1 : new Integer[] { 1, null })
1315 +        for (Integer v2 : new Integer[] { 2, null }) {
1316 +
1317 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1318 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1319 +        final FailingBiConsumer r = new FailingBiConsumer();
1320 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1321 +
1322 +        g.complete(v2);
1323 +        checkIncomplete(h);
1324 +        f.complete(v1);
1325  
902        f = new CompletableFuture<>();
903        g = new CompletableFuture<>();
904        f.complete(3);
905        g.complete(1);
906        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1326          checkCompletedWithWrappedCFException(h);
1327 +        checkCompletedNormally(f, v1);
1328 +        checkCompletedNormally(g, v2);
1329 +        }
1330      }
1331  
1332      /**
1333       * thenAcceptBoth result completes exceptionally if either source cancelled
1334       */
1335 <    public void testThenAcceptBoth4() {
1336 <        CompletableFuture<Integer> f, g;
1337 <        CompletableFuture<Void> h;
1338 <        SubtractAction r;
1335 >    public void testThenAcceptBoth_sourceCancelled1() {
1336 >        for (ExecutionMode m : ExecutionMode.values())
1337 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1338 >        for (Integer v1 : new Integer[] { 1, null }) {
1339  
1340 <        f = new CompletableFuture<>();
1341 <        g = new CompletableFuture<>();
1342 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1343 <        assertTrue(f.cancel(true));
1340 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1341 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1342 >        final SubtractAction r = new SubtractAction();
1343 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1344 >
1345 >        assertTrue(f.cancel(mayInterruptIfRunning));
1346          checkIncomplete(h);
1347 <        g.complete(1);
1347 >        g.complete(v1);
1348 >
1349          checkCompletedWithWrappedCancellationException(h);
1350 +        checkCancelled(f);
1351 +        assertFalse(r.ran());
1352 +        checkCompletedNormally(g, v1);
1353 +        }
1354 +    }
1355  
1356 <        f = new CompletableFuture<>();
1357 <        g = new CompletableFuture<>();
1358 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1359 <        assertTrue(g.cancel(true));
1356 >    public void testThenAcceptBoth_sourceCancelled2() {
1357 >        for (ExecutionMode m : ExecutionMode.values())
1358 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1359 >        for (Integer v1 : new Integer[] { 1, null }) {
1360 >
1361 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1362 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1363 >        final SubtractAction r = new SubtractAction();
1364 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1365 >
1366 >        assertTrue(g.cancel(mayInterruptIfRunning));
1367          checkIncomplete(h);
1368 <        f.complete(3);
932 <        checkCompletedWithWrappedCancellationException(h);
1368 >        f.complete(v1);
1369  
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());
1370          checkCompletedWithWrappedCancellationException(h);
1371 +        checkCancelled(g);
1372 +        assertFalse(r.ran());
1373 +        checkCompletedNormally(f, v1);
1374 +        }
1375 +    }
1376 +
1377 +    public void testThenAcceptBoth_sourceCancelled3() {
1378 +        for (ExecutionMode m : ExecutionMode.values())
1379 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1380 +        for (Integer v1 : new Integer[] { 1, null }) {
1381 +
1382 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1383 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1384 +        final SubtractAction r = new SubtractAction();
1385 +
1386 +        assertTrue(g.cancel(mayInterruptIfRunning));
1387 +        f.complete(v1);
1388 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1389  
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());
1390          checkCompletedWithWrappedCancellationException(h);
1391 +        checkCancelled(g);
1392 +        assertFalse(r.ran());
1393 +        checkCompletedNormally(f, v1);
1394 +        }
1395      }
1396  
1397 <    /**
1398 <     * Permits the testing of parallel code for the 3 different
1399 <     * execution modes without repeating all the testing code.
1400 <     */
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 <        },
1397 >    public void testThenAcceptBoth_sourceCancelled4() {
1398 >        for (ExecutionMode m : ExecutionMode.values())
1399 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1400 >        for (Integer v1 : new Integer[] { 1, null }) {
1401  
1402 <        DEFAULT_ASYNC {
1403 <            public <T,U> CompletableFuture<Void> runAfterBoth
1404 <                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) {
964 <                return f.runAfterBothAsync(g, r);
965 <            }
966 <        },
1402 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1403 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1404 >        final SubtractAction r = new SubtractAction();
1405  
1406 <        EXECUTOR {
1407 <            public <T,U> CompletableFuture<Void> runAfterBoth
1408 <                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) {
971 <                return f.runAfterBothAsync(g, r, new ThreadExecutor());
972 <            }
973 <        };
1406 >        assertTrue(f.cancel(mayInterruptIfRunning));
1407 >        g.complete(v1);
1408 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1409  
1410 <        public abstract <T,U> CompletableFuture<Void> runAfterBoth
1411 <            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r);
1410 >        checkCompletedWithWrappedCancellationException(h);
1411 >        checkCancelled(f);
1412 >        assertFalse(r.ran());
1413 >        checkCompletedNormally(g, v1);
1414 >        }
1415      }
1416  
1417      /**
# Line 1713 | Line 2151 | public class CompletableFutureTest exten
2151      }
2152  
2153      /**
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    /**
2154       * applyToEitherAsync result completes normally after normal
2155       * completion of sources
2156       */
# Line 2406 | Line 2590 | public class CompletableFutureTest exten
2590      }
2591  
2592      /**
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    /**
2593       * applyToEitherAsync result completes normally after normal
2594       * completion of sources
2595       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines