5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
< |
import junit.framework.*; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
> |
|
11 |
> |
import java.lang.reflect.Method; |
12 |
> |
import java.lang.reflect.Modifier; |
13 |
> |
|
14 |
> |
import java.util.stream.Collectors; |
15 |
> |
import java.util.stream.Stream; |
16 |
> |
|
17 |
> |
import java.util.ArrayList; |
18 |
> |
import java.util.Arrays; |
19 |
> |
import java.util.List; |
20 |
> |
import java.util.Objects; |
21 |
> |
import java.util.Set; |
22 |
|
import java.util.concurrent.Callable; |
10 |
– |
import java.util.concurrent.Executor; |
11 |
– |
import java.util.concurrent.ExecutorService; |
12 |
– |
import java.util.concurrent.Executors; |
23 |
|
import java.util.concurrent.CancellationException; |
14 |
– |
import java.util.concurrent.CountDownLatch; |
15 |
– |
import java.util.concurrent.ExecutionException; |
16 |
– |
import java.util.concurrent.Future; |
24 |
|
import java.util.concurrent.CompletableFuture; |
25 |
|
import java.util.concurrent.CompletionException; |
26 |
|
import java.util.concurrent.CompletionStage; |
27 |
+ |
import java.util.concurrent.ExecutionException; |
28 |
+ |
import java.util.concurrent.Executor; |
29 |
|
import java.util.concurrent.ForkJoinPool; |
30 |
|
import java.util.concurrent.ForkJoinTask; |
31 |
|
import java.util.concurrent.TimeoutException; |
32 |
+ |
import java.util.concurrent.TimeUnit; |
33 |
|
import java.util.concurrent.atomic.AtomicInteger; |
34 |
< |
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; |
34 |
> |
import java.util.concurrent.atomic.AtomicReference; |
35 |
|
import java.util.function.BiConsumer; |
30 |
– |
import java.util.function.Function; |
36 |
|
import java.util.function.BiFunction; |
37 |
+ |
import java.util.function.Consumer; |
38 |
+ |
import java.util.function.Function; |
39 |
+ |
import java.util.function.Predicate; |
40 |
+ |
import java.util.function.Supplier; |
41 |
+ |
|
42 |
+ |
import junit.framework.Test; |
43 |
+ |
import junit.framework.TestSuite; |
44 |
|
|
45 |
|
public class CompletableFutureTest extends JSR166TestCase { |
46 |
|
|
47 |
|
public static void main(String[] args) { |
48 |
< |
junit.textui.TestRunner.run(suite()); |
48 |
> |
main(suite(), args); |
49 |
|
} |
50 |
|
public static Test suite() { |
51 |
|
return new TestSuite(CompletableFutureTest.class); |
56 |
|
void checkIncomplete(CompletableFuture<?> f) { |
57 |
|
assertFalse(f.isDone()); |
58 |
|
assertFalse(f.isCancelled()); |
59 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
59 |
> |
assertTrue(f.toString().contains("Not completed")); |
60 |
|
try { |
61 |
|
assertNull(f.getNow(null)); |
62 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
69 |
|
} |
70 |
|
|
71 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
72 |
< |
try { |
73 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
62 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
72 |
> |
checkTimedGet(f, value); |
73 |
> |
|
74 |
|
try { |
75 |
|
assertEquals(value, f.join()); |
76 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
86 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
87 |
|
} |
88 |
|
|
89 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
90 |
< |
try { |
91 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
92 |
< |
shouldThrow(); |
93 |
< |
} catch (ExecutionException success) { |
94 |
< |
assertTrue(success.getCause() instanceof CFException); |
95 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
96 |
< |
try { |
97 |
< |
f.join(); |
98 |
< |
shouldThrow(); |
99 |
< |
} catch (CompletionException success) { |
100 |
< |
assertTrue(success.getCause() instanceof CFException); |
101 |
< |
} |
102 |
< |
try { |
103 |
< |
f.getNow(null); |
104 |
< |
shouldThrow(); |
105 |
< |
} catch (CompletionException success) { |
95 |
< |
assertTrue(success.getCause() instanceof CFException); |
89 |
> |
/** |
90 |
> |
* Returns the "raw" internal exceptional completion of f, |
91 |
> |
* without any additional wrapping with CompletionException. |
92 |
> |
*/ |
93 |
> |
<U> Throwable exceptionalCompletion(CompletableFuture<U> f) { |
94 |
> |
// handle (and whenComplete) can distinguish between "direct" |
95 |
> |
// and "wrapped" exceptional completion |
96 |
> |
return f.handle((U u, Throwable t) -> t).join(); |
97 |
> |
} |
98 |
> |
|
99 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, |
100 |
> |
boolean wrapped, |
101 |
> |
Consumer<Throwable> checker) { |
102 |
> |
Throwable cause = exceptionalCompletion(f); |
103 |
> |
if (wrapped) { |
104 |
> |
assertTrue(cause instanceof CompletionException); |
105 |
> |
cause = cause.getCause(); |
106 |
|
} |
107 |
< |
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 |
< |
} |
107 |
> |
checker.accept(cause); |
108 |
|
|
109 |
< |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
109 |
< |
Throwable ex) { |
109 |
> |
long startTime = System.nanoTime(); |
110 |
|
try { |
111 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
112 |
|
shouldThrow(); |
113 |
|
} catch (ExecutionException success) { |
114 |
< |
assertSame(ex, success.getCause()); |
114 |
> |
assertSame(cause, success.getCause()); |
115 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
116 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
117 |
+ |
|
118 |
|
try { |
119 |
|
f.join(); |
120 |
|
shouldThrow(); |
121 |
|
} catch (CompletionException success) { |
122 |
< |
assertSame(ex, success.getCause()); |
123 |
< |
} |
122 |
> |
assertSame(cause, success.getCause()); |
123 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
124 |
> |
|
125 |
|
try { |
126 |
|
f.getNow(null); |
127 |
|
shouldThrow(); |
128 |
|
} catch (CompletionException success) { |
129 |
< |
assertSame(ex, success.getCause()); |
130 |
< |
} |
129 |
> |
assertSame(cause, success.getCause()); |
130 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
131 |
> |
|
132 |
|
try { |
133 |
|
f.get(); |
134 |
|
shouldThrow(); |
135 |
|
} catch (ExecutionException success) { |
136 |
< |
assertSame(ex, success.getCause()); |
136 |
> |
assertSame(cause, success.getCause()); |
137 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
138 |
< |
|
135 |
< |
assertTrue(f.isDone()); |
138 |
> |
|
139 |
|
assertFalse(f.isCancelled()); |
140 |
+ |
assertTrue(f.isDone()); |
141 |
+ |
assertTrue(f.isCompletedExceptionally()); |
142 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
143 |
|
} |
144 |
|
|
145 |
< |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
146 |
< |
Throwable ex) { |
147 |
< |
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); } |
145 |
> |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
146 |
> |
checkCompletedExceptionally(f, true, |
147 |
> |
(t) -> assertTrue(t instanceof CFException)); |
148 |
|
} |
149 |
|
|
150 |
< |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
151 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
152 |
< |
try { |
153 |
< |
CompletableFuture<Throwable> spy = f.handle |
154 |
< |
((U u, Throwable t) -> t); |
155 |
< |
assertSame(ex, spy.join()); |
156 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
150 |
> |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
151 |
> |
checkCompletedExceptionally(f, true, |
152 |
> |
(t) -> assertTrue(t instanceof CancellationException)); |
153 |
> |
} |
154 |
> |
|
155 |
> |
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
156 |
> |
checkCompletedExceptionally(f, false, |
157 |
> |
(t) -> assertTrue(t instanceof TimeoutException)); |
158 |
> |
} |
159 |
> |
|
160 |
> |
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
161 |
> |
Throwable ex) { |
162 |
> |
checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex)); |
163 |
> |
} |
164 |
> |
|
165 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
166 |
> |
checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex)); |
167 |
|
} |
168 |
|
|
169 |
|
void checkCancelled(CompletableFuture<?> f) { |
170 |
+ |
long startTime = System.nanoTime(); |
171 |
|
try { |
172 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
173 |
|
shouldThrow(); |
174 |
|
} catch (CancellationException success) { |
175 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
176 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
177 |
+ |
|
178 |
|
try { |
179 |
|
f.join(); |
180 |
|
shouldThrow(); |
188 |
|
shouldThrow(); |
189 |
|
} catch (CancellationException success) { |
190 |
|
} 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 |
– |
} |
191 |
|
|
192 |
< |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
193 |
< |
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); } |
192 |
> |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
193 |
> |
|
194 |
|
assertTrue(f.isDone()); |
211 |
– |
assertFalse(f.isCancelled()); |
195 |
|
assertTrue(f.isCompletedExceptionally()); |
196 |
+ |
assertTrue(f.isCancelled()); |
197 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
198 |
|
} |
199 |
|
|
211 |
|
* isCancelled, join, get, and getNow |
212 |
|
*/ |
213 |
|
public void testComplete() { |
214 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
215 |
+ |
{ |
216 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
217 |
|
checkIncomplete(f); |
218 |
< |
f.complete(one); |
219 |
< |
checkCompletedNormally(f, one); |
220 |
< |
} |
218 |
> |
assertTrue(f.complete(v1)); |
219 |
> |
assertFalse(f.complete(v1)); |
220 |
> |
checkCompletedNormally(f, v1); |
221 |
> |
}} |
222 |
|
|
223 |
|
/** |
224 |
|
* completeExceptionally completes exceptionally, as indicated by |
237 |
|
* methods isDone, isCancelled, join, get, and getNow |
238 |
|
*/ |
239 |
|
public void testCancel() { |
240 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
241 |
+ |
{ |
242 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
243 |
|
checkIncomplete(f); |
244 |
< |
assertTrue(f.cancel(true)); |
244 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
245 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
246 |
> |
assertTrue(f.cancel(!mayInterruptIfRunning)); |
247 |
|
checkCancelled(f); |
248 |
< |
} |
248 |
> |
}} |
249 |
|
|
250 |
|
/** |
251 |
|
* obtrudeValue forces completion with given value |
253 |
|
public void testObtrudeValue() { |
254 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
255 |
|
checkIncomplete(f); |
256 |
< |
f.complete(one); |
256 |
> |
assertTrue(f.complete(one)); |
257 |
|
checkCompletedNormally(f, one); |
258 |
|
f.obtrudeValue(three); |
259 |
|
checkCompletedNormally(f, three); |
280 |
|
CompletableFuture<Integer> f; |
281 |
|
|
282 |
|
f = new CompletableFuture<>(); |
283 |
< |
f.complete(v1); |
283 |
> |
assertTrue(f.complete(v1)); |
284 |
|
for (int i = 0; i < 2; i++) { |
285 |
|
f.obtrudeException(ex = new CFException()); |
286 |
|
checkCompletedExceptionally(f, ex); |
300 |
|
checkCompletedExceptionally(f, ex); |
301 |
|
f.completeExceptionally(new CFException()); |
302 |
|
checkCompletedExceptionally(f, ex); |
303 |
< |
f.complete(v1); |
303 |
> |
assertFalse(f.complete(v1)); |
304 |
|
checkCompletedExceptionally(f, ex); |
305 |
|
}} |
306 |
|
|
308 |
|
* getNumberOfDependents returns number of dependent tasks |
309 |
|
*/ |
310 |
|
public void testGetNumberOfDependents() { |
311 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
312 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
313 |
+ |
{ |
314 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
315 |
|
assertEquals(0, f.getNumberOfDependents()); |
316 |
< |
CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
316 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
317 |
|
assertEquals(1, f.getNumberOfDependents()); |
318 |
|
assertEquals(0, g.getNumberOfDependents()); |
319 |
< |
CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
319 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
320 |
|
assertEquals(2, f.getNumberOfDependents()); |
321 |
< |
f.complete(1); |
321 |
> |
assertEquals(0, h.getNumberOfDependents()); |
322 |
> |
assertTrue(f.complete(v1)); |
323 |
|
checkCompletedNormally(g, null); |
324 |
+ |
checkCompletedNormally(h, null); |
325 |
|
assertEquals(0, f.getNumberOfDependents()); |
326 |
|
assertEquals(0, g.getNumberOfDependents()); |
327 |
< |
} |
327 |
> |
assertEquals(0, h.getNumberOfDependents()); |
328 |
> |
}} |
329 |
|
|
330 |
|
/** |
331 |
|
* toString indicates current completion state |
336 |
|
f = new CompletableFuture<String>(); |
337 |
|
assertTrue(f.toString().contains("[Not completed]")); |
338 |
|
|
339 |
< |
f.complete("foo"); |
339 |
> |
assertTrue(f.complete("foo")); |
340 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
341 |
|
|
342 |
|
f = new CompletableFuture<String>(); |
343 |
< |
f.completeExceptionally(new IndexOutOfBoundsException()); |
343 |
> |
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
344 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
345 |
+ |
|
346 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
347 |
+ |
f = new CompletableFuture<String>(); |
348 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
349 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
350 |
+ |
} |
351 |
|
} |
352 |
|
|
353 |
|
/** |
515 |
|
} |
516 |
|
} |
517 |
|
|
515 |
– |
|
518 |
|
class CompletableFutureInc extends CheckedIntegerAction |
519 |
|
implements Function<Integer, CompletableFuture<Integer>> |
520 |
|
{ |
523 |
|
invoked(); |
524 |
|
value = x; |
525 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
526 |
< |
f.complete(inc(x)); |
526 |
> |
assertTrue(f.complete(inc(x))); |
527 |
|
return f; |
528 |
|
} |
529 |
|
} |
553 |
|
} |
554 |
|
} |
555 |
|
|
556 |
+ |
static final boolean defaultExecutorIsCommonPool |
557 |
+ |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
558 |
+ |
|
559 |
|
/** |
560 |
|
* Permits the testing of parallel code for the 3 different |
561 |
|
* execution modes without copy/pasting all the test methods. |
562 |
|
*/ |
563 |
|
enum ExecutionMode { |
564 |
< |
DEFAULT { |
564 |
> |
SYNC { |
565 |
|
public void checkExecutionMode() { |
566 |
|
assertFalse(ThreadExecutor.startedCurrentThread()); |
567 |
|
assertNull(ForkJoinTask.getPool()); |
637 |
|
|
638 |
|
ASYNC { |
639 |
|
public void checkExecutionMode() { |
640 |
< |
assertSame(ForkJoinPool.commonPool(), |
641 |
< |
ForkJoinTask.getPool()); |
640 |
> |
assertEquals(defaultExecutorIsCommonPool, |
641 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
642 |
|
} |
643 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
644 |
|
return CompletableFuture.runAsync(a); |
834 |
|
{ |
835 |
|
final AtomicInteger a = new AtomicInteger(0); |
836 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
837 |
< |
if (!createIncomplete) f.complete(v1); |
837 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
838 |
|
final CompletableFuture<Integer> g = f.exceptionally |
839 |
|
((Throwable t) -> { |
835 |
– |
// Should not be called |
840 |
|
a.getAndIncrement(); |
841 |
< |
throw new AssertionError(); |
841 |
> |
threadFail("should not be called"); |
842 |
> |
return null; // unreached |
843 |
|
}); |
844 |
< |
if (createIncomplete) f.complete(v1); |
844 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
845 |
|
|
846 |
|
checkCompletedNormally(g, v1); |
847 |
|
checkCompletedNormally(f, v1); |
848 |
|
assertEquals(0, a.get()); |
849 |
|
}} |
850 |
|
|
846 |
– |
|
851 |
|
/** |
852 |
|
* exceptionally action completes with function value on source |
853 |
|
* exception |
862 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
863 |
|
final CompletableFuture<Integer> g = f.exceptionally |
864 |
|
((Throwable t) -> { |
865 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
865 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
866 |
|
threadAssertSame(t, ex); |
867 |
|
a.getAndIncrement(); |
868 |
|
return v1; |
875 |
|
|
876 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
877 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
874 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
878 |
|
{ |
879 |
|
final AtomicInteger a = new AtomicInteger(0); |
880 |
|
final CFException ex1 = new CFException(); |
883 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
884 |
|
final CompletableFuture<Integer> g = f.exceptionally |
885 |
|
((Throwable t) -> { |
886 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
886 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
887 |
|
threadAssertSame(t, ex1); |
888 |
|
a.getAndIncrement(); |
889 |
|
throw ex2; |
895 |
|
}} |
896 |
|
|
897 |
|
/** |
898 |
+ |
* whenComplete action executes on normal completion, propagating |
899 |
+ |
* source result. |
900 |
+ |
*/ |
901 |
+ |
public void testWhenComplete_normalCompletion() { |
902 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
903 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
904 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
905 |
+ |
{ |
906 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
907 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
908 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
909 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
910 |
+ |
(f, |
911 |
+ |
(Integer x, Throwable t) -> { |
912 |
+ |
m.checkExecutionMode(); |
913 |
+ |
threadAssertSame(x, v1); |
914 |
+ |
threadAssertNull(t); |
915 |
+ |
a.getAndIncrement(); |
916 |
+ |
}); |
917 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
918 |
+ |
|
919 |
+ |
checkCompletedNormally(g, v1); |
920 |
+ |
checkCompletedNormally(f, v1); |
921 |
+ |
assertEquals(1, a.get()); |
922 |
+ |
}} |
923 |
+ |
|
924 |
+ |
/** |
925 |
+ |
* whenComplete action executes on exceptional completion, propagating |
926 |
+ |
* source result. |
927 |
+ |
*/ |
928 |
+ |
public void testWhenComplete_exceptionalCompletion() { |
929 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
930 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
931 |
+ |
{ |
932 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
933 |
+ |
final CFException ex = new CFException(); |
934 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
935 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
936 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
937 |
+ |
(f, |
938 |
+ |
(Integer x, Throwable t) -> { |
939 |
+ |
m.checkExecutionMode(); |
940 |
+ |
threadAssertNull(x); |
941 |
+ |
threadAssertSame(t, ex); |
942 |
+ |
a.getAndIncrement(); |
943 |
+ |
}); |
944 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
945 |
+ |
|
946 |
+ |
checkCompletedWithWrappedException(g, ex); |
947 |
+ |
checkCompletedExceptionally(f, ex); |
948 |
+ |
assertEquals(1, a.get()); |
949 |
+ |
}} |
950 |
+ |
|
951 |
+ |
/** |
952 |
+ |
* whenComplete action executes on cancelled source, propagating |
953 |
+ |
* CancellationException. |
954 |
+ |
*/ |
955 |
+ |
public void testWhenComplete_sourceCancelled() { |
956 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
957 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
958 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
959 |
+ |
{ |
960 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
961 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
962 |
+ |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
963 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
964 |
+ |
(f, |
965 |
+ |
(Integer x, Throwable t) -> { |
966 |
+ |
m.checkExecutionMode(); |
967 |
+ |
threadAssertNull(x); |
968 |
+ |
threadAssertTrue(t instanceof CancellationException); |
969 |
+ |
a.getAndIncrement(); |
970 |
+ |
}); |
971 |
+ |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
972 |
+ |
|
973 |
+ |
checkCompletedWithWrappedCancellationException(g); |
974 |
+ |
checkCancelled(f); |
975 |
+ |
assertEquals(1, a.get()); |
976 |
+ |
}} |
977 |
+ |
|
978 |
+ |
/** |
979 |
+ |
* If a whenComplete action throws an exception when triggered by |
980 |
+ |
* a normal completion, it completes exceptionally |
981 |
+ |
*/ |
982 |
+ |
public void testWhenComplete_actionFailed() { |
983 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
984 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
985 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
986 |
+ |
{ |
987 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
988 |
+ |
final CFException ex = new CFException(); |
989 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
990 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
991 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
992 |
+ |
(f, |
993 |
+ |
(Integer x, Throwable t) -> { |
994 |
+ |
m.checkExecutionMode(); |
995 |
+ |
threadAssertSame(x, v1); |
996 |
+ |
threadAssertNull(t); |
997 |
+ |
a.getAndIncrement(); |
998 |
+ |
throw ex; |
999 |
+ |
}); |
1000 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
1001 |
+ |
|
1002 |
+ |
checkCompletedWithWrappedException(g, ex); |
1003 |
+ |
checkCompletedNormally(f, v1); |
1004 |
+ |
assertEquals(1, a.get()); |
1005 |
+ |
}} |
1006 |
+ |
|
1007 |
+ |
/** |
1008 |
+ |
* If a whenComplete action throws an exception when triggered by |
1009 |
+ |
* a source completion that also throws an exception, the source |
1010 |
+ |
* exception takes precedence. |
1011 |
+ |
*/ |
1012 |
+ |
public void testWhenComplete_actionFailedSourceFailed() { |
1013 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
1014 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1015 |
+ |
{ |
1016 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
1017 |
+ |
final CFException ex1 = new CFException(); |
1018 |
+ |
final CFException ex2 = new CFException(); |
1019 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1020 |
+ |
|
1021 |
+ |
if (!createIncomplete) f.completeExceptionally(ex1); |
1022 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
1023 |
+ |
(f, |
1024 |
+ |
(Integer x, Throwable t) -> { |
1025 |
+ |
m.checkExecutionMode(); |
1026 |
+ |
threadAssertSame(t, ex1); |
1027 |
+ |
threadAssertNull(x); |
1028 |
+ |
a.getAndIncrement(); |
1029 |
+ |
throw ex2; |
1030 |
+ |
}); |
1031 |
+ |
if (createIncomplete) f.completeExceptionally(ex1); |
1032 |
+ |
|
1033 |
+ |
checkCompletedWithWrappedException(g, ex1); |
1034 |
+ |
checkCompletedExceptionally(f, ex1); |
1035 |
+ |
assertEquals(1, a.get()); |
1036 |
+ |
}} |
1037 |
+ |
|
1038 |
+ |
/** |
1039 |
|
* handle action completes normally with function value on normal |
1040 |
|
* completion of source |
1041 |
|
*/ |
1046 |
|
{ |
1047 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1048 |
|
final AtomicInteger a = new AtomicInteger(0); |
1049 |
< |
if (!createIncomplete) f.complete(v1); |
1049 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1050 |
|
final CompletableFuture<Integer> g = m.handle |
1051 |
|
(f, |
1052 |
|
(Integer x, Throwable t) -> { |
1056 |
|
a.getAndIncrement(); |
1057 |
|
return inc(v1); |
1058 |
|
}); |
1059 |
< |
if (createIncomplete) f.complete(v1); |
1059 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1060 |
|
|
1061 |
|
checkCompletedNormally(g, inc(v1)); |
1062 |
|
checkCompletedNormally(f, v1); |
1157 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1158 |
|
final AtomicInteger a = new AtomicInteger(0); |
1159 |
|
final CFException ex = new CFException(); |
1160 |
< |
if (!createIncomplete) f.complete(v1); |
1160 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1161 |
|
final CompletableFuture<Integer> g = m.handle |
1162 |
|
(f, |
1163 |
|
(Integer x, Throwable t) -> { |
1167 |
|
a.getAndIncrement(); |
1168 |
|
throw ex; |
1169 |
|
}); |
1170 |
< |
if (createIncomplete) f.complete(v1); |
1170 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1171 |
|
|
1172 |
|
checkCompletedWithWrappedException(g, ex); |
1173 |
|
checkCompletedNormally(f, v1); |
1248 |
|
*/ |
1249 |
|
public void testThenRun_normalCompletion() { |
1250 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1107 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1251 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1252 |
|
{ |
1253 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1254 |
< |
final Noop r = new Noop(m); |
1255 |
< |
if (!createIncomplete) f.complete(v1); |
1113 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1114 |
< |
if (createIncomplete) { |
1115 |
< |
checkIncomplete(g); |
1116 |
< |
f.complete(v1); |
1117 |
< |
} |
1254 |
> |
final Noop[] rs = new Noop[6]; |
1255 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1256 |
|
|
1257 |
< |
checkCompletedNormally(g, null); |
1257 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1258 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1259 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1260 |
> |
checkIncomplete(h0); |
1261 |
> |
checkIncomplete(h1); |
1262 |
> |
checkIncomplete(h2); |
1263 |
> |
assertTrue(f.complete(v1)); |
1264 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1265 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1266 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1267 |
> |
|
1268 |
> |
checkCompletedNormally(h0, null); |
1269 |
> |
checkCompletedNormally(h1, null); |
1270 |
> |
checkCompletedNormally(h2, null); |
1271 |
> |
checkCompletedNormally(h3, null); |
1272 |
> |
checkCompletedNormally(h4, null); |
1273 |
> |
checkCompletedNormally(h5, null); |
1274 |
|
checkCompletedNormally(f, v1); |
1275 |
< |
r.assertInvoked(); |
1275 |
> |
for (Noop r : rs) r.assertInvoked(); |
1276 |
|
}} |
1277 |
|
|
1278 |
|
/** |
1281 |
|
*/ |
1282 |
|
public void testThenRun_exceptionalCompletion() { |
1283 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1130 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1284 |
|
{ |
1285 |
|
final CFException ex = new CFException(); |
1286 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1287 |
< |
final Noop r = new Noop(m); |
1288 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1136 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1137 |
< |
if (createIncomplete) { |
1138 |
< |
checkIncomplete(g); |
1139 |
< |
f.completeExceptionally(ex); |
1140 |
< |
} |
1287 |
> |
final Noop[] rs = new Noop[6]; |
1288 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1289 |
|
|
1290 |
< |
checkCompletedWithWrappedException(g, ex); |
1290 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1291 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1292 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1293 |
> |
checkIncomplete(h0); |
1294 |
> |
checkIncomplete(h1); |
1295 |
> |
checkIncomplete(h2); |
1296 |
> |
assertTrue(f.completeExceptionally(ex)); |
1297 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1298 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1299 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1300 |
> |
|
1301 |
> |
checkCompletedWithWrappedException(h0, ex); |
1302 |
> |
checkCompletedWithWrappedException(h1, ex); |
1303 |
> |
checkCompletedWithWrappedException(h2, ex); |
1304 |
> |
checkCompletedWithWrappedException(h3, ex); |
1305 |
> |
checkCompletedWithWrappedException(h4, ex); |
1306 |
> |
checkCompletedWithWrappedException(h5, ex); |
1307 |
|
checkCompletedExceptionally(f, ex); |
1308 |
< |
r.assertNotInvoked(); |
1308 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1309 |
|
}} |
1310 |
|
|
1311 |
|
/** |
1313 |
|
*/ |
1314 |
|
public void testThenRun_sourceCancelled() { |
1315 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1152 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1316 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1317 |
|
{ |
1318 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1319 |
< |
final Noop r = new Noop(m); |
1320 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1158 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1159 |
< |
if (createIncomplete) { |
1160 |
< |
checkIncomplete(g); |
1161 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1162 |
< |
} |
1319 |
> |
final Noop[] rs = new Noop[6]; |
1320 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1321 |
|
|
1322 |
< |
checkCompletedWithWrappedCancellationException(g); |
1322 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1323 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1324 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1325 |
> |
checkIncomplete(h0); |
1326 |
> |
checkIncomplete(h1); |
1327 |
> |
checkIncomplete(h2); |
1328 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1329 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1330 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1331 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1332 |
> |
|
1333 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1334 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1335 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1336 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1337 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1338 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1339 |
|
checkCancelled(f); |
1340 |
< |
r.assertNotInvoked(); |
1340 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1341 |
|
}} |
1342 |
|
|
1343 |
|
/** |
1345 |
|
*/ |
1346 |
|
public void testThenRun_actionFailed() { |
1347 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1174 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1348 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1349 |
|
{ |
1350 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1351 |
< |
final FailingRunnable r = new FailingRunnable(m); |
1352 |
< |
if (!createIncomplete) f.complete(v1); |
1180 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1181 |
< |
if (createIncomplete) { |
1182 |
< |
checkIncomplete(g); |
1183 |
< |
f.complete(v1); |
1184 |
< |
} |
1351 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1352 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1353 |
|
|
1354 |
< |
checkCompletedWithWrappedCFException(g); |
1354 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1355 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1356 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1357 |
> |
assertTrue(f.complete(v1)); |
1358 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1359 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1360 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1361 |
> |
|
1362 |
> |
checkCompletedWithWrappedCFException(h0); |
1363 |
> |
checkCompletedWithWrappedCFException(h1); |
1364 |
> |
checkCompletedWithWrappedCFException(h2); |
1365 |
> |
checkCompletedWithWrappedCFException(h3); |
1366 |
> |
checkCompletedWithWrappedCFException(h4); |
1367 |
> |
checkCompletedWithWrappedCFException(h5); |
1368 |
|
checkCompletedNormally(f, v1); |
1369 |
|
}} |
1370 |
|
|
1373 |
|
*/ |
1374 |
|
public void testThenApply_normalCompletion() { |
1375 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1195 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1376 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1377 |
|
{ |
1378 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1379 |
< |
final IncFunction r = new IncFunction(m); |
1380 |
< |
if (!createIncomplete) f.complete(v1); |
1201 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1202 |
< |
if (createIncomplete) { |
1203 |
< |
checkIncomplete(g); |
1204 |
< |
f.complete(v1); |
1205 |
< |
} |
1379 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1380 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1381 |
|
|
1382 |
< |
checkCompletedNormally(g, inc(v1)); |
1382 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1383 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1384 |
> |
checkIncomplete(h0); |
1385 |
> |
checkIncomplete(h1); |
1386 |
> |
assertTrue(f.complete(v1)); |
1387 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1388 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1389 |
> |
|
1390 |
> |
checkCompletedNormally(h0, inc(v1)); |
1391 |
> |
checkCompletedNormally(h1, inc(v1)); |
1392 |
> |
checkCompletedNormally(h2, inc(v1)); |
1393 |
> |
checkCompletedNormally(h3, inc(v1)); |
1394 |
|
checkCompletedNormally(f, v1); |
1395 |
< |
r.assertValue(inc(v1)); |
1395 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1396 |
|
}} |
1397 |
|
|
1398 |
|
/** |
1401 |
|
*/ |
1402 |
|
public void testThenApply_exceptionalCompletion() { |
1403 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1218 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1404 |
|
{ |
1405 |
|
final CFException ex = new CFException(); |
1406 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1407 |
< |
final IncFunction r = new IncFunction(m); |
1408 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1224 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1225 |
< |
if (createIncomplete) { |
1226 |
< |
checkIncomplete(g); |
1227 |
< |
f.completeExceptionally(ex); |
1228 |
< |
} |
1407 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1408 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1409 |
|
|
1410 |
< |
checkCompletedWithWrappedException(g, ex); |
1410 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1411 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1412 |
> |
assertTrue(f.completeExceptionally(ex)); |
1413 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1414 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1415 |
> |
|
1416 |
> |
checkCompletedWithWrappedException(h0, ex); |
1417 |
> |
checkCompletedWithWrappedException(h1, ex); |
1418 |
> |
checkCompletedWithWrappedException(h2, ex); |
1419 |
> |
checkCompletedWithWrappedException(h3, ex); |
1420 |
|
checkCompletedExceptionally(f, ex); |
1421 |
< |
r.assertNotInvoked(); |
1421 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1422 |
|
}} |
1423 |
|
|
1424 |
|
/** |
1426 |
|
*/ |
1427 |
|
public void testThenApply_sourceCancelled() { |
1428 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1240 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1429 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1430 |
|
{ |
1431 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1432 |
< |
final IncFunction r = new IncFunction(m); |
1433 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1246 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1247 |
< |
if (createIncomplete) { |
1248 |
< |
checkIncomplete(g); |
1249 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1250 |
< |
} |
1432 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1433 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1434 |
|
|
1435 |
< |
checkCompletedWithWrappedCancellationException(g); |
1435 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1436 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1437 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1438 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1439 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1440 |
> |
|
1441 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1442 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1443 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1444 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1445 |
|
checkCancelled(f); |
1446 |
< |
r.assertNotInvoked(); |
1446 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1447 |
|
}} |
1448 |
|
|
1449 |
|
/** |
1451 |
|
*/ |
1452 |
|
public void testThenApply_actionFailed() { |
1453 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1262 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1454 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1455 |
|
{ |
1456 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1457 |
< |
final FailingFunction r = new FailingFunction(m); |
1458 |
< |
if (!createIncomplete) f.complete(v1); |
1268 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1269 |
< |
if (createIncomplete) { |
1270 |
< |
checkIncomplete(g); |
1271 |
< |
f.complete(v1); |
1272 |
< |
} |
1457 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1458 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1459 |
|
|
1460 |
< |
checkCompletedWithWrappedCFException(g); |
1460 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1461 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1462 |
> |
assertTrue(f.complete(v1)); |
1463 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1464 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1465 |
> |
|
1466 |
> |
checkCompletedWithWrappedCFException(h0); |
1467 |
> |
checkCompletedWithWrappedCFException(h1); |
1468 |
> |
checkCompletedWithWrappedCFException(h2); |
1469 |
> |
checkCompletedWithWrappedCFException(h3); |
1470 |
|
checkCompletedNormally(f, v1); |
1471 |
|
}} |
1472 |
|
|
1475 |
|
*/ |
1476 |
|
public void testThenAccept_normalCompletion() { |
1477 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1283 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1478 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1479 |
|
{ |
1480 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1481 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1482 |
< |
if (!createIncomplete) f.complete(v1); |
1289 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1290 |
< |
if (createIncomplete) { |
1291 |
< |
checkIncomplete(g); |
1292 |
< |
f.complete(v1); |
1293 |
< |
} |
1481 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1482 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1483 |
|
|
1484 |
< |
checkCompletedNormally(g, null); |
1485 |
< |
r.assertValue(v1); |
1484 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1485 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1486 |
> |
checkIncomplete(h0); |
1487 |
> |
checkIncomplete(h1); |
1488 |
> |
assertTrue(f.complete(v1)); |
1489 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1490 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1491 |
> |
|
1492 |
> |
checkCompletedNormally(h0, null); |
1493 |
> |
checkCompletedNormally(h1, null); |
1494 |
> |
checkCompletedNormally(h2, null); |
1495 |
> |
checkCompletedNormally(h3, null); |
1496 |
|
checkCompletedNormally(f, v1); |
1497 |
+ |
for (NoopConsumer r : rs) r.assertValue(v1); |
1498 |
|
}} |
1499 |
|
|
1500 |
|
/** |
1503 |
|
*/ |
1504 |
|
public void testThenAccept_exceptionalCompletion() { |
1505 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1306 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1506 |
|
{ |
1507 |
|
final CFException ex = new CFException(); |
1508 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1509 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1510 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1312 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1313 |
< |
if (createIncomplete) { |
1314 |
< |
checkIncomplete(g); |
1315 |
< |
f.completeExceptionally(ex); |
1316 |
< |
} |
1509 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1510 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1511 |
|
|
1512 |
< |
checkCompletedWithWrappedException(g, ex); |
1512 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1513 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1514 |
> |
assertTrue(f.completeExceptionally(ex)); |
1515 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1516 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1517 |
> |
|
1518 |
> |
checkCompletedWithWrappedException(h0, ex); |
1519 |
> |
checkCompletedWithWrappedException(h1, ex); |
1520 |
> |
checkCompletedWithWrappedException(h2, ex); |
1521 |
> |
checkCompletedWithWrappedException(h3, ex); |
1522 |
|
checkCompletedExceptionally(f, ex); |
1523 |
< |
r.assertNotInvoked(); |
1523 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1524 |
|
}} |
1525 |
|
|
1526 |
|
/** |
1528 |
|
*/ |
1529 |
|
public void testThenAccept_sourceCancelled() { |
1530 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1328 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1531 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1532 |
|
{ |
1533 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1534 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1535 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1334 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1335 |
< |
if (createIncomplete) { |
1336 |
< |
checkIncomplete(g); |
1337 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1338 |
< |
} |
1534 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1535 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1536 |
|
|
1537 |
< |
checkCompletedWithWrappedCancellationException(g); |
1537 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1538 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1539 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1540 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1541 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1542 |
> |
|
1543 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1544 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1545 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1546 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1547 |
|
checkCancelled(f); |
1548 |
< |
r.assertNotInvoked(); |
1548 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1549 |
|
}} |
1550 |
|
|
1551 |
|
/** |
1553 |
|
*/ |
1554 |
|
public void testThenAccept_actionFailed() { |
1555 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1350 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1556 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1557 |
|
{ |
1558 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1559 |
< |
final FailingConsumer r = new FailingConsumer(m); |
1560 |
< |
if (!createIncomplete) f.complete(v1); |
1356 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1357 |
< |
if (createIncomplete) { |
1358 |
< |
checkIncomplete(g); |
1359 |
< |
f.complete(v1); |
1360 |
< |
} |
1559 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1560 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1561 |
|
|
1562 |
< |
checkCompletedWithWrappedCFException(g); |
1562 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1563 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1564 |
> |
assertTrue(f.complete(v1)); |
1565 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1566 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1567 |
> |
|
1568 |
> |
checkCompletedWithWrappedCFException(h0); |
1569 |
> |
checkCompletedWithWrappedCFException(h1); |
1570 |
> |
checkCompletedWithWrappedCFException(h2); |
1571 |
> |
checkCompletedWithWrappedCFException(h3); |
1572 |
|
checkCompletedNormally(f, v1); |
1573 |
|
}} |
1574 |
|
|
1578 |
|
*/ |
1579 |
|
public void testThenCombine_normalCompletion() { |
1580 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1372 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1581 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1582 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1583 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1584 |
|
{ |
1585 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1586 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1587 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1587 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1588 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1589 |
|
|
1590 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1591 |
< |
if (!createIncomplete) |
1592 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1593 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1594 |
< |
if (createIncomplete) { |
1595 |
< |
checkIncomplete(h); |
1596 |
< |
r.assertNotInvoked(); |
1597 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1598 |
< |
} |
1590 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1591 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1592 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1593 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1594 |
> |
|
1595 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1596 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1597 |
> |
assertTrue(fst.complete(w1)); |
1598 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1599 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1600 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1601 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1602 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1603 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1604 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1605 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1606 |
> |
assertTrue(snd.complete(w2)); |
1607 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1608 |
> |
|
1609 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1610 |
> |
checkCompletedNormally(h2, subtract(v1, v2)); |
1611 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1612 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1613 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1614 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1615 |
|
|
1391 |
– |
checkCompletedNormally(h, subtract(v1, v2)); |
1616 |
|
checkCompletedNormally(f, v1); |
1617 |
|
checkCompletedNormally(g, v2); |
1394 |
– |
r.assertValue(subtract(v1, v2)); |
1618 |
|
}} |
1619 |
|
|
1620 |
|
/** |
1621 |
|
* thenCombine result completes exceptionally after exceptional |
1622 |
|
* completion of either source |
1623 |
|
*/ |
1624 |
< |
public void testThenCombine_exceptionalCompletion() { |
1624 |
> |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
1625 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1403 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1626 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1627 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1628 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1629 |
|
{ |
1630 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1631 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1632 |
|
final CFException ex = new CFException(); |
1633 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1634 |
< |
|
1635 |
< |
(fFirst ? f : g).complete(v1); |
1636 |
< |
if (!createIncomplete) |
1637 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1638 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1639 |
< |
if (createIncomplete) { |
1640 |
< |
checkIncomplete(h); |
1641 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1642 |
< |
} |
1633 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1634 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1635 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1636 |
> |
|
1637 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1638 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1639 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1640 |
> |
() -> fst.completeExceptionally(ex) : |
1641 |
> |
() -> fst.complete(v1); |
1642 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1643 |
> |
() -> snd.complete(v1) : |
1644 |
> |
() -> snd.completeExceptionally(ex); |
1645 |
> |
|
1646 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1647 |
> |
assertTrue(complete1.call()); |
1648 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1649 |
> |
checkIncomplete(h1); |
1650 |
> |
checkIncomplete(h2); |
1651 |
> |
assertTrue(complete2.call()); |
1652 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1653 |
|
|
1654 |
< |
checkCompletedWithWrappedException(h, ex); |
1655 |
< |
r.assertNotInvoked(); |
1656 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1657 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1654 |
> |
checkCompletedWithWrappedException(h1, ex); |
1655 |
> |
checkCompletedWithWrappedException(h2, ex); |
1656 |
> |
checkCompletedWithWrappedException(h3, ex); |
1657 |
> |
r1.assertNotInvoked(); |
1658 |
> |
r2.assertNotInvoked(); |
1659 |
> |
r3.assertNotInvoked(); |
1660 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1661 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1662 |
|
}} |
1663 |
|
|
1664 |
|
/** |
1665 |
|
* thenCombine result completes exceptionally if either source cancelled |
1666 |
|
*/ |
1667 |
< |
public void testThenCombine_sourceCancelled() { |
1667 |
> |
public void testThenCombine_sourceCancelled() throws Throwable { |
1668 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1669 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1433 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1670 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1671 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1672 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1673 |
|
{ |
1674 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1675 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1676 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1676 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1677 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1678 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1679 |
|
|
1680 |
< |
(fFirst ? f : g).complete(v1); |
1681 |
< |
if (!createIncomplete) |
1682 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1683 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1684 |
< |
if (createIncomplete) { |
1685 |
< |
checkIncomplete(h); |
1686 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1687 |
< |
} |
1680 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1681 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1682 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1683 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1684 |
> |
() -> fst.complete(v1); |
1685 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1686 |
> |
() -> snd.complete(v1) : |
1687 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1688 |
|
|
1689 |
< |
checkCompletedWithWrappedCancellationException(h); |
1690 |
< |
checkCancelled(!fFirst ? f : g); |
1691 |
< |
r.assertNotInvoked(); |
1692 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1689 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1690 |
> |
assertTrue(complete1.call()); |
1691 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1692 |
> |
checkIncomplete(h1); |
1693 |
> |
checkIncomplete(h2); |
1694 |
> |
assertTrue(complete2.call()); |
1695 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1696 |
> |
|
1697 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1698 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1699 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1700 |
> |
r1.assertNotInvoked(); |
1701 |
> |
r2.assertNotInvoked(); |
1702 |
> |
r3.assertNotInvoked(); |
1703 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1704 |
> |
checkCancelled(failFirst ? fst : snd); |
1705 |
|
}} |
1706 |
|
|
1707 |
|
/** |
1715 |
|
{ |
1716 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1717 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1718 |
< |
final FailingBiFunction r = new FailingBiFunction(m); |
1719 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1718 |
> |
final FailingBiFunction r1 = new FailingBiFunction(m); |
1719 |
> |
final FailingBiFunction r2 = new FailingBiFunction(m); |
1720 |
> |
final FailingBiFunction r3 = new FailingBiFunction(m); |
1721 |
> |
|
1722 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1723 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1724 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1725 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1726 |
> |
|
1727 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1728 |
> |
assertTrue(fst.complete(w1)); |
1729 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1730 |
> |
assertTrue(snd.complete(w2)); |
1731 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1732 |
|
|
1733 |
< |
if (fFirst) { |
1734 |
< |
f.complete(v1); |
1735 |
< |
g.complete(v2); |
1736 |
< |
} else { |
1737 |
< |
g.complete(v2); |
1738 |
< |
f.complete(v1); |
1476 |
< |
} |
1477 |
< |
|
1478 |
< |
checkCompletedWithWrappedCFException(h); |
1733 |
> |
checkCompletedWithWrappedCFException(h1); |
1734 |
> |
checkCompletedWithWrappedCFException(h2); |
1735 |
> |
checkCompletedWithWrappedCFException(h3); |
1736 |
> |
r1.assertInvoked(); |
1737 |
> |
r2.assertInvoked(); |
1738 |
> |
r3.assertInvoked(); |
1739 |
|
checkCompletedNormally(f, v1); |
1740 |
|
checkCompletedNormally(g, v2); |
1741 |
|
}} |
1746 |
|
*/ |
1747 |
|
public void testThenAcceptBoth_normalCompletion() { |
1748 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1489 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1749 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1750 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1751 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1752 |
|
{ |
1753 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1754 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1755 |
< |
final SubtractAction r = new SubtractAction(m); |
1756 |
< |
|
1757 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1758 |
< |
if (!createIncomplete) |
1759 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1760 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1761 |
< |
if (createIncomplete) { |
1762 |
< |
checkIncomplete(h); |
1763 |
< |
r.assertNotInvoked(); |
1764 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1765 |
< |
} |
1755 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1756 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1757 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1758 |
> |
|
1759 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1760 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1761 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1762 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1763 |
> |
|
1764 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1765 |
> |
assertTrue(fst.complete(w1)); |
1766 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1767 |
> |
checkIncomplete(h1); |
1768 |
> |
checkIncomplete(h2); |
1769 |
> |
r1.assertNotInvoked(); |
1770 |
> |
r2.assertNotInvoked(); |
1771 |
> |
assertTrue(snd.complete(w2)); |
1772 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1773 |
|
|
1774 |
< |
checkCompletedNormally(h, null); |
1775 |
< |
r.assertValue(subtract(v1, v2)); |
1774 |
> |
checkCompletedNormally(h1, null); |
1775 |
> |
checkCompletedNormally(h2, null); |
1776 |
> |
checkCompletedNormally(h3, null); |
1777 |
> |
r1.assertValue(subtract(v1, v2)); |
1778 |
> |
r2.assertValue(subtract(v1, v2)); |
1779 |
> |
r3.assertValue(subtract(v1, v2)); |
1780 |
|
checkCompletedNormally(f, v1); |
1781 |
|
checkCompletedNormally(g, v2); |
1782 |
|
}} |
1785 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1786 |
|
* completion of either source |
1787 |
|
*/ |
1788 |
< |
public void testThenAcceptBoth_exceptionalCompletion() { |
1788 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1789 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1520 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1790 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1791 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1792 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1793 |
|
{ |
1794 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1795 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1796 |
|
final CFException ex = new CFException(); |
1797 |
< |
final SubtractAction r = new SubtractAction(m); |
1798 |
< |
|
1799 |
< |
(fFirst ? f : g).complete(v1); |
1800 |
< |
if (!createIncomplete) |
1801 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1802 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1803 |
< |
if (createIncomplete) { |
1804 |
< |
checkIncomplete(h); |
1805 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1806 |
< |
} |
1797 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1798 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1799 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1800 |
> |
|
1801 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1802 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1803 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1804 |
> |
() -> fst.completeExceptionally(ex) : |
1805 |
> |
() -> fst.complete(v1); |
1806 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1807 |
> |
() -> snd.complete(v1) : |
1808 |
> |
() -> snd.completeExceptionally(ex); |
1809 |
> |
|
1810 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1811 |
> |
assertTrue(complete1.call()); |
1812 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1813 |
> |
checkIncomplete(h1); |
1814 |
> |
checkIncomplete(h2); |
1815 |
> |
assertTrue(complete2.call()); |
1816 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1817 |
|
|
1818 |
< |
checkCompletedWithWrappedException(h, ex); |
1819 |
< |
r.assertNotInvoked(); |
1820 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1821 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1818 |
> |
checkCompletedWithWrappedException(h1, ex); |
1819 |
> |
checkCompletedWithWrappedException(h2, ex); |
1820 |
> |
checkCompletedWithWrappedException(h3, ex); |
1821 |
> |
r1.assertNotInvoked(); |
1822 |
> |
r2.assertNotInvoked(); |
1823 |
> |
r3.assertNotInvoked(); |
1824 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1825 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1826 |
|
}} |
1827 |
|
|
1828 |
|
/** |
1829 |
|
* thenAcceptBoth result completes exceptionally if either source cancelled |
1830 |
|
*/ |
1831 |
< |
public void testThenAcceptBoth_sourceCancelled() { |
1831 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1832 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1833 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1550 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1834 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1835 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1836 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1837 |
|
{ |
1838 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1839 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1840 |
< |
final SubtractAction r = new SubtractAction(m); |
1840 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1841 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1842 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1843 |
|
|
1844 |
< |
(fFirst ? f : g).complete(v1); |
1845 |
< |
if (!createIncomplete) |
1846 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1847 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1848 |
< |
if (createIncomplete) { |
1849 |
< |
checkIncomplete(h); |
1850 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1851 |
< |
} |
1844 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1845 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1846 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1847 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1848 |
> |
() -> fst.complete(v1); |
1849 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1850 |
> |
() -> snd.complete(v1) : |
1851 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1852 |
|
|
1853 |
< |
checkCompletedWithWrappedCancellationException(h); |
1854 |
< |
checkCancelled(!fFirst ? f : g); |
1855 |
< |
r.assertNotInvoked(); |
1856 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1853 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1854 |
> |
assertTrue(complete1.call()); |
1855 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1856 |
> |
checkIncomplete(h1); |
1857 |
> |
checkIncomplete(h2); |
1858 |
> |
assertTrue(complete2.call()); |
1859 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1860 |
> |
|
1861 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1862 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1863 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1864 |
> |
r1.assertNotInvoked(); |
1865 |
> |
r2.assertNotInvoked(); |
1866 |
> |
r3.assertNotInvoked(); |
1867 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1868 |
> |
checkCancelled(failFirst ? fst : snd); |
1869 |
|
}} |
1870 |
|
|
1871 |
|
/** |
1879 |
|
{ |
1880 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1881 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1882 |
< |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1883 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1882 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1883 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1884 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1885 |
> |
|
1886 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1887 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1888 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1889 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1890 |
> |
|
1891 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1892 |
> |
assertTrue(fst.complete(w1)); |
1893 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1894 |
> |
assertTrue(snd.complete(w2)); |
1895 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1896 |
|
|
1897 |
< |
if (fFirst) { |
1898 |
< |
f.complete(v1); |
1899 |
< |
g.complete(v2); |
1900 |
< |
} else { |
1901 |
< |
g.complete(v2); |
1902 |
< |
f.complete(v1); |
1593 |
< |
} |
1594 |
< |
|
1595 |
< |
checkCompletedWithWrappedCFException(h); |
1897 |
> |
checkCompletedWithWrappedCFException(h1); |
1898 |
> |
checkCompletedWithWrappedCFException(h2); |
1899 |
> |
checkCompletedWithWrappedCFException(h3); |
1900 |
> |
r1.assertInvoked(); |
1901 |
> |
r2.assertInvoked(); |
1902 |
> |
r3.assertInvoked(); |
1903 |
|
checkCompletedNormally(f, v1); |
1904 |
|
checkCompletedNormally(g, v2); |
1905 |
|
}} |
1910 |
|
*/ |
1911 |
|
public void testRunAfterBoth_normalCompletion() { |
1912 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1606 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1913 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1914 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1915 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1916 |
|
{ |
1917 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1918 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1919 |
< |
final Noop r = new Noop(m); |
1920 |
< |
|
1921 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1922 |
< |
if (!createIncomplete) |
1923 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1924 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1925 |
< |
if (createIncomplete) { |
1926 |
< |
checkIncomplete(h); |
1927 |
< |
r.assertNotInvoked(); |
1928 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1929 |
< |
} |
1919 |
> |
final Noop r1 = new Noop(m); |
1920 |
> |
final Noop r2 = new Noop(m); |
1921 |
> |
final Noop r3 = new Noop(m); |
1922 |
> |
|
1923 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1924 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1925 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1926 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1927 |
> |
|
1928 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1929 |
> |
assertTrue(fst.complete(w1)); |
1930 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1931 |
> |
checkIncomplete(h1); |
1932 |
> |
checkIncomplete(h2); |
1933 |
> |
r1.assertNotInvoked(); |
1934 |
> |
r2.assertNotInvoked(); |
1935 |
> |
assertTrue(snd.complete(w2)); |
1936 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1937 |
|
|
1938 |
< |
checkCompletedNormally(h, null); |
1939 |
< |
r.assertInvoked(); |
1938 |
> |
checkCompletedNormally(h1, null); |
1939 |
> |
checkCompletedNormally(h2, null); |
1940 |
> |
checkCompletedNormally(h3, null); |
1941 |
> |
r1.assertInvoked(); |
1942 |
> |
r2.assertInvoked(); |
1943 |
> |
r3.assertInvoked(); |
1944 |
|
checkCompletedNormally(f, v1); |
1945 |
|
checkCompletedNormally(g, v2); |
1946 |
|
}} |
1949 |
|
* runAfterBoth result completes exceptionally after exceptional |
1950 |
|
* completion of either source |
1951 |
|
*/ |
1952 |
< |
public void testRunAfterBoth_exceptionalCompletion() { |
1952 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
1953 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1637 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1954 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1955 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1956 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1957 |
|
{ |
1958 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1959 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1960 |
|
final CFException ex = new CFException(); |
1961 |
< |
final Noop r = new Noop(m); |
1962 |
< |
|
1963 |
< |
(fFirst ? f : g).complete(v1); |
1964 |
< |
if (!createIncomplete) |
1965 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1966 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1967 |
< |
if (createIncomplete) { |
1968 |
< |
checkIncomplete(h); |
1969 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1970 |
< |
} |
1961 |
> |
final Noop r1 = new Noop(m); |
1962 |
> |
final Noop r2 = new Noop(m); |
1963 |
> |
final Noop r3 = new Noop(m); |
1964 |
> |
|
1965 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1966 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1967 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1968 |
> |
() -> fst.completeExceptionally(ex) : |
1969 |
> |
() -> fst.complete(v1); |
1970 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1971 |
> |
() -> snd.complete(v1) : |
1972 |
> |
() -> snd.completeExceptionally(ex); |
1973 |
> |
|
1974 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1975 |
> |
assertTrue(complete1.call()); |
1976 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1977 |
> |
checkIncomplete(h1); |
1978 |
> |
checkIncomplete(h2); |
1979 |
> |
assertTrue(complete2.call()); |
1980 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1981 |
|
|
1982 |
< |
checkCompletedWithWrappedException(h, ex); |
1983 |
< |
r.assertNotInvoked(); |
1984 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1985 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1982 |
> |
checkCompletedWithWrappedException(h1, ex); |
1983 |
> |
checkCompletedWithWrappedException(h2, ex); |
1984 |
> |
checkCompletedWithWrappedException(h3, ex); |
1985 |
> |
r1.assertNotInvoked(); |
1986 |
> |
r2.assertNotInvoked(); |
1987 |
> |
r3.assertNotInvoked(); |
1988 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1989 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1990 |
|
}} |
1991 |
|
|
1992 |
|
/** |
1993 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
1994 |
|
*/ |
1995 |
< |
public void testRunAfterBoth_sourceCancelled() { |
1995 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
1996 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1997 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1667 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1998 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1999 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
2000 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2001 |
|
{ |
2002 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2003 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2004 |
< |
final Noop r = new Noop(m); |
2004 |
> |
final Noop r1 = new Noop(m); |
2005 |
> |
final Noop r2 = new Noop(m); |
2006 |
> |
final Noop r3 = new Noop(m); |
2007 |
|
|
2008 |
+ |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2009 |
+ |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2010 |
+ |
final Callable<Boolean> complete1 = failFirst ? |
2011 |
+ |
() -> fst.cancel(mayInterruptIfRunning) : |
2012 |
+ |
() -> fst.complete(v1); |
2013 |
+ |
final Callable<Boolean> complete2 = failFirst ? |
2014 |
+ |
() -> snd.complete(v1) : |
2015 |
+ |
() -> snd.cancel(mayInterruptIfRunning); |
2016 |
|
|
2017 |
< |
(fFirst ? f : g).complete(v1); |
2018 |
< |
if (!createIncomplete) |
2019 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2020 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2021 |
< |
if (createIncomplete) { |
2022 |
< |
checkIncomplete(h); |
2023 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1683 |
< |
} |
2017 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2018 |
> |
assertTrue(complete1.call()); |
2019 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2020 |
> |
checkIncomplete(h1); |
2021 |
> |
checkIncomplete(h2); |
2022 |
> |
assertTrue(complete2.call()); |
2023 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2024 |
|
|
2025 |
< |
checkCompletedWithWrappedCancellationException(h); |
2026 |
< |
checkCancelled(!fFirst ? f : g); |
2027 |
< |
r.assertNotInvoked(); |
2028 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2025 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2026 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2027 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2028 |
> |
r1.assertNotInvoked(); |
2029 |
> |
r2.assertNotInvoked(); |
2030 |
> |
r3.assertNotInvoked(); |
2031 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2032 |
> |
checkCancelled(failFirst ? fst : snd); |
2033 |
|
}} |
2034 |
|
|
2035 |
|
/** |
2045 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2046 |
|
final FailingRunnable r1 = new FailingRunnable(m); |
2047 |
|
final FailingRunnable r2 = new FailingRunnable(m); |
2048 |
+ |
final FailingRunnable r3 = new FailingRunnable(m); |
2049 |
|
|
2050 |
< |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2051 |
< |
if (fFirst) { |
2052 |
< |
f.complete(v1); |
2053 |
< |
g.complete(v2); |
2054 |
< |
} else { |
2055 |
< |
g.complete(v2); |
2056 |
< |
f.complete(v1); |
2057 |
< |
} |
2058 |
< |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2050 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2051 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2052 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2053 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2054 |
> |
|
2055 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2056 |
> |
assertTrue(fst.complete(w1)); |
2057 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2058 |
> |
assertTrue(snd.complete(w2)); |
2059 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2060 |
|
|
2061 |
|
checkCompletedWithWrappedCFException(h1); |
2062 |
|
checkCompletedWithWrappedCFException(h2); |
2063 |
+ |
checkCompletedWithWrappedCFException(h3); |
2064 |
+ |
r1.assertInvoked(); |
2065 |
+ |
r2.assertInvoked(); |
2066 |
+ |
r3.assertInvoked(); |
2067 |
|
checkCompletedNormally(f, v1); |
2068 |
|
checkCompletedNormally(g, v2); |
2069 |
|
}} |
2186 |
|
|
2187 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2188 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2189 |
< |
if (fFirst) { |
2190 |
< |
f.complete(v1); |
1841 |
< |
g.completeExceptionally(ex); |
1842 |
< |
} else { |
1843 |
< |
g.completeExceptionally(ex); |
1844 |
< |
f.complete(v1); |
1845 |
< |
} |
2189 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2190 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2191 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2192 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2193 |
|
|
2293 |
|
|
2294 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2295 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2296 |
< |
if (fFirst) { |
2297 |
< |
f.complete(v1); |
1953 |
< |
g.cancel(mayInterruptIfRunning); |
1954 |
< |
} else { |
1955 |
< |
g.cancel(mayInterruptIfRunning); |
1956 |
< |
f.complete(v1); |
1957 |
< |
} |
2296 |
> |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2297 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2298 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2299 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2300 |
|
|
2496 |
|
|
2497 |
|
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2498 |
|
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2499 |
< |
if (fFirst) { |
2500 |
< |
f.complete(v1); |
2161 |
< |
g.completeExceptionally(ex); |
2162 |
< |
} else { |
2163 |
< |
g.completeExceptionally(ex); |
2164 |
< |
f.complete(v1); |
2165 |
< |
} |
2499 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2500 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2501 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2502 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2503 |
|
|
2700 |
|
checkIncomplete(h1); |
2701 |
|
rs[0].assertNotInvoked(); |
2702 |
|
rs[1].assertNotInvoked(); |
2703 |
< |
f.completeExceptionally(ex); |
2703 |
> |
assertTrue(f.completeExceptionally(ex)); |
2704 |
|
checkCompletedWithWrappedException(h0, ex); |
2705 |
|
checkCompletedWithWrappedException(h1, ex); |
2706 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2708 |
|
checkCompletedWithWrappedException(h2, ex); |
2709 |
|
checkCompletedWithWrappedException(h3, ex); |
2710 |
|
|
2711 |
< |
g.complete(v1); |
2711 |
> |
assertTrue(g.complete(v1)); |
2712 |
|
|
2713 |
|
// unspecified behavior - both source completions available |
2714 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2751 |
|
|
2752 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2753 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2754 |
< |
if (fFirst) { |
2755 |
< |
f.complete(v1); |
2421 |
< |
g.completeExceptionally(ex); |
2422 |
< |
} else { |
2423 |
< |
g.completeExceptionally(ex); |
2424 |
< |
f.complete(v1); |
2425 |
< |
} |
2754 |
> |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2755 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2756 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2757 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2758 |
|
|
2817 |
|
checkCompletedWithWrappedCancellationException(h2); |
2818 |
|
checkCompletedWithWrappedCancellationException(h3); |
2819 |
|
|
2820 |
< |
g.complete(v1); |
2820 |
> |
assertTrue(g.complete(v1)); |
2821 |
|
|
2822 |
|
// unspecified behavior - both source completions available |
2823 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2861 |
|
|
2862 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2863 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2864 |
< |
f.complete(v1); |
2864 |
> |
assertTrue(f.complete(v1)); |
2865 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2866 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2867 |
|
checkCompletedWithWrappedCFException(h0); |
2869 |
|
checkCompletedWithWrappedCFException(h2); |
2870 |
|
checkCompletedWithWrappedCFException(h3); |
2871 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2872 |
< |
g.complete(v2); |
2872 |
> |
assertTrue(g.complete(v2)); |
2873 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2874 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2875 |
|
checkCompletedWithWrappedCFException(h4); |
2890 |
|
{ |
2891 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2892 |
|
final CompletableFutureInc r = new CompletableFutureInc(m); |
2893 |
< |
if (!createIncomplete) f.complete(v1); |
2893 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2894 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2895 |
< |
if (createIncomplete) f.complete(v1); |
2895 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2896 |
|
|
2897 |
|
checkCompletedNormally(g, inc(v1)); |
2898 |
|
checkCompletedNormally(f, v1); |
2930 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2931 |
|
final FailingCompletableFutureFunction r |
2932 |
|
= new FailingCompletableFutureFunction(m); |
2933 |
< |
if (!createIncomplete) f.complete(v1); |
2933 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2934 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2935 |
< |
if (createIncomplete) f.complete(v1); |
2935 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2936 |
|
|
2937 |
|
checkCompletedWithWrappedCFException(g); |
2938 |
|
checkCompletedNormally(f, v1); |
2959 |
|
checkCancelled(f); |
2960 |
|
}} |
2961 |
|
|
2962 |
+ |
/** |
2963 |
+ |
* thenCompose result completes exceptionally if the result of the action does |
2964 |
+ |
*/ |
2965 |
+ |
public void testThenCompose_actionReturnsFailingFuture() { |
2966 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
2967 |
+ |
for (int order = 0; order < 6; order++) |
2968 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
2969 |
+ |
{ |
2970 |
+ |
final CFException ex = new CFException(); |
2971 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2972 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2973 |
+ |
final CompletableFuture<Integer> h; |
2974 |
+ |
// Test all permutations of orders |
2975 |
+ |
switch (order) { |
2976 |
+ |
case 0: |
2977 |
+ |
assertTrue(f.complete(v1)); |
2978 |
+ |
assertTrue(g.completeExceptionally(ex)); |
2979 |
+ |
h = m.thenCompose(f, (x -> g)); |
2980 |
+ |
break; |
2981 |
+ |
case 1: |
2982 |
+ |
assertTrue(f.complete(v1)); |
2983 |
+ |
h = m.thenCompose(f, (x -> g)); |
2984 |
+ |
assertTrue(g.completeExceptionally(ex)); |
2985 |
+ |
break; |
2986 |
+ |
case 2: |
2987 |
+ |
assertTrue(g.completeExceptionally(ex)); |
2988 |
+ |
assertTrue(f.complete(v1)); |
2989 |
+ |
h = m.thenCompose(f, (x -> g)); |
2990 |
+ |
break; |
2991 |
+ |
case 3: |
2992 |
+ |
assertTrue(g.completeExceptionally(ex)); |
2993 |
+ |
h = m.thenCompose(f, (x -> g)); |
2994 |
+ |
assertTrue(f.complete(v1)); |
2995 |
+ |
break; |
2996 |
+ |
case 4: |
2997 |
+ |
h = m.thenCompose(f, (x -> g)); |
2998 |
+ |
assertTrue(f.complete(v1)); |
2999 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3000 |
+ |
break; |
3001 |
+ |
case 5: |
3002 |
+ |
h = m.thenCompose(f, (x -> g)); |
3003 |
+ |
assertTrue(f.complete(v1)); |
3004 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3005 |
+ |
break; |
3006 |
+ |
default: throw new AssertionError(); |
3007 |
+ |
} |
3008 |
+ |
|
3009 |
+ |
checkCompletedExceptionally(g, ex); |
3010 |
+ |
checkCompletedWithWrappedException(h, ex); |
3011 |
+ |
checkCompletedNormally(f, v1); |
3012 |
+ |
}} |
3013 |
+ |
|
3014 |
|
// other static methods |
3015 |
|
|
3016 |
|
/** |
3027 |
|
* when all components complete normally |
3028 |
|
*/ |
3029 |
|
public void testAllOf_normal() throws Exception { |
3030 |
< |
for (int k = 1; k < 20; ++k) { |
3030 |
> |
for (int k = 1; k < 10; k++) { |
3031 |
|
CompletableFuture<Integer>[] fs |
3032 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3033 |
< |
for (int i = 0; i < k; ++i) |
3033 |
> |
for (int i = 0; i < k; i++) |
3034 |
|
fs[i] = new CompletableFuture<>(); |
3035 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3036 |
< |
for (int i = 0; i < k; ++i) { |
3036 |
> |
for (int i = 0; i < k; i++) { |
3037 |
|
checkIncomplete(f); |
3038 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3039 |
|
fs[i].complete(one); |
3044 |
|
} |
3045 |
|
|
3046 |
|
public void testAllOf_backwards() throws Exception { |
3047 |
< |
for (int k = 1; k < 20; ++k) { |
3047 |
> |
for (int k = 1; k < 10; k++) { |
3048 |
|
CompletableFuture<Integer>[] fs |
3049 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3050 |
< |
for (int i = 0; i < k; ++i) |
3050 |
> |
for (int i = 0; i < k; i++) |
3051 |
|
fs[i] = new CompletableFuture<>(); |
3052 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3053 |
|
for (int i = k - 1; i >= 0; i--) { |
3060 |
|
} |
3061 |
|
} |
3062 |
|
|
3063 |
+ |
public void testAllOf_exceptional() throws Exception { |
3064 |
+ |
for (int k = 1; k < 10; k++) { |
3065 |
+ |
CompletableFuture<Integer>[] fs |
3066 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3067 |
+ |
CFException ex = new CFException(); |
3068 |
+ |
for (int i = 0; i < k; i++) |
3069 |
+ |
fs[i] = new CompletableFuture<>(); |
3070 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3071 |
+ |
for (int i = 0; i < k; i++) { |
3072 |
+ |
checkIncomplete(f); |
3073 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3074 |
+ |
if (i != k / 2) { |
3075 |
+ |
fs[i].complete(i); |
3076 |
+ |
checkCompletedNormally(fs[i], i); |
3077 |
+ |
} else { |
3078 |
+ |
fs[i].completeExceptionally(ex); |
3079 |
+ |
checkCompletedExceptionally(fs[i], ex); |
3080 |
+ |
} |
3081 |
+ |
} |
3082 |
+ |
checkCompletedWithWrappedException(f, ex); |
3083 |
+ |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3084 |
+ |
} |
3085 |
+ |
} |
3086 |
+ |
|
3087 |
|
/** |
3088 |
|
* anyOf(no component futures) returns an incomplete future |
3089 |
|
*/ |
3090 |
|
public void testAnyOf_empty() throws Exception { |
3091 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3092 |
+ |
{ |
3093 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3094 |
|
checkIncomplete(f); |
3095 |
< |
} |
3095 |
> |
|
3096 |
> |
f.complete(v1); |
3097 |
> |
checkCompletedNormally(f, v1); |
3098 |
> |
}} |
3099 |
|
|
3100 |
|
/** |
3101 |
|
* anyOf returns a future completed normally with a value when |
3102 |
|
* a component future does |
3103 |
|
*/ |
3104 |
|
public void testAnyOf_normal() throws Exception { |
3105 |
< |
for (int k = 0; k < 10; ++k) { |
3105 |
> |
for (int k = 0; k < 10; k++) { |
3106 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3107 |
< |
for (int i = 0; i < k; ++i) |
3107 |
> |
for (int i = 0; i < k; i++) |
3108 |
|
fs[i] = new CompletableFuture<>(); |
3109 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3110 |
|
checkIncomplete(f); |
3111 |
< |
for (int i = 0; i < k; ++i) { |
3112 |
< |
fs[i].complete(one); |
3113 |
< |
checkCompletedNormally(f, one); |
3114 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
3111 |
> |
for (int i = 0; i < k; i++) { |
3112 |
> |
fs[i].complete(i); |
3113 |
> |
checkCompletedNormally(f, 0); |
3114 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3115 |
> |
assertTrue(0 <= x && x <= i); |
3116 |
> |
} |
3117 |
> |
} |
3118 |
> |
} |
3119 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3120 |
> |
for (int k = 0; k < 10; k++) { |
3121 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
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 = k - 1; i >= 0; i--) { |
3127 |
> |
fs[i].complete(i); |
3128 |
> |
checkCompletedNormally(f, k - 1); |
3129 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3130 |
> |
assertTrue(i <= x && x <= k - 1); |
3131 |
|
} |
3132 |
|
} |
3133 |
|
} |
3136 |
|
* anyOf result completes exceptionally when any component does. |
3137 |
|
*/ |
3138 |
|
public void testAnyOf_exceptional() throws Exception { |
3139 |
< |
for (int k = 0; k < 10; ++k) { |
3139 |
> |
for (int k = 0; k < 10; k++) { |
3140 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3141 |
< |
for (int i = 0; i < k; ++i) |
3141 |
> |
CFException[] exs = new CFException[k]; |
3142 |
> |
for (int i = 0; i < k; i++) { |
3143 |
|
fs[i] = new CompletableFuture<>(); |
3144 |
+ |
exs[i] = new CFException(); |
3145 |
+ |
} |
3146 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3147 |
|
checkIncomplete(f); |
3148 |
< |
for (int i = 0; i < k; ++i) { |
3149 |
< |
fs[i].completeExceptionally(new CFException()); |
3150 |
< |
checkCompletedWithWrappedCFException(f); |
3148 |
> |
for (int i = 0; i < k; i++) { |
3149 |
> |
fs[i].completeExceptionally(exs[i]); |
3150 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3151 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3152 |
> |
} |
3153 |
> |
} |
3154 |
> |
} |
3155 |
> |
|
3156 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3157 |
> |
for (int k = 0; k < 10; k++) { |
3158 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3159 |
> |
CFException[] exs = new CFException[k]; |
3160 |
> |
for (int i = 0; i < k; i++) { |
3161 |
> |
fs[i] = new CompletableFuture<>(); |
3162 |
> |
exs[i] = new CFException(); |
3163 |
> |
} |
3164 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3165 |
> |
checkIncomplete(f); |
3166 |
> |
for (int i = k - 1; i >= 0; i--) { |
3167 |
> |
fs[i].completeExceptionally(exs[i]); |
3168 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3169 |
|
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3170 |
|
} |
3171 |
|
} |
3178 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3179 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3180 |
|
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2733 |
– |
CompletableFuture<?> h; |
3181 |
|
ThreadExecutor exec = new ThreadExecutor(); |
3182 |
|
|
3183 |
|
Runnable[] throwingActions = { |
3184 |
|
() -> CompletableFuture.supplyAsync(null), |
3185 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3186 |
< |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
3186 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3187 |
|
|
3188 |
|
() -> CompletableFuture.runAsync(null), |
3189 |
|
() -> CompletableFuture.runAsync(null, exec), |
3274 |
|
() -> CompletableFuture.anyOf(null, f), |
3275 |
|
|
3276 |
|
() -> f.obtrudeException(null), |
3277 |
+ |
|
3278 |
+ |
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3279 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3280 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null), |
3281 |
+ |
|
3282 |
+ |
() -> f.orTimeout(1L, null), |
3283 |
+ |
() -> f.completeOnTimeout(42, 1L, null), |
3284 |
+ |
|
3285 |
+ |
() -> CompletableFuture.failedFuture(null), |
3286 |
+ |
() -> CompletableFuture.failedStage(null), |
3287 |
|
}; |
3288 |
|
|
3289 |
|
assertThrows(NullPointerException.class, throwingActions); |
3298 |
|
assertSame(f, f.toCompletableFuture()); |
3299 |
|
} |
3300 |
|
|
3301 |
+ |
// jdk9 |
3302 |
+ |
|
3303 |
|
/** |
3304 |
< |
* whenComplete action executes on normal completion, propagating |
2846 |
< |
* source result. |
3304 |
> |
* newIncompleteFuture returns an incomplete CompletableFuture |
3305 |
|
*/ |
3306 |
< |
public void testWhenComplete_normalCompletion1() { |
2849 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2850 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
3306 |
> |
public void testNewIncompleteFuture() { |
3307 |
|
for (Integer v1 : new Integer[] { 1, null }) |
3308 |
|
{ |
3309 |
< |
final AtomicInteger a = new AtomicInteger(0); |
3310 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3311 |
< |
if (!createIncomplete) f.complete(v1); |
3312 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
3313 |
< |
(f, |
2858 |
< |
(Integer x, Throwable t) -> { |
2859 |
< |
m.checkExecutionMode(); |
2860 |
< |
threadAssertSame(x, v1); |
2861 |
< |
threadAssertNull(t); |
2862 |
< |
a.getAndIncrement(); |
2863 |
< |
}); |
2864 |
< |
if (createIncomplete) f.complete(v1); |
2865 |
< |
|
2866 |
< |
checkCompletedNormally(g, v1); |
3309 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3310 |
> |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3311 |
> |
checkIncomplete(f); |
3312 |
> |
checkIncomplete(g); |
3313 |
> |
f.complete(v1); |
3314 |
|
checkCompletedNormally(f, v1); |
3315 |
< |
assertEquals(1, a.get()); |
3315 |
> |
checkIncomplete(g); |
3316 |
> |
g.complete(v1); |
3317 |
> |
checkCompletedNormally(g, v1); |
3318 |
> |
assertSame(g.getClass(), CompletableFuture.class); |
3319 |
|
}} |
3320 |
|
|
3321 |
|
/** |
3322 |
< |
* whenComplete action executes on exceptional completion, propagating |
2873 |
< |
* source result. |
3322 |
> |
* completedStage returns a completed CompletionStage |
3323 |
|
*/ |
3324 |
< |
public void testWhenComplete_exceptionalCompletion() { |
3325 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
3326 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
3327 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
3328 |
< |
{ |
3329 |
< |
final AtomicInteger a = new AtomicInteger(0); |
3330 |
< |
final CFException ex = new CFException(); |
3331 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3332 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
3333 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
3334 |
< |
(f, |
3335 |
< |
(Integer x, Throwable t) -> { |
3336 |
< |
m.checkExecutionMode(); |
3337 |
< |
threadAssertNull(x); |
3338 |
< |
threadAssertSame(t, ex); |
3339 |
< |
a.getAndIncrement(); |
3340 |
< |
}); |
3341 |
< |
if (createIncomplete) f.completeExceptionally(ex); |
3324 |
> |
public void testCompletedStage() { |
3325 |
> |
AtomicInteger x = new AtomicInteger(0); |
3326 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3327 |
> |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3328 |
> |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3329 |
> |
assertEquals(x.get(), 1); |
3330 |
> |
assertNull(r.get()); |
3331 |
> |
} |
3332 |
> |
|
3333 |
> |
/** |
3334 |
> |
* defaultExecutor by default returns the commonPool if |
3335 |
> |
* it supports more than one thread. |
3336 |
> |
*/ |
3337 |
> |
public void testDefaultExecutor() { |
3338 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3339 |
> |
Executor e = f.defaultExecutor(); |
3340 |
> |
Executor c = ForkJoinPool.commonPool(); |
3341 |
> |
if (ForkJoinPool.getCommonPoolParallelism() > 1) |
3342 |
> |
assertSame(e, c); |
3343 |
> |
else |
3344 |
> |
assertNotSame(e, c); |
3345 |
> |
} |
3346 |
> |
|
3347 |
> |
/** |
3348 |
> |
* failedFuture returns a CompletableFuture completed |
3349 |
> |
* exceptionally with the given Exception |
3350 |
> |
*/ |
3351 |
> |
public void testFailedFuture() { |
3352 |
> |
CFException ex = new CFException(); |
3353 |
> |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3354 |
> |
checkCompletedExceptionally(f, ex); |
3355 |
> |
} |
3356 |
> |
|
3357 |
> |
/** |
3358 |
> |
* failedFuture(null) throws NPE |
3359 |
> |
*/ |
3360 |
> |
public void testFailedFuture_null() { |
3361 |
> |
try { |
3362 |
> |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3363 |
> |
shouldThrow(); |
3364 |
> |
} catch (NullPointerException success) {} |
3365 |
> |
} |
3366 |
> |
|
3367 |
> |
/** |
3368 |
> |
* copy returns a CompletableFuture that is completed normally, |
3369 |
> |
* with the same value, when source is. |
3370 |
> |
*/ |
3371 |
> |
public void testCopy() { |
3372 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3373 |
> |
CompletableFuture<Integer> g = f.copy(); |
3374 |
> |
checkIncomplete(f); |
3375 |
> |
checkIncomplete(g); |
3376 |
> |
f.complete(1); |
3377 |
> |
checkCompletedNormally(f, 1); |
3378 |
> |
checkCompletedNormally(g, 1); |
3379 |
> |
} |
3380 |
> |
|
3381 |
> |
/** |
3382 |
> |
* copy returns a CompletableFuture that is completed exceptionally |
3383 |
> |
* when source is. |
3384 |
> |
*/ |
3385 |
> |
public void testCopy2() { |
3386 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3387 |
> |
CompletableFuture<Integer> g = f.copy(); |
3388 |
> |
checkIncomplete(f); |
3389 |
> |
checkIncomplete(g); |
3390 |
> |
CFException ex = new CFException(); |
3391 |
> |
f.completeExceptionally(ex); |
3392 |
|
checkCompletedExceptionally(f, ex); |
3393 |
|
checkCompletedWithWrappedException(g, ex); |
3394 |
< |
assertEquals(1, a.get()); |
3394 |
> |
} |
3395 |
> |
|
3396 |
> |
/** |
3397 |
> |
* minimalCompletionStage returns a CompletableFuture that is |
3398 |
> |
* completed normally, with the same value, when source is. |
3399 |
> |
*/ |
3400 |
> |
public void testMinimalCompletionStage() { |
3401 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3402 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3403 |
> |
AtomicInteger x = new AtomicInteger(0); |
3404 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3405 |
> |
checkIncomplete(f); |
3406 |
> |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3407 |
> |
f.complete(1); |
3408 |
> |
checkCompletedNormally(f, 1); |
3409 |
> |
assertEquals(x.get(), 1); |
3410 |
> |
assertNull(r.get()); |
3411 |
> |
} |
3412 |
> |
|
3413 |
> |
/** |
3414 |
> |
* minimalCompletionStage returns a CompletableFuture that is |
3415 |
> |
* completed exceptionally when source is. |
3416 |
> |
*/ |
3417 |
> |
public void testMinimalCompletionStage2() { |
3418 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3419 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3420 |
> |
AtomicInteger x = new AtomicInteger(0); |
3421 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3422 |
> |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3423 |
> |
checkIncomplete(f); |
3424 |
> |
CFException ex = new CFException(); |
3425 |
> |
f.completeExceptionally(ex); |
3426 |
> |
checkCompletedExceptionally(f, ex); |
3427 |
> |
assertEquals(x.get(), 0); |
3428 |
> |
assertEquals(r.get().getCause(), ex); |
3429 |
> |
} |
3430 |
> |
|
3431 |
> |
/** |
3432 |
> |
* failedStage returns a CompletionStage completed |
3433 |
> |
* exceptionally with the given Exception |
3434 |
> |
*/ |
3435 |
> |
public void testFailedStage() { |
3436 |
> |
CFException ex = new CFException(); |
3437 |
> |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3438 |
> |
AtomicInteger x = new AtomicInteger(0); |
3439 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3440 |
> |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3441 |
> |
assertEquals(x.get(), 0); |
3442 |
> |
assertEquals(r.get(), ex); |
3443 |
> |
} |
3444 |
> |
|
3445 |
> |
/** |
3446 |
> |
* completeAsync completes with value of given supplier |
3447 |
> |
*/ |
3448 |
> |
public void testCompleteAsync() { |
3449 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3450 |
> |
{ |
3451 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3452 |
> |
f.completeAsync(() -> v1); |
3453 |
> |
f.join(); |
3454 |
> |
checkCompletedNormally(f, v1); |
3455 |
|
}} |
3456 |
|
|
3457 |
|
/** |
3458 |
< |
* whenComplete action executes on cancelled source, propagating |
2900 |
< |
* CancellationException. |
3458 |
> |
* completeAsync completes exceptionally if given supplier throws |
3459 |
|
*/ |
3460 |
< |
public void testWhenComplete_sourceCancelled() { |
3461 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
3462 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3463 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
3460 |
> |
public void testCompleteAsync2() { |
3461 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3462 |
> |
CFException ex = new CFException(); |
3463 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3464 |
> |
try { |
3465 |
> |
f.join(); |
3466 |
> |
shouldThrow(); |
3467 |
> |
} catch (CompletionException success) {} |
3468 |
> |
checkCompletedWithWrappedException(f, ex); |
3469 |
> |
} |
3470 |
> |
|
3471 |
> |
/** |
3472 |
> |
* completeAsync with given executor completes with value of given supplier |
3473 |
> |
*/ |
3474 |
> |
public void testCompleteAsync3() { |
3475 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3476 |
|
{ |
3477 |
< |
final AtomicInteger a = new AtomicInteger(0); |
3478 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3479 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
3480 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
3481 |
< |
(f, |
3482 |
< |
(Integer x, Throwable t) -> { |
3483 |
< |
m.checkExecutionMode(); |
2914 |
< |
threadAssertNull(x); |
2915 |
< |
threadAssertTrue(t instanceof CancellationException); |
2916 |
< |
a.getAndIncrement(); |
2917 |
< |
}); |
2918 |
< |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
3477 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3478 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3479 |
> |
f.completeAsync(() -> v1, executor); |
3480 |
> |
assertSame(v1, f.join()); |
3481 |
> |
checkCompletedNormally(f, v1); |
3482 |
> |
assertEquals(1, executor.count.get()); |
3483 |
> |
}} |
3484 |
|
|
3485 |
< |
checkCompletedWithWrappedCancellationException(g); |
3486 |
< |
checkCancelled(f); |
3487 |
< |
assertEquals(1, a.get()); |
3485 |
> |
/** |
3486 |
> |
* completeAsync with given executor completes exceptionally if |
3487 |
> |
* given supplier throws |
3488 |
> |
*/ |
3489 |
> |
public void testCompleteAsync4() { |
3490 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3491 |
> |
CFException ex = new CFException(); |
3492 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3493 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3494 |
> |
try { |
3495 |
> |
f.join(); |
3496 |
> |
shouldThrow(); |
3497 |
> |
} catch (CompletionException success) {} |
3498 |
> |
checkCompletedWithWrappedException(f, ex); |
3499 |
> |
assertEquals(1, executor.count.get()); |
3500 |
> |
} |
3501 |
> |
|
3502 |
> |
/** |
3503 |
> |
* orTimeout completes with TimeoutException if not complete |
3504 |
> |
*/ |
3505 |
> |
public void testOrTimeout_timesOut() { |
3506 |
> |
long timeoutMillis = timeoutMillis(); |
3507 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3508 |
> |
long startTime = System.nanoTime(); |
3509 |
> |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3510 |
> |
checkCompletedWithTimeoutException(f); |
3511 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3512 |
> |
} |
3513 |
> |
|
3514 |
> |
/** |
3515 |
> |
* orTimeout completes normally if completed before timeout |
3516 |
> |
*/ |
3517 |
> |
public void testOrTimeout_completed() { |
3518 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3519 |
> |
{ |
3520 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3521 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3522 |
> |
long startTime = System.nanoTime(); |
3523 |
> |
f.complete(v1); |
3524 |
> |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3525 |
> |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3526 |
> |
g.complete(v1); |
3527 |
> |
checkCompletedNormally(f, v1); |
3528 |
> |
checkCompletedNormally(g, v1); |
3529 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3530 |
|
}} |
3531 |
|
|
3532 |
|
/** |
3533 |
< |
* If a whenComplete action throws an exception when triggered by |
2927 |
< |
* a normal completion, it completes exceptionally |
3533 |
> |
* completeOnTimeout completes with given value if not complete |
3534 |
|
*/ |
3535 |
< |
public void testWhenComplete_actionFailed() { |
3536 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
3537 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
3535 |
> |
public void testCompleteOnTimeout_timesOut() { |
3536 |
> |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3537 |
> |
() -> testCompleteOnTimeout_timesOut(null)); |
3538 |
> |
} |
3539 |
> |
|
3540 |
> |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3541 |
> |
long timeoutMillis = timeoutMillis(); |
3542 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3543 |
> |
long startTime = System.nanoTime(); |
3544 |
> |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3545 |
> |
assertSame(v, f.join()); |
3546 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3547 |
> |
f.complete(99); // should have no effect |
3548 |
> |
checkCompletedNormally(f, v); |
3549 |
> |
} |
3550 |
> |
|
3551 |
> |
/** |
3552 |
> |
* completeOnTimeout has no effect if completed within timeout |
3553 |
> |
*/ |
3554 |
> |
public void testCompleteOnTimeout_completed() { |
3555 |
|
for (Integer v1 : new Integer[] { 1, null }) |
3556 |
|
{ |
3557 |
< |
final AtomicInteger a = new AtomicInteger(0); |
3558 |
< |
final CFException ex = new CFException(); |
3559 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3560 |
< |
if (!createIncomplete) f.complete(v1); |
3561 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
3562 |
< |
(f, |
3563 |
< |
(Integer x, Throwable t) -> { |
2941 |
< |
m.checkExecutionMode(); |
2942 |
< |
threadAssertSame(x, v1); |
2943 |
< |
threadAssertNull(t); |
2944 |
< |
a.getAndIncrement(); |
2945 |
< |
throw ex; |
2946 |
< |
}); |
2947 |
< |
if (createIncomplete) f.complete(v1); |
3557 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3558 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3559 |
> |
long startTime = System.nanoTime(); |
3560 |
> |
f.complete(v1); |
3561 |
> |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3562 |
> |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3563 |
> |
g.complete(v1); |
3564 |
|
checkCompletedNormally(f, v1); |
3565 |
< |
checkCompletedWithWrappedException(g, ex); |
3566 |
< |
assertEquals(1, a.get()); |
3565 |
> |
checkCompletedNormally(g, v1); |
3566 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3567 |
|
}} |
3568 |
|
|
3569 |
|
/** |
3570 |
< |
* If a whenComplete action throws an exception when triggered by |
2955 |
< |
* a source completion that also throws an exception, the source |
2956 |
< |
* exception takes precedence. |
3570 |
> |
* delayedExecutor returns an executor that delays submission |
3571 |
|
*/ |
3572 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
3573 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
3572 |
> |
public void testDelayedExecutor() { |
3573 |
> |
testInParallel(() -> testDelayedExecutor(null, null), |
3574 |
> |
() -> testDelayedExecutor(null, 1), |
3575 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3576 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3577 |
> |
} |
3578 |
> |
|
3579 |
> |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3580 |
> |
long timeoutMillis = timeoutMillis(); |
3581 |
> |
// Use an "unreasonably long" long timeout to catch lingering threads |
3582 |
> |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3583 |
> |
final Executor delayer, longDelayer; |
3584 |
> |
if (executor == null) { |
3585 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3586 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3587 |
> |
} else { |
3588 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3589 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3590 |
> |
} |
3591 |
> |
long startTime = System.nanoTime(); |
3592 |
> |
CompletableFuture<Integer> f = |
3593 |
> |
CompletableFuture.supplyAsync(() -> v, delayer); |
3594 |
> |
CompletableFuture<Integer> g = |
3595 |
> |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3596 |
> |
|
3597 |
> |
assertNull(g.getNow(null)); |
3598 |
> |
|
3599 |
> |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3600 |
> |
long millisElapsed = millisElapsedSince(startTime); |
3601 |
> |
assertTrue(millisElapsed >= timeoutMillis); |
3602 |
> |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3603 |
> |
|
3604 |
> |
checkCompletedNormally(f, v); |
3605 |
> |
|
3606 |
> |
checkIncomplete(g); |
3607 |
> |
assertTrue(g.cancel(true)); |
3608 |
> |
} |
3609 |
> |
|
3610 |
> |
//--- tests of implementation details; not part of official tck --- |
3611 |
> |
|
3612 |
> |
Object resultOf(CompletableFuture<?> f) { |
3613 |
> |
try { |
3614 |
> |
java.lang.reflect.Field resultField |
3615 |
> |
= CompletableFuture.class.getDeclaredField("result"); |
3616 |
> |
resultField.setAccessible(true); |
3617 |
> |
return resultField.get(f); |
3618 |
> |
} catch (Throwable t) { throw new AssertionError(t); } |
3619 |
> |
} |
3620 |
> |
|
3621 |
> |
public void testExceptionPropagationReusesResultObject() { |
3622 |
> |
if (!testImplementationDetails) return; |
3623 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2961 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
3624 |
|
{ |
3625 |
< |
final AtomicInteger a = new AtomicInteger(0); |
3626 |
< |
final CFException ex1 = new CFException(); |
3627 |
< |
final CFException ex2 = new CFException(); |
2966 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3625 |
> |
final CFException ex = new CFException(); |
3626 |
> |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3627 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3628 |
|
|
3629 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
3630 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2970 |
< |
(f, |
2971 |
< |
(Integer x, Throwable t) -> { |
2972 |
< |
m.checkExecutionMode(); |
2973 |
< |
threadAssertSame(t, ex1); |
2974 |
< |
threadAssertNull(x); |
2975 |
< |
a.getAndIncrement(); |
2976 |
< |
throw ex2; |
2977 |
< |
}); |
2978 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
3629 |
> |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3630 |
> |
= new ArrayList<>(); |
3631 |
|
|
3632 |
< |
checkCompletedExceptionally(f, ex1); |
3633 |
< |
checkCompletedWithWrappedException(g, ex1); |
3634 |
< |
assertEquals(1, a.get()); |
3632 |
> |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3633 |
> |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3634 |
> |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3635 |
> |
|
3636 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3637 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3638 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3639 |
> |
|
3640 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3641 |
> |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3642 |
> |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3643 |
> |
|
3644 |
> |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3645 |
> |
|
3646 |
> |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3647 |
> |
|
3648 |
> |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3649 |
> |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3650 |
> |
|
3651 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3652 |
> |
fun : funs) { |
3653 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3654 |
> |
f.completeExceptionally(ex); |
3655 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3656 |
> |
checkCompletedWithWrappedException(src, ex); |
3657 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3658 |
> |
checkCompletedWithWrappedException(dep, ex); |
3659 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3660 |
> |
} |
3661 |
> |
|
3662 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3663 |
> |
fun : funs) { |
3664 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3665 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3666 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3667 |
> |
f.completeExceptionally(ex); |
3668 |
> |
checkCompletedWithWrappedException(src, ex); |
3669 |
> |
checkCompletedWithWrappedException(dep, ex); |
3670 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3671 |
> |
} |
3672 |
> |
|
3673 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3674 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3675 |
> |
fun : funs) { |
3676 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3677 |
> |
f.cancel(mayInterruptIfRunning); |
3678 |
> |
checkCancelled(f); |
3679 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3680 |
> |
checkCompletedWithWrappedCancellationException(src); |
3681 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3682 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3683 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3684 |
> |
} |
3685 |
> |
|
3686 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3687 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3688 |
> |
fun : funs) { |
3689 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3690 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3691 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3692 |
> |
f.cancel(mayInterruptIfRunning); |
3693 |
> |
checkCancelled(f); |
3694 |
> |
checkCompletedWithWrappedCancellationException(src); |
3695 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3696 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3697 |
> |
} |
3698 |
|
}} |
3699 |
|
|
3700 |
+ |
/** |
3701 |
+ |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3702 |
+ |
*/ |
3703 |
+ |
public void testMinimalCompletionStage_minimality() { |
3704 |
+ |
if (!testImplementationDetails) return; |
3705 |
+ |
Function<Method, String> toSignature = |
3706 |
+ |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3707 |
+ |
Predicate<Method> isNotStatic = |
3708 |
+ |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3709 |
+ |
List<Method> minimalMethods = |
3710 |
+ |
Stream.of(Object.class, CompletionStage.class) |
3711 |
+ |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3712 |
+ |
.filter(isNotStatic) |
3713 |
+ |
.collect(Collectors.toList()); |
3714 |
+ |
// Methods from CompletableFuture permitted NOT to throw UOE |
3715 |
+ |
String[] signatureWhitelist = { |
3716 |
+ |
"newIncompleteFuture[]", |
3717 |
+ |
"defaultExecutor[]", |
3718 |
+ |
"minimalCompletionStage[]", |
3719 |
+ |
"copy[]", |
3720 |
+ |
}; |
3721 |
+ |
Set<String> permittedMethodSignatures = |
3722 |
+ |
Stream.concat(minimalMethods.stream().map(toSignature), |
3723 |
+ |
Stream.of(signatureWhitelist)) |
3724 |
+ |
.collect(Collectors.toSet()); |
3725 |
+ |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3726 |
+ |
.filter(isNotStatic) |
3727 |
+ |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3728 |
+ |
.collect(Collectors.toList()); |
3729 |
+ |
|
3730 |
+ |
CompletionStage<Integer> minimalStage = |
3731 |
+ |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3732 |
+ |
|
3733 |
+ |
List<Method> bugs = new ArrayList<>(); |
3734 |
+ |
for (Method method : allMethods) { |
3735 |
+ |
Class<?>[] parameterTypes = method.getParameterTypes(); |
3736 |
+ |
Object[] args = new Object[parameterTypes.length]; |
3737 |
+ |
// Manufacture boxed primitives for primitive params |
3738 |
+ |
for (int i = 0; i < args.length; i++) { |
3739 |
+ |
Class<?> type = parameterTypes[i]; |
3740 |
+ |
if (parameterTypes[i] == boolean.class) |
3741 |
+ |
args[i] = false; |
3742 |
+ |
else if (parameterTypes[i] == int.class) |
3743 |
+ |
args[i] = 0; |
3744 |
+ |
else if (parameterTypes[i] == long.class) |
3745 |
+ |
args[i] = 0L; |
3746 |
+ |
} |
3747 |
+ |
try { |
3748 |
+ |
method.invoke(minimalStage, args); |
3749 |
+ |
bugs.add(method); |
3750 |
+ |
} |
3751 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3752 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3753 |
+ |
bugs.add(method); |
3754 |
+ |
// expected.getCause().printStackTrace(); |
3755 |
+ |
} |
3756 |
+ |
} |
3757 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3758 |
+ |
} |
3759 |
+ |
if (!bugs.isEmpty()) |
3760 |
+ |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
3761 |
+ |
} |
3762 |
+ |
|
3763 |
+ |
// static <U> U join(CompletionStage<U> stage) { |
3764 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3765 |
+ |
// stage.whenComplete((v, ex) -> { |
3766 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3767 |
+ |
// }); |
3768 |
+ |
// return f.join(); |
3769 |
+ |
// } |
3770 |
+ |
|
3771 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
3772 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3773 |
+ |
// stage.whenComplete((v, ex) -> { |
3774 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3775 |
+ |
// }); |
3776 |
+ |
// return f.isDone(); |
3777 |
+ |
// } |
3778 |
+ |
|
3779 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
3780 |
+ |
// return stage.toCompletableFuture().copy().join(); |
3781 |
+ |
// } |
3782 |
+ |
|
3783 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
3784 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
3785 |
+ |
// } |
3786 |
+ |
|
3787 |
|
} |