5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
< |
import junit.framework.*; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
> |
|
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.List; |
13 |
> |
import java.util.Objects; |
14 |
|
import java.util.concurrent.Callable; |
10 |
– |
import java.util.concurrent.Executor; |
11 |
– |
import java.util.concurrent.ExecutorService; |
12 |
– |
import java.util.concurrent.Executors; |
15 |
|
import java.util.concurrent.CancellationException; |
14 |
– |
import java.util.concurrent.CountDownLatch; |
15 |
– |
import java.util.concurrent.ExecutionException; |
16 |
– |
import java.util.concurrent.Future; |
16 |
|
import java.util.concurrent.CompletableFuture; |
17 |
|
import java.util.concurrent.CompletionException; |
18 |
|
import java.util.concurrent.CompletionStage; |
19 |
+ |
import java.util.concurrent.ExecutionException; |
20 |
+ |
import java.util.concurrent.Executor; |
21 |
|
import java.util.concurrent.ForkJoinPool; |
22 |
|
import java.util.concurrent.ForkJoinTask; |
23 |
|
import java.util.concurrent.TimeoutException; |
24 |
|
import java.util.concurrent.atomic.AtomicInteger; |
24 |
– |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
25 |
– |
import static java.util.concurrent.TimeUnit.SECONDS; |
26 |
– |
import java.util.*; |
27 |
– |
import java.util.function.Supplier; |
28 |
– |
import java.util.function.Consumer; |
25 |
|
import java.util.function.BiConsumer; |
30 |
– |
import java.util.function.Function; |
26 |
|
import java.util.function.BiFunction; |
27 |
+ |
import java.util.function.Consumer; |
28 |
+ |
import java.util.function.Function; |
29 |
+ |
import java.util.function.Supplier; |
30 |
+ |
|
31 |
+ |
import junit.framework.Test; |
32 |
+ |
import junit.framework.TestSuite; |
33 |
|
|
34 |
|
public class CompletableFutureTest extends JSR166TestCase { |
35 |
|
|
58 |
|
} |
59 |
|
|
60 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
61 |
< |
try { |
62 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
62 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
61 |
> |
checkTimedGet(f, value); |
62 |
> |
|
63 |
|
try { |
64 |
|
assertEquals(value, f.join()); |
65 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
76 |
|
} |
77 |
|
|
78 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
79 |
+ |
long startTime = System.nanoTime(); |
80 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
81 |
|
try { |
82 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
82 |
> |
f.get(timeoutMillis, MILLISECONDS); |
83 |
|
shouldThrow(); |
84 |
|
} catch (ExecutionException success) { |
85 |
|
assertTrue(success.getCause() instanceof CFException); |
86 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
87 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
88 |
+ |
|
89 |
|
try { |
90 |
|
f.join(); |
91 |
|
shouldThrow(); |
111 |
|
|
112 |
|
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
113 |
|
Throwable ex) { |
114 |
+ |
long startTime = System.nanoTime(); |
115 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
116 |
|
try { |
117 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
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); |
123 |
+ |
|
124 |
|
try { |
125 |
|
f.join(); |
126 |
|
shouldThrow(); |
166 |
|
} |
167 |
|
|
168 |
|
void checkCancelled(CompletableFuture<?> f) { |
169 |
+ |
long startTime = System.nanoTime(); |
170 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
171 |
|
try { |
172 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
172 |
> |
f.get(timeoutMillis, MILLISECONDS); |
173 |
|
shouldThrow(); |
174 |
|
} catch (CancellationException success) { |
175 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
176 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
177 |
+ |
|
178 |
|
try { |
179 |
|
f.join(); |
180 |
|
shouldThrow(); |
195 |
|
} |
196 |
|
|
197 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
198 |
+ |
long startTime = System.nanoTime(); |
199 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
200 |
|
try { |
201 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
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); |
207 |
+ |
|
208 |
|
try { |
209 |
|
f.join(); |
210 |
|
shouldThrow(); |
585 |
|
} |
586 |
|
} |
587 |
|
|
588 |
+ |
static final boolean defaultExecutorIsCommonPool |
589 |
+ |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
590 |
+ |
|
591 |
|
/** |
592 |
|
* Permits the testing of parallel code for the 3 different |
593 |
|
* execution modes without copy/pasting all the test methods. |
594 |
|
*/ |
595 |
|
enum ExecutionMode { |
596 |
< |
DEFAULT { |
596 |
> |
SYNC { |
597 |
|
public void checkExecutionMode() { |
598 |
|
assertFalse(ThreadExecutor.startedCurrentThread()); |
599 |
|
assertNull(ForkJoinTask.getPool()); |
669 |
|
|
670 |
|
ASYNC { |
671 |
|
public void checkExecutionMode() { |
672 |
< |
assertSame(ForkJoinPool.commonPool(), |
673 |
< |
ForkJoinTask.getPool()); |
672 |
> |
assertEquals(defaultExecutorIsCommonPool, |
673 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
674 |
|
} |
675 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
676 |
|
return CompletableFuture.runAsync(a); |
894 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
895 |
|
final CompletableFuture<Integer> g = f.exceptionally |
896 |
|
((Throwable t) -> { |
897 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
897 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
898 |
|
threadAssertSame(t, ex); |
899 |
|
a.getAndIncrement(); |
900 |
|
return v1; |
916 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
917 |
|
final CompletableFuture<Integer> g = f.exceptionally |
918 |
|
((Throwable t) -> { |
919 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
919 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
920 |
|
threadAssertSame(t, ex1); |
921 |
|
a.getAndIncrement(); |
922 |
|
throw ex2; |
2994 |
|
checkCancelled(f); |
2995 |
|
}} |
2996 |
|
|
2997 |
+ |
/** |
2998 |
+ |
* thenCompose result completes exceptionally if the result of the action does |
2999 |
+ |
*/ |
3000 |
+ |
public void testThenCompose_actionReturnsFailingFuture() { |
3001 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
3002 |
+ |
for (int order = 0; order < 6; order++) |
3003 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3004 |
+ |
{ |
3005 |
+ |
final CFException ex = new CFException(); |
3006 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3007 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3008 |
+ |
final CompletableFuture<Integer> h; |
3009 |
+ |
// Test all permutations of orders |
3010 |
+ |
switch (order) { |
3011 |
+ |
case 0: |
3012 |
+ |
assertTrue(f.complete(v1)); |
3013 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3014 |
+ |
h = m.thenCompose(f, (x -> g)); |
3015 |
+ |
break; |
3016 |
+ |
case 1: |
3017 |
+ |
assertTrue(f.complete(v1)); |
3018 |
+ |
h = m.thenCompose(f, (x -> g)); |
3019 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3020 |
+ |
break; |
3021 |
+ |
case 2: |
3022 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3023 |
+ |
assertTrue(f.complete(v1)); |
3024 |
+ |
h = m.thenCompose(f, (x -> g)); |
3025 |
+ |
break; |
3026 |
+ |
case 3: |
3027 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3028 |
+ |
h = m.thenCompose(f, (x -> g)); |
3029 |
+ |
assertTrue(f.complete(v1)); |
3030 |
+ |
break; |
3031 |
+ |
case 4: |
3032 |
+ |
h = m.thenCompose(f, (x -> g)); |
3033 |
+ |
assertTrue(f.complete(v1)); |
3034 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3035 |
+ |
break; |
3036 |
+ |
case 5: |
3037 |
+ |
h = m.thenCompose(f, (x -> g)); |
3038 |
+ |
assertTrue(f.complete(v1)); |
3039 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3040 |
+ |
break; |
3041 |
+ |
default: throw new AssertionError(); |
3042 |
+ |
} |
3043 |
+ |
|
3044 |
+ |
checkCompletedExceptionally(g, ex); |
3045 |
+ |
checkCompletedWithWrappedException(h, ex); |
3046 |
+ |
checkCompletedNormally(f, v1); |
3047 |
+ |
}} |
3048 |
+ |
|
3049 |
|
// other static methods |
3050 |
|
|
3051 |
|
/** |
3219 |
|
Runnable[] throwingActions = { |
3220 |
|
() -> CompletableFuture.supplyAsync(null), |
3221 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3222 |
< |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
3222 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3223 |
|
|
3224 |
|
() -> CompletableFuture.runAsync(null), |
3225 |
|
() -> CompletableFuture.runAsync(null, exec), |