--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/02 00:46:52 1.39 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/02 04:13:54 1.43 @@ -375,8 +375,12 @@ public class CompletableFutureTest exten } } static final class Noop implements Runnable { + int invocationCount = 0; boolean ran; - public void run() { ran = true; } + public void run() { + invocationCount++; + ran = true; + } } static final class FailingSupplier implements Supplier { @@ -943,6 +947,8 @@ public class CompletableFutureTest exten * of sources */ public void testThenCombine_normalCompletion1() { + for (boolean createdIncomplete : 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 }) { @@ -950,75 +956,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); - assertFalse(r.ran()); - g.complete(v2); - - checkCompletedNormally(h, subtract(v1, v2)); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - } - } - - 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); - assertFalse(r.ran()); - f.complete(v1); - - checkCompletedNormally(h, subtract(v1, v2)); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - } - } - - 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(); + CompletableFuture h = null; + if (createdIncomplete) h = m.thenCombine(f, g, r); - 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); - } - } - - 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(); - - f.complete(v1); - g.complete(v2); - final CompletableFuture h = m.thenCombine(f, g, r); + if (fFirst) + f.complete(v1); + else + g.complete(v2); + if (createdIncomplete) checkIncomplete(h); + assertEquals(r.invocationCount, 0); + if (!fFirst) + f.complete(v1); + else + g.complete(v2); + if (!createdIncomplete) h = m.thenCombine(f, g, r); checkCompletedNormally(h, subtract(v1, v2)); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); + assertEquals(r.invocationCount, 1); } } @@ -1042,7 +998,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(f, ex); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); } } @@ -1063,7 +1019,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(g, ex); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); } } @@ -1083,7 +1039,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(g, ex); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); } } @@ -1103,7 +1059,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(f, ex); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); } } @@ -1170,7 +1126,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(f); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); } } @@ -1191,7 +1147,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(g); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); } } @@ -1211,7 +1167,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(g); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); } } @@ -1231,7 +1187,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(f); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); } } @@ -1344,7 +1300,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(f, ex); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); } } @@ -1365,7 +1321,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(g, ex); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); } } @@ -1385,7 +1341,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(g, ex); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); } } @@ -1472,7 +1428,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(f); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); } } @@ -1493,7 +1449,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(g); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); } } @@ -1513,7 +1469,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(g); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); } } @@ -1533,7 +1489,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(f); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); } } @@ -1558,7 +1514,7 @@ public class CompletableFutureTest exten g.complete(v2); checkCompletedNormally(h, null); - assertTrue(r.ran); + assertEquals(r.invocationCount, 1); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); } @@ -1580,7 +1536,7 @@ public class CompletableFutureTest exten f.complete(v1); checkCompletedNormally(h, null); - assertTrue(r.ran); + assertEquals(r.invocationCount, 1); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); } @@ -1620,7 +1576,7 @@ public class CompletableFutureTest exten final CompletableFuture h = m.runAfterBoth(f, g, r); checkCompletedNormally(h, null); - assertTrue(r.ran); + assertEquals(r.invocationCount, 1); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); } @@ -1646,7 +1602,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(f, ex); - assertFalse(r.ran); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); } } @@ -1667,7 +1623,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(g, ex); - assertFalse(r.ran); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); } } @@ -1687,7 +1643,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(g, ex); - assertFalse(r.ran); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); } } @@ -1707,7 +1663,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); checkCompletedWithWrappedCFException(f, ex); - assertFalse(r.ran); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); } } @@ -1774,7 +1730,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(f); - assertFalse(r.ran); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); } } @@ -1795,7 +1751,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(g); - assertFalse(r.ran); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); } } @@ -1815,7 +1771,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(g); - assertFalse(r.ran); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); } } @@ -1835,7 +1791,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(f); - assertFalse(r.ran); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); } } @@ -1902,6 +1858,7 @@ public class CompletableFutureTest exten // unspecified behavior assertTrue(Objects.equals(h.join(), inc(v1)) || Objects.equals(h.join(), inc(v2))); + assertEquals(r.invocationCount, 1); } } @@ -1923,7 +1880,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); g.complete(v1); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); checkCompletedWithWrappedCFException(f, ex); checkCompletedWithWrappedCFException(h, ex); @@ -1944,7 +1901,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); f.complete(v1); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(h, ex); @@ -1968,10 +1925,10 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), inc(v1)); - assertTrue(r.ran()); + assertEquals(r.invocationCount, 1); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); } checkCompletedWithWrappedCFException(g, ex); @@ -1996,14 +1953,13 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), inc(v1)); - assertTrue(r.ran()); + assertEquals(r.invocationCount, 1); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); } checkCompletedWithWrappedCFException(f, ex); - assertFalse(r.ran()); checkCompletedNormally(g, v1); } } @@ -2065,7 +2021,7 @@ public class CompletableFutureTest exten g.complete(v1); checkCancelled(f); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(g, v1); checkCompletedWithWrappedCancellationException(h); } @@ -2086,7 +2042,7 @@ public class CompletableFutureTest exten f.complete(v1); checkCancelled(g); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); checkCompletedNormally(f, v1); checkCompletedWithWrappedCancellationException(h); } @@ -2109,10 +2065,10 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), inc(v1)); - assertTrue(r.ran()); + assertEquals(r.invocationCount, 1); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); } checkCancelled(g); @@ -2137,10 +2093,10 @@ public class CompletableFutureTest exten Integer v; try { assertEquals(h.join(), inc(v1)); - assertTrue(r.ran()); + assertEquals(r.invocationCount, 1); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); - assertFalse(r.ran()); + assertEquals(r.invocationCount, 0); } checkCancelled(f); @@ -2152,146 +2108,623 @@ public class CompletableFutureTest exten * acceptEither result completes normally after normal completion * of either source */ - public void testAcceptEither() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.acceptEither(f2, r); - f.complete(one); - checkCompletedNormally(g, null); - f2.complete(one); - checkCompletedNormally(g, null); - assertEquals(r.value, (Integer) 2); + public void testAcceptEither_normalCompletion1() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) + for (Integer v2 : new Integer[] { 2, null }) { - r = new IncAction(); - f = new CompletableFuture<>(); - f.complete(one); - f2 = new CompletableFuture<>(); - g = f.acceptEither(f2, r); - checkCompletedNormally(g, null); - assertEquals(r.value, (Integer) 2); + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final IncAction r = new IncAction(); + final CompletableFuture h = m.acceptEither(f, g, r); + + f.complete(v1); + checkCompletedNormally(h, null); + assertEquals(r.value, inc(v1)); + g.complete(v2); + + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + checkCompletedNormally(h, null); + } + } + + public void testAcceptEither_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 IncAction r = new IncAction(); + final CompletableFuture h = m.acceptEither(f, g, r); + + g.complete(v2); + checkCompletedNormally(h, null); + assertEquals(r.value, inc(v2)); + f.complete(v1); + + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + checkCompletedNormally(h, null); + } + } + public void testAcceptEither_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 IncAction r = new IncAction(); + + f.complete(v1); + g.complete(v2); + final CompletableFuture h = m.acceptEither(f, g, r); + + checkCompletedNormally(h, null); + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + + // unspecified behavior + assertTrue(Objects.equals(r.value, inc(v1)) || + Objects.equals(r.value, inc(v2))); + } } /** * acceptEither result completes exceptionally after exceptional * completion of either source */ - public void testAcceptEither2() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.acceptEither(f2, r); - f.completeExceptionally(new CFException()); - f2.complete(one); - checkCompletedWithWrappedCFException(g); + public void testAcceptEither_exceptionalCompletion1() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { - r = new IncAction(); - f = new CompletableFuture<>(); - f2 = new CompletableFuture<>(); - f2.completeExceptionally(new CFException()); - g = f.acceptEither(f2, r); - checkCompletedWithWrappedCFException(g); + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final IncAction r = new IncAction(); + final CompletableFuture h = m.acceptEither(f, g, r); + final CFException ex = new CFException(); + + f.completeExceptionally(ex); + checkCompletedWithWrappedCFException(h, ex); + g.complete(v1); + + assertEquals(r.invocationCount, 0); + checkCompletedNormally(g, v1); + checkCompletedWithWrappedCFException(f, ex); + checkCompletedWithWrappedCFException(h, ex); + } + } + + public void testAcceptEither_exceptionalCompletion2() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final IncAction r = new IncAction(); + final CompletableFuture h = m.acceptEither(f, g, r); + final CFException ex = new CFException(); + + g.completeExceptionally(ex); + checkCompletedWithWrappedCFException(h, ex); + f.complete(v1); + + assertEquals(r.invocationCount, 0); + checkCompletedNormally(f, v1); + checkCompletedWithWrappedCFException(g, ex); + checkCompletedWithWrappedCFException(h, ex); + } + } + + public void testAcceptEither_exceptionalCompletion3() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final IncAction r = new IncAction(); + final CFException ex = new CFException(); + + g.completeExceptionally(ex); + f.complete(v1); + final CompletableFuture h = m.acceptEither(f, g, r); + + // unspecified behavior + Integer v; + try { + assertEquals(h.join(), null); + assertEquals(r.invocationCount, 1); + assertEquals(inc(v1), r.value); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h, ex); + assertEquals(r.invocationCount, 0); + } + + checkCompletedWithWrappedCFException(g, ex); + checkCompletedNormally(f, v1); + } + } + + public void testAcceptEither_exceptionalCompletion4() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final IncAction r = new IncAction(); + final CFException ex = new CFException(); + + f.completeExceptionally(ex); + g.complete(v1); + final CompletableFuture h = m.acceptEither(f, g, r); + + // unspecified behavior + Integer v; + try { + assertEquals(h.join(), null); + assertEquals(r.invocationCount, 1); + assertEquals(inc(v1), r.value); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h, ex); + assertEquals(r.invocationCount, 0); + } + + checkCompletedWithWrappedCFException(f, ex); + checkCompletedNormally(g, v1); + } } /** * acceptEither result completes exceptionally if action does */ - public void testAcceptEither3() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - FailingConsumer r = new FailingConsumer(); - CompletableFuture g = f.acceptEither(f2, r); - f2.complete(two); - checkCompletedWithWrappedCFException(g); + public void testAcceptEither_actionFailed1() { + 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 FailingConsumer r = new FailingConsumer(); + final CompletableFuture h = m.acceptEither(f, g, r); + + f.complete(v1); + checkCompletedWithWrappedCFException(h); + g.complete(v2); + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + } + } + + public void testAcceptEither_actionFailed2() { + 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 FailingConsumer r = new FailingConsumer(); + final CompletableFuture h = m.acceptEither(f, g, r); + + g.complete(v2); + checkCompletedWithWrappedCFException(h); + f.complete(v1); + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + } } /** * acceptEither result completes exceptionally if either source cancelled */ - public void testAcceptEither4() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.acceptEither(f2, r); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - f = new CompletableFuture<>(); - f2 = new CompletableFuture<>(); - assertTrue(f2.cancel(true)); - checkCompletedWithWrappedCancellationException(g); + public void testAcceptEither_sourceCancelled1() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final IncAction r = new IncAction(); + final CompletableFuture h = m.acceptEither(f, g, r); + + assertTrue(f.cancel(mayInterruptIfRunning)); + checkCompletedWithWrappedCancellationException(h); + g.complete(v1); + + checkCancelled(f); + assertEquals(r.invocationCount, 0); + checkCompletedNormally(g, v1); + checkCompletedWithWrappedCancellationException(h); + } + } + + public void testAcceptEither_sourceCancelled2() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final IncAction r = new IncAction(); + final CompletableFuture h = m.acceptEither(f, g, r); + + assertTrue(g.cancel(mayInterruptIfRunning)); + checkCompletedWithWrappedCancellationException(h); + f.complete(v1); + + checkCancelled(g); + assertEquals(r.invocationCount, 0); + checkCompletedNormally(f, v1); + checkCompletedWithWrappedCancellationException(h); + } + } + + public void testAcceptEither_sourceCancelled3() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final IncAction r = new IncAction(); + + assertTrue(g.cancel(mayInterruptIfRunning)); + f.complete(v1); + final CompletableFuture h = m.acceptEither(f, g, r); + + // unspecified behavior + Integer v; + try { + assertEquals(h.join(), null); + assertEquals(r.invocationCount, 1); + assertEquals(inc(v1), r.value); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h); + assertEquals(r.invocationCount, 0); + } + + checkCancelled(g); + checkCompletedNormally(f, v1); + } + } + + public void testAcceptEither_sourceCancelled4() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final IncAction r = new IncAction(); + + assertTrue(f.cancel(mayInterruptIfRunning)); + g.complete(v1); + final CompletableFuture h = m.acceptEither(f, g, r); + + // unspecified behavior + Integer v; + try { + assertEquals(h.join(), null); + assertEquals(r.invocationCount, 1); + assertEquals(inc(v1), r.value); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h); + assertEquals(r.invocationCount, 0); + } + + checkCancelled(f); + checkCompletedNormally(g, v1); + } } /** * runAfterEither result completes normally after normal completion * of either source */ - public void testRunAfterEither() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.runAfterEither(f2, r); - f.complete(one); - checkCompletedNormally(g, null); - f2.complete(one); - checkCompletedNormally(g, null); - assertTrue(r.ran); + public void testRunAfterEither_normalCompletion1() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) + for (Integer v2 : new Integer[] { 2, null }) { - r = new Noop(); - f = new CompletableFuture<>(); - f.complete(one); - f2 = new CompletableFuture<>(); - g = f.runAfterEither(f2, r); - checkCompletedNormally(g, null); - assertTrue(r.ran); + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final Noop r = new Noop(); + final CompletableFuture h = m.runAfterEither(f, g, r); + + f.complete(v1); + checkCompletedNormally(h, null); + assertEquals(r.invocationCount, 1); + g.complete(v2); + + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + checkCompletedNormally(h, null); + assertEquals(r.invocationCount, 1); + } + } + + public void testRunAfterEither_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 Noop r = new Noop(); + final CompletableFuture h = m.runAfterEither(f, g, r); + + g.complete(v2); + checkCompletedNormally(h, null); + assertEquals(r.invocationCount, 1); + f.complete(v1); + + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + checkCompletedNormally(h, null); + assertEquals(r.invocationCount, 1); + } + } + public void testRunAfterEither_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 Noop r = new Noop(); + + f.complete(v1); + g.complete(v2); + final CompletableFuture h = m.runAfterEither(f, g, r); + + checkCompletedNormally(h, null); + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + assertEquals(r.invocationCount, 1); + } } /** * runAfterEither result completes exceptionally after exceptional * completion of either source */ - public void testRunAfterEither2() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.runAfterEither(f2, r); - f.completeExceptionally(new CFException()); - f2.complete(one); - checkCompletedWithWrappedCFException(g); + public void testRunAfterEither_exceptionalCompletion1() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { - r = new Noop(); - f = new CompletableFuture<>(); - f2 = new CompletableFuture<>(); - f2.completeExceptionally(new CFException()); - g = f.runAfterEither(f2, r); - checkCompletedWithWrappedCFException(g); + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final Noop r = new Noop(); + final CompletableFuture h = m.runAfterEither(f, g, r); + final CFException ex = new CFException(); + + f.completeExceptionally(ex); + checkCompletedWithWrappedCFException(h, ex); + g.complete(v1); + + assertEquals(r.invocationCount, 0); + checkCompletedNormally(g, v1); + checkCompletedWithWrappedCFException(f, ex); + checkCompletedWithWrappedCFException(h, ex); + } + } + + public void testRunAfterEither_exceptionalCompletion2() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final Noop r = new Noop(); + final CompletableFuture h = m.runAfterEither(f, g, r); + final CFException ex = new CFException(); + + g.completeExceptionally(ex); + checkCompletedWithWrappedCFException(h, ex); + f.complete(v1); + + assertEquals(r.invocationCount, 0); + checkCompletedNormally(f, v1); + checkCompletedWithWrappedCFException(g, ex); + checkCompletedWithWrappedCFException(h, ex); + } + } + + public void testRunAfterEither_exceptionalCompletion3() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final Noop r = new Noop(); + final CFException ex = new CFException(); + + g.completeExceptionally(ex); + f.complete(v1); + final CompletableFuture h = m.runAfterEither(f, g, r); + + // unspecified behavior + Integer v; + try { + assertEquals(h.join(), null); + assertEquals(r.invocationCount, 1); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h, ex); + assertEquals(r.invocationCount, 0); + } + + checkCompletedWithWrappedCFException(g, ex); + checkCompletedNormally(f, v1); + } + } + + public void testRunAfterEither_exceptionalCompletion4() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final Noop r = new Noop(); + final CFException ex = new CFException(); + + f.completeExceptionally(ex); + g.complete(v1); + final CompletableFuture h = m.runAfterEither(f, g, r); + + // unspecified behavior + Integer v; + try { + assertEquals(h.join(), null); + assertEquals(r.invocationCount, 1); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h, ex); + assertEquals(r.invocationCount, 0); + } + + checkCompletedWithWrappedCFException(f, ex); + checkCompletedNormally(g, v1); + } } /** * runAfterEither result completes exceptionally if action does */ - public void testRunAfterEither3() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - FailingNoop r = new FailingNoop(); - CompletableFuture g = f.runAfterEither(f2, r); - f2.complete(two); - checkCompletedWithWrappedCFException(g); + public void testRunAfterEither_actionFailed1() { + 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 FailingNoop r = new FailingNoop(); + final CompletableFuture h = m.runAfterEither(f, g, r); + + f.complete(v1); + checkCompletedWithWrappedCFException(h); + g.complete(v2); + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + } + } + + public void testRunAfterEither_actionFailed2() { + 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 FailingNoop r = new FailingNoop(); + final CompletableFuture h = m.runAfterEither(f, g, r); + + g.complete(v2); + checkCompletedWithWrappedCFException(h); + f.complete(v1); + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + } } /** * runAfterEither result completes exceptionally if either source cancelled */ - public void testRunAfterEither4() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.runAfterEither(f2, r); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - f = new CompletableFuture<>(); - f2 = new CompletableFuture<>(); - assertTrue(f2.cancel(true)); - checkCompletedWithWrappedCancellationException(g); + public void testRunAfterEither_sourceCancelled1() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final Noop r = new Noop(); + final CompletableFuture h = m.runAfterEither(f, g, r); + + assertTrue(f.cancel(mayInterruptIfRunning)); + checkCompletedWithWrappedCancellationException(h); + g.complete(v1); + + checkCancelled(f); + assertEquals(r.invocationCount, 0); + checkCompletedNormally(g, v1); + checkCompletedWithWrappedCancellationException(h); + } + } + + public void testRunAfterEither_sourceCancelled2() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final Noop r = new Noop(); + final CompletableFuture h = m.runAfterEither(f, g, r); + + assertTrue(g.cancel(mayInterruptIfRunning)); + checkCompletedWithWrappedCancellationException(h); + f.complete(v1); + + checkCancelled(g); + assertEquals(r.invocationCount, 0); + checkCompletedNormally(f, v1); + checkCompletedWithWrappedCancellationException(h); + } + } + + public void testRunAfterEither_sourceCancelled3() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final Noop r = new Noop(); + + assertTrue(g.cancel(mayInterruptIfRunning)); + f.complete(v1); + final CompletableFuture h = m.runAfterEither(f, g, r); + + // unspecified behavior + Integer v; + try { + assertEquals(h.join(), null); + assertEquals(r.invocationCount, 1); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h); + assertEquals(r.invocationCount, 0); + } + + checkCancelled(g); + checkCompletedNormally(f, v1); + } + } + + public void testRunAfterEither_sourceCancelled4() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) { + + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final Noop r = new Noop(); + + assertTrue(f.cancel(mayInterruptIfRunning)); + g.complete(v1); + final CompletableFuture h = m.runAfterEither(f, g, r); + + // unspecified behavior + Integer v; + try { + assertEquals(h.join(), null); + assertEquals(r.invocationCount, 1); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h); + assertEquals(r.invocationCount, 0); + } + + checkCancelled(f); + checkCompletedNormally(g, v1); + } } /** @@ -2517,156 +2950,6 @@ public class CompletableFutureTest exten } /** - * acceptEitherAsync result completes normally after normal - * completion of sources - */ - public void testAcceptEitherAsync() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.acceptEitherAsync(f2, r); - f.complete(one); - checkCompletedNormally(g, null); - assertEquals(r.value, (Integer) 2); - - r = new IncAction(); - f = new CompletableFuture<>(); - f.complete(one); - f2 = new CompletableFuture<>(); - g = f.acceptEitherAsync(f2, r); - checkCompletedNormally(g, null); - assertEquals(r.value, (Integer) 2); - } - - /** - * acceptEitherAsync result completes exceptionally after exceptional - * completion of source - */ - public void testAcceptEitherAsync2() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.acceptEitherAsync(f2, r); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); - - r = new IncAction(); - f = new CompletableFuture<>(); - f2 = new CompletableFuture<>(); - f2.completeExceptionally(new CFException()); - g = f.acceptEitherAsync(f2, r); - f.complete(one); - checkCompletedWithWrappedCFException(g); - } - - /** - * acceptEitherAsync result completes exceptionally if action does - */ - public void testAcceptEitherAsync3() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - FailingConsumer r = new FailingConsumer(); - CompletableFuture g = f.acceptEitherAsync(f2, r); - f.complete(one); - checkCompletedWithWrappedCFException(g); - } - - /** - * acceptEitherAsync result completes exceptionally if either - * source cancelled - */ - public void testAcceptEitherAsync4() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.acceptEitherAsync(f2, r); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - - r = new IncAction(); - f = new CompletableFuture<>(); - f2 = new CompletableFuture<>(); - assertTrue(f2.cancel(true)); - g = f.acceptEitherAsync(f2, r); - checkCompletedWithWrappedCancellationException(g); - } - - /** - * runAfterEitherAsync result completes normally after normal - * completion of sources - */ - public void testRunAfterEitherAsync() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.runAfterEitherAsync(f2, r); - f.complete(one); - checkCompletedNormally(g, null); - assertTrue(r.ran); - - r = new Noop(); - f = new CompletableFuture<>(); - f.complete(one); - f2 = new CompletableFuture<>(); - g = f.runAfterEitherAsync(f2, r); - checkCompletedNormally(g, null); - assertTrue(r.ran); - } - - /** - * runAfterEitherAsync result completes exceptionally after exceptional - * completion of source - */ - public void testRunAfterEitherAsync2() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.runAfterEitherAsync(f2, r); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); - - r = new Noop(); - f = new CompletableFuture<>(); - f2 = new CompletableFuture<>(); - f2.completeExceptionally(new CFException()); - g = f.runAfterEitherAsync(f2, r); - f.complete(one); - checkCompletedWithWrappedCFException(g); - } - - /** - * runAfterEitherAsync result completes exceptionally if action does - */ - public void testRunAfterEitherAsync3() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - FailingNoop r = new FailingNoop(); - CompletableFuture g = f.runAfterEitherAsync(f2, r); - f.complete(one); - checkCompletedWithWrappedCFException(g); - } - - /** - * runAfterEitherAsync result completes exceptionally if either - * source cancelled - */ - public void testRunAfterEitherAsync4() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.runAfterEitherAsync(f2, r); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - - r = new Noop(); - f = new CompletableFuture<>(); - f2 = new CompletableFuture<>(); - assertTrue(f2.cancel(true)); - g = f.runAfterEitherAsync(f2, r); - checkCompletedWithWrappedCancellationException(g); - } - - /** * thenComposeAsync result completes normally after normal * completion of source */ @@ -2890,156 +3173,6 @@ public class CompletableFutureTest exten } /** - * acceptEitherAsync result completes normally after normal - * completion of sources - */ - public void testAcceptEitherAsyncE() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); - f.complete(one); - checkCompletedNormally(g, null); - assertEquals(r.value, (Integer) 2); - - r = new IncAction(); - f = new CompletableFuture<>(); - f.complete(one); - f2 = new CompletableFuture<>(); - g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); - checkCompletedNormally(g, null); - assertEquals(r.value, (Integer) 2); - } - - /** - * acceptEitherAsync result completes exceptionally after exceptional - * completion of source - */ - public void testAcceptEitherAsync2E() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); - - r = new IncAction(); - f = new CompletableFuture<>(); - f2 = new CompletableFuture<>(); - f2.completeExceptionally(new CFException()); - g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); - f.complete(one); - checkCompletedWithWrappedCFException(g); - } - - /** - * acceptEitherAsync result completes exceptionally if action does - */ - public void testAcceptEitherAsync3E() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - FailingConsumer r = new FailingConsumer(); - CompletableFuture g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); - f.complete(one); - checkCompletedWithWrappedCFException(g); - } - - /** - * acceptEitherAsync result completes exceptionally if either - * source cancelled - */ - public void testAcceptEitherAsync4E() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - - r = new IncAction(); - f = new CompletableFuture<>(); - f2 = new CompletableFuture<>(); - assertTrue(f2.cancel(true)); - g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); - checkCompletedWithWrappedCancellationException(g); - } - - /** - * runAfterEitherAsync result completes normally after normal - * completion of sources - */ - public void testRunAfterEitherAsyncE() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); - f.complete(one); - checkCompletedNormally(g, null); - assertTrue(r.ran); - - r = new Noop(); - f = new CompletableFuture<>(); - f.complete(one); - f2 = new CompletableFuture<>(); - g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); - checkCompletedNormally(g, null); - assertTrue(r.ran); - } - - /** - * runAfterEitherAsync result completes exceptionally after exceptional - * completion of source - */ - public void testRunAfterEitherAsync2E() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); - - r = new Noop(); - f = new CompletableFuture<>(); - f2 = new CompletableFuture<>(); - f2.completeExceptionally(new CFException()); - g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); - f.complete(one); - checkCompletedWithWrappedCFException(g); - } - - /** - * runAfterEitherAsync result completes exceptionally if action does - */ - public void testRunAfterEitherAsync3E() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - FailingNoop r = new FailingNoop(); - CompletableFuture g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); - f.complete(one); - checkCompletedWithWrappedCFException(g); - } - - /** - * runAfterEitherAsync result completes exceptionally if either - * source cancelled - */ - public void testRunAfterEitherAsync4E() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture f2 = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - - r = new Noop(); - f = new CompletableFuture<>(); - f2 = new CompletableFuture<>(); - assertTrue(f2.cancel(true)); - g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); - checkCompletedWithWrappedCancellationException(g); - } - - /** * thenComposeAsync result completes normally after normal * completion of source */ @@ -3283,137 +3416,97 @@ public class CompletableFutureTest exten * whenComplete action executes on normal completion, propagating * source result. */ - public void testWhenComplete1() { + public void testWhenComplete_normalCompletion1() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { + final AtomicInteger a = new AtomicInteger(); - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = - f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); - f.complete(three); - checkCompletedNormally(f, three); - checkCompletedNormally(g, three); + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = + m.whenComplete(f, + (Integer x, Throwable t) -> { + threadAssertSame(x, v1); + threadAssertNull(t); + a.getAndIncrement(); + }); + f.complete(v1); + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v1); assertEquals(a.get(), 1); + } } /** * whenComplete action executes on exceptional completion, propagating * source result. */ - public void testWhenComplete2() { + public void testWhenComplete_exceptionalCompletion() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { + final AtomicInteger a = new AtomicInteger(); - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = - f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); - f.completeExceptionally(new CFException()); - assertTrue(f.isCompletedExceptionally()); - assertTrue(g.isCompletedExceptionally()); + final CFException ex = new CFException(); + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = m.whenComplete + (f, + (Integer x, Throwable t) -> { + threadAssertNull(x); + threadAssertSame(t, ex); + a.getAndIncrement(); + }); + f.completeExceptionally(ex); + checkCompletedWithWrappedCFException(f, ex); + checkCompletedWithWrappedCFException(g, ex); assertEquals(a.get(), 1); + } } /** * If a whenComplete action throws an exception when triggered by * a normal completion, it completes exceptionally */ - public void testWhenComplete3() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = - f.whenComplete((Integer x, Throwable t) -> - { throw new CFException(); } ); - f.complete(three); - checkCompletedNormally(f, three); - assertTrue(g.isCompletedExceptionally()); - checkCompletedWithWrappedCFException(g); - } - - /** - * whenCompleteAsync action executes on normal completion, propagating - * source result. - */ - public void testWhenCompleteAsync1() { - final AtomicInteger a = new AtomicInteger(); - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = - f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); - f.complete(three); - checkCompletedNormally(f, three); - checkCompletedNormally(g, three); - assertEquals(a.get(), 1); - } - - /** - * whenCompleteAsync action executes on exceptional completion, propagating - * source result. - */ - public void testWhenCompleteAsync2() { - final AtomicInteger a = new AtomicInteger(); - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = - f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(f); - checkCompletedWithWrappedCFException(g); - } - - /** - * If a whenCompleteAsync action throws an exception when - * triggered by a normal completion, it completes exceptionally - */ - public void testWhenCompleteAsync3() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = - f.whenCompleteAsync((Integer x, Throwable t) -> - { throw new CFException(); } ); - f.complete(three); - checkCompletedNormally(f, three); - checkCompletedWithWrappedCFException(g); - } + public void testWhenComplete_actionFailed() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { - /** - * whenCompleteAsync action executes on normal completion, propagating - * source result. - */ - public void testWhenCompleteAsync1e() { - final AtomicInteger a = new AtomicInteger(); - ThreadExecutor exec = new ThreadExecutor(); - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = - f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), - exec); - f.complete(three); - checkCompletedNormally(f, three); - checkCompletedNormally(g, three); - assertEquals(a.get(), 1); + final CFException ex = new CFException(); + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = m.whenComplete + (f, + (Integer x, Throwable t) -> { + threadAssertSame(x, v1); + threadAssertNull(t); + throw ex; + }); + f.complete(v1); + checkCompletedNormally(f, v1); + checkCompletedWithWrappedCFException(g, ex); + } } /** - * whenCompleteAsync action executes on exceptional completion, propagating - * source result. + * If a whenComplete action throws an exception when triggered by + * a source completion that also throws an exception, the source + * exception takes precedence. */ - public void testWhenCompleteAsync2e() { - final AtomicInteger a = new AtomicInteger(); - ThreadExecutor exec = new ThreadExecutor(); - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = - f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), - exec); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(f); - checkCompletedWithWrappedCFException(g); - } + public void testWhenComplete_actionFailedSourceFailed() { + for (ExecutionMode m : ExecutionMode.values()) + for (Integer v1 : new Integer[] { 1, null }) { - /** - * If a whenCompleteAsync action throws an exception when triggered - * by a normal completion, it completes exceptionally - */ - public void testWhenCompleteAsync3e() { - ThreadExecutor exec = new ThreadExecutor(); - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = - f.whenCompleteAsync((Integer x, Throwable t) -> - { throw new CFException(); }, - exec); - f.complete(three); - checkCompletedNormally(f, three); - checkCompletedWithWrappedCFException(g); + final CFException ex1 = new CFException(); + final CFException ex2 = new CFException(); + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = m.whenComplete + (f, + (Integer x, Throwable t) -> { + threadAssertSame(t, ex1); + threadAssertNull(x); + throw ex2; + }); + f.completeExceptionally(ex1); + checkCompletedWithWrappedCFException(f, ex1); + checkCompletedWithWrappedCFException(g, ex1); + } } /**