--- jsr166/src/test/tck/CompletableFutureTest.java 2013/04/07 15:04:14 1.19 +++ jsr166/src/test/tck/CompletableFutureTest.java 2013/04/08 16:45:15 1.20 @@ -55,6 +55,9 @@ public class CompletableFutureTest exten void checkCompletedNormally(CompletableFuture f, T value) { try { + assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); + } catch (Throwable fail) { threadUnexpectedException(fail); } + try { assertEquals(value, f.join()); } catch (Throwable fail) { threadUnexpectedException(fail); } try { @@ -63,9 +66,6 @@ public class CompletableFutureTest exten try { assertEquals(value, f.get()); } catch (Throwable fail) { threadUnexpectedException(fail); } - try { - assertEquals(value, f.get(0L, SECONDS)); - } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(f.isDone()); assertFalse(f.isCancelled()); assertTrue(f.toString().contains("[Completed normally]")); @@ -73,6 +73,12 @@ public class CompletableFutureTest exten void checkCompletedWithWrappedCFException(CompletableFuture f) { try { + f.get(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof CFException); + } catch (Throwable fail) { threadUnexpectedException(fail); } + try { f.join(); shouldThrow(); } catch (CompletionException success) { @@ -90,12 +96,6 @@ public class CompletableFutureTest exten } catch (ExecutionException success) { assertTrue(success.getCause() instanceof CFException); } catch (Throwable fail) { threadUnexpectedException(fail); } - try { - f.get(0L, SECONDS); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof CFException); - } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(f.isDone()); assertFalse(f.isCancelled()); assertTrue(f.toString().contains("[Completed exceptionally]")); @@ -103,6 +103,11 @@ public class CompletableFutureTest exten void checkCancelled(CompletableFuture f) { try { + f.get(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + try { f.join(); shouldThrow(); } catch (CancellationException success) {} @@ -115,11 +120,6 @@ public class CompletableFutureTest exten shouldThrow(); } catch (CancellationException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } - try { - f.get(0L, SECONDS); - shouldThrow(); - } catch (CancellationException success) { - } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(f.isDone()); assertTrue(f.isCancelled()); assertTrue(f.toString().contains("[Completed exceptionally]")); @@ -127,6 +127,12 @@ public class CompletableFutureTest exten void checkCompletedWithWrappedCancellationException(CompletableFuture f) { try { + f.get(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof CancellationException); + } catch (Throwable fail) { threadUnexpectedException(fail); } + try { f.join(); shouldThrow(); } catch (CompletionException success) { @@ -144,12 +150,6 @@ public class CompletableFutureTest exten } catch (ExecutionException success) { assertTrue(success.getCause() instanceof CancellationException); } catch (Throwable fail) { threadUnexpectedException(fail); } - try { - f.get(0L, SECONDS); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof CancellationException); - } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(f.isDone()); assertFalse(f.isCancelled()); assertTrue(f.toString().contains("[Completed exceptionally]")); @@ -332,7 +332,9 @@ public class CompletableFutureTest exten static final class CompletableFutureInc implements Function> { + boolean ran; public CompletableFuture apply(Integer x) { + ran = true; CompletableFuture f = new CompletableFuture(); f.complete(Integer.valueOf(x.intValue() + 1)); return f; @@ -1797,11 +1799,18 @@ public class CompletableFutureTest exten * completion of source */ public void testThenComposeAsync() { - CompletableFuture f = new CompletableFuture(); - CompletableFutureInc r = new CompletableFutureInc(); - CompletableFuture g = f.thenComposeAsync(r); + CompletableFuture f, g; + CompletableFutureInc r; + + f = new CompletableFuture(); + g = f.thenComposeAsync(r = new CompletableFutureInc()); f.complete(one); checkCompletedNormally(g, two); + + f = new CompletableFuture(); + f.complete(one); + g = f.thenComposeAsync(r = new CompletableFutureInc()); + checkCompletedNormally(g, two); } /** @@ -1809,32 +1818,55 @@ public class CompletableFutureTest exten * exceptional completion of source */ public void testThenComposeAsync2() { - CompletableFuture f = new CompletableFuture(); - CompletableFutureInc r = new CompletableFutureInc(); - CompletableFuture g = f.thenComposeAsync(r); + CompletableFuture f, g; + CompletableFutureInc r; + + f = new CompletableFuture(); + g = f.thenComposeAsync(r = new CompletableFutureInc()); + f.completeExceptionally(new CFException()); + checkCompletedWithWrappedCFException(g); + assertFalse(r.ran); + + f = new CompletableFuture(); f.completeExceptionally(new CFException()); + g = f.thenComposeAsync(r = new CompletableFutureInc()); checkCompletedWithWrappedCFException(g); + assertFalse(r.ran); } /** * thenComposeAsync result completes exceptionally if action does */ public void testThenComposeAsync3() { - CompletableFuture f = new CompletableFuture(); - FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); - CompletableFuture g = f.thenComposeAsync(r); + CompletableFuture f, g; + FailingCompletableFutureFunction r; + + f = new CompletableFuture(); + g = f.thenComposeAsync(r = new FailingCompletableFutureFunction()); f.complete(one); checkCompletedWithWrappedCFException(g); + + f = new CompletableFuture(); + f.complete(one); + g = f.thenComposeAsync(r = new FailingCompletableFutureFunction()); + checkCompletedWithWrappedCFException(g); } /** * thenComposeAsync result completes exceptionally if source cancelled */ public void testThenComposeAsync4() { - CompletableFuture f = new CompletableFuture(); - CompletableFutureInc r = new CompletableFutureInc(); - CompletableFuture g = f.thenComposeAsync(r); + CompletableFuture f, g; + CompletableFutureInc r; + + f = new CompletableFuture(); + g = f.thenComposeAsync(r = new CompletableFutureInc()); + assertTrue(f.cancel(true)); + checkCompletedWithWrappedCancellationException(g); + + f = new CompletableFuture(); assertTrue(f.cancel(true)); + g = f.thenComposeAsync(r = new CompletableFutureInc()); checkCompletedWithWrappedCancellationException(g); }