--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/07 23:32:17 1.78 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/16 17:08:15 1.79 @@ -1529,88 +1529,126 @@ public class CompletableFutureTest exten */ public void testThenCombine_normalCompletion() { for (ExecutionMode m : ExecutionMode.values()) - for (boolean createIncomplete : new boolean[] { true, false }) for (boolean fFirst : 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 SubtractFunction r = new SubtractFunction(m); - - assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); - if (!createIncomplete) - assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); - final CompletableFuture h = m.thenCombine(f, g, r); - if (createIncomplete) { - checkIncomplete(h); - r.assertNotInvoked(); - assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); - } - - checkCompletedNormally(h, subtract(v1, v2)); + final SubtractFunction r1 = new SubtractFunction(m); + final SubtractFunction r2 = new SubtractFunction(m); + final SubtractFunction r3 = new SubtractFunction(m); + + final CompletableFuture fst = fFirst ? f : g; + final CompletableFuture snd = !fFirst ? f : g; + final Integer w1 = fFirst ? v1 : v2; + final Integer w2 = !fFirst ? v1 : v2; + + final CompletableFuture h1 = m.thenCombine(f, g, r1); + assertTrue(fst.complete(w1)); + final CompletableFuture h2 = m.thenCombine(f, g, r2); + checkIncomplete(h1); + checkIncomplete(h2); + r1.assertNotInvoked(); + r2.assertNotInvoked(); + assertTrue(snd.complete(w2)); + final CompletableFuture h3 = m.thenCombine(f, g, r3); + + checkCompletedNormally(h1, subtract(v1, v2)); + checkCompletedNormally(h2, subtract(v1, v2)); + checkCompletedNormally(h3, subtract(v1, v2)); + r1.assertValue(subtract(v1, v2)); + r2.assertValue(subtract(v1, v2)); + r3.assertValue(subtract(v1, v2)); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - r.assertValue(subtract(v1, v2)); }} /** * thenCombine result completes exceptionally after exceptional * completion of either source */ - public void testThenCombine_exceptionalCompletion() { + public void testThenCombine_exceptionalCompletion() throws Throwable { for (ExecutionMode m : ExecutionMode.values()) - for (boolean createIncomplete : new boolean[] { true, false }) for (boolean fFirst : new boolean[] { true, false }) + for (boolean failFirst : 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 SubtractFunction r = new SubtractFunction(m); - - assertTrue((fFirst ? f : g).complete(v1)); - if (!createIncomplete) - assertTrue((!fFirst ? f : g).completeExceptionally(ex)); - final CompletableFuture h = m.thenCombine(f, g, r); - if (createIncomplete) { - checkIncomplete(h); - assertTrue((!fFirst ? f : g).completeExceptionally(ex)); - } - - checkCompletedWithWrappedException(h, ex); - r.assertNotInvoked(); - checkCompletedNormally(fFirst ? f : g, v1); - checkCompletedExceptionally(!fFirst ? f : g, ex); + final SubtractFunction r1 = new SubtractFunction(m); + final SubtractFunction r2 = new SubtractFunction(m); + final SubtractFunction r3 = new SubtractFunction(m); + + final CompletableFuture fst = fFirst ? f : g; + final CompletableFuture snd = !fFirst ? f : g; + final Callable complete1 = failFirst ? + () -> fst.completeExceptionally(ex) : + () -> fst.complete(v1); + final Callable complete2 = failFirst ? + () -> snd.complete(v1) : + () -> snd.completeExceptionally(ex); + + final CompletableFuture h1 = m.thenCombine(f, g, r1); + assertTrue(complete1.call()); + final CompletableFuture h2 = m.thenCombine(f, g, r2); + checkIncomplete(h1); + checkIncomplete(h2); + assertTrue(complete2.call()); + final CompletableFuture h3 = m.thenCombine(f, g, r3); + + checkCompletedWithWrappedException(h1, ex); + checkCompletedWithWrappedException(h2, ex); + checkCompletedWithWrappedException(h3, ex); + r1.assertNotInvoked(); + r2.assertNotInvoked(); + r3.assertNotInvoked(); + checkCompletedNormally(failFirst ? snd : fst, v1); + checkCompletedExceptionally(failFirst ? fst : snd, ex); }} /** * thenCombine result completes exceptionally if either source cancelled */ - public void testThenCombine_sourceCancelled() { + public void testThenCombine_sourceCancelled() throws Throwable { for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) - for (boolean createIncomplete : new boolean[] { true, false }) for (boolean fFirst : new boolean[] { true, false }) + for (boolean failFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(m); - - assertTrue((fFirst ? f : g).complete(v1)); - if (!createIncomplete) - assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); - final CompletableFuture h = m.thenCombine(f, g, r); - if (createIncomplete) { - checkIncomplete(h); - assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); - } - - checkCompletedWithWrappedCancellationException(h); - checkCancelled(!fFirst ? f : g); - r.assertNotInvoked(); - checkCompletedNormally(fFirst ? f : g, v1); + final SubtractFunction r1 = new SubtractFunction(m); + final SubtractFunction r2 = new SubtractFunction(m); + final SubtractFunction r3 = new SubtractFunction(m); + + final CompletableFuture fst = fFirst ? f : g; + final CompletableFuture snd = !fFirst ? f : g; + final Callable complete1 = failFirst ? + () -> fst.cancel(mayInterruptIfRunning) : + () -> fst.complete(v1); + final Callable complete2 = failFirst ? + () -> snd.complete(v1) : + () -> snd.cancel(mayInterruptIfRunning); + + final CompletableFuture h1 = m.thenCombine(f, g, r1); + assertTrue(complete1.call()); + final CompletableFuture h2 = m.thenCombine(f, g, r2); + checkIncomplete(h1); + checkIncomplete(h2); + assertTrue(complete2.call()); + final CompletableFuture h3 = m.thenCombine(f, g, r3); + + checkCompletedWithWrappedCancellationException(h1); + checkCompletedWithWrappedCancellationException(h2); + checkCompletedWithWrappedCancellationException(h3); + r1.assertNotInvoked(); + r2.assertNotInvoked(); + r3.assertNotInvoked(); + checkCompletedNormally(failFirst ? snd : fst, v1); + checkCancelled(failFirst ? fst : snd); }} /** @@ -1624,13 +1662,24 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final FailingBiFunction r = new FailingBiFunction(m); - final CompletableFuture h = m.thenCombine(f, g, r); - - assertTrue( fFirst ? f.complete(v1) : g.complete(v2)); - assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); - - checkCompletedWithWrappedCFException(h); + final FailingBiFunction r1 = new FailingBiFunction(m); + final FailingBiFunction r2 = new FailingBiFunction(m); + final FailingBiFunction r3 = new FailingBiFunction(m); + + final CompletableFuture fst = fFirst ? f : g; + final CompletableFuture snd = !fFirst ? f : g; + final Integer w1 = fFirst ? v1 : v2; + final Integer w2 = !fFirst ? v1 : v2; + + final CompletableFuture h1 = m.thenCombine(f, g, r1); + assertTrue(fst.complete(w1)); + final CompletableFuture h2 = m.thenCombine(f, g, r2); + assertTrue(snd.complete(w2)); + final CompletableFuture h3 = m.thenCombine(f, g, r3); + + checkCompletedWithWrappedCFException(h1); + checkCompletedWithWrappedCFException(h2); + checkCompletedWithWrappedCFException(h3); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); }}