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.71 by jsr166, Fri Jun 6 20:01:16 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 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 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  
# Line 2136 | Line 2320 | public class CompletableFutureTest exten
2320              assertEquals(null, h0.join());
2321              rs[0].assertValue(v1);
2322          } catch (CompletionException ok) {
2323 <            checkCompletedWithWrappedCFException(h0, ex);
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 <            checkCompletedWithWrappedCFException(h1, ex);
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 <            checkCompletedWithWrappedCFException(h2, ex);
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 <            checkCompletedWithWrappedCFException(h3, ex);
2344 >            checkCompletedWithWrappedException(h3, ex);
2345              rs[3].assertNotInvoked();
2346          }
2347  
2348          checkCompletedNormally(f, v1);
2349 <        checkCompletedWithWrappedCFException(g, ex);
2349 >        checkCompletedExceptionally(g, ex);
2350      }}
2351  
2352      /**
# Line 2331 | 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 2347 | 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  
# Line 2396 | Line 2580 | public class CompletableFutureTest exten
2580              assertEquals(null, h0.join());
2581              rs[0].assertInvoked();
2582          } catch (CompletionException ok) {
2583 <            checkCompletedWithWrappedCFException(h0, ex);
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 <            checkCompletedWithWrappedCFException(h1, ex);
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 <            checkCompletedWithWrappedCFException(h2, ex);
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 <            checkCompletedWithWrappedCFException(h3, ex);
2604 >            checkCompletedWithWrappedException(h3, ex);
2605              rs[3].assertNotInvoked();
2606          }
2607  
2608          checkCompletedNormally(f, v1);
2609 <        checkCompletedWithWrappedCFException(g, ex);
2609 >        checkCompletedExceptionally(g, ex);
2610      }}
2611  
2612      /**
# Line 2549 | 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 2806 | Line 2990 | public class CompletableFutureTest exten
2990          assertSame(f, f.toCompletableFuture());
2991      }
2992  
2809    /**
2810     * whenComplete action executes on normal completion, propagating
2811     * source result.
2812     */
2813    public void testWhenComplete_normalCompletion1() {
2814        for (ExecutionMode m : ExecutionMode.values())
2815        for (boolean createIncomplete : new boolean[] { true, false })
2816        for (Integer v1 : new Integer[] { 1, null })
2817    {
2818        final AtomicInteger a = new AtomicInteger(0);
2819        final CompletableFuture<Integer> f = new CompletableFuture<>();
2820        if (!createIncomplete) f.complete(v1);
2821        final CompletableFuture<Integer> g = m.whenComplete
2822            (f,
2823             (Integer x, Throwable t) -> {
2824                m.checkExecutionMode();
2825                threadAssertSame(x, v1);
2826                threadAssertNull(t);
2827                a.getAndIncrement();
2828            });
2829        if (createIncomplete) f.complete(v1);
2830
2831        checkCompletedNormally(g, v1);
2832        checkCompletedNormally(f, v1);
2833        assertEquals(1, a.get());
2834    }}
2835
2836    /**
2837     * whenComplete action executes on exceptional completion, propagating
2838     * source result.
2839     */
2840    public void testWhenComplete_exceptionalCompletion() {
2841        for (ExecutionMode m : ExecutionMode.values())
2842        for (boolean createIncomplete : new boolean[] { true, false })
2843        for (Integer v1 : new Integer[] { 1, null })
2844    {
2845        final AtomicInteger a = new AtomicInteger(0);
2846        final CFException ex = new CFException();
2847        final CompletableFuture<Integer> f = new CompletableFuture<>();
2848        if (!createIncomplete) f.completeExceptionally(ex);
2849        final CompletableFuture<Integer> g = m.whenComplete
2850            (f,
2851             (Integer x, Throwable t) -> {
2852                m.checkExecutionMode();
2853                threadAssertNull(x);
2854                threadAssertSame(t, ex);
2855                a.getAndIncrement();
2856            });
2857        if (createIncomplete) f.completeExceptionally(ex);
2858        checkCompletedWithWrappedCFException(f, ex);
2859        checkCompletedWithWrappedCFException(g, ex);
2860        assertEquals(1, a.get());
2861    }}
2862
2863    /**
2864     * whenComplete action executes on cancelled source, propagating
2865     * CancellationException.
2866     */
2867    public void testWhenComplete_sourceCancelled() {
2868        for (ExecutionMode m : ExecutionMode.values())
2869        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2870        for (boolean createIncomplete : new boolean[] { true, false })
2871    {
2872        final AtomicInteger a = new AtomicInteger(0);
2873        final CompletableFuture<Integer> f = new CompletableFuture<>();
2874        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2875        final CompletableFuture<Integer> g = m.whenComplete
2876            (f,
2877             (Integer x, Throwable t) -> {
2878                m.checkExecutionMode();
2879                threadAssertNull(x);
2880                threadAssertTrue(t instanceof CancellationException);
2881                a.getAndIncrement();
2882            });
2883        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2884
2885        checkCompletedWithWrappedCancellationException(g);
2886        checkCancelled(f);
2887        assertEquals(1, a.get());
2888    }}
2889
2890    /**
2891     * If a whenComplete action throws an exception when triggered by
2892     * a normal completion, it completes exceptionally
2893     */
2894    public void testWhenComplete_actionFailed() {
2895        for (boolean createIncomplete : new boolean[] { true, false })
2896        for (ExecutionMode m : ExecutionMode.values())
2897        for (Integer v1 : new Integer[] { 1, null })
2898    {
2899        final AtomicInteger a = new AtomicInteger(0);
2900        final CFException ex = new CFException();
2901        final CompletableFuture<Integer> f = new CompletableFuture<>();
2902        if (!createIncomplete) f.complete(v1);
2903        final CompletableFuture<Integer> g = m.whenComplete
2904            (f,
2905             (Integer x, Throwable t) -> {
2906                m.checkExecutionMode();
2907                threadAssertSame(x, v1);
2908                threadAssertNull(t);
2909                a.getAndIncrement();
2910                throw ex;
2911            });
2912        if (createIncomplete) f.complete(v1);
2913        checkCompletedNormally(f, v1);
2914        checkCompletedWithWrappedCFException(g, ex);
2915        assertEquals(1, a.get());
2916    }}
2917
2918    /**
2919     * If a whenComplete action throws an exception when triggered by
2920     * a source completion that also throws an exception, the source
2921     * exception takes precedence.
2922     */
2923    public void testWhenComplete_actionFailedSourceFailed() {
2924        for (boolean createIncomplete : new boolean[] { true, false })
2925        for (ExecutionMode m : ExecutionMode.values())
2926        for (Integer v1 : new Integer[] { 1, null })
2927    {
2928        final AtomicInteger a = new AtomicInteger(0);
2929        final CFException ex1 = new CFException();
2930        final CFException ex2 = new CFException();
2931        final CompletableFuture<Integer> f = new CompletableFuture<>();
2932
2933        if (!createIncomplete) f.completeExceptionally(ex1);
2934        final CompletableFuture<Integer> g = m.whenComplete
2935            (f,
2936             (Integer x, Throwable t) -> {
2937                m.checkExecutionMode();
2938                threadAssertSame(t, ex1);
2939                threadAssertNull(x);
2940                a.getAndIncrement();
2941                throw ex2;
2942            });
2943        if (createIncomplete) f.completeExceptionally(ex1);
2944
2945        checkCompletedWithWrappedCFException(f, ex1);
2946        checkCompletedWithWrappedCFException(g, ex1);
2947        assertEquals(1, a.get());
2948    }}
2949
2993   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines