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.64 by jsr166, Fri Jun 6 17:08:48 2014 UTC vs.
Revision 1.75 by jsr166, Sat Jun 7 21:14:42 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
# Line 310 | Line 345 | public class CompletableFutureTest exten
345          f = new CompletableFuture<String>();
346          f.completeExceptionally(new IndexOutOfBoundsException());
347          assertTrue(f.toString().contains("[Completed exceptionally]"));
348 +
349 +        f = new CompletableFuture<String>();
350 +        f.cancel(true);
351 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
352 +
353 +        f = new CompletableFuture<String>();
354 +        f.cancel(false);
355 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
356      }
357  
358      /**
# Line 808 | Line 851 | public class CompletableFutureTest exten
851          assertEquals(0, a.get());
852      }}
853  
811
854      /**
855       * exceptionally action completes with function value on source
856       * exception
# Line 852 | Line 894 | public class CompletableFutureTest exten
894              });
895          if (createIncomplete) f.completeExceptionally(ex1);
896  
897 <        checkCompletedWithWrappedCFException(g, ex2);
897 >        checkCompletedWithWrappedException(g, ex2);
898 >        assertEquals(1, a.get());
899 >    }}
900 >
901 >    /**
902 >     * whenComplete action executes on normal completion, propagating
903 >     * source result.
904 >     */
905 >    public void testWhenComplete_normalCompletion1() {
906 >        for (ExecutionMode m : ExecutionMode.values())
907 >        for (boolean createIncomplete : new boolean[] { true, false })
908 >        for (Integer v1 : new Integer[] { 1, null })
909 >    {
910 >        final AtomicInteger a = new AtomicInteger(0);
911 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
912 >        if (!createIncomplete) f.complete(v1);
913 >        final CompletableFuture<Integer> g = m.whenComplete
914 >            (f,
915 >             (Integer x, Throwable t) -> {
916 >                m.checkExecutionMode();
917 >                threadAssertSame(x, v1);
918 >                threadAssertNull(t);
919 >                a.getAndIncrement();
920 >            });
921 >        if (createIncomplete) f.complete(v1);
922 >
923 >        checkCompletedNormally(g, v1);
924 >        checkCompletedNormally(f, v1);
925 >        assertEquals(1, a.get());
926 >    }}
927 >
928 >    /**
929 >     * whenComplete action executes on exceptional completion, propagating
930 >     * source result.
931 >     */
932 >    public void testWhenComplete_exceptionalCompletion() {
933 >        for (ExecutionMode m : ExecutionMode.values())
934 >        for (boolean createIncomplete : new boolean[] { true, false })
935 >        for (Integer v1 : new Integer[] { 1, null })
936 >    {
937 >        final AtomicInteger a = new AtomicInteger(0);
938 >        final CFException ex = new CFException();
939 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
940 >        if (!createIncomplete) f.completeExceptionally(ex);
941 >        final CompletableFuture<Integer> g = m.whenComplete
942 >            (f,
943 >             (Integer x, Throwable t) -> {
944 >                m.checkExecutionMode();
945 >                threadAssertNull(x);
946 >                threadAssertSame(t, ex);
947 >                a.getAndIncrement();
948 >            });
949 >        if (createIncomplete) f.completeExceptionally(ex);
950 >
951 >        checkCompletedWithWrappedException(g, ex);
952 >        checkCompletedExceptionally(f, ex);
953 >        assertEquals(1, a.get());
954 >    }}
955 >
956 >    /**
957 >     * whenComplete action executes on cancelled source, propagating
958 >     * CancellationException.
959 >     */
960 >    public void testWhenComplete_sourceCancelled() {
961 >        for (ExecutionMode m : ExecutionMode.values())
962 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
963 >        for (boolean createIncomplete : new boolean[] { true, false })
964 >    {
965 >        final AtomicInteger a = new AtomicInteger(0);
966 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
967 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
968 >        final CompletableFuture<Integer> g = m.whenComplete
969 >            (f,
970 >             (Integer x, Throwable t) -> {
971 >                m.checkExecutionMode();
972 >                threadAssertNull(x);
973 >                threadAssertTrue(t instanceof CancellationException);
974 >                a.getAndIncrement();
975 >            });
976 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
977 >
978 >        checkCompletedWithWrappedCancellationException(g);
979 >        checkCancelled(f);
980 >        assertEquals(1, a.get());
981 >    }}
982 >
983 >    /**
984 >     * If a whenComplete action throws an exception when triggered by
985 >     * a normal completion, it completes exceptionally
986 >     */
987 >    public void testWhenComplete_actionFailed() {
988 >        for (boolean createIncomplete : new boolean[] { true, false })
989 >        for (ExecutionMode m : ExecutionMode.values())
990 >        for (Integer v1 : new Integer[] { 1, null })
991 >    {
992 >        final AtomicInteger a = new AtomicInteger(0);
993 >        final CFException ex = new CFException();
994 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
995 >        if (!createIncomplete) f.complete(v1);
996 >        final CompletableFuture<Integer> g = m.whenComplete
997 >            (f,
998 >             (Integer x, Throwable t) -> {
999 >                m.checkExecutionMode();
1000 >                threadAssertSame(x, v1);
1001 >                threadAssertNull(t);
1002 >                a.getAndIncrement();
1003 >                throw ex;
1004 >            });
1005 >        if (createIncomplete) f.complete(v1);
1006 >
1007 >        checkCompletedWithWrappedException(g, ex);
1008 >        checkCompletedNormally(f, v1);
1009 >        assertEquals(1, a.get());
1010 >    }}
1011 >
1012 >    /**
1013 >     * If a whenComplete action throws an exception when triggered by
1014 >     * a source completion that also throws an exception, the source
1015 >     * exception takes precedence.
1016 >     */
1017 >    public void testWhenComplete_actionFailedSourceFailed() {
1018 >        for (boolean createIncomplete : new boolean[] { true, false })
1019 >        for (ExecutionMode m : ExecutionMode.values())
1020 >        for (Integer v1 : new Integer[] { 1, null })
1021 >    {
1022 >        final AtomicInteger a = new AtomicInteger(0);
1023 >        final CFException ex1 = new CFException();
1024 >        final CFException ex2 = new CFException();
1025 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1026 >
1027 >        if (!createIncomplete) f.completeExceptionally(ex1);
1028 >        final CompletableFuture<Integer> g = m.whenComplete
1029 >            (f,
1030 >             (Integer x, Throwable t) -> {
1031 >                m.checkExecutionMode();
1032 >                threadAssertSame(t, ex1);
1033 >                threadAssertNull(x);
1034 >                a.getAndIncrement();
1035 >                throw ex2;
1036 >            });
1037 >        if (createIncomplete) f.completeExceptionally(ex1);
1038 >
1039 >        checkCompletedWithWrappedException(g, ex1);
1040 >        checkCompletedExceptionally(f, ex1);
1041          assertEquals(1, a.get());
1042      }}
1043  
# Line 909 | Line 1094 | public class CompletableFutureTest exten
1094          if (createIncomplete) f.completeExceptionally(ex);
1095  
1096          checkCompletedNormally(g, v1);
1097 <        checkCompletedWithWrappedCFException(f, ex);
1097 >        checkCompletedExceptionally(f, ex);
1098          assertEquals(1, a.get());
1099      }}
1100  
# Line 965 | Line 1150 | public class CompletableFutureTest exten
1150              });
1151          if (createIncomplete) f.completeExceptionally(ex1);
1152  
1153 <        checkCompletedWithWrappedCFException(g, ex2);
1154 <        checkCompletedWithWrappedCFException(f, ex1);
1153 >        checkCompletedWithWrappedException(g, ex2);
1154 >        checkCompletedExceptionally(f, ex1);
1155          assertEquals(1, a.get());
1156      }}
1157  
# Line 990 | Line 1175 | public class CompletableFutureTest exten
1175              });
1176          if (createIncomplete) f.complete(v1);
1177  
1178 <        checkCompletedWithWrappedCFException(g, ex);
1178 >        checkCompletedWithWrappedException(g, ex);
1179          checkCompletedNormally(f, v1);
1180          assertEquals(1, a.get());
1181      }}
# Line 1104 | Line 1289 | public class CompletableFutureTest exten
1289              f.completeExceptionally(ex);
1290          }
1291  
1292 <        checkCompletedWithWrappedCFException(g, ex);
1293 <        checkCompletedWithWrappedCFException(f, ex);
1292 >        checkCompletedWithWrappedException(g, ex);
1293 >        checkCompletedExceptionally(f, ex);
1294          r.assertNotInvoked();
1295      }}
1296  
# Line 1171 | Line 1356 | public class CompletableFutureTest exten
1356  
1357          checkCompletedNormally(g, inc(v1));
1358          checkCompletedNormally(f, v1);
1359 <        r.assertInvoked();
1359 >        r.assertValue(inc(v1));
1360      }}
1361  
1362      /**
# Line 1192 | Line 1377 | public class CompletableFutureTest exten
1377              f.completeExceptionally(ex);
1378          }
1379  
1380 <        checkCompletedWithWrappedCFException(g, ex);
1381 <        checkCompletedWithWrappedCFException(f, ex);
1380 >        checkCompletedWithWrappedException(g, ex);
1381 >        checkCompletedExceptionally(f, ex);
1382          r.assertNotInvoked();
1383      }}
1384  
# Line 1280 | Line 1465 | public class CompletableFutureTest exten
1465              f.completeExceptionally(ex);
1466          }
1467  
1468 <        checkCompletedWithWrappedCFException(g, ex);
1469 <        checkCompletedWithWrappedCFException(f, ex);
1468 >        checkCompletedWithWrappedException(g, ex);
1469 >        checkCompletedExceptionally(f, ex);
1470          r.assertNotInvoked();
1471      }}
1472  
# Line 1356 | Line 1541 | public class CompletableFutureTest exten
1541          checkCompletedNormally(h, subtract(v1, v2));
1542          checkCompletedNormally(f, v1);
1543          checkCompletedNormally(g, v2);
1544 <        r.assertInvoked();
1544 >        r.assertValue(subtract(v1, v2));
1545      }}
1546  
1547      /**
# Line 1383 | Line 1568 | public class CompletableFutureTest exten
1568              (!fFirst ? f : g).completeExceptionally(ex);
1569          }
1570  
1571 <        checkCompletedWithWrappedCFException(h, ex);
1571 >        checkCompletedWithWrappedException(h, ex);
1572          r.assertNotInvoked();
1573          checkCompletedNormally(fFirst ? f : g, v1);
1574 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1574 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1575      }}
1576  
1577      /**
# Line 1500 | Line 1685 | public class CompletableFutureTest exten
1685              (!fFirst ? f : g).completeExceptionally(ex);
1686          }
1687  
1688 <        checkCompletedWithWrappedCFException(h, ex);
1688 >        checkCompletedWithWrappedException(h, ex);
1689          r.assertNotInvoked();
1690          checkCompletedNormally(fFirst ? f : g, v1);
1691 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1691 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1692      }}
1693  
1694      /**
# Line 1617 | Line 1802 | public class CompletableFutureTest exten
1802              (!fFirst ? f : g).completeExceptionally(ex);
1803          }
1804  
1805 <        checkCompletedWithWrappedCFException(h, ex);
1805 >        checkCompletedWithWrappedException(h, ex);
1806          r.assertNotInvoked();
1807          checkCompletedNormally(fFirst ? f : g, v1);
1808 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1808 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1809      }}
1810  
1811      /**
# Line 1637 | Line 1822 | public class CompletableFutureTest exten
1822          final CompletableFuture<Integer> g = new CompletableFuture<>();
1823          final Noop r = new Noop(m);
1824  
1640
1825          (fFirst ? f : g).complete(v1);
1826          if (!createIncomplete)
1827              assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
# Line 1752 | Line 1936 | public class CompletableFutureTest exten
1936          rs[0].assertNotInvoked();
1937          rs[1].assertNotInvoked();
1938          f.completeExceptionally(ex);
1939 <        checkCompletedWithWrappedCFException(h0, ex);
1940 <        checkCompletedWithWrappedCFException(h1, ex);
1939 >        checkCompletedWithWrappedException(h0, ex);
1940 >        checkCompletedWithWrappedException(h1, ex);
1941          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1942          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1943 <        checkCompletedWithWrappedCFException(h2, ex);
1944 <        checkCompletedWithWrappedCFException(h3, ex);
1943 >        checkCompletedWithWrappedException(h2, ex);
1944 >        checkCompletedWithWrappedException(h3, ex);
1945          g.complete(v1);
1946  
1947          // unspecified behavior - both source completions available
# Line 1765 | Line 1949 | public class CompletableFutureTest exten
1949          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1950          try {
1951              assertEquals(inc(v1), h4.join());
1952 <            rs[4].assertInvoked();
1952 >            rs[4].assertValue(inc(v1));
1953          } catch (CompletionException ok) {
1954 <            checkCompletedWithWrappedCFException(h4, ex);
1954 >            checkCompletedWithWrappedException(h4, ex);
1955              rs[4].assertNotInvoked();
1956          }
1957          try {
1958              assertEquals(inc(v1), h5.join());
1959 <            rs[5].assertInvoked();
1959 >            rs[5].assertValue(inc(v1));
1960          } catch (CompletionException ok) {
1961 <            checkCompletedWithWrappedCFException(h5, ex);
1961 >            checkCompletedWithWrappedException(h5, ex);
1962              rs[5].assertNotInvoked();
1963          }
1964  
1965 <        checkCompletedWithWrappedCFException(f, ex);
1965 >        checkCompletedExceptionally(f, ex);
1966          checkCompletedNormally(g, v1);
1967 <        checkCompletedWithWrappedCFException(h0, ex);
1968 <        checkCompletedWithWrappedCFException(h1, ex);
1969 <        checkCompletedWithWrappedCFException(h2, ex);
1970 <        checkCompletedWithWrappedCFException(h3, ex);
1971 <        checkCompletedWithWrappedCFException(h4, ex);
1967 >        checkCompletedWithWrappedException(h0, ex);
1968 >        checkCompletedWithWrappedException(h1, ex);
1969 >        checkCompletedWithWrappedException(h2, ex);
1970 >        checkCompletedWithWrappedException(h3, ex);
1971 >        checkCompletedWithWrappedException(h4, ex);
1972          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1973      }}
1974  
1975 +    public void testApplyToEither_exceptionalCompletion2() {
1976 +        for (ExecutionMode m : ExecutionMode.values())
1977 +        for (boolean fFirst : new boolean[] { true, false })
1978 +        for (Integer v1 : new Integer[] { 1, null })
1979 +    {
1980 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1981 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1982 +        final CFException ex = new CFException();
1983 +        final IncFunction[] rs = new IncFunction[6];
1984 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1985 +
1986 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1987 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1988 +        if (fFirst) {
1989 +            f.complete(v1);
1990 +            g.completeExceptionally(ex);
1991 +        } else {
1992 +            g.completeExceptionally(ex);
1993 +            f.complete(v1);
1994 +        }
1995 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1996 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1997 +
1998 +        // unspecified behavior - both source completions available
1999 +        try {
2000 +            assertEquals(inc(v1), h0.join());
2001 +            rs[0].assertValue(inc(v1));
2002 +        } catch (CompletionException ok) {
2003 +            checkCompletedWithWrappedException(h0, ex);
2004 +            rs[0].assertNotInvoked();
2005 +        }
2006 +        try {
2007 +            assertEquals(inc(v1), h1.join());
2008 +            rs[1].assertValue(inc(v1));
2009 +        } catch (CompletionException ok) {
2010 +            checkCompletedWithWrappedException(h1, ex);
2011 +            rs[1].assertNotInvoked();
2012 +        }
2013 +        try {
2014 +            assertEquals(inc(v1), h2.join());
2015 +            rs[2].assertValue(inc(v1));
2016 +        } catch (CompletionException ok) {
2017 +            checkCompletedWithWrappedException(h2, ex);
2018 +            rs[2].assertNotInvoked();
2019 +        }
2020 +        try {
2021 +            assertEquals(inc(v1), h3.join());
2022 +            rs[3].assertValue(inc(v1));
2023 +        } catch (CompletionException ok) {
2024 +            checkCompletedWithWrappedException(h3, ex);
2025 +            rs[3].assertNotInvoked();
2026 +        }
2027 +
2028 +        checkCompletedNormally(f, v1);
2029 +        checkCompletedExceptionally(g, ex);
2030 +    }}
2031 +
2032      /**
2033       * applyToEither result completes exceptionally if either source cancelled
2034       */
# Line 1821 | Line 2062 | public class CompletableFutureTest exten
2062          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2063          try {
2064              assertEquals(inc(v1), h4.join());
2065 <            rs[4].assertInvoked();
2065 >            rs[4].assertValue(inc(v1));
2066          } catch (CompletionException ok) {
2067              checkCompletedWithWrappedCancellationException(h4);
2068              rs[4].assertNotInvoked();
2069          }
2070          try {
2071              assertEquals(inc(v1), h5.join());
2072 <            rs[5].assertInvoked();
2072 >            rs[5].assertValue(inc(v1));
2073          } catch (CompletionException ok) {
2074              checkCompletedWithWrappedCancellationException(h5);
2075              rs[5].assertNotInvoked();
# Line 1843 | Line 2084 | public class CompletableFutureTest exten
2084          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2085      }}
2086  
2087 +    public void testApplyToEither_sourceCancelled2() {
2088 +        for (ExecutionMode m : ExecutionMode.values())
2089 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2090 +        for (boolean fFirst : new boolean[] { true, false })
2091 +        for (Integer v1 : new Integer[] { 1, null })
2092 +    {
2093 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2094 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2095 +        final IncFunction[] rs = new IncFunction[6];
2096 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2097 +
2098 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2099 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2100 +        if (fFirst) {
2101 +            f.complete(v1);
2102 +            g.cancel(mayInterruptIfRunning);
2103 +        } else {
2104 +            g.cancel(mayInterruptIfRunning);
2105 +            f.complete(v1);
2106 +        }
2107 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2108 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2109 +
2110 +        // unspecified behavior - both source completions available
2111 +        try {
2112 +            assertEquals(inc(v1), h0.join());
2113 +            rs[0].assertValue(inc(v1));
2114 +        } catch (CompletionException ok) {
2115 +            checkCompletedWithWrappedCancellationException(h0);
2116 +            rs[0].assertNotInvoked();
2117 +        }
2118 +        try {
2119 +            assertEquals(inc(v1), h1.join());
2120 +            rs[1].assertValue(inc(v1));
2121 +        } catch (CompletionException ok) {
2122 +            checkCompletedWithWrappedCancellationException(h1);
2123 +            rs[1].assertNotInvoked();
2124 +        }
2125 +        try {
2126 +            assertEquals(inc(v1), h2.join());
2127 +            rs[2].assertValue(inc(v1));
2128 +        } catch (CompletionException ok) {
2129 +            checkCompletedWithWrappedCancellationException(h2);
2130 +            rs[2].assertNotInvoked();
2131 +        }
2132 +        try {
2133 +            assertEquals(inc(v1), h3.join());
2134 +            rs[3].assertValue(inc(v1));
2135 +        } catch (CompletionException ok) {
2136 +            checkCompletedWithWrappedCancellationException(h3);
2137 +            rs[3].assertNotInvoked();
2138 +        }
2139 +
2140 +        checkCompletedNormally(f, v1);
2141 +        checkCancelled(g);
2142 +    }}
2143 +
2144      /**
2145       * applyToEither result completes exceptionally if action does
2146       */
# Line 1957 | Line 2255 | public class CompletableFutureTest exten
2255          rs[0].assertNotInvoked();
2256          rs[1].assertNotInvoked();
2257          f.completeExceptionally(ex);
2258 <        checkCompletedWithWrappedCFException(h0, ex);
2259 <        checkCompletedWithWrappedCFException(h1, ex);
2258 >        checkCompletedWithWrappedException(h0, ex);
2259 >        checkCompletedWithWrappedException(h1, ex);
2260          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2261          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2262 <        checkCompletedWithWrappedCFException(h2, ex);
2263 <        checkCompletedWithWrappedCFException(h3, ex);
2262 >        checkCompletedWithWrappedException(h2, ex);
2263 >        checkCompletedWithWrappedException(h3, ex);
2264  
2265          g.complete(v1);
2266  
# Line 1973 | Line 2271 | public class CompletableFutureTest exten
2271              assertNull(h4.join());
2272              rs[4].assertValue(v1);
2273          } catch (CompletionException ok) {
2274 <            checkCompletedWithWrappedCFException(h4, ex);
2274 >            checkCompletedWithWrappedException(h4, ex);
2275              rs[4].assertNotInvoked();
2276          }
2277          try {
2278              assertNull(h5.join());
2279              rs[5].assertValue(v1);
2280          } catch (CompletionException ok) {
2281 <            checkCompletedWithWrappedCFException(h5, ex);
2281 >            checkCompletedWithWrappedException(h5, ex);
2282              rs[5].assertNotInvoked();
2283          }
2284  
2285 <        checkCompletedWithWrappedCFException(f, ex);
2285 >        checkCompletedExceptionally(f, ex);
2286          checkCompletedNormally(g, v1);
2287 <        checkCompletedWithWrappedCFException(h0, ex);
2288 <        checkCompletedWithWrappedCFException(h1, ex);
2289 <        checkCompletedWithWrappedCFException(h2, ex);
2290 <        checkCompletedWithWrappedCFException(h3, ex);
2291 <        checkCompletedWithWrappedCFException(h4, ex);
2287 >        checkCompletedWithWrappedException(h0, ex);
2288 >        checkCompletedWithWrappedException(h1, ex);
2289 >        checkCompletedWithWrappedException(h2, ex);
2290 >        checkCompletedWithWrappedException(h3, ex);
2291 >        checkCompletedWithWrappedException(h4, ex);
2292          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2293      }}
2294  
2295 +    public void testAcceptEither_exceptionalCompletion2() {
2296 +        for (ExecutionMode m : ExecutionMode.values())
2297 +        for (boolean fFirst : new boolean[] { true, false })
2298 +        for (Integer v1 : new Integer[] { 1, null })
2299 +    {
2300 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2301 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2302 +        final CFException ex = new CFException();
2303 +        final NoopConsumer[] rs = new NoopConsumer[6];
2304 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2305 +
2306 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2307 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2308 +        if (fFirst) {
2309 +            f.complete(v1);
2310 +            g.completeExceptionally(ex);
2311 +        } else {
2312 +            g.completeExceptionally(ex);
2313 +            f.complete(v1);
2314 +        }
2315 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2316 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2317 +
2318 +        // unspecified behavior - both source completions available
2319 +        try {
2320 +            assertEquals(null, h0.join());
2321 +            rs[0].assertValue(v1);
2322 +        } catch (CompletionException ok) {
2323 +            checkCompletedWithWrappedException(h0, ex);
2324 +            rs[0].assertNotInvoked();
2325 +        }
2326 +        try {
2327 +            assertEquals(null, h1.join());
2328 +            rs[1].assertValue(v1);
2329 +        } catch (CompletionException ok) {
2330 +            checkCompletedWithWrappedException(h1, ex);
2331 +            rs[1].assertNotInvoked();
2332 +        }
2333 +        try {
2334 +            assertEquals(null, h2.join());
2335 +            rs[2].assertValue(v1);
2336 +        } catch (CompletionException ok) {
2337 +            checkCompletedWithWrappedException(h2, ex);
2338 +            rs[2].assertNotInvoked();
2339 +        }
2340 +        try {
2341 +            assertEquals(null, h3.join());
2342 +            rs[3].assertValue(v1);
2343 +        } catch (CompletionException ok) {
2344 +            checkCompletedWithWrappedException(h3, ex);
2345 +            rs[3].assertNotInvoked();
2346 +        }
2347 +
2348 +        checkCompletedNormally(f, v1);
2349 +        checkCompletedExceptionally(g, ex);
2350 +    }}
2351 +
2352      /**
2353       * acceptEither result completes exceptionally if either source cancelled
2354       */
# Line 2095 | Line 2450 | public class CompletableFutureTest exten
2450       * runAfterEither result completes normally after normal completion
2451       * of either source
2452       */
2453 <    public void testRunAfterEither_normalCompletion1() {
2453 >    public void testRunAfterEither_normalCompletion() {
2454          for (ExecutionMode m : ExecutionMode.values())
2455          for (Integer v1 : new Integer[] { 1, null })
2456          for (Integer v2 : new Integer[] { 2, null })
2457      {
2458          final CompletableFuture<Integer> f = new CompletableFuture<>();
2459          final CompletableFuture<Integer> g = new CompletableFuture<>();
2460 <        final Noop r = new Noop(m);
2461 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2460 >        final Noop[] rs = new Noop[6];
2461 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2462  
2463 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2464 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2465 +        checkIncomplete(h0);
2466 +        checkIncomplete(h1);
2467 +        rs[0].assertNotInvoked();
2468 +        rs[1].assertNotInvoked();
2469          f.complete(v1);
2470 <        checkCompletedNormally(h, null);
2471 <        r.assertInvoked();
2472 <        g.complete(v2);
2473 <
2474 <        checkCompletedNormally(f, v1);
2475 <        checkCompletedNormally(g, v2);
2476 <        checkCompletedNormally(h, null);
2477 <        r.assertInvoked();
2478 <    }}
2479 <
2119 <    public void testRunAfterEither_normalCompletion2() {
2120 <        for (ExecutionMode m : ExecutionMode.values())
2121 <        for (Integer v1 : new Integer[] { 1, null })
2122 <        for (Integer v2 : new Integer[] { 2, null })
2123 <    {
2124 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2125 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2126 <        final Noop r = new Noop(m);
2127 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2470 >        checkCompletedNormally(h0, null);
2471 >        checkCompletedNormally(h1, null);
2472 >        rs[0].assertInvoked();
2473 >        rs[1].assertInvoked();
2474 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2475 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2476 >        checkCompletedNormally(h2, null);
2477 >        checkCompletedNormally(h3, null);
2478 >        rs[2].assertInvoked();
2479 >        rs[3].assertInvoked();
2480  
2481          g.complete(v2);
2130        checkCompletedNormally(h, null);
2131        r.assertInvoked();
2132        f.complete(v1);
2482  
2483 <        checkCompletedNormally(f, v1);
2484 <        checkCompletedNormally(g, v2);
2136 <        checkCompletedNormally(h, null);
2137 <        r.assertInvoked();
2138 <        }}
2483 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2484 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2485  
2140    public void testRunAfterEither_normalCompletion3() {
2141        for (ExecutionMode m : ExecutionMode.values())
2142        for (Integer v1 : new Integer[] { 1, null })
2143        for (Integer v2 : new Integer[] { 2, null })
2144    {
2145        final CompletableFuture<Integer> f = new CompletableFuture<>();
2146        final CompletableFuture<Integer> g = new CompletableFuture<>();
2147        final Noop r = new Noop(m);
2148
2149        f.complete(v1);
2150        g.complete(v2);
2151        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2152
2153        checkCompletedNormally(h, null);
2486          checkCompletedNormally(f, v1);
2487          checkCompletedNormally(g, v2);
2488 <        r.assertInvoked();
2488 >        checkCompletedNormally(h0, null);
2489 >        checkCompletedNormally(h1, null);
2490 >        checkCompletedNormally(h2, null);
2491 >        checkCompletedNormally(h3, null);
2492 >        checkCompletedNormally(h4, null);
2493 >        checkCompletedNormally(h5, null);
2494 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2495      }}
2496  
2497      /**
2498       * runAfterEither result completes exceptionally after exceptional
2499       * completion of either source
2500       */
2501 <    public void testRunAfterEither_exceptionalCompletion1() {
2501 >    public void testRunAfterEither_exceptionalCompletion() {
2502          for (ExecutionMode m : ExecutionMode.values())
2503          for (Integer v1 : new Integer[] { 1, null })
2504      {
2505          final CompletableFuture<Integer> f = new CompletableFuture<>();
2506          final CompletableFuture<Integer> g = new CompletableFuture<>();
2169        final Noop r = new Noop(m);
2170        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2507          final CFException ex = new CFException();
2508 +        final Noop[] rs = new Noop[6];
2509 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2510  
2511 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2512 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2513 +        checkIncomplete(h0);
2514 +        checkIncomplete(h1);
2515 +        rs[0].assertNotInvoked();
2516 +        rs[1].assertNotInvoked();
2517          f.completeExceptionally(ex);
2518 <        checkCompletedWithWrappedCFException(h, ex);
2518 >        checkCompletedWithWrappedException(h0, ex);
2519 >        checkCompletedWithWrappedException(h1, ex);
2520 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2521 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2522 >        checkCompletedWithWrappedException(h2, ex);
2523 >        checkCompletedWithWrappedException(h3, ex);
2524 >
2525          g.complete(v1);
2526  
2527 <        r.assertNotInvoked();
2527 >        // unspecified behavior - both source completions available
2528 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2529 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2530 >        try {
2531 >            assertNull(h4.join());
2532 >            rs[4].assertInvoked();
2533 >        } catch (CompletionException ok) {
2534 >            checkCompletedWithWrappedException(h4, ex);
2535 >            rs[4].assertNotInvoked();
2536 >        }
2537 >        try {
2538 >            assertNull(h5.join());
2539 >            rs[5].assertInvoked();
2540 >        } catch (CompletionException ok) {
2541 >            checkCompletedWithWrappedException(h5, ex);
2542 >            rs[5].assertNotInvoked();
2543 >        }
2544 >
2545 >        checkCompletedExceptionally(f, ex);
2546          checkCompletedNormally(g, v1);
2547 <        checkCompletedWithWrappedCFException(f, ex);
2548 <        checkCompletedWithWrappedCFException(h, ex);
2547 >        checkCompletedWithWrappedException(h0, ex);
2548 >        checkCompletedWithWrappedException(h1, ex);
2549 >        checkCompletedWithWrappedException(h2, ex);
2550 >        checkCompletedWithWrappedException(h3, ex);
2551 >        checkCompletedWithWrappedException(h4, ex);
2552 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2553      }}
2554  
2555      public void testRunAfterEither_exceptionalCompletion2() {
2556          for (ExecutionMode m : ExecutionMode.values())
2557 +        for (boolean fFirst : new boolean[] { true, false })
2558          for (Integer v1 : new Integer[] { 1, null })
2559      {
2560          final CompletableFuture<Integer> f = new CompletableFuture<>();
2561          final CompletableFuture<Integer> g = new CompletableFuture<>();
2189        final Noop r = new Noop(m);
2190        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2191        final CFException ex = new CFException();
2192
2193        g.completeExceptionally(ex);
2194        checkCompletedWithWrappedCFException(h, ex);
2195        f.complete(v1);
2196
2197        r.assertNotInvoked();
2198        checkCompletedNormally(f, v1);
2199        checkCompletedWithWrappedCFException(g, ex);
2200        checkCompletedWithWrappedCFException(h, ex);
2201    }}
2202
2203    public void testRunAfterEither_exceptionalCompletion3() {
2204        for (ExecutionMode m : ExecutionMode.values())
2205        for (Integer v1 : new Integer[] { 1, null })
2206    {
2207        final CompletableFuture<Integer> f = new CompletableFuture<>();
2208        final CompletableFuture<Integer> g = new CompletableFuture<>();
2209        final Noop r = new Noop(m);
2562          final CFException ex = new CFException();
2563 +        final Noop[] rs = new Noop[6];
2564 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2565  
2566 <        g.completeExceptionally(ex);
2567 <        f.complete(v1);
2568 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2566 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2567 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2568 >        if (fFirst) {
2569 >            f.complete(v1);
2570 >            g.completeExceptionally(ex);
2571 >        } else {
2572 >            g.completeExceptionally(ex);
2573 >            f.complete(v1);
2574 >        }
2575 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2576 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2577  
2578 <        // unspecified behavior
2217 <        Integer v;
2578 >        // unspecified behavior - both source completions available
2579          try {
2580 <            assertNull(h.join());
2581 <            r.assertInvoked();
2580 >            assertEquals(null, h0.join());
2581 >            rs[0].assertInvoked();
2582          } catch (CompletionException ok) {
2583 <            checkCompletedWithWrappedCFException(h, ex);
2584 <            r.assertNotInvoked();
2583 >            checkCompletedWithWrappedException(h0, ex);
2584 >            rs[0].assertNotInvoked();
2585 >        }
2586 >        try {
2587 >            assertEquals(null, h1.join());
2588 >            rs[1].assertInvoked();
2589 >        } catch (CompletionException ok) {
2590 >            checkCompletedWithWrappedException(h1, ex);
2591 >            rs[1].assertNotInvoked();
2592 >        }
2593 >        try {
2594 >            assertEquals(null, h2.join());
2595 >            rs[2].assertInvoked();
2596 >        } catch (CompletionException ok) {
2597 >            checkCompletedWithWrappedException(h2, ex);
2598 >            rs[2].assertNotInvoked();
2599 >        }
2600 >        try {
2601 >            assertEquals(null, h3.join());
2602 >            rs[3].assertInvoked();
2603 >        } catch (CompletionException ok) {
2604 >            checkCompletedWithWrappedException(h3, ex);
2605 >            rs[3].assertNotInvoked();
2606          }
2607  
2226        checkCompletedWithWrappedCFException(g, ex);
2608          checkCompletedNormally(f, v1);
2609 +        checkCompletedExceptionally(g, ex);
2610      }}
2611  
2612 <    public void testRunAfterEither_exceptionalCompletion4() {
2612 >    /**
2613 >     * runAfterEither result completes exceptionally if either source cancelled
2614 >     */
2615 >    public void testRunAfterEither_sourceCancelled() {
2616          for (ExecutionMode m : ExecutionMode.values())
2617 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2618          for (Integer v1 : new Integer[] { 1, null })
2619      {
2620          final CompletableFuture<Integer> f = new CompletableFuture<>();
2621          final CompletableFuture<Integer> g = new CompletableFuture<>();
2622 <        final Noop r = new Noop(m);
2623 <        final CFException ex = new CFException();
2622 >        final Noop[] rs = new Noop[6];
2623 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2624 >
2625 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2626 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2627 >        checkIncomplete(h0);
2628 >        checkIncomplete(h1);
2629 >        rs[0].assertNotInvoked();
2630 >        rs[1].assertNotInvoked();
2631 >        f.cancel(mayInterruptIfRunning);
2632 >        checkCompletedWithWrappedCancellationException(h0);
2633 >        checkCompletedWithWrappedCancellationException(h1);
2634 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2635 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2636 >        checkCompletedWithWrappedCancellationException(h2);
2637 >        checkCompletedWithWrappedCancellationException(h3);
2638  
2239        f.completeExceptionally(ex);
2639          g.complete(v1);
2241        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2640  
2641 <        // unspecified behavior
2642 <        Integer v;
2641 >        // unspecified behavior - both source completions available
2642 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2643 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2644          try {
2645 <            assertNull(h.join());
2646 <            r.assertInvoked();
2645 >            assertNull(h4.join());
2646 >            rs[4].assertInvoked();
2647          } catch (CompletionException ok) {
2648 <            checkCompletedWithWrappedCFException(h, ex);
2649 <            r.assertNotInvoked();
2648 >            checkCompletedWithWrappedCancellationException(h4);
2649 >            rs[4].assertNotInvoked();
2650 >        }
2651 >        try {
2652 >            assertNull(h5.join());
2653 >            rs[5].assertInvoked();
2654 >        } catch (CompletionException ok) {
2655 >            checkCompletedWithWrappedCancellationException(h5);
2656 >            rs[5].assertNotInvoked();
2657          }
2658  
2659 <        checkCompletedWithWrappedCFException(f, ex);
2659 >        checkCancelled(f);
2660          checkCompletedNormally(g, v1);
2661 +        checkCompletedWithWrappedCancellationException(h0);
2662 +        checkCompletedWithWrappedCancellationException(h1);
2663 +        checkCompletedWithWrappedCancellationException(h2);
2664 +        checkCompletedWithWrappedCancellationException(h3);
2665 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2666      }}
2667  
2668      /**
2669       * runAfterEither result completes exceptionally if action does
2670       */
2671 <    public void testRunAfterEither_actionFailed1() {
2671 >    public void testRunAfterEither_actionFailed() {
2672          for (ExecutionMode m : ExecutionMode.values())
2673          for (Integer v1 : new Integer[] { 1, null })
2674          for (Integer v2 : new Integer[] { 2, null })
2675      {
2676          final CompletableFuture<Integer> f = new CompletableFuture<>();
2677          final CompletableFuture<Integer> g = new CompletableFuture<>();
2678 <        final FailingRunnable r = new FailingRunnable(m);
2679 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2678 >        final FailingRunnable[] rs = new FailingRunnable[6];
2679 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2680  
2681 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2682 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2683          f.complete(v1);
2684 <        checkCompletedWithWrappedCFException(h);
2684 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2685 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2686 >        checkCompletedWithWrappedCFException(h0);
2687 >        checkCompletedWithWrappedCFException(h1);
2688 >        checkCompletedWithWrappedCFException(h2);
2689 >        checkCompletedWithWrappedCFException(h3);
2690 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2691          g.complete(v2);
2692 <        checkCompletedNormally(f, v1);
2693 <        checkCompletedNormally(g, v2);
2694 <    }}
2695 <
2277 <    public void testRunAfterEither_actionFailed2() {
2278 <        for (ExecutionMode m : ExecutionMode.values())
2279 <        for (Integer v1 : new Integer[] { 1, null })
2280 <        for (Integer v2 : new Integer[] { 2, null })
2281 <    {
2282 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2283 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2284 <        final FailingRunnable r = new FailingRunnable(m);
2285 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2692 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2693 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2694 >        checkCompletedWithWrappedCFException(h4);
2695 >        checkCompletedWithWrappedCFException(h5);
2696  
2287        g.complete(v2);
2288        checkCompletedWithWrappedCFException(h);
2289        f.complete(v1);
2697          checkCompletedNormally(f, v1);
2698          checkCompletedNormally(g, v2);
2699 <    }}
2293 <
2294 <    /**
2295 <     * runAfterEither result completes exceptionally if either source cancelled
2296 <     */
2297 <    public void testRunAfterEither_sourceCancelled1() {
2298 <        for (ExecutionMode m : ExecutionMode.values())
2299 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2300 <        for (Integer v1 : new Integer[] { 1, null })
2301 <    {
2302 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2303 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2304 <        final Noop r = new Noop(m);
2305 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2306 <
2307 <        assertTrue(f.cancel(mayInterruptIfRunning));
2308 <        checkCompletedWithWrappedCancellationException(h);
2309 <        g.complete(v1);
2310 <
2311 <        checkCancelled(f);
2312 <        r.assertNotInvoked();
2313 <        checkCompletedNormally(g, v1);
2314 <        checkCompletedWithWrappedCancellationException(h);
2315 <    }}
2316 <
2317 <    public void testRunAfterEither_sourceCancelled2() {
2318 <        for (ExecutionMode m : ExecutionMode.values())
2319 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2320 <        for (Integer v1 : new Integer[] { 1, null })
2321 <    {
2322 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2323 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2324 <        final Noop r = new Noop(m);
2325 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2326 <
2327 <        assertTrue(g.cancel(mayInterruptIfRunning));
2328 <        checkCompletedWithWrappedCancellationException(h);
2329 <        f.complete(v1);
2330 <
2331 <        checkCancelled(g);
2332 <        r.assertNotInvoked();
2333 <        checkCompletedNormally(f, v1);
2334 <        checkCompletedWithWrappedCancellationException(h);
2335 <    }}
2336 <
2337 <    public void testRunAfterEither_sourceCancelled3() {
2338 <        for (ExecutionMode m : ExecutionMode.values())
2339 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2340 <        for (Integer v1 : new Integer[] { 1, null })
2341 <    {
2342 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2343 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2344 <        final Noop r = new Noop(m);
2345 <
2346 <        assertTrue(g.cancel(mayInterruptIfRunning));
2347 <        f.complete(v1);
2348 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2349 <
2350 <        // unspecified behavior
2351 <        Integer v;
2352 <        try {
2353 <            assertNull(h.join());
2354 <            r.assertInvoked();
2355 <        } catch (CompletionException ok) {
2356 <            checkCompletedWithWrappedCancellationException(h);
2357 <            r.assertNotInvoked();
2358 <        }
2359 <
2360 <        checkCancelled(g);
2361 <        checkCompletedNormally(f, v1);
2362 <    }}
2363 <
2364 <    public void testRunAfterEither_sourceCancelled4() {
2365 <        for (ExecutionMode m : ExecutionMode.values())
2366 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2367 <        for (Integer v1 : new Integer[] { 1, null })
2368 <    {
2369 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2370 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2371 <        final Noop r = new Noop(m);
2372 <
2373 <        assertTrue(f.cancel(mayInterruptIfRunning));
2374 <        g.complete(v1);
2375 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2376 <
2377 <        // unspecified behavior
2378 <        Integer v;
2379 <        try {
2380 <            assertNull(h.join());
2381 <            r.assertInvoked();
2382 <        } catch (CompletionException ok) {
2383 <            checkCompletedWithWrappedCancellationException(h);
2384 <            r.assertNotInvoked();
2385 <        }
2386 <
2387 <        checkCancelled(f);
2388 <        checkCompletedNormally(g, v1);
2699 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2700      }}
2701  
2702      /**
# Line 2404 | Line 2715 | public class CompletableFutureTest exten
2715  
2716          checkCompletedNormally(g, inc(v1));
2717          checkCompletedNormally(f, v1);
2718 <        r.assertInvoked();
2718 >        r.assertValue(v1);
2719      }}
2720  
2721      /**
# Line 2422 | Line 2733 | public class CompletableFutureTest exten
2733          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2734          if (createIncomplete) f.completeExceptionally(ex);
2735  
2736 <        checkCompletedWithWrappedCFException(g, ex);
2737 <        checkCompletedWithWrappedCFException(f, ex);
2736 >        checkCompletedWithWrappedException(g, ex);
2737 >        checkCompletedExceptionally(f, ex);
2738          r.assertNotInvoked();
2739      }}
2740  
# Line 2679 | Line 2990 | public class CompletableFutureTest exten
2990          assertSame(f, f.toCompletableFuture());
2991      }
2992  
2682    /**
2683     * whenComplete action executes on normal completion, propagating
2684     * source result.
2685     */
2686    public void testWhenComplete_normalCompletion1() {
2687        for (ExecutionMode m : ExecutionMode.values())
2688        for (boolean createIncomplete : new boolean[] { true, false })
2689        for (Integer v1 : new Integer[] { 1, null })
2690    {
2691        final AtomicInteger a = new AtomicInteger(0);
2692        final CompletableFuture<Integer> f = new CompletableFuture<>();
2693        if (!createIncomplete) f.complete(v1);
2694        final CompletableFuture<Integer> g = m.whenComplete
2695            (f,
2696             (Integer x, Throwable t) -> {
2697                threadAssertSame(x, v1);
2698                threadAssertNull(t);
2699                a.getAndIncrement();
2700            });
2701        if (createIncomplete) f.complete(v1);
2702
2703        checkCompletedNormally(g, v1);
2704        checkCompletedNormally(f, v1);
2705        assertEquals(1, a.get());
2706    }}
2707
2708    /**
2709     * whenComplete action executes on exceptional completion, propagating
2710     * source result.
2711     */
2712    public void testWhenComplete_exceptionalCompletion() {
2713        for (ExecutionMode m : ExecutionMode.values())
2714        for (boolean createIncomplete : new boolean[] { true, false })
2715        for (Integer v1 : new Integer[] { 1, null })
2716    {
2717        final AtomicInteger a = new AtomicInteger(0);
2718        final CFException ex = new CFException();
2719        final CompletableFuture<Integer> f = new CompletableFuture<>();
2720        if (!createIncomplete) f.completeExceptionally(ex);
2721        final CompletableFuture<Integer> g = m.whenComplete
2722            (f,
2723             (Integer x, Throwable t) -> {
2724                threadAssertNull(x);
2725                threadAssertSame(t, ex);
2726                a.getAndIncrement();
2727            });
2728        if (createIncomplete) f.completeExceptionally(ex);
2729        checkCompletedWithWrappedCFException(f, ex);
2730        checkCompletedWithWrappedCFException(g, ex);
2731        assertEquals(1, a.get());
2732    }}
2733
2734    /**
2735     * whenComplete action executes on cancelled source, propagating
2736     * CancellationException.
2737     */
2738    public void testWhenComplete_sourceCancelled() {
2739        for (ExecutionMode m : ExecutionMode.values())
2740        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2741        for (boolean createIncomplete : new boolean[] { true, false })
2742    {
2743        final AtomicInteger a = new AtomicInteger(0);
2744        final CompletableFuture<Integer> f = new CompletableFuture<>();
2745        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2746        final CompletableFuture<Integer> g = m.whenComplete
2747            (f,
2748             (Integer x, Throwable t) -> {
2749                threadAssertNull(x);
2750                threadAssertTrue(t instanceof CancellationException);
2751                a.getAndIncrement();
2752            });
2753        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2754
2755        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2756        checkCompletedWithWrappedCancellationException(g);
2757        checkCancelled(f);
2758        assertEquals(1, a.get());
2759    }}
2760
2761    /**
2762     * If a whenComplete action throws an exception when triggered by
2763     * a normal completion, it completes exceptionally
2764     */
2765    public void testWhenComplete_actionFailed() {
2766        for (boolean createIncomplete : new boolean[] { true, false })
2767        for (ExecutionMode m : ExecutionMode.values())
2768        for (Integer v1 : new Integer[] { 1, null })
2769    {
2770        final AtomicInteger a = new AtomicInteger(0);
2771        final CFException ex = new CFException();
2772        final CompletableFuture<Integer> f = new CompletableFuture<>();
2773        if (!createIncomplete) f.complete(v1);
2774        final CompletableFuture<Integer> g = m.whenComplete
2775            (f,
2776             (Integer x, Throwable t) -> {
2777                threadAssertSame(x, v1);
2778                threadAssertNull(t);
2779                a.getAndIncrement();
2780                throw ex;
2781            });
2782        if (createIncomplete) f.complete(v1);
2783        checkCompletedNormally(f, v1);
2784        checkCompletedWithWrappedCFException(g, ex);
2785        assertEquals(1, a.get());
2786    }}
2787
2788    /**
2789     * If a whenComplete action throws an exception when triggered by
2790     * a source completion that also throws an exception, the source
2791     * exception takes precedence.
2792     */
2793    public void testWhenComplete_actionFailedSourceFailed() {
2794        for (boolean createIncomplete : new boolean[] { true, false })
2795        for (ExecutionMode m : ExecutionMode.values())
2796        for (Integer v1 : new Integer[] { 1, null })
2797    {
2798        final AtomicInteger a = new AtomicInteger(0);
2799        final CFException ex1 = new CFException();
2800        final CFException ex2 = new CFException();
2801        final CompletableFuture<Integer> f = new CompletableFuture<>();
2802
2803        if (!createIncomplete) f.completeExceptionally(ex1);
2804        final CompletableFuture<Integer> g = m.whenComplete
2805            (f,
2806             (Integer x, Throwable t) -> {
2807                threadAssertSame(t, ex1);
2808                threadAssertNull(x);
2809                a.getAndIncrement();
2810                throw ex2;
2811            });
2812        if (createIncomplete) f.completeExceptionally(ex1);
2813
2814        checkCompletedWithWrappedCFException(f, ex1);
2815        checkCompletedWithWrappedCFException(g, ex1);
2816        assertEquals(1, a.get());
2817    }}
2818
2993   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines