--- jsr166/src/test/tck/CompletableFutureTest.java 2013/04/04 04:16:02 1.17 +++ jsr166/src/test/tck/CompletableFutureTest.java 2013/04/07 14:54:06 1.18 @@ -282,12 +282,14 @@ public class CompletableFutureTest exten checkCompletedNormally(f, "test"); } + // Choose non-commutative actions for better coverage + static final Supplier supplyOne = () -> Integer.valueOf(1); static final Function inc = (Integer x) -> Integer.valueOf(x.intValue() + 1); - static final BiFunction add = - (Integer x, Integer y) -> Integer.valueOf(x.intValue() + y.intValue()); + static final BiFunction subtract = + (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue()); static final class IncAction implements Consumer { int value; public void accept(Integer x) { value = x.intValue() + 1; } @@ -328,7 +330,8 @@ public class CompletableFutureTest exten public void run() { ran = true; throw new CFException(); } } - static final class CompletableFutureInc implements Function> { + static final class CompletableFutureInc + implements Function> { public CompletableFuture apply(Integer x) { CompletableFuture f = new CompletableFuture(); f.complete(Integer.valueOf(x.intValue() + 1)); @@ -336,7 +339,8 @@ public class CompletableFutureTest exten } } - static final class FailingCompletableFutureFunction implements Function> { + static final class FailingCompletableFutureFunction + implements Function> { boolean ran; public CompletableFuture apply(Integer x) { ran = true; throw new CFException(); @@ -626,24 +630,34 @@ public class CompletableFutureTest exten /** - * thenCombine result completes normally after normal completion of sources + * thenCombine result completes normally after normal completion + * of sources */ public void testThenCombine() { - CompletableFuture f = new CompletableFuture(); - CompletableFuture f2 = new CompletableFuture(); - CompletableFuture g = f.thenCombine(f2, add); - f.complete(one); - checkIncomplete(g); - f2.complete(two); - checkCompletedNormally(g, three); + CompletableFuture f, g, h; f = new CompletableFuture(); - f.complete(one); - f2 = new CompletableFuture(); - g = f.thenCombine(f2, add); - checkIncomplete(g); - f2.complete(two); - checkCompletedNormally(g, three); + g = new CompletableFuture(); + h = f.thenCombine(g, subtract); + f.complete(3); + checkIncomplete(h); + g.complete(1); + checkCompletedNormally(h, 2); + + f = new CompletableFuture(); + g = new CompletableFuture(); + h = f.thenCombine(g, subtract); + g.complete(1); + checkIncomplete(h); + f.complete(3); + checkCompletedNormally(h, 2); + + f = new CompletableFuture(); + g = new CompletableFuture(); + g.complete(1); + f.complete(3); + h = f.thenCombine(g, subtract); + checkCompletedNormally(h, 2); } /** @@ -651,19 +665,37 @@ public class CompletableFutureTest exten * completion of either source */ public void testThenCombine2() { - CompletableFuture f = new CompletableFuture(); - CompletableFuture f2 = new CompletableFuture(); - CompletableFuture g = f.thenCombine(f2, add); + CompletableFuture f, g, h; + + f = new CompletableFuture(); + g = new CompletableFuture(); + h = f.thenCombine(g, subtract); f.completeExceptionally(new CFException()); - f2.complete(two); - checkCompletedWithWrappedCFException(g); + checkIncomplete(h); + g.complete(1); + checkCompletedWithWrappedCFException(h); f = new CompletableFuture(); - f.complete(one); - f2 = new CompletableFuture(); - g = f.thenCombine(f2, add); - f2.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); + g = new CompletableFuture(); + h = f.thenCombine(g, subtract); + g.completeExceptionally(new CFException()); + checkIncomplete(h); + f.complete(3); + checkCompletedWithWrappedCFException(h); + + f = new CompletableFuture(); + g = new CompletableFuture(); + f.complete(3); + g.completeExceptionally(new CFException()); + h = f.thenCombine(g, subtract); + checkCompletedWithWrappedCFException(h); + + f = new CompletableFuture(); + g = new CompletableFuture(); + f.completeExceptionally(new CFException()); + g.complete(3); + h = f.thenCombine(g, subtract); + checkCompletedWithWrappedCFException(h); } /** @@ -676,26 +708,40 @@ public class CompletableFutureTest exten CompletableFuture g = f.thenCombine(f2, r); f.complete(one); checkIncomplete(g); + assertFalse(r.ran); f2.complete(two); checkCompletedWithWrappedCFException(g); + assertTrue(r.ran); } /** * thenCombine result completes exceptionally if either source cancelled */ public void testThenCombine4() { - CompletableFuture f = new CompletableFuture(); - CompletableFuture f2 = new CompletableFuture(); - CompletableFuture g = f.thenCombine(f2, add); + CompletableFuture f, g, h; + + f = new CompletableFuture(); + g = new CompletableFuture(); + h = f.thenCombine(g, subtract); assertTrue(f.cancel(true)); - f2.complete(two); - checkCompletedWithWrappedCancellationException(g); + checkIncomplete(h); + g.complete(1); + checkCompletedWithWrappedCancellationException(h); + f = new CompletableFuture(); - f2 = new CompletableFuture(); - g = f.thenCombine(f2, add); - f.complete(one); - assertTrue(f2.cancel(true)); - checkCompletedWithWrappedCancellationException(g); + g = new CompletableFuture(); + h = f.thenCombine(g, subtract); + assertTrue(g.cancel(true)); + checkIncomplete(h); + f.complete(3); + checkCompletedWithWrappedCancellationException(h); + + f = new CompletableFuture(); + g = new CompletableFuture(); + assertTrue(f.cancel(true)); + assertTrue(g.cancel(true)); + h = f.thenCombine(g, subtract); + checkCompletedWithWrappedCancellationException(h); } /** @@ -1273,33 +1319,61 @@ public class CompletableFutureTest exten * completion of sources */ public void testThenCombineAsync() { - CompletableFuture f = new CompletableFuture(); - CompletableFuture f2 = new CompletableFuture(); - CompletableFuture g = f.thenCombineAsync(f2, add); - f.complete(one); - checkIncomplete(g); - f2.complete(two); - checkCompletedNormally(g, three); + CompletableFuture f, g, h; + + f = new CompletableFuture(); + g = new CompletableFuture(); + h = f.thenCombineAsync(g, subtract); + f.complete(3); + checkIncomplete(h); + g.complete(1); + checkCompletedNormally(h, 2); + + f = new CompletableFuture(); + g = new CompletableFuture(); + h = f.thenCombineAsync(g, subtract); + g.complete(1); + checkIncomplete(h); + f.complete(3); + checkCompletedNormally(h, 2); + + f = new CompletableFuture(); + g = new CompletableFuture(); + g.complete(1); + f.complete(3); + h = f.thenCombineAsync(g, subtract); + checkCompletedNormally(h, 2); } /** * thenCombineAsync result completes exceptionally after exceptional - * completion of source + * completion of either source */ public void testThenCombineAsync2() { - CompletableFuture f = new CompletableFuture(); - CompletableFuture f2 = new CompletableFuture(); - CompletableFuture g = f.thenCombineAsync(f2, add); + CompletableFuture f, g, h; + + f = new CompletableFuture(); + g = new CompletableFuture(); + h = f.thenCombineAsync(g, subtract); f.completeExceptionally(new CFException()); - f2.complete(two); - checkCompletedWithWrappedCFException(g); + checkIncomplete(h); + g.complete(1); + checkCompletedWithWrappedCFException(h); f = new CompletableFuture(); - f2 = new CompletableFuture(); - g = f.thenCombineAsync(f2, add); - f.complete(one); - f2.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); + g = new CompletableFuture(); + h = f.thenCombineAsync(g, subtract); + g.completeExceptionally(new CFException()); + checkIncomplete(h); + f.complete(3); + checkCompletedWithWrappedCFException(h); + + f = new CompletableFuture(); + g = new CompletableFuture(); + g.completeExceptionally(new CFException()); + f.complete(3); + h = f.thenCombineAsync(g, subtract); + checkCompletedWithWrappedCFException(h); } /** @@ -1312,27 +1386,47 @@ public class CompletableFutureTest exten CompletableFuture g = f.thenCombineAsync(f2, r); f.complete(one); checkIncomplete(g); + assertFalse(r.ran); f2.complete(two); checkCompletedWithWrappedCFException(g); + assertTrue(r.ran); } /** * thenCombineAsync result completes exceptionally if either source cancelled */ public void testThenCombineAsync4() { - CompletableFuture f = new CompletableFuture(); - CompletableFuture f2 = new CompletableFuture(); - CompletableFuture g = f.thenCombineAsync(f2, add); + CompletableFuture f, g, h; + + f = new CompletableFuture(); + g = new CompletableFuture(); + h = f.thenCombineAsync(g, subtract); assertTrue(f.cancel(true)); - f2.complete(two); - checkCompletedWithWrappedCancellationException(g); + checkIncomplete(h); + g.complete(1); + checkCompletedWithWrappedCancellationException(h); f = new CompletableFuture(); - f2 = new CompletableFuture(); - g = f.thenCombineAsync(f2, add); - f.complete(one); - assertTrue(f2.cancel(true)); - checkCompletedWithWrappedCancellationException(g); + g = new CompletableFuture(); + h = f.thenCombineAsync(g, subtract); + assertTrue(g.cancel(true)); + checkIncomplete(h); + f.complete(3); + checkCompletedWithWrappedCancellationException(h); + + f = new CompletableFuture(); + g = new CompletableFuture(); + g.complete(3); + assertTrue(f.cancel(true)); + h = f.thenCombineAsync(g, subtract); + checkCompletedWithWrappedCancellationException(h); + + f = new CompletableFuture(); + g = new CompletableFuture(); + f.complete(3); + assertTrue(g.cancel(true)); + h = f.thenCombineAsync(g, subtract); + checkCompletedWithWrappedCancellationException(h); } /** @@ -1891,38 +1985,77 @@ public class CompletableFutureTest exten assertTrue(f.cancel(true)); checkCompletedWithWrappedCancellationException(g); } + /** * thenCombineAsync result completes normally after normal * completion of sources */ public void testThenCombineAsyncE() { - CompletableFuture f = new CompletableFuture(); - CompletableFuture f2 = new CompletableFuture(); - CompletableFuture g = f.thenCombineAsync(f2, add, new ThreadExecutor()); - f.complete(one); - checkIncomplete(g); - f2.complete(two); - checkCompletedNormally(g, three); + CompletableFuture f, g, h; + ThreadExecutor e = new ThreadExecutor(); + int count = 0; + + f = new CompletableFuture(); + g = new CompletableFuture(); + h = f.thenCombineAsync(g, subtract, e); + f.complete(3); + checkIncomplete(h); + g.complete(1); + checkCompletedNormally(h, 2); + assertEquals(++count, e.count.get()); + + f = new CompletableFuture(); + g = new CompletableFuture(); + h = f.thenCombineAsync(g, subtract, e); + g.complete(1); + checkIncomplete(h); + f.complete(3); + checkCompletedNormally(h, 2); + assertEquals(++count, e.count.get()); + + f = new CompletableFuture(); + g = new CompletableFuture(); + g.complete(1); + f.complete(3); + h = f.thenCombineAsync(g, subtract, e); + checkCompletedNormally(h, 2); + assertEquals(++count, e.count.get()); } /** * thenCombineAsync result completes exceptionally after exceptional - * completion of source + * completion of either source */ public void testThenCombineAsync2E() { - CompletableFuture f = new CompletableFuture(); - CompletableFuture f2 = new CompletableFuture(); - CompletableFuture g = f.thenCombineAsync(f2, add, new ThreadExecutor()); + CompletableFuture f, g, h; + ThreadExecutor e = new ThreadExecutor(); + int count = 0; + + f = new CompletableFuture(); + g = new CompletableFuture(); + h = f.thenCombineAsync(g, subtract, e); f.completeExceptionally(new CFException()); - f2.complete(two); - checkCompletedWithWrappedCFException(g); + checkIncomplete(h); + g.complete(1); + checkCompletedWithWrappedCFException(h); f = new CompletableFuture(); - f2 = new CompletableFuture(); - g = f.thenCombineAsync(f2, add, new ThreadExecutor()); - f.complete(one); - f2.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); + g = new CompletableFuture(); + h = f.thenCombineAsync(g, subtract, e); + g.completeExceptionally(new CFException()); + checkIncomplete(h); + f.complete(3); + checkCompletedWithWrappedCFException(h); + + f = new CompletableFuture(); + g = new CompletableFuture(); + g.completeExceptionally(new CFException()); + h = f.thenCombineAsync(g, subtract, e); + checkIncomplete(h); + f.complete(3); + checkCompletedWithWrappedCFException(h); + + assertEquals(0, e.count.get()); } /** @@ -1935,27 +2068,51 @@ public class CompletableFutureTest exten CompletableFuture g = f.thenCombineAsync(f2, r, new ThreadExecutor()); f.complete(one); checkIncomplete(g); + assertFalse(r.ran); f2.complete(two); checkCompletedWithWrappedCFException(g); + assertTrue(r.ran); } /** * thenCombineAsync result completes exceptionally if either source cancelled */ public void testThenCombineAsync4E() { - CompletableFuture f = new CompletableFuture(); - CompletableFuture f2 = new CompletableFuture(); - CompletableFuture g = f.thenCombineAsync(f2, add, new ThreadExecutor()); + CompletableFuture f, g, h; + ThreadExecutor e = new ThreadExecutor(); + + f = new CompletableFuture(); + g = new CompletableFuture(); + h = f.thenCombineAsync(g, subtract, e); assertTrue(f.cancel(true)); - f2.complete(two); - checkCompletedWithWrappedCancellationException(g); + checkIncomplete(h); + g.complete(1); + checkCompletedWithWrappedCancellationException(h); f = new CompletableFuture(); - f2 = new CompletableFuture(); - g = f.thenCombineAsync(f2, add, new ThreadExecutor()); - f.complete(one); - assertTrue(f2.cancel(true)); - checkCompletedWithWrappedCancellationException(g); + g = new CompletableFuture(); + h = f.thenCombineAsync(g, subtract, e); + assertTrue(g.cancel(true)); + checkIncomplete(h); + f.complete(3); + checkCompletedWithWrappedCancellationException(h); + + f = new CompletableFuture(); + g = new CompletableFuture(); + assertTrue(g.cancel(true)); + h = f.thenCombineAsync(g, subtract, e); + checkIncomplete(h); + f.complete(3); + checkCompletedWithWrappedCancellationException(h); + + f = new CompletableFuture(); + g = new CompletableFuture(); + assertTrue(f.cancel(true)); + assertTrue(g.cancel(true)); + h = f.thenCombineAsync(g, subtract, e); + checkCompletedWithWrappedCancellationException(h); + + assertEquals(0, e.count.get()); } /**