16 |
|
import java.util.concurrent.Future; |
17 |
|
import java.util.concurrent.CompletableFuture; |
18 |
|
import java.util.concurrent.CompletionException; |
19 |
+ |
import java.util.concurrent.CompletionStage; |
20 |
+ |
import java.util.concurrent.ForkJoinPool; |
21 |
+ |
import java.util.concurrent.ForkJoinTask; |
22 |
|
import java.util.concurrent.TimeoutException; |
23 |
|
import java.util.concurrent.atomic.AtomicInteger; |
24 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
57 |
|
} |
58 |
|
|
59 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
60 |
+ |
checkTimedGet(f, value); |
61 |
+ |
|
62 |
|
try { |
63 |
|
assertEquals(value, f.join()); |
64 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
68 |
|
try { |
69 |
|
assertEquals(value, f.get()); |
70 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
66 |
– |
try { |
67 |
– |
assertEquals(value, f.get(0L, SECONDS)); |
68 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
71 |
|
assertTrue(f.isDone()); |
72 |
|
assertFalse(f.isCancelled()); |
73 |
+ |
assertFalse(f.isCompletedExceptionally()); |
74 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
75 |
|
} |
76 |
|
|
77 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
78 |
+ |
long startTime = System.nanoTime(); |
79 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
80 |
+ |
try { |
81 |
+ |
f.get(timeoutMillis, MILLISECONDS); |
82 |
+ |
shouldThrow(); |
83 |
+ |
} catch (ExecutionException success) { |
84 |
+ |
assertTrue(success.getCause() instanceof CFException); |
85 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
86 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
87 |
+ |
|
88 |
|
try { |
89 |
|
f.join(); |
90 |
|
shouldThrow(); |
103 |
|
} catch (ExecutionException success) { |
104 |
|
assertTrue(success.getCause() instanceof CFException); |
105 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
106 |
+ |
assertTrue(f.isDone()); |
107 |
+ |
assertFalse(f.isCancelled()); |
108 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
109 |
+ |
} |
110 |
+ |
|
111 |
+ |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
112 |
+ |
Throwable ex) { |
113 |
+ |
long startTime = System.nanoTime(); |
114 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
115 |
|
try { |
116 |
< |
f.get(0L, SECONDS); |
116 |
> |
f.get(timeoutMillis, MILLISECONDS); |
117 |
|
shouldThrow(); |
118 |
|
} catch (ExecutionException success) { |
119 |
< |
assertTrue(success.getCause() instanceof CFException); |
119 |
> |
assertSame(ex, success.getCause()); |
120 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
121 |
> |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
122 |
> |
|
123 |
> |
try { |
124 |
> |
f.join(); |
125 |
> |
shouldThrow(); |
126 |
> |
} catch (CompletionException success) { |
127 |
> |
assertSame(ex, success.getCause()); |
128 |
> |
} |
129 |
> |
try { |
130 |
> |
f.getNow(null); |
131 |
> |
shouldThrow(); |
132 |
> |
} catch (CompletionException success) { |
133 |
> |
assertSame(ex, success.getCause()); |
134 |
> |
} |
135 |
> |
try { |
136 |
> |
f.get(); |
137 |
> |
shouldThrow(); |
138 |
> |
} catch (ExecutionException success) { |
139 |
> |
assertSame(ex, success.getCause()); |
140 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
141 |
+ |
|
142 |
|
assertTrue(f.isDone()); |
143 |
|
assertFalse(f.isCancelled()); |
144 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
145 |
|
} |
146 |
|
|
147 |
+ |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
148 |
+ |
Throwable ex) { |
149 |
+ |
checkCompletedExceptionallyWithRootCause(f, ex); |
150 |
+ |
try { |
151 |
+ |
CompletableFuture<Throwable> spy = f.handle |
152 |
+ |
((U u, Throwable t) -> t); |
153 |
+ |
assertTrue(spy.join() instanceof CompletionException); |
154 |
+ |
assertSame(ex, spy.join().getCause()); |
155 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
156 |
+ |
} |
157 |
+ |
|
158 |
+ |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
159 |
+ |
checkCompletedExceptionallyWithRootCause(f, ex); |
160 |
+ |
try { |
161 |
+ |
CompletableFuture<Throwable> spy = f.handle |
162 |
+ |
((U u, Throwable t) -> t); |
163 |
+ |
assertSame(ex, spy.join()); |
164 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
165 |
+ |
} |
166 |
+ |
|
167 |
|
void checkCancelled(CompletableFuture<?> f) { |
168 |
+ |
long startTime = System.nanoTime(); |
169 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
170 |
+ |
try { |
171 |
+ |
f.get(timeoutMillis, MILLISECONDS); |
172 |
+ |
shouldThrow(); |
173 |
+ |
} catch (CancellationException success) { |
174 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
175 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
176 |
+ |
|
177 |
|
try { |
178 |
|
f.join(); |
179 |
|
shouldThrow(); |
187 |
|
shouldThrow(); |
188 |
|
} catch (CancellationException success) { |
189 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
118 |
– |
try { |
119 |
– |
f.get(0L, SECONDS); |
120 |
– |
shouldThrow(); |
121 |
– |
} catch (CancellationException success) { |
122 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
190 |
|
assertTrue(f.isDone()); |
191 |
+ |
assertTrue(f.isCompletedExceptionally()); |
192 |
|
assertTrue(f.isCancelled()); |
193 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
194 |
|
} |
195 |
|
|
196 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
197 |
+ |
long startTime = System.nanoTime(); |
198 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
199 |
+ |
try { |
200 |
+ |
f.get(timeoutMillis, MILLISECONDS); |
201 |
+ |
shouldThrow(); |
202 |
+ |
} catch (ExecutionException success) { |
203 |
+ |
assertTrue(success.getCause() instanceof CancellationException); |
204 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
205 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
206 |
+ |
|
207 |
|
try { |
208 |
|
f.join(); |
209 |
|
shouldThrow(); |
222 |
|
} catch (ExecutionException success) { |
223 |
|
assertTrue(success.getCause() instanceof CancellationException); |
224 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
147 |
– |
try { |
148 |
– |
f.get(0L, SECONDS); |
149 |
– |
shouldThrow(); |
150 |
– |
} catch (ExecutionException success) { |
151 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
152 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
225 |
|
assertTrue(f.isDone()); |
226 |
|
assertFalse(f.isCancelled()); |
227 |
+ |
assertTrue(f.isCompletedExceptionally()); |
228 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
229 |
|
} |
230 |
|
|
233 |
|
* by methods isDone, isCancelled, and getNow |
234 |
|
*/ |
235 |
|
public void testConstructor() { |
236 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
236 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
237 |
|
checkIncomplete(f); |
238 |
|
} |
239 |
|
|
242 |
|
* isCancelled, join, get, and getNow |
243 |
|
*/ |
244 |
|
public void testComplete() { |
245 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
245 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
246 |
> |
{ |
247 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
248 |
|
checkIncomplete(f); |
249 |
< |
f.complete(one); |
250 |
< |
checkCompletedNormally(f, one); |
251 |
< |
} |
249 |
> |
assertTrue(f.complete(v1)); |
250 |
> |
assertFalse(f.complete(v1)); |
251 |
> |
checkCompletedNormally(f, v1); |
252 |
> |
}} |
253 |
|
|
254 |
|
/** |
255 |
|
* completeExceptionally completes exceptionally, as indicated by |
256 |
|
* methods isDone, isCancelled, join, get, and getNow |
257 |
|
*/ |
258 |
|
public void testCompleteExceptionally() { |
259 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
259 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
260 |
> |
CFException ex = new CFException(); |
261 |
|
checkIncomplete(f); |
262 |
< |
f.completeExceptionally(new CFException()); |
263 |
< |
checkCompletedWithWrappedCFException(f); |
262 |
> |
f.completeExceptionally(ex); |
263 |
> |
checkCompletedExceptionally(f, ex); |
264 |
|
} |
265 |
|
|
266 |
|
/** |
268 |
|
* methods isDone, isCancelled, join, get, and getNow |
269 |
|
*/ |
270 |
|
public void testCancel() { |
271 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
271 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
272 |
> |
{ |
273 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
274 |
|
checkIncomplete(f); |
275 |
|
assertTrue(f.cancel(true)); |
276 |
+ |
assertTrue(f.cancel(true)); |
277 |
|
checkCancelled(f); |
278 |
< |
} |
278 |
> |
}} |
279 |
|
|
280 |
|
/** |
281 |
|
* obtrudeValue forces completion with given value |
282 |
|
*/ |
283 |
|
public void testObtrudeValue() { |
284 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
284 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
285 |
|
checkIncomplete(f); |
286 |
< |
f.complete(one); |
286 |
> |
assertTrue(f.complete(one)); |
287 |
|
checkCompletedNormally(f, one); |
288 |
|
f.obtrudeValue(three); |
289 |
|
checkCompletedNormally(f, three); |
290 |
|
f.obtrudeValue(two); |
291 |
|
checkCompletedNormally(f, two); |
292 |
< |
f = new CompletableFuture<Integer>(); |
292 |
> |
f = new CompletableFuture<>(); |
293 |
|
f.obtrudeValue(three); |
294 |
|
checkCompletedNormally(f, three); |
295 |
< |
f = new CompletableFuture<Integer>(); |
295 |
> |
f.obtrudeValue(null); |
296 |
> |
checkCompletedNormally(f, null); |
297 |
> |
f = new CompletableFuture<>(); |
298 |
|
f.completeExceptionally(new CFException()); |
299 |
|
f.obtrudeValue(four); |
300 |
|
checkCompletedNormally(f, four); |
304 |
|
* obtrudeException forces completion with given exception |
305 |
|
*/ |
306 |
|
public void testObtrudeException() { |
307 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
308 |
< |
checkIncomplete(f); |
309 |
< |
f.complete(one); |
310 |
< |
checkCompletedNormally(f, one); |
311 |
< |
f.obtrudeException(new CFException()); |
312 |
< |
checkCompletedWithWrappedCFException(f); |
313 |
< |
f = new CompletableFuture<Integer>(); |
314 |
< |
f.obtrudeException(new CFException()); |
315 |
< |
checkCompletedWithWrappedCFException(f); |
316 |
< |
f = new CompletableFuture<Integer>(); |
317 |
< |
f.completeExceptionally(new CFException()); |
318 |
< |
f.obtrudeValue(four); |
319 |
< |
checkCompletedNormally(f, four); |
320 |
< |
f.obtrudeException(new CFException()); |
321 |
< |
checkCompletedWithWrappedCFException(f); |
322 |
< |
} |
307 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
308 |
> |
{ |
309 |
> |
CFException ex; |
310 |
> |
CompletableFuture<Integer> f; |
311 |
> |
|
312 |
> |
f = new CompletableFuture<>(); |
313 |
> |
assertTrue(f.complete(v1)); |
314 |
> |
for (int i = 0; i < 2; i++) { |
315 |
> |
f.obtrudeException(ex = new CFException()); |
316 |
> |
checkCompletedExceptionally(f, ex); |
317 |
> |
} |
318 |
> |
|
319 |
> |
f = new CompletableFuture<>(); |
320 |
> |
for (int i = 0; i < 2; i++) { |
321 |
> |
f.obtrudeException(ex = new CFException()); |
322 |
> |
checkCompletedExceptionally(f, ex); |
323 |
> |
} |
324 |
> |
|
325 |
> |
f = new CompletableFuture<>(); |
326 |
> |
f.completeExceptionally(ex = new CFException()); |
327 |
> |
f.obtrudeValue(v1); |
328 |
> |
checkCompletedNormally(f, v1); |
329 |
> |
f.obtrudeException(ex = new CFException()); |
330 |
> |
checkCompletedExceptionally(f, ex); |
331 |
> |
f.completeExceptionally(new CFException()); |
332 |
> |
checkCompletedExceptionally(f, ex); |
333 |
> |
assertFalse(f.complete(v1)); |
334 |
> |
checkCompletedExceptionally(f, ex); |
335 |
> |
}} |
336 |
|
|
337 |
|
/** |
338 |
|
* getNumberOfDependents returns number of dependent tasks |
339 |
|
*/ |
340 |
|
public void testGetNumberOfDependents() { |
341 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
342 |
< |
assertEquals(f.getNumberOfDependents(), 0); |
343 |
< |
CompletableFuture g = f.thenRun(new Noop()); |
344 |
< |
assertEquals(f.getNumberOfDependents(), 1); |
345 |
< |
assertEquals(g.getNumberOfDependents(), 0); |
346 |
< |
CompletableFuture h = f.thenRun(new Noop()); |
347 |
< |
assertEquals(f.getNumberOfDependents(), 2); |
348 |
< |
f.complete(1); |
349 |
< |
checkCompletedNormally(g, null); |
350 |
< |
assertEquals(f.getNumberOfDependents(), 0); |
351 |
< |
assertEquals(g.getNumberOfDependents(), 0); |
352 |
< |
} |
353 |
< |
|
341 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
342 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
343 |
> |
{ |
344 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
345 |
> |
assertEquals(0, f.getNumberOfDependents()); |
346 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
347 |
> |
assertEquals(1, f.getNumberOfDependents()); |
348 |
> |
assertEquals(0, g.getNumberOfDependents()); |
349 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
350 |
> |
assertEquals(2, f.getNumberOfDependents()); |
351 |
> |
assertEquals(0, h.getNumberOfDependents()); |
352 |
> |
assertTrue(f.complete(v1)); |
353 |
> |
checkCompletedNormally(g, null); |
354 |
> |
checkCompletedNormally(h, null); |
355 |
> |
assertEquals(0, f.getNumberOfDependents()); |
356 |
> |
assertEquals(0, g.getNumberOfDependents()); |
357 |
> |
assertEquals(0, h.getNumberOfDependents()); |
358 |
> |
}} |
359 |
|
|
360 |
|
/** |
361 |
|
* toString indicates current completion state |
366 |
|
f = new CompletableFuture<String>(); |
367 |
|
assertTrue(f.toString().contains("[Not completed]")); |
368 |
|
|
369 |
< |
f.complete("foo"); |
369 |
> |
assertTrue(f.complete("foo")); |
370 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
371 |
|
|
372 |
|
f = new CompletableFuture<String>(); |
373 |
< |
f.completeExceptionally(new IndexOutOfBoundsException()); |
373 |
> |
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
374 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
375 |
+ |
|
376 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
377 |
+ |
f = new CompletableFuture<String>(); |
378 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
379 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
380 |
+ |
} |
381 |
|
} |
382 |
|
|
383 |
|
/** |
388 |
|
checkCompletedNormally(f, "test"); |
389 |
|
} |
390 |
|
|
391 |
< |
static final Supplier<Integer> supplyOne = |
392 |
< |
() -> Integer.valueOf(1); |
393 |
< |
static final Function<Integer, Integer> inc = |
394 |
< |
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
395 |
< |
static final BiFunction<Integer, Integer, Integer> add = |
396 |
< |
(Integer x, Integer y) -> Integer.valueOf(x.intValue() + y.intValue()); |
397 |
< |
static final class IncAction implements Consumer<Integer> { |
398 |
< |
int value; |
399 |
< |
public void accept(Integer x) { value = x.intValue() + 1; } |
391 |
> |
abstract class CheckedAction { |
392 |
> |
int invocationCount = 0; |
393 |
> |
final ExecutionMode m; |
394 |
> |
CheckedAction(ExecutionMode m) { this.m = m; } |
395 |
> |
void invoked() { |
396 |
> |
m.checkExecutionMode(); |
397 |
> |
assertEquals(0, invocationCount++); |
398 |
> |
} |
399 |
> |
void assertNotInvoked() { assertEquals(0, invocationCount); } |
400 |
> |
void assertInvoked() { assertEquals(1, invocationCount); } |
401 |
> |
} |
402 |
> |
|
403 |
> |
abstract class CheckedIntegerAction extends CheckedAction { |
404 |
> |
Integer value; |
405 |
> |
CheckedIntegerAction(ExecutionMode m) { super(m); } |
406 |
> |
void assertValue(Integer expected) { |
407 |
> |
assertInvoked(); |
408 |
> |
assertEquals(expected, value); |
409 |
> |
} |
410 |
> |
} |
411 |
> |
|
412 |
> |
class IntegerSupplier extends CheckedAction |
413 |
> |
implements Supplier<Integer> |
414 |
> |
{ |
415 |
> |
final Integer value; |
416 |
> |
IntegerSupplier(ExecutionMode m, Integer value) { |
417 |
> |
super(m); |
418 |
> |
this.value = value; |
419 |
> |
} |
420 |
> |
public Integer get() { |
421 |
> |
invoked(); |
422 |
> |
return value; |
423 |
> |
} |
424 |
> |
} |
425 |
> |
|
426 |
> |
// A function that handles and produces null values as well. |
427 |
> |
static Integer inc(Integer x) { |
428 |
> |
return (x == null) ? null : x + 1; |
429 |
> |
} |
430 |
> |
|
431 |
> |
class NoopConsumer extends CheckedIntegerAction |
432 |
> |
implements Consumer<Integer> |
433 |
> |
{ |
434 |
> |
NoopConsumer(ExecutionMode m) { super(m); } |
435 |
> |
public void accept(Integer x) { |
436 |
> |
invoked(); |
437 |
> |
value = x; |
438 |
> |
} |
439 |
> |
} |
440 |
> |
|
441 |
> |
class IncFunction extends CheckedIntegerAction |
442 |
> |
implements Function<Integer,Integer> |
443 |
> |
{ |
444 |
> |
IncFunction(ExecutionMode m) { super(m); } |
445 |
> |
public Integer apply(Integer x) { |
446 |
> |
invoked(); |
447 |
> |
return value = inc(x); |
448 |
> |
} |
449 |
> |
} |
450 |
> |
|
451 |
> |
// Choose non-commutative actions for better coverage |
452 |
> |
// A non-commutative function that handles and produces null values as well. |
453 |
> |
static Integer subtract(Integer x, Integer y) { |
454 |
> |
return (x == null && y == null) ? null : |
455 |
> |
((x == null) ? 42 : x.intValue()) |
456 |
> |
- ((y == null) ? 99 : y.intValue()); |
457 |
|
} |
458 |
< |
static final class AddAction implements BiConsumer<Integer, Integer> { |
459 |
< |
int value; |
458 |
> |
|
459 |
> |
class SubtractAction extends CheckedIntegerAction |
460 |
> |
implements BiConsumer<Integer, Integer> |
461 |
> |
{ |
462 |
> |
SubtractAction(ExecutionMode m) { super(m); } |
463 |
|
public void accept(Integer x, Integer y) { |
464 |
< |
value = x.intValue() + y.intValue(); |
464 |
> |
invoked(); |
465 |
> |
value = subtract(x, y); |
466 |
> |
} |
467 |
> |
} |
468 |
> |
|
469 |
> |
class SubtractFunction extends CheckedIntegerAction |
470 |
> |
implements BiFunction<Integer, Integer, Integer> |
471 |
> |
{ |
472 |
> |
SubtractFunction(ExecutionMode m) { super(m); } |
473 |
> |
public Integer apply(Integer x, Integer y) { |
474 |
> |
invoked(); |
475 |
> |
return value = subtract(x, y); |
476 |
|
} |
477 |
|
} |
478 |
< |
static final class Noop implements Runnable { |
479 |
< |
boolean ran; |
480 |
< |
public void run() { ran = true; } |
478 |
> |
|
479 |
> |
class Noop extends CheckedAction implements Runnable { |
480 |
> |
Noop(ExecutionMode m) { super(m); } |
481 |
> |
public void run() { |
482 |
> |
invoked(); |
483 |
> |
} |
484 |
|
} |
485 |
|
|
486 |
< |
static final class FailingSupplier implements Supplier<Integer> { |
487 |
< |
boolean ran; |
488 |
< |
public Integer get() { ran = true; throw new CFException(); } |
486 |
> |
class FailingSupplier extends CheckedAction |
487 |
> |
implements Supplier<Integer> |
488 |
> |
{ |
489 |
> |
FailingSupplier(ExecutionMode m) { super(m); } |
490 |
> |
public Integer get() { |
491 |
> |
invoked(); |
492 |
> |
throw new CFException(); |
493 |
> |
} |
494 |
|
} |
495 |
< |
static final class FailingConsumer implements Consumer<Integer> { |
496 |
< |
boolean ran; |
497 |
< |
public void accept(Integer x) { ran = true; throw new CFException(); } |
495 |
> |
|
496 |
> |
class FailingConsumer extends CheckedIntegerAction |
497 |
> |
implements Consumer<Integer> |
498 |
> |
{ |
499 |
> |
FailingConsumer(ExecutionMode m) { super(m); } |
500 |
> |
public void accept(Integer x) { |
501 |
> |
invoked(); |
502 |
> |
value = x; |
503 |
> |
throw new CFException(); |
504 |
> |
} |
505 |
|
} |
506 |
< |
static final class FailingBiConsumer implements BiConsumer<Integer, Integer> { |
507 |
< |
boolean ran; |
508 |
< |
public void accept(Integer x, Integer y) { ran = true; throw new CFException(); } |
506 |
> |
|
507 |
> |
class FailingBiConsumer extends CheckedIntegerAction |
508 |
> |
implements BiConsumer<Integer, Integer> |
509 |
> |
{ |
510 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); } |
511 |
> |
public void accept(Integer x, Integer y) { |
512 |
> |
invoked(); |
513 |
> |
value = subtract(x, y); |
514 |
> |
throw new CFException(); |
515 |
> |
} |
516 |
|
} |
517 |
< |
static final class FailingFunction implements Function<Integer, Integer> { |
518 |
< |
boolean ran; |
519 |
< |
public Integer apply(Integer x) { ran = true; throw new CFException(); } |
517 |
> |
|
518 |
> |
class FailingFunction extends CheckedIntegerAction |
519 |
> |
implements Function<Integer, Integer> |
520 |
> |
{ |
521 |
> |
FailingFunction(ExecutionMode m) { super(m); } |
522 |
> |
public Integer apply(Integer x) { |
523 |
> |
invoked(); |
524 |
> |
value = x; |
525 |
> |
throw new CFException(); |
526 |
> |
} |
527 |
|
} |
528 |
< |
static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> { |
529 |
< |
boolean ran; |
530 |
< |
public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); } |
528 |
> |
|
529 |
> |
class FailingBiFunction extends CheckedIntegerAction |
530 |
> |
implements BiFunction<Integer, Integer, Integer> |
531 |
> |
{ |
532 |
> |
FailingBiFunction(ExecutionMode m) { super(m); } |
533 |
> |
public Integer apply(Integer x, Integer y) { |
534 |
> |
invoked(); |
535 |
> |
value = subtract(x, y); |
536 |
> |
throw new CFException(); |
537 |
> |
} |
538 |
|
} |
539 |
< |
static final class FailingNoop implements Runnable { |
540 |
< |
boolean ran; |
541 |
< |
public void run() { ran = true; throw new CFException(); } |
539 |
> |
|
540 |
> |
class FailingRunnable extends CheckedAction implements Runnable { |
541 |
> |
FailingRunnable(ExecutionMode m) { super(m); } |
542 |
> |
public void run() { |
543 |
> |
invoked(); |
544 |
> |
throw new CFException(); |
545 |
> |
} |
546 |
|
} |
547 |
|
|
548 |
< |
static final class CompletableFutureInc implements Function<Integer, CompletableFuture<Integer>> { |
548 |
> |
|
549 |
> |
class CompletableFutureInc extends CheckedIntegerAction |
550 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
551 |
> |
{ |
552 |
> |
CompletableFutureInc(ExecutionMode m) { super(m); } |
553 |
|
public CompletableFuture<Integer> apply(Integer x) { |
554 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
555 |
< |
f.complete(Integer.valueOf(x.intValue() + 1)); |
554 |
> |
invoked(); |
555 |
> |
value = x; |
556 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
557 |
> |
assertTrue(f.complete(inc(x))); |
558 |
|
return f; |
559 |
|
} |
560 |
|
} |
561 |
|
|
562 |
< |
static final class FailingCompletableFutureFunction implements Function<Integer, CompletableFuture<Integer>> { |
563 |
< |
boolean ran; |
562 |
> |
class FailingCompletableFutureFunction extends CheckedIntegerAction |
563 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
564 |
> |
{ |
565 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); } |
566 |
|
public CompletableFuture<Integer> apply(Integer x) { |
567 |
< |
ran = true; throw new CFException(); |
567 |
> |
invoked(); |
568 |
> |
value = x; |
569 |
> |
throw new CFException(); |
570 |
|
} |
571 |
|
} |
572 |
|
|
573 |
|
// Used for explicit executor tests |
574 |
|
static final class ThreadExecutor implements Executor { |
575 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
576 |
+ |
static final ThreadGroup tg = new ThreadGroup("ThreadExecutor"); |
577 |
+ |
static boolean startedCurrentThread() { |
578 |
+ |
return Thread.currentThread().getThreadGroup() == tg; |
579 |
+ |
} |
580 |
+ |
|
581 |
|
public void execute(Runnable r) { |
582 |
< |
new Thread(r).start(); |
582 |
> |
count.getAndIncrement(); |
583 |
> |
new Thread(tg, r).start(); |
584 |
|
} |
585 |
|
} |
586 |
|
|
587 |
< |
static final class ExceptionToInteger implements Function<Throwable, Integer> { |
588 |
< |
public Integer apply(Throwable x) { return Integer.valueOf(3); } |
589 |
< |
} |
587 |
> |
/** |
588 |
> |
* Permits the testing of parallel code for the 3 different |
589 |
> |
* execution modes without copy/pasting all the test methods. |
590 |
> |
*/ |
591 |
> |
enum ExecutionMode { |
592 |
> |
SYNC { |
593 |
> |
public void checkExecutionMode() { |
594 |
> |
assertFalse(ThreadExecutor.startedCurrentThread()); |
595 |
> |
assertNull(ForkJoinTask.getPool()); |
596 |
> |
} |
597 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
598 |
> |
throw new UnsupportedOperationException(); |
599 |
> |
} |
600 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
601 |
> |
throw new UnsupportedOperationException(); |
602 |
> |
} |
603 |
> |
public <T> CompletableFuture<Void> thenRun |
604 |
> |
(CompletableFuture<T> f, Runnable a) { |
605 |
> |
return f.thenRun(a); |
606 |
> |
} |
607 |
> |
public <T> CompletableFuture<Void> thenAccept |
608 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
609 |
> |
return f.thenAccept(a); |
610 |
> |
} |
611 |
> |
public <T,U> CompletableFuture<U> thenApply |
612 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
613 |
> |
return f.thenApply(a); |
614 |
> |
} |
615 |
> |
public <T,U> CompletableFuture<U> thenCompose |
616 |
> |
(CompletableFuture<T> f, |
617 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
618 |
> |
return f.thenCompose(a); |
619 |
> |
} |
620 |
> |
public <T,U> CompletableFuture<U> handle |
621 |
> |
(CompletableFuture<T> f, |
622 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
623 |
> |
return f.handle(a); |
624 |
> |
} |
625 |
> |
public <T> CompletableFuture<T> whenComplete |
626 |
> |
(CompletableFuture<T> f, |
627 |
> |
BiConsumer<? super T,? super Throwable> a) { |
628 |
> |
return f.whenComplete(a); |
629 |
> |
} |
630 |
> |
public <T,U> CompletableFuture<Void> runAfterBoth |
631 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
632 |
> |
return f.runAfterBoth(g, a); |
633 |
> |
} |
634 |
> |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
635 |
> |
(CompletableFuture<T> f, |
636 |
> |
CompletionStage<? extends U> g, |
637 |
> |
BiConsumer<? super T,? super U> a) { |
638 |
> |
return f.thenAcceptBoth(g, a); |
639 |
> |
} |
640 |
> |
public <T,U,V> CompletableFuture<V> thenCombine |
641 |
> |
(CompletableFuture<T> f, |
642 |
> |
CompletionStage<? extends U> g, |
643 |
> |
BiFunction<? super T,? super U,? extends V> a) { |
644 |
> |
return f.thenCombine(g, a); |
645 |
> |
} |
646 |
> |
public <T> CompletableFuture<Void> runAfterEither |
647 |
> |
(CompletableFuture<T> f, |
648 |
> |
CompletionStage<?> g, |
649 |
> |
java.lang.Runnable a) { |
650 |
> |
return f.runAfterEither(g, a); |
651 |
> |
} |
652 |
> |
public <T> CompletableFuture<Void> acceptEither |
653 |
> |
(CompletableFuture<T> f, |
654 |
> |
CompletionStage<? extends T> g, |
655 |
> |
Consumer<? super T> a) { |
656 |
> |
return f.acceptEither(g, a); |
657 |
> |
} |
658 |
> |
public <T,U> CompletableFuture<U> applyToEither |
659 |
> |
(CompletableFuture<T> f, |
660 |
> |
CompletionStage<? extends T> g, |
661 |
> |
Function<? super T,U> a) { |
662 |
> |
return f.applyToEither(g, a); |
663 |
> |
} |
664 |
> |
}, |
665 |
|
|
666 |
< |
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
667 |
< |
public Integer apply(Integer x, Throwable t) { |
668 |
< |
return (t == null) ? two : three; |
669 |
< |
} |
670 |
< |
} |
666 |
> |
ASYNC { |
667 |
> |
public void checkExecutionMode() { |
668 |
> |
assertSame(ForkJoinPool.commonPool(), |
669 |
> |
ForkJoinTask.getPool()); |
670 |
> |
} |
671 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
672 |
> |
return CompletableFuture.runAsync(a); |
673 |
> |
} |
674 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
675 |
> |
return CompletableFuture.supplyAsync(a); |
676 |
> |
} |
677 |
> |
public <T> CompletableFuture<Void> thenRun |
678 |
> |
(CompletableFuture<T> f, Runnable a) { |
679 |
> |
return f.thenRunAsync(a); |
680 |
> |
} |
681 |
> |
public <T> CompletableFuture<Void> thenAccept |
682 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
683 |
> |
return f.thenAcceptAsync(a); |
684 |
> |
} |
685 |
> |
public <T,U> CompletableFuture<U> thenApply |
686 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
687 |
> |
return f.thenApplyAsync(a); |
688 |
> |
} |
689 |
> |
public <T,U> CompletableFuture<U> thenCompose |
690 |
> |
(CompletableFuture<T> f, |
691 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
692 |
> |
return f.thenComposeAsync(a); |
693 |
> |
} |
694 |
> |
public <T,U> CompletableFuture<U> handle |
695 |
> |
(CompletableFuture<T> f, |
696 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
697 |
> |
return f.handleAsync(a); |
698 |
> |
} |
699 |
> |
public <T> CompletableFuture<T> whenComplete |
700 |
> |
(CompletableFuture<T> f, |
701 |
> |
BiConsumer<? super T,? super Throwable> a) { |
702 |
> |
return f.whenCompleteAsync(a); |
703 |
> |
} |
704 |
> |
public <T,U> CompletableFuture<Void> runAfterBoth |
705 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
706 |
> |
return f.runAfterBothAsync(g, a); |
707 |
> |
} |
708 |
> |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
709 |
> |
(CompletableFuture<T> f, |
710 |
> |
CompletionStage<? extends U> g, |
711 |
> |
BiConsumer<? super T,? super U> a) { |
712 |
> |
return f.thenAcceptBothAsync(g, a); |
713 |
> |
} |
714 |
> |
public <T,U,V> CompletableFuture<V> thenCombine |
715 |
> |
(CompletableFuture<T> f, |
716 |
> |
CompletionStage<? extends U> g, |
717 |
> |
BiFunction<? super T,? super U,? extends V> a) { |
718 |
> |
return f.thenCombineAsync(g, a); |
719 |
> |
} |
720 |
> |
public <T> CompletableFuture<Void> runAfterEither |
721 |
> |
(CompletableFuture<T> f, |
722 |
> |
CompletionStage<?> g, |
723 |
> |
java.lang.Runnable a) { |
724 |
> |
return f.runAfterEitherAsync(g, a); |
725 |
> |
} |
726 |
> |
public <T> CompletableFuture<Void> acceptEither |
727 |
> |
(CompletableFuture<T> f, |
728 |
> |
CompletionStage<? extends T> g, |
729 |
> |
Consumer<? super T> a) { |
730 |
> |
return f.acceptEitherAsync(g, a); |
731 |
> |
} |
732 |
> |
public <T,U> CompletableFuture<U> applyToEither |
733 |
> |
(CompletableFuture<T> f, |
734 |
> |
CompletionStage<? extends T> g, |
735 |
> |
Function<? super T,U> a) { |
736 |
> |
return f.applyToEitherAsync(g, a); |
737 |
> |
} |
738 |
> |
}, |
739 |
> |
|
740 |
> |
EXECUTOR { |
741 |
> |
public void checkExecutionMode() { |
742 |
> |
assertTrue(ThreadExecutor.startedCurrentThread()); |
743 |
> |
} |
744 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
745 |
> |
return CompletableFuture.runAsync(a, new ThreadExecutor()); |
746 |
> |
} |
747 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
748 |
> |
return CompletableFuture.supplyAsync(a, new ThreadExecutor()); |
749 |
> |
} |
750 |
> |
public <T> CompletableFuture<Void> thenRun |
751 |
> |
(CompletableFuture<T> f, Runnable a) { |
752 |
> |
return f.thenRunAsync(a, new ThreadExecutor()); |
753 |
> |
} |
754 |
> |
public <T> CompletableFuture<Void> thenAccept |
755 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
756 |
> |
return f.thenAcceptAsync(a, new ThreadExecutor()); |
757 |
> |
} |
758 |
> |
public <T,U> CompletableFuture<U> thenApply |
759 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
760 |
> |
return f.thenApplyAsync(a, new ThreadExecutor()); |
761 |
> |
} |
762 |
> |
public <T,U> CompletableFuture<U> thenCompose |
763 |
> |
(CompletableFuture<T> f, |
764 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
765 |
> |
return f.thenComposeAsync(a, new ThreadExecutor()); |
766 |
> |
} |
767 |
> |
public <T,U> CompletableFuture<U> handle |
768 |
> |
(CompletableFuture<T> f, |
769 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
770 |
> |
return f.handleAsync(a, new ThreadExecutor()); |
771 |
> |
} |
772 |
> |
public <T> CompletableFuture<T> whenComplete |
773 |
> |
(CompletableFuture<T> f, |
774 |
> |
BiConsumer<? super T,? super Throwable> a) { |
775 |
> |
return f.whenCompleteAsync(a, new ThreadExecutor()); |
776 |
> |
} |
777 |
> |
public <T,U> CompletableFuture<Void> runAfterBoth |
778 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
779 |
> |
return f.runAfterBothAsync(g, a, new ThreadExecutor()); |
780 |
> |
} |
781 |
> |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
782 |
> |
(CompletableFuture<T> f, |
783 |
> |
CompletionStage<? extends U> g, |
784 |
> |
BiConsumer<? super T,? super U> a) { |
785 |
> |
return f.thenAcceptBothAsync(g, a, new ThreadExecutor()); |
786 |
> |
} |
787 |
> |
public <T,U,V> CompletableFuture<V> thenCombine |
788 |
> |
(CompletableFuture<T> f, |
789 |
> |
CompletionStage<? extends U> g, |
790 |
> |
BiFunction<? super T,? super U,? extends V> a) { |
791 |
> |
return f.thenCombineAsync(g, a, new ThreadExecutor()); |
792 |
> |
} |
793 |
> |
public <T> CompletableFuture<Void> runAfterEither |
794 |
> |
(CompletableFuture<T> f, |
795 |
> |
CompletionStage<?> g, |
796 |
> |
java.lang.Runnable a) { |
797 |
> |
return f.runAfterEitherAsync(g, a, new ThreadExecutor()); |
798 |
> |
} |
799 |
> |
public <T> CompletableFuture<Void> acceptEither |
800 |
> |
(CompletableFuture<T> f, |
801 |
> |
CompletionStage<? extends T> g, |
802 |
> |
Consumer<? super T> a) { |
803 |
> |
return f.acceptEitherAsync(g, a, new ThreadExecutor()); |
804 |
> |
} |
805 |
> |
public <T,U> CompletableFuture<U> applyToEither |
806 |
> |
(CompletableFuture<T> f, |
807 |
> |
CompletionStage<? extends T> g, |
808 |
> |
Function<? super T,U> a) { |
809 |
> |
return f.applyToEitherAsync(g, a, new ThreadExecutor()); |
810 |
> |
} |
811 |
> |
}; |
812 |
|
|
813 |
+ |
public abstract void checkExecutionMode(); |
814 |
+ |
public abstract CompletableFuture<Void> runAsync(Runnable a); |
815 |
+ |
public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); |
816 |
+ |
public abstract <T> CompletableFuture<Void> thenRun |
817 |
+ |
(CompletableFuture<T> f, Runnable a); |
818 |
+ |
public abstract <T> CompletableFuture<Void> thenAccept |
819 |
+ |
(CompletableFuture<T> f, Consumer<? super T> a); |
820 |
+ |
public abstract <T,U> CompletableFuture<U> thenApply |
821 |
+ |
(CompletableFuture<T> f, Function<? super T,U> a); |
822 |
+ |
public abstract <T,U> CompletableFuture<U> thenCompose |
823 |
+ |
(CompletableFuture<T> f, |
824 |
+ |
Function<? super T,? extends CompletionStage<U>> a); |
825 |
+ |
public abstract <T,U> CompletableFuture<U> handle |
826 |
+ |
(CompletableFuture<T> f, |
827 |
+ |
BiFunction<? super T,Throwable,? extends U> a); |
828 |
+ |
public abstract <T> CompletableFuture<T> whenComplete |
829 |
+ |
(CompletableFuture<T> f, |
830 |
+ |
BiConsumer<? super T,? super Throwable> a); |
831 |
+ |
public abstract <T,U> CompletableFuture<Void> runAfterBoth |
832 |
+ |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a); |
833 |
+ |
public abstract <T,U> CompletableFuture<Void> thenAcceptBoth |
834 |
+ |
(CompletableFuture<T> f, |
835 |
+ |
CompletionStage<? extends U> g, |
836 |
+ |
BiConsumer<? super T,? super U> a); |
837 |
+ |
public abstract <T,U,V> CompletableFuture<V> thenCombine |
838 |
+ |
(CompletableFuture<T> f, |
839 |
+ |
CompletionStage<? extends U> g, |
840 |
+ |
BiFunction<? super T,? super U,? extends V> a); |
841 |
+ |
public abstract <T> CompletableFuture<Void> runAfterEither |
842 |
+ |
(CompletableFuture<T> f, |
843 |
+ |
CompletionStage<?> g, |
844 |
+ |
java.lang.Runnable a); |
845 |
+ |
public abstract <T> CompletableFuture<Void> acceptEither |
846 |
+ |
(CompletableFuture<T> f, |
847 |
+ |
CompletionStage<? extends T> g, |
848 |
+ |
Consumer<? super T> a); |
849 |
+ |
public abstract <T,U> CompletableFuture<U> applyToEither |
850 |
+ |
(CompletableFuture<T> f, |
851 |
+ |
CompletionStage<? extends T> g, |
852 |
+ |
Function<? super T,U> a); |
853 |
+ |
} |
854 |
+ |
|
855 |
+ |
/** |
856 |
+ |
* exceptionally action is not invoked when source completes |
857 |
+ |
* normally, and source result is propagated |
858 |
+ |
*/ |
859 |
+ |
public void testExceptionally_normalCompletion() { |
860 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
861 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
862 |
+ |
{ |
863 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
864 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
865 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
866 |
+ |
final CompletableFuture<Integer> g = f.exceptionally |
867 |
+ |
((Throwable t) -> { |
868 |
+ |
// Should not be called |
869 |
+ |
a.getAndIncrement(); |
870 |
+ |
throw new AssertionError(); |
871 |
+ |
}); |
872 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
873 |
+ |
|
874 |
+ |
checkCompletedNormally(g, v1); |
875 |
+ |
checkCompletedNormally(f, v1); |
876 |
+ |
assertEquals(0, a.get()); |
877 |
+ |
}} |
878 |
|
|
879 |
|
/** |
880 |
|
* exceptionally action completes with function value on source |
881 |
< |
* exception; otherwise with source value |
881 |
> |
* exception |
882 |
|
*/ |
883 |
< |
public void testExceptionally() { |
884 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
885 |
< |
ExceptionToInteger r = new ExceptionToInteger(); |
886 |
< |
CompletableFuture<Integer> g = f.exceptionally(r); |
887 |
< |
f.completeExceptionally(new CFException()); |
888 |
< |
checkCompletedNormally(g, three); |
883 |
> |
public void testExceptionally_exceptionalCompletion() { |
884 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
885 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
886 |
> |
{ |
887 |
> |
final AtomicInteger a = new AtomicInteger(0); |
888 |
> |
final CFException ex = new CFException(); |
889 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
890 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
891 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
892 |
> |
((Throwable t) -> { |
893 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
894 |
> |
threadAssertSame(t, ex); |
895 |
> |
a.getAndIncrement(); |
896 |
> |
return v1; |
897 |
> |
}); |
898 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
899 |
> |
|
900 |
> |
checkCompletedNormally(g, v1); |
901 |
> |
assertEquals(1, a.get()); |
902 |
> |
}} |
903 |
> |
|
904 |
> |
public void testExceptionally_exceptionalCompletionActionFailed() { |
905 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
906 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
907 |
> |
{ |
908 |
> |
final AtomicInteger a = new AtomicInteger(0); |
909 |
> |
final CFException ex1 = new CFException(); |
910 |
> |
final CFException ex2 = new CFException(); |
911 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
912 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
913 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
914 |
> |
((Throwable t) -> { |
915 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
916 |
> |
threadAssertSame(t, ex1); |
917 |
> |
a.getAndIncrement(); |
918 |
> |
throw ex2; |
919 |
> |
}); |
920 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
921 |
> |
|
922 |
> |
checkCompletedWithWrappedException(g, ex2); |
923 |
> |
assertEquals(1, a.get()); |
924 |
> |
}} |
925 |
> |
|
926 |
> |
/** |
927 |
> |
* whenComplete action executes on normal completion, propagating |
928 |
> |
* source result. |
929 |
> |
*/ |
930 |
> |
public void testWhenComplete_normalCompletion1() { |
931 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
932 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
933 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
934 |
> |
{ |
935 |
> |
final AtomicInteger a = new AtomicInteger(0); |
936 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
937 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
938 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
939 |
> |
(f, |
940 |
> |
(Integer x, Throwable t) -> { |
941 |
> |
m.checkExecutionMode(); |
942 |
> |
threadAssertSame(x, v1); |
943 |
> |
threadAssertNull(t); |
944 |
> |
a.getAndIncrement(); |
945 |
> |
}); |
946 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
947 |
> |
|
948 |
> |
checkCompletedNormally(g, v1); |
949 |
> |
checkCompletedNormally(f, v1); |
950 |
> |
assertEquals(1, a.get()); |
951 |
> |
}} |
952 |
> |
|
953 |
> |
/** |
954 |
> |
* whenComplete action executes on exceptional completion, propagating |
955 |
> |
* source result. |
956 |
> |
*/ |
957 |
> |
public void testWhenComplete_exceptionalCompletion() { |
958 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
959 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
960 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
961 |
> |
{ |
962 |
> |
final AtomicInteger a = new AtomicInteger(0); |
963 |
> |
final CFException ex = new CFException(); |
964 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
965 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
966 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
967 |
> |
(f, |
968 |
> |
(Integer x, Throwable t) -> { |
969 |
> |
m.checkExecutionMode(); |
970 |
> |
threadAssertNull(x); |
971 |
> |
threadAssertSame(t, ex); |
972 |
> |
a.getAndIncrement(); |
973 |
> |
}); |
974 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
975 |
> |
|
976 |
> |
checkCompletedWithWrappedException(g, ex); |
977 |
> |
checkCompletedExceptionally(f, ex); |
978 |
> |
assertEquals(1, a.get()); |
979 |
> |
}} |
980 |
> |
|
981 |
> |
/** |
982 |
> |
* whenComplete action executes on cancelled source, propagating |
983 |
> |
* CancellationException. |
984 |
> |
*/ |
985 |
> |
public void testWhenComplete_sourceCancelled() { |
986 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
987 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
988 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
989 |
> |
{ |
990 |
> |
final AtomicInteger a = new AtomicInteger(0); |
991 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
992 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
993 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
994 |
> |
(f, |
995 |
> |
(Integer x, Throwable t) -> { |
996 |
> |
m.checkExecutionMode(); |
997 |
> |
threadAssertNull(x); |
998 |
> |
threadAssertTrue(t instanceof CancellationException); |
999 |
> |
a.getAndIncrement(); |
1000 |
> |
}); |
1001 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1002 |
|
|
1003 |
< |
f = new CompletableFuture<Integer>(); |
1004 |
< |
r = new ExceptionToInteger(); |
1005 |
< |
g = f.exceptionally(r); |
1006 |
< |
f.complete(one); |
379 |
< |
checkCompletedNormally(g, one); |
380 |
< |
} |
1003 |
> |
checkCompletedWithWrappedCancellationException(g); |
1004 |
> |
checkCancelled(f); |
1005 |
> |
assertEquals(1, a.get()); |
1006 |
> |
}} |
1007 |
|
|
1008 |
|
/** |
1009 |
< |
* handle action completes normally with function value on either |
1010 |
< |
* normal or exceptional completion of source |
1009 |
> |
* If a whenComplete action throws an exception when triggered by |
1010 |
> |
* a normal completion, it completes exceptionally |
1011 |
|
*/ |
1012 |
< |
public void testHandle() { |
1013 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1014 |
< |
IntegerHandler r = new IntegerHandler(); |
1015 |
< |
CompletableFuture<Integer> g = f.handle(r); |
1016 |
< |
f.completeExceptionally(new CFException()); |
1017 |
< |
checkCompletedNormally(g, three); |
1012 |
> |
public void testWhenComplete_actionFailed() { |
1013 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1014 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1015 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1016 |
> |
{ |
1017 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1018 |
> |
final CFException ex = new CFException(); |
1019 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1020 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1021 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1022 |
> |
(f, |
1023 |
> |
(Integer x, Throwable t) -> { |
1024 |
> |
m.checkExecutionMode(); |
1025 |
> |
threadAssertSame(x, v1); |
1026 |
> |
threadAssertNull(t); |
1027 |
> |
a.getAndIncrement(); |
1028 |
> |
throw ex; |
1029 |
> |
}); |
1030 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1031 |
> |
|
1032 |
> |
checkCompletedWithWrappedException(g, ex); |
1033 |
> |
checkCompletedNormally(f, v1); |
1034 |
> |
assertEquals(1, a.get()); |
1035 |
> |
}} |
1036 |
> |
|
1037 |
> |
/** |
1038 |
> |
* If a whenComplete action throws an exception when triggered by |
1039 |
> |
* a source completion that also throws an exception, the source |
1040 |
> |
* exception takes precedence. |
1041 |
> |
*/ |
1042 |
> |
public void testWhenComplete_actionFailedSourceFailed() { |
1043 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1044 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1045 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1046 |
> |
{ |
1047 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1048 |
> |
final CFException ex1 = new CFException(); |
1049 |
> |
final CFException ex2 = new CFException(); |
1050 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1051 |
> |
|
1052 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1053 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1054 |
> |
(f, |
1055 |
> |
(Integer x, Throwable t) -> { |
1056 |
> |
m.checkExecutionMode(); |
1057 |
> |
threadAssertSame(t, ex1); |
1058 |
> |
threadAssertNull(x); |
1059 |
> |
a.getAndIncrement(); |
1060 |
> |
throw ex2; |
1061 |
> |
}); |
1062 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1063 |
> |
|
1064 |
> |
checkCompletedWithWrappedException(g, ex1); |
1065 |
> |
checkCompletedExceptionally(f, ex1); |
1066 |
> |
assertEquals(1, a.get()); |
1067 |
> |
}} |
1068 |
> |
|
1069 |
> |
/** |
1070 |
> |
* handle action completes normally with function value on normal |
1071 |
> |
* completion of source |
1072 |
> |
*/ |
1073 |
> |
public void testHandle_normalCompletion() { |
1074 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1075 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1076 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1077 |
> |
{ |
1078 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1079 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1080 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1081 |
> |
final CompletableFuture<Integer> g = m.handle |
1082 |
> |
(f, |
1083 |
> |
(Integer x, Throwable t) -> { |
1084 |
> |
m.checkExecutionMode(); |
1085 |
> |
threadAssertSame(x, v1); |
1086 |
> |
threadAssertNull(t); |
1087 |
> |
a.getAndIncrement(); |
1088 |
> |
return inc(v1); |
1089 |
> |
}); |
1090 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1091 |
> |
|
1092 |
> |
checkCompletedNormally(g, inc(v1)); |
1093 |
> |
checkCompletedNormally(f, v1); |
1094 |
> |
assertEquals(1, a.get()); |
1095 |
> |
}} |
1096 |
|
|
1097 |
< |
f = new CompletableFuture<Integer>(); |
1098 |
< |
r = new IntegerHandler(); |
1099 |
< |
g = f.handle(r); |
1100 |
< |
f.complete(one); |
1101 |
< |
checkCompletedNormally(g, two); |
1102 |
< |
} |
1097 |
> |
/** |
1098 |
> |
* handle action completes normally with function value on |
1099 |
> |
* exceptional completion of source |
1100 |
> |
*/ |
1101 |
> |
public void testHandle_exceptionalCompletion() { |
1102 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1103 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1104 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1105 |
> |
{ |
1106 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1107 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1108 |
> |
final CFException ex = new CFException(); |
1109 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
1110 |
> |
final CompletableFuture<Integer> g = m.handle |
1111 |
> |
(f, |
1112 |
> |
(Integer x, Throwable t) -> { |
1113 |
> |
m.checkExecutionMode(); |
1114 |
> |
threadAssertNull(x); |
1115 |
> |
threadAssertSame(t, ex); |
1116 |
> |
a.getAndIncrement(); |
1117 |
> |
return v1; |
1118 |
> |
}); |
1119 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
1120 |
> |
|
1121 |
> |
checkCompletedNormally(g, v1); |
1122 |
> |
checkCompletedExceptionally(f, ex); |
1123 |
> |
assertEquals(1, a.get()); |
1124 |
> |
}} |
1125 |
> |
|
1126 |
> |
/** |
1127 |
> |
* handle action completes normally with function value on |
1128 |
> |
* cancelled source |
1129 |
> |
*/ |
1130 |
> |
public void testHandle_sourceCancelled() { |
1131 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1132 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1133 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1134 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1135 |
> |
{ |
1136 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1137 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1138 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1139 |
> |
final CompletableFuture<Integer> g = m.handle |
1140 |
> |
(f, |
1141 |
> |
(Integer x, Throwable t) -> { |
1142 |
> |
m.checkExecutionMode(); |
1143 |
> |
threadAssertNull(x); |
1144 |
> |
threadAssertTrue(t instanceof CancellationException); |
1145 |
> |
a.getAndIncrement(); |
1146 |
> |
return v1; |
1147 |
> |
}); |
1148 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1149 |
> |
|
1150 |
> |
checkCompletedNormally(g, v1); |
1151 |
> |
checkCancelled(f); |
1152 |
> |
assertEquals(1, a.get()); |
1153 |
> |
}} |
1154 |
|
|
1155 |
|
/** |
1156 |
< |
* runAsync completes after running Runnable |
1156 |
> |
* handle result completes exceptionally if action does |
1157 |
|
*/ |
1158 |
< |
public void testRunAsync() { |
1159 |
< |
Noop r = new Noop(); |
1160 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
1161 |
< |
assertNull(f.join()); |
1162 |
< |
assertTrue(r.ran); |
1163 |
< |
checkCompletedNormally(f, null); |
1164 |
< |
} |
1158 |
> |
public void testHandle_sourceFailedActionFailed() { |
1159 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1160 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1161 |
> |
{ |
1162 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1163 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1164 |
> |
final CFException ex1 = new CFException(); |
1165 |
> |
final CFException ex2 = new CFException(); |
1166 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1167 |
> |
final CompletableFuture<Integer> g = m.handle |
1168 |
> |
(f, |
1169 |
> |
(Integer x, Throwable t) -> { |
1170 |
> |
m.checkExecutionMode(); |
1171 |
> |
threadAssertNull(x); |
1172 |
> |
threadAssertSame(ex1, t); |
1173 |
> |
a.getAndIncrement(); |
1174 |
> |
throw ex2; |
1175 |
> |
}); |
1176 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1177 |
> |
|
1178 |
> |
checkCompletedWithWrappedException(g, ex2); |
1179 |
> |
checkCompletedExceptionally(f, ex1); |
1180 |
> |
assertEquals(1, a.get()); |
1181 |
> |
}} |
1182 |
> |
|
1183 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1184 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1185 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1186 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1187 |
> |
{ |
1188 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1189 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1190 |
> |
final CFException ex = new CFException(); |
1191 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1192 |
> |
final CompletableFuture<Integer> g = m.handle |
1193 |
> |
(f, |
1194 |
> |
(Integer x, Throwable t) -> { |
1195 |
> |
m.checkExecutionMode(); |
1196 |
> |
threadAssertSame(x, v1); |
1197 |
> |
threadAssertNull(t); |
1198 |
> |
a.getAndIncrement(); |
1199 |
> |
throw ex; |
1200 |
> |
}); |
1201 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1202 |
> |
|
1203 |
> |
checkCompletedWithWrappedException(g, ex); |
1204 |
> |
checkCompletedNormally(f, v1); |
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 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor()); |
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()); |
418 |
– |
assertTrue(r.ran); |
1221 |
|
checkCompletedNormally(f, null); |
1222 |
< |
} |
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); |
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 |
|
checkCompletedWithWrappedCFException(f); |
1238 |
< |
assertTrue(r.ran); |
1239 |
< |
} |
1238 |
> |
r.assertInvoked(); |
1239 |
> |
}} |
1240 |
|
|
1241 |
|
/** |
1242 |
|
* supplyAsync completes with result of supplier |
1243 |
|
*/ |
1244 |
< |
public void testSupplyAsync() { |
1245 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne); |
1246 |
< |
assertEquals(f.join(), one); |
1247 |
< |
} |
1248 |
< |
|
1249 |
< |
/** |
1250 |
< |
* supplyAsync with executor completes with result of supplier |
1251 |
< |
*/ |
1252 |
< |
public void testSupplyAsync2() { |
1253 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
1254 |
< |
assertEquals(f.join(), one); |
1255 |
< |
} |
1244 |
> |
public void testSupplyAsync_normalCompletion() { |
1245 |
> |
ExecutionMode[] executionModes = { |
1246 |
> |
ExecutionMode.ASYNC, |
1247 |
> |
ExecutionMode.EXECUTOR, |
1248 |
> |
}; |
1249 |
> |
for (ExecutionMode m : executionModes) |
1250 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1251 |
> |
{ |
1252 |
> |
final IntegerSupplier r = new IntegerSupplier(m, v1); |
1253 |
> |
final CompletableFuture<Integer> f = m.supplyAsync(r); |
1254 |
> |
assertSame(v1, f.join()); |
1255 |
> |
checkCompletedNormally(f, v1); |
1256 |
> |
r.assertInvoked(); |
1257 |
> |
}} |
1258 |
|
|
1259 |
|
/** |
1260 |
|
* Failing supplyAsync completes exceptionally |
1261 |
|
*/ |
1262 |
< |
public void testSupplyAsync3() { |
1263 |
< |
FailingSupplier r = new FailingSupplier(); |
1264 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r); |
1262 |
> |
public void testSupplyAsync_exceptionalCompletion() { |
1263 |
> |
ExecutionMode[] executionModes = { |
1264 |
> |
ExecutionMode.ASYNC, |
1265 |
> |
ExecutionMode.EXECUTOR, |
1266 |
> |
}; |
1267 |
> |
for (ExecutionMode m : executionModes) |
1268 |
> |
{ |
1269 |
> |
FailingSupplier r = new FailingSupplier(m); |
1270 |
> |
CompletableFuture<Integer> f = m.supplyAsync(r); |
1271 |
|
checkCompletedWithWrappedCFException(f); |
1272 |
< |
assertTrue(r.ran); |
1273 |
< |
} |
1272 |
> |
r.assertInvoked(); |
1273 |
> |
}} |
1274 |
|
|
1275 |
|
// seq completion methods |
1276 |
|
|
1277 |
|
/** |
1278 |
|
* thenRun result completes normally after normal completion of source |
1279 |
|
*/ |
1280 |
< |
public void testThenRun() { |
1281 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1282 |
< |
Noop r = new Noop(); |
1283 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
1284 |
< |
f.complete(null); |
1285 |
< |
checkCompletedNormally(g, null); |
1286 |
< |
// reordered version |
1287 |
< |
f = new CompletableFuture<Integer>(); |
1288 |
< |
f.complete(null); |
1289 |
< |
r = new Noop(); |
1290 |
< |
g = f.thenRun(r); |
1291 |
< |
checkCompletedNormally(g, null); |
1292 |
< |
} |
1280 |
> |
public void testThenRun_normalCompletion() { |
1281 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1282 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1283 |
> |
{ |
1284 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1285 |
> |
final Noop[] rs = new Noop[6]; |
1286 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1287 |
> |
|
1288 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1289 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1290 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1291 |
> |
checkIncomplete(h0); |
1292 |
> |
checkIncomplete(h1); |
1293 |
> |
checkIncomplete(h2); |
1294 |
> |
assertTrue(f.complete(v1)); |
1295 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1296 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1297 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1298 |
> |
|
1299 |
> |
checkCompletedNormally(h0, null); |
1300 |
> |
checkCompletedNormally(h1, null); |
1301 |
> |
checkCompletedNormally(h2, null); |
1302 |
> |
checkCompletedNormally(h3, null); |
1303 |
> |
checkCompletedNormally(h4, null); |
1304 |
> |
checkCompletedNormally(h5, null); |
1305 |
> |
checkCompletedNormally(f, v1); |
1306 |
> |
for (Noop r : rs) r.assertInvoked(); |
1307 |
> |
}} |
1308 |
|
|
1309 |
|
/** |
1310 |
|
* thenRun result completes exceptionally after exceptional |
1311 |
|
* completion of source |
1312 |
|
*/ |
1313 |
< |
public void testThenRun2() { |
1314 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1315 |
< |
Noop r = new Noop(); |
1316 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
1317 |
< |
f.completeExceptionally(new CFException()); |
1318 |
< |
checkCompletedWithWrappedCFException(g); |
1319 |
< |
} |
1313 |
> |
public void testThenRun_exceptionalCompletion() { |
1314 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1315 |
> |
{ |
1316 |
> |
final CFException ex = new CFException(); |
1317 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1318 |
> |
final Noop[] rs = new Noop[6]; |
1319 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1320 |
> |
|
1321 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1322 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1323 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1324 |
> |
checkIncomplete(h0); |
1325 |
> |
checkIncomplete(h1); |
1326 |
> |
checkIncomplete(h2); |
1327 |
> |
assertTrue(f.completeExceptionally(ex)); |
1328 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1329 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1330 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1331 |
> |
|
1332 |
> |
checkCompletedWithWrappedException(h0, ex); |
1333 |
> |
checkCompletedWithWrappedException(h1, ex); |
1334 |
> |
checkCompletedWithWrappedException(h2, ex); |
1335 |
> |
checkCompletedWithWrappedException(h3, ex); |
1336 |
> |
checkCompletedWithWrappedException(h4, ex); |
1337 |
> |
checkCompletedWithWrappedException(h5, ex); |
1338 |
> |
checkCompletedExceptionally(f, ex); |
1339 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1340 |
> |
}} |
1341 |
|
|
1342 |
|
/** |
1343 |
< |
* thenRun result completes exceptionally if action does |
1343 |
> |
* thenRun result completes exceptionally if source cancelled |
1344 |
|
*/ |
1345 |
< |
public void testThenRun3() { |
1346 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1347 |
< |
FailingNoop r = new FailingNoop(); |
1348 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
1349 |
< |
f.complete(null); |
1350 |
< |
checkCompletedWithWrappedCFException(g); |
1351 |
< |
} |
1345 |
> |
public void testThenRun_sourceCancelled() { |
1346 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1347 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1348 |
> |
{ |
1349 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1350 |
> |
final Noop[] rs = new Noop[6]; |
1351 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1352 |
> |
|
1353 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1354 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1355 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1356 |
> |
checkIncomplete(h0); |
1357 |
> |
checkIncomplete(h1); |
1358 |
> |
checkIncomplete(h2); |
1359 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1360 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1361 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1362 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1363 |
> |
|
1364 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1365 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1366 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1367 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1368 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1369 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1370 |
> |
checkCancelled(f); |
1371 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1372 |
> |
}} |
1373 |
|
|
1374 |
|
/** |
1375 |
< |
* thenRun result completes exceptionally if source cancelled |
1375 |
> |
* thenRun result completes exceptionally if action does |
1376 |
|
*/ |
1377 |
< |
public void testThenRun4() { |
1378 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1379 |
< |
Noop r = new Noop(); |
1380 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
1381 |
< |
assertTrue(f.cancel(true)); |
1382 |
< |
checkCompletedWithWrappedCancellationException(g); |
1383 |
< |
} |
1377 |
> |
public void testThenRun_actionFailed() { |
1378 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1379 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1380 |
> |
{ |
1381 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1382 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1383 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1384 |
> |
|
1385 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1386 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1387 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1388 |
> |
assertTrue(f.complete(v1)); |
1389 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1390 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1391 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1392 |
> |
|
1393 |
> |
checkCompletedWithWrappedCFException(h0); |
1394 |
> |
checkCompletedWithWrappedCFException(h1); |
1395 |
> |
checkCompletedWithWrappedCFException(h2); |
1396 |
> |
checkCompletedWithWrappedCFException(h3); |
1397 |
> |
checkCompletedWithWrappedCFException(h4); |
1398 |
> |
checkCompletedWithWrappedCFException(h5); |
1399 |
> |
checkCompletedNormally(f, v1); |
1400 |
> |
}} |
1401 |
|
|
1402 |
|
/** |
1403 |
|
* thenApply result completes normally after normal completion of source |
1404 |
|
*/ |
1405 |
< |
public void testThenApply() { |
1406 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1407 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
1408 |
< |
f.complete(one); |
1409 |
< |
checkCompletedNormally(g, two); |
1410 |
< |
} |
1405 |
> |
public void testThenApply_normalCompletion() { |
1406 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1407 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1408 |
> |
{ |
1409 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1410 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1411 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1412 |
> |
|
1413 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1414 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1415 |
> |
checkIncomplete(h0); |
1416 |
> |
checkIncomplete(h1); |
1417 |
> |
assertTrue(f.complete(v1)); |
1418 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1419 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1420 |
> |
|
1421 |
> |
checkCompletedNormally(h0, inc(v1)); |
1422 |
> |
checkCompletedNormally(h1, inc(v1)); |
1423 |
> |
checkCompletedNormally(h2, inc(v1)); |
1424 |
> |
checkCompletedNormally(h3, inc(v1)); |
1425 |
> |
checkCompletedNormally(f, v1); |
1426 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1427 |
> |
}} |
1428 |
|
|
1429 |
|
/** |
1430 |
|
* thenApply result completes exceptionally after exceptional |
1431 |
|
* completion of source |
1432 |
|
*/ |
1433 |
< |
public void testThenApply2() { |
1434 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1435 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
1436 |
< |
f.completeExceptionally(new CFException()); |
1437 |
< |
checkCompletedWithWrappedCFException(g); |
1438 |
< |
} |
1433 |
> |
public void testThenApply_exceptionalCompletion() { |
1434 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1435 |
> |
{ |
1436 |
> |
final CFException ex = new CFException(); |
1437 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1438 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1439 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1440 |
> |
|
1441 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1442 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1443 |
> |
assertTrue(f.completeExceptionally(ex)); |
1444 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1445 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1446 |
> |
|
1447 |
> |
checkCompletedWithWrappedException(h0, ex); |
1448 |
> |
checkCompletedWithWrappedException(h1, ex); |
1449 |
> |
checkCompletedWithWrappedException(h2, ex); |
1450 |
> |
checkCompletedWithWrappedException(h3, ex); |
1451 |
> |
checkCompletedExceptionally(f, ex); |
1452 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1453 |
> |
}} |
1454 |
|
|
1455 |
|
/** |
1456 |
< |
* thenApply result completes exceptionally if action does |
1456 |
> |
* thenApply result completes exceptionally if source cancelled |
1457 |
|
*/ |
1458 |
< |
public void testThenApply3() { |
1459 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1460 |
< |
CompletableFuture<Integer> g = f.thenApply(new FailingFunction()); |
1461 |
< |
f.complete(one); |
1462 |
< |
checkCompletedWithWrappedCFException(g); |
1463 |
< |
} |
1458 |
> |
public void testThenApply_sourceCancelled() { |
1459 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1460 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1461 |
> |
{ |
1462 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1463 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1464 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1465 |
> |
|
1466 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1467 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1468 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1469 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1470 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1471 |
> |
|
1472 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1473 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1474 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1475 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1476 |
> |
checkCancelled(f); |
1477 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1478 |
> |
}} |
1479 |
|
|
1480 |
|
/** |
1481 |
< |
* thenApply result completes exceptionally if source cancelled |
1481 |
> |
* thenApply result completes exceptionally if action does |
1482 |
|
*/ |
1483 |
< |
public void testThenApply4() { |
1484 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1485 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
1486 |
< |
assertTrue(f.cancel(true)); |
1487 |
< |
checkCompletedWithWrappedCancellationException(g); |
1488 |
< |
} |
1483 |
> |
public void testThenApply_actionFailed() { |
1484 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1485 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1486 |
> |
{ |
1487 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1488 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1489 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1490 |
> |
|
1491 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1492 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1493 |
> |
assertTrue(f.complete(v1)); |
1494 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1495 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1496 |
> |
|
1497 |
> |
checkCompletedWithWrappedCFException(h0); |
1498 |
> |
checkCompletedWithWrappedCFException(h1); |
1499 |
> |
checkCompletedWithWrappedCFException(h2); |
1500 |
> |
checkCompletedWithWrappedCFException(h3); |
1501 |
> |
checkCompletedNormally(f, v1); |
1502 |
> |
}} |
1503 |
|
|
1504 |
|
/** |
1505 |
|
* thenAccept result completes normally after normal completion of source |
1506 |
|
*/ |
1507 |
< |
public void testThenAccept() { |
1508 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1509 |
< |
IncAction r = new IncAction(); |
1510 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1511 |
< |
f.complete(one); |
1512 |
< |
checkCompletedNormally(g, null); |
1513 |
< |
assertEquals(r.value, 2); |
1514 |
< |
} |
1507 |
> |
public void testThenAccept_normalCompletion() { |
1508 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1509 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1510 |
> |
{ |
1511 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1512 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1513 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1514 |
> |
|
1515 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1516 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1517 |
> |
checkIncomplete(h0); |
1518 |
> |
checkIncomplete(h1); |
1519 |
> |
assertTrue(f.complete(v1)); |
1520 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1521 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1522 |
> |
|
1523 |
> |
checkCompletedNormally(h0, null); |
1524 |
> |
checkCompletedNormally(h1, null); |
1525 |
> |
checkCompletedNormally(h2, null); |
1526 |
> |
checkCompletedNormally(h3, null); |
1527 |
> |
checkCompletedNormally(f, v1); |
1528 |
> |
for (NoopConsumer r : rs) r.assertValue(v1); |
1529 |
> |
}} |
1530 |
|
|
1531 |
|
/** |
1532 |
|
* thenAccept result completes exceptionally after exceptional |
1533 |
|
* completion of source |
1534 |
|
*/ |
1535 |
< |
public void testThenAccept2() { |
1536 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1537 |
< |
IncAction r = new IncAction(); |
1538 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1539 |
< |
f.completeExceptionally(new CFException()); |
1540 |
< |
checkCompletedWithWrappedCFException(g); |
1541 |
< |
} |
1542 |
< |
|
1543 |
< |
/** |
1544 |
< |
* thenAccept result completes exceptionally if action does |
1545 |
< |
*/ |
1546 |
< |
public void testThenAccept3() { |
1547 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1548 |
< |
FailingConsumer r = new FailingConsumer(); |
1549 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1550 |
< |
f.complete(one); |
1551 |
< |
checkCompletedWithWrappedCFException(g); |
1552 |
< |
assertTrue(r.ran); |
1553 |
< |
} |
1535 |
> |
public void testThenAccept_exceptionalCompletion() { |
1536 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1537 |
> |
{ |
1538 |
> |
final CFException ex = new CFException(); |
1539 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1540 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1541 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1542 |
> |
|
1543 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1544 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1545 |
> |
assertTrue(f.completeExceptionally(ex)); |
1546 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1547 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1548 |
> |
|
1549 |
> |
checkCompletedWithWrappedException(h0, ex); |
1550 |
> |
checkCompletedWithWrappedException(h1, ex); |
1551 |
> |
checkCompletedWithWrappedException(h2, ex); |
1552 |
> |
checkCompletedWithWrappedException(h3, ex); |
1553 |
> |
checkCompletedExceptionally(f, ex); |
1554 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1555 |
> |
}} |
1556 |
|
|
1557 |
|
/** |
1558 |
|
* thenAccept result completes exceptionally if source cancelled |
1559 |
|
*/ |
1560 |
< |
public void testThenAccept4() { |
1561 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1562 |
< |
IncAction r = new IncAction(); |
1563 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1564 |
< |
assertTrue(f.cancel(true)); |
1565 |
< |
checkCompletedWithWrappedCancellationException(g); |
1566 |
< |
} |
1567 |
< |
|
1560 |
> |
public void testThenAccept_sourceCancelled() { |
1561 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1562 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1563 |
> |
{ |
1564 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1565 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1566 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1567 |
> |
|
1568 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1569 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1570 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1571 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1572 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1573 |
> |
|
1574 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1575 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1576 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1577 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1578 |
> |
checkCancelled(f); |
1579 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1580 |
> |
}} |
1581 |
|
|
1582 |
|
/** |
1583 |
< |
* thenCombine result completes normally after normal completion of sources |
1583 |
> |
* thenAccept result completes exceptionally if action does |
1584 |
|
*/ |
1585 |
< |
public void testThenCombine() { |
1586 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1587 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1588 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, add); |
1589 |
< |
f.complete(one); |
1590 |
< |
checkIncomplete(g); |
1591 |
< |
f2.complete(two); |
1592 |
< |
checkCompletedNormally(g, three); |
1593 |
< |
|
1594 |
< |
f = new CompletableFuture<Integer>(); |
1595 |
< |
f.complete(one); |
1596 |
< |
f2 = new CompletableFuture<Integer>(); |
1597 |
< |
g = f.thenCombine(f2, add); |
1598 |
< |
checkIncomplete(g); |
1599 |
< |
f2.complete(two); |
1600 |
< |
checkCompletedNormally(g, three); |
1601 |
< |
} |
1585 |
> |
public void testThenAccept_actionFailed() { |
1586 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1587 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1588 |
> |
{ |
1589 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1590 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1591 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1592 |
> |
|
1593 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1594 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1595 |
> |
assertTrue(f.complete(v1)); |
1596 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1597 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1598 |
> |
|
1599 |
> |
checkCompletedWithWrappedCFException(h0); |
1600 |
> |
checkCompletedWithWrappedCFException(h1); |
1601 |
> |
checkCompletedWithWrappedCFException(h2); |
1602 |
> |
checkCompletedWithWrappedCFException(h3); |
1603 |
> |
checkCompletedNormally(f, v1); |
1604 |
> |
}} |
1605 |
> |
|
1606 |
> |
/** |
1607 |
> |
* thenCombine result completes normally after normal completion |
1608 |
> |
* of sources |
1609 |
> |
*/ |
1610 |
> |
public void testThenCombine_normalCompletion() { |
1611 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1612 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1613 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1614 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1615 |
> |
{ |
1616 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1617 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1618 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1619 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1620 |
> |
|
1621 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1622 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1623 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1624 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1625 |
> |
|
1626 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1627 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1628 |
> |
assertTrue(fst.complete(w1)); |
1629 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1630 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1631 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1632 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1633 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1634 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1635 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1636 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1637 |
> |
assertTrue(snd.complete(w2)); |
1638 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1639 |
> |
|
1640 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1641 |
> |
checkCompletedNormally(h2, subtract(v1, v2)); |
1642 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1643 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1644 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1645 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1646 |
> |
|
1647 |
> |
checkCompletedNormally(f, v1); |
1648 |
> |
checkCompletedNormally(g, v2); |
1649 |
> |
}} |
1650 |
|
|
1651 |
|
/** |
1652 |
|
* thenCombine result completes exceptionally after exceptional |
1653 |
|
* completion of either source |
1654 |
|
*/ |
1655 |
< |
public void testThenCombine2() { |
1656 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1657 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1658 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, add); |
1659 |
< |
f.completeExceptionally(new CFException()); |
1660 |
< |
f2.complete(two); |
1661 |
< |
checkCompletedWithWrappedCFException(g); |
1662 |
< |
|
1663 |
< |
f = new CompletableFuture<Integer>(); |
1664 |
< |
f.complete(one); |
1665 |
< |
f2 = new CompletableFuture<Integer>(); |
1666 |
< |
g = f.thenCombine(f2, add); |
1667 |
< |
f2.completeExceptionally(new CFException()); |
1668 |
< |
checkCompletedWithWrappedCFException(g); |
1669 |
< |
} |
1655 |
> |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
1656 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1657 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1658 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1659 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1660 |
> |
{ |
1661 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1662 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1663 |
> |
final CFException ex = new CFException(); |
1664 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1665 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1666 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1667 |
> |
|
1668 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1669 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1670 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1671 |
> |
() -> fst.completeExceptionally(ex) : |
1672 |
> |
() -> fst.complete(v1); |
1673 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1674 |
> |
() -> snd.complete(v1) : |
1675 |
> |
() -> snd.completeExceptionally(ex); |
1676 |
> |
|
1677 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1678 |
> |
assertTrue(complete1.call()); |
1679 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1680 |
> |
checkIncomplete(h1); |
1681 |
> |
checkIncomplete(h2); |
1682 |
> |
assertTrue(complete2.call()); |
1683 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1684 |
> |
|
1685 |
> |
checkCompletedWithWrappedException(h1, ex); |
1686 |
> |
checkCompletedWithWrappedException(h2, ex); |
1687 |
> |
checkCompletedWithWrappedException(h3, ex); |
1688 |
> |
r1.assertNotInvoked(); |
1689 |
> |
r2.assertNotInvoked(); |
1690 |
> |
r3.assertNotInvoked(); |
1691 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1692 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1693 |
> |
}} |
1694 |
|
|
1695 |
|
/** |
1696 |
< |
* thenCombine result completes exceptionally if action does |
1696 |
> |
* thenCombine result completes exceptionally if either source cancelled |
1697 |
|
*/ |
1698 |
< |
public void testThenCombine3() { |
1699 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1700 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1701 |
< |
FailingBiFunction r = new FailingBiFunction(); |
1702 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, r); |
1703 |
< |
f.complete(one); |
1704 |
< |
checkIncomplete(g); |
1705 |
< |
f2.complete(two); |
1706 |
< |
checkCompletedWithWrappedCFException(g); |
1707 |
< |
} |
1698 |
> |
public void testThenCombine_sourceCancelled() throws Throwable { |
1699 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1700 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1701 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1702 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1703 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1704 |
> |
{ |
1705 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1706 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1707 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1708 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1709 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1710 |
> |
|
1711 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1712 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1713 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1714 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1715 |
> |
() -> fst.complete(v1); |
1716 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1717 |
> |
() -> snd.complete(v1) : |
1718 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1719 |
> |
|
1720 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1721 |
> |
assertTrue(complete1.call()); |
1722 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1723 |
> |
checkIncomplete(h1); |
1724 |
> |
checkIncomplete(h2); |
1725 |
> |
assertTrue(complete2.call()); |
1726 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1727 |
> |
|
1728 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1729 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1730 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1731 |
> |
r1.assertNotInvoked(); |
1732 |
> |
r2.assertNotInvoked(); |
1733 |
> |
r3.assertNotInvoked(); |
1734 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1735 |
> |
checkCancelled(failFirst ? fst : snd); |
1736 |
> |
}} |
1737 |
|
|
1738 |
|
/** |
1739 |
< |
* thenCombine result completes exceptionally if either source cancelled |
1739 |
> |
* thenCombine result completes exceptionally if action does |
1740 |
|
*/ |
1741 |
< |
public void testThenCombine4() { |
1742 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1743 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1744 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, add); |
1745 |
< |
assertTrue(f.cancel(true)); |
1746 |
< |
f2.complete(two); |
1747 |
< |
checkCompletedWithWrappedCancellationException(g); |
1748 |
< |
f = new CompletableFuture<Integer>(); |
1749 |
< |
f2 = new CompletableFuture<Integer>(); |
1750 |
< |
g = f.thenCombine(f2, add); |
1751 |
< |
f.complete(one); |
1752 |
< |
assertTrue(f2.cancel(true)); |
1753 |
< |
checkCompletedWithWrappedCancellationException(g); |
1754 |
< |
} |
1741 |
> |
public void testThenCombine_actionFailed() { |
1742 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1743 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1744 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1745 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1746 |
> |
{ |
1747 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1748 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1749 |
> |
final FailingBiFunction r1 = new FailingBiFunction(m); |
1750 |
> |
final FailingBiFunction r2 = new FailingBiFunction(m); |
1751 |
> |
final FailingBiFunction r3 = new FailingBiFunction(m); |
1752 |
> |
|
1753 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1754 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1755 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1756 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1757 |
> |
|
1758 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1759 |
> |
assertTrue(fst.complete(w1)); |
1760 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1761 |
> |
assertTrue(snd.complete(w2)); |
1762 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1763 |
> |
|
1764 |
> |
checkCompletedWithWrappedCFException(h1); |
1765 |
> |
checkCompletedWithWrappedCFException(h2); |
1766 |
> |
checkCompletedWithWrappedCFException(h3); |
1767 |
> |
r1.assertInvoked(); |
1768 |
> |
r2.assertInvoked(); |
1769 |
> |
r3.assertInvoked(); |
1770 |
> |
checkCompletedNormally(f, v1); |
1771 |
> |
checkCompletedNormally(g, v2); |
1772 |
> |
}} |
1773 |
|
|
1774 |
|
/** |
1775 |
|
* thenAcceptBoth result completes normally after normal |
1776 |
|
* completion of sources |
1777 |
|
*/ |
1778 |
< |
public void testThenAcceptBoth() { |
1779 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1780 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1781 |
< |
AddAction r = new AddAction(); |
1782 |
< |
CompletableFuture<Void> g = f.thenAcceptBoth(f2, r); |
1783 |
< |
f.complete(one); |
1784 |
< |
checkIncomplete(g); |
1785 |
< |
f2.complete(two); |
1786 |
< |
checkCompletedNormally(g, null); |
1787 |
< |
assertEquals(r.value, 3); |
1788 |
< |
|
1789 |
< |
r = new AddAction(); |
1790 |
< |
f = new CompletableFuture<Integer>(); |
1791 |
< |
f.complete(one); |
1792 |
< |
f2 = new CompletableFuture<Integer>(); |
1793 |
< |
g = f.thenAcceptBoth(f2, r); |
1794 |
< |
checkIncomplete(g); |
1795 |
< |
f2.complete(two); |
1796 |
< |
checkCompletedNormally(g, null); |
1797 |
< |
assertEquals(r.value, 3); |
1798 |
< |
} |
1778 |
> |
public void testThenAcceptBoth_normalCompletion() { |
1779 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1780 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1781 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1782 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1783 |
> |
{ |
1784 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1785 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1786 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1787 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1788 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1789 |
> |
|
1790 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1791 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1792 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1793 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1794 |
> |
|
1795 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1796 |
> |
assertTrue(fst.complete(w1)); |
1797 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1798 |
> |
checkIncomplete(h1); |
1799 |
> |
checkIncomplete(h2); |
1800 |
> |
r1.assertNotInvoked(); |
1801 |
> |
r2.assertNotInvoked(); |
1802 |
> |
assertTrue(snd.complete(w2)); |
1803 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1804 |
> |
|
1805 |
> |
checkCompletedNormally(h1, null); |
1806 |
> |
checkCompletedNormally(h2, null); |
1807 |
> |
checkCompletedNormally(h3, null); |
1808 |
> |
r1.assertValue(subtract(v1, v2)); |
1809 |
> |
r2.assertValue(subtract(v1, v2)); |
1810 |
> |
r3.assertValue(subtract(v1, v2)); |
1811 |
> |
checkCompletedNormally(f, v1); |
1812 |
> |
checkCompletedNormally(g, v2); |
1813 |
> |
}} |
1814 |
|
|
1815 |
|
/** |
1816 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1817 |
|
* completion of either source |
1818 |
|
*/ |
1819 |
< |
public void testThenAcceptBoth2() { |
1820 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1821 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1822 |
< |
AddAction r = new AddAction(); |
1823 |
< |
CompletableFuture<Void> g = f.thenAcceptBoth(f2, r); |
1824 |
< |
f.completeExceptionally(new CFException()); |
1825 |
< |
f2.complete(two); |
1826 |
< |
checkCompletedWithWrappedCFException(g); |
1827 |
< |
|
1828 |
< |
r = new AddAction(); |
1829 |
< |
f = new CompletableFuture<Integer>(); |
1830 |
< |
f.complete(one); |
1831 |
< |
f2 = new CompletableFuture<Integer>(); |
1832 |
< |
g = f.thenAcceptBoth(f2, r); |
1833 |
< |
f2.completeExceptionally(new CFException()); |
1834 |
< |
checkCompletedWithWrappedCFException(g); |
1835 |
< |
} |
1819 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1820 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1821 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1822 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1823 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1824 |
> |
{ |
1825 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1826 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1827 |
> |
final CFException ex = new CFException(); |
1828 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1829 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1830 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1831 |
> |
|
1832 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1833 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1834 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1835 |
> |
() -> fst.completeExceptionally(ex) : |
1836 |
> |
() -> fst.complete(v1); |
1837 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1838 |
> |
() -> snd.complete(v1) : |
1839 |
> |
() -> snd.completeExceptionally(ex); |
1840 |
> |
|
1841 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1842 |
> |
assertTrue(complete1.call()); |
1843 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1844 |
> |
checkIncomplete(h1); |
1845 |
> |
checkIncomplete(h2); |
1846 |
> |
assertTrue(complete2.call()); |
1847 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1848 |
> |
|
1849 |
> |
checkCompletedWithWrappedException(h1, ex); |
1850 |
> |
checkCompletedWithWrappedException(h2, ex); |
1851 |
> |
checkCompletedWithWrappedException(h3, ex); |
1852 |
> |
r1.assertNotInvoked(); |
1853 |
> |
r2.assertNotInvoked(); |
1854 |
> |
r3.assertNotInvoked(); |
1855 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1856 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1857 |
> |
}} |
1858 |
|
|
1859 |
|
/** |
1860 |
< |
* thenAcceptBoth result completes exceptionally if action does |
1860 |
> |
* thenAcceptBoth result completes exceptionally if either source cancelled |
1861 |
|
*/ |
1862 |
< |
public void testThenAcceptBoth3() { |
1863 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1864 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1865 |
< |
FailingBiConsumer r = new FailingBiConsumer(); |
1866 |
< |
CompletableFuture<Void> g = f.thenAcceptBoth(f2, r); |
1867 |
< |
f.complete(one); |
1868 |
< |
checkIncomplete(g); |
1869 |
< |
f2.complete(two); |
1870 |
< |
checkCompletedWithWrappedCFException(g); |
1871 |
< |
} |
1862 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1863 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1864 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1865 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1866 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1867 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1868 |
> |
{ |
1869 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1870 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1871 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1872 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1873 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1874 |
> |
|
1875 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1876 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1877 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1878 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1879 |
> |
() -> fst.complete(v1); |
1880 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1881 |
> |
() -> snd.complete(v1) : |
1882 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1883 |
> |
|
1884 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1885 |
> |
assertTrue(complete1.call()); |
1886 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1887 |
> |
checkIncomplete(h1); |
1888 |
> |
checkIncomplete(h2); |
1889 |
> |
assertTrue(complete2.call()); |
1890 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1891 |
> |
|
1892 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1893 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1894 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1895 |
> |
r1.assertNotInvoked(); |
1896 |
> |
r2.assertNotInvoked(); |
1897 |
> |
r3.assertNotInvoked(); |
1898 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1899 |
> |
checkCancelled(failFirst ? fst : snd); |
1900 |
> |
}} |
1901 |
|
|
1902 |
|
/** |
1903 |
< |
* thenAcceptBoth result completes exceptionally if either source cancelled |
1903 |
> |
* thenAcceptBoth result completes exceptionally if action does |
1904 |
|
*/ |
1905 |
< |
public void testThenAcceptBoth4() { |
1906 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1907 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1908 |
< |
AddAction r = new AddAction(); |
1909 |
< |
CompletableFuture<Void> g = f.thenAcceptBoth(f2, r); |
1910 |
< |
assertTrue(f.cancel(true)); |
1911 |
< |
f2.complete(two); |
1912 |
< |
checkCompletedWithWrappedCancellationException(g); |
1913 |
< |
f = new CompletableFuture<Integer>(); |
1914 |
< |
f2 = new CompletableFuture<Integer>(); |
1915 |
< |
r = new AddAction(); |
1916 |
< |
g = f.thenAcceptBoth(f2, r); |
1917 |
< |
f.complete(one); |
1918 |
< |
assertTrue(f2.cancel(true)); |
1919 |
< |
checkCompletedWithWrappedCancellationException(g); |
1920 |
< |
} |
1905 |
> |
public void testThenAcceptBoth_actionFailed() { |
1906 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1907 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1908 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1909 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1910 |
> |
{ |
1911 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1912 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1913 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1914 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1915 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1916 |
> |
|
1917 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1918 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1919 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1920 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1921 |
> |
|
1922 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1923 |
> |
assertTrue(fst.complete(w1)); |
1924 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1925 |
> |
assertTrue(snd.complete(w2)); |
1926 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1927 |
> |
|
1928 |
> |
checkCompletedWithWrappedCFException(h1); |
1929 |
> |
checkCompletedWithWrappedCFException(h2); |
1930 |
> |
checkCompletedWithWrappedCFException(h3); |
1931 |
> |
r1.assertInvoked(); |
1932 |
> |
r2.assertInvoked(); |
1933 |
> |
r3.assertInvoked(); |
1934 |
> |
checkCompletedNormally(f, v1); |
1935 |
> |
checkCompletedNormally(g, v2); |
1936 |
> |
}} |
1937 |
|
|
1938 |
|
/** |
1939 |
|
* runAfterBoth result completes normally after normal |
1940 |
|
* completion of sources |
1941 |
|
*/ |
1942 |
< |
public void testRunAfterBoth() { |
1943 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1944 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1945 |
< |
Noop r = new Noop(); |
1946 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
1947 |
< |
f.complete(one); |
1948 |
< |
checkIncomplete(g); |
1949 |
< |
f2.complete(two); |
1950 |
< |
checkCompletedNormally(g, null); |
1951 |
< |
assertTrue(r.ran); |
1952 |
< |
|
1953 |
< |
r = new Noop(); |
1954 |
< |
f = new CompletableFuture<Integer>(); |
1955 |
< |
f.complete(one); |
1956 |
< |
f2 = new CompletableFuture<Integer>(); |
1957 |
< |
g = f.runAfterBoth(f2, r); |
1958 |
< |
checkIncomplete(g); |
1959 |
< |
f2.complete(two); |
1960 |
< |
checkCompletedNormally(g, null); |
1961 |
< |
assertTrue(r.ran); |
1962 |
< |
} |
1942 |
> |
public void testRunAfterBoth_normalCompletion() { |
1943 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1944 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1945 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1946 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1947 |
> |
{ |
1948 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1949 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1950 |
> |
final Noop r1 = new Noop(m); |
1951 |
> |
final Noop r2 = new Noop(m); |
1952 |
> |
final Noop r3 = new Noop(m); |
1953 |
> |
|
1954 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1955 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1956 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1957 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1958 |
> |
|
1959 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1960 |
> |
assertTrue(fst.complete(w1)); |
1961 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1962 |
> |
checkIncomplete(h1); |
1963 |
> |
checkIncomplete(h2); |
1964 |
> |
r1.assertNotInvoked(); |
1965 |
> |
r2.assertNotInvoked(); |
1966 |
> |
assertTrue(snd.complete(w2)); |
1967 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1968 |
> |
|
1969 |
> |
checkCompletedNormally(h1, null); |
1970 |
> |
checkCompletedNormally(h2, null); |
1971 |
> |
checkCompletedNormally(h3, null); |
1972 |
> |
r1.assertInvoked(); |
1973 |
> |
r2.assertInvoked(); |
1974 |
> |
r3.assertInvoked(); |
1975 |
> |
checkCompletedNormally(f, v1); |
1976 |
> |
checkCompletedNormally(g, v2); |
1977 |
> |
}} |
1978 |
|
|
1979 |
|
/** |
1980 |
|
* runAfterBoth result completes exceptionally after exceptional |
1981 |
|
* completion of either source |
1982 |
|
*/ |
1983 |
< |
public void testRunAfterBoth2() { |
1984 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1985 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1986 |
< |
Noop r = new Noop(); |
1987 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
1988 |
< |
f.completeExceptionally(new CFException()); |
1989 |
< |
f2.complete(two); |
1990 |
< |
checkCompletedWithWrappedCFException(g); |
1991 |
< |
|
1992 |
< |
r = new Noop(); |
1993 |
< |
f = new CompletableFuture<Integer>(); |
1994 |
< |
f.complete(one); |
1995 |
< |
f2 = new CompletableFuture<Integer>(); |
1996 |
< |
g = f.runAfterBoth(f2, r); |
1997 |
< |
f2.completeExceptionally(new CFException()); |
1998 |
< |
checkCompletedWithWrappedCFException(g); |
1999 |
< |
} |
1983 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
1984 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1985 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1986 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1987 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1988 |
> |
{ |
1989 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1990 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1991 |
> |
final CFException ex = new CFException(); |
1992 |
> |
final Noop r1 = new Noop(m); |
1993 |
> |
final Noop r2 = new Noop(m); |
1994 |
> |
final Noop r3 = new Noop(m); |
1995 |
> |
|
1996 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1997 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1998 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1999 |
> |
() -> fst.completeExceptionally(ex) : |
2000 |
> |
() -> fst.complete(v1); |
2001 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2002 |
> |
() -> snd.complete(v1) : |
2003 |
> |
() -> snd.completeExceptionally(ex); |
2004 |
> |
|
2005 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2006 |
> |
assertTrue(complete1.call()); |
2007 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2008 |
> |
checkIncomplete(h1); |
2009 |
> |
checkIncomplete(h2); |
2010 |
> |
assertTrue(complete2.call()); |
2011 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2012 |
> |
|
2013 |
> |
checkCompletedWithWrappedException(h1, ex); |
2014 |
> |
checkCompletedWithWrappedException(h2, ex); |
2015 |
> |
checkCompletedWithWrappedException(h3, ex); |
2016 |
> |
r1.assertNotInvoked(); |
2017 |
> |
r2.assertNotInvoked(); |
2018 |
> |
r3.assertNotInvoked(); |
2019 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2020 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
2021 |
> |
}} |
2022 |
|
|
2023 |
|
/** |
2024 |
< |
* runAfterBoth result completes exceptionally if action does |
2024 |
> |
* runAfterBoth result completes exceptionally if either source cancelled |
2025 |
|
*/ |
2026 |
< |
public void testRunAfterBoth3() { |
2027 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2028 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2029 |
< |
FailingNoop r = new FailingNoop(); |
2030 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
2031 |
< |
f.complete(one); |
2032 |
< |
checkIncomplete(g); |
2033 |
< |
f2.complete(two); |
2034 |
< |
checkCompletedWithWrappedCFException(g); |
2035 |
< |
} |
2026 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
2027 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2028 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2029 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2030 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
2031 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2032 |
> |
{ |
2033 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2034 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2035 |
> |
final Noop r1 = new Noop(m); |
2036 |
> |
final Noop r2 = new Noop(m); |
2037 |
> |
final Noop r3 = new Noop(m); |
2038 |
> |
|
2039 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2040 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2041 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2042 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
2043 |
> |
() -> fst.complete(v1); |
2044 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2045 |
> |
() -> snd.complete(v1) : |
2046 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
2047 |
> |
|
2048 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2049 |
> |
assertTrue(complete1.call()); |
2050 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2051 |
> |
checkIncomplete(h1); |
2052 |
> |
checkIncomplete(h2); |
2053 |
> |
assertTrue(complete2.call()); |
2054 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2055 |
> |
|
2056 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2057 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2058 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2059 |
> |
r1.assertNotInvoked(); |
2060 |
> |
r2.assertNotInvoked(); |
2061 |
> |
r3.assertNotInvoked(); |
2062 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2063 |
> |
checkCancelled(failFirst ? fst : snd); |
2064 |
> |
}} |
2065 |
|
|
2066 |
|
/** |
2067 |
< |
* runAfterBoth result completes exceptionally if either source cancelled |
2067 |
> |
* runAfterBoth result completes exceptionally if action does |
2068 |
|
*/ |
2069 |
< |
public void testRunAfterBoth4() { |
2070 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2071 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2072 |
< |
Noop r = new Noop(); |
2073 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
2074 |
< |
assertTrue(f.cancel(true)); |
2075 |
< |
f2.complete(two); |
2076 |
< |
checkCompletedWithWrappedCancellationException(g); |
2077 |
< |
f = new CompletableFuture<Integer>(); |
2078 |
< |
f2 = new CompletableFuture<Integer>(); |
2079 |
< |
r = new Noop(); |
2080 |
< |
g = f.runAfterBoth(f2, r); |
2081 |
< |
f.complete(one); |
2082 |
< |
assertTrue(f2.cancel(true)); |
2083 |
< |
checkCompletedWithWrappedCancellationException(g); |
2084 |
< |
} |
2069 |
> |
public void testRunAfterBoth_actionFailed() { |
2070 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2071 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2072 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2073 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2074 |
> |
{ |
2075 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2076 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2077 |
> |
final FailingRunnable r1 = new FailingRunnable(m); |
2078 |
> |
final FailingRunnable r2 = new FailingRunnable(m); |
2079 |
> |
final FailingRunnable r3 = new FailingRunnable(m); |
2080 |
> |
|
2081 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2082 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2083 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2084 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2085 |
> |
|
2086 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2087 |
> |
assertTrue(fst.complete(w1)); |
2088 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2089 |
> |
assertTrue(snd.complete(w2)); |
2090 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2091 |
> |
|
2092 |
> |
checkCompletedWithWrappedCFException(h1); |
2093 |
> |
checkCompletedWithWrappedCFException(h2); |
2094 |
> |
checkCompletedWithWrappedCFException(h3); |
2095 |
> |
r1.assertInvoked(); |
2096 |
> |
r2.assertInvoked(); |
2097 |
> |
r3.assertInvoked(); |
2098 |
> |
checkCompletedNormally(f, v1); |
2099 |
> |
checkCompletedNormally(g, v2); |
2100 |
> |
}} |
2101 |
|
|
2102 |
|
/** |
2103 |
|
* applyToEither result completes normally after normal completion |
2104 |
|
* of either source |
2105 |
|
*/ |
2106 |
< |
public void testApplyToEither() { |
2107 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2108 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2109 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
2110 |
< |
f.complete(one); |
2111 |
< |
checkCompletedNormally(g, two); |
2112 |
< |
f2.complete(one); |
2113 |
< |
checkCompletedNormally(g, two); |
2114 |
< |
|
2115 |
< |
f = new CompletableFuture<Integer>(); |
2116 |
< |
f.complete(one); |
2117 |
< |
f2 = new CompletableFuture<Integer>(); |
2118 |
< |
g = f.applyToEither(f2, inc); |
2119 |
< |
checkCompletedNormally(g, two); |
2120 |
< |
} |
2106 |
> |
public void testApplyToEither_normalCompletion() { |
2107 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2108 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2109 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2110 |
> |
{ |
2111 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2112 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2113 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2114 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2115 |
> |
|
2116 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2117 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2118 |
> |
checkIncomplete(h0); |
2119 |
> |
checkIncomplete(h1); |
2120 |
> |
rs[0].assertNotInvoked(); |
2121 |
> |
rs[1].assertNotInvoked(); |
2122 |
> |
f.complete(v1); |
2123 |
> |
checkCompletedNormally(h0, inc(v1)); |
2124 |
> |
checkCompletedNormally(h1, inc(v1)); |
2125 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2126 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2127 |
> |
checkCompletedNormally(h2, inc(v1)); |
2128 |
> |
checkCompletedNormally(h3, inc(v1)); |
2129 |
> |
g.complete(v2); |
2130 |
> |
|
2131 |
> |
// unspecified behavior - both source completions available |
2132 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2133 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2134 |
> |
rs[4].assertValue(h4.join()); |
2135 |
> |
rs[5].assertValue(h5.join()); |
2136 |
> |
assertTrue(Objects.equals(inc(v1), h4.join()) || |
2137 |
> |
Objects.equals(inc(v2), h4.join())); |
2138 |
> |
assertTrue(Objects.equals(inc(v1), h5.join()) || |
2139 |
> |
Objects.equals(inc(v2), h5.join())); |
2140 |
> |
|
2141 |
> |
checkCompletedNormally(f, v1); |
2142 |
> |
checkCompletedNormally(g, v2); |
2143 |
> |
checkCompletedNormally(h0, inc(v1)); |
2144 |
> |
checkCompletedNormally(h1, inc(v1)); |
2145 |
> |
checkCompletedNormally(h2, inc(v1)); |
2146 |
> |
checkCompletedNormally(h3, inc(v1)); |
2147 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); |
2148 |
> |
}} |
2149 |
|
|
2150 |
|
/** |
2151 |
|
* applyToEither result completes exceptionally after exceptional |
2152 |
|
* completion of either source |
2153 |
|
*/ |
2154 |
< |
public void testApplyToEither2() { |
2155 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2156 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2157 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
2158 |
< |
f.completeExceptionally(new CFException()); |
2159 |
< |
f2.complete(one); |
2160 |
< |
checkCompletedWithWrappedCFException(g); |
2154 |
> |
public void testApplyToEither_exceptionalCompletion() { |
2155 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2156 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2157 |
> |
{ |
2158 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2159 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2160 |
> |
final CFException ex = new CFException(); |
2161 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2162 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2163 |
> |
|
2164 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2165 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2166 |
> |
checkIncomplete(h0); |
2167 |
> |
checkIncomplete(h1); |
2168 |
> |
rs[0].assertNotInvoked(); |
2169 |
> |
rs[1].assertNotInvoked(); |
2170 |
> |
f.completeExceptionally(ex); |
2171 |
> |
checkCompletedWithWrappedException(h0, ex); |
2172 |
> |
checkCompletedWithWrappedException(h1, ex); |
2173 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2174 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2175 |
> |
checkCompletedWithWrappedException(h2, ex); |
2176 |
> |
checkCompletedWithWrappedException(h3, ex); |
2177 |
> |
g.complete(v1); |
2178 |
> |
|
2179 |
> |
// unspecified behavior - both source completions available |
2180 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2181 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2182 |
> |
try { |
2183 |
> |
assertEquals(inc(v1), h4.join()); |
2184 |
> |
rs[4].assertValue(inc(v1)); |
2185 |
> |
} catch (CompletionException ok) { |
2186 |
> |
checkCompletedWithWrappedException(h4, ex); |
2187 |
> |
rs[4].assertNotInvoked(); |
2188 |
> |
} |
2189 |
> |
try { |
2190 |
> |
assertEquals(inc(v1), h5.join()); |
2191 |
> |
rs[5].assertValue(inc(v1)); |
2192 |
> |
} catch (CompletionException ok) { |
2193 |
> |
checkCompletedWithWrappedException(h5, ex); |
2194 |
> |
rs[5].assertNotInvoked(); |
2195 |
> |
} |
2196 |
|
|
2197 |
< |
f = new CompletableFuture<Integer>(); |
2198 |
< |
f2 = new CompletableFuture<Integer>(); |
2199 |
< |
f2.completeExceptionally(new CFException()); |
2200 |
< |
g = f.applyToEither(f2, inc); |
2201 |
< |
checkCompletedWithWrappedCFException(g); |
2202 |
< |
} |
2197 |
> |
checkCompletedExceptionally(f, ex); |
2198 |
> |
checkCompletedNormally(g, v1); |
2199 |
> |
checkCompletedWithWrappedException(h0, ex); |
2200 |
> |
checkCompletedWithWrappedException(h1, ex); |
2201 |
> |
checkCompletedWithWrappedException(h2, ex); |
2202 |
> |
checkCompletedWithWrappedException(h3, ex); |
2203 |
> |
checkCompletedWithWrappedException(h4, ex); |
2204 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2205 |
> |
}} |
2206 |
> |
|
2207 |
> |
public void testApplyToEither_exceptionalCompletion2() { |
2208 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2209 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2210 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2211 |
> |
{ |
2212 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2213 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2214 |
> |
final CFException ex = new CFException(); |
2215 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2216 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2217 |
> |
|
2218 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2219 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2220 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2221 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2222 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2223 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2224 |
|
|
2225 |
< |
/** |
2226 |
< |
* applyToEither result completes exceptionally if action does |
2227 |
< |
*/ |
2228 |
< |
public void testApplyToEither3() { |
2229 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2230 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2231 |
< |
FailingFunction r = new FailingFunction(); |
2232 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, r); |
2233 |
< |
f2.complete(two); |
2234 |
< |
checkCompletedWithWrappedCFException(g); |
2235 |
< |
} |
2225 |
> |
// unspecified behavior - both source completions available |
2226 |
> |
try { |
2227 |
> |
assertEquals(inc(v1), h0.join()); |
2228 |
> |
rs[0].assertValue(inc(v1)); |
2229 |
> |
} catch (CompletionException ok) { |
2230 |
> |
checkCompletedWithWrappedException(h0, ex); |
2231 |
> |
rs[0].assertNotInvoked(); |
2232 |
> |
} |
2233 |
> |
try { |
2234 |
> |
assertEquals(inc(v1), h1.join()); |
2235 |
> |
rs[1].assertValue(inc(v1)); |
2236 |
> |
} catch (CompletionException ok) { |
2237 |
> |
checkCompletedWithWrappedException(h1, ex); |
2238 |
> |
rs[1].assertNotInvoked(); |
2239 |
> |
} |
2240 |
> |
try { |
2241 |
> |
assertEquals(inc(v1), h2.join()); |
2242 |
> |
rs[2].assertValue(inc(v1)); |
2243 |
> |
} catch (CompletionException ok) { |
2244 |
> |
checkCompletedWithWrappedException(h2, ex); |
2245 |
> |
rs[2].assertNotInvoked(); |
2246 |
> |
} |
2247 |
> |
try { |
2248 |
> |
assertEquals(inc(v1), h3.join()); |
2249 |
> |
rs[3].assertValue(inc(v1)); |
2250 |
> |
} catch (CompletionException ok) { |
2251 |
> |
checkCompletedWithWrappedException(h3, ex); |
2252 |
> |
rs[3].assertNotInvoked(); |
2253 |
> |
} |
2254 |
|
|
2255 |
< |
/** |
2256 |
< |
* applyToEither result completes exceptionally if either source cancelled |
2257 |
< |
*/ |
891 |
< |
public void testApplyToEither4() { |
892 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
893 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
894 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
895 |
< |
assertTrue(f.cancel(true)); |
896 |
< |
checkCompletedWithWrappedCancellationException(g); |
897 |
< |
f = new CompletableFuture<Integer>(); |
898 |
< |
f2 = new CompletableFuture<Integer>(); |
899 |
< |
assertTrue(f2.cancel(true)); |
900 |
< |
checkCompletedWithWrappedCancellationException(g); |
901 |
< |
} |
2255 |
> |
checkCompletedNormally(f, v1); |
2256 |
> |
checkCompletedExceptionally(g, ex); |
2257 |
> |
}} |
2258 |
|
|
2259 |
|
/** |
2260 |
< |
* acceptEither result completes normally after normal completion |
905 |
< |
* of either source |
2260 |
> |
* applyToEither result completes exceptionally if either source cancelled |
2261 |
|
*/ |
2262 |
< |
public void testAcceptEither() { |
2263 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2264 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2265 |
< |
IncAction r = new IncAction(); |
2266 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
2267 |
< |
f.complete(one); |
2268 |
< |
checkCompletedNormally(g, null); |
2269 |
< |
f2.complete(one); |
2270 |
< |
checkCompletedNormally(g, null); |
2271 |
< |
assertEquals(r.value, 2); |
2272 |
< |
|
2273 |
< |
r = new IncAction(); |
2274 |
< |
f = new CompletableFuture<Integer>(); |
2275 |
< |
f.complete(one); |
2276 |
< |
f2 = new CompletableFuture<Integer>(); |
2277 |
< |
g = f.acceptEither(f2, r); |
2278 |
< |
checkCompletedNormally(g, null); |
2279 |
< |
assertEquals(r.value, 2); |
2280 |
< |
} |
2262 |
> |
public void testApplyToEither_sourceCancelled() { |
2263 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2264 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2265 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2266 |
> |
{ |
2267 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2268 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2269 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2270 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2271 |
> |
|
2272 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2273 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2274 |
> |
checkIncomplete(h0); |
2275 |
> |
checkIncomplete(h1); |
2276 |
> |
rs[0].assertNotInvoked(); |
2277 |
> |
rs[1].assertNotInvoked(); |
2278 |
> |
f.cancel(mayInterruptIfRunning); |
2279 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2280 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2281 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2282 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2283 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2284 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2285 |
> |
g.complete(v1); |
2286 |
> |
|
2287 |
> |
// unspecified behavior - both source completions available |
2288 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2289 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2290 |
> |
try { |
2291 |
> |
assertEquals(inc(v1), h4.join()); |
2292 |
> |
rs[4].assertValue(inc(v1)); |
2293 |
> |
} catch (CompletionException ok) { |
2294 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2295 |
> |
rs[4].assertNotInvoked(); |
2296 |
> |
} |
2297 |
> |
try { |
2298 |
> |
assertEquals(inc(v1), h5.join()); |
2299 |
> |
rs[5].assertValue(inc(v1)); |
2300 |
> |
} catch (CompletionException ok) { |
2301 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2302 |
> |
rs[5].assertNotInvoked(); |
2303 |
> |
} |
2304 |
|
|
2305 |
< |
/** |
2306 |
< |
* acceptEither result completes exceptionally after exceptional |
2307 |
< |
* completion of either source |
2308 |
< |
*/ |
2309 |
< |
public void testAcceptEither2() { |
2310 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2311 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2312 |
< |
IncAction r = new IncAction(); |
2313 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
2314 |
< |
f.completeExceptionally(new CFException()); |
2315 |
< |
f2.complete(one); |
2316 |
< |
checkCompletedWithWrappedCFException(g); |
2305 |
> |
checkCancelled(f); |
2306 |
> |
checkCompletedNormally(g, v1); |
2307 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2308 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2309 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2310 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2311 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2312 |
> |
}} |
2313 |
> |
|
2314 |
> |
public void testApplyToEither_sourceCancelled2() { |
2315 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2316 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2317 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2318 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2319 |
> |
{ |
2320 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2321 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2322 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2323 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2324 |
> |
|
2325 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2326 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2327 |
> |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2328 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2329 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2330 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2331 |
|
|
2332 |
< |
r = new IncAction(); |
2333 |
< |
f = new CompletableFuture<Integer>(); |
2334 |
< |
f2 = new CompletableFuture<Integer>(); |
2335 |
< |
f2.completeExceptionally(new CFException()); |
2336 |
< |
g = f.acceptEither(f2, r); |
2337 |
< |
checkCompletedWithWrappedCFException(g); |
2338 |
< |
} |
2332 |
> |
// unspecified behavior - both source completions available |
2333 |
> |
try { |
2334 |
> |
assertEquals(inc(v1), h0.join()); |
2335 |
> |
rs[0].assertValue(inc(v1)); |
2336 |
> |
} catch (CompletionException ok) { |
2337 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2338 |
> |
rs[0].assertNotInvoked(); |
2339 |
> |
} |
2340 |
> |
try { |
2341 |
> |
assertEquals(inc(v1), h1.join()); |
2342 |
> |
rs[1].assertValue(inc(v1)); |
2343 |
> |
} catch (CompletionException ok) { |
2344 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2345 |
> |
rs[1].assertNotInvoked(); |
2346 |
> |
} |
2347 |
> |
try { |
2348 |
> |
assertEquals(inc(v1), h2.join()); |
2349 |
> |
rs[2].assertValue(inc(v1)); |
2350 |
> |
} catch (CompletionException ok) { |
2351 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2352 |
> |
rs[2].assertNotInvoked(); |
2353 |
> |
} |
2354 |
> |
try { |
2355 |
> |
assertEquals(inc(v1), h3.join()); |
2356 |
> |
rs[3].assertValue(inc(v1)); |
2357 |
> |
} catch (CompletionException ok) { |
2358 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2359 |
> |
rs[3].assertNotInvoked(); |
2360 |
> |
} |
2361 |
|
|
2362 |
< |
/** |
2363 |
< |
* acceptEither result completes exceptionally if action does |
2364 |
< |
*/ |
951 |
< |
public void testAcceptEither3() { |
952 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
953 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
954 |
< |
FailingConsumer r = new FailingConsumer(); |
955 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
956 |
< |
f2.complete(two); |
957 |
< |
checkCompletedWithWrappedCFException(g); |
958 |
< |
} |
2362 |
> |
checkCompletedNormally(f, v1); |
2363 |
> |
checkCancelled(g); |
2364 |
> |
}} |
2365 |
|
|
2366 |
|
/** |
2367 |
< |
* acceptEither result completes exceptionally if either source cancelled |
2367 |
> |
* applyToEither result completes exceptionally if action does |
2368 |
|
*/ |
2369 |
< |
public void testAcceptEither4() { |
2370 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2371 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2372 |
< |
IncAction r = new IncAction(); |
2373 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
2374 |
< |
assertTrue(f.cancel(true)); |
2375 |
< |
checkCompletedWithWrappedCancellationException(g); |
2376 |
< |
f = new CompletableFuture<Integer>(); |
2377 |
< |
f2 = new CompletableFuture<Integer>(); |
2378 |
< |
assertTrue(f2.cancel(true)); |
2379 |
< |
checkCompletedWithWrappedCancellationException(g); |
2380 |
< |
} |
2381 |
< |
|
2369 |
> |
public void testApplyToEither_actionFailed() { |
2370 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2371 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2372 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2373 |
> |
{ |
2374 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2375 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2376 |
> |
final FailingFunction[] rs = new FailingFunction[6]; |
2377 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
2378 |
> |
|
2379 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2380 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2381 |
> |
f.complete(v1); |
2382 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2383 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2384 |
> |
checkCompletedWithWrappedCFException(h0); |
2385 |
> |
checkCompletedWithWrappedCFException(h1); |
2386 |
> |
checkCompletedWithWrappedCFException(h2); |
2387 |
> |
checkCompletedWithWrappedCFException(h3); |
2388 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2389 |
> |
|
2390 |
> |
g.complete(v2); |
2391 |
> |
|
2392 |
> |
// unspecified behavior - both source completions available |
2393 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2394 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2395 |
> |
|
2396 |
> |
checkCompletedWithWrappedCFException(h4); |
2397 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2398 |
> |
Objects.equals(v2, rs[4].value)); |
2399 |
> |
checkCompletedWithWrappedCFException(h5); |
2400 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2401 |
> |
Objects.equals(v2, rs[5].value)); |
2402 |
> |
|
2403 |
> |
checkCompletedNormally(f, v1); |
2404 |
> |
checkCompletedNormally(g, v2); |
2405 |
> |
}} |
2406 |
|
|
2407 |
|
/** |
2408 |
< |
* runAfterEither result completes normally after normal completion |
2408 |
> |
* acceptEither result completes normally after normal completion |
2409 |
|
* of either source |
2410 |
|
*/ |
2411 |
< |
public void testRunAfterEither() { |
2412 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2413 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2414 |
< |
Noop r = new Noop(); |
2415 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
2416 |
< |
f.complete(one); |
2417 |
< |
checkCompletedNormally(g, null); |
2418 |
< |
f2.complete(one); |
2419 |
< |
checkCompletedNormally(g, null); |
2420 |
< |
assertTrue(r.ran); |
2421 |
< |
|
2422 |
< |
r = new Noop(); |
2423 |
< |
f = new CompletableFuture<Integer>(); |
2424 |
< |
f.complete(one); |
2425 |
< |
f2 = new CompletableFuture<Integer>(); |
2426 |
< |
g = f.runAfterEither(f2, r); |
2427 |
< |
checkCompletedNormally(g, null); |
2428 |
< |
assertTrue(r.ran); |
2429 |
< |
} |
2411 |
> |
public void testAcceptEither_normalCompletion() { |
2412 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2413 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2414 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2415 |
> |
{ |
2416 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2417 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2418 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2419 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2420 |
> |
|
2421 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2422 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2423 |
> |
checkIncomplete(h0); |
2424 |
> |
checkIncomplete(h1); |
2425 |
> |
rs[0].assertNotInvoked(); |
2426 |
> |
rs[1].assertNotInvoked(); |
2427 |
> |
f.complete(v1); |
2428 |
> |
checkCompletedNormally(h0, null); |
2429 |
> |
checkCompletedNormally(h1, null); |
2430 |
> |
rs[0].assertValue(v1); |
2431 |
> |
rs[1].assertValue(v1); |
2432 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2433 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2434 |
> |
checkCompletedNormally(h2, null); |
2435 |
> |
checkCompletedNormally(h3, null); |
2436 |
> |
rs[2].assertValue(v1); |
2437 |
> |
rs[3].assertValue(v1); |
2438 |
> |
g.complete(v2); |
2439 |
> |
|
2440 |
> |
// unspecified behavior - both source completions available |
2441 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2442 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2443 |
> |
checkCompletedNormally(h4, null); |
2444 |
> |
checkCompletedNormally(h5, null); |
2445 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2446 |
> |
Objects.equals(v2, rs[4].value)); |
2447 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2448 |
> |
Objects.equals(v2, rs[5].value)); |
2449 |
> |
|
2450 |
> |
checkCompletedNormally(f, v1); |
2451 |
> |
checkCompletedNormally(g, v2); |
2452 |
> |
checkCompletedNormally(h0, null); |
2453 |
> |
checkCompletedNormally(h1, null); |
2454 |
> |
checkCompletedNormally(h2, null); |
2455 |
> |
checkCompletedNormally(h3, null); |
2456 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2457 |
> |
}} |
2458 |
|
|
2459 |
|
/** |
2460 |
< |
* runAfterEither result completes exceptionally after exceptional |
2460 |
> |
* acceptEither result completes exceptionally after exceptional |
2461 |
|
* completion of either source |
2462 |
|
*/ |
2463 |
< |
public void testRunAfterEither2() { |
2464 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2465 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2466 |
< |
Noop r = new Noop(); |
2467 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
2468 |
< |
f.completeExceptionally(new CFException()); |
2469 |
< |
f2.complete(one); |
2470 |
< |
checkCompletedWithWrappedCFException(g); |
2471 |
< |
|
2472 |
< |
r = new Noop(); |
2473 |
< |
f = new CompletableFuture<Integer>(); |
2474 |
< |
f2 = new CompletableFuture<Integer>(); |
2475 |
< |
f2.completeExceptionally(new CFException()); |
2476 |
< |
g = f.runAfterEither(f2, r); |
2477 |
< |
checkCompletedWithWrappedCFException(g); |
2478 |
< |
} |
2479 |
< |
|
2480 |
< |
/** |
2481 |
< |
* runAfterEither result completes exceptionally if action does |
2482 |
< |
*/ |
2483 |
< |
public void testRunAfterEither3() { |
2484 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2485 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2486 |
< |
FailingNoop r = new FailingNoop(); |
2487 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
2488 |
< |
f2.complete(two); |
2489 |
< |
checkCompletedWithWrappedCFException(g); |
2490 |
< |
} |
2491 |
< |
|
1034 |
< |
/** |
1035 |
< |
* runAfterEither result completes exceptionally if either source cancelled |
1036 |
< |
*/ |
1037 |
< |
public void testRunAfterEither4() { |
1038 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1039 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1040 |
< |
Noop r = new Noop(); |
1041 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
1042 |
< |
assertTrue(f.cancel(true)); |
1043 |
< |
checkCompletedWithWrappedCancellationException(g); |
1044 |
< |
f = new CompletableFuture<Integer>(); |
1045 |
< |
f2 = new CompletableFuture<Integer>(); |
1046 |
< |
assertTrue(f2.cancel(true)); |
1047 |
< |
checkCompletedWithWrappedCancellationException(g); |
1048 |
< |
} |
1049 |
< |
|
1050 |
< |
/** |
1051 |
< |
* thenCompose result completes normally after normal completion of source |
1052 |
< |
*/ |
1053 |
< |
public void testThenCompose() { |
1054 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1055 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1056 |
< |
CompletableFuture<Integer> g = f.thenCompose(r); |
1057 |
< |
f.complete(one); |
1058 |
< |
checkCompletedNormally(g, two); |
1059 |
< |
} |
1060 |
< |
|
1061 |
< |
/** |
1062 |
< |
* thenCompose result completes exceptionally after exceptional |
1063 |
< |
* completion of source |
1064 |
< |
*/ |
1065 |
< |
public void testThenCompose2() { |
1066 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1067 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1068 |
< |
CompletableFuture<Integer> g = f.thenCompose(r); |
1069 |
< |
f.completeExceptionally(new CFException()); |
1070 |
< |
checkCompletedWithWrappedCFException(g); |
1071 |
< |
} |
1072 |
< |
|
1073 |
< |
/** |
1074 |
< |
* thenCompose result completes exceptionally if action does |
1075 |
< |
*/ |
1076 |
< |
public void testThenCompose3() { |
1077 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1078 |
< |
FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); |
1079 |
< |
CompletableFuture<Integer> g = f.thenCompose(r); |
1080 |
< |
f.complete(one); |
1081 |
< |
checkCompletedWithWrappedCFException(g); |
1082 |
< |
} |
1083 |
< |
|
1084 |
< |
/** |
1085 |
< |
* thenCompose result completes exceptionally if source cancelled |
1086 |
< |
*/ |
1087 |
< |
public void testThenCompose4() { |
1088 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1089 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1090 |
< |
CompletableFuture<Integer> g = f.thenCompose(r); |
1091 |
< |
assertTrue(f.cancel(true)); |
1092 |
< |
checkCompletedWithWrappedCancellationException(g); |
1093 |
< |
} |
1094 |
< |
|
1095 |
< |
|
1096 |
< |
// asyncs |
1097 |
< |
|
1098 |
< |
/** |
1099 |
< |
* thenRunAsync result completes normally after normal completion of source |
1100 |
< |
*/ |
1101 |
< |
public void testThenRunAsync() { |
1102 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1103 |
< |
Noop r = new Noop(); |
1104 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
1105 |
< |
f.complete(null); |
1106 |
< |
checkCompletedNormally(g, null); |
1107 |
< |
|
1108 |
< |
// reordered version |
1109 |
< |
f = new CompletableFuture<Integer>(); |
1110 |
< |
f.complete(null); |
1111 |
< |
r = new Noop(); |
1112 |
< |
g = f.thenRunAsync(r); |
1113 |
< |
checkCompletedNormally(g, null); |
1114 |
< |
} |
1115 |
< |
|
1116 |
< |
/** |
1117 |
< |
* thenRunAsync result completes exceptionally after exceptional |
1118 |
< |
* completion of source |
1119 |
< |
*/ |
1120 |
< |
public void testThenRunAsync2() { |
1121 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1122 |
< |
Noop r = new Noop(); |
1123 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
1124 |
< |
f.completeExceptionally(new CFException()); |
2463 |
> |
public void testAcceptEither_exceptionalCompletion() { |
2464 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2465 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2466 |
> |
{ |
2467 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2468 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2469 |
> |
final CFException ex = new CFException(); |
2470 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2471 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2472 |
> |
|
2473 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2474 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2475 |
> |
checkIncomplete(h0); |
2476 |
> |
checkIncomplete(h1); |
2477 |
> |
rs[0].assertNotInvoked(); |
2478 |
> |
rs[1].assertNotInvoked(); |
2479 |
> |
f.completeExceptionally(ex); |
2480 |
> |
checkCompletedWithWrappedException(h0, ex); |
2481 |
> |
checkCompletedWithWrappedException(h1, ex); |
2482 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2483 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2484 |
> |
checkCompletedWithWrappedException(h2, ex); |
2485 |
> |
checkCompletedWithWrappedException(h3, ex); |
2486 |
> |
|
2487 |
> |
g.complete(v1); |
2488 |
> |
|
2489 |
> |
// unspecified behavior - both source completions available |
2490 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2491 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2492 |
|
try { |
2493 |
< |
g.join(); |
2494 |
< |
shouldThrow(); |
2495 |
< |
} catch (Exception ok) { |
2493 |
> |
assertNull(h4.join()); |
2494 |
> |
rs[4].assertValue(v1); |
2495 |
> |
} catch (CompletionException ok) { |
2496 |
> |
checkCompletedWithWrappedException(h4, ex); |
2497 |
> |
rs[4].assertNotInvoked(); |
2498 |
|
} |
1130 |
– |
checkCompletedWithWrappedCFException(g); |
1131 |
– |
} |
1132 |
– |
|
1133 |
– |
/** |
1134 |
– |
* thenRunAsync result completes exceptionally if action does |
1135 |
– |
*/ |
1136 |
– |
public void testThenRunAsync3() { |
1137 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1138 |
– |
FailingNoop r = new FailingNoop(); |
1139 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r); |
1140 |
– |
f.complete(null); |
1141 |
– |
checkCompletedWithWrappedCFException(g); |
1142 |
– |
} |
1143 |
– |
|
1144 |
– |
/** |
1145 |
– |
* thenRunAsync result completes exceptionally if source cancelled |
1146 |
– |
*/ |
1147 |
– |
public void testThenRunAsync4() { |
1148 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1149 |
– |
Noop r = new Noop(); |
1150 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r); |
1151 |
– |
assertTrue(f.cancel(true)); |
1152 |
– |
checkCompletedWithWrappedCancellationException(g); |
1153 |
– |
} |
1154 |
– |
|
1155 |
– |
/** |
1156 |
– |
* thenApplyAsync result completes normally after normal completion of source |
1157 |
– |
*/ |
1158 |
– |
public void testThenApplyAsync() { |
1159 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1160 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
1161 |
– |
f.complete(one); |
1162 |
– |
checkCompletedNormally(g, two); |
1163 |
– |
} |
1164 |
– |
|
1165 |
– |
/** |
1166 |
– |
* thenApplyAsync result completes exceptionally after exceptional |
1167 |
– |
* completion of source |
1168 |
– |
*/ |
1169 |
– |
public void testThenApplyAsync2() { |
1170 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1171 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
1172 |
– |
f.completeExceptionally(new CFException()); |
1173 |
– |
checkCompletedWithWrappedCFException(g); |
1174 |
– |
} |
1175 |
– |
|
1176 |
– |
/** |
1177 |
– |
* thenApplyAsync result completes exceptionally if action does |
1178 |
– |
*/ |
1179 |
– |
public void testThenApplyAsync3() { |
1180 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1181 |
– |
FailingFunction r = new FailingFunction(); |
1182 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(r); |
1183 |
– |
f.complete(null); |
1184 |
– |
checkCompletedWithWrappedCFException(g); |
1185 |
– |
} |
1186 |
– |
|
1187 |
– |
/** |
1188 |
– |
* thenApplyAsync result completes exceptionally if source cancelled |
1189 |
– |
*/ |
1190 |
– |
public void testThenApplyAsync4() { |
1191 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1192 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
1193 |
– |
assertTrue(f.cancel(true)); |
1194 |
– |
checkCompletedWithWrappedCancellationException(g); |
1195 |
– |
} |
1196 |
– |
|
1197 |
– |
/** |
1198 |
– |
* thenAcceptAsync result completes normally after normal |
1199 |
– |
* completion of source |
1200 |
– |
*/ |
1201 |
– |
public void testThenAcceptAsync() { |
1202 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1203 |
– |
IncAction r = new IncAction(); |
1204 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
1205 |
– |
f.complete(one); |
1206 |
– |
checkCompletedNormally(g, null); |
1207 |
– |
assertEquals(r.value, 2); |
1208 |
– |
} |
1209 |
– |
|
1210 |
– |
/** |
1211 |
– |
* thenAcceptAsync result completes exceptionally after exceptional |
1212 |
– |
* completion of source |
1213 |
– |
*/ |
1214 |
– |
public void testThenAcceptAsync2() { |
1215 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1216 |
– |
IncAction r = new IncAction(); |
1217 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
1218 |
– |
f.completeExceptionally(new CFException()); |
1219 |
– |
checkCompletedWithWrappedCFException(g); |
1220 |
– |
} |
1221 |
– |
|
1222 |
– |
/** |
1223 |
– |
* thenAcceptAsync result completes exceptionally if action does |
1224 |
– |
*/ |
1225 |
– |
public void testThenAcceptAsync3() { |
1226 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1227 |
– |
FailingConsumer r = new FailingConsumer(); |
1228 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
1229 |
– |
f.complete(null); |
1230 |
– |
checkCompletedWithWrappedCFException(g); |
1231 |
– |
} |
1232 |
– |
|
1233 |
– |
/** |
1234 |
– |
* thenAcceptAsync result completes exceptionally if source cancelled |
1235 |
– |
*/ |
1236 |
– |
public void testThenAcceptAsync4() { |
1237 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1238 |
– |
IncAction r = new IncAction(); |
1239 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
1240 |
– |
assertTrue(f.cancel(true)); |
1241 |
– |
checkCompletedWithWrappedCancellationException(g); |
1242 |
– |
} |
1243 |
– |
/** |
1244 |
– |
* thenCombineAsync result completes normally after normal |
1245 |
– |
* completion of sources |
1246 |
– |
*/ |
1247 |
– |
public void testThenCombineAsync() { |
1248 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1249 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1250 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add); |
1251 |
– |
f.complete(one); |
1252 |
– |
checkIncomplete(g); |
1253 |
– |
f2.complete(two); |
1254 |
– |
checkCompletedNormally(g, three); |
1255 |
– |
} |
1256 |
– |
|
1257 |
– |
/** |
1258 |
– |
* thenCombineAsync result completes exceptionally after exceptional |
1259 |
– |
* completion of source |
1260 |
– |
*/ |
1261 |
– |
public void testThenCombineAsync2() { |
1262 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1263 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1264 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add); |
1265 |
– |
f.completeExceptionally(new CFException()); |
1266 |
– |
f2.complete(two); |
1267 |
– |
checkCompletedWithWrappedCFException(g); |
1268 |
– |
|
1269 |
– |
f = new CompletableFuture<Integer>(); |
1270 |
– |
f2 = new CompletableFuture<Integer>(); |
1271 |
– |
g = f.thenCombineAsync(f2, add); |
1272 |
– |
f.complete(one); |
1273 |
– |
f2.completeExceptionally(new CFException()); |
1274 |
– |
checkCompletedWithWrappedCFException(g); |
1275 |
– |
} |
1276 |
– |
|
1277 |
– |
/** |
1278 |
– |
* thenCombineAsync result completes exceptionally if action does |
1279 |
– |
*/ |
1280 |
– |
public void testThenCombineAsync3() { |
1281 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1282 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1283 |
– |
FailingBiFunction r = new FailingBiFunction(); |
1284 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, r); |
1285 |
– |
f.complete(one); |
1286 |
– |
checkIncomplete(g); |
1287 |
– |
f2.complete(two); |
1288 |
– |
checkCompletedWithWrappedCFException(g); |
1289 |
– |
} |
1290 |
– |
|
1291 |
– |
/** |
1292 |
– |
* thenCombineAsync result completes exceptionally if either source cancelled |
1293 |
– |
*/ |
1294 |
– |
public void testThenCombineAsync4() { |
1295 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1296 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1297 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add); |
1298 |
– |
assertTrue(f.cancel(true)); |
1299 |
– |
f2.complete(two); |
1300 |
– |
checkCompletedWithWrappedCancellationException(g); |
1301 |
– |
|
1302 |
– |
f = new CompletableFuture<Integer>(); |
1303 |
– |
f2 = new CompletableFuture<Integer>(); |
1304 |
– |
g = f.thenCombineAsync(f2, add); |
1305 |
– |
f.complete(one); |
1306 |
– |
assertTrue(f2.cancel(true)); |
1307 |
– |
checkCompletedWithWrappedCancellationException(g); |
1308 |
– |
} |
1309 |
– |
|
1310 |
– |
/** |
1311 |
– |
* thenAcceptBothAsync result completes normally after normal |
1312 |
– |
* completion of sources |
1313 |
– |
*/ |
1314 |
– |
public void testThenAcceptBothAsync() { |
1315 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1316 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1317 |
– |
AddAction r = new AddAction(); |
1318 |
– |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1319 |
– |
f.complete(one); |
1320 |
– |
checkIncomplete(g); |
1321 |
– |
f2.complete(two); |
1322 |
– |
checkCompletedNormally(g, null); |
1323 |
– |
assertEquals(r.value, 3); |
1324 |
– |
} |
1325 |
– |
|
1326 |
– |
/** |
1327 |
– |
* thenAcceptBothAsync result completes exceptionally after exceptional |
1328 |
– |
* completion of source |
1329 |
– |
*/ |
1330 |
– |
public void testThenAcceptBothAsync2() { |
1331 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1332 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1333 |
– |
AddAction r = new AddAction(); |
1334 |
– |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1335 |
– |
f.completeExceptionally(new CFException()); |
1336 |
– |
f2.complete(two); |
1337 |
– |
checkCompletedWithWrappedCFException(g); |
1338 |
– |
|
1339 |
– |
r = new AddAction(); |
1340 |
– |
f = new CompletableFuture<Integer>(); |
1341 |
– |
f2 = new CompletableFuture<Integer>(); |
1342 |
– |
g = f.thenAcceptBothAsync(f2, r); |
1343 |
– |
f.complete(one); |
1344 |
– |
f2.completeExceptionally(new CFException()); |
1345 |
– |
checkCompletedWithWrappedCFException(g); |
1346 |
– |
} |
1347 |
– |
|
1348 |
– |
/** |
1349 |
– |
* thenAcceptBothAsync result completes exceptionally if action does |
1350 |
– |
*/ |
1351 |
– |
public void testThenAcceptBothAsync3() { |
1352 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1353 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1354 |
– |
FailingBiConsumer r = new FailingBiConsumer(); |
1355 |
– |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1356 |
– |
f.complete(one); |
1357 |
– |
checkIncomplete(g); |
1358 |
– |
f2.complete(two); |
1359 |
– |
checkCompletedWithWrappedCFException(g); |
1360 |
– |
} |
1361 |
– |
|
1362 |
– |
/** |
1363 |
– |
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
1364 |
– |
*/ |
1365 |
– |
public void testThenAcceptBothAsync4() { |
1366 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1367 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1368 |
– |
AddAction r = new AddAction(); |
1369 |
– |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1370 |
– |
assertTrue(f.cancel(true)); |
1371 |
– |
f2.complete(two); |
1372 |
– |
checkCompletedWithWrappedCancellationException(g); |
1373 |
– |
|
1374 |
– |
r = new AddAction(); |
1375 |
– |
f = new CompletableFuture<Integer>(); |
1376 |
– |
f2 = new CompletableFuture<Integer>(); |
1377 |
– |
g = f.thenAcceptBothAsync(f2, r); |
1378 |
– |
f.complete(one); |
1379 |
– |
assertTrue(f2.cancel(true)); |
1380 |
– |
checkCompletedWithWrappedCancellationException(g); |
1381 |
– |
} |
1382 |
– |
|
1383 |
– |
/** |
1384 |
– |
* runAfterBothAsync result completes normally after normal |
1385 |
– |
* completion of sources |
1386 |
– |
*/ |
1387 |
– |
public void testRunAfterBothAsync() { |
1388 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1389 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1390 |
– |
Noop r = new Noop(); |
1391 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1392 |
– |
f.complete(one); |
1393 |
– |
checkIncomplete(g); |
1394 |
– |
f2.complete(two); |
1395 |
– |
checkCompletedNormally(g, null); |
1396 |
– |
assertTrue(r.ran); |
1397 |
– |
} |
1398 |
– |
|
1399 |
– |
/** |
1400 |
– |
* runAfterBothAsync result completes exceptionally after exceptional |
1401 |
– |
* completion of source |
1402 |
– |
*/ |
1403 |
– |
public void testRunAfterBothAsync2() { |
1404 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1405 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1406 |
– |
Noop r = new Noop(); |
1407 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1408 |
– |
f.completeExceptionally(new CFException()); |
1409 |
– |
f2.complete(two); |
1410 |
– |
checkCompletedWithWrappedCFException(g); |
1411 |
– |
|
1412 |
– |
r = new Noop(); |
1413 |
– |
f = new CompletableFuture<Integer>(); |
1414 |
– |
f2 = new CompletableFuture<Integer>(); |
1415 |
– |
g = f.runAfterBothAsync(f2, r); |
1416 |
– |
f.complete(one); |
1417 |
– |
f2.completeExceptionally(new CFException()); |
1418 |
– |
checkCompletedWithWrappedCFException(g); |
1419 |
– |
} |
1420 |
– |
|
1421 |
– |
/** |
1422 |
– |
* runAfterBothAsync result completes exceptionally if action does |
1423 |
– |
*/ |
1424 |
– |
public void testRunAfterBothAsync3() { |
1425 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1426 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1427 |
– |
FailingNoop r = new FailingNoop(); |
1428 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1429 |
– |
f.complete(one); |
1430 |
– |
checkIncomplete(g); |
1431 |
– |
f2.complete(two); |
1432 |
– |
checkCompletedWithWrappedCFException(g); |
1433 |
– |
} |
1434 |
– |
|
1435 |
– |
/** |
1436 |
– |
* runAfterBothAsync result completes exceptionally if either source cancelled |
1437 |
– |
*/ |
1438 |
– |
public void testRunAfterBothAsync4() { |
1439 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1440 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1441 |
– |
Noop r = new Noop(); |
1442 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1443 |
– |
assertTrue(f.cancel(true)); |
1444 |
– |
f2.complete(two); |
1445 |
– |
checkCompletedWithWrappedCancellationException(g); |
1446 |
– |
|
1447 |
– |
r = new Noop(); |
1448 |
– |
f = new CompletableFuture<Integer>(); |
1449 |
– |
f2 = new CompletableFuture<Integer>(); |
1450 |
– |
g = f.runAfterBothAsync(f2, r); |
1451 |
– |
f.complete(one); |
1452 |
– |
assertTrue(f2.cancel(true)); |
1453 |
– |
checkCompletedWithWrappedCancellationException(g); |
1454 |
– |
} |
1455 |
– |
|
1456 |
– |
/** |
1457 |
– |
* applyToEitherAsync result completes normally after normal |
1458 |
– |
* completion of sources |
1459 |
– |
*/ |
1460 |
– |
public void testApplyToEitherAsync() { |
1461 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1462 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1463 |
– |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
1464 |
– |
f.complete(one); |
1465 |
– |
checkCompletedNormally(g, two); |
1466 |
– |
|
1467 |
– |
f = new CompletableFuture<Integer>(); |
1468 |
– |
f.complete(one); |
1469 |
– |
f2 = new CompletableFuture<Integer>(); |
1470 |
– |
g = f.applyToEitherAsync(f2, inc); |
1471 |
– |
checkCompletedNormally(g, two); |
1472 |
– |
} |
1473 |
– |
|
1474 |
– |
/** |
1475 |
– |
* applyToEitherAsync result completes exceptionally after exceptional |
1476 |
– |
* completion of source |
1477 |
– |
*/ |
1478 |
– |
public void testApplyToEitherAsync2() { |
1479 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1480 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1481 |
– |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
1482 |
– |
f.completeExceptionally(new CFException()); |
1483 |
– |
checkCompletedWithWrappedCFException(g); |
1484 |
– |
|
1485 |
– |
f = new CompletableFuture<Integer>(); |
1486 |
– |
f2 = new CompletableFuture<Integer>(); |
1487 |
– |
f2.completeExceptionally(new CFException()); |
1488 |
– |
g = f.applyToEitherAsync(f2, inc); |
1489 |
– |
f.complete(one); |
1490 |
– |
checkCompletedWithWrappedCFException(g); |
1491 |
– |
} |
1492 |
– |
|
1493 |
– |
/** |
1494 |
– |
* applyToEitherAsync result completes exceptionally if action does |
1495 |
– |
*/ |
1496 |
– |
public void testApplyToEitherAsync3() { |
1497 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1498 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1499 |
– |
FailingFunction r = new FailingFunction(); |
1500 |
– |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r); |
1501 |
– |
f.complete(one); |
1502 |
– |
checkCompletedWithWrappedCFException(g); |
1503 |
– |
} |
1504 |
– |
|
1505 |
– |
/** |
1506 |
– |
* applyToEitherAsync result completes exceptionally if either source cancelled |
1507 |
– |
*/ |
1508 |
– |
public void testApplyToEitherAsync4() { |
1509 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1510 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1511 |
– |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
1512 |
– |
assertTrue(f.cancel(true)); |
1513 |
– |
checkCompletedWithWrappedCancellationException(g); |
1514 |
– |
|
1515 |
– |
f = new CompletableFuture<Integer>(); |
1516 |
– |
f2 = new CompletableFuture<Integer>(); |
1517 |
– |
assertTrue(f2.cancel(true)); |
1518 |
– |
g = f.applyToEitherAsync(f2, inc); |
1519 |
– |
checkCompletedWithWrappedCancellationException(g); |
1520 |
– |
} |
1521 |
– |
|
1522 |
– |
/** |
1523 |
– |
* acceptEitherAsync result completes normally after normal |
1524 |
– |
* completion of sources |
1525 |
– |
*/ |
1526 |
– |
public void testAcceptEitherAsync() { |
1527 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1528 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1529 |
– |
IncAction r = new IncAction(); |
1530 |
– |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1531 |
– |
f.complete(one); |
1532 |
– |
checkCompletedNormally(g, null); |
1533 |
– |
assertEquals(r.value, 2); |
1534 |
– |
|
1535 |
– |
r = new IncAction(); |
1536 |
– |
f = new CompletableFuture<Integer>(); |
1537 |
– |
f.complete(one); |
1538 |
– |
f2 = new CompletableFuture<Integer>(); |
1539 |
– |
g = f.acceptEitherAsync(f2, r); |
1540 |
– |
checkCompletedNormally(g, null); |
1541 |
– |
assertEquals(r.value, 2); |
1542 |
– |
} |
1543 |
– |
|
1544 |
– |
/** |
1545 |
– |
* acceptEitherAsync result completes exceptionally after exceptional |
1546 |
– |
* completion of source |
1547 |
– |
*/ |
1548 |
– |
public void testAcceptEitherAsync2() { |
1549 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1550 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1551 |
– |
IncAction r = new IncAction(); |
1552 |
– |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1553 |
– |
f.completeExceptionally(new CFException()); |
1554 |
– |
checkCompletedWithWrappedCFException(g); |
1555 |
– |
|
1556 |
– |
r = new IncAction(); |
1557 |
– |
f = new CompletableFuture<Integer>(); |
1558 |
– |
f2 = new CompletableFuture<Integer>(); |
1559 |
– |
f2.completeExceptionally(new CFException()); |
1560 |
– |
g = f.acceptEitherAsync(f2, r); |
1561 |
– |
f.complete(one); |
1562 |
– |
checkCompletedWithWrappedCFException(g); |
1563 |
– |
} |
1564 |
– |
|
1565 |
– |
/** |
1566 |
– |
* acceptEitherAsync result completes exceptionally if action does |
1567 |
– |
*/ |
1568 |
– |
public void testAcceptEitherAsync3() { |
1569 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1570 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1571 |
– |
FailingConsumer r = new FailingConsumer(); |
1572 |
– |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1573 |
– |
f.complete(one); |
1574 |
– |
checkCompletedWithWrappedCFException(g); |
1575 |
– |
} |
1576 |
– |
|
1577 |
– |
/** |
1578 |
– |
* acceptEitherAsync result completes exceptionally if either |
1579 |
– |
* source cancelled |
1580 |
– |
*/ |
1581 |
– |
public void testAcceptEitherAsync4() { |
1582 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1583 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1584 |
– |
IncAction r = new IncAction(); |
1585 |
– |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1586 |
– |
assertTrue(f.cancel(true)); |
1587 |
– |
checkCompletedWithWrappedCancellationException(g); |
1588 |
– |
|
1589 |
– |
r = new IncAction(); |
1590 |
– |
f = new CompletableFuture<Integer>(); |
1591 |
– |
f2 = new CompletableFuture<Integer>(); |
1592 |
– |
assertTrue(f2.cancel(true)); |
1593 |
– |
g = f.acceptEitherAsync(f2, r); |
1594 |
– |
checkCompletedWithWrappedCancellationException(g); |
1595 |
– |
} |
1596 |
– |
|
1597 |
– |
/** |
1598 |
– |
* runAfterEitherAsync result completes normally after normal |
1599 |
– |
* completion of sources |
1600 |
– |
*/ |
1601 |
– |
public void testRunAfterEitherAsync() { |
1602 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1603 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1604 |
– |
Noop r = new Noop(); |
1605 |
– |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1606 |
– |
f.complete(one); |
1607 |
– |
checkCompletedNormally(g, null); |
1608 |
– |
assertTrue(r.ran); |
1609 |
– |
|
1610 |
– |
r = new Noop(); |
1611 |
– |
f = new CompletableFuture<Integer>(); |
1612 |
– |
f.complete(one); |
1613 |
– |
f2 = new CompletableFuture<Integer>(); |
1614 |
– |
g = f.runAfterEitherAsync(f2, r); |
1615 |
– |
checkCompletedNormally(g, null); |
1616 |
– |
assertTrue(r.ran); |
1617 |
– |
} |
1618 |
– |
|
1619 |
– |
/** |
1620 |
– |
* runAfterEitherAsync result completes exceptionally after exceptional |
1621 |
– |
* completion of source |
1622 |
– |
*/ |
1623 |
– |
public void testRunAfterEitherAsync2() { |
1624 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1625 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1626 |
– |
Noop r = new Noop(); |
1627 |
– |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1628 |
– |
f.completeExceptionally(new CFException()); |
1629 |
– |
checkCompletedWithWrappedCFException(g); |
1630 |
– |
|
1631 |
– |
r = new Noop(); |
1632 |
– |
f = new CompletableFuture<Integer>(); |
1633 |
– |
f2 = new CompletableFuture<Integer>(); |
1634 |
– |
f2.completeExceptionally(new CFException()); |
1635 |
– |
g = f.runAfterEitherAsync(f2, r); |
1636 |
– |
f.complete(one); |
1637 |
– |
checkCompletedWithWrappedCFException(g); |
1638 |
– |
} |
1639 |
– |
|
1640 |
– |
/** |
1641 |
– |
* runAfterEitherAsync result completes exceptionally if action does |
1642 |
– |
*/ |
1643 |
– |
public void testRunAfterEitherAsync3() { |
1644 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1645 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1646 |
– |
FailingNoop r = new FailingNoop(); |
1647 |
– |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1648 |
– |
f.complete(one); |
1649 |
– |
checkCompletedWithWrappedCFException(g); |
1650 |
– |
} |
1651 |
– |
|
1652 |
– |
/** |
1653 |
– |
* runAfterEitherAsync result completes exceptionally if either |
1654 |
– |
* source cancelled |
1655 |
– |
*/ |
1656 |
– |
public void testRunAfterEitherAsync4() { |
1657 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1658 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1659 |
– |
Noop r = new Noop(); |
1660 |
– |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1661 |
– |
assertTrue(f.cancel(true)); |
1662 |
– |
checkCompletedWithWrappedCancellationException(g); |
1663 |
– |
|
1664 |
– |
r = new Noop(); |
1665 |
– |
f = new CompletableFuture<Integer>(); |
1666 |
– |
f2 = new CompletableFuture<Integer>(); |
1667 |
– |
assertTrue(f2.cancel(true)); |
1668 |
– |
g = f.runAfterEitherAsync(f2, r); |
1669 |
– |
checkCompletedWithWrappedCancellationException(g); |
1670 |
– |
} |
1671 |
– |
|
1672 |
– |
/** |
1673 |
– |
* thenComposeAsync result completes normally after normal |
1674 |
– |
* completion of source |
1675 |
– |
*/ |
1676 |
– |
public void testThenComposeAsync() { |
1677 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1678 |
– |
CompletableFutureInc r = new CompletableFutureInc(); |
1679 |
– |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1680 |
– |
f.complete(one); |
1681 |
– |
checkCompletedNormally(g, two); |
1682 |
– |
} |
1683 |
– |
|
1684 |
– |
/** |
1685 |
– |
* thenComposeAsync result completes exceptionally after |
1686 |
– |
* exceptional completion of source |
1687 |
– |
*/ |
1688 |
– |
public void testThenComposeAsync2() { |
1689 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1690 |
– |
CompletableFutureInc r = new CompletableFutureInc(); |
1691 |
– |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1692 |
– |
f.completeExceptionally(new CFException()); |
1693 |
– |
checkCompletedWithWrappedCFException(g); |
1694 |
– |
} |
1695 |
– |
|
1696 |
– |
/** |
1697 |
– |
* thenComposeAsync result completes exceptionally if action does |
1698 |
– |
*/ |
1699 |
– |
public void testThenComposeAsync3() { |
1700 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1701 |
– |
FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); |
1702 |
– |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1703 |
– |
f.complete(one); |
1704 |
– |
checkCompletedWithWrappedCFException(g); |
1705 |
– |
} |
1706 |
– |
|
1707 |
– |
/** |
1708 |
– |
* thenComposeAsync result completes exceptionally if source cancelled |
1709 |
– |
*/ |
1710 |
– |
public void testThenComposeAsync4() { |
1711 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1712 |
– |
CompletableFutureInc r = new CompletableFutureInc(); |
1713 |
– |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1714 |
– |
assertTrue(f.cancel(true)); |
1715 |
– |
checkCompletedWithWrappedCancellationException(g); |
1716 |
– |
} |
1717 |
– |
|
1718 |
– |
|
1719 |
– |
// async with explicit executors |
1720 |
– |
|
1721 |
– |
/** |
1722 |
– |
* thenRunAsync result completes normally after normal completion of source |
1723 |
– |
*/ |
1724 |
– |
public void testThenRunAsyncE() { |
1725 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1726 |
– |
Noop r = new Noop(); |
1727 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
1728 |
– |
f.complete(null); |
1729 |
– |
checkCompletedNormally(g, null); |
1730 |
– |
|
1731 |
– |
// reordered version |
1732 |
– |
f = new CompletableFuture<Integer>(); |
1733 |
– |
f.complete(null); |
1734 |
– |
r = new Noop(); |
1735 |
– |
g = f.thenRunAsync(r, new ThreadExecutor()); |
1736 |
– |
checkCompletedNormally(g, null); |
1737 |
– |
} |
1738 |
– |
|
1739 |
– |
/** |
1740 |
– |
* thenRunAsync result completes exceptionally after exceptional |
1741 |
– |
* completion of source |
1742 |
– |
*/ |
1743 |
– |
public void testThenRunAsync2E() { |
1744 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1745 |
– |
Noop r = new Noop(); |
1746 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
1747 |
– |
f.completeExceptionally(new CFException()); |
2499 |
|
try { |
2500 |
< |
g.join(); |
2501 |
< |
shouldThrow(); |
2502 |
< |
} catch (Exception ok) { |
2500 |
> |
assertNull(h5.join()); |
2501 |
> |
rs[5].assertValue(v1); |
2502 |
> |
} catch (CompletionException ok) { |
2503 |
> |
checkCompletedWithWrappedException(h5, ex); |
2504 |
> |
rs[5].assertNotInvoked(); |
2505 |
|
} |
1753 |
– |
checkCompletedWithWrappedCFException(g); |
1754 |
– |
} |
1755 |
– |
|
1756 |
– |
/** |
1757 |
– |
* thenRunAsync result completes exceptionally if action does |
1758 |
– |
*/ |
1759 |
– |
public void testThenRunAsync3E() { |
1760 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1761 |
– |
FailingNoop r = new FailingNoop(); |
1762 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
1763 |
– |
f.complete(null); |
1764 |
– |
checkCompletedWithWrappedCFException(g); |
1765 |
– |
} |
1766 |
– |
|
1767 |
– |
/** |
1768 |
– |
* thenRunAsync result completes exceptionally if source cancelled |
1769 |
– |
*/ |
1770 |
– |
public void testThenRunAsync4E() { |
1771 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1772 |
– |
Noop r = new Noop(); |
1773 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
1774 |
– |
assertTrue(f.cancel(true)); |
1775 |
– |
checkCompletedWithWrappedCancellationException(g); |
1776 |
– |
} |
1777 |
– |
|
1778 |
– |
/** |
1779 |
– |
* thenApplyAsync result completes normally after normal completion of source |
1780 |
– |
*/ |
1781 |
– |
public void testThenApplyAsyncE() { |
1782 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1783 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
1784 |
– |
f.complete(one); |
1785 |
– |
checkCompletedNormally(g, two); |
1786 |
– |
} |
1787 |
– |
|
1788 |
– |
/** |
1789 |
– |
* thenApplyAsync result completes exceptionally after exceptional |
1790 |
– |
* completion of source |
1791 |
– |
*/ |
1792 |
– |
public void testThenApplyAsync2E() { |
1793 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1794 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
1795 |
– |
f.completeExceptionally(new CFException()); |
1796 |
– |
checkCompletedWithWrappedCFException(g); |
1797 |
– |
} |
1798 |
– |
|
1799 |
– |
/** |
1800 |
– |
* thenApplyAsync result completes exceptionally if action does |
1801 |
– |
*/ |
1802 |
– |
public void testThenApplyAsync3E() { |
1803 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1804 |
– |
FailingFunction r = new FailingFunction(); |
1805 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor()); |
1806 |
– |
f.complete(null); |
1807 |
– |
checkCompletedWithWrappedCFException(g); |
1808 |
– |
} |
1809 |
– |
|
1810 |
– |
/** |
1811 |
– |
* thenApplyAsync result completes exceptionally if source cancelled |
1812 |
– |
*/ |
1813 |
– |
public void testThenApplyAsync4E() { |
1814 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1815 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
1816 |
– |
assertTrue(f.cancel(true)); |
1817 |
– |
checkCompletedWithWrappedCancellationException(g); |
1818 |
– |
} |
1819 |
– |
|
1820 |
– |
/** |
1821 |
– |
* thenAcceptAsync result completes normally after normal |
1822 |
– |
* completion of source |
1823 |
– |
*/ |
1824 |
– |
public void testThenAcceptAsyncE() { |
1825 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1826 |
– |
IncAction r = new IncAction(); |
1827 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
1828 |
– |
f.complete(one); |
1829 |
– |
checkCompletedNormally(g, null); |
1830 |
– |
assertEquals(r.value, 2); |
1831 |
– |
} |
1832 |
– |
|
1833 |
– |
/** |
1834 |
– |
* thenAcceptAsync result completes exceptionally after exceptional |
1835 |
– |
* completion of source |
1836 |
– |
*/ |
1837 |
– |
public void testThenAcceptAsync2E() { |
1838 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1839 |
– |
IncAction r = new IncAction(); |
1840 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
1841 |
– |
f.completeExceptionally(new CFException()); |
1842 |
– |
checkCompletedWithWrappedCFException(g); |
1843 |
– |
} |
1844 |
– |
|
1845 |
– |
/** |
1846 |
– |
* thenAcceptAsync result completes exceptionally if action does |
1847 |
– |
*/ |
1848 |
– |
public void testThenAcceptAsync3E() { |
1849 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1850 |
– |
FailingConsumer r = new FailingConsumer(); |
1851 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
1852 |
– |
f.complete(null); |
1853 |
– |
checkCompletedWithWrappedCFException(g); |
1854 |
– |
} |
1855 |
– |
|
1856 |
– |
/** |
1857 |
– |
* thenAcceptAsync result completes exceptionally if source cancelled |
1858 |
– |
*/ |
1859 |
– |
public void testThenAcceptAsync4E() { |
1860 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1861 |
– |
IncAction r = new IncAction(); |
1862 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
1863 |
– |
assertTrue(f.cancel(true)); |
1864 |
– |
checkCompletedWithWrappedCancellationException(g); |
1865 |
– |
} |
1866 |
– |
/** |
1867 |
– |
* thenCombineAsync result completes normally after normal |
1868 |
– |
* completion of sources |
1869 |
– |
*/ |
1870 |
– |
public void testThenCombineAsyncE() { |
1871 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1872 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1873 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
1874 |
– |
f.complete(one); |
1875 |
– |
checkIncomplete(g); |
1876 |
– |
f2.complete(two); |
1877 |
– |
checkCompletedNormally(g, three); |
1878 |
– |
} |
1879 |
– |
|
1880 |
– |
/** |
1881 |
– |
* thenCombineAsync result completes exceptionally after exceptional |
1882 |
– |
* completion of source |
1883 |
– |
*/ |
1884 |
– |
public void testThenCombineAsync2E() { |
1885 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1886 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1887 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
1888 |
– |
f.completeExceptionally(new CFException()); |
1889 |
– |
f2.complete(two); |
1890 |
– |
checkCompletedWithWrappedCFException(g); |
1891 |
– |
|
1892 |
– |
f = new CompletableFuture<Integer>(); |
1893 |
– |
f2 = new CompletableFuture<Integer>(); |
1894 |
– |
g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
1895 |
– |
f.complete(one); |
1896 |
– |
f2.completeExceptionally(new CFException()); |
1897 |
– |
checkCompletedWithWrappedCFException(g); |
1898 |
– |
} |
1899 |
– |
|
1900 |
– |
/** |
1901 |
– |
* thenCombineAsync result completes exceptionally if action does |
1902 |
– |
*/ |
1903 |
– |
public void testThenCombineAsync3E() { |
1904 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1905 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1906 |
– |
FailingBiFunction r = new FailingBiFunction(); |
1907 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor()); |
1908 |
– |
f.complete(one); |
1909 |
– |
checkIncomplete(g); |
1910 |
– |
f2.complete(two); |
1911 |
– |
checkCompletedWithWrappedCFException(g); |
1912 |
– |
} |
1913 |
– |
|
1914 |
– |
/** |
1915 |
– |
* thenCombineAsync result completes exceptionally if either source cancelled |
1916 |
– |
*/ |
1917 |
– |
public void testThenCombineAsync4E() { |
1918 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1919 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1920 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
1921 |
– |
assertTrue(f.cancel(true)); |
1922 |
– |
f2.complete(two); |
1923 |
– |
checkCompletedWithWrappedCancellationException(g); |
1924 |
– |
|
1925 |
– |
f = new CompletableFuture<Integer>(); |
1926 |
– |
f2 = new CompletableFuture<Integer>(); |
1927 |
– |
g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
1928 |
– |
f.complete(one); |
1929 |
– |
assertTrue(f2.cancel(true)); |
1930 |
– |
checkCompletedWithWrappedCancellationException(g); |
1931 |
– |
} |
1932 |
– |
|
1933 |
– |
/** |
1934 |
– |
* thenAcceptBothAsync result completes normally after normal |
1935 |
– |
* completion of sources |
1936 |
– |
*/ |
1937 |
– |
public void testThenAcceptBothAsyncE() { |
1938 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1939 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1940 |
– |
AddAction r = new AddAction(); |
1941 |
– |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
1942 |
– |
f.complete(one); |
1943 |
– |
checkIncomplete(g); |
1944 |
– |
f2.complete(two); |
1945 |
– |
checkCompletedNormally(g, null); |
1946 |
– |
assertEquals(r.value, 3); |
1947 |
– |
} |
1948 |
– |
|
1949 |
– |
/** |
1950 |
– |
* thenAcceptBothAsync result completes exceptionally after exceptional |
1951 |
– |
* completion of source |
1952 |
– |
*/ |
1953 |
– |
public void testThenAcceptBothAsync2E() { |
1954 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1955 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1956 |
– |
AddAction r = new AddAction(); |
1957 |
– |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
1958 |
– |
f.completeExceptionally(new CFException()); |
1959 |
– |
f2.complete(two); |
1960 |
– |
checkCompletedWithWrappedCFException(g); |
1961 |
– |
|
1962 |
– |
r = new AddAction(); |
1963 |
– |
f = new CompletableFuture<Integer>(); |
1964 |
– |
f2 = new CompletableFuture<Integer>(); |
1965 |
– |
g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
1966 |
– |
f.complete(one); |
1967 |
– |
f2.completeExceptionally(new CFException()); |
1968 |
– |
checkCompletedWithWrappedCFException(g); |
1969 |
– |
} |
1970 |
– |
|
1971 |
– |
/** |
1972 |
– |
* thenAcceptBothAsync result completes exceptionally if action does |
1973 |
– |
*/ |
1974 |
– |
public void testThenAcceptBothAsync3E() { |
1975 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1976 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1977 |
– |
FailingBiConsumer r = new FailingBiConsumer(); |
1978 |
– |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
1979 |
– |
f.complete(one); |
1980 |
– |
checkIncomplete(g); |
1981 |
– |
f2.complete(two); |
1982 |
– |
checkCompletedWithWrappedCFException(g); |
1983 |
– |
} |
1984 |
– |
|
1985 |
– |
/** |
1986 |
– |
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
1987 |
– |
*/ |
1988 |
– |
public void testThenAcceptBothAsync4E() { |
1989 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1990 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1991 |
– |
AddAction r = new AddAction(); |
1992 |
– |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
1993 |
– |
assertTrue(f.cancel(true)); |
1994 |
– |
f2.complete(two); |
1995 |
– |
checkCompletedWithWrappedCancellationException(g); |
1996 |
– |
|
1997 |
– |
r = new AddAction(); |
1998 |
– |
f = new CompletableFuture<Integer>(); |
1999 |
– |
f2 = new CompletableFuture<Integer>(); |
2000 |
– |
g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
2001 |
– |
f.complete(one); |
2002 |
– |
assertTrue(f2.cancel(true)); |
2003 |
– |
checkCompletedWithWrappedCancellationException(g); |
2004 |
– |
} |
2005 |
– |
|
2006 |
– |
/** |
2007 |
– |
* runAfterBothAsync result completes normally after normal |
2008 |
– |
* completion of sources |
2009 |
– |
*/ |
2010 |
– |
public void testRunAfterBothAsyncE() { |
2011 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2012 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2013 |
– |
Noop r = new Noop(); |
2014 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2015 |
– |
f.complete(one); |
2016 |
– |
checkIncomplete(g); |
2017 |
– |
f2.complete(two); |
2018 |
– |
checkCompletedNormally(g, null); |
2019 |
– |
assertTrue(r.ran); |
2020 |
– |
} |
2506 |
|
|
2507 |
< |
/** |
2508 |
< |
* runAfterBothAsync result completes exceptionally after exceptional |
2509 |
< |
* completion of source |
2510 |
< |
*/ |
2511 |
< |
public void testRunAfterBothAsync2E() { |
2512 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2513 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2514 |
< |
Noop r = new Noop(); |
2515 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2516 |
< |
f.completeExceptionally(new CFException()); |
2517 |
< |
f2.complete(two); |
2518 |
< |
checkCompletedWithWrappedCFException(g); |
2507 |
> |
checkCompletedExceptionally(f, ex); |
2508 |
> |
checkCompletedNormally(g, v1); |
2509 |
> |
checkCompletedWithWrappedException(h0, ex); |
2510 |
> |
checkCompletedWithWrappedException(h1, ex); |
2511 |
> |
checkCompletedWithWrappedException(h2, ex); |
2512 |
> |
checkCompletedWithWrappedException(h3, ex); |
2513 |
> |
checkCompletedWithWrappedException(h4, ex); |
2514 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2515 |
> |
}} |
2516 |
> |
|
2517 |
> |
public void testAcceptEither_exceptionalCompletion2() { |
2518 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2519 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2520 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2521 |
> |
{ |
2522 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2523 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2524 |
> |
final CFException ex = new CFException(); |
2525 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2526 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2527 |
> |
|
2528 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2529 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2530 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2531 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2532 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2533 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2534 |
|
|
2535 |
< |
r = new Noop(); |
2536 |
< |
f = new CompletableFuture<Integer>(); |
2537 |
< |
f2 = new CompletableFuture<Integer>(); |
2538 |
< |
g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2539 |
< |
f.complete(one); |
2540 |
< |
f2.completeExceptionally(new CFException()); |
2541 |
< |
checkCompletedWithWrappedCFException(g); |
2542 |
< |
} |
2535 |
> |
// unspecified behavior - both source completions available |
2536 |
> |
try { |
2537 |
> |
assertEquals(null, h0.join()); |
2538 |
> |
rs[0].assertValue(v1); |
2539 |
> |
} catch (CompletionException ok) { |
2540 |
> |
checkCompletedWithWrappedException(h0, ex); |
2541 |
> |
rs[0].assertNotInvoked(); |
2542 |
> |
} |
2543 |
> |
try { |
2544 |
> |
assertEquals(null, h1.join()); |
2545 |
> |
rs[1].assertValue(v1); |
2546 |
> |
} catch (CompletionException ok) { |
2547 |
> |
checkCompletedWithWrappedException(h1, ex); |
2548 |
> |
rs[1].assertNotInvoked(); |
2549 |
> |
} |
2550 |
> |
try { |
2551 |
> |
assertEquals(null, h2.join()); |
2552 |
> |
rs[2].assertValue(v1); |
2553 |
> |
} catch (CompletionException ok) { |
2554 |
> |
checkCompletedWithWrappedException(h2, ex); |
2555 |
> |
rs[2].assertNotInvoked(); |
2556 |
> |
} |
2557 |
> |
try { |
2558 |
> |
assertEquals(null, h3.join()); |
2559 |
> |
rs[3].assertValue(v1); |
2560 |
> |
} catch (CompletionException ok) { |
2561 |
> |
checkCompletedWithWrappedException(h3, ex); |
2562 |
> |
rs[3].assertNotInvoked(); |
2563 |
> |
} |
2564 |
|
|
2565 |
< |
/** |
2566 |
< |
* runAfterBothAsync result completes exceptionally if action does |
2567 |
< |
*/ |
2047 |
< |
public void testRunAfterBothAsync3E() { |
2048 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2049 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2050 |
< |
FailingNoop r = new FailingNoop(); |
2051 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2052 |
< |
f.complete(one); |
2053 |
< |
checkIncomplete(g); |
2054 |
< |
f2.complete(two); |
2055 |
< |
checkCompletedWithWrappedCFException(g); |
2056 |
< |
} |
2565 |
> |
checkCompletedNormally(f, v1); |
2566 |
> |
checkCompletedExceptionally(g, ex); |
2567 |
> |
}} |
2568 |
|
|
2569 |
|
/** |
2570 |
< |
* runAfterBothAsync result completes exceptionally if either source cancelled |
2570 |
> |
* acceptEither result completes exceptionally if either source cancelled |
2571 |
|
*/ |
2572 |
< |
public void testRunAfterBothAsync4E() { |
2573 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2574 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2575 |
< |
Noop r = new Noop(); |
2576 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2577 |
< |
assertTrue(f.cancel(true)); |
2578 |
< |
f2.complete(two); |
2579 |
< |
checkCompletedWithWrappedCancellationException(g); |
2580 |
< |
|
2581 |
< |
r = new Noop(); |
2582 |
< |
f = new CompletableFuture<Integer>(); |
2583 |
< |
f2 = new CompletableFuture<Integer>(); |
2584 |
< |
g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2585 |
< |
f.complete(one); |
2586 |
< |
assertTrue(f2.cancel(true)); |
2587 |
< |
checkCompletedWithWrappedCancellationException(g); |
2588 |
< |
} |
2572 |
> |
public void testAcceptEither_sourceCancelled() { |
2573 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2574 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2575 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2576 |
> |
{ |
2577 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2578 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2579 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2580 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2581 |
> |
|
2582 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2583 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2584 |
> |
checkIncomplete(h0); |
2585 |
> |
checkIncomplete(h1); |
2586 |
> |
rs[0].assertNotInvoked(); |
2587 |
> |
rs[1].assertNotInvoked(); |
2588 |
> |
f.cancel(mayInterruptIfRunning); |
2589 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2590 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2591 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2592 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2593 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2594 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2595 |
> |
|
2596 |
> |
g.complete(v1); |
2597 |
> |
|
2598 |
> |
// unspecified behavior - both source completions available |
2599 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2600 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2601 |
> |
try { |
2602 |
> |
assertNull(h4.join()); |
2603 |
> |
rs[4].assertValue(v1); |
2604 |
> |
} catch (CompletionException ok) { |
2605 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2606 |
> |
rs[4].assertNotInvoked(); |
2607 |
> |
} |
2608 |
> |
try { |
2609 |
> |
assertNull(h5.join()); |
2610 |
> |
rs[5].assertValue(v1); |
2611 |
> |
} catch (CompletionException ok) { |
2612 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2613 |
> |
rs[5].assertNotInvoked(); |
2614 |
> |
} |
2615 |
|
|
2616 |
< |
/** |
2617 |
< |
* applyToEitherAsync result completes normally after normal |
2618 |
< |
* completion of sources |
2619 |
< |
*/ |
2620 |
< |
public void testApplyToEitherAsyncE() { |
2621 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2622 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2623 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2087 |
< |
f.complete(one); |
2088 |
< |
checkCompletedNormally(g, two); |
2089 |
< |
|
2090 |
< |
f = new CompletableFuture<Integer>(); |
2091 |
< |
f.complete(one); |
2092 |
< |
f2 = new CompletableFuture<Integer>(); |
2093 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2094 |
< |
checkCompletedNormally(g, two); |
2095 |
< |
} |
2616 |
> |
checkCancelled(f); |
2617 |
> |
checkCompletedNormally(g, v1); |
2618 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2619 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2620 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2621 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2622 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2623 |
> |
}} |
2624 |
|
|
2625 |
|
/** |
2626 |
< |
* applyToEitherAsync result completes exceptionally after exceptional |
2099 |
< |
* completion of source |
2626 |
> |
* acceptEither result completes exceptionally if action does |
2627 |
|
*/ |
2628 |
< |
public void testApplyToEitherAsync2E() { |
2629 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2630 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2631 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2632 |
< |
f.completeExceptionally(new CFException()); |
2633 |
< |
checkCompletedWithWrappedCFException(g); |
2634 |
< |
|
2635 |
< |
f = new CompletableFuture<Integer>(); |
2636 |
< |
f2 = new CompletableFuture<Integer>(); |
2637 |
< |
f2.completeExceptionally(new CFException()); |
2638 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2639 |
< |
f.complete(one); |
2640 |
< |
checkCompletedWithWrappedCFException(g); |
2641 |
< |
} |
2628 |
> |
public void testAcceptEither_actionFailed() { |
2629 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2630 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2631 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2632 |
> |
{ |
2633 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2634 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2635 |
> |
final FailingConsumer[] rs = new FailingConsumer[6]; |
2636 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
2637 |
> |
|
2638 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2639 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2640 |
> |
f.complete(v1); |
2641 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2642 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2643 |
> |
checkCompletedWithWrappedCFException(h0); |
2644 |
> |
checkCompletedWithWrappedCFException(h1); |
2645 |
> |
checkCompletedWithWrappedCFException(h2); |
2646 |
> |
checkCompletedWithWrappedCFException(h3); |
2647 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2648 |
> |
|
2649 |
> |
g.complete(v2); |
2650 |
> |
|
2651 |
> |
// unspecified behavior - both source completions available |
2652 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2653 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2654 |
> |
|
2655 |
> |
checkCompletedWithWrappedCFException(h4); |
2656 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2657 |
> |
Objects.equals(v2, rs[4].value)); |
2658 |
> |
checkCompletedWithWrappedCFException(h5); |
2659 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2660 |
> |
Objects.equals(v2, rs[5].value)); |
2661 |
> |
|
2662 |
> |
checkCompletedNormally(f, v1); |
2663 |
> |
checkCompletedNormally(g, v2); |
2664 |
> |
}} |
2665 |
|
|
2666 |
|
/** |
2667 |
< |
* applyToEitherAsync result completes exceptionally if action does |
2667 |
> |
* runAfterEither result completes normally after normal completion |
2668 |
> |
* of either source |
2669 |
|
*/ |
2670 |
< |
public void testApplyToEitherAsync3E() { |
2671 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2672 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2673 |
< |
FailingFunction r = new FailingFunction(); |
2674 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor()); |
2675 |
< |
f.complete(one); |
2676 |
< |
checkCompletedWithWrappedCFException(g); |
2677 |
< |
} |
2670 |
> |
public void testRunAfterEither_normalCompletion() { |
2671 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2672 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2673 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2674 |
> |
{ |
2675 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2676 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2677 |
> |
final Noop[] rs = new Noop[6]; |
2678 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2679 |
> |
|
2680 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2681 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2682 |
> |
checkIncomplete(h0); |
2683 |
> |
checkIncomplete(h1); |
2684 |
> |
rs[0].assertNotInvoked(); |
2685 |
> |
rs[1].assertNotInvoked(); |
2686 |
> |
f.complete(v1); |
2687 |
> |
checkCompletedNormally(h0, null); |
2688 |
> |
checkCompletedNormally(h1, null); |
2689 |
> |
rs[0].assertInvoked(); |
2690 |
> |
rs[1].assertInvoked(); |
2691 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2692 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2693 |
> |
checkCompletedNormally(h2, null); |
2694 |
> |
checkCompletedNormally(h3, null); |
2695 |
> |
rs[2].assertInvoked(); |
2696 |
> |
rs[3].assertInvoked(); |
2697 |
> |
|
2698 |
> |
g.complete(v2); |
2699 |
> |
|
2700 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2701 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2702 |
> |
|
2703 |
> |
checkCompletedNormally(f, v1); |
2704 |
> |
checkCompletedNormally(g, v2); |
2705 |
> |
checkCompletedNormally(h0, null); |
2706 |
> |
checkCompletedNormally(h1, null); |
2707 |
> |
checkCompletedNormally(h2, null); |
2708 |
> |
checkCompletedNormally(h3, null); |
2709 |
> |
checkCompletedNormally(h4, null); |
2710 |
> |
checkCompletedNormally(h5, null); |
2711 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2712 |
> |
}} |
2713 |
|
|
2714 |
|
/** |
2715 |
< |
* applyToEitherAsync result completes exceptionally if either source cancelled |
2715 |
> |
* runAfterEither result completes exceptionally after exceptional |
2716 |
> |
* completion of either source |
2717 |
|
*/ |
2718 |
< |
public void testApplyToEitherAsync4E() { |
2719 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2720 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2721 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2722 |
< |
assertTrue(f.cancel(true)); |
2723 |
< |
checkCompletedWithWrappedCancellationException(g); |
2718 |
> |
public void testRunAfterEither_exceptionalCompletion() { |
2719 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2720 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2721 |
> |
{ |
2722 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2723 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2724 |
> |
final CFException ex = new CFException(); |
2725 |
> |
final Noop[] rs = new Noop[6]; |
2726 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2727 |
> |
|
2728 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2729 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2730 |
> |
checkIncomplete(h0); |
2731 |
> |
checkIncomplete(h1); |
2732 |
> |
rs[0].assertNotInvoked(); |
2733 |
> |
rs[1].assertNotInvoked(); |
2734 |
> |
assertTrue(f.completeExceptionally(ex)); |
2735 |
> |
checkCompletedWithWrappedException(h0, ex); |
2736 |
> |
checkCompletedWithWrappedException(h1, ex); |
2737 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2738 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2739 |
> |
checkCompletedWithWrappedException(h2, ex); |
2740 |
> |
checkCompletedWithWrappedException(h3, ex); |
2741 |
> |
|
2742 |
> |
assertTrue(g.complete(v1)); |
2743 |
> |
|
2744 |
> |
// unspecified behavior - both source completions available |
2745 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2746 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2747 |
> |
try { |
2748 |
> |
assertNull(h4.join()); |
2749 |
> |
rs[4].assertInvoked(); |
2750 |
> |
} catch (CompletionException ok) { |
2751 |
> |
checkCompletedWithWrappedException(h4, ex); |
2752 |
> |
rs[4].assertNotInvoked(); |
2753 |
> |
} |
2754 |
> |
try { |
2755 |
> |
assertNull(h5.join()); |
2756 |
> |
rs[5].assertInvoked(); |
2757 |
> |
} catch (CompletionException ok) { |
2758 |
> |
checkCompletedWithWrappedException(h5, ex); |
2759 |
> |
rs[5].assertNotInvoked(); |
2760 |
> |
} |
2761 |
|
|
2762 |
< |
f = new CompletableFuture<Integer>(); |
2763 |
< |
f2 = new CompletableFuture<Integer>(); |
2764 |
< |
assertTrue(f2.cancel(true)); |
2765 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2766 |
< |
checkCompletedWithWrappedCancellationException(g); |
2767 |
< |
} |
2762 |
> |
checkCompletedExceptionally(f, ex); |
2763 |
> |
checkCompletedNormally(g, v1); |
2764 |
> |
checkCompletedWithWrappedException(h0, ex); |
2765 |
> |
checkCompletedWithWrappedException(h1, ex); |
2766 |
> |
checkCompletedWithWrappedException(h2, ex); |
2767 |
> |
checkCompletedWithWrappedException(h3, ex); |
2768 |
> |
checkCompletedWithWrappedException(h4, ex); |
2769 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2770 |
> |
}} |
2771 |
> |
|
2772 |
> |
public void testRunAfterEither_exceptionalCompletion2() { |
2773 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2774 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2775 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2776 |
> |
{ |
2777 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2778 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2779 |
> |
final CFException ex = new CFException(); |
2780 |
> |
final Noop[] rs = new Noop[6]; |
2781 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2782 |
> |
|
2783 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2784 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2785 |
> |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2786 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2787 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2788 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2789 |
|
|
2790 |
< |
/** |
2791 |
< |
* acceptEitherAsync result completes normally after normal |
2792 |
< |
* completion of sources |
2793 |
< |
*/ |
2794 |
< |
public void testAcceptEitherAsyncE() { |
2795 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2796 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2797 |
< |
IncAction r = new IncAction(); |
2798 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2799 |
< |
f.complete(one); |
2800 |
< |
checkCompletedNormally(g, null); |
2801 |
< |
assertEquals(r.value, 2); |
2790 |
> |
// unspecified behavior - both source completions available |
2791 |
> |
try { |
2792 |
> |
assertEquals(null, h0.join()); |
2793 |
> |
rs[0].assertInvoked(); |
2794 |
> |
} catch (CompletionException ok) { |
2795 |
> |
checkCompletedWithWrappedException(h0, ex); |
2796 |
> |
rs[0].assertNotInvoked(); |
2797 |
> |
} |
2798 |
> |
try { |
2799 |
> |
assertEquals(null, h1.join()); |
2800 |
> |
rs[1].assertInvoked(); |
2801 |
> |
} catch (CompletionException ok) { |
2802 |
> |
checkCompletedWithWrappedException(h1, ex); |
2803 |
> |
rs[1].assertNotInvoked(); |
2804 |
> |
} |
2805 |
> |
try { |
2806 |
> |
assertEquals(null, h2.join()); |
2807 |
> |
rs[2].assertInvoked(); |
2808 |
> |
} catch (CompletionException ok) { |
2809 |
> |
checkCompletedWithWrappedException(h2, ex); |
2810 |
> |
rs[2].assertNotInvoked(); |
2811 |
> |
} |
2812 |
> |
try { |
2813 |
> |
assertEquals(null, h3.join()); |
2814 |
> |
rs[3].assertInvoked(); |
2815 |
> |
} catch (CompletionException ok) { |
2816 |
> |
checkCompletedWithWrappedException(h3, ex); |
2817 |
> |
rs[3].assertNotInvoked(); |
2818 |
> |
} |
2819 |
|
|
2820 |
< |
r = new IncAction(); |
2821 |
< |
f = new CompletableFuture<Integer>(); |
2822 |
< |
f.complete(one); |
2161 |
< |
f2 = new CompletableFuture<Integer>(); |
2162 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2163 |
< |
checkCompletedNormally(g, null); |
2164 |
< |
assertEquals(r.value, 2); |
2165 |
< |
} |
2820 |
> |
checkCompletedNormally(f, v1); |
2821 |
> |
checkCompletedExceptionally(g, ex); |
2822 |
> |
}} |
2823 |
|
|
2824 |
|
/** |
2825 |
< |
* acceptEitherAsync result completes exceptionally after exceptional |
2169 |
< |
* completion of source |
2825 |
> |
* runAfterEither result completes exceptionally if either source cancelled |
2826 |
|
*/ |
2827 |
< |
public void testAcceptEitherAsync2E() { |
2828 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2829 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2830 |
< |
IncAction r = new IncAction(); |
2831 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2832 |
< |
f.completeExceptionally(new CFException()); |
2833 |
< |
checkCompletedWithWrappedCFException(g); |
2834 |
< |
|
2835 |
< |
r = new IncAction(); |
2836 |
< |
f = new CompletableFuture<Integer>(); |
2837 |
< |
f2 = new CompletableFuture<Integer>(); |
2838 |
< |
f2.completeExceptionally(new CFException()); |
2839 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2840 |
< |
f.complete(one); |
2841 |
< |
checkCompletedWithWrappedCFException(g); |
2842 |
< |
} |
2827 |
> |
public void testRunAfterEither_sourceCancelled() { |
2828 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2829 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2830 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2831 |
> |
{ |
2832 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2833 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2834 |
> |
final Noop[] rs = new Noop[6]; |
2835 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2836 |
> |
|
2837 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2838 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2839 |
> |
checkIncomplete(h0); |
2840 |
> |
checkIncomplete(h1); |
2841 |
> |
rs[0].assertNotInvoked(); |
2842 |
> |
rs[1].assertNotInvoked(); |
2843 |
> |
f.cancel(mayInterruptIfRunning); |
2844 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2845 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2846 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2847 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2848 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2849 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2850 |
> |
|
2851 |
> |
assertTrue(g.complete(v1)); |
2852 |
> |
|
2853 |
> |
// unspecified behavior - both source completions available |
2854 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2855 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2856 |
> |
try { |
2857 |
> |
assertNull(h4.join()); |
2858 |
> |
rs[4].assertInvoked(); |
2859 |
> |
} catch (CompletionException ok) { |
2860 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2861 |
> |
rs[4].assertNotInvoked(); |
2862 |
> |
} |
2863 |
> |
try { |
2864 |
> |
assertNull(h5.join()); |
2865 |
> |
rs[5].assertInvoked(); |
2866 |
> |
} catch (CompletionException ok) { |
2867 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2868 |
> |
rs[5].assertNotInvoked(); |
2869 |
> |
} |
2870 |
|
|
2871 |
< |
/** |
2872 |
< |
* acceptEitherAsync result completes exceptionally if action does |
2873 |
< |
*/ |
2874 |
< |
public void testAcceptEitherAsync3E() { |
2875 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2876 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2877 |
< |
FailingConsumer r = new FailingConsumer(); |
2878 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2196 |
< |
f.complete(one); |
2197 |
< |
checkCompletedWithWrappedCFException(g); |
2198 |
< |
} |
2871 |
> |
checkCancelled(f); |
2872 |
> |
checkCompletedNormally(g, v1); |
2873 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2874 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2875 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2876 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2877 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2878 |
> |
}} |
2879 |
|
|
2880 |
|
/** |
2881 |
< |
* acceptEitherAsync result completes exceptionally if either |
2202 |
< |
* source cancelled |
2881 |
> |
* runAfterEither result completes exceptionally if action does |
2882 |
|
*/ |
2883 |
< |
public void testAcceptEitherAsync4E() { |
2884 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2885 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2886 |
< |
IncAction r = new IncAction(); |
2887 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2888 |
< |
assertTrue(f.cancel(true)); |
2889 |
< |
checkCompletedWithWrappedCancellationException(g); |
2890 |
< |
|
2891 |
< |
r = new IncAction(); |
2892 |
< |
f = new CompletableFuture<Integer>(); |
2893 |
< |
f2 = new CompletableFuture<Integer>(); |
2894 |
< |
assertTrue(f2.cancel(true)); |
2895 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2896 |
< |
checkCompletedWithWrappedCancellationException(g); |
2897 |
< |
} |
2883 |
> |
public void testRunAfterEither_actionFailed() { |
2884 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2885 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2886 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2887 |
> |
{ |
2888 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2889 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2890 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
2891 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
2892 |
> |
|
2893 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2894 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2895 |
> |
assertTrue(f.complete(v1)); |
2896 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2897 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2898 |
> |
checkCompletedWithWrappedCFException(h0); |
2899 |
> |
checkCompletedWithWrappedCFException(h1); |
2900 |
> |
checkCompletedWithWrappedCFException(h2); |
2901 |
> |
checkCompletedWithWrappedCFException(h3); |
2902 |
> |
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2903 |
> |
assertTrue(g.complete(v2)); |
2904 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2905 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2906 |
> |
checkCompletedWithWrappedCFException(h4); |
2907 |
> |
checkCompletedWithWrappedCFException(h5); |
2908 |
> |
|
2909 |
> |
checkCompletedNormally(f, v1); |
2910 |
> |
checkCompletedNormally(g, v2); |
2911 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2912 |
> |
}} |
2913 |
|
|
2914 |
|
/** |
2915 |
< |
* runAfterEitherAsync result completes normally after normal |
2222 |
< |
* completion of sources |
2915 |
> |
* thenCompose result completes normally after normal completion of source |
2916 |
|
*/ |
2917 |
< |
public void testRunAfterEitherAsyncE() { |
2918 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2919 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2920 |
< |
Noop r = new Noop(); |
2921 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2922 |
< |
f.complete(one); |
2923 |
< |
checkCompletedNormally(g, null); |
2924 |
< |
assertTrue(r.ran); |
2925 |
< |
|
2926 |
< |
r = new Noop(); |
2927 |
< |
f = new CompletableFuture<Integer>(); |
2928 |
< |
f.complete(one); |
2929 |
< |
f2 = new CompletableFuture<Integer>(); |
2930 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2931 |
< |
checkCompletedNormally(g, null); |
2239 |
< |
assertTrue(r.ran); |
2240 |
< |
} |
2917 |
> |
public void testThenCompose_normalCompletion() { |
2918 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2919 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2920 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2921 |
> |
{ |
2922 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2923 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2924 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2925 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2926 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2927 |
> |
|
2928 |
> |
checkCompletedNormally(g, inc(v1)); |
2929 |
> |
checkCompletedNormally(f, v1); |
2930 |
> |
r.assertValue(v1); |
2931 |
> |
}} |
2932 |
|
|
2933 |
|
/** |
2934 |
< |
* runAfterEitherAsync result completes exceptionally after exceptional |
2934 |
> |
* thenCompose result completes exceptionally after exceptional |
2935 |
|
* completion of source |
2936 |
|
*/ |
2937 |
< |
public void testRunAfterEitherAsync2E() { |
2938 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2939 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2940 |
< |
Noop r = new Noop(); |
2941 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2942 |
< |
f.completeExceptionally(new CFException()); |
2943 |
< |
checkCompletedWithWrappedCFException(g); |
2944 |
< |
|
2945 |
< |
r = new Noop(); |
2946 |
< |
f = new CompletableFuture<Integer>(); |
2947 |
< |
f2 = new CompletableFuture<Integer>(); |
2948 |
< |
f2.completeExceptionally(new CFException()); |
2949 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2950 |
< |
f.complete(one); |
2951 |
< |
checkCompletedWithWrappedCFException(g); |
2261 |
< |
} |
2262 |
< |
|
2263 |
< |
/** |
2264 |
< |
* runAfterEitherAsync result completes exceptionally if action does |
2265 |
< |
*/ |
2266 |
< |
public void testRunAfterEitherAsync3E() { |
2267 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2268 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2269 |
< |
FailingNoop r = new FailingNoop(); |
2270 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2271 |
< |
f.complete(one); |
2272 |
< |
checkCompletedWithWrappedCFException(g); |
2273 |
< |
} |
2274 |
< |
|
2275 |
< |
/** |
2276 |
< |
* runAfterEitherAsync result completes exceptionally if either |
2277 |
< |
* source cancelled |
2278 |
< |
*/ |
2279 |
< |
public void testRunAfterEitherAsync4E() { |
2280 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2281 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2282 |
< |
Noop r = new Noop(); |
2283 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2284 |
< |
assertTrue(f.cancel(true)); |
2285 |
< |
checkCompletedWithWrappedCancellationException(g); |
2286 |
< |
|
2287 |
< |
r = new Noop(); |
2288 |
< |
f = new CompletableFuture<Integer>(); |
2289 |
< |
f2 = new CompletableFuture<Integer>(); |
2290 |
< |
assertTrue(f2.cancel(true)); |
2291 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2292 |
< |
checkCompletedWithWrappedCancellationException(g); |
2293 |
< |
} |
2937 |
> |
public void testThenCompose_exceptionalCompletion() { |
2938 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2939 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2940 |
> |
{ |
2941 |
> |
final CFException ex = new CFException(); |
2942 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2943 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2944 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
2945 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2946 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
2947 |
> |
|
2948 |
> |
checkCompletedWithWrappedException(g, ex); |
2949 |
> |
checkCompletedExceptionally(f, ex); |
2950 |
> |
r.assertNotInvoked(); |
2951 |
> |
}} |
2952 |
|
|
2953 |
|
/** |
2954 |
< |
* thenComposeAsync result completes normally after normal |
2297 |
< |
* completion of source |
2954 |
> |
* thenCompose result completes exceptionally if action does |
2955 |
|
*/ |
2956 |
< |
public void testThenComposeAsyncE() { |
2957 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2958 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
2959 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2960 |
< |
f.complete(one); |
2961 |
< |
checkCompletedNormally(g, two); |
2962 |
< |
} |
2956 |
> |
public void testThenCompose_actionFailed() { |
2957 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2958 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2959 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2960 |
> |
{ |
2961 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2962 |
> |
final FailingCompletableFutureFunction r |
2963 |
> |
= new FailingCompletableFutureFunction(m); |
2964 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2965 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2966 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2967 |
|
|
2307 |
– |
/** |
2308 |
– |
* thenComposeAsync result completes exceptionally after |
2309 |
– |
* exceptional completion of source |
2310 |
– |
*/ |
2311 |
– |
public void testThenComposeAsync2E() { |
2312 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2313 |
– |
CompletableFutureInc r = new CompletableFutureInc(); |
2314 |
– |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2315 |
– |
f.completeExceptionally(new CFException()); |
2968 |
|
checkCompletedWithWrappedCFException(g); |
2969 |
< |
} |
2969 |
> |
checkCompletedNormally(f, v1); |
2970 |
> |
}} |
2971 |
|
|
2972 |
|
/** |
2973 |
< |
* thenComposeAsync result completes exceptionally if action does |
2973 |
> |
* thenCompose result completes exceptionally if source cancelled |
2974 |
|
*/ |
2975 |
< |
public void testThenComposeAsync3E() { |
2976 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2977 |
< |
FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); |
2978 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2979 |
< |
f.complete(one); |
2980 |
< |
checkCompletedWithWrappedCFException(g); |
2981 |
< |
} |
2975 |
> |
public void testThenCompose_sourceCancelled() { |
2976 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2977 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2978 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2979 |
> |
{ |
2980 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2981 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2982 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2983 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2984 |
> |
if (createIncomplete) { |
2985 |
> |
checkIncomplete(g); |
2986 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2987 |
> |
} |
2988 |
|
|
2330 |
– |
/** |
2331 |
– |
* thenComposeAsync result completes exceptionally if source cancelled |
2332 |
– |
*/ |
2333 |
– |
public void testThenComposeAsync4E() { |
2334 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2335 |
– |
CompletableFutureInc r = new CompletableFutureInc(); |
2336 |
– |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2337 |
– |
assertTrue(f.cancel(true)); |
2989 |
|
checkCompletedWithWrappedCancellationException(g); |
2990 |
< |
} |
2990 |
> |
checkCancelled(f); |
2991 |
> |
}} |
2992 |
|
|
2993 |
|
// other static methods |
2994 |
|
|
2997 |
|
* with the value null |
2998 |
|
*/ |
2999 |
|
public void testAllOf_empty() throws Exception { |
3000 |
< |
CompletableFuture<?> f = CompletableFuture.allOf(); |
3000 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(); |
3001 |
|
checkCompletedNormally(f, null); |
3002 |
|
} |
3003 |
|
|
3004 |
|
/** |
3005 |
< |
* allOf returns a future completed when all components complete |
3005 |
> |
* allOf returns a future completed normally with the value null |
3006 |
> |
* when all components complete normally |
3007 |
|
*/ |
3008 |
< |
public void testAllOf() throws Exception { |
3009 |
< |
for (int k = 1; k < 20; ++k) { |
3010 |
< |
CompletableFuture[] fs = new CompletableFuture[k]; |
3011 |
< |
for (int i = 0; i < k; ++i) |
3012 |
< |
fs[i] = new CompletableFuture<Integer>(); |
3008 |
> |
public void testAllOf_normal() throws Exception { |
3009 |
> |
for (int k = 1; k < 10; k++) { |
3010 |
> |
CompletableFuture<Integer>[] fs |
3011 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3012 |
> |
for (int i = 0; i < k; i++) |
3013 |
> |
fs[i] = new CompletableFuture<>(); |
3014 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3015 |
> |
for (int i = 0; i < k; i++) { |
3016 |
> |
checkIncomplete(f); |
3017 |
> |
checkIncomplete(CompletableFuture.allOf(fs)); |
3018 |
> |
fs[i].complete(one); |
3019 |
> |
} |
3020 |
> |
checkCompletedNormally(f, null); |
3021 |
> |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
3022 |
> |
} |
3023 |
> |
} |
3024 |
> |
|
3025 |
> |
public void testAllOf_backwards() throws Exception { |
3026 |
> |
for (int k = 1; k < 10; k++) { |
3027 |
> |
CompletableFuture<Integer>[] fs |
3028 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3029 |
> |
for (int i = 0; i < k; i++) |
3030 |
> |
fs[i] = new CompletableFuture<>(); |
3031 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3032 |
< |
for (int i = 0; i < k; ++i) { |
3032 |
> |
for (int i = k - 1; i >= 0; i--) { |
3033 |
|
checkIncomplete(f); |
3034 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3035 |
|
fs[i].complete(one); |
3036 |
|
} |
3037 |
|
checkCompletedNormally(f, null); |
3038 |
+ |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
3039 |
+ |
} |
3040 |
+ |
} |
3041 |
+ |
|
3042 |
+ |
public void testAllOf_exceptional() throws Exception { |
3043 |
+ |
for (int k = 1; k < 10; k++) { |
3044 |
+ |
CompletableFuture<Integer>[] fs |
3045 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3046 |
+ |
CFException ex = new CFException(); |
3047 |
+ |
for (int i = 0; i < k; i++) |
3048 |
+ |
fs[i] = new CompletableFuture<>(); |
3049 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3050 |
+ |
for (int i = 0; i < k; i++) { |
3051 |
+ |
checkIncomplete(f); |
3052 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3053 |
+ |
if (i != k/2) { |
3054 |
+ |
fs[i].complete(i); |
3055 |
+ |
checkCompletedNormally(fs[i], i); |
3056 |
+ |
} else { |
3057 |
+ |
fs[i].completeExceptionally(ex); |
3058 |
+ |
checkCompletedExceptionally(fs[i], ex); |
3059 |
+ |
} |
3060 |
+ |
} |
3061 |
+ |
checkCompletedWithWrappedException(f, ex); |
3062 |
+ |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3063 |
|
} |
3064 |
|
} |
3065 |
|
|
3067 |
|
* anyOf(no component futures) returns an incomplete future |
3068 |
|
*/ |
3069 |
|
public void testAnyOf_empty() throws Exception { |
3070 |
< |
CompletableFuture<?> f = CompletableFuture.anyOf(); |
3070 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3071 |
> |
{ |
3072 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3073 |
|
checkIncomplete(f); |
3074 |
+ |
|
3075 |
+ |
f.complete(v1); |
3076 |
+ |
checkCompletedNormally(f, v1); |
3077 |
+ |
}} |
3078 |
+ |
|
3079 |
+ |
/** |
3080 |
+ |
* anyOf returns a future completed normally with a value when |
3081 |
+ |
* a component future does |
3082 |
+ |
*/ |
3083 |
+ |
public void testAnyOf_normal() throws Exception { |
3084 |
+ |
for (int k = 0; k < 10; k++) { |
3085 |
+ |
CompletableFuture[] fs = new CompletableFuture[k]; |
3086 |
+ |
for (int i = 0; i < k; i++) |
3087 |
+ |
fs[i] = new CompletableFuture<>(); |
3088 |
+ |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3089 |
+ |
checkIncomplete(f); |
3090 |
+ |
for (int i = 0; i < k; i++) { |
3091 |
+ |
fs[i].complete(i); |
3092 |
+ |
checkCompletedNormally(f, 0); |
3093 |
+ |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3094 |
+ |
assertTrue(0 <= x && x <= i); |
3095 |
+ |
} |
3096 |
+ |
} |
3097 |
+ |
} |
3098 |
+ |
public void testAnyOf_normal_backwards() throws Exception { |
3099 |
+ |
for (int k = 0; k < 10; k++) { |
3100 |
+ |
CompletableFuture[] fs = new CompletableFuture[k]; |
3101 |
+ |
for (int i = 0; i < k; i++) |
3102 |
+ |
fs[i] = new CompletableFuture<>(); |
3103 |
+ |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3104 |
+ |
checkIncomplete(f); |
3105 |
+ |
for (int i = k - 1; i >= 0; i--) { |
3106 |
+ |
fs[i].complete(i); |
3107 |
+ |
checkCompletedNormally(f, k - 1); |
3108 |
+ |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3109 |
+ |
assertTrue(i <= x && x <= k - 1); |
3110 |
+ |
} |
3111 |
+ |
} |
3112 |
|
} |
3113 |
|
|
3114 |
|
/** |
3115 |
< |
* anyOf returns a future completed when any components complete |
3115 |
> |
* anyOf result completes exceptionally when any component does. |
3116 |
|
*/ |
3117 |
< |
public void testAnyOf() throws Exception { |
3118 |
< |
for (int k = 1; k < 20; ++k) { |
3117 |
> |
public void testAnyOf_exceptional() throws Exception { |
3118 |
> |
for (int k = 0; k < 10; k++) { |
3119 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3120 |
< |
for (int i = 0; i < k; ++i) |
3121 |
< |
fs[i] = new CompletableFuture<Integer>(); |
3120 |
> |
CFException[] exs = new CFException[k]; |
3121 |
> |
for (int i = 0; i < k; i++) { |
3122 |
> |
fs[i] = new CompletableFuture<>(); |
3123 |
> |
exs[i] = new CFException(); |
3124 |
> |
} |
3125 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3126 |
|
checkIncomplete(f); |
3127 |
< |
for (int i = 0; i < k; ++i) { |
3128 |
< |
fs[i].complete(one); |
3129 |
< |
checkCompletedNormally(f, one); |
3127 |
> |
for (int i = 0; i < k; i++) { |
3128 |
> |
fs[i].completeExceptionally(exs[i]); |
3129 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3130 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3131 |
> |
} |
3132 |
> |
} |
3133 |
> |
} |
3134 |
> |
|
3135 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3136 |
> |
for (int k = 0; k < 10; k++) { |
3137 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3138 |
> |
CFException[] exs = new CFException[k]; |
3139 |
> |
for (int i = 0; i < k; i++) { |
3140 |
> |
fs[i] = new CompletableFuture<>(); |
3141 |
> |
exs[i] = new CFException(); |
3142 |
> |
} |
3143 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3144 |
> |
checkIncomplete(f); |
3145 |
> |
for (int i = k - 1; i >= 0; i--) { |
3146 |
> |
fs[i].completeExceptionally(exs[i]); |
3147 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3148 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3149 |
|
} |
3150 |
|
} |
3151 |
|
} |
3154 |
|
* Completion methods throw NullPointerException with null arguments |
3155 |
|
*/ |
3156 |
|
public void testNPE() { |
3157 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
3158 |
< |
CompletableFuture<Integer> g = new CompletableFuture<Integer>(); |
3157 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3158 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3159 |
|
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
3160 |
|
CompletableFuture<?> h; |
3161 |
< |
Executor exec = new ThreadExecutor(); |
3161 |
> |
ThreadExecutor exec = new ThreadExecutor(); |
3162 |
|
|
3163 |
|
Runnable[] throwingActions = { |
3164 |
< |
() -> { CompletableFuture.supplyAsync(null); }, |
3165 |
< |
() -> { CompletableFuture.supplyAsync(null, exec); }, |
3166 |
< |
() -> { CompletableFuture.supplyAsync(() -> one, null); }, |
3167 |
< |
|
3168 |
< |
() -> { CompletableFuture.runAsync(null); }, |
3169 |
< |
() -> { CompletableFuture.runAsync(null, exec); }, |
3170 |
< |
() -> { CompletableFuture.runAsync(() -> {}, null); }, |
3171 |
< |
|
3172 |
< |
() -> { f.completeExceptionally(null); }, |
3173 |
< |
|
3174 |
< |
() -> { f.thenApply(null); }, |
3175 |
< |
() -> { f.thenApplyAsync(null); }, |
3176 |
< |
() -> { f.thenApplyAsync((x) -> x, null); }, |
3177 |
< |
() -> { f.thenApplyAsync(null, exec); }, |
3178 |
< |
|
3179 |
< |
() -> { f.thenAccept(null); }, |
3180 |
< |
() -> { f.thenAcceptAsync(null); }, |
3181 |
< |
() -> { f.thenAcceptAsync((x) -> { ; }, null); }, |
3182 |
< |
() -> { f.thenAcceptAsync(null, exec); }, |
3183 |
< |
|
3184 |
< |
() -> { f.thenRun(null); }, |
3185 |
< |
() -> { f.thenRunAsync(null); }, |
3186 |
< |
() -> { f.thenRunAsync(() -> { ; }, null); }, |
3187 |
< |
() -> { f.thenRunAsync(null, exec); }, |
3188 |
< |
|
3189 |
< |
() -> { f.thenCombine(g, null); }, |
3190 |
< |
() -> { f.thenCombineAsync(g, null); }, |
3191 |
< |
() -> { f.thenCombineAsync(g, null, exec); }, |
3192 |
< |
() -> { f.thenCombine(nullFuture, (x, y) -> x); }, |
3193 |
< |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x); }, |
3194 |
< |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); }, |
3195 |
< |
() -> { f.thenCombineAsync(g, (x, y) -> x, null); }, |
3196 |
< |
|
3197 |
< |
() -> { f.thenAcceptBoth(g, null); }, |
3198 |
< |
() -> { f.thenAcceptBothAsync(g, null); }, |
3199 |
< |
() -> { f.thenAcceptBothAsync(g, null, exec); }, |
3200 |
< |
() -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); }, |
3201 |
< |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); }, |
3202 |
< |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); }, |
3203 |
< |
() -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); }, |
3204 |
< |
|
3205 |
< |
() -> { f.runAfterBoth(g, null); }, |
3206 |
< |
() -> { f.runAfterBothAsync(g, null); }, |
3207 |
< |
() -> { f.runAfterBothAsync(g, null, exec); }, |
3208 |
< |
() -> { f.runAfterBoth(nullFuture, () -> {}); }, |
3209 |
< |
() -> { f.runAfterBothAsync(nullFuture, () -> {}); }, |
3210 |
< |
() -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); }, |
3211 |
< |
() -> { f.runAfterBothAsync(g, () -> {}, null); }, |
3212 |
< |
|
3213 |
< |
() -> { f.applyToEither(g, null); }, |
3214 |
< |
() -> { f.applyToEitherAsync(g, null); }, |
3215 |
< |
() -> { f.applyToEitherAsync(g, null, exec); }, |
3216 |
< |
() -> { f.applyToEither(nullFuture, (x) -> x); }, |
3217 |
< |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x); }, |
3218 |
< |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); }, |
3219 |
< |
() -> { f.applyToEitherAsync(g, (x) -> x, null); }, |
3220 |
< |
|
3221 |
< |
() -> { f.acceptEither(g, null); }, |
3222 |
< |
() -> { f.acceptEitherAsync(g, null); }, |
3223 |
< |
() -> { f.acceptEitherAsync(g, null, exec); }, |
3224 |
< |
() -> { f.acceptEither(nullFuture, (x) -> {}); }, |
3225 |
< |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}); }, |
3226 |
< |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); }, |
3227 |
< |
() -> { f.acceptEitherAsync(g, (x) -> {}, null); }, |
3228 |
< |
|
3229 |
< |
() -> { f.runAfterEither(g, null); }, |
3230 |
< |
() -> { f.runAfterEitherAsync(g, null); }, |
3231 |
< |
() -> { f.runAfterEitherAsync(g, null, exec); }, |
3232 |
< |
() -> { f.runAfterEither(nullFuture, () -> {}); }, |
3233 |
< |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}); }, |
3234 |
< |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); }, |
3235 |
< |
() -> { f.runAfterEitherAsync(g, () -> {}, null); }, |
3236 |
< |
|
3237 |
< |
() -> { f.thenCompose(null); }, |
3238 |
< |
() -> { f.thenComposeAsync(null); }, |
3239 |
< |
() -> { f.thenComposeAsync(new CompletableFutureInc(), null); }, |
3240 |
< |
() -> { f.thenComposeAsync(null, exec); }, |
3241 |
< |
|
3242 |
< |
() -> { f.exceptionally(null); }, |
3243 |
< |
|
3244 |
< |
() -> { f.handle(null); }, |
3245 |
< |
|
3246 |
< |
() -> { CompletableFuture.allOf((CompletableFuture<?>)null); }, |
3247 |
< |
() -> { CompletableFuture.allOf((CompletableFuture<?>[])null); }, |
3248 |
< |
() -> { CompletableFuture.allOf(f, null); }, |
3249 |
< |
() -> { CompletableFuture.allOf(null, f); }, |
3250 |
< |
|
3251 |
< |
() -> { CompletableFuture.anyOf((CompletableFuture<?>)null); }, |
3252 |
< |
() -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); }, |
3253 |
< |
() -> { CompletableFuture.anyOf(f, null); }, |
3254 |
< |
() -> { CompletableFuture.anyOf(null, f); }, |
3255 |
< |
|
3256 |
< |
// TODO: Crashes javac with lambda-8-2013-03-31... |
2498 |
< |
//() -> { CompletableFuture<?> x = f.thenAccept(null); }, |
2499 |
< |
//() -> { CompletableFuture<Void> x = f.thenRun(null); }, |
2500 |
< |
//() -> { CompletableFuture<Integer> x = f.thenApply(() -> { ; }); }, |
3164 |
> |
() -> CompletableFuture.supplyAsync(null), |
3165 |
> |
() -> CompletableFuture.supplyAsync(null, exec), |
3166 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3167 |
> |
|
3168 |
> |
() -> CompletableFuture.runAsync(null), |
3169 |
> |
() -> CompletableFuture.runAsync(null, exec), |
3170 |
> |
() -> CompletableFuture.runAsync(() -> {}, null), |
3171 |
> |
|
3172 |
> |
() -> f.completeExceptionally(null), |
3173 |
> |
|
3174 |
> |
() -> f.thenApply(null), |
3175 |
> |
() -> f.thenApplyAsync(null), |
3176 |
> |
() -> f.thenApplyAsync((x) -> x, null), |
3177 |
> |
() -> f.thenApplyAsync(null, exec), |
3178 |
> |
|
3179 |
> |
() -> f.thenAccept(null), |
3180 |
> |
() -> f.thenAcceptAsync(null), |
3181 |
> |
() -> f.thenAcceptAsync((x) -> {} , null), |
3182 |
> |
() -> f.thenAcceptAsync(null, exec), |
3183 |
> |
|
3184 |
> |
() -> f.thenRun(null), |
3185 |
> |
() -> f.thenRunAsync(null), |
3186 |
> |
() -> f.thenRunAsync(() -> {} , null), |
3187 |
> |
() -> f.thenRunAsync(null, exec), |
3188 |
> |
|
3189 |
> |
() -> f.thenCombine(g, null), |
3190 |
> |
() -> f.thenCombineAsync(g, null), |
3191 |
> |
() -> f.thenCombineAsync(g, null, exec), |
3192 |
> |
() -> f.thenCombine(nullFuture, (x, y) -> x), |
3193 |
> |
() -> f.thenCombineAsync(nullFuture, (x, y) -> x), |
3194 |
> |
() -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec), |
3195 |
> |
() -> f.thenCombineAsync(g, (x, y) -> x, null), |
3196 |
> |
|
3197 |
> |
() -> f.thenAcceptBoth(g, null), |
3198 |
> |
() -> f.thenAcceptBothAsync(g, null), |
3199 |
> |
() -> f.thenAcceptBothAsync(g, null, exec), |
3200 |
> |
() -> f.thenAcceptBoth(nullFuture, (x, y) -> {}), |
3201 |
> |
() -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}), |
3202 |
> |
() -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec), |
3203 |
> |
() -> f.thenAcceptBothAsync(g, (x, y) -> {}, null), |
3204 |
> |
|
3205 |
> |
() -> f.runAfterBoth(g, null), |
3206 |
> |
() -> f.runAfterBothAsync(g, null), |
3207 |
> |
() -> f.runAfterBothAsync(g, null, exec), |
3208 |
> |
() -> f.runAfterBoth(nullFuture, () -> {}), |
3209 |
> |
() -> f.runAfterBothAsync(nullFuture, () -> {}), |
3210 |
> |
() -> f.runAfterBothAsync(nullFuture, () -> {}, exec), |
3211 |
> |
() -> f.runAfterBothAsync(g, () -> {}, null), |
3212 |
> |
|
3213 |
> |
() -> f.applyToEither(g, null), |
3214 |
> |
() -> f.applyToEitherAsync(g, null), |
3215 |
> |
() -> f.applyToEitherAsync(g, null, exec), |
3216 |
> |
() -> f.applyToEither(nullFuture, (x) -> x), |
3217 |
> |
() -> f.applyToEitherAsync(nullFuture, (x) -> x), |
3218 |
> |
() -> f.applyToEitherAsync(nullFuture, (x) -> x, exec), |
3219 |
> |
() -> f.applyToEitherAsync(g, (x) -> x, null), |
3220 |
> |
|
3221 |
> |
() -> f.acceptEither(g, null), |
3222 |
> |
() -> f.acceptEitherAsync(g, null), |
3223 |
> |
() -> f.acceptEitherAsync(g, null, exec), |
3224 |
> |
() -> f.acceptEither(nullFuture, (x) -> {}), |
3225 |
> |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}), |
3226 |
> |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec), |
3227 |
> |
() -> f.acceptEitherAsync(g, (x) -> {}, null), |
3228 |
> |
|
3229 |
> |
() -> f.runAfterEither(g, null), |
3230 |
> |
() -> f.runAfterEitherAsync(g, null), |
3231 |
> |
() -> f.runAfterEitherAsync(g, null, exec), |
3232 |
> |
() -> f.runAfterEither(nullFuture, () -> {}), |
3233 |
> |
() -> f.runAfterEitherAsync(nullFuture, () -> {}), |
3234 |
> |
() -> f.runAfterEitherAsync(nullFuture, () -> {}, exec), |
3235 |
> |
() -> f.runAfterEitherAsync(g, () -> {}, null), |
3236 |
> |
|
3237 |
> |
() -> f.thenCompose(null), |
3238 |
> |
() -> f.thenComposeAsync(null), |
3239 |
> |
() -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null), |
3240 |
> |
() -> f.thenComposeAsync(null, exec), |
3241 |
> |
|
3242 |
> |
() -> f.exceptionally(null), |
3243 |
> |
|
3244 |
> |
() -> f.handle(null), |
3245 |
> |
|
3246 |
> |
() -> CompletableFuture.allOf((CompletableFuture<?>)null), |
3247 |
> |
() -> CompletableFuture.allOf((CompletableFuture<?>[])null), |
3248 |
> |
() -> CompletableFuture.allOf(f, null), |
3249 |
> |
() -> CompletableFuture.allOf(null, f), |
3250 |
> |
|
3251 |
> |
() -> CompletableFuture.anyOf((CompletableFuture<?>)null), |
3252 |
> |
() -> CompletableFuture.anyOf((CompletableFuture<?>[])null), |
3253 |
> |
() -> CompletableFuture.anyOf(f, null), |
3254 |
> |
() -> CompletableFuture.anyOf(null, f), |
3255 |
> |
|
3256 |
> |
() -> f.obtrudeException(null), |
3257 |
|
}; |
3258 |
|
|
3259 |
|
assertThrows(NullPointerException.class, throwingActions); |
3260 |
+ |
assertEquals(0, exec.count.get()); |
3261 |
+ |
} |
3262 |
+ |
|
3263 |
+ |
/** |
3264 |
+ |
* toCompletableFuture returns this CompletableFuture. |
3265 |
+ |
*/ |
3266 |
+ |
public void testToCompletableFuture() { |
3267 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3268 |
+ |
assertSame(f, f.toCompletableFuture()); |
3269 |
|
} |
3270 |
|
|
3271 |
+ |
//--- tests of implementation details; not part of official tck --- |
3272 |
+ |
|
3273 |
+ |
Object resultOf(CompletableFuture<?> f) { |
3274 |
+ |
try { |
3275 |
+ |
java.lang.reflect.Field resultField |
3276 |
+ |
= CompletableFuture.class.getDeclaredField("result"); |
3277 |
+ |
resultField.setAccessible(true); |
3278 |
+ |
return resultField.get(f); |
3279 |
+ |
} catch (Throwable t) { throw new AssertionError(t); } |
3280 |
+ |
} |
3281 |
+ |
|
3282 |
+ |
public void testExceptionPropagationReusesResultObject() { |
3283 |
+ |
if (!testImplementationDetails) return; |
3284 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
3285 |
+ |
{ |
3286 |
+ |
final CFException ex = new CFException(); |
3287 |
+ |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3288 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3289 |
+ |
|
3290 |
+ |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3291 |
+ |
= new ArrayList<>(); |
3292 |
+ |
|
3293 |
+ |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3294 |
+ |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3295 |
+ |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3296 |
+ |
|
3297 |
+ |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3298 |
+ |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3299 |
+ |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3300 |
+ |
|
3301 |
+ |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3302 |
+ |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3303 |
+ |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3304 |
+ |
|
3305 |
+ |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3306 |
+ |
|
3307 |
+ |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3308 |
+ |
|
3309 |
+ |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3310 |
+ |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3311 |
+ |
|
3312 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3313 |
+ |
fun : funs) { |
3314 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3315 |
+ |
f.completeExceptionally(ex); |
3316 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3317 |
+ |
checkCompletedWithWrappedException(src, ex); |
3318 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3319 |
+ |
checkCompletedWithWrappedException(dep, ex); |
3320 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3321 |
+ |
} |
3322 |
+ |
|
3323 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3324 |
+ |
fun : funs) { |
3325 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3326 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3327 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3328 |
+ |
f.completeExceptionally(ex); |
3329 |
+ |
checkCompletedWithWrappedException(src, ex); |
3330 |
+ |
checkCompletedWithWrappedException(dep, ex); |
3331 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3332 |
+ |
} |
3333 |
+ |
|
3334 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3335 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3336 |
+ |
fun : funs) { |
3337 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3338 |
+ |
f.cancel(mayInterruptIfRunning); |
3339 |
+ |
checkCancelled(f); |
3340 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3341 |
+ |
checkCompletedWithWrappedCancellationException(src); |
3342 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3343 |
+ |
checkCompletedWithWrappedCancellationException(dep); |
3344 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3345 |
+ |
} |
3346 |
+ |
|
3347 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3348 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3349 |
+ |
fun : funs) { |
3350 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3351 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3352 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3353 |
+ |
f.cancel(mayInterruptIfRunning); |
3354 |
+ |
checkCancelled(f); |
3355 |
+ |
checkCompletedWithWrappedCancellationException(src); |
3356 |
+ |
checkCompletedWithWrappedCancellationException(dep); |
3357 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3358 |
+ |
} |
3359 |
+ |
}} |
3360 |
+ |
|
3361 |
|
} |