75 |
|
try { |
76 |
|
f.join(); |
77 |
|
shouldThrow(); |
78 |
< |
} catch (Throwable ex) { |
78 |
> |
} catch (Throwable ex) { |
79 |
|
assertTrue(ex instanceof CompletionException && |
80 |
|
((CompletionException)ex).getCause() instanceof CFException); |
81 |
|
} |
82 |
|
try { |
83 |
|
f.getNow(null); |
84 |
|
shouldThrow(); |
85 |
< |
} catch (Throwable ex) { |
85 |
> |
} catch (Throwable ex) { |
86 |
|
assertTrue(ex instanceof CompletionException && |
87 |
|
((CompletionException)ex).getCause() instanceof CFException); |
88 |
|
} |
89 |
|
try { |
90 |
|
f.get(); |
91 |
|
shouldThrow(); |
92 |
< |
} catch (Throwable ex) { |
92 |
> |
} catch (Throwable ex) { |
93 |
|
assertTrue(ex instanceof ExecutionException && |
94 |
|
((ExecutionException)ex).getCause() instanceof CFException); |
95 |
|
} |
96 |
|
try { |
97 |
|
f.get(0L, SECONDS); |
98 |
|
shouldThrow(); |
99 |
< |
} catch (Throwable ex) { |
99 |
> |
} catch (Throwable ex) { |
100 |
|
assertTrue(ex instanceof ExecutionException && |
101 |
|
((ExecutionException)ex).getCause() instanceof CFException); |
102 |
|
} |
108 |
|
try { |
109 |
|
f.join(); |
110 |
|
shouldThrow(); |
111 |
< |
} catch (Throwable ex) { |
111 |
> |
} catch (Throwable ex) { |
112 |
|
assertTrue(ex instanceof CancellationException); |
113 |
|
} |
114 |
|
try { |
115 |
|
f.getNow(null); |
116 |
|
shouldThrow(); |
117 |
< |
} catch (Throwable ex) { |
117 |
> |
} catch (Throwable ex) { |
118 |
|
assertTrue(ex instanceof CancellationException); |
119 |
|
} |
120 |
|
try { |
121 |
|
f.get(); |
122 |
|
shouldThrow(); |
123 |
< |
} catch (Throwable ex) { |
123 |
> |
} catch (Throwable ex) { |
124 |
|
assertTrue(ex instanceof CancellationException); |
125 |
|
} |
126 |
|
try { |
127 |
|
f.get(0L, SECONDS); |
128 |
|
shouldThrow(); |
129 |
< |
} catch (Throwable ex) { |
129 |
> |
} catch (Throwable ex) { |
130 |
|
assertTrue(ex instanceof CancellationException); |
131 |
|
} |
132 |
|
assertTrue(f.isDone()); |
137 |
|
try { |
138 |
|
f.join(); |
139 |
|
shouldThrow(); |
140 |
< |
} catch (Throwable ex) { |
140 |
> |
} catch (Throwable ex) { |
141 |
|
assertTrue(ex instanceof CompletionException && |
142 |
|
((CompletionException)ex).getCause() instanceof CancellationException); |
143 |
|
} |
144 |
|
try { |
145 |
|
f.getNow(null); |
146 |
|
shouldThrow(); |
147 |
< |
} catch (Throwable ex) { |
147 |
> |
} catch (Throwable ex) { |
148 |
|
assertTrue(ex instanceof CompletionException && |
149 |
|
((CompletionException)ex).getCause() instanceof CancellationException); |
150 |
|
} |
151 |
|
try { |
152 |
|
f.get(); |
153 |
|
shouldThrow(); |
154 |
< |
} catch (Throwable ex) { |
154 |
> |
} catch (Throwable ex) { |
155 |
|
assertTrue(ex instanceof ExecutionException && |
156 |
|
((ExecutionException)ex).getCause() instanceof CancellationException); |
157 |
|
} |
158 |
|
try { |
159 |
|
f.get(0L, SECONDS); |
160 |
|
shouldThrow(); |
161 |
< |
} catch (Throwable ex) { |
161 |
> |
} catch (Throwable ex) { |
162 |
|
assertTrue(ex instanceof ExecutionException && |
163 |
|
((ExecutionException)ex).getCause() instanceof CancellationException); |
164 |
|
} |
249 |
|
f.obtrudeException(new CFException()); |
250 |
|
checkCompletedWithWrappedCFException(f); |
251 |
|
} |
252 |
< |
|
252 |
> |
|
253 |
|
/** |
254 |
|
* getNumberOfDependents returns number of dependent tasks |
255 |
|
*/ |
285 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
286 |
|
} |
287 |
|
|
288 |
< |
static final Supplier<Integer> supplyOne = |
288 |
> |
static final Supplier<Integer> supplyOne = |
289 |
|
() -> Integer.valueOf(1); |
290 |
< |
static final Function<Integer, Integer> inc = |
290 |
> |
static final Function<Integer, Integer> inc = |
291 |
|
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
292 |
< |
static final BiFunction<Integer, Integer, Integer> add = |
292 |
> |
static final BiFunction<Integer, Integer, Integer> add = |
293 |
|
(Integer x, Integer y) -> Integer.valueOf(x.intValue() + y.intValue()); |
294 |
|
static final class IncAction implements Consumer<Integer> { |
295 |
|
int value; |
297 |
|
} |
298 |
|
static final class AddAction implements BiConsumer<Integer, Integer> { |
299 |
|
int value; |
300 |
< |
public void accept(Integer x, Integer y) { |
301 |
< |
value = x.intValue() + y.intValue(); |
300 |
> |
public void accept(Integer x, Integer y) { |
301 |
> |
value = x.intValue() + y.intValue(); |
302 |
|
} |
303 |
|
} |
304 |
|
static final class Noop implements Runnable { |
345 |
|
ran = true; throw new CFException(); |
346 |
|
} |
347 |
|
} |
348 |
< |
|
348 |
> |
|
349 |
|
// Used for explicit executor tests |
350 |
|
static final class ThreadExecutor implements Executor { |
351 |
|
public void execute(Runnable r) { |
358 |
|
} |
359 |
|
|
360 |
|
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
361 |
< |
public Integer apply(Integer x, Throwable t) { |
361 |
> |
public Integer apply(Integer x, Throwable t) { |
362 |
|
return (t == null) ? two : three; |
363 |
|
} |
364 |
|
} |
457 |
|
} |
458 |
|
|
459 |
|
// seq conmpletion methods |
460 |
< |
|
460 |
> |
|
461 |
|
/** |
462 |
|
* thenRun result completes normally after normal completion of source |
463 |
|
*/ |
1126 |
|
try { |
1127 |
|
g.join(); |
1128 |
|
shouldThrow(); |
1129 |
< |
} catch(Exception ok) { |
1129 |
> |
} catch (Exception ok) { |
1130 |
|
} |
1131 |
|
checkCompletedWithWrappedCFException(g); |
1132 |
|
} |
1141 |
|
f.complete(null); |
1142 |
|
checkCompletedWithWrappedCFException(g); |
1143 |
|
} |
1144 |
< |
|
1144 |
> |
|
1145 |
|
/** |
1146 |
|
* thenRunAsync result completes exceptionally if source cancelled |
1147 |
|
*/ |
1184 |
|
f.complete(null); |
1185 |
|
checkCompletedWithWrappedCFException(g); |
1186 |
|
} |
1187 |
< |
|
1187 |
> |
|
1188 |
|
/** |
1189 |
|
* thenApplyAsync result completes exceptionally if source cancelled |
1190 |
|
*/ |
1230 |
|
f.complete(null); |
1231 |
|
checkCompletedWithWrappedCFException(g); |
1232 |
|
} |
1233 |
< |
|
1233 |
> |
|
1234 |
|
/** |
1235 |
|
* thenAcceptAsync result completes exceptionally if source cancelled |
1236 |
|
*/ |
1288 |
|
f2.complete(two); |
1289 |
|
checkCompletedWithWrappedCFException(g); |
1290 |
|
} |
1291 |
< |
|
1291 |
> |
|
1292 |
|
/** |
1293 |
|
* thenCombineAsync result completes exceptionally if either source cancelled |
1294 |
|
*/ |
1299 |
|
assertTrue(f.cancel(true)); |
1300 |
|
f2.complete(two); |
1301 |
|
checkCompletedWithWrappedCancellationException(g); |
1302 |
< |
|
1302 |
> |
|
1303 |
|
f = new CompletableFuture<Integer>(); |
1304 |
|
f2 = new CompletableFuture<Integer>(); |
1305 |
|
g = f.thenCombineAsync(f2, add); |
1353 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1354 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1355 |
|
FailingBiConsumer r = new FailingBiConsumer(); |
1356 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1356 |
> |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1357 |
|
f.complete(one); |
1358 |
|
checkIncomplete(g); |
1359 |
|
f2.complete(two); |
1360 |
|
checkCompletedWithWrappedCFException(g); |
1361 |
|
} |
1362 |
< |
|
1362 |
> |
|
1363 |
|
/** |
1364 |
|
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
1365 |
|
*/ |
1371 |
|
assertTrue(f.cancel(true)); |
1372 |
|
f2.complete(two); |
1373 |
|
checkCompletedWithWrappedCancellationException(g); |
1374 |
< |
|
1374 |
> |
|
1375 |
|
r = new AddAction(); |
1376 |
|
f = new CompletableFuture<Integer>(); |
1377 |
|
f2 = new CompletableFuture<Integer>(); |
1426 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1427 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1428 |
|
FailingNoop r = new FailingNoop(); |
1429 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1429 |
> |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1430 |
|
f.complete(one); |
1431 |
|
checkIncomplete(g); |
1432 |
|
f2.complete(two); |
1433 |
|
checkCompletedWithWrappedCFException(g); |
1434 |
|
} |
1435 |
< |
|
1435 |
> |
|
1436 |
|
/** |
1437 |
|
* runAfterBothAsync result completes exceptionally if either source cancelled |
1438 |
|
*/ |
1444 |
|
assertTrue(f.cancel(true)); |
1445 |
|
f2.complete(two); |
1446 |
|
checkCompletedWithWrappedCancellationException(g); |
1447 |
< |
|
1447 |
> |
|
1448 |
|
r = new Noop(); |
1449 |
|
f = new CompletableFuture<Integer>(); |
1450 |
|
f2 = new CompletableFuture<Integer>(); |
1498 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1499 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1500 |
|
FailingFunction r = new FailingFunction(); |
1501 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r); |
1501 |
> |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r); |
1502 |
|
f.complete(one); |
1503 |
|
checkCompletedWithWrappedCFException(g); |
1504 |
|
} |
1505 |
< |
|
1505 |
> |
|
1506 |
|
/** |
1507 |
|
* applyToEitherAsync result completes exceptionally if either source cancelled |
1508 |
|
*/ |
1512 |
|
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
1513 |
|
assertTrue(f.cancel(true)); |
1514 |
|
checkCompletedWithWrappedCancellationException(g); |
1515 |
< |
|
1515 |
> |
|
1516 |
|
f = new CompletableFuture<Integer>(); |
1517 |
|
f2 = new CompletableFuture<Integer>(); |
1518 |
|
assertTrue(f2.cancel(true)); |
1570 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1571 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1572 |
|
FailingConsumer r = new FailingConsumer(); |
1573 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1573 |
> |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1574 |
|
f.complete(one); |
1575 |
|
checkCompletedWithWrappedCFException(g); |
1576 |
|
} |
1577 |
< |
|
1577 |
> |
|
1578 |
|
/** |
1579 |
|
* acceptEitherAsync result completes exceptionally if either |
1580 |
|
* source cancelled |
1586 |
|
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1587 |
|
assertTrue(f.cancel(true)); |
1588 |
|
checkCompletedWithWrappedCancellationException(g); |
1589 |
< |
|
1589 |
> |
|
1590 |
|
r = new IncAction(); |
1591 |
|
f = new CompletableFuture<Integer>(); |
1592 |
|
f2 = new CompletableFuture<Integer>(); |
1645 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1646 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1647 |
|
FailingNoop r = new FailingNoop(); |
1648 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1648 |
> |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1649 |
|
f.complete(one); |
1650 |
|
checkCompletedWithWrappedCFException(g); |
1651 |
|
} |
1652 |
< |
|
1652 |
> |
|
1653 |
|
/** |
1654 |
|
* runAfterEitherAsync result completes exceptionally if either |
1655 |
|
* source cancelled |
1661 |
|
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1662 |
|
assertTrue(f.cancel(true)); |
1663 |
|
checkCompletedWithWrappedCancellationException(g); |
1664 |
< |
|
1664 |
> |
|
1665 |
|
r = new Noop(); |
1666 |
|
f = new CompletableFuture<Integer>(); |
1667 |
|
f2 = new CompletableFuture<Integer>(); |
1748 |
|
try { |
1749 |
|
g.join(); |
1750 |
|
shouldThrow(); |
1751 |
< |
} catch(Exception ok) { |
1751 |
> |
} catch (Exception ok) { |
1752 |
|
} |
1753 |
|
checkCompletedWithWrappedCFException(g); |
1754 |
|
} |
1763 |
|
f.complete(null); |
1764 |
|
checkCompletedWithWrappedCFException(g); |
1765 |
|
} |
1766 |
< |
|
1766 |
> |
|
1767 |
|
/** |
1768 |
|
* thenRunAsync result completes exceptionally if source cancelled |
1769 |
|
*/ |
1806 |
|
f.complete(null); |
1807 |
|
checkCompletedWithWrappedCFException(g); |
1808 |
|
} |
1809 |
< |
|
1809 |
> |
|
1810 |
|
/** |
1811 |
|
* thenApplyAsync result completes exceptionally if source cancelled |
1812 |
|
*/ |
1852 |
|
f.complete(null); |
1853 |
|
checkCompletedWithWrappedCFException(g); |
1854 |
|
} |
1855 |
< |
|
1855 |
> |
|
1856 |
|
/** |
1857 |
|
* thenAcceptAsync result completes exceptionally if source cancelled |
1858 |
|
*/ |
1910 |
|
f2.complete(two); |
1911 |
|
checkCompletedWithWrappedCFException(g); |
1912 |
|
} |
1913 |
< |
|
1913 |
> |
|
1914 |
|
/** |
1915 |
|
* thenCombineAsync result completes exceptionally if either source cancelled |
1916 |
|
*/ |
1921 |
|
assertTrue(f.cancel(true)); |
1922 |
|
f2.complete(two); |
1923 |
|
checkCompletedWithWrappedCancellationException(g); |
1924 |
< |
|
1924 |
> |
|
1925 |
|
f = new CompletableFuture<Integer>(); |
1926 |
|
f2 = new CompletableFuture<Integer>(); |
1927 |
|
g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
1975 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1976 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1977 |
|
FailingBiConsumer r = new FailingBiConsumer(); |
1978 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
1978 |
> |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
1979 |
|
f.complete(one); |
1980 |
|
checkIncomplete(g); |
1981 |
|
f2.complete(two); |
1982 |
|
checkCompletedWithWrappedCFException(g); |
1983 |
|
} |
1984 |
< |
|
1984 |
> |
|
1985 |
|
/** |
1986 |
|
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
1987 |
|
*/ |
1993 |
|
assertTrue(f.cancel(true)); |
1994 |
|
f2.complete(two); |
1995 |
|
checkCompletedWithWrappedCancellationException(g); |
1996 |
< |
|
1996 |
> |
|
1997 |
|
r = new AddAction(); |
1998 |
|
f = new CompletableFuture<Integer>(); |
1999 |
|
f2 = new CompletableFuture<Integer>(); |
2048 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2049 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2050 |
|
FailingNoop r = new FailingNoop(); |
2051 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2051 |
> |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2052 |
|
f.complete(one); |
2053 |
|
checkIncomplete(g); |
2054 |
|
f2.complete(two); |
2055 |
|
checkCompletedWithWrappedCFException(g); |
2056 |
|
} |
2057 |
< |
|
2057 |
> |
|
2058 |
|
/** |
2059 |
|
* runAfterBothAsync result completes exceptionally if either source cancelled |
2060 |
|
*/ |
2066 |
|
assertTrue(f.cancel(true)); |
2067 |
|
f2.complete(two); |
2068 |
|
checkCompletedWithWrappedCancellationException(g); |
2069 |
< |
|
2069 |
> |
|
2070 |
|
r = new Noop(); |
2071 |
|
f = new CompletableFuture<Integer>(); |
2072 |
|
f2 = new CompletableFuture<Integer>(); |
2120 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2121 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2122 |
|
FailingFunction r = new FailingFunction(); |
2123 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor()); |
2123 |
> |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor()); |
2124 |
|
f.complete(one); |
2125 |
|
checkCompletedWithWrappedCFException(g); |
2126 |
|
} |
2127 |
< |
|
2127 |
> |
|
2128 |
|
/** |
2129 |
|
* applyToEitherAsync result completes exceptionally if either source cancelled |
2130 |
|
*/ |
2134 |
|
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2135 |
|
assertTrue(f.cancel(true)); |
2136 |
|
checkCompletedWithWrappedCancellationException(g); |
2137 |
< |
|
2137 |
> |
|
2138 |
|
f = new CompletableFuture<Integer>(); |
2139 |
|
f2 = new CompletableFuture<Integer>(); |
2140 |
|
assertTrue(f2.cancel(true)); |
2192 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2193 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2194 |
|
FailingConsumer r = new FailingConsumer(); |
2195 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2195 |
> |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2196 |
|
f.complete(one); |
2197 |
|
checkCompletedWithWrappedCFException(g); |
2198 |
|
} |
2199 |
< |
|
2199 |
> |
|
2200 |
|
/** |
2201 |
|
* acceptEitherAsync result completes exceptionally if either |
2202 |
|
* source cancelled |
2208 |
|
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2209 |
|
assertTrue(f.cancel(true)); |
2210 |
|
checkCompletedWithWrappedCancellationException(g); |
2211 |
< |
|
2211 |
> |
|
2212 |
|
r = new IncAction(); |
2213 |
|
f = new CompletableFuture<Integer>(); |
2214 |
|
f2 = new CompletableFuture<Integer>(); |
2267 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2268 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2269 |
|
FailingNoop r = new FailingNoop(); |
2270 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2270 |
> |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2271 |
|
f.complete(one); |
2272 |
|
checkCompletedWithWrappedCFException(g); |
2273 |
|
} |
2274 |
< |
|
2274 |
> |
|
2275 |
|
/** |
2276 |
|
* runAfterEitherAsync result completes exceptionally if either |
2277 |
|
* source cancelled |
2283 |
|
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2284 |
|
assertTrue(f.cancel(true)); |
2285 |
|
checkCompletedWithWrappedCancellationException(g); |
2286 |
< |
|
2286 |
> |
|
2287 |
|
r = new Noop(); |
2288 |
|
f = new CompletableFuture<Integer>(); |
2289 |
|
f2 = new CompletableFuture<Integer>(); |
2337 |
|
checkCompletedWithWrappedCancellationException(g); |
2338 |
|
} |
2339 |
|
|
2340 |
< |
// other static methods |
2340 |
> |
// other static methods |
2341 |
|
|
2342 |
|
/** |
2343 |
|
* allOf(no component futures) returns a future completed normally |
2354 |
|
public void testAllOf() throws Exception { |
2355 |
|
for (int k = 1; k < 20; ++k) { |
2356 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
2357 |
< |
for (int i = 0; i < k; ++i) |
2357 |
> |
for (int i = 0; i < k; ++i) |
2358 |
|
fs[i] = new CompletableFuture<Integer>(); |
2359 |
|
CompletableFuture<?> f = CompletableFuture.allOf(fs); |
2360 |
|
for (int i = 0; i < k; ++i) { |
2380 |
|
public void testAnyOf() throws Exception { |
2381 |
|
for (int k = 1; k < 20; ++k) { |
2382 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
2383 |
< |
for (int i = 0; i < k; ++i) |
2383 |
> |
for (int i = 0; i < k; ++i) |
2384 |
|
fs[i] = new CompletableFuture<Integer>(); |
2385 |
|
CompletableFuture<?> f = CompletableFuture.anyOf(fs); |
2386 |
|
checkIncomplete(f); |