5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
< |
import junit.framework.*; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
> |
import static java.util.concurrent.CompletableFuture.completedFuture; |
11 |
> |
import static java.util.concurrent.CompletableFuture.failedFuture; |
12 |
> |
|
13 |
> |
import java.lang.reflect.Method; |
14 |
> |
import java.lang.reflect.Modifier; |
15 |
> |
|
16 |
> |
import java.util.stream.Collectors; |
17 |
> |
import java.util.stream.Stream; |
18 |
> |
|
19 |
> |
import java.util.ArrayList; |
20 |
> |
import java.util.Arrays; |
21 |
> |
import java.util.List; |
22 |
> |
import java.util.Objects; |
23 |
> |
import java.util.Set; |
24 |
|
import java.util.concurrent.Callable; |
10 |
– |
import java.util.concurrent.Executor; |
11 |
– |
import java.util.concurrent.ExecutorService; |
12 |
– |
import java.util.concurrent.Executors; |
25 |
|
import java.util.concurrent.CancellationException; |
14 |
– |
import java.util.concurrent.CountDownLatch; |
15 |
– |
import java.util.concurrent.ExecutionException; |
16 |
– |
import java.util.concurrent.Future; |
26 |
|
import java.util.concurrent.CompletableFuture; |
27 |
|
import java.util.concurrent.CompletionException; |
28 |
+ |
import java.util.concurrent.CompletionStage; |
29 |
+ |
import java.util.concurrent.ExecutionException; |
30 |
+ |
import java.util.concurrent.Executor; |
31 |
+ |
import java.util.concurrent.ForkJoinPool; |
32 |
+ |
import java.util.concurrent.ForkJoinTask; |
33 |
+ |
import java.util.concurrent.RejectedExecutionException; |
34 |
|
import java.util.concurrent.TimeoutException; |
35 |
|
import java.util.concurrent.atomic.AtomicInteger; |
36 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
23 |
< |
import java.util.*; |
24 |
< |
import java.util.function.Supplier; |
25 |
< |
import java.util.function.Consumer; |
36 |
> |
import java.util.concurrent.atomic.AtomicReference; |
37 |
|
import java.util.function.BiConsumer; |
27 |
– |
import java.util.function.Function; |
38 |
|
import java.util.function.BiFunction; |
39 |
+ |
import java.util.function.Consumer; |
40 |
+ |
import java.util.function.Function; |
41 |
+ |
import java.util.function.Predicate; |
42 |
+ |
import java.util.function.Supplier; |
43 |
+ |
|
44 |
+ |
import junit.framework.AssertionFailedError; |
45 |
+ |
import junit.framework.Test; |
46 |
+ |
import junit.framework.TestSuite; |
47 |
|
|
48 |
|
public class CompletableFutureTest extends JSR166TestCase { |
49 |
|
|
50 |
|
public static void main(String[] args) { |
51 |
< |
junit.textui.TestRunner.run(suite()); |
51 |
> |
main(suite(), args); |
52 |
|
} |
53 |
|
public static Test suite() { |
54 |
|
return new TestSuite(CompletableFutureTest.class); |
59 |
|
void checkIncomplete(CompletableFuture<?> f) { |
60 |
|
assertFalse(f.isDone()); |
61 |
|
assertFalse(f.isCancelled()); |
62 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
62 |
> |
assertTrue(f.toString().contains("Not completed")); |
63 |
|
try { |
64 |
|
assertNull(f.getNow(null)); |
65 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
66 |
|
try { |
67 |
< |
f.get(0L, SECONDS); |
67 |
> |
f.get(randomExpiredTimeout(), randomTimeUnit()); |
68 |
|
shouldThrow(); |
69 |
|
} |
70 |
|
catch (TimeoutException success) {} |
72 |
|
} |
73 |
|
|
74 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
75 |
< |
try { |
76 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
59 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
75 |
> |
checkTimedGet(f, value); |
76 |
> |
|
77 |
|
try { |
78 |
|
assertEquals(value, f.join()); |
62 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
63 |
– |
try { |
79 |
|
assertEquals(value, f.getNow(null)); |
65 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
66 |
– |
try { |
80 |
|
assertEquals(value, f.get()); |
81 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
82 |
|
assertTrue(f.isDone()); |
83 |
|
assertFalse(f.isCancelled()); |
84 |
+ |
assertFalse(f.isCompletedExceptionally()); |
85 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
86 |
|
} |
87 |
|
|
88 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
88 |
> |
/** |
89 |
> |
* Returns the "raw" internal exceptional completion of f, |
90 |
> |
* without any additional wrapping with CompletionException. |
91 |
> |
*/ |
92 |
> |
Throwable exceptionalCompletion(CompletableFuture<?> f) { |
93 |
> |
// handle (and whenComplete and exceptionally) can distinguish |
94 |
> |
// between "direct" and "wrapped" exceptional completion |
95 |
> |
return f.handle((u, t) -> t).join(); |
96 |
> |
} |
97 |
> |
|
98 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, |
99 |
> |
boolean wrapped, |
100 |
> |
Consumer<Throwable> checker) { |
101 |
> |
Throwable cause = exceptionalCompletion(f); |
102 |
> |
if (wrapped) { |
103 |
> |
assertTrue(cause instanceof CompletionException); |
104 |
> |
cause = cause.getCause(); |
105 |
> |
} |
106 |
> |
checker.accept(cause); |
107 |
> |
|
108 |
> |
long startTime = System.nanoTime(); |
109 |
|
try { |
110 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
111 |
|
shouldThrow(); |
112 |
|
} catch (ExecutionException success) { |
113 |
< |
assertTrue(success.getCause() instanceof CFException); |
113 |
> |
assertSame(cause, success.getCause()); |
114 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
115 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
116 |
+ |
|
117 |
|
try { |
118 |
|
f.join(); |
119 |
|
shouldThrow(); |
120 |
|
} catch (CompletionException success) { |
121 |
< |
assertTrue(success.getCause() instanceof CFException); |
122 |
< |
} |
121 |
> |
assertSame(cause, success.getCause()); |
122 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
123 |
> |
|
124 |
|
try { |
125 |
|
f.getNow(null); |
126 |
|
shouldThrow(); |
127 |
|
} catch (CompletionException success) { |
128 |
< |
assertTrue(success.getCause() instanceof CFException); |
129 |
< |
} |
128 |
> |
assertSame(cause, success.getCause()); |
129 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
130 |
> |
|
131 |
|
try { |
132 |
|
f.get(); |
133 |
|
shouldThrow(); |
134 |
|
} catch (ExecutionException success) { |
135 |
< |
assertTrue(success.getCause() instanceof CFException); |
135 |
> |
assertSame(cause, success.getCause()); |
136 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
137 |
< |
assertTrue(f.isDone()); |
137 |
> |
|
138 |
|
assertFalse(f.isCancelled()); |
139 |
+ |
assertTrue(f.isDone()); |
140 |
+ |
assertTrue(f.isCompletedExceptionally()); |
141 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
142 |
|
} |
143 |
|
|
144 |
+ |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
145 |
+ |
checkCompletedExceptionally(f, true, |
146 |
+ |
t -> assertTrue(t instanceof CFException)); |
147 |
+ |
} |
148 |
+ |
|
149 |
+ |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
150 |
+ |
checkCompletedExceptionally(f, true, |
151 |
+ |
t -> assertTrue(t instanceof CancellationException)); |
152 |
+ |
} |
153 |
+ |
|
154 |
+ |
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
155 |
+ |
checkCompletedExceptionally(f, false, |
156 |
+ |
t -> assertTrue(t instanceof TimeoutException)); |
157 |
+ |
} |
158 |
+ |
|
159 |
+ |
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
160 |
+ |
Throwable ex) { |
161 |
+ |
checkCompletedExceptionally(f, true, t -> assertSame(t, ex)); |
162 |
+ |
} |
163 |
+ |
|
164 |
+ |
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
165 |
+ |
checkCompletedExceptionally(f, false, t -> assertSame(t, ex)); |
166 |
+ |
} |
167 |
+ |
|
168 |
|
void checkCancelled(CompletableFuture<?> f) { |
169 |
+ |
long startTime = System.nanoTime(); |
170 |
|
try { |
171 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
172 |
|
shouldThrow(); |
173 |
|
} catch (CancellationException success) { |
174 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
175 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
176 |
+ |
|
177 |
|
try { |
178 |
|
f.join(); |
179 |
|
shouldThrow(); |
187 |
|
shouldThrow(); |
188 |
|
} catch (CancellationException success) { |
189 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
123 |
– |
assertTrue(f.isDone()); |
124 |
– |
assertTrue(f.isCancelled()); |
125 |
– |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
126 |
– |
} |
190 |
|
|
191 |
< |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
192 |
< |
try { |
130 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
131 |
< |
shouldThrow(); |
132 |
< |
} catch (ExecutionException success) { |
133 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
134 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
135 |
< |
try { |
136 |
< |
f.join(); |
137 |
< |
shouldThrow(); |
138 |
< |
} catch (CompletionException success) { |
139 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
140 |
< |
} |
141 |
< |
try { |
142 |
< |
f.getNow(null); |
143 |
< |
shouldThrow(); |
144 |
< |
} catch (CompletionException success) { |
145 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
146 |
< |
} |
147 |
< |
try { |
148 |
< |
f.get(); |
149 |
< |
shouldThrow(); |
150 |
< |
} catch (ExecutionException success) { |
151 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
152 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
191 |
> |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
192 |
> |
|
193 |
|
assertTrue(f.isDone()); |
194 |
< |
assertFalse(f.isCancelled()); |
194 |
> |
assertTrue(f.isCompletedExceptionally()); |
195 |
> |
assertTrue(f.isCancelled()); |
196 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
197 |
|
} |
198 |
|
|
201 |
|
* by methods isDone, isCancelled, and getNow |
202 |
|
*/ |
203 |
|
public void testConstructor() { |
204 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
204 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
205 |
|
checkIncomplete(f); |
206 |
|
} |
207 |
|
|
210 |
|
* isCancelled, join, get, and getNow |
211 |
|
*/ |
212 |
|
public void testComplete() { |
213 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
213 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
214 |
> |
{ |
215 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
216 |
|
checkIncomplete(f); |
217 |
< |
f.complete(one); |
218 |
< |
checkCompletedNormally(f, one); |
219 |
< |
} |
217 |
> |
assertTrue(f.complete(v1)); |
218 |
> |
assertFalse(f.complete(v1)); |
219 |
> |
checkCompletedNormally(f, v1); |
220 |
> |
}} |
221 |
|
|
222 |
|
/** |
223 |
|
* completeExceptionally completes exceptionally, as indicated by |
224 |
|
* methods isDone, isCancelled, join, get, and getNow |
225 |
|
*/ |
226 |
|
public void testCompleteExceptionally() { |
227 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
227 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
228 |
> |
CFException ex = new CFException(); |
229 |
|
checkIncomplete(f); |
230 |
< |
f.completeExceptionally(new CFException()); |
231 |
< |
checkCompletedWithWrappedCFException(f); |
230 |
> |
f.completeExceptionally(ex); |
231 |
> |
checkCompletedExceptionally(f, ex); |
232 |
|
} |
233 |
|
|
234 |
|
/** |
236 |
|
* methods isDone, isCancelled, join, get, and getNow |
237 |
|
*/ |
238 |
|
public void testCancel() { |
239 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
239 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
240 |
> |
{ |
241 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
242 |
|
checkIncomplete(f); |
243 |
< |
assertTrue(f.cancel(true)); |
243 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
244 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
245 |
> |
assertTrue(f.cancel(!mayInterruptIfRunning)); |
246 |
|
checkCancelled(f); |
247 |
< |
} |
247 |
> |
}} |
248 |
|
|
249 |
|
/** |
250 |
|
* obtrudeValue forces completion with given value |
251 |
|
*/ |
252 |
|
public void testObtrudeValue() { |
253 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
253 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
254 |
|
checkIncomplete(f); |
255 |
< |
f.complete(one); |
255 |
> |
assertTrue(f.complete(one)); |
256 |
|
checkCompletedNormally(f, one); |
257 |
|
f.obtrudeValue(three); |
258 |
|
checkCompletedNormally(f, three); |
259 |
|
f.obtrudeValue(two); |
260 |
|
checkCompletedNormally(f, two); |
261 |
< |
f = new CompletableFuture<Integer>(); |
261 |
> |
f = new CompletableFuture<>(); |
262 |
|
f.obtrudeValue(three); |
263 |
|
checkCompletedNormally(f, three); |
264 |
< |
f = new CompletableFuture<Integer>(); |
264 |
> |
f.obtrudeValue(null); |
265 |
> |
checkCompletedNormally(f, null); |
266 |
> |
f = new CompletableFuture<>(); |
267 |
|
f.completeExceptionally(new CFException()); |
268 |
|
f.obtrudeValue(four); |
269 |
|
checkCompletedNormally(f, four); |
273 |
|
* obtrudeException forces completion with given exception |
274 |
|
*/ |
275 |
|
public void testObtrudeException() { |
276 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
277 |
< |
checkIncomplete(f); |
278 |
< |
f.complete(one); |
279 |
< |
checkCompletedNormally(f, one); |
280 |
< |
f.obtrudeException(new CFException()); |
281 |
< |
checkCompletedWithWrappedCFException(f); |
282 |
< |
f = new CompletableFuture<Integer>(); |
283 |
< |
f.obtrudeException(new CFException()); |
284 |
< |
checkCompletedWithWrappedCFException(f); |
285 |
< |
f = new CompletableFuture<Integer>(); |
276 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
277 |
> |
{ |
278 |
> |
CFException ex; |
279 |
> |
CompletableFuture<Integer> f; |
280 |
> |
|
281 |
> |
f = new CompletableFuture<>(); |
282 |
> |
assertTrue(f.complete(v1)); |
283 |
> |
for (int i = 0; i < 2; i++) { |
284 |
> |
f.obtrudeException(ex = new CFException()); |
285 |
> |
checkCompletedExceptionally(f, ex); |
286 |
> |
} |
287 |
> |
|
288 |
> |
f = new CompletableFuture<>(); |
289 |
> |
for (int i = 0; i < 2; i++) { |
290 |
> |
f.obtrudeException(ex = new CFException()); |
291 |
> |
checkCompletedExceptionally(f, ex); |
292 |
> |
} |
293 |
> |
|
294 |
> |
f = new CompletableFuture<>(); |
295 |
> |
f.completeExceptionally(ex = new CFException()); |
296 |
> |
f.obtrudeValue(v1); |
297 |
> |
checkCompletedNormally(f, v1); |
298 |
> |
f.obtrudeException(ex = new CFException()); |
299 |
> |
checkCompletedExceptionally(f, ex); |
300 |
|
f.completeExceptionally(new CFException()); |
301 |
< |
f.obtrudeValue(four); |
302 |
< |
checkCompletedNormally(f, four); |
303 |
< |
f.obtrudeException(new CFException()); |
304 |
< |
checkCompletedWithWrappedCFException(f); |
240 |
< |
} |
301 |
> |
checkCompletedExceptionally(f, ex); |
302 |
> |
assertFalse(f.complete(v1)); |
303 |
> |
checkCompletedExceptionally(f, ex); |
304 |
> |
}} |
305 |
|
|
306 |
|
/** |
307 |
|
* getNumberOfDependents returns number of dependent tasks |
308 |
|
*/ |
309 |
|
public void testGetNumberOfDependents() { |
310 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
311 |
< |
assertEquals(f.getNumberOfDependents(), 0); |
312 |
< |
CompletableFuture g = f.thenRun(new Noop()); |
313 |
< |
assertEquals(f.getNumberOfDependents(), 1); |
314 |
< |
assertEquals(g.getNumberOfDependents(), 0); |
315 |
< |
CompletableFuture h = f.thenRun(new Noop()); |
316 |
< |
assertEquals(f.getNumberOfDependents(), 2); |
317 |
< |
f.complete(1); |
310 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
311 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
312 |
> |
{ |
313 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
314 |
> |
assertEquals(0, f.getNumberOfDependents()); |
315 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
316 |
> |
assertEquals(1, f.getNumberOfDependents()); |
317 |
> |
assertEquals(0, g.getNumberOfDependents()); |
318 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
319 |
> |
assertEquals(2, f.getNumberOfDependents()); |
320 |
> |
assertEquals(0, h.getNumberOfDependents()); |
321 |
> |
assertTrue(f.complete(v1)); |
322 |
|
checkCompletedNormally(g, null); |
323 |
< |
assertEquals(f.getNumberOfDependents(), 0); |
324 |
< |
assertEquals(g.getNumberOfDependents(), 0); |
325 |
< |
} |
326 |
< |
|
323 |
> |
checkCompletedNormally(h, null); |
324 |
> |
assertEquals(0, f.getNumberOfDependents()); |
325 |
> |
assertEquals(0, g.getNumberOfDependents()); |
326 |
> |
assertEquals(0, h.getNumberOfDependents()); |
327 |
> |
}} |
328 |
|
|
329 |
|
/** |
330 |
|
* toString indicates current completion state |
335 |
|
f = new CompletableFuture<String>(); |
336 |
|
assertTrue(f.toString().contains("[Not completed]")); |
337 |
|
|
338 |
< |
f.complete("foo"); |
338 |
> |
assertTrue(f.complete("foo")); |
339 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
340 |
|
|
341 |
|
f = new CompletableFuture<String>(); |
342 |
< |
f.completeExceptionally(new IndexOutOfBoundsException()); |
342 |
> |
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
343 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
344 |
+ |
|
345 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
346 |
+ |
f = new CompletableFuture<String>(); |
347 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
348 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
349 |
+ |
} |
350 |
|
} |
351 |
|
|
352 |
|
/** |
357 |
|
checkCompletedNormally(f, "test"); |
358 |
|
} |
359 |
|
|
360 |
< |
// Choose non-commutative actions for better coverage |
360 |
> |
abstract static class CheckedAction { |
361 |
> |
int invocationCount = 0; |
362 |
> |
final ExecutionMode m; |
363 |
> |
CheckedAction(ExecutionMode m) { this.m = m; } |
364 |
> |
void invoked() { |
365 |
> |
m.checkExecutionMode(); |
366 |
> |
assertEquals(0, invocationCount++); |
367 |
> |
} |
368 |
> |
void assertNotInvoked() { assertEquals(0, invocationCount); } |
369 |
> |
void assertInvoked() { assertEquals(1, invocationCount); } |
370 |
> |
} |
371 |
> |
|
372 |
> |
abstract static class CheckedIntegerAction extends CheckedAction { |
373 |
> |
Integer value; |
374 |
> |
CheckedIntegerAction(ExecutionMode m) { super(m); } |
375 |
> |
void assertValue(Integer expected) { |
376 |
> |
assertInvoked(); |
377 |
> |
assertEquals(expected, value); |
378 |
> |
} |
379 |
> |
} |
380 |
> |
|
381 |
> |
static class IntegerSupplier extends CheckedAction |
382 |
> |
implements Supplier<Integer> |
383 |
> |
{ |
384 |
> |
final Integer value; |
385 |
> |
IntegerSupplier(ExecutionMode m, Integer value) { |
386 |
> |
super(m); |
387 |
> |
this.value = value; |
388 |
> |
} |
389 |
> |
public Integer get() { |
390 |
> |
invoked(); |
391 |
> |
return value; |
392 |
> |
} |
393 |
> |
} |
394 |
> |
|
395 |
> |
// A function that handles and produces null values as well. |
396 |
> |
static Integer inc(Integer x) { |
397 |
> |
return (x == null) ? null : x + 1; |
398 |
> |
} |
399 |
> |
|
400 |
> |
static class NoopConsumer extends CheckedIntegerAction |
401 |
> |
implements Consumer<Integer> |
402 |
> |
{ |
403 |
> |
NoopConsumer(ExecutionMode m) { super(m); } |
404 |
> |
public void accept(Integer x) { |
405 |
> |
invoked(); |
406 |
> |
value = x; |
407 |
> |
} |
408 |
> |
} |
409 |
> |
|
410 |
> |
static class IncFunction extends CheckedIntegerAction |
411 |
> |
implements Function<Integer,Integer> |
412 |
> |
{ |
413 |
> |
IncFunction(ExecutionMode m) { super(m); } |
414 |
> |
public Integer apply(Integer x) { |
415 |
> |
invoked(); |
416 |
> |
return value = inc(x); |
417 |
> |
} |
418 |
> |
} |
419 |
|
|
420 |
< |
static final Supplier<Integer> supplyOne = |
421 |
< |
() -> Integer.valueOf(1); |
422 |
< |
static final Function<Integer, Integer> inc = |
423 |
< |
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
424 |
< |
static final BiFunction<Integer, Integer, Integer> subtract = |
425 |
< |
(Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue()); |
293 |
< |
static final class IncAction implements Consumer<Integer> { |
294 |
< |
int value; |
295 |
< |
public void accept(Integer x) { value = x.intValue() + 1; } |
420 |
> |
// Choose non-commutative actions for better coverage |
421 |
> |
// A non-commutative function that handles and produces null values as well. |
422 |
> |
static Integer subtract(Integer x, Integer y) { |
423 |
> |
return (x == null && y == null) ? null : |
424 |
> |
((x == null) ? 42 : x.intValue()) |
425 |
> |
- ((y == null) ? 99 : y.intValue()); |
426 |
|
} |
427 |
< |
static final class AddAction implements BiConsumer<Integer, Integer> { |
428 |
< |
int value; |
427 |
> |
|
428 |
> |
static class SubtractAction extends CheckedIntegerAction |
429 |
> |
implements BiConsumer<Integer, Integer> |
430 |
> |
{ |
431 |
> |
SubtractAction(ExecutionMode m) { super(m); } |
432 |
|
public void accept(Integer x, Integer y) { |
433 |
< |
value = x.intValue() + y.intValue(); |
433 |
> |
invoked(); |
434 |
> |
value = subtract(x, y); |
435 |
|
} |
436 |
|
} |
437 |
< |
static final class Noop implements Runnable { |
438 |
< |
boolean ran; |
439 |
< |
public void run() { ran = true; } |
437 |
> |
|
438 |
> |
static class SubtractFunction extends CheckedIntegerAction |
439 |
> |
implements BiFunction<Integer, Integer, Integer> |
440 |
> |
{ |
441 |
> |
SubtractFunction(ExecutionMode m) { super(m); } |
442 |
> |
public Integer apply(Integer x, Integer y) { |
443 |
> |
invoked(); |
444 |
> |
return value = subtract(x, y); |
445 |
> |
} |
446 |
|
} |
447 |
|
|
448 |
< |
static final class FailingSupplier implements Supplier<Integer> { |
449 |
< |
boolean ran; |
450 |
< |
public Integer get() { ran = true; throw new CFException(); } |
448 |
> |
static class Noop extends CheckedAction implements Runnable { |
449 |
> |
Noop(ExecutionMode m) { super(m); } |
450 |
> |
public void run() { |
451 |
> |
invoked(); |
452 |
> |
} |
453 |
|
} |
454 |
< |
static final class FailingConsumer implements Consumer<Integer> { |
455 |
< |
boolean ran; |
456 |
< |
public void accept(Integer x) { ran = true; throw new CFException(); } |
454 |
> |
|
455 |
> |
static class FailingSupplier extends CheckedAction |
456 |
> |
implements Supplier<Integer> |
457 |
> |
{ |
458 |
> |
final CFException ex; |
459 |
> |
FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); } |
460 |
> |
public Integer get() { |
461 |
> |
invoked(); |
462 |
> |
throw ex; |
463 |
> |
} |
464 |
|
} |
465 |
< |
static final class FailingBiConsumer implements BiConsumer<Integer, Integer> { |
466 |
< |
boolean ran; |
467 |
< |
public void accept(Integer x, Integer y) { ran = true; throw new CFException(); } |
465 |
> |
|
466 |
> |
static class FailingConsumer extends CheckedIntegerAction |
467 |
> |
implements Consumer<Integer> |
468 |
> |
{ |
469 |
> |
final CFException ex; |
470 |
> |
FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
471 |
> |
public void accept(Integer x) { |
472 |
> |
invoked(); |
473 |
> |
value = x; |
474 |
> |
throw ex; |
475 |
> |
} |
476 |
|
} |
477 |
< |
static final class FailingFunction implements Function<Integer, Integer> { |
478 |
< |
boolean ran; |
479 |
< |
public Integer apply(Integer x) { ran = true; throw new CFException(); } |
477 |
> |
|
478 |
> |
static class FailingBiConsumer extends CheckedIntegerAction |
479 |
> |
implements BiConsumer<Integer, Integer> |
480 |
> |
{ |
481 |
> |
final CFException ex; |
482 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
483 |
> |
public void accept(Integer x, Integer y) { |
484 |
> |
invoked(); |
485 |
> |
value = subtract(x, y); |
486 |
> |
throw ex; |
487 |
> |
} |
488 |
|
} |
489 |
< |
static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> { |
490 |
< |
boolean ran; |
491 |
< |
public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); } |
489 |
> |
|
490 |
> |
static class FailingFunction extends CheckedIntegerAction |
491 |
> |
implements Function<Integer, Integer> |
492 |
> |
{ |
493 |
> |
final CFException ex; |
494 |
> |
FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
495 |
> |
public Integer apply(Integer x) { |
496 |
> |
invoked(); |
497 |
> |
value = x; |
498 |
> |
throw ex; |
499 |
> |
} |
500 |
> |
} |
501 |
> |
|
502 |
> |
static class FailingBiFunction extends CheckedIntegerAction |
503 |
> |
implements BiFunction<Integer, Integer, Integer> |
504 |
> |
{ |
505 |
> |
final CFException ex; |
506 |
> |
FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
507 |
> |
public Integer apply(Integer x, Integer y) { |
508 |
> |
invoked(); |
509 |
> |
value = subtract(x, y); |
510 |
> |
throw ex; |
511 |
> |
} |
512 |
|
} |
513 |
< |
static final class FailingNoop implements Runnable { |
514 |
< |
boolean ran; |
515 |
< |
public void run() { ran = true; throw new CFException(); } |
513 |
> |
|
514 |
> |
static class FailingRunnable extends CheckedAction implements Runnable { |
515 |
> |
final CFException ex; |
516 |
> |
FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); } |
517 |
> |
public void run() { |
518 |
> |
invoked(); |
519 |
> |
throw ex; |
520 |
> |
} |
521 |
|
} |
522 |
|
|
523 |
< |
static final class CompletableFutureInc |
524 |
< |
implements Function<Integer, CompletableFuture<Integer>> { |
525 |
< |
boolean ran; |
523 |
> |
static class CompletableFutureInc extends CheckedIntegerAction |
524 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
525 |
> |
{ |
526 |
> |
CompletableFutureInc(ExecutionMode m) { super(m); } |
527 |
|
public CompletableFuture<Integer> apply(Integer x) { |
528 |
< |
ran = true; |
529 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
530 |
< |
f.complete(Integer.valueOf(x.intValue() + 1)); |
528 |
> |
invoked(); |
529 |
> |
value = x; |
530 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
531 |
> |
assertTrue(f.complete(inc(x))); |
532 |
|
return f; |
533 |
|
} |
534 |
|
} |
535 |
|
|
536 |
< |
static final class FailingCompletableFutureFunction |
537 |
< |
implements Function<Integer, CompletableFuture<Integer>> { |
538 |
< |
boolean ran; |
536 |
> |
static class FailingCompletableFutureFunction extends CheckedIntegerAction |
537 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
538 |
> |
{ |
539 |
> |
final CFException ex; |
540 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
541 |
|
public CompletableFuture<Integer> apply(Integer x) { |
542 |
< |
ran = true; throw new CFException(); |
542 |
> |
invoked(); |
543 |
> |
value = x; |
544 |
> |
throw ex; |
545 |
> |
} |
546 |
> |
} |
547 |
> |
|
548 |
> |
static class CountingRejectingExecutor implements Executor { |
549 |
> |
final RejectedExecutionException ex = new RejectedExecutionException(); |
550 |
> |
final AtomicInteger count = new AtomicInteger(0); |
551 |
> |
public void execute(Runnable r) { |
552 |
> |
count.getAndIncrement(); |
553 |
> |
throw ex; |
554 |
|
} |
555 |
|
} |
556 |
|
|
557 |
|
// Used for explicit executor tests |
558 |
|
static final class ThreadExecutor implements Executor { |
559 |
< |
AtomicInteger count = new AtomicInteger(0); |
559 |
> |
final AtomicInteger count = new AtomicInteger(0); |
560 |
> |
static final ThreadGroup tg = new ThreadGroup("ThreadExecutor"); |
561 |
> |
static boolean startedCurrentThread() { |
562 |
> |
return Thread.currentThread().getThreadGroup() == tg; |
563 |
> |
} |
564 |
|
|
565 |
|
public void execute(Runnable r) { |
566 |
|
count.getAndIncrement(); |
567 |
< |
new Thread(r).start(); |
567 |
> |
new Thread(tg, r).start(); |
568 |
|
} |
569 |
|
} |
570 |
|
|
571 |
< |
static final class ExceptionToInteger implements Function<Throwable, Integer> { |
572 |
< |
public Integer apply(Throwable x) { return Integer.valueOf(3); } |
364 |
< |
} |
571 |
> |
static final boolean defaultExecutorIsCommonPool |
572 |
> |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
573 |
|
|
574 |
< |
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
575 |
< |
boolean ran; |
576 |
< |
public Integer apply(Integer x, Throwable t) { |
577 |
< |
ran = true; |
578 |
< |
return (t == null) ? two : three; |
579 |
< |
} |
580 |
< |
} |
574 |
> |
/** |
575 |
> |
* Permits the testing of parallel code for the 3 different |
576 |
> |
* execution modes without copy/pasting all the test methods. |
577 |
> |
*/ |
578 |
> |
enum ExecutionMode { |
579 |
> |
SYNC { |
580 |
> |
public void checkExecutionMode() { |
581 |
> |
assertFalse(ThreadExecutor.startedCurrentThread()); |
582 |
> |
assertNull(ForkJoinTask.getPool()); |
583 |
> |
} |
584 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
585 |
> |
throw new UnsupportedOperationException(); |
586 |
> |
} |
587 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
588 |
> |
throw new UnsupportedOperationException(); |
589 |
> |
} |
590 |
> |
public <T> CompletableFuture<Void> thenRun |
591 |
> |
(CompletableFuture<T> f, Runnable a) { |
592 |
> |
return f.thenRun(a); |
593 |
> |
} |
594 |
> |
public <T> CompletableFuture<Void> thenAccept |
595 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
596 |
> |
return f.thenAccept(a); |
597 |
> |
} |
598 |
> |
public <T,U> CompletableFuture<U> thenApply |
599 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
600 |
> |
return f.thenApply(a); |
601 |
> |
} |
602 |
> |
public <T,U> CompletableFuture<U> thenCompose |
603 |
> |
(CompletableFuture<T> f, |
604 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
605 |
> |
return f.thenCompose(a); |
606 |
> |
} |
607 |
> |
public <T,U> CompletableFuture<U> handle |
608 |
> |
(CompletableFuture<T> f, |
609 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
610 |
> |
return f.handle(a); |
611 |
> |
} |
612 |
> |
public <T> CompletableFuture<T> whenComplete |
613 |
> |
(CompletableFuture<T> f, |
614 |
> |
BiConsumer<? super T,? super Throwable> a) { |
615 |
> |
return f.whenComplete(a); |
616 |
> |
} |
617 |
> |
public <T,U> CompletableFuture<Void> runAfterBoth |
618 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
619 |
> |
return f.runAfterBoth(g, a); |
620 |
> |
} |
621 |
> |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
622 |
> |
(CompletableFuture<T> f, |
623 |
> |
CompletionStage<? extends U> g, |
624 |
> |
BiConsumer<? super T,? super U> a) { |
625 |
> |
return f.thenAcceptBoth(g, a); |
626 |
> |
} |
627 |
> |
public <T,U,V> CompletableFuture<V> thenCombine |
628 |
> |
(CompletableFuture<T> f, |
629 |
> |
CompletionStage<? extends U> g, |
630 |
> |
BiFunction<? super T,? super U,? extends V> a) { |
631 |
> |
return f.thenCombine(g, a); |
632 |
> |
} |
633 |
> |
public <T> CompletableFuture<Void> runAfterEither |
634 |
> |
(CompletableFuture<T> f, |
635 |
> |
CompletionStage<?> g, |
636 |
> |
java.lang.Runnable a) { |
637 |
> |
return f.runAfterEither(g, a); |
638 |
> |
} |
639 |
> |
public <T> CompletableFuture<Void> acceptEither |
640 |
> |
(CompletableFuture<T> f, |
641 |
> |
CompletionStage<? extends T> g, |
642 |
> |
Consumer<? super T> a) { |
643 |
> |
return f.acceptEither(g, a); |
644 |
> |
} |
645 |
> |
public <T,U> CompletableFuture<U> applyToEither |
646 |
> |
(CompletableFuture<T> f, |
647 |
> |
CompletionStage<? extends T> g, |
648 |
> |
Function<? super T,U> a) { |
649 |
> |
return f.applyToEither(g, a); |
650 |
> |
} |
651 |
> |
}, |
652 |
> |
|
653 |
> |
ASYNC { |
654 |
> |
public void checkExecutionMode() { |
655 |
> |
assertEquals(defaultExecutorIsCommonPool, |
656 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
657 |
> |
} |
658 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
659 |
> |
return CompletableFuture.runAsync(a); |
660 |
> |
} |
661 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
662 |
> |
return CompletableFuture.supplyAsync(a); |
663 |
> |
} |
664 |
> |
public <T> CompletableFuture<Void> thenRun |
665 |
> |
(CompletableFuture<T> f, Runnable a) { |
666 |
> |
return f.thenRunAsync(a); |
667 |
> |
} |
668 |
> |
public <T> CompletableFuture<Void> thenAccept |
669 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
670 |
> |
return f.thenAcceptAsync(a); |
671 |
> |
} |
672 |
> |
public <T,U> CompletableFuture<U> thenApply |
673 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
674 |
> |
return f.thenApplyAsync(a); |
675 |
> |
} |
676 |
> |
public <T,U> CompletableFuture<U> thenCompose |
677 |
> |
(CompletableFuture<T> f, |
678 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
679 |
> |
return f.thenComposeAsync(a); |
680 |
> |
} |
681 |
> |
public <T,U> CompletableFuture<U> handle |
682 |
> |
(CompletableFuture<T> f, |
683 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
684 |
> |
return f.handleAsync(a); |
685 |
> |
} |
686 |
> |
public <T> CompletableFuture<T> whenComplete |
687 |
> |
(CompletableFuture<T> f, |
688 |
> |
BiConsumer<? super T,? super Throwable> a) { |
689 |
> |
return f.whenCompleteAsync(a); |
690 |
> |
} |
691 |
> |
public <T,U> CompletableFuture<Void> runAfterBoth |
692 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
693 |
> |
return f.runAfterBothAsync(g, a); |
694 |
> |
} |
695 |
> |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
696 |
> |
(CompletableFuture<T> f, |
697 |
> |
CompletionStage<? extends U> g, |
698 |
> |
BiConsumer<? super T,? super U> a) { |
699 |
> |
return f.thenAcceptBothAsync(g, a); |
700 |
> |
} |
701 |
> |
public <T,U,V> CompletableFuture<V> thenCombine |
702 |
> |
(CompletableFuture<T> f, |
703 |
> |
CompletionStage<? extends U> g, |
704 |
> |
BiFunction<? super T,? super U,? extends V> a) { |
705 |
> |
return f.thenCombineAsync(g, a); |
706 |
> |
} |
707 |
> |
public <T> CompletableFuture<Void> runAfterEither |
708 |
> |
(CompletableFuture<T> f, |
709 |
> |
CompletionStage<?> g, |
710 |
> |
java.lang.Runnable a) { |
711 |
> |
return f.runAfterEitherAsync(g, a); |
712 |
> |
} |
713 |
> |
public <T> CompletableFuture<Void> acceptEither |
714 |
> |
(CompletableFuture<T> f, |
715 |
> |
CompletionStage<? extends T> g, |
716 |
> |
Consumer<? super T> a) { |
717 |
> |
return f.acceptEitherAsync(g, a); |
718 |
> |
} |
719 |
> |
public <T,U> CompletableFuture<U> applyToEither |
720 |
> |
(CompletableFuture<T> f, |
721 |
> |
CompletionStage<? extends T> g, |
722 |
> |
Function<? super T,U> a) { |
723 |
> |
return f.applyToEitherAsync(g, a); |
724 |
> |
} |
725 |
> |
}, |
726 |
|
|
727 |
+ |
EXECUTOR { |
728 |
+ |
public void checkExecutionMode() { |
729 |
+ |
assertTrue(ThreadExecutor.startedCurrentThread()); |
730 |
+ |
} |
731 |
+ |
public CompletableFuture<Void> runAsync(Runnable a) { |
732 |
+ |
return CompletableFuture.runAsync(a, new ThreadExecutor()); |
733 |
+ |
} |
734 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
735 |
+ |
return CompletableFuture.supplyAsync(a, new ThreadExecutor()); |
736 |
+ |
} |
737 |
+ |
public <T> CompletableFuture<Void> thenRun |
738 |
+ |
(CompletableFuture<T> f, Runnable a) { |
739 |
+ |
return f.thenRunAsync(a, new ThreadExecutor()); |
740 |
+ |
} |
741 |
+ |
public <T> CompletableFuture<Void> thenAccept |
742 |
+ |
(CompletableFuture<T> f, Consumer<? super T> a) { |
743 |
+ |
return f.thenAcceptAsync(a, new ThreadExecutor()); |
744 |
+ |
} |
745 |
+ |
public <T,U> CompletableFuture<U> thenApply |
746 |
+ |
(CompletableFuture<T> f, Function<? super T,U> a) { |
747 |
+ |
return f.thenApplyAsync(a, new ThreadExecutor()); |
748 |
+ |
} |
749 |
+ |
public <T,U> CompletableFuture<U> thenCompose |
750 |
+ |
(CompletableFuture<T> f, |
751 |
+ |
Function<? super T,? extends CompletionStage<U>> a) { |
752 |
+ |
return f.thenComposeAsync(a, new ThreadExecutor()); |
753 |
+ |
} |
754 |
+ |
public <T,U> CompletableFuture<U> handle |
755 |
+ |
(CompletableFuture<T> f, |
756 |
+ |
BiFunction<? super T,Throwable,? extends U> a) { |
757 |
+ |
return f.handleAsync(a, new ThreadExecutor()); |
758 |
+ |
} |
759 |
+ |
public <T> CompletableFuture<T> whenComplete |
760 |
+ |
(CompletableFuture<T> f, |
761 |
+ |
BiConsumer<? super T,? super Throwable> a) { |
762 |
+ |
return f.whenCompleteAsync(a, new ThreadExecutor()); |
763 |
+ |
} |
764 |
+ |
public <T,U> CompletableFuture<Void> runAfterBoth |
765 |
+ |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
766 |
+ |
return f.runAfterBothAsync(g, a, new ThreadExecutor()); |
767 |
+ |
} |
768 |
+ |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
769 |
+ |
(CompletableFuture<T> f, |
770 |
+ |
CompletionStage<? extends U> g, |
771 |
+ |
BiConsumer<? super T,? super U> a) { |
772 |
+ |
return f.thenAcceptBothAsync(g, a, new ThreadExecutor()); |
773 |
+ |
} |
774 |
+ |
public <T,U,V> CompletableFuture<V> thenCombine |
775 |
+ |
(CompletableFuture<T> f, |
776 |
+ |
CompletionStage<? extends U> g, |
777 |
+ |
BiFunction<? super T,? super U,? extends V> a) { |
778 |
+ |
return f.thenCombineAsync(g, a, new ThreadExecutor()); |
779 |
+ |
} |
780 |
+ |
public <T> CompletableFuture<Void> runAfterEither |
781 |
+ |
(CompletableFuture<T> f, |
782 |
+ |
CompletionStage<?> g, |
783 |
+ |
java.lang.Runnable a) { |
784 |
+ |
return f.runAfterEitherAsync(g, a, new ThreadExecutor()); |
785 |
+ |
} |
786 |
+ |
public <T> CompletableFuture<Void> acceptEither |
787 |
+ |
(CompletableFuture<T> f, |
788 |
+ |
CompletionStage<? extends T> g, |
789 |
+ |
Consumer<? super T> a) { |
790 |
+ |
return f.acceptEitherAsync(g, a, new ThreadExecutor()); |
791 |
+ |
} |
792 |
+ |
public <T,U> CompletableFuture<U> applyToEither |
793 |
+ |
(CompletableFuture<T> f, |
794 |
+ |
CompletionStage<? extends T> g, |
795 |
+ |
Function<? super T,U> a) { |
796 |
+ |
return f.applyToEitherAsync(g, a, new ThreadExecutor()); |
797 |
+ |
} |
798 |
+ |
}; |
799 |
+ |
|
800 |
+ |
public abstract void checkExecutionMode(); |
801 |
+ |
public abstract CompletableFuture<Void> runAsync(Runnable a); |
802 |
+ |
public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); |
803 |
+ |
public abstract <T> CompletableFuture<Void> thenRun |
804 |
+ |
(CompletableFuture<T> f, Runnable a); |
805 |
+ |
public abstract <T> CompletableFuture<Void> thenAccept |
806 |
+ |
(CompletableFuture<T> f, Consumer<? super T> a); |
807 |
+ |
public abstract <T,U> CompletableFuture<U> thenApply |
808 |
+ |
(CompletableFuture<T> f, Function<? super T,U> a); |
809 |
+ |
public abstract <T,U> CompletableFuture<U> thenCompose |
810 |
+ |
(CompletableFuture<T> f, |
811 |
+ |
Function<? super T,? extends CompletionStage<U>> a); |
812 |
+ |
public abstract <T,U> CompletableFuture<U> handle |
813 |
+ |
(CompletableFuture<T> f, |
814 |
+ |
BiFunction<? super T,Throwable,? extends U> a); |
815 |
+ |
public abstract <T> CompletableFuture<T> whenComplete |
816 |
+ |
(CompletableFuture<T> f, |
817 |
+ |
BiConsumer<? super T,? super Throwable> a); |
818 |
+ |
public abstract <T,U> CompletableFuture<Void> runAfterBoth |
819 |
+ |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a); |
820 |
+ |
public abstract <T,U> CompletableFuture<Void> thenAcceptBoth |
821 |
+ |
(CompletableFuture<T> f, |
822 |
+ |
CompletionStage<? extends U> g, |
823 |
+ |
BiConsumer<? super T,? super U> a); |
824 |
+ |
public abstract <T,U,V> CompletableFuture<V> thenCombine |
825 |
+ |
(CompletableFuture<T> f, |
826 |
+ |
CompletionStage<? extends U> g, |
827 |
+ |
BiFunction<? super T,? super U,? extends V> a); |
828 |
+ |
public abstract <T> CompletableFuture<Void> runAfterEither |
829 |
+ |
(CompletableFuture<T> f, |
830 |
+ |
CompletionStage<?> g, |
831 |
+ |
java.lang.Runnable a); |
832 |
+ |
public abstract <T> CompletableFuture<Void> acceptEither |
833 |
+ |
(CompletableFuture<T> f, |
834 |
+ |
CompletionStage<? extends T> g, |
835 |
+ |
Consumer<? super T> a); |
836 |
+ |
public abstract <T,U> CompletableFuture<U> applyToEither |
837 |
+ |
(CompletableFuture<T> f, |
838 |
+ |
CompletionStage<? extends T> g, |
839 |
+ |
Function<? super T,U> a); |
840 |
+ |
} |
841 |
+ |
|
842 |
+ |
/** |
843 |
+ |
* exceptionally action is not invoked when source completes |
844 |
+ |
* normally, and source result is propagated |
845 |
+ |
*/ |
846 |
+ |
public void testExceptionally_normalCompletion() { |
847 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
848 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
849 |
+ |
{ |
850 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
851 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
852 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
853 |
+ |
final CompletableFuture<Integer> g = f.exceptionally |
854 |
+ |
((Throwable t) -> { |
855 |
+ |
a.getAndIncrement(); |
856 |
+ |
threadFail("should not be called"); |
857 |
+ |
return null; // unreached |
858 |
+ |
}); |
859 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
860 |
+ |
|
861 |
+ |
checkCompletedNormally(g, v1); |
862 |
+ |
checkCompletedNormally(f, v1); |
863 |
+ |
assertEquals(0, a.get()); |
864 |
+ |
}} |
865 |
|
|
866 |
|
/** |
867 |
|
* exceptionally action completes with function value on source |
868 |
< |
* exception; otherwise with source value |
868 |
> |
* exception |
869 |
|
*/ |
870 |
< |
public void testExceptionally() { |
871 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
872 |
< |
ExceptionToInteger r = new ExceptionToInteger(); |
873 |
< |
CompletableFuture<Integer> g = f.exceptionally(r); |
874 |
< |
f.completeExceptionally(new CFException()); |
875 |
< |
checkCompletedNormally(g, three); |
870 |
> |
public void testExceptionally_exceptionalCompletion() { |
871 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
872 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
873 |
> |
{ |
874 |
> |
final AtomicInteger a = new AtomicInteger(0); |
875 |
> |
final CFException ex = new CFException(); |
876 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
877 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
878 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
879 |
> |
((Throwable t) -> { |
880 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
881 |
> |
threadAssertSame(t, ex); |
882 |
> |
a.getAndIncrement(); |
883 |
> |
return v1; |
884 |
> |
}); |
885 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
886 |
> |
|
887 |
> |
checkCompletedNormally(g, v1); |
888 |
> |
assertEquals(1, a.get()); |
889 |
> |
}} |
890 |
> |
|
891 |
> |
/** |
892 |
> |
* If an "exceptionally action" throws an exception, it completes |
893 |
> |
* exceptionally with that exception |
894 |
> |
*/ |
895 |
> |
public void testExceptionally_exceptionalCompletionActionFailed() { |
896 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
897 |
> |
{ |
898 |
> |
final AtomicInteger a = new AtomicInteger(0); |
899 |
> |
final CFException ex1 = new CFException(); |
900 |
> |
final CFException ex2 = new CFException(); |
901 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
902 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
903 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
904 |
> |
((Throwable t) -> { |
905 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
906 |
> |
threadAssertSame(t, ex1); |
907 |
> |
a.getAndIncrement(); |
908 |
> |
throw ex2; |
909 |
> |
}); |
910 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
911 |
> |
|
912 |
> |
checkCompletedWithWrappedException(g, ex2); |
913 |
> |
checkCompletedExceptionally(f, ex1); |
914 |
> |
assertEquals(1, a.get()); |
915 |
> |
}} |
916 |
> |
|
917 |
> |
/** |
918 |
> |
* whenComplete action executes on normal completion, propagating |
919 |
> |
* source result. |
920 |
> |
*/ |
921 |
> |
public void testWhenComplete_normalCompletion() { |
922 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
923 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
924 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
925 |
> |
{ |
926 |
> |
final AtomicInteger a = new AtomicInteger(0); |
927 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
928 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
929 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
930 |
> |
(f, |
931 |
> |
(Integer result, Throwable t) -> { |
932 |
> |
m.checkExecutionMode(); |
933 |
> |
threadAssertSame(result, v1); |
934 |
> |
threadAssertNull(t); |
935 |
> |
a.getAndIncrement(); |
936 |
> |
}); |
937 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
938 |
> |
|
939 |
> |
checkCompletedNormally(g, v1); |
940 |
> |
checkCompletedNormally(f, v1); |
941 |
> |
assertEquals(1, a.get()); |
942 |
> |
}} |
943 |
> |
|
944 |
> |
/** |
945 |
> |
* whenComplete action executes on exceptional completion, propagating |
946 |
> |
* source result. |
947 |
> |
*/ |
948 |
> |
public void testWhenComplete_exceptionalCompletion() { |
949 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
950 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
951 |
> |
{ |
952 |
> |
final AtomicInteger a = new AtomicInteger(0); |
953 |
> |
final CFException ex = new CFException(); |
954 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
955 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
956 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
957 |
> |
(f, |
958 |
> |
(Integer result, Throwable t) -> { |
959 |
> |
m.checkExecutionMode(); |
960 |
> |
threadAssertNull(result); |
961 |
> |
threadAssertSame(t, ex); |
962 |
> |
a.getAndIncrement(); |
963 |
> |
}); |
964 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
965 |
> |
|
966 |
> |
checkCompletedWithWrappedException(g, ex); |
967 |
> |
checkCompletedExceptionally(f, ex); |
968 |
> |
assertEquals(1, a.get()); |
969 |
> |
}} |
970 |
> |
|
971 |
> |
/** |
972 |
> |
* whenComplete action executes on cancelled source, propagating |
973 |
> |
* CancellationException. |
974 |
> |
*/ |
975 |
> |
public void testWhenComplete_sourceCancelled() { |
976 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
977 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
978 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
979 |
> |
{ |
980 |
> |
final AtomicInteger a = new AtomicInteger(0); |
981 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
982 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
983 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
984 |
> |
(f, |
985 |
> |
(Integer result, Throwable t) -> { |
986 |
> |
m.checkExecutionMode(); |
987 |
> |
threadAssertNull(result); |
988 |
> |
threadAssertTrue(t instanceof CancellationException); |
989 |
> |
a.getAndIncrement(); |
990 |
> |
}); |
991 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
992 |
|
|
993 |
< |
f = new CompletableFuture<Integer>(); |
994 |
< |
r = new ExceptionToInteger(); |
995 |
< |
g = f.exceptionally(r); |
996 |
< |
f.complete(one); |
390 |
< |
checkCompletedNormally(g, one); |
391 |
< |
} |
993 |
> |
checkCompletedWithWrappedCancellationException(g); |
994 |
> |
checkCancelled(f); |
995 |
> |
assertEquals(1, a.get()); |
996 |
> |
}} |
997 |
|
|
998 |
|
/** |
999 |
< |
* handle action completes normally with function value on either |
1000 |
< |
* normal or exceptional completion of source |
999 |
> |
* If a whenComplete action throws an exception when triggered by |
1000 |
> |
* a normal completion, it completes exceptionally |
1001 |
|
*/ |
1002 |
< |
public void testHandle() { |
1003 |
< |
CompletableFuture<Integer> f, g; |
1004 |
< |
IntegerHandler r; |
1002 |
> |
public void testWhenComplete_sourceCompletedNormallyActionFailed() { |
1003 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1004 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1005 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1006 |
> |
{ |
1007 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1008 |
> |
final CFException ex = new CFException(); |
1009 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1010 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1011 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1012 |
> |
(f, |
1013 |
> |
(Integer result, Throwable t) -> { |
1014 |
> |
m.checkExecutionMode(); |
1015 |
> |
threadAssertSame(result, v1); |
1016 |
> |
threadAssertNull(t); |
1017 |
> |
a.getAndIncrement(); |
1018 |
> |
throw ex; |
1019 |
> |
}); |
1020 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1021 |
> |
|
1022 |
> |
checkCompletedWithWrappedException(g, ex); |
1023 |
> |
checkCompletedNormally(f, v1); |
1024 |
> |
assertEquals(1, a.get()); |
1025 |
> |
}} |
1026 |
> |
|
1027 |
> |
/** |
1028 |
> |
* If a whenComplete action throws an exception when triggered by |
1029 |
> |
* a source completion that also throws an exception, the source |
1030 |
> |
* exception takes precedence (unlike handle) |
1031 |
> |
*/ |
1032 |
> |
public void testWhenComplete_sourceFailedActionFailed() { |
1033 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1034 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1035 |
> |
{ |
1036 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1037 |
> |
final CFException ex1 = new CFException(); |
1038 |
> |
final CFException ex2 = new CFException(); |
1039 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1040 |
> |
|
1041 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1042 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1043 |
> |
(f, |
1044 |
> |
(Integer result, Throwable t) -> { |
1045 |
> |
m.checkExecutionMode(); |
1046 |
> |
threadAssertSame(t, ex1); |
1047 |
> |
threadAssertNull(result); |
1048 |
> |
a.getAndIncrement(); |
1049 |
> |
throw ex2; |
1050 |
> |
}); |
1051 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1052 |
> |
|
1053 |
> |
checkCompletedWithWrappedException(g, ex1); |
1054 |
> |
checkCompletedExceptionally(f, ex1); |
1055 |
> |
if (testImplementationDetails) { |
1056 |
> |
assertEquals(1, ex1.getSuppressed().length); |
1057 |
> |
assertSame(ex2, ex1.getSuppressed()[0]); |
1058 |
> |
} |
1059 |
> |
assertEquals(1, a.get()); |
1060 |
> |
}} |
1061 |
|
|
1062 |
< |
f = new CompletableFuture<Integer>(); |
1063 |
< |
f.completeExceptionally(new CFException()); |
1064 |
< |
g = f.handle(r = new IntegerHandler()); |
1065 |
< |
assertTrue(r.ran); |
1066 |
< |
checkCompletedNormally(g, three); |
1067 |
< |
|
1068 |
< |
f = new CompletableFuture<Integer>(); |
1069 |
< |
g = f.handle(r = new IntegerHandler()); |
1070 |
< |
assertFalse(r.ran); |
1071 |
< |
f.completeExceptionally(new CFException()); |
1072 |
< |
checkCompletedNormally(g, three); |
1073 |
< |
assertTrue(r.ran); |
1062 |
> |
/** |
1063 |
> |
* handle action completes normally with function value on normal |
1064 |
> |
* completion of source |
1065 |
> |
*/ |
1066 |
> |
public void testHandle_normalCompletion() { |
1067 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1068 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1069 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1070 |
> |
{ |
1071 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1072 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1073 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1074 |
> |
final CompletableFuture<Integer> g = m.handle |
1075 |
> |
(f, |
1076 |
> |
(Integer result, Throwable t) -> { |
1077 |
> |
m.checkExecutionMode(); |
1078 |
> |
threadAssertSame(result, v1); |
1079 |
> |
threadAssertNull(t); |
1080 |
> |
a.getAndIncrement(); |
1081 |
> |
return inc(v1); |
1082 |
> |
}); |
1083 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1084 |
> |
|
1085 |
> |
checkCompletedNormally(g, inc(v1)); |
1086 |
> |
checkCompletedNormally(f, v1); |
1087 |
> |
assertEquals(1, a.get()); |
1088 |
> |
}} |
1089 |
|
|
1090 |
< |
f = new CompletableFuture<Integer>(); |
1091 |
< |
f.complete(one); |
1092 |
< |
g = f.handle(r = new IntegerHandler()); |
1093 |
< |
assertTrue(r.ran); |
1094 |
< |
checkCompletedNormally(g, two); |
1095 |
< |
|
1096 |
< |
f = new CompletableFuture<Integer>(); |
1097 |
< |
g = f.handle(r = new IntegerHandler()); |
1098 |
< |
assertFalse(r.ran); |
1099 |
< |
f.complete(one); |
1100 |
< |
assertTrue(r.ran); |
1101 |
< |
checkCompletedNormally(g, two); |
1102 |
< |
} |
1090 |
> |
/** |
1091 |
> |
* handle action completes normally with function value on |
1092 |
> |
* exceptional completion of source |
1093 |
> |
*/ |
1094 |
> |
public void testHandle_exceptionalCompletion() { |
1095 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1096 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1097 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1098 |
> |
{ |
1099 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1100 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1101 |
> |
final CFException ex = new CFException(); |
1102 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
1103 |
> |
final CompletableFuture<Integer> g = m.handle |
1104 |
> |
(f, |
1105 |
> |
(Integer result, Throwable t) -> { |
1106 |
> |
m.checkExecutionMode(); |
1107 |
> |
threadAssertNull(result); |
1108 |
> |
threadAssertSame(t, ex); |
1109 |
> |
a.getAndIncrement(); |
1110 |
> |
return v1; |
1111 |
> |
}); |
1112 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
1113 |
> |
|
1114 |
> |
checkCompletedNormally(g, v1); |
1115 |
> |
checkCompletedExceptionally(f, ex); |
1116 |
> |
assertEquals(1, a.get()); |
1117 |
> |
}} |
1118 |
> |
|
1119 |
> |
/** |
1120 |
> |
* handle action completes normally with function value on |
1121 |
> |
* cancelled source |
1122 |
> |
*/ |
1123 |
> |
public void testHandle_sourceCancelled() { |
1124 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1125 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1126 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1127 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1128 |
> |
{ |
1129 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1130 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1131 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1132 |
> |
final CompletableFuture<Integer> g = m.handle |
1133 |
> |
(f, |
1134 |
> |
(Integer result, Throwable t) -> { |
1135 |
> |
m.checkExecutionMode(); |
1136 |
> |
threadAssertNull(result); |
1137 |
> |
threadAssertTrue(t instanceof CancellationException); |
1138 |
> |
a.getAndIncrement(); |
1139 |
> |
return v1; |
1140 |
> |
}); |
1141 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1142 |
> |
|
1143 |
> |
checkCompletedNormally(g, v1); |
1144 |
> |
checkCancelled(f); |
1145 |
> |
assertEquals(1, a.get()); |
1146 |
> |
}} |
1147 |
|
|
1148 |
|
/** |
1149 |
< |
* runAsync completes after running Runnable |
1149 |
> |
* If a "handle action" throws an exception when triggered by |
1150 |
> |
* a normal completion, it completes exceptionally |
1151 |
|
*/ |
1152 |
< |
public void testRunAsync() { |
1153 |
< |
Noop r = new Noop(); |
1154 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
1155 |
< |
assertNull(f.join()); |
1156 |
< |
assertTrue(r.ran); |
1157 |
< |
checkCompletedNormally(f, null); |
1158 |
< |
} |
1152 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1153 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1154 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1155 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1156 |
> |
{ |
1157 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1158 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1159 |
> |
final CFException ex = new CFException(); |
1160 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1161 |
> |
final CompletableFuture<Integer> g = m.handle |
1162 |
> |
(f, |
1163 |
> |
(Integer result, Throwable t) -> { |
1164 |
> |
m.checkExecutionMode(); |
1165 |
> |
threadAssertSame(result, v1); |
1166 |
> |
threadAssertNull(t); |
1167 |
> |
a.getAndIncrement(); |
1168 |
> |
throw ex; |
1169 |
> |
}); |
1170 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1171 |
> |
|
1172 |
> |
checkCompletedWithWrappedException(g, ex); |
1173 |
> |
checkCompletedNormally(f, v1); |
1174 |
> |
assertEquals(1, a.get()); |
1175 |
> |
}} |
1176 |
> |
|
1177 |
> |
/** |
1178 |
> |
* If a "handle action" throws an exception when triggered by |
1179 |
> |
* a source completion that also throws an exception, the action |
1180 |
> |
* exception takes precedence (unlike whenComplete) |
1181 |
> |
*/ |
1182 |
> |
public void testHandle_sourceFailedActionFailed() { |
1183 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1184 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1185 |
> |
{ |
1186 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1187 |
> |
final CFException ex1 = new CFException(); |
1188 |
> |
final CFException ex2 = new CFException(); |
1189 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1190 |
> |
|
1191 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1192 |
> |
final CompletableFuture<Integer> g = m.handle |
1193 |
> |
(f, |
1194 |
> |
(Integer result, Throwable t) -> { |
1195 |
> |
m.checkExecutionMode(); |
1196 |
> |
threadAssertNull(result); |
1197 |
> |
threadAssertSame(ex1, t); |
1198 |
> |
a.getAndIncrement(); |
1199 |
> |
throw ex2; |
1200 |
> |
}); |
1201 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1202 |
> |
|
1203 |
> |
checkCompletedWithWrappedException(g, ex2); |
1204 |
> |
checkCompletedExceptionally(f, ex1); |
1205 |
> |
assertEquals(1, a.get()); |
1206 |
> |
}} |
1207 |
|
|
1208 |
|
/** |
1209 |
< |
* runAsync with executor completes after running Runnable |
1209 |
> |
* runAsync completes after running Runnable |
1210 |
|
*/ |
1211 |
< |
public void testRunAsync2() { |
1212 |
< |
Noop r = new Noop(); |
1213 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
1214 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
1211 |
> |
public void testRunAsync_normalCompletion() { |
1212 |
> |
ExecutionMode[] executionModes = { |
1213 |
> |
ExecutionMode.ASYNC, |
1214 |
> |
ExecutionMode.EXECUTOR, |
1215 |
> |
}; |
1216 |
> |
for (ExecutionMode m : executionModes) |
1217 |
> |
{ |
1218 |
> |
final Noop r = new Noop(m); |
1219 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1220 |
|
assertNull(f.join()); |
447 |
– |
assertTrue(r.ran); |
1221 |
|
checkCompletedNormally(f, null); |
1222 |
< |
assertEquals(1, exec.count.get()); |
1223 |
< |
} |
1222 |
> |
r.assertInvoked(); |
1223 |
> |
}} |
1224 |
|
|
1225 |
|
/** |
1226 |
|
* failing runAsync completes exceptionally after running Runnable |
1227 |
|
*/ |
1228 |
< |
public void testRunAsync3() { |
1229 |
< |
FailingNoop r = new FailingNoop(); |
1230 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
1231 |
< |
checkCompletedWithWrappedCFException(f); |
1232 |
< |
assertTrue(r.ran); |
1233 |
< |
} |
1228 |
> |
public void testRunAsync_exceptionalCompletion() { |
1229 |
> |
ExecutionMode[] executionModes = { |
1230 |
> |
ExecutionMode.ASYNC, |
1231 |
> |
ExecutionMode.EXECUTOR, |
1232 |
> |
}; |
1233 |
> |
for (ExecutionMode m : executionModes) |
1234 |
> |
{ |
1235 |
> |
final FailingRunnable r = new FailingRunnable(m); |
1236 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1237 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1238 |
> |
r.assertInvoked(); |
1239 |
> |
}} |
1240 |
|
|
1241 |
< |
/** |
1242 |
< |
* supplyAsync completes with result of supplier |
1243 |
< |
*/ |
1244 |
< |
public void testSupplyAsync() { |
1245 |
< |
CompletableFuture<Integer> f; |
1246 |
< |
f = CompletableFuture.supplyAsync(supplyOne); |
1247 |
< |
assertEquals(f.join(), one); |
1248 |
< |
checkCompletedNormally(f, one); |
1241 |
> |
public void testRunAsync_rejectingExecutor() { |
1242 |
> |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1243 |
> |
try { |
1244 |
> |
CompletableFuture.runAsync(() -> {}, e); |
1245 |
> |
shouldThrow(); |
1246 |
> |
} catch (Throwable t) { |
1247 |
> |
assertSame(e.ex, t); |
1248 |
> |
} |
1249 |
> |
|
1250 |
> |
assertEquals(1, e.count.get()); |
1251 |
|
} |
1252 |
|
|
1253 |
|
/** |
1254 |
< |
* supplyAsync with executor completes with result of supplier |
1254 |
> |
* supplyAsync completes with result of supplier |
1255 |
|
*/ |
1256 |
< |
public void testSupplyAsync2() { |
1257 |
< |
CompletableFuture<Integer> f; |
1258 |
< |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
1259 |
< |
assertEquals(f.join(), one); |
1260 |
< |
checkCompletedNormally(f, one); |
1261 |
< |
} |
1256 |
> |
public void testSupplyAsync_normalCompletion() { |
1257 |
> |
ExecutionMode[] executionModes = { |
1258 |
> |
ExecutionMode.ASYNC, |
1259 |
> |
ExecutionMode.EXECUTOR, |
1260 |
> |
}; |
1261 |
> |
for (ExecutionMode m : executionModes) |
1262 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1263 |
> |
{ |
1264 |
> |
final IntegerSupplier r = new IntegerSupplier(m, v1); |
1265 |
> |
final CompletableFuture<Integer> f = m.supplyAsync(r); |
1266 |
> |
assertSame(v1, f.join()); |
1267 |
> |
checkCompletedNormally(f, v1); |
1268 |
> |
r.assertInvoked(); |
1269 |
> |
}} |
1270 |
|
|
1271 |
|
/** |
1272 |
|
* Failing supplyAsync completes exceptionally |
1273 |
|
*/ |
1274 |
< |
public void testSupplyAsync3() { |
1275 |
< |
FailingSupplier r = new FailingSupplier(); |
1276 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r); |
1277 |
< |
checkCompletedWithWrappedCFException(f); |
1278 |
< |
assertTrue(r.ran); |
1274 |
> |
public void testSupplyAsync_exceptionalCompletion() { |
1275 |
> |
ExecutionMode[] executionModes = { |
1276 |
> |
ExecutionMode.ASYNC, |
1277 |
> |
ExecutionMode.EXECUTOR, |
1278 |
> |
}; |
1279 |
> |
for (ExecutionMode m : executionModes) |
1280 |
> |
{ |
1281 |
> |
FailingSupplier r = new FailingSupplier(m); |
1282 |
> |
CompletableFuture<Integer> f = m.supplyAsync(r); |
1283 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1284 |
> |
r.assertInvoked(); |
1285 |
> |
}} |
1286 |
> |
|
1287 |
> |
public void testSupplyAsync_rejectingExecutor() { |
1288 |
> |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1289 |
> |
try { |
1290 |
> |
CompletableFuture.supplyAsync(() -> null, e); |
1291 |
> |
shouldThrow(); |
1292 |
> |
} catch (Throwable t) { |
1293 |
> |
assertSame(e.ex, t); |
1294 |
> |
} |
1295 |
> |
|
1296 |
> |
assertEquals(1, e.count.get()); |
1297 |
|
} |
1298 |
|
|
1299 |
|
// seq completion methods |
1301 |
|
/** |
1302 |
|
* thenRun result completes normally after normal completion of source |
1303 |
|
*/ |
1304 |
< |
public void testThenRun() { |
1305 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1306 |
< |
Noop r = new Noop(); |
1307 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
1308 |
< |
f.complete(null); |
1309 |
< |
checkCompletedNormally(g, null); |
1310 |
< |
// reordered version |
1311 |
< |
f = new CompletableFuture<Integer>(); |
1312 |
< |
f.complete(null); |
1313 |
< |
r = new Noop(); |
1314 |
< |
g = f.thenRun(r); |
1315 |
< |
checkCompletedNormally(g, null); |
1316 |
< |
} |
1304 |
> |
public void testThenRun_normalCompletion() { |
1305 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1306 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1307 |
> |
{ |
1308 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1309 |
> |
final Noop[] rs = new Noop[6]; |
1310 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1311 |
> |
|
1312 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1313 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1314 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1315 |
> |
checkIncomplete(h0); |
1316 |
> |
checkIncomplete(h1); |
1317 |
> |
checkIncomplete(h2); |
1318 |
> |
assertTrue(f.complete(v1)); |
1319 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1320 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1321 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1322 |
> |
|
1323 |
> |
checkCompletedNormally(h0, null); |
1324 |
> |
checkCompletedNormally(h1, null); |
1325 |
> |
checkCompletedNormally(h2, null); |
1326 |
> |
checkCompletedNormally(h3, null); |
1327 |
> |
checkCompletedNormally(h4, null); |
1328 |
> |
checkCompletedNormally(h5, null); |
1329 |
> |
checkCompletedNormally(f, v1); |
1330 |
> |
for (Noop r : rs) r.assertInvoked(); |
1331 |
> |
}} |
1332 |
|
|
1333 |
|
/** |
1334 |
|
* thenRun result completes exceptionally after exceptional |
1335 |
|
* completion of source |
1336 |
|
*/ |
1337 |
< |
public void testThenRun2() { |
1338 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1339 |
< |
Noop r = new Noop(); |
1340 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
1341 |
< |
f.completeExceptionally(new CFException()); |
1342 |
< |
checkCompletedWithWrappedCFException(g); |
1343 |
< |
} |
1337 |
> |
public void testThenRun_exceptionalCompletion() { |
1338 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1339 |
> |
{ |
1340 |
> |
final CFException ex = new CFException(); |
1341 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1342 |
> |
final Noop[] rs = new Noop[6]; |
1343 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1344 |
> |
|
1345 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1346 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1347 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1348 |
> |
checkIncomplete(h0); |
1349 |
> |
checkIncomplete(h1); |
1350 |
> |
checkIncomplete(h2); |
1351 |
> |
assertTrue(f.completeExceptionally(ex)); |
1352 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1353 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1354 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1355 |
> |
|
1356 |
> |
checkCompletedWithWrappedException(h0, ex); |
1357 |
> |
checkCompletedWithWrappedException(h1, ex); |
1358 |
> |
checkCompletedWithWrappedException(h2, ex); |
1359 |
> |
checkCompletedWithWrappedException(h3, ex); |
1360 |
> |
checkCompletedWithWrappedException(h4, ex); |
1361 |
> |
checkCompletedWithWrappedException(h5, ex); |
1362 |
> |
checkCompletedExceptionally(f, ex); |
1363 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1364 |
> |
}} |
1365 |
|
|
1366 |
|
/** |
1367 |
< |
* thenRun result completes exceptionally if action does |
1367 |
> |
* thenRun result completes exceptionally if source cancelled |
1368 |
|
*/ |
1369 |
< |
public void testThenRun3() { |
1370 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1371 |
< |
FailingNoop r = new FailingNoop(); |
1372 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
1373 |
< |
f.complete(null); |
1374 |
< |
checkCompletedWithWrappedCFException(g); |
1375 |
< |
} |
1369 |
> |
public void testThenRun_sourceCancelled() { |
1370 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1371 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1372 |
> |
{ |
1373 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1374 |
> |
final Noop[] rs = new Noop[6]; |
1375 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1376 |
> |
|
1377 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1378 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1379 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1380 |
> |
checkIncomplete(h0); |
1381 |
> |
checkIncomplete(h1); |
1382 |
> |
checkIncomplete(h2); |
1383 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1384 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1385 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1386 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1387 |
> |
|
1388 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1389 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1390 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1391 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1392 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1393 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1394 |
> |
checkCancelled(f); |
1395 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1396 |
> |
}} |
1397 |
|
|
1398 |
|
/** |
1399 |
< |
* thenRun result completes exceptionally if source cancelled |
1399 |
> |
* thenRun result completes exceptionally if action does |
1400 |
|
*/ |
1401 |
< |
public void testThenRun4() { |
1402 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1403 |
< |
Noop r = new Noop(); |
1404 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
1405 |
< |
assertTrue(f.cancel(true)); |
1406 |
< |
checkCompletedWithWrappedCancellationException(g); |
1407 |
< |
} |
1401 |
> |
public void testThenRun_actionFailed() { |
1402 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1403 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1404 |
> |
{ |
1405 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1406 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1407 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1408 |
> |
|
1409 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1410 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1411 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1412 |
> |
assertTrue(f.complete(v1)); |
1413 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1414 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1415 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1416 |
> |
|
1417 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1418 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1419 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1420 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1421 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
1422 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
1423 |
> |
checkCompletedNormally(f, v1); |
1424 |
> |
}} |
1425 |
|
|
1426 |
|
/** |
1427 |
|
* thenApply result completes normally after normal completion of source |
1428 |
|
*/ |
1429 |
< |
public void testThenApply() { |
1430 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1431 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
1432 |
< |
f.complete(one); |
1433 |
< |
checkCompletedNormally(g, two); |
1434 |
< |
} |
1429 |
> |
public void testThenApply_normalCompletion() { |
1430 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1431 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1432 |
> |
{ |
1433 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1434 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1435 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1436 |
> |
|
1437 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1438 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1439 |
> |
checkIncomplete(h0); |
1440 |
> |
checkIncomplete(h1); |
1441 |
> |
assertTrue(f.complete(v1)); |
1442 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1443 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1444 |
> |
|
1445 |
> |
checkCompletedNormally(h0, inc(v1)); |
1446 |
> |
checkCompletedNormally(h1, inc(v1)); |
1447 |
> |
checkCompletedNormally(h2, inc(v1)); |
1448 |
> |
checkCompletedNormally(h3, inc(v1)); |
1449 |
> |
checkCompletedNormally(f, v1); |
1450 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1451 |
> |
}} |
1452 |
|
|
1453 |
|
/** |
1454 |
|
* thenApply result completes exceptionally after exceptional |
1455 |
|
* completion of source |
1456 |
|
*/ |
1457 |
< |
public void testThenApply2() { |
1458 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1459 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
1460 |
< |
f.completeExceptionally(new CFException()); |
1461 |
< |
checkCompletedWithWrappedCFException(g); |
1462 |
< |
} |
1457 |
> |
public void testThenApply_exceptionalCompletion() { |
1458 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1459 |
> |
{ |
1460 |
> |
final CFException ex = new CFException(); |
1461 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1462 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1463 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1464 |
> |
|
1465 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1466 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1467 |
> |
assertTrue(f.completeExceptionally(ex)); |
1468 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1469 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1470 |
> |
|
1471 |
> |
checkCompletedWithWrappedException(h0, ex); |
1472 |
> |
checkCompletedWithWrappedException(h1, ex); |
1473 |
> |
checkCompletedWithWrappedException(h2, ex); |
1474 |
> |
checkCompletedWithWrappedException(h3, ex); |
1475 |
> |
checkCompletedExceptionally(f, ex); |
1476 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1477 |
> |
}} |
1478 |
|
|
1479 |
|
/** |
1480 |
< |
* thenApply result completes exceptionally if action does |
1480 |
> |
* thenApply result completes exceptionally if source cancelled |
1481 |
|
*/ |
1482 |
< |
public void testThenApply3() { |
1483 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1484 |
< |
CompletableFuture<Integer> g = f.thenApply(new FailingFunction()); |
1485 |
< |
f.complete(one); |
1486 |
< |
checkCompletedWithWrappedCFException(g); |
1487 |
< |
} |
1482 |
> |
public void testThenApply_sourceCancelled() { |
1483 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1484 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1485 |
> |
{ |
1486 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1487 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1488 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1489 |
> |
|
1490 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1491 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1492 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1493 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1494 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1495 |
> |
|
1496 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1497 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1498 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1499 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1500 |
> |
checkCancelled(f); |
1501 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1502 |
> |
}} |
1503 |
|
|
1504 |
|
/** |
1505 |
< |
* thenApply result completes exceptionally if source cancelled |
1505 |
> |
* thenApply result completes exceptionally if action does |
1506 |
|
*/ |
1507 |
< |
public void testThenApply4() { |
1508 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1509 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
1510 |
< |
assertTrue(f.cancel(true)); |
1511 |
< |
checkCompletedWithWrappedCancellationException(g); |
1512 |
< |
} |
1507 |
> |
public void testThenApply_actionFailed() { |
1508 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1509 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1510 |
> |
{ |
1511 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1512 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1513 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1514 |
> |
|
1515 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1516 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1517 |
> |
assertTrue(f.complete(v1)); |
1518 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1519 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1520 |
> |
|
1521 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1522 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1523 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1524 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1525 |
> |
checkCompletedNormally(f, v1); |
1526 |
> |
}} |
1527 |
|
|
1528 |
|
/** |
1529 |
|
* thenAccept result completes normally after normal completion of source |
1530 |
|
*/ |
1531 |
< |
public void testThenAccept() { |
1532 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1533 |
< |
IncAction r = new IncAction(); |
1534 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1535 |
< |
f.complete(one); |
1536 |
< |
checkCompletedNormally(g, null); |
1537 |
< |
assertEquals(r.value, 2); |
1538 |
< |
} |
1531 |
> |
public void testThenAccept_normalCompletion() { |
1532 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1533 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1534 |
> |
{ |
1535 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1536 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1537 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1538 |
> |
|
1539 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1540 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1541 |
> |
checkIncomplete(h0); |
1542 |
> |
checkIncomplete(h1); |
1543 |
> |
assertTrue(f.complete(v1)); |
1544 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1545 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1546 |
> |
|
1547 |
> |
checkCompletedNormally(h0, null); |
1548 |
> |
checkCompletedNormally(h1, null); |
1549 |
> |
checkCompletedNormally(h2, null); |
1550 |
> |
checkCompletedNormally(h3, null); |
1551 |
> |
checkCompletedNormally(f, v1); |
1552 |
> |
for (NoopConsumer r : rs) r.assertValue(v1); |
1553 |
> |
}} |
1554 |
|
|
1555 |
|
/** |
1556 |
|
* thenAccept result completes exceptionally after exceptional |
1557 |
|
* completion of source |
1558 |
|
*/ |
1559 |
< |
public void testThenAccept2() { |
1560 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1561 |
< |
IncAction r = new IncAction(); |
1562 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1563 |
< |
f.completeExceptionally(new CFException()); |
1564 |
< |
checkCompletedWithWrappedCFException(g); |
1565 |
< |
} |
1559 |
> |
public void testThenAccept_exceptionalCompletion() { |
1560 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1561 |
> |
{ |
1562 |
> |
final CFException ex = new CFException(); |
1563 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1564 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1565 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1566 |
> |
|
1567 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1568 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1569 |
> |
assertTrue(f.completeExceptionally(ex)); |
1570 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1571 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1572 |
> |
|
1573 |
> |
checkCompletedWithWrappedException(h0, ex); |
1574 |
> |
checkCompletedWithWrappedException(h1, ex); |
1575 |
> |
checkCompletedWithWrappedException(h2, ex); |
1576 |
> |
checkCompletedWithWrappedException(h3, ex); |
1577 |
> |
checkCompletedExceptionally(f, ex); |
1578 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1579 |
> |
}} |
1580 |
|
|
1581 |
|
/** |
1582 |
< |
* thenAccept result completes exceptionally if action does |
1582 |
> |
* thenAccept result completes exceptionally if source cancelled |
1583 |
|
*/ |
1584 |
< |
public void testThenAccept3() { |
1585 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1586 |
< |
FailingConsumer r = new FailingConsumer(); |
1587 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1588 |
< |
f.complete(one); |
1589 |
< |
checkCompletedWithWrappedCFException(g); |
1590 |
< |
assertTrue(r.ran); |
1591 |
< |
} |
1584 |
> |
public void testThenAccept_sourceCancelled() { |
1585 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1586 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1587 |
> |
{ |
1588 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1589 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1590 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1591 |
> |
|
1592 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1593 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1594 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1595 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1596 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1597 |
> |
|
1598 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1599 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1600 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1601 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1602 |
> |
checkCancelled(f); |
1603 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1604 |
> |
}} |
1605 |
|
|
1606 |
|
/** |
1607 |
< |
* thenAccept result completes exceptionally if source cancelled |
1607 |
> |
* thenAccept result completes exceptionally if action does |
1608 |
|
*/ |
1609 |
< |
public void testThenAccept4() { |
1610 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1611 |
< |
IncAction r = new IncAction(); |
1612 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1613 |
< |
assertTrue(f.cancel(true)); |
1614 |
< |
checkCompletedWithWrappedCancellationException(g); |
1615 |
< |
} |
1616 |
< |
|
1609 |
> |
public void testThenAccept_actionFailed() { |
1610 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1611 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1612 |
> |
{ |
1613 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1614 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1615 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1616 |
> |
|
1617 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1618 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1619 |
> |
assertTrue(f.complete(v1)); |
1620 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1621 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1622 |
> |
|
1623 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1624 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1625 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1626 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1627 |
> |
checkCompletedNormally(f, v1); |
1628 |
> |
}} |
1629 |
|
|
1630 |
|
/** |
1631 |
|
* thenCombine result completes normally after normal completion |
1632 |
|
* of sources |
1633 |
|
*/ |
1634 |
< |
public void testThenCombine() { |
1635 |
< |
CompletableFuture<Integer> f, g, h; |
1636 |
< |
|
1637 |
< |
f = new CompletableFuture<Integer>(); |
1638 |
< |
g = new CompletableFuture<Integer>(); |
1639 |
< |
h = f.thenCombine(g, subtract); |
1640 |
< |
f.complete(3); |
1641 |
< |
checkIncomplete(h); |
1642 |
< |
g.complete(1); |
1643 |
< |
checkCompletedNormally(h, 2); |
1644 |
< |
|
1645 |
< |
f = new CompletableFuture<Integer>(); |
1646 |
< |
g = new CompletableFuture<Integer>(); |
1647 |
< |
h = f.thenCombine(g, subtract); |
1648 |
< |
g.complete(1); |
1649 |
< |
checkIncomplete(h); |
1650 |
< |
f.complete(3); |
1651 |
< |
checkCompletedNormally(h, 2); |
1652 |
< |
|
1653 |
< |
f = new CompletableFuture<Integer>(); |
1654 |
< |
g = new CompletableFuture<Integer>(); |
1655 |
< |
g.complete(1); |
1656 |
< |
f.complete(3); |
1657 |
< |
h = f.thenCombine(g, subtract); |
1658 |
< |
checkCompletedNormally(h, 2); |
1659 |
< |
} |
1634 |
> |
public void testThenCombine_normalCompletion() { |
1635 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1636 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1637 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1638 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1639 |
> |
{ |
1640 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1641 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1642 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1643 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1644 |
> |
|
1645 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1646 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1647 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1648 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1649 |
> |
|
1650 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1651 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1652 |
> |
assertTrue(fst.complete(w1)); |
1653 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1654 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1655 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1656 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1657 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1658 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1659 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1660 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1661 |
> |
assertTrue(snd.complete(w2)); |
1662 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1663 |
> |
|
1664 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1665 |
> |
checkCompletedNormally(h2, subtract(v1, v2)); |
1666 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1667 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1668 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1669 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1670 |
> |
|
1671 |
> |
checkCompletedNormally(f, v1); |
1672 |
> |
checkCompletedNormally(g, v2); |
1673 |
> |
}} |
1674 |
|
|
1675 |
|
/** |
1676 |
|
* thenCombine result completes exceptionally after exceptional |
1677 |
|
* completion of either source |
1678 |
|
*/ |
1679 |
< |
public void testThenCombine2() { |
1680 |
< |
CompletableFuture<Integer> f, g, h; |
1681 |
< |
|
1682 |
< |
f = new CompletableFuture<Integer>(); |
1683 |
< |
g = new CompletableFuture<Integer>(); |
1684 |
< |
h = f.thenCombine(g, subtract); |
1685 |
< |
f.completeExceptionally(new CFException()); |
1686 |
< |
checkIncomplete(h); |
1687 |
< |
g.complete(1); |
1688 |
< |
checkCompletedWithWrappedCFException(h); |
1689 |
< |
|
1690 |
< |
f = new CompletableFuture<Integer>(); |
1691 |
< |
g = new CompletableFuture<Integer>(); |
1692 |
< |
h = f.thenCombine(g, subtract); |
1693 |
< |
g.completeExceptionally(new CFException()); |
1694 |
< |
checkIncomplete(h); |
1695 |
< |
f.complete(3); |
1696 |
< |
checkCompletedWithWrappedCFException(h); |
1697 |
< |
|
1698 |
< |
f = new CompletableFuture<Integer>(); |
1699 |
< |
g = new CompletableFuture<Integer>(); |
1700 |
< |
f.complete(3); |
1701 |
< |
g.completeExceptionally(new CFException()); |
1702 |
< |
h = f.thenCombine(g, subtract); |
1703 |
< |
checkCompletedWithWrappedCFException(h); |
1704 |
< |
|
1705 |
< |
f = new CompletableFuture<Integer>(); |
1706 |
< |
g = new CompletableFuture<Integer>(); |
1707 |
< |
f.completeExceptionally(new CFException()); |
1708 |
< |
g.complete(3); |
1709 |
< |
h = f.thenCombine(g, subtract); |
1710 |
< |
checkCompletedWithWrappedCFException(h); |
1711 |
< |
} |
1679 |
> |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
1680 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1681 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1682 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1683 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1684 |
> |
{ |
1685 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1686 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1687 |
> |
final CFException ex = new CFException(); |
1688 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1689 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1690 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1691 |
> |
|
1692 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1693 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1694 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1695 |
> |
() -> fst.completeExceptionally(ex) : |
1696 |
> |
() -> fst.complete(v1); |
1697 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1698 |
> |
() -> snd.complete(v1) : |
1699 |
> |
() -> snd.completeExceptionally(ex); |
1700 |
> |
|
1701 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1702 |
> |
assertTrue(complete1.call()); |
1703 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1704 |
> |
checkIncomplete(h1); |
1705 |
> |
checkIncomplete(h2); |
1706 |
> |
assertTrue(complete2.call()); |
1707 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1708 |
> |
|
1709 |
> |
checkCompletedWithWrappedException(h1, ex); |
1710 |
> |
checkCompletedWithWrappedException(h2, ex); |
1711 |
> |
checkCompletedWithWrappedException(h3, ex); |
1712 |
> |
r1.assertNotInvoked(); |
1713 |
> |
r2.assertNotInvoked(); |
1714 |
> |
r3.assertNotInvoked(); |
1715 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1716 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1717 |
> |
}} |
1718 |
|
|
1719 |
|
/** |
1720 |
< |
* thenCombine result completes exceptionally if action does |
1720 |
> |
* thenCombine result completes exceptionally if either source cancelled |
1721 |
|
*/ |
1722 |
< |
public void testThenCombine3() { |
1723 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1724 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1725 |
< |
FailingBiFunction r = new FailingBiFunction(); |
1726 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, r); |
1727 |
< |
f.complete(one); |
1728 |
< |
checkIncomplete(g); |
1729 |
< |
assertFalse(r.ran); |
1730 |
< |
f2.complete(two); |
1731 |
< |
checkCompletedWithWrappedCFException(g); |
1732 |
< |
assertTrue(r.ran); |
1733 |
< |
} |
1722 |
> |
public void testThenCombine_sourceCancelled() throws Throwable { |
1723 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1724 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1725 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1726 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1727 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1728 |
> |
{ |
1729 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1730 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1731 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1732 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1733 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1734 |
> |
|
1735 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1736 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1737 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1738 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1739 |
> |
() -> fst.complete(v1); |
1740 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1741 |
> |
() -> snd.complete(v1) : |
1742 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1743 |
> |
|
1744 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1745 |
> |
assertTrue(complete1.call()); |
1746 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1747 |
> |
checkIncomplete(h1); |
1748 |
> |
checkIncomplete(h2); |
1749 |
> |
assertTrue(complete2.call()); |
1750 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1751 |
> |
|
1752 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1753 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1754 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1755 |
> |
r1.assertNotInvoked(); |
1756 |
> |
r2.assertNotInvoked(); |
1757 |
> |
r3.assertNotInvoked(); |
1758 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1759 |
> |
checkCancelled(failFirst ? fst : snd); |
1760 |
> |
}} |
1761 |
|
|
1762 |
|
/** |
1763 |
< |
* thenCombine result completes exceptionally if either source cancelled |
1763 |
> |
* thenCombine result completes exceptionally if action does |
1764 |
|
*/ |
1765 |
< |
public void testThenCombine4() { |
1766 |
< |
CompletableFuture<Integer> f, g, h; |
1767 |
< |
|
1768 |
< |
f = new CompletableFuture<Integer>(); |
1769 |
< |
g = new CompletableFuture<Integer>(); |
1770 |
< |
h = f.thenCombine(g, subtract); |
1771 |
< |
assertTrue(f.cancel(true)); |
1772 |
< |
checkIncomplete(h); |
1773 |
< |
g.complete(1); |
1774 |
< |
checkCompletedWithWrappedCancellationException(h); |
1775 |
< |
|
1776 |
< |
f = new CompletableFuture<Integer>(); |
1777 |
< |
g = new CompletableFuture<Integer>(); |
1778 |
< |
h = f.thenCombine(g, subtract); |
1779 |
< |
assertTrue(g.cancel(true)); |
1780 |
< |
checkIncomplete(h); |
1781 |
< |
f.complete(3); |
1782 |
< |
checkCompletedWithWrappedCancellationException(h); |
1783 |
< |
|
1784 |
< |
f = new CompletableFuture<Integer>(); |
1785 |
< |
g = new CompletableFuture<Integer>(); |
1786 |
< |
assertTrue(f.cancel(true)); |
1787 |
< |
assertTrue(g.cancel(true)); |
1788 |
< |
h = f.thenCombine(g, subtract); |
1789 |
< |
checkCompletedWithWrappedCancellationException(h); |
1790 |
< |
} |
1765 |
> |
public void testThenCombine_actionFailed() { |
1766 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1767 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1768 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1769 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1770 |
> |
{ |
1771 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1772 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1773 |
> |
final FailingBiFunction r1 = new FailingBiFunction(m); |
1774 |
> |
final FailingBiFunction r2 = new FailingBiFunction(m); |
1775 |
> |
final FailingBiFunction r3 = new FailingBiFunction(m); |
1776 |
> |
|
1777 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1778 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1779 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1780 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1781 |
> |
|
1782 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1783 |
> |
assertTrue(fst.complete(w1)); |
1784 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1785 |
> |
assertTrue(snd.complete(w2)); |
1786 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1787 |
> |
|
1788 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1789 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1790 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1791 |
> |
r1.assertInvoked(); |
1792 |
> |
r2.assertInvoked(); |
1793 |
> |
r3.assertInvoked(); |
1794 |
> |
checkCompletedNormally(f, v1); |
1795 |
> |
checkCompletedNormally(g, v2); |
1796 |
> |
}} |
1797 |
|
|
1798 |
|
/** |
1799 |
|
* thenAcceptBoth result completes normally after normal |
1800 |
|
* completion of sources |
1801 |
|
*/ |
1802 |
< |
public void testThenAcceptBoth() { |
1803 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1804 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1805 |
< |
AddAction r = new AddAction(); |
1806 |
< |
CompletableFuture<Void> g = f.thenAcceptBoth(f2, r); |
1807 |
< |
f.complete(one); |
1808 |
< |
checkIncomplete(g); |
1809 |
< |
f2.complete(two); |
1810 |
< |
checkCompletedNormally(g, null); |
1811 |
< |
assertEquals(r.value, 3); |
1812 |
< |
|
1813 |
< |
r = new AddAction(); |
1814 |
< |
f = new CompletableFuture<Integer>(); |
1815 |
< |
f.complete(one); |
1816 |
< |
f2 = new CompletableFuture<Integer>(); |
1817 |
< |
g = f.thenAcceptBoth(f2, r); |
1818 |
< |
checkIncomplete(g); |
1819 |
< |
f2.complete(two); |
1820 |
< |
checkCompletedNormally(g, null); |
1821 |
< |
assertEquals(r.value, 3); |
1822 |
< |
} |
1802 |
> |
public void testThenAcceptBoth_normalCompletion() { |
1803 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1804 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1805 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1806 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1807 |
> |
{ |
1808 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1809 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1810 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1811 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1812 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1813 |
> |
|
1814 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1815 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1816 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1817 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1818 |
> |
|
1819 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1820 |
> |
assertTrue(fst.complete(w1)); |
1821 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1822 |
> |
checkIncomplete(h1); |
1823 |
> |
checkIncomplete(h2); |
1824 |
> |
r1.assertNotInvoked(); |
1825 |
> |
r2.assertNotInvoked(); |
1826 |
> |
assertTrue(snd.complete(w2)); |
1827 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1828 |
> |
|
1829 |
> |
checkCompletedNormally(h1, null); |
1830 |
> |
checkCompletedNormally(h2, null); |
1831 |
> |
checkCompletedNormally(h3, null); |
1832 |
> |
r1.assertValue(subtract(v1, v2)); |
1833 |
> |
r2.assertValue(subtract(v1, v2)); |
1834 |
> |
r3.assertValue(subtract(v1, v2)); |
1835 |
> |
checkCompletedNormally(f, v1); |
1836 |
> |
checkCompletedNormally(g, v2); |
1837 |
> |
}} |
1838 |
|
|
1839 |
|
/** |
1840 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1841 |
|
* completion of either source |
1842 |
|
*/ |
1843 |
< |
public void testThenAcceptBoth2() { |
1844 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1845 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1846 |
< |
AddAction r = new AddAction(); |
1847 |
< |
CompletableFuture<Void> g = f.thenAcceptBoth(f2, r); |
1848 |
< |
f.completeExceptionally(new CFException()); |
1849 |
< |
f2.complete(two); |
1850 |
< |
checkCompletedWithWrappedCFException(g); |
1851 |
< |
|
1852 |
< |
r = new AddAction(); |
1853 |
< |
f = new CompletableFuture<Integer>(); |
1854 |
< |
f.complete(one); |
1855 |
< |
f2 = new CompletableFuture<Integer>(); |
1856 |
< |
g = f.thenAcceptBoth(f2, r); |
1857 |
< |
f2.completeExceptionally(new CFException()); |
1858 |
< |
checkCompletedWithWrappedCFException(g); |
1859 |
< |
} |
1843 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1844 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1845 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1846 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1847 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1848 |
> |
{ |
1849 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1850 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1851 |
> |
final CFException ex = new CFException(); |
1852 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1853 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1854 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1855 |
> |
|
1856 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1857 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1858 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1859 |
> |
() -> fst.completeExceptionally(ex) : |
1860 |
> |
() -> fst.complete(v1); |
1861 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1862 |
> |
() -> snd.complete(v1) : |
1863 |
> |
() -> snd.completeExceptionally(ex); |
1864 |
> |
|
1865 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1866 |
> |
assertTrue(complete1.call()); |
1867 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1868 |
> |
checkIncomplete(h1); |
1869 |
> |
checkIncomplete(h2); |
1870 |
> |
assertTrue(complete2.call()); |
1871 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1872 |
> |
|
1873 |
> |
checkCompletedWithWrappedException(h1, ex); |
1874 |
> |
checkCompletedWithWrappedException(h2, ex); |
1875 |
> |
checkCompletedWithWrappedException(h3, ex); |
1876 |
> |
r1.assertNotInvoked(); |
1877 |
> |
r2.assertNotInvoked(); |
1878 |
> |
r3.assertNotInvoked(); |
1879 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1880 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1881 |
> |
}} |
1882 |
|
|
1883 |
|
/** |
1884 |
< |
* thenAcceptBoth result completes exceptionally if action does |
1884 |
> |
* thenAcceptBoth result completes exceptionally if either source cancelled |
1885 |
|
*/ |
1886 |
< |
public void testThenAcceptBoth3() { |
1887 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1888 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1889 |
< |
FailingBiConsumer r = new FailingBiConsumer(); |
1890 |
< |
CompletableFuture<Void> g = f.thenAcceptBoth(f2, r); |
1891 |
< |
f.complete(one); |
1892 |
< |
checkIncomplete(g); |
1893 |
< |
f2.complete(two); |
1894 |
< |
checkCompletedWithWrappedCFException(g); |
1895 |
< |
} |
1886 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1887 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1888 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1889 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1890 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1891 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1892 |
> |
{ |
1893 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1894 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1895 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1896 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1897 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1898 |
> |
|
1899 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1900 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1901 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1902 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1903 |
> |
() -> fst.complete(v1); |
1904 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1905 |
> |
() -> snd.complete(v1) : |
1906 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1907 |
> |
|
1908 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1909 |
> |
assertTrue(complete1.call()); |
1910 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1911 |
> |
checkIncomplete(h1); |
1912 |
> |
checkIncomplete(h2); |
1913 |
> |
assertTrue(complete2.call()); |
1914 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1915 |
> |
|
1916 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1917 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1918 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1919 |
> |
r1.assertNotInvoked(); |
1920 |
> |
r2.assertNotInvoked(); |
1921 |
> |
r3.assertNotInvoked(); |
1922 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1923 |
> |
checkCancelled(failFirst ? fst : snd); |
1924 |
> |
}} |
1925 |
|
|
1926 |
|
/** |
1927 |
< |
* thenAcceptBoth result completes exceptionally if either source cancelled |
1927 |
> |
* thenAcceptBoth result completes exceptionally if action does |
1928 |
|
*/ |
1929 |
< |
public void testThenAcceptBoth4() { |
1930 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1931 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1932 |
< |
AddAction r = new AddAction(); |
1933 |
< |
CompletableFuture<Void> g = f.thenAcceptBoth(f2, r); |
1934 |
< |
assertTrue(f.cancel(true)); |
1935 |
< |
f2.complete(two); |
1936 |
< |
checkCompletedWithWrappedCancellationException(g); |
1937 |
< |
f = new CompletableFuture<Integer>(); |
1938 |
< |
f2 = new CompletableFuture<Integer>(); |
1939 |
< |
r = new AddAction(); |
1940 |
< |
g = f.thenAcceptBoth(f2, r); |
1941 |
< |
f.complete(one); |
1942 |
< |
assertTrue(f2.cancel(true)); |
1943 |
< |
checkCompletedWithWrappedCancellationException(g); |
1944 |
< |
} |
1929 |
> |
public void testThenAcceptBoth_actionFailed() { |
1930 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1931 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1932 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1933 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1934 |
> |
{ |
1935 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1936 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1937 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1938 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1939 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1940 |
> |
|
1941 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1942 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1943 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1944 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1945 |
> |
|
1946 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1947 |
> |
assertTrue(fst.complete(w1)); |
1948 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1949 |
> |
assertTrue(snd.complete(w2)); |
1950 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1951 |
> |
|
1952 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1953 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1954 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1955 |
> |
r1.assertInvoked(); |
1956 |
> |
r2.assertInvoked(); |
1957 |
> |
r3.assertInvoked(); |
1958 |
> |
checkCompletedNormally(f, v1); |
1959 |
> |
checkCompletedNormally(g, v2); |
1960 |
> |
}} |
1961 |
|
|
1962 |
|
/** |
1963 |
|
* runAfterBoth result completes normally after normal |
1964 |
|
* completion of sources |
1965 |
|
*/ |
1966 |
< |
public void testRunAfterBoth() { |
1967 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1968 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1969 |
< |
Noop r = new Noop(); |
1970 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
1971 |
< |
f.complete(one); |
1972 |
< |
checkIncomplete(g); |
1973 |
< |
f2.complete(two); |
1974 |
< |
checkCompletedNormally(g, null); |
1975 |
< |
assertTrue(r.ran); |
1976 |
< |
|
1977 |
< |
r = new Noop(); |
1978 |
< |
f = new CompletableFuture<Integer>(); |
1979 |
< |
f.complete(one); |
1980 |
< |
f2 = new CompletableFuture<Integer>(); |
1981 |
< |
g = f.runAfterBoth(f2, r); |
1982 |
< |
checkIncomplete(g); |
1983 |
< |
f2.complete(two); |
1984 |
< |
checkCompletedNormally(g, null); |
1985 |
< |
assertTrue(r.ran); |
1986 |
< |
} |
1966 |
> |
public void testRunAfterBoth_normalCompletion() { |
1967 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1968 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1969 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1970 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1971 |
> |
{ |
1972 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1973 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1974 |
> |
final Noop r1 = new Noop(m); |
1975 |
> |
final Noop r2 = new Noop(m); |
1976 |
> |
final Noop r3 = new Noop(m); |
1977 |
> |
|
1978 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1979 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1980 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1981 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1982 |
> |
|
1983 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1984 |
> |
assertTrue(fst.complete(w1)); |
1985 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1986 |
> |
checkIncomplete(h1); |
1987 |
> |
checkIncomplete(h2); |
1988 |
> |
r1.assertNotInvoked(); |
1989 |
> |
r2.assertNotInvoked(); |
1990 |
> |
assertTrue(snd.complete(w2)); |
1991 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1992 |
> |
|
1993 |
> |
checkCompletedNormally(h1, null); |
1994 |
> |
checkCompletedNormally(h2, null); |
1995 |
> |
checkCompletedNormally(h3, null); |
1996 |
> |
r1.assertInvoked(); |
1997 |
> |
r2.assertInvoked(); |
1998 |
> |
r3.assertInvoked(); |
1999 |
> |
checkCompletedNormally(f, v1); |
2000 |
> |
checkCompletedNormally(g, v2); |
2001 |
> |
}} |
2002 |
|
|
2003 |
|
/** |
2004 |
|
* runAfterBoth result completes exceptionally after exceptional |
2005 |
|
* completion of either source |
2006 |
|
*/ |
2007 |
< |
public void testRunAfterBoth2() { |
2008 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2009 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2010 |
< |
Noop r = new Noop(); |
2011 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
2012 |
< |
f.completeExceptionally(new CFException()); |
2013 |
< |
f2.complete(two); |
2014 |
< |
checkCompletedWithWrappedCFException(g); |
2015 |
< |
|
2016 |
< |
r = new Noop(); |
2017 |
< |
f = new CompletableFuture<Integer>(); |
2018 |
< |
f.complete(one); |
2019 |
< |
f2 = new CompletableFuture<Integer>(); |
2020 |
< |
g = f.runAfterBoth(f2, r); |
2021 |
< |
f2.completeExceptionally(new CFException()); |
2022 |
< |
checkCompletedWithWrappedCFException(g); |
2023 |
< |
} |
2007 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
2008 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2009 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2010 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
2011 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2012 |
> |
{ |
2013 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2014 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2015 |
> |
final CFException ex = new CFException(); |
2016 |
> |
final Noop r1 = new Noop(m); |
2017 |
> |
final Noop r2 = new Noop(m); |
2018 |
> |
final Noop r3 = new Noop(m); |
2019 |
> |
|
2020 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2021 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2022 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2023 |
> |
() -> fst.completeExceptionally(ex) : |
2024 |
> |
() -> fst.complete(v1); |
2025 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2026 |
> |
() -> snd.complete(v1) : |
2027 |
> |
() -> snd.completeExceptionally(ex); |
2028 |
> |
|
2029 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2030 |
> |
assertTrue(complete1.call()); |
2031 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2032 |
> |
checkIncomplete(h1); |
2033 |
> |
checkIncomplete(h2); |
2034 |
> |
assertTrue(complete2.call()); |
2035 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2036 |
> |
|
2037 |
> |
checkCompletedWithWrappedException(h1, ex); |
2038 |
> |
checkCompletedWithWrappedException(h2, ex); |
2039 |
> |
checkCompletedWithWrappedException(h3, ex); |
2040 |
> |
r1.assertNotInvoked(); |
2041 |
> |
r2.assertNotInvoked(); |
2042 |
> |
r3.assertNotInvoked(); |
2043 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2044 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
2045 |
> |
}} |
2046 |
|
|
2047 |
|
/** |
2048 |
< |
* runAfterBoth result completes exceptionally if action does |
2048 |
> |
* runAfterBoth result completes exceptionally if either source cancelled |
2049 |
|
*/ |
2050 |
< |
public void testRunAfterBoth3() { |
2051 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2052 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2053 |
< |
FailingNoop r = new FailingNoop(); |
2054 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
2055 |
< |
f.complete(one); |
2056 |
< |
checkIncomplete(g); |
2057 |
< |
f2.complete(two); |
2058 |
< |
checkCompletedWithWrappedCFException(g); |
2059 |
< |
} |
2050 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
2051 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2052 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2053 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2054 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
2055 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2056 |
> |
{ |
2057 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2058 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2059 |
> |
final Noop r1 = new Noop(m); |
2060 |
> |
final Noop r2 = new Noop(m); |
2061 |
> |
final Noop r3 = new Noop(m); |
2062 |
> |
|
2063 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2064 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2065 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2066 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
2067 |
> |
() -> fst.complete(v1); |
2068 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2069 |
> |
() -> snd.complete(v1) : |
2070 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
2071 |
> |
|
2072 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2073 |
> |
assertTrue(complete1.call()); |
2074 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2075 |
> |
checkIncomplete(h1); |
2076 |
> |
checkIncomplete(h2); |
2077 |
> |
assertTrue(complete2.call()); |
2078 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2079 |
> |
|
2080 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2081 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2082 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2083 |
> |
r1.assertNotInvoked(); |
2084 |
> |
r2.assertNotInvoked(); |
2085 |
> |
r3.assertNotInvoked(); |
2086 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2087 |
> |
checkCancelled(failFirst ? fst : snd); |
2088 |
> |
}} |
2089 |
|
|
2090 |
|
/** |
2091 |
< |
* runAfterBoth result completes exceptionally if either source cancelled |
2091 |
> |
* runAfterBoth result completes exceptionally if action does |
2092 |
|
*/ |
2093 |
< |
public void testRunAfterBoth4() { |
2094 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2095 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2096 |
< |
Noop r = new Noop(); |
2097 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
2098 |
< |
assertTrue(f.cancel(true)); |
2099 |
< |
f2.complete(two); |
2100 |
< |
checkCompletedWithWrappedCancellationException(g); |
2101 |
< |
f = new CompletableFuture<Integer>(); |
2102 |
< |
f2 = new CompletableFuture<Integer>(); |
2103 |
< |
r = new Noop(); |
2104 |
< |
g = f.runAfterBoth(f2, r); |
2105 |
< |
f.complete(one); |
2106 |
< |
assertTrue(f2.cancel(true)); |
2107 |
< |
checkCompletedWithWrappedCancellationException(g); |
2108 |
< |
} |
2093 |
> |
public void testRunAfterBoth_actionFailed() { |
2094 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2095 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2096 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2097 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2098 |
> |
{ |
2099 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2100 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2101 |
> |
final FailingRunnable r1 = new FailingRunnable(m); |
2102 |
> |
final FailingRunnable r2 = new FailingRunnable(m); |
2103 |
> |
final FailingRunnable r3 = new FailingRunnable(m); |
2104 |
> |
|
2105 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2106 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2107 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2108 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2109 |
> |
|
2110 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2111 |
> |
assertTrue(fst.complete(w1)); |
2112 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2113 |
> |
assertTrue(snd.complete(w2)); |
2114 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2115 |
> |
|
2116 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
2117 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
2118 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
2119 |
> |
r1.assertInvoked(); |
2120 |
> |
r2.assertInvoked(); |
2121 |
> |
r3.assertInvoked(); |
2122 |
> |
checkCompletedNormally(f, v1); |
2123 |
> |
checkCompletedNormally(g, v2); |
2124 |
> |
}} |
2125 |
|
|
2126 |
|
/** |
2127 |
|
* applyToEither result completes normally after normal completion |
2128 |
|
* of either source |
2129 |
|
*/ |
2130 |
< |
public void testApplyToEither() { |
2131 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2132 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2133 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
2134 |
< |
f.complete(one); |
2135 |
< |
checkCompletedNormally(g, two); |
2136 |
< |
f2.complete(one); |
2137 |
< |
checkCompletedNormally(g, two); |
2138 |
< |
|
2139 |
< |
f = new CompletableFuture<Integer>(); |
2140 |
< |
f.complete(one); |
2141 |
< |
f2 = new CompletableFuture<Integer>(); |
2142 |
< |
g = f.applyToEither(f2, inc); |
2143 |
< |
checkCompletedNormally(g, two); |
2144 |
< |
} |
2130 |
> |
public void testApplyToEither_normalCompletion() { |
2131 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2132 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2133 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2134 |
> |
{ |
2135 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2136 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2137 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2138 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2139 |
> |
|
2140 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2141 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2142 |
> |
checkIncomplete(h0); |
2143 |
> |
checkIncomplete(h1); |
2144 |
> |
rs[0].assertNotInvoked(); |
2145 |
> |
rs[1].assertNotInvoked(); |
2146 |
> |
f.complete(v1); |
2147 |
> |
checkCompletedNormally(h0, inc(v1)); |
2148 |
> |
checkCompletedNormally(h1, inc(v1)); |
2149 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2150 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2151 |
> |
checkCompletedNormally(h2, inc(v1)); |
2152 |
> |
checkCompletedNormally(h3, inc(v1)); |
2153 |
> |
g.complete(v2); |
2154 |
> |
|
2155 |
> |
// unspecified behavior - both source completions available |
2156 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2157 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2158 |
> |
rs[4].assertValue(h4.join()); |
2159 |
> |
rs[5].assertValue(h5.join()); |
2160 |
> |
assertTrue(Objects.equals(inc(v1), h4.join()) || |
2161 |
> |
Objects.equals(inc(v2), h4.join())); |
2162 |
> |
assertTrue(Objects.equals(inc(v1), h5.join()) || |
2163 |
> |
Objects.equals(inc(v2), h5.join())); |
2164 |
> |
|
2165 |
> |
checkCompletedNormally(f, v1); |
2166 |
> |
checkCompletedNormally(g, v2); |
2167 |
> |
checkCompletedNormally(h0, inc(v1)); |
2168 |
> |
checkCompletedNormally(h1, inc(v1)); |
2169 |
> |
checkCompletedNormally(h2, inc(v1)); |
2170 |
> |
checkCompletedNormally(h3, inc(v1)); |
2171 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); |
2172 |
> |
}} |
2173 |
|
|
2174 |
|
/** |
2175 |
|
* applyToEither result completes exceptionally after exceptional |
2176 |
|
* completion of either source |
2177 |
|
*/ |
2178 |
< |
public void testApplyToEither2() { |
2179 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2180 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2181 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
2182 |
< |
f.completeExceptionally(new CFException()); |
2183 |
< |
f2.complete(one); |
2184 |
< |
checkCompletedWithWrappedCFException(g); |
2178 |
> |
public void testApplyToEither_exceptionalCompletion() { |
2179 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2180 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2181 |
> |
{ |
2182 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2183 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2184 |
> |
final CFException ex = new CFException(); |
2185 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2186 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2187 |
> |
|
2188 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2189 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2190 |
> |
checkIncomplete(h0); |
2191 |
> |
checkIncomplete(h1); |
2192 |
> |
rs[0].assertNotInvoked(); |
2193 |
> |
rs[1].assertNotInvoked(); |
2194 |
> |
f.completeExceptionally(ex); |
2195 |
> |
checkCompletedWithWrappedException(h0, ex); |
2196 |
> |
checkCompletedWithWrappedException(h1, ex); |
2197 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2198 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2199 |
> |
checkCompletedWithWrappedException(h2, ex); |
2200 |
> |
checkCompletedWithWrappedException(h3, ex); |
2201 |
> |
g.complete(v1); |
2202 |
> |
|
2203 |
> |
// unspecified behavior - both source completions available |
2204 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2205 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2206 |
> |
try { |
2207 |
> |
assertEquals(inc(v1), h4.join()); |
2208 |
> |
rs[4].assertValue(inc(v1)); |
2209 |
> |
} catch (CompletionException ok) { |
2210 |
> |
checkCompletedWithWrappedException(h4, ex); |
2211 |
> |
rs[4].assertNotInvoked(); |
2212 |
> |
} |
2213 |
> |
try { |
2214 |
> |
assertEquals(inc(v1), h5.join()); |
2215 |
> |
rs[5].assertValue(inc(v1)); |
2216 |
> |
} catch (CompletionException ok) { |
2217 |
> |
checkCompletedWithWrappedException(h5, ex); |
2218 |
> |
rs[5].assertNotInvoked(); |
2219 |
> |
} |
2220 |
|
|
2221 |
< |
f = new CompletableFuture<Integer>(); |
2222 |
< |
f2 = new CompletableFuture<Integer>(); |
2223 |
< |
f2.completeExceptionally(new CFException()); |
2224 |
< |
g = f.applyToEither(f2, inc); |
2225 |
< |
checkCompletedWithWrappedCFException(g); |
2226 |
< |
} |
2221 |
> |
checkCompletedExceptionally(f, ex); |
2222 |
> |
checkCompletedNormally(g, v1); |
2223 |
> |
checkCompletedWithWrappedException(h0, ex); |
2224 |
> |
checkCompletedWithWrappedException(h1, ex); |
2225 |
> |
checkCompletedWithWrappedException(h2, ex); |
2226 |
> |
checkCompletedWithWrappedException(h3, ex); |
2227 |
> |
checkCompletedWithWrappedException(h4, ex); |
2228 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2229 |
> |
}} |
2230 |
> |
|
2231 |
> |
public void testApplyToEither_exceptionalCompletion2() { |
2232 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2233 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2234 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2235 |
> |
{ |
2236 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2237 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2238 |
> |
final CFException ex = new CFException(); |
2239 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2240 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2241 |
> |
|
2242 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2243 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2244 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2245 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2246 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2247 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2248 |
|
|
2249 |
< |
/** |
2250 |
< |
* applyToEither result completes exceptionally if action does |
2251 |
< |
*/ |
2252 |
< |
public void testApplyToEither3() { |
2253 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2254 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2255 |
< |
FailingFunction r = new FailingFunction(); |
2256 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, r); |
2257 |
< |
f2.complete(two); |
2258 |
< |
checkCompletedWithWrappedCFException(g); |
2259 |
< |
} |
2249 |
> |
// unspecified behavior - both source completions available |
2250 |
> |
try { |
2251 |
> |
assertEquals(inc(v1), h0.join()); |
2252 |
> |
rs[0].assertValue(inc(v1)); |
2253 |
> |
} catch (CompletionException ok) { |
2254 |
> |
checkCompletedWithWrappedException(h0, ex); |
2255 |
> |
rs[0].assertNotInvoked(); |
2256 |
> |
} |
2257 |
> |
try { |
2258 |
> |
assertEquals(inc(v1), h1.join()); |
2259 |
> |
rs[1].assertValue(inc(v1)); |
2260 |
> |
} catch (CompletionException ok) { |
2261 |
> |
checkCompletedWithWrappedException(h1, ex); |
2262 |
> |
rs[1].assertNotInvoked(); |
2263 |
> |
} |
2264 |
> |
try { |
2265 |
> |
assertEquals(inc(v1), h2.join()); |
2266 |
> |
rs[2].assertValue(inc(v1)); |
2267 |
> |
} catch (CompletionException ok) { |
2268 |
> |
checkCompletedWithWrappedException(h2, ex); |
2269 |
> |
rs[2].assertNotInvoked(); |
2270 |
> |
} |
2271 |
> |
try { |
2272 |
> |
assertEquals(inc(v1), h3.join()); |
2273 |
> |
rs[3].assertValue(inc(v1)); |
2274 |
> |
} catch (CompletionException ok) { |
2275 |
> |
checkCompletedWithWrappedException(h3, ex); |
2276 |
> |
rs[3].assertNotInvoked(); |
2277 |
> |
} |
2278 |
|
|
2279 |
< |
/** |
2280 |
< |
* applyToEither result completes exceptionally if either source cancelled |
2281 |
< |
*/ |
967 |
< |
public void testApplyToEither4() { |
968 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
969 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
970 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
971 |
< |
assertTrue(f.cancel(true)); |
972 |
< |
checkCompletedWithWrappedCancellationException(g); |
973 |
< |
f = new CompletableFuture<Integer>(); |
974 |
< |
f2 = new CompletableFuture<Integer>(); |
975 |
< |
assertTrue(f2.cancel(true)); |
976 |
< |
checkCompletedWithWrappedCancellationException(g); |
977 |
< |
} |
2279 |
> |
checkCompletedNormally(f, v1); |
2280 |
> |
checkCompletedExceptionally(g, ex); |
2281 |
> |
}} |
2282 |
|
|
2283 |
|
/** |
2284 |
< |
* acceptEither result completes normally after normal completion |
981 |
< |
* of either source |
2284 |
> |
* applyToEither result completes exceptionally if either source cancelled |
2285 |
|
*/ |
2286 |
< |
public void testAcceptEither() { |
2287 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2288 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2289 |
< |
IncAction r = new IncAction(); |
2290 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
2291 |
< |
f.complete(one); |
2292 |
< |
checkCompletedNormally(g, null); |
2293 |
< |
f2.complete(one); |
2294 |
< |
checkCompletedNormally(g, null); |
2295 |
< |
assertEquals(r.value, 2); |
2296 |
< |
|
2297 |
< |
r = new IncAction(); |
2298 |
< |
f = new CompletableFuture<Integer>(); |
2299 |
< |
f.complete(one); |
2300 |
< |
f2 = new CompletableFuture<Integer>(); |
2301 |
< |
g = f.acceptEither(f2, r); |
2302 |
< |
checkCompletedNormally(g, null); |
2303 |
< |
assertEquals(r.value, 2); |
2304 |
< |
} |
2286 |
> |
public void testApplyToEither_sourceCancelled() { |
2287 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2288 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2289 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2290 |
> |
{ |
2291 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2292 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2293 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2294 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2295 |
> |
|
2296 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2297 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2298 |
> |
checkIncomplete(h0); |
2299 |
> |
checkIncomplete(h1); |
2300 |
> |
rs[0].assertNotInvoked(); |
2301 |
> |
rs[1].assertNotInvoked(); |
2302 |
> |
f.cancel(mayInterruptIfRunning); |
2303 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2304 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2305 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2306 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2307 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2308 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2309 |
> |
g.complete(v1); |
2310 |
> |
|
2311 |
> |
// unspecified behavior - both source completions available |
2312 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2313 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2314 |
> |
try { |
2315 |
> |
assertEquals(inc(v1), h4.join()); |
2316 |
> |
rs[4].assertValue(inc(v1)); |
2317 |
> |
} catch (CompletionException ok) { |
2318 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2319 |
> |
rs[4].assertNotInvoked(); |
2320 |
> |
} |
2321 |
> |
try { |
2322 |
> |
assertEquals(inc(v1), h5.join()); |
2323 |
> |
rs[5].assertValue(inc(v1)); |
2324 |
> |
} catch (CompletionException ok) { |
2325 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2326 |
> |
rs[5].assertNotInvoked(); |
2327 |
> |
} |
2328 |
|
|
2329 |
< |
/** |
2330 |
< |
* acceptEither result completes exceptionally after exceptional |
2331 |
< |
* completion of either source |
2332 |
< |
*/ |
2333 |
< |
public void testAcceptEither2() { |
2334 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2335 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2336 |
< |
IncAction r = new IncAction(); |
2337 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
2338 |
< |
f.completeExceptionally(new CFException()); |
2339 |
< |
f2.complete(one); |
2340 |
< |
checkCompletedWithWrappedCFException(g); |
2329 |
> |
checkCancelled(f); |
2330 |
> |
checkCompletedNormally(g, v1); |
2331 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2332 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2333 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2334 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2335 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2336 |
> |
}} |
2337 |
> |
|
2338 |
> |
public void testApplyToEither_sourceCancelled2() { |
2339 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2340 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2341 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2342 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2343 |
> |
{ |
2344 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2345 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2346 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2347 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2348 |
> |
|
2349 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2350 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2351 |
> |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2352 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2353 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2354 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2355 |
|
|
2356 |
< |
r = new IncAction(); |
2357 |
< |
f = new CompletableFuture<Integer>(); |
2358 |
< |
f2 = new CompletableFuture<Integer>(); |
2359 |
< |
f2.completeExceptionally(new CFException()); |
2360 |
< |
g = f.acceptEither(f2, r); |
2361 |
< |
checkCompletedWithWrappedCFException(g); |
2362 |
< |
} |
2356 |
> |
// unspecified behavior - both source completions available |
2357 |
> |
try { |
2358 |
> |
assertEquals(inc(v1), h0.join()); |
2359 |
> |
rs[0].assertValue(inc(v1)); |
2360 |
> |
} catch (CompletionException ok) { |
2361 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2362 |
> |
rs[0].assertNotInvoked(); |
2363 |
> |
} |
2364 |
> |
try { |
2365 |
> |
assertEquals(inc(v1), h1.join()); |
2366 |
> |
rs[1].assertValue(inc(v1)); |
2367 |
> |
} catch (CompletionException ok) { |
2368 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2369 |
> |
rs[1].assertNotInvoked(); |
2370 |
> |
} |
2371 |
> |
try { |
2372 |
> |
assertEquals(inc(v1), h2.join()); |
2373 |
> |
rs[2].assertValue(inc(v1)); |
2374 |
> |
} catch (CompletionException ok) { |
2375 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2376 |
> |
rs[2].assertNotInvoked(); |
2377 |
> |
} |
2378 |
> |
try { |
2379 |
> |
assertEquals(inc(v1), h3.join()); |
2380 |
> |
rs[3].assertValue(inc(v1)); |
2381 |
> |
} catch (CompletionException ok) { |
2382 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2383 |
> |
rs[3].assertNotInvoked(); |
2384 |
> |
} |
2385 |
|
|
2386 |
< |
/** |
2387 |
< |
* acceptEither result completes exceptionally if action does |
2388 |
< |
*/ |
1027 |
< |
public void testAcceptEither3() { |
1028 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1029 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1030 |
< |
FailingConsumer r = new FailingConsumer(); |
1031 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
1032 |
< |
f2.complete(two); |
1033 |
< |
checkCompletedWithWrappedCFException(g); |
1034 |
< |
} |
2386 |
> |
checkCompletedNormally(f, v1); |
2387 |
> |
checkCancelled(g); |
2388 |
> |
}} |
2389 |
|
|
2390 |
|
/** |
2391 |
< |
* acceptEither result completes exceptionally if either source cancelled |
2391 |
> |
* applyToEither result completes exceptionally if action does |
2392 |
|
*/ |
2393 |
< |
public void testAcceptEither4() { |
2394 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2395 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2396 |
< |
IncAction r = new IncAction(); |
2397 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
2398 |
< |
assertTrue(f.cancel(true)); |
2399 |
< |
checkCompletedWithWrappedCancellationException(g); |
2400 |
< |
f = new CompletableFuture<Integer>(); |
2401 |
< |
f2 = new CompletableFuture<Integer>(); |
2402 |
< |
assertTrue(f2.cancel(true)); |
2403 |
< |
checkCompletedWithWrappedCancellationException(g); |
2404 |
< |
} |
2405 |
< |
|
2393 |
> |
public void testApplyToEither_actionFailed() { |
2394 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2395 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2396 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2397 |
> |
{ |
2398 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2399 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2400 |
> |
final FailingFunction[] rs = new FailingFunction[6]; |
2401 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
2402 |
> |
|
2403 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2404 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2405 |
> |
f.complete(v1); |
2406 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2407 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2408 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2409 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2410 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2411 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2412 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2413 |
> |
|
2414 |
> |
g.complete(v2); |
2415 |
> |
|
2416 |
> |
// unspecified behavior - both source completions available |
2417 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2418 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2419 |
> |
|
2420 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2421 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2422 |
> |
Objects.equals(v2, rs[4].value)); |
2423 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2424 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2425 |
> |
Objects.equals(v2, rs[5].value)); |
2426 |
> |
|
2427 |
> |
checkCompletedNormally(f, v1); |
2428 |
> |
checkCompletedNormally(g, v2); |
2429 |
> |
}} |
2430 |
|
|
2431 |
|
/** |
2432 |
< |
* runAfterEither result completes normally after normal completion |
2432 |
> |
* acceptEither result completes normally after normal completion |
2433 |
|
* of either source |
2434 |
|
*/ |
2435 |
< |
public void testRunAfterEither() { |
2436 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2437 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2438 |
< |
Noop r = new Noop(); |
2439 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
2440 |
< |
f.complete(one); |
2441 |
< |
checkCompletedNormally(g, null); |
2442 |
< |
f2.complete(one); |
2443 |
< |
checkCompletedNormally(g, null); |
2444 |
< |
assertTrue(r.ran); |
2445 |
< |
|
2446 |
< |
r = new Noop(); |
2447 |
< |
f = new CompletableFuture<Integer>(); |
2448 |
< |
f.complete(one); |
2449 |
< |
f2 = new CompletableFuture<Integer>(); |
2450 |
< |
g = f.runAfterEither(f2, r); |
2451 |
< |
checkCompletedNormally(g, null); |
2452 |
< |
assertTrue(r.ran); |
2453 |
< |
} |
2435 |
> |
public void testAcceptEither_normalCompletion() { |
2436 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2437 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2438 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2439 |
> |
{ |
2440 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2441 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2442 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2443 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2444 |
> |
|
2445 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2446 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2447 |
> |
checkIncomplete(h0); |
2448 |
> |
checkIncomplete(h1); |
2449 |
> |
rs[0].assertNotInvoked(); |
2450 |
> |
rs[1].assertNotInvoked(); |
2451 |
> |
f.complete(v1); |
2452 |
> |
checkCompletedNormally(h0, null); |
2453 |
> |
checkCompletedNormally(h1, null); |
2454 |
> |
rs[0].assertValue(v1); |
2455 |
> |
rs[1].assertValue(v1); |
2456 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2457 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2458 |
> |
checkCompletedNormally(h2, null); |
2459 |
> |
checkCompletedNormally(h3, null); |
2460 |
> |
rs[2].assertValue(v1); |
2461 |
> |
rs[3].assertValue(v1); |
2462 |
> |
g.complete(v2); |
2463 |
> |
|
2464 |
> |
// unspecified behavior - both source completions available |
2465 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2466 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2467 |
> |
checkCompletedNormally(h4, null); |
2468 |
> |
checkCompletedNormally(h5, null); |
2469 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2470 |
> |
Objects.equals(v2, rs[4].value)); |
2471 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2472 |
> |
Objects.equals(v2, rs[5].value)); |
2473 |
> |
|
2474 |
> |
checkCompletedNormally(f, v1); |
2475 |
> |
checkCompletedNormally(g, v2); |
2476 |
> |
checkCompletedNormally(h0, null); |
2477 |
> |
checkCompletedNormally(h1, null); |
2478 |
> |
checkCompletedNormally(h2, null); |
2479 |
> |
checkCompletedNormally(h3, null); |
2480 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2481 |
> |
}} |
2482 |
|
|
2483 |
|
/** |
2484 |
< |
* runAfterEither result completes exceptionally after exceptional |
2484 |
> |
* acceptEither result completes exceptionally after exceptional |
2485 |
|
* completion of either source |
2486 |
|
*/ |
2487 |
< |
public void testRunAfterEither2() { |
2488 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2489 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2490 |
< |
Noop r = new Noop(); |
2491 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
2492 |
< |
f.completeExceptionally(new CFException()); |
2493 |
< |
f2.complete(one); |
2494 |
< |
checkCompletedWithWrappedCFException(g); |
2487 |
> |
public void testAcceptEither_exceptionalCompletion() { |
2488 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2489 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2490 |
> |
{ |
2491 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2492 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2493 |
> |
final CFException ex = new CFException(); |
2494 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2495 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2496 |
> |
|
2497 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2498 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2499 |
> |
checkIncomplete(h0); |
2500 |
> |
checkIncomplete(h1); |
2501 |
> |
rs[0].assertNotInvoked(); |
2502 |
> |
rs[1].assertNotInvoked(); |
2503 |
> |
f.completeExceptionally(ex); |
2504 |
> |
checkCompletedWithWrappedException(h0, ex); |
2505 |
> |
checkCompletedWithWrappedException(h1, ex); |
2506 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2507 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2508 |
> |
checkCompletedWithWrappedException(h2, ex); |
2509 |
> |
checkCompletedWithWrappedException(h3, ex); |
2510 |
> |
|
2511 |
> |
g.complete(v1); |
2512 |
> |
|
2513 |
> |
// unspecified behavior - both source completions available |
2514 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2515 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2516 |
> |
try { |
2517 |
> |
assertNull(h4.join()); |
2518 |
> |
rs[4].assertValue(v1); |
2519 |
> |
} catch (CompletionException ok) { |
2520 |
> |
checkCompletedWithWrappedException(h4, ex); |
2521 |
> |
rs[4].assertNotInvoked(); |
2522 |
> |
} |
2523 |
> |
try { |
2524 |
> |
assertNull(h5.join()); |
2525 |
> |
rs[5].assertValue(v1); |
2526 |
> |
} catch (CompletionException ok) { |
2527 |
> |
checkCompletedWithWrappedException(h5, ex); |
2528 |
> |
rs[5].assertNotInvoked(); |
2529 |
> |
} |
2530 |
|
|
2531 |
< |
r = new Noop(); |
2532 |
< |
f = new CompletableFuture<Integer>(); |
2533 |
< |
f2 = new CompletableFuture<Integer>(); |
2534 |
< |
f2.completeExceptionally(new CFException()); |
2535 |
< |
g = f.runAfterEither(f2, r); |
2536 |
< |
checkCompletedWithWrappedCFException(g); |
2537 |
< |
} |
2531 |
> |
checkCompletedExceptionally(f, ex); |
2532 |
> |
checkCompletedNormally(g, v1); |
2533 |
> |
checkCompletedWithWrappedException(h0, ex); |
2534 |
> |
checkCompletedWithWrappedException(h1, ex); |
2535 |
> |
checkCompletedWithWrappedException(h2, ex); |
2536 |
> |
checkCompletedWithWrappedException(h3, ex); |
2537 |
> |
checkCompletedWithWrappedException(h4, ex); |
2538 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2539 |
> |
}} |
2540 |
> |
|
2541 |
> |
public void testAcceptEither_exceptionalCompletion2() { |
2542 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2543 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2544 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2545 |
> |
{ |
2546 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2547 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2548 |
> |
final CFException ex = new CFException(); |
2549 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2550 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2551 |
> |
|
2552 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2553 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2554 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2555 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2556 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2557 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2558 |
|
|
2559 |
< |
/** |
2560 |
< |
* runAfterEither result completes exceptionally if action does |
2561 |
< |
*/ |
2562 |
< |
public void testRunAfterEither3() { |
2563 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2564 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2565 |
< |
FailingNoop r = new FailingNoop(); |
2566 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
2567 |
< |
f2.complete(two); |
2568 |
< |
checkCompletedWithWrappedCFException(g); |
2569 |
< |
} |
2570 |
< |
|
2571 |
< |
/** |
2572 |
< |
* runAfterEither result completes exceptionally if either source cancelled |
2573 |
< |
*/ |
2574 |
< |
public void testRunAfterEither4() { |
2575 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2576 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2577 |
< |
Noop r = new Noop(); |
2578 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
2579 |
< |
assertTrue(f.cancel(true)); |
2580 |
< |
checkCompletedWithWrappedCancellationException(g); |
2581 |
< |
f = new CompletableFuture<Integer>(); |
2582 |
< |
f2 = new CompletableFuture<Integer>(); |
2583 |
< |
assertTrue(f2.cancel(true)); |
2584 |
< |
checkCompletedWithWrappedCancellationException(g); |
2585 |
< |
} |
2559 |
> |
// unspecified behavior - both source completions available |
2560 |
> |
try { |
2561 |
> |
assertNull(h0.join()); |
2562 |
> |
rs[0].assertValue(v1); |
2563 |
> |
} catch (CompletionException ok) { |
2564 |
> |
checkCompletedWithWrappedException(h0, ex); |
2565 |
> |
rs[0].assertNotInvoked(); |
2566 |
> |
} |
2567 |
> |
try { |
2568 |
> |
assertNull(h1.join()); |
2569 |
> |
rs[1].assertValue(v1); |
2570 |
> |
} catch (CompletionException ok) { |
2571 |
> |
checkCompletedWithWrappedException(h1, ex); |
2572 |
> |
rs[1].assertNotInvoked(); |
2573 |
> |
} |
2574 |
> |
try { |
2575 |
> |
assertNull(h2.join()); |
2576 |
> |
rs[2].assertValue(v1); |
2577 |
> |
} catch (CompletionException ok) { |
2578 |
> |
checkCompletedWithWrappedException(h2, ex); |
2579 |
> |
rs[2].assertNotInvoked(); |
2580 |
> |
} |
2581 |
> |
try { |
2582 |
> |
assertNull(h3.join()); |
2583 |
> |
rs[3].assertValue(v1); |
2584 |
> |
} catch (CompletionException ok) { |
2585 |
> |
checkCompletedWithWrappedException(h3, ex); |
2586 |
> |
rs[3].assertNotInvoked(); |
2587 |
> |
} |
2588 |
|
|
2589 |
< |
/** |
2590 |
< |
* thenCompose result completes normally after normal completion of source |
2591 |
< |
*/ |
1129 |
< |
public void testThenCompose() { |
1130 |
< |
CompletableFuture<Integer> f, g; |
1131 |
< |
CompletableFutureInc r; |
1132 |
< |
|
1133 |
< |
f = new CompletableFuture<Integer>(); |
1134 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
1135 |
< |
f.complete(one); |
1136 |
< |
checkCompletedNormally(g, two); |
1137 |
< |
assertTrue(r.ran); |
1138 |
< |
|
1139 |
< |
f = new CompletableFuture<Integer>(); |
1140 |
< |
f.complete(one); |
1141 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
1142 |
< |
checkCompletedNormally(g, two); |
1143 |
< |
assertTrue(r.ran); |
1144 |
< |
} |
2589 |
> |
checkCompletedNormally(f, v1); |
2590 |
> |
checkCompletedExceptionally(g, ex); |
2591 |
> |
}} |
2592 |
|
|
2593 |
|
/** |
2594 |
< |
* thenCompose result completes exceptionally after exceptional |
1148 |
< |
* completion of source |
2594 |
> |
* acceptEither result completes exceptionally if either source cancelled |
2595 |
|
*/ |
2596 |
< |
public void testThenCompose2() { |
2597 |
< |
CompletableFuture<Integer> f, g; |
2598 |
< |
CompletableFutureInc r; |
2599 |
< |
|
2600 |
< |
f = new CompletableFuture<Integer>(); |
2601 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2602 |
< |
f.completeExceptionally(new CFException()); |
2603 |
< |
checkCompletedWithWrappedCFException(g); |
2596 |
> |
public void testAcceptEither_sourceCancelled() { |
2597 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2598 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2599 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2600 |
> |
{ |
2601 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2602 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2603 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2604 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2605 |
> |
|
2606 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2607 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2608 |
> |
checkIncomplete(h0); |
2609 |
> |
checkIncomplete(h1); |
2610 |
> |
rs[0].assertNotInvoked(); |
2611 |
> |
rs[1].assertNotInvoked(); |
2612 |
> |
f.cancel(mayInterruptIfRunning); |
2613 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2614 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2615 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2616 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2617 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2618 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2619 |
> |
|
2620 |
> |
g.complete(v1); |
2621 |
> |
|
2622 |
> |
// unspecified behavior - both source completions available |
2623 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2624 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2625 |
> |
try { |
2626 |
> |
assertNull(h4.join()); |
2627 |
> |
rs[4].assertValue(v1); |
2628 |
> |
} catch (CompletionException ok) { |
2629 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2630 |
> |
rs[4].assertNotInvoked(); |
2631 |
> |
} |
2632 |
> |
try { |
2633 |
> |
assertNull(h5.join()); |
2634 |
> |
rs[5].assertValue(v1); |
2635 |
> |
} catch (CompletionException ok) { |
2636 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2637 |
> |
rs[5].assertNotInvoked(); |
2638 |
> |
} |
2639 |
|
|
2640 |
< |
f = new CompletableFuture<Integer>(); |
2641 |
< |
f.completeExceptionally(new CFException()); |
2642 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2643 |
< |
checkCompletedWithWrappedCFException(g); |
2644 |
< |
} |
2640 |
> |
checkCancelled(f); |
2641 |
> |
checkCompletedNormally(g, v1); |
2642 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2643 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2644 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2645 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2646 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2647 |
> |
}} |
2648 |
|
|
2649 |
|
/** |
2650 |
< |
* thenCompose result completes exceptionally if action does |
2650 |
> |
* acceptEither result completes exceptionally if action does |
2651 |
|
*/ |
2652 |
< |
public void testThenCompose3() { |
2653 |
< |
CompletableFuture<Integer> f, g; |
2654 |
< |
FailingCompletableFutureFunction r; |
2655 |
< |
|
2656 |
< |
f = new CompletableFuture<Integer>(); |
2657 |
< |
g = f.thenCompose(r = new FailingCompletableFutureFunction()); |
2658 |
< |
f.complete(one); |
2659 |
< |
checkCompletedWithWrappedCFException(g); |
2660 |
< |
|
2661 |
< |
f = new CompletableFuture<Integer>(); |
2662 |
< |
f.complete(one); |
2663 |
< |
g = f.thenCompose(r = new FailingCompletableFutureFunction()); |
2664 |
< |
checkCompletedWithWrappedCFException(g); |
2665 |
< |
} |
2652 |
> |
public void testAcceptEither_actionFailed() { |
2653 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2654 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2655 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2656 |
> |
{ |
2657 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2658 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2659 |
> |
final FailingConsumer[] rs = new FailingConsumer[6]; |
2660 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
2661 |
> |
|
2662 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2663 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2664 |
> |
f.complete(v1); |
2665 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2666 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2667 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2668 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2669 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2670 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2671 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2672 |
> |
|
2673 |
> |
g.complete(v2); |
2674 |
> |
|
2675 |
> |
// unspecified behavior - both source completions available |
2676 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2677 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2678 |
> |
|
2679 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2680 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2681 |
> |
Objects.equals(v2, rs[4].value)); |
2682 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2683 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2684 |
> |
Objects.equals(v2, rs[5].value)); |
2685 |
> |
|
2686 |
> |
checkCompletedNormally(f, v1); |
2687 |
> |
checkCompletedNormally(g, v2); |
2688 |
> |
}} |
2689 |
|
|
2690 |
|
/** |
2691 |
< |
* thenCompose result completes exceptionally if source cancelled |
2691 |
> |
* runAfterEither result completes normally after normal completion |
2692 |
> |
* of either source |
2693 |
|
*/ |
2694 |
< |
public void testThenCompose4() { |
2695 |
< |
CompletableFuture<Integer> f, g; |
2696 |
< |
CompletableFutureInc r; |
2697 |
< |
|
2698 |
< |
f = new CompletableFuture<Integer>(); |
2699 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2700 |
< |
assertTrue(f.cancel(true)); |
2701 |
< |
checkCompletedWithWrappedCancellationException(g); |
2702 |
< |
|
2703 |
< |
f = new CompletableFuture<Integer>(); |
2704 |
< |
assertTrue(f.cancel(true)); |
2705 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2706 |
< |
checkCompletedWithWrappedCancellationException(g); |
2707 |
< |
} |
2708 |
< |
|
2709 |
< |
|
2710 |
< |
// asyncs |
2694 |
> |
public void testRunAfterEither_normalCompletion() { |
2695 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2696 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2697 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2698 |
> |
for (boolean pushNop : new boolean[] { true, false }) |
2699 |
> |
{ |
2700 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2701 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2702 |
> |
final Noop[] rs = new Noop[6]; |
2703 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2704 |
> |
|
2705 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2706 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2707 |
> |
checkIncomplete(h0); |
2708 |
> |
checkIncomplete(h1); |
2709 |
> |
rs[0].assertNotInvoked(); |
2710 |
> |
rs[1].assertNotInvoked(); |
2711 |
> |
if (pushNop) { // ad hoc test of intra-completion interference |
2712 |
> |
m.thenRun(f, () -> {}); |
2713 |
> |
m.thenRun(g, () -> {}); |
2714 |
> |
} |
2715 |
> |
f.complete(v1); |
2716 |
> |
checkCompletedNormally(h0, null); |
2717 |
> |
checkCompletedNormally(h1, null); |
2718 |
> |
rs[0].assertInvoked(); |
2719 |
> |
rs[1].assertInvoked(); |
2720 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2721 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2722 |
> |
checkCompletedNormally(h2, null); |
2723 |
> |
checkCompletedNormally(h3, null); |
2724 |
> |
rs[2].assertInvoked(); |
2725 |
> |
rs[3].assertInvoked(); |
2726 |
> |
|
2727 |
> |
g.complete(v2); |
2728 |
> |
|
2729 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2730 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2731 |
> |
|
2732 |
> |
checkCompletedNormally(f, v1); |
2733 |
> |
checkCompletedNormally(g, v2); |
2734 |
> |
checkCompletedNormally(h0, null); |
2735 |
> |
checkCompletedNormally(h1, null); |
2736 |
> |
checkCompletedNormally(h2, null); |
2737 |
> |
checkCompletedNormally(h3, null); |
2738 |
> |
checkCompletedNormally(h4, null); |
2739 |
> |
checkCompletedNormally(h5, null); |
2740 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2741 |
> |
}} |
2742 |
|
|
2743 |
|
/** |
2744 |
< |
* thenRunAsync result completes normally after normal completion of source |
2744 |
> |
* runAfterEither result completes exceptionally after exceptional |
2745 |
> |
* completion of either source |
2746 |
|
*/ |
2747 |
< |
public void testThenRunAsync() { |
2748 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2749 |
< |
Noop r = new Noop(); |
2750 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
2751 |
< |
f.complete(null); |
2752 |
< |
checkCompletedNormally(g, null); |
2747 |
> |
public void testRunAfterEither_exceptionalCompletion() { |
2748 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2749 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2750 |
> |
{ |
2751 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2752 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2753 |
> |
final CFException ex = new CFException(); |
2754 |
> |
final Noop[] rs = new Noop[6]; |
2755 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2756 |
> |
|
2757 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2758 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2759 |
> |
checkIncomplete(h0); |
2760 |
> |
checkIncomplete(h1); |
2761 |
> |
rs[0].assertNotInvoked(); |
2762 |
> |
rs[1].assertNotInvoked(); |
2763 |
> |
assertTrue(f.completeExceptionally(ex)); |
2764 |
> |
checkCompletedWithWrappedException(h0, ex); |
2765 |
> |
checkCompletedWithWrappedException(h1, ex); |
2766 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2767 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2768 |
> |
checkCompletedWithWrappedException(h2, ex); |
2769 |
> |
checkCompletedWithWrappedException(h3, ex); |
2770 |
> |
|
2771 |
> |
assertTrue(g.complete(v1)); |
2772 |
> |
|
2773 |
> |
// unspecified behavior - both source completions available |
2774 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2775 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2776 |
> |
try { |
2777 |
> |
assertNull(h4.join()); |
2778 |
> |
rs[4].assertInvoked(); |
2779 |
> |
} catch (CompletionException ok) { |
2780 |
> |
checkCompletedWithWrappedException(h4, ex); |
2781 |
> |
rs[4].assertNotInvoked(); |
2782 |
> |
} |
2783 |
> |
try { |
2784 |
> |
assertNull(h5.join()); |
2785 |
> |
rs[5].assertInvoked(); |
2786 |
> |
} catch (CompletionException ok) { |
2787 |
> |
checkCompletedWithWrappedException(h5, ex); |
2788 |
> |
rs[5].assertNotInvoked(); |
2789 |
> |
} |
2790 |
|
|
2791 |
< |
// reordered version |
2792 |
< |
f = new CompletableFuture<Integer>(); |
2793 |
< |
f.complete(null); |
2794 |
< |
r = new Noop(); |
2795 |
< |
g = f.thenRunAsync(r); |
2796 |
< |
checkCompletedNormally(g, null); |
2797 |
< |
} |
2791 |
> |
checkCompletedExceptionally(f, ex); |
2792 |
> |
checkCompletedNormally(g, v1); |
2793 |
> |
checkCompletedWithWrappedException(h0, ex); |
2794 |
> |
checkCompletedWithWrappedException(h1, ex); |
2795 |
> |
checkCompletedWithWrappedException(h2, ex); |
2796 |
> |
checkCompletedWithWrappedException(h3, ex); |
2797 |
> |
checkCompletedWithWrappedException(h4, ex); |
2798 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2799 |
> |
}} |
2800 |
> |
|
2801 |
> |
public void testRunAfterEither_exceptionalCompletion2() { |
2802 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2803 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2804 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2805 |
> |
{ |
2806 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2807 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2808 |
> |
final CFException ex = new CFException(); |
2809 |
> |
final Noop[] rs = new Noop[6]; |
2810 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2811 |
> |
|
2812 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2813 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2814 |
> |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2815 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2816 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2817 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2818 |
|
|
2819 |
< |
/** |
1223 |
< |
* thenRunAsync result completes exceptionally after exceptional |
1224 |
< |
* completion of source |
1225 |
< |
*/ |
1226 |
< |
public void testThenRunAsync2() { |
1227 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1228 |
< |
Noop r = new Noop(); |
1229 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
1230 |
< |
f.completeExceptionally(new CFException()); |
2819 |
> |
// unspecified behavior - both source completions available |
2820 |
|
try { |
2821 |
< |
g.join(); |
2822 |
< |
shouldThrow(); |
2823 |
< |
} catch (Exception ok) { |
2821 |
> |
assertNull(h0.join()); |
2822 |
> |
rs[0].assertInvoked(); |
2823 |
> |
} catch (CompletionException ok) { |
2824 |
> |
checkCompletedWithWrappedException(h0, ex); |
2825 |
> |
rs[0].assertNotInvoked(); |
2826 |
> |
} |
2827 |
> |
try { |
2828 |
> |
assertNull(h1.join()); |
2829 |
> |
rs[1].assertInvoked(); |
2830 |
> |
} catch (CompletionException ok) { |
2831 |
> |
checkCompletedWithWrappedException(h1, ex); |
2832 |
> |
rs[1].assertNotInvoked(); |
2833 |
> |
} |
2834 |
> |
try { |
2835 |
> |
assertNull(h2.join()); |
2836 |
> |
rs[2].assertInvoked(); |
2837 |
> |
} catch (CompletionException ok) { |
2838 |
> |
checkCompletedWithWrappedException(h2, ex); |
2839 |
> |
rs[2].assertNotInvoked(); |
2840 |
> |
} |
2841 |
> |
try { |
2842 |
> |
assertNull(h3.join()); |
2843 |
> |
rs[3].assertInvoked(); |
2844 |
> |
} catch (CompletionException ok) { |
2845 |
> |
checkCompletedWithWrappedException(h3, ex); |
2846 |
> |
rs[3].assertNotInvoked(); |
2847 |
|
} |
1236 |
– |
checkCompletedWithWrappedCFException(g); |
1237 |
– |
} |
1238 |
– |
|
1239 |
– |
/** |
1240 |
– |
* thenRunAsync result completes exceptionally if action does |
1241 |
– |
*/ |
1242 |
– |
public void testThenRunAsync3() { |
1243 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1244 |
– |
FailingNoop r = new FailingNoop(); |
1245 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r); |
1246 |
– |
f.complete(null); |
1247 |
– |
checkCompletedWithWrappedCFException(g); |
1248 |
– |
} |
2848 |
|
|
2849 |
< |
/** |
2850 |
< |
* thenRunAsync result completes exceptionally if source cancelled |
2851 |
< |
*/ |
1253 |
< |
public void testThenRunAsync4() { |
1254 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1255 |
< |
Noop r = new Noop(); |
1256 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
1257 |
< |
assertTrue(f.cancel(true)); |
1258 |
< |
checkCompletedWithWrappedCancellationException(g); |
1259 |
< |
} |
2849 |
> |
checkCompletedNormally(f, v1); |
2850 |
> |
checkCompletedExceptionally(g, ex); |
2851 |
> |
}} |
2852 |
|
|
2853 |
|
/** |
2854 |
< |
* thenApplyAsync result completes normally after normal completion of source |
2854 |
> |
* runAfterEither result completes exceptionally if either source cancelled |
2855 |
|
*/ |
2856 |
< |
public void testThenApplyAsync() { |
2857 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2858 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
2859 |
< |
f.complete(one); |
2860 |
< |
checkCompletedNormally(g, two); |
2861 |
< |
} |
2856 |
> |
public void testRunAfterEither_sourceCancelled() { |
2857 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2858 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2859 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2860 |
> |
{ |
2861 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2862 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2863 |
> |
final Noop[] rs = new Noop[6]; |
2864 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2865 |
> |
|
2866 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2867 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2868 |
> |
checkIncomplete(h0); |
2869 |
> |
checkIncomplete(h1); |
2870 |
> |
rs[0].assertNotInvoked(); |
2871 |
> |
rs[1].assertNotInvoked(); |
2872 |
> |
f.cancel(mayInterruptIfRunning); |
2873 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2874 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2875 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2876 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2877 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2878 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2879 |
> |
|
2880 |
> |
assertTrue(g.complete(v1)); |
2881 |
> |
|
2882 |
> |
// unspecified behavior - both source completions available |
2883 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2884 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2885 |
> |
try { |
2886 |
> |
assertNull(h4.join()); |
2887 |
> |
rs[4].assertInvoked(); |
2888 |
> |
} catch (CompletionException ok) { |
2889 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2890 |
> |
rs[4].assertNotInvoked(); |
2891 |
> |
} |
2892 |
> |
try { |
2893 |
> |
assertNull(h5.join()); |
2894 |
> |
rs[5].assertInvoked(); |
2895 |
> |
} catch (CompletionException ok) { |
2896 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2897 |
> |
rs[5].assertNotInvoked(); |
2898 |
> |
} |
2899 |
|
|
2900 |
< |
/** |
2901 |
< |
* thenApplyAsync result completes exceptionally after exceptional |
2902 |
< |
* completion of source |
2903 |
< |
*/ |
2904 |
< |
public void testThenApplyAsync2() { |
2905 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2906 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
2907 |
< |
f.completeExceptionally(new CFException()); |
1279 |
< |
checkCompletedWithWrappedCFException(g); |
1280 |
< |
} |
2900 |
> |
checkCancelled(f); |
2901 |
> |
checkCompletedNormally(g, v1); |
2902 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2903 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2904 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2905 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2906 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2907 |
> |
}} |
2908 |
|
|
2909 |
|
/** |
2910 |
< |
* thenApplyAsync result completes exceptionally if action does |
2910 |
> |
* runAfterEither result completes exceptionally if action does |
2911 |
|
*/ |
2912 |
< |
public void testThenApplyAsync3() { |
2913 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2914 |
< |
FailingFunction r = new FailingFunction(); |
2915 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(r); |
2916 |
< |
f.complete(null); |
2917 |
< |
checkCompletedWithWrappedCFException(g); |
2918 |
< |
} |
2912 |
> |
public void testRunAfterEither_actionFailed() { |
2913 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2914 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2915 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2916 |
> |
{ |
2917 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2918 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2919 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
2920 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
2921 |
> |
|
2922 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2923 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2924 |
> |
assertTrue(f.complete(v1)); |
2925 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2926 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2927 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2928 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2929 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2930 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2931 |
> |
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2932 |
> |
assertTrue(g.complete(v2)); |
2933 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2934 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2935 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2936 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2937 |
> |
|
2938 |
> |
checkCompletedNormally(f, v1); |
2939 |
> |
checkCompletedNormally(g, v2); |
2940 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2941 |
> |
}} |
2942 |
|
|
2943 |
|
/** |
2944 |
< |
* thenApplyAsync result completes exceptionally if source cancelled |
2944 |
> |
* thenCompose result completes normally after normal completion of source |
2945 |
|
*/ |
2946 |
< |
public void testThenApplyAsync4() { |
2947 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2948 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
2949 |
< |
assertTrue(f.cancel(true)); |
2950 |
< |
checkCompletedWithWrappedCancellationException(g); |
2951 |
< |
} |
2946 |
> |
public void testThenCompose_normalCompletion() { |
2947 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2948 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2949 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2950 |
> |
{ |
2951 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2952 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2953 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2954 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2955 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2956 |
> |
|
2957 |
> |
checkCompletedNormally(g, inc(v1)); |
2958 |
> |
checkCompletedNormally(f, v1); |
2959 |
> |
r.assertValue(v1); |
2960 |
> |
}} |
2961 |
|
|
2962 |
|
/** |
2963 |
< |
* thenAcceptAsync result completes normally after normal |
2963 |
> |
* thenCompose result completes exceptionally after exceptional |
2964 |
|
* completion of source |
2965 |
|
*/ |
2966 |
< |
public void testThenAcceptAsync() { |
2967 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2968 |
< |
IncAction r = new IncAction(); |
2969 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
2970 |
< |
f.complete(one); |
2971 |
< |
checkCompletedNormally(g, null); |
2972 |
< |
assertEquals(r.value, 2); |
2973 |
< |
} |
2966 |
> |
public void testThenCompose_exceptionalCompletion() { |
2967 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2968 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2969 |
> |
{ |
2970 |
> |
final CFException ex = new CFException(); |
2971 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2972 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2973 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
2974 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2975 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
2976 |
> |
|
2977 |
> |
checkCompletedWithWrappedException(g, ex); |
2978 |
> |
checkCompletedExceptionally(f, ex); |
2979 |
> |
r.assertNotInvoked(); |
2980 |
> |
}} |
2981 |
|
|
2982 |
|
/** |
2983 |
< |
* thenAcceptAsync result completes exceptionally after exceptional |
1318 |
< |
* completion of source |
2983 |
> |
* thenCompose result completes exceptionally if action does |
2984 |
|
*/ |
2985 |
< |
public void testThenAcceptAsync2() { |
2986 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2987 |
< |
IncAction r = new IncAction(); |
2988 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
2989 |
< |
f.completeExceptionally(new CFException()); |
2990 |
< |
checkCompletedWithWrappedCFException(g); |
2991 |
< |
} |
2985 |
> |
public void testThenCompose_actionFailed() { |
2986 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2987 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2988 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2989 |
> |
{ |
2990 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2991 |
> |
final FailingCompletableFutureFunction r |
2992 |
> |
= new FailingCompletableFutureFunction(m); |
2993 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2994 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2995 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2996 |
> |
|
2997 |
> |
checkCompletedWithWrappedException(g, r.ex); |
2998 |
> |
checkCompletedNormally(f, v1); |
2999 |
> |
}} |
3000 |
|
|
3001 |
|
/** |
3002 |
< |
* thenAcceptAsync result completes exceptionally if action does |
3002 |
> |
* thenCompose result completes exceptionally if source cancelled |
3003 |
|
*/ |
3004 |
< |
public void testThenAcceptAsync3() { |
3005 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3006 |
< |
FailingConsumer r = new FailingConsumer(); |
3007 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
3008 |
< |
f.complete(null); |
3009 |
< |
checkCompletedWithWrappedCFException(g); |
3010 |
< |
} |
3004 |
> |
public void testThenCompose_sourceCancelled() { |
3005 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3006 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3007 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3008 |
> |
{ |
3009 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3010 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
3011 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
3012 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
3013 |
> |
if (createIncomplete) { |
3014 |
> |
checkIncomplete(g); |
3015 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
3016 |
> |
} |
3017 |
|
|
1339 |
– |
/** |
1340 |
– |
* thenAcceptAsync result completes exceptionally if source cancelled |
1341 |
– |
*/ |
1342 |
– |
public void testThenAcceptAsync4() { |
1343 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1344 |
– |
IncAction r = new IncAction(); |
1345 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
1346 |
– |
assertTrue(f.cancel(true)); |
3018 |
|
checkCompletedWithWrappedCancellationException(g); |
3019 |
< |
} |
3020 |
< |
|
1350 |
< |
/** |
1351 |
< |
* thenCombineAsync result completes normally after normal |
1352 |
< |
* completion of sources |
1353 |
< |
*/ |
1354 |
< |
public void testThenCombineAsync() { |
1355 |
< |
CompletableFuture<Integer> f, g, h; |
1356 |
< |
|
1357 |
< |
f = new CompletableFuture<Integer>(); |
1358 |
< |
g = new CompletableFuture<Integer>(); |
1359 |
< |
h = f.thenCombineAsync(g, subtract); |
1360 |
< |
f.complete(3); |
1361 |
< |
checkIncomplete(h); |
1362 |
< |
g.complete(1); |
1363 |
< |
checkCompletedNormally(h, 2); |
1364 |
< |
|
1365 |
< |
f = new CompletableFuture<Integer>(); |
1366 |
< |
g = new CompletableFuture<Integer>(); |
1367 |
< |
h = f.thenCombineAsync(g, subtract); |
1368 |
< |
g.complete(1); |
1369 |
< |
checkIncomplete(h); |
1370 |
< |
f.complete(3); |
1371 |
< |
checkCompletedNormally(h, 2); |
1372 |
< |
|
1373 |
< |
f = new CompletableFuture<Integer>(); |
1374 |
< |
g = new CompletableFuture<Integer>(); |
1375 |
< |
g.complete(1); |
1376 |
< |
f.complete(3); |
1377 |
< |
h = f.thenCombineAsync(g, subtract); |
1378 |
< |
checkCompletedNormally(h, 2); |
1379 |
< |
} |
3019 |
> |
checkCancelled(f); |
3020 |
> |
}} |
3021 |
|
|
3022 |
|
/** |
3023 |
< |
* thenCombineAsync result completes exceptionally after exceptional |
1383 |
< |
* completion of either source |
3023 |
> |
* thenCompose result completes exceptionally if the result of the action does |
3024 |
|
*/ |
3025 |
< |
public void testThenCombineAsync2() { |
3026 |
< |
CompletableFuture<Integer> f, g, h; |
3025 |
> |
public void testThenCompose_actionReturnsFailingFuture() { |
3026 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3027 |
> |
for (int order = 0; order < 6; order++) |
3028 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3029 |
> |
{ |
3030 |
> |
final CFException ex = new CFException(); |
3031 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3032 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3033 |
> |
final CompletableFuture<Integer> h; |
3034 |
> |
// Test all permutations of orders |
3035 |
> |
switch (order) { |
3036 |
> |
case 0: |
3037 |
> |
assertTrue(f.complete(v1)); |
3038 |
> |
assertTrue(g.completeExceptionally(ex)); |
3039 |
> |
h = m.thenCompose(f, (x -> g)); |
3040 |
> |
break; |
3041 |
> |
case 1: |
3042 |
> |
assertTrue(f.complete(v1)); |
3043 |
> |
h = m.thenCompose(f, (x -> g)); |
3044 |
> |
assertTrue(g.completeExceptionally(ex)); |
3045 |
> |
break; |
3046 |
> |
case 2: |
3047 |
> |
assertTrue(g.completeExceptionally(ex)); |
3048 |
> |
assertTrue(f.complete(v1)); |
3049 |
> |
h = m.thenCompose(f, (x -> g)); |
3050 |
> |
break; |
3051 |
> |
case 3: |
3052 |
> |
assertTrue(g.completeExceptionally(ex)); |
3053 |
> |
h = m.thenCompose(f, (x -> g)); |
3054 |
> |
assertTrue(f.complete(v1)); |
3055 |
> |
break; |
3056 |
> |
case 4: |
3057 |
> |
h = m.thenCompose(f, (x -> g)); |
3058 |
> |
assertTrue(f.complete(v1)); |
3059 |
> |
assertTrue(g.completeExceptionally(ex)); |
3060 |
> |
break; |
3061 |
> |
case 5: |
3062 |
> |
h = m.thenCompose(f, (x -> g)); |
3063 |
> |
assertTrue(f.complete(v1)); |
3064 |
> |
assertTrue(g.completeExceptionally(ex)); |
3065 |
> |
break; |
3066 |
> |
default: throw new AssertionError(); |
3067 |
> |
} |
3068 |
|
|
3069 |
< |
f = new CompletableFuture<Integer>(); |
3070 |
< |
g = new CompletableFuture<Integer>(); |
3071 |
< |
h = f.thenCombineAsync(g, subtract); |
3072 |
< |
f.completeExceptionally(new CFException()); |
1392 |
< |
checkIncomplete(h); |
1393 |
< |
g.complete(1); |
1394 |
< |
checkCompletedWithWrappedCFException(h); |
1395 |
< |
|
1396 |
< |
f = new CompletableFuture<Integer>(); |
1397 |
< |
g = new CompletableFuture<Integer>(); |
1398 |
< |
h = f.thenCombineAsync(g, subtract); |
1399 |
< |
g.completeExceptionally(new CFException()); |
1400 |
< |
checkIncomplete(h); |
1401 |
< |
f.complete(3); |
1402 |
< |
checkCompletedWithWrappedCFException(h); |
1403 |
< |
|
1404 |
< |
f = new CompletableFuture<Integer>(); |
1405 |
< |
g = new CompletableFuture<Integer>(); |
1406 |
< |
g.completeExceptionally(new CFException()); |
1407 |
< |
f.complete(3); |
1408 |
< |
h = f.thenCombineAsync(g, subtract); |
1409 |
< |
checkCompletedWithWrappedCFException(h); |
1410 |
< |
} |
3069 |
> |
checkCompletedExceptionally(g, ex); |
3070 |
> |
checkCompletedWithWrappedException(h, ex); |
3071 |
> |
checkCompletedNormally(f, v1); |
3072 |
> |
}} |
3073 |
|
|
3074 |
< |
/** |
1413 |
< |
* thenCombineAsync result completes exceptionally if action does |
1414 |
< |
*/ |
1415 |
< |
public void testThenCombineAsync3() { |
1416 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1417 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1418 |
< |
FailingBiFunction r = new FailingBiFunction(); |
1419 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, r); |
1420 |
< |
f.complete(one); |
1421 |
< |
checkIncomplete(g); |
1422 |
< |
assertFalse(r.ran); |
1423 |
< |
f2.complete(two); |
1424 |
< |
checkCompletedWithWrappedCFException(g); |
1425 |
< |
assertTrue(r.ran); |
1426 |
< |
} |
3074 |
> |
// other static methods |
3075 |
|
|
3076 |
|
/** |
3077 |
< |
* thenCombineAsync result completes exceptionally if either source cancelled |
3077 |
> |
* allOf(no component futures) returns a future completed normally |
3078 |
> |
* with the value null |
3079 |
|
*/ |
3080 |
< |
public void testThenCombineAsync4() { |
3081 |
< |
CompletableFuture<Integer> f, g, h; |
3082 |
< |
|
1434 |
< |
f = new CompletableFuture<Integer>(); |
1435 |
< |
g = new CompletableFuture<Integer>(); |
1436 |
< |
h = f.thenCombineAsync(g, subtract); |
1437 |
< |
assertTrue(f.cancel(true)); |
1438 |
< |
checkIncomplete(h); |
1439 |
< |
g.complete(1); |
1440 |
< |
checkCompletedWithWrappedCancellationException(h); |
1441 |
< |
|
1442 |
< |
f = new CompletableFuture<Integer>(); |
1443 |
< |
g = new CompletableFuture<Integer>(); |
1444 |
< |
h = f.thenCombineAsync(g, subtract); |
1445 |
< |
assertTrue(g.cancel(true)); |
1446 |
< |
checkIncomplete(h); |
1447 |
< |
f.complete(3); |
1448 |
< |
checkCompletedWithWrappedCancellationException(h); |
1449 |
< |
|
1450 |
< |
f = new CompletableFuture<Integer>(); |
1451 |
< |
g = new CompletableFuture<Integer>(); |
1452 |
< |
g.complete(3); |
1453 |
< |
assertTrue(f.cancel(true)); |
1454 |
< |
h = f.thenCombineAsync(g, subtract); |
1455 |
< |
checkCompletedWithWrappedCancellationException(h); |
1456 |
< |
|
1457 |
< |
f = new CompletableFuture<Integer>(); |
1458 |
< |
g = new CompletableFuture<Integer>(); |
1459 |
< |
f.complete(3); |
1460 |
< |
assertTrue(g.cancel(true)); |
1461 |
< |
h = f.thenCombineAsync(g, subtract); |
1462 |
< |
checkCompletedWithWrappedCancellationException(h); |
3080 |
> |
public void testAllOf_empty() throws Exception { |
3081 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(); |
3082 |
> |
checkCompletedNormally(f, null); |
3083 |
|
} |
3084 |
|
|
3085 |
|
/** |
3086 |
< |
* thenAcceptBothAsync result completes normally after normal |
3087 |
< |
* completion of sources |
3086 |
> |
* allOf returns a future completed normally with the value null |
3087 |
> |
* when all components complete normally |
3088 |
|
*/ |
3089 |
< |
public void testThenAcceptBothAsync() { |
3090 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3091 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3092 |
< |
AddAction r = new AddAction(); |
3093 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
3094 |
< |
f.complete(one); |
3095 |
< |
checkIncomplete(g); |
3096 |
< |
f2.complete(two); |
3097 |
< |
checkCompletedNormally(g, null); |
3098 |
< |
assertEquals(r.value, 3); |
3089 |
> |
public void testAllOf_normal() throws Exception { |
3090 |
> |
for (int k = 1; k < 10; k++) { |
3091 |
> |
CompletableFuture<Integer>[] fs |
3092 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3093 |
> |
for (int i = 0; i < k; i++) |
3094 |
> |
fs[i] = new CompletableFuture<>(); |
3095 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3096 |
> |
for (int i = 0; i < k; i++) { |
3097 |
> |
checkIncomplete(f); |
3098 |
> |
checkIncomplete(CompletableFuture.allOf(fs)); |
3099 |
> |
fs[i].complete(one); |
3100 |
> |
} |
3101 |
> |
checkCompletedNormally(f, null); |
3102 |
> |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
3103 |
> |
} |
3104 |
|
} |
3105 |
|
|
3106 |
< |
/** |
3107 |
< |
* thenAcceptBothAsync result completes exceptionally after exceptional |
3108 |
< |
* completion of source |
3109 |
< |
*/ |
3110 |
< |
public void testThenAcceptBothAsync2() { |
3111 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3112 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3113 |
< |
AddAction r = new AddAction(); |
3114 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
3115 |
< |
f.completeExceptionally(new CFException()); |
3116 |
< |
f2.complete(two); |
3117 |
< |
checkCompletedWithWrappedCFException(g); |
3118 |
< |
|
3119 |
< |
r = new AddAction(); |
3120 |
< |
f = new CompletableFuture<Integer>(); |
1496 |
< |
f2 = new CompletableFuture<Integer>(); |
1497 |
< |
g = f.thenAcceptBothAsync(f2, r); |
1498 |
< |
f.complete(one); |
1499 |
< |
f2.completeExceptionally(new CFException()); |
1500 |
< |
checkCompletedWithWrappedCFException(g); |
3106 |
> |
public void testAllOf_normal_backwards() throws Exception { |
3107 |
> |
for (int k = 1; k < 10; k++) { |
3108 |
> |
CompletableFuture<Integer>[] fs |
3109 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3110 |
> |
for (int i = 0; i < k; i++) |
3111 |
> |
fs[i] = new CompletableFuture<>(); |
3112 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3113 |
> |
for (int i = k - 1; i >= 0; i--) { |
3114 |
> |
checkIncomplete(f); |
3115 |
> |
checkIncomplete(CompletableFuture.allOf(fs)); |
3116 |
> |
fs[i].complete(one); |
3117 |
> |
} |
3118 |
> |
checkCompletedNormally(f, null); |
3119 |
> |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
3120 |
> |
} |
3121 |
|
} |
3122 |
|
|
3123 |
< |
/** |
3124 |
< |
* thenAcceptBothAsync result completes exceptionally if action does |
3125 |
< |
*/ |
3126 |
< |
public void testThenAcceptBothAsync3() { |
3127 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3128 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3129 |
< |
FailingBiConsumer r = new FailingBiConsumer(); |
3130 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
3131 |
< |
f.complete(one); |
3132 |
< |
checkIncomplete(g); |
3133 |
< |
f2.complete(two); |
3134 |
< |
checkCompletedWithWrappedCFException(g); |
3123 |
> |
public void testAllOf_exceptional() throws Exception { |
3124 |
> |
for (int k = 1; k < 10; k++) { |
3125 |
> |
CompletableFuture<Integer>[] fs |
3126 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3127 |
> |
CFException ex = new CFException(); |
3128 |
> |
for (int i = 0; i < k; i++) |
3129 |
> |
fs[i] = new CompletableFuture<>(); |
3130 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3131 |
> |
for (int i = 0; i < k; i++) { |
3132 |
> |
checkIncomplete(f); |
3133 |
> |
checkIncomplete(CompletableFuture.allOf(fs)); |
3134 |
> |
if (i != k / 2) { |
3135 |
> |
fs[i].complete(i); |
3136 |
> |
checkCompletedNormally(fs[i], i); |
3137 |
> |
} else { |
3138 |
> |
fs[i].completeExceptionally(ex); |
3139 |
> |
checkCompletedExceptionally(fs[i], ex); |
3140 |
> |
} |
3141 |
> |
} |
3142 |
> |
checkCompletedWithWrappedException(f, ex); |
3143 |
> |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3144 |
> |
} |
3145 |
|
} |
3146 |
|
|
3147 |
|
/** |
3148 |
< |
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
3148 |
> |
* anyOf(no component futures) returns an incomplete future |
3149 |
|
*/ |
3150 |
< |
public void testThenAcceptBothAsync4() { |
3151 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3152 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3153 |
< |
AddAction r = new AddAction(); |
3154 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1525 |
< |
assertTrue(f.cancel(true)); |
1526 |
< |
f2.complete(two); |
1527 |
< |
checkCompletedWithWrappedCancellationException(g); |
1528 |
< |
|
1529 |
< |
r = new AddAction(); |
1530 |
< |
f = new CompletableFuture<Integer>(); |
1531 |
< |
f2 = new CompletableFuture<Integer>(); |
1532 |
< |
g = f.thenAcceptBothAsync(f2, r); |
1533 |
< |
f.complete(one); |
1534 |
< |
assertTrue(f2.cancel(true)); |
1535 |
< |
checkCompletedWithWrappedCancellationException(g); |
1536 |
< |
} |
3150 |
> |
public void testAnyOf_empty() throws Exception { |
3151 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3152 |
> |
{ |
3153 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3154 |
> |
checkIncomplete(f); |
3155 |
|
|
3156 |
< |
/** |
3157 |
< |
* runAfterBothAsync result completes normally after normal |
3158 |
< |
* completion of sources |
1541 |
< |
*/ |
1542 |
< |
public void testRunAfterBothAsync() { |
1543 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1544 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1545 |
< |
Noop r = new Noop(); |
1546 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1547 |
< |
f.complete(one); |
1548 |
< |
checkIncomplete(g); |
1549 |
< |
f2.complete(two); |
1550 |
< |
checkCompletedNormally(g, null); |
1551 |
< |
assertTrue(r.ran); |
1552 |
< |
} |
3156 |
> |
f.complete(v1); |
3157 |
> |
checkCompletedNormally(f, v1); |
3158 |
> |
}} |
3159 |
|
|
3160 |
|
/** |
3161 |
< |
* runAfterBothAsync result completes exceptionally after exceptional |
3162 |
< |
* completion of source |
3161 |
> |
* anyOf returns a future completed normally with a value when |
3162 |
> |
* a component future does |
3163 |
|
*/ |
3164 |
< |
public void testRunAfterBothAsync2() { |
3165 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3166 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3167 |
< |
Noop r = new Noop(); |
3168 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
3169 |
< |
f.completeExceptionally(new CFException()); |
3170 |
< |
f2.complete(two); |
3171 |
< |
checkCompletedWithWrappedCFException(g); |
3172 |
< |
|
3173 |
< |
r = new Noop(); |
3174 |
< |
f = new CompletableFuture<Integer>(); |
3175 |
< |
f2 = new CompletableFuture<Integer>(); |
3176 |
< |
g = f.runAfterBothAsync(f2, r); |
3177 |
< |
f.complete(one); |
1572 |
< |
f2.completeExceptionally(new CFException()); |
1573 |
< |
checkCompletedWithWrappedCFException(g); |
1574 |
< |
} |
1575 |
< |
|
1576 |
< |
/** |
1577 |
< |
* runAfterBothAsync result completes exceptionally if action does |
1578 |
< |
*/ |
1579 |
< |
public void testRunAfterBothAsync3() { |
1580 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1581 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1582 |
< |
FailingNoop r = new FailingNoop(); |
1583 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1584 |
< |
f.complete(one); |
1585 |
< |
checkIncomplete(g); |
1586 |
< |
f2.complete(two); |
1587 |
< |
checkCompletedWithWrappedCFException(g); |
3164 |
> |
public void testAnyOf_normal() throws Exception { |
3165 |
> |
for (int k = 0; k < 10; k++) { |
3166 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3167 |
> |
for (int i = 0; i < k; i++) |
3168 |
> |
fs[i] = new CompletableFuture<>(); |
3169 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3170 |
> |
checkIncomplete(f); |
3171 |
> |
for (int i = 0; i < k; i++) { |
3172 |
> |
fs[i].complete(i); |
3173 |
> |
checkCompletedNormally(f, 0); |
3174 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3175 |
> |
assertTrue(0 <= x && x <= i); |
3176 |
> |
} |
3177 |
> |
} |
3178 |
|
} |
3179 |
< |
|
3180 |
< |
/** |
3181 |
< |
* runAfterBothAsync result completes exceptionally if either source cancelled |
3182 |
< |
*/ |
3183 |
< |
public void testRunAfterBothAsync4() { |
3184 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3185 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3186 |
< |
Noop r = new Noop(); |
3187 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
3188 |
< |
assertTrue(f.cancel(true)); |
3189 |
< |
f2.complete(two); |
3190 |
< |
checkCompletedWithWrappedCancellationException(g); |
3191 |
< |
|
3192 |
< |
r = new Noop(); |
1603 |
< |
f = new CompletableFuture<Integer>(); |
1604 |
< |
f2 = new CompletableFuture<Integer>(); |
1605 |
< |
g = f.runAfterBothAsync(f2, r); |
1606 |
< |
f.complete(one); |
1607 |
< |
assertTrue(f2.cancel(true)); |
1608 |
< |
checkCompletedWithWrappedCancellationException(g); |
3179 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3180 |
> |
for (int k = 0; k < 10; k++) { |
3181 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3182 |
> |
for (int i = 0; i < k; i++) |
3183 |
> |
fs[i] = new CompletableFuture<>(); |
3184 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3185 |
> |
checkIncomplete(f); |
3186 |
> |
for (int i = k - 1; i >= 0; i--) { |
3187 |
> |
fs[i].complete(i); |
3188 |
> |
checkCompletedNormally(f, k - 1); |
3189 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3190 |
> |
assertTrue(i <= x && x <= k - 1); |
3191 |
> |
} |
3192 |
> |
} |
3193 |
|
} |
3194 |
|
|
3195 |
|
/** |
3196 |
< |
* applyToEitherAsync result completes normally after normal |
1613 |
< |
* completion of sources |
3196 |
> |
* anyOf result completes exceptionally when any component does. |
3197 |
|
*/ |
3198 |
< |
public void testApplyToEitherAsync() { |
3199 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3200 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3201 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
3202 |
< |
f.complete(one); |
3203 |
< |
checkCompletedNormally(g, two); |
3204 |
< |
|
3205 |
< |
f = new CompletableFuture<Integer>(); |
3206 |
< |
f.complete(one); |
3207 |
< |
f2 = new CompletableFuture<Integer>(); |
3208 |
< |
g = f.applyToEitherAsync(f2, inc); |
3209 |
< |
checkCompletedNormally(g, two); |
3198 |
> |
public void testAnyOf_exceptional() throws Exception { |
3199 |
> |
for (int k = 0; k < 10; k++) { |
3200 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3201 |
> |
CFException[] exs = new CFException[k]; |
3202 |
> |
for (int i = 0; i < k; i++) { |
3203 |
> |
fs[i] = new CompletableFuture<>(); |
3204 |
> |
exs[i] = new CFException(); |
3205 |
> |
} |
3206 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3207 |
> |
checkIncomplete(f); |
3208 |
> |
for (int i = 0; i < k; i++) { |
3209 |
> |
fs[i].completeExceptionally(exs[i]); |
3210 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3211 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3212 |
> |
} |
3213 |
> |
} |
3214 |
|
} |
3215 |
|
|
3216 |
< |
/** |
3217 |
< |
* applyToEitherAsync result completes exceptionally after exceptional |
3218 |
< |
* completion of source |
3219 |
< |
*/ |
3220 |
< |
public void testApplyToEitherAsync2() { |
3221 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3222 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3223 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
3224 |
< |
f.completeExceptionally(new CFException()); |
3225 |
< |
checkCompletedWithWrappedCFException(g); |
3226 |
< |
|
3227 |
< |
f = new CompletableFuture<Integer>(); |
3228 |
< |
f2 = new CompletableFuture<Integer>(); |
3229 |
< |
f2.completeExceptionally(new CFException()); |
3230 |
< |
g = f.applyToEitherAsync(f2, inc); |
3231 |
< |
f.complete(one); |
1645 |
< |
checkCompletedWithWrappedCFException(g); |
3216 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3217 |
> |
for (int k = 0; k < 10; k++) { |
3218 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3219 |
> |
CFException[] exs = new CFException[k]; |
3220 |
> |
for (int i = 0; i < k; i++) { |
3221 |
> |
fs[i] = new CompletableFuture<>(); |
3222 |
> |
exs[i] = new CFException(); |
3223 |
> |
} |
3224 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3225 |
> |
checkIncomplete(f); |
3226 |
> |
for (int i = k - 1; i >= 0; i--) { |
3227 |
> |
fs[i].completeExceptionally(exs[i]); |
3228 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3229 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3230 |
> |
} |
3231 |
> |
} |
3232 |
|
} |
3233 |
|
|
3234 |
|
/** |
3235 |
< |
* applyToEitherAsync result completes exceptionally if action does |
3235 |
> |
* Completion methods throw NullPointerException with null arguments |
3236 |
|
*/ |
3237 |
< |
public void testApplyToEitherAsync3() { |
3238 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3239 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3240 |
< |
FailingFunction r = new FailingFunction(); |
3241 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r); |
3242 |
< |
f.complete(one); |
1657 |
< |
checkCompletedWithWrappedCFException(g); |
1658 |
< |
} |
3237 |
> |
@SuppressWarnings("FutureReturnValueIgnored") |
3238 |
> |
public void testNPE() { |
3239 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3240 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3241 |
> |
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
3242 |
> |
ThreadExecutor exec = new ThreadExecutor(); |
3243 |
|
|
3244 |
< |
/** |
3245 |
< |
* applyToEitherAsync result completes exceptionally if either source cancelled |
3246 |
< |
*/ |
3247 |
< |
public void testApplyToEitherAsync4() { |
3248 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3249 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3250 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
3251 |
< |
assertTrue(f.cancel(true)); |
3252 |
< |
checkCompletedWithWrappedCancellationException(g); |
3244 |
> |
Runnable[] throwingActions = { |
3245 |
> |
() -> CompletableFuture.supplyAsync(null), |
3246 |
> |
() -> CompletableFuture.supplyAsync(null, exec), |
3247 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3248 |
> |
|
3249 |
> |
() -> CompletableFuture.runAsync(null), |
3250 |
> |
() -> CompletableFuture.runAsync(null, exec), |
3251 |
> |
() -> CompletableFuture.runAsync(() -> {}, null), |
3252 |
> |
|
3253 |
> |
() -> f.completeExceptionally(null), |
3254 |
> |
|
3255 |
> |
() -> f.thenApply(null), |
3256 |
> |
() -> f.thenApplyAsync(null), |
3257 |
> |
() -> f.thenApplyAsync(x -> x, null), |
3258 |
> |
() -> f.thenApplyAsync(null, exec), |
3259 |
> |
|
3260 |
> |
() -> f.thenAccept(null), |
3261 |
> |
() -> f.thenAcceptAsync(null), |
3262 |
> |
() -> f.thenAcceptAsync(x -> {} , null), |
3263 |
> |
() -> f.thenAcceptAsync(null, exec), |
3264 |
> |
|
3265 |
> |
() -> f.thenRun(null), |
3266 |
> |
() -> f.thenRunAsync(null), |
3267 |
> |
() -> f.thenRunAsync(() -> {} , null), |
3268 |
> |
() -> f.thenRunAsync(null, exec), |
3269 |
> |
|
3270 |
> |
() -> f.thenCombine(g, null), |
3271 |
> |
() -> f.thenCombineAsync(g, null), |
3272 |
> |
() -> f.thenCombineAsync(g, null, exec), |
3273 |
> |
() -> f.thenCombine(nullFuture, (x, y) -> x), |
3274 |
> |
() -> f.thenCombineAsync(nullFuture, (x, y) -> x), |
3275 |
> |
() -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec), |
3276 |
> |
() -> f.thenCombineAsync(g, (x, y) -> x, null), |
3277 |
> |
|
3278 |
> |
() -> f.thenAcceptBoth(g, null), |
3279 |
> |
() -> f.thenAcceptBothAsync(g, null), |
3280 |
> |
() -> f.thenAcceptBothAsync(g, null, exec), |
3281 |
> |
() -> f.thenAcceptBoth(nullFuture, (x, y) -> {}), |
3282 |
> |
() -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}), |
3283 |
> |
() -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec), |
3284 |
> |
() -> f.thenAcceptBothAsync(g, (x, y) -> {}, null), |
3285 |
> |
|
3286 |
> |
() -> f.runAfterBoth(g, null), |
3287 |
> |
() -> f.runAfterBothAsync(g, null), |
3288 |
> |
() -> f.runAfterBothAsync(g, null, exec), |
3289 |
> |
() -> f.runAfterBoth(nullFuture, () -> {}), |
3290 |
> |
() -> f.runAfterBothAsync(nullFuture, () -> {}), |
3291 |
> |
() -> f.runAfterBothAsync(nullFuture, () -> {}, exec), |
3292 |
> |
() -> f.runAfterBothAsync(g, () -> {}, null), |
3293 |
> |
|
3294 |
> |
() -> f.applyToEither(g, null), |
3295 |
> |
() -> f.applyToEitherAsync(g, null), |
3296 |
> |
() -> f.applyToEitherAsync(g, null, exec), |
3297 |
> |
() -> f.applyToEither(nullFuture, x -> x), |
3298 |
> |
() -> f.applyToEitherAsync(nullFuture, x -> x), |
3299 |
> |
() -> f.applyToEitherAsync(nullFuture, x -> x, exec), |
3300 |
> |
() -> f.applyToEitherAsync(g, x -> x, null), |
3301 |
> |
|
3302 |
> |
() -> f.acceptEither(g, null), |
3303 |
> |
() -> f.acceptEitherAsync(g, null), |
3304 |
> |
() -> f.acceptEitherAsync(g, null, exec), |
3305 |
> |
() -> f.acceptEither(nullFuture, x -> {}), |
3306 |
> |
() -> f.acceptEitherAsync(nullFuture, x -> {}), |
3307 |
> |
() -> f.acceptEitherAsync(nullFuture, x -> {}, exec), |
3308 |
> |
() -> f.acceptEitherAsync(g, x -> {}, null), |
3309 |
> |
|
3310 |
> |
() -> f.runAfterEither(g, null), |
3311 |
> |
() -> f.runAfterEitherAsync(g, null), |
3312 |
> |
() -> f.runAfterEitherAsync(g, null, exec), |
3313 |
> |
() -> f.runAfterEither(nullFuture, () -> {}), |
3314 |
> |
() -> f.runAfterEitherAsync(nullFuture, () -> {}), |
3315 |
> |
() -> f.runAfterEitherAsync(nullFuture, () -> {}, exec), |
3316 |
> |
() -> f.runAfterEitherAsync(g, () -> {}, null), |
3317 |
> |
|
3318 |
> |
() -> f.thenCompose(null), |
3319 |
> |
() -> f.thenComposeAsync(null), |
3320 |
> |
() -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null), |
3321 |
> |
() -> f.thenComposeAsync(null, exec), |
3322 |
> |
|
3323 |
> |
() -> f.exceptionally(null), |
3324 |
> |
|
3325 |
> |
() -> f.handle(null), |
3326 |
> |
|
3327 |
> |
() -> CompletableFuture.allOf((CompletableFuture<?>)null), |
3328 |
> |
() -> CompletableFuture.allOf((CompletableFuture<?>[])null), |
3329 |
> |
() -> CompletableFuture.allOf(f, null), |
3330 |
> |
() -> CompletableFuture.allOf(null, f), |
3331 |
> |
|
3332 |
> |
() -> CompletableFuture.anyOf((CompletableFuture<?>)null), |
3333 |
> |
() -> CompletableFuture.anyOf((CompletableFuture<?>[])null), |
3334 |
> |
() -> CompletableFuture.anyOf(f, null), |
3335 |
> |
() -> CompletableFuture.anyOf(null, f), |
3336 |
> |
|
3337 |
> |
() -> f.obtrudeException(null), |
3338 |
> |
|
3339 |
> |
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3340 |
> |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
3341 |
> |
() -> CompletableFuture.delayedExecutor(1L, null), |
3342 |
|
|
3343 |
< |
f = new CompletableFuture<Integer>(); |
3344 |
< |
f2 = new CompletableFuture<Integer>(); |
1672 |
< |
assertTrue(f2.cancel(true)); |
1673 |
< |
g = f.applyToEitherAsync(f2, inc); |
1674 |
< |
checkCompletedWithWrappedCancellationException(g); |
1675 |
< |
} |
3343 |
> |
() -> f.orTimeout(1L, null), |
3344 |
> |
() -> f.completeOnTimeout(42, 1L, null), |
3345 |
|
|
3346 |
< |
/** |
3347 |
< |
* acceptEitherAsync result completes normally after normal |
3348 |
< |
* completion of sources |
1680 |
< |
*/ |
1681 |
< |
public void testAcceptEitherAsync() { |
1682 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1683 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1684 |
< |
IncAction r = new IncAction(); |
1685 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1686 |
< |
f.complete(one); |
1687 |
< |
checkCompletedNormally(g, null); |
1688 |
< |
assertEquals(r.value, 2); |
3346 |
> |
() -> CompletableFuture.failedFuture(null), |
3347 |
> |
() -> CompletableFuture.failedStage(null), |
3348 |
> |
}; |
3349 |
|
|
3350 |
< |
r = new IncAction(); |
3351 |
< |
f = new CompletableFuture<Integer>(); |
1692 |
< |
f.complete(one); |
1693 |
< |
f2 = new CompletableFuture<Integer>(); |
1694 |
< |
g = f.acceptEitherAsync(f2, r); |
1695 |
< |
checkCompletedNormally(g, null); |
1696 |
< |
assertEquals(r.value, 2); |
3350 |
> |
assertThrows(NullPointerException.class, throwingActions); |
3351 |
> |
assertEquals(0, exec.count.get()); |
3352 |
|
} |
3353 |
|
|
3354 |
|
/** |
3355 |
< |
* acceptEitherAsync result completes exceptionally after exceptional |
1701 |
< |
* completion of source |
3355 |
> |
* Test submissions to an executor that rejects all tasks. |
3356 |
|
*/ |
3357 |
< |
public void testAcceptEitherAsync2() { |
3358 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3359 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3360 |
< |
IncAction r = new IncAction(); |
3361 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
3362 |
< |
f.completeExceptionally(new CFException()); |
3363 |
< |
checkCompletedWithWrappedCFException(g); |
3357 |
> |
public void testRejectingExecutor() { |
3358 |
> |
for (Integer v : new Integer[] { 1, null }) |
3359 |
> |
{ |
3360 |
> |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3361 |
> |
|
3362 |
> |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3363 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3364 |
> |
|
3365 |
> |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3366 |
> |
|
3367 |
> |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3368 |
> |
srcs.add(complete); |
3369 |
> |
srcs.add(incomplete); |
3370 |
> |
|
3371 |
> |
for (CompletableFuture<Integer> src : srcs) { |
3372 |
> |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3373 |
> |
fs.add(src.thenRunAsync(() -> {}, e)); |
3374 |
> |
fs.add(src.thenAcceptAsync(z -> {}, e)); |
3375 |
> |
fs.add(src.thenApplyAsync(z -> z, e)); |
3376 |
> |
|
3377 |
> |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3378 |
> |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3379 |
> |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3380 |
> |
|
3381 |
> |
fs.add(src.applyToEitherAsync(src, z -> z, e)); |
3382 |
> |
fs.add(src.acceptEitherAsync(src, z -> {}, e)); |
3383 |
> |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3384 |
> |
|
3385 |
> |
fs.add(src.thenComposeAsync(z -> null, e)); |
3386 |
> |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3387 |
> |
fs.add(src.handleAsync((z, t) -> null, e)); |
3388 |
> |
|
3389 |
> |
for (CompletableFuture<?> future : fs) { |
3390 |
> |
if (src.isDone()) |
3391 |
> |
checkCompletedWithWrappedException(future, e.ex); |
3392 |
> |
else |
3393 |
> |
checkIncomplete(future); |
3394 |
> |
} |
3395 |
> |
futures.addAll(fs); |
3396 |
> |
} |
3397 |
|
|
3398 |
< |
r = new IncAction(); |
3399 |
< |
f = new CompletableFuture<Integer>(); |
1713 |
< |
f2 = new CompletableFuture<Integer>(); |
1714 |
< |
f2.completeExceptionally(new CFException()); |
1715 |
< |
g = f.acceptEitherAsync(f2, r); |
1716 |
< |
f.complete(one); |
1717 |
< |
checkCompletedWithWrappedCFException(g); |
1718 |
< |
} |
3398 |
> |
{ |
3399 |
> |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3400 |
|
|
3401 |
< |
/** |
3402 |
< |
* acceptEitherAsync result completes exceptionally if action does |
1722 |
< |
*/ |
1723 |
< |
public void testAcceptEitherAsync3() { |
1724 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1725 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1726 |
< |
FailingConsumer r = new FailingConsumer(); |
1727 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1728 |
< |
f.complete(one); |
1729 |
< |
checkCompletedWithWrappedCFException(g); |
1730 |
< |
} |
3401 |
> |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3402 |
> |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3403 |
|
|
3404 |
< |
/** |
3405 |
< |
* acceptEitherAsync result completes exceptionally if either |
1734 |
< |
* source cancelled |
1735 |
< |
*/ |
1736 |
< |
public void testAcceptEitherAsync4() { |
1737 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1738 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1739 |
< |
IncAction r = new IncAction(); |
1740 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1741 |
< |
assertTrue(f.cancel(true)); |
1742 |
< |
checkCompletedWithWrappedCancellationException(g); |
3404 |
> |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3405 |
> |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3406 |
|
|
3407 |
< |
r = new IncAction(); |
3408 |
< |
f = new CompletableFuture<Integer>(); |
1746 |
< |
f2 = new CompletableFuture<Integer>(); |
1747 |
< |
assertTrue(f2.cancel(true)); |
1748 |
< |
g = f.acceptEitherAsync(f2, r); |
1749 |
< |
checkCompletedWithWrappedCancellationException(g); |
1750 |
< |
} |
3407 |
> |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3408 |
> |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3409 |
|
|
3410 |
< |
/** |
3411 |
< |
* runAfterEitherAsync result completes normally after normal |
3412 |
< |
* completion of sources |
3413 |
< |
*/ |
1756 |
< |
public void testRunAfterEitherAsync() { |
1757 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1758 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1759 |
< |
Noop r = new Noop(); |
1760 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1761 |
< |
f.complete(one); |
1762 |
< |
checkCompletedNormally(g, null); |
1763 |
< |
assertTrue(r.ran); |
1764 |
< |
|
1765 |
< |
r = new Noop(); |
1766 |
< |
f = new CompletableFuture<Integer>(); |
1767 |
< |
f.complete(one); |
1768 |
< |
f2 = new CompletableFuture<Integer>(); |
1769 |
< |
g = f.runAfterEitherAsync(f2, r); |
1770 |
< |
checkCompletedNormally(g, null); |
1771 |
< |
assertTrue(r.ran); |
1772 |
< |
} |
3410 |
> |
for (CompletableFuture<?> future : fs) |
3411 |
> |
checkIncomplete(future); |
3412 |
> |
futures.addAll(fs); |
3413 |
> |
} |
3414 |
|
|
3415 |
< |
/** |
3416 |
< |
* runAfterEitherAsync result completes exceptionally after exceptional |
1776 |
< |
* completion of source |
1777 |
< |
*/ |
1778 |
< |
public void testRunAfterEitherAsync2() { |
1779 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1780 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1781 |
< |
Noop r = new Noop(); |
1782 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1783 |
< |
f.completeExceptionally(new CFException()); |
1784 |
< |
checkCompletedWithWrappedCFException(g); |
3415 |
> |
{ |
3416 |
> |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3417 |
|
|
3418 |
< |
r = new Noop(); |
3419 |
< |
f = new CompletableFuture<Integer>(); |
1788 |
< |
f2 = new CompletableFuture<Integer>(); |
1789 |
< |
f2.completeExceptionally(new CFException()); |
1790 |
< |
g = f.runAfterEitherAsync(f2, r); |
1791 |
< |
f.complete(one); |
1792 |
< |
checkCompletedWithWrappedCFException(g); |
1793 |
< |
} |
3418 |
> |
fs.add(complete.applyToEitherAsync(incomplete, z -> z, e)); |
3419 |
> |
fs.add(incomplete.applyToEitherAsync(complete, z -> z, e)); |
3420 |
|
|
3421 |
< |
/** |
3422 |
< |
* runAfterEitherAsync result completes exceptionally if action does |
1797 |
< |
*/ |
1798 |
< |
public void testRunAfterEitherAsync3() { |
1799 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1800 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1801 |
< |
FailingNoop r = new FailingNoop(); |
1802 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1803 |
< |
f.complete(one); |
1804 |
< |
checkCompletedWithWrappedCFException(g); |
1805 |
< |
} |
3421 |
> |
fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e)); |
3422 |
> |
fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e)); |
3423 |
|
|
3424 |
< |
/** |
3425 |
< |
* runAfterEitherAsync result completes exceptionally if either |
1809 |
< |
* source cancelled |
1810 |
< |
*/ |
1811 |
< |
public void testRunAfterEitherAsync4() { |
1812 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1813 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1814 |
< |
Noop r = new Noop(); |
1815 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1816 |
< |
assertTrue(f.cancel(true)); |
1817 |
< |
checkCompletedWithWrappedCancellationException(g); |
3424 |
> |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3425 |
> |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3426 |
|
|
3427 |
< |
r = new Noop(); |
3428 |
< |
f = new CompletableFuture<Integer>(); |
3429 |
< |
f2 = new CompletableFuture<Integer>(); |
3430 |
< |
assertTrue(f2.cancel(true)); |
1823 |
< |
g = f.runAfterEitherAsync(f2, r); |
1824 |
< |
checkCompletedWithWrappedCancellationException(g); |
1825 |
< |
} |
3427 |
> |
for (CompletableFuture<?> future : fs) |
3428 |
> |
checkCompletedWithWrappedException(future, e.ex); |
3429 |
> |
futures.addAll(fs); |
3430 |
> |
} |
3431 |
|
|
3432 |
< |
/** |
1828 |
< |
* thenComposeAsync result completes normally after normal |
1829 |
< |
* completion of source |
1830 |
< |
*/ |
1831 |
< |
public void testThenComposeAsync() { |
1832 |
< |
CompletableFuture<Integer> f, g; |
1833 |
< |
CompletableFutureInc r; |
3432 |
> |
incomplete.complete(v); |
3433 |
|
|
3434 |
< |
f = new CompletableFuture<Integer>(); |
3435 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1837 |
< |
f.complete(one); |
1838 |
< |
checkCompletedNormally(g, two); |
3434 |
> |
for (CompletableFuture<?> future : futures) |
3435 |
> |
checkCompletedWithWrappedException(future, e.ex); |
3436 |
|
|
3437 |
< |
f = new CompletableFuture<Integer>(); |
3438 |
< |
f.complete(one); |
1842 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1843 |
< |
checkCompletedNormally(g, two); |
1844 |
< |
} |
3437 |
> |
assertEquals(futures.size(), e.count.get()); |
3438 |
> |
}} |
3439 |
|
|
3440 |
|
/** |
3441 |
< |
* thenComposeAsync result completes exceptionally after |
3442 |
< |
* exceptional completion of source |
3441 |
> |
* Test submissions to an executor that rejects all tasks, but |
3442 |
> |
* should never be invoked because the dependent future is |
3443 |
> |
* explicitly completed. |
3444 |
|
*/ |
3445 |
< |
public void testThenComposeAsync2() { |
3446 |
< |
CompletableFuture<Integer> f, g; |
3447 |
< |
CompletableFutureInc r; |
3445 |
> |
public void testRejectingExecutorNeverInvoked() { |
3446 |
> |
for (Integer v : new Integer[] { 1, null }) |
3447 |
> |
{ |
3448 |
> |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3449 |
|
|
3450 |
< |
f = new CompletableFuture<Integer>(); |
3451 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1856 |
< |
f.completeExceptionally(new CFException()); |
1857 |
< |
checkCompletedWithWrappedCFException(g); |
1858 |
< |
assertFalse(r.ran); |
3450 |
> |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3451 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3452 |
|
|
3453 |
< |
f = new CompletableFuture<Integer>(); |
1861 |
< |
f.completeExceptionally(new CFException()); |
1862 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1863 |
< |
checkCompletedWithWrappedCFException(g); |
1864 |
< |
assertFalse(r.ran); |
1865 |
< |
} |
3453 |
> |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3454 |
|
|
3455 |
< |
/** |
3456 |
< |
* thenComposeAsync result completes exceptionally if action does |
3457 |
< |
*/ |
1870 |
< |
public void testThenComposeAsync3() { |
1871 |
< |
CompletableFuture<Integer> f, g; |
1872 |
< |
FailingCompletableFutureFunction r; |
3455 |
> |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3456 |
> |
srcs.add(complete); |
3457 |
> |
srcs.add(incomplete); |
3458 |
|
|
3459 |
< |
f = new CompletableFuture<Integer>(); |
3460 |
< |
g = f.thenComposeAsync(r = new FailingCompletableFutureFunction()); |
3461 |
< |
f.complete(one); |
3462 |
< |
checkCompletedWithWrappedCFException(g); |
3459 |
> |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3460 |
> |
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3461 |
> |
fs.add(incomplete.thenAcceptAsync(z -> {}, e)); |
3462 |
> |
fs.add(incomplete.thenApplyAsync(z -> z, e)); |
3463 |
|
|
3464 |
< |
f = new CompletableFuture<Integer>(); |
3465 |
< |
f.complete(one); |
3466 |
< |
g = f.thenComposeAsync(r = new FailingCompletableFutureFunction()); |
1882 |
< |
checkCompletedWithWrappedCFException(g); |
1883 |
< |
} |
3464 |
> |
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3465 |
> |
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3466 |
> |
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3467 |
|
|
3468 |
< |
/** |
3469 |
< |
* thenComposeAsync result completes exceptionally if source cancelled |
3470 |
< |
*/ |
1888 |
< |
public void testThenComposeAsync4() { |
1889 |
< |
CompletableFuture<Integer> f, g; |
1890 |
< |
CompletableFutureInc r; |
3468 |
> |
fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e)); |
3469 |
> |
fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e)); |
3470 |
> |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3471 |
|
|
3472 |
< |
f = new CompletableFuture<Integer>(); |
3473 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
3474 |
< |
assertTrue(f.cancel(true)); |
1895 |
< |
checkCompletedWithWrappedCancellationException(g); |
3472 |
> |
fs.add(incomplete.thenComposeAsync(z -> null, e)); |
3473 |
> |
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3474 |
> |
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3475 |
|
|
3476 |
< |
f = new CompletableFuture<Integer>(); |
3477 |
< |
assertTrue(f.cancel(true)); |
1899 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1900 |
< |
checkCompletedWithWrappedCancellationException(g); |
1901 |
< |
} |
3476 |
> |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3477 |
> |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3478 |
|
|
3479 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3480 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3481 |
|
|
3482 |
< |
// async with explicit executors |
3482 |
> |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3483 |
> |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3484 |
|
|
3485 |
< |
/** |
3486 |
< |
* thenRunAsync result completes normally after normal completion of source |
1908 |
< |
*/ |
1909 |
< |
public void testThenRunAsyncE() { |
1910 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1911 |
< |
Noop r = new Noop(); |
1912 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
1913 |
< |
f.complete(null); |
1914 |
< |
checkCompletedNormally(g, null); |
3485 |
> |
for (CompletableFuture<?> future : fs) |
3486 |
> |
checkIncomplete(future); |
3487 |
|
|
3488 |
< |
// reordered version |
3489 |
< |
f = new CompletableFuture<Integer>(); |
1918 |
< |
f.complete(null); |
1919 |
< |
r = new Noop(); |
1920 |
< |
g = f.thenRunAsync(r, new ThreadExecutor()); |
1921 |
< |
checkCompletedNormally(g, null); |
1922 |
< |
} |
3488 |
> |
for (CompletableFuture<?> future : fs) |
3489 |
> |
future.complete(null); |
3490 |
|
|
3491 |
< |
/** |
1925 |
< |
* thenRunAsync result completes exceptionally after exceptional |
1926 |
< |
* completion of source |
1927 |
< |
*/ |
1928 |
< |
public void testThenRunAsync2E() { |
1929 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1930 |
< |
Noop r = new Noop(); |
1931 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
1932 |
< |
f.completeExceptionally(new CFException()); |
1933 |
< |
try { |
1934 |
< |
g.join(); |
1935 |
< |
shouldThrow(); |
1936 |
< |
} catch (Exception ok) { |
1937 |
< |
} |
1938 |
< |
checkCompletedWithWrappedCFException(g); |
1939 |
< |
} |
3491 |
> |
incomplete.complete(v); |
3492 |
|
|
3493 |
< |
/** |
3494 |
< |
* thenRunAsync result completes exceptionally if action does |
1943 |
< |
*/ |
1944 |
< |
public void testThenRunAsync3E() { |
1945 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1946 |
< |
FailingNoop r = new FailingNoop(); |
1947 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
1948 |
< |
f.complete(null); |
1949 |
< |
checkCompletedWithWrappedCFException(g); |
1950 |
< |
} |
3493 |
> |
for (CompletableFuture<?> future : fs) |
3494 |
> |
checkCompletedNormally(future, null); |
3495 |
|
|
3496 |
< |
/** |
3497 |
< |
* thenRunAsync result completes exceptionally if source cancelled |
1954 |
< |
*/ |
1955 |
< |
public void testThenRunAsync4E() { |
1956 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1957 |
< |
Noop r = new Noop(); |
1958 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
1959 |
< |
assertTrue(f.cancel(true)); |
1960 |
< |
checkCompletedWithWrappedCancellationException(g); |
1961 |
< |
} |
3496 |
> |
assertEquals(0, e.count.get()); |
3497 |
> |
}} |
3498 |
|
|
3499 |
|
/** |
3500 |
< |
* thenApplyAsync result completes normally after normal completion of source |
3500 |
> |
* toCompletableFuture returns this CompletableFuture. |
3501 |
|
*/ |
3502 |
< |
public void testThenApplyAsyncE() { |
3503 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3504 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
1969 |
< |
f.complete(one); |
1970 |
< |
checkCompletedNormally(g, two); |
3502 |
> |
public void testToCompletableFuture() { |
3503 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3504 |
> |
assertSame(f, f.toCompletableFuture()); |
3505 |
|
} |
3506 |
|
|
3507 |
< |
/** |
1974 |
< |
* thenApplyAsync result completes exceptionally after exceptional |
1975 |
< |
* completion of source |
1976 |
< |
*/ |
1977 |
< |
public void testThenApplyAsync2E() { |
1978 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1979 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
1980 |
< |
f.completeExceptionally(new CFException()); |
1981 |
< |
checkCompletedWithWrappedCFException(g); |
1982 |
< |
} |
3507 |
> |
// jdk9 |
3508 |
|
|
3509 |
|
/** |
3510 |
< |
* thenApplyAsync result completes exceptionally if action does |
3510 |
> |
* newIncompleteFuture returns an incomplete CompletableFuture |
3511 |
|
*/ |
3512 |
< |
public void testThenApplyAsync3E() { |
3513 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3514 |
< |
FailingFunction r = new FailingFunction(); |
3515 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor()); |
3516 |
< |
f.complete(null); |
3517 |
< |
checkCompletedWithWrappedCFException(g); |
3518 |
< |
} |
3512 |
> |
public void testNewIncompleteFuture() { |
3513 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3514 |
> |
{ |
3515 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3516 |
> |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3517 |
> |
checkIncomplete(f); |
3518 |
> |
checkIncomplete(g); |
3519 |
> |
f.complete(v1); |
3520 |
> |
checkCompletedNormally(f, v1); |
3521 |
> |
checkIncomplete(g); |
3522 |
> |
g.complete(v1); |
3523 |
> |
checkCompletedNormally(g, v1); |
3524 |
> |
assertSame(g.getClass(), CompletableFuture.class); |
3525 |
> |
}} |
3526 |
|
|
3527 |
|
/** |
3528 |
< |
* thenApplyAsync result completes exceptionally if source cancelled |
3528 |
> |
* completedStage returns a completed CompletionStage |
3529 |
|
*/ |
3530 |
< |
public void testThenApplyAsync4E() { |
3531 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3532 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
3533 |
< |
assertTrue(f.cancel(true)); |
3534 |
< |
checkCompletedWithWrappedCancellationException(g); |
3530 |
> |
public void testCompletedStage() { |
3531 |
> |
AtomicInteger x = new AtomicInteger(0); |
3532 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3533 |
> |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3534 |
> |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3535 |
> |
assertEquals(x.get(), 1); |
3536 |
> |
assertNull(r.get()); |
3537 |
|
} |
3538 |
|
|
3539 |
|
/** |
3540 |
< |
* thenAcceptAsync result completes normally after normal |
3541 |
< |
* completion of source |
3540 |
> |
* defaultExecutor by default returns the commonPool if |
3541 |
> |
* it supports more than one thread. |
3542 |
|
*/ |
3543 |
< |
public void testThenAcceptAsyncE() { |
3544 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3545 |
< |
IncAction r = new IncAction(); |
3546 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
3547 |
< |
f.complete(one); |
3548 |
< |
checkCompletedNormally(g, null); |
3549 |
< |
assertEquals(r.value, 2); |
3543 |
> |
public void testDefaultExecutor() { |
3544 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3545 |
> |
Executor e = f.defaultExecutor(); |
3546 |
> |
Executor c = ForkJoinPool.commonPool(); |
3547 |
> |
if (ForkJoinPool.getCommonPoolParallelism() > 1) |
3548 |
> |
assertSame(e, c); |
3549 |
> |
else |
3550 |
> |
assertNotSame(e, c); |
3551 |
|
} |
3552 |
|
|
3553 |
|
/** |
3554 |
< |
* thenAcceptAsync result completes exceptionally after exceptional |
3555 |
< |
* completion of source |
3554 |
> |
* failedFuture returns a CompletableFuture completed |
3555 |
> |
* exceptionally with the given Exception |
3556 |
|
*/ |
3557 |
< |
public void testThenAcceptAsync2E() { |
3558 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3559 |
< |
IncAction r = new IncAction(); |
3560 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
2026 |
< |
f.completeExceptionally(new CFException()); |
2027 |
< |
checkCompletedWithWrappedCFException(g); |
3557 |
> |
public void testFailedFuture() { |
3558 |
> |
CFException ex = new CFException(); |
3559 |
> |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3560 |
> |
checkCompletedExceptionally(f, ex); |
3561 |
|
} |
3562 |
|
|
3563 |
|
/** |
3564 |
< |
* thenAcceptAsync result completes exceptionally if action does |
3564 |
> |
* failedFuture(null) throws NPE |
3565 |
|
*/ |
3566 |
< |
public void testThenAcceptAsync3E() { |
3567 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3568 |
< |
FailingConsumer r = new FailingConsumer(); |
3569 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
3570 |
< |
f.complete(null); |
2038 |
< |
checkCompletedWithWrappedCFException(g); |
3566 |
> |
public void testFailedFuture_null() { |
3567 |
> |
try { |
3568 |
> |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3569 |
> |
shouldThrow(); |
3570 |
> |
} catch (NullPointerException success) {} |
3571 |
|
} |
3572 |
|
|
3573 |
|
/** |
3574 |
< |
* thenAcceptAsync result completes exceptionally if source cancelled |
3574 |
> |
* copy returns a CompletableFuture that is completed normally, |
3575 |
> |
* with the same value, when source is. |
3576 |
|
*/ |
3577 |
< |
public void testThenAcceptAsync4E() { |
3578 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3579 |
< |
IncAction r = new IncAction(); |
3580 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
3581 |
< |
assertTrue(f.cancel(true)); |
3582 |
< |
checkCompletedWithWrappedCancellationException(g); |
3583 |
< |
} |
3577 |
> |
public void testCopy_normalCompletion() { |
3578 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3579 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3580 |
> |
{ |
3581 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3582 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
3583 |
> |
CompletableFuture<Integer> g = f.copy(); |
3584 |
> |
if (createIncomplete) { |
3585 |
> |
checkIncomplete(f); |
3586 |
> |
checkIncomplete(g); |
3587 |
> |
assertTrue(f.complete(v1)); |
3588 |
> |
} |
3589 |
> |
checkCompletedNormally(f, v1); |
3590 |
> |
checkCompletedNormally(g, v1); |
3591 |
> |
}} |
3592 |
> |
|
3593 |
> |
/** |
3594 |
> |
* copy returns a CompletableFuture that is completed exceptionally |
3595 |
> |
* when source is. |
3596 |
> |
*/ |
3597 |
> |
public void testCopy_exceptionalCompletion() { |
3598 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3599 |
> |
{ |
3600 |
> |
CFException ex = new CFException(); |
3601 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3602 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
3603 |
> |
CompletableFuture<Integer> g = f.copy(); |
3604 |
> |
if (createIncomplete) { |
3605 |
> |
checkIncomplete(f); |
3606 |
> |
checkIncomplete(g); |
3607 |
> |
f.completeExceptionally(ex); |
3608 |
> |
} |
3609 |
> |
checkCompletedExceptionally(f, ex); |
3610 |
> |
checkCompletedWithWrappedException(g, ex); |
3611 |
> |
}} |
3612 |
|
|
3613 |
|
/** |
3614 |
< |
* thenCombineAsync result completes normally after normal |
3615 |
< |
* completion of sources |
3616 |
< |
*/ |
3617 |
< |
public void testThenCombineAsyncE() { |
3618 |
< |
CompletableFuture<Integer> f, g, h; |
3619 |
< |
ThreadExecutor e = new ThreadExecutor(); |
3620 |
< |
int count = 0; |
3621 |
< |
|
3622 |
< |
f = new CompletableFuture<Integer>(); |
3623 |
< |
g = new CompletableFuture<Integer>(); |
2063 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2064 |
< |
f.complete(3); |
2065 |
< |
checkIncomplete(h); |
2066 |
< |
g.complete(1); |
2067 |
< |
checkCompletedNormally(h, 2); |
2068 |
< |
assertEquals(++count, e.count.get()); |
2069 |
< |
|
2070 |
< |
f = new CompletableFuture<Integer>(); |
2071 |
< |
g = new CompletableFuture<Integer>(); |
2072 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2073 |
< |
g.complete(1); |
2074 |
< |
checkIncomplete(h); |
2075 |
< |
f.complete(3); |
2076 |
< |
checkCompletedNormally(h, 2); |
2077 |
< |
assertEquals(++count, e.count.get()); |
2078 |
< |
|
2079 |
< |
f = new CompletableFuture<Integer>(); |
2080 |
< |
g = new CompletableFuture<Integer>(); |
2081 |
< |
g.complete(1); |
2082 |
< |
f.complete(3); |
2083 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2084 |
< |
checkCompletedNormally(h, 2); |
2085 |
< |
assertEquals(++count, e.count.get()); |
3614 |
> |
* Completion of a copy does not complete its source. |
3615 |
> |
*/ |
3616 |
> |
public void testCopy_oneWayPropagation() { |
3617 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3618 |
> |
assertTrue(f.copy().complete(1)); |
3619 |
> |
assertTrue(f.copy().complete(null)); |
3620 |
> |
assertTrue(f.copy().cancel(true)); |
3621 |
> |
assertTrue(f.copy().cancel(false)); |
3622 |
> |
assertTrue(f.copy().completeExceptionally(new CFException())); |
3623 |
> |
checkIncomplete(f); |
3624 |
|
} |
3625 |
|
|
3626 |
|
/** |
3627 |
< |
* thenCombineAsync result completes exceptionally after exceptional |
3628 |
< |
* completion of either source |
3627 |
> |
* minimalCompletionStage returns a CompletableFuture that is |
3628 |
> |
* completed normally, with the same value, when source is. |
3629 |
|
*/ |
3630 |
< |
public void testThenCombineAsync2E() { |
3631 |
< |
CompletableFuture<Integer> f, g, h; |
3632 |
< |
ThreadExecutor e = new ThreadExecutor(); |
3633 |
< |
int count = 0; |
3634 |
< |
|
3635 |
< |
f = new CompletableFuture<Integer>(); |
3636 |
< |
g = new CompletableFuture<Integer>(); |
3637 |
< |
h = f.thenCombineAsync(g, subtract, e); |
3638 |
< |
f.completeExceptionally(new CFException()); |
3639 |
< |
checkIncomplete(h); |
3640 |
< |
g.complete(1); |
2103 |
< |
checkCompletedWithWrappedCFException(h); |
2104 |
< |
|
2105 |
< |
f = new CompletableFuture<Integer>(); |
2106 |
< |
g = new CompletableFuture<Integer>(); |
2107 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2108 |
< |
g.completeExceptionally(new CFException()); |
2109 |
< |
checkIncomplete(h); |
2110 |
< |
f.complete(3); |
2111 |
< |
checkCompletedWithWrappedCFException(h); |
2112 |
< |
|
2113 |
< |
f = new CompletableFuture<Integer>(); |
2114 |
< |
g = new CompletableFuture<Integer>(); |
2115 |
< |
g.completeExceptionally(new CFException()); |
2116 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2117 |
< |
checkIncomplete(h); |
2118 |
< |
f.complete(3); |
2119 |
< |
checkCompletedWithWrappedCFException(h); |
2120 |
< |
|
2121 |
< |
assertEquals(0, e.count.get()); |
3630 |
> |
public void testMinimalCompletionStage() { |
3631 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3632 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3633 |
> |
AtomicInteger x = new AtomicInteger(0); |
3634 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3635 |
> |
checkIncomplete(f); |
3636 |
> |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3637 |
> |
f.complete(1); |
3638 |
> |
checkCompletedNormally(f, 1); |
3639 |
> |
assertEquals(x.get(), 1); |
3640 |
> |
assertNull(r.get()); |
3641 |
|
} |
3642 |
|
|
3643 |
|
/** |
3644 |
< |
* thenCombineAsync result completes exceptionally if action does |
3644 |
> |
* minimalCompletionStage returns a CompletableFuture that is |
3645 |
> |
* completed exceptionally when source is. |
3646 |
|
*/ |
3647 |
< |
public void testThenCombineAsync3E() { |
3648 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3649 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3650 |
< |
FailingBiFunction r = new FailingBiFunction(); |
3651 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor()); |
3652 |
< |
f.complete(one); |
3653 |
< |
checkIncomplete(g); |
3654 |
< |
assertFalse(r.ran); |
3655 |
< |
f2.complete(two); |
3656 |
< |
checkCompletedWithWrappedCFException(g); |
3657 |
< |
assertTrue(r.ran); |
3658 |
< |
} |
2139 |
< |
|
2140 |
< |
/** |
2141 |
< |
* thenCombineAsync result completes exceptionally if either source cancelled |
2142 |
< |
*/ |
2143 |
< |
public void testThenCombineAsync4E() { |
2144 |
< |
CompletableFuture<Integer> f, g, h; |
2145 |
< |
ThreadExecutor e = new ThreadExecutor(); |
2146 |
< |
|
2147 |
< |
f = new CompletableFuture<Integer>(); |
2148 |
< |
g = new CompletableFuture<Integer>(); |
2149 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2150 |
< |
assertTrue(f.cancel(true)); |
2151 |
< |
checkIncomplete(h); |
2152 |
< |
g.complete(1); |
2153 |
< |
checkCompletedWithWrappedCancellationException(h); |
2154 |
< |
|
2155 |
< |
f = new CompletableFuture<Integer>(); |
2156 |
< |
g = new CompletableFuture<Integer>(); |
2157 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2158 |
< |
assertTrue(g.cancel(true)); |
2159 |
< |
checkIncomplete(h); |
2160 |
< |
f.complete(3); |
2161 |
< |
checkCompletedWithWrappedCancellationException(h); |
2162 |
< |
|
2163 |
< |
f = new CompletableFuture<Integer>(); |
2164 |
< |
g = new CompletableFuture<Integer>(); |
2165 |
< |
assertTrue(g.cancel(true)); |
2166 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2167 |
< |
checkIncomplete(h); |
2168 |
< |
f.complete(3); |
2169 |
< |
checkCompletedWithWrappedCancellationException(h); |
2170 |
< |
|
2171 |
< |
f = new CompletableFuture<Integer>(); |
2172 |
< |
g = new CompletableFuture<Integer>(); |
2173 |
< |
assertTrue(f.cancel(true)); |
2174 |
< |
assertTrue(g.cancel(true)); |
2175 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2176 |
< |
checkCompletedWithWrappedCancellationException(h); |
2177 |
< |
|
2178 |
< |
assertEquals(0, e.count.get()); |
3647 |
> |
public void testMinimalCompletionStage2() { |
3648 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3649 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3650 |
> |
AtomicInteger x = new AtomicInteger(0); |
3651 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3652 |
> |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3653 |
> |
checkIncomplete(f); |
3654 |
> |
CFException ex = new CFException(); |
3655 |
> |
f.completeExceptionally(ex); |
3656 |
> |
checkCompletedExceptionally(f, ex); |
3657 |
> |
assertEquals(x.get(), 0); |
3658 |
> |
assertEquals(r.get().getCause(), ex); |
3659 |
|
} |
3660 |
|
|
3661 |
|
/** |
3662 |
< |
* thenAcceptBothAsync result completes normally after normal |
3663 |
< |
* completion of sources |
3662 |
> |
* failedStage returns a CompletionStage completed |
3663 |
> |
* exceptionally with the given Exception |
3664 |
|
*/ |
3665 |
< |
public void testThenAcceptBothAsyncE() { |
3666 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3667 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3668 |
< |
AddAction r = new AddAction(); |
3669 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
3670 |
< |
f.complete(one); |
3671 |
< |
checkIncomplete(g); |
3672 |
< |
f2.complete(two); |
2193 |
< |
checkCompletedNormally(g, null); |
2194 |
< |
assertEquals(r.value, 3); |
3665 |
> |
public void testFailedStage() { |
3666 |
> |
CFException ex = new CFException(); |
3667 |
> |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3668 |
> |
AtomicInteger x = new AtomicInteger(0); |
3669 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3670 |
> |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3671 |
> |
assertEquals(x.get(), 0); |
3672 |
> |
assertEquals(r.get(), ex); |
3673 |
|
} |
3674 |
|
|
3675 |
|
/** |
3676 |
< |
* thenAcceptBothAsync result completes exceptionally after exceptional |
2199 |
< |
* completion of source |
3676 |
> |
* completeAsync completes with value of given supplier |
3677 |
|
*/ |
3678 |
< |
public void testThenAcceptBothAsync2E() { |
3679 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3680 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3681 |
< |
AddAction r = new AddAction(); |
3682 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
3683 |
< |
f.completeExceptionally(new CFException()); |
3684 |
< |
f2.complete(two); |
3685 |
< |
checkCompletedWithWrappedCFException(g); |
2209 |
< |
|
2210 |
< |
r = new AddAction(); |
2211 |
< |
f = new CompletableFuture<Integer>(); |
2212 |
< |
f2 = new CompletableFuture<Integer>(); |
2213 |
< |
g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
2214 |
< |
f.complete(one); |
2215 |
< |
f2.completeExceptionally(new CFException()); |
2216 |
< |
checkCompletedWithWrappedCFException(g); |
2217 |
< |
} |
3678 |
> |
public void testCompleteAsync() { |
3679 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3680 |
> |
{ |
3681 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3682 |
> |
f.completeAsync(() -> v1); |
3683 |
> |
f.join(); |
3684 |
> |
checkCompletedNormally(f, v1); |
3685 |
> |
}} |
3686 |
|
|
3687 |
|
/** |
3688 |
< |
* thenAcceptBothAsync result completes exceptionally if action does |
2221 |
< |
*/ |
2222 |
< |
public void testThenAcceptBothAsync3E() { |
2223 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2224 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2225 |
< |
FailingBiConsumer r = new FailingBiConsumer(); |
2226 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
2227 |
< |
f.complete(one); |
2228 |
< |
checkIncomplete(g); |
2229 |
< |
f2.complete(two); |
2230 |
< |
checkCompletedWithWrappedCFException(g); |
2231 |
< |
} |
2232 |
< |
|
2233 |
< |
/** |
2234 |
< |
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
3688 |
> |
* completeAsync completes exceptionally if given supplier throws |
3689 |
|
*/ |
3690 |
< |
public void testThenAcceptBothAsync4E() { |
3691 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3692 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3693 |
< |
AddAction r = new AddAction(); |
3694 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
3695 |
< |
assertTrue(f.cancel(true)); |
3696 |
< |
f2.complete(two); |
3697 |
< |
checkCompletedWithWrappedCancellationException(g); |
3698 |
< |
|
2245 |
< |
r = new AddAction(); |
2246 |
< |
f = new CompletableFuture<Integer>(); |
2247 |
< |
f2 = new CompletableFuture<Integer>(); |
2248 |
< |
g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
2249 |
< |
f.complete(one); |
2250 |
< |
assertTrue(f2.cancel(true)); |
2251 |
< |
checkCompletedWithWrappedCancellationException(g); |
3690 |
> |
public void testCompleteAsync2() { |
3691 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3692 |
> |
CFException ex = new CFException(); |
3693 |
> |
f.completeAsync(() -> { throw ex; }); |
3694 |
> |
try { |
3695 |
> |
f.join(); |
3696 |
> |
shouldThrow(); |
3697 |
> |
} catch (CompletionException success) {} |
3698 |
> |
checkCompletedWithWrappedException(f, ex); |
3699 |
|
} |
3700 |
|
|
3701 |
|
/** |
3702 |
< |
* runAfterBothAsync result completes normally after normal |
2256 |
< |
* completion of sources |
3702 |
> |
* completeAsync with given executor completes with value of given supplier |
3703 |
|
*/ |
3704 |
< |
public void testRunAfterBothAsyncE() { |
3705 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3706 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3707 |
< |
Noop r = new Noop(); |
3708 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
3709 |
< |
f.complete(one); |
3710 |
< |
checkIncomplete(g); |
3711 |
< |
f2.complete(two); |
3712 |
< |
checkCompletedNormally(g, null); |
3713 |
< |
assertTrue(r.ran); |
2268 |
< |
} |
3704 |
> |
public void testCompleteAsync3() { |
3705 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3706 |
> |
{ |
3707 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3708 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3709 |
> |
f.completeAsync(() -> v1, executor); |
3710 |
> |
assertSame(v1, f.join()); |
3711 |
> |
checkCompletedNormally(f, v1); |
3712 |
> |
assertEquals(1, executor.count.get()); |
3713 |
> |
}} |
3714 |
|
|
3715 |
|
/** |
3716 |
< |
* runAfterBothAsync result completes exceptionally after exceptional |
3717 |
< |
* completion of source |
3716 |
> |
* completeAsync with given executor completes exceptionally if |
3717 |
> |
* given supplier throws |
3718 |
|
*/ |
3719 |
< |
public void testRunAfterBothAsync2E() { |
3720 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3721 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3722 |
< |
Noop r = new Noop(); |
3723 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
3724 |
< |
f.completeExceptionally(new CFException()); |
3725 |
< |
f2.complete(two); |
3726 |
< |
checkCompletedWithWrappedCFException(g); |
3719 |
> |
public void testCompleteAsync4() { |
3720 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3721 |
> |
CFException ex = new CFException(); |
3722 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3723 |
> |
f.completeAsync(() -> { throw ex; }, executor); |
3724 |
> |
try { |
3725 |
> |
f.join(); |
3726 |
> |
shouldThrow(); |
3727 |
> |
} catch (CompletionException success) {} |
3728 |
> |
checkCompletedWithWrappedException(f, ex); |
3729 |
> |
assertEquals(1, executor.count.get()); |
3730 |
> |
} |
3731 |
> |
|
3732 |
> |
/** |
3733 |
> |
* orTimeout completes with TimeoutException if not complete |
3734 |
> |
*/ |
3735 |
> |
public void testOrTimeout_timesOut() { |
3736 |
> |
long timeoutMillis = timeoutMillis(); |
3737 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3738 |
> |
long startTime = System.nanoTime(); |
3739 |
> |
assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); |
3740 |
> |
checkCompletedWithTimeoutException(f); |
3741 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3742 |
> |
} |
3743 |
> |
|
3744 |
> |
/** |
3745 |
> |
* orTimeout completes normally if completed before timeout |
3746 |
> |
*/ |
3747 |
> |
public void testOrTimeout_completed() { |
3748 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3749 |
> |
{ |
3750 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3751 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3752 |
> |
long startTime = System.nanoTime(); |
3753 |
> |
f.complete(v1); |
3754 |
> |
assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3755 |
> |
assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3756 |
> |
g.complete(v1); |
3757 |
> |
checkCompletedNormally(f, v1); |
3758 |
> |
checkCompletedNormally(g, v1); |
3759 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3760 |
> |
}} |
3761 |
> |
|
3762 |
> |
/** |
3763 |
> |
* completeOnTimeout completes with given value if not complete |
3764 |
> |
*/ |
3765 |
> |
public void testCompleteOnTimeout_timesOut() { |
3766 |
> |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3767 |
> |
() -> testCompleteOnTimeout_timesOut(null)); |
3768 |
> |
} |
3769 |
> |
|
3770 |
> |
/** |
3771 |
> |
* completeOnTimeout completes with given value if not complete |
3772 |
> |
*/ |
3773 |
> |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3774 |
> |
long timeoutMillis = timeoutMillis(); |
3775 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3776 |
> |
long startTime = System.nanoTime(); |
3777 |
> |
assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); |
3778 |
> |
assertSame(v, f.join()); |
3779 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3780 |
> |
f.complete(99); // should have no effect |
3781 |
> |
checkCompletedNormally(f, v); |
3782 |
> |
} |
3783 |
> |
|
3784 |
> |
/** |
3785 |
> |
* completeOnTimeout has no effect if completed within timeout |
3786 |
> |
*/ |
3787 |
> |
public void testCompleteOnTimeout_completed() { |
3788 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3789 |
> |
{ |
3790 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3791 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3792 |
> |
long startTime = System.nanoTime(); |
3793 |
> |
f.complete(v1); |
3794 |
> |
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3795 |
> |
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3796 |
> |
g.complete(v1); |
3797 |
> |
checkCompletedNormally(f, v1); |
3798 |
> |
checkCompletedNormally(g, v1); |
3799 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3800 |
> |
}} |
3801 |
> |
|
3802 |
> |
/** |
3803 |
> |
* delayedExecutor returns an executor that delays submission |
3804 |
> |
*/ |
3805 |
> |
public void testDelayedExecutor() { |
3806 |
> |
testInParallel(() -> testDelayedExecutor(null, null), |
3807 |
> |
() -> testDelayedExecutor(null, 1), |
3808 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3809 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3810 |
> |
} |
3811 |
> |
|
3812 |
> |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3813 |
> |
long timeoutMillis = timeoutMillis(); |
3814 |
> |
// Use an "unreasonably long" long timeout to catch lingering threads |
3815 |
> |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3816 |
> |
final Executor delayer, longDelayer; |
3817 |
> |
if (executor == null) { |
3818 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3819 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3820 |
> |
} else { |
3821 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3822 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3823 |
> |
} |
3824 |
> |
long startTime = System.nanoTime(); |
3825 |
> |
CompletableFuture<Integer> f = |
3826 |
> |
CompletableFuture.supplyAsync(() -> v, delayer); |
3827 |
> |
CompletableFuture<Integer> g = |
3828 |
> |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3829 |
> |
|
3830 |
> |
assertNull(g.getNow(null)); |
3831 |
> |
|
3832 |
> |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3833 |
> |
long millisElapsed = millisElapsedSince(startTime); |
3834 |
> |
assertTrue(millisElapsed >= timeoutMillis); |
3835 |
> |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3836 |
|
|
3837 |
< |
r = new Noop(); |
2284 |
< |
f = new CompletableFuture<Integer>(); |
2285 |
< |
f2 = new CompletableFuture<Integer>(); |
2286 |
< |
g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2287 |
< |
f.complete(one); |
2288 |
< |
f2.completeExceptionally(new CFException()); |
2289 |
< |
checkCompletedWithWrappedCFException(g); |
2290 |
< |
} |
3837 |
> |
checkCompletedNormally(f, v); |
3838 |
|
|
2292 |
– |
/** |
2293 |
– |
* runAfterBothAsync result completes exceptionally if action does |
2294 |
– |
*/ |
2295 |
– |
public void testRunAfterBothAsync3E() { |
2296 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2297 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2298 |
– |
FailingNoop r = new FailingNoop(); |
2299 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2300 |
– |
f.complete(one); |
3839 |
|
checkIncomplete(g); |
3840 |
< |
f2.complete(two); |
2303 |
< |
checkCompletedWithWrappedCFException(g); |
2304 |
< |
} |
2305 |
< |
|
2306 |
< |
/** |
2307 |
< |
* runAfterBothAsync result completes exceptionally if either source cancelled |
2308 |
< |
*/ |
2309 |
< |
public void testRunAfterBothAsync4E() { |
2310 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2311 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2312 |
< |
Noop r = new Noop(); |
2313 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2314 |
< |
assertTrue(f.cancel(true)); |
2315 |
< |
f2.complete(two); |
2316 |
< |
checkCompletedWithWrappedCancellationException(g); |
2317 |
< |
|
2318 |
< |
r = new Noop(); |
2319 |
< |
f = new CompletableFuture<Integer>(); |
2320 |
< |
f2 = new CompletableFuture<Integer>(); |
2321 |
< |
g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2322 |
< |
f.complete(one); |
2323 |
< |
assertTrue(f2.cancel(true)); |
2324 |
< |
checkCompletedWithWrappedCancellationException(g); |
2325 |
< |
} |
2326 |
< |
|
2327 |
< |
/** |
2328 |
< |
* applyToEitherAsync result completes normally after normal |
2329 |
< |
* completion of sources |
2330 |
< |
*/ |
2331 |
< |
public void testApplyToEitherAsyncE() { |
2332 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2333 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2334 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2335 |
< |
f.complete(one); |
2336 |
< |
checkCompletedNormally(g, two); |
2337 |
< |
|
2338 |
< |
f = new CompletableFuture<Integer>(); |
2339 |
< |
f.complete(one); |
2340 |
< |
f2 = new CompletableFuture<Integer>(); |
2341 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2342 |
< |
checkCompletedNormally(g, two); |
2343 |
< |
} |
2344 |
< |
|
2345 |
< |
/** |
2346 |
< |
* applyToEitherAsync result completes exceptionally after exceptional |
2347 |
< |
* completion of source |
2348 |
< |
*/ |
2349 |
< |
public void testApplyToEitherAsync2E() { |
2350 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2351 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2352 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2353 |
< |
f.completeExceptionally(new CFException()); |
2354 |
< |
checkCompletedWithWrappedCFException(g); |
2355 |
< |
|
2356 |
< |
f = new CompletableFuture<Integer>(); |
2357 |
< |
f2 = new CompletableFuture<Integer>(); |
2358 |
< |
f2.completeExceptionally(new CFException()); |
2359 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2360 |
< |
f.complete(one); |
2361 |
< |
checkCompletedWithWrappedCFException(g); |
3840 |
> |
assertTrue(g.cancel(true)); |
3841 |
|
} |
3842 |
|
|
3843 |
< |
/** |
2365 |
< |
* applyToEitherAsync result completes exceptionally if action does |
2366 |
< |
*/ |
2367 |
< |
public void testApplyToEitherAsync3E() { |
2368 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2369 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2370 |
< |
FailingFunction r = new FailingFunction(); |
2371 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor()); |
2372 |
< |
f.complete(one); |
2373 |
< |
checkCompletedWithWrappedCFException(g); |
2374 |
< |
} |
3843 |
> |
//--- tests of implementation details; not part of official tck --- |
3844 |
|
|
3845 |
< |
/** |
3846 |
< |
* applyToEitherAsync result completes exceptionally if either source cancelled |
3847 |
< |
*/ |
3848 |
< |
public void testApplyToEitherAsync4E() { |
3849 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3850 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3851 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
3852 |
< |
assertTrue(f.cancel(true)); |
3853 |
< |
checkCompletedWithWrappedCancellationException(g); |
3845 |
> |
Object resultOf(CompletableFuture<?> f) { |
3846 |
> |
SecurityManager sm = System.getSecurityManager(); |
3847 |
> |
if (sm != null) { |
3848 |
> |
try { |
3849 |
> |
System.setSecurityManager(null); |
3850 |
> |
} catch (SecurityException giveUp) { |
3851 |
> |
return "Reflection not available"; |
3852 |
> |
} |
3853 |
> |
} |
3854 |
|
|
3855 |
< |
f = new CompletableFuture<Integer>(); |
3856 |
< |
f2 = new CompletableFuture<Integer>(); |
3857 |
< |
assertTrue(f2.cancel(true)); |
3858 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
3859 |
< |
checkCompletedWithWrappedCancellationException(g); |
3855 |
> |
try { |
3856 |
> |
java.lang.reflect.Field resultField |
3857 |
> |
= CompletableFuture.class.getDeclaredField("result"); |
3858 |
> |
resultField.setAccessible(true); |
3859 |
> |
return resultField.get(f); |
3860 |
> |
} catch (Throwable t) { |
3861 |
> |
throw new AssertionError(t); |
3862 |
> |
} finally { |
3863 |
> |
if (sm != null) System.setSecurityManager(sm); |
3864 |
> |
} |
3865 |
|
} |
3866 |
|
|
3867 |
< |
/** |
3868 |
< |
* acceptEitherAsync result completes normally after normal |
3869 |
< |
* completion of sources |
3870 |
< |
*/ |
3871 |
< |
public void testAcceptEitherAsyncE() { |
3872 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3873 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3874 |
< |
IncAction r = new IncAction(); |
3875 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
3876 |
< |
f.complete(one); |
3877 |
< |
checkCompletedNormally(g, null); |
3878 |
< |
assertEquals(r.value, 2); |
3867 |
> |
public void testExceptionPropagationReusesResultObject() { |
3868 |
> |
if (!testImplementationDetails) return; |
3869 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3870 |
> |
{ |
3871 |
> |
final CFException ex = new CFException(); |
3872 |
> |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3873 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3874 |
> |
|
3875 |
> |
final Runnable noopRunnable = new Noop(m); |
3876 |
> |
final Consumer<Integer> noopConsumer = new NoopConsumer(m); |
3877 |
> |
final Function<Integer, Integer> incFunction = new IncFunction(m); |
3878 |
> |
|
3879 |
> |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3880 |
> |
= new ArrayList<>(); |
3881 |
> |
|
3882 |
> |
funs.add(y -> m.thenRun(y, noopRunnable)); |
3883 |
> |
funs.add(y -> m.thenAccept(y, noopConsumer)); |
3884 |
> |
funs.add(y -> m.thenApply(y, incFunction)); |
3885 |
> |
|
3886 |
> |
funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable)); |
3887 |
> |
funs.add(y -> m.acceptEither(y, incomplete, noopConsumer)); |
3888 |
> |
funs.add(y -> m.applyToEither(y, incomplete, incFunction)); |
3889 |
> |
|
3890 |
> |
funs.add(y -> m.runAfterBoth(y, v42, noopRunnable)); |
3891 |
> |
funs.add(y -> m.runAfterBoth(v42, y, noopRunnable)); |
3892 |
> |
funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3893 |
> |
funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3894 |
> |
funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3895 |
> |
funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3896 |
> |
|
3897 |
> |
funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3898 |
> |
|
3899 |
> |
funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m))); |
3900 |
> |
|
3901 |
> |
funs.add(y -> CompletableFuture.allOf(y)); |
3902 |
> |
funs.add(y -> CompletableFuture.allOf(y, v42)); |
3903 |
> |
funs.add(y -> CompletableFuture.allOf(v42, y)); |
3904 |
> |
funs.add(y -> CompletableFuture.anyOf(y)); |
3905 |
> |
funs.add(y -> CompletableFuture.anyOf(y, incomplete)); |
3906 |
> |
funs.add(y -> CompletableFuture.anyOf(incomplete, y)); |
3907 |
> |
|
3908 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3909 |
> |
fun : funs) { |
3910 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3911 |
> |
f.completeExceptionally(ex); |
3912 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3913 |
> |
checkCompletedWithWrappedException(src, ex); |
3914 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3915 |
> |
checkCompletedWithWrappedException(dep, ex); |
3916 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3917 |
> |
} |
3918 |
|
|
3919 |
< |
r = new IncAction(); |
3920 |
< |
f = new CompletableFuture<Integer>(); |
3921 |
< |
f.complete(one); |
3922 |
< |
f2 = new CompletableFuture<Integer>(); |
3923 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
3924 |
< |
checkCompletedNormally(g, null); |
3925 |
< |
assertEquals(r.value, 2); |
3926 |
< |
} |
3919 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3920 |
> |
fun : funs) { |
3921 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3922 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3923 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3924 |
> |
f.completeExceptionally(ex); |
3925 |
> |
checkCompletedWithWrappedException(src, ex); |
3926 |
> |
checkCompletedWithWrappedException(dep, ex); |
3927 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3928 |
> |
} |
3929 |
|
|
3930 |
< |
/** |
3931 |
< |
* acceptEitherAsync result completes exceptionally after exceptional |
3932 |
< |
* completion of source |
3933 |
< |
*/ |
3934 |
< |
public void testAcceptEitherAsync2E() { |
3935 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3936 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3937 |
< |
IncAction r = new IncAction(); |
3938 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
3939 |
< |
f.completeExceptionally(new CFException()); |
3940 |
< |
checkCompletedWithWrappedCFException(g); |
3930 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3931 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3932 |
> |
fun : funs) { |
3933 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3934 |
> |
f.cancel(mayInterruptIfRunning); |
3935 |
> |
checkCancelled(f); |
3936 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3937 |
> |
checkCompletedWithWrappedCancellationException(src); |
3938 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3939 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3940 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3941 |
> |
} |
3942 |
|
|
3943 |
< |
r = new IncAction(); |
3944 |
< |
f = new CompletableFuture<Integer>(); |
3945 |
< |
f2 = new CompletableFuture<Integer>(); |
3946 |
< |
f2.completeExceptionally(new CFException()); |
3947 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
3948 |
< |
f.complete(one); |
3949 |
< |
checkCompletedWithWrappedCFException(g); |
3950 |
< |
} |
3943 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3944 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3945 |
> |
fun : funs) { |
3946 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3947 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3948 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3949 |
> |
f.cancel(mayInterruptIfRunning); |
3950 |
> |
checkCancelled(f); |
3951 |
> |
checkCompletedWithWrappedCancellationException(src); |
3952 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3953 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3954 |
> |
} |
3955 |
> |
}} |
3956 |
|
|
3957 |
|
/** |
3958 |
< |
* acceptEitherAsync result completes exceptionally if action does |
3958 |
> |
* Minimal completion stages throw UOE for most non-CompletionStage methods |
3959 |
|
*/ |
3960 |
< |
public void testAcceptEitherAsync3E() { |
3961 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3962 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
3963 |
< |
FailingConsumer r = new FailingConsumer(); |
3964 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
3965 |
< |
f.complete(one); |
3966 |
< |
checkCompletedWithWrappedCFException(g); |
3960 |
> |
public void testMinimalCompletionStage_minimality() { |
3961 |
> |
if (!testImplementationDetails) return; |
3962 |
> |
Function<Method, String> toSignature = |
3963 |
> |
method -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3964 |
> |
Predicate<Method> isNotStatic = |
3965 |
> |
method -> (method.getModifiers() & Modifier.STATIC) == 0; |
3966 |
> |
List<Method> minimalMethods = |
3967 |
> |
Stream.of(Object.class, CompletionStage.class) |
3968 |
> |
.flatMap(klazz -> Stream.of(klazz.getMethods())) |
3969 |
> |
.filter(isNotStatic) |
3970 |
> |
.collect(Collectors.toList()); |
3971 |
> |
// Methods from CompletableFuture permitted NOT to throw UOE |
3972 |
> |
String[] signatureWhitelist = { |
3973 |
> |
"newIncompleteFuture[]", |
3974 |
> |
"defaultExecutor[]", |
3975 |
> |
"minimalCompletionStage[]", |
3976 |
> |
"copy[]", |
3977 |
> |
}; |
3978 |
> |
Set<String> permittedMethodSignatures = |
3979 |
> |
Stream.concat(minimalMethods.stream().map(toSignature), |
3980 |
> |
Stream.of(signatureWhitelist)) |
3981 |
> |
.collect(Collectors.toSet()); |
3982 |
> |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3983 |
> |
.filter(isNotStatic) |
3984 |
> |
.filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3985 |
> |
.collect(Collectors.toList()); |
3986 |
> |
|
3987 |
> |
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
3988 |
> |
CompletionStage<Integer> min = |
3989 |
> |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3990 |
> |
stages.add(min); |
3991 |
> |
stages.add(min.thenApply(x -> x)); |
3992 |
> |
stages.add(CompletableFuture.completedStage(1)); |
3993 |
> |
stages.add(CompletableFuture.failedStage(new CFException())); |
3994 |
> |
|
3995 |
> |
List<Method> bugs = new ArrayList<>(); |
3996 |
> |
for (Method method : allMethods) { |
3997 |
> |
Class<?>[] parameterTypes = method.getParameterTypes(); |
3998 |
> |
Object[] args = new Object[parameterTypes.length]; |
3999 |
> |
// Manufacture boxed primitives for primitive params |
4000 |
> |
for (int i = 0; i < args.length; i++) { |
4001 |
> |
Class<?> type = parameterTypes[i]; |
4002 |
> |
if (parameterTypes[i] == boolean.class) |
4003 |
> |
args[i] = false; |
4004 |
> |
else if (parameterTypes[i] == int.class) |
4005 |
> |
args[i] = 0; |
4006 |
> |
else if (parameterTypes[i] == long.class) |
4007 |
> |
args[i] = 0L; |
4008 |
> |
} |
4009 |
> |
for (CompletionStage<Integer> stage : stages) { |
4010 |
> |
try { |
4011 |
> |
method.invoke(stage, args); |
4012 |
> |
bugs.add(method); |
4013 |
> |
} |
4014 |
> |
catch (java.lang.reflect.InvocationTargetException expected) { |
4015 |
> |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
4016 |
> |
bugs.add(method); |
4017 |
> |
// expected.getCause().printStackTrace(); |
4018 |
> |
} |
4019 |
> |
} |
4020 |
> |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
4021 |
> |
} |
4022 |
> |
} |
4023 |
> |
if (!bugs.isEmpty()) |
4024 |
> |
throw new Error("Methods did not throw UOE: " + bugs); |
4025 |
|
} |
4026 |
|
|
4027 |
|
/** |
4028 |
< |
* acceptEitherAsync result completes exceptionally if either |
4029 |
< |
* source cancelled |
4028 |
> |
* minimalStage.toCompletableFuture() returns a CompletableFuture that |
4029 |
> |
* is completed normally, with the same value, when source is. |
4030 |
|
*/ |
4031 |
< |
public void testAcceptEitherAsync4E() { |
4032 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
4033 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
4034 |
< |
IncAction r = new IncAction(); |
4035 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
4036 |
< |
assertTrue(f.cancel(true)); |
4037 |
< |
checkCompletedWithWrappedCancellationException(g); |
4038 |
< |
|
4039 |
< |
r = new IncAction(); |
4040 |
< |
f = new CompletableFuture<Integer>(); |
4041 |
< |
f2 = new CompletableFuture<Integer>(); |
4042 |
< |
assertTrue(f2.cancel(true)); |
4043 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
4044 |
< |
checkCompletedWithWrappedCancellationException(g); |
4045 |
< |
} |
4031 |
> |
public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() { |
4032 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
4033 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
4034 |
> |
{ |
4035 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4036 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4037 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4038 |
> |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4039 |
> |
if (createIncomplete) { |
4040 |
> |
checkIncomplete(f); |
4041 |
> |
checkIncomplete(g); |
4042 |
> |
assertTrue(f.complete(v1)); |
4043 |
> |
} |
4044 |
> |
checkCompletedNormally(f, v1); |
4045 |
> |
checkCompletedNormally(g, v1); |
4046 |
> |
}} |
4047 |
> |
|
4048 |
> |
/** |
4049 |
> |
* minimalStage.toCompletableFuture() returns a CompletableFuture that |
4050 |
> |
* is completed exceptionally when source is. |
4051 |
> |
*/ |
4052 |
> |
public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() { |
4053 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
4054 |
> |
{ |
4055 |
> |
CFException ex = new CFException(); |
4056 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4057 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4058 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
4059 |
> |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4060 |
> |
if (createIncomplete) { |
4061 |
> |
checkIncomplete(f); |
4062 |
> |
checkIncomplete(g); |
4063 |
> |
f.completeExceptionally(ex); |
4064 |
> |
} |
4065 |
> |
checkCompletedExceptionally(f, ex); |
4066 |
> |
checkCompletedWithWrappedException(g, ex); |
4067 |
> |
}} |
4068 |
|
|
4069 |
|
/** |
4070 |
< |
* runAfterEitherAsync result completes normally after normal |
4071 |
< |
* completion of sources |
4072 |
< |
*/ |
4073 |
< |
public void testRunAfterEitherAsyncE() { |
4074 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
4075 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
4076 |
< |
Noop r = new Noop(); |
4077 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
4078 |
< |
f.complete(one); |
4079 |
< |
checkCompletedNormally(g, null); |
4080 |
< |
assertTrue(r.ran); |
4081 |
< |
|
4082 |
< |
r = new Noop(); |
2482 |
< |
f = new CompletableFuture<Integer>(); |
2483 |
< |
f.complete(one); |
2484 |
< |
f2 = new CompletableFuture<Integer>(); |
2485 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2486 |
< |
checkCompletedNormally(g, null); |
2487 |
< |
assertTrue(r.ran); |
2488 |
< |
} |
4070 |
> |
* minimalStage.toCompletableFuture() gives mutable CompletableFuture |
4071 |
> |
*/ |
4072 |
> |
public void testMinimalCompletionStage_toCompletableFuture_mutable() { |
4073 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
4074 |
> |
{ |
4075 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4076 |
> |
CompletionStage minimal = f.minimalCompletionStage(); |
4077 |
> |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4078 |
> |
assertTrue(g.complete(v1)); |
4079 |
> |
checkCompletedNormally(g, v1); |
4080 |
> |
checkIncomplete(f); |
4081 |
> |
checkIncomplete(minimal.toCompletableFuture()); |
4082 |
> |
}} |
4083 |
|
|
4084 |
|
/** |
4085 |
< |
* runAfterEitherAsync result completes exceptionally after exceptional |
2492 |
< |
* completion of source |
4085 |
> |
* minimalStage.toCompletableFuture().join() awaits completion |
4086 |
|
*/ |
4087 |
< |
public void testRunAfterEitherAsync2E() { |
4088 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
4089 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
4090 |
< |
Noop r = new Noop(); |
4091 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
4092 |
< |
f.completeExceptionally(new CFException()); |
4093 |
< |
checkCompletedWithWrappedCFException(g); |
4094 |
< |
|
4095 |
< |
r = new Noop(); |
4096 |
< |
f = new CompletableFuture<Integer>(); |
4097 |
< |
f2 = new CompletableFuture<Integer>(); |
4098 |
< |
f2.completeExceptionally(new CFException()); |
4099 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
4100 |
< |
f.complete(one); |
4101 |
< |
checkCompletedWithWrappedCFException(g); |
4087 |
> |
public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception { |
4088 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
4089 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
4090 |
> |
{ |
4091 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4092 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4093 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4094 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
4095 |
> |
assertEquals(v1, minimal.toCompletableFuture().join()); |
4096 |
> |
assertEquals(v1, minimal.toCompletableFuture().get()); |
4097 |
> |
checkCompletedNormally(minimal.toCompletableFuture(), v1); |
4098 |
> |
}} |
4099 |
> |
|
4100 |
> |
/** |
4101 |
> |
* Completion of a toCompletableFuture copy of a minimal stage |
4102 |
> |
* does not complete its source. |
4103 |
> |
*/ |
4104 |
> |
public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() { |
4105 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4106 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
4107 |
> |
assertTrue(g.toCompletableFuture().complete(1)); |
4108 |
> |
assertTrue(g.toCompletableFuture().complete(null)); |
4109 |
> |
assertTrue(g.toCompletableFuture().cancel(true)); |
4110 |
> |
assertTrue(g.toCompletableFuture().cancel(false)); |
4111 |
> |
assertTrue(g.toCompletableFuture().completeExceptionally(new CFException())); |
4112 |
> |
checkIncomplete(g.toCompletableFuture()); |
4113 |
> |
f.complete(1); |
4114 |
> |
checkCompletedNormally(g.toCompletableFuture(), 1); |
4115 |
|
} |
4116 |
|
|
4117 |
< |
/** |
4118 |
< |
* runAfterEitherAsync result completes exceptionally if action does |
4119 |
< |
*/ |
4120 |
< |
public void testRunAfterEitherAsync3E() { |
4121 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
4122 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
4123 |
< |
FailingNoop r = new FailingNoop(); |
4124 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
4125 |
< |
f.complete(one); |
4126 |
< |
checkCompletedWithWrappedCFException(g); |
4117 |
> |
/** Demo utility method for external reliable toCompletableFuture */ |
4118 |
> |
static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) { |
4119 |
> |
CompletableFuture<T> f = new CompletableFuture<>(); |
4120 |
> |
stage.handle((T t, Throwable ex) -> { |
4121 |
> |
if (ex != null) f.completeExceptionally(ex); |
4122 |
> |
else f.complete(t); |
4123 |
> |
return null; |
4124 |
> |
}); |
4125 |
> |
return f; |
4126 |
> |
} |
4127 |
> |
|
4128 |
> |
/** Demo utility method to join a CompletionStage */ |
4129 |
> |
static <T> T join(CompletionStage<T> stage) { |
4130 |
> |
return toCompletableFuture(stage).join(); |
4131 |
|
} |
4132 |
|
|
4133 |
|
/** |
4134 |
< |
* runAfterEitherAsync result completes exceptionally if either |
4135 |
< |
* source cancelled |
4136 |
< |
*/ |
4137 |
< |
public void testRunAfterEitherAsync4E() { |
4138 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
4139 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
4140 |
< |
Noop r = new Noop(); |
4141 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
4142 |
< |
assertTrue(f.cancel(true)); |
4143 |
< |
checkCompletedWithWrappedCancellationException(g); |
4144 |
< |
|
4145 |
< |
r = new Noop(); |
4146 |
< |
f = new CompletableFuture<Integer>(); |
4147 |
< |
f2 = new CompletableFuture<Integer>(); |
4148 |
< |
assertTrue(f2.cancel(true)); |
4149 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
4150 |
< |
checkCompletedWithWrappedCancellationException(g); |
4151 |
< |
} |
4134 |
> |
* Joining a minimal stage "by hand" works |
4135 |
> |
*/ |
4136 |
> |
public void testMinimalCompletionStage_join_by_hand() { |
4137 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
4138 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
4139 |
> |
{ |
4140 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4141 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4142 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
4143 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4144 |
> |
minimal.thenAccept(x -> g.complete(x)); |
4145 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
4146 |
> |
g.join(); |
4147 |
> |
checkCompletedNormally(g, v1); |
4148 |
> |
checkCompletedNormally(f, v1); |
4149 |
> |
assertEquals(v1, join(minimal)); |
4150 |
> |
}} |
4151 |
> |
|
4152 |
> |
static class Monad { |
4153 |
> |
static class ZeroException extends RuntimeException { |
4154 |
> |
public ZeroException() { super("monadic zero"); } |
4155 |
> |
} |
4156 |
> |
// "return", "unit" |
4157 |
> |
static <T> CompletableFuture<T> unit(T value) { |
4158 |
> |
return completedFuture(value); |
4159 |
> |
} |
4160 |
> |
// monadic zero ? |
4161 |
> |
static <T> CompletableFuture<T> zero() { |
4162 |
> |
return failedFuture(new ZeroException()); |
4163 |
> |
} |
4164 |
> |
// >=> |
4165 |
> |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
4166 |
> |
(Function<T, CompletableFuture<U>> f, |
4167 |
> |
Function<U, CompletableFuture<V>> g) { |
4168 |
> |
return x -> f.apply(x).thenCompose(g); |
4169 |
> |
} |
4170 |
|
|
4171 |
< |
/** |
4172 |
< |
* thenComposeAsync result completes normally after normal |
4173 |
< |
* completion of source |
4174 |
< |
*/ |
4175 |
< |
public void testThenComposeAsyncE() { |
4176 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
4177 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
4178 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2551 |
< |
f.complete(one); |
2552 |
< |
checkCompletedNormally(g, two); |
2553 |
< |
} |
4171 |
> |
static void assertZero(CompletableFuture<?> f) { |
4172 |
> |
try { |
4173 |
> |
f.getNow(null); |
4174 |
> |
throw new AssertionFailedError("should throw"); |
4175 |
> |
} catch (CompletionException success) { |
4176 |
> |
assertTrue(success.getCause() instanceof ZeroException); |
4177 |
> |
} |
4178 |
> |
} |
4179 |
|
|
4180 |
< |
/** |
4181 |
< |
* thenComposeAsync result completes exceptionally after |
4182 |
< |
* exceptional completion of source |
4183 |
< |
*/ |
4184 |
< |
public void testThenComposeAsync2E() { |
4185 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
4186 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
4187 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
4188 |
< |
f.completeExceptionally(new CFException()); |
4189 |
< |
checkCompletedWithWrappedCFException(g); |
4190 |
< |
} |
4180 |
> |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
4181 |
> |
CompletableFuture<T> g) { |
4182 |
> |
T fval = null, gval = null; |
4183 |
> |
Throwable fex = null, gex = null; |
4184 |
> |
|
4185 |
> |
try { fval = f.get(); } |
4186 |
> |
catch (ExecutionException ex) { fex = ex.getCause(); } |
4187 |
> |
catch (Throwable ex) { fex = ex; } |
4188 |
> |
|
4189 |
> |
try { gval = g.get(); } |
4190 |
> |
catch (ExecutionException ex) { gex = ex.getCause(); } |
4191 |
> |
catch (Throwable ex) { gex = ex; } |
4192 |
> |
|
4193 |
> |
if (fex != null || gex != null) |
4194 |
> |
assertSame(fex.getClass(), gex.getClass()); |
4195 |
> |
else |
4196 |
> |
assertEquals(fval, gval); |
4197 |
> |
} |
4198 |
|
|
4199 |
< |
/** |
4200 |
< |
* thenComposeAsync result completes exceptionally if action does |
4201 |
< |
*/ |
2570 |
< |
public void testThenComposeAsync3E() { |
2571 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2572 |
< |
FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); |
2573 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2574 |
< |
f.complete(one); |
2575 |
< |
checkCompletedWithWrappedCFException(g); |
2576 |
< |
} |
4199 |
> |
static class PlusFuture<T> extends CompletableFuture<T> { |
4200 |
> |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
4201 |
> |
} |
4202 |
|
|
4203 |
< |
/** |
4204 |
< |
* thenComposeAsync result completes exceptionally if source cancelled |
4205 |
< |
*/ |
4206 |
< |
public void testThenComposeAsync4E() { |
4207 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
4208 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
4209 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
4210 |
< |
assertTrue(f.cancel(true)); |
4211 |
< |
checkCompletedWithWrappedCancellationException(g); |
4203 |
> |
/** Implements "monadic plus". */ |
4204 |
> |
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
4205 |
> |
CompletableFuture<? extends T> g) { |
4206 |
> |
PlusFuture<T> plus = new PlusFuture<T>(); |
4207 |
> |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
4208 |
> |
try { |
4209 |
> |
if (ex == null) { |
4210 |
> |
if (plus.complete(result)) |
4211 |
> |
if (plus.firstFailure.get() != null) |
4212 |
> |
plus.firstFailure.set(null); |
4213 |
> |
} |
4214 |
> |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
4215 |
> |
if (plus.isDone()) |
4216 |
> |
plus.firstFailure.set(null); |
4217 |
> |
} |
4218 |
> |
else { |
4219 |
> |
// first failure has precedence |
4220 |
> |
Throwable first = plus.firstFailure.getAndSet(null); |
4221 |
> |
|
4222 |
> |
// may fail with "Self-suppression not permitted" |
4223 |
> |
try { first.addSuppressed(ex); } |
4224 |
> |
catch (Exception ignored) {} |
4225 |
> |
|
4226 |
> |
plus.completeExceptionally(first); |
4227 |
> |
} |
4228 |
> |
} catch (Throwable unexpected) { |
4229 |
> |
plus.completeExceptionally(unexpected); |
4230 |
> |
} |
4231 |
> |
}; |
4232 |
> |
f.whenComplete(action); |
4233 |
> |
g.whenComplete(action); |
4234 |
> |
return plus; |
4235 |
> |
} |
4236 |
|
} |
4237 |
|
|
2589 |
– |
// other static methods |
2590 |
– |
|
4238 |
|
/** |
4239 |
< |
* allOf(no component futures) returns a future completed normally |
4240 |
< |
* with the value null |
4239 |
> |
* CompletableFuture is an additive monad - sort of. |
4240 |
> |
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
4241 |
|
*/ |
4242 |
< |
public void testAllOf_empty() throws Exception { |
4243 |
< |
CompletableFuture<?> f = CompletableFuture.allOf(); |
4244 |
< |
checkCompletedNormally(f, null); |
4245 |
< |
} |
4242 |
> |
public void testAdditiveMonad() throws Throwable { |
4243 |
> |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
4244 |
> |
CompletableFuture<Long> zero = Monad.zero(); |
4245 |
> |
|
4246 |
> |
// Some mutually non-commutative functions |
4247 |
> |
Function<Long, CompletableFuture<Long>> triple |
4248 |
> |
= x -> Monad.unit(3 * x); |
4249 |
> |
Function<Long, CompletableFuture<Long>> inc |
4250 |
> |
= x -> Monad.unit(x + 1); |
4251 |
> |
|
4252 |
> |
// unit is a right identity: m >>= unit === m |
4253 |
> |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
4254 |
> |
inc.apply(5L)); |
4255 |
> |
// unit is a left identity: (unit x) >>= f === f x |
4256 |
> |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
4257 |
> |
inc.apply(5L)); |
4258 |
> |
|
4259 |
> |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
4260 |
> |
Monad.assertFutureEquals( |
4261 |
> |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
4262 |
> |
unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple))); |
4263 |
> |
|
4264 |
> |
// The case for CompletableFuture as an additive monad is weaker... |
4265 |
> |
|
4266 |
> |
// zero is a monadic zero |
4267 |
> |
Monad.assertZero(zero); |
4268 |
> |
|
4269 |
> |
// left zero: zero >>= f === zero |
4270 |
> |
Monad.assertZero(zero.thenCompose(inc)); |
4271 |
> |
// right zero: f >>= (\x -> zero) === zero |
4272 |
> |
Monad.assertZero(inc.apply(5L).thenCompose(x -> zero)); |
4273 |
> |
|
4274 |
> |
// f plus zero === f |
4275 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
4276 |
> |
Monad.plus(Monad.unit(5L), zero)); |
4277 |
> |
// zero plus f === f |
4278 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
4279 |
> |
Monad.plus(zero, Monad.unit(5L))); |
4280 |
> |
// zero plus zero === zero |
4281 |
> |
Monad.assertZero(Monad.plus(zero, zero)); |
4282 |
> |
{ |
4283 |
> |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
4284 |
> |
Monad.unit(8L)); |
4285 |
> |
// non-determinism |
4286 |
> |
assertTrue(f.get() == 5L || f.get() == 8L); |
4287 |
> |
} |
4288 |
|
|
4289 |
< |
/** |
4290 |
< |
* allOf returns a future completed when all components complete |
4291 |
< |
*/ |
4292 |
< |
public void testAllOf() throws Exception { |
4293 |
< |
for (int k = 1; k < 20; ++k) { |
4294 |
< |
CompletableFuture[] fs = new CompletableFuture[k]; |
4295 |
< |
for (int i = 0; i < k; ++i) |
4296 |
< |
fs[i] = new CompletableFuture<Integer>(); |
4297 |
< |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
4298 |
< |
for (int i = 0; i < k; ++i) { |
4299 |
< |
checkIncomplete(f); |
4300 |
< |
fs[i].complete(one); |
4301 |
< |
} |
4302 |
< |
checkCompletedNormally(f, null); |
4289 |
> |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
4290 |
> |
// f plus godot === f (doesn't wait for godot) |
4291 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
4292 |
> |
Monad.plus(Monad.unit(5L), godot)); |
4293 |
> |
// godot plus f === f (doesn't wait for godot) |
4294 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
4295 |
> |
Monad.plus(godot, Monad.unit(5L))); |
4296 |
> |
} |
4297 |
> |
|
4298 |
> |
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4299 |
> |
public void testRecursiveChains() throws Throwable { |
4300 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
4301 |
> |
for (boolean addDeadEnds : new boolean[] { true, false }) |
4302 |
> |
{ |
4303 |
> |
final int val = 42; |
4304 |
> |
final int n = expensiveTests ? 1_000 : 2; |
4305 |
> |
CompletableFuture<Integer> head = new CompletableFuture<>(); |
4306 |
> |
CompletableFuture<Integer> tail = head; |
4307 |
> |
for (int i = 0; i < n; i++) { |
4308 |
> |
if (addDeadEnds) m.thenApply(tail, v -> v + 1); |
4309 |
> |
tail = m.thenApply(tail, v -> v + 1); |
4310 |
> |
if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1); |
4311 |
> |
tail = m.applyToEither(tail, tail, v -> v + 1); |
4312 |
> |
if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1); |
4313 |
> |
tail = m.thenCombine(tail, tail, (v, w) -> v + 1); |
4314 |
> |
} |
4315 |
> |
head.complete(val); |
4316 |
> |
assertEquals(val + 3 * n, (int) tail.join()); |
4317 |
> |
}} |
4318 |
> |
|
4319 |
> |
/** |
4320 |
> |
* A single CompletableFuture with many dependents. |
4321 |
> |
* A demo of scalability - runtime is O(n). |
4322 |
> |
*/ |
4323 |
> |
public void testManyDependents() throws Throwable { |
4324 |
> |
final int n = expensiveTests ? 1_000_000 : 10; |
4325 |
> |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4326 |
> |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
4327 |
> |
final AtomicInteger count = new AtomicInteger(0); |
4328 |
> |
for (int i = 0; i < n; i++) { |
4329 |
> |
head.thenRun(() -> count.getAndIncrement()); |
4330 |
> |
head.thenAccept(x -> count.getAndIncrement()); |
4331 |
> |
head.thenApply(x -> count.getAndIncrement()); |
4332 |
> |
|
4333 |
> |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
4334 |
> |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
4335 |
> |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
4336 |
> |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
4337 |
> |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
4338 |
> |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
4339 |
> |
|
4340 |
> |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
4341 |
> |
head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement()); |
4342 |
> |
head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement()); |
4343 |
> |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
4344 |
> |
new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement()); |
4345 |
> |
new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement()); |
4346 |
|
} |
4347 |
+ |
head.complete(null); |
4348 |
+ |
assertEquals(5 * 3 * n, count.get()); |
4349 |
|
} |
4350 |
|
|
4351 |
< |
/** |
4352 |
< |
* anyOf(no component futures) returns an incomplete future |
4353 |
< |
*/ |
4354 |
< |
public void testAnyOf_empty() throws Exception { |
4355 |
< |
CompletableFuture<?> f = CompletableFuture.anyOf(); |
4356 |
< |
checkIncomplete(f); |
4357 |
< |
} |
4351 |
> |
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4352 |
> |
public void testCoCompletionGarbageRetention() throws Throwable { |
4353 |
> |
final int n = expensiveTests ? 1_000_000 : 10; |
4354 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4355 |
> |
CompletableFuture<Integer> f; |
4356 |
> |
for (int i = 0; i < n; i++) { |
4357 |
> |
f = new CompletableFuture<>(); |
4358 |
> |
f.runAfterEither(incomplete, () -> {}); |
4359 |
> |
f.complete(null); |
4360 |
> |
|
4361 |
> |
f = new CompletableFuture<>(); |
4362 |
> |
f.acceptEither(incomplete, x -> {}); |
4363 |
> |
f.complete(null); |
4364 |
> |
|
4365 |
> |
f = new CompletableFuture<>(); |
4366 |
> |
f.applyToEither(incomplete, x -> x); |
4367 |
> |
f.complete(null); |
4368 |
> |
|
4369 |
> |
f = new CompletableFuture<>(); |
4370 |
> |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4371 |
> |
f.complete(null); |
4372 |
> |
} |
4373 |
|
|
4374 |
< |
/** |
4375 |
< |
* anyOf returns a future completed when any components complete |
4376 |
< |
*/ |
4377 |
< |
public void testAnyOf() throws Exception { |
4378 |
< |
for (int k = 1; k < 20; ++k) { |
4379 |
< |
CompletableFuture[] fs = new CompletableFuture[k]; |
4380 |
< |
for (int i = 0; i < k; ++i) |
4381 |
< |
fs[i] = new CompletableFuture<Integer>(); |
4382 |
< |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
4383 |
< |
checkIncomplete(f); |
4384 |
< |
for (int i = 0; i < k; ++i) { |
4385 |
< |
fs[i].complete(one); |
4386 |
< |
checkCompletedNormally(f, one); |
4387 |
< |
} |
4374 |
> |
for (int i = 0; i < n; i++) { |
4375 |
> |
f = new CompletableFuture<>(); |
4376 |
> |
incomplete.runAfterEither(f, () -> {}); |
4377 |
> |
f.complete(null); |
4378 |
> |
|
4379 |
> |
f = new CompletableFuture<>(); |
4380 |
> |
incomplete.acceptEither(f, x -> {}); |
4381 |
> |
f.complete(null); |
4382 |
> |
|
4383 |
> |
f = new CompletableFuture<>(); |
4384 |
> |
incomplete.applyToEither(f, x -> x); |
4385 |
> |
f.complete(null); |
4386 |
> |
|
4387 |
> |
f = new CompletableFuture<>(); |
4388 |
> |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4389 |
> |
f.complete(null); |
4390 |
|
} |
4391 |
|
} |
4392 |
|
|
4393 |
|
/** |
4394 |
< |
* Completion methods throw NullPointerException with null arguments |
4395 |
< |
*/ |
4396 |
< |
public void testNPE() { |
4397 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
4398 |
< |
CompletableFuture<Integer> g = new CompletableFuture<Integer>(); |
4399 |
< |
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
4400 |
< |
CompletableFuture<?> h; |
4401 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
4402 |
< |
|
4403 |
< |
Runnable[] throwingActions = { |
4404 |
< |
() -> { CompletableFuture.supplyAsync(null); }, |
4405 |
< |
() -> { CompletableFuture.supplyAsync(null, exec); }, |
4406 |
< |
() -> { CompletableFuture.supplyAsync(supplyOne, null); }, |
4407 |
< |
|
4408 |
< |
() -> { CompletableFuture.runAsync(null); }, |
4409 |
< |
() -> { CompletableFuture.runAsync(null, exec); }, |
4410 |
< |
() -> { CompletableFuture.runAsync(() -> {}, null); }, |
4411 |
< |
|
4412 |
< |
() -> { f.completeExceptionally(null); }, |
4413 |
< |
|
4414 |
< |
() -> { f.thenApply(null); }, |
4415 |
< |
() -> { f.thenApplyAsync(null); }, |
4416 |
< |
() -> { f.thenApplyAsync((x) -> x, null); }, |
4417 |
< |
() -> { f.thenApplyAsync(null, exec); }, |
4418 |
< |
|
4419 |
< |
() -> { f.thenAccept(null); }, |
4420 |
< |
() -> { f.thenAcceptAsync(null); }, |
4421 |
< |
() -> { f.thenAcceptAsync((x) -> { ; }, null); }, |
4422 |
< |
() -> { f.thenAcceptAsync(null, exec); }, |
4423 |
< |
|
4424 |
< |
() -> { f.thenRun(null); }, |
4425 |
< |
() -> { f.thenRunAsync(null); }, |
4426 |
< |
() -> { f.thenRunAsync(() -> { ; }, null); }, |
4427 |
< |
() -> { f.thenRunAsync(null, exec); }, |
4428 |
< |
|
4429 |
< |
() -> { f.thenCombine(g, null); }, |
4430 |
< |
() -> { f.thenCombineAsync(g, null); }, |
4431 |
< |
() -> { f.thenCombineAsync(g, null, exec); }, |
4432 |
< |
() -> { f.thenCombine(nullFuture, (x, y) -> x); }, |
4433 |
< |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x); }, |
4434 |
< |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); }, |
4435 |
< |
() -> { f.thenCombineAsync(g, (x, y) -> x, null); }, |
4436 |
< |
|
4437 |
< |
() -> { f.thenAcceptBoth(g, null); }, |
4438 |
< |
() -> { f.thenAcceptBothAsync(g, null); }, |
4439 |
< |
() -> { f.thenAcceptBothAsync(g, null, exec); }, |
4440 |
< |
() -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); }, |
4441 |
< |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); }, |
4442 |
< |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); }, |
4443 |
< |
() -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); }, |
4444 |
< |
|
4445 |
< |
() -> { f.runAfterBoth(g, null); }, |
4446 |
< |
() -> { f.runAfterBothAsync(g, null); }, |
4447 |
< |
() -> { f.runAfterBothAsync(g, null, exec); }, |
4448 |
< |
() -> { f.runAfterBoth(nullFuture, () -> {}); }, |
4449 |
< |
() -> { f.runAfterBothAsync(nullFuture, () -> {}); }, |
4450 |
< |
() -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); }, |
4451 |
< |
() -> { f.runAfterBothAsync(g, () -> {}, null); }, |
4452 |
< |
|
4453 |
< |
() -> { f.applyToEither(g, null); }, |
4454 |
< |
() -> { f.applyToEitherAsync(g, null); }, |
4455 |
< |
() -> { f.applyToEitherAsync(g, null, exec); }, |
4456 |
< |
() -> { f.applyToEither(nullFuture, (x) -> x); }, |
4457 |
< |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x); }, |
4458 |
< |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); }, |
4459 |
< |
() -> { f.applyToEitherAsync(g, (x) -> x, null); }, |
4460 |
< |
|
4461 |
< |
() -> { f.acceptEither(g, null); }, |
4462 |
< |
() -> { f.acceptEitherAsync(g, null); }, |
4463 |
< |
() -> { f.acceptEitherAsync(g, null, exec); }, |
4464 |
< |
() -> { f.acceptEither(nullFuture, (x) -> {}); }, |
4465 |
< |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}); }, |
4466 |
< |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); }, |
4467 |
< |
() -> { f.acceptEitherAsync(g, (x) -> {}, null); }, |
4468 |
< |
|
4469 |
< |
() -> { f.runAfterEither(g, null); }, |
4470 |
< |
() -> { f.runAfterEitherAsync(g, null); }, |
4471 |
< |
() -> { f.runAfterEitherAsync(g, null, exec); }, |
4472 |
< |
() -> { f.runAfterEither(nullFuture, () -> {}); }, |
4473 |
< |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}); }, |
4474 |
< |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); }, |
4475 |
< |
() -> { f.runAfterEitherAsync(g, () -> {}, null); }, |
4476 |
< |
|
4477 |
< |
() -> { f.thenCompose(null); }, |
4478 |
< |
() -> { f.thenComposeAsync(null); }, |
4479 |
< |
() -> { f.thenComposeAsync(new CompletableFutureInc(), null); }, |
4480 |
< |
() -> { f.thenComposeAsync(null, exec); }, |
2730 |
< |
|
2731 |
< |
() -> { f.exceptionally(null); }, |
2732 |
< |
|
2733 |
< |
() -> { f.handle(null); }, |
2734 |
< |
|
2735 |
< |
() -> { CompletableFuture.allOf((CompletableFuture<?>)null); }, |
2736 |
< |
() -> { CompletableFuture.allOf((CompletableFuture<?>[])null); }, |
2737 |
< |
() -> { CompletableFuture.allOf(f, null); }, |
2738 |
< |
() -> { CompletableFuture.allOf(null, f); }, |
2739 |
< |
|
2740 |
< |
() -> { CompletableFuture.anyOf((CompletableFuture<?>)null); }, |
2741 |
< |
() -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); }, |
2742 |
< |
() -> { CompletableFuture.anyOf(f, null); }, |
2743 |
< |
() -> { CompletableFuture.anyOf(null, f); }, |
2744 |
< |
}; |
2745 |
< |
|
2746 |
< |
assertThrows(NullPointerException.class, throwingActions); |
2747 |
< |
assertEquals(0, exec.count.get()); |
2748 |
< |
} |
4394 |
> |
* Reproduction recipe for: |
4395 |
> |
* 8160402: Garbage retention with CompletableFuture.anyOf |
4396 |
> |
* cvs update -D '2016-05-01' ./src/main/java/util/concurrent/CompletableFuture.java && ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck; cvs update -A |
4397 |
> |
*/ |
4398 |
> |
public void testAnyOfGarbageRetention() throws Throwable { |
4399 |
> |
for (Integer v : new Integer[] { 1, null }) |
4400 |
> |
{ |
4401 |
> |
final int n = expensiveTests ? 100_000 : 10; |
4402 |
> |
CompletableFuture<Integer>[] fs |
4403 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4404 |
> |
for (int i = 0; i < fs.length; i++) |
4405 |
> |
fs[i] = new CompletableFuture<>(); |
4406 |
> |
fs[fs.length - 1].complete(v); |
4407 |
> |
for (int i = 0; i < n; i++) |
4408 |
> |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4409 |
> |
}} |
4410 |
> |
|
4411 |
> |
/** |
4412 |
> |
* Checks for garbage retention with allOf. |
4413 |
> |
* |
4414 |
> |
* As of 2016-07, fails with OOME: |
4415 |
> |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck |
4416 |
> |
*/ |
4417 |
> |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4418 |
> |
final int n = expensiveTests ? 100_000 : 10; |
4419 |
> |
CompletableFuture<Integer>[] fs |
4420 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4421 |
> |
for (int i = 0; i < fs.length; i++) |
4422 |
> |
fs[i] = new CompletableFuture<>(); |
4423 |
> |
for (int i = 0; i < n; i++) |
4424 |
> |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4425 |
> |
} |
4426 |
> |
|
4427 |
> |
/** |
4428 |
> |
* Checks for garbage retention when a dependent future is |
4429 |
> |
* cancelled and garbage-collected. |
4430 |
> |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4431 |
> |
* |
4432 |
> |
* As of 2016-07, fails with OOME: |
4433 |
> |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck |
4434 |
> |
*/ |
4435 |
> |
public void testCancelledGarbageRetention() throws Throwable { |
4436 |
> |
final int n = expensiveTests ? 100_000 : 10; |
4437 |
> |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4438 |
> |
for (int i = 0; i < n; i++) |
4439 |
> |
assertTrue(neverCompleted.thenRun(() -> {}).cancel(true)); |
4440 |
> |
} |
4441 |
> |
|
4442 |
> |
/** |
4443 |
> |
* Checks for garbage retention when MinimalStage.toCompletableFuture() |
4444 |
> |
* is invoked many times. |
4445 |
> |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4446 |
> |
* |
4447 |
> |
* As of 2016-07, fails with OOME: |
4448 |
> |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck |
4449 |
> |
*/ |
4450 |
> |
public void testToCompletableFutureGarbageRetention() throws Throwable { |
4451 |
> |
final int n = expensiveTests ? 900_000 : 10; |
4452 |
> |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4453 |
> |
CompletionStage minimal = neverCompleted.minimalCompletionStage(); |
4454 |
> |
for (int i = 0; i < n; i++) |
4455 |
> |
assertTrue(minimal.toCompletableFuture().cancel(true)); |
4456 |
> |
} |
4457 |
> |
|
4458 |
> |
// static <U> U join(CompletionStage<U> stage) { |
4459 |
> |
// CompletableFuture<U> f = new CompletableFuture<>(); |
4460 |
> |
// stage.whenComplete((v, ex) -> { |
4461 |
> |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
4462 |
> |
// }); |
4463 |
> |
// return f.join(); |
4464 |
> |
// } |
4465 |
> |
|
4466 |
> |
// static <U> boolean isDone(CompletionStage<U> stage) { |
4467 |
> |
// CompletableFuture<U> f = new CompletableFuture<>(); |
4468 |
> |
// stage.whenComplete((v, ex) -> { |
4469 |
> |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
4470 |
> |
// }); |
4471 |
> |
// return f.isDone(); |
4472 |
> |
// } |
4473 |
> |
|
4474 |
> |
// static <U> U join2(CompletionStage<U> stage) { |
4475 |
> |
// return stage.toCompletableFuture().copy().join(); |
4476 |
> |
// } |
4477 |
> |
|
4478 |
> |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
4479 |
> |
// return stage.toCompletableFuture().copy().isDone(); |
4480 |
> |
// } |
4481 |
|
|
4482 |
|
} |