--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/01 23:12:45 1.36 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/01 23:51:44 1.38 @@ -318,12 +318,18 @@ public class CompletableFutureTest exten // Choose non-commutative actions for better coverage - // A non-commutative function that handles null values as well - public static int subtract(Integer x, Integer y) { - return ((x == null) ? 42 : x.intValue()) + // A non-commutative function that handles and produces null values as well. + static Integer subtract(Integer x, Integer y) { + return (x == null && y == null) ? null : + ((x == null) ? 42 : x.intValue()) - ((y == null) ? 99 : y.intValue()); } + // A function that handles and produces null values as well. + static Integer inc(Integer x) { + return (x == null) ? null : x + 1; + } + static final Supplier supplyOne = () -> Integer.valueOf(1); static final Function inc = @@ -331,12 +337,26 @@ public class CompletableFutureTest exten static final BiFunction subtract = (Integer x, Integer y) -> subtract(x, y); static final class IncAction implements Consumer { - int value; - public void accept(Integer x) { value = x.intValue() + 1; } + int invocationCount = 0; + Integer value; + public boolean ran() { return invocationCount == 1; } + public void accept(Integer x) { + invocationCount++; + value = inc(x); + } + } + static final class IncFunction implements Function { + int invocationCount = 0; + Integer value; + public boolean ran() { return invocationCount == 1; } + public Integer apply(Integer x) { + invocationCount++; + return value = inc(x); + } } static final class SubtractAction implements BiConsumer { - volatile int invocationCount = 0; - int value; + int invocationCount = 0; + Integer value; // Check this action was invoked exactly once when result is computed. public boolean ran() { return invocationCount == 1; } public void accept(Integer x, Integer y) { @@ -345,13 +365,13 @@ public class CompletableFutureTest exten } } static final class SubtractFunction implements BiFunction { - volatile int invocationCount = 0; - int value; + int invocationCount = 0; + Integer value; // Check this action was invoked exactly once when result is computed. public boolean ran() { return invocationCount == 1; } public Integer apply(Integer x, Integer y) { invocationCount++; - return subtract(x, y); + return value = subtract(x, y); } } static final class Noop implements Runnable { @@ -447,6 +467,34 @@ public class CompletableFutureTest exten BiFunction a) { return f.thenCombine(g, a); } + public CompletableFuture applyToEither + (CompletableFuture f, + CompletionStage g, + Function a) { + return f.applyToEither(g, a); + } + public CompletableFuture acceptEither + (CompletableFuture f, + CompletionStage g, + Consumer a) { + return f.acceptEither(g, a); + } + public CompletableFuture runAfterEither + (CompletableFuture f, + CompletionStage g, + java.lang.Runnable a) { + return f.runAfterEither(g, a); + } + public CompletableFuture thenCompose + (CompletableFuture f, + Function> a) { + return f.thenCompose(a); + } + public CompletableFuture whenComplete + (CompletableFuture f, + BiConsumer a) { + return f.whenComplete(a); + } }, // /** Experimental way to do more testing */ @@ -480,6 +528,34 @@ public class CompletableFutureTest exten BiFunction a) { return f.thenCombineAsync(g, a); } + public CompletableFuture applyToEither + (CompletableFuture f, + CompletionStage g, + Function a) { + return f.applyToEitherAsync(g, a); + } + public CompletableFuture acceptEither + (CompletableFuture f, + CompletionStage g, + Consumer a) { + return f.acceptEitherAsync(g, a); + } + public CompletableFuture runAfterEither + (CompletableFuture f, + CompletionStage g, + java.lang.Runnable a) { + return f.runAfterEitherAsync(g, a); + } + public CompletableFuture thenCompose + (CompletableFuture f, + Function> a) { + return f.thenComposeAsync(a); + } + public CompletableFuture whenComplete + (CompletableFuture f, + BiConsumer a) { + return f.whenCompleteAsync(a); + } }, // REVERSE_DEFAULT_ASYNC { @@ -512,6 +588,34 @@ public class CompletableFutureTest exten BiFunction a) { return f.thenCombineAsync(g, a, new ThreadExecutor()); } + public CompletableFuture applyToEither + (CompletableFuture f, + CompletionStage g, + Function a) { + return f.applyToEitherAsync(g, a, new ThreadExecutor()); + } + public CompletableFuture acceptEither + (CompletableFuture f, + CompletionStage g, + Consumer a) { + return f.acceptEitherAsync(g, a, new ThreadExecutor()); + } + public CompletableFuture runAfterEither + (CompletableFuture f, + CompletionStage g, + java.lang.Runnable a) { + return f.runAfterEitherAsync(g, a, new ThreadExecutor()); + } + public CompletableFuture thenCompose + (CompletableFuture f, + Function> a) { + return f.thenComposeAsync(a, new ThreadExecutor()); + } + public CompletableFuture whenComplete + (CompletableFuture f, + BiConsumer a) { + return f.whenCompleteAsync(a, new ThreadExecutor()); + } }; public abstract CompletableFuture runAfterBoth @@ -524,6 +628,26 @@ public class CompletableFutureTest exten (CompletableFuture f, CompletionStage g, BiFunction a); + public abstract CompletableFuture applyToEither + (CompletableFuture f, + CompletionStage g, + Function a); + public abstract CompletableFuture acceptEither + (CompletableFuture f, + CompletionStage g, + Consumer a); + public abstract CompletableFuture runAfterEither + (CompletableFuture f, + CompletionStage g, + java.lang.Runnable a); + public abstract CompletableFuture thenCompose + (CompletableFuture f, + Function> a); + public abstract CompletableFuture whenComplete + (CompletableFuture f, + BiConsumer a); + + } /** @@ -776,7 +900,7 @@ public class CompletableFutureTest exten CompletableFuture g = f.thenAccept(r); f.complete(one); checkCompletedNormally(g, null); - assertEquals(r.value, 2); + assertEquals(r.value, (Integer) 2); } /** @@ -930,7 +1054,7 @@ public class CompletableFutureTest exten final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); final SubtractFunction r = new SubtractFunction(); - final CompletableFuture h = m.thenCombine(f, g, subtract); + final CompletableFuture h = m.thenCombine(f, g, r); final CFException ex = new CFException(); g.completeExceptionally(ex); @@ -975,7 +1099,7 @@ public class CompletableFutureTest exten f.completeExceptionally(ex); g.complete(v1); - final CompletableFuture h = m.thenCombine(f, g, subtract); + final CompletableFuture h = m.thenCombine(f, g, r); checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(f, ex); @@ -1128,7 +1252,7 @@ public class CompletableFutureTest exten f.complete(v1); checkIncomplete(h); - assertEquals(r.value, 0); + assertFalse(r.ran()); g.complete(v2); checkCompletedNormally(h, null); @@ -1150,7 +1274,7 @@ public class CompletableFutureTest exten g.complete(v2); checkIncomplete(h); - assertEquals(r.value, 0); + assertFalse(r.ran()); f.complete(v1); checkCompletedNormally(h, null); @@ -1795,7 +1919,7 @@ public class CompletableFutureTest exten checkCompletedNormally(g, null); f2.complete(one); checkCompletedNormally(g, null); - assertEquals(r.value, 2); + assertEquals(r.value, (Integer) 2); r = new IncAction(); f = new CompletableFuture<>(); @@ -1803,7 +1927,7 @@ public class CompletableFutureTest exten f2 = new CompletableFuture<>(); g = f.acceptEither(f2, r); checkCompletedNormally(g, null); - assertEquals(r.value, 2); + assertEquals(r.value, (Integer) 2); } /** @@ -2113,7 +2237,7 @@ public class CompletableFutureTest exten CompletableFuture g = f.thenAcceptAsync(r); f.complete(one); checkCompletedNormally(g, null); - assertEquals(r.value, 2); + assertEquals(r.value, (Integer) 2); } /** @@ -2227,7 +2351,7 @@ public class CompletableFutureTest exten CompletableFuture g = f.acceptEitherAsync(f2, r); f.complete(one); checkCompletedNormally(g, null); - assertEquals(r.value, 2); + assertEquals(r.value, (Integer) 2); r = new IncAction(); f = new CompletableFuture<>(); @@ -2235,7 +2359,7 @@ public class CompletableFutureTest exten f2 = new CompletableFuture<>(); g = f.acceptEitherAsync(f2, r); checkCompletedNormally(g, null); - assertEquals(r.value, 2); + assertEquals(r.value, (Integer) 2); } /** @@ -2552,7 +2676,7 @@ public class CompletableFutureTest exten CompletableFuture g = f.thenAcceptAsync(r, new ThreadExecutor()); f.complete(one); checkCompletedNormally(g, null); - assertEquals(r.value, 2); + assertEquals(r.value, (Integer) 2); } /** @@ -2666,7 +2790,7 @@ public class CompletableFutureTest exten CompletableFuture g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); f.complete(one); checkCompletedNormally(g, null); - assertEquals(r.value, 2); + assertEquals(r.value, (Integer) 2); r = new IncAction(); f = new CompletableFuture<>(); @@ -2674,7 +2798,7 @@ public class CompletableFutureTest exten f2 = new CompletableFuture<>(); g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); checkCompletedNormally(g, null); - assertEquals(r.value, 2); + assertEquals(r.value, (Integer) 2); } /**