jdk Udiff test/java/util/concurrent/CompletableFuture/Basic.java (original) (raw)
@@ -502,11 +502,11 @@ check(cf1.isDone() || cf2.isDone());
cf1 = supplyAsync(() -> { throw new RuntimeException(); });
cf2 = supplyAsync(() -> 2);
cf3 = cf1.applyToEither(cf2, (x) -> { check(x == 2); return x; });
try { check(cf3.join() == 1); } catch (CompletionException x) { pass(); }
try { check(cf3.join() == 2); } catch (CompletionException x) { pass(); } check(cf3.isDone()); check(cf1.isDone() || cf2.isDone()); cf1 = supplyAsync(() -> 1); cf2 = supplyAsync(() -> { throw new RuntimeException(); });
@@ -518,10 +518,27 @@ cf1 = supplyAsync(() -> { throw new RuntimeException(); }); cf2 = supplyAsync(() -> { throw new RuntimeException(); }); cf3 = cf1.applyToEitherAsync(cf2, (x) -> { fail(); return x; }); checkCompletedExceptionally(cf3); check(cf1.isDone() || cf2.isDone()); +
final Phaser phaser = new Phaser(2);
cf1 = supplyAsync(() -> { phaser.arriveAndAwaitAdvance(); return 1; });
cf2 = supplyAsync(() -> 2);
cf3 = cf1.applyToEither(cf2, (x) -> { check(x == 2); return x; });
checkCompletedNormally(cf3, 2);
checkCompletedNormally(cf2, 2);
check(!cf1.isDone());
phaser.arrive();
cf1 = supplyAsync(() -> 1);
cf2 = supplyAsync(() -> { phaser.arriveAndAwaitAdvance(); return 2; });
cf3 = cf1.applyToEitherAsync(cf2, (x) -> { check(x == 1); return x; });
checkCompletedNormally(cf3, 1);
checkCompletedNormally(cf1, 1);
check(!cf2.isDone());
phaser.arrive(); } catch (Throwable t) { unexpected(t); } //---------------------------------------------------------------- // acceptEitherXXX tests //----------------------------------------------------------------
@@ -568,10 +585,27 @@ cf1 = supplyAsync(() -> { throw new RuntimeException(); }); cf2 = supplyAsync(() -> { throw new RuntimeException(); }); cf3 = cf2.acceptEitherAsync(cf1, (x) -> { fail(); }); checkCompletedExceptionally(cf3); check(cf1.isDone() || cf2.isDone()); +
final Phaser phaser = new Phaser(2);
cf1 = supplyAsync(() -> { phaser.arriveAndAwaitAdvance(); return 1; });
cf2 = supplyAsync(() -> 2);
cf3 = cf1.acceptEither(cf2, (x) -> { check(x == 2); });
checkCompletedNormally(cf3, null);
checkCompletedNormally(cf2, 2);
check(!cf1.isDone());
phaser.arrive();
cf1 = supplyAsync(() -> 1);
cf2 = supplyAsync(() -> { phaser.arriveAndAwaitAdvance(); return 2; });
cf3 = cf1.acceptEitherAsync(cf2, (x) -> { check(x == 1); });
checkCompletedNormally(cf3, null);
checkCompletedNormally(cf1, 1);
check(!cf2.isDone());
phaser.arrive(); } catch (Throwable t) { unexpected(t); } //---------------------------------------------------------------- // runAfterEitherXXX tests //----------------------------------------------------------------
@@ -603,31 +637,56 @@
before = atomicInt.get();
cf1 = runAsync(() -> { throw new RuntimeException(); });
cf2 = runAsync(() -> { });
cf3 = cf2.runAfterEither(cf1, () -> { atomicInt.incrementAndGet(); });
try { check(cf3.join() == null); } catch (CompletionException x) { pass(); }
try {
check(cf3.join() == null);
check(atomicInt.get() == (before + 1));
} catch (CompletionException x) { pass(); } check(cf3.isDone()); check(cf1.isDone() || cf2.isDone());
check(atomicInt.get() == (before + 1)); before = atomicInt.get(); cf1 = runAsync(() -> { }); cf2 = runAsync(() -> { throw new RuntimeException(); }); cf3 = cf1.runAfterEitherAsync(cf2, () -> { atomicInt.incrementAndGet(); });
try { check(cf3.join() == null); } catch (CompletionException x) { pass(); }
try {
check(cf3.join() == null);
check(atomicInt.get() == (before + 1));
} catch (CompletionException x) { pass(); } check(cf3.isDone()); check(cf1.isDone() || cf2.isDone());
check(atomicInt.get() == (before + 1)); before = atomicInt.get(); cf1 = runAsync(() -> { throw new RuntimeException(); }); cf2 = runAsync(() -> { throw new RuntimeException(); }); cf3 = cf2.runAfterEitherAsync(cf1, () -> { atomicInt.incrementAndGet(); }, executor); checkCompletedExceptionally(cf3); check(cf1.isDone() || cf2.isDone()); check(atomicInt.get() == before);
final Phaser phaser = new Phaser(2);
before = atomicInt.get();
cf1 = runAsync(() -> { phaser.arriveAndAwaitAdvance(); });
cf2 = runAsync(() -> { });
cf3 = cf1.runAfterEither(cf2, () -> { atomicInt.incrementAndGet(); });
checkCompletedNormally(cf3, null);
checkCompletedNormally(cf2, null);
check(!cf1.isDone());
check(atomicInt.get() == (before + 1));
phaser.arrive();
before = atomicInt.get();
cf1 = runAsync(() -> { });
cf2 = runAsync(() -> { phaser.arriveAndAwaitAdvance(); });
cf3 = cf1.runAfterEitherAsync(cf2, () -> { atomicInt.incrementAndGet(); });
checkCompletedNormally(cf3, null);
checkCompletedNormally(cf1, null);
check(!cf2.isDone());
check(atomicInt.get() == (before + 1));
phaser.arrive(); } catch (Throwable t) { unexpected(t); } //---------------------------------------------------------------- // thenComposeXXX tests //----------------------------------------------------------------