320 |
|
checkCompletedNormally(f, "test"); |
321 |
|
} |
322 |
|
|
323 |
< |
// Choose non-commutative actions for better coverage |
324 |
< |
|
325 |
< |
// A non-commutative function that handles and produces null values as well. |
326 |
< |
static Integer subtract(Integer x, Integer y) { |
327 |
< |
return (x == null && y == null) ? null : |
328 |
< |
((x == null) ? 42 : x.intValue()) |
329 |
< |
- ((y == null) ? 99 : y.intValue()); |
323 |
> |
static final class IntegerSupplier implements Supplier<Integer> { |
324 |
> |
final ExecutionMode m; |
325 |
> |
int invocationCount = 0; |
326 |
> |
final Integer value; |
327 |
> |
IntegerSupplier(ExecutionMode m, Integer value) { |
328 |
> |
this.m = m; |
329 |
> |
this.value = value; |
330 |
> |
} |
331 |
> |
public Integer get() { |
332 |
> |
m.checkExecutionMode(); |
333 |
> |
invocationCount++; |
334 |
> |
return value; |
335 |
> |
} |
336 |
|
} |
337 |
|
|
338 |
|
// A function that handles and produces null values as well. |
340 |
|
return (x == null) ? null : x + 1; |
341 |
|
} |
342 |
|
|
337 |
– |
static final Supplier<Integer> supplyOne = |
338 |
– |
() -> Integer.valueOf(1); |
339 |
– |
static final Function<Integer, Integer> inc = |
340 |
– |
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
341 |
– |
static final BiFunction<Integer, Integer, Integer> subtract = |
342 |
– |
(Integer x, Integer y) -> subtract(x, y); |
343 |
|
static final class IncAction implements Consumer<Integer> { |
344 |
|
int invocationCount = 0; |
345 |
|
Integer value; |
349 |
|
} |
350 |
|
} |
351 |
|
static final class IncFunction implements Function<Integer,Integer> { |
352 |
+ |
final ExecutionMode m; |
353 |
|
int invocationCount = 0; |
354 |
|
Integer value; |
355 |
+ |
IncFunction(ExecutionMode m) { this.m = m; } |
356 |
|
public Integer apply(Integer x) { |
357 |
+ |
m.checkExecutionMode(); |
358 |
|
invocationCount++; |
359 |
|
return value = inc(x); |
360 |
|
} |
361 |
|
} |
362 |
+ |
|
363 |
+ |
// Choose non-commutative actions for better coverage |
364 |
+ |
// A non-commutative function that handles and produces null values as well. |
365 |
+ |
static Integer subtract(Integer x, Integer y) { |
366 |
+ |
return (x == null && y == null) ? null : |
367 |
+ |
((x == null) ? 42 : x.intValue()) |
368 |
+ |
- ((y == null) ? 99 : y.intValue()); |
369 |
+ |
} |
370 |
+ |
|
371 |
|
static final class SubtractAction implements BiConsumer<Integer, Integer> { |
372 |
+ |
final ExecutionMode m; |
373 |
|
int invocationCount = 0; |
374 |
|
Integer value; |
375 |
|
// Check this action was invoked exactly once when result is computed. |
376 |
+ |
SubtractAction(ExecutionMode m) { this.m = m; } |
377 |
|
public void accept(Integer x, Integer y) { |
378 |
+ |
m.checkExecutionMode(); |
379 |
|
invocationCount++; |
380 |
|
value = subtract(x, y); |
381 |
|
} |
382 |
|
} |
383 |
|
static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> { |
384 |
+ |
final ExecutionMode m; |
385 |
|
int invocationCount = 0; |
386 |
|
Integer value; |
387 |
|
// Check this action was invoked exactly once when result is computed. |
388 |
+ |
SubtractFunction(ExecutionMode m) { this.m = m; } |
389 |
|
public Integer apply(Integer x, Integer y) { |
390 |
+ |
m.checkExecutionMode(); |
391 |
|
invocationCount++; |
392 |
|
return value = subtract(x, y); |
393 |
|
} |
394 |
|
} |
395 |
+ |
|
396 |
|
static final class Noop implements Runnable { |
397 |
|
final ExecutionMode m; |
398 |
|
int invocationCount = 0; |
404 |
|
} |
405 |
|
|
406 |
|
static final class FailingSupplier implements Supplier<Integer> { |
407 |
+ |
final ExecutionMode m; |
408 |
|
int invocationCount = 0; |
409 |
+ |
FailingSupplier(ExecutionMode m) { this.m = m; } |
410 |
|
public Integer get() { |
411 |
+ |
m.checkExecutionMode(); |
412 |
|
invocationCount++; |
413 |
|
throw new CFException(); |
414 |
|
} |
415 |
|
} |
416 |
|
static final class FailingConsumer implements Consumer<Integer> { |
417 |
+ |
final ExecutionMode m; |
418 |
|
int invocationCount = 0; |
419 |
+ |
FailingConsumer(ExecutionMode m) { this.m = m; } |
420 |
|
public void accept(Integer x) { |
421 |
+ |
m.checkExecutionMode(); |
422 |
|
invocationCount++; |
423 |
|
throw new CFException(); |
424 |
|
} |
425 |
|
} |
426 |
|
static final class FailingBiConsumer implements BiConsumer<Integer, Integer> { |
427 |
+ |
final ExecutionMode m; |
428 |
|
int invocationCount = 0; |
429 |
+ |
FailingBiConsumer(ExecutionMode m) { this.m = m; } |
430 |
|
public void accept(Integer x, Integer y) { |
431 |
+ |
m.checkExecutionMode(); |
432 |
|
invocationCount++; |
433 |
|
throw new CFException(); |
434 |
|
} |
435 |
|
} |
436 |
|
static final class FailingFunction implements Function<Integer, Integer> { |
437 |
+ |
final ExecutionMode m; |
438 |
|
int invocationCount = 0; |
439 |
+ |
FailingFunction(ExecutionMode m) { this.m = m; } |
440 |
|
public Integer apply(Integer x) { |
441 |
+ |
m.checkExecutionMode(); |
442 |
|
invocationCount++; |
443 |
|
throw new CFException(); |
444 |
|
} |
445 |
|
} |
446 |
|
static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> { |
447 |
+ |
final ExecutionMode m; |
448 |
|
int invocationCount = 0; |
449 |
+ |
FailingBiFunction(ExecutionMode m) { this.m = m; } |
450 |
|
public Integer apply(Integer x, Integer y) { |
451 |
+ |
m.checkExecutionMode(); |
452 |
|
invocationCount++; |
453 |
|
throw new CFException(); |
454 |
|
} |
455 |
|
} |
456 |
|
static final class FailingRunnable implements Runnable { |
457 |
+ |
final ExecutionMode m; |
458 |
|
int invocationCount = 0; |
459 |
+ |
FailingRunnable(ExecutionMode m) { this.m = m; } |
460 |
|
public void run() { |
461 |
+ |
m.checkExecutionMode(); |
462 |
|
invocationCount++; |
463 |
|
throw new CFException(); |
464 |
|
} |
466 |
|
|
467 |
|
static final class CompletableFutureInc |
468 |
|
implements Function<Integer, CompletableFuture<Integer>> { |
469 |
+ |
final ExecutionMode m; |
470 |
|
int invocationCount = 0; |
471 |
+ |
CompletableFutureInc(ExecutionMode m) { this.m = m; } |
472 |
|
public CompletableFuture<Integer> apply(Integer x) { |
473 |
+ |
m.checkExecutionMode(); |
474 |
|
invocationCount++; |
475 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
476 |
|
f.complete(inc(x)); |
480 |
|
|
481 |
|
static final class FailingCompletableFutureFunction |
482 |
|
implements Function<Integer, CompletableFuture<Integer>> { |
483 |
+ |
final ExecutionMode m; |
484 |
|
int invocationCount = 0; |
485 |
+ |
FailingCompletableFutureFunction(ExecutionMode m) { this.m = m; } |
486 |
|
public CompletableFuture<Integer> apply(Integer x) { |
487 |
+ |
m.checkExecutionMode(); |
488 |
|
invocationCount++; |
489 |
|
throw new CFException(); |
490 |
|
} |
492 |
|
|
493 |
|
// Used for explicit executor tests |
494 |
|
static final class ThreadExecutor implements Executor { |
495 |
< |
AtomicInteger count = new AtomicInteger(0); |
495 |
> |
final AtomicInteger count = new AtomicInteger(0); |
496 |
> |
static final ThreadGroup tg = new ThreadGroup("ThreadExecutor"); |
497 |
> |
static boolean startedCurrentThread() { |
498 |
> |
return Thread.currentThread().getThreadGroup() == tg; |
499 |
> |
} |
500 |
|
|
501 |
|
public void execute(Runnable r) { |
502 |
|
count.getAndIncrement(); |
503 |
< |
new Thread(r).start(); |
503 |
> |
new Thread(tg, r).start(); |
504 |
|
} |
505 |
|
} |
506 |
|
|
507 |
|
/** |
508 |
|
* Permits the testing of parallel code for the 3 different |
509 |
< |
* execution modes without repeating all the testing code. |
509 |
> |
* execution modes without copy/pasting all the test methods. |
510 |
|
*/ |
511 |
|
enum ExecutionMode { |
512 |
|
DEFAULT { |
513 |
|
public void checkExecutionMode() { |
514 |
+ |
assertFalse(ThreadExecutor.startedCurrentThread()); |
515 |
|
assertNull(ForkJoinTask.getPool()); |
516 |
|
} |
517 |
+ |
public CompletableFuture<Void> runAsync(Runnable a) { |
518 |
+ |
throw new UnsupportedOperationException(); |
519 |
+ |
} |
520 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
521 |
+ |
throw new UnsupportedOperationException(); |
522 |
+ |
} |
523 |
|
public <T> CompletableFuture<Void> thenRun |
524 |
|
(CompletableFuture<T> f, Runnable a) { |
525 |
|
return f.thenRun(a); |
588 |
|
assertSame(ForkJoinPool.commonPool(), |
589 |
|
ForkJoinTask.getPool()); |
590 |
|
} |
591 |
+ |
public CompletableFuture<Void> runAsync(Runnable a) { |
592 |
+ |
return CompletableFuture.runAsync(a); |
593 |
+ |
} |
594 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
595 |
+ |
return CompletableFuture.supplyAsync(a); |
596 |
+ |
} |
597 |
|
public <T> CompletableFuture<Void> thenRun |
598 |
|
(CompletableFuture<T> f, Runnable a) { |
599 |
|
return f.thenRunAsync(a); |
659 |
|
|
660 |
|
EXECUTOR { |
661 |
|
public void checkExecutionMode() { |
662 |
< |
//TODO |
662 |
> |
assertTrue(ThreadExecutor.startedCurrentThread()); |
663 |
> |
} |
664 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
665 |
> |
return CompletableFuture.runAsync(a, new ThreadExecutor()); |
666 |
> |
} |
667 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
668 |
> |
return CompletableFuture.supplyAsync(a, new ThreadExecutor()); |
669 |
|
} |
670 |
|
public <T> CompletableFuture<Void> thenRun |
671 |
|
(CompletableFuture<T> f, Runnable a) { |
731 |
|
}; |
732 |
|
|
733 |
|
public abstract void checkExecutionMode(); |
734 |
+ |
public abstract CompletableFuture<Void> runAsync(Runnable a); |
735 |
+ |
public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); |
736 |
|
public abstract <T> CompletableFuture<Void> thenRun |
737 |
|
(CompletableFuture<T> f, Runnable a); |
738 |
|
public abstract <T> CompletableFuture<Void> thenAccept |
811 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
812 |
|
final CompletableFuture<Integer> g = f.exceptionally |
813 |
|
((Throwable t) -> { |
814 |
+ |
ExecutionMode.DEFAULT.checkExecutionMode(); |
815 |
|
threadAssertSame(t, ex); |
816 |
|
a.getAndIncrement(); |
817 |
|
return v1; |
833 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
834 |
|
final CompletableFuture<Integer> g = f.exceptionally |
835 |
|
((Throwable t) -> { |
836 |
+ |
ExecutionMode.DEFAULT.checkExecutionMode(); |
837 |
|
threadAssertSame(t, ex1); |
838 |
|
a.getAndIncrement(); |
839 |
|
throw ex2; |
859 |
|
final CompletableFuture<Integer> g = m.handle |
860 |
|
(f, |
861 |
|
(Integer x, Throwable t) -> { |
862 |
+ |
m.checkExecutionMode(); |
863 |
|
threadAssertSame(x, v1); |
864 |
|
threadAssertNull(t); |
865 |
|
a.getAndIncrement(); |
888 |
|
final CompletableFuture<Integer> g = m.handle |
889 |
|
(f, |
890 |
|
(Integer x, Throwable t) -> { |
891 |
+ |
m.checkExecutionMode(); |
892 |
|
threadAssertNull(x); |
893 |
|
threadAssertSame(t, ex); |
894 |
|
a.getAndIncrement(); |
917 |
|
final CompletableFuture<Integer> g = m.handle |
918 |
|
(f, |
919 |
|
(Integer x, Throwable t) -> { |
920 |
+ |
m.checkExecutionMode(); |
921 |
|
threadAssertNull(x); |
922 |
|
threadAssertTrue(t instanceof CancellationException); |
923 |
|
a.getAndIncrement(); |
945 |
|
final CompletableFuture<Integer> g = m.handle |
946 |
|
(f, |
947 |
|
(Integer x, Throwable t) -> { |
948 |
+ |
m.checkExecutionMode(); |
949 |
|
threadAssertNull(x); |
950 |
|
threadAssertSame(ex1, t); |
951 |
|
a.getAndIncrement(); |
970 |
|
final CompletableFuture<Integer> g = m.handle |
971 |
|
(f, |
972 |
|
(Integer x, Throwable t) -> { |
973 |
+ |
m.checkExecutionMode(); |
974 |
|
threadAssertSame(x, v1); |
975 |
|
threadAssertNull(t); |
976 |
|
a.getAndIncrement(); |
986 |
|
/** |
987 |
|
* runAsync completes after running Runnable |
988 |
|
*/ |
989 |
< |
public void testRunAsync() { |
990 |
< |
Noop r = new Noop(ExecutionMode.ASYNC); |
991 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
989 |
> |
public void testRunAsync_normalCompletion() { |
990 |
> |
ExecutionMode[] executionModes = { |
991 |
> |
ExecutionMode.ASYNC, |
992 |
> |
ExecutionMode.EXECUTOR, |
993 |
> |
}; |
994 |
> |
for (ExecutionMode m : executionModes) |
995 |
> |
{ |
996 |
> |
final Noop r = new Noop(m); |
997 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
998 |
|
assertNull(f.join()); |
918 |
– |
assertEquals(1, r.invocationCount); |
999 |
|
checkCompletedNormally(f, null); |
920 |
– |
} |
921 |
– |
|
922 |
– |
/** |
923 |
– |
* runAsync with executor completes after running Runnable |
924 |
– |
*/ |
925 |
– |
public void testRunAsync2() { |
926 |
– |
Noop r = new Noop(ExecutionMode.EXECUTOR); |
927 |
– |
ThreadExecutor exec = new ThreadExecutor(); |
928 |
– |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
929 |
– |
assertNull(f.join()); |
1000 |
|
assertEquals(1, r.invocationCount); |
1001 |
< |
checkCompletedNormally(f, null); |
932 |
< |
assertEquals(1, exec.count.get()); |
933 |
< |
} |
1001 |
> |
}} |
1002 |
|
|
1003 |
|
/** |
1004 |
|
* failing runAsync completes exceptionally after running Runnable |
1005 |
|
*/ |
1006 |
< |
public void testRunAsync3() { |
1007 |
< |
FailingRunnable r = new FailingRunnable(); |
1008 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
1006 |
> |
public void testRunAsync_exceptionalCompletion() { |
1007 |
> |
ExecutionMode[] executionModes = { |
1008 |
> |
ExecutionMode.ASYNC, |
1009 |
> |
ExecutionMode.EXECUTOR, |
1010 |
> |
}; |
1011 |
> |
for (ExecutionMode m : executionModes) |
1012 |
> |
{ |
1013 |
> |
final FailingRunnable r = new FailingRunnable(m); |
1014 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1015 |
|
checkCompletedWithWrappedCFException(f); |
1016 |
|
assertEquals(1, r.invocationCount); |
1017 |
< |
} |
1017 |
> |
}} |
1018 |
|
|
1019 |
|
/** |
1020 |
|
* supplyAsync completes with result of supplier |
1021 |
|
*/ |
1022 |
< |
public void testSupplyAsync() { |
1023 |
< |
CompletableFuture<Integer> f; |
1024 |
< |
f = CompletableFuture.supplyAsync(supplyOne); |
1025 |
< |
assertEquals(f.join(), one); |
1026 |
< |
checkCompletedNormally(f, one); |
1027 |
< |
} |
1028 |
< |
|
1029 |
< |
/** |
1030 |
< |
* supplyAsync with executor completes with result of supplier |
1031 |
< |
*/ |
1032 |
< |
public void testSupplyAsync2() { |
1033 |
< |
CompletableFuture<Integer> f; |
1034 |
< |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
1035 |
< |
assertEquals(f.join(), one); |
962 |
< |
checkCompletedNormally(f, one); |
963 |
< |
} |
1022 |
> |
public void testSupplyAsync_normalCompletion() { |
1023 |
> |
ExecutionMode[] executionModes = { |
1024 |
> |
ExecutionMode.ASYNC, |
1025 |
> |
ExecutionMode.EXECUTOR, |
1026 |
> |
}; |
1027 |
> |
for (ExecutionMode m : executionModes) |
1028 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1029 |
> |
{ |
1030 |
> |
final IntegerSupplier r = new IntegerSupplier(m, v1); |
1031 |
> |
final CompletableFuture<Integer> f = m.supplyAsync(r); |
1032 |
> |
assertSame(v1, f.join()); |
1033 |
> |
checkCompletedNormally(f, v1); |
1034 |
> |
assertEquals(1, r.invocationCount); |
1035 |
> |
}} |
1036 |
|
|
1037 |
|
/** |
1038 |
|
* Failing supplyAsync completes exceptionally |
1039 |
|
*/ |
1040 |
< |
public void testSupplyAsync3() { |
1041 |
< |
FailingSupplier r = new FailingSupplier(); |
1042 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r); |
1040 |
> |
public void testSupplyAsync_exceptionalCompletion() { |
1041 |
> |
ExecutionMode[] executionModes = { |
1042 |
> |
ExecutionMode.ASYNC, |
1043 |
> |
ExecutionMode.EXECUTOR, |
1044 |
> |
}; |
1045 |
> |
for (ExecutionMode m : executionModes) |
1046 |
> |
{ |
1047 |
> |
FailingSupplier r = new FailingSupplier(m); |
1048 |
> |
CompletableFuture<Integer> f = m.supplyAsync(r); |
1049 |
|
checkCompletedWithWrappedCFException(f); |
1050 |
|
assertEquals(1, r.invocationCount); |
1051 |
< |
} |
1051 |
> |
}} |
1052 |
|
|
1053 |
|
// seq completion methods |
1054 |
|
|
1108 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1109 |
|
final Noop r = new Noop(m); |
1110 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1111 |
< |
final CompletableFuture<Void> g = f.thenRun(r); |
1111 |
> |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1112 |
|
if (createIncomplete) { |
1113 |
|
checkIncomplete(g); |
1114 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
1128 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1129 |
|
{ |
1130 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1131 |
< |
final FailingRunnable r = new FailingRunnable(); |
1131 |
> |
final FailingRunnable r = new FailingRunnable(m); |
1132 |
|
if (!createIncomplete) f.complete(v1); |
1133 |
< |
final CompletableFuture<Void> g = f.thenRun(r); |
1133 |
> |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1134 |
|
if (createIncomplete) { |
1135 |
|
checkIncomplete(g); |
1136 |
|
f.complete(v1); |
1149 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1150 |
|
{ |
1151 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1152 |
< |
final IncFunction r = new IncFunction(); |
1152 |
> |
final IncFunction r = new IncFunction(m); |
1153 |
|
if (!createIncomplete) f.complete(v1); |
1154 |
|
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1155 |
|
if (createIncomplete) { |
1172 |
|
{ |
1173 |
|
final CFException ex = new CFException(); |
1174 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1175 |
< |
final IncFunction r = new IncFunction(); |
1175 |
> |
final IncFunction r = new IncFunction(m); |
1176 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1177 |
|
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1178 |
|
if (createIncomplete) { |
1194 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1195 |
|
{ |
1196 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1197 |
< |
final IncFunction r = new IncFunction(); |
1197 |
> |
final IncFunction r = new IncFunction(m); |
1198 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1199 |
< |
final CompletableFuture<Integer> g = f.thenApply(r); |
1199 |
> |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1200 |
|
if (createIncomplete) { |
1201 |
|
checkIncomplete(g); |
1202 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
1216 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1217 |
|
{ |
1218 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1219 |
< |
final FailingFunction r = new FailingFunction(); |
1219 |
> |
final FailingFunction r = new FailingFunction(m); |
1220 |
|
if (!createIncomplete) f.complete(v1); |
1221 |
< |
final CompletableFuture<Integer> g = f.thenApply(r); |
1221 |
> |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1222 |
|
if (createIncomplete) { |
1223 |
|
checkIncomplete(g); |
1224 |
|
f.complete(v1); |
1283 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1284 |
|
{ |
1285 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1286 |
< |
final FailingConsumer r = new FailingConsumer(); |
1286 |
> |
final FailingConsumer r = new FailingConsumer(m); |
1287 |
|
if (!createIncomplete) f.complete(v1); |
1288 |
< |
final CompletableFuture<Void> g = f.thenAccept(r); |
1288 |
> |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1289 |
|
if (createIncomplete) { |
1290 |
|
checkIncomplete(g); |
1291 |
|
f.complete(v1); |
1306 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1307 |
|
final IncAction r = new IncAction(); |
1308 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1309 |
< |
final CompletableFuture<Void> g = f.thenAccept(r); |
1309 |
> |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1310 |
|
if (createIncomplete) { |
1311 |
|
checkIncomplete(g); |
1312 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
1330 |
|
{ |
1331 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1332 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1333 |
< |
final SubtractFunction r = new SubtractFunction(); |
1333 |
> |
final SubtractFunction r = new SubtractFunction(m); |
1334 |
|
|
1335 |
|
if (fFirst) f.complete(v1); else g.complete(v2); |
1336 |
|
if (!createIncomplete) |
1361 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1362 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1363 |
|
final CFException ex = new CFException(); |
1364 |
< |
final SubtractFunction r = new SubtractFunction(); |
1364 |
> |
final SubtractFunction r = new SubtractFunction(m); |
1365 |
|
|
1366 |
|
(fFirst ? f : g).complete(v1); |
1367 |
|
if (!createIncomplete) |
1389 |
|
{ |
1390 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1391 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1392 |
< |
final FailingBiFunction r = new FailingBiFunction(); |
1392 |
> |
final FailingBiFunction r = new FailingBiFunction(m); |
1393 |
|
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1394 |
|
|
1395 |
|
if (fFirst) { |
1417 |
|
{ |
1418 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1419 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1420 |
< |
final SubtractFunction r = new SubtractFunction(); |
1420 |
> |
final SubtractFunction r = new SubtractFunction(m); |
1421 |
|
|
1422 |
|
(fFirst ? f : g).complete(v1); |
1423 |
|
if (!createIncomplete) |
1447 |
|
{ |
1448 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1449 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1450 |
< |
final SubtractAction r = new SubtractAction(); |
1450 |
> |
final SubtractAction r = new SubtractAction(m); |
1451 |
|
|
1452 |
|
if (fFirst) f.complete(v1); else g.complete(v2); |
1453 |
|
if (!createIncomplete) |
1478 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1479 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1480 |
|
final CFException ex = new CFException(); |
1481 |
< |
final SubtractAction r = new SubtractAction(); |
1481 |
> |
final SubtractAction r = new SubtractAction(m); |
1482 |
|
|
1483 |
|
(fFirst ? f : g).complete(v1); |
1484 |
|
if (!createIncomplete) |
1506 |
|
{ |
1507 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1508 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1509 |
< |
final FailingBiConsumer r = new FailingBiConsumer(); |
1509 |
> |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1510 |
|
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1511 |
|
|
1512 |
|
if (fFirst) { |
1534 |
|
{ |
1535 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1536 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1537 |
< |
final SubtractAction r = new SubtractAction(); |
1537 |
> |
final SubtractAction r = new SubtractAction(m); |
1538 |
|
|
1539 |
|
(fFirst ? f : g).complete(v1); |
1540 |
|
if (!createIncomplete) |
1623 |
|
{ |
1624 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1625 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1626 |
< |
final FailingRunnable r = new FailingRunnable(); |
1626 |
> |
final FailingRunnable r = new FailingRunnable(m); |
1627 |
|
|
1628 |
|
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r); |
1629 |
|
if (fFirst) { |
1684 |
|
{ |
1685 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1686 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1687 |
< |
final IncFunction r = new IncFunction(); |
1687 |
> |
final IncFunction r = new IncFunction(m); |
1688 |
|
|
1689 |
|
if (!createIncomplete) |
1690 |
|
if (fFirst) f.complete(v1); else g.complete(v2); |
1710 |
|
{ |
1711 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1712 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1713 |
< |
final IncFunction r = new IncFunction(); |
1713 |
> |
final IncFunction r = new IncFunction(m); |
1714 |
|
|
1715 |
|
if (fFirst) { |
1716 |
|
f.complete(v1); |
1744 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1745 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1746 |
|
final CFException ex = new CFException(); |
1747 |
< |
final IncFunction r = new IncFunction(); |
1747 |
> |
final IncFunction r = new IncFunction(m); |
1748 |
|
|
1749 |
|
if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex); |
1750 |
|
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1771 |
|
{ |
1772 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1773 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1774 |
< |
final IncFunction r1 = new IncFunction(); |
1775 |
< |
final IncFunction r2 = new IncFunction(); |
1774 |
> |
final IncFunction r1 = new IncFunction(m); |
1775 |
> |
final IncFunction r2 = new IncFunction(m); |
1776 |
|
final CFException ex = new CFException(); |
1777 |
|
final CompletableFuture<Integer> j = (reverseArgs ? g : f); |
1778 |
|
final CompletableFuture<Integer> k = (reverseArgs ? f : g); |
1817 |
|
{ |
1818 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1819 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1820 |
< |
final FailingFunction r = new FailingFunction(); |
1820 |
> |
final FailingFunction r = new FailingFunction(m); |
1821 |
|
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1822 |
|
|
1823 |
|
f.complete(v1); |
1834 |
|
{ |
1835 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1836 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1837 |
< |
final FailingFunction r = new FailingFunction(); |
1837 |
> |
final FailingFunction r = new FailingFunction(m); |
1838 |
|
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1839 |
|
|
1840 |
|
g.complete(v2); |
1856 |
|
{ |
1857 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1858 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1859 |
< |
final IncFunction r = new IncFunction(); |
1859 |
> |
final IncFunction r = new IncFunction(m); |
1860 |
|
|
1861 |
|
if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1862 |
|
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1884 |
|
{ |
1885 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1886 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1887 |
< |
final IncFunction r1 = new IncFunction(); |
1888 |
< |
final IncFunction r2 = new IncFunction(); |
1887 |
> |
final IncFunction r1 = new IncFunction(m); |
1888 |
> |
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); |
2097 |
|
{ |
2098 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2099 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2100 |
< |
final FailingConsumer r = new FailingConsumer(); |
2100 |
> |
final FailingConsumer r = new FailingConsumer(m); |
2101 |
|
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2102 |
|
|
2103 |
|
f.complete(v1); |
2114 |
|
{ |
2115 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2116 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2117 |
< |
final FailingConsumer r = new FailingConsumer(); |
2117 |
> |
final FailingConsumer r = new FailingConsumer(m); |
2118 |
|
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2119 |
|
|
2120 |
|
g.complete(v2); |
2396 |
|
{ |
2397 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2398 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2399 |
< |
final FailingRunnable r = new FailingRunnable(); |
2399 |
> |
final FailingRunnable r = new FailingRunnable(m); |
2400 |
|
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2401 |
|
|
2402 |
|
f.complete(v1); |
2413 |
|
{ |
2414 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2415 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2416 |
< |
final FailingRunnable r = new FailingRunnable(); |
2416 |
> |
final FailingRunnable r = new FailingRunnable(m); |
2417 |
|
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2418 |
|
|
2419 |
|
g.complete(v2); |
2529 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2530 |
|
{ |
2531 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2532 |
< |
final CompletableFutureInc r = new CompletableFutureInc(); |
2532 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2533 |
|
if (!createIncomplete) f.complete(v1); |
2534 |
< |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2534 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2535 |
|
if (createIncomplete) f.complete(v1); |
2536 |
|
|
2537 |
|
checkCompletedNormally(g, inc(v1)); |
2548 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
2549 |
|
{ |
2550 |
|
final CFException ex = new CFException(); |
2551 |
< |
final CompletableFutureInc r = new CompletableFutureInc(); |
2551 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2552 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2553 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
2554 |
< |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2554 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2555 |
|
if (createIncomplete) f.completeExceptionally(ex); |
2556 |
|
|
2557 |
|
checkCompletedWithWrappedCFException(g, ex); |
2569 |
|
{ |
2570 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2571 |
|
final FailingCompletableFutureFunction r |
2572 |
< |
= new FailingCompletableFutureFunction(); |
2572 |
> |
= new FailingCompletableFutureFunction(m); |
2573 |
|
if (!createIncomplete) f.complete(v1); |
2574 |
< |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2574 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2575 |
|
if (createIncomplete) f.complete(v1); |
2576 |
|
|
2577 |
|
checkCompletedWithWrappedCFException(g); |
2587 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2588 |
|
{ |
2589 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2590 |
< |
final CompletableFutureInc r = new CompletableFutureInc(); |
2590 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2591 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2592 |
< |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2592 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2593 |
|
if (createIncomplete) { |
2594 |
|
checkIncomplete(g); |
2595 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
2616 |
|
*/ |
2617 |
|
public void testAllOf_normal() throws Exception { |
2618 |
|
for (int k = 1; k < 20; ++k) { |
2619 |
< |
CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2619 |
> |
CompletableFuture<Integer>[] fs |
2620 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2621 |
|
for (int i = 0; i < k; ++i) |
2622 |
|
fs[i] = new CompletableFuture<>(); |
2623 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2631 |
|
} |
2632 |
|
} |
2633 |
|
|
2634 |
+ |
public void testAllOf_backwards() throws Exception { |
2635 |
+ |
for (int k = 1; k < 20; ++k) { |
2636 |
+ |
CompletableFuture<Integer>[] fs |
2637 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2638 |
+ |
for (int i = 0; i < k; ++i) |
2639 |
+ |
fs[i] = new CompletableFuture<>(); |
2640 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2641 |
+ |
for (int i = k - 1; i >= 0; i--) { |
2642 |
+ |
checkIncomplete(f); |
2643 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
2644 |
+ |
fs[i].complete(one); |
2645 |
+ |
} |
2646 |
+ |
checkCompletedNormally(f, null); |
2647 |
+ |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
2648 |
+ |
} |
2649 |
+ |
} |
2650 |
+ |
|
2651 |
|
/** |
2652 |
|
* anyOf(no component futures) returns an incomplete future |
2653 |
|
*/ |
2706 |
|
Runnable[] throwingActions = { |
2707 |
|
() -> CompletableFuture.supplyAsync(null), |
2708 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
2709 |
< |
() -> CompletableFuture.supplyAsync(supplyOne, null), |
2709 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
2710 |
|
|
2711 |
|
() -> CompletableFuture.runAsync(null), |
2712 |
|
() -> CompletableFuture.runAsync(null, exec), |
2779 |
|
|
2780 |
|
() -> f.thenCompose(null), |
2781 |
|
() -> f.thenComposeAsync(null), |
2782 |
< |
() -> f.thenComposeAsync(new CompletableFutureInc(), null), |
2782 |
> |
() -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null), |
2783 |
|
() -> f.thenComposeAsync(null, exec), |
2784 |
|
|
2785 |
|
() -> f.exceptionally(null), |