--- jsr166/src/test/tck/CompletableFutureTest.java 2013/03/22 16:10:19 1.6 +++ jsr166/src/test/tck/CompletableFutureTest.java 2013/03/31 18:17:18 1.13 @@ -53,7 +53,7 @@ public class CompletableFutureTest exten catch (Throwable fail) { threadUnexpectedException(fail); } } - void checkCompletedNormally(CompletableFuture f, Object value) { + void checkCompletedNormally(CompletableFuture f, T value) { try { assertEquals(value, f.join()); } catch (Throwable fail) { threadUnexpectedException(fail); } @@ -75,31 +75,27 @@ public class CompletableFutureTest exten try { f.join(); shouldThrow(); - } catch (Throwable ex) { - assertTrue(ex instanceof CompletionException && - ((CompletionException)ex).getCause() instanceof CFException); + } catch (CompletionException success) { + assertTrue(success.getCause() instanceof CFException); } try { f.getNow(null); shouldThrow(); - } catch (Throwable ex) { - assertTrue(ex instanceof CompletionException && - ((CompletionException)ex).getCause() instanceof CFException); + } catch (CompletionException success) { + assertTrue(success.getCause() instanceof CFException); } try { f.get(); shouldThrow(); - } catch (Throwable ex) { - assertTrue(ex instanceof ExecutionException && - ((ExecutionException)ex).getCause() instanceof CFException); - } + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof CFException); + } catch (Throwable fail) { threadUnexpectedException(fail); } try { f.get(0L, SECONDS); shouldThrow(); - } catch (Throwable ex) { - assertTrue(ex instanceof ExecutionException && - ((ExecutionException)ex).getCause() instanceof CFException); - } + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof CFException); + } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(f.isDone()); assertFalse(f.isCancelled()); } @@ -108,27 +104,21 @@ public class CompletableFutureTest exten try { f.join(); shouldThrow(); - } catch (Throwable ex) { - assertTrue(ex instanceof CancellationException); - } + } catch (CancellationException success) {} try { f.getNow(null); shouldThrow(); - } catch (Throwable ex) { - assertTrue(ex instanceof CancellationException); - } + } catch (CancellationException success) {} try { f.get(); shouldThrow(); - } catch (Throwable ex) { - assertTrue(ex instanceof CancellationException); - } + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } try { f.get(0L, SECONDS); shouldThrow(); - } catch (Throwable ex) { - assertTrue(ex instanceof CancellationException); - } + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(f.isDone()); assertTrue(f.isCancelled()); } @@ -137,31 +127,27 @@ public class CompletableFutureTest exten try { f.join(); shouldThrow(); - } catch (Throwable ex) { - assertTrue(ex instanceof CompletionException && - ((CompletionException)ex).getCause() instanceof CancellationException); + } catch (CompletionException success) { + assertTrue(success.getCause() instanceof CancellationException); } try { f.getNow(null); shouldThrow(); - } catch (Throwable ex) { - assertTrue(ex instanceof CompletionException && - ((CompletionException)ex).getCause() instanceof CancellationException); + } catch (CompletionException success) { + assertTrue(success.getCause() instanceof CancellationException); } try { f.get(); shouldThrow(); - } catch (Throwable ex) { - assertTrue(ex instanceof ExecutionException && - ((ExecutionException)ex).getCause() instanceof CancellationException); - } + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof CancellationException); + } catch (Throwable fail) { threadUnexpectedException(fail); } try { f.get(0L, SECONDS); shouldThrow(); - } catch (Throwable ex) { - assertTrue(ex instanceof ExecutionException && - ((ExecutionException)ex).getCause() instanceof CancellationException); - } + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof CancellationException); + } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(f.isDone()); assertFalse(f.isCancelled()); } @@ -285,6 +271,14 @@ public class CompletableFutureTest exten assertTrue(f.toString().contains("[Completed exceptionally]")); } + /** + * completedFuture returns a completed CompletableFuture with given value + */ + public void testCompletedFuture() { + CompletableFuture f = CompletableFuture.completedFuture("test"); + checkCompletedNormally(f, "test"); + } + static final Supplier supplyOne = () -> Integer.valueOf(1); static final Function inc = @@ -456,7 +450,7 @@ public class CompletableFutureTest exten assertTrue(r.ran); } - // seq conmpletion methods + // seq completion methods /** * thenRun result completes normally after normal completion of source @@ -1671,7 +1665,8 @@ public class CompletableFutureTest exten } /** - * thenCompse result completes normally after normal completion of source + * thenComposeAsync result completes normally after normal + * completion of source */ public void testThenComposeAsync() { CompletableFuture f = new CompletableFuture(); @@ -1682,8 +1677,8 @@ public class CompletableFutureTest exten } /** - * thenComposeAsync result completes exceptionally after exceptional - * completion of source + * thenComposeAsync result completes exceptionally after + * exceptional completion of source */ public void testThenComposeAsync2() { CompletableFuture f = new CompletableFuture(); @@ -1716,7 +1711,7 @@ public class CompletableFutureTest exten } - // aaync with explicit executors + // async with explicit executors /** * thenRunAsync result completes normally after normal completion of source @@ -2293,7 +2288,8 @@ public class CompletableFutureTest exten } /** - * thenCompse result completes normally after normal completion of source + * thenComposeAsync result completes normally after normal + * completion of source */ public void testThenComposeAsyncE() { CompletableFuture f = new CompletableFuture(); @@ -2304,8 +2300,8 @@ public class CompletableFutureTest exten } /** - * thenComposeAsync result completes exceptionally after exceptional - * completion of source + * thenComposeAsync result completes exceptionally after + * exceptional completion of source */ public void testThenComposeAsync2E() { CompletableFuture f = new CompletableFuture(); @@ -2356,13 +2352,12 @@ public class CompletableFutureTest exten CompletableFuture[] fs = new CompletableFuture[k]; for (int i = 0; i < k; ++i) fs[i] = new CompletableFuture(); - CompletableFuture f = CompletableFuture.allOf(fs); + CompletableFuture f = CompletableFuture.allOf(fs); for (int i = 0; i < k; ++i) { checkIncomplete(f); fs[i].complete(one); } - assertTrue(f.isDone()); - assertFalse(f.isCancelled()); + checkCompletedNormally(f, null); } } @@ -2382,11 +2377,11 @@ public class CompletableFutureTest exten CompletableFuture[] fs = new CompletableFuture[k]; for (int i = 0; i < k; ++i) fs[i] = new CompletableFuture(); - CompletableFuture f = CompletableFuture.anyOf(fs); + CompletableFuture f = CompletableFuture.anyOf(fs); checkIncomplete(f); for (int i = 0; i < k; ++i) { fs[i].complete(one); - assertTrue(f.isDone()); + checkCompletedNormally(f, one); } } }