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(); |
546 |
|
} |
547 |
|
} |
548 |
|
|
533 |
– |
|
549 |
|
class CompletableFutureInc extends CheckedIntegerAction |
550 |
|
implements Function<Integer, CompletableFuture<Integer>> |
551 |
|
{ |
584 |
|
} |
585 |
|
} |
586 |
|
|
587 |
+ |
static final boolean defaultExecutorIsCommonPool |
588 |
+ |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
589 |
+ |
|
590 |
|
/** |
591 |
|
* Permits the testing of parallel code for the 3 different |
592 |
|
* execution modes without copy/pasting all the test methods. |
593 |
|
*/ |
594 |
|
enum ExecutionMode { |
595 |
< |
DEFAULT { |
595 |
> |
SYNC { |
596 |
|
public void checkExecutionMode() { |
597 |
|
assertFalse(ThreadExecutor.startedCurrentThread()); |
598 |
|
assertNull(ForkJoinTask.getPool()); |
668 |
|
|
669 |
|
ASYNC { |
670 |
|
public void checkExecutionMode() { |
671 |
< |
assertSame(ForkJoinPool.commonPool(), |
672 |
< |
ForkJoinTask.getPool()); |
671 |
> |
assertEquals(defaultExecutorIsCommonPool, |
672 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
673 |
|
} |
674 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
675 |
|
return CompletableFuture.runAsync(a); |
893 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
894 |
|
final CompletableFuture<Integer> g = f.exceptionally |
895 |
|
((Throwable t) -> { |
896 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
896 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
897 |
|
threadAssertSame(t, ex); |
898 |
|
a.getAndIncrement(); |
899 |
|
return v1; |
915 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
916 |
|
final CompletableFuture<Integer> g = f.exceptionally |
917 |
|
((Throwable t) -> { |
918 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
918 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
919 |
|
threadAssertSame(t, ex1); |
920 |
|
a.getAndIncrement(); |
921 |
|
throw ex2; |
1618 |
|
{ |
1619 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1620 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1621 |
< |
final SubtractFunction r1 = new SubtractFunction(m); |
1622 |
< |
final SubtractFunction r2 = new SubtractFunction(m); |
1605 |
< |
final SubtractFunction r3 = new SubtractFunction(m); |
1621 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1622 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1623 |
|
|
1624 |
|
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1625 |
|
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1626 |
|
final Integer w1 = fFirst ? v1 : v2; |
1627 |
|
final Integer w2 = !fFirst ? v1 : v2; |
1628 |
|
|
1629 |
< |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1629 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1630 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1631 |
|
assertTrue(fst.complete(w1)); |
1632 |
< |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1633 |
< |
checkIncomplete(h1); |
1634 |
< |
checkIncomplete(h2); |
1635 |
< |
r1.assertNotInvoked(); |
1636 |
< |
r2.assertNotInvoked(); |
1632 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1633 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1634 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1635 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1636 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1637 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1638 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1639 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1640 |
|
assertTrue(snd.complete(w2)); |
1641 |
< |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1641 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1642 |
|
|
1643 |
< |
checkCompletedNormally(h1, subtract(v1, v2)); |
1643 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1644 |
|
checkCompletedNormally(h2, subtract(v1, v2)); |
1645 |
< |
checkCompletedNormally(h3, subtract(v1, v2)); |
1646 |
< |
r1.assertValue(subtract(v1, v2)); |
1647 |
< |
r2.assertValue(subtract(v1, v2)); |
1648 |
< |
r3.assertValue(subtract(v1, v2)); |
1645 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1646 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1647 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1648 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1649 |
> |
|
1650 |
|
checkCompletedNormally(f, v1); |
1651 |
|
checkCompletedNormally(g, v2); |
1652 |
|
}} |
2993 |
|
checkCancelled(f); |
2994 |
|
}} |
2995 |
|
|
2996 |
+ |
/** |
2997 |
+ |
* thenCompose result completes exceptionally if the result of the action does |
2998 |
+ |
*/ |
2999 |
+ |
public void testThenCompose_actionReturnsFailingFuture() { |
3000 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
3001 |
+ |
for (int order = 0; order < 6; order++) |
3002 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3003 |
+ |
{ |
3004 |
+ |
final CFException ex = new CFException(); |
3005 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3006 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3007 |
+ |
final CompletableFuture<Integer> h; |
3008 |
+ |
// Test all permutations of orders |
3009 |
+ |
switch (order) { |
3010 |
+ |
case 0: |
3011 |
+ |
assertTrue(f.complete(v1)); |
3012 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3013 |
+ |
h = m.thenCompose(f, (x -> g)); |
3014 |
+ |
break; |
3015 |
+ |
case 1: |
3016 |
+ |
assertTrue(f.complete(v1)); |
3017 |
+ |
h = m.thenCompose(f, (x -> g)); |
3018 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3019 |
+ |
break; |
3020 |
+ |
case 2: |
3021 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3022 |
+ |
assertTrue(f.complete(v1)); |
3023 |
+ |
h = m.thenCompose(f, (x -> g)); |
3024 |
+ |
break; |
3025 |
+ |
case 3: |
3026 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3027 |
+ |
h = m.thenCompose(f, (x -> g)); |
3028 |
+ |
assertTrue(f.complete(v1)); |
3029 |
+ |
break; |
3030 |
+ |
case 4: |
3031 |
+ |
h = m.thenCompose(f, (x -> g)); |
3032 |
+ |
assertTrue(f.complete(v1)); |
3033 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3034 |
+ |
break; |
3035 |
+ |
case 5: |
3036 |
+ |
h = m.thenCompose(f, (x -> g)); |
3037 |
+ |
assertTrue(f.complete(v1)); |
3038 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3039 |
+ |
break; |
3040 |
+ |
default: throw new AssertionError(); |
3041 |
+ |
} |
3042 |
+ |
|
3043 |
+ |
checkCompletedExceptionally(g, ex); |
3044 |
+ |
checkCompletedWithWrappedException(h, ex); |
3045 |
+ |
checkCompletedNormally(f, v1); |
3046 |
+ |
}} |
3047 |
+ |
|
3048 |
|
// other static methods |
3049 |
|
|
3050 |
|
/** |
3218 |
|
Runnable[] throwingActions = { |
3219 |
|
() -> CompletableFuture.supplyAsync(null), |
3220 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3221 |
< |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
3221 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3222 |
|
|
3223 |
|
() -> CompletableFuture.runAsync(null), |
3224 |
|
() -> CompletableFuture.runAsync(null, exec), |