8 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
|
|
11 |
+ |
import java.lang.reflect.Method; |
12 |
+ |
import java.lang.reflect.Modifier; |
13 |
+ |
|
14 |
+ |
import java.util.stream.Collectors; |
15 |
+ |
import java.util.stream.Stream; |
16 |
+ |
|
17 |
|
import java.util.ArrayList; |
18 |
+ |
import java.util.Arrays; |
19 |
|
import java.util.List; |
20 |
|
import java.util.Objects; |
21 |
+ |
import java.util.Set; |
22 |
|
import java.util.concurrent.Callable; |
23 |
|
import java.util.concurrent.CancellationException; |
24 |
|
import java.util.concurrent.CompletableFuture; |
29 |
|
import java.util.concurrent.ForkJoinPool; |
30 |
|
import java.util.concurrent.ForkJoinTask; |
31 |
|
import java.util.concurrent.TimeoutException; |
32 |
+ |
import java.util.concurrent.TimeUnit; |
33 |
|
import java.util.concurrent.atomic.AtomicInteger; |
34 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
35 |
|
import java.util.function.BiConsumer; |
36 |
|
import java.util.function.BiFunction; |
37 |
|
import java.util.function.Consumer; |
38 |
|
import java.util.function.Function; |
39 |
+ |
import java.util.function.Predicate; |
40 |
|
import java.util.function.Supplier; |
41 |
|
|
42 |
|
import junit.framework.Test; |
45 |
|
public class CompletableFutureTest extends JSR166TestCase { |
46 |
|
|
47 |
|
public static void main(String[] args) { |
48 |
< |
junit.textui.TestRunner.run(suite()); |
48 |
> |
main(suite(), args); |
49 |
|
} |
50 |
|
public static Test suite() { |
51 |
|
return new TestSuite(CompletableFutureTest.class); |
56 |
|
void checkIncomplete(CompletableFuture<?> f) { |
57 |
|
assertFalse(f.isDone()); |
58 |
|
assertFalse(f.isCancelled()); |
59 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
59 |
> |
assertTrue(f.toString().contains("Not completed")); |
60 |
|
try { |
61 |
|
assertNull(f.getNow(null)); |
62 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
86 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
87 |
|
} |
88 |
|
|
89 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
89 |
> |
/** |
90 |
> |
* Returns the "raw" internal exceptional completion of f, |
91 |
> |
* without any additional wrapping with CompletionException. |
92 |
> |
*/ |
93 |
> |
<U> Throwable exceptionalCompletion(CompletableFuture<U> f) { |
94 |
> |
// handle (and whenComplete) can distinguish between "direct" |
95 |
> |
// and "wrapped" exceptional completion |
96 |
> |
return f.handle((U u, Throwable t) -> t).join(); |
97 |
> |
} |
98 |
> |
|
99 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, |
100 |
> |
boolean wrapped, |
101 |
> |
Consumer<Throwable> checker) { |
102 |
> |
Throwable cause = exceptionalCompletion(f); |
103 |
> |
if (wrapped) { |
104 |
> |
assertTrue(cause instanceof CompletionException); |
105 |
> |
cause = cause.getCause(); |
106 |
> |
} |
107 |
> |
checker.accept(cause); |
108 |
> |
|
109 |
|
long startTime = System.nanoTime(); |
80 |
– |
long timeoutMillis = LONG_DELAY_MS; |
110 |
|
try { |
111 |
< |
f.get(timeoutMillis, MILLISECONDS); |
111 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
112 |
|
shouldThrow(); |
113 |
|
} catch (ExecutionException success) { |
114 |
< |
assertTrue(success.getCause() instanceof CFException); |
114 |
> |
assertSame(cause, success.getCause()); |
115 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
116 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
116 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
117 |
|
|
118 |
|
try { |
119 |
|
f.join(); |
120 |
|
shouldThrow(); |
121 |
|
} catch (CompletionException success) { |
122 |
< |
assertTrue(success.getCause() instanceof CFException); |
123 |
< |
} |
122 |
> |
assertSame(cause, success.getCause()); |
123 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
124 |
> |
|
125 |
|
try { |
126 |
|
f.getNow(null); |
127 |
|
shouldThrow(); |
128 |
|
} catch (CompletionException success) { |
129 |
< |
assertTrue(success.getCause() instanceof CFException); |
130 |
< |
} |
129 |
> |
assertSame(cause, success.getCause()); |
130 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
131 |
> |
|
132 |
|
try { |
133 |
|
f.get(); |
134 |
|
shouldThrow(); |
135 |
|
} catch (ExecutionException success) { |
136 |
< |
assertTrue(success.getCause() instanceof CFException); |
136 |
> |
assertSame(cause, success.getCause()); |
137 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
138 |
< |
assertTrue(f.isDone()); |
138 |
> |
|
139 |
|
assertFalse(f.isCancelled()); |
140 |
+ |
assertTrue(f.isDone()); |
141 |
+ |
assertTrue(f.isCompletedExceptionally()); |
142 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
143 |
|
} |
144 |
|
|
145 |
< |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
146 |
< |
Throwable ex) { |
147 |
< |
long startTime = System.nanoTime(); |
148 |
< |
long timeoutMillis = LONG_DELAY_MS; |
116 |
< |
try { |
117 |
< |
f.get(timeoutMillis, MILLISECONDS); |
118 |
< |
shouldThrow(); |
119 |
< |
} catch (ExecutionException success) { |
120 |
< |
assertSame(ex, success.getCause()); |
121 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
122 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
145 |
> |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
146 |
> |
checkCompletedExceptionally(f, true, |
147 |
> |
(t) -> assertTrue(t instanceof CFException)); |
148 |
> |
} |
149 |
|
|
150 |
< |
try { |
151 |
< |
f.join(); |
152 |
< |
shouldThrow(); |
153 |
< |
} catch (CompletionException success) { |
128 |
< |
assertSame(ex, success.getCause()); |
129 |
< |
} |
130 |
< |
try { |
131 |
< |
f.getNow(null); |
132 |
< |
shouldThrow(); |
133 |
< |
} catch (CompletionException success) { |
134 |
< |
assertSame(ex, success.getCause()); |
135 |
< |
} |
136 |
< |
try { |
137 |
< |
f.get(); |
138 |
< |
shouldThrow(); |
139 |
< |
} catch (ExecutionException success) { |
140 |
< |
assertSame(ex, success.getCause()); |
141 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
150 |
> |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
151 |
> |
checkCompletedExceptionally(f, true, |
152 |
> |
(t) -> assertTrue(t instanceof CancellationException)); |
153 |
> |
} |
154 |
|
|
155 |
< |
assertTrue(f.isDone()); |
156 |
< |
assertFalse(f.isCancelled()); |
157 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
155 |
> |
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
156 |
> |
checkCompletedExceptionally(f, false, |
157 |
> |
(t) -> assertTrue(t instanceof TimeoutException)); |
158 |
|
} |
159 |
|
|
160 |
< |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
161 |
< |
Throwable ex) { |
162 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
151 |
< |
try { |
152 |
< |
CompletableFuture<Throwable> spy = f.handle |
153 |
< |
((U u, Throwable t) -> t); |
154 |
< |
assertTrue(spy.join() instanceof CompletionException); |
155 |
< |
assertSame(ex, spy.join().getCause()); |
156 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
160 |
> |
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
161 |
> |
Throwable ex) { |
162 |
> |
checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex)); |
163 |
|
} |
164 |
|
|
165 |
< |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
166 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
161 |
< |
try { |
162 |
< |
CompletableFuture<Throwable> spy = f.handle |
163 |
< |
((U u, Throwable t) -> t); |
164 |
< |
assertSame(ex, spy.join()); |
165 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
165 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
166 |
> |
checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex)); |
167 |
|
} |
168 |
|
|
169 |
|
void checkCancelled(CompletableFuture<?> f) { |
170 |
|
long startTime = System.nanoTime(); |
170 |
– |
long timeoutMillis = LONG_DELAY_MS; |
171 |
|
try { |
172 |
< |
f.get(timeoutMillis, MILLISECONDS); |
172 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
173 |
|
shouldThrow(); |
174 |
|
} catch (CancellationException success) { |
175 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
176 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
176 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
177 |
|
|
178 |
|
try { |
179 |
|
f.join(); |
188 |
|
shouldThrow(); |
189 |
|
} catch (CancellationException success) { |
190 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
191 |
– |
assertTrue(f.isDone()); |
192 |
– |
assertTrue(f.isCompletedExceptionally()); |
193 |
– |
assertTrue(f.isCancelled()); |
194 |
– |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
195 |
– |
} |
191 |
|
|
192 |
< |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
198 |
< |
long startTime = System.nanoTime(); |
199 |
< |
long timeoutMillis = LONG_DELAY_MS; |
200 |
< |
try { |
201 |
< |
f.get(timeoutMillis, MILLISECONDS); |
202 |
< |
shouldThrow(); |
203 |
< |
} catch (ExecutionException success) { |
204 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
205 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
206 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
192 |
> |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
193 |
|
|
208 |
– |
try { |
209 |
– |
f.join(); |
210 |
– |
shouldThrow(); |
211 |
– |
} catch (CompletionException success) { |
212 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
213 |
– |
} |
214 |
– |
try { |
215 |
– |
f.getNow(null); |
216 |
– |
shouldThrow(); |
217 |
– |
} catch (CompletionException success) { |
218 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
219 |
– |
} |
220 |
– |
try { |
221 |
– |
f.get(); |
222 |
– |
shouldThrow(); |
223 |
– |
} catch (ExecutionException success) { |
224 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
225 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
194 |
|
assertTrue(f.isDone()); |
227 |
– |
assertFalse(f.isCancelled()); |
195 |
|
assertTrue(f.isCompletedExceptionally()); |
196 |
+ |
assertTrue(f.isCancelled()); |
197 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
198 |
|
} |
199 |
|
|
241 |
|
{ |
242 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
243 |
|
checkIncomplete(f); |
244 |
< |
assertTrue(f.cancel(true)); |
245 |
< |
assertTrue(f.cancel(true)); |
244 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
245 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
246 |
> |
assertTrue(f.cancel(!mayInterruptIfRunning)); |
247 |
|
checkCancelled(f); |
248 |
|
}} |
249 |
|
|
515 |
|
} |
516 |
|
} |
517 |
|
|
549 |
– |
|
518 |
|
class CompletableFutureInc extends CheckedIntegerAction |
519 |
|
implements Function<Integer, CompletableFuture<Integer>> |
520 |
|
{ |
875 |
|
|
876 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
877 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
910 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
878 |
|
{ |
879 |
|
final AtomicInteger a = new AtomicInteger(0); |
880 |
|
final CFException ex1 = new CFException(); |
928 |
|
public void testWhenComplete_exceptionalCompletion() { |
929 |
|
for (ExecutionMode m : ExecutionMode.values()) |
930 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
964 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
931 |
|
{ |
932 |
|
final AtomicInteger a = new AtomicInteger(0); |
933 |
|
final CFException ex = new CFException(); |
1012 |
|
public void testWhenComplete_actionFailedSourceFailed() { |
1013 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1014 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1049 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1015 |
|
{ |
1016 |
|
final AtomicInteger a = new AtomicInteger(0); |
1017 |
|
final CFException ex1 = new CFException(); |
2959 |
|
checkCancelled(f); |
2960 |
|
}} |
2961 |
|
|
2962 |
+ |
/** |
2963 |
+ |
* thenCompose result completes exceptionally if the result of the action does |
2964 |
+ |
*/ |
2965 |
+ |
public void testThenCompose_actionReturnsFailingFuture() { |
2966 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
2967 |
+ |
for (int order = 0; order < 6; order++) |
2968 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
2969 |
+ |
{ |
2970 |
+ |
final CFException ex = new CFException(); |
2971 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2972 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2973 |
+ |
final CompletableFuture<Integer> h; |
2974 |
+ |
// Test all permutations of orders |
2975 |
+ |
switch (order) { |
2976 |
+ |
case 0: |
2977 |
+ |
assertTrue(f.complete(v1)); |
2978 |
+ |
assertTrue(g.completeExceptionally(ex)); |
2979 |
+ |
h = m.thenCompose(f, (x -> g)); |
2980 |
+ |
break; |
2981 |
+ |
case 1: |
2982 |
+ |
assertTrue(f.complete(v1)); |
2983 |
+ |
h = m.thenCompose(f, (x -> g)); |
2984 |
+ |
assertTrue(g.completeExceptionally(ex)); |
2985 |
+ |
break; |
2986 |
+ |
case 2: |
2987 |
+ |
assertTrue(g.completeExceptionally(ex)); |
2988 |
+ |
assertTrue(f.complete(v1)); |
2989 |
+ |
h = m.thenCompose(f, (x -> g)); |
2990 |
+ |
break; |
2991 |
+ |
case 3: |
2992 |
+ |
assertTrue(g.completeExceptionally(ex)); |
2993 |
+ |
h = m.thenCompose(f, (x -> g)); |
2994 |
+ |
assertTrue(f.complete(v1)); |
2995 |
+ |
break; |
2996 |
+ |
case 4: |
2997 |
+ |
h = m.thenCompose(f, (x -> g)); |
2998 |
+ |
assertTrue(f.complete(v1)); |
2999 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3000 |
+ |
break; |
3001 |
+ |
case 5: |
3002 |
+ |
h = m.thenCompose(f, (x -> g)); |
3003 |
+ |
assertTrue(f.complete(v1)); |
3004 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3005 |
+ |
break; |
3006 |
+ |
default: throw new AssertionError(); |
3007 |
+ |
} |
3008 |
+ |
|
3009 |
+ |
checkCompletedExceptionally(g, ex); |
3010 |
+ |
checkCompletedWithWrappedException(h, ex); |
3011 |
+ |
checkCompletedNormally(f, v1); |
3012 |
+ |
}} |
3013 |
+ |
|
3014 |
|
// other static methods |
3015 |
|
|
3016 |
|
/** |
3071 |
|
for (int i = 0; i < k; i++) { |
3072 |
|
checkIncomplete(f); |
3073 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3074 |
< |
if (i != k/2) { |
3074 |
> |
if (i != k / 2) { |
3075 |
|
fs[i].complete(i); |
3076 |
|
checkCompletedNormally(fs[i], i); |
3077 |
|
} else { |
3178 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3179 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3180 |
|
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
3164 |
– |
CompletableFuture<?> h; |
3181 |
|
ThreadExecutor exec = new ThreadExecutor(); |
3182 |
|
|
3183 |
|
Runnable[] throwingActions = { |
3274 |
|
() -> CompletableFuture.anyOf(null, f), |
3275 |
|
|
3276 |
|
() -> f.obtrudeException(null), |
3277 |
+ |
|
3278 |
+ |
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3279 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3280 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null), |
3281 |
+ |
|
3282 |
+ |
() -> f.orTimeout(1L, null), |
3283 |
+ |
() -> f.completeOnTimeout(42, 1L, null), |
3284 |
+ |
|
3285 |
+ |
() -> CompletableFuture.failedFuture(null), |
3286 |
+ |
() -> CompletableFuture.failedStage(null), |
3287 |
|
}; |
3288 |
|
|
3289 |
|
assertThrows(NullPointerException.class, throwingActions); |
3298 |
|
assertSame(f, f.toCompletableFuture()); |
3299 |
|
} |
3300 |
|
|
3301 |
+ |
// jdk9 |
3302 |
+ |
|
3303 |
+ |
/** |
3304 |
+ |
* newIncompleteFuture returns an incomplete CompletableFuture |
3305 |
+ |
*/ |
3306 |
+ |
public void testNewIncompleteFuture() { |
3307 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3308 |
+ |
{ |
3309 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3310 |
+ |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3311 |
+ |
checkIncomplete(f); |
3312 |
+ |
checkIncomplete(g); |
3313 |
+ |
f.complete(v1); |
3314 |
+ |
checkCompletedNormally(f, v1); |
3315 |
+ |
checkIncomplete(g); |
3316 |
+ |
g.complete(v1); |
3317 |
+ |
checkCompletedNormally(g, v1); |
3318 |
+ |
assertSame(g.getClass(), CompletableFuture.class); |
3319 |
+ |
}} |
3320 |
+ |
|
3321 |
+ |
/** |
3322 |
+ |
* completedStage returns a completed CompletionStage |
3323 |
+ |
*/ |
3324 |
+ |
public void testCompletedStage() { |
3325 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3326 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3327 |
+ |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3328 |
+ |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3329 |
+ |
assertEquals(x.get(), 1); |
3330 |
+ |
assertNull(r.get()); |
3331 |
+ |
} |
3332 |
+ |
|
3333 |
+ |
/** |
3334 |
+ |
* defaultExecutor by default returns the commonPool if |
3335 |
+ |
* it supports more than one thread. |
3336 |
+ |
*/ |
3337 |
+ |
public void testDefaultExecutor() { |
3338 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3339 |
+ |
Executor e = f.defaultExecutor(); |
3340 |
+ |
Executor c = ForkJoinPool.commonPool(); |
3341 |
+ |
if (ForkJoinPool.getCommonPoolParallelism() > 1) |
3342 |
+ |
assertSame(e, c); |
3343 |
+ |
else |
3344 |
+ |
assertNotSame(e, c); |
3345 |
+ |
} |
3346 |
+ |
|
3347 |
+ |
/** |
3348 |
+ |
* failedFuture returns a CompletableFuture completed |
3349 |
+ |
* exceptionally with the given Exception |
3350 |
+ |
*/ |
3351 |
+ |
public void testFailedFuture() { |
3352 |
+ |
CFException ex = new CFException(); |
3353 |
+ |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3354 |
+ |
checkCompletedExceptionally(f, ex); |
3355 |
+ |
} |
3356 |
+ |
|
3357 |
+ |
/** |
3358 |
+ |
* failedFuture(null) throws NPE |
3359 |
+ |
*/ |
3360 |
+ |
public void testFailedFuture_null() { |
3361 |
+ |
try { |
3362 |
+ |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3363 |
+ |
shouldThrow(); |
3364 |
+ |
} catch (NullPointerException success) {} |
3365 |
+ |
} |
3366 |
+ |
|
3367 |
+ |
/** |
3368 |
+ |
* copy returns a CompletableFuture that is completed normally, |
3369 |
+ |
* with the same value, when source is. |
3370 |
+ |
*/ |
3371 |
+ |
public void testCopy() { |
3372 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3373 |
+ |
CompletableFuture<Integer> g = f.copy(); |
3374 |
+ |
checkIncomplete(f); |
3375 |
+ |
checkIncomplete(g); |
3376 |
+ |
f.complete(1); |
3377 |
+ |
checkCompletedNormally(f, 1); |
3378 |
+ |
checkCompletedNormally(g, 1); |
3379 |
+ |
} |
3380 |
+ |
|
3381 |
+ |
/** |
3382 |
+ |
* copy returns a CompletableFuture that is completed exceptionally |
3383 |
+ |
* when source is. |
3384 |
+ |
*/ |
3385 |
+ |
public void testCopy2() { |
3386 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3387 |
+ |
CompletableFuture<Integer> g = f.copy(); |
3388 |
+ |
checkIncomplete(f); |
3389 |
+ |
checkIncomplete(g); |
3390 |
+ |
CFException ex = new CFException(); |
3391 |
+ |
f.completeExceptionally(ex); |
3392 |
+ |
checkCompletedExceptionally(f, ex); |
3393 |
+ |
checkCompletedWithWrappedException(g, ex); |
3394 |
+ |
} |
3395 |
+ |
|
3396 |
+ |
/** |
3397 |
+ |
* minimalCompletionStage returns a CompletableFuture that is |
3398 |
+ |
* completed normally, with the same value, when source is. |
3399 |
+ |
*/ |
3400 |
+ |
public void testMinimalCompletionStage() { |
3401 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3402 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3403 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3404 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3405 |
+ |
checkIncomplete(f); |
3406 |
+ |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3407 |
+ |
f.complete(1); |
3408 |
+ |
checkCompletedNormally(f, 1); |
3409 |
+ |
assertEquals(x.get(), 1); |
3410 |
+ |
assertNull(r.get()); |
3411 |
+ |
} |
3412 |
+ |
|
3413 |
+ |
/** |
3414 |
+ |
* minimalCompletionStage returns a CompletableFuture that is |
3415 |
+ |
* completed exceptionally when source is. |
3416 |
+ |
*/ |
3417 |
+ |
public void testMinimalCompletionStage2() { |
3418 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3419 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3420 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3421 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3422 |
+ |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3423 |
+ |
checkIncomplete(f); |
3424 |
+ |
CFException ex = new CFException(); |
3425 |
+ |
f.completeExceptionally(ex); |
3426 |
+ |
checkCompletedExceptionally(f, ex); |
3427 |
+ |
assertEquals(x.get(), 0); |
3428 |
+ |
assertEquals(r.get().getCause(), ex); |
3429 |
+ |
} |
3430 |
+ |
|
3431 |
+ |
/** |
3432 |
+ |
* failedStage returns a CompletionStage completed |
3433 |
+ |
* exceptionally with the given Exception |
3434 |
+ |
*/ |
3435 |
+ |
public void testFailedStage() { |
3436 |
+ |
CFException ex = new CFException(); |
3437 |
+ |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3438 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3439 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3440 |
+ |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3441 |
+ |
assertEquals(x.get(), 0); |
3442 |
+ |
assertEquals(r.get(), ex); |
3443 |
+ |
} |
3444 |
+ |
|
3445 |
+ |
/** |
3446 |
+ |
* completeAsync completes with value of given supplier |
3447 |
+ |
*/ |
3448 |
+ |
public void testCompleteAsync() { |
3449 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3450 |
+ |
{ |
3451 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3452 |
+ |
f.completeAsync(() -> v1); |
3453 |
+ |
f.join(); |
3454 |
+ |
checkCompletedNormally(f, v1); |
3455 |
+ |
}} |
3456 |
+ |
|
3457 |
+ |
/** |
3458 |
+ |
* completeAsync completes exceptionally if given supplier throws |
3459 |
+ |
*/ |
3460 |
+ |
public void testCompleteAsync2() { |
3461 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3462 |
+ |
CFException ex = new CFException(); |
3463 |
+ |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3464 |
+ |
try { |
3465 |
+ |
f.join(); |
3466 |
+ |
shouldThrow(); |
3467 |
+ |
} catch (CompletionException success) {} |
3468 |
+ |
checkCompletedWithWrappedException(f, ex); |
3469 |
+ |
} |
3470 |
+ |
|
3471 |
+ |
/** |
3472 |
+ |
* completeAsync with given executor completes with value of given supplier |
3473 |
+ |
*/ |
3474 |
+ |
public void testCompleteAsync3() { |
3475 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3476 |
+ |
{ |
3477 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3478 |
+ |
ThreadExecutor executor = new ThreadExecutor(); |
3479 |
+ |
f.completeAsync(() -> v1, executor); |
3480 |
+ |
assertSame(v1, f.join()); |
3481 |
+ |
checkCompletedNormally(f, v1); |
3482 |
+ |
assertEquals(1, executor.count.get()); |
3483 |
+ |
}} |
3484 |
+ |
|
3485 |
+ |
/** |
3486 |
+ |
* completeAsync with given executor completes exceptionally if |
3487 |
+ |
* given supplier throws |
3488 |
+ |
*/ |
3489 |
+ |
public void testCompleteAsync4() { |
3490 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3491 |
+ |
CFException ex = new CFException(); |
3492 |
+ |
ThreadExecutor executor = new ThreadExecutor(); |
3493 |
+ |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3494 |
+ |
try { |
3495 |
+ |
f.join(); |
3496 |
+ |
shouldThrow(); |
3497 |
+ |
} catch (CompletionException success) {} |
3498 |
+ |
checkCompletedWithWrappedException(f, ex); |
3499 |
+ |
assertEquals(1, executor.count.get()); |
3500 |
+ |
} |
3501 |
+ |
|
3502 |
+ |
/** |
3503 |
+ |
* orTimeout completes with TimeoutException if not complete |
3504 |
+ |
*/ |
3505 |
+ |
public void testOrTimeout_timesOut() { |
3506 |
+ |
long timeoutMillis = timeoutMillis(); |
3507 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3508 |
+ |
long startTime = System.nanoTime(); |
3509 |
+ |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3510 |
+ |
checkCompletedWithTimeoutException(f); |
3511 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3512 |
+ |
} |
3513 |
+ |
|
3514 |
+ |
/** |
3515 |
+ |
* orTimeout completes normally if completed before timeout |
3516 |
+ |
*/ |
3517 |
+ |
public void testOrTimeout_completed() { |
3518 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3519 |
+ |
{ |
3520 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3521 |
+ |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3522 |
+ |
long startTime = System.nanoTime(); |
3523 |
+ |
f.complete(v1); |
3524 |
+ |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3525 |
+ |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3526 |
+ |
g.complete(v1); |
3527 |
+ |
checkCompletedNormally(f, v1); |
3528 |
+ |
checkCompletedNormally(g, v1); |
3529 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3530 |
+ |
}} |
3531 |
+ |
|
3532 |
+ |
/** |
3533 |
+ |
* completeOnTimeout completes with given value if not complete |
3534 |
+ |
*/ |
3535 |
+ |
public void testCompleteOnTimeout_timesOut() { |
3536 |
+ |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3537 |
+ |
() -> testCompleteOnTimeout_timesOut(null)); |
3538 |
+ |
} |
3539 |
+ |
|
3540 |
+ |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3541 |
+ |
long timeoutMillis = timeoutMillis(); |
3542 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3543 |
+ |
long startTime = System.nanoTime(); |
3544 |
+ |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3545 |
+ |
assertSame(v, f.join()); |
3546 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3547 |
+ |
f.complete(99); // should have no effect |
3548 |
+ |
checkCompletedNormally(f, v); |
3549 |
+ |
} |
3550 |
+ |
|
3551 |
+ |
/** |
3552 |
+ |
* completeOnTimeout has no effect if completed within timeout |
3553 |
+ |
*/ |
3554 |
+ |
public void testCompleteOnTimeout_completed() { |
3555 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3556 |
+ |
{ |
3557 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3558 |
+ |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3559 |
+ |
long startTime = System.nanoTime(); |
3560 |
+ |
f.complete(v1); |
3561 |
+ |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3562 |
+ |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3563 |
+ |
g.complete(v1); |
3564 |
+ |
checkCompletedNormally(f, v1); |
3565 |
+ |
checkCompletedNormally(g, v1); |
3566 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3567 |
+ |
}} |
3568 |
+ |
|
3569 |
+ |
/** |
3570 |
+ |
* delayedExecutor returns an executor that delays submission |
3571 |
+ |
*/ |
3572 |
+ |
public void testDelayedExecutor() { |
3573 |
+ |
testInParallel(() -> testDelayedExecutor(null, null), |
3574 |
+ |
() -> testDelayedExecutor(null, 1), |
3575 |
+ |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3576 |
+ |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3577 |
+ |
} |
3578 |
+ |
|
3579 |
+ |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3580 |
+ |
long timeoutMillis = timeoutMillis(); |
3581 |
+ |
// Use an "unreasonably long" long timeout to catch lingering threads |
3582 |
+ |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3583 |
+ |
final Executor delayer, longDelayer; |
3584 |
+ |
if (executor == null) { |
3585 |
+ |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3586 |
+ |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3587 |
+ |
} else { |
3588 |
+ |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3589 |
+ |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3590 |
+ |
} |
3591 |
+ |
long startTime = System.nanoTime(); |
3592 |
+ |
CompletableFuture<Integer> f = |
3593 |
+ |
CompletableFuture.supplyAsync(() -> v, delayer); |
3594 |
+ |
CompletableFuture<Integer> g = |
3595 |
+ |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3596 |
+ |
|
3597 |
+ |
assertNull(g.getNow(null)); |
3598 |
+ |
|
3599 |
+ |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3600 |
+ |
long millisElapsed = millisElapsedSince(startTime); |
3601 |
+ |
assertTrue(millisElapsed >= timeoutMillis); |
3602 |
+ |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3603 |
+ |
|
3604 |
+ |
checkCompletedNormally(f, v); |
3605 |
+ |
|
3606 |
+ |
checkIncomplete(g); |
3607 |
+ |
assertTrue(g.cancel(true)); |
3608 |
+ |
} |
3609 |
+ |
|
3610 |
|
//--- tests of implementation details; not part of official tck --- |
3611 |
|
|
3612 |
|
Object resultOf(CompletableFuture<?> f) { |
3697 |
|
} |
3698 |
|
}} |
3699 |
|
|
3700 |
+ |
/** |
3701 |
+ |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3702 |
+ |
*/ |
3703 |
+ |
public void testMinimalCompletionStage_minimality() { |
3704 |
+ |
if (!testImplementationDetails) return; |
3705 |
+ |
Function<Method, String> toSignature = |
3706 |
+ |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3707 |
+ |
Predicate<Method> isNotStatic = |
3708 |
+ |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3709 |
+ |
List<Method> minimalMethods = |
3710 |
+ |
Stream.of(Object.class, CompletionStage.class) |
3711 |
+ |
.map((klazz) -> Stream.of(klazz.getMethods())) |
3712 |
+ |
.reduce(Stream::concat) |
3713 |
+ |
.orElseGet(Stream::empty) |
3714 |
+ |
.filter(isNotStatic) |
3715 |
+ |
.collect(Collectors.toList()); |
3716 |
+ |
// Methods from CompletableFuture permitted NOT to throw UOE |
3717 |
+ |
String[] signatureWhitelist = { |
3718 |
+ |
"newIncompleteFuture[]", |
3719 |
+ |
"defaultExecutor[]", |
3720 |
+ |
"minimalCompletionStage[]", |
3721 |
+ |
"copy[]", |
3722 |
+ |
}; |
3723 |
+ |
Set<String> permittedMethodSignatures = |
3724 |
+ |
Stream.concat(minimalMethods.stream().map(toSignature), |
3725 |
+ |
Stream.of(signatureWhitelist)) |
3726 |
+ |
.collect(Collectors.toSet()); |
3727 |
+ |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3728 |
+ |
.filter(isNotStatic) |
3729 |
+ |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3730 |
+ |
.collect(Collectors.toList()); |
3731 |
+ |
|
3732 |
+ |
CompletionStage<Integer> minimalStage = |
3733 |
+ |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3734 |
+ |
|
3735 |
+ |
List<Method> bugs = new ArrayList<>(); |
3736 |
+ |
for (Method method : allMethods) { |
3737 |
+ |
Class<?>[] parameterTypes = method.getParameterTypes(); |
3738 |
+ |
Object[] args = new Object[parameterTypes.length]; |
3739 |
+ |
// Manufacture boxed primitives for primitive params |
3740 |
+ |
for (int i = 0; i < args.length; i++) { |
3741 |
+ |
Class<?> type = parameterTypes[i]; |
3742 |
+ |
if (parameterTypes[i] == boolean.class) |
3743 |
+ |
args[i] = false; |
3744 |
+ |
else if (parameterTypes[i] == int.class) |
3745 |
+ |
args[i] = 0; |
3746 |
+ |
else if (parameterTypes[i] == long.class) |
3747 |
+ |
args[i] = 0L; |
3748 |
+ |
} |
3749 |
+ |
try { |
3750 |
+ |
method.invoke(minimalStage, args); |
3751 |
+ |
bugs.add(method); |
3752 |
+ |
} |
3753 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3754 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3755 |
+ |
bugs.add(method); |
3756 |
+ |
// expected.getCause().printStackTrace(); |
3757 |
+ |
} |
3758 |
+ |
} |
3759 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3760 |
+ |
} |
3761 |
+ |
if (!bugs.isEmpty()) |
3762 |
+ |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
3763 |
+ |
} |
3764 |
+ |
|
3765 |
+ |
// static <U> U join(CompletionStage<U> stage) { |
3766 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3767 |
+ |
// stage.whenComplete((v, ex) -> { |
3768 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3769 |
+ |
// }); |
3770 |
+ |
// return f.join(); |
3771 |
+ |
// } |
3772 |
+ |
|
3773 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
3774 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3775 |
+ |
// stage.whenComplete((v, ex) -> { |
3776 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3777 |
+ |
// }); |
3778 |
+ |
// return f.isDone(); |
3779 |
+ |
// } |
3780 |
+ |
|
3781 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
3782 |
+ |
// return stage.toCompletableFuture().copy().join(); |
3783 |
+ |
// } |
3784 |
+ |
|
3785 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
3786 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
3787 |
+ |
// } |
3788 |
+ |
|
3789 |
|
} |