57 |
|
} |
58 |
|
|
59 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
60 |
< |
try { |
61 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
62 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
60 |
> |
checkTimedGet(f, value); |
61 |
> |
|
62 |
|
try { |
63 |
|
assertEquals(value, f.join()); |
64 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
75 |
|
} |
76 |
|
|
77 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
78 |
+ |
long startTime = System.nanoTime(); |
79 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
80 |
|
try { |
81 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
81 |
> |
f.get(timeoutMillis, MILLISECONDS); |
82 |
|
shouldThrow(); |
83 |
|
} catch (ExecutionException success) { |
84 |
|
assertTrue(success.getCause() instanceof CFException); |
85 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
86 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
87 |
+ |
|
88 |
|
try { |
89 |
|
f.join(); |
90 |
|
shouldThrow(); |
110 |
|
|
111 |
|
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
112 |
|
Throwable ex) { |
113 |
+ |
long startTime = System.nanoTime(); |
114 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
115 |
|
try { |
116 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
116 |
> |
f.get(timeoutMillis, MILLISECONDS); |
117 |
|
shouldThrow(); |
118 |
|
} catch (ExecutionException success) { |
119 |
|
assertSame(ex, success.getCause()); |
120 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
121 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
122 |
+ |
|
123 |
|
try { |
124 |
|
f.join(); |
125 |
|
shouldThrow(); |
165 |
|
} |
166 |
|
|
167 |
|
void checkCancelled(CompletableFuture<?> f) { |
168 |
+ |
long startTime = System.nanoTime(); |
169 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
170 |
|
try { |
171 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
171 |
> |
f.get(timeoutMillis, MILLISECONDS); |
172 |
|
shouldThrow(); |
173 |
|
} catch (CancellationException success) { |
174 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
175 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
176 |
+ |
|
177 |
|
try { |
178 |
|
f.join(); |
179 |
|
shouldThrow(); |
194 |
|
} |
195 |
|
|
196 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
197 |
+ |
long startTime = System.nanoTime(); |
198 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
199 |
|
try { |
200 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
200 |
> |
f.get(timeoutMillis, MILLISECONDS); |
201 |
|
shouldThrow(); |
202 |
|
} catch (ExecutionException success) { |
203 |
|
assertTrue(success.getCause() instanceof CancellationException); |
204 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
205 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
206 |
+ |
|
207 |
|
try { |
208 |
|
f.join(); |
209 |
|
shouldThrow(); |
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 |
|
}} |
3166 |
|
Runnable[] throwingActions = { |
3167 |
|
() -> CompletableFuture.supplyAsync(null), |
3168 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3169 |
< |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
3169 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3170 |
|
|
3171 |
|
() -> CompletableFuture.runAsync(null), |
3172 |
|
() -> CompletableFuture.runAsync(null, exec), |