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 |
345 |
|
f = new CompletableFuture<String>(); |
346 |
|
f.completeExceptionally(new IndexOutOfBoundsException()); |
347 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
348 |
+ |
|
349 |
+ |
f = new CompletableFuture<String>(); |
350 |
+ |
f.cancel(true); |
351 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
352 |
+ |
|
353 |
+ |
f = new CompletableFuture<String>(); |
354 |
+ |
f.cancel(false); |
355 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
356 |
|
} |
357 |
|
|
358 |
|
/** |
363 |
|
checkCompletedNormally(f, "test"); |
364 |
|
} |
365 |
|
|
366 |
< |
static final class IntegerSupplier implements Supplier<Integer> { |
324 |
< |
final ExecutionMode m; |
366 |
> |
abstract class CheckedAction { |
367 |
|
int invocationCount = 0; |
368 |
+ |
final ExecutionMode m; |
369 |
+ |
CheckedAction(ExecutionMode m) { this.m = m; } |
370 |
+ |
void invoked() { |
371 |
+ |
m.checkExecutionMode(); |
372 |
+ |
assertEquals(0, invocationCount++); |
373 |
+ |
} |
374 |
+ |
void assertNotInvoked() { assertEquals(0, invocationCount); } |
375 |
+ |
void assertInvoked() { assertEquals(1, invocationCount); } |
376 |
+ |
} |
377 |
+ |
|
378 |
+ |
abstract class CheckedIntegerAction extends CheckedAction { |
379 |
+ |
Integer value; |
380 |
+ |
CheckedIntegerAction(ExecutionMode m) { super(m); } |
381 |
+ |
void assertValue(Integer expected) { |
382 |
+ |
assertInvoked(); |
383 |
+ |
assertEquals(expected, value); |
384 |
+ |
} |
385 |
+ |
} |
386 |
+ |
|
387 |
+ |
class IntegerSupplier extends CheckedAction |
388 |
+ |
implements Supplier<Integer> |
389 |
+ |
{ |
390 |
|
final Integer value; |
391 |
|
IntegerSupplier(ExecutionMode m, Integer value) { |
392 |
< |
this.m = m; |
392 |
> |
super(m); |
393 |
|
this.value = value; |
394 |
|
} |
395 |
|
public Integer get() { |
396 |
< |
m.checkExecutionMode(); |
333 |
< |
invocationCount++; |
396 |
> |
invoked(); |
397 |
|
return value; |
398 |
|
} |
399 |
|
} |
403 |
|
return (x == null) ? null : x + 1; |
404 |
|
} |
405 |
|
|
406 |
< |
static final class IncAction implements Consumer<Integer> { |
407 |
< |
int invocationCount = 0; |
408 |
< |
Integer value; |
406 |
> |
class NoopConsumer extends CheckedIntegerAction |
407 |
> |
implements Consumer<Integer> |
408 |
> |
{ |
409 |
> |
NoopConsumer(ExecutionMode m) { super(m); } |
410 |
|
public void accept(Integer x) { |
411 |
< |
invocationCount++; |
412 |
< |
value = inc(x); |
411 |
> |
invoked(); |
412 |
> |
value = x; |
413 |
|
} |
414 |
|
} |
415 |
< |
static final class IncFunction implements Function<Integer,Integer> { |
416 |
< |
final ExecutionMode m; |
417 |
< |
int invocationCount = 0; |
418 |
< |
Integer value; |
419 |
< |
IncFunction(ExecutionMode m) { this.m = m; } |
415 |
> |
|
416 |
> |
class IncFunction extends CheckedIntegerAction |
417 |
> |
implements Function<Integer,Integer> |
418 |
> |
{ |
419 |
> |
IncFunction(ExecutionMode m) { super(m); } |
420 |
|
public Integer apply(Integer x) { |
421 |
< |
m.checkExecutionMode(); |
358 |
< |
invocationCount++; |
421 |
> |
invoked(); |
422 |
|
return value = inc(x); |
423 |
|
} |
424 |
|
} |
431 |
|
- ((y == null) ? 99 : y.intValue()); |
432 |
|
} |
433 |
|
|
434 |
< |
static final class SubtractAction implements BiConsumer<Integer, Integer> { |
435 |
< |
final ExecutionMode m; |
436 |
< |
int invocationCount = 0; |
437 |
< |
Integer value; |
375 |
< |
// Check this action was invoked exactly once when result is computed. |
376 |
< |
SubtractAction(ExecutionMode m) { this.m = m; } |
434 |
> |
class SubtractAction extends CheckedIntegerAction |
435 |
> |
implements BiConsumer<Integer, Integer> |
436 |
> |
{ |
437 |
> |
SubtractAction(ExecutionMode m) { super(m); } |
438 |
|
public void accept(Integer x, Integer y) { |
439 |
< |
m.checkExecutionMode(); |
379 |
< |
invocationCount++; |
439 |
> |
invoked(); |
440 |
|
value = subtract(x, y); |
441 |
|
} |
442 |
|
} |
443 |
< |
static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> { |
444 |
< |
final ExecutionMode m; |
445 |
< |
int invocationCount = 0; |
446 |
< |
Integer value; |
447 |
< |
// Check this action was invoked exactly once when result is computed. |
388 |
< |
SubtractFunction(ExecutionMode m) { this.m = m; } |
443 |
> |
|
444 |
> |
class SubtractFunction extends CheckedIntegerAction |
445 |
> |
implements BiFunction<Integer, Integer, Integer> |
446 |
> |
{ |
447 |
> |
SubtractFunction(ExecutionMode m) { super(m); } |
448 |
|
public Integer apply(Integer x, Integer y) { |
449 |
< |
m.checkExecutionMode(); |
391 |
< |
invocationCount++; |
449 |
> |
invoked(); |
450 |
|
return value = subtract(x, y); |
451 |
|
} |
452 |
|
} |
453 |
|
|
454 |
< |
static final class Noop implements Runnable { |
455 |
< |
final ExecutionMode m; |
398 |
< |
int invocationCount = 0; |
399 |
< |
Noop(ExecutionMode m) { this.m = m; } |
454 |
> |
class Noop extends CheckedAction implements Runnable { |
455 |
> |
Noop(ExecutionMode m) { super(m); } |
456 |
|
public void run() { |
457 |
< |
m.checkExecutionMode(); |
402 |
< |
invocationCount++; |
457 |
> |
invoked(); |
458 |
|
} |
459 |
|
} |
460 |
|
|
461 |
< |
static final class FailingSupplier implements Supplier<Integer> { |
462 |
< |
final ExecutionMode m; |
463 |
< |
int invocationCount = 0; |
464 |
< |
FailingSupplier(ExecutionMode m) { this.m = m; } |
461 |
> |
class FailingSupplier extends CheckedAction |
462 |
> |
implements Supplier<Integer> |
463 |
> |
{ |
464 |
> |
FailingSupplier(ExecutionMode m) { super(m); } |
465 |
|
public Integer get() { |
466 |
< |
m.checkExecutionMode(); |
412 |
< |
invocationCount++; |
466 |
> |
invoked(); |
467 |
|
throw new CFException(); |
468 |
|
} |
469 |
|
} |
470 |
< |
static final class FailingConsumer implements Consumer<Integer> { |
471 |
< |
final ExecutionMode m; |
472 |
< |
int invocationCount = 0; |
473 |
< |
FailingConsumer(ExecutionMode m) { this.m = m; } |
470 |
> |
|
471 |
> |
class FailingConsumer extends CheckedIntegerAction |
472 |
> |
implements Consumer<Integer> |
473 |
> |
{ |
474 |
> |
FailingConsumer(ExecutionMode m) { super(m); } |
475 |
|
public void accept(Integer x) { |
476 |
< |
m.checkExecutionMode(); |
477 |
< |
invocationCount++; |
476 |
> |
invoked(); |
477 |
> |
value = x; |
478 |
|
throw new CFException(); |
479 |
|
} |
480 |
|
} |
481 |
< |
static final class FailingBiConsumer implements BiConsumer<Integer, Integer> { |
482 |
< |
final ExecutionMode m; |
483 |
< |
int invocationCount = 0; |
484 |
< |
FailingBiConsumer(ExecutionMode m) { this.m = m; } |
481 |
> |
|
482 |
> |
class FailingBiConsumer extends CheckedIntegerAction |
483 |
> |
implements BiConsumer<Integer, Integer> |
484 |
> |
{ |
485 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); } |
486 |
|
public void accept(Integer x, Integer y) { |
487 |
< |
m.checkExecutionMode(); |
488 |
< |
invocationCount++; |
487 |
> |
invoked(); |
488 |
> |
value = subtract(x, y); |
489 |
|
throw new CFException(); |
490 |
|
} |
491 |
|
} |
492 |
< |
static final class FailingFunction implements Function<Integer, Integer> { |
493 |
< |
final ExecutionMode m; |
494 |
< |
int invocationCount = 0; |
495 |
< |
FailingFunction(ExecutionMode m) { this.m = m; } |
492 |
> |
|
493 |
> |
class FailingFunction extends CheckedIntegerAction |
494 |
> |
implements Function<Integer, Integer> |
495 |
> |
{ |
496 |
> |
FailingFunction(ExecutionMode m) { super(m); } |
497 |
|
public Integer apply(Integer x) { |
498 |
< |
m.checkExecutionMode(); |
499 |
< |
invocationCount++; |
498 |
> |
invoked(); |
499 |
> |
value = x; |
500 |
|
throw new CFException(); |
501 |
|
} |
502 |
|
} |
503 |
< |
static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> { |
504 |
< |
final ExecutionMode m; |
505 |
< |
int invocationCount = 0; |
506 |
< |
FailingBiFunction(ExecutionMode m) { this.m = m; } |
503 |
> |
|
504 |
> |
class FailingBiFunction extends CheckedIntegerAction |
505 |
> |
implements BiFunction<Integer, Integer, Integer> |
506 |
> |
{ |
507 |
> |
FailingBiFunction(ExecutionMode m) { super(m); } |
508 |
|
public Integer apply(Integer x, Integer y) { |
509 |
< |
m.checkExecutionMode(); |
510 |
< |
invocationCount++; |
509 |
> |
invoked(); |
510 |
> |
value = subtract(x, y); |
511 |
|
throw new CFException(); |
512 |
|
} |
513 |
|
} |
514 |
< |
static final class FailingRunnable implements Runnable { |
515 |
< |
final ExecutionMode m; |
516 |
< |
int invocationCount = 0; |
459 |
< |
FailingRunnable(ExecutionMode m) { this.m = m; } |
514 |
> |
|
515 |
> |
class FailingRunnable extends CheckedAction implements Runnable { |
516 |
> |
FailingRunnable(ExecutionMode m) { super(m); } |
517 |
|
public void run() { |
518 |
< |
m.checkExecutionMode(); |
462 |
< |
invocationCount++; |
518 |
> |
invoked(); |
519 |
|
throw new CFException(); |
520 |
|
} |
521 |
|
} |
522 |
|
|
523 |
< |
static final class CompletableFutureInc |
524 |
< |
implements Function<Integer, CompletableFuture<Integer>> { |
525 |
< |
final ExecutionMode m; |
526 |
< |
int invocationCount = 0; |
527 |
< |
CompletableFutureInc(ExecutionMode m) { this.m = m; } |
523 |
> |
|
524 |
> |
class CompletableFutureInc extends CheckedIntegerAction |
525 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
526 |
> |
{ |
527 |
> |
CompletableFutureInc(ExecutionMode m) { super(m); } |
528 |
|
public CompletableFuture<Integer> apply(Integer x) { |
529 |
< |
m.checkExecutionMode(); |
530 |
< |
invocationCount++; |
529 |
> |
invoked(); |
530 |
> |
value = x; |
531 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
532 |
|
f.complete(inc(x)); |
533 |
|
return f; |
534 |
|
} |
535 |
|
} |
536 |
|
|
537 |
< |
static final class FailingCompletableFutureFunction |
538 |
< |
implements Function<Integer, CompletableFuture<Integer>> { |
539 |
< |
final ExecutionMode m; |
540 |
< |
int invocationCount = 0; |
485 |
< |
FailingCompletableFutureFunction(ExecutionMode m) { this.m = m; } |
537 |
> |
class FailingCompletableFutureFunction extends CheckedIntegerAction |
538 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
539 |
> |
{ |
540 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); } |
541 |
|
public CompletableFuture<Integer> apply(Integer x) { |
542 |
< |
m.checkExecutionMode(); |
543 |
< |
invocationCount++; |
542 |
> |
invoked(); |
543 |
> |
value = x; |
544 |
|
throw new CFException(); |
545 |
|
} |
546 |
|
} |
851 |
|
assertEquals(0, a.get()); |
852 |
|
}} |
853 |
|
|
799 |
– |
|
854 |
|
/** |
855 |
|
* exceptionally action completes with function value on source |
856 |
|
* exception |
894 |
|
}); |
895 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
896 |
|
|
897 |
< |
checkCompletedWithWrappedCFException(g, ex2); |
897 |
> |
checkCompletedWithWrappedException(g, ex2); |
898 |
> |
assertEquals(1, a.get()); |
899 |
> |
}} |
900 |
> |
|
901 |
> |
/** |
902 |
> |
* whenComplete action executes on normal completion, propagating |
903 |
> |
* source result. |
904 |
> |
*/ |
905 |
> |
public void testWhenComplete_normalCompletion1() { |
906 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
907 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
908 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
909 |
> |
{ |
910 |
> |
final AtomicInteger a = new AtomicInteger(0); |
911 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
912 |
> |
if (!createIncomplete) f.complete(v1); |
913 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
914 |
> |
(f, |
915 |
> |
(Integer x, Throwable t) -> { |
916 |
> |
m.checkExecutionMode(); |
917 |
> |
threadAssertSame(x, v1); |
918 |
> |
threadAssertNull(t); |
919 |
> |
a.getAndIncrement(); |
920 |
> |
}); |
921 |
> |
if (createIncomplete) f.complete(v1); |
922 |
> |
|
923 |
> |
checkCompletedNormally(g, v1); |
924 |
> |
checkCompletedNormally(f, v1); |
925 |
> |
assertEquals(1, a.get()); |
926 |
> |
}} |
927 |
> |
|
928 |
> |
/** |
929 |
> |
* whenComplete action executes on exceptional completion, propagating |
930 |
> |
* source result. |
931 |
> |
*/ |
932 |
> |
public void testWhenComplete_exceptionalCompletion() { |
933 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
934 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
935 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
936 |
> |
{ |
937 |
> |
final AtomicInteger a = new AtomicInteger(0); |
938 |
> |
final CFException ex = new CFException(); |
939 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
940 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
941 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
942 |
> |
(f, |
943 |
> |
(Integer x, Throwable t) -> { |
944 |
> |
m.checkExecutionMode(); |
945 |
> |
threadAssertNull(x); |
946 |
> |
threadAssertSame(t, ex); |
947 |
> |
a.getAndIncrement(); |
948 |
> |
}); |
949 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
950 |
> |
|
951 |
> |
checkCompletedWithWrappedException(g, ex); |
952 |
> |
checkCompletedExceptionally(f, ex); |
953 |
> |
assertEquals(1, a.get()); |
954 |
> |
}} |
955 |
> |
|
956 |
> |
/** |
957 |
> |
* whenComplete action executes on cancelled source, propagating |
958 |
> |
* CancellationException. |
959 |
> |
*/ |
960 |
> |
public void testWhenComplete_sourceCancelled() { |
961 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
962 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
963 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
964 |
> |
{ |
965 |
> |
final AtomicInteger a = new AtomicInteger(0); |
966 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
967 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
968 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
969 |
> |
(f, |
970 |
> |
(Integer x, Throwable t) -> { |
971 |
> |
m.checkExecutionMode(); |
972 |
> |
threadAssertNull(x); |
973 |
> |
threadAssertTrue(t instanceof CancellationException); |
974 |
> |
a.getAndIncrement(); |
975 |
> |
}); |
976 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
977 |
> |
|
978 |
> |
checkCompletedWithWrappedCancellationException(g); |
979 |
> |
checkCancelled(f); |
980 |
> |
assertEquals(1, a.get()); |
981 |
> |
}} |
982 |
> |
|
983 |
> |
/** |
984 |
> |
* If a whenComplete action throws an exception when triggered by |
985 |
> |
* a normal completion, it completes exceptionally |
986 |
> |
*/ |
987 |
> |
public void testWhenComplete_actionFailed() { |
988 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
989 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
990 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
991 |
> |
{ |
992 |
> |
final AtomicInteger a = new AtomicInteger(0); |
993 |
> |
final CFException ex = new CFException(); |
994 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
995 |
> |
if (!createIncomplete) f.complete(v1); |
996 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
997 |
> |
(f, |
998 |
> |
(Integer x, Throwable t) -> { |
999 |
> |
m.checkExecutionMode(); |
1000 |
> |
threadAssertSame(x, v1); |
1001 |
> |
threadAssertNull(t); |
1002 |
> |
a.getAndIncrement(); |
1003 |
> |
throw ex; |
1004 |
> |
}); |
1005 |
> |
if (createIncomplete) f.complete(v1); |
1006 |
> |
|
1007 |
> |
checkCompletedWithWrappedException(g, ex); |
1008 |
> |
checkCompletedNormally(f, v1); |
1009 |
> |
assertEquals(1, a.get()); |
1010 |
> |
}} |
1011 |
> |
|
1012 |
> |
/** |
1013 |
> |
* If a whenComplete action throws an exception when triggered by |
1014 |
> |
* a source completion that also throws an exception, the source |
1015 |
> |
* exception takes precedence. |
1016 |
> |
*/ |
1017 |
> |
public void testWhenComplete_actionFailedSourceFailed() { |
1018 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1019 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1020 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1021 |
> |
{ |
1022 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1023 |
> |
final CFException ex1 = new CFException(); |
1024 |
> |
final CFException ex2 = new CFException(); |
1025 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1026 |
> |
|
1027 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1028 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1029 |
> |
(f, |
1030 |
> |
(Integer x, Throwable t) -> { |
1031 |
> |
m.checkExecutionMode(); |
1032 |
> |
threadAssertSame(t, ex1); |
1033 |
> |
threadAssertNull(x); |
1034 |
> |
a.getAndIncrement(); |
1035 |
> |
throw ex2; |
1036 |
> |
}); |
1037 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1038 |
> |
|
1039 |
> |
checkCompletedWithWrappedException(g, ex1); |
1040 |
> |
checkCompletedExceptionally(f, ex1); |
1041 |
|
assertEquals(1, a.get()); |
1042 |
|
}} |
1043 |
|
|
1094 |
|
if (createIncomplete) f.completeExceptionally(ex); |
1095 |
|
|
1096 |
|
checkCompletedNormally(g, v1); |
1097 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1097 |
> |
checkCompletedExceptionally(f, ex); |
1098 |
|
assertEquals(1, a.get()); |
1099 |
|
}} |
1100 |
|
|
1150 |
|
}); |
1151 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
1152 |
|
|
1153 |
< |
checkCompletedWithWrappedCFException(g, ex2); |
1154 |
< |
checkCompletedWithWrappedCFException(f, ex1); |
1153 |
> |
checkCompletedWithWrappedException(g, ex2); |
1154 |
> |
checkCompletedExceptionally(f, ex1); |
1155 |
|
assertEquals(1, a.get()); |
1156 |
|
}} |
1157 |
|
|
1175 |
|
}); |
1176 |
|
if (createIncomplete) f.complete(v1); |
1177 |
|
|
1178 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1178 |
> |
checkCompletedWithWrappedException(g, ex); |
1179 |
|
checkCompletedNormally(f, v1); |
1180 |
|
assertEquals(1, a.get()); |
1181 |
|
}} |
1194 |
|
final CompletableFuture<Void> f = m.runAsync(r); |
1195 |
|
assertNull(f.join()); |
1196 |
|
checkCompletedNormally(f, null); |
1197 |
< |
assertEquals(1, r.invocationCount); |
1197 |
> |
r.assertInvoked(); |
1198 |
|
}} |
1199 |
|
|
1200 |
|
/** |
1210 |
|
final FailingRunnable r = new FailingRunnable(m); |
1211 |
|
final CompletableFuture<Void> f = m.runAsync(r); |
1212 |
|
checkCompletedWithWrappedCFException(f); |
1213 |
< |
assertEquals(1, r.invocationCount); |
1213 |
> |
r.assertInvoked(); |
1214 |
|
}} |
1215 |
|
|
1216 |
|
/** |
1228 |
|
final CompletableFuture<Integer> f = m.supplyAsync(r); |
1229 |
|
assertSame(v1, f.join()); |
1230 |
|
checkCompletedNormally(f, v1); |
1231 |
< |
assertEquals(1, r.invocationCount); |
1231 |
> |
r.assertInvoked(); |
1232 |
|
}} |
1233 |
|
|
1234 |
|
/** |
1244 |
|
FailingSupplier r = new FailingSupplier(m); |
1245 |
|
CompletableFuture<Integer> f = m.supplyAsync(r); |
1246 |
|
checkCompletedWithWrappedCFException(f); |
1247 |
< |
assertEquals(1, r.invocationCount); |
1247 |
> |
r.assertInvoked(); |
1248 |
|
}} |
1249 |
|
|
1250 |
|
// seq completion methods |
1268 |
|
|
1269 |
|
checkCompletedNormally(g, null); |
1270 |
|
checkCompletedNormally(f, v1); |
1271 |
< |
assertEquals(1, r.invocationCount); |
1271 |
> |
r.assertInvoked(); |
1272 |
|
}} |
1273 |
|
|
1274 |
|
/** |
1289 |
|
f.completeExceptionally(ex); |
1290 |
|
} |
1291 |
|
|
1292 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1293 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1294 |
< |
assertEquals(0, r.invocationCount); |
1292 |
> |
checkCompletedWithWrappedException(g, ex); |
1293 |
> |
checkCompletedExceptionally(f, ex); |
1294 |
> |
r.assertNotInvoked(); |
1295 |
|
}} |
1296 |
|
|
1297 |
|
/** |
1313 |
|
|
1314 |
|
checkCompletedWithWrappedCancellationException(g); |
1315 |
|
checkCancelled(f); |
1316 |
< |
assertEquals(0, r.invocationCount); |
1316 |
> |
r.assertNotInvoked(); |
1317 |
|
}} |
1318 |
|
|
1319 |
|
/** |
1356 |
|
|
1357 |
|
checkCompletedNormally(g, inc(v1)); |
1358 |
|
checkCompletedNormally(f, v1); |
1359 |
< |
assertEquals(1, r.invocationCount); |
1359 |
> |
r.assertValue(inc(v1)); |
1360 |
|
}} |
1361 |
|
|
1362 |
|
/** |
1377 |
|
f.completeExceptionally(ex); |
1378 |
|
} |
1379 |
|
|
1380 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1381 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1382 |
< |
assertEquals(0, r.invocationCount); |
1380 |
> |
checkCompletedWithWrappedException(g, ex); |
1381 |
> |
checkCompletedExceptionally(f, ex); |
1382 |
> |
r.assertNotInvoked(); |
1383 |
|
}} |
1384 |
|
|
1385 |
|
/** |
1401 |
|
|
1402 |
|
checkCompletedWithWrappedCancellationException(g); |
1403 |
|
checkCancelled(f); |
1404 |
< |
assertEquals(0, r.invocationCount); |
1404 |
> |
r.assertNotInvoked(); |
1405 |
|
}} |
1406 |
|
|
1407 |
|
/** |
1434 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1435 |
|
{ |
1436 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1437 |
< |
final IncAction r = new IncAction(); |
1437 |
> |
final NoopConsumer r = new NoopConsumer(m); |
1438 |
|
if (!createIncomplete) f.complete(v1); |
1439 |
|
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1440 |
|
if (createIncomplete) { |
1443 |
|
} |
1444 |
|
|
1445 |
|
checkCompletedNormally(g, null); |
1446 |
+ |
r.assertValue(v1); |
1447 |
|
checkCompletedNormally(f, v1); |
1250 |
– |
assertEquals(1, r.invocationCount); |
1251 |
– |
assertEquals(inc(v1), r.value); |
1448 |
|
}} |
1449 |
|
|
1450 |
|
/** |
1457 |
|
{ |
1458 |
|
final CFException ex = new CFException(); |
1459 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1460 |
< |
final IncAction r = new IncAction(); |
1460 |
> |
final NoopConsumer r = new NoopConsumer(m); |
1461 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1462 |
|
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1463 |
|
if (createIncomplete) { |
1465 |
|
f.completeExceptionally(ex); |
1466 |
|
} |
1467 |
|
|
1468 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1469 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1470 |
< |
assertEquals(0, r.invocationCount); |
1468 |
> |
checkCompletedWithWrappedException(g, ex); |
1469 |
> |
checkCompletedExceptionally(f, ex); |
1470 |
> |
r.assertNotInvoked(); |
1471 |
|
}} |
1472 |
|
|
1473 |
|
/** |
1479 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1480 |
|
{ |
1481 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1482 |
< |
final IncAction r = new IncAction(); |
1482 |
> |
final NoopConsumer r = new NoopConsumer(m); |
1483 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1484 |
|
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1485 |
|
if (createIncomplete) { |
1489 |
|
|
1490 |
|
checkCompletedWithWrappedCancellationException(g); |
1491 |
|
checkCancelled(f); |
1492 |
< |
assertEquals(0, r.invocationCount); |
1492 |
> |
r.assertNotInvoked(); |
1493 |
|
}} |
1494 |
|
|
1495 |
|
/** |
1534 |
|
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1535 |
|
if (createIncomplete) { |
1536 |
|
checkIncomplete(h); |
1537 |
< |
assertEquals(0, r.invocationCount); |
1537 |
> |
r.assertNotInvoked(); |
1538 |
|
if (!fFirst) f.complete(v1); else g.complete(v2); |
1539 |
|
} |
1540 |
|
|
1541 |
|
checkCompletedNormally(h, subtract(v1, v2)); |
1542 |
|
checkCompletedNormally(f, v1); |
1543 |
|
checkCompletedNormally(g, v2); |
1544 |
< |
assertEquals(1, r.invocationCount); |
1544 |
> |
r.assertValue(subtract(v1, v2)); |
1545 |
|
}} |
1546 |
|
|
1547 |
|
/** |
1568 |
|
(!fFirst ? f : g).completeExceptionally(ex); |
1569 |
|
} |
1570 |
|
|
1571 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1572 |
< |
assertEquals(0, r.invocationCount); |
1571 |
> |
checkCompletedWithWrappedException(h, ex); |
1572 |
> |
r.assertNotInvoked(); |
1573 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1574 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1574 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1575 |
|
}} |
1576 |
|
|
1577 |
|
/** |
1599 |
|
|
1600 |
|
checkCompletedWithWrappedCancellationException(h); |
1601 |
|
checkCancelled(!fFirst ? f : g); |
1602 |
< |
assertEquals(0, r.invocationCount); |
1602 |
> |
r.assertNotInvoked(); |
1603 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1604 |
|
}} |
1605 |
|
|
1651 |
|
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1652 |
|
if (createIncomplete) { |
1653 |
|
checkIncomplete(h); |
1654 |
< |
assertEquals(0, r.invocationCount); |
1654 |
> |
r.assertNotInvoked(); |
1655 |
|
if (!fFirst) f.complete(v1); else g.complete(v2); |
1656 |
|
} |
1657 |
|
|
1658 |
|
checkCompletedNormally(h, null); |
1659 |
< |
assertEquals(subtract(v1, v2), r.value); |
1659 |
> |
r.assertValue(subtract(v1, v2)); |
1660 |
|
checkCompletedNormally(f, v1); |
1661 |
|
checkCompletedNormally(g, v2); |
1662 |
|
}} |
1685 |
|
(!fFirst ? f : g).completeExceptionally(ex); |
1686 |
|
} |
1687 |
|
|
1688 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1689 |
< |
assertEquals(0, r.invocationCount); |
1688 |
> |
checkCompletedWithWrappedException(h, ex); |
1689 |
> |
r.assertNotInvoked(); |
1690 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1691 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1691 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1692 |
|
}} |
1693 |
|
|
1694 |
|
/** |
1716 |
|
|
1717 |
|
checkCompletedWithWrappedCancellationException(h); |
1718 |
|
checkCancelled(!fFirst ? f : g); |
1719 |
< |
assertEquals(0, r.invocationCount); |
1719 |
> |
r.assertNotInvoked(); |
1720 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1721 |
|
}} |
1722 |
|
|
1768 |
|
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1769 |
|
if (createIncomplete) { |
1770 |
|
checkIncomplete(h); |
1771 |
< |
assertEquals(0, r.invocationCount); |
1771 |
> |
r.assertNotInvoked(); |
1772 |
|
if (!fFirst) f.complete(v1); else g.complete(v2); |
1773 |
|
} |
1774 |
|
|
1775 |
|
checkCompletedNormally(h, null); |
1776 |
< |
assertEquals(1, r.invocationCount); |
1776 |
> |
r.assertInvoked(); |
1777 |
|
checkCompletedNormally(f, v1); |
1778 |
|
checkCompletedNormally(g, v2); |
1779 |
|
}} |
1802 |
|
(!fFirst ? f : g).completeExceptionally(ex); |
1803 |
|
} |
1804 |
|
|
1805 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1806 |
< |
assertEquals(0, r.invocationCount); |
1805 |
> |
checkCompletedWithWrappedException(h, ex); |
1806 |
> |
r.assertNotInvoked(); |
1807 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1808 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1808 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1809 |
|
}} |
1810 |
|
|
1811 |
|
/** |
1822 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1823 |
|
final Noop r = new Noop(m); |
1824 |
|
|
1629 |
– |
|
1825 |
|
(fFirst ? f : g).complete(v1); |
1826 |
|
if (!createIncomplete) |
1827 |
|
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1833 |
|
|
1834 |
|
checkCompletedWithWrappedCancellationException(h); |
1835 |
|
checkCancelled(!fFirst ? f : g); |
1836 |
< |
assertEquals(0, r.invocationCount); |
1836 |
> |
r.assertNotInvoked(); |
1837 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1838 |
|
}} |
1839 |
|
|
1848 |
|
{ |
1849 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1850 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1851 |
< |
final FailingRunnable r = new FailingRunnable(m); |
1851 |
> |
final FailingRunnable r1 = new FailingRunnable(m); |
1852 |
> |
final FailingRunnable r2 = new FailingRunnable(m); |
1853 |
|
|
1854 |
< |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r); |
1854 |
> |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1855 |
|
if (fFirst) { |
1856 |
|
f.complete(v1); |
1857 |
|
g.complete(v2); |
1859 |
|
g.complete(v2); |
1860 |
|
f.complete(v1); |
1861 |
|
} |
1862 |
< |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r); |
1862 |
> |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1863 |
|
|
1864 |
|
checkCompletedWithWrappedCFException(h1); |
1865 |
|
checkCompletedWithWrappedCFException(h2); |
1873 |
|
*/ |
1874 |
|
public void testApplyToEither_normalCompletion() { |
1875 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1680 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1681 |
– |
for (boolean fFirst : new boolean[] { true, false }) |
1682 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1683 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
1684 |
– |
{ |
1685 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1686 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1687 |
– |
final IncFunction r = new IncFunction(m); |
1688 |
– |
|
1689 |
– |
if (!createIncomplete) |
1690 |
– |
if (fFirst) f.complete(v1); else g.complete(v2); |
1691 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1692 |
– |
if (createIncomplete) { |
1693 |
– |
checkIncomplete(h); |
1694 |
– |
assertEquals(0, r.invocationCount); |
1695 |
– |
if (fFirst) f.complete(v1); else g.complete(v2); |
1696 |
– |
} |
1697 |
– |
checkCompletedNormally(h, inc(fFirst ? v1 : v2)); |
1698 |
– |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1699 |
– |
|
1700 |
– |
checkCompletedNormally(f, v1); |
1701 |
– |
checkCompletedNormally(g, v2); |
1702 |
– |
checkCompletedNormally(h, inc(fFirst ? v1 : v2)); |
1703 |
– |
}} |
1704 |
– |
|
1705 |
– |
public void testApplyToEither_normalCompletionBothAvailable() { |
1706 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1707 |
– |
for (boolean fFirst : new boolean[] { true, false }) |
1876 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1877 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1878 |
|
{ |
1879 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1880 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1881 |
< |
final IncFunction r = new IncFunction(m); |
1881 |
> |
final IncFunction[] rs = new IncFunction[6]; |
1882 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1883 |
|
|
1884 |
< |
if (fFirst) { |
1885 |
< |
f.complete(v1); |
1886 |
< |
g.complete(v2); |
1887 |
< |
} else { |
1888 |
< |
g.complete(v2); |
1889 |
< |
f.complete(v1); |
1890 |
< |
} |
1884 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
1885 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
1886 |
> |
checkIncomplete(h0); |
1887 |
> |
checkIncomplete(h1); |
1888 |
> |
rs[0].assertNotInvoked(); |
1889 |
> |
rs[1].assertNotInvoked(); |
1890 |
> |
f.complete(v1); |
1891 |
> |
checkCompletedNormally(h0, inc(v1)); |
1892 |
> |
checkCompletedNormally(h1, inc(v1)); |
1893 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1894 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
1895 |
> |
checkCompletedNormally(h2, inc(v1)); |
1896 |
> |
checkCompletedNormally(h3, inc(v1)); |
1897 |
> |
g.complete(v2); |
1898 |
|
|
1899 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1899 |
> |
// unspecified behavior - both source completions available |
1900 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
1901 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
1902 |
> |
rs[4].assertValue(h4.join()); |
1903 |
> |
rs[5].assertValue(h5.join()); |
1904 |
> |
assertTrue(Objects.equals(inc(v1), h4.join()) || |
1905 |
> |
Objects.equals(inc(v2), h4.join())); |
1906 |
> |
assertTrue(Objects.equals(inc(v1), h5.join()) || |
1907 |
> |
Objects.equals(inc(v2), h5.join())); |
1908 |
|
|
1909 |
|
checkCompletedNormally(f, v1); |
1910 |
|
checkCompletedNormally(g, v2); |
1911 |
< |
|
1912 |
< |
// unspecified behavior |
1913 |
< |
assertTrue(Objects.equals(h.join(), inc(v1)) || |
1914 |
< |
Objects.equals(h.join(), inc(v2))); |
1915 |
< |
assertEquals(1, r.invocationCount); |
1911 |
> |
checkCompletedNormally(h0, inc(v1)); |
1912 |
> |
checkCompletedNormally(h1, inc(v1)); |
1913 |
> |
checkCompletedNormally(h2, inc(v1)); |
1914 |
> |
checkCompletedNormally(h3, inc(v1)); |
1915 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); |
1916 |
|
}} |
1917 |
|
|
1918 |
|
/** |
1919 |
|
* applyToEither result completes exceptionally after exceptional |
1920 |
|
* completion of either source |
1921 |
|
*/ |
1922 |
< |
public void testApplyToEither_exceptionalCompletion1() { |
1922 |
> |
public void testApplyToEither_exceptionalCompletion() { |
1923 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1740 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1741 |
– |
for (boolean fFirst : new boolean[] { true, false }) |
1924 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1925 |
|
{ |
1926 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1927 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1928 |
|
final CFException ex = new CFException(); |
1929 |
< |
final IncFunction r = new IncFunction(m); |
1929 |
> |
final IncFunction[] rs = new IncFunction[6]; |
1930 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1931 |
|
|
1932 |
< |
if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex); |
1933 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1934 |
< |
if (createIncomplete) { |
1935 |
< |
checkIncomplete(h); |
1936 |
< |
assertEquals(0, r.invocationCount); |
1937 |
< |
(fFirst ? f : g).completeExceptionally(ex); |
1938 |
< |
} |
1932 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
1933 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
1934 |
> |
checkIncomplete(h0); |
1935 |
> |
checkIncomplete(h1); |
1936 |
> |
rs[0].assertNotInvoked(); |
1937 |
> |
rs[1].assertNotInvoked(); |
1938 |
> |
f.completeExceptionally(ex); |
1939 |
> |
checkCompletedWithWrappedException(h0, ex); |
1940 |
> |
checkCompletedWithWrappedException(h1, ex); |
1941 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1942 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
1943 |
> |
checkCompletedWithWrappedException(h2, ex); |
1944 |
> |
checkCompletedWithWrappedException(h3, ex); |
1945 |
> |
g.complete(v1); |
1946 |
|
|
1947 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1948 |
< |
(!fFirst ? f : g).complete(v1); |
1947 |
> |
// unspecified behavior - both source completions available |
1948 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
1949 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
1950 |
> |
try { |
1951 |
> |
assertEquals(inc(v1), h4.join()); |
1952 |
> |
rs[4].assertValue(inc(v1)); |
1953 |
> |
} catch (CompletionException ok) { |
1954 |
> |
checkCompletedWithWrappedException(h4, ex); |
1955 |
> |
rs[4].assertNotInvoked(); |
1956 |
> |
} |
1957 |
> |
try { |
1958 |
> |
assertEquals(inc(v1), h5.join()); |
1959 |
> |
rs[5].assertValue(inc(v1)); |
1960 |
> |
} catch (CompletionException ok) { |
1961 |
> |
checkCompletedWithWrappedException(h5, ex); |
1962 |
> |
rs[5].assertNotInvoked(); |
1963 |
> |
} |
1964 |
|
|
1965 |
< |
assertEquals(0, r.invocationCount); |
1966 |
< |
checkCompletedNormally(!fFirst ? f : g, v1); |
1967 |
< |
checkCompletedWithWrappedCFException(fFirst ? f : g, ex); |
1968 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1965 |
> |
checkCompletedExceptionally(f, ex); |
1966 |
> |
checkCompletedNormally(g, v1); |
1967 |
> |
checkCompletedWithWrappedException(h0, ex); |
1968 |
> |
checkCompletedWithWrappedException(h1, ex); |
1969 |
> |
checkCompletedWithWrappedException(h2, ex); |
1970 |
> |
checkCompletedWithWrappedException(h3, ex); |
1971 |
> |
checkCompletedWithWrappedException(h4, ex); |
1972 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
1973 |
|
}} |
1974 |
|
|
1975 |
|
public void testApplyToEither_exceptionalCompletion2() { |
1976 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1768 |
– |
for (boolean reverseArgs : new boolean[] { true, false }) |
1977 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1978 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1979 |
|
{ |
1980 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1981 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1774 |
– |
final IncFunction r1 = new IncFunction(m); |
1775 |
– |
final IncFunction r2 = new IncFunction(m); |
1982 |
|
final CFException ex = new CFException(); |
1983 |
< |
final CompletableFuture<Integer> j = (reverseArgs ? g : f); |
1984 |
< |
final CompletableFuture<Integer> k = (reverseArgs ? f : g); |
1985 |
< |
final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1); |
1983 |
> |
final IncFunction[] rs = new IncFunction[6]; |
1984 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1985 |
> |
|
1986 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
1987 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
1988 |
|
if (fFirst) { |
1989 |
|
f.complete(v1); |
1990 |
|
g.completeExceptionally(ex); |
1992 |
|
g.completeExceptionally(ex); |
1993 |
|
f.complete(v1); |
1994 |
|
} |
1995 |
< |
final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2); |
1995 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1996 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
1997 |
|
|
1998 |
< |
// unspecified behavior |
1998 |
> |
// unspecified behavior - both source completions available |
1999 |
> |
try { |
2000 |
> |
assertEquals(inc(v1), h0.join()); |
2001 |
> |
rs[0].assertValue(inc(v1)); |
2002 |
> |
} catch (CompletionException ok) { |
2003 |
> |
checkCompletedWithWrappedException(h0, ex); |
2004 |
> |
rs[0].assertNotInvoked(); |
2005 |
> |
} |
2006 |
|
try { |
2007 |
|
assertEquals(inc(v1), h1.join()); |
2008 |
< |
assertEquals(1, r1.invocationCount); |
2008 |
> |
rs[1].assertValue(inc(v1)); |
2009 |
|
} catch (CompletionException ok) { |
2010 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2011 |
< |
assertEquals(0, r1.invocationCount); |
2010 |
> |
checkCompletedWithWrappedException(h1, ex); |
2011 |
> |
rs[1].assertNotInvoked(); |
2012 |
|
} |
1797 |
– |
|
2013 |
|
try { |
2014 |
|
assertEquals(inc(v1), h2.join()); |
2015 |
< |
assertEquals(1, r2.invocationCount); |
2015 |
> |
rs[2].assertValue(inc(v1)); |
2016 |
|
} catch (CompletionException ok) { |
2017 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2018 |
< |
assertEquals(0, r2.invocationCount); |
2017 |
> |
checkCompletedWithWrappedException(h2, ex); |
2018 |
> |
rs[2].assertNotInvoked(); |
2019 |
> |
} |
2020 |
> |
try { |
2021 |
> |
assertEquals(inc(v1), h3.join()); |
2022 |
> |
rs[3].assertValue(inc(v1)); |
2023 |
> |
} catch (CompletionException ok) { |
2024 |
> |
checkCompletedWithWrappedException(h3, ex); |
2025 |
> |
rs[3].assertNotInvoked(); |
2026 |
|
} |
2027 |
|
|
1806 |
– |
checkCompletedWithWrappedCFException(g, ex); |
1807 |
– |
checkCompletedNormally(f, v1); |
1808 |
– |
}} |
1809 |
– |
|
1810 |
– |
/** |
1811 |
– |
* applyToEither result completes exceptionally if action does |
1812 |
– |
*/ |
1813 |
– |
public void testApplyToEither_actionFailed1() { |
1814 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1815 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1816 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
1817 |
– |
{ |
1818 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1819 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1820 |
– |
final FailingFunction r = new FailingFunction(m); |
1821 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1822 |
– |
|
1823 |
– |
f.complete(v1); |
1824 |
– |
checkCompletedWithWrappedCFException(h); |
1825 |
– |
g.complete(v2); |
1826 |
– |
checkCompletedNormally(f, v1); |
1827 |
– |
checkCompletedNormally(g, v2); |
1828 |
– |
}} |
1829 |
– |
|
1830 |
– |
public void testApplyToEither_actionFailed2() { |
1831 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1832 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1833 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
1834 |
– |
{ |
1835 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1836 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1837 |
– |
final FailingFunction r = new FailingFunction(m); |
1838 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1839 |
– |
|
1840 |
– |
g.complete(v2); |
1841 |
– |
checkCompletedWithWrappedCFException(h); |
1842 |
– |
f.complete(v1); |
2028 |
|
checkCompletedNormally(f, v1); |
2029 |
< |
checkCompletedNormally(g, v2); |
2029 |
> |
checkCompletedExceptionally(g, ex); |
2030 |
|
}} |
2031 |
|
|
2032 |
|
/** |
2033 |
|
* applyToEither result completes exceptionally if either source cancelled |
2034 |
|
*/ |
2035 |
< |
public void testApplyToEither_sourceCancelled1() { |
2035 |
> |
public void testApplyToEither_sourceCancelled() { |
2036 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2037 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1853 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1854 |
– |
for (boolean fFirst : new boolean[] { true, false }) |
2038 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2039 |
|
{ |
2040 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2041 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2042 |
< |
final IncFunction r = new IncFunction(m); |
2042 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2043 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2044 |
|
|
2045 |
< |
if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2046 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2047 |
< |
if (createIncomplete) { |
2048 |
< |
checkIncomplete(h); |
2049 |
< |
assertEquals(0, r.invocationCount); |
2050 |
< |
assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2051 |
< |
} |
2045 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2046 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2047 |
> |
checkIncomplete(h0); |
2048 |
> |
checkIncomplete(h1); |
2049 |
> |
rs[0].assertNotInvoked(); |
2050 |
> |
rs[1].assertNotInvoked(); |
2051 |
> |
f.cancel(mayInterruptIfRunning); |
2052 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2053 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2054 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2055 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2056 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2057 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2058 |
> |
g.complete(v1); |
2059 |
|
|
2060 |
< |
checkCompletedWithWrappedCancellationException(h); |
2061 |
< |
(!fFirst ? f : g).complete(v1); |
2060 |
> |
// unspecified behavior - both source completions available |
2061 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2062 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2063 |
> |
try { |
2064 |
> |
assertEquals(inc(v1), h4.join()); |
2065 |
> |
rs[4].assertValue(inc(v1)); |
2066 |
> |
} catch (CompletionException ok) { |
2067 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2068 |
> |
rs[4].assertNotInvoked(); |
2069 |
> |
} |
2070 |
> |
try { |
2071 |
> |
assertEquals(inc(v1), h5.join()); |
2072 |
> |
rs[5].assertValue(inc(v1)); |
2073 |
> |
} catch (CompletionException ok) { |
2074 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2075 |
> |
rs[5].assertNotInvoked(); |
2076 |
> |
} |
2077 |
|
|
2078 |
< |
assertEquals(0, r.invocationCount); |
2079 |
< |
checkCompletedNormally(!fFirst ? f : g, v1); |
2080 |
< |
checkCancelled(fFirst ? f : g); |
2081 |
< |
checkCompletedWithWrappedCancellationException(h); |
2078 |
> |
checkCancelled(f); |
2079 |
> |
checkCompletedNormally(g, v1); |
2080 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2081 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2082 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2083 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2084 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2085 |
|
}} |
2086 |
|
|
2087 |
|
public void testApplyToEither_sourceCancelled2() { |
2088 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2089 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1881 |
– |
for (boolean reverseArgs : new boolean[] { true, false }) |
2090 |
|
for (boolean fFirst : new boolean[] { true, false }) |
2091 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2092 |
|
{ |
2093 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2094 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2095 |
< |
final IncFunction r1 = new IncFunction(m); |
2096 |
< |
final IncFunction r2 = new IncFunction(m); |
1889 |
< |
final CFException ex = new CFException(); |
1890 |
< |
final CompletableFuture<Integer> j = (reverseArgs ? g : f); |
1891 |
< |
final CompletableFuture<Integer> k = (reverseArgs ? f : g); |
2095 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2096 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2097 |
|
|
2098 |
< |
final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1); |
2098 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2099 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2100 |
|
if (fFirst) { |
2101 |
|
f.complete(v1); |
2102 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2102 |
> |
g.cancel(mayInterruptIfRunning); |
2103 |
|
} else { |
2104 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2104 |
> |
g.cancel(mayInterruptIfRunning); |
2105 |
|
f.complete(v1); |
2106 |
|
} |
2107 |
< |
final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2); |
2107 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2108 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2109 |
|
|
2110 |
< |
// unspecified behavior |
2110 |
> |
// unspecified behavior - both source completions available |
2111 |
> |
try { |
2112 |
> |
assertEquals(inc(v1), h0.join()); |
2113 |
> |
rs[0].assertValue(inc(v1)); |
2114 |
> |
} catch (CompletionException ok) { |
2115 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2116 |
> |
rs[0].assertNotInvoked(); |
2117 |
> |
} |
2118 |
|
try { |
2119 |
|
assertEquals(inc(v1), h1.join()); |
2120 |
< |
assertEquals(1, r1.invocationCount); |
2120 |
> |
rs[1].assertValue(inc(v1)); |
2121 |
|
} catch (CompletionException ok) { |
2122 |
|
checkCompletedWithWrappedCancellationException(h1); |
2123 |
< |
assertEquals(0, r1.invocationCount); |
2123 |
> |
rs[1].assertNotInvoked(); |
2124 |
|
} |
1911 |
– |
|
2125 |
|
try { |
2126 |
|
assertEquals(inc(v1), h2.join()); |
2127 |
< |
assertEquals(1, r2.invocationCount); |
2127 |
> |
rs[2].assertValue(inc(v1)); |
2128 |
|
} catch (CompletionException ok) { |
2129 |
|
checkCompletedWithWrappedCancellationException(h2); |
2130 |
< |
assertEquals(0, r2.invocationCount); |
2130 |
> |
rs[2].assertNotInvoked(); |
2131 |
> |
} |
2132 |
> |
try { |
2133 |
> |
assertEquals(inc(v1), h3.join()); |
2134 |
> |
rs[3].assertValue(inc(v1)); |
2135 |
> |
} catch (CompletionException ok) { |
2136 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2137 |
> |
rs[3].assertNotInvoked(); |
2138 |
|
} |
2139 |
|
|
1920 |
– |
checkCancelled(g); |
2140 |
|
checkCompletedNormally(f, v1); |
2141 |
+ |
checkCancelled(g); |
2142 |
|
}} |
2143 |
|
|
2144 |
|
/** |
2145 |
< |
* acceptEither result completes normally after normal completion |
1926 |
< |
* of either source |
2145 |
> |
* applyToEither result completes exceptionally if action does |
2146 |
|
*/ |
2147 |
< |
public void testAcceptEither_normalCompletion1() { |
2147 |
> |
public void testApplyToEither_actionFailed() { |
2148 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2149 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2150 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2151 |
|
{ |
2152 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2153 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2154 |
< |
final IncAction r = new IncAction(); |
2155 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2154 |
> |
final FailingFunction[] rs = new FailingFunction[6]; |
2155 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
2156 |
|
|
2157 |
+ |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2158 |
+ |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2159 |
|
f.complete(v1); |
2160 |
< |
checkCompletedNormally(h, null); |
2161 |
< |
assertEquals(inc(v1), r.value); |
2162 |
< |
g.complete(v2); |
2163 |
< |
|
2164 |
< |
checkCompletedNormally(f, v1); |
2165 |
< |
checkCompletedNormally(g, v2); |
2166 |
< |
checkCompletedNormally(h, null); |
1946 |
< |
}} |
1947 |
< |
|
1948 |
< |
public void testAcceptEither_normalCompletion2() { |
1949 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1950 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1951 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1952 |
< |
{ |
1953 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1954 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1955 |
< |
final IncAction r = new IncAction(); |
1956 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2160 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2161 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2162 |
> |
checkCompletedWithWrappedCFException(h0); |
2163 |
> |
checkCompletedWithWrappedCFException(h1); |
2164 |
> |
checkCompletedWithWrappedCFException(h2); |
2165 |
> |
checkCompletedWithWrappedCFException(h3); |
2166 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2167 |
|
|
2168 |
|
g.complete(v2); |
2169 |
< |
checkCompletedNormally(h, null); |
2170 |
< |
assertEquals(inc(v2), r.value); |
2171 |
< |
f.complete(v1); |
2169 |
> |
|
2170 |
> |
// unspecified behavior - both source completions available |
2171 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2172 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2173 |
> |
|
2174 |
> |
checkCompletedWithWrappedCFException(h4); |
2175 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2176 |
> |
Objects.equals(v2, rs[4].value)); |
2177 |
> |
checkCompletedWithWrappedCFException(h5); |
2178 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2179 |
> |
Objects.equals(v2, rs[5].value)); |
2180 |
|
|
2181 |
|
checkCompletedNormally(f, v1); |
2182 |
|
checkCompletedNormally(g, v2); |
1965 |
– |
checkCompletedNormally(h, null); |
2183 |
|
}} |
2184 |
|
|
2185 |
< |
public void testAcceptEither_normalCompletion3() { |
2185 |
> |
/** |
2186 |
> |
* acceptEither result completes normally after normal completion |
2187 |
> |
* of either source |
2188 |
> |
*/ |
2189 |
> |
public void testAcceptEither_normalCompletion() { |
2190 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2191 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2192 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2193 |
|
{ |
2194 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2195 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2196 |
< |
final IncAction r = new IncAction(); |
2196 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2197 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2198 |
|
|
2199 |
+ |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2200 |
+ |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2201 |
+ |
checkIncomplete(h0); |
2202 |
+ |
checkIncomplete(h1); |
2203 |
+ |
rs[0].assertNotInvoked(); |
2204 |
+ |
rs[1].assertNotInvoked(); |
2205 |
|
f.complete(v1); |
2206 |
+ |
checkCompletedNormally(h0, null); |
2207 |
+ |
checkCompletedNormally(h1, null); |
2208 |
+ |
rs[0].assertValue(v1); |
2209 |
+ |
rs[1].assertValue(v1); |
2210 |
+ |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2211 |
+ |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2212 |
+ |
checkCompletedNormally(h2, null); |
2213 |
+ |
checkCompletedNormally(h3, null); |
2214 |
+ |
rs[2].assertValue(v1); |
2215 |
+ |
rs[3].assertValue(v1); |
2216 |
|
g.complete(v2); |
1979 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2217 |
|
|
2218 |
< |
checkCompletedNormally(h, null); |
2218 |
> |
// unspecified behavior - both source completions available |
2219 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2220 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2221 |
> |
checkCompletedNormally(h4, null); |
2222 |
> |
checkCompletedNormally(h5, null); |
2223 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2224 |
> |
Objects.equals(v2, rs[4].value)); |
2225 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2226 |
> |
Objects.equals(v2, rs[5].value)); |
2227 |
> |
|
2228 |
|
checkCompletedNormally(f, v1); |
2229 |
|
checkCompletedNormally(g, v2); |
2230 |
< |
|
2231 |
< |
// unspecified behavior |
2232 |
< |
assertTrue(Objects.equals(r.value, inc(v1)) || |
2233 |
< |
Objects.equals(r.value, inc(v2))); |
2230 |
> |
checkCompletedNormally(h0, null); |
2231 |
> |
checkCompletedNormally(h1, null); |
2232 |
> |
checkCompletedNormally(h2, null); |
2233 |
> |
checkCompletedNormally(h3, null); |
2234 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2235 |
|
}} |
2236 |
|
|
2237 |
|
/** |
2238 |
|
* acceptEither result completes exceptionally after exceptional |
2239 |
|
* completion of either source |
2240 |
|
*/ |
2241 |
< |
public void testAcceptEither_exceptionalCompletion1() { |
2241 |
> |
public void testAcceptEither_exceptionalCompletion() { |
2242 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2243 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2244 |
|
{ |
2245 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2246 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2000 |
– |
final IncAction r = new IncAction(); |
2001 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2247 |
|
final CFException ex = new CFException(); |
2248 |
+ |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2249 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2250 |
|
|
2251 |
+ |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2252 |
+ |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2253 |
+ |
checkIncomplete(h0); |
2254 |
+ |
checkIncomplete(h1); |
2255 |
+ |
rs[0].assertNotInvoked(); |
2256 |
+ |
rs[1].assertNotInvoked(); |
2257 |
|
f.completeExceptionally(ex); |
2258 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2258 |
> |
checkCompletedWithWrappedException(h0, ex); |
2259 |
> |
checkCompletedWithWrappedException(h1, ex); |
2260 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2261 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2262 |
> |
checkCompletedWithWrappedException(h2, ex); |
2263 |
> |
checkCompletedWithWrappedException(h3, ex); |
2264 |
> |
|
2265 |
|
g.complete(v1); |
2266 |
|
|
2267 |
< |
assertEquals(0, r.invocationCount); |
2267 |
> |
// unspecified behavior - both source completions available |
2268 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2269 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2270 |
> |
try { |
2271 |
> |
assertNull(h4.join()); |
2272 |
> |
rs[4].assertValue(v1); |
2273 |
> |
} catch (CompletionException ok) { |
2274 |
> |
checkCompletedWithWrappedException(h4, ex); |
2275 |
> |
rs[4].assertNotInvoked(); |
2276 |
> |
} |
2277 |
> |
try { |
2278 |
> |
assertNull(h5.join()); |
2279 |
> |
rs[5].assertValue(v1); |
2280 |
> |
} catch (CompletionException ok) { |
2281 |
> |
checkCompletedWithWrappedException(h5, ex); |
2282 |
> |
rs[5].assertNotInvoked(); |
2283 |
> |
} |
2284 |
> |
|
2285 |
> |
checkCompletedExceptionally(f, ex); |
2286 |
|
checkCompletedNormally(g, v1); |
2287 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2288 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2287 |
> |
checkCompletedWithWrappedException(h0, ex); |
2288 |
> |
checkCompletedWithWrappedException(h1, ex); |
2289 |
> |
checkCompletedWithWrappedException(h2, ex); |
2290 |
> |
checkCompletedWithWrappedException(h3, ex); |
2291 |
> |
checkCompletedWithWrappedException(h4, ex); |
2292 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2293 |
|
}} |
2294 |
|
|
2295 |
|
public void testAcceptEither_exceptionalCompletion2() { |
2296 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2297 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2298 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2299 |
|
{ |
2300 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2301 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2020 |
– |
final IncAction r = new IncAction(); |
2021 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2022 |
– |
final CFException ex = new CFException(); |
2023 |
– |
|
2024 |
– |
g.completeExceptionally(ex); |
2025 |
– |
checkCompletedWithWrappedCFException(h, ex); |
2026 |
– |
f.complete(v1); |
2027 |
– |
|
2028 |
– |
assertEquals(0, r.invocationCount); |
2029 |
– |
checkCompletedNormally(f, v1); |
2030 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2031 |
– |
checkCompletedWithWrappedCFException(h, ex); |
2032 |
– |
}} |
2033 |
– |
|
2034 |
– |
public void testAcceptEither_exceptionalCompletion3() { |
2035 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2036 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2037 |
– |
{ |
2038 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2039 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2040 |
– |
final IncAction r = new IncAction(); |
2302 |
|
final CFException ex = new CFException(); |
2303 |
+ |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2304 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2305 |
|
|
2306 |
< |
g.completeExceptionally(ex); |
2307 |
< |
f.complete(v1); |
2308 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2306 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2307 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2308 |
> |
if (fFirst) { |
2309 |
> |
f.complete(v1); |
2310 |
> |
g.completeExceptionally(ex); |
2311 |
> |
} else { |
2312 |
> |
g.completeExceptionally(ex); |
2313 |
> |
f.complete(v1); |
2314 |
> |
} |
2315 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2316 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2317 |
|
|
2318 |
< |
// unspecified behavior |
2048 |
< |
Integer v; |
2318 |
> |
// unspecified behavior - both source completions available |
2319 |
|
try { |
2320 |
< |
assertNull(h.join()); |
2321 |
< |
assertEquals(1, r.invocationCount); |
2052 |
< |
assertEquals(inc(v1), r.value); |
2320 |
> |
assertEquals(null, h0.join()); |
2321 |
> |
rs[0].assertValue(v1); |
2322 |
|
} catch (CompletionException ok) { |
2323 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2324 |
< |
assertEquals(0, r.invocationCount); |
2323 |
> |
checkCompletedWithWrappedException(h0, ex); |
2324 |
> |
rs[0].assertNotInvoked(); |
2325 |
|
} |
2057 |
– |
|
2058 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2059 |
– |
checkCompletedNormally(f, v1); |
2060 |
– |
}} |
2061 |
– |
|
2062 |
– |
public void testAcceptEither_exceptionalCompletion4() { |
2063 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2064 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2065 |
– |
{ |
2066 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2067 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2068 |
– |
final IncAction r = new IncAction(); |
2069 |
– |
final CFException ex = new CFException(); |
2070 |
– |
|
2071 |
– |
f.completeExceptionally(ex); |
2072 |
– |
g.complete(v1); |
2073 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2074 |
– |
|
2075 |
– |
// unspecified behavior |
2076 |
– |
Integer v; |
2326 |
|
try { |
2327 |
< |
assertNull(h.join()); |
2328 |
< |
assertEquals(1, r.invocationCount); |
2080 |
< |
assertEquals(inc(v1), r.value); |
2327 |
> |
assertEquals(null, h1.join()); |
2328 |
> |
rs[1].assertValue(v1); |
2329 |
|
} catch (CompletionException ok) { |
2330 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2331 |
< |
assertEquals(0, r.invocationCount); |
2330 |
> |
checkCompletedWithWrappedException(h1, ex); |
2331 |
> |
rs[1].assertNotInvoked(); |
2332 |
> |
} |
2333 |
> |
try { |
2334 |
> |
assertEquals(null, h2.join()); |
2335 |
> |
rs[2].assertValue(v1); |
2336 |
> |
} catch (CompletionException ok) { |
2337 |
> |
checkCompletedWithWrappedException(h2, ex); |
2338 |
> |
rs[2].assertNotInvoked(); |
2339 |
> |
} |
2340 |
> |
try { |
2341 |
> |
assertEquals(null, h3.join()); |
2342 |
> |
rs[3].assertValue(v1); |
2343 |
> |
} catch (CompletionException ok) { |
2344 |
> |
checkCompletedWithWrappedException(h3, ex); |
2345 |
> |
rs[3].assertNotInvoked(); |
2346 |
|
} |
2347 |
|
|
2086 |
– |
checkCompletedWithWrappedCFException(f, ex); |
2087 |
– |
checkCompletedNormally(g, v1); |
2088 |
– |
}} |
2089 |
– |
|
2090 |
– |
/** |
2091 |
– |
* acceptEither result completes exceptionally if action does |
2092 |
– |
*/ |
2093 |
– |
public void testAcceptEither_actionFailed1() { |
2094 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2095 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2096 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2097 |
– |
{ |
2098 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2099 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2100 |
– |
final FailingConsumer r = new FailingConsumer(m); |
2101 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2102 |
– |
|
2103 |
– |
f.complete(v1); |
2104 |
– |
checkCompletedWithWrappedCFException(h); |
2105 |
– |
g.complete(v2); |
2106 |
– |
checkCompletedNormally(f, v1); |
2107 |
– |
checkCompletedNormally(g, v2); |
2108 |
– |
}} |
2109 |
– |
|
2110 |
– |
public void testAcceptEither_actionFailed2() { |
2111 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2112 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2113 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2114 |
– |
{ |
2115 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2116 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2117 |
– |
final FailingConsumer r = new FailingConsumer(m); |
2118 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2119 |
– |
|
2120 |
– |
g.complete(v2); |
2121 |
– |
checkCompletedWithWrappedCFException(h); |
2122 |
– |
f.complete(v1); |
2348 |
|
checkCompletedNormally(f, v1); |
2349 |
< |
checkCompletedNormally(g, v2); |
2349 |
> |
checkCompletedExceptionally(g, ex); |
2350 |
|
}} |
2351 |
|
|
2352 |
|
/** |
2353 |
|
* acceptEither result completes exceptionally if either source cancelled |
2354 |
|
*/ |
2355 |
< |
public void testAcceptEither_sourceCancelled1() { |
2131 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2132 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2133 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2134 |
< |
{ |
2135 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2136 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2137 |
< |
final IncAction r = new IncAction(); |
2138 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2139 |
< |
|
2140 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2141 |
< |
checkCompletedWithWrappedCancellationException(h); |
2142 |
< |
g.complete(v1); |
2143 |
< |
|
2144 |
< |
checkCancelled(f); |
2145 |
< |
assertEquals(0, r.invocationCount); |
2146 |
< |
checkCompletedNormally(g, v1); |
2147 |
< |
checkCompletedWithWrappedCancellationException(h); |
2148 |
< |
}} |
2149 |
< |
|
2150 |
< |
public void testAcceptEither_sourceCancelled2() { |
2355 |
> |
public void testAcceptEither_sourceCancelled() { |
2356 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2357 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2358 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2359 |
|
{ |
2360 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2361 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2362 |
< |
final IncAction r = new IncAction(); |
2363 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2362 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2363 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2364 |
|
|
2365 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2366 |
< |
checkCompletedWithWrappedCancellationException(h); |
2367 |
< |
f.complete(v1); |
2368 |
< |
|
2369 |
< |
checkCancelled(g); |
2370 |
< |
assertEquals(0, r.invocationCount); |
2371 |
< |
checkCompletedNormally(f, v1); |
2372 |
< |
checkCompletedWithWrappedCancellationException(h); |
2373 |
< |
}} |
2374 |
< |
|
2375 |
< |
public void testAcceptEither_sourceCancelled3() { |
2376 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2377 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2173 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2174 |
< |
{ |
2175 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2176 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2177 |
< |
final IncAction r = new IncAction(); |
2365 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2366 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2367 |
> |
checkIncomplete(h0); |
2368 |
> |
checkIncomplete(h1); |
2369 |
> |
rs[0].assertNotInvoked(); |
2370 |
> |
rs[1].assertNotInvoked(); |
2371 |
> |
f.cancel(mayInterruptIfRunning); |
2372 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2373 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2374 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2375 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2376 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2377 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2378 |
|
|
2379 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2180 |
< |
f.complete(v1); |
2181 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2379 |
> |
g.complete(v1); |
2380 |
|
|
2381 |
< |
// unspecified behavior |
2382 |
< |
Integer v; |
2381 |
> |
// unspecified behavior - both source completions available |
2382 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2383 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2384 |
|
try { |
2385 |
< |
assertNull(h.join()); |
2386 |
< |
assertEquals(1, r.invocationCount); |
2188 |
< |
assertEquals(inc(v1), r.value); |
2385 |
> |
assertNull(h4.join()); |
2386 |
> |
rs[4].assertValue(v1); |
2387 |
|
} catch (CompletionException ok) { |
2388 |
< |
checkCompletedWithWrappedCancellationException(h); |
2389 |
< |
assertEquals(0, r.invocationCount); |
2388 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2389 |
> |
rs[4].assertNotInvoked(); |
2390 |
|
} |
2193 |
– |
|
2194 |
– |
checkCancelled(g); |
2195 |
– |
checkCompletedNormally(f, v1); |
2196 |
– |
}} |
2197 |
– |
|
2198 |
– |
public void testAcceptEither_sourceCancelled4() { |
2199 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2200 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2201 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2202 |
– |
{ |
2203 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2204 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2205 |
– |
final IncAction r = new IncAction(); |
2206 |
– |
|
2207 |
– |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2208 |
– |
g.complete(v1); |
2209 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2210 |
– |
|
2211 |
– |
// unspecified behavior |
2212 |
– |
Integer v; |
2391 |
|
try { |
2392 |
< |
assertNull(h.join()); |
2393 |
< |
assertEquals(1, r.invocationCount); |
2216 |
< |
assertEquals(inc(v1), r.value); |
2392 |
> |
assertNull(h5.join()); |
2393 |
> |
rs[5].assertValue(v1); |
2394 |
|
} catch (CompletionException ok) { |
2395 |
< |
checkCompletedWithWrappedCancellationException(h); |
2396 |
< |
assertEquals(0, r.invocationCount); |
2395 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2396 |
> |
rs[5].assertNotInvoked(); |
2397 |
|
} |
2398 |
|
|
2399 |
|
checkCancelled(f); |
2400 |
|
checkCompletedNormally(g, v1); |
2401 |
+ |
checkCompletedWithWrappedCancellationException(h0); |
2402 |
+ |
checkCompletedWithWrappedCancellationException(h1); |
2403 |
+ |
checkCompletedWithWrappedCancellationException(h2); |
2404 |
+ |
checkCompletedWithWrappedCancellationException(h3); |
2405 |
+ |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2406 |
|
}} |
2407 |
|
|
2408 |
|
/** |
2409 |
< |
* runAfterEither result completes normally after normal completion |
2228 |
< |
* of either source |
2409 |
> |
* acceptEither result completes exceptionally if action does |
2410 |
|
*/ |
2411 |
< |
public void testRunAfterEither_normalCompletion1() { |
2411 |
> |
public void testAcceptEither_actionFailed() { |
2412 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2413 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2414 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2415 |
|
{ |
2416 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2417 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2418 |
< |
final Noop r = new Noop(m); |
2419 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2418 |
> |
final FailingConsumer[] rs = new FailingConsumer[6]; |
2419 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
2420 |
|
|
2421 |
+ |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2422 |
+ |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2423 |
|
f.complete(v1); |
2424 |
< |
checkCompletedNormally(h, null); |
2425 |
< |
assertEquals(1, r.invocationCount); |
2424 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2425 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2426 |
> |
checkCompletedWithWrappedCFException(h0); |
2427 |
> |
checkCompletedWithWrappedCFException(h1); |
2428 |
> |
checkCompletedWithWrappedCFException(h2); |
2429 |
> |
checkCompletedWithWrappedCFException(h3); |
2430 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2431 |
> |
|
2432 |
|
g.complete(v2); |
2433 |
|
|
2434 |
+ |
// unspecified behavior - both source completions available |
2435 |
+ |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2436 |
+ |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2437 |
+ |
|
2438 |
+ |
checkCompletedWithWrappedCFException(h4); |
2439 |
+ |
assertTrue(Objects.equals(v1, rs[4].value) || |
2440 |
+ |
Objects.equals(v2, rs[4].value)); |
2441 |
+ |
checkCompletedWithWrappedCFException(h5); |
2442 |
+ |
assertTrue(Objects.equals(v1, rs[5].value) || |
2443 |
+ |
Objects.equals(v2, rs[5].value)); |
2444 |
+ |
|
2445 |
|
checkCompletedNormally(f, v1); |
2446 |
|
checkCompletedNormally(g, v2); |
2247 |
– |
checkCompletedNormally(h, null); |
2248 |
– |
assertEquals(1, r.invocationCount); |
2447 |
|
}} |
2448 |
|
|
2449 |
< |
public void testRunAfterEither_normalCompletion2() { |
2449 |
> |
/** |
2450 |
> |
* runAfterEither result completes normally after normal completion |
2451 |
> |
* of either source |
2452 |
> |
*/ |
2453 |
> |
public void testRunAfterEither_normalCompletion() { |
2454 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2455 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2456 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2457 |
|
{ |
2458 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2459 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2460 |
< |
final Noop r = new Noop(m); |
2461 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2460 |
> |
final Noop[] rs = new Noop[6]; |
2461 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2462 |
|
|
2463 |
< |
g.complete(v2); |
2464 |
< |
checkCompletedNormally(h, null); |
2465 |
< |
assertEquals(1, r.invocationCount); |
2463 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2464 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2465 |
> |
checkIncomplete(h0); |
2466 |
> |
checkIncomplete(h1); |
2467 |
> |
rs[0].assertNotInvoked(); |
2468 |
> |
rs[1].assertNotInvoked(); |
2469 |
|
f.complete(v1); |
2470 |
+ |
checkCompletedNormally(h0, null); |
2471 |
+ |
checkCompletedNormally(h1, null); |
2472 |
+ |
rs[0].assertInvoked(); |
2473 |
+ |
rs[1].assertInvoked(); |
2474 |
+ |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2475 |
+ |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2476 |
+ |
checkCompletedNormally(h2, null); |
2477 |
+ |
checkCompletedNormally(h3, null); |
2478 |
+ |
rs[2].assertInvoked(); |
2479 |
+ |
rs[3].assertInvoked(); |
2480 |
|
|
2266 |
– |
checkCompletedNormally(f, v1); |
2267 |
– |
checkCompletedNormally(g, v2); |
2268 |
– |
checkCompletedNormally(h, null); |
2269 |
– |
assertEquals(1, r.invocationCount); |
2270 |
– |
}} |
2271 |
– |
|
2272 |
– |
public void testRunAfterEither_normalCompletion3() { |
2273 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2274 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2275 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2276 |
– |
{ |
2277 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2278 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2279 |
– |
final Noop r = new Noop(m); |
2280 |
– |
|
2281 |
– |
f.complete(v1); |
2481 |
|
g.complete(v2); |
2283 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2482 |
|
|
2483 |
< |
checkCompletedNormally(h, null); |
2483 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2484 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2485 |
> |
|
2486 |
|
checkCompletedNormally(f, v1); |
2487 |
|
checkCompletedNormally(g, v2); |
2488 |
< |
assertEquals(1, r.invocationCount); |
2488 |
> |
checkCompletedNormally(h0, null); |
2489 |
> |
checkCompletedNormally(h1, null); |
2490 |
> |
checkCompletedNormally(h2, null); |
2491 |
> |
checkCompletedNormally(h3, null); |
2492 |
> |
checkCompletedNormally(h4, null); |
2493 |
> |
checkCompletedNormally(h5, null); |
2494 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2495 |
|
}} |
2496 |
|
|
2497 |
|
/** |
2498 |
|
* runAfterEither result completes exceptionally after exceptional |
2499 |
|
* completion of either source |
2500 |
|
*/ |
2501 |
< |
public void testRunAfterEither_exceptionalCompletion1() { |
2501 |
> |
public void testRunAfterEither_exceptionalCompletion() { |
2502 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2503 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2504 |
|
{ |
2505 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2506 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2301 |
– |
final Noop r = new Noop(m); |
2302 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2507 |
|
final CFException ex = new CFException(); |
2508 |
+ |
final Noop[] rs = new Noop[6]; |
2509 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2510 |
|
|
2511 |
+ |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2512 |
+ |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2513 |
+ |
checkIncomplete(h0); |
2514 |
+ |
checkIncomplete(h1); |
2515 |
+ |
rs[0].assertNotInvoked(); |
2516 |
+ |
rs[1].assertNotInvoked(); |
2517 |
|
f.completeExceptionally(ex); |
2518 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2518 |
> |
checkCompletedWithWrappedException(h0, ex); |
2519 |
> |
checkCompletedWithWrappedException(h1, ex); |
2520 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2521 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2522 |
> |
checkCompletedWithWrappedException(h2, ex); |
2523 |
> |
checkCompletedWithWrappedException(h3, ex); |
2524 |
> |
|
2525 |
|
g.complete(v1); |
2526 |
|
|
2527 |
< |
assertEquals(0, r.invocationCount); |
2527 |
> |
// unspecified behavior - both source completions available |
2528 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2529 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2530 |
> |
try { |
2531 |
> |
assertNull(h4.join()); |
2532 |
> |
rs[4].assertInvoked(); |
2533 |
> |
} catch (CompletionException ok) { |
2534 |
> |
checkCompletedWithWrappedException(h4, ex); |
2535 |
> |
rs[4].assertNotInvoked(); |
2536 |
> |
} |
2537 |
> |
try { |
2538 |
> |
assertNull(h5.join()); |
2539 |
> |
rs[5].assertInvoked(); |
2540 |
> |
} catch (CompletionException ok) { |
2541 |
> |
checkCompletedWithWrappedException(h5, ex); |
2542 |
> |
rs[5].assertNotInvoked(); |
2543 |
> |
} |
2544 |
> |
|
2545 |
> |
checkCompletedExceptionally(f, ex); |
2546 |
|
checkCompletedNormally(g, v1); |
2547 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2548 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2547 |
> |
checkCompletedWithWrappedException(h0, ex); |
2548 |
> |
checkCompletedWithWrappedException(h1, ex); |
2549 |
> |
checkCompletedWithWrappedException(h2, ex); |
2550 |
> |
checkCompletedWithWrappedException(h3, ex); |
2551 |
> |
checkCompletedWithWrappedException(h4, ex); |
2552 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2553 |
|
}} |
2554 |
|
|
2555 |
|
public void testRunAfterEither_exceptionalCompletion2() { |
2556 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2557 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2558 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2559 |
|
{ |
2560 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2561 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2321 |
– |
final Noop r = new Noop(m); |
2322 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2323 |
– |
final CFException ex = new CFException(); |
2324 |
– |
|
2325 |
– |
g.completeExceptionally(ex); |
2326 |
– |
checkCompletedWithWrappedCFException(h, ex); |
2327 |
– |
f.complete(v1); |
2328 |
– |
|
2329 |
– |
assertEquals(0, r.invocationCount); |
2330 |
– |
checkCompletedNormally(f, v1); |
2331 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2332 |
– |
checkCompletedWithWrappedCFException(h, ex); |
2333 |
– |
}} |
2334 |
– |
|
2335 |
– |
public void testRunAfterEither_exceptionalCompletion3() { |
2336 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2337 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2338 |
– |
{ |
2339 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2340 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2341 |
– |
final Noop r = new Noop(m); |
2562 |
|
final CFException ex = new CFException(); |
2563 |
+ |
final Noop[] rs = new Noop[6]; |
2564 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2565 |
|
|
2566 |
< |
g.completeExceptionally(ex); |
2567 |
< |
f.complete(v1); |
2568 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2566 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2567 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2568 |
> |
if (fFirst) { |
2569 |
> |
f.complete(v1); |
2570 |
> |
g.completeExceptionally(ex); |
2571 |
> |
} else { |
2572 |
> |
g.completeExceptionally(ex); |
2573 |
> |
f.complete(v1); |
2574 |
> |
} |
2575 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2576 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2577 |
|
|
2578 |
< |
// unspecified behavior |
2579 |
< |
Integer v; |
2578 |
> |
// unspecified behavior - both source completions available |
2579 |
> |
try { |
2580 |
> |
assertEquals(null, h0.join()); |
2581 |
> |
rs[0].assertInvoked(); |
2582 |
> |
} catch (CompletionException ok) { |
2583 |
> |
checkCompletedWithWrappedException(h0, ex); |
2584 |
> |
rs[0].assertNotInvoked(); |
2585 |
> |
} |
2586 |
> |
try { |
2587 |
> |
assertEquals(null, h1.join()); |
2588 |
> |
rs[1].assertInvoked(); |
2589 |
> |
} catch (CompletionException ok) { |
2590 |
> |
checkCompletedWithWrappedException(h1, ex); |
2591 |
> |
rs[1].assertNotInvoked(); |
2592 |
> |
} |
2593 |
> |
try { |
2594 |
> |
assertEquals(null, h2.join()); |
2595 |
> |
rs[2].assertInvoked(); |
2596 |
> |
} catch (CompletionException ok) { |
2597 |
> |
checkCompletedWithWrappedException(h2, ex); |
2598 |
> |
rs[2].assertNotInvoked(); |
2599 |
> |
} |
2600 |
|
try { |
2601 |
< |
assertNull(h.join()); |
2602 |
< |
assertEquals(1, r.invocationCount); |
2601 |
> |
assertEquals(null, h3.join()); |
2602 |
> |
rs[3].assertInvoked(); |
2603 |
|
} catch (CompletionException ok) { |
2604 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2605 |
< |
assertEquals(0, r.invocationCount); |
2604 |
> |
checkCompletedWithWrappedException(h3, ex); |
2605 |
> |
rs[3].assertNotInvoked(); |
2606 |
|
} |
2607 |
|
|
2358 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2608 |
|
checkCompletedNormally(f, v1); |
2609 |
+ |
checkCompletedExceptionally(g, ex); |
2610 |
|
}} |
2611 |
|
|
2612 |
< |
public void testRunAfterEither_exceptionalCompletion4() { |
2612 |
> |
/** |
2613 |
> |
* runAfterEither result completes exceptionally if either source cancelled |
2614 |
> |
*/ |
2615 |
> |
public void testRunAfterEither_sourceCancelled() { |
2616 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2617 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2618 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2619 |
|
{ |
2620 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2621 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2622 |
< |
final Noop r = new Noop(m); |
2623 |
< |
final CFException ex = new CFException(); |
2622 |
> |
final Noop[] rs = new Noop[6]; |
2623 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2624 |
> |
|
2625 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2626 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2627 |
> |
checkIncomplete(h0); |
2628 |
> |
checkIncomplete(h1); |
2629 |
> |
rs[0].assertNotInvoked(); |
2630 |
> |
rs[1].assertNotInvoked(); |
2631 |
> |
f.cancel(mayInterruptIfRunning); |
2632 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2633 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2634 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2635 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2636 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2637 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2638 |
|
|
2371 |
– |
f.completeExceptionally(ex); |
2639 |
|
g.complete(v1); |
2373 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2640 |
|
|
2641 |
< |
// unspecified behavior |
2642 |
< |
Integer v; |
2641 |
> |
// unspecified behavior - both source completions available |
2642 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2643 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2644 |
> |
try { |
2645 |
> |
assertNull(h4.join()); |
2646 |
> |
rs[4].assertInvoked(); |
2647 |
> |
} catch (CompletionException ok) { |
2648 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2649 |
> |
rs[4].assertNotInvoked(); |
2650 |
> |
} |
2651 |
|
try { |
2652 |
< |
assertNull(h.join()); |
2653 |
< |
assertEquals(1, r.invocationCount); |
2652 |
> |
assertNull(h5.join()); |
2653 |
> |
rs[5].assertInvoked(); |
2654 |
|
} catch (CompletionException ok) { |
2655 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2656 |
< |
assertEquals(0, r.invocationCount); |
2655 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2656 |
> |
rs[5].assertNotInvoked(); |
2657 |
|
} |
2658 |
|
|
2659 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2659 |
> |
checkCancelled(f); |
2660 |
|
checkCompletedNormally(g, v1); |
2661 |
+ |
checkCompletedWithWrappedCancellationException(h0); |
2662 |
+ |
checkCompletedWithWrappedCancellationException(h1); |
2663 |
+ |
checkCompletedWithWrappedCancellationException(h2); |
2664 |
+ |
checkCompletedWithWrappedCancellationException(h3); |
2665 |
+ |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2666 |
|
}} |
2667 |
|
|
2668 |
|
/** |
2669 |
|
* runAfterEither result completes exceptionally if action does |
2670 |
|
*/ |
2671 |
< |
public void testRunAfterEither_actionFailed1() { |
2671 |
> |
public void testRunAfterEither_actionFailed() { |
2672 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2673 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2674 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2675 |
|
{ |
2676 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2677 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2678 |
< |
final FailingRunnable r = new FailingRunnable(m); |
2679 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2678 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
2679 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
2680 |
|
|
2681 |
+ |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2682 |
+ |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2683 |
|
f.complete(v1); |
2684 |
< |
checkCompletedWithWrappedCFException(h); |
2684 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2685 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2686 |
> |
checkCompletedWithWrappedCFException(h0); |
2687 |
> |
checkCompletedWithWrappedCFException(h1); |
2688 |
> |
checkCompletedWithWrappedCFException(h2); |
2689 |
> |
checkCompletedWithWrappedCFException(h3); |
2690 |
> |
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2691 |
|
g.complete(v2); |
2692 |
< |
checkCompletedNormally(f, v1); |
2693 |
< |
checkCompletedNormally(g, v2); |
2694 |
< |
}} |
2692 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2693 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2694 |
> |
checkCompletedWithWrappedCFException(h4); |
2695 |
> |
checkCompletedWithWrappedCFException(h5); |
2696 |
|
|
2409 |
– |
public void testRunAfterEither_actionFailed2() { |
2410 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2411 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2412 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2413 |
– |
{ |
2414 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2415 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2416 |
– |
final FailingRunnable r = new FailingRunnable(m); |
2417 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2418 |
– |
|
2419 |
– |
g.complete(v2); |
2420 |
– |
checkCompletedWithWrappedCFException(h); |
2421 |
– |
f.complete(v1); |
2697 |
|
checkCompletedNormally(f, v1); |
2698 |
|
checkCompletedNormally(g, v2); |
2699 |
< |
}} |
2425 |
< |
|
2426 |
< |
/** |
2427 |
< |
* runAfterEither result completes exceptionally if either source cancelled |
2428 |
< |
*/ |
2429 |
< |
public void testRunAfterEither_sourceCancelled1() { |
2430 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2431 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2432 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2433 |
< |
{ |
2434 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2435 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2436 |
< |
final Noop r = new Noop(m); |
2437 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2438 |
< |
|
2439 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2440 |
< |
checkCompletedWithWrappedCancellationException(h); |
2441 |
< |
g.complete(v1); |
2442 |
< |
|
2443 |
< |
checkCancelled(f); |
2444 |
< |
assertEquals(0, r.invocationCount); |
2445 |
< |
checkCompletedNormally(g, v1); |
2446 |
< |
checkCompletedWithWrappedCancellationException(h); |
2447 |
< |
}} |
2448 |
< |
|
2449 |
< |
public void testRunAfterEither_sourceCancelled2() { |
2450 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2451 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2452 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2453 |
< |
{ |
2454 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2455 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2456 |
< |
final Noop r = new Noop(m); |
2457 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2458 |
< |
|
2459 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2460 |
< |
checkCompletedWithWrappedCancellationException(h); |
2461 |
< |
f.complete(v1); |
2462 |
< |
|
2463 |
< |
checkCancelled(g); |
2464 |
< |
assertEquals(0, r.invocationCount); |
2465 |
< |
checkCompletedNormally(f, v1); |
2466 |
< |
checkCompletedWithWrappedCancellationException(h); |
2467 |
< |
}} |
2468 |
< |
|
2469 |
< |
public void testRunAfterEither_sourceCancelled3() { |
2470 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2471 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2472 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2473 |
< |
{ |
2474 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2475 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2476 |
< |
final Noop r = new Noop(m); |
2477 |
< |
|
2478 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2479 |
< |
f.complete(v1); |
2480 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2481 |
< |
|
2482 |
< |
// unspecified behavior |
2483 |
< |
Integer v; |
2484 |
< |
try { |
2485 |
< |
assertNull(h.join()); |
2486 |
< |
assertEquals(1, r.invocationCount); |
2487 |
< |
} catch (CompletionException ok) { |
2488 |
< |
checkCompletedWithWrappedCancellationException(h); |
2489 |
< |
assertEquals(0, r.invocationCount); |
2490 |
< |
} |
2491 |
< |
|
2492 |
< |
checkCancelled(g); |
2493 |
< |
checkCompletedNormally(f, v1); |
2494 |
< |
}} |
2495 |
< |
|
2496 |
< |
public void testRunAfterEither_sourceCancelled4() { |
2497 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2498 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2499 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2500 |
< |
{ |
2501 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2502 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2503 |
< |
final Noop r = new Noop(m); |
2504 |
< |
|
2505 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2506 |
< |
g.complete(v1); |
2507 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2508 |
< |
|
2509 |
< |
// unspecified behavior |
2510 |
< |
Integer v; |
2511 |
< |
try { |
2512 |
< |
assertNull(h.join()); |
2513 |
< |
assertEquals(1, r.invocationCount); |
2514 |
< |
} catch (CompletionException ok) { |
2515 |
< |
checkCompletedWithWrappedCancellationException(h); |
2516 |
< |
assertEquals(0, r.invocationCount); |
2517 |
< |
} |
2518 |
< |
|
2519 |
< |
checkCancelled(f); |
2520 |
< |
checkCompletedNormally(g, v1); |
2699 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2700 |
|
}} |
2701 |
|
|
2702 |
|
/** |
2715 |
|
|
2716 |
|
checkCompletedNormally(g, inc(v1)); |
2717 |
|
checkCompletedNormally(f, v1); |
2718 |
< |
assertEquals(1, r.invocationCount); |
2718 |
> |
r.assertValue(v1); |
2719 |
|
}} |
2720 |
|
|
2721 |
|
/** |
2733 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2734 |
|
if (createIncomplete) f.completeExceptionally(ex); |
2735 |
|
|
2736 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2737 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2738 |
< |
assertEquals(0, r.invocationCount); |
2736 |
> |
checkCompletedWithWrappedException(g, ex); |
2737 |
> |
checkCompletedExceptionally(f, ex); |
2738 |
> |
r.assertNotInvoked(); |
2739 |
|
}} |
2740 |
|
|
2741 |
|
/** |
2990 |
|
assertSame(f, f.toCompletableFuture()); |
2991 |
|
} |
2992 |
|
|
2814 |
– |
/** |
2815 |
– |
* whenComplete action executes on normal completion, propagating |
2816 |
– |
* source result. |
2817 |
– |
*/ |
2818 |
– |
public void testWhenComplete_normalCompletion1() { |
2819 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2820 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2821 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2822 |
– |
{ |
2823 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2824 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2825 |
– |
if (!createIncomplete) f.complete(v1); |
2826 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2827 |
– |
(f, |
2828 |
– |
(Integer x, Throwable t) -> { |
2829 |
– |
threadAssertSame(x, v1); |
2830 |
– |
threadAssertNull(t); |
2831 |
– |
a.getAndIncrement(); |
2832 |
– |
}); |
2833 |
– |
if (createIncomplete) f.complete(v1); |
2834 |
– |
|
2835 |
– |
checkCompletedNormally(g, v1); |
2836 |
– |
checkCompletedNormally(f, v1); |
2837 |
– |
assertEquals(1, a.get()); |
2838 |
– |
}} |
2839 |
– |
|
2840 |
– |
/** |
2841 |
– |
* whenComplete action executes on exceptional completion, propagating |
2842 |
– |
* source result. |
2843 |
– |
*/ |
2844 |
– |
public void testWhenComplete_exceptionalCompletion() { |
2845 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2846 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2847 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2848 |
– |
{ |
2849 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2850 |
– |
final CFException ex = new CFException(); |
2851 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2852 |
– |
if (!createIncomplete) f.completeExceptionally(ex); |
2853 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2854 |
– |
(f, |
2855 |
– |
(Integer x, Throwable t) -> { |
2856 |
– |
threadAssertNull(x); |
2857 |
– |
threadAssertSame(t, ex); |
2858 |
– |
a.getAndIncrement(); |
2859 |
– |
}); |
2860 |
– |
if (createIncomplete) f.completeExceptionally(ex); |
2861 |
– |
checkCompletedWithWrappedCFException(f, ex); |
2862 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2863 |
– |
assertEquals(1, a.get()); |
2864 |
– |
}} |
2865 |
– |
|
2866 |
– |
/** |
2867 |
– |
* whenComplete action executes on cancelled source, propagating |
2868 |
– |
* CancellationException. |
2869 |
– |
*/ |
2870 |
– |
public void testWhenComplete_sourceCancelled() { |
2871 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2872 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2873 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2874 |
– |
{ |
2875 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2876 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2877 |
– |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2878 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2879 |
– |
(f, |
2880 |
– |
(Integer x, Throwable t) -> { |
2881 |
– |
threadAssertNull(x); |
2882 |
– |
threadAssertTrue(t instanceof CancellationException); |
2883 |
– |
a.getAndIncrement(); |
2884 |
– |
}); |
2885 |
– |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2886 |
– |
|
2887 |
– |
//try { g.join(); } catch (Throwable t) { throw new Error(t); } |
2888 |
– |
checkCompletedWithWrappedCancellationException(g); |
2889 |
– |
checkCancelled(f); |
2890 |
– |
assertEquals(1, a.get()); |
2891 |
– |
}} |
2892 |
– |
|
2893 |
– |
/** |
2894 |
– |
* If a whenComplete action throws an exception when triggered by |
2895 |
– |
* a normal completion, it completes exceptionally |
2896 |
– |
*/ |
2897 |
– |
public void testWhenComplete_actionFailed() { |
2898 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2899 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2900 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2901 |
– |
{ |
2902 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2903 |
– |
final CFException ex = new CFException(); |
2904 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2905 |
– |
if (!createIncomplete) f.complete(v1); |
2906 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2907 |
– |
(f, |
2908 |
– |
(Integer x, Throwable t) -> { |
2909 |
– |
threadAssertSame(x, v1); |
2910 |
– |
threadAssertNull(t); |
2911 |
– |
a.getAndIncrement(); |
2912 |
– |
throw ex; |
2913 |
– |
}); |
2914 |
– |
if (createIncomplete) f.complete(v1); |
2915 |
– |
checkCompletedNormally(f, v1); |
2916 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2917 |
– |
assertEquals(1, a.get()); |
2918 |
– |
}} |
2919 |
– |
|
2920 |
– |
/** |
2921 |
– |
* If a whenComplete action throws an exception when triggered by |
2922 |
– |
* a source completion that also throws an exception, the source |
2923 |
– |
* exception takes precedence. |
2924 |
– |
*/ |
2925 |
– |
public void testWhenComplete_actionFailedSourceFailed() { |
2926 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2927 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2928 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2929 |
– |
{ |
2930 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2931 |
– |
final CFException ex1 = new CFException(); |
2932 |
– |
final CFException ex2 = new CFException(); |
2933 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2934 |
– |
|
2935 |
– |
if (!createIncomplete) f.completeExceptionally(ex1); |
2936 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2937 |
– |
(f, |
2938 |
– |
(Integer x, Throwable t) -> { |
2939 |
– |
threadAssertSame(t, ex1); |
2940 |
– |
threadAssertNull(x); |
2941 |
– |
a.getAndIncrement(); |
2942 |
– |
throw ex2; |
2943 |
– |
}); |
2944 |
– |
if (createIncomplete) f.completeExceptionally(ex1); |
2945 |
– |
|
2946 |
– |
checkCompletedWithWrappedCFException(f, ex1); |
2947 |
– |
checkCompletedWithWrappedCFException(g, ex1); |
2948 |
– |
assertEquals(1, a.get()); |
2949 |
– |
}} |
2950 |
– |
|
2993 |
|
} |