53 |
|
catch (Throwable fail) { threadUnexpectedException(fail); } |
54 |
|
} |
55 |
|
|
56 |
< |
void checkCompletedNormally(CompletableFuture<?> f, Object value) { |
56 |
> |
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
57 |
|
try { |
58 |
|
assertEquals(value, f.join()); |
59 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
75 |
|
try { |
76 |
|
f.join(); |
77 |
|
shouldThrow(); |
78 |
< |
} catch (Throwable ex) { |
79 |
< |
assertTrue(ex instanceof CompletionException && |
80 |
< |
((CompletionException)ex).getCause() instanceof CFException); |
78 |
> |
} catch (CompletionException success) { |
79 |
> |
assertTrue(success.getCause() instanceof CFException); |
80 |
|
} |
81 |
|
try { |
82 |
|
f.getNow(null); |
83 |
|
shouldThrow(); |
84 |
< |
} catch (Throwable ex) { |
85 |
< |
assertTrue(ex instanceof CompletionException && |
87 |
< |
((CompletionException)ex).getCause() instanceof CFException); |
84 |
> |
} catch (CompletionException success) { |
85 |
> |
assertTrue(success.getCause() instanceof CFException); |
86 |
|
} |
87 |
|
try { |
88 |
|
f.get(); |
89 |
|
shouldThrow(); |
90 |
< |
} catch (Throwable ex) { |
91 |
< |
assertTrue(ex instanceof ExecutionException && |
92 |
< |
((ExecutionException)ex).getCause() instanceof CFException); |
95 |
< |
} |
90 |
> |
} catch (ExecutionException success) { |
91 |
> |
assertTrue(success.getCause() instanceof CFException); |
92 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
93 |
|
try { |
94 |
|
f.get(0L, SECONDS); |
95 |
|
shouldThrow(); |
96 |
< |
} catch (Throwable ex) { |
97 |
< |
assertTrue(ex instanceof ExecutionException && |
98 |
< |
((ExecutionException)ex).getCause() instanceof CFException); |
102 |
< |
} |
96 |
> |
} catch (ExecutionException success) { |
97 |
> |
assertTrue(success.getCause() instanceof CFException); |
98 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
99 |
|
assertTrue(f.isDone()); |
100 |
|
assertFalse(f.isCancelled()); |
101 |
|
} |
104 |
|
try { |
105 |
|
f.join(); |
106 |
|
shouldThrow(); |
107 |
< |
} catch (Throwable ex) { |
112 |
< |
assertTrue(ex instanceof CancellationException); |
113 |
< |
} |
107 |
> |
} catch (CancellationException success) {} |
108 |
|
try { |
109 |
|
f.getNow(null); |
110 |
|
shouldThrow(); |
111 |
< |
} catch (Throwable ex) { |
118 |
< |
assertTrue(ex instanceof CancellationException); |
119 |
< |
} |
111 |
> |
} catch (CancellationException success) {} |
112 |
|
try { |
113 |
|
f.get(); |
114 |
|
shouldThrow(); |
115 |
< |
} catch (Throwable ex) { |
116 |
< |
assertTrue(ex instanceof CancellationException); |
125 |
< |
} |
115 |
> |
} catch (CancellationException success) { |
116 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
117 |
|
try { |
118 |
|
f.get(0L, SECONDS); |
119 |
|
shouldThrow(); |
120 |
< |
} catch (Throwable ex) { |
121 |
< |
assertTrue(ex instanceof CancellationException); |
131 |
< |
} |
120 |
> |
} catch (CancellationException success) { |
121 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
122 |
|
assertTrue(f.isDone()); |
123 |
|
assertTrue(f.isCancelled()); |
124 |
|
} |
127 |
|
try { |
128 |
|
f.join(); |
129 |
|
shouldThrow(); |
130 |
< |
} catch (Throwable ex) { |
131 |
< |
assertTrue(ex instanceof CompletionException && |
142 |
< |
((CompletionException)ex).getCause() instanceof CancellationException); |
130 |
> |
} catch (CompletionException success) { |
131 |
> |
assertTrue(success.getCause() instanceof CancellationException); |
132 |
|
} |
133 |
|
try { |
134 |
|
f.getNow(null); |
135 |
|
shouldThrow(); |
136 |
< |
} catch (Throwable ex) { |
137 |
< |
assertTrue(ex instanceof CompletionException && |
149 |
< |
((CompletionException)ex).getCause() instanceof CancellationException); |
136 |
> |
} catch (CompletionException success) { |
137 |
> |
assertTrue(success.getCause() instanceof CancellationException); |
138 |
|
} |
139 |
|
try { |
140 |
|
f.get(); |
141 |
|
shouldThrow(); |
142 |
< |
} catch (Throwable ex) { |
143 |
< |
assertTrue(ex instanceof ExecutionException && |
144 |
< |
((ExecutionException)ex).getCause() instanceof CancellationException); |
157 |
< |
} |
142 |
> |
} catch (ExecutionException success) { |
143 |
> |
assertTrue(success.getCause() instanceof CancellationException); |
144 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
145 |
|
try { |
146 |
|
f.get(0L, SECONDS); |
147 |
|
shouldThrow(); |
148 |
< |
} catch (Throwable ex) { |
149 |
< |
assertTrue(ex instanceof ExecutionException && |
150 |
< |
((ExecutionException)ex).getCause() instanceof CancellationException); |
164 |
< |
} |
148 |
> |
} catch (ExecutionException success) { |
149 |
> |
assertTrue(success.getCause() instanceof CancellationException); |
150 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
151 |
|
assertTrue(f.isDone()); |
152 |
|
assertFalse(f.isCancelled()); |
153 |
|
} |
271 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
272 |
|
} |
273 |
|
|
274 |
+ |
/** |
275 |
+ |
* completedFuture returns a completed CompletableFuture with given value |
276 |
+ |
*/ |
277 |
+ |
public void testCompletedFuture() { |
278 |
+ |
CompletableFuture<String> f = CompletableFuture.completedFuture("test"); |
279 |
+ |
checkCompletedNormally(f, "test"); |
280 |
+ |
} |
281 |
+ |
|
282 |
|
static final Supplier<Integer> supplyOne = |
283 |
|
() -> Integer.valueOf(1); |
284 |
|
static final Function<Integer, Integer> inc = |
450 |
|
assertTrue(r.ran); |
451 |
|
} |
452 |
|
|
453 |
< |
// seq conmpletion methods |
453 |
> |
// seq completion methods |
454 |
|
|
455 |
|
/** |
456 |
|
* thenRun result completes normally after normal completion of source |
1665 |
|
} |
1666 |
|
|
1667 |
|
/** |
1668 |
< |
* thenCompse result completes normally after normal completion of source |
1668 |
> |
* thenComposeAsync result completes normally after normal |
1669 |
> |
* completion of source |
1670 |
|
*/ |
1671 |
|
public void testThenComposeAsync() { |
1672 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1677 |
|
} |
1678 |
|
|
1679 |
|
/** |
1680 |
< |
* thenComposeAsync result completes exceptionally after exceptional |
1681 |
< |
* completion of source |
1680 |
> |
* thenComposeAsync result completes exceptionally after |
1681 |
> |
* exceptional completion of source |
1682 |
|
*/ |
1683 |
|
public void testThenComposeAsync2() { |
1684 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1711 |
|
} |
1712 |
|
|
1713 |
|
|
1714 |
< |
// aaync with explicit executors |
1714 |
> |
// async with explicit executors |
1715 |
|
|
1716 |
|
/** |
1717 |
|
* thenRunAsync result completes normally after normal completion of source |
2288 |
|
} |
2289 |
|
|
2290 |
|
/** |
2291 |
< |
* thenCompse result completes normally after normal completion of source |
2291 |
> |
* thenComposeAsync result completes normally after normal |
2292 |
> |
* completion of source |
2293 |
|
*/ |
2294 |
|
public void testThenComposeAsyncE() { |
2295 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2300 |
|
} |
2301 |
|
|
2302 |
|
/** |
2303 |
< |
* thenComposeAsync result completes exceptionally after exceptional |
2304 |
< |
* completion of source |
2303 |
> |
* thenComposeAsync result completes exceptionally after |
2304 |
> |
* exceptional completion of source |
2305 |
|
*/ |
2306 |
|
public void testThenComposeAsync2E() { |
2307 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2352 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
2353 |
|
for (int i = 0; i < k; ++i) |
2354 |
|
fs[i] = new CompletableFuture<Integer>(); |
2355 |
< |
CompletableFuture<?> f = CompletableFuture.allOf(fs); |
2355 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2356 |
|
for (int i = 0; i < k; ++i) { |
2357 |
|
checkIncomplete(f); |
2358 |
|
fs[i].complete(one); |
2359 |
|
} |
2360 |
< |
assertTrue(f.isDone()); |
2365 |
< |
assertFalse(f.isCancelled()); |
2360 |
> |
checkCompletedNormally(f, null); |
2361 |
|
} |
2362 |
|
} |
2363 |
|
|
2377 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
2378 |
|
for (int i = 0; i < k; ++i) |
2379 |
|
fs[i] = new CompletableFuture<Integer>(); |
2380 |
< |
CompletableFuture<?> f = CompletableFuture.anyOf(fs); |
2380 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
2381 |
|
checkIncomplete(f); |
2382 |
|
for (int i = 0; i < k; ++i) { |
2383 |
|
fs[i].complete(one); |
2384 |
< |
assertTrue(f.isDone()); |
2384 |
> |
checkCompletedNormally(f, one); |
2385 |
|
} |
2386 |
|
} |
2387 |
|
} |