test/jdk/java/util/concurrent/tck/CompletableFutureTest.java
changeset 51950 a1c24d06e2b5
parent 50764 5637aca18f1d
child 52730 345266000aba
equal deleted inserted replaced
51949:2a1e47af3c6b 51950:a1c24d06e2b5
    84 
    84 
    85     void checkIncomplete(CompletableFuture<?> f) {
    85     void checkIncomplete(CompletableFuture<?> f) {
    86         assertFalse(f.isDone());
    86         assertFalse(f.isDone());
    87         assertFalse(f.isCancelled());
    87         assertFalse(f.isCancelled());
    88         assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
    88         assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
       
    89 
       
    90         Object result = null;
    89         try {
    91         try {
    90             assertNull(f.getNow(null));
    92             result = f.getNow(null);
    91         } catch (Throwable fail) { threadUnexpectedException(fail); }
    93         } catch (Throwable fail) { threadUnexpectedException(fail); }
       
    94         assertNull(result);
       
    95 
    92         try {
    96         try {
    93             f.get(randomExpiredTimeout(), randomTimeUnit());
    97             f.get(randomExpiredTimeout(), randomTimeUnit());
    94             shouldThrow();
    98             shouldThrow();
    95         }
    99         }
    96         catch (TimeoutException success) {}
   100         catch (TimeoutException success) {}
    97         catch (Throwable fail) { threadUnexpectedException(fail); }
   101         catch (Throwable fail) { threadUnexpectedException(fail); }
    98     }
   102     }
    99 
   103 
   100     <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
   104     <T> void checkCompletedNormally(CompletableFuture<T> f, T expectedValue) {
   101         checkTimedGet(f, value);
   105         checkTimedGet(f, expectedValue);
   102 
   106 
       
   107         assertEquals(expectedValue, f.join());
       
   108         assertEquals(expectedValue, f.getNow(null));
       
   109 
       
   110         T result = null;
   103         try {
   111         try {
   104             assertEquals(value, f.join());
   112             result = f.get();
   105             assertEquals(value, f.getNow(null));
       
   106             assertEquals(value, f.get());
       
   107         } catch (Throwable fail) { threadUnexpectedException(fail); }
   113         } catch (Throwable fail) { threadUnexpectedException(fail); }
       
   114         assertEquals(expectedValue, result);
       
   115 
   108         assertTrue(f.isDone());
   116         assertTrue(f.isDone());
   109         assertFalse(f.isCancelled());
   117         assertFalse(f.isCancelled());
   110         assertFalse(f.isCompletedExceptionally());
   118         assertFalse(f.isCompletedExceptionally());
   111         assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
   119         assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
   112     }
   120     }
   568     {
   576     {
   569         CompletableFutureInc(ExecutionMode m) { super(m); }
   577         CompletableFutureInc(ExecutionMode m) { super(m); }
   570         public CompletableFuture<Integer> apply(Integer x) {
   578         public CompletableFuture<Integer> apply(Integer x) {
   571             invoked();
   579             invoked();
   572             value = x;
   580             value = x;
   573             CompletableFuture<Integer> f = new CompletableFuture<>();
   581             return CompletableFuture.completedFuture(inc(x));
   574             assertTrue(f.complete(inc(x)));
   582         }
   575             return f;
   583     }
       
   584 
       
   585     static class FailingExceptionalCompletableFutureFunction extends CheckedAction
       
   586         implements Function<Throwable, CompletableFuture<Integer>>
       
   587     {
       
   588         final CFException ex;
       
   589         FailingExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
       
   590         public CompletableFuture<Integer> apply(Throwable x) {
       
   591             invoked();
       
   592             throw ex;
       
   593         }
       
   594     }
       
   595 
       
   596     static class ExceptionalCompletableFutureFunction extends CheckedAction
       
   597         implements Function<Throwable, CompletionStage<Integer>> {
       
   598         final Integer value = 3;
       
   599         ExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); }
       
   600         public CompletionStage<Integer> apply(Throwable x) {
       
   601             invoked();
       
   602             return CompletableFuture.completedFuture(value);
   576         }
   603         }
   577     }
   604     }
   578 
   605 
   579     static class FailingCompletableFutureFunction extends CheckedIntegerAction
   606     static class FailingCompletableFutureFunction extends CheckedIntegerAction
   580         implements Function<Integer, CompletableFuture<Integer>>
   607         implements Function<Integer, CompletableFuture<Integer>>
   689                 (CompletableFuture<T> f,
   716                 (CompletableFuture<T> f,
   690                  CompletionStage<? extends T> g,
   717                  CompletionStage<? extends T> g,
   691                  Function<? super T,U> a) {
   718                  Function<? super T,U> a) {
   692                 return f.applyToEither(g, a);
   719                 return f.applyToEither(g, a);
   693             }
   720             }
       
   721             public <T> CompletableFuture<T> exceptionally
       
   722                 (CompletableFuture<T> f,
       
   723                  Function<Throwable, ? extends T> fn) {
       
   724                 return f.exceptionally(fn);
       
   725             }
       
   726             public <T> CompletableFuture<T> exceptionallyCompose
       
   727                 (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
       
   728                 return f.exceptionallyCompose(fn);
       
   729             }
   694         },
   730         },
   695 
       
   696         ASYNC {
   731         ASYNC {
   697             public void checkExecutionMode() {
   732             public void checkExecutionMode() {
   698                 assertEquals(defaultExecutorIsCommonPool,
   733                 assertEquals(defaultExecutorIsCommonPool,
   699                              (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
   734                              (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
   700             }
   735             }
   763                 (CompletableFuture<T> f,
   798                 (CompletableFuture<T> f,
   764                  CompletionStage<? extends T> g,
   799                  CompletionStage<? extends T> g,
   765                  Function<? super T,U> a) {
   800                  Function<? super T,U> a) {
   766                 return f.applyToEitherAsync(g, a);
   801                 return f.applyToEitherAsync(g, a);
   767             }
   802             }
       
   803             public <T> CompletableFuture<T> exceptionally
       
   804                 (CompletableFuture<T> f,
       
   805                  Function<Throwable, ? extends T> fn) {
       
   806                 return f.exceptionallyAsync(fn);
       
   807             }
       
   808 
       
   809             public <T> CompletableFuture<T> exceptionallyCompose
       
   810                 (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
       
   811                 return f.exceptionallyComposeAsync(fn);
       
   812             }
       
   813 
   768         },
   814         },
   769 
   815 
   770         EXECUTOR {
   816         EXECUTOR {
   771             public void checkExecutionMode() {
   817             public void checkExecutionMode() {
   772                 assertTrue(ThreadExecutor.startedCurrentThread());
   818                 assertTrue(ThreadExecutor.startedCurrentThread());
   836                 (CompletableFuture<T> f,
   882                 (CompletableFuture<T> f,
   837                  CompletionStage<? extends T> g,
   883                  CompletionStage<? extends T> g,
   838                  Function<? super T,U> a) {
   884                  Function<? super T,U> a) {
   839                 return f.applyToEitherAsync(g, a, new ThreadExecutor());
   885                 return f.applyToEitherAsync(g, a, new ThreadExecutor());
   840             }
   886             }
       
   887             public <T> CompletableFuture<T> exceptionally
       
   888                 (CompletableFuture<T> f,
       
   889                  Function<Throwable, ? extends T> fn) {
       
   890                 return f.exceptionallyAsync(fn, new ThreadExecutor());
       
   891             }
       
   892             public <T> CompletableFuture<T> exceptionallyCompose
       
   893                 (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
       
   894                 return f.exceptionallyComposeAsync(fn, new ThreadExecutor());
       
   895             }
       
   896 
   841         };
   897         };
   842 
   898 
   843         public abstract void checkExecutionMode();
   899         public abstract void checkExecutionMode();
   844         public abstract CompletableFuture<Void> runAsync(Runnable a);
   900         public abstract CompletableFuture<Void> runAsync(Runnable a);
   845         public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
   901         public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
   878              Consumer<? super T> a);
   934              Consumer<? super T> a);
   879         public abstract <T,U> CompletableFuture<U> applyToEither
   935         public abstract <T,U> CompletableFuture<U> applyToEither
   880             (CompletableFuture<T> f,
   936             (CompletableFuture<T> f,
   881              CompletionStage<? extends T> g,
   937              CompletionStage<? extends T> g,
   882              Function<? super T,U> a);
   938              Function<? super T,U> a);
       
   939         public abstract <T> CompletableFuture<T> exceptionally
       
   940             (CompletableFuture<T> f,
       
   941              Function<Throwable, ? extends T> fn);
       
   942         public abstract <T> CompletableFuture<T> exceptionallyCompose
       
   943             (CompletableFuture<T> f,
       
   944              Function<Throwable, ? extends CompletionStage<T>> fn);
   883     }
   945     }
   884 
   946 
   885     /**
   947     /**
   886      * exceptionally action is not invoked when source completes
   948      * exceptionally action is not invoked when source completes
   887      * normally, and source result is propagated
   949      * normally, and source result is propagated
   888      */
   950      */
   889     public void testExceptionally_normalCompletion() {
   951     public void testExceptionally_normalCompletion() {
       
   952         for (ExecutionMode m : ExecutionMode.values())
   890         for (boolean createIncomplete : new boolean[] { true, false })
   953         for (boolean createIncomplete : new boolean[] { true, false })
   891         for (Integer v1 : new Integer[] { 1, null })
   954         for (Integer v1 : new Integer[] { 1, null })
   892     {
   955     {
   893         final AtomicInteger a = new AtomicInteger(0);
       
   894         final CompletableFuture<Integer> f = new CompletableFuture<>();
   956         final CompletableFuture<Integer> f = new CompletableFuture<>();
   895         if (!createIncomplete) assertTrue(f.complete(v1));
   957         if (!createIncomplete) assertTrue(f.complete(v1));
   896         final CompletableFuture<Integer> g = f.exceptionally
   958         final CompletableFuture<Integer> g = m.exceptionally
   897             ((Throwable t) -> {
   959             (f, (Throwable t) -> {
   898                 a.getAndIncrement();
       
   899                 threadFail("should not be called");
   960                 threadFail("should not be called");
   900                 return null;            // unreached
   961                 return null;            // unreached
   901             });
   962             });
   902         if (createIncomplete) assertTrue(f.complete(v1));
   963         if (createIncomplete) assertTrue(f.complete(v1));
   903 
   964 
   904         checkCompletedNormally(g, v1);
   965         checkCompletedNormally(g, v1);
   905         checkCompletedNormally(f, v1);
   966         checkCompletedNormally(f, v1);
   906         assertEquals(0, a.get());
       
   907     }}
   967     }}
   908 
   968 
   909     /**
   969     /**
   910      * exceptionally action completes with function value on source
   970      * exceptionally action completes with function value on source
   911      * exception
   971      * exception
   912      */
   972      */
   913     public void testExceptionally_exceptionalCompletion() {
   973     public void testExceptionally_exceptionalCompletion() {
       
   974         for (ExecutionMode m : ExecutionMode.values())
   914         for (boolean createIncomplete : new boolean[] { true, false })
   975         for (boolean createIncomplete : new boolean[] { true, false })
   915         for (Integer v1 : new Integer[] { 1, null })
   976         for (Integer v1 : new Integer[] { 1, null })
   916     {
   977     {
   917         final AtomicInteger a = new AtomicInteger(0);
   978         final AtomicInteger a = new AtomicInteger(0);
   918         final CFException ex = new CFException();
   979         final CFException ex = new CFException();
   919         final CompletableFuture<Integer> f = new CompletableFuture<>();
   980         final CompletableFuture<Integer> f = new CompletableFuture<>();
   920         if (!createIncomplete) f.completeExceptionally(ex);
   981         if (!createIncomplete) f.completeExceptionally(ex);
   921         final CompletableFuture<Integer> g = f.exceptionally
   982         final CompletableFuture<Integer> g = m.exceptionally
   922             ((Throwable t) -> {
   983             (f, (Throwable t) -> {
   923                 ExecutionMode.SYNC.checkExecutionMode();
   984                 m.checkExecutionMode();
   924                 threadAssertSame(t, ex);
   985                 threadAssertSame(t, ex);
   925                 a.getAndIncrement();
   986                 a.getAndIncrement();
   926                 return v1;
   987                 return v1;
   927             });
   988             });
   928         if (createIncomplete) f.completeExceptionally(ex);
   989         if (createIncomplete) f.completeExceptionally(ex);
   934     /**
   995     /**
   935      * If an "exceptionally action" throws an exception, it completes
   996      * If an "exceptionally action" throws an exception, it completes
   936      * exceptionally with that exception
   997      * exceptionally with that exception
   937      */
   998      */
   938     public void testExceptionally_exceptionalCompletionActionFailed() {
   999     public void testExceptionally_exceptionalCompletionActionFailed() {
       
  1000         for (ExecutionMode m : ExecutionMode.values())
   939         for (boolean createIncomplete : new boolean[] { true, false })
  1001         for (boolean createIncomplete : new boolean[] { true, false })
   940     {
  1002     {
   941         final AtomicInteger a = new AtomicInteger(0);
  1003         final AtomicInteger a = new AtomicInteger(0);
   942         final CFException ex1 = new CFException();
  1004         final CFException ex1 = new CFException();
   943         final CFException ex2 = new CFException();
  1005         final CFException ex2 = new CFException();
   944         final CompletableFuture<Integer> f = new CompletableFuture<>();
  1006         final CompletableFuture<Integer> f = new CompletableFuture<>();
   945         if (!createIncomplete) f.completeExceptionally(ex1);
  1007         if (!createIncomplete) f.completeExceptionally(ex1);
   946         final CompletableFuture<Integer> g = f.exceptionally
  1008         final CompletableFuture<Integer> g = m.exceptionally
   947             ((Throwable t) -> {
  1009             (f, (Throwable t) -> {
   948                 ExecutionMode.SYNC.checkExecutionMode();
  1010                 m.checkExecutionMode();
   949                 threadAssertSame(t, ex1);
  1011                 threadAssertSame(t, ex1);
   950                 a.getAndIncrement();
  1012                 a.getAndIncrement();
   951                 throw ex2;
  1013                 throw ex2;
   952             });
  1014             });
   953         if (createIncomplete) f.completeExceptionally(ex1);
  1015         if (createIncomplete) f.completeExceptionally(ex1);
  3114         checkCompletedExceptionally(g, ex);
  3176         checkCompletedExceptionally(g, ex);
  3115         checkCompletedWithWrappedException(h, ex);
  3177         checkCompletedWithWrappedException(h, ex);
  3116         checkCompletedNormally(f, v1);
  3178         checkCompletedNormally(f, v1);
  3117     }}
  3179     }}
  3118 
  3180 
       
  3181     /**
       
  3182      * exceptionallyCompose result completes normally after normal
       
  3183      * completion of source
       
  3184      */
       
  3185     public void testExceptionallyCompose_normalCompletion() {
       
  3186         for (ExecutionMode m : ExecutionMode.values())
       
  3187         for (boolean createIncomplete : new boolean[] { true, false })
       
  3188         for (Integer v1 : new Integer[] { 1, null })
       
  3189     {
       
  3190         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  3191         final ExceptionalCompletableFutureFunction r =
       
  3192             new ExceptionalCompletableFutureFunction(m);
       
  3193         if (!createIncomplete) assertTrue(f.complete(v1));
       
  3194         final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
       
  3195         if (createIncomplete) assertTrue(f.complete(v1));
       
  3196 
       
  3197         checkCompletedNormally(f, v1);
       
  3198         checkCompletedNormally(g, v1);
       
  3199         r.assertNotInvoked();
       
  3200     }}
       
  3201 
       
  3202     /**
       
  3203      * exceptionallyCompose result completes normally after exceptional
       
  3204      * completion of source
       
  3205      */
       
  3206     public void testExceptionallyCompose_exceptionalCompletion() {
       
  3207         for (ExecutionMode m : ExecutionMode.values())
       
  3208         for (boolean createIncomplete : new boolean[] { true, false })
       
  3209     {
       
  3210         final CFException ex = new CFException();
       
  3211         final ExceptionalCompletableFutureFunction r =
       
  3212             new ExceptionalCompletableFutureFunction(m);
       
  3213         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  3214         if (!createIncomplete) f.completeExceptionally(ex);
       
  3215         final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
       
  3216         if (createIncomplete) f.completeExceptionally(ex);
       
  3217 
       
  3218         checkCompletedExceptionally(f, ex);
       
  3219         checkCompletedNormally(g, r.value);
       
  3220         r.assertInvoked();
       
  3221     }}
       
  3222 
       
  3223     /**
       
  3224      * exceptionallyCompose completes exceptionally on exception if action does
       
  3225      */
       
  3226     public void testExceptionallyCompose_actionFailed() {
       
  3227         for (ExecutionMode m : ExecutionMode.values())
       
  3228         for (boolean createIncomplete : new boolean[] { true, false })
       
  3229     {
       
  3230         final CFException ex = new CFException();
       
  3231         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  3232         final FailingExceptionalCompletableFutureFunction r
       
  3233             = new FailingExceptionalCompletableFutureFunction(m);
       
  3234         if (!createIncomplete) f.completeExceptionally(ex);
       
  3235         final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
       
  3236         if (createIncomplete) f.completeExceptionally(ex);
       
  3237 
       
  3238         checkCompletedExceptionally(f, ex);
       
  3239         checkCompletedWithWrappedException(g, r.ex);
       
  3240         r.assertInvoked();
       
  3241     }}
       
  3242 
       
  3243     /**
       
  3244      * exceptionallyCompose result completes exceptionally if the
       
  3245      * result of the action does
       
  3246      */
       
  3247     public void testExceptionallyCompose_actionReturnsFailingFuture() {
       
  3248         for (ExecutionMode m : ExecutionMode.values())
       
  3249         for (int order = 0; order < 6; order++)
       
  3250     {
       
  3251         final CFException ex0 = new CFException();
       
  3252         final CFException ex = new CFException();
       
  3253         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  3254         final CompletableFuture<Integer> g = new CompletableFuture<>();
       
  3255         final CompletableFuture<Integer> h;
       
  3256         // Test all permutations of orders
       
  3257         switch (order) {
       
  3258         case 0:
       
  3259             assertTrue(f.completeExceptionally(ex0));
       
  3260             assertTrue(g.completeExceptionally(ex));
       
  3261             h = m.exceptionallyCompose(f, (x -> g));
       
  3262             break;
       
  3263         case 1:
       
  3264             assertTrue(f.completeExceptionally(ex0));
       
  3265             h = m.exceptionallyCompose(f, (x -> g));
       
  3266             assertTrue(g.completeExceptionally(ex));
       
  3267             break;
       
  3268         case 2:
       
  3269             assertTrue(g.completeExceptionally(ex));
       
  3270             assertTrue(f.completeExceptionally(ex0));
       
  3271             h = m.exceptionallyCompose(f, (x -> g));
       
  3272             break;
       
  3273         case 3:
       
  3274             assertTrue(g.completeExceptionally(ex));
       
  3275             h = m.exceptionallyCompose(f, (x -> g));
       
  3276             assertTrue(f.completeExceptionally(ex0));
       
  3277             break;
       
  3278         case 4:
       
  3279             h = m.exceptionallyCompose(f, (x -> g));
       
  3280             assertTrue(f.completeExceptionally(ex0));
       
  3281             assertTrue(g.completeExceptionally(ex));
       
  3282             break;
       
  3283         case 5:
       
  3284             h = m.exceptionallyCompose(f, (x -> g));
       
  3285             assertTrue(f.completeExceptionally(ex0));
       
  3286             assertTrue(g.completeExceptionally(ex));
       
  3287             break;
       
  3288         default: throw new AssertionError();
       
  3289         }
       
  3290 
       
  3291         checkCompletedExceptionally(g, ex);
       
  3292         checkCompletedWithWrappedException(h, ex);
       
  3293         checkCompletedExceptionally(f, ex0);
       
  3294     }}
       
  3295 
  3119     // other static methods
  3296     // other static methods
  3120 
  3297 
  3121     /**
  3298     /**
  3122      * allOf(no component futures) returns a future completed normally
  3299      * allOf(no component futures) returns a future completed normally
  3123      * with the value null
  3300      * with the value null
  4525 
  4702 
  4526 //     static <U> boolean isDone2(CompletionStage<U> stage) {
  4703 //     static <U> boolean isDone2(CompletionStage<U> stage) {
  4527 //         return stage.toCompletableFuture().copy().isDone();
  4704 //         return stage.toCompletableFuture().copy().isDone();
  4528 //     }
  4705 //     }
  4529 
  4706 
       
  4707     // For testing default implementations
       
  4708     // Only non-default interface methods defined.
       
  4709     static final class DelegatedCompletionStage<T> implements CompletionStage<T> {
       
  4710         final CompletableFuture<T> cf;
       
  4711         DelegatedCompletionStage(CompletableFuture<T> cf) { this.cf = cf; }
       
  4712         public CompletableFuture<T> toCompletableFuture() {
       
  4713             return cf; }
       
  4714         public CompletionStage<Void> thenRun
       
  4715             (Runnable action) {
       
  4716             return cf.thenRun(action); }
       
  4717         public CompletionStage<Void> thenRunAsync
       
  4718             (Runnable action) {
       
  4719             return cf.thenRunAsync(action); }
       
  4720         public CompletionStage<Void> thenRunAsync
       
  4721             (Runnable action,
       
  4722              Executor executor) {
       
  4723             return cf.thenRunAsync(action, executor); }
       
  4724         public CompletionStage<Void> thenAccept
       
  4725             (Consumer<? super T> action) {
       
  4726             return cf.thenAccept(action); }
       
  4727         public CompletionStage<Void> thenAcceptAsync
       
  4728             (Consumer<? super T> action) {
       
  4729             return cf.thenAcceptAsync(action); }
       
  4730         public CompletionStage<Void> thenAcceptAsync
       
  4731             (Consumer<? super T> action,
       
  4732              Executor executor) {
       
  4733             return cf.thenAcceptAsync(action, executor); }
       
  4734         public <U> CompletionStage<U> thenApply
       
  4735             (Function<? super T,? extends U> a) {
       
  4736             return cf.thenApply(a); }
       
  4737         public <U> CompletionStage<U> thenApplyAsync
       
  4738             (Function<? super T,? extends U> fn) {
       
  4739             return cf.thenApplyAsync(fn); }
       
  4740         public <U> CompletionStage<U> thenApplyAsync
       
  4741             (Function<? super T,? extends U> fn,
       
  4742              Executor executor) {
       
  4743             return cf.thenApplyAsync(fn, executor); }
       
  4744         public <U,V> CompletionStage<V> thenCombine
       
  4745             (CompletionStage<? extends U> other,
       
  4746              BiFunction<? super T,? super U,? extends V> fn) {
       
  4747             return cf.thenCombine(other, fn); }
       
  4748         public <U,V> CompletionStage<V> thenCombineAsync
       
  4749             (CompletionStage<? extends U> other,
       
  4750              BiFunction<? super T,? super U,? extends V> fn) {
       
  4751             return cf.thenCombineAsync(other, fn); }
       
  4752         public <U,V> CompletionStage<V> thenCombineAsync
       
  4753             (CompletionStage<? extends U> other,
       
  4754              BiFunction<? super T,? super U,? extends V> fn,
       
  4755              Executor executor) {
       
  4756             return cf.thenCombineAsync(other, fn, executor); }
       
  4757         public <U> CompletionStage<Void> thenAcceptBoth
       
  4758             (CompletionStage<? extends U> other,
       
  4759              BiConsumer<? super T, ? super U> action) {
       
  4760             return cf.thenAcceptBoth(other, action); }
       
  4761         public <U> CompletionStage<Void> thenAcceptBothAsync
       
  4762             (CompletionStage<? extends U> other,
       
  4763              BiConsumer<? super T, ? super U> action) {
       
  4764             return cf.thenAcceptBothAsync(other, action); }
       
  4765         public <U> CompletionStage<Void> thenAcceptBothAsync
       
  4766             (CompletionStage<? extends U> other,
       
  4767              BiConsumer<? super T, ? super U> action,
       
  4768              Executor executor) {
       
  4769             return cf.thenAcceptBothAsync(other, action, executor); }
       
  4770         public CompletionStage<Void> runAfterBoth
       
  4771             (CompletionStage<?> other,
       
  4772              Runnable action) {
       
  4773             return cf.runAfterBoth(other, action); }
       
  4774         public CompletionStage<Void> runAfterBothAsync
       
  4775             (CompletionStage<?> other,
       
  4776              Runnable action) {
       
  4777             return cf.runAfterBothAsync(other, action); }
       
  4778         public CompletionStage<Void> runAfterBothAsync
       
  4779             (CompletionStage<?> other,
       
  4780              Runnable action,
       
  4781              Executor executor) {
       
  4782             return cf.runAfterBothAsync(other, action, executor); }
       
  4783         public <U> CompletionStage<U> applyToEither
       
  4784             (CompletionStage<? extends T> other,
       
  4785              Function<? super T, U> fn) {
       
  4786             return cf.applyToEither(other, fn); }
       
  4787         public <U> CompletionStage<U> applyToEitherAsync
       
  4788             (CompletionStage<? extends T> other,
       
  4789              Function<? super T, U> fn) {
       
  4790             return cf.applyToEitherAsync(other, fn); }
       
  4791         public <U> CompletionStage<U> applyToEitherAsync
       
  4792             (CompletionStage<? extends T> other,
       
  4793              Function<? super T, U> fn,
       
  4794              Executor executor) {
       
  4795             return cf.applyToEitherAsync(other, fn, executor); }
       
  4796         public CompletionStage<Void> acceptEither
       
  4797             (CompletionStage<? extends T> other,
       
  4798              Consumer<? super T> action) {
       
  4799             return cf.acceptEither(other, action); }
       
  4800         public CompletionStage<Void> acceptEitherAsync
       
  4801             (CompletionStage<? extends T> other,
       
  4802              Consumer<? super T> action) {
       
  4803             return cf.acceptEitherAsync(other, action); }
       
  4804         public CompletionStage<Void> acceptEitherAsync
       
  4805             (CompletionStage<? extends T> other,
       
  4806              Consumer<? super T> action,
       
  4807              Executor executor) {
       
  4808             return cf.acceptEitherAsync(other, action, executor); }
       
  4809         public CompletionStage<Void> runAfterEither
       
  4810             (CompletionStage<?> other,
       
  4811              Runnable action) {
       
  4812             return cf.runAfterEither(other, action); }
       
  4813         public CompletionStage<Void> runAfterEitherAsync
       
  4814             (CompletionStage<?> other,
       
  4815              Runnable action) {
       
  4816             return cf.runAfterEitherAsync(other, action); }
       
  4817         public CompletionStage<Void> runAfterEitherAsync
       
  4818             (CompletionStage<?> other,
       
  4819              Runnable action,
       
  4820              Executor executor) {
       
  4821             return cf.runAfterEitherAsync(other, action, executor); }
       
  4822         public <U> CompletionStage<U> thenCompose
       
  4823             (Function<? super T, ? extends CompletionStage<U>> fn) {
       
  4824             return cf.thenCompose(fn); }
       
  4825         public <U> CompletionStage<U> thenComposeAsync
       
  4826             (Function<? super T, ? extends CompletionStage<U>> fn) {
       
  4827             return cf.thenComposeAsync(fn); }
       
  4828         public <U> CompletionStage<U> thenComposeAsync
       
  4829             (Function<? super T, ? extends CompletionStage<U>> fn,
       
  4830              Executor executor) {
       
  4831             return cf.thenComposeAsync(fn, executor); }
       
  4832         public <U> CompletionStage<U> handle
       
  4833             (BiFunction<? super T, Throwable, ? extends U> fn) {
       
  4834             return cf.handle(fn); }
       
  4835         public <U> CompletionStage<U> handleAsync
       
  4836             (BiFunction<? super T, Throwable, ? extends U> fn) {
       
  4837             return cf.handleAsync(fn); }
       
  4838         public <U> CompletionStage<U> handleAsync
       
  4839             (BiFunction<? super T, Throwable, ? extends U> fn,
       
  4840              Executor executor) {
       
  4841             return cf.handleAsync(fn, executor); }
       
  4842         public CompletionStage<T> whenComplete
       
  4843             (BiConsumer<? super T, ? super Throwable> action) {
       
  4844             return cf.whenComplete(action); }
       
  4845         public CompletionStage<T> whenCompleteAsync
       
  4846             (BiConsumer<? super T, ? super Throwable> action) {
       
  4847             return cf.whenCompleteAsync(action); }
       
  4848         public CompletionStage<T> whenCompleteAsync
       
  4849             (BiConsumer<? super T, ? super Throwable> action,
       
  4850              Executor executor) {
       
  4851             return cf.whenCompleteAsync(action, executor); }
       
  4852         public CompletionStage<T> exceptionally
       
  4853             (Function<Throwable, ? extends T> fn) {
       
  4854             return cf.exceptionally(fn); }
       
  4855     }
       
  4856 
       
  4857     /**
       
  4858      * default-implemented exceptionallyAsync action is not invoked when
       
  4859      * source completes normally, and source result is propagated
       
  4860      */
       
  4861     public void testDefaultExceptionallyAsync_normalCompletion() {
       
  4862         for (boolean createIncomplete : new boolean[] { true, false })
       
  4863         for (Integer v1 : new Integer[] { 1, null })
       
  4864     {
       
  4865         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  4866         final DelegatedCompletionStage<Integer> d =
       
  4867             new DelegatedCompletionStage<Integer>(f);
       
  4868         if (!createIncomplete) assertTrue(f.complete(v1));
       
  4869         final CompletionStage<Integer> g = d.exceptionallyAsync
       
  4870             ((Throwable t) -> {
       
  4871                 threadFail("should not be called");
       
  4872                 return null;            // unreached
       
  4873             });
       
  4874         if (createIncomplete) assertTrue(f.complete(v1));
       
  4875 
       
  4876         checkCompletedNormally(g.toCompletableFuture(), v1);
       
  4877     }}
       
  4878 
       
  4879     /**
       
  4880      * default-implemented exceptionallyAsync action completes with
       
  4881      * function value on source exception
       
  4882      */
       
  4883     public void testDefaultExceptionallyAsync_exceptionalCompletion() {
       
  4884         for (boolean createIncomplete : new boolean[] { true, false })
       
  4885         for (Integer v1 : new Integer[] { 1, null })
       
  4886     {
       
  4887         final AtomicInteger a = new AtomicInteger(0);
       
  4888         final CFException ex = new CFException();
       
  4889         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  4890         final DelegatedCompletionStage<Integer> d =
       
  4891             new DelegatedCompletionStage<Integer>(f);
       
  4892         if (!createIncomplete) f.completeExceptionally(ex);
       
  4893         final CompletionStage<Integer> g = d.exceptionallyAsync
       
  4894             ((Throwable t) -> {
       
  4895                 threadAssertSame(t, ex);
       
  4896                 a.getAndIncrement();
       
  4897                 return v1;
       
  4898             });
       
  4899         if (createIncomplete) f.completeExceptionally(ex);
       
  4900 
       
  4901         checkCompletedNormally(g.toCompletableFuture(), v1);
       
  4902         assertEquals(1, a.get());
       
  4903     }}
       
  4904 
       
  4905     /**
       
  4906      * Under default implementation, if an "exceptionally action"
       
  4907      * throws an exception, it completes exceptionally with that
       
  4908      * exception
       
  4909      */
       
  4910     public void testDefaultExceptionallyAsync_exceptionalCompletionActionFailed() {
       
  4911         for (boolean createIncomplete : new boolean[] { true, false })
       
  4912     {
       
  4913         final AtomicInteger a = new AtomicInteger(0);
       
  4914         final CFException ex1 = new CFException();
       
  4915         final CFException ex2 = new CFException();
       
  4916         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  4917         final DelegatedCompletionStage<Integer> d =
       
  4918             new DelegatedCompletionStage<Integer>(f);
       
  4919         if (!createIncomplete) f.completeExceptionally(ex1);
       
  4920         final CompletionStage<Integer> g = d.exceptionallyAsync
       
  4921             ((Throwable t) -> {
       
  4922                 threadAssertSame(t, ex1);
       
  4923                 a.getAndIncrement();
       
  4924                 throw ex2;
       
  4925             });
       
  4926         if (createIncomplete) f.completeExceptionally(ex1);
       
  4927 
       
  4928         checkCompletedWithWrappedException(g.toCompletableFuture(), ex2);
       
  4929         checkCompletedExceptionally(f, ex1);
       
  4930         checkCompletedExceptionally(d.toCompletableFuture(), ex1);
       
  4931         assertEquals(1, a.get());
       
  4932     }}
       
  4933 
       
  4934     /**
       
  4935      * default-implemented exceptionallyCompose result completes
       
  4936      * normally after normal completion of source
       
  4937      */
       
  4938     public void testDefaultExceptionallyCompose_normalCompletion() {
       
  4939         for (boolean createIncomplete : new boolean[] { true, false })
       
  4940         for (Integer v1 : new Integer[] { 1, null })
       
  4941     {
       
  4942         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  4943         final ExceptionalCompletableFutureFunction r =
       
  4944             new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
       
  4945         final DelegatedCompletionStage<Integer> d =
       
  4946             new DelegatedCompletionStage<Integer>(f);
       
  4947         if (!createIncomplete) assertTrue(f.complete(v1));
       
  4948         final CompletionStage<Integer> g = d.exceptionallyCompose(r);
       
  4949         if (createIncomplete) assertTrue(f.complete(v1));
       
  4950 
       
  4951         checkCompletedNormally(f, v1);
       
  4952         checkCompletedNormally(g.toCompletableFuture(), v1);
       
  4953         r.assertNotInvoked();
       
  4954     }}
       
  4955 
       
  4956     /**
       
  4957      * default-implemented exceptionallyCompose result completes
       
  4958      * normally after exceptional completion of source
       
  4959      */
       
  4960     public void testDefaultExceptionallyCompose_exceptionalCompletion() {
       
  4961         for (boolean createIncomplete : new boolean[] { true, false })
       
  4962     {
       
  4963         final CFException ex = new CFException();
       
  4964         final ExceptionalCompletableFutureFunction r =
       
  4965             new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
       
  4966         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  4967         final DelegatedCompletionStage<Integer> d =
       
  4968             new DelegatedCompletionStage<Integer>(f);
       
  4969         if (!createIncomplete) f.completeExceptionally(ex);
       
  4970         final CompletionStage<Integer> g = d.exceptionallyCompose(r);
       
  4971         if (createIncomplete) f.completeExceptionally(ex);
       
  4972 
       
  4973         checkCompletedExceptionally(f, ex);
       
  4974         checkCompletedNormally(g.toCompletableFuture(), r.value);
       
  4975         r.assertInvoked();
       
  4976     }}
       
  4977 
       
  4978     /**
       
  4979      * default-implemented exceptionallyCompose completes
       
  4980      * exceptionally on exception if action does
       
  4981      */
       
  4982     public void testDefaultExceptionallyCompose_actionFailed() {
       
  4983         for (boolean createIncomplete : new boolean[] { true, false })
       
  4984     {
       
  4985         final CFException ex = new CFException();
       
  4986         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  4987         final FailingExceptionalCompletableFutureFunction r
       
  4988             = new FailingExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
       
  4989         final DelegatedCompletionStage<Integer> d =
       
  4990             new DelegatedCompletionStage<Integer>(f);
       
  4991         if (!createIncomplete) f.completeExceptionally(ex);
       
  4992         final CompletionStage<Integer> g = d.exceptionallyCompose(r);
       
  4993         if (createIncomplete) f.completeExceptionally(ex);
       
  4994 
       
  4995         checkCompletedExceptionally(f, ex);
       
  4996         checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
       
  4997         r.assertInvoked();
       
  4998     }}
       
  4999 
       
  5000     /**
       
  5001      * default-implemented exceptionallyComposeAsync result completes
       
  5002      * normally after normal completion of source
       
  5003      */
       
  5004     public void testDefaultExceptionallyComposeAsync_normalCompletion() {
       
  5005         for (boolean createIncomplete : new boolean[] { true, false })
       
  5006         for (Integer v1 : new Integer[] { 1, null })
       
  5007     {
       
  5008         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  5009         final ExceptionalCompletableFutureFunction r =
       
  5010             new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
       
  5011         final DelegatedCompletionStage<Integer> d =
       
  5012             new DelegatedCompletionStage<Integer>(f);
       
  5013         if (!createIncomplete) assertTrue(f.complete(v1));
       
  5014         final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
       
  5015         if (createIncomplete) assertTrue(f.complete(v1));
       
  5016 
       
  5017         checkCompletedNormally(f, v1);
       
  5018         checkCompletedNormally(g.toCompletableFuture(), v1);
       
  5019         r.assertNotInvoked();
       
  5020     }}
       
  5021 
       
  5022     /**
       
  5023      * default-implemented exceptionallyComposeAsync result completes
       
  5024      * normally after exceptional completion of source
       
  5025      */
       
  5026     public void testDefaultExceptionallyComposeAsync_exceptionalCompletion() {
       
  5027         for (boolean createIncomplete : new boolean[] { true, false })
       
  5028     {
       
  5029         final CFException ex = new CFException();
       
  5030         final ExceptionalCompletableFutureFunction r =
       
  5031             new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
       
  5032         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  5033         final DelegatedCompletionStage<Integer> d =
       
  5034             new DelegatedCompletionStage<Integer>(f);
       
  5035         if (!createIncomplete) f.completeExceptionally(ex);
       
  5036         final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
       
  5037         if (createIncomplete) f.completeExceptionally(ex);
       
  5038 
       
  5039         checkCompletedExceptionally(f, ex);
       
  5040         checkCompletedNormally(g.toCompletableFuture(), r.value);
       
  5041         r.assertInvoked();
       
  5042     }}
       
  5043 
       
  5044     /**
       
  5045      * default-implemented exceptionallyComposeAsync completes
       
  5046      * exceptionally on exception if action does
       
  5047      */
       
  5048     public void testDefaultExceptionallyComposeAsync_actionFailed() {
       
  5049         for (boolean createIncomplete : new boolean[] { true, false })
       
  5050     {
       
  5051         final CFException ex = new CFException();
       
  5052         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  5053         final FailingExceptionalCompletableFutureFunction r
       
  5054             = new FailingExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
       
  5055         final DelegatedCompletionStage<Integer> d =
       
  5056             new DelegatedCompletionStage<Integer>(f);
       
  5057         if (!createIncomplete) f.completeExceptionally(ex);
       
  5058         final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
       
  5059         if (createIncomplete) f.completeExceptionally(ex);
       
  5060 
       
  5061         checkCompletedExceptionally(f, ex);
       
  5062         checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
       
  5063         r.assertInvoked();
       
  5064     }}
       
  5065 
       
  5066     /**
       
  5067      * default-implemented exceptionallyComposeAsync result completes
       
  5068      * normally after normal completion of source
       
  5069      */
       
  5070     public void testDefaultExceptionallyComposeAsyncExecutor_normalCompletion() {
       
  5071         for (boolean createIncomplete : new boolean[] { true, false })
       
  5072         for (Integer v1 : new Integer[] { 1, null })
       
  5073     {
       
  5074         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  5075         final ExceptionalCompletableFutureFunction r =
       
  5076             new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
       
  5077         final DelegatedCompletionStage<Integer> d =
       
  5078             new DelegatedCompletionStage<Integer>(f);
       
  5079         if (!createIncomplete) assertTrue(f.complete(v1));
       
  5080         final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor());
       
  5081         if (createIncomplete) assertTrue(f.complete(v1));
       
  5082 
       
  5083         checkCompletedNormally(f, v1);
       
  5084         checkCompletedNormally(g.toCompletableFuture(), v1);
       
  5085         r.assertNotInvoked();
       
  5086     }}
       
  5087 
       
  5088     /**
       
  5089      * default-implemented exceptionallyComposeAsync result completes
       
  5090      * normally after exceptional completion of source
       
  5091      */
       
  5092     public void testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion() {
       
  5093         for (boolean createIncomplete : new boolean[] { true, false })
       
  5094     {
       
  5095         final CFException ex = new CFException();
       
  5096         final ExceptionalCompletableFutureFunction r =
       
  5097             new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
       
  5098         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  5099         final DelegatedCompletionStage<Integer> d =
       
  5100             new DelegatedCompletionStage<Integer>(f);
       
  5101         if (!createIncomplete) f.completeExceptionally(ex);
       
  5102         final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor());
       
  5103         if (createIncomplete) f.completeExceptionally(ex);
       
  5104 
       
  5105         checkCompletedExceptionally(f, ex);
       
  5106         checkCompletedNormally(g.toCompletableFuture(), r.value);
       
  5107         r.assertInvoked();
       
  5108     }}
       
  5109 
       
  5110     /**
       
  5111      * default-implemented exceptionallyComposeAsync completes
       
  5112      * exceptionally on exception if action does
       
  5113      */
       
  5114     public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() {
       
  5115         for (boolean createIncomplete : new boolean[] { true, false })
       
  5116     {
       
  5117         final CFException ex = new CFException();
       
  5118         final CompletableFuture<Integer> f = new CompletableFuture<>();
       
  5119         final FailingExceptionalCompletableFutureFunction r
       
  5120             = new FailingExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
       
  5121         final DelegatedCompletionStage<Integer> d =
       
  5122             new DelegatedCompletionStage<Integer>(f);
       
  5123         if (!createIncomplete) f.completeExceptionally(ex);
       
  5124         final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor());
       
  5125         if (createIncomplete) f.completeExceptionally(ex);
       
  5126 
       
  5127         checkCompletedExceptionally(f, ex);
       
  5128         checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
       
  5129         r.assertInvoked();
       
  5130     }}
       
  5131 
  4530 }
  5132 }