17 |
|
import java.util.concurrent.CompletableFuture; |
18 |
|
import java.util.concurrent.CompletionException; |
19 |
|
import java.util.concurrent.CompletionStage; |
20 |
+ |
import java.util.concurrent.ForkJoinPool; |
21 |
+ |
import java.util.concurrent.ForkJoinTask; |
22 |
|
import java.util.concurrent.TimeoutException; |
23 |
|
import java.util.concurrent.atomic.AtomicInteger; |
24 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
460 |
|
*/ |
461 |
|
enum ExecutionMode { |
462 |
|
DEFAULT { |
463 |
+ |
public void checkExecutionMode() { |
464 |
+ |
assertNull(ForkJoinTask.getPool()); |
465 |
+ |
} |
466 |
|
public <T> CompletableFuture<Void> thenRun |
467 |
|
(CompletableFuture<T> f, Runnable a) { |
468 |
|
return f.thenRun(a); |
526 |
|
} |
527 |
|
}, |
528 |
|
|
529 |
< |
DEFAULT_ASYNC { |
529 |
> |
ASYNC { |
530 |
> |
public void checkExecutionMode() { |
531 |
> |
assertSame(ForkJoinPool.commonPool(), |
532 |
> |
ForkJoinTask.getPool()); |
533 |
> |
} |
534 |
|
public <T> CompletableFuture<Void> thenRun |
535 |
|
(CompletableFuture<T> f, Runnable a) { |
536 |
|
return f.thenRunAsync(a); |
595 |
|
}, |
596 |
|
|
597 |
|
EXECUTOR { |
598 |
+ |
public void checkExecutionMode() { |
599 |
+ |
//TODO |
600 |
+ |
} |
601 |
|
public <T> CompletableFuture<Void> thenRun |
602 |
|
(CompletableFuture<T> f, Runnable a) { |
603 |
|
return f.thenRunAsync(a, new ThreadExecutor()); |
661 |
|
} |
662 |
|
}; |
663 |
|
|
664 |
+ |
public abstract void checkExecutionMode(); |
665 |
|
public abstract <T> CompletableFuture<Void> thenRun |
666 |
|
(CompletableFuture<T> f, Runnable a); |
667 |
|
public abstract <T> CompletableFuture<Void> thenAccept |
974 |
|
/** |
975 |
|
* thenRun result completes normally after normal completion of source |
976 |
|
*/ |
977 |
< |
public void testThenRun() { |
978 |
< |
CompletableFuture<Integer> f; |
979 |
< |
CompletableFuture<Void> g; |
980 |
< |
Noop r; |
981 |
< |
|
982 |
< |
f = new CompletableFuture<>(); |
983 |
< |
g = f.thenRun(r = new Noop()); |
984 |
< |
f.complete(null); |
985 |
< |
checkCompletedNormally(g, null); |
986 |
< |
assertEquals(1, r.invocationCount); |
977 |
> |
public void testThenRun_normalCompletion() { |
978 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
979 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
980 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
981 |
> |
{ |
982 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
983 |
> |
final Noop r = new Noop(); |
984 |
> |
if (!createIncomplete) f.complete(v1); |
985 |
> |
final CompletableFuture<Void> g = m.thenRun(f, r); |
986 |
> |
if (createIncomplete) f.complete(v1); |
987 |
|
|
975 |
– |
f = new CompletableFuture<>(); |
976 |
– |
f.complete(null); |
977 |
– |
g = f.thenRun(r = new Noop()); |
988 |
|
checkCompletedNormally(g, null); |
989 |
+ |
checkCompletedNormally(f, v1); |
990 |
|
assertEquals(1, r.invocationCount); |
991 |
< |
} |
991 |
> |
}} |
992 |
|
|
993 |
|
/** |
994 |
|
* thenRun result completes exceptionally after exceptional |
995 |
|
* completion of source |
996 |
|
*/ |
997 |
< |
public void testThenRun2() { |
998 |
< |
CompletableFuture<Integer> f; |
999 |
< |
CompletableFuture<Void> g; |
1000 |
< |
Noop r; |
1001 |
< |
|
1002 |
< |
f = new CompletableFuture<>(); |
1003 |
< |
g = f.thenRun(r = new Noop()); |
1004 |
< |
f.completeExceptionally(new CFException()); |
1005 |
< |
checkCompletedWithWrappedCFException(g); |
1006 |
< |
assertEquals(0, r.invocationCount); |
997 |
> |
public void testThenRun_exceptionalCompletion() { |
998 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
999 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1000 |
> |
{ |
1001 |
> |
final CFException ex = new CFException(); |
1002 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1003 |
> |
final Noop r = new Noop(); |
1004 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
1005 |
> |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1006 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
1007 |
|
|
1008 |
< |
f = new CompletableFuture<>(); |
1009 |
< |
f.completeExceptionally(new CFException()); |
999 |
< |
g = f.thenRun(r = new Noop()); |
1000 |
< |
checkCompletedWithWrappedCFException(g); |
1008 |
> |
checkCompletedWithWrappedCFException(g, ex); |
1009 |
> |
checkCompletedWithWrappedCFException(f, ex); |
1010 |
|
assertEquals(0, r.invocationCount); |
1011 |
< |
} |
1011 |
> |
}} |
1012 |
|
|
1013 |
|
/** |
1014 |
< |
* thenRun result completes exceptionally if action does |
1014 |
> |
* thenRun result completes exceptionally if source cancelled |
1015 |
|
*/ |
1016 |
< |
public void testThenRun3() { |
1017 |
< |
CompletableFuture<Integer> f; |
1018 |
< |
CompletableFuture<Void> g; |
1019 |
< |
FailingNoop r; |
1020 |
< |
|
1021 |
< |
f = new CompletableFuture<>(); |
1022 |
< |
g = f.thenRun(r = new FailingNoop()); |
1023 |
< |
f.complete(null); |
1024 |
< |
checkCompletedWithWrappedCFException(g); |
1016 |
> |
public void testThenRun_sourceCancelled() { |
1017 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1018 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1019 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1020 |
> |
{ |
1021 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1022 |
> |
final Noop r = new Noop(); |
1023 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1024 |
> |
final CompletableFuture<Void> g = f.thenRun(r); |
1025 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1026 |
|
|
1027 |
< |
f = new CompletableFuture<>(); |
1028 |
< |
f.complete(null); |
1029 |
< |
g = f.thenRun(r = new FailingNoop()); |
1030 |
< |
checkCompletedWithWrappedCFException(g); |
1021 |
< |
} |
1027 |
> |
checkCompletedWithWrappedCancellationException(g); |
1028 |
> |
checkCancelled(f); |
1029 |
> |
assertEquals(0, r.invocationCount); |
1030 |
> |
}} |
1031 |
|
|
1032 |
|
/** |
1033 |
< |
* thenRun result completes exceptionally if source cancelled |
1033 |
> |
* thenRun result completes exceptionally if action does |
1034 |
|
*/ |
1035 |
< |
public void testThenRun4() { |
1036 |
< |
CompletableFuture<Integer> f; |
1037 |
< |
CompletableFuture<Void> g; |
1038 |
< |
Noop r; |
1039 |
< |
|
1040 |
< |
f = new CompletableFuture<>(); |
1041 |
< |
g = f.thenRun(r = new Noop()); |
1042 |
< |
assertTrue(f.cancel(true)); |
1043 |
< |
checkCompletedWithWrappedCancellationException(g); |
1035 |
> |
public void testThenRun_actionFailed() { |
1036 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1037 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1038 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1039 |
> |
{ |
1040 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1041 |
> |
final FailingNoop r = new FailingNoop(); |
1042 |
> |
if (!createIncomplete) f.complete(v1); |
1043 |
> |
final CompletableFuture<Void> g = f.thenRun(r); |
1044 |
> |
if (createIncomplete) f.complete(v1); |
1045 |
|
|
1046 |
< |
f = new CompletableFuture<>(); |
1047 |
< |
assertTrue(f.cancel(true)); |
1048 |
< |
g = f.thenRun(r = new Noop()); |
1039 |
< |
checkCompletedWithWrappedCancellationException(g); |
1040 |
< |
} |
1046 |
> |
checkCompletedWithWrappedCFException(g); |
1047 |
> |
checkCompletedNormally(f, v1); |
1048 |
> |
}} |
1049 |
|
|
1050 |
|
/** |
1051 |
|
* thenApply result completes normally after normal completion of source |
2848 |
|
/** |
2849 |
|
* thenCompose result completes normally after normal completion of source |
2850 |
|
*/ |
2851 |
< |
public void testThenCompose_normalCompletion1() { |
2851 |
> |
public void testThenCompose_normalCompletion() { |
2852 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2853 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
2854 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2855 |
|
{ |
2856 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2857 |
|
final CompletableFutureInc r = new CompletableFutureInc(); |
2858 |
+ |
if (!createIncomplete) f.complete(v1); |
2859 |
|
final CompletableFuture<Integer> g = f.thenCompose(r); |
2860 |
< |
f.complete(v1); |
2851 |
< |
checkCompletedNormally(g, inc(v1)); |
2852 |
< |
checkCompletedNormally(f, v1); |
2853 |
< |
assertEquals(1, r.invocationCount); |
2854 |
< |
}} |
2860 |
> |
if (createIncomplete) f.complete(v1); |
2861 |
|
|
2856 |
– |
public void testThenCompose_normalCompletion2() { |
2857 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2858 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2859 |
– |
{ |
2860 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2861 |
– |
final CompletableFutureInc r = new CompletableFutureInc(); |
2862 |
– |
f.complete(v1); |
2863 |
– |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2862 |
|
checkCompletedNormally(g, inc(v1)); |
2863 |
|
checkCompletedNormally(f, v1); |
2864 |
|
assertEquals(1, r.invocationCount); |
2868 |
|
* thenCompose result completes exceptionally after exceptional |
2869 |
|
* completion of source |
2870 |
|
*/ |
2871 |
< |
public void testThenCompose_exceptionalCompletion1() { |
2871 |
> |
public void testThenCompose_exceptionalCompletion() { |
2872 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2873 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
2874 |
|
{ |
2875 |
|
final CFException ex = new CFException(); |
2876 |
|
final CompletableFutureInc r = new CompletableFutureInc(); |
2877 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2878 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
2879 |
|
final CompletableFuture<Integer> g = f.thenCompose(r); |
2880 |
< |
f.completeExceptionally(ex); |
2881 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2882 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2883 |
< |
}} |
2880 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
2881 |
|
|
2885 |
– |
public void testThenCompose_exceptionalCompletion2() { |
2886 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2887 |
– |
{ |
2888 |
– |
final CFException ex = new CFException(); |
2889 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2890 |
– |
f.completeExceptionally(ex); |
2891 |
– |
final CompletableFutureInc r = new CompletableFutureInc(); |
2892 |
– |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2882 |
|
checkCompletedWithWrappedCFException(g, ex); |
2883 |
|
checkCompletedWithWrappedCFException(f, ex); |
2884 |
+ |
assertEquals(0, r.invocationCount); |
2885 |
|
}} |
2886 |
|
|
2887 |
|
/** |
2888 |
|
* thenCompose result completes exceptionally if action does |
2889 |
|
*/ |
2890 |
< |
public void testThenCompose_actionFailed1() { |
2890 |
> |
public void testThenCompose_actionFailed() { |
2891 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2892 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
2893 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2894 |
|
{ |
2895 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2896 |
|
final FailingCompletableFutureFunction r |
2897 |
|
= new FailingCompletableFutureFunction(); |
2898 |
+ |
if (!createIncomplete) f.complete(v1); |
2899 |
|
final CompletableFuture<Integer> g = f.thenCompose(r); |
2900 |
< |
f.complete(v1); |
2909 |
< |
checkCompletedWithWrappedCFException(g); |
2910 |
< |
checkCompletedNormally(f, v1); |
2911 |
< |
}} |
2900 |
> |
if (createIncomplete) f.complete(v1); |
2901 |
|
|
2913 |
– |
public void testThenCompose_actionFailed2() { |
2914 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2915 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2916 |
– |
{ |
2917 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2918 |
– |
f.complete(v1); |
2919 |
– |
final FailingCompletableFutureFunction r |
2920 |
– |
= new FailingCompletableFutureFunction(); |
2921 |
– |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2902 |
|
checkCompletedWithWrappedCFException(g); |
2903 |
|
checkCompletedNormally(f, v1); |
2904 |
|
}} |
2906 |
|
/** |
2907 |
|
* thenCompose result completes exceptionally if source cancelled |
2908 |
|
*/ |
2909 |
< |
public void testThenCompose_sourceCancelled1() { |
2909 |
> |
public void testThenCompose_sourceCancelled() { |
2910 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2911 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
2912 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2913 |
|
{ |
2914 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2915 |
|
final CompletableFutureInc r = new CompletableFutureInc(); |
2916 |
+ |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2917 |
|
final CompletableFuture<Integer> g = f.thenCompose(r); |
2918 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2937 |
< |
checkCompletedWithWrappedCancellationException(g); |
2938 |
< |
checkCancelled(f); |
2939 |
< |
}} |
2918 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2919 |
|
|
2941 |
– |
public void testThenCompose_sourceCancelled2() { |
2942 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2943 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2944 |
– |
{ |
2945 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2946 |
– |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2947 |
– |
final CompletableFutureInc r = new CompletableFutureInc(); |
2948 |
– |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2920 |
|
checkCompletedWithWrappedCancellationException(g); |
2921 |
|
checkCancelled(f); |
2922 |
|
}} |
2924 |
|
// asyncs |
2925 |
|
|
2926 |
|
/** |
2956 |
– |
* thenRunAsync result completes normally after normal completion of source |
2957 |
– |
*/ |
2958 |
– |
public void testThenRunAsync() { |
2959 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2960 |
– |
Noop r = new Noop(); |
2961 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r); |
2962 |
– |
f.complete(null); |
2963 |
– |
checkCompletedNormally(g, null); |
2964 |
– |
|
2965 |
– |
// reordered version |
2966 |
– |
f = new CompletableFuture<>(); |
2967 |
– |
f.complete(null); |
2968 |
– |
r = new Noop(); |
2969 |
– |
g = f.thenRunAsync(r); |
2970 |
– |
checkCompletedNormally(g, null); |
2971 |
– |
} |
2972 |
– |
|
2973 |
– |
/** |
2974 |
– |
* thenRunAsync result completes exceptionally after exceptional |
2975 |
– |
* completion of source |
2976 |
– |
*/ |
2977 |
– |
public void testThenRunAsync2() { |
2978 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2979 |
– |
Noop r = new Noop(); |
2980 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r); |
2981 |
– |
f.completeExceptionally(new CFException()); |
2982 |
– |
try { |
2983 |
– |
g.join(); |
2984 |
– |
shouldThrow(); |
2985 |
– |
} catch (CompletionException success) {} |
2986 |
– |
checkCompletedWithWrappedCFException(g); |
2987 |
– |
} |
2988 |
– |
|
2989 |
– |
/** |
2990 |
– |
* thenRunAsync result completes exceptionally if action does |
2991 |
– |
*/ |
2992 |
– |
public void testThenRunAsync3() { |
2993 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2994 |
– |
FailingNoop r = new FailingNoop(); |
2995 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r); |
2996 |
– |
f.complete(null); |
2997 |
– |
checkCompletedWithWrappedCFException(g); |
2998 |
– |
} |
2999 |
– |
|
3000 |
– |
/** |
3001 |
– |
* thenRunAsync result completes exceptionally if source cancelled |
3002 |
– |
*/ |
3003 |
– |
public void testThenRunAsync4() { |
3004 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3005 |
– |
Noop r = new Noop(); |
3006 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r); |
3007 |
– |
assertTrue(f.cancel(true)); |
3008 |
– |
checkCompletedWithWrappedCancellationException(g); |
3009 |
– |
} |
3010 |
– |
|
3011 |
– |
/** |
2927 |
|
* thenApplyAsync result completes normally after normal completion of source |
2928 |
|
*/ |
2929 |
|
public void testThenApplyAsync() { |
3015 |
|
// async with explicit executors |
3016 |
|
|
3017 |
|
/** |
3103 |
– |
* thenRunAsync result completes normally after normal completion of source |
3104 |
– |
*/ |
3105 |
– |
public void testThenRunAsyncE() { |
3106 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3107 |
– |
Noop r = new Noop(); |
3108 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
3109 |
– |
f.complete(null); |
3110 |
– |
checkCompletedNormally(g, null); |
3111 |
– |
|
3112 |
– |
// reordered version |
3113 |
– |
f = new CompletableFuture<>(); |
3114 |
– |
f.complete(null); |
3115 |
– |
r = new Noop(); |
3116 |
– |
g = f.thenRunAsync(r, new ThreadExecutor()); |
3117 |
– |
checkCompletedNormally(g, null); |
3118 |
– |
} |
3119 |
– |
|
3120 |
– |
/** |
3121 |
– |
* thenRunAsync result completes exceptionally after exceptional |
3122 |
– |
* completion of source |
3123 |
– |
*/ |
3124 |
– |
public void testThenRunAsync2E() { |
3125 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3126 |
– |
Noop r = new Noop(); |
3127 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
3128 |
– |
f.completeExceptionally(new CFException()); |
3129 |
– |
try { |
3130 |
– |
g.join(); |
3131 |
– |
shouldThrow(); |
3132 |
– |
} catch (CompletionException success) {} |
3133 |
– |
checkCompletedWithWrappedCFException(g); |
3134 |
– |
} |
3135 |
– |
|
3136 |
– |
/** |
3137 |
– |
* thenRunAsync result completes exceptionally if action does |
3138 |
– |
*/ |
3139 |
– |
public void testThenRunAsync3E() { |
3140 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3141 |
– |
FailingNoop r = new FailingNoop(); |
3142 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
3143 |
– |
f.complete(null); |
3144 |
– |
checkCompletedWithWrappedCFException(g); |
3145 |
– |
} |
3146 |
– |
|
3147 |
– |
/** |
3148 |
– |
* thenRunAsync result completes exceptionally if source cancelled |
3149 |
– |
*/ |
3150 |
– |
public void testThenRunAsync4E() { |
3151 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3152 |
– |
Noop r = new Noop(); |
3153 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
3154 |
– |
assertTrue(f.cancel(true)); |
3155 |
– |
checkCompletedWithWrappedCancellationException(g); |
3156 |
– |
} |
3157 |
– |
|
3158 |
– |
/** |
3018 |
|
* thenApplyAsync result completes normally after normal completion of source |
3019 |
|
*/ |
3020 |
|
public void testThenApplyAsyncE() { |