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; |
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; |
392 |
|
return value = subtract(x, y); |
393 |
|
} |
394 |
|
} |
395 |
+ |
|
396 |
|
static final class Noop implements Runnable { |
397 |
|
final ExecutionMode m; |
398 |
|
int invocationCount = 0; |
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); |
661 |
|
public void checkExecutionMode() { |
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) { |
672 |
|
return f.thenRunAsync(a, new ThreadExecutor()); |
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()); |
955 |
– |
assertEquals(1, r.invocationCount); |
999 |
|
checkCompletedNormally(f, null); |
957 |
– |
} |
958 |
– |
|
959 |
– |
/** |
960 |
– |
* runAsync with executor completes after running Runnable |
961 |
– |
*/ |
962 |
– |
public void testRunAsync2() { |
963 |
– |
Noop r = new Noop(ExecutionMode.EXECUTOR); |
964 |
– |
ThreadExecutor exec = new ThreadExecutor(); |
965 |
– |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
966 |
– |
assertNull(f.join()); |
1000 |
|
assertEquals(1, r.invocationCount); |
1001 |
< |
checkCompletedNormally(f, null); |
969 |
< |
assertEquals(1, exec.count.get()); |
970 |
< |
} |
1001 |
> |
}} |
1002 |
|
|
1003 |
|
/** |
1004 |
|
* failing runAsync completes exceptionally after running Runnable |
1005 |
|
*/ |
1006 |
< |
public void testRunAsync3() { |
1007 |
< |
FailingRunnable r = new FailingRunnable(ExecutionMode.ASYNC); |
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); |
999 |
< |
checkCompletedNormally(f, one); |
1000 |
< |
} |
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(ExecutionMode.ASYNC); |
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 |
|
|
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), |