--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/02 02:19:23 1.42 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/02 06:07:34 1.45 @@ -339,7 +339,6 @@ public class CompletableFutureTest exten static final class IncAction implements Consumer { int invocationCount = 0; Integer value; - public boolean ran() { return invocationCount == 1; } public void accept(Integer x) { invocationCount++; value = inc(x); @@ -348,7 +347,6 @@ public class CompletableFutureTest exten 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); @@ -358,7 +356,6 @@ public class CompletableFutureTest exten 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) { invocationCount++; value = subtract(x, y); @@ -368,7 +365,6 @@ public class CompletableFutureTest exten 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 value = subtract(x, y); @@ -376,54 +372,71 @@ public class CompletableFutureTest exten } static final class Noop implements Runnable { int invocationCount = 0; - boolean ran; public void run() { invocationCount++; - ran = true; } } static final class FailingSupplier implements Supplier { - boolean ran; - public Integer get() { ran = true; throw new CFException(); } + int invocationCount = 0; + public Integer get() { + invocationCount++; + throw new CFException(); + } } static final class FailingConsumer implements Consumer { - boolean ran; - public void accept(Integer x) { ran = true; throw new CFException(); } + int invocationCount = 0; + public void accept(Integer x) { + invocationCount++; + throw new CFException(); + } } static final class FailingBiConsumer implements BiConsumer { - boolean ran; - public void accept(Integer x, Integer y) { ran = true; throw new CFException(); } + int invocationCount = 0; + public void accept(Integer x, Integer y) { + invocationCount++; + throw new CFException(); + } } static final class FailingFunction implements Function { - boolean ran; - public Integer apply(Integer x) { ran = true; throw new CFException(); } + int invocationCount = 0; + public Integer apply(Integer x) { + invocationCount++; + throw new CFException(); + } } static final class FailingBiFunction implements BiFunction { - boolean ran; - public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); } + int invocationCount = 0; + public Integer apply(Integer x, Integer y) { + invocationCount++; + throw new CFException(); + } } static final class FailingNoop implements Runnable { - boolean ran; - public void run() { ran = true; throw new CFException(); } + int invocationCount = 0; + public void run() { + invocationCount++; + throw new CFException(); + } } static final class CompletableFutureInc implements Function> { - boolean ran; + int invocationCount = 0; public CompletableFuture apply(Integer x) { - ran = true; + invocationCount++; CompletableFuture f = new CompletableFuture<>(); - f.complete(Integer.valueOf(x.intValue() + 1)); + f.complete(inc(x)); return f; } } static final class FailingCompletableFutureFunction implements Function> { - boolean ran; + int invocationCount = 0; public CompletableFuture apply(Integer x) { - ran = true; throw new CFException(); + invocationCount++; + throw new CFException(); } } @@ -442,9 +455,9 @@ public class CompletableFutureTest exten } static final class IntegerHandler implements BiFunction { - boolean ran; + int invocationCount = 0; public Integer apply(Integer x, Throwable t) { - ran = true; + invocationCount++; return (t == null) ? two : three; } } @@ -501,20 +514,6 @@ public class CompletableFutureTest exten } }, -// /** Experimental way to do more testing */ -// REVERSE_DEFAULT { -// public CompletableFuture runAfterBoth -// (CompletableFuture f, CompletableFuture g, Runnable a) { -// return g.runAfterBoth(f, a); -// } -// public CompletableFuture thenAcceptBoth -// (CompletableFuture f, -// CompletionStage g, -// BiConsumer a) { -// return DEFAULT.thenAcceptBoth(f, g, a); -// } -// }, - DEFAULT_ASYNC { public CompletableFuture runAfterBoth (CompletableFuture f, CompletableFuture g, Runnable a) { @@ -562,19 +561,6 @@ public class CompletableFutureTest exten } }, -// REVERSE_DEFAULT_ASYNC { -// public CompletableFuture runAfterBoth -// (CompletableFuture f, CompletableFuture g, Runnable a) { -// return f.runAfterBothAsync(g, a); -// } -// public CompletableFuture thenAcceptBoth -// (CompletableFuture f, -// CompletionStage g, -// BiConsumer a) { -// return DEFAULT_ASYNC.thenAcceptBoth(f, g, a); -// } -// }, - EXECUTOR { public CompletableFuture runAfterBoth (CompletableFuture f, CompletableFuture g, Runnable a) { @@ -683,27 +669,28 @@ public class CompletableFutureTest exten f = new CompletableFuture<>(); f.completeExceptionally(new CFException()); g = f.handle(r = new IntegerHandler()); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); + assertEquals(1, r.invocationCount); checkCompletedNormally(g, three); f = new CompletableFuture<>(); g = f.handle(r = new IntegerHandler()); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); f.completeExceptionally(new CFException()); checkCompletedNormally(g, three); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); f = new CompletableFuture<>(); f.complete(one); g = f.handle(r = new IntegerHandler()); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); checkCompletedNormally(g, two); f = new CompletableFuture<>(); g = f.handle(r = new IntegerHandler()); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); f.complete(one); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); checkCompletedNormally(g, two); } @@ -714,7 +701,7 @@ public class CompletableFutureTest exten Noop r = new Noop(); CompletableFuture f = CompletableFuture.runAsync(r); assertNull(f.join()); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); checkCompletedNormally(f, null); } @@ -726,7 +713,7 @@ public class CompletableFutureTest exten ThreadExecutor exec = new ThreadExecutor(); CompletableFuture f = CompletableFuture.runAsync(r, exec); assertNull(f.join()); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); checkCompletedNormally(f, null); assertEquals(1, exec.count.get()); } @@ -738,7 +725,7 @@ public class CompletableFutureTest exten FailingNoop r = new FailingNoop(); CompletableFuture f = CompletableFuture.runAsync(r); checkCompletedWithWrappedCFException(f); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); } /** @@ -768,7 +755,7 @@ public class CompletableFutureTest exten FailingSupplier r = new FailingSupplier(); CompletableFuture f = CompletableFuture.supplyAsync(r); checkCompletedWithWrappedCFException(f); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); } // seq completion methods @@ -785,13 +772,13 @@ public class CompletableFutureTest exten g = f.thenRun(r = new Noop()); f.complete(null); checkCompletedNormally(g, null); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); f = new CompletableFuture<>(); f.complete(null); g = f.thenRun(r = new Noop()); checkCompletedNormally(g, null); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); } /** @@ -807,13 +794,13 @@ public class CompletableFutureTest exten g = f.thenRun(r = new Noop()); f.completeExceptionally(new CFException()); checkCompletedWithWrappedCFException(g); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); f = new CompletableFuture<>(); f.completeExceptionally(new CFException()); g = f.thenRun(r = new Noop()); checkCompletedWithWrappedCFException(g); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); } /** @@ -928,7 +915,7 @@ public class CompletableFutureTest exten CompletableFuture g = f.thenAccept(r); f.complete(one); checkCompletedWithWrappedCFException(g); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); } /** @@ -947,6 +934,8 @@ public class CompletableFutureTest exten * of sources */ public void testThenCombine_normalCompletion1() { + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) for (Integer v2 : new Integer[] { 2, null }) { @@ -954,79 +943,25 @@ 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, r); - - f.complete(v1); - checkIncomplete(h); - assertEquals(r.invocationCount, 0); - g.complete(v2); - - checkCompletedNormally(h, subtract(v1, v2)); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - assertEquals(r.invocationCount, 1); - } - } - - public void testThenCombine_normalCompletion2() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) { - - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(); - final CompletableFuture h = m.thenCombine(f, g, r); - - g.complete(v2); - checkIncomplete(h); - assertEquals(r.invocationCount, 0); - f.complete(v1); - - checkCompletedNormally(h, subtract(v1, v2)); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - assertEquals(r.invocationCount, 1); - } - } - - public void testThenCombine_normalCompletion3() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) { - - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(); - - g.complete(v2); - f.complete(v1); - final CompletableFuture h = m.thenCombine(f, g, r); - - checkCompletedNormally(h, subtract(v1, v2)); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - assertEquals(r.invocationCount, 1); - } - } - - public void testThenCombine_normalCompletion4() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) { - - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(); + CompletableFuture h = null; + if (createIncomplete) h = m.thenCombine(f, g, r); - f.complete(v1); - g.complete(v2); - final CompletableFuture h = m.thenCombine(f, g, r); + if (fFirst) + f.complete(v1); + else + g.complete(v2); + if (createIncomplete) checkIncomplete(h); + assertEquals(0, r.invocationCount); + if (!fFirst) + f.complete(v1); + else + g.complete(v2); + if (!createIncomplete) h = m.thenCombine(f, g, r); checkCompletedNormally(h, subtract(v1, v2)); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } } @@ -1050,7 +985,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); } } @@ -1071,7 +1006,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(g, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); } } @@ -1091,7 +1026,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(g, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); } } @@ -1111,7 +1046,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); } } @@ -1178,7 +1113,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(f); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); } } @@ -1199,7 +1134,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(g); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); } } @@ -1219,7 +1154,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(g); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); } } @@ -1239,7 +1174,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(f); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); } } @@ -1260,7 +1195,7 @@ public class CompletableFutureTest exten f.complete(v1); checkIncomplete(h); - assertFalse(r.ran()); + assertEquals(0, r.invocationCount); g.complete(v2); checkCompletedNormally(h, null); @@ -1282,7 +1217,7 @@ public class CompletableFutureTest exten g.complete(v2); checkIncomplete(h); - assertFalse(r.ran()); + assertEquals(0, r.invocationCount); f.complete(v1); checkCompletedNormally(h, null); @@ -1352,7 +1287,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); } } @@ -1373,7 +1308,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(g, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); } } @@ -1393,7 +1328,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(g, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); } } @@ -1413,7 +1348,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(f, ex); - assertFalse(r.ran()); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); } } @@ -1480,7 +1415,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(f); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); } } @@ -1501,7 +1436,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(g); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); } } @@ -1521,7 +1456,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(g); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); } } @@ -1541,7 +1476,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(f); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); } } @@ -1562,11 +1497,11 @@ public class CompletableFutureTest exten f.complete(v1); checkIncomplete(h); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); g.complete(v2); checkCompletedNormally(h, null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); } @@ -1584,11 +1519,11 @@ public class CompletableFutureTest exten g.complete(v2); checkIncomplete(h); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); f.complete(v1); checkCompletedNormally(h, null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); } @@ -1608,7 +1543,7 @@ public class CompletableFutureTest exten final CompletableFuture h = m.runAfterBoth(f, g, r); checkCompletedNormally(h, null); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); } @@ -1628,7 +1563,7 @@ public class CompletableFutureTest exten final CompletableFuture h = m.runAfterBoth(f, g, r); checkCompletedNormally(h, null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); } @@ -1654,7 +1589,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); } } @@ -1675,7 +1610,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(g, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); } } @@ -1695,7 +1630,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(g, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); } } @@ -1715,7 +1650,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); } } @@ -1782,7 +1717,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(f); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); } } @@ -1803,7 +1738,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(g); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); } } @@ -1823,7 +1758,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(g); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); } } @@ -1843,7 +1778,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(f); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); } } @@ -1910,7 +1845,7 @@ public class CompletableFutureTest exten // unspecified behavior assertTrue(Objects.equals(h.join(), inc(v1)) || Objects.equals(h.join(), inc(v2))); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } } @@ -1932,7 +1867,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); g.complete(v1); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); checkCompletedWithWrappedCFException(f, ex); checkCompletedWithWrappedCFException(h, ex); @@ -1953,7 +1888,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); f.complete(v1); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(h, ex); @@ -1977,10 +1912,10 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), inc(v1)); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); } checkCompletedWithWrappedCFException(g, ex); @@ -2005,10 +1940,10 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), inc(v1)); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); } checkCompletedWithWrappedCFException(f, ex); @@ -2073,7 +2008,7 @@ public class CompletableFutureTest exten g.complete(v1); checkCancelled(f); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); checkCompletedWithWrappedCancellationException(h); } @@ -2094,7 +2029,7 @@ public class CompletableFutureTest exten f.complete(v1); checkCancelled(g); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); checkCompletedWithWrappedCancellationException(h); } @@ -2117,10 +2052,10 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), inc(v1)); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); } checkCancelled(g); @@ -2145,10 +2080,10 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), inc(v1)); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); } checkCancelled(f); @@ -2242,7 +2177,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); g.complete(v1); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); checkCompletedWithWrappedCFException(f, ex); checkCompletedWithWrappedCFException(h, ex); @@ -2263,7 +2198,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); f.complete(v1); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(h, ex); @@ -2287,11 +2222,11 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); assertEquals(inc(v1), r.value); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); } checkCompletedWithWrappedCFException(g, ex); @@ -2316,11 +2251,11 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); assertEquals(inc(v1), r.value); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); } checkCompletedWithWrappedCFException(f, ex); @@ -2385,7 +2320,7 @@ public class CompletableFutureTest exten g.complete(v1); checkCancelled(f); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); checkCompletedWithWrappedCancellationException(h); } @@ -2406,7 +2341,7 @@ public class CompletableFutureTest exten f.complete(v1); checkCancelled(g); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); checkCompletedWithWrappedCancellationException(h); } @@ -2429,11 +2364,11 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); assertEquals(inc(v1), r.value); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); } checkCancelled(g); @@ -2458,11 +2393,11 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); assertEquals(inc(v1), r.value); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); } checkCancelled(f); @@ -2486,13 +2421,13 @@ public class CompletableFutureTest exten f.complete(v1); checkCompletedNormally(h, null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); g.complete(v2); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); checkCompletedNormally(h, null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } } @@ -2508,13 +2443,13 @@ public class CompletableFutureTest exten g.complete(v2); checkCompletedNormally(h, null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); f.complete(v1); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); checkCompletedNormally(h, null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } } public void testRunAfterEither_normalCompletion3() { @@ -2533,7 +2468,7 @@ public class CompletableFutureTest exten checkCompletedNormally(h, null); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } } @@ -2555,7 +2490,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); g.complete(v1); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); checkCompletedWithWrappedCFException(f, ex); checkCompletedWithWrappedCFException(h, ex); @@ -2576,7 +2511,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); f.complete(v1); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(h, ex); @@ -2600,10 +2535,10 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); } checkCompletedWithWrappedCFException(g, ex); @@ -2628,10 +2563,10 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); } checkCompletedWithWrappedCFException(f, ex); @@ -2696,7 +2631,7 @@ public class CompletableFutureTest exten g.complete(v1); checkCancelled(f); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(g, v1); checkCompletedWithWrappedCancellationException(h); } @@ -2717,7 +2652,7 @@ public class CompletableFutureTest exten f.complete(v1); checkCancelled(g); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); checkCompletedNormally(f, v1); checkCompletedWithWrappedCancellationException(h); } @@ -2740,10 +2675,10 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); } checkCancelled(g); @@ -2768,10 +2703,10 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), null); - assertEquals(r.invocationCount, 1); + assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); - assertEquals(r.invocationCount, 0); + assertEquals(0, r.invocationCount); } checkCancelled(f); @@ -2782,76 +2717,122 @@ public class CompletableFutureTest exten /** * thenCompose result completes normally after normal completion of source */ - public void testThenCompose() { - CompletableFuture f, g; - CompletableFutureInc r; + public void testThenCompose_normalCompletion1() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { - f = new CompletableFuture<>(); - g = f.thenCompose(r = new CompletableFutureInc()); - f.complete(one); - checkCompletedNormally(g, two); - assertTrue(r.ran); + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFutureInc r = new CompletableFutureInc(); + final CompletableFuture g = f.thenCompose(r); + f.complete(v1); + checkCompletedNormally(g, inc(v1)); + checkCompletedNormally(f, v1); + assertEquals(1, r.invocationCount); + } + } - f = new CompletableFuture<>(); - f.complete(one); - g = f.thenCompose(r = new CompletableFutureInc()); - checkCompletedNormally(g, two); - assertTrue(r.ran); + public void testThenCompose_normalCompletion2() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFutureInc r = new CompletableFutureInc(); + f.complete(v1); + final CompletableFuture g = f.thenCompose(r); + checkCompletedNormally(g, inc(v1)); + checkCompletedNormally(f, v1); + assertEquals(1, r.invocationCount); + } } /** * thenCompose result completes exceptionally after exceptional * completion of source */ - public void testThenCompose2() { - CompletableFuture f, g; - CompletableFutureInc r; + public void testThenCompose_exceptionalCompletion1() { + for (ExecutionMode m : ExecutionMode.values()) { - f = new CompletableFuture<>(); - g = f.thenCompose(r = new CompletableFutureInc()); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); + final CFException ex = new CFException(); + final CompletableFutureInc r = new CompletableFutureInc(); + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = f.thenCompose(r); + f.completeExceptionally(ex); + checkCompletedWithWrappedCFException(g, ex); + checkCompletedWithWrappedCFException(f, ex); + } + } - f = new CompletableFuture<>(); - f.completeExceptionally(new CFException()); - g = f.thenCompose(r = new CompletableFutureInc()); - checkCompletedWithWrappedCFException(g); + public void testThenCompose_exceptionalCompletion2() { + for (ExecutionMode m : ExecutionMode.values()) { + + final CFException ex = new CFException(); + final CompletableFuture f = new CompletableFuture<>(); + f.completeExceptionally(ex); + final CompletableFutureInc r = new CompletableFutureInc(); + final CompletableFuture g = f.thenCompose(r); + checkCompletedWithWrappedCFException(g, ex); + checkCompletedWithWrappedCFException(f, ex); + } } /** * thenCompose result completes exceptionally if action does */ - public void testThenCompose3() { - CompletableFuture f, g; - FailingCompletableFutureFunction r; + public void testThenCompose_actionFailed1() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { - f = new CompletableFuture<>(); - g = f.thenCompose(r = new FailingCompletableFutureFunction()); - f.complete(one); + final CompletableFuture f = new CompletableFuture<>(); + final FailingCompletableFutureFunction r + = new FailingCompletableFutureFunction(); + final CompletableFuture g = f.thenCompose(r); + f.complete(v1); checkCompletedWithWrappedCFException(g); + checkCompletedNormally(f, v1); + } + } - f = new CompletableFuture<>(); - f.complete(one); - g = f.thenCompose(r = new FailingCompletableFutureFunction()); + public void testThenCompose_actionFailed2() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + f.complete(v1); + final FailingCompletableFutureFunction r + = new FailingCompletableFutureFunction(); + final CompletableFuture g = f.thenCompose(r); checkCompletedWithWrappedCFException(g); + checkCompletedNormally(f, v1); + } } /** * thenCompose result completes exceptionally if source cancelled */ - public void testThenCompose4() { - CompletableFuture f, g; - CompletableFutureInc r; + public void testThenCompose_sourceCancelled1() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { - f = new CompletableFuture<>(); - g = f.thenCompose(r = new CompletableFutureInc()); - assertTrue(f.cancel(true)); + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFutureInc r = new CompletableFutureInc(); + final CompletableFuture g = f.thenCompose(r); + assertTrue(f.cancel(mayInterruptIfRunning)); checkCompletedWithWrappedCancellationException(g); + checkCancelled(f); + } + } - f = new CompletableFuture<>(); - assertTrue(f.cancel(true)); - g = f.thenCompose(r = new CompletableFutureInc()); + public void testThenCompose_sourceCancelled2() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { + + final CompletableFuture f = new CompletableFuture<>(); + assertTrue(f.cancel(mayInterruptIfRunning)); + final CompletableFutureInc r = new CompletableFutureInc(); + final CompletableFuture g = f.thenCompose(r); checkCompletedWithWrappedCancellationException(g); + checkCancelled(f); + } } // asyncs @@ -3001,82 +2982,6 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(g); } - /** - * thenComposeAsync result completes normally after normal - * completion of source - */ - public void testThenComposeAsync() { - 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); - } - - /** - * thenComposeAsync result completes exceptionally after - * exceptional completion of source - */ - public void testThenComposeAsync2() { - 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, 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, 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); - } - // async with explicit executors /** @@ -3224,52 +3129,6 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(g); } - /** - * thenComposeAsync result completes normally after normal - * completion of source - */ - public void testThenComposeAsyncE() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFutureInc r = new CompletableFutureInc(); - CompletableFuture g = f.thenComposeAsync(r, new ThreadExecutor()); - f.complete(one); - checkCompletedNormally(g, two); - } - - /** - * thenComposeAsync result completes exceptionally after - * exceptional completion of source - */ - public void testThenComposeAsync2E() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFutureInc r = new CompletableFutureInc(); - CompletableFuture g = f.thenComposeAsync(r, new ThreadExecutor()); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenComposeAsync result completes exceptionally if action does - */ - public void testThenComposeAsync3E() { - CompletableFuture f = new CompletableFuture<>(); - FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); - CompletableFuture g = f.thenComposeAsync(r, new ThreadExecutor()); - f.complete(one); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenComposeAsync result completes exceptionally if source cancelled - */ - public void testThenComposeAsync4E() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFutureInc r = new CompletableFutureInc(); - CompletableFuture g = f.thenComposeAsync(r, new ThreadExecutor()); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - } - // other static methods /** @@ -3469,11 +3328,13 @@ public class CompletableFutureTest exten * source result. */ public void testWhenComplete_normalCompletion1() { + for (boolean createIncomplete : new boolean[] { true, false }) for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) { final AtomicInteger a = new AtomicInteger(); final CompletableFuture f = new CompletableFuture<>(); + if (!createIncomplete) f.complete(v1); final CompletableFuture g = m.whenComplete(f, (Integer x, Throwable t) -> { @@ -3481,7 +3342,7 @@ public class CompletableFutureTest exten threadAssertNull(t); a.getAndIncrement(); }); - f.complete(v1); + if (createIncomplete) f.complete(v1); checkCompletedNormally(f, v1); checkCompletedNormally(g, v1); assertEquals(a.get(), 1); @@ -3493,12 +3354,14 @@ public class CompletableFutureTest exten * source result. */ public void testWhenComplete_exceptionalCompletion() { + for (boolean createIncomplete : new boolean[] { true, false }) for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) { final AtomicInteger a = new AtomicInteger(); final CFException ex = new CFException(); final CompletableFuture f = new CompletableFuture<>(); + if (!createIncomplete) f.completeExceptionally(ex); final CompletableFuture g = m.whenComplete (f, (Integer x, Throwable t) -> { @@ -3506,7 +3369,7 @@ public class CompletableFutureTest exten threadAssertSame(t, ex); a.getAndIncrement(); }); - f.completeExceptionally(ex); + if (createIncomplete) f.completeExceptionally(ex); checkCompletedWithWrappedCFException(f, ex); checkCompletedWithWrappedCFException(g, ex); assertEquals(a.get(), 1); @@ -3518,11 +3381,13 @@ public class CompletableFutureTest exten * a normal completion, it completes exceptionally */ public void testWhenComplete_actionFailed() { + for (boolean createIncomplete : new boolean[] { true, false }) for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) { final CFException ex = new CFException(); final CompletableFuture f = new CompletableFuture<>(); + if (!createIncomplete) f.complete(v1); final CompletableFuture g = m.whenComplete (f, (Integer x, Throwable t) -> { @@ -3530,7 +3395,7 @@ public class CompletableFutureTest exten threadAssertNull(t); throw ex; }); - f.complete(v1); + if (createIncomplete) f.complete(v1); checkCompletedNormally(f, v1); checkCompletedWithWrappedCFException(g, ex); } @@ -3542,12 +3407,15 @@ public class CompletableFutureTest exten * exception takes precedence. */ public void testWhenComplete_actionFailedSourceFailed() { + for (boolean createIncomplete : new boolean[] { true, false }) for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) { final CFException ex1 = new CFException(); final CFException ex2 = new CFException(); final CompletableFuture f = new CompletableFuture<>(); + + if (!createIncomplete) f.completeExceptionally(ex1); final CompletableFuture g = m.whenComplete (f, (Integer x, Throwable t) -> { @@ -3555,7 +3423,8 @@ public class CompletableFutureTest exten threadAssertNull(x); throw ex2; }); - f.completeExceptionally(ex1); + if (createIncomplete) f.completeExceptionally(ex1); + checkCompletedWithWrappedCFException(f, ex1); checkCompletedWithWrappedCFException(g, ex1); } @@ -3571,35 +3440,35 @@ public class CompletableFutureTest exten f = new CompletableFuture<>(); g = f.handleAsync(r = new IntegerHandler()); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); f.completeExceptionally(new CFException()); checkCompletedWithWrappedCFException(f); checkCompletedNormally(g, three); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); f = new CompletableFuture<>(); g = f.handleAsync(r = new IntegerHandler()); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); f.completeExceptionally(new CFException()); checkCompletedWithWrappedCFException(f); checkCompletedNormally(g, three); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); f = new CompletableFuture<>(); g = f.handleAsync(r = new IntegerHandler()); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); f.complete(one); checkCompletedNormally(f, one); checkCompletedNormally(g, two); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); f = new CompletableFuture<>(); g = f.handleAsync(r = new IntegerHandler()); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); f.complete(one); checkCompletedNormally(f, one); checkCompletedNormally(g, two); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); } /** @@ -3614,35 +3483,35 @@ public class CompletableFutureTest exten f = new CompletableFuture<>(); g = f.handleAsync(r = new IntegerHandler(), exec); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); f.completeExceptionally(new CFException()); checkCompletedWithWrappedCFException(f); checkCompletedNormally(g, three); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); f = new CompletableFuture<>(); g = f.handleAsync(r = new IntegerHandler(), exec); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); f.completeExceptionally(new CFException()); checkCompletedWithWrappedCFException(f); checkCompletedNormally(g, three); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); f = new CompletableFuture<>(); g = f.handleAsync(r = new IntegerHandler(), exec); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); f.complete(one); checkCompletedNormally(f, one); checkCompletedNormally(g, two); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); f = new CompletableFuture<>(); g = f.handleAsync(r = new IntegerHandler(), exec); - assertFalse(r.ran); + assertEquals(0, r.invocationCount); f.complete(one); checkCompletedNormally(f, one); checkCompletedNormally(g, two); - assertTrue(r.ran); + assertEquals(1, r.invocationCount); } }