test/jdk/java/util/concurrent/CompletableFuture/Basic.java
changeset 48541 946e34c2dec9
parent 47306 90b7465b9ac7
child 51754 594919232b8f
equal deleted inserted replaced
48540:221cf8307606 48541:946e34c2dec9
   149 
   149 
   150         //----------------------------------------------------------------
   150         //----------------------------------------------------------------
   151         // runAsync tests
   151         // runAsync tests
   152         //----------------------------------------------------------------
   152         //----------------------------------------------------------------
   153         try {
   153         try {
   154             CompletableFuture<Void> cf = runAsync(() -> { });
   154             CompletableFuture<Void> cf = runAsync(() -> {});
   155             checkCompletedNormally(cf, cf.join());
   155             checkCompletedNormally(cf, cf.join());
   156             cf = runAsync(() -> { }, commonPool());
   156             cf = runAsync(() -> {}, commonPool());
   157             checkCompletedNormally(cf, cf.join());
   157             checkCompletedNormally(cf, cf.join());
   158             cf = runAsync(() -> { }, executor);
   158             cf = runAsync(() -> {}, executor);
   159             checkCompletedNormally(cf, cf.join());
   159             checkCompletedNormally(cf, cf.join());
   160             cf = runAsync(() -> { throw new RuntimeException(); });
   160             cf = runAsync(() -> { throw new RuntimeException(); });
   161             checkCompletedExceptionally(cf);
   161             checkCompletedExceptionally(cf);
   162             cf = runAsync(() -> { throw new RuntimeException(); }, commonPool());
   162             cf = runAsync(() -> { throw new RuntimeException(); }, commonPool());
   163             checkCompletedExceptionally(cf);
   163             checkCompletedExceptionally(cf);
   198         // thenApplyXXX tests
   198         // thenApplyXXX tests
   199         //----------------------------------------------------------------
   199         //----------------------------------------------------------------
   200         try {
   200         try {
   201             CompletableFuture<Integer> cf2;
   201             CompletableFuture<Integer> cf2;
   202             CompletableFuture<String> cf1 = supplyAsync(() -> "a test string");
   202             CompletableFuture<String> cf1 = supplyAsync(() -> "a test string");
   203             cf2 = cf1.thenApply((x) -> { if (x.equals("a test string")) return 1; else return 0; });
   203             cf2 = cf1.thenApply(x -> x.equals("a test string") ? 1 : 0);
   204             checkCompletedNormally(cf1, "a test string");
   204             checkCompletedNormally(cf1, "a test string");
   205             checkCompletedNormally(cf2, 1);
   205             checkCompletedNormally(cf2, 1);
   206 
   206 
   207             cf1 = supplyAsync(() -> "a test string");
   207             cf1 = supplyAsync(() -> "a test string");
   208             cf2 = cf1.thenApplyAsync((x) -> { if (x.equals("a test string")) return 1; else return 0; });
   208             cf2 = cf1.thenApplyAsync(x -> x.equals("a test string") ? 1 : 0);
   209             checkCompletedNormally(cf1, "a test string");
   209             checkCompletedNormally(cf1, "a test string");
   210             checkCompletedNormally(cf2, 1);
   210             checkCompletedNormally(cf2, 1);
   211 
   211 
   212             cf1 = supplyAsync(() -> "a test string");
   212             cf1 = supplyAsync(() -> "a test string");
   213             cf2 = cf1.thenApplyAsync((x) -> { if (x.equals("a test string")) return 1; else return 0; }, executor);
   213             cf2 = cf1.thenApplyAsync(x -> x.equals("a test string") ? 1 : 0, executor);
   214             checkCompletedNormally(cf1, "a test string");
   214             checkCompletedNormally(cf1, "a test string");
   215             checkCompletedNormally(cf2, 1);
   215             checkCompletedNormally(cf2, 1);
   216 
   216 
   217             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   217             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   218             cf2 = cf1.thenApply((x) -> { return 0; } );
   218             cf2 = cf1.thenApply(x -> 0);
   219             checkCompletedExceptionally(cf1);
   219             checkCompletedExceptionally(cf1);
   220             checkCompletedExceptionally(cf2);
   220             checkCompletedExceptionally(cf2);
   221 
   221 
   222             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   222             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   223             cf2 = cf1.thenApplyAsync((x) -> { return 0; } );
   223             cf2 = cf1.thenApplyAsync(x -> 0);
   224             checkCompletedExceptionally(cf1);
   224             checkCompletedExceptionally(cf1);
   225             checkCompletedExceptionally(cf2);
   225             checkCompletedExceptionally(cf2);
   226 
   226 
   227             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   227             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   228             cf2 = cf1.thenApplyAsync((x) -> { return 0; }, executor);
   228             cf2 = cf1.thenApplyAsync(x -> 0, executor);
   229             checkCompletedExceptionally(cf1);
   229             checkCompletedExceptionally(cf1);
   230             checkCompletedExceptionally(cf2);
   230             checkCompletedExceptionally(cf2);
   231         } catch (Throwable t) { unexpected(t); }
   231         } catch (Throwable t) { unexpected(t); }
   232 
   232 
   233         //----------------------------------------------------------------
   233         //----------------------------------------------------------------
   235         //----------------------------------------------------------------
   235         //----------------------------------------------------------------
   236         try {
   236         try {
   237             CompletableFuture<Void> cf2;
   237             CompletableFuture<Void> cf2;
   238             int before = atomicInt.get();
   238             int before = atomicInt.get();
   239             CompletableFuture<String> cf1 = supplyAsync(() -> "a test string");
   239             CompletableFuture<String> cf1 = supplyAsync(() -> "a test string");
   240             cf2 = cf1.thenAccept((x) -> { if (x.equals("a test string")) { atomicInt.incrementAndGet(); return; } throw new RuntimeException(); });
   240             cf2 = cf1.thenAccept(x -> { if (x.equals("a test string")) { atomicInt.incrementAndGet(); return; } throw new RuntimeException(); });
   241             checkCompletedNormally(cf1, "a test string");
   241             checkCompletedNormally(cf1, "a test string");
   242             checkCompletedNormally(cf2, null);
   242             checkCompletedNormally(cf2, null);
   243             check(atomicInt.get() == (before + 1));
   243             check(atomicInt.get() == (before + 1));
   244 
   244 
   245             before = atomicInt.get();
   245             before = atomicInt.get();
   246             cf1 = supplyAsync(() -> "a test string");
   246             cf1 = supplyAsync(() -> "a test string");
   247             cf2 = cf1.thenAcceptAsync((x) -> { if (x.equals("a test string")) { atomicInt.incrementAndGet(); return; } throw new RuntimeException(); });
   247             cf2 = cf1.thenAcceptAsync(x -> { if (x.equals("a test string")) { atomicInt.incrementAndGet(); return; } throw new RuntimeException(); });
   248             checkCompletedNormally(cf1, "a test string");
   248             checkCompletedNormally(cf1, "a test string");
   249             checkCompletedNormally(cf2, null);
   249             checkCompletedNormally(cf2, null);
   250             check(atomicInt.get() == (before + 1));
   250             check(atomicInt.get() == (before + 1));
   251 
   251 
   252             before = atomicInt.get();
   252             before = atomicInt.get();
   253             cf1 = supplyAsync(() -> "a test string");
   253             cf1 = supplyAsync(() -> "a test string");
   254             cf2 = cf1.thenAcceptAsync((x) -> { if (x.equals("a test string")) { atomicInt.incrementAndGet(); return; } throw new RuntimeException(); }, executor);
   254             cf2 = cf1.thenAcceptAsync(x -> { if (x.equals("a test string")) { atomicInt.incrementAndGet(); return; } throw new RuntimeException(); }, executor);
   255             checkCompletedNormally(cf1, "a test string");
   255             checkCompletedNormally(cf1, "a test string");
   256             checkCompletedNormally(cf2, null);
   256             checkCompletedNormally(cf2, null);
   257             check(atomicInt.get() == (before + 1));
   257             check(atomicInt.get() == (before + 1));
   258 
   258 
   259             before = atomicInt.get();
   259             before = atomicInt.get();
   260             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   260             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   261             cf2 = cf1.thenAccept((x) -> { atomicInt.incrementAndGet(); } );
   261             cf2 = cf1.thenAccept(x -> atomicInt.incrementAndGet());
   262             checkCompletedExceptionally(cf1);
   262             checkCompletedExceptionally(cf1);
   263             checkCompletedExceptionally(cf2);
   263             checkCompletedExceptionally(cf2);
   264             check(atomicInt.get() == before);
   264             check(atomicInt.get() == before);
   265 
   265 
   266             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   266             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   267             cf2 = cf1.thenAcceptAsync((x) -> { atomicInt.incrementAndGet(); } );
   267             cf2 = cf1.thenAcceptAsync(x -> atomicInt.incrementAndGet());
   268             checkCompletedExceptionally(cf1);
   268             checkCompletedExceptionally(cf1);
   269             checkCompletedExceptionally(cf2);
   269             checkCompletedExceptionally(cf2);
   270             check(atomicInt.get() == before);
   270             check(atomicInt.get() == before);
   271 
   271 
   272             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   272             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   273             cf2 = cf1.thenAcceptAsync((x) -> { atomicInt.incrementAndGet(); }, executor );
   273             cf2 = cf1.thenAcceptAsync(x -> atomicInt.incrementAndGet(), executor );
   274             checkCompletedExceptionally(cf1);
   274             checkCompletedExceptionally(cf1);
   275             checkCompletedExceptionally(cf2);
   275             checkCompletedExceptionally(cf2);
   276             check(atomicInt.get() == before);
   276             check(atomicInt.get() == before);
   277         } catch (Throwable t) { unexpected(t); }
   277         } catch (Throwable t) { unexpected(t); }
   278 
   278 
   281         //----------------------------------------------------------------
   281         //----------------------------------------------------------------
   282         try {
   282         try {
   283             CompletableFuture<Void> cf2;
   283             CompletableFuture<Void> cf2;
   284             int before = atomicInt.get();
   284             int before = atomicInt.get();
   285             CompletableFuture<String> cf1 = supplyAsync(() -> "a test string");
   285             CompletableFuture<String> cf1 = supplyAsync(() -> "a test string");
   286             cf2 = cf1.thenRun(() -> { atomicInt.incrementAndGet(); });
   286             cf2 = cf1.thenRun(() -> atomicInt.incrementAndGet());
   287             checkCompletedNormally(cf1, "a test string");
   287             checkCompletedNormally(cf1, "a test string");
   288             checkCompletedNormally(cf2, null);
   288             checkCompletedNormally(cf2, null);
   289             check(atomicInt.get() == (before + 1));
   289             check(atomicInt.get() == (before + 1));
   290 
   290 
   291             before = atomicInt.get();
   291             before = atomicInt.get();
   292             cf1 = supplyAsync(() -> "a test string");
   292             cf1 = supplyAsync(() -> "a test string");
   293             cf2 = cf1.thenRunAsync(() -> { atomicInt.incrementAndGet(); });
   293             cf2 = cf1.thenRunAsync(() -> atomicInt.incrementAndGet());
   294             checkCompletedNormally(cf1, "a test string");
   294             checkCompletedNormally(cf1, "a test string");
   295             checkCompletedNormally(cf2, null);
   295             checkCompletedNormally(cf2, null);
   296             check(atomicInt.get() == (before + 1));
   296             check(atomicInt.get() == (before + 1));
   297 
   297 
   298             before = atomicInt.get();
   298             before = atomicInt.get();
   299             cf1 = supplyAsync(() -> "a test string");
   299             cf1 = supplyAsync(() -> "a test string");
   300             cf2 = cf1.thenRunAsync(() -> { atomicInt.incrementAndGet(); }, executor);
   300             cf2 = cf1.thenRunAsync(() -> atomicInt.incrementAndGet(), executor);
   301             checkCompletedNormally(cf1, "a test string");
   301             checkCompletedNormally(cf1, "a test string");
   302             checkCompletedNormally(cf2, null);
   302             checkCompletedNormally(cf2, null);
   303             check(atomicInt.get() == (before + 1));
   303             check(atomicInt.get() == (before + 1));
   304 
   304 
   305             before = atomicInt.get();
   305             before = atomicInt.get();
   306             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   306             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   307             cf2 = cf1.thenRun(() -> { atomicInt.incrementAndGet(); });
   307             cf2 = cf1.thenRun(() -> atomicInt.incrementAndGet());
   308             checkCompletedExceptionally(cf1);
   308             checkCompletedExceptionally(cf1);
   309             checkCompletedExceptionally(cf2);
   309             checkCompletedExceptionally(cf2);
   310             check(atomicInt.get() == before);
   310             check(atomicInt.get() == before);
   311 
   311 
   312             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   312             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   313             cf2 = cf1.thenRunAsync(() -> { atomicInt.incrementAndGet(); });
   313             cf2 = cf1.thenRunAsync(() -> atomicInt.incrementAndGet());
   314             checkCompletedExceptionally(cf1);
   314             checkCompletedExceptionally(cf1);
   315             checkCompletedExceptionally(cf2);
   315             checkCompletedExceptionally(cf2);
   316             check(atomicInt.get() == before);
   316             check(atomicInt.get() == before);
   317 
   317 
   318             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   318             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   319             cf2 = cf1.thenRunAsync(() -> { atomicInt.incrementAndGet(); }, executor);
   319             cf2 = cf1.thenRunAsync(() -> atomicInt.incrementAndGet(), executor);
   320             checkCompletedExceptionally(cf1);
   320             checkCompletedExceptionally(cf1);
   321             checkCompletedExceptionally(cf2);
   321             checkCompletedExceptionally(cf2);
   322             check(atomicInt.get() == before);
   322             check(atomicInt.get() == before);
   323         } catch (Throwable t) { unexpected(t); }
   323         } catch (Throwable t) { unexpected(t); }
   324 
   324 
   327         //----------------------------------------------------------------
   327         //----------------------------------------------------------------
   328         try {
   328         try {
   329             CompletableFuture<Integer> cf3;
   329             CompletableFuture<Integer> cf3;
   330             CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
   330             CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
   331             CompletableFuture<Integer> cf2 = supplyAsync(() -> 1);
   331             CompletableFuture<Integer> cf2 = supplyAsync(() -> 1);
   332             cf3 = cf1.thenCombine(cf2, (x, y) -> { return x + y; });
   332             cf3 = cf1.thenCombine(cf2, (x, y) -> x + y);
   333             checkCompletedNormally(cf1, 1);
   333             checkCompletedNormally(cf1, 1);
   334             checkCompletedNormally(cf2, 1);
   334             checkCompletedNormally(cf2, 1);
   335             checkCompletedNormally(cf3, 2);
   335             checkCompletedNormally(cf3, 2);
   336 
   336 
   337             cf1 = supplyAsync(() -> 1);
   337             cf1 = supplyAsync(() -> 1);
   338             cf2 = supplyAsync(() -> 1);
   338             cf2 = supplyAsync(() -> 1);
   339             cf3 = cf1.thenCombineAsync(cf2, (x, y) -> { return x + y; });
   339             cf3 = cf1.thenCombineAsync(cf2, (x, y) -> x + y);
   340             checkCompletedNormally(cf1, 1);
   340             checkCompletedNormally(cf1, 1);
   341             checkCompletedNormally(cf2, 1);
   341             checkCompletedNormally(cf2, 1);
   342             checkCompletedNormally(cf3, 2);
   342             checkCompletedNormally(cf3, 2);
   343 
   343 
   344             cf1 = supplyAsync(() -> 1);
   344             cf1 = supplyAsync(() -> 1);
   345             cf2 = supplyAsync(() -> 1);
   345             cf2 = supplyAsync(() -> 1);
   346             cf3 = cf1.thenCombineAsync(cf2, (x, y) -> { return x + y; }, executor);
   346             cf3 = cf1.thenCombineAsync(cf2, (x, y) -> x + y, executor);
   347             checkCompletedNormally(cf1, 1);
   347             checkCompletedNormally(cf1, 1);
   348             checkCompletedNormally(cf2, 1);
   348             checkCompletedNormally(cf2, 1);
   349             checkCompletedNormally(cf3, 2);
   349             checkCompletedNormally(cf3, 2);
   350 
   350 
   351             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   351             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   352             cf2 = supplyAsync(() -> 1);
   352             cf2 = supplyAsync(() -> 1);
   353             cf3 = cf1.thenCombine(cf2, (x, y) -> { return 0; });
   353             cf3 = cf1.thenCombine(cf2, (x, y) -> 0);
   354             checkCompletedExceptionally(cf1);
   354             checkCompletedExceptionally(cf1);
   355             checkCompletedNormally(cf2, 1);
   355             checkCompletedNormally(cf2, 1);
   356             checkCompletedExceptionally(cf3);
   356             checkCompletedExceptionally(cf3);
   357 
   357 
   358             cf1 = supplyAsync(() -> 1);
   358             cf1 = supplyAsync(() -> 1);
   359             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   359             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   360             cf3 = cf1.thenCombineAsync(cf2, (x, y) -> { return 0; });
   360             cf3 = cf1.thenCombineAsync(cf2, (x, y) -> 0);
   361             checkCompletedNormally(cf1, 1);
   361             checkCompletedNormally(cf1, 1);
   362             checkCompletedExceptionally(cf2);
   362             checkCompletedExceptionally(cf2);
   363             checkCompletedExceptionally(cf3);
   363             checkCompletedExceptionally(cf3);
   364 
   364 
   365             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   365             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   366             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   366             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   367             cf3 = cf1.thenCombineAsync(cf2, (x, y) -> { return 0; }, executor);
   367             cf3 = cf1.thenCombineAsync(cf2, (x, y) -> 0, executor);
   368             checkCompletedExceptionally(cf1);
   368             checkCompletedExceptionally(cf1);
   369             checkCompletedExceptionally(cf2);
   369             checkCompletedExceptionally(cf2);
   370             checkCompletedExceptionally(cf3);
   370             checkCompletedExceptionally(cf3);
   371         } catch (Throwable t) { unexpected(t); }
   371         } catch (Throwable t) { unexpected(t); }
   372 
   372 
   403             check(atomicInt.get() == (before + 1));
   403             check(atomicInt.get() == (before + 1));
   404 
   404 
   405             before = atomicInt.get();
   405             before = atomicInt.get();
   406             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   406             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   407             cf2 = supplyAsync(() -> 1);
   407             cf2 = supplyAsync(() -> 1);
   408             cf3 = cf1.thenAcceptBoth(cf2, (x, y) -> { atomicInt.incrementAndGet(); });
   408             cf3 = cf1.thenAcceptBoth(cf2, (x, y) -> atomicInt.incrementAndGet());
   409             checkCompletedExceptionally(cf1);
   409             checkCompletedExceptionally(cf1);
   410             checkCompletedNormally(cf2, 1);
   410             checkCompletedNormally(cf2, 1);
   411             checkCompletedExceptionally(cf3);
   411             checkCompletedExceptionally(cf3);
   412             check(atomicInt.get() == before);
   412             check(atomicInt.get() == before);
   413 
   413 
   414             cf1 = supplyAsync(() -> 1);
   414             cf1 = supplyAsync(() -> 1);
   415             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   415             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   416             cf3 = cf1.thenAcceptBothAsync(cf2, (x, y) -> { atomicInt.incrementAndGet(); });
   416             cf3 = cf1.thenAcceptBothAsync(cf2, (x, y) -> atomicInt.incrementAndGet());
   417             checkCompletedNormally(cf1, 1);
   417             checkCompletedNormally(cf1, 1);
   418             checkCompletedExceptionally(cf2);
   418             checkCompletedExceptionally(cf2);
   419             checkCompletedExceptionally(cf3);
   419             checkCompletedExceptionally(cf3);
   420             check(atomicInt.get() == before);
   420             check(atomicInt.get() == before);
   421 
   421 
   422             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   422             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   423             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   423             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   424             cf3 = cf1.thenAcceptBothAsync(cf2, (x, y) -> { atomicInt.incrementAndGet(); }, executor);
   424             cf3 = cf1.thenAcceptBothAsync(cf2, (x, y) -> atomicInt.incrementAndGet(), executor);
   425             checkCompletedExceptionally(cf1);
   425             checkCompletedExceptionally(cf1);
   426             checkCompletedExceptionally(cf2);
   426             checkCompletedExceptionally(cf2);
   427             checkCompletedExceptionally(cf3);
   427             checkCompletedExceptionally(cf3);
   428             check(atomicInt.get() == before);
   428             check(atomicInt.get() == before);
   429         } catch (Throwable t) { unexpected(t); }
   429         } catch (Throwable t) { unexpected(t); }
   461             check(atomicInt.get() == (before + 1));
   461             check(atomicInt.get() == (before + 1));
   462 
   462 
   463             before = atomicInt.get();
   463             before = atomicInt.get();
   464             CompletableFuture<Integer> cf4 = supplyAsync(() -> { throw new RuntimeException(); });
   464             CompletableFuture<Integer> cf4 = supplyAsync(() -> { throw new RuntimeException(); });
   465             CompletableFuture<Integer> cf5 = supplyAsync(() -> 1);
   465             CompletableFuture<Integer> cf5 = supplyAsync(() -> 1);
   466             cf3 = cf5.runAfterBothAsync(cf4, () -> { atomicInt.incrementAndGet(); }, executor);
   466             cf3 = cf5.runAfterBothAsync(cf4, () -> atomicInt.incrementAndGet(), executor);
   467             checkCompletedExceptionally(cf4);
   467             checkCompletedExceptionally(cf4);
   468             checkCompletedNormally(cf5, 1);
   468             checkCompletedNormally(cf5, 1);
   469             checkCompletedExceptionally(cf3);
   469             checkCompletedExceptionally(cf3);
   470             check(atomicInt.get() == before);
   470             check(atomicInt.get() == before);
   471 
   471 
   472             before = atomicInt.get();
   472             before = atomicInt.get();
   473             cf4 = supplyAsync(() -> 1);
   473             cf4 = supplyAsync(() -> 1);
   474             cf5 = supplyAsync(() -> { throw new RuntimeException(); });
   474             cf5 = supplyAsync(() -> { throw new RuntimeException(); });
   475             cf3 = cf5.runAfterBothAsync(cf4, () -> { atomicInt.incrementAndGet(); });
   475             cf3 = cf5.runAfterBothAsync(cf4, () -> atomicInt.incrementAndGet());
   476             checkCompletedNormally(cf4, 1);
   476             checkCompletedNormally(cf4, 1);
   477             checkCompletedExceptionally(cf5);
   477             checkCompletedExceptionally(cf5);
   478             checkCompletedExceptionally(cf3);
   478             checkCompletedExceptionally(cf3);
   479             check(atomicInt.get() == before);
   479             check(atomicInt.get() == before);
   480 
   480 
   481             before = atomicInt.get();
   481             before = atomicInt.get();
   482             cf4 = supplyAsync(() -> { throw new RuntimeException(); });
   482             cf4 = supplyAsync(() -> { throw new RuntimeException(); });
   483             cf5 = supplyAsync(() -> { throw new RuntimeException(); });
   483             cf5 = supplyAsync(() -> { throw new RuntimeException(); });
   484             cf3 = cf5.runAfterBoth(cf4, () -> { atomicInt.incrementAndGet(); });
   484             cf3 = cf5.runAfterBoth(cf4, () -> atomicInt.incrementAndGet());
   485             checkCompletedExceptionally(cf4);
   485             checkCompletedExceptionally(cf4);
   486             checkCompletedExceptionally(cf5);
   486             checkCompletedExceptionally(cf5);
   487             checkCompletedExceptionally(cf3);
   487             checkCompletedExceptionally(cf3);
   488             check(atomicInt.get() == before);
   488             check(atomicInt.get() == before);
   489         } catch (Throwable t) { unexpected(t); }
   489         } catch (Throwable t) { unexpected(t); }
   493         //----------------------------------------------------------------
   493         //----------------------------------------------------------------
   494         try {
   494         try {
   495             CompletableFuture<Integer> cf3;
   495             CompletableFuture<Integer> cf3;
   496             CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
   496             CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
   497             CompletableFuture<Integer> cf2 = supplyAsync(() -> 2);
   497             CompletableFuture<Integer> cf2 = supplyAsync(() -> 2);
   498             cf3 = cf1.applyToEither(cf2, (x) -> { check(x == 1 || x == 2); return x; });
   498             cf3 = cf1.applyToEither(cf2, x -> { check(x == 1 || x == 2); return x; });
   499             checkCompletedNormally(cf3, new Object[] {1, 2});
   499             checkCompletedNormally(cf3, new Object[] {1, 2});
   500             check(cf1.isDone() || cf2.isDone());
   500             check(cf1.isDone() || cf2.isDone());
   501 
   501 
   502             cf1 = supplyAsync(() -> 1);
   502             cf1 = supplyAsync(() -> 1);
   503             cf2 = supplyAsync(() -> 2);
   503             cf2 = supplyAsync(() -> 2);
   504             cf3 = cf1.applyToEitherAsync(cf2, (x) -> { check(x == 1 || x == 2); return x; });
   504             cf3 = cf1.applyToEitherAsync(cf2, x -> { check(x == 1 || x == 2); return x; });
   505             checkCompletedNormally(cf3, new Object[] {1, 2});
   505             checkCompletedNormally(cf3, new Object[] {1, 2});
   506             check(cf1.isDone() || cf2.isDone());
   506             check(cf1.isDone() || cf2.isDone());
   507 
   507 
   508             cf1 = supplyAsync(() -> 1);
   508             cf1 = supplyAsync(() -> 1);
   509             cf2 = supplyAsync(() -> 2);
   509             cf2 = supplyAsync(() -> 2);
   510             cf3 = cf1.applyToEitherAsync(cf2, (x) -> { check(x == 1 || x == 2); return x; }, executor);
   510             cf3 = cf1.applyToEitherAsync(cf2, x -> { check(x == 1 || x == 2); return x; }, executor);
   511             checkCompletedNormally(cf3, new Object[] {1, 2});
   511             checkCompletedNormally(cf3, new Object[] {1, 2});
   512             check(cf1.isDone() || cf2.isDone());
   512             check(cf1.isDone() || cf2.isDone());
   513 
   513 
   514             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   514             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   515             cf2 = supplyAsync(() -> 2);
   515             cf2 = supplyAsync(() -> 2);
   516             cf3 = cf1.applyToEither(cf2, (x) -> { check(x == 2); return x; });
   516             cf3 = cf1.applyToEither(cf2, x -> { check(x == 2); return x; });
   517             try { check(cf3.join() == 2); } catch (CompletionException x) { pass(); }
   517             try { check(cf3.join() == 2); } catch (CompletionException x) { pass(); }
   518             check(cf3.isDone());
   518             check(cf3.isDone());
   519             check(cf1.isDone() || cf2.isDone());
   519             check(cf1.isDone() || cf2.isDone());
   520 
   520 
   521             cf1 = supplyAsync(() -> 1);
   521             cf1 = supplyAsync(() -> 1);
   522             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   522             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   523             cf3 = cf1.applyToEitherAsync(cf2, (x) -> { check(x == 1); return x; });
   523             cf3 = cf1.applyToEitherAsync(cf2, x -> { check(x == 1); return x; });
   524             try { check(cf3.join() == 1); } catch (CompletionException x) { pass(); }
   524             try { check(cf3.join() == 1); } catch (CompletionException x) { pass(); }
   525             check(cf3.isDone());
   525             check(cf3.isDone());
   526             check(cf1.isDone() || cf2.isDone());
   526             check(cf1.isDone() || cf2.isDone());
   527 
   527 
   528             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   528             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   529             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   529             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   530             cf3 = cf1.applyToEitherAsync(cf2, (x) -> { fail(); return x; });
   530             cf3 = cf1.applyToEitherAsync(cf2, x -> { fail(); return x; });
   531             checkCompletedExceptionally(cf3);
   531             checkCompletedExceptionally(cf3);
   532             check(cf1.isDone() || cf2.isDone());
   532             check(cf1.isDone() || cf2.isDone());
   533 
   533 
   534             final Phaser cf3Done = new Phaser(2);
   534             final Phaser cf3Done = new Phaser(2);
   535             cf1 = supplyAsync(() -> { cf3Done.arriveAndAwaitAdvance(); return 1; });
   535             cf1 = supplyAsync(() -> { cf3Done.arriveAndAwaitAdvance(); return 1; });
   536             cf2 = supplyAsync(() -> 2);
   536             cf2 = supplyAsync(() -> 2);
   537             cf3 = cf1.applyToEither(cf2, (x) -> { check(x == 2); return x; });
   537             cf3 = cf1.applyToEither(cf2, x -> { check(x == 2); return x; });
   538             checkCompletedNormally(cf3, 2);
   538             checkCompletedNormally(cf3, 2);
   539             checkCompletedNormally(cf2, 2);
   539             checkCompletedNormally(cf2, 2);
   540             check(!cf1.isDone());
   540             check(!cf1.isDone());
   541             cf3Done.arrive();
   541             cf3Done.arrive();
   542             checkCompletedNormally(cf1, 1);
   542             checkCompletedNormally(cf1, 1);
   543             checkCompletedNormally(cf3, 2);
   543             checkCompletedNormally(cf3, 2);
   544 
   544 
   545             cf1 = supplyAsync(() -> 1);
   545             cf1 = supplyAsync(() -> 1);
   546             cf2 = supplyAsync(() -> { cf3Done.arriveAndAwaitAdvance(); return 2; });
   546             cf2 = supplyAsync(() -> { cf3Done.arriveAndAwaitAdvance(); return 2; });
   547             cf3 = cf1.applyToEitherAsync(cf2, (x) -> { check(x == 1); return x; });
   547             cf3 = cf1.applyToEitherAsync(cf2, x -> { check(x == 1); return x; });
   548             checkCompletedNormally(cf3, 1);
   548             checkCompletedNormally(cf3, 1);
   549             checkCompletedNormally(cf1, 1);
   549             checkCompletedNormally(cf1, 1);
   550             check(!cf2.isDone());
   550             check(!cf2.isDone());
   551             cf3Done.arrive();
   551             cf3Done.arrive();
   552             checkCompletedNormally(cf2, 2);
   552             checkCompletedNormally(cf2, 2);
   559         try {
   559         try {
   560             CompletableFuture<Void> cf3;
   560             CompletableFuture<Void> cf3;
   561             int before = atomicInt.get();
   561             int before = atomicInt.get();
   562             CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
   562             CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
   563             CompletableFuture<Integer> cf2 = supplyAsync(() -> 2);
   563             CompletableFuture<Integer> cf2 = supplyAsync(() -> 2);
   564             cf3 = cf1.acceptEither(cf2, (x) -> { check(x == 1 || x == 2); atomicInt.incrementAndGet(); });
   564             cf3 = cf1.acceptEither(cf2, x -> { check(x == 1 || x == 2); atomicInt.incrementAndGet(); });
   565             checkCompletedNormally(cf3, null);
   565             checkCompletedNormally(cf3, null);
   566             check(cf1.isDone() || cf2.isDone());
   566             check(cf1.isDone() || cf2.isDone());
   567             check(atomicInt.get() == (before + 1));
   567             check(atomicInt.get() == (before + 1));
   568 
   568 
   569             before = atomicInt.get();
   569             before = atomicInt.get();
   570             cf1 = supplyAsync(() -> 1);
   570             cf1 = supplyAsync(() -> 1);
   571             cf2 = supplyAsync(() -> 2);
   571             cf2 = supplyAsync(() -> 2);
   572             cf3 = cf1.acceptEitherAsync(cf2, (x) -> { check(x == 1 || x == 2); atomicInt.incrementAndGet(); });
   572             cf3 = cf1.acceptEitherAsync(cf2, x -> { check(x == 1 || x == 2); atomicInt.incrementAndGet(); });
   573             checkCompletedNormally(cf3, null);
   573             checkCompletedNormally(cf3, null);
   574             check(cf1.isDone() || cf2.isDone());
   574             check(cf1.isDone() || cf2.isDone());
   575             check(atomicInt.get() == (before + 1));
   575             check(atomicInt.get() == (before + 1));
   576 
   576 
   577             before = atomicInt.get();
   577             before = atomicInt.get();
   578             cf1 = supplyAsync(() -> 1);
   578             cf1 = supplyAsync(() -> 1);
   579             cf2 = supplyAsync(() -> 2);
   579             cf2 = supplyAsync(() -> 2);
   580             cf3 = cf2.acceptEitherAsync(cf1, (x) -> { check(x == 1 || x == 2); atomicInt.incrementAndGet(); }, executor);
   580             cf3 = cf2.acceptEitherAsync(cf1, x -> { check(x == 1 || x == 2); atomicInt.incrementAndGet(); }, executor);
   581             checkCompletedNormally(cf3, null);
   581             checkCompletedNormally(cf3, null);
   582             check(cf1.isDone() || cf2.isDone());
   582             check(cf1.isDone() || cf2.isDone());
   583             check(atomicInt.get() == (before + 1));
   583             check(atomicInt.get() == (before + 1));
   584 
   584 
   585             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   585             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   586             cf2 = supplyAsync(() -> 2);
   586             cf2 = supplyAsync(() -> 2);
   587             cf3 = cf2.acceptEitherAsync(cf1, (x) -> { check(x == 2); }, executor);
   587             cf3 = cf2.acceptEitherAsync(cf1, x -> { check(x == 2); }, executor);
   588             try { check(cf3.join() == null); } catch (CompletionException x) { pass(); }
   588             try { check(cf3.join() == null); } catch (CompletionException x) { pass(); }
   589             check(cf3.isDone());
   589             check(cf3.isDone());
   590             check(cf1.isDone() || cf2.isDone());
   590             check(cf1.isDone() || cf2.isDone());
   591 
   591 
   592             cf1 = supplyAsync(() -> 1);
   592             cf1 = supplyAsync(() -> 1);
   593             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   593             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   594             cf3 = cf2.acceptEitherAsync(cf1, (x) -> { check(x == 1); });
   594             cf3 = cf2.acceptEitherAsync(cf1, x -> { check(x == 1); });
   595             try { check(cf3.join() == null); } catch (CompletionException x) { pass(); }
   595             try { check(cf3.join() == null); } catch (CompletionException x) { pass(); }
   596             check(cf3.isDone());
   596             check(cf3.isDone());
   597             check(cf1.isDone() || cf2.isDone());
   597             check(cf1.isDone() || cf2.isDone());
   598 
   598 
   599             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   599             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   600             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   600             cf2 = supplyAsync(() -> { throw new RuntimeException(); });
   601             cf3 = cf2.acceptEitherAsync(cf1, (x) -> { fail(); });
   601             cf3 = cf2.acceptEitherAsync(cf1, x -> { fail(); });
   602             checkCompletedExceptionally(cf3);
   602             checkCompletedExceptionally(cf3);
   603             check(cf1.isDone() || cf2.isDone());
   603             check(cf1.isDone() || cf2.isDone());
   604 
   604 
   605             final Phaser cf3Done = new Phaser(2);
   605             final Phaser cf3Done = new Phaser(2);
   606             cf1 = supplyAsync(() -> { cf3Done.arriveAndAwaitAdvance(); return 1; });
   606             cf1 = supplyAsync(() -> { cf3Done.arriveAndAwaitAdvance(); return 1; });
   607             cf2 = supplyAsync(() -> 2);
   607             cf2 = supplyAsync(() -> 2);
   608             cf3 = cf1.acceptEither(cf2, (x) -> { check(x == 2); });
   608             cf3 = cf1.acceptEither(cf2, x -> { check(x == 2); });
   609             checkCompletedNormally(cf3, null);
   609             checkCompletedNormally(cf3, null);
   610             checkCompletedNormally(cf2, 2);
   610             checkCompletedNormally(cf2, 2);
   611             check(!cf1.isDone());
   611             check(!cf1.isDone());
   612             cf3Done.arrive();
   612             cf3Done.arrive();
   613             checkCompletedNormally(cf1, 1);
   613             checkCompletedNormally(cf1, 1);
   614             checkCompletedNormally(cf3, null);
   614             checkCompletedNormally(cf3, null);
   615 
   615 
   616             cf1 = supplyAsync(() -> 1);
   616             cf1 = supplyAsync(() -> 1);
   617             cf2 = supplyAsync(() -> { cf3Done.arriveAndAwaitAdvance(); return 2; });
   617             cf2 = supplyAsync(() -> { cf3Done.arriveAndAwaitAdvance(); return 2; });
   618             cf3 = cf1.acceptEitherAsync(cf2, (x) -> { check(x == 1); });
   618             cf3 = cf1.acceptEitherAsync(cf2, x -> { check(x == 1); });
   619             checkCompletedNormally(cf3, null);
   619             checkCompletedNormally(cf3, null);
   620             checkCompletedNormally(cf1, 1);
   620             checkCompletedNormally(cf1, 1);
   621             check(!cf2.isDone());
   621             check(!cf2.isDone());
   622             cf3Done.arrive();
   622             cf3Done.arrive();
   623             checkCompletedNormally(cf2, 2);
   623             checkCompletedNormally(cf2, 2);
   628         // runAfterEitherXXX tests
   628         // runAfterEitherXXX tests
   629         //----------------------------------------------------------------
   629         //----------------------------------------------------------------
   630         try {
   630         try {
   631             CompletableFuture<Void> cf3;
   631             CompletableFuture<Void> cf3;
   632             int before = atomicInt.get();
   632             int before = atomicInt.get();
   633             CompletableFuture<Void> cf1 = runAsync(() -> { });
   633             CompletableFuture<Void> cf1 = runAsync(() -> {});
   634             CompletableFuture<Void> cf2 = runAsync(() -> { });
   634             CompletableFuture<Void> cf2 = runAsync(() -> {});
   635             cf3 = cf1.runAfterEither(cf2, () -> { atomicInt.incrementAndGet(); });
   635             cf3 = cf1.runAfterEither(cf2, () -> atomicInt.incrementAndGet());
   636             checkCompletedNormally(cf3, null);
   636             checkCompletedNormally(cf3, null);
   637             check(cf1.isDone() || cf2.isDone());
   637             check(cf1.isDone() || cf2.isDone());
   638             check(atomicInt.get() == (before + 1));
   638             check(atomicInt.get() == (before + 1));
   639 
   639 
   640             before = atomicInt.get();
   640             before = atomicInt.get();
   641             cf1 = runAsync(() -> { });
   641             cf1 = runAsync(() -> {});
   642             cf2 = runAsync(() -> { });
   642             cf2 = runAsync(() -> {});
   643             cf3 = cf1.runAfterEitherAsync(cf2, () -> { atomicInt.incrementAndGet(); });
   643             cf3 = cf1.runAfterEitherAsync(cf2, () -> atomicInt.incrementAndGet());
   644             checkCompletedNormally(cf3, null);
   644             checkCompletedNormally(cf3, null);
   645             check(cf1.isDone() || cf2.isDone());
   645             check(cf1.isDone() || cf2.isDone());
   646             check(atomicInt.get() == (before + 1));
   646             check(atomicInt.get() == (before + 1));
   647 
   647 
   648             before = atomicInt.get();
   648             before = atomicInt.get();
   649             cf1 = runAsync(() -> { });
   649             cf1 = runAsync(() -> {});
   650             cf2 = runAsync(() -> { });
   650             cf2 = runAsync(() -> {});
   651             cf3 = cf2.runAfterEitherAsync(cf1, () -> { atomicInt.incrementAndGet(); }, executor);
   651             cf3 = cf2.runAfterEitherAsync(cf1, () -> atomicInt.incrementAndGet(), executor);
   652             checkCompletedNormally(cf3, null);
   652             checkCompletedNormally(cf3, null);
   653             check(cf1.isDone() || cf2.isDone());
   653             check(cf1.isDone() || cf2.isDone());
   654             check(atomicInt.get() == (before + 1));
   654             check(atomicInt.get() == (before + 1));
   655 
   655 
   656             before = atomicInt.get();
   656             before = atomicInt.get();
   657             cf1 = runAsync(() -> { throw new RuntimeException(); });
   657             cf1 = runAsync(() -> { throw new RuntimeException(); });
   658             cf2 = runAsync(() -> { });
   658             cf2 = runAsync(() -> {});
   659             cf3 = cf2.runAfterEither(cf1, () -> { atomicInt.incrementAndGet(); });
   659             cf3 = cf2.runAfterEither(cf1, () -> atomicInt.incrementAndGet());
   660             try {
   660             try {
   661                 check(cf3.join() == null);
   661                 check(cf3.join() == null);
   662                 check(atomicInt.get() == (before + 1));
   662                 check(atomicInt.get() == (before + 1));
   663             } catch (CompletionException x) { pass(); }
   663             } catch (CompletionException x) { pass(); }
   664             check(cf3.isDone());
   664             check(cf3.isDone());
   665             check(cf1.isDone() || cf2.isDone());
   665             check(cf1.isDone() || cf2.isDone());
   666 
   666 
   667             before = atomicInt.get();
   667             before = atomicInt.get();
   668             cf1 = runAsync(() -> { });
   668             cf1 = runAsync(() -> {});
   669             cf2 = runAsync(() -> { throw new RuntimeException(); });
   669             cf2 = runAsync(() -> { throw new RuntimeException(); });
   670             cf3 = cf1.runAfterEitherAsync(cf2, () -> { atomicInt.incrementAndGet(); });
   670             cf3 = cf1.runAfterEitherAsync(cf2, () -> atomicInt.incrementAndGet());
   671             try {
   671             try {
   672                 check(cf3.join() == null);
   672                 check(cf3.join() == null);
   673                 check(atomicInt.get() == (before + 1));
   673                 check(atomicInt.get() == (before + 1));
   674             } catch (CompletionException x) { pass(); }
   674             } catch (CompletionException x) { pass(); }
   675             check(cf3.isDone());
   675             check(cf3.isDone());
   676             check(cf1.isDone() || cf2.isDone());
   676             check(cf1.isDone() || cf2.isDone());
   677 
   677 
   678             before = atomicInt.get();
   678             before = atomicInt.get();
   679             cf1 = runAsync(() -> { throw new RuntimeException(); });
   679             cf1 = runAsync(() -> { throw new RuntimeException(); });
   680             cf2 = runAsync(() -> { throw new RuntimeException(); });
   680             cf2 = runAsync(() -> { throw new RuntimeException(); });
   681             cf3 = cf2.runAfterEitherAsync(cf1, () -> { atomicInt.incrementAndGet(); }, executor);
   681             cf3 = cf2.runAfterEitherAsync(cf1, () -> atomicInt.incrementAndGet(), executor);
   682             checkCompletedExceptionally(cf3);
   682             checkCompletedExceptionally(cf3);
   683             check(cf1.isDone() || cf2.isDone());
   683             check(cf1.isDone() || cf2.isDone());
   684             check(atomicInt.get() == before);
   684             check(atomicInt.get() == before);
   685 
   685 
   686             final Phaser cf3Done = new Phaser(2);
   686             final Phaser cf3Done = new Phaser(2);
   687             before = atomicInt.get();
   687             before = atomicInt.get();
   688             cf1 = runAsync(() -> { cf3Done.arriveAndAwaitAdvance(); });
   688             cf1 = runAsync(() -> cf3Done.arriveAndAwaitAdvance());
   689             cf2 = runAsync(() -> { });
   689             cf2 = runAsync(() -> {});
   690             cf3 = cf1.runAfterEither(cf2, () -> { atomicInt.incrementAndGet(); });
   690             cf3 = cf1.runAfterEither(cf2, () -> atomicInt.incrementAndGet());
   691             checkCompletedNormally(cf3, null);
   691             checkCompletedNormally(cf3, null);
   692             checkCompletedNormally(cf2, null);
   692             checkCompletedNormally(cf2, null);
   693             check(!cf1.isDone());
   693             check(!cf1.isDone());
   694             check(atomicInt.get() == (before + 1));
   694             check(atomicInt.get() == (before + 1));
   695             cf3Done.arrive();
   695             cf3Done.arrive();
   696             checkCompletedNormally(cf1, null);
   696             checkCompletedNormally(cf1, null);
   697             checkCompletedNormally(cf3, null);
   697             checkCompletedNormally(cf3, null);
   698 
   698 
   699             before = atomicInt.get();
   699             before = atomicInt.get();
   700             cf1 = runAsync(() -> { });
   700             cf1 = runAsync(() -> {});
   701             cf2 = runAsync(() -> { cf3Done.arriveAndAwaitAdvance(); });
   701             cf2 = runAsync(() -> cf3Done.arriveAndAwaitAdvance());
   702             cf3 = cf1.runAfterEitherAsync(cf2, () -> { atomicInt.incrementAndGet(); });
   702             cf3 = cf1.runAfterEitherAsync(cf2, () -> atomicInt.incrementAndGet());
   703             checkCompletedNormally(cf3, null);
   703             checkCompletedNormally(cf3, null);
   704             checkCompletedNormally(cf1, null);
   704             checkCompletedNormally(cf1, null);
   705             check(!cf2.isDone());
   705             check(!cf2.isDone());
   706             check(atomicInt.get() == (before + 1));
   706             check(atomicInt.get() == (before + 1));
   707             cf3Done.arrive();
   707             cf3Done.arrive();
   713         // thenComposeXXX tests
   713         // thenComposeXXX tests
   714         //----------------------------------------------------------------
   714         //----------------------------------------------------------------
   715         try {
   715         try {
   716             CompletableFuture<Integer> cf2;
   716             CompletableFuture<Integer> cf2;
   717             CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
   717             CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
   718             cf2 = cf1.thenCompose((x) -> { check(x ==1); return CompletableFuture.completedFuture(2); });
   718             cf2 = cf1.thenCompose(x -> { check(x == 1); return CompletableFuture.completedFuture(2); });
   719             checkCompletedNormally(cf1, 1);
   719             checkCompletedNormally(cf1, 1);
   720             checkCompletedNormally(cf2, 2);
   720             checkCompletedNormally(cf2, 2);
   721 
   721 
   722             cf1 = supplyAsync(() -> 1);
   722             cf1 = supplyAsync(() -> 1);
   723             cf2 = cf1.thenComposeAsync((x) -> { check(x ==1); return CompletableFuture.completedFuture(2); });
   723             cf2 = cf1.thenComposeAsync(x -> { check(x == 1); return CompletableFuture.completedFuture(2); });
   724             checkCompletedNormally(cf1, 1);
   724             checkCompletedNormally(cf1, 1);
   725             checkCompletedNormally(cf2, 2);
   725             checkCompletedNormally(cf2, 2);
   726 
   726 
   727             cf1 = supplyAsync(() -> 1);
   727             cf1 = supplyAsync(() -> 1);
   728             cf2 = cf1.thenComposeAsync((x) -> { check(x ==1); return CompletableFuture.completedFuture(2); }, executor);
   728             cf2 = cf1.thenComposeAsync(x -> { check(x == 1); return CompletableFuture.completedFuture(2); }, executor);
   729             checkCompletedNormally(cf1, 1);
   729             checkCompletedNormally(cf1, 1);
   730             checkCompletedNormally(cf2, 2);
   730             checkCompletedNormally(cf2, 2);
   731 
   731 
   732             int before = atomicInt.get();
   732             int before = atomicInt.get();
   733             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   733             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   734             cf2 = cf1.thenCompose((x) -> { atomicInt.incrementAndGet(); return CompletableFuture.completedFuture(2); });
   734             cf2 = cf1.thenCompose(x -> { atomicInt.incrementAndGet(); return CompletableFuture.completedFuture(2); });
   735             checkCompletedExceptionally(cf1);
   735             checkCompletedExceptionally(cf1);
   736             checkCompletedExceptionally(cf2);
   736             checkCompletedExceptionally(cf2);
   737             check(atomicInt.get() == before);
   737             check(atomicInt.get() == before);
   738 
   738 
   739             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   739             cf1 = supplyAsync(() -> { throw new RuntimeException(); });
   740             cf2 = cf1.thenComposeAsync((x) -> { atomicInt.incrementAndGet(); return CompletableFuture.completedFuture(2); });
   740             cf2 = cf1.thenComposeAsync(x -> { atomicInt.incrementAndGet(); return CompletableFuture.completedFuture(2); });
   741             checkCompletedExceptionally(cf1);
   741             checkCompletedExceptionally(cf1);
   742             checkCompletedExceptionally(cf2);
   742             checkCompletedExceptionally(cf2);
   743             check(atomicInt.get() == before);
   743             check(atomicInt.get() == before);
   744 
   744 
   745             cf1 = supplyAsync(() -> 1);
   745             cf1 = supplyAsync(() -> 1);
   746             cf2 = cf1.thenComposeAsync((x) -> { throw new RuntimeException(); }, executor);
   746             cf2 = cf1.thenComposeAsync(x -> { throw new RuntimeException(); }, executor);
   747             checkCompletedNormally(cf1, 1);
   747             checkCompletedNormally(cf1, 1);
   748             checkCompletedExceptionally(cf2);
   748             checkCompletedExceptionally(cf2);
   749         } catch (Throwable t) { unexpected(t); }
   749         } catch (Throwable t) { unexpected(t); }
   750 
   750 
   751         //----------------------------------------------------------------
   751         //----------------------------------------------------------------
   785         // exceptionally tests
   785         // exceptionally tests
   786         //----------------------------------------------------------------
   786         //----------------------------------------------------------------
   787         try {
   787         try {
   788             CompletableFuture<Integer> cf2;
   788             CompletableFuture<Integer> cf2;
   789             CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
   789             CompletableFuture<Integer> cf1 = supplyAsync(() -> 1);
   790             cf2 = cf1.exceptionally((t) -> { fail("function should never be called"); return 2;});
   790             cf2 = cf1.exceptionally(t -> { fail("function should never be called"); return 2;});
   791             checkCompletedNormally(cf1, 1);
   791             checkCompletedNormally(cf1, 1);
   792             checkCompletedNormally(cf2, 1);
   792             checkCompletedNormally(cf2, 1);
   793 
   793 
   794             final RuntimeException t = new RuntimeException();
   794             final RuntimeException t = new RuntimeException();
   795             cf1 = supplyAsync(() -> { throw t; });
   795             cf1 = supplyAsync(() -> { throw t; });
   796             cf2 = cf1.exceptionally((x) -> { check(x.getCause() == t); return 2;});
   796             cf2 = cf1.exceptionally(x -> { check(x.getCause() == t); return 2;});
   797             checkCompletedExceptionally(cf1);
   797             checkCompletedExceptionally(cf1);
   798             checkCompletedNormally(cf2, 2);
   798             checkCompletedNormally(cf2, 2);
   799         } catch (Throwable t) { unexpected(t); }
   799         } catch (Throwable t) { unexpected(t); }
   800 
   800 
   801         //----------------------------------------------------------------
   801         //----------------------------------------------------------------