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.67 by jsr166, Fri Jun 6 19:25:41 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 1767 | Line 1951 | public class CompletableFutureTest exten
1951              assertEquals(inc(v1), h4.join());
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].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  
# Line 1816 | Line 2000 | public class CompletableFutureTest exten
2000              assertEquals(inc(v1), h0.join());
2001              rs[0].assertValue(inc(v1));
2002          } catch (CompletionException ok) {
2003 <            checkCompletedWithWrappedCFException(h0, ex);
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 <            checkCompletedWithWrappedCFException(h1, ex);
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 <            checkCompletedWithWrappedCFException(h2, ex);
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 <            checkCompletedWithWrappedCFException(h3, ex);
2024 >            checkCompletedWithWrappedException(h3, ex);
2025              rs[3].assertNotInvoked();
2026          }
2027  
2028          checkCompletedNormally(f, v1);
2029 <        checkCompletedWithWrappedCFException(g, ex);
2029 >        checkCompletedExceptionally(g, ex);
2030      }}
2031  
2032      /**
# Line 2071 | 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 2087 | 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 2274 | Line 2515 | public class CompletableFutureTest exten
2515          rs[0].assertNotInvoked();
2516          rs[1].assertNotInvoked();
2517          f.completeExceptionally(ex);
2518 <        checkCompletedWithWrappedCFException(h0, ex);
2519 <        checkCompletedWithWrappedCFException(h1, 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 <        checkCompletedWithWrappedCFException(h2, ex);
2523 <        checkCompletedWithWrappedCFException(h3, ex);
2522 >        checkCompletedWithWrappedException(h2, ex);
2523 >        checkCompletedWithWrappedException(h3, ex);
2524  
2525          g.complete(v1);
2526  
# Line 2290 | Line 2531 | public class CompletableFutureTest exten
2531              assertNull(h4.join());
2532              rs[4].assertInvoked();
2533          } catch (CompletionException ok) {
2534 <            checkCompletedWithWrappedCFException(h4, ex);
2534 >            checkCompletedWithWrappedException(h4, ex);
2535              rs[4].assertNotInvoked();
2536          }
2537          try {
2538              assertNull(h5.join());
2539              rs[5].assertInvoked();
2540          } catch (CompletionException ok) {
2541 <            checkCompletedWithWrappedCFException(h5, ex);
2541 >            checkCompletedWithWrappedException(h5, ex);
2542              rs[5].assertNotInvoked();
2543          }
2544  
2545 <        checkCompletedWithWrappedCFException(f, ex);
2545 >        checkCompletedExceptionally(f, ex);
2546          checkCompletedNormally(g, v1);
2547 <        checkCompletedWithWrappedCFException(h0, ex);
2548 <        checkCompletedWithWrappedCFException(h1, ex);
2549 <        checkCompletedWithWrappedCFException(h2, ex);
2550 <        checkCompletedWithWrappedCFException(h3, ex);
2551 <        checkCompletedWithWrappedCFException(h4, 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<>();
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 +        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 - both source completions available
2579 +        try {
2580 +            assertEquals(null, h0.join());
2581 +            rs[0].assertInvoked();
2582 +        } catch (CompletionException ok) {
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 +
2608 +        checkCompletedNormally(f, v1);
2609 +        checkCompletedExceptionally(g, ex);
2610 +    }}
2611 +
2612      /**
2613       * runAfterEither result completes exceptionally if either source cancelled
2614       */
# Line 2417 | 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 2435 | 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 2692 | Line 2990 | public class CompletableFutureTest exten
2990          assertSame(f, f.toCompletableFuture());
2991      }
2992  
2695    /**
2696     * whenComplete action executes on normal completion, propagating
2697     * source result.
2698     */
2699    public void testWhenComplete_normalCompletion1() {
2700        for (ExecutionMode m : ExecutionMode.values())
2701        for (boolean createIncomplete : new boolean[] { true, false })
2702        for (Integer v1 : new Integer[] { 1, null })
2703    {
2704        final AtomicInteger a = new AtomicInteger(0);
2705        final CompletableFuture<Integer> f = new CompletableFuture<>();
2706        if (!createIncomplete) f.complete(v1);
2707        final CompletableFuture<Integer> g = m.whenComplete
2708            (f,
2709             (Integer x, Throwable t) -> {
2710                threadAssertSame(x, v1);
2711                threadAssertNull(t);
2712                a.getAndIncrement();
2713            });
2714        if (createIncomplete) f.complete(v1);
2715
2716        checkCompletedNormally(g, v1);
2717        checkCompletedNormally(f, v1);
2718        assertEquals(1, a.get());
2719    }}
2720
2721    /**
2722     * whenComplete action executes on exceptional completion, propagating
2723     * source result.
2724     */
2725    public void testWhenComplete_exceptionalCompletion() {
2726        for (ExecutionMode m : ExecutionMode.values())
2727        for (boolean createIncomplete : new boolean[] { true, false })
2728        for (Integer v1 : new Integer[] { 1, null })
2729    {
2730        final AtomicInteger a = new AtomicInteger(0);
2731        final CFException ex = new CFException();
2732        final CompletableFuture<Integer> f = new CompletableFuture<>();
2733        if (!createIncomplete) f.completeExceptionally(ex);
2734        final CompletableFuture<Integer> g = m.whenComplete
2735            (f,
2736             (Integer x, Throwable t) -> {
2737                threadAssertNull(x);
2738                threadAssertSame(t, ex);
2739                a.getAndIncrement();
2740            });
2741        if (createIncomplete) f.completeExceptionally(ex);
2742        checkCompletedWithWrappedCFException(f, ex);
2743        checkCompletedWithWrappedCFException(g, ex);
2744        assertEquals(1, a.get());
2745    }}
2746
2747    /**
2748     * whenComplete action executes on cancelled source, propagating
2749     * CancellationException.
2750     */
2751    public void testWhenComplete_sourceCancelled() {
2752        for (ExecutionMode m : ExecutionMode.values())
2753        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2754        for (boolean createIncomplete : new boolean[] { true, false })
2755    {
2756        final AtomicInteger a = new AtomicInteger(0);
2757        final CompletableFuture<Integer> f = new CompletableFuture<>();
2758        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2759        final CompletableFuture<Integer> g = m.whenComplete
2760            (f,
2761             (Integer x, Throwable t) -> {
2762                threadAssertNull(x);
2763                threadAssertTrue(t instanceof CancellationException);
2764                a.getAndIncrement();
2765            });
2766        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2767
2768        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2769        checkCompletedWithWrappedCancellationException(g);
2770        checkCancelled(f);
2771        assertEquals(1, a.get());
2772    }}
2773
2774    /**
2775     * If a whenComplete action throws an exception when triggered by
2776     * a normal completion, it completes exceptionally
2777     */
2778    public void testWhenComplete_actionFailed() {
2779        for (boolean createIncomplete : new boolean[] { true, false })
2780        for (ExecutionMode m : ExecutionMode.values())
2781        for (Integer v1 : new Integer[] { 1, null })
2782    {
2783        final AtomicInteger a = new AtomicInteger(0);
2784        final CFException ex = new CFException();
2785        final CompletableFuture<Integer> f = new CompletableFuture<>();
2786        if (!createIncomplete) f.complete(v1);
2787        final CompletableFuture<Integer> g = m.whenComplete
2788            (f,
2789             (Integer x, Throwable t) -> {
2790                threadAssertSame(x, v1);
2791                threadAssertNull(t);
2792                a.getAndIncrement();
2793                throw ex;
2794            });
2795        if (createIncomplete) f.complete(v1);
2796        checkCompletedNormally(f, v1);
2797        checkCompletedWithWrappedCFException(g, ex);
2798        assertEquals(1, a.get());
2799    }}
2800
2801    /**
2802     * If a whenComplete action throws an exception when triggered by
2803     * a source completion that also throws an exception, the source
2804     * exception takes precedence.
2805     */
2806    public void testWhenComplete_actionFailedSourceFailed() {
2807        for (boolean createIncomplete : new boolean[] { true, false })
2808        for (ExecutionMode m : ExecutionMode.values())
2809        for (Integer v1 : new Integer[] { 1, null })
2810    {
2811        final AtomicInteger a = new AtomicInteger(0);
2812        final CFException ex1 = new CFException();
2813        final CFException ex2 = new CFException();
2814        final CompletableFuture<Integer> f = new CompletableFuture<>();
2815
2816        if (!createIncomplete) f.completeExceptionally(ex1);
2817        final CompletableFuture<Integer> g = m.whenComplete
2818            (f,
2819             (Integer x, Throwable t) -> {
2820                threadAssertSame(t, ex1);
2821                threadAssertNull(x);
2822                a.getAndIncrement();
2823                throw ex2;
2824            });
2825        if (createIncomplete) f.completeExceptionally(ex1);
2826
2827        checkCompletedWithWrappedCFException(f, ex1);
2828        checkCompletedWithWrappedCFException(g, ex1);
2829        assertEquals(1, a.get());
2830    }}
2831
2993   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines