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(); |
109 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
110 |
|
} |
111 |
|
|
112 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f, |
113 |
< |
CFException ex) { |
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(); |
139 |
|
} catch (ExecutionException success) { |
140 |
|
assertSame(ex, success.getCause()); |
141 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
142 |
+ |
|
143 |
|
assertTrue(f.isDone()); |
144 |
|
assertFalse(f.isCancelled()); |
145 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
146 |
|
} |
147 |
|
|
148 |
+ |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
149 |
+ |
Throwable ex) { |
150 |
+ |
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); } |
157 |
+ |
} |
158 |
+ |
|
159 |
+ |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
160 |
+ |
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); } |
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(); |
243 |
|
* isCancelled, join, get, and getNow |
244 |
|
*/ |
245 |
|
public void testComplete() { |
246 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
247 |
+ |
{ |
248 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
249 |
|
checkIncomplete(f); |
250 |
< |
f.complete(one); |
251 |
< |
checkCompletedNormally(f, one); |
252 |
< |
} |
250 |
> |
assertTrue(f.complete(v1)); |
251 |
> |
assertFalse(f.complete(v1)); |
252 |
> |
checkCompletedNormally(f, v1); |
253 |
> |
}} |
254 |
|
|
255 |
|
/** |
256 |
|
* completeExceptionally completes exceptionally, as indicated by |
258 |
|
*/ |
259 |
|
public void testCompleteExceptionally() { |
260 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
261 |
+ |
CFException ex = new CFException(); |
262 |
|
checkIncomplete(f); |
263 |
< |
f.completeExceptionally(new CFException()); |
264 |
< |
checkCompletedWithWrappedCFException(f); |
263 |
> |
f.completeExceptionally(ex); |
264 |
> |
checkCompletedExceptionally(f, ex); |
265 |
|
} |
266 |
|
|
267 |
|
/** |
269 |
|
* methods isDone, isCancelled, join, get, and getNow |
270 |
|
*/ |
271 |
|
public void testCancel() { |
272 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
273 |
+ |
{ |
274 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
275 |
|
checkIncomplete(f); |
276 |
|
assertTrue(f.cancel(true)); |
277 |
+ |
assertTrue(f.cancel(true)); |
278 |
|
checkCancelled(f); |
279 |
< |
} |
279 |
> |
}} |
280 |
|
|
281 |
|
/** |
282 |
|
* obtrudeValue forces completion with given value |
284 |
|
public void testObtrudeValue() { |
285 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
286 |
|
checkIncomplete(f); |
287 |
< |
f.complete(one); |
287 |
> |
assertTrue(f.complete(one)); |
288 |
|
checkCompletedNormally(f, one); |
289 |
|
f.obtrudeValue(three); |
290 |
|
checkCompletedNormally(f, three); |
305 |
|
* obtrudeException forces completion with given exception |
306 |
|
*/ |
307 |
|
public void testObtrudeException() { |
308 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
309 |
< |
checkIncomplete(f); |
310 |
< |
f.complete(one); |
311 |
< |
checkCompletedNormally(f, one); |
312 |
< |
f.obtrudeException(new CFException()); |
269 |
< |
checkCompletedWithWrappedCFException(f); |
308 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
309 |
> |
{ |
310 |
> |
CFException ex; |
311 |
> |
CompletableFuture<Integer> f; |
312 |
> |
|
313 |
|
f = new CompletableFuture<>(); |
314 |
< |
f.obtrudeException(new CFException()); |
315 |
< |
checkCompletedWithWrappedCFException(f); |
314 |
> |
assertTrue(f.complete(v1)); |
315 |
> |
for (int i = 0; i < 2; i++) { |
316 |
> |
f.obtrudeException(ex = new CFException()); |
317 |
> |
checkCompletedExceptionally(f, ex); |
318 |
> |
} |
319 |
> |
|
320 |
> |
f = new CompletableFuture<>(); |
321 |
> |
for (int i = 0; i < 2; i++) { |
322 |
> |
f.obtrudeException(ex = new CFException()); |
323 |
> |
checkCompletedExceptionally(f, ex); |
324 |
> |
} |
325 |
> |
|
326 |
|
f = new CompletableFuture<>(); |
327 |
+ |
f.completeExceptionally(ex = new CFException()); |
328 |
+ |
f.obtrudeValue(v1); |
329 |
+ |
checkCompletedNormally(f, v1); |
330 |
+ |
f.obtrudeException(ex = new CFException()); |
331 |
+ |
checkCompletedExceptionally(f, ex); |
332 |
|
f.completeExceptionally(new CFException()); |
333 |
< |
f.obtrudeValue(four); |
334 |
< |
checkCompletedNormally(f, four); |
335 |
< |
f.obtrudeException(new CFException()); |
336 |
< |
checkCompletedWithWrappedCFException(f); |
279 |
< |
} |
333 |
> |
checkCompletedExceptionally(f, ex); |
334 |
> |
assertFalse(f.complete(v1)); |
335 |
> |
checkCompletedExceptionally(f, ex); |
336 |
> |
}} |
337 |
|
|
338 |
|
/** |
339 |
|
* getNumberOfDependents returns number of dependent tasks |
340 |
|
*/ |
341 |
|
public void testGetNumberOfDependents() { |
342 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
343 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
344 |
+ |
{ |
345 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
346 |
|
assertEquals(0, f.getNumberOfDependents()); |
347 |
< |
CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
347 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
348 |
|
assertEquals(1, f.getNumberOfDependents()); |
349 |
|
assertEquals(0, g.getNumberOfDependents()); |
350 |
< |
CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
350 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
351 |
|
assertEquals(2, f.getNumberOfDependents()); |
352 |
< |
f.complete(1); |
352 |
> |
assertEquals(0, h.getNumberOfDependents()); |
353 |
> |
assertTrue(f.complete(v1)); |
354 |
|
checkCompletedNormally(g, null); |
355 |
+ |
checkCompletedNormally(h, null); |
356 |
|
assertEquals(0, f.getNumberOfDependents()); |
357 |
|
assertEquals(0, g.getNumberOfDependents()); |
358 |
< |
} |
358 |
> |
assertEquals(0, h.getNumberOfDependents()); |
359 |
> |
}} |
360 |
|
|
361 |
|
/** |
362 |
|
* toString indicates current completion state |
367 |
|
f = new CompletableFuture<String>(); |
368 |
|
assertTrue(f.toString().contains("[Not completed]")); |
369 |
|
|
370 |
< |
f.complete("foo"); |
370 |
> |
assertTrue(f.complete("foo")); |
371 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
372 |
|
|
373 |
|
f = new CompletableFuture<String>(); |
374 |
< |
f.completeExceptionally(new IndexOutOfBoundsException()); |
374 |
> |
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
375 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
376 |
+ |
|
377 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
378 |
+ |
f = new CompletableFuture<String>(); |
379 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
380 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
381 |
+ |
} |
382 |
|
} |
383 |
|
|
384 |
|
/** |
555 |
|
invoked(); |
556 |
|
value = x; |
557 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
558 |
< |
f.complete(inc(x)); |
558 |
> |
assertTrue(f.complete(inc(x))); |
559 |
|
return f; |
560 |
|
} |
561 |
|
} |
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); |
866 |
|
{ |
867 |
|
final AtomicInteger a = new AtomicInteger(0); |
868 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
869 |
< |
if (!createIncomplete) f.complete(v1); |
869 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
870 |
|
final CompletableFuture<Integer> g = f.exceptionally |
871 |
|
((Throwable t) -> { |
872 |
|
// Should not be called |
873 |
|
a.getAndIncrement(); |
874 |
|
throw new AssertionError(); |
875 |
|
}); |
876 |
< |
if (createIncomplete) f.complete(v1); |
876 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
877 |
|
|
878 |
|
checkCompletedNormally(g, v1); |
879 |
|
checkCompletedNormally(f, v1); |
880 |
|
assertEquals(0, a.get()); |
881 |
|
}} |
882 |
|
|
811 |
– |
|
883 |
|
/** |
884 |
|
* exceptionally action completes with function value on source |
885 |
|
* exception |
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; |
923 |
|
}); |
924 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
925 |
|
|
926 |
< |
checkCompletedWithWrappedCFException(g, ex2); |
926 |
> |
checkCompletedWithWrappedException(g, ex2); |
927 |
> |
assertEquals(1, a.get()); |
928 |
> |
}} |
929 |
> |
|
930 |
> |
/** |
931 |
> |
* whenComplete action executes on normal completion, propagating |
932 |
> |
* source result. |
933 |
> |
*/ |
934 |
> |
public void testWhenComplete_normalCompletion1() { |
935 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
936 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
937 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
938 |
> |
{ |
939 |
> |
final AtomicInteger a = new AtomicInteger(0); |
940 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
941 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
942 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
943 |
> |
(f, |
944 |
> |
(Integer x, Throwable t) -> { |
945 |
> |
m.checkExecutionMode(); |
946 |
> |
threadAssertSame(x, v1); |
947 |
> |
threadAssertNull(t); |
948 |
> |
a.getAndIncrement(); |
949 |
> |
}); |
950 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
951 |
> |
|
952 |
> |
checkCompletedNormally(g, v1); |
953 |
> |
checkCompletedNormally(f, v1); |
954 |
> |
assertEquals(1, a.get()); |
955 |
> |
}} |
956 |
> |
|
957 |
> |
/** |
958 |
> |
* whenComplete action executes on exceptional completion, propagating |
959 |
> |
* source result. |
960 |
> |
*/ |
961 |
> |
public void testWhenComplete_exceptionalCompletion() { |
962 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
963 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
964 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
965 |
> |
{ |
966 |
> |
final AtomicInteger a = new AtomicInteger(0); |
967 |
> |
final CFException ex = new CFException(); |
968 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
969 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
970 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
971 |
> |
(f, |
972 |
> |
(Integer x, Throwable t) -> { |
973 |
> |
m.checkExecutionMode(); |
974 |
> |
threadAssertNull(x); |
975 |
> |
threadAssertSame(t, ex); |
976 |
> |
a.getAndIncrement(); |
977 |
> |
}); |
978 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
979 |
> |
|
980 |
> |
checkCompletedWithWrappedException(g, ex); |
981 |
> |
checkCompletedExceptionally(f, ex); |
982 |
> |
assertEquals(1, a.get()); |
983 |
> |
}} |
984 |
> |
|
985 |
> |
/** |
986 |
> |
* whenComplete action executes on cancelled source, propagating |
987 |
> |
* CancellationException. |
988 |
> |
*/ |
989 |
> |
public void testWhenComplete_sourceCancelled() { |
990 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
991 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
992 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
993 |
> |
{ |
994 |
> |
final AtomicInteger a = new AtomicInteger(0); |
995 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
996 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
997 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
998 |
> |
(f, |
999 |
> |
(Integer x, Throwable t) -> { |
1000 |
> |
m.checkExecutionMode(); |
1001 |
> |
threadAssertNull(x); |
1002 |
> |
threadAssertTrue(t instanceof CancellationException); |
1003 |
> |
a.getAndIncrement(); |
1004 |
> |
}); |
1005 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1006 |
> |
|
1007 |
> |
checkCompletedWithWrappedCancellationException(g); |
1008 |
> |
checkCancelled(f); |
1009 |
> |
assertEquals(1, a.get()); |
1010 |
> |
}} |
1011 |
> |
|
1012 |
> |
/** |
1013 |
> |
* If a whenComplete action throws an exception when triggered by |
1014 |
> |
* a normal completion, it completes exceptionally |
1015 |
> |
*/ |
1016 |
> |
public void testWhenComplete_actionFailed() { |
1017 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1018 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1019 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1020 |
> |
{ |
1021 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1022 |
> |
final CFException ex = new CFException(); |
1023 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1024 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1025 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1026 |
> |
(f, |
1027 |
> |
(Integer x, Throwable t) -> { |
1028 |
> |
m.checkExecutionMode(); |
1029 |
> |
threadAssertSame(x, v1); |
1030 |
> |
threadAssertNull(t); |
1031 |
> |
a.getAndIncrement(); |
1032 |
> |
throw ex; |
1033 |
> |
}); |
1034 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1035 |
> |
|
1036 |
> |
checkCompletedWithWrappedException(g, ex); |
1037 |
> |
checkCompletedNormally(f, v1); |
1038 |
> |
assertEquals(1, a.get()); |
1039 |
> |
}} |
1040 |
> |
|
1041 |
> |
/** |
1042 |
> |
* If a whenComplete action throws an exception when triggered by |
1043 |
> |
* a source completion that also throws an exception, the source |
1044 |
> |
* exception takes precedence. |
1045 |
> |
*/ |
1046 |
> |
public void testWhenComplete_actionFailedSourceFailed() { |
1047 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1048 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1049 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1050 |
> |
{ |
1051 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1052 |
> |
final CFException ex1 = new CFException(); |
1053 |
> |
final CFException ex2 = new CFException(); |
1054 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1055 |
> |
|
1056 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1057 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1058 |
> |
(f, |
1059 |
> |
(Integer x, Throwable t) -> { |
1060 |
> |
m.checkExecutionMode(); |
1061 |
> |
threadAssertSame(t, ex1); |
1062 |
> |
threadAssertNull(x); |
1063 |
> |
a.getAndIncrement(); |
1064 |
> |
throw ex2; |
1065 |
> |
}); |
1066 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1067 |
> |
|
1068 |
> |
checkCompletedWithWrappedException(g, ex1); |
1069 |
> |
checkCompletedExceptionally(f, ex1); |
1070 |
|
assertEquals(1, a.get()); |
1071 |
|
}} |
1072 |
|
|
1081 |
|
{ |
1082 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1083 |
|
final AtomicInteger a = new AtomicInteger(0); |
1084 |
< |
if (!createIncomplete) f.complete(v1); |
1084 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1085 |
|
final CompletableFuture<Integer> g = m.handle |
1086 |
|
(f, |
1087 |
|
(Integer x, Throwable t) -> { |
1091 |
|
a.getAndIncrement(); |
1092 |
|
return inc(v1); |
1093 |
|
}); |
1094 |
< |
if (createIncomplete) f.complete(v1); |
1094 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1095 |
|
|
1096 |
|
checkCompletedNormally(g, inc(v1)); |
1097 |
|
checkCompletedNormally(f, v1); |
1123 |
|
if (createIncomplete) f.completeExceptionally(ex); |
1124 |
|
|
1125 |
|
checkCompletedNormally(g, v1); |
1126 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1126 |
> |
checkCompletedExceptionally(f, ex); |
1127 |
|
assertEquals(1, a.get()); |
1128 |
|
}} |
1129 |
|
|
1179 |
|
}); |
1180 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
1181 |
|
|
1182 |
< |
checkCompletedWithWrappedCFException(g, ex2); |
1183 |
< |
checkCompletedWithWrappedCFException(f, ex1); |
1182 |
> |
checkCompletedWithWrappedException(g, ex2); |
1183 |
> |
checkCompletedExceptionally(f, ex1); |
1184 |
|
assertEquals(1, a.get()); |
1185 |
|
}} |
1186 |
|
|
1192 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1193 |
|
final AtomicInteger a = new AtomicInteger(0); |
1194 |
|
final CFException ex = new CFException(); |
1195 |
< |
if (!createIncomplete) f.complete(v1); |
1195 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1196 |
|
final CompletableFuture<Integer> g = m.handle |
1197 |
|
(f, |
1198 |
|
(Integer x, Throwable t) -> { |
1202 |
|
a.getAndIncrement(); |
1203 |
|
throw ex; |
1204 |
|
}); |
1205 |
< |
if (createIncomplete) f.complete(v1); |
1205 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1206 |
|
|
1207 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1207 |
> |
checkCompletedWithWrappedException(g, ex); |
1208 |
|
checkCompletedNormally(f, v1); |
1209 |
|
assertEquals(1, a.get()); |
1210 |
|
}} |
1283 |
|
*/ |
1284 |
|
public void testThenRun_normalCompletion() { |
1285 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1072 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1286 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1287 |
|
{ |
1288 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1289 |
< |
final Noop r = new Noop(m); |
1290 |
< |
if (!createIncomplete) f.complete(v1); |
1078 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1079 |
< |
if (createIncomplete) { |
1080 |
< |
checkIncomplete(g); |
1081 |
< |
f.complete(v1); |
1082 |
< |
} |
1289 |
> |
final Noop[] rs = new Noop[6]; |
1290 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1291 |
|
|
1292 |
< |
checkCompletedNormally(g, null); |
1292 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1293 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1294 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1295 |
> |
checkIncomplete(h0); |
1296 |
> |
checkIncomplete(h1); |
1297 |
> |
checkIncomplete(h2); |
1298 |
> |
assertTrue(f.complete(v1)); |
1299 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1300 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1301 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1302 |
> |
|
1303 |
> |
checkCompletedNormally(h0, null); |
1304 |
> |
checkCompletedNormally(h1, null); |
1305 |
> |
checkCompletedNormally(h2, null); |
1306 |
> |
checkCompletedNormally(h3, null); |
1307 |
> |
checkCompletedNormally(h4, null); |
1308 |
> |
checkCompletedNormally(h5, null); |
1309 |
|
checkCompletedNormally(f, v1); |
1310 |
< |
r.assertInvoked(); |
1310 |
> |
for (Noop r : rs) r.assertInvoked(); |
1311 |
|
}} |
1312 |
|
|
1313 |
|
/** |
1316 |
|
*/ |
1317 |
|
public void testThenRun_exceptionalCompletion() { |
1318 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1095 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1319 |
|
{ |
1320 |
|
final CFException ex = new CFException(); |
1321 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1322 |
< |
final Noop r = new Noop(m); |
1323 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1101 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1102 |
< |
if (createIncomplete) { |
1103 |
< |
checkIncomplete(g); |
1104 |
< |
f.completeExceptionally(ex); |
1105 |
< |
} |
1322 |
> |
final Noop[] rs = new Noop[6]; |
1323 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1324 |
|
|
1325 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1326 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1327 |
< |
r.assertNotInvoked(); |
1325 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1326 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1327 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1328 |
> |
checkIncomplete(h0); |
1329 |
> |
checkIncomplete(h1); |
1330 |
> |
checkIncomplete(h2); |
1331 |
> |
assertTrue(f.completeExceptionally(ex)); |
1332 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1333 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1334 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1335 |
> |
|
1336 |
> |
checkCompletedWithWrappedException(h0, ex); |
1337 |
> |
checkCompletedWithWrappedException(h1, ex); |
1338 |
> |
checkCompletedWithWrappedException(h2, ex); |
1339 |
> |
checkCompletedWithWrappedException(h3, ex); |
1340 |
> |
checkCompletedWithWrappedException(h4, ex); |
1341 |
> |
checkCompletedWithWrappedException(h5, ex); |
1342 |
> |
checkCompletedExceptionally(f, ex); |
1343 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1344 |
|
}} |
1345 |
|
|
1346 |
|
/** |
1348 |
|
*/ |
1349 |
|
public void testThenRun_sourceCancelled() { |
1350 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1117 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1351 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1352 |
|
{ |
1353 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1354 |
< |
final Noop r = new Noop(m); |
1355 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1123 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1124 |
< |
if (createIncomplete) { |
1125 |
< |
checkIncomplete(g); |
1126 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1127 |
< |
} |
1354 |
> |
final Noop[] rs = new Noop[6]; |
1355 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1356 |
|
|
1357 |
< |
checkCompletedWithWrappedCancellationException(g); |
1357 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1358 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1359 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1360 |
> |
checkIncomplete(h0); |
1361 |
> |
checkIncomplete(h1); |
1362 |
> |
checkIncomplete(h2); |
1363 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1364 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1365 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1366 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1367 |
> |
|
1368 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1369 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1370 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1371 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1372 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1373 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1374 |
|
checkCancelled(f); |
1375 |
< |
r.assertNotInvoked(); |
1375 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1376 |
|
}} |
1377 |
|
|
1378 |
|
/** |
1380 |
|
*/ |
1381 |
|
public void testThenRun_actionFailed() { |
1382 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1139 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1383 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1384 |
|
{ |
1385 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1386 |
< |
final FailingRunnable r = new FailingRunnable(m); |
1387 |
< |
if (!createIncomplete) f.complete(v1); |
1145 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1146 |
< |
if (createIncomplete) { |
1147 |
< |
checkIncomplete(g); |
1148 |
< |
f.complete(v1); |
1149 |
< |
} |
1386 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1387 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1388 |
|
|
1389 |
< |
checkCompletedWithWrappedCFException(g); |
1389 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1390 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1391 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1392 |
> |
assertTrue(f.complete(v1)); |
1393 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1394 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1395 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1396 |
> |
|
1397 |
> |
checkCompletedWithWrappedCFException(h0); |
1398 |
> |
checkCompletedWithWrappedCFException(h1); |
1399 |
> |
checkCompletedWithWrappedCFException(h2); |
1400 |
> |
checkCompletedWithWrappedCFException(h3); |
1401 |
> |
checkCompletedWithWrappedCFException(h4); |
1402 |
> |
checkCompletedWithWrappedCFException(h5); |
1403 |
|
checkCompletedNormally(f, v1); |
1404 |
|
}} |
1405 |
|
|
1408 |
|
*/ |
1409 |
|
public void testThenApply_normalCompletion() { |
1410 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1160 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1411 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1412 |
|
{ |
1413 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1414 |
< |
final IncFunction r = new IncFunction(m); |
1415 |
< |
if (!createIncomplete) f.complete(v1); |
1166 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1167 |
< |
if (createIncomplete) { |
1168 |
< |
checkIncomplete(g); |
1169 |
< |
f.complete(v1); |
1170 |
< |
} |
1414 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1415 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1416 |
|
|
1417 |
< |
checkCompletedNormally(g, inc(v1)); |
1417 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1418 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1419 |
> |
checkIncomplete(h0); |
1420 |
> |
checkIncomplete(h1); |
1421 |
> |
assertTrue(f.complete(v1)); |
1422 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1423 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1424 |
> |
|
1425 |
> |
checkCompletedNormally(h0, inc(v1)); |
1426 |
> |
checkCompletedNormally(h1, inc(v1)); |
1427 |
> |
checkCompletedNormally(h2, inc(v1)); |
1428 |
> |
checkCompletedNormally(h3, inc(v1)); |
1429 |
|
checkCompletedNormally(f, v1); |
1430 |
< |
r.assertValue(inc(v1)); |
1430 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1431 |
|
}} |
1432 |
|
|
1433 |
|
/** |
1436 |
|
*/ |
1437 |
|
public void testThenApply_exceptionalCompletion() { |
1438 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1183 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1439 |
|
{ |
1440 |
|
final CFException ex = new CFException(); |
1441 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1442 |
< |
final IncFunction r = new IncFunction(m); |
1443 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1189 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1190 |
< |
if (createIncomplete) { |
1191 |
< |
checkIncomplete(g); |
1192 |
< |
f.completeExceptionally(ex); |
1193 |
< |
} |
1442 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1443 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1444 |
|
|
1445 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1446 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1447 |
< |
r.assertNotInvoked(); |
1445 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1446 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1447 |
> |
assertTrue(f.completeExceptionally(ex)); |
1448 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1449 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1450 |
> |
|
1451 |
> |
checkCompletedWithWrappedException(h0, ex); |
1452 |
> |
checkCompletedWithWrappedException(h1, ex); |
1453 |
> |
checkCompletedWithWrappedException(h2, ex); |
1454 |
> |
checkCompletedWithWrappedException(h3, ex); |
1455 |
> |
checkCompletedExceptionally(f, ex); |
1456 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1457 |
|
}} |
1458 |
|
|
1459 |
|
/** |
1461 |
|
*/ |
1462 |
|
public void testThenApply_sourceCancelled() { |
1463 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1205 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1464 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1465 |
|
{ |
1466 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1467 |
< |
final IncFunction r = new IncFunction(m); |
1468 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1211 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1212 |
< |
if (createIncomplete) { |
1213 |
< |
checkIncomplete(g); |
1214 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1215 |
< |
} |
1467 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1468 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1469 |
|
|
1470 |
< |
checkCompletedWithWrappedCancellationException(g); |
1470 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1471 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1472 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1473 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1474 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1475 |
> |
|
1476 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1477 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1478 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1479 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1480 |
|
checkCancelled(f); |
1481 |
< |
r.assertNotInvoked(); |
1481 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1482 |
|
}} |
1483 |
|
|
1484 |
|
/** |
1486 |
|
*/ |
1487 |
|
public void testThenApply_actionFailed() { |
1488 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1227 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1489 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1490 |
|
{ |
1491 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1492 |
< |
final FailingFunction r = new FailingFunction(m); |
1493 |
< |
if (!createIncomplete) f.complete(v1); |
1233 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1234 |
< |
if (createIncomplete) { |
1235 |
< |
checkIncomplete(g); |
1236 |
< |
f.complete(v1); |
1237 |
< |
} |
1492 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1493 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1494 |
|
|
1495 |
< |
checkCompletedWithWrappedCFException(g); |
1495 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1496 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1497 |
> |
assertTrue(f.complete(v1)); |
1498 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1499 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1500 |
> |
|
1501 |
> |
checkCompletedWithWrappedCFException(h0); |
1502 |
> |
checkCompletedWithWrappedCFException(h1); |
1503 |
> |
checkCompletedWithWrappedCFException(h2); |
1504 |
> |
checkCompletedWithWrappedCFException(h3); |
1505 |
|
checkCompletedNormally(f, v1); |
1506 |
|
}} |
1507 |
|
|
1510 |
|
*/ |
1511 |
|
public void testThenAccept_normalCompletion() { |
1512 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1248 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1513 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1514 |
|
{ |
1515 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1516 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1517 |
< |
if (!createIncomplete) f.complete(v1); |
1254 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1255 |
< |
if (createIncomplete) { |
1256 |
< |
checkIncomplete(g); |
1257 |
< |
f.complete(v1); |
1258 |
< |
} |
1516 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1517 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1518 |
|
|
1519 |
< |
checkCompletedNormally(g, null); |
1520 |
< |
r.assertValue(v1); |
1519 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1520 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1521 |
> |
checkIncomplete(h0); |
1522 |
> |
checkIncomplete(h1); |
1523 |
> |
assertTrue(f.complete(v1)); |
1524 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1525 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1526 |
> |
|
1527 |
> |
checkCompletedNormally(h0, null); |
1528 |
> |
checkCompletedNormally(h1, null); |
1529 |
> |
checkCompletedNormally(h2, null); |
1530 |
> |
checkCompletedNormally(h3, null); |
1531 |
|
checkCompletedNormally(f, v1); |
1532 |
+ |
for (NoopConsumer r : rs) r.assertValue(v1); |
1533 |
|
}} |
1534 |
|
|
1535 |
|
/** |
1538 |
|
*/ |
1539 |
|
public void testThenAccept_exceptionalCompletion() { |
1540 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1271 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1541 |
|
{ |
1542 |
|
final CFException ex = new CFException(); |
1543 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1544 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1545 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1277 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1278 |
< |
if (createIncomplete) { |
1279 |
< |
checkIncomplete(g); |
1280 |
< |
f.completeExceptionally(ex); |
1281 |
< |
} |
1544 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1545 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1546 |
|
|
1547 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1548 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1549 |
< |
r.assertNotInvoked(); |
1547 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1548 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1549 |
> |
assertTrue(f.completeExceptionally(ex)); |
1550 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1551 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1552 |
> |
|
1553 |
> |
checkCompletedWithWrappedException(h0, ex); |
1554 |
> |
checkCompletedWithWrappedException(h1, ex); |
1555 |
> |
checkCompletedWithWrappedException(h2, ex); |
1556 |
> |
checkCompletedWithWrappedException(h3, ex); |
1557 |
> |
checkCompletedExceptionally(f, ex); |
1558 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1559 |
|
}} |
1560 |
|
|
1561 |
|
/** |
1563 |
|
*/ |
1564 |
|
public void testThenAccept_sourceCancelled() { |
1565 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1293 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1566 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1567 |
|
{ |
1568 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1569 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1570 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1299 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1300 |
< |
if (createIncomplete) { |
1301 |
< |
checkIncomplete(g); |
1302 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1303 |
< |
} |
1569 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1570 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1571 |
|
|
1572 |
< |
checkCompletedWithWrappedCancellationException(g); |
1572 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1573 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1574 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1575 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1576 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1577 |
> |
|
1578 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1579 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1580 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1581 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1582 |
|
checkCancelled(f); |
1583 |
< |
r.assertNotInvoked(); |
1583 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1584 |
|
}} |
1585 |
|
|
1586 |
|
/** |
1588 |
|
*/ |
1589 |
|
public void testThenAccept_actionFailed() { |
1590 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1315 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1591 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1592 |
|
{ |
1593 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1594 |
< |
final FailingConsumer r = new FailingConsumer(m); |
1595 |
< |
if (!createIncomplete) f.complete(v1); |
1321 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1322 |
< |
if (createIncomplete) { |
1323 |
< |
checkIncomplete(g); |
1324 |
< |
f.complete(v1); |
1325 |
< |
} |
1594 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1595 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1596 |
|
|
1597 |
< |
checkCompletedWithWrappedCFException(g); |
1597 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1598 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1599 |
> |
assertTrue(f.complete(v1)); |
1600 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1601 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1602 |
> |
|
1603 |
> |
checkCompletedWithWrappedCFException(h0); |
1604 |
> |
checkCompletedWithWrappedCFException(h1); |
1605 |
> |
checkCompletedWithWrappedCFException(h2); |
1606 |
> |
checkCompletedWithWrappedCFException(h3); |
1607 |
|
checkCompletedNormally(f, v1); |
1608 |
|
}} |
1609 |
|
|
1613 |
|
*/ |
1614 |
|
public void testThenCombine_normalCompletion() { |
1615 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1337 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1616 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1617 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1618 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1619 |
|
{ |
1620 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1621 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1622 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1622 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1623 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1624 |
|
|
1625 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1626 |
< |
if (!createIncomplete) |
1627 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1628 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1629 |
< |
if (createIncomplete) { |
1630 |
< |
checkIncomplete(h); |
1631 |
< |
r.assertNotInvoked(); |
1632 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1633 |
< |
} |
1625 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1626 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1627 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1628 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1629 |
> |
|
1630 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1631 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1632 |
> |
assertTrue(fst.complete(w1)); |
1633 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1634 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1635 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1636 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1637 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1638 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1639 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1640 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1641 |
> |
assertTrue(snd.complete(w2)); |
1642 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1643 |
> |
|
1644 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1645 |
> |
checkCompletedNormally(h2, subtract(v1, v2)); |
1646 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1647 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1648 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1649 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1650 |
|
|
1356 |
– |
checkCompletedNormally(h, subtract(v1, v2)); |
1651 |
|
checkCompletedNormally(f, v1); |
1652 |
|
checkCompletedNormally(g, v2); |
1359 |
– |
r.assertValue(subtract(v1, v2)); |
1653 |
|
}} |
1654 |
|
|
1655 |
|
/** |
1656 |
|
* thenCombine result completes exceptionally after exceptional |
1657 |
|
* completion of either source |
1658 |
|
*/ |
1659 |
< |
public void testThenCombine_exceptionalCompletion() { |
1659 |
> |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
1660 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1368 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1661 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1662 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1663 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1664 |
|
{ |
1665 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1666 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1667 |
|
final CFException ex = new CFException(); |
1668 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1669 |
< |
|
1670 |
< |
(fFirst ? f : g).complete(v1); |
1671 |
< |
if (!createIncomplete) |
1672 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1673 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1674 |
< |
if (createIncomplete) { |
1675 |
< |
checkIncomplete(h); |
1676 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1677 |
< |
} |
1678 |
< |
|
1679 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1680 |
< |
r.assertNotInvoked(); |
1681 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1682 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1668 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1669 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1670 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1671 |
> |
|
1672 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1673 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1674 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1675 |
> |
() -> fst.completeExceptionally(ex) : |
1676 |
> |
() -> fst.complete(v1); |
1677 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1678 |
> |
() -> snd.complete(v1) : |
1679 |
> |
() -> snd.completeExceptionally(ex); |
1680 |
> |
|
1681 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1682 |
> |
assertTrue(complete1.call()); |
1683 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1684 |
> |
checkIncomplete(h1); |
1685 |
> |
checkIncomplete(h2); |
1686 |
> |
assertTrue(complete2.call()); |
1687 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1688 |
> |
|
1689 |
> |
checkCompletedWithWrappedException(h1, ex); |
1690 |
> |
checkCompletedWithWrappedException(h2, ex); |
1691 |
> |
checkCompletedWithWrappedException(h3, ex); |
1692 |
> |
r1.assertNotInvoked(); |
1693 |
> |
r2.assertNotInvoked(); |
1694 |
> |
r3.assertNotInvoked(); |
1695 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1696 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1697 |
|
}} |
1698 |
|
|
1699 |
|
/** |
1700 |
|
* thenCombine result completes exceptionally if either source cancelled |
1701 |
|
*/ |
1702 |
< |
public void testThenCombine_sourceCancelled() { |
1702 |
> |
public void testThenCombine_sourceCancelled() throws Throwable { |
1703 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1704 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1398 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1705 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1706 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1707 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1708 |
|
{ |
1709 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1710 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1711 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1711 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1712 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1713 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1714 |
|
|
1715 |
< |
(fFirst ? f : g).complete(v1); |
1716 |
< |
if (!createIncomplete) |
1717 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1718 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1719 |
< |
if (createIncomplete) { |
1720 |
< |
checkIncomplete(h); |
1721 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1722 |
< |
} |
1715 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1716 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1717 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1718 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1719 |
> |
() -> fst.complete(v1); |
1720 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1721 |
> |
() -> snd.complete(v1) : |
1722 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1723 |
|
|
1724 |
< |
checkCompletedWithWrappedCancellationException(h); |
1725 |
< |
checkCancelled(!fFirst ? f : g); |
1726 |
< |
r.assertNotInvoked(); |
1727 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1724 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1725 |
> |
assertTrue(complete1.call()); |
1726 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1727 |
> |
checkIncomplete(h1); |
1728 |
> |
checkIncomplete(h2); |
1729 |
> |
assertTrue(complete2.call()); |
1730 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1731 |
> |
|
1732 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1733 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1734 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1735 |
> |
r1.assertNotInvoked(); |
1736 |
> |
r2.assertNotInvoked(); |
1737 |
> |
r3.assertNotInvoked(); |
1738 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1739 |
> |
checkCancelled(failFirst ? fst : snd); |
1740 |
|
}} |
1741 |
|
|
1742 |
|
/** |
1750 |
|
{ |
1751 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1752 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1753 |
< |
final FailingBiFunction r = new FailingBiFunction(m); |
1754 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1753 |
> |
final FailingBiFunction r1 = new FailingBiFunction(m); |
1754 |
> |
final FailingBiFunction r2 = new FailingBiFunction(m); |
1755 |
> |
final FailingBiFunction r3 = new FailingBiFunction(m); |
1756 |
> |
|
1757 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1758 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1759 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1760 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1761 |
> |
|
1762 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1763 |
> |
assertTrue(fst.complete(w1)); |
1764 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1765 |
> |
assertTrue(snd.complete(w2)); |
1766 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1767 |
|
|
1768 |
< |
if (fFirst) { |
1769 |
< |
f.complete(v1); |
1770 |
< |
g.complete(v2); |
1771 |
< |
} else { |
1772 |
< |
g.complete(v2); |
1773 |
< |
f.complete(v1); |
1441 |
< |
} |
1442 |
< |
|
1443 |
< |
checkCompletedWithWrappedCFException(h); |
1768 |
> |
checkCompletedWithWrappedCFException(h1); |
1769 |
> |
checkCompletedWithWrappedCFException(h2); |
1770 |
> |
checkCompletedWithWrappedCFException(h3); |
1771 |
> |
r1.assertInvoked(); |
1772 |
> |
r2.assertInvoked(); |
1773 |
> |
r3.assertInvoked(); |
1774 |
|
checkCompletedNormally(f, v1); |
1775 |
|
checkCompletedNormally(g, v2); |
1776 |
|
}} |
1781 |
|
*/ |
1782 |
|
public void testThenAcceptBoth_normalCompletion() { |
1783 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1454 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1784 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1785 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1786 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1787 |
|
{ |
1788 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1789 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1790 |
< |
final SubtractAction r = new SubtractAction(m); |
1791 |
< |
|
1792 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1793 |
< |
if (!createIncomplete) |
1794 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1795 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1796 |
< |
if (createIncomplete) { |
1797 |
< |
checkIncomplete(h); |
1798 |
< |
r.assertNotInvoked(); |
1799 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1800 |
< |
} |
1790 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1791 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1792 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1793 |
> |
|
1794 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1795 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1796 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1797 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1798 |
> |
|
1799 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1800 |
> |
assertTrue(fst.complete(w1)); |
1801 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1802 |
> |
checkIncomplete(h1); |
1803 |
> |
checkIncomplete(h2); |
1804 |
> |
r1.assertNotInvoked(); |
1805 |
> |
r2.assertNotInvoked(); |
1806 |
> |
assertTrue(snd.complete(w2)); |
1807 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1808 |
|
|
1809 |
< |
checkCompletedNormally(h, null); |
1810 |
< |
r.assertValue(subtract(v1, v2)); |
1809 |
> |
checkCompletedNormally(h1, null); |
1810 |
> |
checkCompletedNormally(h2, null); |
1811 |
> |
checkCompletedNormally(h3, null); |
1812 |
> |
r1.assertValue(subtract(v1, v2)); |
1813 |
> |
r2.assertValue(subtract(v1, v2)); |
1814 |
> |
r3.assertValue(subtract(v1, v2)); |
1815 |
|
checkCompletedNormally(f, v1); |
1816 |
|
checkCompletedNormally(g, v2); |
1817 |
|
}} |
1820 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1821 |
|
* completion of either source |
1822 |
|
*/ |
1823 |
< |
public void testThenAcceptBoth_exceptionalCompletion() { |
1823 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1824 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1485 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1825 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1826 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1827 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1828 |
|
{ |
1829 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1830 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1831 |
|
final CFException ex = new CFException(); |
1832 |
< |
final SubtractAction r = new SubtractAction(m); |
1833 |
< |
|
1834 |
< |
(fFirst ? f : g).complete(v1); |
1835 |
< |
if (!createIncomplete) |
1836 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1837 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1838 |
< |
if (createIncomplete) { |
1839 |
< |
checkIncomplete(h); |
1840 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1841 |
< |
} |
1842 |
< |
|
1843 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1844 |
< |
r.assertNotInvoked(); |
1845 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1846 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1832 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1833 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1834 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1835 |
> |
|
1836 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1837 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1838 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1839 |
> |
() -> fst.completeExceptionally(ex) : |
1840 |
> |
() -> fst.complete(v1); |
1841 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1842 |
> |
() -> snd.complete(v1) : |
1843 |
> |
() -> snd.completeExceptionally(ex); |
1844 |
> |
|
1845 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1846 |
> |
assertTrue(complete1.call()); |
1847 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1848 |
> |
checkIncomplete(h1); |
1849 |
> |
checkIncomplete(h2); |
1850 |
> |
assertTrue(complete2.call()); |
1851 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1852 |
> |
|
1853 |
> |
checkCompletedWithWrappedException(h1, ex); |
1854 |
> |
checkCompletedWithWrappedException(h2, ex); |
1855 |
> |
checkCompletedWithWrappedException(h3, ex); |
1856 |
> |
r1.assertNotInvoked(); |
1857 |
> |
r2.assertNotInvoked(); |
1858 |
> |
r3.assertNotInvoked(); |
1859 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1860 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1861 |
|
}} |
1862 |
|
|
1863 |
|
/** |
1864 |
|
* thenAcceptBoth result completes exceptionally if either source cancelled |
1865 |
|
*/ |
1866 |
< |
public void testThenAcceptBoth_sourceCancelled() { |
1866 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1867 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1868 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1515 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1869 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1870 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1871 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1872 |
|
{ |
1873 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1874 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1875 |
< |
final SubtractAction r = new SubtractAction(m); |
1875 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1876 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1877 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1878 |
|
|
1879 |
< |
(fFirst ? f : g).complete(v1); |
1880 |
< |
if (!createIncomplete) |
1881 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1882 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1883 |
< |
if (createIncomplete) { |
1884 |
< |
checkIncomplete(h); |
1885 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1886 |
< |
} |
1879 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1880 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1881 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1882 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1883 |
> |
() -> fst.complete(v1); |
1884 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1885 |
> |
() -> snd.complete(v1) : |
1886 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1887 |
|
|
1888 |
< |
checkCompletedWithWrappedCancellationException(h); |
1889 |
< |
checkCancelled(!fFirst ? f : g); |
1890 |
< |
r.assertNotInvoked(); |
1891 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1888 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1889 |
> |
assertTrue(complete1.call()); |
1890 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1891 |
> |
checkIncomplete(h1); |
1892 |
> |
checkIncomplete(h2); |
1893 |
> |
assertTrue(complete2.call()); |
1894 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1895 |
> |
|
1896 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1897 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1898 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1899 |
> |
r1.assertNotInvoked(); |
1900 |
> |
r2.assertNotInvoked(); |
1901 |
> |
r3.assertNotInvoked(); |
1902 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1903 |
> |
checkCancelled(failFirst ? fst : snd); |
1904 |
|
}} |
1905 |
|
|
1906 |
|
/** |
1914 |
|
{ |
1915 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1916 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1917 |
< |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1918 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1917 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1918 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1919 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1920 |
> |
|
1921 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1922 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1923 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1924 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1925 |
> |
|
1926 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1927 |
> |
assertTrue(fst.complete(w1)); |
1928 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1929 |
> |
assertTrue(snd.complete(w2)); |
1930 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1931 |
|
|
1932 |
< |
if (fFirst) { |
1933 |
< |
f.complete(v1); |
1934 |
< |
g.complete(v2); |
1935 |
< |
} else { |
1936 |
< |
g.complete(v2); |
1937 |
< |
f.complete(v1); |
1558 |
< |
} |
1559 |
< |
|
1560 |
< |
checkCompletedWithWrappedCFException(h); |
1932 |
> |
checkCompletedWithWrappedCFException(h1); |
1933 |
> |
checkCompletedWithWrappedCFException(h2); |
1934 |
> |
checkCompletedWithWrappedCFException(h3); |
1935 |
> |
r1.assertInvoked(); |
1936 |
> |
r2.assertInvoked(); |
1937 |
> |
r3.assertInvoked(); |
1938 |
|
checkCompletedNormally(f, v1); |
1939 |
|
checkCompletedNormally(g, v2); |
1940 |
|
}} |
1945 |
|
*/ |
1946 |
|
public void testRunAfterBoth_normalCompletion() { |
1947 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1571 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1948 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1949 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1950 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1951 |
|
{ |
1952 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1953 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1954 |
< |
final Noop r = new Noop(m); |
1955 |
< |
|
1956 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1957 |
< |
if (!createIncomplete) |
1958 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1959 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1960 |
< |
if (createIncomplete) { |
1961 |
< |
checkIncomplete(h); |
1962 |
< |
r.assertNotInvoked(); |
1963 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1964 |
< |
} |
1954 |
> |
final Noop r1 = new Noop(m); |
1955 |
> |
final Noop r2 = new Noop(m); |
1956 |
> |
final Noop r3 = new Noop(m); |
1957 |
> |
|
1958 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1959 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1960 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1961 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1962 |
> |
|
1963 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1964 |
> |
assertTrue(fst.complete(w1)); |
1965 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1966 |
> |
checkIncomplete(h1); |
1967 |
> |
checkIncomplete(h2); |
1968 |
> |
r1.assertNotInvoked(); |
1969 |
> |
r2.assertNotInvoked(); |
1970 |
> |
assertTrue(snd.complete(w2)); |
1971 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1972 |
|
|
1973 |
< |
checkCompletedNormally(h, null); |
1974 |
< |
r.assertInvoked(); |
1973 |
> |
checkCompletedNormally(h1, null); |
1974 |
> |
checkCompletedNormally(h2, null); |
1975 |
> |
checkCompletedNormally(h3, null); |
1976 |
> |
r1.assertInvoked(); |
1977 |
> |
r2.assertInvoked(); |
1978 |
> |
r3.assertInvoked(); |
1979 |
|
checkCompletedNormally(f, v1); |
1980 |
|
checkCompletedNormally(g, v2); |
1981 |
|
}} |
1984 |
|
* runAfterBoth result completes exceptionally after exceptional |
1985 |
|
* completion of either source |
1986 |
|
*/ |
1987 |
< |
public void testRunAfterBoth_exceptionalCompletion() { |
1987 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
1988 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1602 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1989 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1990 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1991 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1992 |
|
{ |
1993 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1994 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1995 |
|
final CFException ex = new CFException(); |
1996 |
< |
final Noop r = new Noop(m); |
1997 |
< |
|
1998 |
< |
(fFirst ? f : g).complete(v1); |
1999 |
< |
if (!createIncomplete) |
2000 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
2001 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2002 |
< |
if (createIncomplete) { |
2003 |
< |
checkIncomplete(h); |
2004 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
2005 |
< |
} |
2006 |
< |
|
2007 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2008 |
< |
r.assertNotInvoked(); |
2009 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2010 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1996 |
> |
final Noop r1 = new Noop(m); |
1997 |
> |
final Noop r2 = new Noop(m); |
1998 |
> |
final Noop r3 = new Noop(m); |
1999 |
> |
|
2000 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2001 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2002 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2003 |
> |
() -> fst.completeExceptionally(ex) : |
2004 |
> |
() -> fst.complete(v1); |
2005 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2006 |
> |
() -> snd.complete(v1) : |
2007 |
> |
() -> snd.completeExceptionally(ex); |
2008 |
> |
|
2009 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2010 |
> |
assertTrue(complete1.call()); |
2011 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2012 |
> |
checkIncomplete(h1); |
2013 |
> |
checkIncomplete(h2); |
2014 |
> |
assertTrue(complete2.call()); |
2015 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2016 |
> |
|
2017 |
> |
checkCompletedWithWrappedException(h1, ex); |
2018 |
> |
checkCompletedWithWrappedException(h2, ex); |
2019 |
> |
checkCompletedWithWrappedException(h3, ex); |
2020 |
> |
r1.assertNotInvoked(); |
2021 |
> |
r2.assertNotInvoked(); |
2022 |
> |
r3.assertNotInvoked(); |
2023 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2024 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
2025 |
|
}} |
2026 |
|
|
2027 |
|
/** |
2028 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
2029 |
|
*/ |
2030 |
< |
public void testRunAfterBoth_sourceCancelled() { |
2030 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
2031 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2032 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1632 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2033 |
|
for (boolean fFirst : new boolean[] { true, false }) |
2034 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
2035 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2036 |
|
{ |
2037 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2038 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2039 |
< |
final Noop r = new Noop(m); |
2039 |
> |
final Noop r1 = new Noop(m); |
2040 |
> |
final Noop r2 = new Noop(m); |
2041 |
> |
final Noop r3 = new Noop(m); |
2042 |
|
|
2043 |
+ |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2044 |
+ |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2045 |
+ |
final Callable<Boolean> complete1 = failFirst ? |
2046 |
+ |
() -> fst.cancel(mayInterruptIfRunning) : |
2047 |
+ |
() -> fst.complete(v1); |
2048 |
+ |
final Callable<Boolean> complete2 = failFirst ? |
2049 |
+ |
() -> snd.complete(v1) : |
2050 |
+ |
() -> snd.cancel(mayInterruptIfRunning); |
2051 |
|
|
2052 |
< |
(fFirst ? f : g).complete(v1); |
2053 |
< |
if (!createIncomplete) |
2054 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2055 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2056 |
< |
if (createIncomplete) { |
2057 |
< |
checkIncomplete(h); |
2058 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1648 |
< |
} |
2052 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2053 |
> |
assertTrue(complete1.call()); |
2054 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2055 |
> |
checkIncomplete(h1); |
2056 |
> |
checkIncomplete(h2); |
2057 |
> |
assertTrue(complete2.call()); |
2058 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2059 |
|
|
2060 |
< |
checkCompletedWithWrappedCancellationException(h); |
2061 |
< |
checkCancelled(!fFirst ? f : g); |
2062 |
< |
r.assertNotInvoked(); |
2063 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2060 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2061 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2062 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2063 |
> |
r1.assertNotInvoked(); |
2064 |
> |
r2.assertNotInvoked(); |
2065 |
> |
r3.assertNotInvoked(); |
2066 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2067 |
> |
checkCancelled(failFirst ? fst : snd); |
2068 |
|
}} |
2069 |
|
|
2070 |
|
/** |
2080 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2081 |
|
final FailingRunnable r1 = new FailingRunnable(m); |
2082 |
|
final FailingRunnable r2 = new FailingRunnable(m); |
2083 |
+ |
final FailingRunnable r3 = new FailingRunnable(m); |
2084 |
|
|
2085 |
< |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2086 |
< |
if (fFirst) { |
2087 |
< |
f.complete(v1); |
2088 |
< |
g.complete(v2); |
2089 |
< |
} else { |
2090 |
< |
g.complete(v2); |
2091 |
< |
f.complete(v1); |
2092 |
< |
} |
2093 |
< |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2085 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2086 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2087 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2088 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2089 |
> |
|
2090 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2091 |
> |
assertTrue(fst.complete(w1)); |
2092 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2093 |
> |
assertTrue(snd.complete(w2)); |
2094 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2095 |
|
|
2096 |
|
checkCompletedWithWrappedCFException(h1); |
2097 |
|
checkCompletedWithWrappedCFException(h2); |
2098 |
+ |
checkCompletedWithWrappedCFException(h3); |
2099 |
+ |
r1.assertInvoked(); |
2100 |
+ |
r2.assertInvoked(); |
2101 |
+ |
r3.assertInvoked(); |
2102 |
|
checkCompletedNormally(f, v1); |
2103 |
|
checkCompletedNormally(g, v2); |
2104 |
|
}} |
2172 |
|
rs[0].assertNotInvoked(); |
2173 |
|
rs[1].assertNotInvoked(); |
2174 |
|
f.completeExceptionally(ex); |
2175 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2176 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2175 |
> |
checkCompletedWithWrappedException(h0, ex); |
2176 |
> |
checkCompletedWithWrappedException(h1, ex); |
2177 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2178 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2179 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2180 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2179 |
> |
checkCompletedWithWrappedException(h2, ex); |
2180 |
> |
checkCompletedWithWrappedException(h3, ex); |
2181 |
|
g.complete(v1); |
2182 |
|
|
2183 |
|
// unspecified behavior - both source completions available |
2187 |
|
assertEquals(inc(v1), h4.join()); |
2188 |
|
rs[4].assertValue(inc(v1)); |
2189 |
|
} catch (CompletionException ok) { |
2190 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2190 |
> |
checkCompletedWithWrappedException(h4, ex); |
2191 |
|
rs[4].assertNotInvoked(); |
2192 |
|
} |
2193 |
|
try { |
2194 |
|
assertEquals(inc(v1), h5.join()); |
2195 |
|
rs[5].assertValue(inc(v1)); |
2196 |
|
} catch (CompletionException ok) { |
2197 |
< |
checkCompletedWithWrappedCFException(h5, ex); |
2197 |
> |
checkCompletedWithWrappedException(h5, ex); |
2198 |
|
rs[5].assertNotInvoked(); |
2199 |
|
} |
2200 |
|
|
2201 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2201 |
> |
checkCompletedExceptionally(f, ex); |
2202 |
|
checkCompletedNormally(g, v1); |
2203 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2204 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2205 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2206 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2207 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2203 |
> |
checkCompletedWithWrappedException(h0, ex); |
2204 |
> |
checkCompletedWithWrappedException(h1, ex); |
2205 |
> |
checkCompletedWithWrappedException(h2, ex); |
2206 |
> |
checkCompletedWithWrappedException(h3, ex); |
2207 |
> |
checkCompletedWithWrappedException(h4, ex); |
2208 |
|
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2209 |
|
}} |
2210 |
|
|
2221 |
|
|
2222 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2223 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2224 |
< |
if (fFirst) { |
2225 |
< |
f.complete(v1); |
1806 |
< |
g.completeExceptionally(ex); |
1807 |
< |
} else { |
1808 |
< |
g.completeExceptionally(ex); |
1809 |
< |
f.complete(v1); |
1810 |
< |
} |
2224 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2225 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2226 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2227 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2228 |
|
|
2231 |
|
assertEquals(inc(v1), h0.join()); |
2232 |
|
rs[0].assertValue(inc(v1)); |
2233 |
|
} catch (CompletionException ok) { |
2234 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2234 |
> |
checkCompletedWithWrappedException(h0, ex); |
2235 |
|
rs[0].assertNotInvoked(); |
2236 |
|
} |
2237 |
|
try { |
2238 |
|
assertEquals(inc(v1), h1.join()); |
2239 |
|
rs[1].assertValue(inc(v1)); |
2240 |
|
} catch (CompletionException ok) { |
2241 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2241 |
> |
checkCompletedWithWrappedException(h1, ex); |
2242 |
|
rs[1].assertNotInvoked(); |
2243 |
|
} |
2244 |
|
try { |
2245 |
|
assertEquals(inc(v1), h2.join()); |
2246 |
|
rs[2].assertValue(inc(v1)); |
2247 |
|
} catch (CompletionException ok) { |
2248 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2248 |
> |
checkCompletedWithWrappedException(h2, ex); |
2249 |
|
rs[2].assertNotInvoked(); |
2250 |
|
} |
2251 |
|
try { |
2252 |
|
assertEquals(inc(v1), h3.join()); |
2253 |
|
rs[3].assertValue(inc(v1)); |
2254 |
|
} catch (CompletionException ok) { |
2255 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2255 |
> |
checkCompletedWithWrappedException(h3, ex); |
2256 |
|
rs[3].assertNotInvoked(); |
2257 |
|
} |
2258 |
|
|
2259 |
|
checkCompletedNormally(f, v1); |
2260 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2260 |
> |
checkCompletedExceptionally(g, ex); |
2261 |
|
}} |
2262 |
|
|
2263 |
|
/** |
2328 |
|
|
2329 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2330 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2331 |
< |
if (fFirst) { |
2332 |
< |
f.complete(v1); |
1918 |
< |
g.cancel(mayInterruptIfRunning); |
1919 |
< |
} else { |
1920 |
< |
g.cancel(mayInterruptIfRunning); |
1921 |
< |
f.complete(v1); |
1922 |
< |
} |
2331 |
> |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2332 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2333 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2334 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2335 |
|
|
2481 |
|
rs[0].assertNotInvoked(); |
2482 |
|
rs[1].assertNotInvoked(); |
2483 |
|
f.completeExceptionally(ex); |
2484 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2485 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2484 |
> |
checkCompletedWithWrappedException(h0, ex); |
2485 |
> |
checkCompletedWithWrappedException(h1, ex); |
2486 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2487 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2488 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2489 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2488 |
> |
checkCompletedWithWrappedException(h2, ex); |
2489 |
> |
checkCompletedWithWrappedException(h3, ex); |
2490 |
|
|
2491 |
|
g.complete(v1); |
2492 |
|
|
2497 |
|
assertNull(h4.join()); |
2498 |
|
rs[4].assertValue(v1); |
2499 |
|
} catch (CompletionException ok) { |
2500 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2500 |
> |
checkCompletedWithWrappedException(h4, ex); |
2501 |
|
rs[4].assertNotInvoked(); |
2502 |
|
} |
2503 |
|
try { |
2504 |
|
assertNull(h5.join()); |
2505 |
|
rs[5].assertValue(v1); |
2506 |
|
} catch (CompletionException ok) { |
2507 |
< |
checkCompletedWithWrappedCFException(h5, ex); |
2507 |
> |
checkCompletedWithWrappedException(h5, ex); |
2508 |
|
rs[5].assertNotInvoked(); |
2509 |
|
} |
2510 |
|
|
2511 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2511 |
> |
checkCompletedExceptionally(f, ex); |
2512 |
|
checkCompletedNormally(g, v1); |
2513 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2514 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2515 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2516 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2517 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2513 |
> |
checkCompletedWithWrappedException(h0, ex); |
2514 |
> |
checkCompletedWithWrappedException(h1, ex); |
2515 |
> |
checkCompletedWithWrappedException(h2, ex); |
2516 |
> |
checkCompletedWithWrappedException(h3, ex); |
2517 |
> |
checkCompletedWithWrappedException(h4, ex); |
2518 |
|
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2519 |
|
}} |
2520 |
|
|
2531 |
|
|
2532 |
|
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2533 |
|
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2534 |
< |
if (fFirst) { |
2535 |
< |
f.complete(v1); |
2126 |
< |
g.completeExceptionally(ex); |
2127 |
< |
} else { |
2128 |
< |
g.completeExceptionally(ex); |
2129 |
< |
f.complete(v1); |
2130 |
< |
} |
2534 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2535 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2536 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2537 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2538 |
|
|
2541 |
|
assertEquals(null, h0.join()); |
2542 |
|
rs[0].assertValue(v1); |
2543 |
|
} catch (CompletionException ok) { |
2544 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2544 |
> |
checkCompletedWithWrappedException(h0, ex); |
2545 |
|
rs[0].assertNotInvoked(); |
2546 |
|
} |
2547 |
|
try { |
2548 |
|
assertEquals(null, h1.join()); |
2549 |
|
rs[1].assertValue(v1); |
2550 |
|
} catch (CompletionException ok) { |
2551 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2551 |
> |
checkCompletedWithWrappedException(h1, ex); |
2552 |
|
rs[1].assertNotInvoked(); |
2553 |
|
} |
2554 |
|
try { |
2555 |
|
assertEquals(null, h2.join()); |
2556 |
|
rs[2].assertValue(v1); |
2557 |
|
} catch (CompletionException ok) { |
2558 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2558 |
> |
checkCompletedWithWrappedException(h2, ex); |
2559 |
|
rs[2].assertNotInvoked(); |
2560 |
|
} |
2561 |
|
try { |
2562 |
|
assertEquals(null, h3.join()); |
2563 |
|
rs[3].assertValue(v1); |
2564 |
|
} catch (CompletionException ok) { |
2565 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2565 |
> |
checkCompletedWithWrappedException(h3, ex); |
2566 |
|
rs[3].assertNotInvoked(); |
2567 |
|
} |
2568 |
|
|
2569 |
|
checkCompletedNormally(f, v1); |
2570 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2570 |
> |
checkCompletedExceptionally(g, ex); |
2571 |
|
}} |
2572 |
|
|
2573 |
|
/** |
2735 |
|
checkIncomplete(h1); |
2736 |
|
rs[0].assertNotInvoked(); |
2737 |
|
rs[1].assertNotInvoked(); |
2738 |
< |
f.completeExceptionally(ex); |
2739 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2740 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2738 |
> |
assertTrue(f.completeExceptionally(ex)); |
2739 |
> |
checkCompletedWithWrappedException(h0, ex); |
2740 |
> |
checkCompletedWithWrappedException(h1, ex); |
2741 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2742 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2743 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2744 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2743 |
> |
checkCompletedWithWrappedException(h2, ex); |
2744 |
> |
checkCompletedWithWrappedException(h3, ex); |
2745 |
|
|
2746 |
< |
g.complete(v1); |
2746 |
> |
assertTrue(g.complete(v1)); |
2747 |
|
|
2748 |
|
// unspecified behavior - both source completions available |
2749 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2752 |
|
assertNull(h4.join()); |
2753 |
|
rs[4].assertInvoked(); |
2754 |
|
} catch (CompletionException ok) { |
2755 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2755 |
> |
checkCompletedWithWrappedException(h4, ex); |
2756 |
|
rs[4].assertNotInvoked(); |
2757 |
|
} |
2758 |
|
try { |
2759 |
|
assertNull(h5.join()); |
2760 |
|
rs[5].assertInvoked(); |
2761 |
|
} catch (CompletionException ok) { |
2762 |
< |
checkCompletedWithWrappedCFException(h5, ex); |
2762 |
> |
checkCompletedWithWrappedException(h5, ex); |
2763 |
|
rs[5].assertNotInvoked(); |
2764 |
|
} |
2765 |
|
|
2766 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2766 |
> |
checkCompletedExceptionally(f, ex); |
2767 |
|
checkCompletedNormally(g, v1); |
2768 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2769 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2770 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2771 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2772 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2768 |
> |
checkCompletedWithWrappedException(h0, ex); |
2769 |
> |
checkCompletedWithWrappedException(h1, ex); |
2770 |
> |
checkCompletedWithWrappedException(h2, ex); |
2771 |
> |
checkCompletedWithWrappedException(h3, ex); |
2772 |
> |
checkCompletedWithWrappedException(h4, ex); |
2773 |
|
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2774 |
|
}} |
2775 |
|
|
2786 |
|
|
2787 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2788 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2789 |
< |
if (fFirst) { |
2790 |
< |
f.complete(v1); |
2386 |
< |
g.completeExceptionally(ex); |
2387 |
< |
} else { |
2388 |
< |
g.completeExceptionally(ex); |
2389 |
< |
f.complete(v1); |
2390 |
< |
} |
2789 |
> |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2790 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2791 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2792 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2793 |
|
|
2796 |
|
assertEquals(null, h0.join()); |
2797 |
|
rs[0].assertInvoked(); |
2798 |
|
} catch (CompletionException ok) { |
2799 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2799 |
> |
checkCompletedWithWrappedException(h0, ex); |
2800 |
|
rs[0].assertNotInvoked(); |
2801 |
|
} |
2802 |
|
try { |
2803 |
|
assertEquals(null, h1.join()); |
2804 |
|
rs[1].assertInvoked(); |
2805 |
|
} catch (CompletionException ok) { |
2806 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2806 |
> |
checkCompletedWithWrappedException(h1, ex); |
2807 |
|
rs[1].assertNotInvoked(); |
2808 |
|
} |
2809 |
|
try { |
2810 |
|
assertEquals(null, h2.join()); |
2811 |
|
rs[2].assertInvoked(); |
2812 |
|
} catch (CompletionException ok) { |
2813 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2813 |
> |
checkCompletedWithWrappedException(h2, ex); |
2814 |
|
rs[2].assertNotInvoked(); |
2815 |
|
} |
2816 |
|
try { |
2817 |
|
assertEquals(null, h3.join()); |
2818 |
|
rs[3].assertInvoked(); |
2819 |
|
} catch (CompletionException ok) { |
2820 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2820 |
> |
checkCompletedWithWrappedException(h3, ex); |
2821 |
|
rs[3].assertNotInvoked(); |
2822 |
|
} |
2823 |
|
|
2824 |
|
checkCompletedNormally(f, v1); |
2825 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2825 |
> |
checkCompletedExceptionally(g, ex); |
2826 |
|
}} |
2827 |
|
|
2828 |
|
/** |
2852 |
|
checkCompletedWithWrappedCancellationException(h2); |
2853 |
|
checkCompletedWithWrappedCancellationException(h3); |
2854 |
|
|
2855 |
< |
g.complete(v1); |
2855 |
> |
assertTrue(g.complete(v1)); |
2856 |
|
|
2857 |
|
// unspecified behavior - both source completions available |
2858 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2896 |
|
|
2897 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2898 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2899 |
< |
f.complete(v1); |
2899 |
> |
assertTrue(f.complete(v1)); |
2900 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2901 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2902 |
|
checkCompletedWithWrappedCFException(h0); |
2904 |
|
checkCompletedWithWrappedCFException(h2); |
2905 |
|
checkCompletedWithWrappedCFException(h3); |
2906 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2907 |
< |
g.complete(v2); |
2907 |
> |
assertTrue(g.complete(v2)); |
2908 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2909 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2910 |
|
checkCompletedWithWrappedCFException(h4); |
2925 |
|
{ |
2926 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2927 |
|
final CompletableFutureInc r = new CompletableFutureInc(m); |
2928 |
< |
if (!createIncomplete) f.complete(v1); |
2928 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2929 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2930 |
< |
if (createIncomplete) f.complete(v1); |
2930 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2931 |
|
|
2932 |
|
checkCompletedNormally(g, inc(v1)); |
2933 |
|
checkCompletedNormally(f, v1); |
2949 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2950 |
|
if (createIncomplete) f.completeExceptionally(ex); |
2951 |
|
|
2952 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2953 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2952 |
> |
checkCompletedWithWrappedException(g, ex); |
2953 |
> |
checkCompletedExceptionally(f, ex); |
2954 |
|
r.assertNotInvoked(); |
2955 |
|
}} |
2956 |
|
|
2965 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2966 |
|
final FailingCompletableFutureFunction r |
2967 |
|
= new FailingCompletableFutureFunction(m); |
2968 |
< |
if (!createIncomplete) f.complete(v1); |
2968 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2969 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2970 |
< |
if (createIncomplete) f.complete(v1); |
2970 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2971 |
|
|
2972 |
|
checkCompletedWithWrappedCFException(g); |
2973 |
|
checkCompletedNormally(f, v1); |
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 |
|
/** |
3062 |
|
* when all components complete normally |
3063 |
|
*/ |
3064 |
|
public void testAllOf_normal() throws Exception { |
3065 |
< |
for (int k = 1; k < 20; ++k) { |
3065 |
> |
for (int k = 1; k < 10; k++) { |
3066 |
|
CompletableFuture<Integer>[] fs |
3067 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3068 |
< |
for (int i = 0; i < k; ++i) |
3068 |
> |
for (int i = 0; i < k; i++) |
3069 |
|
fs[i] = new CompletableFuture<>(); |
3070 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3071 |
< |
for (int i = 0; i < k; ++i) { |
3071 |
> |
for (int i = 0; i < k; i++) { |
3072 |
|
checkIncomplete(f); |
3073 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3074 |
|
fs[i].complete(one); |
3079 |
|
} |
3080 |
|
|
3081 |
|
public void testAllOf_backwards() throws Exception { |
3082 |
< |
for (int k = 1; k < 20; ++k) { |
3082 |
> |
for (int k = 1; k < 10; k++) { |
3083 |
|
CompletableFuture<Integer>[] fs |
3084 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3085 |
< |
for (int i = 0; i < k; ++i) |
3085 |
> |
for (int i = 0; i < k; i++) |
3086 |
|
fs[i] = new CompletableFuture<>(); |
3087 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3088 |
|
for (int i = k - 1; i >= 0; i--) { |
3095 |
|
} |
3096 |
|
} |
3097 |
|
|
3098 |
+ |
public void testAllOf_exceptional() throws Exception { |
3099 |
+ |
for (int k = 1; k < 10; k++) { |
3100 |
+ |
CompletableFuture<Integer>[] fs |
3101 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3102 |
+ |
CFException ex = new CFException(); |
3103 |
+ |
for (int i = 0; i < k; i++) |
3104 |
+ |
fs[i] = new CompletableFuture<>(); |
3105 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3106 |
+ |
for (int i = 0; i < k; i++) { |
3107 |
+ |
checkIncomplete(f); |
3108 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3109 |
+ |
if (i != k/2) { |
3110 |
+ |
fs[i].complete(i); |
3111 |
+ |
checkCompletedNormally(fs[i], i); |
3112 |
+ |
} else { |
3113 |
+ |
fs[i].completeExceptionally(ex); |
3114 |
+ |
checkCompletedExceptionally(fs[i], ex); |
3115 |
+ |
} |
3116 |
+ |
} |
3117 |
+ |
checkCompletedWithWrappedException(f, ex); |
3118 |
+ |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3119 |
+ |
} |
3120 |
+ |
} |
3121 |
+ |
|
3122 |
|
/** |
3123 |
|
* anyOf(no component futures) returns an incomplete future |
3124 |
|
*/ |
3125 |
|
public void testAnyOf_empty() throws Exception { |
3126 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3127 |
+ |
{ |
3128 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3129 |
|
checkIncomplete(f); |
3130 |
< |
} |
3130 |
> |
|
3131 |
> |
f.complete(v1); |
3132 |
> |
checkCompletedNormally(f, v1); |
3133 |
> |
}} |
3134 |
|
|
3135 |
|
/** |
3136 |
|
* anyOf returns a future completed normally with a value when |
3137 |
|
* a component future does |
3138 |
|
*/ |
3139 |
|
public void testAnyOf_normal() throws Exception { |
3140 |
< |
for (int k = 0; k < 10; ++k) { |
3140 |
> |
for (int k = 0; k < 10; k++) { |
3141 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3142 |
< |
for (int i = 0; i < k; ++i) |
3142 |
> |
for (int i = 0; i < k; i++) |
3143 |
|
fs[i] = new CompletableFuture<>(); |
3144 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3145 |
|
checkIncomplete(f); |
3146 |
< |
for (int i = 0; i < k; ++i) { |
3147 |
< |
fs[i].complete(one); |
3148 |
< |
checkCompletedNormally(f, one); |
3149 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
3146 |
> |
for (int i = 0; i < k; i++) { |
3147 |
> |
fs[i].complete(i); |
3148 |
> |
checkCompletedNormally(f, 0); |
3149 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3150 |
> |
assertTrue(0 <= x && x <= i); |
3151 |
> |
} |
3152 |
> |
} |
3153 |
> |
} |
3154 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3155 |
> |
for (int k = 0; k < 10; k++) { |
3156 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3157 |
> |
for (int i = 0; i < k; i++) |
3158 |
> |
fs[i] = new CompletableFuture<>(); |
3159 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3160 |
> |
checkIncomplete(f); |
3161 |
> |
for (int i = k - 1; i >= 0; i--) { |
3162 |
> |
fs[i].complete(i); |
3163 |
> |
checkCompletedNormally(f, k - 1); |
3164 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3165 |
> |
assertTrue(i <= x && x <= k - 1); |
3166 |
|
} |
3167 |
|
} |
3168 |
|
} |
3171 |
|
* anyOf result completes exceptionally when any component does. |
3172 |
|
*/ |
3173 |
|
public void testAnyOf_exceptional() throws Exception { |
3174 |
< |
for (int k = 0; k < 10; ++k) { |
3174 |
> |
for (int k = 0; k < 10; k++) { |
3175 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3176 |
> |
CFException[] exs = new CFException[k]; |
3177 |
> |
for (int i = 0; i < k; i++) { |
3178 |
> |
fs[i] = new CompletableFuture<>(); |
3179 |
> |
exs[i] = new CFException(); |
3180 |
> |
} |
3181 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3182 |
> |
checkIncomplete(f); |
3183 |
> |
for (int i = 0; i < k; i++) { |
3184 |
> |
fs[i].completeExceptionally(exs[i]); |
3185 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3186 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3187 |
> |
} |
3188 |
> |
} |
3189 |
> |
} |
3190 |
> |
|
3191 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3192 |
> |
for (int k = 0; k < 10; k++) { |
3193 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3194 |
< |
for (int i = 0; i < k; ++i) |
3194 |
> |
CFException[] exs = new CFException[k]; |
3195 |
> |
for (int i = 0; i < k; i++) { |
3196 |
|
fs[i] = new CompletableFuture<>(); |
3197 |
+ |
exs[i] = new CFException(); |
3198 |
+ |
} |
3199 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3200 |
|
checkIncomplete(f); |
3201 |
< |
for (int i = 0; i < k; ++i) { |
3202 |
< |
fs[i].completeExceptionally(new CFException()); |
3203 |
< |
checkCompletedWithWrappedCFException(f); |
3201 |
> |
for (int i = k - 1; i >= 0; i--) { |
3202 |
> |
fs[i].completeExceptionally(exs[i]); |
3203 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3204 |
|
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3205 |
|
} |
3206 |
|
} |
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), |
3324 |
|
assertSame(f, f.toCompletableFuture()); |
3325 |
|
} |
3326 |
|
|
3327 |
< |
/** |
2810 |
< |
* whenComplete action executes on normal completion, propagating |
2811 |
< |
* source result. |
2812 |
< |
*/ |
2813 |
< |
public void testWhenComplete_normalCompletion1() { |
2814 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2815 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2816 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2817 |
< |
{ |
2818 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2819 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2820 |
< |
if (!createIncomplete) f.complete(v1); |
2821 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2822 |
< |
(f, |
2823 |
< |
(Integer x, Throwable t) -> { |
2824 |
< |
threadAssertSame(x, v1); |
2825 |
< |
threadAssertNull(t); |
2826 |
< |
a.getAndIncrement(); |
2827 |
< |
}); |
2828 |
< |
if (createIncomplete) f.complete(v1); |
3327 |
> |
//--- tests of implementation details; not part of official tck --- |
3328 |
|
|
3329 |
< |
checkCompletedNormally(g, v1); |
3330 |
< |
checkCompletedNormally(f, v1); |
3331 |
< |
assertEquals(1, a.get()); |
3332 |
< |
}} |
3329 |
> |
Object resultOf(CompletableFuture<?> f) { |
3330 |
> |
try { |
3331 |
> |
java.lang.reflect.Field resultField |
3332 |
> |
= CompletableFuture.class.getDeclaredField("result"); |
3333 |
> |
resultField.setAccessible(true); |
3334 |
> |
return resultField.get(f); |
3335 |
> |
} catch (Throwable t) { throw new AssertionError(t); } |
3336 |
> |
} |
3337 |
|
|
3338 |
< |
/** |
3339 |
< |
* whenComplete action executes on exceptional completion, propagating |
2837 |
< |
* source result. |
2838 |
< |
*/ |
2839 |
< |
public void testWhenComplete_exceptionalCompletion() { |
3338 |
> |
public void testExceptionPropagationReusesResultObject() { |
3339 |
> |
if (!testImplementationDetails) return; |
3340 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2841 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2842 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
3341 |
|
{ |
2844 |
– |
final AtomicInteger a = new AtomicInteger(0); |
3342 |
|
final CFException ex = new CFException(); |
3343 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3344 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
2848 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2849 |
< |
(f, |
2850 |
< |
(Integer x, Throwable t) -> { |
2851 |
< |
threadAssertNull(x); |
2852 |
< |
threadAssertSame(t, ex); |
2853 |
< |
a.getAndIncrement(); |
2854 |
< |
}); |
2855 |
< |
if (createIncomplete) f.completeExceptionally(ex); |
2856 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2857 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2858 |
< |
assertEquals(1, a.get()); |
2859 |
< |
}} |
3343 |
> |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3344 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3345 |
|
|
3346 |
< |
/** |
3347 |
< |
* whenComplete action executes on cancelled source, propagating |
2863 |
< |
* CancellationException. |
2864 |
< |
*/ |
2865 |
< |
public void testWhenComplete_sourceCancelled() { |
2866 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2867 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2868 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2869 |
< |
{ |
2870 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2871 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2872 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2873 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2874 |
< |
(f, |
2875 |
< |
(Integer x, Throwable t) -> { |
2876 |
< |
threadAssertNull(x); |
2877 |
< |
threadAssertTrue(t instanceof CancellationException); |
2878 |
< |
a.getAndIncrement(); |
2879 |
< |
}); |
2880 |
< |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
3346 |
> |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3347 |
> |
= new ArrayList<>(); |
3348 |
|
|
3349 |
< |
//try { g.join(); } catch (Throwable t) { throw new Error(t); } |
3350 |
< |
checkCompletedWithWrappedCancellationException(g); |
3351 |
< |
checkCancelled(f); |
2885 |
< |
assertEquals(1, a.get()); |
2886 |
< |
}} |
3349 |
> |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3350 |
> |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3351 |
> |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3352 |
|
|
3353 |
< |
/** |
3354 |
< |
* If a whenComplete action throws an exception when triggered by |
3355 |
< |
* a normal completion, it completes exceptionally |
2891 |
< |
*/ |
2892 |
< |
public void testWhenComplete_actionFailed() { |
2893 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2894 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2895 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2896 |
< |
{ |
2897 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2898 |
< |
final CFException ex = new CFException(); |
2899 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2900 |
< |
if (!createIncomplete) f.complete(v1); |
2901 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2902 |
< |
(f, |
2903 |
< |
(Integer x, Throwable t) -> { |
2904 |
< |
threadAssertSame(x, v1); |
2905 |
< |
threadAssertNull(t); |
2906 |
< |
a.getAndIncrement(); |
2907 |
< |
throw ex; |
2908 |
< |
}); |
2909 |
< |
if (createIncomplete) f.complete(v1); |
2910 |
< |
checkCompletedNormally(f, v1); |
2911 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2912 |
< |
assertEquals(1, a.get()); |
2913 |
< |
}} |
3353 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3354 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3355 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3356 |
|
|
3357 |
< |
/** |
3358 |
< |
* If a whenComplete action throws an exception when triggered by |
3359 |
< |
* a source completion that also throws an exception, the source |
2918 |
< |
* exception takes precedence. |
2919 |
< |
*/ |
2920 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
2921 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2922 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2923 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2924 |
< |
{ |
2925 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2926 |
< |
final CFException ex1 = new CFException(); |
2927 |
< |
final CFException ex2 = new CFException(); |
2928 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3357 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3358 |
> |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3359 |
> |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3360 |
|
|
3361 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
2931 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2932 |
< |
(f, |
2933 |
< |
(Integer x, Throwable t) -> { |
2934 |
< |
threadAssertSame(t, ex1); |
2935 |
< |
threadAssertNull(x); |
2936 |
< |
a.getAndIncrement(); |
2937 |
< |
throw ex2; |
2938 |
< |
}); |
2939 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
3361 |
> |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3362 |
|
|
3363 |
< |
checkCompletedWithWrappedCFException(f, ex1); |
3364 |
< |
checkCompletedWithWrappedCFException(g, ex1); |
3365 |
< |
assertEquals(1, a.get()); |
3363 |
> |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3364 |
> |
|
3365 |
> |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3366 |
> |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3367 |
> |
|
3368 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3369 |
> |
fun : funs) { |
3370 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3371 |
> |
f.completeExceptionally(ex); |
3372 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3373 |
> |
checkCompletedWithWrappedException(src, ex); |
3374 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3375 |
> |
checkCompletedWithWrappedException(dep, ex); |
3376 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3377 |
> |
} |
3378 |
> |
|
3379 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3380 |
> |
fun : funs) { |
3381 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3382 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3383 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3384 |
> |
f.completeExceptionally(ex); |
3385 |
> |
checkCompletedWithWrappedException(src, ex); |
3386 |
> |
checkCompletedWithWrappedException(dep, ex); |
3387 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3388 |
> |
} |
3389 |
> |
|
3390 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3391 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3392 |
> |
fun : funs) { |
3393 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3394 |
> |
f.cancel(mayInterruptIfRunning); |
3395 |
> |
checkCancelled(f); |
3396 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3397 |
> |
checkCompletedWithWrappedCancellationException(src); |
3398 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3399 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3400 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3401 |
> |
} |
3402 |
> |
|
3403 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3404 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3405 |
> |
fun : funs) { |
3406 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3407 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3408 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3409 |
> |
f.cancel(mayInterruptIfRunning); |
3410 |
> |
checkCancelled(f); |
3411 |
> |
checkCompletedWithWrappedCancellationException(src); |
3412 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3413 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3414 |
> |
} |
3415 |
|
}} |
3416 |
|
|
3417 |
|
} |