--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/06 16:54:16 1.63 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/06 19:39:50 1.69 @@ -360,13 +360,13 @@ public class CompletableFutureTest exten return (x == null) ? null : x + 1; } - class IncAction extends CheckedIntegerAction + class NoopConsumer extends CheckedIntegerAction implements Consumer { - IncAction(ExecutionMode m) { super(m); } + NoopConsumer(ExecutionMode m) { super(m); } public void accept(Integer x) { invoked(); - value = inc(x); + value = x; } } @@ -1249,7 +1249,7 @@ public class CompletableFutureTest exten for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); - final IncAction r = new IncAction(m); + final NoopConsumer r = new NoopConsumer(m); if (!createIncomplete) f.complete(v1); final CompletableFuture g = m.thenAccept(f, r); if (createIncomplete) { @@ -1258,9 +1258,8 @@ public class CompletableFutureTest exten } checkCompletedNormally(g, null); + r.assertValue(v1); checkCompletedNormally(f, v1); - r.assertInvoked(); - r.assertValue(inc(v1)); }} /** @@ -1273,7 +1272,7 @@ public class CompletableFutureTest exten { final CFException ex = new CFException(); final CompletableFuture f = new CompletableFuture<>(); - final IncAction r = new IncAction(m); + final NoopConsumer r = new NoopConsumer(m); if (!createIncomplete) f.completeExceptionally(ex); final CompletableFuture g = m.thenAccept(f, r); if (createIncomplete) { @@ -1295,7 +1294,7 @@ public class CompletableFutureTest exten for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { final CompletableFuture f = new CompletableFuture<>(); - final IncAction r = new IncAction(m); + final NoopConsumer r = new NoopConsumer(m); if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); final CompletableFuture g = m.thenAccept(f, r); if (createIncomplete) { @@ -1766,14 +1765,14 @@ public class CompletableFutureTest exten final CompletableFuture h5 = m.applyToEither(g, f, rs[5]); try { assertEquals(inc(v1), h4.join()); - rs[4].assertInvoked(); + rs[4].assertValue(inc(v1)); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h4, ex); rs[4].assertNotInvoked(); } try { assertEquals(inc(v1), h5.join()); - rs[5].assertInvoked(); + rs[5].assertValue(inc(v1)); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h5, ex); rs[5].assertNotInvoked(); @@ -1789,6 +1788,63 @@ public class CompletableFutureTest exten for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); }} + public void testApplyToEither_exceptionalCompletion2() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean fFirst : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final CFException ex = new CFException(); + final IncFunction[] rs = new IncFunction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); + + final CompletableFuture h0 = m.applyToEither(f, g, rs[0]); + final CompletableFuture h1 = m.applyToEither(g, f, rs[1]); + if (fFirst) { + f.complete(v1); + g.completeExceptionally(ex); + } else { + g.completeExceptionally(ex); + f.complete(v1); + } + final CompletableFuture h2 = m.applyToEither(f, g, rs[2]); + final CompletableFuture h3 = m.applyToEither(g, f, rs[3]); + + // unspecified behavior - both source completions available + try { + assertEquals(inc(v1), h0.join()); + rs[0].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h0, ex); + rs[0].assertNotInvoked(); + } + try { + assertEquals(inc(v1), h1.join()); + rs[1].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h1, ex); + rs[1].assertNotInvoked(); + } + try { + assertEquals(inc(v1), h2.join()); + rs[2].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h2, ex); + rs[2].assertNotInvoked(); + } + try { + assertEquals(inc(v1), h3.join()); + rs[3].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h3, ex); + rs[3].assertNotInvoked(); + } + + checkCompletedNormally(f, v1); + checkCompletedWithWrappedCFException(g, ex); + }} + /** * applyToEither result completes exceptionally if either source cancelled */ @@ -1822,14 +1878,14 @@ public class CompletableFutureTest exten final CompletableFuture h5 = m.applyToEither(g, f, rs[5]); try { assertEquals(inc(v1), h4.join()); - rs[4].assertInvoked(); + rs[4].assertValue(inc(v1)); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h4); rs[4].assertNotInvoked(); } try { assertEquals(inc(v1), h5.join()); - rs[5].assertInvoked(); + rs[5].assertValue(inc(v1)); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h5); rs[5].assertNotInvoked(); @@ -1844,6 +1900,63 @@ public class CompletableFutureTest exten for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); }} + public void testApplyToEither_sourceCancelled2() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final IncFunction[] rs = new IncFunction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); + + final CompletableFuture h0 = m.applyToEither(f, g, rs[0]); + final CompletableFuture h1 = m.applyToEither(g, f, rs[1]); + if (fFirst) { + f.complete(v1); + g.cancel(mayInterruptIfRunning); + } else { + g.cancel(mayInterruptIfRunning); + f.complete(v1); + } + final CompletableFuture h2 = m.applyToEither(f, g, rs[2]); + final CompletableFuture h3 = m.applyToEither(g, f, rs[3]); + + // unspecified behavior - both source completions available + try { + assertEquals(inc(v1), h0.join()); + rs[0].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h0); + rs[0].assertNotInvoked(); + } + try { + assertEquals(inc(v1), h1.join()); + rs[1].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h1); + rs[1].assertNotInvoked(); + } + try { + assertEquals(inc(v1), h2.join()); + rs[2].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h2); + rs[2].assertNotInvoked(); + } + try { + assertEquals(inc(v1), h3.join()); + rs[3].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h3); + rs[3].assertNotInvoked(); + } + + checkCompletedNormally(f, v1); + checkCancelled(g); + }} + /** * applyToEither result completes exceptionally if action does */ @@ -1896,8 +2009,8 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction[] rs = new IncAction[6]; - for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m); + final NoopConsumer[] rs = new NoopConsumer[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); final CompletableFuture h0 = m.acceptEither(f, g, rs[0]); final CompletableFuture h1 = m.acceptEither(g, f, rs[1]); @@ -1908,14 +2021,14 @@ public class CompletableFutureTest exten f.complete(v1); checkCompletedNormally(h0, null); checkCompletedNormally(h1, null); - rs[0].assertValue(inc(v1)); - rs[1].assertValue(inc(v1)); + rs[0].assertValue(v1); + rs[1].assertValue(v1); final CompletableFuture h2 = m.acceptEither(f, g, rs[2]); final CompletableFuture h3 = m.acceptEither(g, f, rs[3]); checkCompletedNormally(h2, null); checkCompletedNormally(h3, null); - rs[2].assertValue(inc(v1)); - rs[3].assertValue(inc(v1)); + rs[2].assertValue(v1); + rs[3].assertValue(v1); g.complete(v2); // unspecified behavior - both source completions available @@ -1923,10 +2036,10 @@ public class CompletableFutureTest exten final CompletableFuture h5 = m.acceptEither(g, f, rs[5]); checkCompletedNormally(h4, null); checkCompletedNormally(h5, null); - assertTrue(Objects.equals(inc(v1), rs[4].value) || - Objects.equals(inc(v2), rs[4].value)); - assertTrue(Objects.equals(inc(v1), rs[5].value) || - Objects.equals(inc(v2), rs[5].value)); + assertTrue(Objects.equals(v1, rs[4].value) || + Objects.equals(v2, rs[4].value)); + assertTrue(Objects.equals(v1, rs[5].value) || + Objects.equals(v2, rs[5].value)); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); @@ -1934,7 +2047,7 @@ public class CompletableFutureTest exten checkCompletedNormally(h1, null); checkCompletedNormally(h2, null); checkCompletedNormally(h3, null); - for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); + for (int i = 0; i < 4; i++) rs[i].assertValue(v1); }} /** @@ -1948,8 +2061,8 @@ public class CompletableFutureTest exten final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); final CFException ex = new CFException(); - final IncAction[] rs = new IncAction[6]; - for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m); + final NoopConsumer[] rs = new NoopConsumer[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); final CompletableFuture h0 = m.acceptEither(f, g, rs[0]); final CompletableFuture h1 = m.acceptEither(g, f, rs[1]); @@ -1972,14 +2085,14 @@ public class CompletableFutureTest exten final CompletableFuture h5 = m.acceptEither(g, f, rs[5]); try { assertNull(h4.join()); - rs[4].assertValue(inc(v1)); + rs[4].assertValue(v1); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h4, ex); rs[4].assertNotInvoked(); } try { assertNull(h5.join()); - rs[5].assertValue(inc(v1)); + rs[5].assertValue(v1); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h5, ex); rs[5].assertNotInvoked(); @@ -1995,6 +2108,63 @@ public class CompletableFutureTest exten for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); }} + public void testAcceptEither_exceptionalCompletion2() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean fFirst : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final CFException ex = new CFException(); + final NoopConsumer[] rs = new NoopConsumer[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); + + final CompletableFuture h0 = m.acceptEither(f, g, rs[0]); + final CompletableFuture h1 = m.acceptEither(g, f, rs[1]); + if (fFirst) { + f.complete(v1); + g.completeExceptionally(ex); + } else { + g.completeExceptionally(ex); + f.complete(v1); + } + final CompletableFuture h2 = m.acceptEither(f, g, rs[2]); + final CompletableFuture h3 = m.acceptEither(g, f, rs[3]); + + // unspecified behavior - both source completions available + try { + assertEquals(null, h0.join()); + rs[0].assertValue(v1); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h0, ex); + rs[0].assertNotInvoked(); + } + try { + assertEquals(null, h1.join()); + rs[1].assertValue(v1); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h1, ex); + rs[1].assertNotInvoked(); + } + try { + assertEquals(null, h2.join()); + rs[2].assertValue(v1); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h2, ex); + rs[2].assertNotInvoked(); + } + try { + assertEquals(null, h3.join()); + rs[3].assertValue(v1); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h3, ex); + rs[3].assertNotInvoked(); + } + + checkCompletedNormally(f, v1); + checkCompletedWithWrappedCFException(g, ex); + }} + /** * acceptEither result completes exceptionally if either source cancelled */ @@ -2005,8 +2175,8 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction[] rs = new IncAction[6]; - for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m); + final NoopConsumer[] rs = new NoopConsumer[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); final CompletableFuture h0 = m.acceptEither(f, g, rs[0]); final CompletableFuture h1 = m.acceptEither(g, f, rs[1]); @@ -2029,14 +2199,14 @@ public class CompletableFutureTest exten final CompletableFuture h5 = m.acceptEither(g, f, rs[5]); try { assertNull(h4.join()); - rs[4].assertValue(inc(v1)); + rs[4].assertValue(v1); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h4); rs[4].assertNotInvoked(); } try { assertNull(h5.join()); - rs[5].assertValue(inc(v1)); + rs[5].assertValue(v1); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h5); rs[5].assertNotInvoked(); @@ -2096,297 +2266,253 @@ public class CompletableFutureTest exten * runAfterEither result completes normally after normal completion * of either source */ - public void testRunAfterEither_normalCompletion1() { + public void testRunAfterEither_normalCompletion() { 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(m); - final CompletableFuture h = m.runAfterEither(f, g, r); + final Noop[] rs = new Noop[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); + final CompletableFuture h0 = m.runAfterEither(f, g, rs[0]); + final CompletableFuture h1 = m.runAfterEither(g, f, rs[1]); + checkIncomplete(h0); + checkIncomplete(h1); + rs[0].assertNotInvoked(); + rs[1].assertNotInvoked(); f.complete(v1); - checkCompletedNormally(h, null); - r.assertInvoked(); - g.complete(v2); - - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - checkCompletedNormally(h, null); - r.assertInvoked(); - }} - - 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(m); - final CompletableFuture h = m.runAfterEither(f, g, r); + checkCompletedNormally(h0, null); + checkCompletedNormally(h1, null); + rs[0].assertInvoked(); + rs[1].assertInvoked(); + final CompletableFuture h2 = m.runAfterEither(f, g, rs[2]); + final CompletableFuture h3 = m.runAfterEither(g, f, rs[3]); + checkCompletedNormally(h2, null); + checkCompletedNormally(h3, null); + rs[2].assertInvoked(); + rs[3].assertInvoked(); g.complete(v2); - checkCompletedNormally(h, null); - r.assertInvoked(); - f.complete(v1); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - checkCompletedNormally(h, null); - r.assertInvoked(); - }} - - 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(m); + final CompletableFuture h4 = m.runAfterEither(f, g, rs[4]); + final CompletableFuture h5 = m.runAfterEither(g, f, rs[5]); - f.complete(v1); - g.complete(v2); - final CompletableFuture h = m.runAfterEither(f, g, r); - - checkCompletedNormally(h, null); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - r.assertInvoked(); + checkCompletedNormally(h0, null); + checkCompletedNormally(h1, null); + checkCompletedNormally(h2, null); + checkCompletedNormally(h3, null); + checkCompletedNormally(h4, null); + checkCompletedNormally(h5, null); + for (int i = 0; i < 6; i++) rs[i].assertInvoked(); }} /** * runAfterEither result completes exceptionally after exceptional * completion of either source */ - public void testRunAfterEither_exceptionalCompletion1() { + public void testRunAfterEither_exceptionalCompletion() { 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(m); - final CompletableFuture h = m.runAfterEither(f, g, r); final CFException ex = new CFException(); + final Noop[] rs = new Noop[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); + final CompletableFuture h0 = m.runAfterEither(f, g, rs[0]); + final CompletableFuture h1 = m.runAfterEither(g, f, rs[1]); + checkIncomplete(h0); + checkIncomplete(h1); + rs[0].assertNotInvoked(); + rs[1].assertNotInvoked(); f.completeExceptionally(ex); - checkCompletedWithWrappedCFException(h, ex); + checkCompletedWithWrappedCFException(h0, ex); + checkCompletedWithWrappedCFException(h1, ex); + final CompletableFuture h2 = m.runAfterEither(f, g, rs[2]); + final CompletableFuture h3 = m.runAfterEither(g, f, rs[3]); + checkCompletedWithWrappedCFException(h2, ex); + checkCompletedWithWrappedCFException(h3, ex); + g.complete(v1); - r.assertNotInvoked(); - checkCompletedNormally(g, v1); + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.runAfterEither(f, g, rs[4]); + final CompletableFuture h5 = m.runAfterEither(g, f, rs[5]); + try { + assertNull(h4.join()); + rs[4].assertInvoked(); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h4, ex); + rs[4].assertNotInvoked(); + } + try { + assertNull(h5.join()); + rs[5].assertInvoked(); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h5, ex); + rs[5].assertNotInvoked(); + } + checkCompletedWithWrappedCFException(f, ex); - checkCompletedWithWrappedCFException(h, ex); + checkCompletedNormally(g, v1); + checkCompletedWithWrappedCFException(h0, ex); + checkCompletedWithWrappedCFException(h1, ex); + checkCompletedWithWrappedCFException(h2, ex); + checkCompletedWithWrappedCFException(h3, ex); + checkCompletedWithWrappedCFException(h4, ex); + for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); }} public void testRunAfterEither_exceptionalCompletion2() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean fFirst : 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(m); - final CompletableFuture h = m.runAfterEither(f, g, r); - final CFException ex = new CFException(); - - g.completeExceptionally(ex); - checkCompletedWithWrappedCFException(h, ex); - f.complete(v1); - - r.assertNotInvoked(); - 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(m); final CFException ex = new CFException(); + final Noop[] rs = new Noop[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); - g.completeExceptionally(ex); - f.complete(v1); - final CompletableFuture h = m.runAfterEither(f, g, r); + final CompletableFuture h0 = m.runAfterEither(f, g, rs[0]); + final CompletableFuture h1 = m.runAfterEither(g, f, rs[1]); + if (fFirst) { + f.complete(v1); + g.completeExceptionally(ex); + } else { + g.completeExceptionally(ex); + f.complete(v1); + } + final CompletableFuture h2 = m.runAfterEither(f, g, rs[2]); + final CompletableFuture h3 = m.runAfterEither(g, f, rs[3]); - // unspecified behavior - Integer v; + // unspecified behavior - both source completions available try { - assertNull(h.join()); - r.assertInvoked(); + assertEquals(null, h0.join()); + rs[0].assertInvoked(); } catch (CompletionException ok) { - checkCompletedWithWrappedCFException(h, ex); - r.assertNotInvoked(); + checkCompletedWithWrappedCFException(h0, ex); + rs[0].assertNotInvoked(); + } + try { + assertEquals(null, h1.join()); + rs[1].assertInvoked(); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h1, ex); + rs[1].assertNotInvoked(); + } + try { + assertEquals(null, h2.join()); + rs[2].assertInvoked(); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h2, ex); + rs[2].assertNotInvoked(); + } + try { + assertEquals(null, h3.join()); + rs[3].assertInvoked(); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h3, ex); + rs[3].assertNotInvoked(); } - checkCompletedWithWrappedCFException(g, ex); checkCompletedNormally(f, v1); + checkCompletedWithWrappedCFException(g, ex); }} - public void testRunAfterEither_exceptionalCompletion4() { + /** + * runAfterEither result completes exceptionally if either source cancelled + */ + public void testRunAfterEither_sourceCancelled() { 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(m); - final CFException ex = new CFException(); + final Noop[] rs = new Noop[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); + + final CompletableFuture h0 = m.runAfterEither(f, g, rs[0]); + final CompletableFuture h1 = m.runAfterEither(g, f, rs[1]); + checkIncomplete(h0); + checkIncomplete(h1); + rs[0].assertNotInvoked(); + rs[1].assertNotInvoked(); + f.cancel(mayInterruptIfRunning); + checkCompletedWithWrappedCancellationException(h0); + checkCompletedWithWrappedCancellationException(h1); + final CompletableFuture h2 = m.runAfterEither(f, g, rs[2]); + final CompletableFuture h3 = m.runAfterEither(g, f, rs[3]); + checkCompletedWithWrappedCancellationException(h2); + checkCompletedWithWrappedCancellationException(h3); - f.completeExceptionally(ex); g.complete(v1); - final CompletableFuture h = m.runAfterEither(f, g, r); - // unspecified behavior - Integer v; + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.runAfterEither(f, g, rs[4]); + final CompletableFuture h5 = m.runAfterEither(g, f, rs[5]); try { - assertNull(h.join()); - r.assertInvoked(); + assertNull(h4.join()); + rs[4].assertInvoked(); } catch (CompletionException ok) { - checkCompletedWithWrappedCFException(h, ex); - r.assertNotInvoked(); + checkCompletedWithWrappedCancellationException(h4); + rs[4].assertNotInvoked(); + } + try { + assertNull(h5.join()); + rs[5].assertInvoked(); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h5); + rs[5].assertNotInvoked(); } - checkCompletedWithWrappedCFException(f, ex); + checkCancelled(f); checkCompletedNormally(g, v1); + checkCompletedWithWrappedCancellationException(h0); + checkCompletedWithWrappedCancellationException(h1); + checkCompletedWithWrappedCancellationException(h2); + checkCompletedWithWrappedCancellationException(h3); + for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); }} /** * runAfterEither result completes exceptionally if action does */ - public void testRunAfterEither_actionFailed1() { + public void testRunAfterEither_actionFailed() { 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 FailingRunnable r = new FailingRunnable(m); - final CompletableFuture h = m.runAfterEither(f, g, r); + final FailingRunnable[] rs = new FailingRunnable[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); + final CompletableFuture h0 = m.runAfterEither(f, g, rs[0]); + final CompletableFuture h1 = m.runAfterEither(g, f, rs[1]); f.complete(v1); - checkCompletedWithWrappedCFException(h); + final CompletableFuture h2 = m.runAfterEither(f, g, rs[2]); + final CompletableFuture h3 = m.runAfterEither(g, f, rs[3]); + checkCompletedWithWrappedCFException(h0); + checkCompletedWithWrappedCFException(h1); + checkCompletedWithWrappedCFException(h2); + checkCompletedWithWrappedCFException(h3); + for (int i = 0; i < 4; i++) rs[i].assertInvoked(); 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 FailingRunnable r = new FailingRunnable(m); - final CompletableFuture h = m.runAfterEither(f, g, r); + final CompletableFuture h4 = m.runAfterEither(f, g, rs[4]); + final CompletableFuture h5 = m.runAfterEither(g, f, rs[5]); + checkCompletedWithWrappedCFException(h4); + checkCompletedWithWrappedCFException(h5); - g.complete(v2); - checkCompletedWithWrappedCFException(h); - f.complete(v1); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - }} - - /** - * runAfterEither result completes exceptionally if either source cancelled - */ - 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(m); - final CompletableFuture h = m.runAfterEither(f, g, r); - - assertTrue(f.cancel(mayInterruptIfRunning)); - checkCompletedWithWrappedCancellationException(h); - g.complete(v1); - - checkCancelled(f); - r.assertNotInvoked(); - 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(m); - final CompletableFuture h = m.runAfterEither(f, g, r); - - assertTrue(g.cancel(mayInterruptIfRunning)); - checkCompletedWithWrappedCancellationException(h); - f.complete(v1); - - checkCancelled(g); - r.assertNotInvoked(); - 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(m); - - assertTrue(g.cancel(mayInterruptIfRunning)); - f.complete(v1); - final CompletableFuture h = m.runAfterEither(f, g, r); - - // unspecified behavior - Integer v; - try { - assertNull(h.join()); - r.assertInvoked(); - } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - r.assertNotInvoked(); - } - - 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(m); - - assertTrue(f.cancel(mayInterruptIfRunning)); - g.complete(v1); - final CompletableFuture h = m.runAfterEither(f, g, r); - - // unspecified behavior - Integer v; - try { - assertNull(h.join()); - r.assertInvoked(); - } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - r.assertNotInvoked(); - } - - checkCancelled(f); - checkCompletedNormally(g, v1); + for (int i = 0; i < 6; i++) rs[i].assertInvoked(); }} /**