--- jsr166/src/test/tck/CompletableFutureTest.java 2015/09/03 11:45:34 1.103 +++ jsr166/src/test/tck/CompletableFutureTest.java 2015/09/06 16:47:24 1.120 @@ -3333,6 +3333,7 @@ public class CompletableFutureTest exten () -> f.exceptionally(null), () -> f.handle(null), + () -> CompletableFuture.allOf((CompletableFuture)null), () -> CompletableFuture.allOf((CompletableFuture[])null), () -> CompletableFuture.allOf(f, null), @@ -3344,6 +3345,13 @@ public class CompletableFutureTest exten () -> CompletableFuture.anyOf(null, f), () -> f.obtrudeException(null), + + () -> CompletableFuture.delayedExecutor(1L, SECONDS, null), + () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), + () -> CompletableFuture.delayedExecutor(1L, null), + + () -> f.orTimeout(1L, null), + () -> f.completeOnTimeout(42, 1L, null), }; assertThrows(NullPointerException.class, throwingActions); @@ -3359,22 +3367,30 @@ public class CompletableFutureTest exten } // jdk9 - + /** * newIncompleteFuture returns an incomplete CompletableFuture */ public void testNewIncompleteFuture() { + for (Integer v1 : new Integer[] { 1, null }) + { CompletableFuture f = new CompletableFuture<>(); CompletableFuture g = f.newIncompleteFuture(); checkIncomplete(f); checkIncomplete(g); - } + f.complete(v1); + checkCompletedNormally(f, v1); + checkIncomplete(g); + g.complete(v1); + checkCompletedNormally(g, v1); + assertSame(g.getClass(), CompletableFuture.class); + }} /** * completedStage returns a completed CompletionStage */ public void testCompletedStage() { - AtomicInteger x = new AtomicInteger(); + AtomicInteger x = new AtomicInteger(0); AtomicReference r = new AtomicReference(); CompletionStage f = CompletableFuture.completedStage(1); f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); @@ -3384,14 +3400,16 @@ public class CompletableFutureTest exten /** * defaultExecutor by default returns the commonPool if - * it supports at least one thread. + * it supports more than one thread. */ public void testDefaultExecutor() { CompletableFuture f = new CompletableFuture<>(); Executor e = f.defaultExecutor(); - Executor c = ForkJoinPool.commonPool(); - if (ForkJoinPool.getCommonPoolParallelism() > 0) + Executor c = ForkJoinPool.commonPool(); + if (ForkJoinPool.getCommonPoolParallelism() > 1) assertSame(e, c); + else + assertNotSame(e, c); } /** @@ -3401,17 +3419,17 @@ public class CompletableFutureTest exten public void testFailedFuture() { CFException ex = new CFException(); CompletableFuture f = CompletableFuture.failedFuture(ex); - checkCompletedExceptionallyWithRootCause(f, ex); + checkCompletedExceptionally(f, ex); } /** * failedFuture(null) throws NPE */ - public void testFailedFuture2() { + public void testFailedFuture_null() { try { CompletableFuture f = CompletableFuture.failedFuture(null); - } catch(NullPointerException success) { - } + shouldThrow(); + } catch (NullPointerException success) {} } /** @@ -3450,7 +3468,7 @@ public class CompletableFutureTest exten public void testMinimalCompletionStage() { CompletableFuture f = new CompletableFuture<>(); CompletionStage g = f.minimalCompletionStage(); - AtomicInteger x = new AtomicInteger(); + AtomicInteger x = new AtomicInteger(0); AtomicReference r = new AtomicReference(); checkIncomplete(f); g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); @@ -3467,7 +3485,7 @@ public class CompletableFutureTest exten public void testMinimalCompletionStage2() { CompletableFuture f = new CompletableFuture<>(); CompletionStage g = f.minimalCompletionStage(); - AtomicInteger x = new AtomicInteger(); + AtomicInteger x = new AtomicInteger(0); AtomicReference r = new AtomicReference(); g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); checkIncomplete(f); @@ -3479,17 +3497,17 @@ public class CompletableFutureTest exten } /** - * failedStage returns a Completionstage completed + * failedStage returns a CompletionStage completed * exceptionally with the given Exception */ public void testFailedStage() { CFException ex = new CFException(); CompletionStage f = CompletableFuture.failedStage(ex); - AtomicInteger x = new AtomicInteger(); + AtomicInteger x = new AtomicInteger(0); AtomicReference r = new AtomicReference(); f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); assertEquals(x.get(), 0); - assertEquals(r.get().getCause(), ex); + assertEquals(r.get(), ex); } /** @@ -3512,8 +3530,7 @@ public class CompletableFutureTest exten try { f.join(); shouldThrow(); - } catch(Exception success) { - } + } catch (Exception success) {} checkCompletedWithWrappedCFException(f); } @@ -3538,63 +3555,60 @@ public class CompletableFutureTest exten try { f.join(); shouldThrow(); - } catch(Exception success) { - } + } catch (Exception success) {} checkCompletedWithWrappedCFException(f); } /** - * orTimeout completes with TimeoutException if not complete + * orTimeout completes with TimeoutException if not complete */ public void testOrTimeout() { CompletableFuture f = new CompletableFuture<>(); - f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + f.orTimeout(SHORT_DELAY_MS, MILLISECONDS); checkCompletedExceptionallyWithTimeout(f); } /** - * orTimeout completes normally if completed before timeout + * orTimeout completes normally if completed before timeout */ public void testOrTimeout2() { CompletableFuture f = new CompletableFuture<>(); f.complete(1); - f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + f.orTimeout(SHORT_DELAY_MS, MILLISECONDS); checkCompletedNormally(f, 1); } /** - * completeOnTimeout completes with given value if not complete + * completeOnTimeout completes with given value if not complete */ public void testCompleteOnTimeout() { CompletableFuture f = new CompletableFuture<>(); - f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS); f.join(); checkCompletedNormally(f, -1); } /** - * completeOnTimeout has no effect if completed within timeout + * completeOnTimeout has no effect if completed within timeout */ public void testCompleteOnTimeout2() { CompletableFuture f = new CompletableFuture<>(); f.complete(1); - f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS); checkCompletedNormally(f, 1); } /** * delayedExecutor returns an executor that delays submission */ - public void testDelayedExecutor() { + public void testDelayedExecutor() throws Exception { long timeoutMillis = SMALL_DELAY_MS; Executor d = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); long startTime = System.nanoTime(); CompletableFuture f = CompletableFuture.supplyAsync(() -> 1, d); assertNull(f.getNow(null)); - try { - f.get(LONG_DELAY_MS, MILLISECONDS); - } catch (Throwable fail) { threadUnexpectedException(fail); } + assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); checkCompletedNormally(f, 1); } @@ -3603,7 +3617,7 @@ public class CompletableFutureTest exten * delayedExecutor for a given executor returns an executor that * delays submission */ - public void testDelayedExecutor2() { + public void testDelayedExecutor2() throws Exception { long timeoutMillis = SMALL_DELAY_MS; Executor d = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, @@ -3611,9 +3625,7 @@ public class CompletableFutureTest exten long startTime = System.nanoTime(); CompletableFuture f = CompletableFuture.supplyAsync(() -> 1, d); assertNull(f.getNow(null)); - try { - f.get(LONG_DELAY_MS, MILLISECONDS); - } catch (Throwable fail) { threadUnexpectedException(fail); } + assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); checkCompletedNormally(f, 1); }