ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.63 by jsr166, Fri Jun 6 16:54:16 2014 UTC vs.
Revision 1.76 by jsr166, Sat Jun 7 21:45:13 2014 UTC

# Line 105 | Line 105 | public class CompletableFutureTest exten
105          assertTrue(f.toString().contains("[Completed exceptionally]"));
106      }
107  
108 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
109 <                                              CFException ex) {
108 >    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
109 >                                                      Throwable ex) {
110          try {
111              f.get(LONG_DELAY_MS, MILLISECONDS);
112              shouldThrow();
# Line 131 | Line 131 | public class CompletableFutureTest exten
131          } catch (ExecutionException success) {
132              assertSame(ex, success.getCause());
133          } catch (Throwable fail) { threadUnexpectedException(fail); }
134 +
135          assertTrue(f.isDone());
136          assertFalse(f.isCancelled());
137          assertTrue(f.toString().contains("[Completed exceptionally]"));
138      }
139  
140 +    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
141 +                                                Throwable ex) {
142 +        checkCompletedExceptionallyWithRootCause(f, ex);
143 +        try {
144 +            CompletableFuture<Throwable> spy = f.handle
145 +                ((U u, Throwable t) -> t);
146 +            assertTrue(spy.join() instanceof CompletionException);
147 +            assertSame(ex, spy.join().getCause());
148 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
149 +    }
150 +
151 +    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
152 +        checkCompletedExceptionallyWithRootCause(f, ex);
153 +        try {
154 +            CompletableFuture<Throwable> spy = f.handle
155 +                ((U u, Throwable t) -> t);
156 +            assertSame(ex, spy.join());
157 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
158 +    }
159 +
160      void checkCancelled(CompletableFuture<?> f) {
161          try {
162              f.get(LONG_DELAY_MS, MILLISECONDS);
# Line 218 | Line 239 | public class CompletableFutureTest exten
239       */
240      public void testCompleteExceptionally() {
241          CompletableFuture<Integer> f = new CompletableFuture<>();
242 +        CFException ex = new CFException();
243          checkIncomplete(f);
244 <        f.completeExceptionally(new CFException());
245 <        checkCompletedWithWrappedCFException(f);
244 >        f.completeExceptionally(ex);
245 >        checkCompletedExceptionally(f, ex);
246      }
247  
248      /**
# Line 261 | Line 283 | public class CompletableFutureTest exten
283       * obtrudeException forces completion with given exception
284       */
285      public void testObtrudeException() {
286 <        CompletableFuture<Integer> f = new CompletableFuture<>();
287 <        checkIncomplete(f);
288 <        f.complete(one);
289 <        checkCompletedNormally(f, one);
290 <        f.obtrudeException(new CFException());
269 <        checkCompletedWithWrappedCFException(f);
286 >        for (Integer v1 : new Integer[] { 1, null })
287 >    {
288 >        CFException ex;
289 >        CompletableFuture<Integer> f;
290 >
291          f = new CompletableFuture<>();
292 <        f.obtrudeException(new CFException());
293 <        checkCompletedWithWrappedCFException(f);
292 >        f.complete(v1);
293 >        for (int i = 0; i < 2; i++) {
294 >            f.obtrudeException(ex = new CFException());
295 >            checkCompletedExceptionally(f, ex);
296 >        }
297 >
298 >        f = new CompletableFuture<>();
299 >        for (int i = 0; i < 2; i++) {
300 >            f.obtrudeException(ex = new CFException());
301 >            checkCompletedExceptionally(f, ex);
302 >        }
303 >
304          f = new CompletableFuture<>();
305 +        f.completeExceptionally(ex = new CFException());
306 +        f.obtrudeValue(v1);
307 +        checkCompletedNormally(f, v1);
308 +        f.obtrudeException(ex = new CFException());
309 +        checkCompletedExceptionally(f, ex);
310          f.completeExceptionally(new CFException());
311 <        f.obtrudeValue(four);
312 <        checkCompletedNormally(f, four);
313 <        f.obtrudeException(new CFException());
314 <        checkCompletedWithWrappedCFException(f);
279 <    }
311 >        checkCompletedExceptionally(f, ex);
312 >        f.complete(v1);
313 >        checkCompletedExceptionally(f, ex);
314 >    }}
315  
316      /**
317       * getNumberOfDependents returns number of dependent tasks
318       */
319      public void testGetNumberOfDependents() {
320 +        for (ExecutionMode m : ExecutionMode.values())
321 +    {
322          CompletableFuture<Integer> f = new CompletableFuture<>();
323          assertEquals(0, f.getNumberOfDependents());
324 <        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
324 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
325          assertEquals(1, f.getNumberOfDependents());
326          assertEquals(0, g.getNumberOfDependents());
327 <        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
327 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
328          assertEquals(2, f.getNumberOfDependents());
329 +        assertEquals(0, h.getNumberOfDependents());
330          f.complete(1);
331          checkCompletedNormally(g, null);
332 +        checkCompletedNormally(h, null);
333          assertEquals(0, f.getNumberOfDependents());
334          assertEquals(0, g.getNumberOfDependents());
335 <    }
335 >        assertEquals(0, h.getNumberOfDependents());
336 >    }}
337  
338      /**
339       * toString indicates current completion state
# Line 310 | Line 350 | public class CompletableFutureTest exten
350          f = new CompletableFuture<String>();
351          f.completeExceptionally(new IndexOutOfBoundsException());
352          assertTrue(f.toString().contains("[Completed exceptionally]"));
353 +
354 +        f = new CompletableFuture<String>();
355 +        f.cancel(true);
356 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
357 +
358 +        f = new CompletableFuture<String>();
359 +        f.cancel(false);
360 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
361      }
362  
363      /**
# Line 360 | Line 408 | public class CompletableFutureTest exten
408          return (x == null) ? null : x + 1;
409      }
410  
411 <    class IncAction extends CheckedIntegerAction
411 >    class NoopConsumer extends CheckedIntegerAction
412          implements Consumer<Integer>
413      {
414 <        IncAction(ExecutionMode m) { super(m); }
414 >        NoopConsumer(ExecutionMode m) { super(m); }
415          public void accept(Integer x) {
416              invoked();
417 <            value = inc(x);
417 >            value = x;
418          }
419      }
420  
# Line 808 | Line 856 | public class CompletableFutureTest exten
856          assertEquals(0, a.get());
857      }}
858  
811
859      /**
860       * exceptionally action completes with function value on source
861       * exception
# Line 852 | Line 899 | public class CompletableFutureTest exten
899              });
900          if (createIncomplete) f.completeExceptionally(ex1);
901  
902 <        checkCompletedWithWrappedCFException(g, ex2);
902 >        checkCompletedWithWrappedException(g, ex2);
903 >        assertEquals(1, a.get());
904 >    }}
905 >
906 >    /**
907 >     * whenComplete action executes on normal completion, propagating
908 >     * source result.
909 >     */
910 >    public void testWhenComplete_normalCompletion1() {
911 >        for (ExecutionMode m : ExecutionMode.values())
912 >        for (boolean createIncomplete : new boolean[] { true, false })
913 >        for (Integer v1 : new Integer[] { 1, null })
914 >    {
915 >        final AtomicInteger a = new AtomicInteger(0);
916 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
917 >        if (!createIncomplete) f.complete(v1);
918 >        final CompletableFuture<Integer> g = m.whenComplete
919 >            (f,
920 >             (Integer x, Throwable t) -> {
921 >                m.checkExecutionMode();
922 >                threadAssertSame(x, v1);
923 >                threadAssertNull(t);
924 >                a.getAndIncrement();
925 >            });
926 >        if (createIncomplete) f.complete(v1);
927 >
928 >        checkCompletedNormally(g, v1);
929 >        checkCompletedNormally(f, v1);
930 >        assertEquals(1, a.get());
931 >    }}
932 >
933 >    /**
934 >     * whenComplete action executes on exceptional completion, propagating
935 >     * source result.
936 >     */
937 >    public void testWhenComplete_exceptionalCompletion() {
938 >        for (ExecutionMode m : ExecutionMode.values())
939 >        for (boolean createIncomplete : new boolean[] { true, false })
940 >        for (Integer v1 : new Integer[] { 1, null })
941 >    {
942 >        final AtomicInteger a = new AtomicInteger(0);
943 >        final CFException ex = new CFException();
944 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
945 >        if (!createIncomplete) f.completeExceptionally(ex);
946 >        final CompletableFuture<Integer> g = m.whenComplete
947 >            (f,
948 >             (Integer x, Throwable t) -> {
949 >                m.checkExecutionMode();
950 >                threadAssertNull(x);
951 >                threadAssertSame(t, ex);
952 >                a.getAndIncrement();
953 >            });
954 >        if (createIncomplete) f.completeExceptionally(ex);
955 >
956 >        checkCompletedWithWrappedException(g, ex);
957 >        checkCompletedExceptionally(f, ex);
958 >        assertEquals(1, a.get());
959 >    }}
960 >
961 >    /**
962 >     * whenComplete action executes on cancelled source, propagating
963 >     * CancellationException.
964 >     */
965 >    public void testWhenComplete_sourceCancelled() {
966 >        for (ExecutionMode m : ExecutionMode.values())
967 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
968 >        for (boolean createIncomplete : new boolean[] { true, false })
969 >    {
970 >        final AtomicInteger a = new AtomicInteger(0);
971 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
972 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
973 >        final CompletableFuture<Integer> g = m.whenComplete
974 >            (f,
975 >             (Integer x, Throwable t) -> {
976 >                m.checkExecutionMode();
977 >                threadAssertNull(x);
978 >                threadAssertTrue(t instanceof CancellationException);
979 >                a.getAndIncrement();
980 >            });
981 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
982 >
983 >        checkCompletedWithWrappedCancellationException(g);
984 >        checkCancelled(f);
985 >        assertEquals(1, a.get());
986 >    }}
987 >
988 >    /**
989 >     * If a whenComplete action throws an exception when triggered by
990 >     * a normal completion, it completes exceptionally
991 >     */
992 >    public void testWhenComplete_actionFailed() {
993 >        for (boolean createIncomplete : new boolean[] { true, false })
994 >        for (ExecutionMode m : ExecutionMode.values())
995 >        for (Integer v1 : new Integer[] { 1, null })
996 >    {
997 >        final AtomicInteger a = new AtomicInteger(0);
998 >        final CFException ex = new CFException();
999 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1000 >        if (!createIncomplete) f.complete(v1);
1001 >        final CompletableFuture<Integer> g = m.whenComplete
1002 >            (f,
1003 >             (Integer x, Throwable t) -> {
1004 >                m.checkExecutionMode();
1005 >                threadAssertSame(x, v1);
1006 >                threadAssertNull(t);
1007 >                a.getAndIncrement();
1008 >                throw ex;
1009 >            });
1010 >        if (createIncomplete) f.complete(v1);
1011 >
1012 >        checkCompletedWithWrappedException(g, ex);
1013 >        checkCompletedNormally(f, v1);
1014 >        assertEquals(1, a.get());
1015 >    }}
1016 >
1017 >    /**
1018 >     * If a whenComplete action throws an exception when triggered by
1019 >     * a source completion that also throws an exception, the source
1020 >     * exception takes precedence.
1021 >     */
1022 >    public void testWhenComplete_actionFailedSourceFailed() {
1023 >        for (boolean createIncomplete : new boolean[] { true, false })
1024 >        for (ExecutionMode m : ExecutionMode.values())
1025 >        for (Integer v1 : new Integer[] { 1, null })
1026 >    {
1027 >        final AtomicInteger a = new AtomicInteger(0);
1028 >        final CFException ex1 = new CFException();
1029 >        final CFException ex2 = new CFException();
1030 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1031 >
1032 >        if (!createIncomplete) f.completeExceptionally(ex1);
1033 >        final CompletableFuture<Integer> g = m.whenComplete
1034 >            (f,
1035 >             (Integer x, Throwable t) -> {
1036 >                m.checkExecutionMode();
1037 >                threadAssertSame(t, ex1);
1038 >                threadAssertNull(x);
1039 >                a.getAndIncrement();
1040 >                throw ex2;
1041 >            });
1042 >        if (createIncomplete) f.completeExceptionally(ex1);
1043 >
1044 >        checkCompletedWithWrappedException(g, ex1);
1045 >        checkCompletedExceptionally(f, ex1);
1046          assertEquals(1, a.get());
1047      }}
1048  
# Line 909 | Line 1099 | public class CompletableFutureTest exten
1099          if (createIncomplete) f.completeExceptionally(ex);
1100  
1101          checkCompletedNormally(g, v1);
1102 <        checkCompletedWithWrappedCFException(f, ex);
1102 >        checkCompletedExceptionally(f, ex);
1103          assertEquals(1, a.get());
1104      }}
1105  
# Line 965 | Line 1155 | public class CompletableFutureTest exten
1155              });
1156          if (createIncomplete) f.completeExceptionally(ex1);
1157  
1158 <        checkCompletedWithWrappedCFException(g, ex2);
1159 <        checkCompletedWithWrappedCFException(f, ex1);
1158 >        checkCompletedWithWrappedException(g, ex2);
1159 >        checkCompletedExceptionally(f, ex1);
1160          assertEquals(1, a.get());
1161      }}
1162  
# Line 990 | Line 1180 | public class CompletableFutureTest exten
1180              });
1181          if (createIncomplete) f.complete(v1);
1182  
1183 <        checkCompletedWithWrappedCFException(g, ex);
1183 >        checkCompletedWithWrappedException(g, ex);
1184          checkCompletedNormally(f, v1);
1185          assertEquals(1, a.get());
1186      }}
# Line 1104 | Line 1294 | public class CompletableFutureTest exten
1294              f.completeExceptionally(ex);
1295          }
1296  
1297 <        checkCompletedWithWrappedCFException(g, ex);
1298 <        checkCompletedWithWrappedCFException(f, ex);
1297 >        checkCompletedWithWrappedException(g, ex);
1298 >        checkCompletedExceptionally(f, ex);
1299          r.assertNotInvoked();
1300      }}
1301  
# Line 1171 | Line 1361 | public class CompletableFutureTest exten
1361  
1362          checkCompletedNormally(g, inc(v1));
1363          checkCompletedNormally(f, v1);
1364 <        r.assertInvoked();
1364 >        r.assertValue(inc(v1));
1365      }}
1366  
1367      /**
# Line 1192 | Line 1382 | public class CompletableFutureTest exten
1382              f.completeExceptionally(ex);
1383          }
1384  
1385 <        checkCompletedWithWrappedCFException(g, ex);
1386 <        checkCompletedWithWrappedCFException(f, ex);
1385 >        checkCompletedWithWrappedException(g, ex);
1386 >        checkCompletedExceptionally(f, ex);
1387          r.assertNotInvoked();
1388      }}
1389  
# Line 1249 | Line 1439 | public class CompletableFutureTest exten
1439          for (Integer v1 : new Integer[] { 1, null })
1440      {
1441          final CompletableFuture<Integer> f = new CompletableFuture<>();
1442 <        final IncAction r = new IncAction(m);
1442 >        final NoopConsumer r = new NoopConsumer(m);
1443          if (!createIncomplete) f.complete(v1);
1444          final CompletableFuture<Void> g = m.thenAccept(f, r);
1445          if (createIncomplete) {
# Line 1258 | Line 1448 | public class CompletableFutureTest exten
1448          }
1449  
1450          checkCompletedNormally(g, null);
1451 +        r.assertValue(v1);
1452          checkCompletedNormally(f, v1);
1262        r.assertInvoked();
1263        r.assertValue(inc(v1));
1453      }}
1454  
1455      /**
# Line 1273 | Line 1462 | public class CompletableFutureTest exten
1462      {
1463          final CFException ex = new CFException();
1464          final CompletableFuture<Integer> f = new CompletableFuture<>();
1465 <        final IncAction r = new IncAction(m);
1465 >        final NoopConsumer r = new NoopConsumer(m);
1466          if (!createIncomplete) f.completeExceptionally(ex);
1467          final CompletableFuture<Void> g = m.thenAccept(f, r);
1468          if (createIncomplete) {
# Line 1281 | Line 1470 | public class CompletableFutureTest exten
1470              f.completeExceptionally(ex);
1471          }
1472  
1473 <        checkCompletedWithWrappedCFException(g, ex);
1474 <        checkCompletedWithWrappedCFException(f, ex);
1473 >        checkCompletedWithWrappedException(g, ex);
1474 >        checkCompletedExceptionally(f, ex);
1475          r.assertNotInvoked();
1476      }}
1477  
# Line 1295 | Line 1484 | public class CompletableFutureTest exten
1484          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1485      {
1486          final CompletableFuture<Integer> f = new CompletableFuture<>();
1487 <        final IncAction r = new IncAction(m);
1487 >        final NoopConsumer r = new NoopConsumer(m);
1488          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1489          final CompletableFuture<Void> g = m.thenAccept(f, r);
1490          if (createIncomplete) {
# Line 1357 | Line 1546 | public class CompletableFutureTest exten
1546          checkCompletedNormally(h, subtract(v1, v2));
1547          checkCompletedNormally(f, v1);
1548          checkCompletedNormally(g, v2);
1549 <        r.assertInvoked();
1549 >        r.assertValue(subtract(v1, v2));
1550      }}
1551  
1552      /**
# Line 1384 | Line 1573 | public class CompletableFutureTest exten
1573              (!fFirst ? f : g).completeExceptionally(ex);
1574          }
1575  
1576 <        checkCompletedWithWrappedCFException(h, ex);
1576 >        checkCompletedWithWrappedException(h, ex);
1577          r.assertNotInvoked();
1578          checkCompletedNormally(fFirst ? f : g, v1);
1579 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1579 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1580      }}
1581  
1582      /**
# Line 1501 | Line 1690 | public class CompletableFutureTest exten
1690              (!fFirst ? f : g).completeExceptionally(ex);
1691          }
1692  
1693 <        checkCompletedWithWrappedCFException(h, ex);
1693 >        checkCompletedWithWrappedException(h, ex);
1694          r.assertNotInvoked();
1695          checkCompletedNormally(fFirst ? f : g, v1);
1696 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1696 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1697      }}
1698  
1699      /**
# Line 1618 | Line 1807 | public class CompletableFutureTest exten
1807              (!fFirst ? f : g).completeExceptionally(ex);
1808          }
1809  
1810 <        checkCompletedWithWrappedCFException(h, ex);
1810 >        checkCompletedWithWrappedException(h, ex);
1811          r.assertNotInvoked();
1812          checkCompletedNormally(fFirst ? f : g, v1);
1813 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1813 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1814      }}
1815  
1816      /**
# Line 1638 | Line 1827 | public class CompletableFutureTest exten
1827          final CompletableFuture<Integer> g = new CompletableFuture<>();
1828          final Noop r = new Noop(m);
1829  
1641
1830          (fFirst ? f : g).complete(v1);
1831          if (!createIncomplete)
1832              assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
# Line 1753 | Line 1941 | public class CompletableFutureTest exten
1941          rs[0].assertNotInvoked();
1942          rs[1].assertNotInvoked();
1943          f.completeExceptionally(ex);
1944 <        checkCompletedWithWrappedCFException(h0, ex);
1945 <        checkCompletedWithWrappedCFException(h1, ex);
1944 >        checkCompletedWithWrappedException(h0, ex);
1945 >        checkCompletedWithWrappedException(h1, ex);
1946          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1947          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1948 <        checkCompletedWithWrappedCFException(h2, ex);
1949 <        checkCompletedWithWrappedCFException(h3, ex);
1948 >        checkCompletedWithWrappedException(h2, ex);
1949 >        checkCompletedWithWrappedException(h3, ex);
1950          g.complete(v1);
1951  
1952          // unspecified behavior - both source completions available
# Line 1766 | Line 1954 | public class CompletableFutureTest exten
1954          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1955          try {
1956              assertEquals(inc(v1), h4.join());
1957 <            rs[4].assertInvoked();
1957 >            rs[4].assertValue(inc(v1));
1958          } catch (CompletionException ok) {
1959 <            checkCompletedWithWrappedCFException(h4, ex);
1959 >            checkCompletedWithWrappedException(h4, ex);
1960              rs[4].assertNotInvoked();
1961          }
1962          try {
1963              assertEquals(inc(v1), h5.join());
1964 <            rs[5].assertInvoked();
1964 >            rs[5].assertValue(inc(v1));
1965          } catch (CompletionException ok) {
1966 <            checkCompletedWithWrappedCFException(h5, ex);
1966 >            checkCompletedWithWrappedException(h5, ex);
1967              rs[5].assertNotInvoked();
1968          }
1969  
1970 <        checkCompletedWithWrappedCFException(f, ex);
1970 >        checkCompletedExceptionally(f, ex);
1971          checkCompletedNormally(g, v1);
1972 <        checkCompletedWithWrappedCFException(h0, ex);
1973 <        checkCompletedWithWrappedCFException(h1, ex);
1974 <        checkCompletedWithWrappedCFException(h2, ex);
1975 <        checkCompletedWithWrappedCFException(h3, ex);
1976 <        checkCompletedWithWrappedCFException(h4, ex);
1972 >        checkCompletedWithWrappedException(h0, ex);
1973 >        checkCompletedWithWrappedException(h1, ex);
1974 >        checkCompletedWithWrappedException(h2, ex);
1975 >        checkCompletedWithWrappedException(h3, ex);
1976 >        checkCompletedWithWrappedException(h4, ex);
1977          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1978      }}
1979  
1980 +    public void testApplyToEither_exceptionalCompletion2() {
1981 +        for (ExecutionMode m : ExecutionMode.values())
1982 +        for (boolean fFirst : new boolean[] { true, false })
1983 +        for (Integer v1 : new Integer[] { 1, null })
1984 +    {
1985 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1986 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1987 +        final CFException ex = new CFException();
1988 +        final IncFunction[] rs = new IncFunction[6];
1989 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1990 +
1991 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1992 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1993 +        if (fFirst) {
1994 +            f.complete(v1);
1995 +            g.completeExceptionally(ex);
1996 +        } else {
1997 +            g.completeExceptionally(ex);
1998 +            f.complete(v1);
1999 +        }
2000 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2001 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2002 +
2003 +        // unspecified behavior - both source completions available
2004 +        try {
2005 +            assertEquals(inc(v1), h0.join());
2006 +            rs[0].assertValue(inc(v1));
2007 +        } catch (CompletionException ok) {
2008 +            checkCompletedWithWrappedException(h0, ex);
2009 +            rs[0].assertNotInvoked();
2010 +        }
2011 +        try {
2012 +            assertEquals(inc(v1), h1.join());
2013 +            rs[1].assertValue(inc(v1));
2014 +        } catch (CompletionException ok) {
2015 +            checkCompletedWithWrappedException(h1, ex);
2016 +            rs[1].assertNotInvoked();
2017 +        }
2018 +        try {
2019 +            assertEquals(inc(v1), h2.join());
2020 +            rs[2].assertValue(inc(v1));
2021 +        } catch (CompletionException ok) {
2022 +            checkCompletedWithWrappedException(h2, ex);
2023 +            rs[2].assertNotInvoked();
2024 +        }
2025 +        try {
2026 +            assertEquals(inc(v1), h3.join());
2027 +            rs[3].assertValue(inc(v1));
2028 +        } catch (CompletionException ok) {
2029 +            checkCompletedWithWrappedException(h3, ex);
2030 +            rs[3].assertNotInvoked();
2031 +        }
2032 +
2033 +        checkCompletedNormally(f, v1);
2034 +        checkCompletedExceptionally(g, ex);
2035 +    }}
2036 +
2037      /**
2038       * applyToEither result completes exceptionally if either source cancelled
2039       */
# Line 1822 | Line 2067 | public class CompletableFutureTest exten
2067          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2068          try {
2069              assertEquals(inc(v1), h4.join());
2070 <            rs[4].assertInvoked();
2070 >            rs[4].assertValue(inc(v1));
2071          } catch (CompletionException ok) {
2072              checkCompletedWithWrappedCancellationException(h4);
2073              rs[4].assertNotInvoked();
2074          }
2075          try {
2076              assertEquals(inc(v1), h5.join());
2077 <            rs[5].assertInvoked();
2077 >            rs[5].assertValue(inc(v1));
2078          } catch (CompletionException ok) {
2079              checkCompletedWithWrappedCancellationException(h5);
2080              rs[5].assertNotInvoked();
# Line 1844 | Line 2089 | public class CompletableFutureTest exten
2089          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2090      }}
2091  
2092 +    public void testApplyToEither_sourceCancelled2() {
2093 +        for (ExecutionMode m : ExecutionMode.values())
2094 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2095 +        for (boolean fFirst : new boolean[] { true, false })
2096 +        for (Integer v1 : new Integer[] { 1, null })
2097 +    {
2098 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2099 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2100 +        final IncFunction[] rs = new IncFunction[6];
2101 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2102 +
2103 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2104 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2105 +        if (fFirst) {
2106 +            f.complete(v1);
2107 +            g.cancel(mayInterruptIfRunning);
2108 +        } else {
2109 +            g.cancel(mayInterruptIfRunning);
2110 +            f.complete(v1);
2111 +        }
2112 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2113 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2114 +
2115 +        // unspecified behavior - both source completions available
2116 +        try {
2117 +            assertEquals(inc(v1), h0.join());
2118 +            rs[0].assertValue(inc(v1));
2119 +        } catch (CompletionException ok) {
2120 +            checkCompletedWithWrappedCancellationException(h0);
2121 +            rs[0].assertNotInvoked();
2122 +        }
2123 +        try {
2124 +            assertEquals(inc(v1), h1.join());
2125 +            rs[1].assertValue(inc(v1));
2126 +        } catch (CompletionException ok) {
2127 +            checkCompletedWithWrappedCancellationException(h1);
2128 +            rs[1].assertNotInvoked();
2129 +        }
2130 +        try {
2131 +            assertEquals(inc(v1), h2.join());
2132 +            rs[2].assertValue(inc(v1));
2133 +        } catch (CompletionException ok) {
2134 +            checkCompletedWithWrappedCancellationException(h2);
2135 +            rs[2].assertNotInvoked();
2136 +        }
2137 +        try {
2138 +            assertEquals(inc(v1), h3.join());
2139 +            rs[3].assertValue(inc(v1));
2140 +        } catch (CompletionException ok) {
2141 +            checkCompletedWithWrappedCancellationException(h3);
2142 +            rs[3].assertNotInvoked();
2143 +        }
2144 +
2145 +        checkCompletedNormally(f, v1);
2146 +        checkCancelled(g);
2147 +    }}
2148 +
2149      /**
2150       * applyToEither result completes exceptionally if action does
2151       */
# Line 1896 | Line 2198 | public class CompletableFutureTest exten
2198      {
2199          final CompletableFuture<Integer> f = new CompletableFuture<>();
2200          final CompletableFuture<Integer> g = new CompletableFuture<>();
2201 <        final IncAction[] rs = new IncAction[6];
2202 <        for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m);
2201 >        final NoopConsumer[] rs = new NoopConsumer[6];
2202 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2203  
2204          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2205          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
# Line 1908 | Line 2210 | public class CompletableFutureTest exten
2210          f.complete(v1);
2211          checkCompletedNormally(h0, null);
2212          checkCompletedNormally(h1, null);
2213 <        rs[0].assertValue(inc(v1));
2214 <        rs[1].assertValue(inc(v1));
2213 >        rs[0].assertValue(v1);
2214 >        rs[1].assertValue(v1);
2215          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2216          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2217          checkCompletedNormally(h2, null);
2218          checkCompletedNormally(h3, null);
2219 <        rs[2].assertValue(inc(v1));
2220 <        rs[3].assertValue(inc(v1));
2219 >        rs[2].assertValue(v1);
2220 >        rs[3].assertValue(v1);
2221          g.complete(v2);
2222  
2223          // unspecified behavior - both source completions available
# Line 1923 | Line 2225 | public class CompletableFutureTest exten
2225          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2226          checkCompletedNormally(h4, null);
2227          checkCompletedNormally(h5, null);
2228 <        assertTrue(Objects.equals(inc(v1), rs[4].value) ||
2229 <                   Objects.equals(inc(v2), rs[4].value));
2230 <        assertTrue(Objects.equals(inc(v1), rs[5].value) ||
2231 <                   Objects.equals(inc(v2), rs[5].value));
2228 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2229 >                   Objects.equals(v2, rs[4].value));
2230 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2231 >                   Objects.equals(v2, rs[5].value));
2232  
2233          checkCompletedNormally(f, v1);
2234          checkCompletedNormally(g, v2);
# Line 1934 | Line 2236 | public class CompletableFutureTest exten
2236          checkCompletedNormally(h1, null);
2237          checkCompletedNormally(h2, null);
2238          checkCompletedNormally(h3, null);
2239 <        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
2239 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2240      }}
2241  
2242      /**
# Line 1948 | Line 2250 | public class CompletableFutureTest exten
2250          final CompletableFuture<Integer> f = new CompletableFuture<>();
2251          final CompletableFuture<Integer> g = new CompletableFuture<>();
2252          final CFException ex = new CFException();
2253 <        final IncAction[] rs = new IncAction[6];
2254 <        for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m);
2253 >        final NoopConsumer[] rs = new NoopConsumer[6];
2254 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2255  
2256          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2257          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
# Line 1958 | Line 2260 | public class CompletableFutureTest exten
2260          rs[0].assertNotInvoked();
2261          rs[1].assertNotInvoked();
2262          f.completeExceptionally(ex);
2263 <        checkCompletedWithWrappedCFException(h0, ex);
2264 <        checkCompletedWithWrappedCFException(h1, ex);
2263 >        checkCompletedWithWrappedException(h0, ex);
2264 >        checkCompletedWithWrappedException(h1, ex);
2265          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2266          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2267 <        checkCompletedWithWrappedCFException(h2, ex);
2268 <        checkCompletedWithWrappedCFException(h3, ex);
2267 >        checkCompletedWithWrappedException(h2, ex);
2268 >        checkCompletedWithWrappedException(h3, ex);
2269  
2270          g.complete(v1);
2271  
# Line 1972 | Line 2274 | public class CompletableFutureTest exten
2274          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2275          try {
2276              assertNull(h4.join());
2277 <            rs[4].assertValue(inc(v1));
2277 >            rs[4].assertValue(v1);
2278          } catch (CompletionException ok) {
2279 <            checkCompletedWithWrappedCFException(h4, ex);
2279 >            checkCompletedWithWrappedException(h4, ex);
2280              rs[4].assertNotInvoked();
2281          }
2282          try {
2283              assertNull(h5.join());
2284 <            rs[5].assertValue(inc(v1));
2284 >            rs[5].assertValue(v1);
2285          } catch (CompletionException ok) {
2286 <            checkCompletedWithWrappedCFException(h5, ex);
2286 >            checkCompletedWithWrappedException(h5, ex);
2287              rs[5].assertNotInvoked();
2288          }
2289  
2290 <        checkCompletedWithWrappedCFException(f, ex);
2290 >        checkCompletedExceptionally(f, ex);
2291          checkCompletedNormally(g, v1);
2292 <        checkCompletedWithWrappedCFException(h0, ex);
2293 <        checkCompletedWithWrappedCFException(h1, ex);
2294 <        checkCompletedWithWrappedCFException(h2, ex);
2295 <        checkCompletedWithWrappedCFException(h3, ex);
2296 <        checkCompletedWithWrappedCFException(h4, ex);
2292 >        checkCompletedWithWrappedException(h0, ex);
2293 >        checkCompletedWithWrappedException(h1, ex);
2294 >        checkCompletedWithWrappedException(h2, ex);
2295 >        checkCompletedWithWrappedException(h3, ex);
2296 >        checkCompletedWithWrappedException(h4, ex);
2297          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2298      }}
2299  
2300 +    public void testAcceptEither_exceptionalCompletion2() {
2301 +        for (ExecutionMode m : ExecutionMode.values())
2302 +        for (boolean fFirst : new boolean[] { true, false })
2303 +        for (Integer v1 : new Integer[] { 1, null })
2304 +    {
2305 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2306 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2307 +        final CFException ex = new CFException();
2308 +        final NoopConsumer[] rs = new NoopConsumer[6];
2309 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2310 +
2311 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2312 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2313 +        if (fFirst) {
2314 +            f.complete(v1);
2315 +            g.completeExceptionally(ex);
2316 +        } else {
2317 +            g.completeExceptionally(ex);
2318 +            f.complete(v1);
2319 +        }
2320 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2321 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2322 +
2323 +        // unspecified behavior - both source completions available
2324 +        try {
2325 +            assertEquals(null, h0.join());
2326 +            rs[0].assertValue(v1);
2327 +        } catch (CompletionException ok) {
2328 +            checkCompletedWithWrappedException(h0, ex);
2329 +            rs[0].assertNotInvoked();
2330 +        }
2331 +        try {
2332 +            assertEquals(null, h1.join());
2333 +            rs[1].assertValue(v1);
2334 +        } catch (CompletionException ok) {
2335 +            checkCompletedWithWrappedException(h1, ex);
2336 +            rs[1].assertNotInvoked();
2337 +        }
2338 +        try {
2339 +            assertEquals(null, h2.join());
2340 +            rs[2].assertValue(v1);
2341 +        } catch (CompletionException ok) {
2342 +            checkCompletedWithWrappedException(h2, ex);
2343 +            rs[2].assertNotInvoked();
2344 +        }
2345 +        try {
2346 +            assertEquals(null, h3.join());
2347 +            rs[3].assertValue(v1);
2348 +        } catch (CompletionException ok) {
2349 +            checkCompletedWithWrappedException(h3, ex);
2350 +            rs[3].assertNotInvoked();
2351 +        }
2352 +
2353 +        checkCompletedNormally(f, v1);
2354 +        checkCompletedExceptionally(g, ex);
2355 +    }}
2356 +
2357      /**
2358       * acceptEither result completes exceptionally if either source cancelled
2359       */
# Line 2005 | Line 2364 | public class CompletableFutureTest exten
2364      {
2365          final CompletableFuture<Integer> f = new CompletableFuture<>();
2366          final CompletableFuture<Integer> g = new CompletableFuture<>();
2367 <        final IncAction[] rs = new IncAction[6];
2368 <        for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m);
2367 >        final NoopConsumer[] rs = new NoopConsumer[6];
2368 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2369  
2370          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2371          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
# Line 2029 | Line 2388 | public class CompletableFutureTest exten
2388          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2389          try {
2390              assertNull(h4.join());
2391 <            rs[4].assertValue(inc(v1));
2391 >            rs[4].assertValue(v1);
2392          } catch (CompletionException ok) {
2393              checkCompletedWithWrappedCancellationException(h4);
2394              rs[4].assertNotInvoked();
2395          }
2396          try {
2397              assertNull(h5.join());
2398 <            rs[5].assertValue(inc(v1));
2398 >            rs[5].assertValue(v1);
2399          } catch (CompletionException ok) {
2400              checkCompletedWithWrappedCancellationException(h5);
2401              rs[5].assertNotInvoked();
# Line 2096 | Line 2455 | public class CompletableFutureTest exten
2455       * runAfterEither result completes normally after normal completion
2456       * of either source
2457       */
2458 <    public void testRunAfterEither_normalCompletion1() {
2458 >    public void testRunAfterEither_normalCompletion() {
2459          for (ExecutionMode m : ExecutionMode.values())
2460          for (Integer v1 : new Integer[] { 1, null })
2461          for (Integer v2 : new Integer[] { 2, null })
2462      {
2463          final CompletableFuture<Integer> f = new CompletableFuture<>();
2464          final CompletableFuture<Integer> g = new CompletableFuture<>();
2465 <        final Noop r = new Noop(m);
2466 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2465 >        final Noop[] rs = new Noop[6];
2466 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2467  
2468 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2469 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2470 +        checkIncomplete(h0);
2471 +        checkIncomplete(h1);
2472 +        rs[0].assertNotInvoked();
2473 +        rs[1].assertNotInvoked();
2474          f.complete(v1);
2475 <        checkCompletedNormally(h, null);
2476 <        r.assertInvoked();
2477 <        g.complete(v2);
2478 <
2479 <        checkCompletedNormally(f, v1);
2480 <        checkCompletedNormally(g, v2);
2481 <        checkCompletedNormally(h, null);
2482 <        r.assertInvoked();
2483 <    }}
2484 <
2120 <    public void testRunAfterEither_normalCompletion2() {
2121 <        for (ExecutionMode m : ExecutionMode.values())
2122 <        for (Integer v1 : new Integer[] { 1, null })
2123 <        for (Integer v2 : new Integer[] { 2, null })
2124 <    {
2125 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2126 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2127 <        final Noop r = new Noop(m);
2128 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2475 >        checkCompletedNormally(h0, null);
2476 >        checkCompletedNormally(h1, null);
2477 >        rs[0].assertInvoked();
2478 >        rs[1].assertInvoked();
2479 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2480 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2481 >        checkCompletedNormally(h2, null);
2482 >        checkCompletedNormally(h3, null);
2483 >        rs[2].assertInvoked();
2484 >        rs[3].assertInvoked();
2485  
2486          g.complete(v2);
2131        checkCompletedNormally(h, null);
2132        r.assertInvoked();
2133        f.complete(v1);
2134
2135        checkCompletedNormally(f, v1);
2136        checkCompletedNormally(g, v2);
2137        checkCompletedNormally(h, null);
2138        r.assertInvoked();
2139        }}
2140
2141    public void testRunAfterEither_normalCompletion3() {
2142        for (ExecutionMode m : ExecutionMode.values())
2143        for (Integer v1 : new Integer[] { 1, null })
2144        for (Integer v2 : new Integer[] { 2, null })
2145    {
2146        final CompletableFuture<Integer> f = new CompletableFuture<>();
2147        final CompletableFuture<Integer> g = new CompletableFuture<>();
2148        final Noop r = new Noop(m);
2487  
2488 <        f.complete(v1);
2489 <        g.complete(v2);
2152 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2488 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2489 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2490  
2154        checkCompletedNormally(h, null);
2491          checkCompletedNormally(f, v1);
2492          checkCompletedNormally(g, v2);
2493 <        r.assertInvoked();
2493 >        checkCompletedNormally(h0, null);
2494 >        checkCompletedNormally(h1, null);
2495 >        checkCompletedNormally(h2, null);
2496 >        checkCompletedNormally(h3, null);
2497 >        checkCompletedNormally(h4, null);
2498 >        checkCompletedNormally(h5, null);
2499 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2500      }}
2501  
2502      /**
2503       * runAfterEither result completes exceptionally after exceptional
2504       * completion of either source
2505       */
2506 <    public void testRunAfterEither_exceptionalCompletion1() {
2506 >    public void testRunAfterEither_exceptionalCompletion() {
2507          for (ExecutionMode m : ExecutionMode.values())
2508          for (Integer v1 : new Integer[] { 1, null })
2509      {
2510          final CompletableFuture<Integer> f = new CompletableFuture<>();
2511          final CompletableFuture<Integer> g = new CompletableFuture<>();
2170        final Noop r = new Noop(m);
2171        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2512          final CFException ex = new CFException();
2513 +        final Noop[] rs = new Noop[6];
2514 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2515  
2516 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2517 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2518 +        checkIncomplete(h0);
2519 +        checkIncomplete(h1);
2520 +        rs[0].assertNotInvoked();
2521 +        rs[1].assertNotInvoked();
2522          f.completeExceptionally(ex);
2523 <        checkCompletedWithWrappedCFException(h, ex);
2523 >        checkCompletedWithWrappedException(h0, ex);
2524 >        checkCompletedWithWrappedException(h1, ex);
2525 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2526 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2527 >        checkCompletedWithWrappedException(h2, ex);
2528 >        checkCompletedWithWrappedException(h3, ex);
2529 >
2530          g.complete(v1);
2531  
2532 <        r.assertNotInvoked();
2532 >        // unspecified behavior - both source completions available
2533 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2534 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2535 >        try {
2536 >            assertNull(h4.join());
2537 >            rs[4].assertInvoked();
2538 >        } catch (CompletionException ok) {
2539 >            checkCompletedWithWrappedException(h4, ex);
2540 >            rs[4].assertNotInvoked();
2541 >        }
2542 >        try {
2543 >            assertNull(h5.join());
2544 >            rs[5].assertInvoked();
2545 >        } catch (CompletionException ok) {
2546 >            checkCompletedWithWrappedException(h5, ex);
2547 >            rs[5].assertNotInvoked();
2548 >        }
2549 >
2550 >        checkCompletedExceptionally(f, ex);
2551          checkCompletedNormally(g, v1);
2552 <        checkCompletedWithWrappedCFException(f, ex);
2553 <        checkCompletedWithWrappedCFException(h, ex);
2552 >        checkCompletedWithWrappedException(h0, ex);
2553 >        checkCompletedWithWrappedException(h1, ex);
2554 >        checkCompletedWithWrappedException(h2, ex);
2555 >        checkCompletedWithWrappedException(h3, ex);
2556 >        checkCompletedWithWrappedException(h4, ex);
2557 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2558      }}
2559  
2560      public void testRunAfterEither_exceptionalCompletion2() {
2561          for (ExecutionMode m : ExecutionMode.values())
2562 +        for (boolean fFirst : new boolean[] { true, false })
2563          for (Integer v1 : new Integer[] { 1, null })
2564      {
2565          final CompletableFuture<Integer> f = new CompletableFuture<>();
2566          final CompletableFuture<Integer> g = new CompletableFuture<>();
2190        final Noop r = new Noop(m);
2191        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2192        final CFException ex = new CFException();
2193
2194        g.completeExceptionally(ex);
2195        checkCompletedWithWrappedCFException(h, ex);
2196        f.complete(v1);
2197
2198        r.assertNotInvoked();
2199        checkCompletedNormally(f, v1);
2200        checkCompletedWithWrappedCFException(g, ex);
2201        checkCompletedWithWrappedCFException(h, ex);
2202    }}
2203
2204    public void testRunAfterEither_exceptionalCompletion3() {
2205        for (ExecutionMode m : ExecutionMode.values())
2206        for (Integer v1 : new Integer[] { 1, null })
2207    {
2208        final CompletableFuture<Integer> f = new CompletableFuture<>();
2209        final CompletableFuture<Integer> g = new CompletableFuture<>();
2210        final Noop r = new Noop(m);
2567          final CFException ex = new CFException();
2568 +        final Noop[] rs = new Noop[6];
2569 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2570  
2571 <        g.completeExceptionally(ex);
2572 <        f.complete(v1);
2573 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2571 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2572 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2573 >        if (fFirst) {
2574 >            f.complete(v1);
2575 >            g.completeExceptionally(ex);
2576 >        } else {
2577 >            g.completeExceptionally(ex);
2578 >            f.complete(v1);
2579 >        }
2580 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2581 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2582  
2583 <        // unspecified behavior
2218 <        Integer v;
2583 >        // unspecified behavior - both source completions available
2584          try {
2585 <            assertNull(h.join());
2586 <            r.assertInvoked();
2585 >            assertEquals(null, h0.join());
2586 >            rs[0].assertInvoked();
2587          } catch (CompletionException ok) {
2588 <            checkCompletedWithWrappedCFException(h, ex);
2589 <            r.assertNotInvoked();
2588 >            checkCompletedWithWrappedException(h0, ex);
2589 >            rs[0].assertNotInvoked();
2590 >        }
2591 >        try {
2592 >            assertEquals(null, h1.join());
2593 >            rs[1].assertInvoked();
2594 >        } catch (CompletionException ok) {
2595 >            checkCompletedWithWrappedException(h1, ex);
2596 >            rs[1].assertNotInvoked();
2597 >        }
2598 >        try {
2599 >            assertEquals(null, h2.join());
2600 >            rs[2].assertInvoked();
2601 >        } catch (CompletionException ok) {
2602 >            checkCompletedWithWrappedException(h2, ex);
2603 >            rs[2].assertNotInvoked();
2604 >        }
2605 >        try {
2606 >            assertEquals(null, h3.join());
2607 >            rs[3].assertInvoked();
2608 >        } catch (CompletionException ok) {
2609 >            checkCompletedWithWrappedException(h3, ex);
2610 >            rs[3].assertNotInvoked();
2611          }
2612  
2227        checkCompletedWithWrappedCFException(g, ex);
2613          checkCompletedNormally(f, v1);
2614 +        checkCompletedExceptionally(g, ex);
2615      }}
2616  
2617 <    public void testRunAfterEither_exceptionalCompletion4() {
2617 >    /**
2618 >     * runAfterEither result completes exceptionally if either source cancelled
2619 >     */
2620 >    public void testRunAfterEither_sourceCancelled() {
2621          for (ExecutionMode m : ExecutionMode.values())
2622 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2623          for (Integer v1 : new Integer[] { 1, null })
2624      {
2625          final CompletableFuture<Integer> f = new CompletableFuture<>();
2626          final CompletableFuture<Integer> g = new CompletableFuture<>();
2627 <        final Noop r = new Noop(m);
2628 <        final CFException ex = new CFException();
2627 >        final Noop[] rs = new Noop[6];
2628 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2629 >
2630 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2631 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2632 >        checkIncomplete(h0);
2633 >        checkIncomplete(h1);
2634 >        rs[0].assertNotInvoked();
2635 >        rs[1].assertNotInvoked();
2636 >        f.cancel(mayInterruptIfRunning);
2637 >        checkCompletedWithWrappedCancellationException(h0);
2638 >        checkCompletedWithWrappedCancellationException(h1);
2639 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2640 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2641 >        checkCompletedWithWrappedCancellationException(h2);
2642 >        checkCompletedWithWrappedCancellationException(h3);
2643  
2240        f.completeExceptionally(ex);
2644          g.complete(v1);
2242        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2645  
2646 <        // unspecified behavior
2647 <        Integer v;
2646 >        // unspecified behavior - both source completions available
2647 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2648 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2649          try {
2650 <            assertNull(h.join());
2651 <            r.assertInvoked();
2650 >            assertNull(h4.join());
2651 >            rs[4].assertInvoked();
2652          } catch (CompletionException ok) {
2653 <            checkCompletedWithWrappedCFException(h, ex);
2654 <            r.assertNotInvoked();
2653 >            checkCompletedWithWrappedCancellationException(h4);
2654 >            rs[4].assertNotInvoked();
2655 >        }
2656 >        try {
2657 >            assertNull(h5.join());
2658 >            rs[5].assertInvoked();
2659 >        } catch (CompletionException ok) {
2660 >            checkCompletedWithWrappedCancellationException(h5);
2661 >            rs[5].assertNotInvoked();
2662          }
2663  
2664 <        checkCompletedWithWrappedCFException(f, ex);
2664 >        checkCancelled(f);
2665          checkCompletedNormally(g, v1);
2666 +        checkCompletedWithWrappedCancellationException(h0);
2667 +        checkCompletedWithWrappedCancellationException(h1);
2668 +        checkCompletedWithWrappedCancellationException(h2);
2669 +        checkCompletedWithWrappedCancellationException(h3);
2670 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2671      }}
2672  
2673      /**
2674       * runAfterEither result completes exceptionally if action does
2675       */
2676 <    public void testRunAfterEither_actionFailed1() {
2676 >    public void testRunAfterEither_actionFailed() {
2677          for (ExecutionMode m : ExecutionMode.values())
2678          for (Integer v1 : new Integer[] { 1, null })
2679          for (Integer v2 : new Integer[] { 2, null })
2680      {
2681          final CompletableFuture<Integer> f = new CompletableFuture<>();
2682          final CompletableFuture<Integer> g = new CompletableFuture<>();
2683 <        final FailingRunnable r = new FailingRunnable(m);
2684 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2683 >        final FailingRunnable[] rs = new FailingRunnable[6];
2684 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2685  
2686 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2687 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2688          f.complete(v1);
2689 <        checkCompletedWithWrappedCFException(h);
2689 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2690 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2691 >        checkCompletedWithWrappedCFException(h0);
2692 >        checkCompletedWithWrappedCFException(h1);
2693 >        checkCompletedWithWrappedCFException(h2);
2694 >        checkCompletedWithWrappedCFException(h3);
2695 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2696          g.complete(v2);
2697 <        checkCompletedNormally(f, v1);
2698 <        checkCompletedNormally(g, v2);
2699 <    }}
2700 <
2278 <    public void testRunAfterEither_actionFailed2() {
2279 <        for (ExecutionMode m : ExecutionMode.values())
2280 <        for (Integer v1 : new Integer[] { 1, null })
2281 <        for (Integer v2 : new Integer[] { 2, null })
2282 <    {
2283 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2284 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2285 <        final FailingRunnable r = new FailingRunnable(m);
2286 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2697 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2698 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2699 >        checkCompletedWithWrappedCFException(h4);
2700 >        checkCompletedWithWrappedCFException(h5);
2701  
2288        g.complete(v2);
2289        checkCompletedWithWrappedCFException(h);
2290        f.complete(v1);
2702          checkCompletedNormally(f, v1);
2703          checkCompletedNormally(g, v2);
2704 <    }}
2294 <
2295 <    /**
2296 <     * runAfterEither result completes exceptionally if either source cancelled
2297 <     */
2298 <    public void testRunAfterEither_sourceCancelled1() {
2299 <        for (ExecutionMode m : ExecutionMode.values())
2300 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2301 <        for (Integer v1 : new Integer[] { 1, null })
2302 <    {
2303 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2304 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2305 <        final Noop r = new Noop(m);
2306 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2307 <
2308 <        assertTrue(f.cancel(mayInterruptIfRunning));
2309 <        checkCompletedWithWrappedCancellationException(h);
2310 <        g.complete(v1);
2311 <
2312 <        checkCancelled(f);
2313 <        r.assertNotInvoked();
2314 <        checkCompletedNormally(g, v1);
2315 <        checkCompletedWithWrappedCancellationException(h);
2316 <    }}
2317 <
2318 <    public void testRunAfterEither_sourceCancelled2() {
2319 <        for (ExecutionMode m : ExecutionMode.values())
2320 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2321 <        for (Integer v1 : new Integer[] { 1, null })
2322 <    {
2323 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2324 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2325 <        final Noop r = new Noop(m);
2326 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2327 <
2328 <        assertTrue(g.cancel(mayInterruptIfRunning));
2329 <        checkCompletedWithWrappedCancellationException(h);
2330 <        f.complete(v1);
2331 <
2332 <        checkCancelled(g);
2333 <        r.assertNotInvoked();
2334 <        checkCompletedNormally(f, v1);
2335 <        checkCompletedWithWrappedCancellationException(h);
2336 <    }}
2337 <
2338 <    public void testRunAfterEither_sourceCancelled3() {
2339 <        for (ExecutionMode m : ExecutionMode.values())
2340 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2341 <        for (Integer v1 : new Integer[] { 1, null })
2342 <    {
2343 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2344 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2345 <        final Noop r = new Noop(m);
2346 <
2347 <        assertTrue(g.cancel(mayInterruptIfRunning));
2348 <        f.complete(v1);
2349 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2350 <
2351 <        // unspecified behavior
2352 <        Integer v;
2353 <        try {
2354 <            assertNull(h.join());
2355 <            r.assertInvoked();
2356 <        } catch (CompletionException ok) {
2357 <            checkCompletedWithWrappedCancellationException(h);
2358 <            r.assertNotInvoked();
2359 <        }
2360 <
2361 <        checkCancelled(g);
2362 <        checkCompletedNormally(f, v1);
2363 <    }}
2364 <
2365 <    public void testRunAfterEither_sourceCancelled4() {
2366 <        for (ExecutionMode m : ExecutionMode.values())
2367 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2368 <        for (Integer v1 : new Integer[] { 1, null })
2369 <    {
2370 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2371 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2372 <        final Noop r = new Noop(m);
2373 <
2374 <        assertTrue(f.cancel(mayInterruptIfRunning));
2375 <        g.complete(v1);
2376 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2377 <
2378 <        // unspecified behavior
2379 <        Integer v;
2380 <        try {
2381 <            assertNull(h.join());
2382 <            r.assertInvoked();
2383 <        } catch (CompletionException ok) {
2384 <            checkCompletedWithWrappedCancellationException(h);
2385 <            r.assertNotInvoked();
2386 <        }
2387 <
2388 <        checkCancelled(f);
2389 <        checkCompletedNormally(g, v1);
2704 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2705      }}
2706  
2707      /**
# Line 2405 | Line 2720 | public class CompletableFutureTest exten
2720  
2721          checkCompletedNormally(g, inc(v1));
2722          checkCompletedNormally(f, v1);
2723 <        r.assertInvoked();
2723 >        r.assertValue(v1);
2724      }}
2725  
2726      /**
# Line 2423 | Line 2738 | public class CompletableFutureTest exten
2738          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2739          if (createIncomplete) f.completeExceptionally(ex);
2740  
2741 <        checkCompletedWithWrappedCFException(g, ex);
2742 <        checkCompletedWithWrappedCFException(f, ex);
2741 >        checkCompletedWithWrappedException(g, ex);
2742 >        checkCompletedExceptionally(f, ex);
2743          r.assertNotInvoked();
2744      }}
2745  
# Line 2680 | Line 2995 | public class CompletableFutureTest exten
2995          assertSame(f, f.toCompletableFuture());
2996      }
2997  
2683    /**
2684     * whenComplete action executes on normal completion, propagating
2685     * source result.
2686     */
2687    public void testWhenComplete_normalCompletion1() {
2688        for (ExecutionMode m : ExecutionMode.values())
2689        for (boolean createIncomplete : new boolean[] { true, false })
2690        for (Integer v1 : new Integer[] { 1, null })
2691    {
2692        final AtomicInteger a = new AtomicInteger(0);
2693        final CompletableFuture<Integer> f = new CompletableFuture<>();
2694        if (!createIncomplete) f.complete(v1);
2695        final CompletableFuture<Integer> g = m.whenComplete
2696            (f,
2697             (Integer x, Throwable t) -> {
2698                threadAssertSame(x, v1);
2699                threadAssertNull(t);
2700                a.getAndIncrement();
2701            });
2702        if (createIncomplete) f.complete(v1);
2703
2704        checkCompletedNormally(g, v1);
2705        checkCompletedNormally(f, v1);
2706        assertEquals(1, a.get());
2707    }}
2708
2709    /**
2710     * whenComplete action executes on exceptional completion, propagating
2711     * source result.
2712     */
2713    public void testWhenComplete_exceptionalCompletion() {
2714        for (ExecutionMode m : ExecutionMode.values())
2715        for (boolean createIncomplete : new boolean[] { true, false })
2716        for (Integer v1 : new Integer[] { 1, null })
2717    {
2718        final AtomicInteger a = new AtomicInteger(0);
2719        final CFException ex = new CFException();
2720        final CompletableFuture<Integer> f = new CompletableFuture<>();
2721        if (!createIncomplete) f.completeExceptionally(ex);
2722        final CompletableFuture<Integer> g = m.whenComplete
2723            (f,
2724             (Integer x, Throwable t) -> {
2725                threadAssertNull(x);
2726                threadAssertSame(t, ex);
2727                a.getAndIncrement();
2728            });
2729        if (createIncomplete) f.completeExceptionally(ex);
2730        checkCompletedWithWrappedCFException(f, ex);
2731        checkCompletedWithWrappedCFException(g, ex);
2732        assertEquals(1, a.get());
2733    }}
2734
2735    /**
2736     * whenComplete action executes on cancelled source, propagating
2737     * CancellationException.
2738     */
2739    public void testWhenComplete_sourceCancelled() {
2740        for (ExecutionMode m : ExecutionMode.values())
2741        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2742        for (boolean createIncomplete : new boolean[] { true, false })
2743    {
2744        final AtomicInteger a = new AtomicInteger(0);
2745        final CompletableFuture<Integer> f = new CompletableFuture<>();
2746        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2747        final CompletableFuture<Integer> g = m.whenComplete
2748            (f,
2749             (Integer x, Throwable t) -> {
2750                threadAssertNull(x);
2751                threadAssertTrue(t instanceof CancellationException);
2752                a.getAndIncrement();
2753            });
2754        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2755
2756        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2757        checkCompletedWithWrappedCancellationException(g);
2758        checkCancelled(f);
2759        assertEquals(1, a.get());
2760    }}
2761
2762    /**
2763     * If a whenComplete action throws an exception when triggered by
2764     * a normal completion, it completes exceptionally
2765     */
2766    public void testWhenComplete_actionFailed() {
2767        for (boolean createIncomplete : new boolean[] { true, false })
2768        for (ExecutionMode m : ExecutionMode.values())
2769        for (Integer v1 : new Integer[] { 1, null })
2770    {
2771        final AtomicInteger a = new AtomicInteger(0);
2772        final CFException ex = new CFException();
2773        final CompletableFuture<Integer> f = new CompletableFuture<>();
2774        if (!createIncomplete) f.complete(v1);
2775        final CompletableFuture<Integer> g = m.whenComplete
2776            (f,
2777             (Integer x, Throwable t) -> {
2778                threadAssertSame(x, v1);
2779                threadAssertNull(t);
2780                a.getAndIncrement();
2781                throw ex;
2782            });
2783        if (createIncomplete) f.complete(v1);
2784        checkCompletedNormally(f, v1);
2785        checkCompletedWithWrappedCFException(g, ex);
2786        assertEquals(1, a.get());
2787    }}
2788
2789    /**
2790     * If a whenComplete action throws an exception when triggered by
2791     * a source completion that also throws an exception, the source
2792     * exception takes precedence.
2793     */
2794    public void testWhenComplete_actionFailedSourceFailed() {
2795        for (boolean createIncomplete : new boolean[] { true, false })
2796        for (ExecutionMode m : ExecutionMode.values())
2797        for (Integer v1 : new Integer[] { 1, null })
2798    {
2799        final AtomicInteger a = new AtomicInteger(0);
2800        final CFException ex1 = new CFException();
2801        final CFException ex2 = new CFException();
2802        final CompletableFuture<Integer> f = new CompletableFuture<>();
2803
2804        if (!createIncomplete) f.completeExceptionally(ex1);
2805        final CompletableFuture<Integer> g = m.whenComplete
2806            (f,
2807             (Integer x, Throwable t) -> {
2808                threadAssertSame(t, ex1);
2809                threadAssertNull(x);
2810                a.getAndIncrement();
2811                throw ex2;
2812            });
2813        if (createIncomplete) f.completeExceptionally(ex1);
2814
2815        checkCompletedWithWrappedCFException(f, ex1);
2816        checkCompletedWithWrappedCFException(g, ex1);
2817        assertEquals(1, a.get());
2818    }}
2819
2998   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines