105 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
106 |
|
} |
107 |
|
|
108 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f, |
109 |
< |
CFException ex) { |
108 |
> |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
109 |
> |
Throwable ex) { |
110 |
|
try { |
111 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
112 |
|
shouldThrow(); |
131 |
|
} catch (ExecutionException success) { |
132 |
|
assertSame(ex, success.getCause()); |
133 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
+ |
|
135 |
|
assertTrue(f.isDone()); |
136 |
|
assertFalse(f.isCancelled()); |
137 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
138 |
|
} |
139 |
|
|
140 |
+ |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
141 |
+ |
Throwable ex) { |
142 |
+ |
checkCompletedExceptionallyWithRootCause(f, ex); |
143 |
+ |
try { |
144 |
+ |
CompletableFuture<Throwable> spy = f.handle |
145 |
+ |
((U u, Throwable t) -> t); |
146 |
+ |
assertTrue(spy.join() instanceof CompletionException); |
147 |
+ |
assertSame(ex, spy.join().getCause()); |
148 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
149 |
+ |
} |
150 |
+ |
|
151 |
+ |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
152 |
+ |
checkCompletedExceptionallyWithRootCause(f, ex); |
153 |
+ |
try { |
154 |
+ |
CompletableFuture<Throwable> spy = f.handle |
155 |
+ |
((U u, Throwable t) -> t); |
156 |
+ |
assertSame(ex, spy.join()); |
157 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
158 |
+ |
} |
159 |
+ |
|
160 |
|
void checkCancelled(CompletableFuture<?> f) { |
161 |
|
try { |
162 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
239 |
|
*/ |
240 |
|
public void testCompleteExceptionally() { |
241 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
242 |
+ |
CFException ex = new CFException(); |
243 |
|
checkIncomplete(f); |
244 |
< |
f.completeExceptionally(new CFException()); |
245 |
< |
checkCompletedWithWrappedCFException(f); |
244 |
> |
f.completeExceptionally(ex); |
245 |
> |
checkCompletedExceptionally(f, ex); |
246 |
|
} |
247 |
|
|
248 |
|
/** |
283 |
|
* obtrudeException forces completion with given exception |
284 |
|
*/ |
285 |
|
public void testObtrudeException() { |
286 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
287 |
< |
checkIncomplete(f); |
288 |
< |
f.complete(one); |
289 |
< |
checkCompletedNormally(f, one); |
290 |
< |
f.obtrudeException(new CFException()); |
269 |
< |
checkCompletedWithWrappedCFException(f); |
286 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
287 |
> |
{ |
288 |
> |
CFException ex; |
289 |
> |
CompletableFuture<Integer> f; |
290 |
> |
|
291 |
|
f = new CompletableFuture<>(); |
292 |
< |
f.obtrudeException(new CFException()); |
293 |
< |
checkCompletedWithWrappedCFException(f); |
292 |
> |
f.complete(v1); |
293 |
> |
for (int i = 0; i < 2; i++) { |
294 |
> |
f.obtrudeException(ex = new CFException()); |
295 |
> |
checkCompletedExceptionally(f, ex); |
296 |
> |
} |
297 |
> |
|
298 |
|
f = new CompletableFuture<>(); |
299 |
+ |
for (int i = 0; i < 2; i++) { |
300 |
+ |
f.obtrudeException(ex = new CFException()); |
301 |
+ |
checkCompletedExceptionally(f, ex); |
302 |
+ |
} |
303 |
+ |
|
304 |
+ |
f = new CompletableFuture<>(); |
305 |
+ |
f.completeExceptionally(ex = new CFException()); |
306 |
+ |
f.obtrudeValue(v1); |
307 |
+ |
checkCompletedNormally(f, v1); |
308 |
+ |
f.obtrudeException(ex = new CFException()); |
309 |
+ |
checkCompletedExceptionally(f, ex); |
310 |
|
f.completeExceptionally(new CFException()); |
311 |
< |
f.obtrudeValue(four); |
312 |
< |
checkCompletedNormally(f, four); |
313 |
< |
f.obtrudeException(new CFException()); |
314 |
< |
checkCompletedWithWrappedCFException(f); |
279 |
< |
} |
311 |
> |
checkCompletedExceptionally(f, ex); |
312 |
> |
f.complete(v1); |
313 |
> |
checkCompletedExceptionally(f, ex); |
314 |
> |
}} |
315 |
|
|
316 |
|
/** |
317 |
|
* getNumberOfDependents returns number of dependent tasks |
318 |
|
*/ |
319 |
|
public void testGetNumberOfDependents() { |
320 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
321 |
+ |
{ |
322 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
323 |
|
assertEquals(0, f.getNumberOfDependents()); |
324 |
< |
CompletableFuture g = f.thenRun(new Noop()); |
324 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
325 |
|
assertEquals(1, f.getNumberOfDependents()); |
326 |
|
assertEquals(0, g.getNumberOfDependents()); |
327 |
< |
CompletableFuture h = f.thenRun(new Noop()); |
327 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
328 |
|
assertEquals(2, f.getNumberOfDependents()); |
329 |
+ |
assertEquals(0, h.getNumberOfDependents()); |
330 |
|
f.complete(1); |
331 |
|
checkCompletedNormally(g, null); |
332 |
+ |
checkCompletedNormally(h, null); |
333 |
|
assertEquals(0, f.getNumberOfDependents()); |
334 |
|
assertEquals(0, g.getNumberOfDependents()); |
335 |
< |
} |
335 |
> |
assertEquals(0, h.getNumberOfDependents()); |
336 |
> |
}} |
337 |
|
|
338 |
|
/** |
339 |
|
* toString indicates current completion state |
350 |
|
f = new CompletableFuture<String>(); |
351 |
|
f.completeExceptionally(new IndexOutOfBoundsException()); |
352 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
353 |
+ |
|
354 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
355 |
+ |
f = new CompletableFuture<String>(); |
356 |
+ |
f.cancel(mayInterruptIfRunning); |
357 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
358 |
+ |
} |
359 |
|
} |
360 |
|
|
361 |
|
/** |
366 |
|
checkCompletedNormally(f, "test"); |
367 |
|
} |
368 |
|
|
369 |
< |
// Choose non-commutative actions for better coverage |
369 |
> |
abstract class CheckedAction { |
370 |
> |
int invocationCount = 0; |
371 |
> |
final ExecutionMode m; |
372 |
> |
CheckedAction(ExecutionMode m) { this.m = m; } |
373 |
> |
void invoked() { |
374 |
> |
m.checkExecutionMode(); |
375 |
> |
assertEquals(0, invocationCount++); |
376 |
> |
} |
377 |
> |
void assertNotInvoked() { assertEquals(0, invocationCount); } |
378 |
> |
void assertInvoked() { assertEquals(1, invocationCount); } |
379 |
> |
} |
380 |
|
|
381 |
< |
// A non-commutative function that handles and produces null values as well. |
382 |
< |
static Integer subtract(Integer x, Integer y) { |
383 |
< |
return (x == null && y == null) ? null : |
384 |
< |
((x == null) ? 42 : x.intValue()) |
385 |
< |
- ((y == null) ? 99 : y.intValue()); |
381 |
> |
abstract class CheckedIntegerAction extends CheckedAction { |
382 |
> |
Integer value; |
383 |
> |
CheckedIntegerAction(ExecutionMode m) { super(m); } |
384 |
> |
void assertValue(Integer expected) { |
385 |
> |
assertInvoked(); |
386 |
> |
assertEquals(expected, value); |
387 |
> |
} |
388 |
> |
} |
389 |
> |
|
390 |
> |
class IntegerSupplier extends CheckedAction |
391 |
> |
implements Supplier<Integer> |
392 |
> |
{ |
393 |
> |
final Integer value; |
394 |
> |
IntegerSupplier(ExecutionMode m, Integer value) { |
395 |
> |
super(m); |
396 |
> |
this.value = value; |
397 |
> |
} |
398 |
> |
public Integer get() { |
399 |
> |
invoked(); |
400 |
> |
return value; |
401 |
> |
} |
402 |
|
} |
403 |
|
|
404 |
|
// A function that handles and produces null values as well. |
406 |
|
return (x == null) ? null : x + 1; |
407 |
|
} |
408 |
|
|
409 |
< |
static final Supplier<Integer> supplyOne = |
410 |
< |
() -> Integer.valueOf(1); |
411 |
< |
static final Function<Integer, Integer> inc = |
412 |
< |
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
341 |
< |
static final BiFunction<Integer, Integer, Integer> subtract = |
342 |
< |
(Integer x, Integer y) -> subtract(x, y); |
343 |
< |
static final class IncAction implements Consumer<Integer> { |
344 |
< |
int invocationCount = 0; |
345 |
< |
Integer value; |
409 |
> |
class NoopConsumer extends CheckedIntegerAction |
410 |
> |
implements Consumer<Integer> |
411 |
> |
{ |
412 |
> |
NoopConsumer(ExecutionMode m) { super(m); } |
413 |
|
public void accept(Integer x) { |
414 |
< |
invocationCount++; |
415 |
< |
value = inc(x); |
414 |
> |
invoked(); |
415 |
> |
value = x; |
416 |
|
} |
417 |
|
} |
418 |
< |
static final class IncFunction implements Function<Integer,Integer> { |
419 |
< |
int invocationCount = 0; |
420 |
< |
Integer value; |
418 |
> |
|
419 |
> |
class IncFunction extends CheckedIntegerAction |
420 |
> |
implements Function<Integer,Integer> |
421 |
> |
{ |
422 |
> |
IncFunction(ExecutionMode m) { super(m); } |
423 |
|
public Integer apply(Integer x) { |
424 |
< |
invocationCount++; |
424 |
> |
invoked(); |
425 |
|
return value = inc(x); |
426 |
|
} |
427 |
|
} |
428 |
< |
static final class SubtractAction implements BiConsumer<Integer, Integer> { |
429 |
< |
int invocationCount = 0; |
430 |
< |
Integer value; |
431 |
< |
// Check this action was invoked exactly once when result is computed. |
428 |
> |
|
429 |
> |
// Choose non-commutative actions for better coverage |
430 |
> |
// A non-commutative function that handles and produces null values as well. |
431 |
> |
static Integer subtract(Integer x, Integer y) { |
432 |
> |
return (x == null && y == null) ? null : |
433 |
> |
((x == null) ? 42 : x.intValue()) |
434 |
> |
- ((y == null) ? 99 : y.intValue()); |
435 |
> |
} |
436 |
> |
|
437 |
> |
class SubtractAction extends CheckedIntegerAction |
438 |
> |
implements BiConsumer<Integer, Integer> |
439 |
> |
{ |
440 |
> |
SubtractAction(ExecutionMode m) { super(m); } |
441 |
|
public void accept(Integer x, Integer y) { |
442 |
< |
invocationCount++; |
442 |
> |
invoked(); |
443 |
|
value = subtract(x, y); |
444 |
|
} |
445 |
|
} |
446 |
< |
static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> { |
447 |
< |
int invocationCount = 0; |
448 |
< |
Integer value; |
449 |
< |
// Check this action was invoked exactly once when result is computed. |
446 |
> |
|
447 |
> |
class SubtractFunction extends CheckedIntegerAction |
448 |
> |
implements BiFunction<Integer, Integer, Integer> |
449 |
> |
{ |
450 |
> |
SubtractFunction(ExecutionMode m) { super(m); } |
451 |
|
public Integer apply(Integer x, Integer y) { |
452 |
< |
invocationCount++; |
452 |
> |
invoked(); |
453 |
|
return value = subtract(x, y); |
454 |
|
} |
455 |
|
} |
456 |
< |
static final class Noop implements Runnable { |
457 |
< |
int invocationCount = 0; |
456 |
> |
|
457 |
> |
class Noop extends CheckedAction implements Runnable { |
458 |
> |
Noop(ExecutionMode m) { super(m); } |
459 |
|
public void run() { |
460 |
< |
invocationCount++; |
460 |
> |
invoked(); |
461 |
|
} |
462 |
|
} |
463 |
|
|
464 |
< |
static final class FailingSupplier implements Supplier<Integer> { |
465 |
< |
int invocationCount = 0; |
464 |
> |
class FailingSupplier extends CheckedAction |
465 |
> |
implements Supplier<Integer> |
466 |
> |
{ |
467 |
> |
FailingSupplier(ExecutionMode m) { super(m); } |
468 |
|
public Integer get() { |
469 |
< |
invocationCount++; |
469 |
> |
invoked(); |
470 |
|
throw new CFException(); |
471 |
|
} |
472 |
|
} |
473 |
< |
static final class FailingConsumer implements Consumer<Integer> { |
474 |
< |
int invocationCount = 0; |
473 |
> |
|
474 |
> |
class FailingConsumer extends CheckedIntegerAction |
475 |
> |
implements Consumer<Integer> |
476 |
> |
{ |
477 |
> |
FailingConsumer(ExecutionMode m) { super(m); } |
478 |
|
public void accept(Integer x) { |
479 |
< |
invocationCount++; |
479 |
> |
invoked(); |
480 |
> |
value = x; |
481 |
|
throw new CFException(); |
482 |
|
} |
483 |
|
} |
484 |
< |
static final class FailingBiConsumer implements BiConsumer<Integer, Integer> { |
485 |
< |
int invocationCount = 0; |
484 |
> |
|
485 |
> |
class FailingBiConsumer extends CheckedIntegerAction |
486 |
> |
implements BiConsumer<Integer, Integer> |
487 |
> |
{ |
488 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); } |
489 |
|
public void accept(Integer x, Integer y) { |
490 |
< |
invocationCount++; |
490 |
> |
invoked(); |
491 |
> |
value = subtract(x, y); |
492 |
|
throw new CFException(); |
493 |
|
} |
494 |
|
} |
495 |
< |
static final class FailingFunction implements Function<Integer, Integer> { |
496 |
< |
int invocationCount = 0; |
495 |
> |
|
496 |
> |
class FailingFunction extends CheckedIntegerAction |
497 |
> |
implements Function<Integer, Integer> |
498 |
> |
{ |
499 |
> |
FailingFunction(ExecutionMode m) { super(m); } |
500 |
|
public Integer apply(Integer x) { |
501 |
< |
invocationCount++; |
501 |
> |
invoked(); |
502 |
> |
value = x; |
503 |
|
throw new CFException(); |
504 |
|
} |
505 |
|
} |
506 |
< |
static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> { |
507 |
< |
int invocationCount = 0; |
506 |
> |
|
507 |
> |
class FailingBiFunction extends CheckedIntegerAction |
508 |
> |
implements BiFunction<Integer, Integer, Integer> |
509 |
> |
{ |
510 |
> |
FailingBiFunction(ExecutionMode m) { super(m); } |
511 |
|
public Integer apply(Integer x, Integer y) { |
512 |
< |
invocationCount++; |
512 |
> |
invoked(); |
513 |
> |
value = subtract(x, y); |
514 |
|
throw new CFException(); |
515 |
|
} |
516 |
|
} |
517 |
< |
static final class FailingRunnable implements Runnable { |
518 |
< |
int invocationCount = 0; |
517 |
> |
|
518 |
> |
class FailingRunnable extends CheckedAction implements Runnable { |
519 |
> |
FailingRunnable(ExecutionMode m) { super(m); } |
520 |
|
public void run() { |
521 |
< |
invocationCount++; |
521 |
> |
invoked(); |
522 |
|
throw new CFException(); |
523 |
|
} |
524 |
|
} |
525 |
|
|
526 |
< |
static final class CompletableFutureInc |
527 |
< |
implements Function<Integer, CompletableFuture<Integer>> { |
528 |
< |
int invocationCount = 0; |
526 |
> |
|
527 |
> |
class CompletableFutureInc extends CheckedIntegerAction |
528 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
529 |
> |
{ |
530 |
> |
CompletableFutureInc(ExecutionMode m) { super(m); } |
531 |
|
public CompletableFuture<Integer> apply(Integer x) { |
532 |
< |
invocationCount++; |
532 |
> |
invoked(); |
533 |
> |
value = x; |
534 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
535 |
|
f.complete(inc(x)); |
536 |
|
return f; |
537 |
|
} |
538 |
|
} |
539 |
|
|
540 |
< |
static final class FailingCompletableFutureFunction |
541 |
< |
implements Function<Integer, CompletableFuture<Integer>> { |
542 |
< |
int invocationCount = 0; |
540 |
> |
class FailingCompletableFutureFunction extends CheckedIntegerAction |
541 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
542 |
> |
{ |
543 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); } |
544 |
|
public CompletableFuture<Integer> apply(Integer x) { |
545 |
< |
invocationCount++; |
545 |
> |
invoked(); |
546 |
> |
value = x; |
547 |
|
throw new CFException(); |
548 |
|
} |
549 |
|
} |
550 |
|
|
551 |
|
// Used for explicit executor tests |
552 |
|
static final class ThreadExecutor implements Executor { |
553 |
< |
AtomicInteger count = new AtomicInteger(0); |
553 |
> |
final AtomicInteger count = new AtomicInteger(0); |
554 |
> |
static final ThreadGroup tg = new ThreadGroup("ThreadExecutor"); |
555 |
> |
static boolean startedCurrentThread() { |
556 |
> |
return Thread.currentThread().getThreadGroup() == tg; |
557 |
> |
} |
558 |
|
|
559 |
|
public void execute(Runnable r) { |
560 |
|
count.getAndIncrement(); |
561 |
< |
new Thread(r).start(); |
561 |
> |
new Thread(tg, r).start(); |
562 |
|
} |
563 |
|
} |
564 |
|
|
565 |
|
/** |
566 |
|
* Permits the testing of parallel code for the 3 different |
567 |
< |
* execution modes without repeating all the testing code. |
567 |
> |
* execution modes without copy/pasting all the test methods. |
568 |
|
*/ |
569 |
|
enum ExecutionMode { |
570 |
|
DEFAULT { |
571 |
|
public void checkExecutionMode() { |
572 |
+ |
assertFalse(ThreadExecutor.startedCurrentThread()); |
573 |
|
assertNull(ForkJoinTask.getPool()); |
574 |
|
} |
575 |
+ |
public CompletableFuture<Void> runAsync(Runnable a) { |
576 |
+ |
throw new UnsupportedOperationException(); |
577 |
+ |
} |
578 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
579 |
+ |
throw new UnsupportedOperationException(); |
580 |
+ |
} |
581 |
|
public <T> CompletableFuture<Void> thenRun |
582 |
|
(CompletableFuture<T> f, Runnable a) { |
583 |
|
return f.thenRun(a); |
646 |
|
assertSame(ForkJoinPool.commonPool(), |
647 |
|
ForkJoinTask.getPool()); |
648 |
|
} |
649 |
+ |
public CompletableFuture<Void> runAsync(Runnable a) { |
650 |
+ |
return CompletableFuture.runAsync(a); |
651 |
+ |
} |
652 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
653 |
+ |
return CompletableFuture.supplyAsync(a); |
654 |
+ |
} |
655 |
|
public <T> CompletableFuture<Void> thenRun |
656 |
|
(CompletableFuture<T> f, Runnable a) { |
657 |
|
return f.thenRunAsync(a); |
717 |
|
|
718 |
|
EXECUTOR { |
719 |
|
public void checkExecutionMode() { |
720 |
< |
//TODO |
720 |
> |
assertTrue(ThreadExecutor.startedCurrentThread()); |
721 |
> |
} |
722 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
723 |
> |
return CompletableFuture.runAsync(a, new ThreadExecutor()); |
724 |
> |
} |
725 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
726 |
> |
return CompletableFuture.supplyAsync(a, new ThreadExecutor()); |
727 |
|
} |
728 |
|
public <T> CompletableFuture<Void> thenRun |
729 |
|
(CompletableFuture<T> f, Runnable a) { |
789 |
|
}; |
790 |
|
|
791 |
|
public abstract void checkExecutionMode(); |
792 |
+ |
public abstract CompletableFuture<Void> runAsync(Runnable a); |
793 |
+ |
public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); |
794 |
|
public abstract <T> CompletableFuture<Void> thenRun |
795 |
|
(CompletableFuture<T> f, Runnable a); |
796 |
|
public abstract <T> CompletableFuture<Void> thenAccept |
854 |
|
assertEquals(0, a.get()); |
855 |
|
}} |
856 |
|
|
728 |
– |
|
857 |
|
/** |
858 |
|
* exceptionally action completes with function value on source |
859 |
|
* exception |
868 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
869 |
|
final CompletableFuture<Integer> g = f.exceptionally |
870 |
|
((Throwable t) -> { |
871 |
+ |
ExecutionMode.DEFAULT.checkExecutionMode(); |
872 |
|
threadAssertSame(t, ex); |
873 |
|
a.getAndIncrement(); |
874 |
|
return v1; |
890 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
891 |
|
final CompletableFuture<Integer> g = f.exceptionally |
892 |
|
((Throwable t) -> { |
893 |
+ |
ExecutionMode.DEFAULT.checkExecutionMode(); |
894 |
|
threadAssertSame(t, ex1); |
895 |
|
a.getAndIncrement(); |
896 |
|
throw ex2; |
897 |
|
}); |
898 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
899 |
|
|
900 |
< |
checkCompletedWithWrappedCFException(g, ex2); |
900 |
> |
checkCompletedWithWrappedException(g, ex2); |
901 |
> |
assertEquals(1, a.get()); |
902 |
> |
}} |
903 |
> |
|
904 |
> |
/** |
905 |
> |
* whenComplete action executes on normal completion, propagating |
906 |
> |
* source result. |
907 |
> |
*/ |
908 |
> |
public void testWhenComplete_normalCompletion1() { |
909 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
910 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
911 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
912 |
> |
{ |
913 |
> |
final AtomicInteger a = new AtomicInteger(0); |
914 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
915 |
> |
if (!createIncomplete) f.complete(v1); |
916 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
917 |
> |
(f, |
918 |
> |
(Integer x, Throwable t) -> { |
919 |
> |
m.checkExecutionMode(); |
920 |
> |
threadAssertSame(x, v1); |
921 |
> |
threadAssertNull(t); |
922 |
> |
a.getAndIncrement(); |
923 |
> |
}); |
924 |
> |
if (createIncomplete) f.complete(v1); |
925 |
> |
|
926 |
> |
checkCompletedNormally(g, v1); |
927 |
> |
checkCompletedNormally(f, v1); |
928 |
> |
assertEquals(1, a.get()); |
929 |
> |
}} |
930 |
> |
|
931 |
> |
/** |
932 |
> |
* whenComplete action executes on exceptional completion, propagating |
933 |
> |
* source result. |
934 |
> |
*/ |
935 |
> |
public void testWhenComplete_exceptionalCompletion() { |
936 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
937 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
938 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
939 |
> |
{ |
940 |
> |
final AtomicInteger a = new AtomicInteger(0); |
941 |
> |
final CFException ex = new CFException(); |
942 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
943 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
944 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
945 |
> |
(f, |
946 |
> |
(Integer x, Throwable t) -> { |
947 |
> |
m.checkExecutionMode(); |
948 |
> |
threadAssertNull(x); |
949 |
> |
threadAssertSame(t, ex); |
950 |
> |
a.getAndIncrement(); |
951 |
> |
}); |
952 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
953 |
> |
|
954 |
> |
checkCompletedWithWrappedException(g, ex); |
955 |
> |
checkCompletedExceptionally(f, ex); |
956 |
> |
assertEquals(1, a.get()); |
957 |
> |
}} |
958 |
> |
|
959 |
> |
/** |
960 |
> |
* whenComplete action executes on cancelled source, propagating |
961 |
> |
* CancellationException. |
962 |
> |
*/ |
963 |
> |
public void testWhenComplete_sourceCancelled() { |
964 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
965 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
966 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
967 |
> |
{ |
968 |
> |
final AtomicInteger a = new AtomicInteger(0); |
969 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
970 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
971 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
972 |
> |
(f, |
973 |
> |
(Integer x, Throwable t) -> { |
974 |
> |
m.checkExecutionMode(); |
975 |
> |
threadAssertNull(x); |
976 |
> |
threadAssertTrue(t instanceof CancellationException); |
977 |
> |
a.getAndIncrement(); |
978 |
> |
}); |
979 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
980 |
> |
|
981 |
> |
checkCompletedWithWrappedCancellationException(g); |
982 |
> |
checkCancelled(f); |
983 |
> |
assertEquals(1, a.get()); |
984 |
> |
}} |
985 |
> |
|
986 |
> |
/** |
987 |
> |
* If a whenComplete action throws an exception when triggered by |
988 |
> |
* a normal completion, it completes exceptionally |
989 |
> |
*/ |
990 |
> |
public void testWhenComplete_actionFailed() { |
991 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
992 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
993 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
994 |
> |
{ |
995 |
> |
final AtomicInteger a = new AtomicInteger(0); |
996 |
> |
final CFException ex = new CFException(); |
997 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
998 |
> |
if (!createIncomplete) f.complete(v1); |
999 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1000 |
> |
(f, |
1001 |
> |
(Integer x, Throwable t) -> { |
1002 |
> |
m.checkExecutionMode(); |
1003 |
> |
threadAssertSame(x, v1); |
1004 |
> |
threadAssertNull(t); |
1005 |
> |
a.getAndIncrement(); |
1006 |
> |
throw ex; |
1007 |
> |
}); |
1008 |
> |
if (createIncomplete) f.complete(v1); |
1009 |
> |
|
1010 |
> |
checkCompletedWithWrappedException(g, ex); |
1011 |
> |
checkCompletedNormally(f, v1); |
1012 |
> |
assertEquals(1, a.get()); |
1013 |
> |
}} |
1014 |
> |
|
1015 |
> |
/** |
1016 |
> |
* If a whenComplete action throws an exception when triggered by |
1017 |
> |
* a source completion that also throws an exception, the source |
1018 |
> |
* exception takes precedence. |
1019 |
> |
*/ |
1020 |
> |
public void testWhenComplete_actionFailedSourceFailed() { |
1021 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1022 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1023 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1024 |
> |
{ |
1025 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1026 |
> |
final CFException ex1 = new CFException(); |
1027 |
> |
final CFException ex2 = new CFException(); |
1028 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1029 |
> |
|
1030 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1031 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1032 |
> |
(f, |
1033 |
> |
(Integer x, Throwable t) -> { |
1034 |
> |
m.checkExecutionMode(); |
1035 |
> |
threadAssertSame(t, ex1); |
1036 |
> |
threadAssertNull(x); |
1037 |
> |
a.getAndIncrement(); |
1038 |
> |
throw ex2; |
1039 |
> |
}); |
1040 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1041 |
> |
|
1042 |
> |
checkCompletedWithWrappedException(g, ex1); |
1043 |
> |
checkCompletedExceptionally(f, ex1); |
1044 |
|
assertEquals(1, a.get()); |
1045 |
|
}} |
1046 |
|
|
1059 |
|
final CompletableFuture<Integer> g = m.handle |
1060 |
|
(f, |
1061 |
|
(Integer x, Throwable t) -> { |
1062 |
+ |
m.checkExecutionMode(); |
1063 |
|
threadAssertSame(x, v1); |
1064 |
|
threadAssertNull(t); |
1065 |
|
a.getAndIncrement(); |
1088 |
|
final CompletableFuture<Integer> g = m.handle |
1089 |
|
(f, |
1090 |
|
(Integer x, Throwable t) -> { |
1091 |
+ |
m.checkExecutionMode(); |
1092 |
|
threadAssertNull(x); |
1093 |
|
threadAssertSame(t, ex); |
1094 |
|
a.getAndIncrement(); |
1097 |
|
if (createIncomplete) f.completeExceptionally(ex); |
1098 |
|
|
1099 |
|
checkCompletedNormally(g, v1); |
1100 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1100 |
> |
checkCompletedExceptionally(f, ex); |
1101 |
|
assertEquals(1, a.get()); |
1102 |
|
}} |
1103 |
|
|
1117 |
|
final CompletableFuture<Integer> g = m.handle |
1118 |
|
(f, |
1119 |
|
(Integer x, Throwable t) -> { |
1120 |
+ |
m.checkExecutionMode(); |
1121 |
|
threadAssertNull(x); |
1122 |
|
threadAssertTrue(t instanceof CancellationException); |
1123 |
|
a.getAndIncrement(); |
1145 |
|
final CompletableFuture<Integer> g = m.handle |
1146 |
|
(f, |
1147 |
|
(Integer x, Throwable t) -> { |
1148 |
+ |
m.checkExecutionMode(); |
1149 |
|
threadAssertNull(x); |
1150 |
|
threadAssertSame(ex1, t); |
1151 |
|
a.getAndIncrement(); |
1153 |
|
}); |
1154 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
1155 |
|
|
1156 |
< |
checkCompletedWithWrappedCFException(g, ex2); |
1157 |
< |
checkCompletedWithWrappedCFException(f, ex1); |
1156 |
> |
checkCompletedWithWrappedException(g, ex2); |
1157 |
> |
checkCompletedExceptionally(f, ex1); |
1158 |
|
assertEquals(1, a.get()); |
1159 |
|
}} |
1160 |
|
|
1170 |
|
final CompletableFuture<Integer> g = m.handle |
1171 |
|
(f, |
1172 |
|
(Integer x, Throwable t) -> { |
1173 |
+ |
m.checkExecutionMode(); |
1174 |
|
threadAssertSame(x, v1); |
1175 |
|
threadAssertNull(t); |
1176 |
|
a.getAndIncrement(); |
1178 |
|
}); |
1179 |
|
if (createIncomplete) f.complete(v1); |
1180 |
|
|
1181 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1181 |
> |
checkCompletedWithWrappedException(g, ex); |
1182 |
|
checkCompletedNormally(f, v1); |
1183 |
|
assertEquals(1, a.get()); |
1184 |
|
}} |
1186 |
|
/** |
1187 |
|
* runAsync completes after running Runnable |
1188 |
|
*/ |
1189 |
< |
public void testRunAsync() { |
1190 |
< |
Noop r = new Noop(); |
1191 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
1192 |
< |
assertNull(f.join()); |
1193 |
< |
assertEquals(1, r.invocationCount); |
1194 |
< |
checkCompletedNormally(f, null); |
1195 |
< |
} |
1196 |
< |
|
1197 |
< |
/** |
920 |
< |
* runAsync with executor completes after running Runnable |
921 |
< |
*/ |
922 |
< |
public void testRunAsync2() { |
923 |
< |
Noop r = new Noop(); |
924 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
925 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
1189 |
> |
public void testRunAsync_normalCompletion() { |
1190 |
> |
ExecutionMode[] executionModes = { |
1191 |
> |
ExecutionMode.ASYNC, |
1192 |
> |
ExecutionMode.EXECUTOR, |
1193 |
> |
}; |
1194 |
> |
for (ExecutionMode m : executionModes) |
1195 |
> |
{ |
1196 |
> |
final Noop r = new Noop(m); |
1197 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1198 |
|
assertNull(f.join()); |
927 |
– |
assertEquals(1, r.invocationCount); |
1199 |
|
checkCompletedNormally(f, null); |
1200 |
< |
assertEquals(1, exec.count.get()); |
1201 |
< |
} |
1200 |
> |
r.assertInvoked(); |
1201 |
> |
}} |
1202 |
|
|
1203 |
|
/** |
1204 |
|
* failing runAsync completes exceptionally after running Runnable |
1205 |
|
*/ |
1206 |
< |
public void testRunAsync3() { |
1207 |
< |
FailingRunnable r = new FailingRunnable(); |
1208 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
1206 |
> |
public void testRunAsync_exceptionalCompletion() { |
1207 |
> |
ExecutionMode[] executionModes = { |
1208 |
> |
ExecutionMode.ASYNC, |
1209 |
> |
ExecutionMode.EXECUTOR, |
1210 |
> |
}; |
1211 |
> |
for (ExecutionMode m : executionModes) |
1212 |
> |
{ |
1213 |
> |
final FailingRunnable r = new FailingRunnable(m); |
1214 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1215 |
|
checkCompletedWithWrappedCFException(f); |
1216 |
< |
assertEquals(1, r.invocationCount); |
1217 |
< |
} |
1216 |
> |
r.assertInvoked(); |
1217 |
> |
}} |
1218 |
|
|
1219 |
|
/** |
1220 |
|
* supplyAsync completes with result of supplier |
1221 |
|
*/ |
1222 |
< |
public void testSupplyAsync() { |
1223 |
< |
CompletableFuture<Integer> f; |
1224 |
< |
f = CompletableFuture.supplyAsync(supplyOne); |
1225 |
< |
assertEquals(f.join(), one); |
1226 |
< |
checkCompletedNormally(f, one); |
1227 |
< |
} |
1228 |
< |
|
1229 |
< |
/** |
1230 |
< |
* supplyAsync with executor completes with result of supplier |
1231 |
< |
*/ |
1232 |
< |
public void testSupplyAsync2() { |
1233 |
< |
CompletableFuture<Integer> f; |
1234 |
< |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
1235 |
< |
assertEquals(f.join(), one); |
959 |
< |
checkCompletedNormally(f, one); |
960 |
< |
} |
1222 |
> |
public void testSupplyAsync_normalCompletion() { |
1223 |
> |
ExecutionMode[] executionModes = { |
1224 |
> |
ExecutionMode.ASYNC, |
1225 |
> |
ExecutionMode.EXECUTOR, |
1226 |
> |
}; |
1227 |
> |
for (ExecutionMode m : executionModes) |
1228 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1229 |
> |
{ |
1230 |
> |
final IntegerSupplier r = new IntegerSupplier(m, v1); |
1231 |
> |
final CompletableFuture<Integer> f = m.supplyAsync(r); |
1232 |
> |
assertSame(v1, f.join()); |
1233 |
> |
checkCompletedNormally(f, v1); |
1234 |
> |
r.assertInvoked(); |
1235 |
> |
}} |
1236 |
|
|
1237 |
|
/** |
1238 |
|
* Failing supplyAsync completes exceptionally |
1239 |
|
*/ |
1240 |
< |
public void testSupplyAsync3() { |
1241 |
< |
FailingSupplier r = new FailingSupplier(); |
1242 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r); |
1240 |
> |
public void testSupplyAsync_exceptionalCompletion() { |
1241 |
> |
ExecutionMode[] executionModes = { |
1242 |
> |
ExecutionMode.ASYNC, |
1243 |
> |
ExecutionMode.EXECUTOR, |
1244 |
> |
}; |
1245 |
> |
for (ExecutionMode m : executionModes) |
1246 |
> |
{ |
1247 |
> |
FailingSupplier r = new FailingSupplier(m); |
1248 |
> |
CompletableFuture<Integer> f = m.supplyAsync(r); |
1249 |
|
checkCompletedWithWrappedCFException(f); |
1250 |
< |
assertEquals(1, r.invocationCount); |
1251 |
< |
} |
1250 |
> |
r.assertInvoked(); |
1251 |
> |
}} |
1252 |
|
|
1253 |
|
// seq completion methods |
1254 |
|
|
1261 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1262 |
|
{ |
1263 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1264 |
< |
final Noop r = new Noop(); |
1264 |
> |
final Noop r = new Noop(m); |
1265 |
|
if (!createIncomplete) f.complete(v1); |
1266 |
|
final CompletableFuture<Void> g = m.thenRun(f, r); |
1267 |
|
if (createIncomplete) { |
1271 |
|
|
1272 |
|
checkCompletedNormally(g, null); |
1273 |
|
checkCompletedNormally(f, v1); |
1274 |
< |
assertEquals(1, r.invocationCount); |
1274 |
> |
r.assertInvoked(); |
1275 |
|
}} |
1276 |
|
|
1277 |
|
/** |
1284 |
|
{ |
1285 |
|
final CFException ex = new CFException(); |
1286 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1287 |
< |
final Noop r = new Noop(); |
1287 |
> |
final Noop r = new Noop(m); |
1288 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1289 |
|
final CompletableFuture<Void> g = m.thenRun(f, r); |
1290 |
|
if (createIncomplete) { |
1292 |
|
f.completeExceptionally(ex); |
1293 |
|
} |
1294 |
|
|
1295 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1296 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1297 |
< |
assertEquals(0, r.invocationCount); |
1295 |
> |
checkCompletedWithWrappedException(g, ex); |
1296 |
> |
checkCompletedExceptionally(f, ex); |
1297 |
> |
r.assertNotInvoked(); |
1298 |
|
}} |
1299 |
|
|
1300 |
|
/** |
1306 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1307 |
|
{ |
1308 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1309 |
< |
final Noop r = new Noop(); |
1309 |
> |
final Noop r = new Noop(m); |
1310 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1311 |
< |
final CompletableFuture<Void> g = f.thenRun(r); |
1311 |
> |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1312 |
|
if (createIncomplete) { |
1313 |
|
checkIncomplete(g); |
1314 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
1316 |
|
|
1317 |
|
checkCompletedWithWrappedCancellationException(g); |
1318 |
|
checkCancelled(f); |
1319 |
< |
assertEquals(0, r.invocationCount); |
1319 |
> |
r.assertNotInvoked(); |
1320 |
|
}} |
1321 |
|
|
1322 |
|
/** |
1328 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1329 |
|
{ |
1330 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1331 |
< |
final FailingRunnable r = new FailingRunnable(); |
1331 |
> |
final FailingRunnable r = new FailingRunnable(m); |
1332 |
|
if (!createIncomplete) f.complete(v1); |
1333 |
< |
final CompletableFuture<Void> g = f.thenRun(r); |
1333 |
> |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1334 |
|
if (createIncomplete) { |
1335 |
|
checkIncomplete(g); |
1336 |
|
f.complete(v1); |
1349 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1350 |
|
{ |
1351 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1352 |
< |
final IncFunction r = new IncFunction(); |
1352 |
> |
final IncFunction r = new IncFunction(m); |
1353 |
|
if (!createIncomplete) f.complete(v1); |
1354 |
|
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1355 |
|
if (createIncomplete) { |
1359 |
|
|
1360 |
|
checkCompletedNormally(g, inc(v1)); |
1361 |
|
checkCompletedNormally(f, v1); |
1362 |
< |
assertEquals(1, r.invocationCount); |
1362 |
> |
r.assertValue(inc(v1)); |
1363 |
|
}} |
1364 |
|
|
1365 |
|
/** |
1372 |
|
{ |
1373 |
|
final CFException ex = new CFException(); |
1374 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1375 |
< |
final IncFunction r = new IncFunction(); |
1375 |
> |
final IncFunction r = new IncFunction(m); |
1376 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1377 |
|
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1378 |
|
if (createIncomplete) { |
1380 |
|
f.completeExceptionally(ex); |
1381 |
|
} |
1382 |
|
|
1383 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1384 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1385 |
< |
assertEquals(0, r.invocationCount); |
1383 |
> |
checkCompletedWithWrappedException(g, ex); |
1384 |
> |
checkCompletedExceptionally(f, ex); |
1385 |
> |
r.assertNotInvoked(); |
1386 |
|
}} |
1387 |
|
|
1388 |
|
/** |
1394 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1395 |
|
{ |
1396 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1397 |
< |
final IncFunction r = new IncFunction(); |
1397 |
> |
final IncFunction r = new IncFunction(m); |
1398 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1399 |
< |
final CompletableFuture<Integer> g = f.thenApply(r); |
1399 |
> |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1400 |
|
if (createIncomplete) { |
1401 |
|
checkIncomplete(g); |
1402 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
1404 |
|
|
1405 |
|
checkCompletedWithWrappedCancellationException(g); |
1406 |
|
checkCancelled(f); |
1407 |
< |
assertEquals(0, r.invocationCount); |
1407 |
> |
r.assertNotInvoked(); |
1408 |
|
}} |
1409 |
|
|
1410 |
|
/** |
1416 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1417 |
|
{ |
1418 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1419 |
< |
final FailingFunction r = new FailingFunction(); |
1419 |
> |
final FailingFunction r = new FailingFunction(m); |
1420 |
|
if (!createIncomplete) f.complete(v1); |
1421 |
< |
final CompletableFuture<Integer> g = f.thenApply(r); |
1421 |
> |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1422 |
|
if (createIncomplete) { |
1423 |
|
checkIncomplete(g); |
1424 |
|
f.complete(v1); |
1437 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1438 |
|
{ |
1439 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1440 |
< |
final IncAction r = new IncAction(); |
1440 |
> |
final NoopConsumer r = new NoopConsumer(m); |
1441 |
|
if (!createIncomplete) f.complete(v1); |
1442 |
|
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1443 |
|
if (createIncomplete) { |
1446 |
|
} |
1447 |
|
|
1448 |
|
checkCompletedNormally(g, null); |
1449 |
+ |
r.assertValue(v1); |
1450 |
|
checkCompletedNormally(f, v1); |
1169 |
– |
assertEquals(1, r.invocationCount); |
1170 |
– |
assertEquals(inc(v1), r.value); |
1451 |
|
}} |
1452 |
|
|
1453 |
|
/** |
1460 |
|
{ |
1461 |
|
final CFException ex = new CFException(); |
1462 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1463 |
< |
final IncAction r = new IncAction(); |
1463 |
> |
final NoopConsumer r = new NoopConsumer(m); |
1464 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1465 |
|
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1466 |
|
if (createIncomplete) { |
1468 |
|
f.completeExceptionally(ex); |
1469 |
|
} |
1470 |
|
|
1471 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1472 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1473 |
< |
assertEquals(0, r.invocationCount); |
1471 |
> |
checkCompletedWithWrappedException(g, ex); |
1472 |
> |
checkCompletedExceptionally(f, ex); |
1473 |
> |
r.assertNotInvoked(); |
1474 |
|
}} |
1475 |
|
|
1476 |
|
/** |
1477 |
< |
* thenAccept result completes exceptionally if action does |
1477 |
> |
* thenAccept result completes exceptionally if source cancelled |
1478 |
|
*/ |
1479 |
< |
public void testThenAccept_actionFailed() { |
1479 |
> |
public void testThenAccept_sourceCancelled() { |
1480 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1481 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1482 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1482 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1483 |
|
{ |
1484 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1485 |
< |
final FailingConsumer r = new FailingConsumer(); |
1486 |
< |
if (!createIncomplete) f.complete(v1); |
1487 |
< |
final CompletableFuture<Void> g = f.thenAccept(r); |
1485 |
> |
final NoopConsumer r = new NoopConsumer(m); |
1486 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1487 |
> |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1488 |
|
if (createIncomplete) { |
1489 |
|
checkIncomplete(g); |
1490 |
< |
f.complete(v1); |
1490 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1491 |
|
} |
1492 |
|
|
1493 |
< |
checkCompletedWithWrappedCFException(g); |
1494 |
< |
checkCompletedNormally(f, v1); |
1493 |
> |
checkCompletedWithWrappedCancellationException(g); |
1494 |
> |
checkCancelled(f); |
1495 |
> |
r.assertNotInvoked(); |
1496 |
|
}} |
1497 |
|
|
1498 |
|
/** |
1499 |
< |
* thenAccept result completes exceptionally if source cancelled |
1499 |
> |
* thenAccept result completes exceptionally if action does |
1500 |
|
*/ |
1501 |
< |
public void testThenAccept_sourceCancelled() { |
1501 |
> |
public void testThenAccept_actionFailed() { |
1502 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1503 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1504 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1504 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1505 |
|
{ |
1506 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1507 |
< |
final IncAction r = new IncAction(); |
1508 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1509 |
< |
final CompletableFuture<Void> g = f.thenAccept(r); |
1507 |
> |
final FailingConsumer r = new FailingConsumer(m); |
1508 |
> |
if (!createIncomplete) f.complete(v1); |
1509 |
> |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1510 |
|
if (createIncomplete) { |
1511 |
|
checkIncomplete(g); |
1512 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1512 |
> |
f.complete(v1); |
1513 |
|
} |
1514 |
|
|
1515 |
< |
checkCompletedWithWrappedCancellationException(g); |
1516 |
< |
checkCancelled(f); |
1236 |
< |
assertEquals(0, r.invocationCount); |
1515 |
> |
checkCompletedWithWrappedCFException(g); |
1516 |
> |
checkCompletedNormally(f, v1); |
1517 |
|
}} |
1518 |
|
|
1519 |
|
/** |
1529 |
|
{ |
1530 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1531 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1532 |
< |
final SubtractFunction r = new SubtractFunction(); |
1532 |
> |
final SubtractFunction r = new SubtractFunction(m); |
1533 |
|
|
1534 |
|
if (fFirst) f.complete(v1); else g.complete(v2); |
1535 |
|
if (!createIncomplete) |
1537 |
|
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1538 |
|
if (createIncomplete) { |
1539 |
|
checkIncomplete(h); |
1540 |
< |
assertEquals(0, r.invocationCount); |
1540 |
> |
r.assertNotInvoked(); |
1541 |
|
if (!fFirst) f.complete(v1); else g.complete(v2); |
1542 |
|
} |
1543 |
|
|
1544 |
|
checkCompletedNormally(h, subtract(v1, v2)); |
1545 |
|
checkCompletedNormally(f, v1); |
1546 |
|
checkCompletedNormally(g, v2); |
1547 |
< |
assertEquals(1, r.invocationCount); |
1547 |
> |
r.assertValue(subtract(v1, v2)); |
1548 |
|
}} |
1549 |
|
|
1550 |
|
/** |
1560 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1561 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1562 |
|
final CFException ex = new CFException(); |
1563 |
< |
final SubtractFunction r = new SubtractFunction(); |
1563 |
> |
final SubtractFunction r = new SubtractFunction(m); |
1564 |
|
|
1565 |
|
(fFirst ? f : g).complete(v1); |
1566 |
|
if (!createIncomplete) |
1571 |
|
(!fFirst ? f : g).completeExceptionally(ex); |
1572 |
|
} |
1573 |
|
|
1574 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1575 |
< |
assertEquals(0, r.invocationCount); |
1574 |
> |
checkCompletedWithWrappedException(h, ex); |
1575 |
> |
r.assertNotInvoked(); |
1576 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1577 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1298 |
< |
}} |
1299 |
< |
|
1300 |
< |
/** |
1301 |
< |
* thenCombine result completes exceptionally if action does |
1302 |
< |
*/ |
1303 |
< |
public void testThenCombine_actionFailed() { |
1304 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1305 |
< |
for (boolean fFirst : new boolean[] { true, false }) |
1306 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1307 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1308 |
< |
{ |
1309 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1310 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1311 |
< |
final FailingBiFunction r = new FailingBiFunction(); |
1312 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1313 |
< |
|
1314 |
< |
if (fFirst) { |
1315 |
< |
f.complete(v1); |
1316 |
< |
g.complete(v2); |
1317 |
< |
} else { |
1318 |
< |
g.complete(v2); |
1319 |
< |
f.complete(v1); |
1320 |
< |
} |
1321 |
< |
|
1322 |
< |
checkCompletedWithWrappedCFException(h); |
1323 |
< |
checkCompletedNormally(f, v1); |
1324 |
< |
checkCompletedNormally(g, v2); |
1577 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1578 |
|
}} |
1579 |
|
|
1580 |
|
/** |
1589 |
|
{ |
1590 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1591 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1592 |
< |
final SubtractFunction r = new SubtractFunction(); |
1592 |
> |
final SubtractFunction r = new SubtractFunction(m); |
1593 |
|
|
1594 |
|
(fFirst ? f : g).complete(v1); |
1595 |
|
if (!createIncomplete) |
1602 |
|
|
1603 |
|
checkCompletedWithWrappedCancellationException(h); |
1604 |
|
checkCancelled(!fFirst ? f : g); |
1605 |
< |
assertEquals(0, r.invocationCount); |
1605 |
> |
r.assertNotInvoked(); |
1606 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1607 |
|
}} |
1608 |
|
|
1609 |
|
/** |
1610 |
+ |
* thenCombine result completes exceptionally if action does |
1611 |
+ |
*/ |
1612 |
+ |
public void testThenCombine_actionFailed() { |
1613 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1614 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1615 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1616 |
+ |
for (Integer v2 : new Integer[] { 2, null }) |
1617 |
+ |
{ |
1618 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1619 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1620 |
+ |
final FailingBiFunction r = new FailingBiFunction(m); |
1621 |
+ |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1622 |
+ |
|
1623 |
+ |
if (fFirst) { |
1624 |
+ |
f.complete(v1); |
1625 |
+ |
g.complete(v2); |
1626 |
+ |
} else { |
1627 |
+ |
g.complete(v2); |
1628 |
+ |
f.complete(v1); |
1629 |
+ |
} |
1630 |
+ |
|
1631 |
+ |
checkCompletedWithWrappedCFException(h); |
1632 |
+ |
checkCompletedNormally(f, v1); |
1633 |
+ |
checkCompletedNormally(g, v2); |
1634 |
+ |
}} |
1635 |
+ |
|
1636 |
+ |
/** |
1637 |
|
* thenAcceptBoth result completes normally after normal |
1638 |
|
* completion of sources |
1639 |
|
*/ |
1646 |
|
{ |
1647 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1648 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1649 |
< |
final SubtractAction r = new SubtractAction(); |
1649 |
> |
final SubtractAction r = new SubtractAction(m); |
1650 |
|
|
1651 |
|
if (fFirst) f.complete(v1); else g.complete(v2); |
1652 |
|
if (!createIncomplete) |
1654 |
|
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1655 |
|
if (createIncomplete) { |
1656 |
|
checkIncomplete(h); |
1657 |
< |
assertEquals(0, r.invocationCount); |
1657 |
> |
r.assertNotInvoked(); |
1658 |
|
if (!fFirst) f.complete(v1); else g.complete(v2); |
1659 |
|
} |
1660 |
|
|
1661 |
|
checkCompletedNormally(h, null); |
1662 |
< |
assertEquals(subtract(v1, v2), r.value); |
1662 |
> |
r.assertValue(subtract(v1, v2)); |
1663 |
|
checkCompletedNormally(f, v1); |
1664 |
|
checkCompletedNormally(g, v2); |
1665 |
|
}} |
1677 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1678 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1679 |
|
final CFException ex = new CFException(); |
1680 |
< |
final SubtractAction r = new SubtractAction(); |
1680 |
> |
final SubtractAction r = new SubtractAction(m); |
1681 |
|
|
1682 |
|
(fFirst ? f : g).complete(v1); |
1683 |
|
if (!createIncomplete) |
1688 |
|
(!fFirst ? f : g).completeExceptionally(ex); |
1689 |
|
} |
1690 |
|
|
1691 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1692 |
< |
assertEquals(0, r.invocationCount); |
1691 |
> |
checkCompletedWithWrappedException(h, ex); |
1692 |
> |
r.assertNotInvoked(); |
1693 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1694 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1415 |
< |
}} |
1416 |
< |
|
1417 |
< |
/** |
1418 |
< |
* thenAcceptBoth result completes exceptionally if action does |
1419 |
< |
*/ |
1420 |
< |
public void testThenAcceptBoth_actionFailed() { |
1421 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1422 |
< |
for (boolean fFirst : new boolean[] { true, false }) |
1423 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1424 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1425 |
< |
{ |
1426 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1427 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1428 |
< |
final FailingBiConsumer r = new FailingBiConsumer(); |
1429 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1430 |
< |
|
1431 |
< |
if (fFirst) { |
1432 |
< |
f.complete(v1); |
1433 |
< |
g.complete(v2); |
1434 |
< |
} else { |
1435 |
< |
g.complete(v2); |
1436 |
< |
f.complete(v1); |
1437 |
< |
} |
1438 |
< |
|
1439 |
< |
checkCompletedWithWrappedCFException(h); |
1440 |
< |
checkCompletedNormally(f, v1); |
1441 |
< |
checkCompletedNormally(g, v2); |
1694 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1695 |
|
}} |
1696 |
|
|
1697 |
|
/** |
1706 |
|
{ |
1707 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1708 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1709 |
< |
final SubtractAction r = new SubtractAction(); |
1709 |
> |
final SubtractAction r = new SubtractAction(m); |
1710 |
|
|
1711 |
|
(fFirst ? f : g).complete(v1); |
1712 |
|
if (!createIncomplete) |
1719 |
|
|
1720 |
|
checkCompletedWithWrappedCancellationException(h); |
1721 |
|
checkCancelled(!fFirst ? f : g); |
1722 |
< |
assertEquals(0, r.invocationCount); |
1722 |
> |
r.assertNotInvoked(); |
1723 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1724 |
|
}} |
1725 |
|
|
1726 |
|
/** |
1727 |
+ |
* thenAcceptBoth result completes exceptionally if action does |
1728 |
+ |
*/ |
1729 |
+ |
public void testThenAcceptBoth_actionFailed() { |
1730 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1731 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1732 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1733 |
+ |
for (Integer v2 : new Integer[] { 2, null }) |
1734 |
+ |
{ |
1735 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1736 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1737 |
+ |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1738 |
+ |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1739 |
+ |
|
1740 |
+ |
if (fFirst) { |
1741 |
+ |
f.complete(v1); |
1742 |
+ |
g.complete(v2); |
1743 |
+ |
} else { |
1744 |
+ |
g.complete(v2); |
1745 |
+ |
f.complete(v1); |
1746 |
+ |
} |
1747 |
+ |
|
1748 |
+ |
checkCompletedWithWrappedCFException(h); |
1749 |
+ |
checkCompletedNormally(f, v1); |
1750 |
+ |
checkCompletedNormally(g, v2); |
1751 |
+ |
}} |
1752 |
+ |
|
1753 |
+ |
/** |
1754 |
|
* runAfterBoth result completes normally after normal |
1755 |
|
* completion of sources |
1756 |
|
*/ |
1763 |
|
{ |
1764 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1765 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1766 |
< |
final Noop r = new Noop(); |
1766 |
> |
final Noop r = new Noop(m); |
1767 |
|
|
1768 |
|
if (fFirst) f.complete(v1); else g.complete(v2); |
1769 |
|
if (!createIncomplete) |
1771 |
|
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1772 |
|
if (createIncomplete) { |
1773 |
|
checkIncomplete(h); |
1774 |
< |
assertEquals(0, r.invocationCount); |
1774 |
> |
r.assertNotInvoked(); |
1775 |
|
if (!fFirst) f.complete(v1); else g.complete(v2); |
1776 |
|
} |
1777 |
|
|
1778 |
|
checkCompletedNormally(h, null); |
1779 |
< |
assertEquals(1, r.invocationCount); |
1779 |
> |
r.assertInvoked(); |
1780 |
|
checkCompletedNormally(f, v1); |
1781 |
|
checkCompletedNormally(g, v2); |
1782 |
|
}} |
1794 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1795 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1796 |
|
final CFException ex = new CFException(); |
1797 |
< |
final Noop r = new Noop(); |
1797 |
> |
final Noop r = new Noop(m); |
1798 |
|
|
1799 |
|
(fFirst ? f : g).complete(v1); |
1800 |
|
if (!createIncomplete) |
1805 |
|
(!fFirst ? f : g).completeExceptionally(ex); |
1806 |
|
} |
1807 |
|
|
1808 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1809 |
< |
assertEquals(0, r.invocationCount); |
1808 |
> |
checkCompletedWithWrappedException(h, ex); |
1809 |
> |
r.assertNotInvoked(); |
1810 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1811 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1532 |
< |
}} |
1533 |
< |
|
1534 |
< |
/** |
1535 |
< |
* runAfterBoth result completes exceptionally if action does |
1536 |
< |
*/ |
1537 |
< |
public void testRunAfterBoth_actionFailed() { |
1538 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1539 |
< |
for (boolean fFirst : new boolean[] { true, false }) |
1540 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1541 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1542 |
< |
{ |
1543 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1544 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1545 |
< |
final FailingRunnable r = new FailingRunnable(); |
1546 |
< |
|
1547 |
< |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r); |
1548 |
< |
if (fFirst) { |
1549 |
< |
f.complete(v1); |
1550 |
< |
g.complete(v2); |
1551 |
< |
} else { |
1552 |
< |
g.complete(v2); |
1553 |
< |
f.complete(v1); |
1554 |
< |
} |
1555 |
< |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r); |
1556 |
< |
|
1557 |
< |
checkCompletedWithWrappedCFException(h1); |
1558 |
< |
checkCompletedWithWrappedCFException(h2); |
1559 |
< |
checkCompletedNormally(f, v1); |
1560 |
< |
checkCompletedNormally(g, v2); |
1811 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1812 |
|
}} |
1813 |
|
|
1814 |
|
/** |
1823 |
|
{ |
1824 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1825 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1826 |
< |
final Noop r = new Noop(); |
1576 |
< |
|
1826 |
> |
final Noop r = new Noop(m); |
1827 |
|
|
1828 |
|
(fFirst ? f : g).complete(v1); |
1829 |
|
if (!createIncomplete) |
1836 |
|
|
1837 |
|
checkCompletedWithWrappedCancellationException(h); |
1838 |
|
checkCancelled(!fFirst ? f : g); |
1839 |
< |
assertEquals(0, r.invocationCount); |
1839 |
> |
r.assertNotInvoked(); |
1840 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1841 |
|
}} |
1842 |
|
|
1843 |
|
/** |
1844 |
< |
* applyToEither result completes normally after normal completion |
1595 |
< |
* of either source |
1844 |
> |
* runAfterBoth result completes exceptionally if action does |
1845 |
|
*/ |
1846 |
< |
public void testApplyToEither_normalCompletion() { |
1846 |
> |
public void testRunAfterBoth_actionFailed() { |
1847 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1599 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1848 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1849 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1850 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1851 |
|
{ |
1852 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1853 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1854 |
< |
final IncFunction r = new IncFunction(); |
1854 |
> |
final FailingRunnable r1 = new FailingRunnable(m); |
1855 |
> |
final FailingRunnable r2 = new FailingRunnable(m); |
1856 |
|
|
1857 |
< |
if (!createIncomplete) |
1858 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1859 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1860 |
< |
if (createIncomplete) { |
1861 |
< |
checkIncomplete(h); |
1862 |
< |
assertEquals(0, r.invocationCount); |
1863 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1857 |
> |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1858 |
> |
if (fFirst) { |
1859 |
> |
f.complete(v1); |
1860 |
> |
g.complete(v2); |
1861 |
> |
} else { |
1862 |
> |
g.complete(v2); |
1863 |
> |
f.complete(v1); |
1864 |
|
} |
1865 |
< |
checkCompletedNormally(h, inc(fFirst ? v1 : v2)); |
1617 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1865 |
> |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1866 |
|
|
1867 |
+ |
checkCompletedWithWrappedCFException(h1); |
1868 |
+ |
checkCompletedWithWrappedCFException(h2); |
1869 |
|
checkCompletedNormally(f, v1); |
1870 |
|
checkCompletedNormally(g, v2); |
1621 |
– |
checkCompletedNormally(h, inc(fFirst ? v1 : v2)); |
1871 |
|
}} |
1872 |
|
|
1873 |
< |
public void testApplyToEither_normalCompletionBothAvailable() { |
1873 |
> |
/** |
1874 |
> |
* applyToEither result completes normally after normal completion |
1875 |
> |
* of either source |
1876 |
> |
*/ |
1877 |
> |
public void testApplyToEither_normalCompletion() { |
1878 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1626 |
– |
for (boolean fFirst : new boolean[] { true, false }) |
1879 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1880 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1881 |
|
{ |
1882 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1883 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1884 |
< |
final IncFunction r = new IncFunction(); |
1884 |
> |
final IncFunction[] rs = new IncFunction[6]; |
1885 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1886 |
|
|
1887 |
< |
if (fFirst) { |
1888 |
< |
f.complete(v1); |
1889 |
< |
g.complete(v2); |
1890 |
< |
} else { |
1891 |
< |
g.complete(v2); |
1892 |
< |
f.complete(v1); |
1893 |
< |
} |
1887 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
1888 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
1889 |
> |
checkIncomplete(h0); |
1890 |
> |
checkIncomplete(h1); |
1891 |
> |
rs[0].assertNotInvoked(); |
1892 |
> |
rs[1].assertNotInvoked(); |
1893 |
> |
f.complete(v1); |
1894 |
> |
checkCompletedNormally(h0, inc(v1)); |
1895 |
> |
checkCompletedNormally(h1, inc(v1)); |
1896 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1897 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
1898 |
> |
checkCompletedNormally(h2, inc(v1)); |
1899 |
> |
checkCompletedNormally(h3, inc(v1)); |
1900 |
> |
g.complete(v2); |
1901 |
|
|
1902 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1902 |
> |
// unspecified behavior - both source completions available |
1903 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
1904 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
1905 |
> |
rs[4].assertValue(h4.join()); |
1906 |
> |
rs[5].assertValue(h5.join()); |
1907 |
> |
assertTrue(Objects.equals(inc(v1), h4.join()) || |
1908 |
> |
Objects.equals(inc(v2), h4.join())); |
1909 |
> |
assertTrue(Objects.equals(inc(v1), h5.join()) || |
1910 |
> |
Objects.equals(inc(v2), h5.join())); |
1911 |
|
|
1912 |
|
checkCompletedNormally(f, v1); |
1913 |
|
checkCompletedNormally(g, v2); |
1914 |
< |
|
1915 |
< |
// unspecified behavior |
1916 |
< |
assertTrue(Objects.equals(h.join(), inc(v1)) || |
1917 |
< |
Objects.equals(h.join(), inc(v2))); |
1918 |
< |
assertEquals(1, r.invocationCount); |
1914 |
> |
checkCompletedNormally(h0, inc(v1)); |
1915 |
> |
checkCompletedNormally(h1, inc(v1)); |
1916 |
> |
checkCompletedNormally(h2, inc(v1)); |
1917 |
> |
checkCompletedNormally(h3, inc(v1)); |
1918 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); |
1919 |
|
}} |
1920 |
|
|
1921 |
|
/** |
1922 |
|
* applyToEither result completes exceptionally after exceptional |
1923 |
|
* completion of either source |
1924 |
|
*/ |
1925 |
< |
public void testApplyToEither_exceptionalCompletion1() { |
1925 |
> |
public void testApplyToEither_exceptionalCompletion() { |
1926 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1659 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1660 |
– |
for (boolean fFirst : new boolean[] { true, false }) |
1927 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1928 |
|
{ |
1929 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1930 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1931 |
|
final CFException ex = new CFException(); |
1932 |
< |
final IncFunction r = new IncFunction(); |
1932 |
> |
final IncFunction[] rs = new IncFunction[6]; |
1933 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1934 |
|
|
1935 |
< |
if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex); |
1936 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1937 |
< |
if (createIncomplete) { |
1938 |
< |
checkIncomplete(h); |
1939 |
< |
assertEquals(0, r.invocationCount); |
1940 |
< |
(fFirst ? f : g).completeExceptionally(ex); |
1941 |
< |
} |
1935 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
1936 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
1937 |
> |
checkIncomplete(h0); |
1938 |
> |
checkIncomplete(h1); |
1939 |
> |
rs[0].assertNotInvoked(); |
1940 |
> |
rs[1].assertNotInvoked(); |
1941 |
> |
f.completeExceptionally(ex); |
1942 |
> |
checkCompletedWithWrappedException(h0, ex); |
1943 |
> |
checkCompletedWithWrappedException(h1, ex); |
1944 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1945 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
1946 |
> |
checkCompletedWithWrappedException(h2, ex); |
1947 |
> |
checkCompletedWithWrappedException(h3, ex); |
1948 |
> |
g.complete(v1); |
1949 |
|
|
1950 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1951 |
< |
(!fFirst ? f : g).complete(v1); |
1950 |
> |
// unspecified behavior - both source completions available |
1951 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
1952 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
1953 |
> |
try { |
1954 |
> |
assertEquals(inc(v1), h4.join()); |
1955 |
> |
rs[4].assertValue(inc(v1)); |
1956 |
> |
} catch (CompletionException ok) { |
1957 |
> |
checkCompletedWithWrappedException(h4, ex); |
1958 |
> |
rs[4].assertNotInvoked(); |
1959 |
> |
} |
1960 |
> |
try { |
1961 |
> |
assertEquals(inc(v1), h5.join()); |
1962 |
> |
rs[5].assertValue(inc(v1)); |
1963 |
> |
} catch (CompletionException ok) { |
1964 |
> |
checkCompletedWithWrappedException(h5, ex); |
1965 |
> |
rs[5].assertNotInvoked(); |
1966 |
> |
} |
1967 |
|
|
1968 |
< |
assertEquals(0, r.invocationCount); |
1969 |
< |
checkCompletedNormally(!fFirst ? f : g, v1); |
1970 |
< |
checkCompletedWithWrappedCFException(fFirst ? f : g, ex); |
1971 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1968 |
> |
checkCompletedExceptionally(f, ex); |
1969 |
> |
checkCompletedNormally(g, v1); |
1970 |
> |
checkCompletedWithWrappedException(h0, ex); |
1971 |
> |
checkCompletedWithWrappedException(h1, ex); |
1972 |
> |
checkCompletedWithWrappedException(h2, ex); |
1973 |
> |
checkCompletedWithWrappedException(h3, ex); |
1974 |
> |
checkCompletedWithWrappedException(h4, ex); |
1975 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
1976 |
|
}} |
1977 |
|
|
1978 |
|
public void testApplyToEither_exceptionalCompletion2() { |
1979 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1687 |
– |
for (boolean reverseArgs : new boolean[] { true, false }) |
1980 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1981 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1982 |
|
{ |
1983 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1984 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1693 |
– |
final IncFunction r1 = new IncFunction(); |
1694 |
– |
final IncFunction r2 = new IncFunction(); |
1985 |
|
final CFException ex = new CFException(); |
1986 |
< |
final CompletableFuture<Integer> j = (reverseArgs ? g : f); |
1987 |
< |
final CompletableFuture<Integer> k = (reverseArgs ? f : g); |
1988 |
< |
final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1); |
1986 |
> |
final IncFunction[] rs = new IncFunction[6]; |
1987 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1988 |
> |
|
1989 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
1990 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
1991 |
|
if (fFirst) { |
1992 |
|
f.complete(v1); |
1993 |
|
g.completeExceptionally(ex); |
1995 |
|
g.completeExceptionally(ex); |
1996 |
|
f.complete(v1); |
1997 |
|
} |
1998 |
< |
final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2); |
1998 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1999 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2000 |
|
|
2001 |
< |
// unspecified behavior |
2001 |
> |
// unspecified behavior - both source completions available |
2002 |
> |
try { |
2003 |
> |
assertEquals(inc(v1), h0.join()); |
2004 |
> |
rs[0].assertValue(inc(v1)); |
2005 |
> |
} catch (CompletionException ok) { |
2006 |
> |
checkCompletedWithWrappedException(h0, ex); |
2007 |
> |
rs[0].assertNotInvoked(); |
2008 |
> |
} |
2009 |
|
try { |
2010 |
|
assertEquals(inc(v1), h1.join()); |
2011 |
< |
assertEquals(1, r1.invocationCount); |
2011 |
> |
rs[1].assertValue(inc(v1)); |
2012 |
|
} catch (CompletionException ok) { |
2013 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2014 |
< |
assertEquals(0, r1.invocationCount); |
2013 |
> |
checkCompletedWithWrappedException(h1, ex); |
2014 |
> |
rs[1].assertNotInvoked(); |
2015 |
|
} |
1716 |
– |
|
2016 |
|
try { |
2017 |
|
assertEquals(inc(v1), h2.join()); |
2018 |
< |
assertEquals(1, r2.invocationCount); |
2018 |
> |
rs[2].assertValue(inc(v1)); |
2019 |
|
} catch (CompletionException ok) { |
2020 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2021 |
< |
assertEquals(0, r2.invocationCount); |
2020 |
> |
checkCompletedWithWrappedException(h2, ex); |
2021 |
> |
rs[2].assertNotInvoked(); |
2022 |
> |
} |
2023 |
> |
try { |
2024 |
> |
assertEquals(inc(v1), h3.join()); |
2025 |
> |
rs[3].assertValue(inc(v1)); |
2026 |
> |
} catch (CompletionException ok) { |
2027 |
> |
checkCompletedWithWrappedException(h3, ex); |
2028 |
> |
rs[3].assertNotInvoked(); |
2029 |
|
} |
2030 |
|
|
1725 |
– |
checkCompletedWithWrappedCFException(g, ex); |
1726 |
– |
checkCompletedNormally(f, v1); |
1727 |
– |
}} |
1728 |
– |
|
1729 |
– |
/** |
1730 |
– |
* applyToEither result completes exceptionally if action does |
1731 |
– |
*/ |
1732 |
– |
public void testApplyToEither_actionFailed1() { |
1733 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1734 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1735 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
1736 |
– |
{ |
1737 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1738 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1739 |
– |
final FailingFunction r = new FailingFunction(); |
1740 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1741 |
– |
|
1742 |
– |
f.complete(v1); |
1743 |
– |
checkCompletedWithWrappedCFException(h); |
1744 |
– |
g.complete(v2); |
1745 |
– |
checkCompletedNormally(f, v1); |
1746 |
– |
checkCompletedNormally(g, v2); |
1747 |
– |
}} |
1748 |
– |
|
1749 |
– |
public void testApplyToEither_actionFailed2() { |
1750 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1751 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1752 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
1753 |
– |
{ |
1754 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1755 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1756 |
– |
final FailingFunction r = new FailingFunction(); |
1757 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1758 |
– |
|
1759 |
– |
g.complete(v2); |
1760 |
– |
checkCompletedWithWrappedCFException(h); |
1761 |
– |
f.complete(v1); |
2031 |
|
checkCompletedNormally(f, v1); |
2032 |
< |
checkCompletedNormally(g, v2); |
2032 |
> |
checkCompletedExceptionally(g, ex); |
2033 |
|
}} |
2034 |
|
|
2035 |
|
/** |
2036 |
|
* applyToEither result completes exceptionally if either source cancelled |
2037 |
|
*/ |
2038 |
< |
public void testApplyToEither_sourceCancelled1() { |
2038 |
> |
public void testApplyToEither_sourceCancelled() { |
2039 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2040 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1772 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1773 |
– |
for (boolean fFirst : new boolean[] { true, false }) |
2041 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2042 |
|
{ |
2043 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2044 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2045 |
< |
final IncFunction r = new IncFunction(); |
2045 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2046 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2047 |
|
|
2048 |
< |
if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2049 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2050 |
< |
if (createIncomplete) { |
2051 |
< |
checkIncomplete(h); |
2052 |
< |
assertEquals(0, r.invocationCount); |
2053 |
< |
assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2054 |
< |
} |
2048 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2049 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2050 |
> |
checkIncomplete(h0); |
2051 |
> |
checkIncomplete(h1); |
2052 |
> |
rs[0].assertNotInvoked(); |
2053 |
> |
rs[1].assertNotInvoked(); |
2054 |
> |
f.cancel(mayInterruptIfRunning); |
2055 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2056 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2057 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2058 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2059 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2060 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2061 |
> |
g.complete(v1); |
2062 |
|
|
2063 |
< |
checkCompletedWithWrappedCancellationException(h); |
2064 |
< |
(!fFirst ? f : g).complete(v1); |
2063 |
> |
// unspecified behavior - both source completions available |
2064 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2065 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2066 |
> |
try { |
2067 |
> |
assertEquals(inc(v1), h4.join()); |
2068 |
> |
rs[4].assertValue(inc(v1)); |
2069 |
> |
} catch (CompletionException ok) { |
2070 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2071 |
> |
rs[4].assertNotInvoked(); |
2072 |
> |
} |
2073 |
> |
try { |
2074 |
> |
assertEquals(inc(v1), h5.join()); |
2075 |
> |
rs[5].assertValue(inc(v1)); |
2076 |
> |
} catch (CompletionException ok) { |
2077 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2078 |
> |
rs[5].assertNotInvoked(); |
2079 |
> |
} |
2080 |
|
|
2081 |
< |
assertEquals(0, r.invocationCount); |
2082 |
< |
checkCompletedNormally(!fFirst ? f : g, v1); |
2083 |
< |
checkCancelled(fFirst ? f : g); |
2084 |
< |
checkCompletedWithWrappedCancellationException(h); |
2081 |
> |
checkCancelled(f); |
2082 |
> |
checkCompletedNormally(g, v1); |
2083 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2084 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2085 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2086 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2087 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2088 |
|
}} |
2089 |
|
|
2090 |
|
public void testApplyToEither_sourceCancelled2() { |
2091 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2092 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1800 |
– |
for (boolean reverseArgs : new boolean[] { true, false }) |
2093 |
|
for (boolean fFirst : new boolean[] { true, false }) |
2094 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2095 |
|
{ |
2096 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2097 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2098 |
< |
final IncFunction r1 = new IncFunction(); |
2099 |
< |
final IncFunction r2 = new IncFunction(); |
1808 |
< |
final CFException ex = new CFException(); |
1809 |
< |
final CompletableFuture<Integer> j = (reverseArgs ? g : f); |
1810 |
< |
final CompletableFuture<Integer> k = (reverseArgs ? f : g); |
2098 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2099 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2100 |
|
|
2101 |
< |
final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1); |
2101 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2102 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2103 |
|
if (fFirst) { |
2104 |
|
f.complete(v1); |
2105 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2105 |
> |
g.cancel(mayInterruptIfRunning); |
2106 |
|
} else { |
2107 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2107 |
> |
g.cancel(mayInterruptIfRunning); |
2108 |
|
f.complete(v1); |
2109 |
|
} |
2110 |
< |
final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2); |
2110 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2111 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2112 |
|
|
2113 |
< |
// unspecified behavior |
2113 |
> |
// unspecified behavior - both source completions available |
2114 |
> |
try { |
2115 |
> |
assertEquals(inc(v1), h0.join()); |
2116 |
> |
rs[0].assertValue(inc(v1)); |
2117 |
> |
} catch (CompletionException ok) { |
2118 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2119 |
> |
rs[0].assertNotInvoked(); |
2120 |
> |
} |
2121 |
|
try { |
2122 |
|
assertEquals(inc(v1), h1.join()); |
2123 |
< |
assertEquals(1, r1.invocationCount); |
2123 |
> |
rs[1].assertValue(inc(v1)); |
2124 |
|
} catch (CompletionException ok) { |
2125 |
|
checkCompletedWithWrappedCancellationException(h1); |
2126 |
< |
assertEquals(0, r1.invocationCount); |
2126 |
> |
rs[1].assertNotInvoked(); |
2127 |
|
} |
1830 |
– |
|
2128 |
|
try { |
2129 |
|
assertEquals(inc(v1), h2.join()); |
2130 |
< |
assertEquals(1, r2.invocationCount); |
2130 |
> |
rs[2].assertValue(inc(v1)); |
2131 |
|
} catch (CompletionException ok) { |
2132 |
|
checkCompletedWithWrappedCancellationException(h2); |
2133 |
< |
assertEquals(0, r2.invocationCount); |
2133 |
> |
rs[2].assertNotInvoked(); |
2134 |
> |
} |
2135 |
> |
try { |
2136 |
> |
assertEquals(inc(v1), h3.join()); |
2137 |
> |
rs[3].assertValue(inc(v1)); |
2138 |
> |
} catch (CompletionException ok) { |
2139 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2140 |
> |
rs[3].assertNotInvoked(); |
2141 |
|
} |
2142 |
|
|
1839 |
– |
checkCancelled(g); |
2143 |
|
checkCompletedNormally(f, v1); |
2144 |
+ |
checkCancelled(g); |
2145 |
|
}} |
2146 |
|
|
2147 |
|
/** |
2148 |
< |
* acceptEither result completes normally after normal completion |
1845 |
< |
* of either source |
2148 |
> |
* applyToEither result completes exceptionally if action does |
2149 |
|
*/ |
2150 |
< |
public void testAcceptEither_normalCompletion1() { |
2150 |
> |
public void testApplyToEither_actionFailed() { |
2151 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2152 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2153 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2154 |
|
{ |
2155 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2156 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2157 |
< |
final IncAction r = new IncAction(); |
2158 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2157 |
> |
final FailingFunction[] rs = new FailingFunction[6]; |
2158 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
2159 |
|
|
2160 |
+ |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2161 |
+ |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2162 |
|
f.complete(v1); |
2163 |
< |
checkCompletedNormally(h, null); |
2164 |
< |
assertEquals(inc(v1), r.value); |
2165 |
< |
g.complete(v2); |
2166 |
< |
|
2167 |
< |
checkCompletedNormally(f, v1); |
2168 |
< |
checkCompletedNormally(g, v2); |
2169 |
< |
checkCompletedNormally(h, null); |
1865 |
< |
}} |
1866 |
< |
|
1867 |
< |
public void testAcceptEither_normalCompletion2() { |
1868 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1869 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1870 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1871 |
< |
{ |
1872 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1873 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1874 |
< |
final IncAction r = new IncAction(); |
1875 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2163 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2164 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2165 |
> |
checkCompletedWithWrappedCFException(h0); |
2166 |
> |
checkCompletedWithWrappedCFException(h1); |
2167 |
> |
checkCompletedWithWrappedCFException(h2); |
2168 |
> |
checkCompletedWithWrappedCFException(h3); |
2169 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2170 |
|
|
2171 |
|
g.complete(v2); |
2172 |
< |
checkCompletedNormally(h, null); |
2173 |
< |
assertEquals(inc(v2), r.value); |
2174 |
< |
f.complete(v1); |
2172 |
> |
|
2173 |
> |
// unspecified behavior - both source completions available |
2174 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2175 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2176 |
> |
|
2177 |
> |
checkCompletedWithWrappedCFException(h4); |
2178 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2179 |
> |
Objects.equals(v2, rs[4].value)); |
2180 |
> |
checkCompletedWithWrappedCFException(h5); |
2181 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2182 |
> |
Objects.equals(v2, rs[5].value)); |
2183 |
|
|
2184 |
|
checkCompletedNormally(f, v1); |
2185 |
|
checkCompletedNormally(g, v2); |
1884 |
– |
checkCompletedNormally(h, null); |
2186 |
|
}} |
2187 |
|
|
2188 |
< |
public void testAcceptEither_normalCompletion3() { |
2188 |
> |
/** |
2189 |
> |
* acceptEither result completes normally after normal completion |
2190 |
> |
* of either source |
2191 |
> |
*/ |
2192 |
> |
public void testAcceptEither_normalCompletion() { |
2193 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2194 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2195 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2196 |
|
{ |
2197 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2198 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2199 |
< |
final IncAction r = new IncAction(); |
2199 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2200 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2201 |
|
|
2202 |
+ |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2203 |
+ |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2204 |
+ |
checkIncomplete(h0); |
2205 |
+ |
checkIncomplete(h1); |
2206 |
+ |
rs[0].assertNotInvoked(); |
2207 |
+ |
rs[1].assertNotInvoked(); |
2208 |
|
f.complete(v1); |
2209 |
+ |
checkCompletedNormally(h0, null); |
2210 |
+ |
checkCompletedNormally(h1, null); |
2211 |
+ |
rs[0].assertValue(v1); |
2212 |
+ |
rs[1].assertValue(v1); |
2213 |
+ |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2214 |
+ |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2215 |
+ |
checkCompletedNormally(h2, null); |
2216 |
+ |
checkCompletedNormally(h3, null); |
2217 |
+ |
rs[2].assertValue(v1); |
2218 |
+ |
rs[3].assertValue(v1); |
2219 |
|
g.complete(v2); |
1898 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2220 |
|
|
2221 |
< |
checkCompletedNormally(h, null); |
2221 |
> |
// unspecified behavior - both source completions available |
2222 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2223 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2224 |
> |
checkCompletedNormally(h4, null); |
2225 |
> |
checkCompletedNormally(h5, null); |
2226 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2227 |
> |
Objects.equals(v2, rs[4].value)); |
2228 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2229 |
> |
Objects.equals(v2, rs[5].value)); |
2230 |
> |
|
2231 |
|
checkCompletedNormally(f, v1); |
2232 |
|
checkCompletedNormally(g, v2); |
2233 |
< |
|
2234 |
< |
// unspecified behavior |
2235 |
< |
assertTrue(Objects.equals(r.value, inc(v1)) || |
2236 |
< |
Objects.equals(r.value, inc(v2))); |
2233 |
> |
checkCompletedNormally(h0, null); |
2234 |
> |
checkCompletedNormally(h1, null); |
2235 |
> |
checkCompletedNormally(h2, null); |
2236 |
> |
checkCompletedNormally(h3, null); |
2237 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2238 |
|
}} |
2239 |
|
|
2240 |
|
/** |
2241 |
|
* acceptEither result completes exceptionally after exceptional |
2242 |
|
* completion of either source |
2243 |
|
*/ |
2244 |
< |
public void testAcceptEither_exceptionalCompletion1() { |
2244 |
> |
public void testAcceptEither_exceptionalCompletion() { |
2245 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2246 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2247 |
|
{ |
2248 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2249 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1919 |
– |
final IncAction r = new IncAction(); |
1920 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2250 |
|
final CFException ex = new CFException(); |
2251 |
+ |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2252 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2253 |
|
|
2254 |
+ |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2255 |
+ |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2256 |
+ |
checkIncomplete(h0); |
2257 |
+ |
checkIncomplete(h1); |
2258 |
+ |
rs[0].assertNotInvoked(); |
2259 |
+ |
rs[1].assertNotInvoked(); |
2260 |
|
f.completeExceptionally(ex); |
2261 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2261 |
> |
checkCompletedWithWrappedException(h0, ex); |
2262 |
> |
checkCompletedWithWrappedException(h1, ex); |
2263 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2264 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2265 |
> |
checkCompletedWithWrappedException(h2, ex); |
2266 |
> |
checkCompletedWithWrappedException(h3, ex); |
2267 |
> |
|
2268 |
|
g.complete(v1); |
2269 |
|
|
2270 |
< |
assertEquals(0, r.invocationCount); |
2270 |
> |
// unspecified behavior - both source completions available |
2271 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2272 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2273 |
> |
try { |
2274 |
> |
assertNull(h4.join()); |
2275 |
> |
rs[4].assertValue(v1); |
2276 |
> |
} catch (CompletionException ok) { |
2277 |
> |
checkCompletedWithWrappedException(h4, ex); |
2278 |
> |
rs[4].assertNotInvoked(); |
2279 |
> |
} |
2280 |
> |
try { |
2281 |
> |
assertNull(h5.join()); |
2282 |
> |
rs[5].assertValue(v1); |
2283 |
> |
} catch (CompletionException ok) { |
2284 |
> |
checkCompletedWithWrappedException(h5, ex); |
2285 |
> |
rs[5].assertNotInvoked(); |
2286 |
> |
} |
2287 |
> |
|
2288 |
> |
checkCompletedExceptionally(f, ex); |
2289 |
|
checkCompletedNormally(g, v1); |
2290 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2291 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2290 |
> |
checkCompletedWithWrappedException(h0, ex); |
2291 |
> |
checkCompletedWithWrappedException(h1, ex); |
2292 |
> |
checkCompletedWithWrappedException(h2, ex); |
2293 |
> |
checkCompletedWithWrappedException(h3, ex); |
2294 |
> |
checkCompletedWithWrappedException(h4, ex); |
2295 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2296 |
|
}} |
2297 |
|
|
2298 |
|
public void testAcceptEither_exceptionalCompletion2() { |
2299 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2300 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2301 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2302 |
|
{ |
2303 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2304 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1939 |
– |
final IncAction r = new IncAction(); |
1940 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
1941 |
– |
final CFException ex = new CFException(); |
1942 |
– |
|
1943 |
– |
g.completeExceptionally(ex); |
1944 |
– |
checkCompletedWithWrappedCFException(h, ex); |
1945 |
– |
f.complete(v1); |
1946 |
– |
|
1947 |
– |
assertEquals(0, r.invocationCount); |
1948 |
– |
checkCompletedNormally(f, v1); |
1949 |
– |
checkCompletedWithWrappedCFException(g, ex); |
1950 |
– |
checkCompletedWithWrappedCFException(h, ex); |
1951 |
– |
}} |
1952 |
– |
|
1953 |
– |
public void testAcceptEither_exceptionalCompletion3() { |
1954 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1955 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1956 |
– |
{ |
1957 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1958 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1959 |
– |
final IncAction r = new IncAction(); |
2305 |
|
final CFException ex = new CFException(); |
2306 |
+ |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2307 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2308 |
|
|
2309 |
< |
g.completeExceptionally(ex); |
2310 |
< |
f.complete(v1); |
2311 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2309 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2310 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2311 |
> |
if (fFirst) { |
2312 |
> |
f.complete(v1); |
2313 |
> |
g.completeExceptionally(ex); |
2314 |
> |
} else { |
2315 |
> |
g.completeExceptionally(ex); |
2316 |
> |
f.complete(v1); |
2317 |
> |
} |
2318 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2319 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2320 |
|
|
2321 |
< |
// unspecified behavior |
1967 |
< |
Integer v; |
2321 |
> |
// unspecified behavior - both source completions available |
2322 |
|
try { |
2323 |
< |
assertNull(h.join()); |
2324 |
< |
assertEquals(1, r.invocationCount); |
1971 |
< |
assertEquals(inc(v1), r.value); |
2323 |
> |
assertEquals(null, h0.join()); |
2324 |
> |
rs[0].assertValue(v1); |
2325 |
|
} catch (CompletionException ok) { |
2326 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2327 |
< |
assertEquals(0, r.invocationCount); |
2326 |
> |
checkCompletedWithWrappedException(h0, ex); |
2327 |
> |
rs[0].assertNotInvoked(); |
2328 |
|
} |
1976 |
– |
|
1977 |
– |
checkCompletedWithWrappedCFException(g, ex); |
1978 |
– |
checkCompletedNormally(f, v1); |
1979 |
– |
}} |
1980 |
– |
|
1981 |
– |
public void testAcceptEither_exceptionalCompletion4() { |
1982 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1983 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1984 |
– |
{ |
1985 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1986 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1987 |
– |
final IncAction r = new IncAction(); |
1988 |
– |
final CFException ex = new CFException(); |
1989 |
– |
|
1990 |
– |
f.completeExceptionally(ex); |
1991 |
– |
g.complete(v1); |
1992 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
1993 |
– |
|
1994 |
– |
// unspecified behavior |
1995 |
– |
Integer v; |
2329 |
|
try { |
2330 |
< |
assertNull(h.join()); |
2331 |
< |
assertEquals(1, r.invocationCount); |
1999 |
< |
assertEquals(inc(v1), r.value); |
2330 |
> |
assertEquals(null, h1.join()); |
2331 |
> |
rs[1].assertValue(v1); |
2332 |
|
} catch (CompletionException ok) { |
2333 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2334 |
< |
assertEquals(0, r.invocationCount); |
2333 |
> |
checkCompletedWithWrappedException(h1, ex); |
2334 |
> |
rs[1].assertNotInvoked(); |
2335 |
> |
} |
2336 |
> |
try { |
2337 |
> |
assertEquals(null, h2.join()); |
2338 |
> |
rs[2].assertValue(v1); |
2339 |
> |
} catch (CompletionException ok) { |
2340 |
> |
checkCompletedWithWrappedException(h2, ex); |
2341 |
> |
rs[2].assertNotInvoked(); |
2342 |
> |
} |
2343 |
> |
try { |
2344 |
> |
assertEquals(null, h3.join()); |
2345 |
> |
rs[3].assertValue(v1); |
2346 |
> |
} catch (CompletionException ok) { |
2347 |
> |
checkCompletedWithWrappedException(h3, ex); |
2348 |
> |
rs[3].assertNotInvoked(); |
2349 |
|
} |
2350 |
|
|
2005 |
– |
checkCompletedWithWrappedCFException(f, ex); |
2006 |
– |
checkCompletedNormally(g, v1); |
2007 |
– |
}} |
2008 |
– |
|
2009 |
– |
/** |
2010 |
– |
* acceptEither result completes exceptionally if action does |
2011 |
– |
*/ |
2012 |
– |
public void testAcceptEither_actionFailed1() { |
2013 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2014 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2015 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2016 |
– |
{ |
2017 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2018 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2019 |
– |
final FailingConsumer r = new FailingConsumer(); |
2020 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2021 |
– |
|
2022 |
– |
f.complete(v1); |
2023 |
– |
checkCompletedWithWrappedCFException(h); |
2024 |
– |
g.complete(v2); |
2025 |
– |
checkCompletedNormally(f, v1); |
2026 |
– |
checkCompletedNormally(g, v2); |
2027 |
– |
}} |
2028 |
– |
|
2029 |
– |
public void testAcceptEither_actionFailed2() { |
2030 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2031 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2032 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2033 |
– |
{ |
2034 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2035 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2036 |
– |
final FailingConsumer r = new FailingConsumer(); |
2037 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2038 |
– |
|
2039 |
– |
g.complete(v2); |
2040 |
– |
checkCompletedWithWrappedCFException(h); |
2041 |
– |
f.complete(v1); |
2351 |
|
checkCompletedNormally(f, v1); |
2352 |
< |
checkCompletedNormally(g, v2); |
2352 |
> |
checkCompletedExceptionally(g, ex); |
2353 |
|
}} |
2354 |
|
|
2355 |
|
/** |
2356 |
|
* acceptEither result completes exceptionally if either source cancelled |
2357 |
|
*/ |
2358 |
< |
public void testAcceptEither_sourceCancelled1() { |
2050 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2051 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2052 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2053 |
< |
{ |
2054 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2055 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2056 |
< |
final IncAction r = new IncAction(); |
2057 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2058 |
< |
|
2059 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2060 |
< |
checkCompletedWithWrappedCancellationException(h); |
2061 |
< |
g.complete(v1); |
2062 |
< |
|
2063 |
< |
checkCancelled(f); |
2064 |
< |
assertEquals(0, r.invocationCount); |
2065 |
< |
checkCompletedNormally(g, v1); |
2066 |
< |
checkCompletedWithWrappedCancellationException(h); |
2067 |
< |
}} |
2068 |
< |
|
2069 |
< |
public void testAcceptEither_sourceCancelled2() { |
2358 |
> |
public void testAcceptEither_sourceCancelled() { |
2359 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2360 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2361 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2362 |
|
{ |
2363 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2364 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2365 |
< |
final IncAction r = new IncAction(); |
2366 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2078 |
< |
|
2079 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2080 |
< |
checkCompletedWithWrappedCancellationException(h); |
2081 |
< |
f.complete(v1); |
2082 |
< |
|
2083 |
< |
checkCancelled(g); |
2084 |
< |
assertEquals(0, r.invocationCount); |
2085 |
< |
checkCompletedNormally(f, v1); |
2086 |
< |
checkCompletedWithWrappedCancellationException(h); |
2087 |
< |
}} |
2365 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2366 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2367 |
|
|
2368 |
< |
public void testAcceptEither_sourceCancelled3() { |
2369 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2370 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2371 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2372 |
< |
{ |
2373 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2374 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2375 |
< |
final IncAction r = new IncAction(); |
2368 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2369 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2370 |
> |
checkIncomplete(h0); |
2371 |
> |
checkIncomplete(h1); |
2372 |
> |
rs[0].assertNotInvoked(); |
2373 |
> |
rs[1].assertNotInvoked(); |
2374 |
> |
f.cancel(mayInterruptIfRunning); |
2375 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2376 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2377 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2378 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2379 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2380 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2381 |
|
|
2382 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2099 |
< |
f.complete(v1); |
2100 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2382 |
> |
g.complete(v1); |
2383 |
|
|
2384 |
< |
// unspecified behavior |
2385 |
< |
Integer v; |
2384 |
> |
// unspecified behavior - both source completions available |
2385 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2386 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2387 |
|
try { |
2388 |
< |
assertNull(h.join()); |
2389 |
< |
assertEquals(1, r.invocationCount); |
2107 |
< |
assertEquals(inc(v1), r.value); |
2388 |
> |
assertNull(h4.join()); |
2389 |
> |
rs[4].assertValue(v1); |
2390 |
|
} catch (CompletionException ok) { |
2391 |
< |
checkCompletedWithWrappedCancellationException(h); |
2392 |
< |
assertEquals(0, r.invocationCount); |
2391 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2392 |
> |
rs[4].assertNotInvoked(); |
2393 |
|
} |
2112 |
– |
|
2113 |
– |
checkCancelled(g); |
2114 |
– |
checkCompletedNormally(f, v1); |
2115 |
– |
}} |
2116 |
– |
|
2117 |
– |
public void testAcceptEither_sourceCancelled4() { |
2118 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2119 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2120 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2121 |
– |
{ |
2122 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2123 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2124 |
– |
final IncAction r = new IncAction(); |
2125 |
– |
|
2126 |
– |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2127 |
– |
g.complete(v1); |
2128 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2129 |
– |
|
2130 |
– |
// unspecified behavior |
2131 |
– |
Integer v; |
2394 |
|
try { |
2395 |
< |
assertNull(h.join()); |
2396 |
< |
assertEquals(1, r.invocationCount); |
2135 |
< |
assertEquals(inc(v1), r.value); |
2395 |
> |
assertNull(h5.join()); |
2396 |
> |
rs[5].assertValue(v1); |
2397 |
|
} catch (CompletionException ok) { |
2398 |
< |
checkCompletedWithWrappedCancellationException(h); |
2399 |
< |
assertEquals(0, r.invocationCount); |
2398 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2399 |
> |
rs[5].assertNotInvoked(); |
2400 |
|
} |
2401 |
|
|
2402 |
|
checkCancelled(f); |
2403 |
|
checkCompletedNormally(g, v1); |
2404 |
+ |
checkCompletedWithWrappedCancellationException(h0); |
2405 |
+ |
checkCompletedWithWrappedCancellationException(h1); |
2406 |
+ |
checkCompletedWithWrappedCancellationException(h2); |
2407 |
+ |
checkCompletedWithWrappedCancellationException(h3); |
2408 |
+ |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2409 |
|
}} |
2410 |
|
|
2411 |
|
/** |
2412 |
< |
* runAfterEither result completes normally after normal completion |
2147 |
< |
* of either source |
2412 |
> |
* acceptEither result completes exceptionally if action does |
2413 |
|
*/ |
2414 |
< |
public void testRunAfterEither_normalCompletion1() { |
2414 |
> |
public void testAcceptEither_actionFailed() { |
2415 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2416 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2417 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2418 |
|
{ |
2419 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2420 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2421 |
< |
final Noop r = new Noop(); |
2422 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2421 |
> |
final FailingConsumer[] rs = new FailingConsumer[6]; |
2422 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
2423 |
|
|
2424 |
+ |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2425 |
+ |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2426 |
|
f.complete(v1); |
2427 |
< |
checkCompletedNormally(h, null); |
2428 |
< |
assertEquals(1, r.invocationCount); |
2427 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2428 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2429 |
> |
checkCompletedWithWrappedCFException(h0); |
2430 |
> |
checkCompletedWithWrappedCFException(h1); |
2431 |
> |
checkCompletedWithWrappedCFException(h2); |
2432 |
> |
checkCompletedWithWrappedCFException(h3); |
2433 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2434 |
> |
|
2435 |
|
g.complete(v2); |
2436 |
|
|
2437 |
+ |
// unspecified behavior - both source completions available |
2438 |
+ |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2439 |
+ |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2440 |
+ |
|
2441 |
+ |
checkCompletedWithWrappedCFException(h4); |
2442 |
+ |
assertTrue(Objects.equals(v1, rs[4].value) || |
2443 |
+ |
Objects.equals(v2, rs[4].value)); |
2444 |
+ |
checkCompletedWithWrappedCFException(h5); |
2445 |
+ |
assertTrue(Objects.equals(v1, rs[5].value) || |
2446 |
+ |
Objects.equals(v2, rs[5].value)); |
2447 |
+ |
|
2448 |
|
checkCompletedNormally(f, v1); |
2449 |
|
checkCompletedNormally(g, v2); |
2166 |
– |
checkCompletedNormally(h, null); |
2167 |
– |
assertEquals(1, r.invocationCount); |
2450 |
|
}} |
2451 |
|
|
2452 |
< |
public void testRunAfterEither_normalCompletion2() { |
2452 |
> |
/** |
2453 |
> |
* runAfterEither result completes normally after normal completion |
2454 |
> |
* of either source |
2455 |
> |
*/ |
2456 |
> |
public void testRunAfterEither_normalCompletion() { |
2457 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2458 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2459 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2460 |
|
{ |
2461 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2462 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2463 |
< |
final Noop r = new Noop(); |
2464 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2463 |
> |
final Noop[] rs = new Noop[6]; |
2464 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2465 |
|
|
2466 |
< |
g.complete(v2); |
2467 |
< |
checkCompletedNormally(h, null); |
2468 |
< |
assertEquals(1, r.invocationCount); |
2466 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2467 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2468 |
> |
checkIncomplete(h0); |
2469 |
> |
checkIncomplete(h1); |
2470 |
> |
rs[0].assertNotInvoked(); |
2471 |
> |
rs[1].assertNotInvoked(); |
2472 |
|
f.complete(v1); |
2473 |
+ |
checkCompletedNormally(h0, null); |
2474 |
+ |
checkCompletedNormally(h1, null); |
2475 |
+ |
rs[0].assertInvoked(); |
2476 |
+ |
rs[1].assertInvoked(); |
2477 |
+ |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2478 |
+ |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2479 |
+ |
checkCompletedNormally(h2, null); |
2480 |
+ |
checkCompletedNormally(h3, null); |
2481 |
+ |
rs[2].assertInvoked(); |
2482 |
+ |
rs[3].assertInvoked(); |
2483 |
|
|
2185 |
– |
checkCompletedNormally(f, v1); |
2186 |
– |
checkCompletedNormally(g, v2); |
2187 |
– |
checkCompletedNormally(h, null); |
2188 |
– |
assertEquals(1, r.invocationCount); |
2189 |
– |
}} |
2190 |
– |
|
2191 |
– |
public void testRunAfterEither_normalCompletion3() { |
2192 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2193 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2194 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2195 |
– |
{ |
2196 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2197 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2198 |
– |
final Noop r = new Noop(); |
2199 |
– |
|
2200 |
– |
f.complete(v1); |
2484 |
|
g.complete(v2); |
2202 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2485 |
|
|
2486 |
< |
checkCompletedNormally(h, null); |
2486 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2487 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2488 |
> |
|
2489 |
|
checkCompletedNormally(f, v1); |
2490 |
|
checkCompletedNormally(g, v2); |
2491 |
< |
assertEquals(1, r.invocationCount); |
2491 |
> |
checkCompletedNormally(h0, null); |
2492 |
> |
checkCompletedNormally(h1, null); |
2493 |
> |
checkCompletedNormally(h2, null); |
2494 |
> |
checkCompletedNormally(h3, null); |
2495 |
> |
checkCompletedNormally(h4, null); |
2496 |
> |
checkCompletedNormally(h5, null); |
2497 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2498 |
|
}} |
2499 |
|
|
2500 |
|
/** |
2501 |
|
* runAfterEither result completes exceptionally after exceptional |
2502 |
|
* completion of either source |
2503 |
|
*/ |
2504 |
< |
public void testRunAfterEither_exceptionalCompletion1() { |
2504 |
> |
public void testRunAfterEither_exceptionalCompletion() { |
2505 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2506 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2507 |
|
{ |
2508 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2509 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2220 |
– |
final Noop r = new Noop(); |
2221 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2510 |
|
final CFException ex = new CFException(); |
2511 |
+ |
final Noop[] rs = new Noop[6]; |
2512 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2513 |
|
|
2514 |
+ |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2515 |
+ |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2516 |
+ |
checkIncomplete(h0); |
2517 |
+ |
checkIncomplete(h1); |
2518 |
+ |
rs[0].assertNotInvoked(); |
2519 |
+ |
rs[1].assertNotInvoked(); |
2520 |
|
f.completeExceptionally(ex); |
2521 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2521 |
> |
checkCompletedWithWrappedException(h0, ex); |
2522 |
> |
checkCompletedWithWrappedException(h1, ex); |
2523 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2524 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2525 |
> |
checkCompletedWithWrappedException(h2, ex); |
2526 |
> |
checkCompletedWithWrappedException(h3, ex); |
2527 |
> |
|
2528 |
|
g.complete(v1); |
2529 |
|
|
2530 |
< |
assertEquals(0, r.invocationCount); |
2530 |
> |
// unspecified behavior - both source completions available |
2531 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2532 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2533 |
> |
try { |
2534 |
> |
assertNull(h4.join()); |
2535 |
> |
rs[4].assertInvoked(); |
2536 |
> |
} catch (CompletionException ok) { |
2537 |
> |
checkCompletedWithWrappedException(h4, ex); |
2538 |
> |
rs[4].assertNotInvoked(); |
2539 |
> |
} |
2540 |
> |
try { |
2541 |
> |
assertNull(h5.join()); |
2542 |
> |
rs[5].assertInvoked(); |
2543 |
> |
} catch (CompletionException ok) { |
2544 |
> |
checkCompletedWithWrappedException(h5, ex); |
2545 |
> |
rs[5].assertNotInvoked(); |
2546 |
> |
} |
2547 |
> |
|
2548 |
> |
checkCompletedExceptionally(f, ex); |
2549 |
|
checkCompletedNormally(g, v1); |
2550 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2551 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2550 |
> |
checkCompletedWithWrappedException(h0, ex); |
2551 |
> |
checkCompletedWithWrappedException(h1, ex); |
2552 |
> |
checkCompletedWithWrappedException(h2, ex); |
2553 |
> |
checkCompletedWithWrappedException(h3, ex); |
2554 |
> |
checkCompletedWithWrappedException(h4, ex); |
2555 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2556 |
|
}} |
2557 |
|
|
2558 |
|
public void testRunAfterEither_exceptionalCompletion2() { |
2559 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2560 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2561 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2562 |
|
{ |
2563 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2564 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2240 |
– |
final Noop r = new Noop(); |
2241 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2242 |
– |
final CFException ex = new CFException(); |
2243 |
– |
|
2244 |
– |
g.completeExceptionally(ex); |
2245 |
– |
checkCompletedWithWrappedCFException(h, ex); |
2246 |
– |
f.complete(v1); |
2247 |
– |
|
2248 |
– |
assertEquals(0, r.invocationCount); |
2249 |
– |
checkCompletedNormally(f, v1); |
2250 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2251 |
– |
checkCompletedWithWrappedCFException(h, ex); |
2252 |
– |
}} |
2253 |
– |
|
2254 |
– |
public void testRunAfterEither_exceptionalCompletion3() { |
2255 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2256 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2257 |
– |
{ |
2258 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2259 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2260 |
– |
final Noop r = new Noop(); |
2565 |
|
final CFException ex = new CFException(); |
2566 |
+ |
final Noop[] rs = new Noop[6]; |
2567 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2568 |
|
|
2569 |
< |
g.completeExceptionally(ex); |
2570 |
< |
f.complete(v1); |
2571 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2569 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2570 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2571 |
> |
if (fFirst) { |
2572 |
> |
f.complete(v1); |
2573 |
> |
g.completeExceptionally(ex); |
2574 |
> |
} else { |
2575 |
> |
g.completeExceptionally(ex); |
2576 |
> |
f.complete(v1); |
2577 |
> |
} |
2578 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2579 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2580 |
|
|
2581 |
< |
// unspecified behavior |
2582 |
< |
Integer v; |
2581 |
> |
// unspecified behavior - both source completions available |
2582 |
> |
try { |
2583 |
> |
assertEquals(null, h0.join()); |
2584 |
> |
rs[0].assertInvoked(); |
2585 |
> |
} catch (CompletionException ok) { |
2586 |
> |
checkCompletedWithWrappedException(h0, ex); |
2587 |
> |
rs[0].assertNotInvoked(); |
2588 |
> |
} |
2589 |
> |
try { |
2590 |
> |
assertEquals(null, h1.join()); |
2591 |
> |
rs[1].assertInvoked(); |
2592 |
> |
} catch (CompletionException ok) { |
2593 |
> |
checkCompletedWithWrappedException(h1, ex); |
2594 |
> |
rs[1].assertNotInvoked(); |
2595 |
> |
} |
2596 |
> |
try { |
2597 |
> |
assertEquals(null, h2.join()); |
2598 |
> |
rs[2].assertInvoked(); |
2599 |
> |
} catch (CompletionException ok) { |
2600 |
> |
checkCompletedWithWrappedException(h2, ex); |
2601 |
> |
rs[2].assertNotInvoked(); |
2602 |
> |
} |
2603 |
|
try { |
2604 |
< |
assertNull(h.join()); |
2605 |
< |
assertEquals(1, r.invocationCount); |
2604 |
> |
assertEquals(null, h3.join()); |
2605 |
> |
rs[3].assertInvoked(); |
2606 |
|
} catch (CompletionException ok) { |
2607 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2608 |
< |
assertEquals(0, r.invocationCount); |
2607 |
> |
checkCompletedWithWrappedException(h3, ex); |
2608 |
> |
rs[3].assertNotInvoked(); |
2609 |
|
} |
2610 |
|
|
2277 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2611 |
|
checkCompletedNormally(f, v1); |
2612 |
+ |
checkCompletedExceptionally(g, ex); |
2613 |
|
}} |
2614 |
|
|
2615 |
< |
public void testRunAfterEither_exceptionalCompletion4() { |
2615 |
> |
/** |
2616 |
> |
* runAfterEither result completes exceptionally if either source cancelled |
2617 |
> |
*/ |
2618 |
> |
public void testRunAfterEither_sourceCancelled() { |
2619 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2620 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2621 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2622 |
|
{ |
2623 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2624 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2625 |
< |
final Noop r = new Noop(); |
2626 |
< |
final CFException ex = new CFException(); |
2625 |
> |
final Noop[] rs = new Noop[6]; |
2626 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2627 |
> |
|
2628 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2629 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2630 |
> |
checkIncomplete(h0); |
2631 |
> |
checkIncomplete(h1); |
2632 |
> |
rs[0].assertNotInvoked(); |
2633 |
> |
rs[1].assertNotInvoked(); |
2634 |
> |
f.cancel(mayInterruptIfRunning); |
2635 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2636 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2637 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2638 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2639 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2640 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2641 |
|
|
2290 |
– |
f.completeExceptionally(ex); |
2642 |
|
g.complete(v1); |
2292 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2643 |
|
|
2644 |
< |
// unspecified behavior |
2645 |
< |
Integer v; |
2644 |
> |
// unspecified behavior - both source completions available |
2645 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2646 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2647 |
> |
try { |
2648 |
> |
assertNull(h4.join()); |
2649 |
> |
rs[4].assertInvoked(); |
2650 |
> |
} catch (CompletionException ok) { |
2651 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2652 |
> |
rs[4].assertNotInvoked(); |
2653 |
> |
} |
2654 |
|
try { |
2655 |
< |
assertNull(h.join()); |
2656 |
< |
assertEquals(1, r.invocationCount); |
2655 |
> |
assertNull(h5.join()); |
2656 |
> |
rs[5].assertInvoked(); |
2657 |
|
} catch (CompletionException ok) { |
2658 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2659 |
< |
assertEquals(0, r.invocationCount); |
2658 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2659 |
> |
rs[5].assertNotInvoked(); |
2660 |
|
} |
2661 |
|
|
2662 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2662 |
> |
checkCancelled(f); |
2663 |
|
checkCompletedNormally(g, v1); |
2664 |
+ |
checkCompletedWithWrappedCancellationException(h0); |
2665 |
+ |
checkCompletedWithWrappedCancellationException(h1); |
2666 |
+ |
checkCompletedWithWrappedCancellationException(h2); |
2667 |
+ |
checkCompletedWithWrappedCancellationException(h3); |
2668 |
+ |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2669 |
|
}} |
2670 |
|
|
2671 |
|
/** |
2672 |
|
* runAfterEither result completes exceptionally if action does |
2673 |
|
*/ |
2674 |
< |
public void testRunAfterEither_actionFailed1() { |
2674 |
> |
public void testRunAfterEither_actionFailed() { |
2675 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2676 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2677 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2678 |
|
{ |
2679 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2680 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2681 |
< |
final FailingRunnable r = new FailingRunnable(); |
2682 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2681 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
2682 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
2683 |
|
|
2684 |
+ |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2685 |
+ |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2686 |
|
f.complete(v1); |
2687 |
< |
checkCompletedWithWrappedCFException(h); |
2687 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2688 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2689 |
> |
checkCompletedWithWrappedCFException(h0); |
2690 |
> |
checkCompletedWithWrappedCFException(h1); |
2691 |
> |
checkCompletedWithWrappedCFException(h2); |
2692 |
> |
checkCompletedWithWrappedCFException(h3); |
2693 |
> |
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2694 |
|
g.complete(v2); |
2695 |
< |
checkCompletedNormally(f, v1); |
2696 |
< |
checkCompletedNormally(g, v2); |
2697 |
< |
}} |
2698 |
< |
|
2328 |
< |
public void testRunAfterEither_actionFailed2() { |
2329 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2330 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2331 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
2332 |
< |
{ |
2333 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2334 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2335 |
< |
final FailingRunnable r = new FailingRunnable(); |
2336 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2695 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2696 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2697 |
> |
checkCompletedWithWrappedCFException(h4); |
2698 |
> |
checkCompletedWithWrappedCFException(h5); |
2699 |
|
|
2338 |
– |
g.complete(v2); |
2339 |
– |
checkCompletedWithWrappedCFException(h); |
2340 |
– |
f.complete(v1); |
2700 |
|
checkCompletedNormally(f, v1); |
2701 |
|
checkCompletedNormally(g, v2); |
2702 |
< |
}} |
2344 |
< |
|
2345 |
< |
/** |
2346 |
< |
* runAfterEither result completes exceptionally if either source cancelled |
2347 |
< |
*/ |
2348 |
< |
public void testRunAfterEither_sourceCancelled1() { |
2349 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2350 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2351 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2352 |
< |
{ |
2353 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2354 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2355 |
< |
final Noop r = new Noop(); |
2356 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2357 |
< |
|
2358 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2359 |
< |
checkCompletedWithWrappedCancellationException(h); |
2360 |
< |
g.complete(v1); |
2361 |
< |
|
2362 |
< |
checkCancelled(f); |
2363 |
< |
assertEquals(0, r.invocationCount); |
2364 |
< |
checkCompletedNormally(g, v1); |
2365 |
< |
checkCompletedWithWrappedCancellationException(h); |
2366 |
< |
}} |
2367 |
< |
|
2368 |
< |
public void testRunAfterEither_sourceCancelled2() { |
2369 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2370 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2371 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2372 |
< |
{ |
2373 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2374 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2375 |
< |
final Noop r = new Noop(); |
2376 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2377 |
< |
|
2378 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2379 |
< |
checkCompletedWithWrappedCancellationException(h); |
2380 |
< |
f.complete(v1); |
2381 |
< |
|
2382 |
< |
checkCancelled(g); |
2383 |
< |
assertEquals(0, r.invocationCount); |
2384 |
< |
checkCompletedNormally(f, v1); |
2385 |
< |
checkCompletedWithWrappedCancellationException(h); |
2386 |
< |
}} |
2387 |
< |
|
2388 |
< |
public void testRunAfterEither_sourceCancelled3() { |
2389 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2390 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2391 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2392 |
< |
{ |
2393 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2394 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2395 |
< |
final Noop r = new Noop(); |
2396 |
< |
|
2397 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2398 |
< |
f.complete(v1); |
2399 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2400 |
< |
|
2401 |
< |
// unspecified behavior |
2402 |
< |
Integer v; |
2403 |
< |
try { |
2404 |
< |
assertNull(h.join()); |
2405 |
< |
assertEquals(1, r.invocationCount); |
2406 |
< |
} catch (CompletionException ok) { |
2407 |
< |
checkCompletedWithWrappedCancellationException(h); |
2408 |
< |
assertEquals(0, r.invocationCount); |
2409 |
< |
} |
2410 |
< |
|
2411 |
< |
checkCancelled(g); |
2412 |
< |
checkCompletedNormally(f, v1); |
2413 |
< |
}} |
2414 |
< |
|
2415 |
< |
public void testRunAfterEither_sourceCancelled4() { |
2416 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2417 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2418 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2419 |
< |
{ |
2420 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2421 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2422 |
< |
final Noop r = new Noop(); |
2423 |
< |
|
2424 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2425 |
< |
g.complete(v1); |
2426 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2427 |
< |
|
2428 |
< |
// unspecified behavior |
2429 |
< |
Integer v; |
2430 |
< |
try { |
2431 |
< |
assertNull(h.join()); |
2432 |
< |
assertEquals(1, r.invocationCount); |
2433 |
< |
} catch (CompletionException ok) { |
2434 |
< |
checkCompletedWithWrappedCancellationException(h); |
2435 |
< |
assertEquals(0, r.invocationCount); |
2436 |
< |
} |
2437 |
< |
|
2438 |
< |
checkCancelled(f); |
2439 |
< |
checkCompletedNormally(g, v1); |
2702 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2703 |
|
}} |
2704 |
|
|
2705 |
|
/** |
2711 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2712 |
|
{ |
2713 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2714 |
< |
final CompletableFutureInc r = new CompletableFutureInc(); |
2714 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2715 |
|
if (!createIncomplete) f.complete(v1); |
2716 |
< |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2716 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2717 |
|
if (createIncomplete) f.complete(v1); |
2718 |
|
|
2719 |
|
checkCompletedNormally(g, inc(v1)); |
2720 |
|
checkCompletedNormally(f, v1); |
2721 |
< |
assertEquals(1, r.invocationCount); |
2721 |
> |
r.assertValue(v1); |
2722 |
|
}} |
2723 |
|
|
2724 |
|
/** |
2730 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
2731 |
|
{ |
2732 |
|
final CFException ex = new CFException(); |
2733 |
< |
final CompletableFutureInc r = new CompletableFutureInc(); |
2733 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2734 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2735 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
2736 |
< |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2736 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2737 |
|
if (createIncomplete) f.completeExceptionally(ex); |
2738 |
|
|
2739 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2740 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2741 |
< |
assertEquals(0, r.invocationCount); |
2739 |
> |
checkCompletedWithWrappedException(g, ex); |
2740 |
> |
checkCompletedExceptionally(f, ex); |
2741 |
> |
r.assertNotInvoked(); |
2742 |
|
}} |
2743 |
|
|
2744 |
|
/** |
2751 |
|
{ |
2752 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2753 |
|
final FailingCompletableFutureFunction r |
2754 |
< |
= new FailingCompletableFutureFunction(); |
2754 |
> |
= new FailingCompletableFutureFunction(m); |
2755 |
|
if (!createIncomplete) f.complete(v1); |
2756 |
< |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2756 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2757 |
|
if (createIncomplete) f.complete(v1); |
2758 |
|
|
2759 |
|
checkCompletedWithWrappedCFException(g); |
2769 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2770 |
|
{ |
2771 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2772 |
< |
final CompletableFutureInc r = new CompletableFutureInc(); |
2772 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2773 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2774 |
< |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2774 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2775 |
|
if (createIncomplete) { |
2776 |
|
checkIncomplete(g); |
2777 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
2798 |
|
*/ |
2799 |
|
public void testAllOf_normal() throws Exception { |
2800 |
|
for (int k = 1; k < 20; ++k) { |
2801 |
< |
CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2801 |
> |
CompletableFuture<Integer>[] fs |
2802 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2803 |
|
for (int i = 0; i < k; ++i) |
2804 |
|
fs[i] = new CompletableFuture<>(); |
2805 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2813 |
|
} |
2814 |
|
} |
2815 |
|
|
2816 |
+ |
public void testAllOf_backwards() throws Exception { |
2817 |
+ |
for (int k = 1; k < 20; ++k) { |
2818 |
+ |
CompletableFuture<Integer>[] fs |
2819 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2820 |
+ |
for (int i = 0; i < k; ++i) |
2821 |
+ |
fs[i] = new CompletableFuture<>(); |
2822 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2823 |
+ |
for (int i = k - 1; i >= 0; i--) { |
2824 |
+ |
checkIncomplete(f); |
2825 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
2826 |
+ |
fs[i].complete(one); |
2827 |
+ |
} |
2828 |
+ |
checkCompletedNormally(f, null); |
2829 |
+ |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
2830 |
+ |
} |
2831 |
+ |
} |
2832 |
+ |
|
2833 |
|
/** |
2834 |
|
* anyOf(no component futures) returns an incomplete future |
2835 |
|
*/ |
2888 |
|
Runnable[] throwingActions = { |
2889 |
|
() -> CompletableFuture.supplyAsync(null), |
2890 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
2891 |
< |
() -> CompletableFuture.supplyAsync(supplyOne, null), |
2891 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
2892 |
|
|
2893 |
|
() -> CompletableFuture.runAsync(null), |
2894 |
|
() -> CompletableFuture.runAsync(null, exec), |
2961 |
|
|
2962 |
|
() -> f.thenCompose(null), |
2963 |
|
() -> f.thenComposeAsync(null), |
2964 |
< |
() -> f.thenComposeAsync(new CompletableFutureInc(), null), |
2964 |
> |
() -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null), |
2965 |
|
() -> f.thenComposeAsync(null, exec), |
2966 |
|
|
2967 |
|
() -> f.exceptionally(null), |
2993 |
|
assertSame(f, f.toCompletableFuture()); |
2994 |
|
} |
2995 |
|
|
2715 |
– |
/** |
2716 |
– |
* whenComplete action executes on normal completion, propagating |
2717 |
– |
* source result. |
2718 |
– |
*/ |
2719 |
– |
public void testWhenComplete_normalCompletion1() { |
2720 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2721 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2722 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2723 |
– |
{ |
2724 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2725 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2726 |
– |
if (!createIncomplete) f.complete(v1); |
2727 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2728 |
– |
(f, |
2729 |
– |
(Integer x, Throwable t) -> { |
2730 |
– |
threadAssertSame(x, v1); |
2731 |
– |
threadAssertNull(t); |
2732 |
– |
a.getAndIncrement(); |
2733 |
– |
}); |
2734 |
– |
if (createIncomplete) f.complete(v1); |
2735 |
– |
|
2736 |
– |
checkCompletedNormally(g, v1); |
2737 |
– |
checkCompletedNormally(f, v1); |
2738 |
– |
assertEquals(1, a.get()); |
2739 |
– |
}} |
2740 |
– |
|
2741 |
– |
/** |
2742 |
– |
* whenComplete action executes on exceptional completion, propagating |
2743 |
– |
* source result. |
2744 |
– |
*/ |
2745 |
– |
public void testWhenComplete_exceptionalCompletion() { |
2746 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2747 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2748 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2749 |
– |
{ |
2750 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2751 |
– |
final CFException ex = new CFException(); |
2752 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2753 |
– |
if (!createIncomplete) f.completeExceptionally(ex); |
2754 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2755 |
– |
(f, |
2756 |
– |
(Integer x, Throwable t) -> { |
2757 |
– |
threadAssertNull(x); |
2758 |
– |
threadAssertSame(t, ex); |
2759 |
– |
a.getAndIncrement(); |
2760 |
– |
}); |
2761 |
– |
if (createIncomplete) f.completeExceptionally(ex); |
2762 |
– |
checkCompletedWithWrappedCFException(f, ex); |
2763 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2764 |
– |
assertEquals(1, a.get()); |
2765 |
– |
}} |
2766 |
– |
|
2767 |
– |
/** |
2768 |
– |
* whenComplete action executes on cancelled source, propagating |
2769 |
– |
* CancellationException. |
2770 |
– |
*/ |
2771 |
– |
public void testWhenComplete_sourceCancelled() { |
2772 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2773 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2774 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2775 |
– |
{ |
2776 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2777 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2778 |
– |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2779 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2780 |
– |
(f, |
2781 |
– |
(Integer x, Throwable t) -> { |
2782 |
– |
threadAssertNull(x); |
2783 |
– |
threadAssertTrue(t instanceof CancellationException); |
2784 |
– |
a.getAndIncrement(); |
2785 |
– |
}); |
2786 |
– |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2787 |
– |
|
2788 |
– |
//try { g.join(); } catch (Throwable t) { throw new Error(t); } |
2789 |
– |
checkCompletedWithWrappedCancellationException(g); |
2790 |
– |
checkCancelled(f); |
2791 |
– |
assertEquals(1, a.get()); |
2792 |
– |
}} |
2793 |
– |
|
2794 |
– |
/** |
2795 |
– |
* If a whenComplete action throws an exception when triggered by |
2796 |
– |
* a normal completion, it completes exceptionally |
2797 |
– |
*/ |
2798 |
– |
public void testWhenComplete_actionFailed() { |
2799 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2800 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2801 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2802 |
– |
{ |
2803 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2804 |
– |
final CFException ex = new CFException(); |
2805 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2806 |
– |
if (!createIncomplete) f.complete(v1); |
2807 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2808 |
– |
(f, |
2809 |
– |
(Integer x, Throwable t) -> { |
2810 |
– |
threadAssertSame(x, v1); |
2811 |
– |
threadAssertNull(t); |
2812 |
– |
a.getAndIncrement(); |
2813 |
– |
throw ex; |
2814 |
– |
}); |
2815 |
– |
if (createIncomplete) f.complete(v1); |
2816 |
– |
checkCompletedNormally(f, v1); |
2817 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2818 |
– |
assertEquals(1, a.get()); |
2819 |
– |
}} |
2820 |
– |
|
2821 |
– |
/** |
2822 |
– |
* If a whenComplete action throws an exception when triggered by |
2823 |
– |
* a source completion that also throws an exception, the source |
2824 |
– |
* exception takes precedence. |
2825 |
– |
*/ |
2826 |
– |
public void testWhenComplete_actionFailedSourceFailed() { |
2827 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2828 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2829 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2830 |
– |
{ |
2831 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2832 |
– |
final CFException ex1 = new CFException(); |
2833 |
– |
final CFException ex2 = new CFException(); |
2834 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2835 |
– |
|
2836 |
– |
if (!createIncomplete) f.completeExceptionally(ex1); |
2837 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2838 |
– |
(f, |
2839 |
– |
(Integer x, Throwable t) -> { |
2840 |
– |
threadAssertSame(t, ex1); |
2841 |
– |
threadAssertNull(x); |
2842 |
– |
a.getAndIncrement(); |
2843 |
– |
throw ex2; |
2844 |
– |
}); |
2845 |
– |
if (createIncomplete) f.completeExceptionally(ex1); |
2846 |
– |
|
2847 |
– |
checkCompletedWithWrappedCFException(f, ex1); |
2848 |
– |
checkCompletedWithWrappedCFException(g, ex1); |
2849 |
– |
assertEquals(1, a.get()); |
2850 |
– |
}} |
2851 |
– |
|
2996 |
|
} |