8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.Callable; |
10 |
|
import java.util.concurrent.Executor; |
11 |
– |
import java.util.concurrent.ExecutorService; |
12 |
– |
import java.util.concurrent.Executors; |
11 |
|
import java.util.concurrent.CancellationException; |
14 |
– |
import java.util.concurrent.CountDownLatch; |
12 |
|
import java.util.concurrent.ExecutionException; |
16 |
– |
import java.util.concurrent.Future; |
13 |
|
import java.util.concurrent.CompletableFuture; |
14 |
|
import java.util.concurrent.CompletionException; |
15 |
|
import java.util.concurrent.CompletionStage; |
53 |
|
} |
54 |
|
|
55 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
56 |
< |
try { |
57 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
62 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
56 |
> |
checkTimedGet(f, value); |
57 |
> |
|
58 |
|
try { |
59 |
|
assertEquals(value, f.join()); |
60 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
71 |
|
} |
72 |
|
|
73 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
74 |
+ |
long startTime = System.nanoTime(); |
75 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
76 |
|
try { |
77 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
77 |
> |
f.get(timeoutMillis, MILLISECONDS); |
78 |
|
shouldThrow(); |
79 |
|
} catch (ExecutionException success) { |
80 |
|
assertTrue(success.getCause() instanceof CFException); |
81 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
82 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
83 |
+ |
|
84 |
|
try { |
85 |
|
f.join(); |
86 |
|
shouldThrow(); |
106 |
|
|
107 |
|
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
108 |
|
Throwable ex) { |
109 |
+ |
long startTime = System.nanoTime(); |
110 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
111 |
|
try { |
112 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
112 |
> |
f.get(timeoutMillis, MILLISECONDS); |
113 |
|
shouldThrow(); |
114 |
|
} catch (ExecutionException success) { |
115 |
|
assertSame(ex, success.getCause()); |
116 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
117 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
118 |
+ |
|
119 |
|
try { |
120 |
|
f.join(); |
121 |
|
shouldThrow(); |
161 |
|
} |
162 |
|
|
163 |
|
void checkCancelled(CompletableFuture<?> f) { |
164 |
+ |
long startTime = System.nanoTime(); |
165 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
166 |
|
try { |
167 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
167 |
> |
f.get(timeoutMillis, MILLISECONDS); |
168 |
|
shouldThrow(); |
169 |
|
} catch (CancellationException success) { |
170 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
171 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
172 |
+ |
|
173 |
|
try { |
174 |
|
f.join(); |
175 |
|
shouldThrow(); |
190 |
|
} |
191 |
|
|
192 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
193 |
+ |
long startTime = System.nanoTime(); |
194 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
195 |
|
try { |
196 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
196 |
> |
f.get(timeoutMillis, MILLISECONDS); |
197 |
|
shouldThrow(); |
198 |
|
} catch (ExecutionException success) { |
199 |
|
assertTrue(success.getCause() instanceof CancellationException); |
200 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
201 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
202 |
+ |
|
203 |
|
try { |
204 |
|
f.join(); |
205 |
|
shouldThrow(); |
580 |
|
} |
581 |
|
} |
582 |
|
|
583 |
+ |
static final boolean defaultExecutorIsCommonPool |
584 |
+ |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
585 |
+ |
|
586 |
|
/** |
587 |
|
* Permits the testing of parallel code for the 3 different |
588 |
|
* execution modes without copy/pasting all the test methods. |
589 |
|
*/ |
590 |
|
enum ExecutionMode { |
591 |
< |
DEFAULT { |
591 |
> |
SYNC { |
592 |
|
public void checkExecutionMode() { |
593 |
|
assertFalse(ThreadExecutor.startedCurrentThread()); |
594 |
|
assertNull(ForkJoinTask.getPool()); |
664 |
|
|
665 |
|
ASYNC { |
666 |
|
public void checkExecutionMode() { |
667 |
< |
assertSame(ForkJoinPool.commonPool(), |
668 |
< |
ForkJoinTask.getPool()); |
667 |
> |
assertEquals(defaultExecutorIsCommonPool, |
668 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
669 |
|
} |
670 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
671 |
|
return CompletableFuture.runAsync(a); |
889 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
890 |
|
final CompletableFuture<Integer> g = f.exceptionally |
891 |
|
((Throwable t) -> { |
892 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
892 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
893 |
|
threadAssertSame(t, ex); |
894 |
|
a.getAndIncrement(); |
895 |
|
return v1; |
911 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
912 |
|
final CompletableFuture<Integer> g = f.exceptionally |
913 |
|
((Throwable t) -> { |
914 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
914 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
915 |
|
threadAssertSame(t, ex1); |
916 |
|
a.getAndIncrement(); |
917 |
|
throw ex2; |
3162 |
|
Runnable[] throwingActions = { |
3163 |
|
() -> CompletableFuture.supplyAsync(null), |
3164 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3165 |
< |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
3165 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3166 |
|
|
3167 |
|
() -> CompletableFuture.runAsync(null), |
3168 |
|
() -> CompletableFuture.runAsync(null, exec), |