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 |
> |
import static java.util.concurrent.CompletableFuture.completedFuture; |
11 |
> |
import static java.util.concurrent.CompletableFuture.failedFuture; |
12 |
> |
|
13 |
> |
import java.lang.reflect.Method; |
14 |
> |
import java.lang.reflect.Modifier; |
15 |
> |
|
16 |
> |
import java.util.stream.Collectors; |
17 |
> |
import java.util.stream.Stream; |
18 |
> |
|
19 |
> |
import java.util.ArrayList; |
20 |
> |
import java.util.Arrays; |
21 |
> |
import java.util.List; |
22 |
> |
import java.util.Objects; |
23 |
> |
import java.util.Set; |
24 |
|
import java.util.concurrent.Callable; |
10 |
– |
import java.util.concurrent.Executor; |
11 |
– |
import java.util.concurrent.ExecutorService; |
12 |
– |
import java.util.concurrent.Executors; |
25 |
|
import java.util.concurrent.CancellationException; |
14 |
– |
import java.util.concurrent.CountDownLatch; |
15 |
– |
import java.util.concurrent.ExecutionException; |
16 |
– |
import java.util.concurrent.Future; |
26 |
|
import java.util.concurrent.CompletableFuture; |
27 |
|
import java.util.concurrent.CompletionException; |
28 |
|
import java.util.concurrent.CompletionStage; |
29 |
+ |
import java.util.concurrent.ExecutionException; |
30 |
+ |
import java.util.concurrent.Executor; |
31 |
|
import java.util.concurrent.ForkJoinPool; |
32 |
|
import java.util.concurrent.ForkJoinTask; |
33 |
|
import java.util.concurrent.TimeoutException; |
34 |
+ |
import java.util.concurrent.TimeUnit; |
35 |
|
import java.util.concurrent.atomic.AtomicInteger; |
36 |
< |
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; |
36 |
> |
import java.util.concurrent.atomic.AtomicReference; |
37 |
|
import java.util.function.BiConsumer; |
30 |
– |
import java.util.function.Function; |
38 |
|
import java.util.function.BiFunction; |
39 |
+ |
import java.util.function.Consumer; |
40 |
+ |
import java.util.function.Function; |
41 |
+ |
import java.util.function.Predicate; |
42 |
+ |
import java.util.function.Supplier; |
43 |
+ |
|
44 |
+ |
import junit.framework.AssertionFailedError; |
45 |
+ |
import junit.framework.Test; |
46 |
+ |
import junit.framework.TestSuite; |
47 |
|
|
48 |
|
public class CompletableFutureTest extends JSR166TestCase { |
49 |
|
|
50 |
|
public static void main(String[] args) { |
51 |
< |
junit.textui.TestRunner.run(suite()); |
51 |
> |
main(suite(), args); |
52 |
|
} |
53 |
|
public static Test suite() { |
54 |
|
return new TestSuite(CompletableFutureTest.class); |
59 |
|
void checkIncomplete(CompletableFuture<?> f) { |
60 |
|
assertFalse(f.isDone()); |
61 |
|
assertFalse(f.isCancelled()); |
62 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
62 |
> |
assertTrue(f.toString().contains("Not completed")); |
63 |
|
try { |
64 |
|
assertNull(f.getNow(null)); |
65 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
72 |
|
} |
73 |
|
|
74 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
75 |
< |
try { |
76 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
62 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
75 |
> |
checkTimedGet(f, value); |
76 |
> |
|
77 |
|
try { |
78 |
|
assertEquals(value, f.join()); |
79 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
89 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
90 |
|
} |
91 |
|
|
92 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
93 |
< |
try { |
94 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
95 |
< |
shouldThrow(); |
96 |
< |
} catch (ExecutionException success) { |
97 |
< |
assertTrue(success.getCause() instanceof CFException); |
98 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
99 |
< |
try { |
100 |
< |
f.join(); |
101 |
< |
shouldThrow(); |
102 |
< |
} catch (CompletionException success) { |
103 |
< |
assertTrue(success.getCause() instanceof CFException); |
104 |
< |
} |
105 |
< |
try { |
106 |
< |
f.getNow(null); |
107 |
< |
shouldThrow(); |
108 |
< |
} catch (CompletionException success) { |
95 |
< |
assertTrue(success.getCause() instanceof CFException); |
92 |
> |
/** |
93 |
> |
* Returns the "raw" internal exceptional completion of f, |
94 |
> |
* without any additional wrapping with CompletionException. |
95 |
> |
*/ |
96 |
> |
<U> Throwable exceptionalCompletion(CompletableFuture<U> f) { |
97 |
> |
// handle (and whenComplete) can distinguish between "direct" |
98 |
> |
// and "wrapped" exceptional completion |
99 |
> |
return f.handle((U u, Throwable t) -> t).join(); |
100 |
> |
} |
101 |
> |
|
102 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, |
103 |
> |
boolean wrapped, |
104 |
> |
Consumer<Throwable> checker) { |
105 |
> |
Throwable cause = exceptionalCompletion(f); |
106 |
> |
if (wrapped) { |
107 |
> |
assertTrue(cause instanceof CompletionException); |
108 |
> |
cause = cause.getCause(); |
109 |
|
} |
110 |
< |
try { |
98 |
< |
f.get(); |
99 |
< |
shouldThrow(); |
100 |
< |
} catch (ExecutionException success) { |
101 |
< |
assertTrue(success.getCause() instanceof CFException); |
102 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
103 |
< |
assertTrue(f.isDone()); |
104 |
< |
assertFalse(f.isCancelled()); |
105 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
106 |
< |
} |
110 |
> |
checker.accept(cause); |
111 |
|
|
112 |
< |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
109 |
< |
Throwable ex) { |
112 |
> |
long startTime = System.nanoTime(); |
113 |
|
try { |
114 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
115 |
|
shouldThrow(); |
116 |
|
} catch (ExecutionException success) { |
117 |
< |
assertSame(ex, success.getCause()); |
117 |
> |
assertSame(cause, success.getCause()); |
118 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
119 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
120 |
+ |
|
121 |
|
try { |
122 |
|
f.join(); |
123 |
|
shouldThrow(); |
124 |
|
} catch (CompletionException success) { |
125 |
< |
assertSame(ex, success.getCause()); |
126 |
< |
} |
125 |
> |
assertSame(cause, success.getCause()); |
126 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
127 |
> |
|
128 |
|
try { |
129 |
|
f.getNow(null); |
130 |
|
shouldThrow(); |
131 |
|
} catch (CompletionException success) { |
132 |
< |
assertSame(ex, success.getCause()); |
133 |
< |
} |
132 |
> |
assertSame(cause, success.getCause()); |
133 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
> |
|
135 |
|
try { |
136 |
|
f.get(); |
137 |
|
shouldThrow(); |
138 |
|
} catch (ExecutionException success) { |
139 |
< |
assertSame(ex, success.getCause()); |
139 |
> |
assertSame(cause, success.getCause()); |
140 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
141 |
|
|
135 |
– |
assertTrue(f.isDone()); |
142 |
|
assertFalse(f.isCancelled()); |
143 |
+ |
assertTrue(f.isDone()); |
144 |
+ |
assertTrue(f.isCompletedExceptionally()); |
145 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
146 |
|
} |
147 |
|
|
148 |
< |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
149 |
< |
Throwable ex) { |
150 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
143 |
< |
try { |
144 |
< |
CompletableFuture<Throwable> spy = f.handle |
145 |
< |
((U u, Throwable t) -> t); |
146 |
< |
assertTrue(spy.join() instanceof CompletionException); |
147 |
< |
assertSame(ex, spy.join().getCause()); |
148 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
148 |
> |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
149 |
> |
checkCompletedExceptionally(f, true, |
150 |
> |
(t) -> assertTrue(t instanceof CFException)); |
151 |
|
} |
152 |
|
|
153 |
< |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
154 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
155 |
< |
try { |
156 |
< |
CompletableFuture<Throwable> spy = f.handle |
157 |
< |
((U u, Throwable t) -> t); |
158 |
< |
assertSame(ex, spy.join()); |
159 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
153 |
> |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
154 |
> |
checkCompletedExceptionally(f, true, |
155 |
> |
(t) -> assertTrue(t instanceof CancellationException)); |
156 |
> |
} |
157 |
> |
|
158 |
> |
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
159 |
> |
checkCompletedExceptionally(f, false, |
160 |
> |
(t) -> assertTrue(t instanceof TimeoutException)); |
161 |
> |
} |
162 |
> |
|
163 |
> |
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
164 |
> |
Throwable ex) { |
165 |
> |
checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex)); |
166 |
> |
} |
167 |
> |
|
168 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
169 |
> |
checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex)); |
170 |
|
} |
171 |
|
|
172 |
|
void checkCancelled(CompletableFuture<?> f) { |
173 |
+ |
long startTime = System.nanoTime(); |
174 |
|
try { |
175 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
176 |
|
shouldThrow(); |
177 |
|
} catch (CancellationException success) { |
178 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
179 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
180 |
+ |
|
181 |
|
try { |
182 |
|
f.join(); |
183 |
|
shouldThrow(); |
191 |
|
shouldThrow(); |
192 |
|
} catch (CancellationException success) { |
193 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
179 |
– |
assertTrue(f.isDone()); |
180 |
– |
assertTrue(f.isCompletedExceptionally()); |
181 |
– |
assertTrue(f.isCancelled()); |
182 |
– |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
183 |
– |
} |
194 |
|
|
195 |
< |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
196 |
< |
try { |
187 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
188 |
< |
shouldThrow(); |
189 |
< |
} catch (ExecutionException success) { |
190 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
191 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
192 |
< |
try { |
193 |
< |
f.join(); |
194 |
< |
shouldThrow(); |
195 |
< |
} catch (CompletionException success) { |
196 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
197 |
< |
} |
198 |
< |
try { |
199 |
< |
f.getNow(null); |
200 |
< |
shouldThrow(); |
201 |
< |
} catch (CompletionException success) { |
202 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
203 |
< |
} |
204 |
< |
try { |
205 |
< |
f.get(); |
206 |
< |
shouldThrow(); |
207 |
< |
} catch (ExecutionException success) { |
208 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
209 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
195 |
> |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
196 |
> |
|
197 |
|
assertTrue(f.isDone()); |
211 |
– |
assertFalse(f.isCancelled()); |
198 |
|
assertTrue(f.isCompletedExceptionally()); |
199 |
+ |
assertTrue(f.isCancelled()); |
200 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
201 |
|
} |
202 |
|
|
214 |
|
* isCancelled, join, get, and getNow |
215 |
|
*/ |
216 |
|
public void testComplete() { |
217 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
218 |
+ |
{ |
219 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
220 |
|
checkIncomplete(f); |
221 |
< |
f.complete(one); |
222 |
< |
checkCompletedNormally(f, one); |
223 |
< |
} |
221 |
> |
assertTrue(f.complete(v1)); |
222 |
> |
assertFalse(f.complete(v1)); |
223 |
> |
checkCompletedNormally(f, v1); |
224 |
> |
}} |
225 |
|
|
226 |
|
/** |
227 |
|
* completeExceptionally completes exceptionally, as indicated by |
240 |
|
* methods isDone, isCancelled, join, get, and getNow |
241 |
|
*/ |
242 |
|
public void testCancel() { |
243 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
244 |
+ |
{ |
245 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
246 |
|
checkIncomplete(f); |
247 |
< |
assertTrue(f.cancel(true)); |
247 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
248 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
249 |
> |
assertTrue(f.cancel(!mayInterruptIfRunning)); |
250 |
|
checkCancelled(f); |
251 |
< |
} |
251 |
> |
}} |
252 |
|
|
253 |
|
/** |
254 |
|
* obtrudeValue forces completion with given value |
256 |
|
public void testObtrudeValue() { |
257 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
258 |
|
checkIncomplete(f); |
259 |
< |
f.complete(one); |
259 |
> |
assertTrue(f.complete(one)); |
260 |
|
checkCompletedNormally(f, one); |
261 |
|
f.obtrudeValue(three); |
262 |
|
checkCompletedNormally(f, three); |
283 |
|
CompletableFuture<Integer> f; |
284 |
|
|
285 |
|
f = new CompletableFuture<>(); |
286 |
< |
f.complete(v1); |
286 |
> |
assertTrue(f.complete(v1)); |
287 |
|
for (int i = 0; i < 2; i++) { |
288 |
|
f.obtrudeException(ex = new CFException()); |
289 |
|
checkCompletedExceptionally(f, ex); |
303 |
|
checkCompletedExceptionally(f, ex); |
304 |
|
f.completeExceptionally(new CFException()); |
305 |
|
checkCompletedExceptionally(f, ex); |
306 |
< |
f.complete(v1); |
306 |
> |
assertFalse(f.complete(v1)); |
307 |
|
checkCompletedExceptionally(f, ex); |
308 |
|
}} |
309 |
|
|
312 |
|
*/ |
313 |
|
public void testGetNumberOfDependents() { |
314 |
|
for (ExecutionMode m : ExecutionMode.values()) |
315 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
316 |
|
{ |
317 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
318 |
|
assertEquals(0, f.getNumberOfDependents()); |
322 |
|
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
323 |
|
assertEquals(2, f.getNumberOfDependents()); |
324 |
|
assertEquals(0, h.getNumberOfDependents()); |
325 |
< |
f.complete(1); |
325 |
> |
assertTrue(f.complete(v1)); |
326 |
|
checkCompletedNormally(g, null); |
327 |
|
checkCompletedNormally(h, null); |
328 |
|
assertEquals(0, f.getNumberOfDependents()); |
339 |
|
f = new CompletableFuture<String>(); |
340 |
|
assertTrue(f.toString().contains("[Not completed]")); |
341 |
|
|
342 |
< |
f.complete("foo"); |
342 |
> |
assertTrue(f.complete("foo")); |
343 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
344 |
|
|
345 |
|
f = new CompletableFuture<String>(); |
346 |
< |
f.completeExceptionally(new IndexOutOfBoundsException()); |
352 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
353 |
< |
|
354 |
< |
f = new CompletableFuture<String>(); |
355 |
< |
f.cancel(true); |
346 |
> |
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
347 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
348 |
|
|
349 |
< |
f = new CompletableFuture<String>(); |
350 |
< |
f.cancel(false); |
351 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
349 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
350 |
> |
f = new CompletableFuture<String>(); |
351 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
352 |
> |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
353 |
> |
} |
354 |
|
} |
355 |
|
|
356 |
|
/** |
518 |
|
} |
519 |
|
} |
520 |
|
|
528 |
– |
|
521 |
|
class CompletableFutureInc extends CheckedIntegerAction |
522 |
|
implements Function<Integer, CompletableFuture<Integer>> |
523 |
|
{ |
526 |
|
invoked(); |
527 |
|
value = x; |
528 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
529 |
< |
f.complete(inc(x)); |
529 |
> |
assertTrue(f.complete(inc(x))); |
530 |
|
return f; |
531 |
|
} |
532 |
|
} |
556 |
|
} |
557 |
|
} |
558 |
|
|
559 |
+ |
static final boolean defaultExecutorIsCommonPool |
560 |
+ |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
561 |
+ |
|
562 |
|
/** |
563 |
|
* Permits the testing of parallel code for the 3 different |
564 |
|
* execution modes without copy/pasting all the test methods. |
565 |
|
*/ |
566 |
|
enum ExecutionMode { |
567 |
< |
DEFAULT { |
567 |
> |
SYNC { |
568 |
|
public void checkExecutionMode() { |
569 |
|
assertFalse(ThreadExecutor.startedCurrentThread()); |
570 |
|
assertNull(ForkJoinTask.getPool()); |
640 |
|
|
641 |
|
ASYNC { |
642 |
|
public void checkExecutionMode() { |
643 |
< |
assertSame(ForkJoinPool.commonPool(), |
644 |
< |
ForkJoinTask.getPool()); |
643 |
> |
assertEquals(defaultExecutorIsCommonPool, |
644 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
645 |
|
} |
646 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
647 |
|
return CompletableFuture.runAsync(a); |
837 |
|
{ |
838 |
|
final AtomicInteger a = new AtomicInteger(0); |
839 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
840 |
< |
if (!createIncomplete) f.complete(v1); |
840 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
841 |
|
final CompletableFuture<Integer> g = f.exceptionally |
842 |
|
((Throwable t) -> { |
848 |
– |
// Should not be called |
843 |
|
a.getAndIncrement(); |
844 |
< |
throw new AssertionError(); |
844 |
> |
threadFail("should not be called"); |
845 |
> |
return null; // unreached |
846 |
|
}); |
847 |
< |
if (createIncomplete) f.complete(v1); |
847 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
848 |
|
|
849 |
|
checkCompletedNormally(g, v1); |
850 |
|
checkCompletedNormally(f, v1); |
865 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
866 |
|
final CompletableFuture<Integer> g = f.exceptionally |
867 |
|
((Throwable t) -> { |
868 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
868 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
869 |
|
threadAssertSame(t, ex); |
870 |
|
a.getAndIncrement(); |
871 |
|
return v1; |
876 |
|
assertEquals(1, a.get()); |
877 |
|
}} |
878 |
|
|
879 |
+ |
/** |
880 |
+ |
* If an "exceptionally action" throws an exception, it completes |
881 |
+ |
* exceptionally with that exception |
882 |
+ |
*/ |
883 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
884 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
886 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
885 |
|
{ |
886 |
|
final AtomicInteger a = new AtomicInteger(0); |
887 |
|
final CFException ex1 = new CFException(); |
890 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
891 |
|
final CompletableFuture<Integer> g = f.exceptionally |
892 |
|
((Throwable t) -> { |
893 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
893 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
894 |
|
threadAssertSame(t, ex1); |
895 |
|
a.getAndIncrement(); |
896 |
|
throw ex2; |
898 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
899 |
|
|
900 |
|
checkCompletedWithWrappedException(g, ex2); |
901 |
+ |
checkCompletedExceptionally(f, ex1); |
902 |
|
assertEquals(1, a.get()); |
903 |
|
}} |
904 |
|
|
906 |
|
* whenComplete action executes on normal completion, propagating |
907 |
|
* source result. |
908 |
|
*/ |
909 |
< |
public void testWhenComplete_normalCompletion1() { |
909 |
> |
public void testWhenComplete_normalCompletion() { |
910 |
|
for (ExecutionMode m : ExecutionMode.values()) |
911 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
912 |
|
for (Integer v1 : new Integer[] { 1, null }) |
913 |
|
{ |
914 |
|
final AtomicInteger a = new AtomicInteger(0); |
915 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
916 |
< |
if (!createIncomplete) f.complete(v1); |
916 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
917 |
|
final CompletableFuture<Integer> g = m.whenComplete |
918 |
|
(f, |
919 |
< |
(Integer x, Throwable t) -> { |
919 |
> |
(Integer result, Throwable t) -> { |
920 |
|
m.checkExecutionMode(); |
921 |
< |
threadAssertSame(x, v1); |
921 |
> |
threadAssertSame(result, v1); |
922 |
|
threadAssertNull(t); |
923 |
|
a.getAndIncrement(); |
924 |
|
}); |
925 |
< |
if (createIncomplete) f.complete(v1); |
925 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
926 |
|
|
927 |
|
checkCompletedNormally(g, v1); |
928 |
|
checkCompletedNormally(f, v1); |
936 |
|
public void testWhenComplete_exceptionalCompletion() { |
937 |
|
for (ExecutionMode m : ExecutionMode.values()) |
938 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
940 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
939 |
|
{ |
940 |
|
final AtomicInteger a = new AtomicInteger(0); |
941 |
|
final CFException ex = new CFException(); |
943 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
944 |
|
final CompletableFuture<Integer> g = m.whenComplete |
945 |
|
(f, |
946 |
< |
(Integer x, Throwable t) -> { |
946 |
> |
(Integer result, Throwable t) -> { |
947 |
|
m.checkExecutionMode(); |
948 |
< |
threadAssertNull(x); |
948 |
> |
threadAssertNull(result); |
949 |
|
threadAssertSame(t, ex); |
950 |
|
a.getAndIncrement(); |
951 |
|
}); |
970 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
971 |
|
final CompletableFuture<Integer> g = m.whenComplete |
972 |
|
(f, |
973 |
< |
(Integer x, Throwable t) -> { |
973 |
> |
(Integer result, Throwable t) -> { |
974 |
|
m.checkExecutionMode(); |
975 |
< |
threadAssertNull(x); |
975 |
> |
threadAssertNull(result); |
976 |
|
threadAssertTrue(t instanceof CancellationException); |
977 |
|
a.getAndIncrement(); |
978 |
|
}); |
987 |
|
* If a whenComplete action throws an exception when triggered by |
988 |
|
* a normal completion, it completes exceptionally |
989 |
|
*/ |
990 |
< |
public void testWhenComplete_actionFailed() { |
990 |
> |
public void testWhenComplete_sourceCompletedNormallyActionFailed() { |
991 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
992 |
|
for (ExecutionMode m : ExecutionMode.values()) |
993 |
|
for (Integer v1 : new Integer[] { 1, null }) |
995 |
|
final AtomicInteger a = new AtomicInteger(0); |
996 |
|
final CFException ex = new CFException(); |
997 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
998 |
< |
if (!createIncomplete) f.complete(v1); |
998 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
999 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1000 |
|
(f, |
1001 |
< |
(Integer x, Throwable t) -> { |
1001 |
> |
(Integer result, Throwable t) -> { |
1002 |
|
m.checkExecutionMode(); |
1003 |
< |
threadAssertSame(x, v1); |
1003 |
> |
threadAssertSame(result, v1); |
1004 |
|
threadAssertNull(t); |
1005 |
|
a.getAndIncrement(); |
1006 |
|
throw ex; |
1007 |
|
}); |
1008 |
< |
if (createIncomplete) f.complete(v1); |
1008 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1009 |
|
|
1010 |
|
checkCompletedWithWrappedException(g, ex); |
1011 |
|
checkCompletedNormally(f, v1); |
1015 |
|
/** |
1016 |
|
* If a whenComplete action throws an exception when triggered by |
1017 |
|
* a source completion that also throws an exception, the source |
1018 |
< |
* exception takes precedence. |
1018 |
> |
* exception takes precedence (unlike handle) |
1019 |
|
*/ |
1020 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
1020 |
> |
public void testWhenComplete_sourceFailedActionFailed() { |
1021 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1022 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1025 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1023 |
|
{ |
1024 |
|
final AtomicInteger a = new AtomicInteger(0); |
1025 |
|
final CFException ex1 = new CFException(); |
1029 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
1030 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1031 |
|
(f, |
1032 |
< |
(Integer x, Throwable t) -> { |
1032 |
> |
(Integer result, Throwable t) -> { |
1033 |
|
m.checkExecutionMode(); |
1034 |
|
threadAssertSame(t, ex1); |
1035 |
< |
threadAssertNull(x); |
1035 |
> |
threadAssertNull(result); |
1036 |
|
a.getAndIncrement(); |
1037 |
|
throw ex2; |
1038 |
|
}); |
1054 |
|
{ |
1055 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1056 |
|
final AtomicInteger a = new AtomicInteger(0); |
1057 |
< |
if (!createIncomplete) f.complete(v1); |
1057 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1058 |
|
final CompletableFuture<Integer> g = m.handle |
1059 |
|
(f, |
1060 |
< |
(Integer x, Throwable t) -> { |
1060 |
> |
(Integer result, Throwable t) -> { |
1061 |
|
m.checkExecutionMode(); |
1062 |
< |
threadAssertSame(x, v1); |
1062 |
> |
threadAssertSame(result, v1); |
1063 |
|
threadAssertNull(t); |
1064 |
|
a.getAndIncrement(); |
1065 |
|
return inc(v1); |
1066 |
|
}); |
1067 |
< |
if (createIncomplete) f.complete(v1); |
1067 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1068 |
|
|
1069 |
|
checkCompletedNormally(g, inc(v1)); |
1070 |
|
checkCompletedNormally(f, v1); |
1086 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1087 |
|
final CompletableFuture<Integer> g = m.handle |
1088 |
|
(f, |
1089 |
< |
(Integer x, Throwable t) -> { |
1089 |
> |
(Integer result, Throwable t) -> { |
1090 |
|
m.checkExecutionMode(); |
1091 |
< |
threadAssertNull(x); |
1091 |
> |
threadAssertNull(result); |
1092 |
|
threadAssertSame(t, ex); |
1093 |
|
a.getAndIncrement(); |
1094 |
|
return v1; |
1115 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1116 |
|
final CompletableFuture<Integer> g = m.handle |
1117 |
|
(f, |
1118 |
< |
(Integer x, Throwable t) -> { |
1118 |
> |
(Integer result, Throwable t) -> { |
1119 |
|
m.checkExecutionMode(); |
1120 |
< |
threadAssertNull(x); |
1120 |
> |
threadAssertNull(result); |
1121 |
|
threadAssertTrue(t instanceof CancellationException); |
1122 |
|
a.getAndIncrement(); |
1123 |
|
return v1; |
1130 |
|
}} |
1131 |
|
|
1132 |
|
/** |
1133 |
< |
* handle result completes exceptionally if action does |
1133 |
> |
* If a "handle action" throws an exception when triggered by |
1134 |
> |
* a normal completion, it completes exceptionally |
1135 |
|
*/ |
1136 |
< |
public void testHandle_sourceFailedActionFailed() { |
1136 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1137 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1138 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1139 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1140 |
|
{ |
1141 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1142 |
|
final AtomicInteger a = new AtomicInteger(0); |
1143 |
< |
final CFException ex1 = new CFException(); |
1144 |
< |
final CFException ex2 = new CFException(); |
1146 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
1143 |
> |
final CFException ex = new CFException(); |
1144 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1145 |
|
final CompletableFuture<Integer> g = m.handle |
1146 |
|
(f, |
1147 |
< |
(Integer x, Throwable t) -> { |
1147 |
> |
(Integer result, Throwable t) -> { |
1148 |
|
m.checkExecutionMode(); |
1149 |
< |
threadAssertNull(x); |
1150 |
< |
threadAssertSame(ex1, t); |
1149 |
> |
threadAssertSame(result, v1); |
1150 |
> |
threadAssertNull(t); |
1151 |
|
a.getAndIncrement(); |
1152 |
< |
throw ex2; |
1152 |
> |
throw ex; |
1153 |
|
}); |
1154 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
1154 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1155 |
|
|
1156 |
< |
checkCompletedWithWrappedException(g, ex2); |
1157 |
< |
checkCompletedExceptionally(f, ex1); |
1156 |
> |
checkCompletedWithWrappedException(g, ex); |
1157 |
> |
checkCompletedNormally(f, v1); |
1158 |
|
assertEquals(1, a.get()); |
1159 |
|
}} |
1160 |
|
|
1161 |
< |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1162 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1161 |
> |
/** |
1162 |
> |
* If a "handle action" throws an exception when triggered by |
1163 |
> |
* a source completion that also throws an exception, the action |
1164 |
> |
* exception takes precedence (unlike whenComplete) |
1165 |
> |
*/ |
1166 |
> |
public void testHandle_sourceFailedActionFailed() { |
1167 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1168 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1168 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1169 |
|
{ |
1168 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1170 |
|
final AtomicInteger a = new AtomicInteger(0); |
1171 |
< |
final CFException ex = new CFException(); |
1172 |
< |
if (!createIncomplete) f.complete(v1); |
1171 |
> |
final CFException ex1 = new CFException(); |
1172 |
> |
final CFException ex2 = new CFException(); |
1173 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1174 |
> |
|
1175 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1176 |
|
final CompletableFuture<Integer> g = m.handle |
1177 |
|
(f, |
1178 |
< |
(Integer x, Throwable t) -> { |
1178 |
> |
(Integer result, Throwable t) -> { |
1179 |
|
m.checkExecutionMode(); |
1180 |
< |
threadAssertSame(x, v1); |
1181 |
< |
threadAssertNull(t); |
1180 |
> |
threadAssertNull(result); |
1181 |
> |
threadAssertSame(ex1, t); |
1182 |
|
a.getAndIncrement(); |
1183 |
< |
throw ex; |
1183 |
> |
throw ex2; |
1184 |
|
}); |
1185 |
< |
if (createIncomplete) f.complete(v1); |
1185 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1186 |
|
|
1187 |
< |
checkCompletedWithWrappedException(g, ex); |
1188 |
< |
checkCompletedNormally(f, v1); |
1187 |
> |
checkCompletedWithWrappedException(g, ex2); |
1188 |
> |
checkCompletedExceptionally(f, ex1); |
1189 |
|
assertEquals(1, a.get()); |
1190 |
|
}} |
1191 |
|
|
1263 |
|
*/ |
1264 |
|
public void testThenRun_normalCompletion() { |
1265 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1262 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1266 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1267 |
|
{ |
1268 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1269 |
< |
final Noop r = new Noop(m); |
1270 |
< |
if (!createIncomplete) f.complete(v1); |
1268 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1269 |
< |
if (createIncomplete) { |
1270 |
< |
checkIncomplete(g); |
1271 |
< |
f.complete(v1); |
1272 |
< |
} |
1269 |
> |
final Noop[] rs = new Noop[6]; |
1270 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1271 |
|
|
1272 |
< |
checkCompletedNormally(g, null); |
1272 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1273 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1274 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1275 |
> |
checkIncomplete(h0); |
1276 |
> |
checkIncomplete(h1); |
1277 |
> |
checkIncomplete(h2); |
1278 |
> |
assertTrue(f.complete(v1)); |
1279 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1280 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1281 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1282 |
> |
|
1283 |
> |
checkCompletedNormally(h0, null); |
1284 |
> |
checkCompletedNormally(h1, null); |
1285 |
> |
checkCompletedNormally(h2, null); |
1286 |
> |
checkCompletedNormally(h3, null); |
1287 |
> |
checkCompletedNormally(h4, null); |
1288 |
> |
checkCompletedNormally(h5, null); |
1289 |
|
checkCompletedNormally(f, v1); |
1290 |
< |
r.assertInvoked(); |
1290 |
> |
for (Noop r : rs) r.assertInvoked(); |
1291 |
|
}} |
1292 |
|
|
1293 |
|
/** |
1296 |
|
*/ |
1297 |
|
public void testThenRun_exceptionalCompletion() { |
1298 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1285 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1299 |
|
{ |
1300 |
|
final CFException ex = new CFException(); |
1301 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1302 |
< |
final Noop r = new Noop(m); |
1303 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1291 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1292 |
< |
if (createIncomplete) { |
1293 |
< |
checkIncomplete(g); |
1294 |
< |
f.completeExceptionally(ex); |
1295 |
< |
} |
1302 |
> |
final Noop[] rs = new Noop[6]; |
1303 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1304 |
|
|
1305 |
< |
checkCompletedWithWrappedException(g, ex); |
1305 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1306 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1307 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1308 |
> |
checkIncomplete(h0); |
1309 |
> |
checkIncomplete(h1); |
1310 |
> |
checkIncomplete(h2); |
1311 |
> |
assertTrue(f.completeExceptionally(ex)); |
1312 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1313 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1314 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1315 |
> |
|
1316 |
> |
checkCompletedWithWrappedException(h0, ex); |
1317 |
> |
checkCompletedWithWrappedException(h1, ex); |
1318 |
> |
checkCompletedWithWrappedException(h2, ex); |
1319 |
> |
checkCompletedWithWrappedException(h3, ex); |
1320 |
> |
checkCompletedWithWrappedException(h4, ex); |
1321 |
> |
checkCompletedWithWrappedException(h5, ex); |
1322 |
|
checkCompletedExceptionally(f, ex); |
1323 |
< |
r.assertNotInvoked(); |
1323 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1324 |
|
}} |
1325 |
|
|
1326 |
|
/** |
1328 |
|
*/ |
1329 |
|
public void testThenRun_sourceCancelled() { |
1330 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1307 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1331 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1332 |
|
{ |
1333 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1334 |
< |
final Noop r = new Noop(m); |
1335 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1313 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1314 |
< |
if (createIncomplete) { |
1315 |
< |
checkIncomplete(g); |
1316 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1317 |
< |
} |
1334 |
> |
final Noop[] rs = new Noop[6]; |
1335 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1336 |
|
|
1337 |
< |
checkCompletedWithWrappedCancellationException(g); |
1337 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1338 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1339 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1340 |
> |
checkIncomplete(h0); |
1341 |
> |
checkIncomplete(h1); |
1342 |
> |
checkIncomplete(h2); |
1343 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1344 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1345 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1346 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1347 |
> |
|
1348 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1349 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1350 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1351 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1352 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1353 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1354 |
|
checkCancelled(f); |
1355 |
< |
r.assertNotInvoked(); |
1355 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1356 |
|
}} |
1357 |
|
|
1358 |
|
/** |
1360 |
|
*/ |
1361 |
|
public void testThenRun_actionFailed() { |
1362 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1329 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1363 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1364 |
|
{ |
1365 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1366 |
< |
final FailingRunnable r = new FailingRunnable(m); |
1367 |
< |
if (!createIncomplete) f.complete(v1); |
1335 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1336 |
< |
if (createIncomplete) { |
1337 |
< |
checkIncomplete(g); |
1338 |
< |
f.complete(v1); |
1339 |
< |
} |
1366 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1367 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1368 |
|
|
1369 |
< |
checkCompletedWithWrappedCFException(g); |
1369 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1370 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1371 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1372 |
> |
assertTrue(f.complete(v1)); |
1373 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1374 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1375 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1376 |
> |
|
1377 |
> |
checkCompletedWithWrappedCFException(h0); |
1378 |
> |
checkCompletedWithWrappedCFException(h1); |
1379 |
> |
checkCompletedWithWrappedCFException(h2); |
1380 |
> |
checkCompletedWithWrappedCFException(h3); |
1381 |
> |
checkCompletedWithWrappedCFException(h4); |
1382 |
> |
checkCompletedWithWrappedCFException(h5); |
1383 |
|
checkCompletedNormally(f, v1); |
1384 |
|
}} |
1385 |
|
|
1388 |
|
*/ |
1389 |
|
public void testThenApply_normalCompletion() { |
1390 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1350 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1391 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1392 |
|
{ |
1393 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1394 |
< |
final IncFunction r = new IncFunction(m); |
1395 |
< |
if (!createIncomplete) f.complete(v1); |
1356 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1357 |
< |
if (createIncomplete) { |
1358 |
< |
checkIncomplete(g); |
1359 |
< |
f.complete(v1); |
1360 |
< |
} |
1394 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1395 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1396 |
|
|
1397 |
< |
checkCompletedNormally(g, inc(v1)); |
1397 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1398 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1399 |
> |
checkIncomplete(h0); |
1400 |
> |
checkIncomplete(h1); |
1401 |
> |
assertTrue(f.complete(v1)); |
1402 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1403 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1404 |
> |
|
1405 |
> |
checkCompletedNormally(h0, inc(v1)); |
1406 |
> |
checkCompletedNormally(h1, inc(v1)); |
1407 |
> |
checkCompletedNormally(h2, inc(v1)); |
1408 |
> |
checkCompletedNormally(h3, inc(v1)); |
1409 |
|
checkCompletedNormally(f, v1); |
1410 |
< |
r.assertValue(inc(v1)); |
1410 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1411 |
|
}} |
1412 |
|
|
1413 |
|
/** |
1416 |
|
*/ |
1417 |
|
public void testThenApply_exceptionalCompletion() { |
1418 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1373 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1419 |
|
{ |
1420 |
|
final CFException ex = new CFException(); |
1421 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1422 |
< |
final IncFunction r = new IncFunction(m); |
1423 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1379 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1380 |
< |
if (createIncomplete) { |
1381 |
< |
checkIncomplete(g); |
1382 |
< |
f.completeExceptionally(ex); |
1383 |
< |
} |
1422 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1423 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1424 |
|
|
1425 |
< |
checkCompletedWithWrappedException(g, ex); |
1425 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1426 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1427 |
> |
assertTrue(f.completeExceptionally(ex)); |
1428 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1429 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1430 |
> |
|
1431 |
> |
checkCompletedWithWrappedException(h0, ex); |
1432 |
> |
checkCompletedWithWrappedException(h1, ex); |
1433 |
> |
checkCompletedWithWrappedException(h2, ex); |
1434 |
> |
checkCompletedWithWrappedException(h3, ex); |
1435 |
|
checkCompletedExceptionally(f, ex); |
1436 |
< |
r.assertNotInvoked(); |
1436 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1437 |
|
}} |
1438 |
|
|
1439 |
|
/** |
1441 |
|
*/ |
1442 |
|
public void testThenApply_sourceCancelled() { |
1443 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1395 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1444 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1445 |
|
{ |
1446 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1447 |
< |
final IncFunction r = new IncFunction(m); |
1448 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1401 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1402 |
< |
if (createIncomplete) { |
1403 |
< |
checkIncomplete(g); |
1404 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1405 |
< |
} |
1447 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1448 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1449 |
|
|
1450 |
< |
checkCompletedWithWrappedCancellationException(g); |
1450 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1451 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1452 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1453 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1454 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1455 |
> |
|
1456 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1457 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1458 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1459 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1460 |
|
checkCancelled(f); |
1461 |
< |
r.assertNotInvoked(); |
1461 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1462 |
|
}} |
1463 |
|
|
1464 |
|
/** |
1466 |
|
*/ |
1467 |
|
public void testThenApply_actionFailed() { |
1468 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1417 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1469 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1470 |
|
{ |
1471 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1472 |
< |
final FailingFunction r = new FailingFunction(m); |
1473 |
< |
if (!createIncomplete) f.complete(v1); |
1423 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1424 |
< |
if (createIncomplete) { |
1425 |
< |
checkIncomplete(g); |
1426 |
< |
f.complete(v1); |
1427 |
< |
} |
1472 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1473 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1474 |
|
|
1475 |
< |
checkCompletedWithWrappedCFException(g); |
1475 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1476 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1477 |
> |
assertTrue(f.complete(v1)); |
1478 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1479 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1480 |
> |
|
1481 |
> |
checkCompletedWithWrappedCFException(h0); |
1482 |
> |
checkCompletedWithWrappedCFException(h1); |
1483 |
> |
checkCompletedWithWrappedCFException(h2); |
1484 |
> |
checkCompletedWithWrappedCFException(h3); |
1485 |
|
checkCompletedNormally(f, v1); |
1486 |
|
}} |
1487 |
|
|
1490 |
|
*/ |
1491 |
|
public void testThenAccept_normalCompletion() { |
1492 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1438 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1493 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1494 |
|
{ |
1495 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1496 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1497 |
< |
if (!createIncomplete) f.complete(v1); |
1444 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1445 |
< |
if (createIncomplete) { |
1446 |
< |
checkIncomplete(g); |
1447 |
< |
f.complete(v1); |
1448 |
< |
} |
1496 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1497 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1498 |
|
|
1499 |
< |
checkCompletedNormally(g, null); |
1500 |
< |
r.assertValue(v1); |
1499 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1500 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1501 |
> |
checkIncomplete(h0); |
1502 |
> |
checkIncomplete(h1); |
1503 |
> |
assertTrue(f.complete(v1)); |
1504 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1505 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1506 |
> |
|
1507 |
> |
checkCompletedNormally(h0, null); |
1508 |
> |
checkCompletedNormally(h1, null); |
1509 |
> |
checkCompletedNormally(h2, null); |
1510 |
> |
checkCompletedNormally(h3, null); |
1511 |
|
checkCompletedNormally(f, v1); |
1512 |
+ |
for (NoopConsumer r : rs) r.assertValue(v1); |
1513 |
|
}} |
1514 |
|
|
1515 |
|
/** |
1518 |
|
*/ |
1519 |
|
public void testThenAccept_exceptionalCompletion() { |
1520 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1461 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1521 |
|
{ |
1522 |
|
final CFException ex = new CFException(); |
1523 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1524 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1525 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1467 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1468 |
< |
if (createIncomplete) { |
1469 |
< |
checkIncomplete(g); |
1470 |
< |
f.completeExceptionally(ex); |
1471 |
< |
} |
1524 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1525 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1526 |
|
|
1527 |
< |
checkCompletedWithWrappedException(g, ex); |
1527 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1528 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1529 |
> |
assertTrue(f.completeExceptionally(ex)); |
1530 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1531 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1532 |
> |
|
1533 |
> |
checkCompletedWithWrappedException(h0, ex); |
1534 |
> |
checkCompletedWithWrappedException(h1, ex); |
1535 |
> |
checkCompletedWithWrappedException(h2, ex); |
1536 |
> |
checkCompletedWithWrappedException(h3, ex); |
1537 |
|
checkCompletedExceptionally(f, ex); |
1538 |
< |
r.assertNotInvoked(); |
1538 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1539 |
|
}} |
1540 |
|
|
1541 |
|
/** |
1543 |
|
*/ |
1544 |
|
public void testThenAccept_sourceCancelled() { |
1545 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1483 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1546 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1547 |
|
{ |
1548 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1549 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1550 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1489 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1490 |
< |
if (createIncomplete) { |
1491 |
< |
checkIncomplete(g); |
1492 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1493 |
< |
} |
1549 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1550 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1551 |
|
|
1552 |
< |
checkCompletedWithWrappedCancellationException(g); |
1552 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1553 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1554 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1555 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1556 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1557 |
> |
|
1558 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1559 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1560 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1561 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1562 |
|
checkCancelled(f); |
1563 |
< |
r.assertNotInvoked(); |
1563 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1564 |
|
}} |
1565 |
|
|
1566 |
|
/** |
1568 |
|
*/ |
1569 |
|
public void testThenAccept_actionFailed() { |
1570 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1505 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1571 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1572 |
|
{ |
1573 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1574 |
< |
final FailingConsumer r = new FailingConsumer(m); |
1575 |
< |
if (!createIncomplete) f.complete(v1); |
1511 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1512 |
< |
if (createIncomplete) { |
1513 |
< |
checkIncomplete(g); |
1514 |
< |
f.complete(v1); |
1515 |
< |
} |
1574 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1575 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1576 |
|
|
1577 |
< |
checkCompletedWithWrappedCFException(g); |
1577 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1578 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1579 |
> |
assertTrue(f.complete(v1)); |
1580 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1581 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1582 |
> |
|
1583 |
> |
checkCompletedWithWrappedCFException(h0); |
1584 |
> |
checkCompletedWithWrappedCFException(h1); |
1585 |
> |
checkCompletedWithWrappedCFException(h2); |
1586 |
> |
checkCompletedWithWrappedCFException(h3); |
1587 |
|
checkCompletedNormally(f, v1); |
1588 |
|
}} |
1589 |
|
|
1593 |
|
*/ |
1594 |
|
public void testThenCombine_normalCompletion() { |
1595 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1527 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1596 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1597 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1598 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1599 |
|
{ |
1600 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1601 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1602 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1602 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1603 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1604 |
|
|
1605 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1606 |
< |
if (!createIncomplete) |
1607 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1608 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1609 |
< |
if (createIncomplete) { |
1610 |
< |
checkIncomplete(h); |
1611 |
< |
r.assertNotInvoked(); |
1612 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1613 |
< |
} |
1605 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1606 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1607 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1608 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1609 |
> |
|
1610 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1611 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1612 |
> |
assertTrue(fst.complete(w1)); |
1613 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1614 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1615 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1616 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1617 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1618 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1619 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1620 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1621 |
> |
assertTrue(snd.complete(w2)); |
1622 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1623 |
> |
|
1624 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1625 |
> |
checkCompletedNormally(h2, subtract(v1, v2)); |
1626 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1627 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1628 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1629 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1630 |
|
|
1546 |
– |
checkCompletedNormally(h, subtract(v1, v2)); |
1631 |
|
checkCompletedNormally(f, v1); |
1632 |
|
checkCompletedNormally(g, v2); |
1549 |
– |
r.assertValue(subtract(v1, v2)); |
1633 |
|
}} |
1634 |
|
|
1635 |
|
/** |
1636 |
|
* thenCombine result completes exceptionally after exceptional |
1637 |
|
* completion of either source |
1638 |
|
*/ |
1639 |
< |
public void testThenCombine_exceptionalCompletion() { |
1639 |
> |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
1640 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1558 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1641 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1642 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1643 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1644 |
|
{ |
1645 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1646 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1647 |
|
final CFException ex = new CFException(); |
1648 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1649 |
< |
|
1650 |
< |
(fFirst ? f : g).complete(v1); |
1651 |
< |
if (!createIncomplete) |
1652 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1653 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1654 |
< |
if (createIncomplete) { |
1655 |
< |
checkIncomplete(h); |
1656 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1657 |
< |
} |
1648 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1649 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1650 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1651 |
> |
|
1652 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1653 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1654 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1655 |
> |
() -> fst.completeExceptionally(ex) : |
1656 |
> |
() -> fst.complete(v1); |
1657 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1658 |
> |
() -> snd.complete(v1) : |
1659 |
> |
() -> snd.completeExceptionally(ex); |
1660 |
> |
|
1661 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1662 |
> |
assertTrue(complete1.call()); |
1663 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1664 |
> |
checkIncomplete(h1); |
1665 |
> |
checkIncomplete(h2); |
1666 |
> |
assertTrue(complete2.call()); |
1667 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1668 |
|
|
1669 |
< |
checkCompletedWithWrappedException(h, ex); |
1670 |
< |
r.assertNotInvoked(); |
1671 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1672 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1669 |
> |
checkCompletedWithWrappedException(h1, ex); |
1670 |
> |
checkCompletedWithWrappedException(h2, ex); |
1671 |
> |
checkCompletedWithWrappedException(h3, ex); |
1672 |
> |
r1.assertNotInvoked(); |
1673 |
> |
r2.assertNotInvoked(); |
1674 |
> |
r3.assertNotInvoked(); |
1675 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1676 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1677 |
|
}} |
1678 |
|
|
1679 |
|
/** |
1680 |
|
* thenCombine result completes exceptionally if either source cancelled |
1681 |
|
*/ |
1682 |
< |
public void testThenCombine_sourceCancelled() { |
1682 |
> |
public void testThenCombine_sourceCancelled() throws Throwable { |
1683 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1684 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1588 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1685 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1686 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1687 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1688 |
|
{ |
1689 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1690 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1691 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1691 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1692 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1693 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1694 |
|
|
1695 |
< |
(fFirst ? f : g).complete(v1); |
1696 |
< |
if (!createIncomplete) |
1697 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1698 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1699 |
< |
if (createIncomplete) { |
1700 |
< |
checkIncomplete(h); |
1701 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1702 |
< |
} |
1695 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1696 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1697 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1698 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1699 |
> |
() -> fst.complete(v1); |
1700 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1701 |
> |
() -> snd.complete(v1) : |
1702 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1703 |
|
|
1704 |
< |
checkCompletedWithWrappedCancellationException(h); |
1705 |
< |
checkCancelled(!fFirst ? f : g); |
1706 |
< |
r.assertNotInvoked(); |
1707 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1704 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1705 |
> |
assertTrue(complete1.call()); |
1706 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1707 |
> |
checkIncomplete(h1); |
1708 |
> |
checkIncomplete(h2); |
1709 |
> |
assertTrue(complete2.call()); |
1710 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1711 |
> |
|
1712 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1713 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1714 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1715 |
> |
r1.assertNotInvoked(); |
1716 |
> |
r2.assertNotInvoked(); |
1717 |
> |
r3.assertNotInvoked(); |
1718 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1719 |
> |
checkCancelled(failFirst ? fst : snd); |
1720 |
|
}} |
1721 |
|
|
1722 |
|
/** |
1730 |
|
{ |
1731 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1732 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1733 |
< |
final FailingBiFunction r = new FailingBiFunction(m); |
1734 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1733 |
> |
final FailingBiFunction r1 = new FailingBiFunction(m); |
1734 |
> |
final FailingBiFunction r2 = new FailingBiFunction(m); |
1735 |
> |
final FailingBiFunction r3 = new FailingBiFunction(m); |
1736 |
> |
|
1737 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1738 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1739 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1740 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1741 |
> |
|
1742 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1743 |
> |
assertTrue(fst.complete(w1)); |
1744 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1745 |
> |
assertTrue(snd.complete(w2)); |
1746 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1747 |
|
|
1748 |
< |
if (fFirst) { |
1749 |
< |
f.complete(v1); |
1750 |
< |
g.complete(v2); |
1751 |
< |
} else { |
1752 |
< |
g.complete(v2); |
1753 |
< |
f.complete(v1); |
1631 |
< |
} |
1632 |
< |
|
1633 |
< |
checkCompletedWithWrappedCFException(h); |
1748 |
> |
checkCompletedWithWrappedCFException(h1); |
1749 |
> |
checkCompletedWithWrappedCFException(h2); |
1750 |
> |
checkCompletedWithWrappedCFException(h3); |
1751 |
> |
r1.assertInvoked(); |
1752 |
> |
r2.assertInvoked(); |
1753 |
> |
r3.assertInvoked(); |
1754 |
|
checkCompletedNormally(f, v1); |
1755 |
|
checkCompletedNormally(g, v2); |
1756 |
|
}} |
1761 |
|
*/ |
1762 |
|
public void testThenAcceptBoth_normalCompletion() { |
1763 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1644 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1764 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1765 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1766 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1767 |
|
{ |
1768 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1769 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1770 |
< |
final SubtractAction r = new SubtractAction(m); |
1771 |
< |
|
1772 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1773 |
< |
if (!createIncomplete) |
1774 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1775 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1776 |
< |
if (createIncomplete) { |
1777 |
< |
checkIncomplete(h); |
1778 |
< |
r.assertNotInvoked(); |
1779 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1780 |
< |
} |
1770 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1771 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1772 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1773 |
> |
|
1774 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1775 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1776 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1777 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1778 |
> |
|
1779 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1780 |
> |
assertTrue(fst.complete(w1)); |
1781 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1782 |
> |
checkIncomplete(h1); |
1783 |
> |
checkIncomplete(h2); |
1784 |
> |
r1.assertNotInvoked(); |
1785 |
> |
r2.assertNotInvoked(); |
1786 |
> |
assertTrue(snd.complete(w2)); |
1787 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1788 |
|
|
1789 |
< |
checkCompletedNormally(h, null); |
1790 |
< |
r.assertValue(subtract(v1, v2)); |
1789 |
> |
checkCompletedNormally(h1, null); |
1790 |
> |
checkCompletedNormally(h2, null); |
1791 |
> |
checkCompletedNormally(h3, null); |
1792 |
> |
r1.assertValue(subtract(v1, v2)); |
1793 |
> |
r2.assertValue(subtract(v1, v2)); |
1794 |
> |
r3.assertValue(subtract(v1, v2)); |
1795 |
|
checkCompletedNormally(f, v1); |
1796 |
|
checkCompletedNormally(g, v2); |
1797 |
|
}} |
1800 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1801 |
|
* completion of either source |
1802 |
|
*/ |
1803 |
< |
public void testThenAcceptBoth_exceptionalCompletion() { |
1803 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1804 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1675 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1805 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1806 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1807 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1808 |
|
{ |
1809 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1810 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1811 |
|
final CFException ex = new CFException(); |
1812 |
< |
final SubtractAction r = new SubtractAction(m); |
1813 |
< |
|
1814 |
< |
(fFirst ? f : g).complete(v1); |
1815 |
< |
if (!createIncomplete) |
1816 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1817 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1818 |
< |
if (createIncomplete) { |
1819 |
< |
checkIncomplete(h); |
1820 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1821 |
< |
} |
1812 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1813 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1814 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1815 |
> |
|
1816 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1817 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1818 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1819 |
> |
() -> fst.completeExceptionally(ex) : |
1820 |
> |
() -> fst.complete(v1); |
1821 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1822 |
> |
() -> snd.complete(v1) : |
1823 |
> |
() -> snd.completeExceptionally(ex); |
1824 |
> |
|
1825 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1826 |
> |
assertTrue(complete1.call()); |
1827 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1828 |
> |
checkIncomplete(h1); |
1829 |
> |
checkIncomplete(h2); |
1830 |
> |
assertTrue(complete2.call()); |
1831 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1832 |
|
|
1833 |
< |
checkCompletedWithWrappedException(h, ex); |
1834 |
< |
r.assertNotInvoked(); |
1835 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1836 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1833 |
> |
checkCompletedWithWrappedException(h1, ex); |
1834 |
> |
checkCompletedWithWrappedException(h2, ex); |
1835 |
> |
checkCompletedWithWrappedException(h3, ex); |
1836 |
> |
r1.assertNotInvoked(); |
1837 |
> |
r2.assertNotInvoked(); |
1838 |
> |
r3.assertNotInvoked(); |
1839 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1840 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1841 |
|
}} |
1842 |
|
|
1843 |
|
/** |
1844 |
|
* thenAcceptBoth result completes exceptionally if either source cancelled |
1845 |
|
*/ |
1846 |
< |
public void testThenAcceptBoth_sourceCancelled() { |
1846 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1847 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1848 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1705 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1849 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1850 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1851 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1852 |
|
{ |
1853 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1854 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1855 |
< |
final SubtractAction r = new SubtractAction(m); |
1855 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1856 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1857 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1858 |
|
|
1859 |
< |
(fFirst ? f : g).complete(v1); |
1860 |
< |
if (!createIncomplete) |
1861 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1862 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1863 |
< |
if (createIncomplete) { |
1864 |
< |
checkIncomplete(h); |
1865 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1866 |
< |
} |
1859 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1860 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1861 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1862 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1863 |
> |
() -> fst.complete(v1); |
1864 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1865 |
> |
() -> snd.complete(v1) : |
1866 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1867 |
|
|
1868 |
< |
checkCompletedWithWrappedCancellationException(h); |
1869 |
< |
checkCancelled(!fFirst ? f : g); |
1870 |
< |
r.assertNotInvoked(); |
1871 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1868 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1869 |
> |
assertTrue(complete1.call()); |
1870 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1871 |
> |
checkIncomplete(h1); |
1872 |
> |
checkIncomplete(h2); |
1873 |
> |
assertTrue(complete2.call()); |
1874 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1875 |
> |
|
1876 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1877 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1878 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1879 |
> |
r1.assertNotInvoked(); |
1880 |
> |
r2.assertNotInvoked(); |
1881 |
> |
r3.assertNotInvoked(); |
1882 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1883 |
> |
checkCancelled(failFirst ? fst : snd); |
1884 |
|
}} |
1885 |
|
|
1886 |
|
/** |
1894 |
|
{ |
1895 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1896 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1897 |
< |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1898 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1897 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1898 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1899 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1900 |
> |
|
1901 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1902 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1903 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1904 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1905 |
> |
|
1906 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1907 |
> |
assertTrue(fst.complete(w1)); |
1908 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1909 |
> |
assertTrue(snd.complete(w2)); |
1910 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1911 |
|
|
1912 |
< |
if (fFirst) { |
1913 |
< |
f.complete(v1); |
1914 |
< |
g.complete(v2); |
1915 |
< |
} else { |
1916 |
< |
g.complete(v2); |
1917 |
< |
f.complete(v1); |
1748 |
< |
} |
1749 |
< |
|
1750 |
< |
checkCompletedWithWrappedCFException(h); |
1912 |
> |
checkCompletedWithWrappedCFException(h1); |
1913 |
> |
checkCompletedWithWrappedCFException(h2); |
1914 |
> |
checkCompletedWithWrappedCFException(h3); |
1915 |
> |
r1.assertInvoked(); |
1916 |
> |
r2.assertInvoked(); |
1917 |
> |
r3.assertInvoked(); |
1918 |
|
checkCompletedNormally(f, v1); |
1919 |
|
checkCompletedNormally(g, v2); |
1920 |
|
}} |
1925 |
|
*/ |
1926 |
|
public void testRunAfterBoth_normalCompletion() { |
1927 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1761 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1928 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1929 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1930 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1931 |
|
{ |
1932 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1933 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1934 |
< |
final Noop r = new Noop(m); |
1935 |
< |
|
1936 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1937 |
< |
if (!createIncomplete) |
1938 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1939 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1940 |
< |
if (createIncomplete) { |
1941 |
< |
checkIncomplete(h); |
1942 |
< |
r.assertNotInvoked(); |
1943 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1944 |
< |
} |
1934 |
> |
final Noop r1 = new Noop(m); |
1935 |
> |
final Noop r2 = new Noop(m); |
1936 |
> |
final Noop r3 = new Noop(m); |
1937 |
> |
|
1938 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1939 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1940 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1941 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1942 |
> |
|
1943 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1944 |
> |
assertTrue(fst.complete(w1)); |
1945 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1946 |
> |
checkIncomplete(h1); |
1947 |
> |
checkIncomplete(h2); |
1948 |
> |
r1.assertNotInvoked(); |
1949 |
> |
r2.assertNotInvoked(); |
1950 |
> |
assertTrue(snd.complete(w2)); |
1951 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1952 |
|
|
1953 |
< |
checkCompletedNormally(h, null); |
1954 |
< |
r.assertInvoked(); |
1953 |
> |
checkCompletedNormally(h1, null); |
1954 |
> |
checkCompletedNormally(h2, null); |
1955 |
> |
checkCompletedNormally(h3, null); |
1956 |
> |
r1.assertInvoked(); |
1957 |
> |
r2.assertInvoked(); |
1958 |
> |
r3.assertInvoked(); |
1959 |
|
checkCompletedNormally(f, v1); |
1960 |
|
checkCompletedNormally(g, v2); |
1961 |
|
}} |
1964 |
|
* runAfterBoth result completes exceptionally after exceptional |
1965 |
|
* completion of either source |
1966 |
|
*/ |
1967 |
< |
public void testRunAfterBoth_exceptionalCompletion() { |
1967 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
1968 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1792 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1969 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1970 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1971 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1972 |
|
{ |
1973 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1974 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1975 |
|
final CFException ex = new CFException(); |
1976 |
< |
final Noop r = new Noop(m); |
1977 |
< |
|
1978 |
< |
(fFirst ? f : g).complete(v1); |
1979 |
< |
if (!createIncomplete) |
1980 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1981 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1982 |
< |
if (createIncomplete) { |
1983 |
< |
checkIncomplete(h); |
1984 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1985 |
< |
} |
1976 |
> |
final Noop r1 = new Noop(m); |
1977 |
> |
final Noop r2 = new Noop(m); |
1978 |
> |
final Noop r3 = new Noop(m); |
1979 |
> |
|
1980 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1981 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1982 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1983 |
> |
() -> fst.completeExceptionally(ex) : |
1984 |
> |
() -> fst.complete(v1); |
1985 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1986 |
> |
() -> snd.complete(v1) : |
1987 |
> |
() -> snd.completeExceptionally(ex); |
1988 |
> |
|
1989 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1990 |
> |
assertTrue(complete1.call()); |
1991 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1992 |
> |
checkIncomplete(h1); |
1993 |
> |
checkIncomplete(h2); |
1994 |
> |
assertTrue(complete2.call()); |
1995 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1996 |
|
|
1997 |
< |
checkCompletedWithWrappedException(h, ex); |
1998 |
< |
r.assertNotInvoked(); |
1999 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2000 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1997 |
> |
checkCompletedWithWrappedException(h1, ex); |
1998 |
> |
checkCompletedWithWrappedException(h2, ex); |
1999 |
> |
checkCompletedWithWrappedException(h3, ex); |
2000 |
> |
r1.assertNotInvoked(); |
2001 |
> |
r2.assertNotInvoked(); |
2002 |
> |
r3.assertNotInvoked(); |
2003 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2004 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
2005 |
|
}} |
2006 |
|
|
2007 |
|
/** |
2008 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
2009 |
|
*/ |
2010 |
< |
public void testRunAfterBoth_sourceCancelled() { |
2010 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
2011 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2012 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1822 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2013 |
|
for (boolean fFirst : new boolean[] { true, false }) |
2014 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
2015 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2016 |
|
{ |
2017 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2018 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2019 |
< |
final Noop r = new Noop(m); |
2019 |
> |
final Noop r1 = new Noop(m); |
2020 |
> |
final Noop r2 = new Noop(m); |
2021 |
> |
final Noop r3 = new Noop(m); |
2022 |
|
|
2023 |
< |
(fFirst ? f : g).complete(v1); |
2024 |
< |
if (!createIncomplete) |
2025 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2026 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2027 |
< |
if (createIncomplete) { |
2028 |
< |
checkIncomplete(h); |
2029 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2030 |
< |
} |
2023 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2024 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2025 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2026 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
2027 |
> |
() -> fst.complete(v1); |
2028 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2029 |
> |
() -> snd.complete(v1) : |
2030 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
2031 |
|
|
2032 |
< |
checkCompletedWithWrappedCancellationException(h); |
2033 |
< |
checkCancelled(!fFirst ? f : g); |
2034 |
< |
r.assertNotInvoked(); |
2035 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2032 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2033 |
> |
assertTrue(complete1.call()); |
2034 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2035 |
> |
checkIncomplete(h1); |
2036 |
> |
checkIncomplete(h2); |
2037 |
> |
assertTrue(complete2.call()); |
2038 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2039 |
> |
|
2040 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2041 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2042 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2043 |
> |
r1.assertNotInvoked(); |
2044 |
> |
r2.assertNotInvoked(); |
2045 |
> |
r3.assertNotInvoked(); |
2046 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2047 |
> |
checkCancelled(failFirst ? fst : snd); |
2048 |
|
}} |
2049 |
|
|
2050 |
|
/** |
2060 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2061 |
|
final FailingRunnable r1 = new FailingRunnable(m); |
2062 |
|
final FailingRunnable r2 = new FailingRunnable(m); |
2063 |
+ |
final FailingRunnable r3 = new FailingRunnable(m); |
2064 |
|
|
2065 |
< |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2066 |
< |
if (fFirst) { |
2067 |
< |
f.complete(v1); |
2068 |
< |
g.complete(v2); |
2069 |
< |
} else { |
2070 |
< |
g.complete(v2); |
2071 |
< |
f.complete(v1); |
2072 |
< |
} |
2073 |
< |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2065 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2066 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2067 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2068 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2069 |
> |
|
2070 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2071 |
> |
assertTrue(fst.complete(w1)); |
2072 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2073 |
> |
assertTrue(snd.complete(w2)); |
2074 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2075 |
|
|
2076 |
|
checkCompletedWithWrappedCFException(h1); |
2077 |
|
checkCompletedWithWrappedCFException(h2); |
2078 |
+ |
checkCompletedWithWrappedCFException(h3); |
2079 |
+ |
r1.assertInvoked(); |
2080 |
+ |
r2.assertInvoked(); |
2081 |
+ |
r3.assertInvoked(); |
2082 |
|
checkCompletedNormally(f, v1); |
2083 |
|
checkCompletedNormally(g, v2); |
2084 |
|
}} |
2201 |
|
|
2202 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2203 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2204 |
< |
if (fFirst) { |
2205 |
< |
f.complete(v1); |
1995 |
< |
g.completeExceptionally(ex); |
1996 |
< |
} else { |
1997 |
< |
g.completeExceptionally(ex); |
1998 |
< |
f.complete(v1); |
1999 |
< |
} |
2204 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2205 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2206 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2207 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2208 |
|
|
2308 |
|
|
2309 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2310 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2311 |
< |
if (fFirst) { |
2312 |
< |
f.complete(v1); |
2107 |
< |
g.cancel(mayInterruptIfRunning); |
2108 |
< |
} else { |
2109 |
< |
g.cancel(mayInterruptIfRunning); |
2110 |
< |
f.complete(v1); |
2111 |
< |
} |
2311 |
> |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2312 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2313 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2314 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2315 |
|
|
2511 |
|
|
2512 |
|
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2513 |
|
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2514 |
< |
if (fFirst) { |
2515 |
< |
f.complete(v1); |
2315 |
< |
g.completeExceptionally(ex); |
2316 |
< |
} else { |
2317 |
< |
g.completeExceptionally(ex); |
2318 |
< |
f.complete(v1); |
2319 |
< |
} |
2514 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2515 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2516 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2517 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2518 |
|
|
2715 |
|
checkIncomplete(h1); |
2716 |
|
rs[0].assertNotInvoked(); |
2717 |
|
rs[1].assertNotInvoked(); |
2718 |
< |
f.completeExceptionally(ex); |
2718 |
> |
assertTrue(f.completeExceptionally(ex)); |
2719 |
|
checkCompletedWithWrappedException(h0, ex); |
2720 |
|
checkCompletedWithWrappedException(h1, ex); |
2721 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2723 |
|
checkCompletedWithWrappedException(h2, ex); |
2724 |
|
checkCompletedWithWrappedException(h3, ex); |
2725 |
|
|
2726 |
< |
g.complete(v1); |
2726 |
> |
assertTrue(g.complete(v1)); |
2727 |
|
|
2728 |
|
// unspecified behavior - both source completions available |
2729 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2766 |
|
|
2767 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2768 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2769 |
< |
if (fFirst) { |
2770 |
< |
f.complete(v1); |
2575 |
< |
g.completeExceptionally(ex); |
2576 |
< |
} else { |
2577 |
< |
g.completeExceptionally(ex); |
2578 |
< |
f.complete(v1); |
2579 |
< |
} |
2769 |
> |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2770 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2771 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2772 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2773 |
|
|
2832 |
|
checkCompletedWithWrappedCancellationException(h2); |
2833 |
|
checkCompletedWithWrappedCancellationException(h3); |
2834 |
|
|
2835 |
< |
g.complete(v1); |
2835 |
> |
assertTrue(g.complete(v1)); |
2836 |
|
|
2837 |
|
// unspecified behavior - both source completions available |
2838 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2876 |
|
|
2877 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2878 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2879 |
< |
f.complete(v1); |
2879 |
> |
assertTrue(f.complete(v1)); |
2880 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2881 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2882 |
|
checkCompletedWithWrappedCFException(h0); |
2884 |
|
checkCompletedWithWrappedCFException(h2); |
2885 |
|
checkCompletedWithWrappedCFException(h3); |
2886 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2887 |
< |
g.complete(v2); |
2887 |
> |
assertTrue(g.complete(v2)); |
2888 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2889 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2890 |
|
checkCompletedWithWrappedCFException(h4); |
2905 |
|
{ |
2906 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2907 |
|
final CompletableFutureInc r = new CompletableFutureInc(m); |
2908 |
< |
if (!createIncomplete) f.complete(v1); |
2908 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2909 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2910 |
< |
if (createIncomplete) f.complete(v1); |
2910 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2911 |
|
|
2912 |
|
checkCompletedNormally(g, inc(v1)); |
2913 |
|
checkCompletedNormally(f, v1); |
2945 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2946 |
|
final FailingCompletableFutureFunction r |
2947 |
|
= new FailingCompletableFutureFunction(m); |
2948 |
< |
if (!createIncomplete) f.complete(v1); |
2948 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2949 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2950 |
< |
if (createIncomplete) f.complete(v1); |
2950 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2951 |
|
|
2952 |
|
checkCompletedWithWrappedCFException(g); |
2953 |
|
checkCompletedNormally(f, v1); |
2974 |
|
checkCancelled(f); |
2975 |
|
}} |
2976 |
|
|
2977 |
+ |
/** |
2978 |
+ |
* thenCompose result completes exceptionally if the result of the action does |
2979 |
+ |
*/ |
2980 |
+ |
public void testThenCompose_actionReturnsFailingFuture() { |
2981 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
2982 |
+ |
for (int order = 0; order < 6; order++) |
2983 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
2984 |
+ |
{ |
2985 |
+ |
final CFException ex = new CFException(); |
2986 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2987 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2988 |
+ |
final CompletableFuture<Integer> h; |
2989 |
+ |
// Test all permutations of orders |
2990 |
+ |
switch (order) { |
2991 |
+ |
case 0: |
2992 |
+ |
assertTrue(f.complete(v1)); |
2993 |
+ |
assertTrue(g.completeExceptionally(ex)); |
2994 |
+ |
h = m.thenCompose(f, (x -> g)); |
2995 |
+ |
break; |
2996 |
+ |
case 1: |
2997 |
+ |
assertTrue(f.complete(v1)); |
2998 |
+ |
h = m.thenCompose(f, (x -> g)); |
2999 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3000 |
+ |
break; |
3001 |
+ |
case 2: |
3002 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3003 |
+ |
assertTrue(f.complete(v1)); |
3004 |
+ |
h = m.thenCompose(f, (x -> g)); |
3005 |
+ |
break; |
3006 |
+ |
case 3: |
3007 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3008 |
+ |
h = m.thenCompose(f, (x -> g)); |
3009 |
+ |
assertTrue(f.complete(v1)); |
3010 |
+ |
break; |
3011 |
+ |
case 4: |
3012 |
+ |
h = m.thenCompose(f, (x -> g)); |
3013 |
+ |
assertTrue(f.complete(v1)); |
3014 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3015 |
+ |
break; |
3016 |
+ |
case 5: |
3017 |
+ |
h = m.thenCompose(f, (x -> g)); |
3018 |
+ |
assertTrue(f.complete(v1)); |
3019 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3020 |
+ |
break; |
3021 |
+ |
default: throw new AssertionError(); |
3022 |
+ |
} |
3023 |
+ |
|
3024 |
+ |
checkCompletedExceptionally(g, ex); |
3025 |
+ |
checkCompletedWithWrappedException(h, ex); |
3026 |
+ |
checkCompletedNormally(f, v1); |
3027 |
+ |
}} |
3028 |
+ |
|
3029 |
|
// other static methods |
3030 |
|
|
3031 |
|
/** |
3042 |
|
* when all components complete normally |
3043 |
|
*/ |
3044 |
|
public void testAllOf_normal() throws Exception { |
3045 |
< |
for (int k = 1; k < 20; ++k) { |
3045 |
> |
for (int k = 1; k < 10; k++) { |
3046 |
|
CompletableFuture<Integer>[] fs |
3047 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3048 |
< |
for (int i = 0; i < k; ++i) |
3048 |
> |
for (int i = 0; i < k; i++) |
3049 |
|
fs[i] = new CompletableFuture<>(); |
3050 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3051 |
< |
for (int i = 0; i < k; ++i) { |
3051 |
> |
for (int i = 0; i < k; i++) { |
3052 |
|
checkIncomplete(f); |
3053 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3054 |
|
fs[i].complete(one); |
3059 |
|
} |
3060 |
|
|
3061 |
|
public void testAllOf_backwards() throws Exception { |
3062 |
< |
for (int k = 1; k < 20; ++k) { |
3062 |
> |
for (int k = 1; k < 10; k++) { |
3063 |
|
CompletableFuture<Integer>[] fs |
3064 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3065 |
< |
for (int i = 0; i < k; ++i) |
3065 |
> |
for (int i = 0; i < k; i++) |
3066 |
|
fs[i] = new CompletableFuture<>(); |
3067 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3068 |
|
for (int i = k - 1; i >= 0; i--) { |
3075 |
|
} |
3076 |
|
} |
3077 |
|
|
3078 |
+ |
public void testAllOf_exceptional() throws Exception { |
3079 |
+ |
for (int k = 1; k < 10; k++) { |
3080 |
+ |
CompletableFuture<Integer>[] fs |
3081 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3082 |
+ |
CFException ex = new CFException(); |
3083 |
+ |
for (int i = 0; i < k; i++) |
3084 |
+ |
fs[i] = new CompletableFuture<>(); |
3085 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3086 |
+ |
for (int i = 0; i < k; i++) { |
3087 |
+ |
checkIncomplete(f); |
3088 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3089 |
+ |
if (i != k / 2) { |
3090 |
+ |
fs[i].complete(i); |
3091 |
+ |
checkCompletedNormally(fs[i], i); |
3092 |
+ |
} else { |
3093 |
+ |
fs[i].completeExceptionally(ex); |
3094 |
+ |
checkCompletedExceptionally(fs[i], ex); |
3095 |
+ |
} |
3096 |
+ |
} |
3097 |
+ |
checkCompletedWithWrappedException(f, ex); |
3098 |
+ |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3099 |
+ |
} |
3100 |
+ |
} |
3101 |
+ |
|
3102 |
|
/** |
3103 |
|
* anyOf(no component futures) returns an incomplete future |
3104 |
|
*/ |
3105 |
|
public void testAnyOf_empty() throws Exception { |
3106 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3107 |
+ |
{ |
3108 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3109 |
|
checkIncomplete(f); |
3110 |
< |
} |
3110 |
> |
|
3111 |
> |
f.complete(v1); |
3112 |
> |
checkCompletedNormally(f, v1); |
3113 |
> |
}} |
3114 |
|
|
3115 |
|
/** |
3116 |
|
* anyOf returns a future completed normally with a value when |
3117 |
|
* a component future does |
3118 |
|
*/ |
3119 |
|
public void testAnyOf_normal() throws Exception { |
3120 |
< |
for (int k = 0; k < 10; ++k) { |
3120 |
> |
for (int k = 0; k < 10; k++) { |
3121 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3122 |
< |
for (int i = 0; i < k; ++i) |
3122 |
> |
for (int i = 0; i < k; i++) |
3123 |
|
fs[i] = new CompletableFuture<>(); |
3124 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3125 |
|
checkIncomplete(f); |
3126 |
< |
for (int i = 0; i < k; ++i) { |
3127 |
< |
fs[i].complete(one); |
3128 |
< |
checkCompletedNormally(f, one); |
3129 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
3126 |
> |
for (int i = 0; i < k; i++) { |
3127 |
> |
fs[i].complete(i); |
3128 |
> |
checkCompletedNormally(f, 0); |
3129 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3130 |
> |
assertTrue(0 <= x && x <= i); |
3131 |
> |
} |
3132 |
> |
} |
3133 |
> |
} |
3134 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3135 |
> |
for (int k = 0; k < 10; k++) { |
3136 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3137 |
> |
for (int i = 0; i < k; i++) |
3138 |
> |
fs[i] = new CompletableFuture<>(); |
3139 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3140 |
> |
checkIncomplete(f); |
3141 |
> |
for (int i = k - 1; i >= 0; i--) { |
3142 |
> |
fs[i].complete(i); |
3143 |
> |
checkCompletedNormally(f, k - 1); |
3144 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3145 |
> |
assertTrue(i <= x && x <= k - 1); |
3146 |
|
} |
3147 |
|
} |
3148 |
|
} |
3151 |
|
* anyOf result completes exceptionally when any component does. |
3152 |
|
*/ |
3153 |
|
public void testAnyOf_exceptional() throws Exception { |
3154 |
< |
for (int k = 0; k < 10; ++k) { |
3154 |
> |
for (int k = 0; k < 10; k++) { |
3155 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3156 |
< |
for (int i = 0; i < k; ++i) |
3156 |
> |
CFException[] exs = new CFException[k]; |
3157 |
> |
for (int i = 0; i < k; i++) { |
3158 |
|
fs[i] = new CompletableFuture<>(); |
3159 |
+ |
exs[i] = new CFException(); |
3160 |
+ |
} |
3161 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3162 |
|
checkIncomplete(f); |
3163 |
< |
for (int i = 0; i < k; ++i) { |
3164 |
< |
fs[i].completeExceptionally(new CFException()); |
3165 |
< |
checkCompletedWithWrappedCFException(f); |
3163 |
> |
for (int i = 0; i < k; i++) { |
3164 |
> |
fs[i].completeExceptionally(exs[i]); |
3165 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3166 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3167 |
> |
} |
3168 |
> |
} |
3169 |
> |
} |
3170 |
> |
|
3171 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3172 |
> |
for (int k = 0; k < 10; k++) { |
3173 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3174 |
> |
CFException[] exs = new CFException[k]; |
3175 |
> |
for (int i = 0; i < k; i++) { |
3176 |
> |
fs[i] = new CompletableFuture<>(); |
3177 |
> |
exs[i] = new CFException(); |
3178 |
> |
} |
3179 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3180 |
> |
checkIncomplete(f); |
3181 |
> |
for (int i = k - 1; i >= 0; i--) { |
3182 |
> |
fs[i].completeExceptionally(exs[i]); |
3183 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3184 |
|
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3185 |
|
} |
3186 |
|
} |
3193 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3194 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3195 |
|
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2887 |
– |
CompletableFuture<?> h; |
3196 |
|
ThreadExecutor exec = new ThreadExecutor(); |
3197 |
|
|
3198 |
|
Runnable[] throwingActions = { |
3199 |
|
() -> CompletableFuture.supplyAsync(null), |
3200 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3201 |
< |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
3201 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3202 |
|
|
3203 |
|
() -> CompletableFuture.runAsync(null), |
3204 |
|
() -> CompletableFuture.runAsync(null, exec), |
3289 |
|
() -> CompletableFuture.anyOf(null, f), |
3290 |
|
|
3291 |
|
() -> f.obtrudeException(null), |
3292 |
+ |
|
3293 |
+ |
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3294 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3295 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null), |
3296 |
+ |
|
3297 |
+ |
() -> f.orTimeout(1L, null), |
3298 |
+ |
() -> f.completeOnTimeout(42, 1L, null), |
3299 |
+ |
|
3300 |
+ |
() -> CompletableFuture.failedFuture(null), |
3301 |
+ |
() -> CompletableFuture.failedStage(null), |
3302 |
|
}; |
3303 |
|
|
3304 |
|
assertThrows(NullPointerException.class, throwingActions); |
3313 |
|
assertSame(f, f.toCompletableFuture()); |
3314 |
|
} |
3315 |
|
|
3316 |
+ |
// jdk9 |
3317 |
+ |
|
3318 |
+ |
/** |
3319 |
+ |
* newIncompleteFuture returns an incomplete CompletableFuture |
3320 |
+ |
*/ |
3321 |
+ |
public void testNewIncompleteFuture() { |
3322 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3323 |
+ |
{ |
3324 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3325 |
+ |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3326 |
+ |
checkIncomplete(f); |
3327 |
+ |
checkIncomplete(g); |
3328 |
+ |
f.complete(v1); |
3329 |
+ |
checkCompletedNormally(f, v1); |
3330 |
+ |
checkIncomplete(g); |
3331 |
+ |
g.complete(v1); |
3332 |
+ |
checkCompletedNormally(g, v1); |
3333 |
+ |
assertSame(g.getClass(), CompletableFuture.class); |
3334 |
+ |
}} |
3335 |
+ |
|
3336 |
+ |
/** |
3337 |
+ |
* completedStage returns a completed CompletionStage |
3338 |
+ |
*/ |
3339 |
+ |
public void testCompletedStage() { |
3340 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3341 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3342 |
+ |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3343 |
+ |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3344 |
+ |
assertEquals(x.get(), 1); |
3345 |
+ |
assertNull(r.get()); |
3346 |
+ |
} |
3347 |
+ |
|
3348 |
+ |
/** |
3349 |
+ |
* defaultExecutor by default returns the commonPool if |
3350 |
+ |
* it supports more than one thread. |
3351 |
+ |
*/ |
3352 |
+ |
public void testDefaultExecutor() { |
3353 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3354 |
+ |
Executor e = f.defaultExecutor(); |
3355 |
+ |
Executor c = ForkJoinPool.commonPool(); |
3356 |
+ |
if (ForkJoinPool.getCommonPoolParallelism() > 1) |
3357 |
+ |
assertSame(e, c); |
3358 |
+ |
else |
3359 |
+ |
assertNotSame(e, c); |
3360 |
+ |
} |
3361 |
+ |
|
3362 |
+ |
/** |
3363 |
+ |
* failedFuture returns a CompletableFuture completed |
3364 |
+ |
* exceptionally with the given Exception |
3365 |
+ |
*/ |
3366 |
+ |
public void testFailedFuture() { |
3367 |
+ |
CFException ex = new CFException(); |
3368 |
+ |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3369 |
+ |
checkCompletedExceptionally(f, ex); |
3370 |
+ |
} |
3371 |
+ |
|
3372 |
+ |
/** |
3373 |
+ |
* failedFuture(null) throws NPE |
3374 |
+ |
*/ |
3375 |
+ |
public void testFailedFuture_null() { |
3376 |
+ |
try { |
3377 |
+ |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3378 |
+ |
shouldThrow(); |
3379 |
+ |
} catch (NullPointerException success) {} |
3380 |
+ |
} |
3381 |
+ |
|
3382 |
+ |
/** |
3383 |
+ |
* copy returns a CompletableFuture that is completed normally, |
3384 |
+ |
* with the same value, when source is. |
3385 |
+ |
*/ |
3386 |
+ |
public void testCopy() { |
3387 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3388 |
+ |
CompletableFuture<Integer> g = f.copy(); |
3389 |
+ |
checkIncomplete(f); |
3390 |
+ |
checkIncomplete(g); |
3391 |
+ |
f.complete(1); |
3392 |
+ |
checkCompletedNormally(f, 1); |
3393 |
+ |
checkCompletedNormally(g, 1); |
3394 |
+ |
} |
3395 |
+ |
|
3396 |
+ |
/** |
3397 |
+ |
* copy returns a CompletableFuture that is completed exceptionally |
3398 |
+ |
* when source is. |
3399 |
+ |
*/ |
3400 |
+ |
public void testCopy2() { |
3401 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3402 |
+ |
CompletableFuture<Integer> g = f.copy(); |
3403 |
+ |
checkIncomplete(f); |
3404 |
+ |
checkIncomplete(g); |
3405 |
+ |
CFException ex = new CFException(); |
3406 |
+ |
f.completeExceptionally(ex); |
3407 |
+ |
checkCompletedExceptionally(f, ex); |
3408 |
+ |
checkCompletedWithWrappedException(g, ex); |
3409 |
+ |
} |
3410 |
+ |
|
3411 |
+ |
/** |
3412 |
+ |
* minimalCompletionStage returns a CompletableFuture that is |
3413 |
+ |
* completed normally, with the same value, when source is. |
3414 |
+ |
*/ |
3415 |
+ |
public void testMinimalCompletionStage() { |
3416 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3417 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3418 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3419 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3420 |
+ |
checkIncomplete(f); |
3421 |
+ |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3422 |
+ |
f.complete(1); |
3423 |
+ |
checkCompletedNormally(f, 1); |
3424 |
+ |
assertEquals(x.get(), 1); |
3425 |
+ |
assertNull(r.get()); |
3426 |
+ |
} |
3427 |
+ |
|
3428 |
+ |
/** |
3429 |
+ |
* minimalCompletionStage returns a CompletableFuture that is |
3430 |
+ |
* completed exceptionally when source is. |
3431 |
+ |
*/ |
3432 |
+ |
public void testMinimalCompletionStage2() { |
3433 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3434 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3435 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3436 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3437 |
+ |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3438 |
+ |
checkIncomplete(f); |
3439 |
+ |
CFException ex = new CFException(); |
3440 |
+ |
f.completeExceptionally(ex); |
3441 |
+ |
checkCompletedExceptionally(f, ex); |
3442 |
+ |
assertEquals(x.get(), 0); |
3443 |
+ |
assertEquals(r.get().getCause(), ex); |
3444 |
+ |
} |
3445 |
+ |
|
3446 |
+ |
/** |
3447 |
+ |
* failedStage returns a CompletionStage completed |
3448 |
+ |
* exceptionally with the given Exception |
3449 |
+ |
*/ |
3450 |
+ |
public void testFailedStage() { |
3451 |
+ |
CFException ex = new CFException(); |
3452 |
+ |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3453 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3454 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3455 |
+ |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3456 |
+ |
assertEquals(x.get(), 0); |
3457 |
+ |
assertEquals(r.get(), ex); |
3458 |
+ |
} |
3459 |
+ |
|
3460 |
+ |
/** |
3461 |
+ |
* completeAsync completes with value of given supplier |
3462 |
+ |
*/ |
3463 |
+ |
public void testCompleteAsync() { |
3464 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3465 |
+ |
{ |
3466 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3467 |
+ |
f.completeAsync(() -> v1); |
3468 |
+ |
f.join(); |
3469 |
+ |
checkCompletedNormally(f, v1); |
3470 |
+ |
}} |
3471 |
+ |
|
3472 |
+ |
/** |
3473 |
+ |
* completeAsync completes exceptionally if given supplier throws |
3474 |
+ |
*/ |
3475 |
+ |
public void testCompleteAsync2() { |
3476 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3477 |
+ |
CFException ex = new CFException(); |
3478 |
+ |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3479 |
+ |
try { |
3480 |
+ |
f.join(); |
3481 |
+ |
shouldThrow(); |
3482 |
+ |
} catch (CompletionException success) {} |
3483 |
+ |
checkCompletedWithWrappedException(f, ex); |
3484 |
+ |
} |
3485 |
+ |
|
3486 |
+ |
/** |
3487 |
+ |
* completeAsync with given executor completes with value of given supplier |
3488 |
+ |
*/ |
3489 |
+ |
public void testCompleteAsync3() { |
3490 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3491 |
+ |
{ |
3492 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3493 |
+ |
ThreadExecutor executor = new ThreadExecutor(); |
3494 |
+ |
f.completeAsync(() -> v1, executor); |
3495 |
+ |
assertSame(v1, f.join()); |
3496 |
+ |
checkCompletedNormally(f, v1); |
3497 |
+ |
assertEquals(1, executor.count.get()); |
3498 |
+ |
}} |
3499 |
+ |
|
3500 |
+ |
/** |
3501 |
+ |
* completeAsync with given executor completes exceptionally if |
3502 |
+ |
* given supplier throws |
3503 |
+ |
*/ |
3504 |
+ |
public void testCompleteAsync4() { |
3505 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3506 |
+ |
CFException ex = new CFException(); |
3507 |
+ |
ThreadExecutor executor = new ThreadExecutor(); |
3508 |
+ |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3509 |
+ |
try { |
3510 |
+ |
f.join(); |
3511 |
+ |
shouldThrow(); |
3512 |
+ |
} catch (CompletionException success) {} |
3513 |
+ |
checkCompletedWithWrappedException(f, ex); |
3514 |
+ |
assertEquals(1, executor.count.get()); |
3515 |
+ |
} |
3516 |
+ |
|
3517 |
+ |
/** |
3518 |
+ |
* orTimeout completes with TimeoutException if not complete |
3519 |
+ |
*/ |
3520 |
+ |
public void testOrTimeout_timesOut() { |
3521 |
+ |
long timeoutMillis = timeoutMillis(); |
3522 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3523 |
+ |
long startTime = System.nanoTime(); |
3524 |
+ |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3525 |
+ |
checkCompletedWithTimeoutException(f); |
3526 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3527 |
+ |
} |
3528 |
+ |
|
3529 |
+ |
/** |
3530 |
+ |
* orTimeout completes normally if completed before timeout |
3531 |
+ |
*/ |
3532 |
+ |
public void testOrTimeout_completed() { |
3533 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3534 |
+ |
{ |
3535 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3536 |
+ |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3537 |
+ |
long startTime = System.nanoTime(); |
3538 |
+ |
f.complete(v1); |
3539 |
+ |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3540 |
+ |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3541 |
+ |
g.complete(v1); |
3542 |
+ |
checkCompletedNormally(f, v1); |
3543 |
+ |
checkCompletedNormally(g, v1); |
3544 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3545 |
+ |
}} |
3546 |
+ |
|
3547 |
+ |
/** |
3548 |
+ |
* completeOnTimeout completes with given value if not complete |
3549 |
+ |
*/ |
3550 |
+ |
public void testCompleteOnTimeout_timesOut() { |
3551 |
+ |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3552 |
+ |
() -> testCompleteOnTimeout_timesOut(null)); |
3553 |
+ |
} |
3554 |
+ |
|
3555 |
+ |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3556 |
+ |
long timeoutMillis = timeoutMillis(); |
3557 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3558 |
+ |
long startTime = System.nanoTime(); |
3559 |
+ |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3560 |
+ |
assertSame(v, f.join()); |
3561 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3562 |
+ |
f.complete(99); // should have no effect |
3563 |
+ |
checkCompletedNormally(f, v); |
3564 |
+ |
} |
3565 |
+ |
|
3566 |
+ |
/** |
3567 |
+ |
* completeOnTimeout has no effect if completed within timeout |
3568 |
+ |
*/ |
3569 |
+ |
public void testCompleteOnTimeout_completed() { |
3570 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3571 |
+ |
{ |
3572 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3573 |
+ |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3574 |
+ |
long startTime = System.nanoTime(); |
3575 |
+ |
f.complete(v1); |
3576 |
+ |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3577 |
+ |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3578 |
+ |
g.complete(v1); |
3579 |
+ |
checkCompletedNormally(f, v1); |
3580 |
+ |
checkCompletedNormally(g, v1); |
3581 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3582 |
+ |
}} |
3583 |
+ |
|
3584 |
+ |
/** |
3585 |
+ |
* delayedExecutor returns an executor that delays submission |
3586 |
+ |
*/ |
3587 |
+ |
public void testDelayedExecutor() { |
3588 |
+ |
testInParallel(() -> testDelayedExecutor(null, null), |
3589 |
+ |
() -> testDelayedExecutor(null, 1), |
3590 |
+ |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3591 |
+ |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3592 |
+ |
} |
3593 |
+ |
|
3594 |
+ |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3595 |
+ |
long timeoutMillis = timeoutMillis(); |
3596 |
+ |
// Use an "unreasonably long" long timeout to catch lingering threads |
3597 |
+ |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3598 |
+ |
final Executor delayer, longDelayer; |
3599 |
+ |
if (executor == null) { |
3600 |
+ |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3601 |
+ |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3602 |
+ |
} else { |
3603 |
+ |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3604 |
+ |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3605 |
+ |
} |
3606 |
+ |
long startTime = System.nanoTime(); |
3607 |
+ |
CompletableFuture<Integer> f = |
3608 |
+ |
CompletableFuture.supplyAsync(() -> v, delayer); |
3609 |
+ |
CompletableFuture<Integer> g = |
3610 |
+ |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3611 |
+ |
|
3612 |
+ |
assertNull(g.getNow(null)); |
3613 |
+ |
|
3614 |
+ |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3615 |
+ |
long millisElapsed = millisElapsedSince(startTime); |
3616 |
+ |
assertTrue(millisElapsed >= timeoutMillis); |
3617 |
+ |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3618 |
+ |
|
3619 |
+ |
checkCompletedNormally(f, v); |
3620 |
+ |
|
3621 |
+ |
checkIncomplete(g); |
3622 |
+ |
assertTrue(g.cancel(true)); |
3623 |
+ |
} |
3624 |
+ |
|
3625 |
+ |
//--- tests of implementation details; not part of official tck --- |
3626 |
+ |
|
3627 |
+ |
Object resultOf(CompletableFuture<?> f) { |
3628 |
+ |
try { |
3629 |
+ |
java.lang.reflect.Field resultField |
3630 |
+ |
= CompletableFuture.class.getDeclaredField("result"); |
3631 |
+ |
resultField.setAccessible(true); |
3632 |
+ |
return resultField.get(f); |
3633 |
+ |
} catch (Throwable t) { throw new AssertionError(t); } |
3634 |
+ |
} |
3635 |
+ |
|
3636 |
+ |
public void testExceptionPropagationReusesResultObject() { |
3637 |
+ |
if (!testImplementationDetails) return; |
3638 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
3639 |
+ |
{ |
3640 |
+ |
final CFException ex = new CFException(); |
3641 |
+ |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3642 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3643 |
+ |
|
3644 |
+ |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3645 |
+ |
= new ArrayList<>(); |
3646 |
+ |
|
3647 |
+ |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3648 |
+ |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3649 |
+ |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3650 |
+ |
|
3651 |
+ |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3652 |
+ |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3653 |
+ |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3654 |
+ |
|
3655 |
+ |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3656 |
+ |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3657 |
+ |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3658 |
+ |
|
3659 |
+ |
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3660 |
+ |
|
3661 |
+ |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3662 |
+ |
|
3663 |
+ |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3664 |
+ |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3665 |
+ |
|
3666 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3667 |
+ |
fun : funs) { |
3668 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3669 |
+ |
f.completeExceptionally(ex); |
3670 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3671 |
+ |
checkCompletedWithWrappedException(src, ex); |
3672 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3673 |
+ |
checkCompletedWithWrappedException(dep, ex); |
3674 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3675 |
+ |
} |
3676 |
+ |
|
3677 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3678 |
+ |
fun : funs) { |
3679 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3680 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3681 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3682 |
+ |
f.completeExceptionally(ex); |
3683 |
+ |
checkCompletedWithWrappedException(src, ex); |
3684 |
+ |
checkCompletedWithWrappedException(dep, ex); |
3685 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3686 |
+ |
} |
3687 |
+ |
|
3688 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3689 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3690 |
+ |
fun : funs) { |
3691 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3692 |
+ |
f.cancel(mayInterruptIfRunning); |
3693 |
+ |
checkCancelled(f); |
3694 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3695 |
+ |
checkCompletedWithWrappedCancellationException(src); |
3696 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3697 |
+ |
checkCompletedWithWrappedCancellationException(dep); |
3698 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3699 |
+ |
} |
3700 |
+ |
|
3701 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3702 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3703 |
+ |
fun : funs) { |
3704 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3705 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3706 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3707 |
+ |
f.cancel(mayInterruptIfRunning); |
3708 |
+ |
checkCancelled(f); |
3709 |
+ |
checkCompletedWithWrappedCancellationException(src); |
3710 |
+ |
checkCompletedWithWrappedCancellationException(dep); |
3711 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3712 |
+ |
} |
3713 |
+ |
}} |
3714 |
+ |
|
3715 |
+ |
/** |
3716 |
+ |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3717 |
+ |
*/ |
3718 |
+ |
public void testMinimalCompletionStage_minimality() { |
3719 |
+ |
if (!testImplementationDetails) return; |
3720 |
+ |
Function<Method, String> toSignature = |
3721 |
+ |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3722 |
+ |
Predicate<Method> isNotStatic = |
3723 |
+ |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3724 |
+ |
List<Method> minimalMethods = |
3725 |
+ |
Stream.of(Object.class, CompletionStage.class) |
3726 |
+ |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3727 |
+ |
.filter(isNotStatic) |
3728 |
+ |
.collect(Collectors.toList()); |
3729 |
+ |
// Methods from CompletableFuture permitted NOT to throw UOE |
3730 |
+ |
String[] signatureWhitelist = { |
3731 |
+ |
"newIncompleteFuture[]", |
3732 |
+ |
"defaultExecutor[]", |
3733 |
+ |
"minimalCompletionStage[]", |
3734 |
+ |
"copy[]", |
3735 |
+ |
}; |
3736 |
+ |
Set<String> permittedMethodSignatures = |
3737 |
+ |
Stream.concat(minimalMethods.stream().map(toSignature), |
3738 |
+ |
Stream.of(signatureWhitelist)) |
3739 |
+ |
.collect(Collectors.toSet()); |
3740 |
+ |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3741 |
+ |
.filter(isNotStatic) |
3742 |
+ |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3743 |
+ |
.collect(Collectors.toList()); |
3744 |
+ |
|
3745 |
+ |
CompletionStage<Integer> minimalStage = |
3746 |
+ |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3747 |
+ |
|
3748 |
+ |
List<Method> bugs = new ArrayList<>(); |
3749 |
+ |
for (Method method : allMethods) { |
3750 |
+ |
Class<?>[] parameterTypes = method.getParameterTypes(); |
3751 |
+ |
Object[] args = new Object[parameterTypes.length]; |
3752 |
+ |
// Manufacture boxed primitives for primitive params |
3753 |
+ |
for (int i = 0; i < args.length; i++) { |
3754 |
+ |
Class<?> type = parameterTypes[i]; |
3755 |
+ |
if (parameterTypes[i] == boolean.class) |
3756 |
+ |
args[i] = false; |
3757 |
+ |
else if (parameterTypes[i] == int.class) |
3758 |
+ |
args[i] = 0; |
3759 |
+ |
else if (parameterTypes[i] == long.class) |
3760 |
+ |
args[i] = 0L; |
3761 |
+ |
} |
3762 |
+ |
try { |
3763 |
+ |
method.invoke(minimalStage, args); |
3764 |
+ |
bugs.add(method); |
3765 |
+ |
} |
3766 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3767 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3768 |
+ |
bugs.add(method); |
3769 |
+ |
// expected.getCause().printStackTrace(); |
3770 |
+ |
} |
3771 |
+ |
} |
3772 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3773 |
+ |
} |
3774 |
+ |
if (!bugs.isEmpty()) |
3775 |
+ |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
3776 |
+ |
} |
3777 |
+ |
|
3778 |
+ |
static class Monad { |
3779 |
+ |
static class ZeroException extends RuntimeException { |
3780 |
+ |
public ZeroException() { super("monadic zero"); } |
3781 |
+ |
} |
3782 |
+ |
// "return", "unit" |
3783 |
+ |
static <T> CompletableFuture<T> unit(T value) { |
3784 |
+ |
return completedFuture(value); |
3785 |
+ |
} |
3786 |
+ |
// monadic zero ? |
3787 |
+ |
static <T> CompletableFuture<T> zero() { |
3788 |
+ |
return failedFuture(new ZeroException()); |
3789 |
+ |
} |
3790 |
+ |
// >=> |
3791 |
+ |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
3792 |
+ |
(Function<T, CompletableFuture<U>> f, |
3793 |
+ |
Function<U, CompletableFuture<V>> g) { |
3794 |
+ |
return (x) -> f.apply(x).thenCompose(g); |
3795 |
+ |
} |
3796 |
+ |
|
3797 |
+ |
static void assertZero(CompletableFuture<?> f) { |
3798 |
+ |
try { |
3799 |
+ |
f.getNow(null); |
3800 |
+ |
throw new AssertionFailedError("should throw"); |
3801 |
+ |
} catch (CompletionException success) { |
3802 |
+ |
assertTrue(success.getCause() instanceof ZeroException); |
3803 |
+ |
} |
3804 |
+ |
} |
3805 |
+ |
|
3806 |
+ |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
3807 |
+ |
CompletableFuture<T> g) { |
3808 |
+ |
T fval = null, gval = null; |
3809 |
+ |
Throwable fex = null, gex = null; |
3810 |
+ |
|
3811 |
+ |
try { fval = f.get(); } |
3812 |
+ |
catch (ExecutionException ex) { fex = ex.getCause(); } |
3813 |
+ |
catch (Throwable ex) { fex = ex; } |
3814 |
+ |
|
3815 |
+ |
try { gval = g.get(); } |
3816 |
+ |
catch (ExecutionException ex) { gex = ex.getCause(); } |
3817 |
+ |
catch (Throwable ex) { gex = ex; } |
3818 |
+ |
|
3819 |
+ |
if (fex != null || gex != null) |
3820 |
+ |
assertSame(fex.getClass(), gex.getClass()); |
3821 |
+ |
else |
3822 |
+ |
assertEquals(fval, gval); |
3823 |
+ |
} |
3824 |
+ |
|
3825 |
+ |
static class PlusFuture<T> extends CompletableFuture<T> { |
3826 |
+ |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
3827 |
+ |
} |
3828 |
+ |
|
3829 |
+ |
/** Implements "monadic plus". */ |
3830 |
+ |
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
3831 |
+ |
CompletableFuture<? extends T> g) { |
3832 |
+ |
PlusFuture<T> plus = new PlusFuture<T>(); |
3833 |
+ |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3834 |
+ |
try { |
3835 |
+ |
if (ex == null) { |
3836 |
+ |
if (plus.complete(result)) |
3837 |
+ |
if (plus.firstFailure.get() != null) |
3838 |
+ |
plus.firstFailure.set(null); |
3839 |
+ |
} |
3840 |
+ |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3841 |
+ |
if (plus.isDone()) |
3842 |
+ |
plus.firstFailure.set(null); |
3843 |
+ |
} |
3844 |
+ |
else { |
3845 |
+ |
// first failure has precedence |
3846 |
+ |
Throwable first = plus.firstFailure.getAndSet(null); |
3847 |
+ |
|
3848 |
+ |
// may fail with "Self-suppression not permitted" |
3849 |
+ |
try { first.addSuppressed(ex); } |
3850 |
+ |
catch (Exception ignored) {} |
3851 |
+ |
|
3852 |
+ |
plus.completeExceptionally(first); |
3853 |
+ |
} |
3854 |
+ |
} catch (Throwable unexpected) { |
3855 |
+ |
plus.completeExceptionally(unexpected); |
3856 |
+ |
} |
3857 |
+ |
}; |
3858 |
+ |
f.whenComplete(action); |
3859 |
+ |
g.whenComplete(action); |
3860 |
+ |
return plus; |
3861 |
+ |
} |
3862 |
+ |
} |
3863 |
+ |
|
3864 |
+ |
/** |
3865 |
+ |
* CompletableFuture is an additive monad - sort of. |
3866 |
+ |
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
3867 |
+ |
*/ |
3868 |
+ |
public void testAdditiveMonad() throws Throwable { |
3869 |
+ |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
3870 |
+ |
CompletableFuture<Long> zero = Monad.zero(); |
3871 |
+ |
|
3872 |
+ |
// Some mutually non-commutative functions |
3873 |
+ |
Function<Long, CompletableFuture<Long>> triple |
3874 |
+ |
= (x) -> Monad.unit(3 * x); |
3875 |
+ |
Function<Long, CompletableFuture<Long>> inc |
3876 |
+ |
= (x) -> Monad.unit(x + 1); |
3877 |
+ |
|
3878 |
+ |
// unit is a right identity: m >>= unit === m |
3879 |
+ |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
3880 |
+ |
inc.apply(5L)); |
3881 |
+ |
// unit is a left identity: (unit x) >>= f === f x |
3882 |
+ |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
3883 |
+ |
inc.apply(5L)); |
3884 |
+ |
|
3885 |
+ |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
3886 |
+ |
Monad.assertFutureEquals( |
3887 |
+ |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
3888 |
+ |
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
3889 |
+ |
|
3890 |
+ |
// The case for CompletableFuture as an additive monad is weaker... |
3891 |
+ |
|
3892 |
+ |
// zero is a monadic zero |
3893 |
+ |
Monad.assertZero(zero); |
3894 |
+ |
|
3895 |
+ |
// left zero: zero >>= f === zero |
3896 |
+ |
Monad.assertZero(zero.thenCompose(inc)); |
3897 |
+ |
// right zero: f >>= (\x -> zero) === zero |
3898 |
+ |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
3899 |
+ |
|
3900 |
+ |
// f plus zero === f |
3901 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3902 |
+ |
Monad.plus(Monad.unit(5L), zero)); |
3903 |
+ |
// zero plus f === f |
3904 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3905 |
+ |
Monad.plus(zero, Monad.unit(5L))); |
3906 |
+ |
// zero plus zero === zero |
3907 |
+ |
Monad.assertZero(Monad.plus(zero, zero)); |
3908 |
+ |
{ |
3909 |
+ |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
3910 |
+ |
Monad.unit(8L)); |
3911 |
+ |
// non-determinism |
3912 |
+ |
assertTrue(f.get() == 5L || f.get() == 8L); |
3913 |
+ |
} |
3914 |
+ |
|
3915 |
+ |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
3916 |
+ |
// f plus godot === f (doesn't wait for godot) |
3917 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3918 |
+ |
Monad.plus(Monad.unit(5L), godot)); |
3919 |
+ |
// godot plus f === f (doesn't wait for godot) |
3920 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3921 |
+ |
Monad.plus(godot, Monad.unit(5L))); |
3922 |
+ |
} |
3923 |
+ |
|
3924 |
+ |
// static <U> U join(CompletionStage<U> stage) { |
3925 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3926 |
+ |
// stage.whenComplete((v, ex) -> { |
3927 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3928 |
+ |
// }); |
3929 |
+ |
// return f.join(); |
3930 |
+ |
// } |
3931 |
+ |
|
3932 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
3933 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3934 |
+ |
// stage.whenComplete((v, ex) -> { |
3935 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3936 |
+ |
// }); |
3937 |
+ |
// return f.isDone(); |
3938 |
+ |
// } |
3939 |
+ |
|
3940 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
3941 |
+ |
// return stage.toCompletableFuture().copy().join(); |
3942 |
+ |
// } |
3943 |
+ |
|
3944 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
3945 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
3946 |
+ |
// } |
3947 |
+ |
|
3948 |
|
} |