# HG changeset patch # User chegar # Date 1365963450 -3600 # Node ID 9dd215f8cc5a3d6ff49e7ed5bfcda5ff5bcb5113 # Parent 8fecebee12b03ead20e201ae0fe83842fecbadef 8011799: CompletableFuture/Basic.java fails intermittently Reviewed-by: martin, alanb diff -r 8fecebee12b0 -r 9dd215f8cc5a jdk/test/java/util/concurrent/CompletableFuture/Basic.java --- a/jdk/test/java/util/concurrent/CompletableFuture/Basic.java Thu Apr 11 12:33:33 2013 -0400 +++ b/jdk/test/java/util/concurrent/CompletableFuture/Basic.java Sun Apr 14 19:17:30 2013 +0100 @@ -486,40 +486,40 @@ CompletableFuture cf1 = supplyAsync(() -> 1); CompletableFuture cf2 = supplyAsync(() -> 2); cf3 = cf1.applyToEither(cf2, (x) -> { check(x == 1 || x == 2); return x; }); + checkCompletedNormally(cf3, new Object[] {1, 2}); check(cf1.isDone() || cf2.isDone()); - checkCompletedNormally(cf3, new Object[] {1, 2}); cf1 = supplyAsync(() -> 1); cf2 = supplyAsync(() -> 2); cf3 = cf1.applyToEitherAsync(cf2, (x) -> { check(x == 1 || x == 2); return x; }); + checkCompletedNormally(cf3, new Object[] {1, 2}); check(cf1.isDone() || cf2.isDone()); - checkCompletedNormally(cf3, new Object[] {1, 2}); cf1 = supplyAsync(() -> 1); cf2 = supplyAsync(() -> 2); cf3 = cf1.applyToEitherAsync(cf2, (x) -> { check(x == 1 || x == 2); return x; }, executor); + checkCompletedNormally(cf3, new Object[] {1, 2}); check(cf1.isDone() || cf2.isDone()); - checkCompletedNormally(cf3, new Object[] {1, 2}); cf1 = supplyAsync(() -> { throw new RuntimeException(); }); cf2 = supplyAsync(() -> 2); cf3 = cf1.applyToEither(cf2, (x) -> { check(x == 2); return x; }); - check(cf1.isDone() || cf2.isDone()); try { check(cf3.join() == 1); } catch (CompletionException x) { pass(); } check(cf3.isDone()); + check(cf1.isDone() || cf2.isDone()); cf1 = supplyAsync(() -> 1); cf2 = supplyAsync(() -> { throw new RuntimeException(); }); cf3 = cf1.applyToEitherAsync(cf2, (x) -> { check(x == 1); return x; }); - check(cf1.isDone() || cf2.isDone()); try { check(cf3.join() == 1); } catch (CompletionException x) { pass(); } check(cf3.isDone()); + check(cf1.isDone() || cf2.isDone()); 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()); - checkCompletedExceptionally(cf3); } catch (Throwable t) { unexpected(t); } //---------------------------------------------------------------- @@ -531,45 +531,45 @@ CompletableFuture cf1 = supplyAsync(() -> 1); CompletableFuture cf2 = supplyAsync(() -> 2); cf3 = cf1.acceptEither(cf2, (x) -> { check(x == 1 || x == 2); atomicInt.incrementAndGet(); }); + checkCompletedNormally(cf3, null); check(cf1.isDone() || cf2.isDone()); - checkCompletedNormally(cf3, null); check(atomicInt.get() == (before + 1)); before = atomicInt.get(); cf1 = supplyAsync(() -> 1); cf2 = supplyAsync(() -> 2); cf3 = cf1.acceptEitherAsync(cf2, (x) -> { check(x == 1 || x == 2); atomicInt.incrementAndGet(); }); + checkCompletedNormally(cf3, null); check(cf1.isDone() || cf2.isDone()); - checkCompletedNormally(cf3, null); check(atomicInt.get() == (before + 1)); before = atomicInt.get(); cf1 = supplyAsync(() -> 1); cf2 = supplyAsync(() -> 2); cf3 = cf2.acceptEitherAsync(cf1, (x) -> { check(x == 1 || x == 2); atomicInt.incrementAndGet(); }, executor); + checkCompletedNormally(cf3, null); check(cf1.isDone() || cf2.isDone()); - checkCompletedNormally(cf3, null); check(atomicInt.get() == (before + 1)); cf1 = supplyAsync(() -> { throw new RuntimeException(); }); cf2 = supplyAsync(() -> 2); cf3 = cf2.acceptEitherAsync(cf1, (x) -> { check(x == 2); }, executor); - check(cf1.isDone() || cf2.isDone()); try { check(cf3.join() == null); } catch (CompletionException x) { pass(); } check(cf3.isDone()); + check(cf1.isDone() || cf2.isDone()); cf1 = supplyAsync(() -> 1); cf2 = supplyAsync(() -> { throw new RuntimeException(); }); cf3 = cf2.acceptEitherAsync(cf1, (x) -> { check(x == 1); }); - check(cf1.isDone() || cf2.isDone()); try { check(cf3.join() == null); } catch (CompletionException x) { pass(); } check(cf3.isDone()); + check(cf1.isDone() || cf2.isDone()); cf1 = supplyAsync(() -> { throw new RuntimeException(); }); cf2 = supplyAsync(() -> { throw new RuntimeException(); }); cf3 = cf2.acceptEitherAsync(cf1, (x) -> { fail(); }); + checkCompletedExceptionally(cf3); check(cf1.isDone() || cf2.isDone()); - checkCompletedExceptionally(cf3); } catch (Throwable t) { unexpected(t); } //---------------------------------------------------------------- @@ -581,50 +581,50 @@ CompletableFuture cf1 = runAsync(() -> { }); CompletableFuture cf2 = runAsync(() -> { }); cf3 = cf1.runAfterEither(cf2, () -> { atomicInt.incrementAndGet(); }); + checkCompletedNormally(cf3, null); check(cf1.isDone() || cf2.isDone()); - checkCompletedNormally(cf3, null); check(atomicInt.get() == (before + 1)); before = atomicInt.get(); cf1 = runAsync(() -> { }); cf2 = runAsync(() -> { }); cf3 = cf1.runAfterEitherAsync(cf2, () -> { atomicInt.incrementAndGet(); }); + checkCompletedNormally(cf3, null); check(cf1.isDone() || cf2.isDone()); - checkCompletedNormally(cf3, null); check(atomicInt.get() == (before + 1)); before = atomicInt.get(); cf1 = runAsync(() -> { }); cf2 = runAsync(() -> { }); cf3 = cf2.runAfterEitherAsync(cf1, () -> { atomicInt.incrementAndGet(); }, executor); + checkCompletedNormally(cf3, null); check(cf1.isDone() || cf2.isDone()); - checkCompletedNormally(cf3, null); check(atomicInt.get() == (before + 1)); before = atomicInt.get(); cf1 = runAsync(() -> { throw new RuntimeException(); }); cf2 = runAsync(() -> { }); cf3 = cf2.runAfterEither(cf1, () -> { atomicInt.incrementAndGet(); }); - check(cf1.isDone() || cf2.isDone()); try { check(cf3.join() == null); } 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(); }); - check(cf1.isDone() || cf2.isDone()); try { check(cf3.join() == null); } 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()); - checkCompletedExceptionally(cf3); check(atomicInt.get() == before); } catch (Throwable t) { unexpected(t); } @@ -670,16 +670,16 @@ //---------------------------------------------------------------- // anyOf tests //---------------------------------------------------------------- - //try { - // CompletableFuture cf3; - // for (int k=0; k < 10; k++){ - // CompletableFuture cf1 = supplyAsync(() -> 1); - // CompletableFuture cf2 = supplyAsync(() -> 2); - // cf3 = CompletableFuture.anyOf(cf1, cf2); - // check(cf1.isDone() || cf2.isDone()); - // checkCompletedNormally(cf3, new Object[] {1, 2}); - // } - //} catch (Throwable t) { unexpected(t); } + try { + CompletableFuture cf3; + for (int k=0; k < 10; k++){ + CompletableFuture cf1 = supplyAsync(() -> 1); + CompletableFuture cf2 = supplyAsync(() -> 2); + cf3 = CompletableFuture.anyOf(cf1, cf2); + checkCompletedNormally(cf3, new Object[] {1, 2}); + check(cf1.isDone() || cf2.isDone()); + } + } catch (Throwable t) { unexpected(t); } //---------------------------------------------------------------- // allOf tests