--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/06 01:19:22 1.62 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/06 16:54:16 1.63 @@ -415,7 +415,8 @@ public class CompletableFutureTest exten } } - class FailingSupplier extends CheckedAction implements Supplier + class FailingSupplier extends CheckedAction + implements Supplier { FailingSupplier(ExecutionMode m) { super(m); } public Integer get() { @@ -424,40 +425,46 @@ public class CompletableFutureTest exten } } - class FailingConsumer extends CheckedAction implements Consumer { + class FailingConsumer extends CheckedIntegerAction + implements Consumer + { FailingConsumer(ExecutionMode m) { super(m); } public void accept(Integer x) { invoked(); + value = x; throw new CFException(); } } - class FailingBiConsumer extends CheckedAction + class FailingBiConsumer extends CheckedIntegerAction implements BiConsumer { FailingBiConsumer(ExecutionMode m) { super(m); } public void accept(Integer x, Integer y) { invoked(); + value = subtract(x, y); throw new CFException(); } } - class FailingFunction extends CheckedAction + class FailingFunction extends CheckedIntegerAction implements Function { FailingFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x) { invoked(); + value = x; throw new CFException(); } } - class FailingBiFunction extends CheckedAction + class FailingBiFunction extends CheckedIntegerAction implements BiFunction { FailingBiFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x, Integer y) { invoked(); + value = subtract(x, y); throw new CFException(); } } @@ -471,24 +478,26 @@ public class CompletableFutureTest exten } - class CompletableFutureInc extends CheckedAction + class CompletableFutureInc extends CheckedIntegerAction implements Function> { CompletableFutureInc(ExecutionMode m) { super(m); } public CompletableFuture apply(Integer x) { invoked(); + value = x; CompletableFuture f = new CompletableFuture<>(); f.complete(inc(x)); return f; } } - class FailingCompletableFutureFunction extends CheckedAction + class FailingCompletableFutureFunction extends CheckedIntegerAction implements Function> { FailingCompletableFutureFunction(ExecutionMode m) { super(m); } public CompletableFuture apply(Integer x) { invoked(); + value = x; throw new CFException(); } } @@ -1853,321 +1862,234 @@ public class CompletableFutureTest exten f.complete(v1); final CompletableFuture h2 = m.applyToEither(f, g, rs[2]); final CompletableFuture h3 = m.applyToEither(g, f, rs[3]); - g.complete(v2); - final CompletableFuture h4 = m.applyToEither(f, g, rs[4]); - final CompletableFuture h5 = m.applyToEither(g, f, rs[5]); - - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); checkCompletedWithWrappedCFException(h0); checkCompletedWithWrappedCFException(h1); checkCompletedWithWrappedCFException(h2); checkCompletedWithWrappedCFException(h3); - checkCompletedWithWrappedCFException(h4); - checkCompletedWithWrappedCFException(h5); - for (int i = 0; i < rs.length; i++) rs[i].assertInvoked(); - }} + for (int i = 0; i < 4; i++) rs[i].assertValue(v1); - /** - * acceptEither result completes normally after normal completion - * of either source - */ - public void testAcceptEither_normalCompletion1() { - 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(m); - final CompletableFuture h = m.acceptEither(f, g, r); - - f.complete(v1); - checkCompletedNormally(h, null); - assertEquals(inc(v1), r.value); 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(m); - final CompletableFuture h = m.acceptEither(f, g, r); + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.applyToEither(f, g, rs[4]); + final CompletableFuture h5 = m.applyToEither(g, f, rs[5]); - g.complete(v2); - checkCompletedNormally(h, null); - assertEquals(inc(v2), r.value); - f.complete(v1); + checkCompletedWithWrappedCFException(h4); + assertTrue(Objects.equals(v1, rs[4].value) || + Objects.equals(v2, rs[4].value)); + checkCompletedWithWrappedCFException(h5); + assertTrue(Objects.equals(v1, rs[5].value) || + Objects.equals(v2, rs[5].value)); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - checkCompletedNormally(h, null); }} - public void testAcceptEither_normalCompletion3() { + /** + * acceptEither result completes normally after normal completion + * of either source + */ + public void testAcceptEither_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 IncAction r = new IncAction(m); + final IncAction[] rs = new IncAction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m); + final CompletableFuture h0 = m.acceptEither(f, g, rs[0]); + final CompletableFuture h1 = m.acceptEither(g, f, rs[1]); + checkIncomplete(h0); + checkIncomplete(h1); + rs[0].assertNotInvoked(); + rs[1].assertNotInvoked(); f.complete(v1); + checkCompletedNormally(h0, null); + checkCompletedNormally(h1, null); + rs[0].assertValue(inc(v1)); + rs[1].assertValue(inc(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)); g.complete(v2); - final CompletableFuture h = m.acceptEither(f, g, r); - checkCompletedNormally(h, null); + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.acceptEither(f, g, rs[4]); + 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)); + checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - - // unspecified behavior - assertTrue(Objects.equals(r.value, inc(v1)) || - Objects.equals(r.value, inc(v2))); + checkCompletedNormally(h0, null); + checkCompletedNormally(h1, null); + checkCompletedNormally(h2, null); + checkCompletedNormally(h3, null); + for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); }} /** * acceptEither result completes exceptionally after exceptional * completion of either source */ - public void testAcceptEither_exceptionalCompletion1() { + public void testAcceptEither_exceptionalCompletion() { 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(m); - final CompletableFuture h = m.acceptEither(f, g, r); 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 CompletableFuture h0 = m.acceptEither(f, g, rs[0]); + final CompletableFuture h1 = m.acceptEither(g, f, rs[1]); + checkIncomplete(h0); + checkIncomplete(h1); + rs[0].assertNotInvoked(); + rs[1].assertNotInvoked(); f.completeExceptionally(ex); - checkCompletedWithWrappedCFException(h, ex); - g.complete(v1); - - r.assertNotInvoked(); - 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(m); - final CompletableFuture h = m.acceptEither(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 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(m); - final CFException ex = new CFException(); + checkCompletedWithWrappedCFException(h0, ex); + checkCompletedWithWrappedCFException(h1, ex); + final CompletableFuture h2 = m.acceptEither(f, g, rs[2]); + final CompletableFuture h3 = m.acceptEither(g, f, rs[3]); + checkCompletedWithWrappedCFException(h2, ex); + checkCompletedWithWrappedCFException(h3, ex); - g.completeExceptionally(ex); - f.complete(v1); - final CompletableFuture h = m.acceptEither(f, g, r); + g.complete(v1); - // unspecified behavior - Integer v; + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.acceptEither(f, g, rs[4]); + final CompletableFuture h5 = m.acceptEither(g, f, rs[5]); try { - assertNull(h.join()); - r.assertInvoked(); - assertEquals(inc(v1), r.value); + assertNull(h4.join()); + rs[4].assertValue(inc(v1)); } catch (CompletionException ok) { - checkCompletedWithWrappedCFException(h, ex); - r.assertNotInvoked(); + checkCompletedWithWrappedCFException(h4, ex); + rs[4].assertNotInvoked(); } - - 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(m); - final CFException ex = new CFException(); - - f.completeExceptionally(ex); - g.complete(v1); - final CompletableFuture h = m.acceptEither(f, g, r); - - // unspecified behavior - Integer v; try { - assertNull(h.join()); - r.assertInvoked(); - assertEquals(inc(v1), r.value); + assertNull(h5.join()); + rs[5].assertValue(inc(v1)); } catch (CompletionException ok) { - checkCompletedWithWrappedCFException(h, ex); - r.assertNotInvoked(); + checkCompletedWithWrappedCFException(h5, ex); + rs[5].assertNotInvoked(); } checkCompletedWithWrappedCFException(f, ex); checkCompletedNormally(g, v1); - }} - - /** - * acceptEither result completes exceptionally if action does - */ - 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(m); - 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(m); - final CompletableFuture h = m.acceptEither(f, g, r); - - g.complete(v2); - checkCompletedWithWrappedCFException(h); - f.complete(v1); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); + 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(); }} /** * acceptEither result completes exceptionally if either source cancelled */ - public void testAcceptEither_sourceCancelled1() { + public void testAcceptEither_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 IncAction r = new IncAction(m); - final CompletableFuture h = m.acceptEither(f, g, r); + final IncAction[] rs = new IncAction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m); + + final CompletableFuture h0 = m.acceptEither(f, g, rs[0]); + final CompletableFuture h1 = m.acceptEither(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.acceptEither(f, g, rs[2]); + final CompletableFuture h3 = m.acceptEither(g, f, rs[3]); + checkCompletedWithWrappedCancellationException(h2); + checkCompletedWithWrappedCancellationException(h3); - assertTrue(f.cancel(mayInterruptIfRunning)); - checkCompletedWithWrappedCancellationException(h); g.complete(v1); + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.acceptEither(f, g, rs[4]); + final CompletableFuture h5 = m.acceptEither(g, f, rs[5]); + try { + assertNull(h4.join()); + rs[4].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h4); + rs[4].assertNotInvoked(); + } + try { + assertNull(h5.join()); + rs[5].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h5); + rs[5].assertNotInvoked(); + } + checkCancelled(f); - r.assertNotInvoked(); checkCompletedNormally(g, v1); - checkCompletedWithWrappedCancellationException(h); + checkCompletedWithWrappedCancellationException(h0); + checkCompletedWithWrappedCancellationException(h1); + checkCompletedWithWrappedCancellationException(h2); + checkCompletedWithWrappedCancellationException(h3); + for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); }} - public void testAcceptEither_sourceCancelled2() { + /** + * acceptEither result completes exceptionally if action does + */ + public void testAcceptEither_actionFailed() { for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 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(m); - final CompletableFuture h = m.acceptEither(f, g, r); + final FailingConsumer[] rs = new FailingConsumer[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); - assertTrue(g.cancel(mayInterruptIfRunning)); - checkCompletedWithWrappedCancellationException(h); + final CompletableFuture h0 = m.acceptEither(f, g, rs[0]); + final CompletableFuture h1 = m.acceptEither(g, f, rs[1]); f.complete(v1); + final CompletableFuture h2 = m.acceptEither(f, g, rs[2]); + final CompletableFuture h3 = m.acceptEither(g, f, rs[3]); + checkCompletedWithWrappedCFException(h0); + checkCompletedWithWrappedCFException(h1); + checkCompletedWithWrappedCFException(h2); + checkCompletedWithWrappedCFException(h3); + for (int i = 0; i < 4; i++) rs[i].assertValue(v1); - checkCancelled(g); - r.assertNotInvoked(); - 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(m); + g.complete(v2); - assertTrue(g.cancel(mayInterruptIfRunning)); - f.complete(v1); - final CompletableFuture h = m.acceptEither(f, g, r); + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.acceptEither(f, g, rs[4]); + final CompletableFuture h5 = m.acceptEither(g, f, rs[5]); - // unspecified behavior - Integer v; - try { - assertNull(h.join()); - r.assertInvoked(); - assertEquals(inc(v1), r.value); - } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - r.assertNotInvoked(); - } + checkCompletedWithWrappedCFException(h4); + assertTrue(Objects.equals(v1, rs[4].value) || + Objects.equals(v2, rs[4].value)); + checkCompletedWithWrappedCFException(h5); + assertTrue(Objects.equals(v1, rs[5].value) || + Objects.equals(v2, rs[5].value)); - 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(m); - - assertTrue(f.cancel(mayInterruptIfRunning)); - g.complete(v1); - final CompletableFuture h = m.acceptEither(f, g, r); - - // unspecified behavior - Integer v; - try { - assertNull(h.join()); - r.assertInvoked(); - assertEquals(inc(v1), r.value); - } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - r.assertNotInvoked(); - } - - checkCancelled(f); - checkCompletedNormally(g, v1); + checkCompletedNormally(g, v2); }} /**