5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
< |
import junit.framework.*; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
> |
import static java.util.concurrent.CompletableFuture.completedFuture; |
11 |
> |
import static java.util.concurrent.CompletableFuture.failedFuture; |
12 |
> |
|
13 |
> |
import java.lang.reflect.Method; |
14 |
> |
import java.lang.reflect.Modifier; |
15 |
> |
|
16 |
> |
import java.util.stream.Collectors; |
17 |
> |
import java.util.stream.Stream; |
18 |
> |
|
19 |
> |
import java.util.ArrayList; |
20 |
> |
import java.util.Arrays; |
21 |
> |
import java.util.List; |
22 |
> |
import java.util.Objects; |
23 |
> |
import java.util.Set; |
24 |
|
import java.util.concurrent.Callable; |
10 |
– |
import java.util.concurrent.Executor; |
11 |
– |
import java.util.concurrent.ExecutorService; |
12 |
– |
import java.util.concurrent.Executors; |
25 |
|
import java.util.concurrent.CancellationException; |
14 |
– |
import java.util.concurrent.CountDownLatch; |
15 |
– |
import java.util.concurrent.ExecutionException; |
16 |
– |
import java.util.concurrent.Future; |
26 |
|
import java.util.concurrent.CompletableFuture; |
27 |
|
import java.util.concurrent.CompletionException; |
28 |
|
import java.util.concurrent.CompletionStage; |
29 |
+ |
import java.util.concurrent.ExecutionException; |
30 |
+ |
import java.util.concurrent.Executor; |
31 |
|
import java.util.concurrent.ForkJoinPool; |
32 |
|
import java.util.concurrent.ForkJoinTask; |
33 |
+ |
import java.util.concurrent.RejectedExecutionException; |
34 |
|
import java.util.concurrent.TimeoutException; |
35 |
|
import java.util.concurrent.atomic.AtomicInteger; |
36 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
25 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
26 |
< |
import java.util.*; |
27 |
< |
import java.util.function.Supplier; |
28 |
< |
import java.util.function.Consumer; |
36 |
> |
import java.util.concurrent.atomic.AtomicReference; |
37 |
|
import java.util.function.BiConsumer; |
30 |
– |
import java.util.function.Function; |
38 |
|
import java.util.function.BiFunction; |
39 |
+ |
import java.util.function.Consumer; |
40 |
+ |
import java.util.function.Function; |
41 |
+ |
import java.util.function.Predicate; |
42 |
+ |
import java.util.function.Supplier; |
43 |
+ |
|
44 |
+ |
import junit.framework.AssertionFailedError; |
45 |
+ |
import junit.framework.Test; |
46 |
+ |
import junit.framework.TestSuite; |
47 |
|
|
48 |
|
public class CompletableFutureTest extends JSR166TestCase { |
49 |
|
|
50 |
|
public static void main(String[] args) { |
51 |
< |
junit.textui.TestRunner.run(suite()); |
51 |
> |
main(suite(), args); |
52 |
|
} |
53 |
|
public static Test suite() { |
54 |
|
return new TestSuite(CompletableFutureTest.class); |
59 |
|
void checkIncomplete(CompletableFuture<?> f) { |
60 |
|
assertFalse(f.isDone()); |
61 |
|
assertFalse(f.isCancelled()); |
62 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
62 |
> |
assertTrue(f.toString().contains("Not completed")); |
63 |
|
try { |
64 |
|
assertNull(f.getNow(null)); |
65 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
66 |
|
try { |
67 |
< |
f.get(0L, SECONDS); |
67 |
> |
f.get(randomExpiredTimeout(), randomTimeUnit()); |
68 |
|
shouldThrow(); |
69 |
|
} |
70 |
|
catch (TimeoutException success) {} |
72 |
|
} |
73 |
|
|
74 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
75 |
< |
try { |
76 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
62 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
75 |
> |
checkTimedGet(f, value); |
76 |
> |
|
77 |
|
try { |
78 |
|
assertEquals(value, f.join()); |
65 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
66 |
– |
try { |
79 |
|
assertEquals(value, f.getNow(null)); |
68 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
69 |
– |
try { |
80 |
|
assertEquals(value, f.get()); |
81 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
82 |
|
assertTrue(f.isDone()); |
85 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
86 |
|
} |
87 |
|
|
88 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
89 |
< |
try { |
90 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
91 |
< |
shouldThrow(); |
92 |
< |
} catch (ExecutionException success) { |
93 |
< |
assertTrue(success.getCause() instanceof CFException); |
94 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
95 |
< |
try { |
96 |
< |
f.join(); |
97 |
< |
shouldThrow(); |
98 |
< |
} catch (CompletionException success) { |
99 |
< |
assertTrue(success.getCause() instanceof CFException); |
100 |
< |
} |
101 |
< |
try { |
102 |
< |
f.getNow(null); |
103 |
< |
shouldThrow(); |
104 |
< |
} catch (CompletionException success) { |
95 |
< |
assertTrue(success.getCause() instanceof CFException); |
88 |
> |
/** |
89 |
> |
* Returns the "raw" internal exceptional completion of f, |
90 |
> |
* without any additional wrapping with CompletionException. |
91 |
> |
*/ |
92 |
> |
Throwable exceptionalCompletion(CompletableFuture<?> f) { |
93 |
> |
// handle (and whenComplete and exceptionally) can distinguish |
94 |
> |
// between "direct" and "wrapped" exceptional completion |
95 |
> |
return f.handle((u, t) -> t).join(); |
96 |
> |
} |
97 |
> |
|
98 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, |
99 |
> |
boolean wrapped, |
100 |
> |
Consumer<Throwable> checker) { |
101 |
> |
Throwable cause = exceptionalCompletion(f); |
102 |
> |
if (wrapped) { |
103 |
> |
assertTrue(cause instanceof CompletionException); |
104 |
> |
cause = cause.getCause(); |
105 |
|
} |
106 |
< |
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 |
< |
} |
106 |
> |
checker.accept(cause); |
107 |
|
|
108 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f, |
109 |
< |
CFException ex) { |
108 |
> |
long startTime = System.nanoTime(); |
109 |
|
try { |
110 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
111 |
|
shouldThrow(); |
112 |
|
} catch (ExecutionException success) { |
113 |
< |
assertSame(ex, success.getCause()); |
113 |
> |
assertSame(cause, success.getCause()); |
114 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
115 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
116 |
+ |
|
117 |
|
try { |
118 |
|
f.join(); |
119 |
|
shouldThrow(); |
120 |
|
} catch (CompletionException success) { |
121 |
< |
assertSame(ex, success.getCause()); |
122 |
< |
} |
121 |
> |
assertSame(cause, success.getCause()); |
122 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
123 |
> |
|
124 |
|
try { |
125 |
|
f.getNow(null); |
126 |
|
shouldThrow(); |
127 |
|
} catch (CompletionException success) { |
128 |
< |
assertSame(ex, success.getCause()); |
129 |
< |
} |
128 |
> |
assertSame(cause, success.getCause()); |
129 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
130 |
> |
|
131 |
|
try { |
132 |
|
f.get(); |
133 |
|
shouldThrow(); |
134 |
|
} catch (ExecutionException success) { |
135 |
< |
assertSame(ex, success.getCause()); |
135 |
> |
assertSame(cause, success.getCause()); |
136 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
137 |
< |
assertTrue(f.isDone()); |
137 |
> |
|
138 |
|
assertFalse(f.isCancelled()); |
139 |
+ |
assertTrue(f.isDone()); |
140 |
+ |
assertTrue(f.isCompletedExceptionally()); |
141 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
142 |
|
} |
143 |
|
|
144 |
+ |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
145 |
+ |
checkCompletedExceptionally(f, true, |
146 |
+ |
t -> assertTrue(t instanceof CFException)); |
147 |
+ |
} |
148 |
+ |
|
149 |
+ |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
150 |
+ |
checkCompletedExceptionally(f, true, |
151 |
+ |
t -> assertTrue(t instanceof CancellationException)); |
152 |
+ |
} |
153 |
+ |
|
154 |
+ |
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
155 |
+ |
checkCompletedExceptionally(f, false, |
156 |
+ |
t -> assertTrue(t instanceof TimeoutException)); |
157 |
+ |
} |
158 |
+ |
|
159 |
+ |
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
160 |
+ |
Throwable ex) { |
161 |
+ |
checkCompletedExceptionally(f, true, t -> assertSame(t, ex)); |
162 |
+ |
} |
163 |
+ |
|
164 |
+ |
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
165 |
+ |
checkCompletedExceptionally(f, false, t -> assertSame(t, ex)); |
166 |
+ |
} |
167 |
+ |
|
168 |
|
void checkCancelled(CompletableFuture<?> f) { |
169 |
+ |
long startTime = System.nanoTime(); |
170 |
|
try { |
171 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
172 |
|
shouldThrow(); |
173 |
|
} catch (CancellationException success) { |
174 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
175 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
176 |
+ |
|
177 |
|
try { |
178 |
|
f.join(); |
179 |
|
shouldThrow(); |
187 |
|
shouldThrow(); |
188 |
|
} catch (CancellationException success) { |
189 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
158 |
– |
assertTrue(f.isDone()); |
159 |
– |
assertTrue(f.isCompletedExceptionally()); |
160 |
– |
assertTrue(f.isCancelled()); |
161 |
– |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
162 |
– |
} |
190 |
|
|
191 |
< |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
192 |
< |
try { |
166 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
167 |
< |
shouldThrow(); |
168 |
< |
} catch (ExecutionException success) { |
169 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
170 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
171 |
< |
try { |
172 |
< |
f.join(); |
173 |
< |
shouldThrow(); |
174 |
< |
} catch (CompletionException success) { |
175 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
176 |
< |
} |
177 |
< |
try { |
178 |
< |
f.getNow(null); |
179 |
< |
shouldThrow(); |
180 |
< |
} catch (CompletionException success) { |
181 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
182 |
< |
} |
183 |
< |
try { |
184 |
< |
f.get(); |
185 |
< |
shouldThrow(); |
186 |
< |
} catch (ExecutionException success) { |
187 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
188 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
191 |
> |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
192 |
> |
|
193 |
|
assertTrue(f.isDone()); |
190 |
– |
assertFalse(f.isCancelled()); |
194 |
|
assertTrue(f.isCompletedExceptionally()); |
195 |
+ |
assertTrue(f.isCancelled()); |
196 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
197 |
|
} |
198 |
|
|
210 |
|
* isCancelled, join, get, and getNow |
211 |
|
*/ |
212 |
|
public void testComplete() { |
213 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
214 |
+ |
{ |
215 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
216 |
|
checkIncomplete(f); |
217 |
< |
f.complete(one); |
218 |
< |
checkCompletedNormally(f, one); |
219 |
< |
} |
217 |
> |
assertTrue(f.complete(v1)); |
218 |
> |
assertFalse(f.complete(v1)); |
219 |
> |
checkCompletedNormally(f, v1); |
220 |
> |
}} |
221 |
|
|
222 |
|
/** |
223 |
|
* completeExceptionally completes exceptionally, as indicated by |
225 |
|
*/ |
226 |
|
public void testCompleteExceptionally() { |
227 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
228 |
+ |
CFException ex = new CFException(); |
229 |
|
checkIncomplete(f); |
230 |
< |
f.completeExceptionally(new CFException()); |
231 |
< |
checkCompletedWithWrappedCFException(f); |
230 |
> |
f.completeExceptionally(ex); |
231 |
> |
checkCompletedExceptionally(f, ex); |
232 |
|
} |
233 |
|
|
234 |
|
/** |
236 |
|
* methods isDone, isCancelled, join, get, and getNow |
237 |
|
*/ |
238 |
|
public void testCancel() { |
239 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
240 |
+ |
{ |
241 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
242 |
|
checkIncomplete(f); |
243 |
< |
assertTrue(f.cancel(true)); |
243 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
244 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
245 |
> |
assertTrue(f.cancel(!mayInterruptIfRunning)); |
246 |
|
checkCancelled(f); |
247 |
< |
} |
247 |
> |
}} |
248 |
|
|
249 |
|
/** |
250 |
|
* obtrudeValue forces completion with given value |
252 |
|
public void testObtrudeValue() { |
253 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
254 |
|
checkIncomplete(f); |
255 |
< |
f.complete(one); |
255 |
> |
assertTrue(f.complete(one)); |
256 |
|
checkCompletedNormally(f, one); |
257 |
|
f.obtrudeValue(three); |
258 |
|
checkCompletedNormally(f, three); |
273 |
|
* obtrudeException forces completion with given exception |
274 |
|
*/ |
275 |
|
public void testObtrudeException() { |
276 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
277 |
< |
checkIncomplete(f); |
278 |
< |
f.complete(one); |
279 |
< |
checkCompletedNormally(f, one); |
280 |
< |
f.obtrudeException(new CFException()); |
269 |
< |
checkCompletedWithWrappedCFException(f); |
276 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
277 |
> |
{ |
278 |
> |
CFException ex; |
279 |
> |
CompletableFuture<Integer> f; |
280 |
> |
|
281 |
|
f = new CompletableFuture<>(); |
282 |
< |
f.obtrudeException(new CFException()); |
283 |
< |
checkCompletedWithWrappedCFException(f); |
282 |
> |
assertTrue(f.complete(v1)); |
283 |
> |
for (int i = 0; i < 2; i++) { |
284 |
> |
f.obtrudeException(ex = new CFException()); |
285 |
> |
checkCompletedExceptionally(f, ex); |
286 |
> |
} |
287 |
> |
|
288 |
|
f = new CompletableFuture<>(); |
289 |
+ |
for (int i = 0; i < 2; i++) { |
290 |
+ |
f.obtrudeException(ex = new CFException()); |
291 |
+ |
checkCompletedExceptionally(f, ex); |
292 |
+ |
} |
293 |
+ |
|
294 |
+ |
f = new CompletableFuture<>(); |
295 |
+ |
f.completeExceptionally(ex = new CFException()); |
296 |
+ |
f.obtrudeValue(v1); |
297 |
+ |
checkCompletedNormally(f, v1); |
298 |
+ |
f.obtrudeException(ex = new CFException()); |
299 |
+ |
checkCompletedExceptionally(f, ex); |
300 |
|
f.completeExceptionally(new CFException()); |
301 |
< |
f.obtrudeValue(four); |
302 |
< |
checkCompletedNormally(f, four); |
303 |
< |
f.obtrudeException(new CFException()); |
304 |
< |
checkCompletedWithWrappedCFException(f); |
279 |
< |
} |
301 |
> |
checkCompletedExceptionally(f, ex); |
302 |
> |
assertFalse(f.complete(v1)); |
303 |
> |
checkCompletedExceptionally(f, ex); |
304 |
> |
}} |
305 |
|
|
306 |
|
/** |
307 |
|
* getNumberOfDependents returns number of dependent tasks |
308 |
|
*/ |
309 |
|
public void testGetNumberOfDependents() { |
310 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
311 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
312 |
+ |
{ |
313 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
314 |
|
assertEquals(0, f.getNumberOfDependents()); |
315 |
< |
CompletableFuture g = f.thenRun(new Noop()); |
315 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
316 |
|
assertEquals(1, f.getNumberOfDependents()); |
317 |
|
assertEquals(0, g.getNumberOfDependents()); |
318 |
< |
CompletableFuture h = f.thenRun(new Noop()); |
318 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
319 |
|
assertEquals(2, f.getNumberOfDependents()); |
320 |
< |
f.complete(1); |
320 |
> |
assertEquals(0, h.getNumberOfDependents()); |
321 |
> |
assertTrue(f.complete(v1)); |
322 |
|
checkCompletedNormally(g, null); |
323 |
+ |
checkCompletedNormally(h, null); |
324 |
|
assertEquals(0, f.getNumberOfDependents()); |
325 |
|
assertEquals(0, g.getNumberOfDependents()); |
326 |
< |
} |
326 |
> |
assertEquals(0, h.getNumberOfDependents()); |
327 |
> |
}} |
328 |
|
|
329 |
|
/** |
330 |
|
* toString indicates current completion state |
335 |
|
f = new CompletableFuture<String>(); |
336 |
|
assertTrue(f.toString().contains("[Not completed]")); |
337 |
|
|
338 |
< |
f.complete("foo"); |
338 |
> |
assertTrue(f.complete("foo")); |
339 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
340 |
|
|
341 |
|
f = new CompletableFuture<String>(); |
342 |
< |
f.completeExceptionally(new IndexOutOfBoundsException()); |
342 |
> |
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
343 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
344 |
+ |
|
345 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
346 |
+ |
f = new CompletableFuture<String>(); |
347 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
348 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
349 |
+ |
} |
350 |
|
} |
351 |
|
|
352 |
|
/** |
357 |
|
checkCompletedNormally(f, "test"); |
358 |
|
} |
359 |
|
|
360 |
< |
// Choose non-commutative actions for better coverage |
360 |
> |
abstract static class CheckedAction { |
361 |
> |
int invocationCount = 0; |
362 |
> |
final ExecutionMode m; |
363 |
> |
CheckedAction(ExecutionMode m) { this.m = m; } |
364 |
> |
void invoked() { |
365 |
> |
m.checkExecutionMode(); |
366 |
> |
assertEquals(0, invocationCount++); |
367 |
> |
} |
368 |
> |
void assertNotInvoked() { assertEquals(0, invocationCount); } |
369 |
> |
void assertInvoked() { assertEquals(1, invocationCount); } |
370 |
> |
} |
371 |
|
|
372 |
< |
// A non-commutative function that handles and produces null values as well. |
373 |
< |
static Integer subtract(Integer x, Integer y) { |
374 |
< |
return (x == null && y == null) ? null : |
375 |
< |
((x == null) ? 42 : x.intValue()) |
376 |
< |
- ((y == null) ? 99 : y.intValue()); |
372 |
> |
abstract static class CheckedIntegerAction extends CheckedAction { |
373 |
> |
Integer value; |
374 |
> |
CheckedIntegerAction(ExecutionMode m) { super(m); } |
375 |
> |
void assertValue(Integer expected) { |
376 |
> |
assertInvoked(); |
377 |
> |
assertEquals(expected, value); |
378 |
> |
} |
379 |
> |
} |
380 |
> |
|
381 |
> |
static class IntegerSupplier extends CheckedAction |
382 |
> |
implements Supplier<Integer> |
383 |
> |
{ |
384 |
> |
final Integer value; |
385 |
> |
IntegerSupplier(ExecutionMode m, Integer value) { |
386 |
> |
super(m); |
387 |
> |
this.value = value; |
388 |
> |
} |
389 |
> |
public Integer get() { |
390 |
> |
invoked(); |
391 |
> |
return value; |
392 |
> |
} |
393 |
|
} |
394 |
|
|
395 |
|
// A function that handles and produces null values as well. |
397 |
|
return (x == null) ? null : x + 1; |
398 |
|
} |
399 |
|
|
400 |
< |
static final Supplier<Integer> supplyOne = |
401 |
< |
() -> Integer.valueOf(1); |
402 |
< |
static final Function<Integer, Integer> inc = |
403 |
< |
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
341 |
< |
static final BiFunction<Integer, Integer, Integer> subtract = |
342 |
< |
(Integer x, Integer y) -> subtract(x, y); |
343 |
< |
static final class IncAction implements Consumer<Integer> { |
344 |
< |
int invocationCount = 0; |
345 |
< |
Integer value; |
400 |
> |
static class NoopConsumer extends CheckedIntegerAction |
401 |
> |
implements Consumer<Integer> |
402 |
> |
{ |
403 |
> |
NoopConsumer(ExecutionMode m) { super(m); } |
404 |
|
public void accept(Integer x) { |
405 |
< |
invocationCount++; |
406 |
< |
value = inc(x); |
405 |
> |
invoked(); |
406 |
> |
value = x; |
407 |
|
} |
408 |
|
} |
409 |
< |
static final class IncFunction implements Function<Integer,Integer> { |
410 |
< |
int invocationCount = 0; |
411 |
< |
Integer value; |
409 |
> |
|
410 |
> |
static class IncFunction extends CheckedIntegerAction |
411 |
> |
implements Function<Integer,Integer> |
412 |
> |
{ |
413 |
> |
IncFunction(ExecutionMode m) { super(m); } |
414 |
|
public Integer apply(Integer x) { |
415 |
< |
invocationCount++; |
415 |
> |
invoked(); |
416 |
|
return value = inc(x); |
417 |
|
} |
418 |
|
} |
419 |
< |
static final class SubtractAction implements BiConsumer<Integer, Integer> { |
420 |
< |
int invocationCount = 0; |
421 |
< |
Integer value; |
422 |
< |
// Check this action was invoked exactly once when result is computed. |
419 |
> |
|
420 |
> |
// Choose non-commutative actions for better coverage |
421 |
> |
// A non-commutative function that handles and produces null values as well. |
422 |
> |
static Integer subtract(Integer x, Integer y) { |
423 |
> |
return (x == null && y == null) ? null : |
424 |
> |
((x == null) ? 42 : x.intValue()) |
425 |
> |
- ((y == null) ? 99 : y.intValue()); |
426 |
> |
} |
427 |
> |
|
428 |
> |
static class SubtractAction extends CheckedIntegerAction |
429 |
> |
implements BiConsumer<Integer, Integer> |
430 |
> |
{ |
431 |
> |
SubtractAction(ExecutionMode m) { super(m); } |
432 |
|
public void accept(Integer x, Integer y) { |
433 |
< |
invocationCount++; |
433 |
> |
invoked(); |
434 |
|
value = subtract(x, y); |
435 |
|
} |
436 |
|
} |
437 |
< |
static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> { |
438 |
< |
int invocationCount = 0; |
439 |
< |
Integer value; |
440 |
< |
// Check this action was invoked exactly once when result is computed. |
437 |
> |
|
438 |
> |
static class SubtractFunction extends CheckedIntegerAction |
439 |
> |
implements BiFunction<Integer, Integer, Integer> |
440 |
> |
{ |
441 |
> |
SubtractFunction(ExecutionMode m) { super(m); } |
442 |
|
public Integer apply(Integer x, Integer y) { |
443 |
< |
invocationCount++; |
443 |
> |
invoked(); |
444 |
|
return value = subtract(x, y); |
445 |
|
} |
446 |
|
} |
447 |
< |
static final class Noop implements Runnable { |
448 |
< |
int invocationCount = 0; |
447 |
> |
|
448 |
> |
static class Noop extends CheckedAction implements Runnable { |
449 |
> |
Noop(ExecutionMode m) { super(m); } |
450 |
|
public void run() { |
451 |
< |
invocationCount++; |
451 |
> |
invoked(); |
452 |
|
} |
453 |
|
} |
454 |
|
|
455 |
< |
static final class FailingSupplier implements Supplier<Integer> { |
456 |
< |
int invocationCount = 0; |
455 |
> |
static class FailingSupplier extends CheckedAction |
456 |
> |
implements Supplier<Integer> |
457 |
> |
{ |
458 |
> |
final CFException ex; |
459 |
> |
FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); } |
460 |
|
public Integer get() { |
461 |
< |
invocationCount++; |
462 |
< |
throw new CFException(); |
461 |
> |
invoked(); |
462 |
> |
throw ex; |
463 |
|
} |
464 |
|
} |
465 |
< |
static final class FailingConsumer implements Consumer<Integer> { |
466 |
< |
int invocationCount = 0; |
465 |
> |
|
466 |
> |
static class FailingConsumer extends CheckedIntegerAction |
467 |
> |
implements Consumer<Integer> |
468 |
> |
{ |
469 |
> |
final CFException ex; |
470 |
> |
FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
471 |
|
public void accept(Integer x) { |
472 |
< |
invocationCount++; |
473 |
< |
throw new CFException(); |
472 |
> |
invoked(); |
473 |
> |
value = x; |
474 |
> |
throw ex; |
475 |
|
} |
476 |
|
} |
477 |
< |
static final class FailingBiConsumer implements BiConsumer<Integer, Integer> { |
478 |
< |
int invocationCount = 0; |
477 |
> |
|
478 |
> |
static class FailingBiConsumer extends CheckedIntegerAction |
479 |
> |
implements BiConsumer<Integer, Integer> |
480 |
> |
{ |
481 |
> |
final CFException ex; |
482 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
483 |
|
public void accept(Integer x, Integer y) { |
484 |
< |
invocationCount++; |
485 |
< |
throw new CFException(); |
484 |
> |
invoked(); |
485 |
> |
value = subtract(x, y); |
486 |
> |
throw ex; |
487 |
|
} |
488 |
|
} |
489 |
< |
static final class FailingFunction implements Function<Integer, Integer> { |
490 |
< |
int invocationCount = 0; |
489 |
> |
|
490 |
> |
static class FailingFunction extends CheckedIntegerAction |
491 |
> |
implements Function<Integer, Integer> |
492 |
> |
{ |
493 |
> |
final CFException ex; |
494 |
> |
FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
495 |
|
public Integer apply(Integer x) { |
496 |
< |
invocationCount++; |
497 |
< |
throw new CFException(); |
496 |
> |
invoked(); |
497 |
> |
value = x; |
498 |
> |
throw ex; |
499 |
|
} |
500 |
|
} |
501 |
< |
static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> { |
502 |
< |
int invocationCount = 0; |
501 |
> |
|
502 |
> |
static class FailingBiFunction extends CheckedIntegerAction |
503 |
> |
implements BiFunction<Integer, Integer, Integer> |
504 |
> |
{ |
505 |
> |
final CFException ex; |
506 |
> |
FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
507 |
|
public Integer apply(Integer x, Integer y) { |
508 |
< |
invocationCount++; |
509 |
< |
throw new CFException(); |
508 |
> |
invoked(); |
509 |
> |
value = subtract(x, y); |
510 |
> |
throw ex; |
511 |
|
} |
512 |
|
} |
513 |
< |
static final class FailingRunnable implements Runnable { |
514 |
< |
int invocationCount = 0; |
513 |
> |
|
514 |
> |
static class FailingRunnable extends CheckedAction implements Runnable { |
515 |
> |
final CFException ex; |
516 |
> |
FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); } |
517 |
|
public void run() { |
518 |
< |
invocationCount++; |
519 |
< |
throw new CFException(); |
518 |
> |
invoked(); |
519 |
> |
throw ex; |
520 |
|
} |
521 |
|
} |
522 |
|
|
523 |
< |
static final class CompletableFutureInc |
524 |
< |
implements Function<Integer, CompletableFuture<Integer>> { |
525 |
< |
int invocationCount = 0; |
523 |
> |
static class CompletableFutureInc extends CheckedIntegerAction |
524 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
525 |
> |
{ |
526 |
> |
CompletableFutureInc(ExecutionMode m) { super(m); } |
527 |
|
public CompletableFuture<Integer> apply(Integer x) { |
528 |
< |
invocationCount++; |
528 |
> |
invoked(); |
529 |
> |
value = x; |
530 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
531 |
< |
f.complete(inc(x)); |
531 |
> |
assertTrue(f.complete(inc(x))); |
532 |
|
return f; |
533 |
|
} |
534 |
|
} |
535 |
|
|
536 |
< |
static final class FailingCompletableFutureFunction |
537 |
< |
implements Function<Integer, CompletableFuture<Integer>> { |
538 |
< |
int invocationCount = 0; |
536 |
> |
static class FailingCompletableFutureFunction extends CheckedIntegerAction |
537 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
538 |
> |
{ |
539 |
> |
final CFException ex; |
540 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
541 |
|
public CompletableFuture<Integer> apply(Integer x) { |
542 |
< |
invocationCount++; |
543 |
< |
throw new CFException(); |
542 |
> |
invoked(); |
543 |
> |
value = x; |
544 |
> |
throw ex; |
545 |
> |
} |
546 |
> |
} |
547 |
> |
|
548 |
> |
static class CountingRejectingExecutor implements Executor { |
549 |
> |
final RejectedExecutionException ex = new RejectedExecutionException(); |
550 |
> |
final AtomicInteger count = new AtomicInteger(0); |
551 |
> |
public void execute(Runnable r) { |
552 |
> |
count.getAndIncrement(); |
553 |
> |
throw ex; |
554 |
|
} |
555 |
|
} |
556 |
|
|
557 |
|
// Used for explicit executor tests |
558 |
|
static final class ThreadExecutor implements Executor { |
559 |
< |
AtomicInteger count = new AtomicInteger(0); |
559 |
> |
final AtomicInteger count = new AtomicInteger(0); |
560 |
> |
static final ThreadGroup tg = new ThreadGroup("ThreadExecutor"); |
561 |
> |
static boolean startedCurrentThread() { |
562 |
> |
return Thread.currentThread().getThreadGroup() == tg; |
563 |
> |
} |
564 |
|
|
565 |
|
public void execute(Runnable r) { |
566 |
|
count.getAndIncrement(); |
567 |
< |
new Thread(r).start(); |
567 |
> |
new Thread(tg, r).start(); |
568 |
|
} |
569 |
|
} |
570 |
|
|
571 |
+ |
static final boolean defaultExecutorIsCommonPool |
572 |
+ |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
573 |
+ |
|
574 |
|
/** |
575 |
|
* Permits the testing of parallel code for the 3 different |
576 |
< |
* execution modes without repeating all the testing code. |
576 |
> |
* execution modes without copy/pasting all the test methods. |
577 |
|
*/ |
578 |
|
enum ExecutionMode { |
579 |
< |
DEFAULT { |
579 |
> |
SYNC { |
580 |
|
public void checkExecutionMode() { |
581 |
+ |
assertFalse(ThreadExecutor.startedCurrentThread()); |
582 |
|
assertNull(ForkJoinTask.getPool()); |
583 |
|
} |
584 |
+ |
public CompletableFuture<Void> runAsync(Runnable a) { |
585 |
+ |
throw new UnsupportedOperationException(); |
586 |
+ |
} |
587 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
588 |
+ |
throw new UnsupportedOperationException(); |
589 |
+ |
} |
590 |
|
public <T> CompletableFuture<Void> thenRun |
591 |
|
(CompletableFuture<T> f, Runnable a) { |
592 |
|
return f.thenRun(a); |
652 |
|
|
653 |
|
ASYNC { |
654 |
|
public void checkExecutionMode() { |
655 |
< |
assertSame(ForkJoinPool.commonPool(), |
656 |
< |
ForkJoinTask.getPool()); |
655 |
> |
assertEquals(defaultExecutorIsCommonPool, |
656 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
657 |
> |
} |
658 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
659 |
> |
return CompletableFuture.runAsync(a); |
660 |
> |
} |
661 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
662 |
> |
return CompletableFuture.supplyAsync(a); |
663 |
|
} |
664 |
|
public <T> CompletableFuture<Void> thenRun |
665 |
|
(CompletableFuture<T> f, Runnable a) { |
726 |
|
|
727 |
|
EXECUTOR { |
728 |
|
public void checkExecutionMode() { |
729 |
< |
//TODO |
729 |
> |
assertTrue(ThreadExecutor.startedCurrentThread()); |
730 |
> |
} |
731 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
732 |
> |
return CompletableFuture.runAsync(a, new ThreadExecutor()); |
733 |
> |
} |
734 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
735 |
> |
return CompletableFuture.supplyAsync(a, new ThreadExecutor()); |
736 |
|
} |
737 |
|
public <T> CompletableFuture<Void> thenRun |
738 |
|
(CompletableFuture<T> f, Runnable a) { |
798 |
|
}; |
799 |
|
|
800 |
|
public abstract void checkExecutionMode(); |
801 |
+ |
public abstract CompletableFuture<Void> runAsync(Runnable a); |
802 |
+ |
public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); |
803 |
|
public abstract <T> CompletableFuture<Void> thenRun |
804 |
|
(CompletableFuture<T> f, Runnable a); |
805 |
|
public abstract <T> CompletableFuture<Void> thenAccept |
849 |
|
{ |
850 |
|
final AtomicInteger a = new AtomicInteger(0); |
851 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
852 |
< |
if (!createIncomplete) f.complete(v1); |
852 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
853 |
|
final CompletableFuture<Integer> g = f.exceptionally |
854 |
|
((Throwable t) -> { |
717 |
– |
// Should not be called |
855 |
|
a.getAndIncrement(); |
856 |
< |
throw new AssertionError(); |
856 |
> |
threadFail("should not be called"); |
857 |
> |
return null; // unreached |
858 |
|
}); |
859 |
< |
if (createIncomplete) f.complete(v1); |
859 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
860 |
|
|
861 |
|
checkCompletedNormally(g, v1); |
862 |
|
checkCompletedNormally(f, v1); |
863 |
|
assertEquals(0, a.get()); |
864 |
|
}} |
865 |
|
|
728 |
– |
|
866 |
|
/** |
867 |
|
* exceptionally action completes with function value on source |
868 |
|
* exception |
877 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
878 |
|
final CompletableFuture<Integer> g = f.exceptionally |
879 |
|
((Throwable t) -> { |
880 |
+ |
ExecutionMode.SYNC.checkExecutionMode(); |
881 |
|
threadAssertSame(t, ex); |
882 |
|
a.getAndIncrement(); |
883 |
|
return v1; |
888 |
|
assertEquals(1, a.get()); |
889 |
|
}} |
890 |
|
|
891 |
+ |
/** |
892 |
+ |
* If an "exceptionally action" throws an exception, it completes |
893 |
+ |
* exceptionally with that exception |
894 |
+ |
*/ |
895 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
896 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
755 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
897 |
|
{ |
898 |
|
final AtomicInteger a = new AtomicInteger(0); |
899 |
|
final CFException ex1 = new CFException(); |
902 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
903 |
|
final CompletableFuture<Integer> g = f.exceptionally |
904 |
|
((Throwable t) -> { |
905 |
+ |
ExecutionMode.SYNC.checkExecutionMode(); |
906 |
|
threadAssertSame(t, ex1); |
907 |
|
a.getAndIncrement(); |
908 |
|
throw ex2; |
909 |
|
}); |
910 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
911 |
|
|
912 |
< |
checkCompletedWithWrappedCFException(g, ex2); |
912 |
> |
checkCompletedWithWrappedException(g, ex2); |
913 |
> |
checkCompletedExceptionally(f, ex1); |
914 |
> |
assertEquals(1, a.get()); |
915 |
> |
}} |
916 |
> |
|
917 |
> |
/** |
918 |
> |
* whenComplete action executes on normal completion, propagating |
919 |
> |
* source result. |
920 |
> |
*/ |
921 |
> |
public void testWhenComplete_normalCompletion() { |
922 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
923 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
924 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
925 |
> |
{ |
926 |
> |
final AtomicInteger a = new AtomicInteger(0); |
927 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
928 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
929 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
930 |
> |
(f, |
931 |
> |
(Integer result, Throwable t) -> { |
932 |
> |
m.checkExecutionMode(); |
933 |
> |
threadAssertSame(result, v1); |
934 |
> |
threadAssertNull(t); |
935 |
> |
a.getAndIncrement(); |
936 |
> |
}); |
937 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
938 |
> |
|
939 |
> |
checkCompletedNormally(g, v1); |
940 |
> |
checkCompletedNormally(f, v1); |
941 |
> |
assertEquals(1, a.get()); |
942 |
> |
}} |
943 |
> |
|
944 |
> |
/** |
945 |
> |
* whenComplete action executes on exceptional completion, propagating |
946 |
> |
* source result. |
947 |
> |
*/ |
948 |
> |
public void testWhenComplete_exceptionalCompletion() { |
949 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
950 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
951 |
> |
{ |
952 |
> |
final AtomicInteger a = new AtomicInteger(0); |
953 |
> |
final CFException ex = new CFException(); |
954 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
955 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
956 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
957 |
> |
(f, |
958 |
> |
(Integer result, Throwable t) -> { |
959 |
> |
m.checkExecutionMode(); |
960 |
> |
threadAssertNull(result); |
961 |
> |
threadAssertSame(t, ex); |
962 |
> |
a.getAndIncrement(); |
963 |
> |
}); |
964 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
965 |
> |
|
966 |
> |
checkCompletedWithWrappedException(g, ex); |
967 |
> |
checkCompletedExceptionally(f, ex); |
968 |
> |
assertEquals(1, a.get()); |
969 |
> |
}} |
970 |
> |
|
971 |
> |
/** |
972 |
> |
* whenComplete action executes on cancelled source, propagating |
973 |
> |
* CancellationException. |
974 |
> |
*/ |
975 |
> |
public void testWhenComplete_sourceCancelled() { |
976 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
977 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
978 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
979 |
> |
{ |
980 |
> |
final AtomicInteger a = new AtomicInteger(0); |
981 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
982 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
983 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
984 |
> |
(f, |
985 |
> |
(Integer result, Throwable t) -> { |
986 |
> |
m.checkExecutionMode(); |
987 |
> |
threadAssertNull(result); |
988 |
> |
threadAssertTrue(t instanceof CancellationException); |
989 |
> |
a.getAndIncrement(); |
990 |
> |
}); |
991 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
992 |
> |
|
993 |
> |
checkCompletedWithWrappedCancellationException(g); |
994 |
> |
checkCancelled(f); |
995 |
> |
assertEquals(1, a.get()); |
996 |
> |
}} |
997 |
> |
|
998 |
> |
/** |
999 |
> |
* If a whenComplete action throws an exception when triggered by |
1000 |
> |
* a normal completion, it completes exceptionally |
1001 |
> |
*/ |
1002 |
> |
public void testWhenComplete_sourceCompletedNormallyActionFailed() { |
1003 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1004 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1005 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1006 |
> |
{ |
1007 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1008 |
> |
final CFException ex = new CFException(); |
1009 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1010 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1011 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1012 |
> |
(f, |
1013 |
> |
(Integer result, Throwable t) -> { |
1014 |
> |
m.checkExecutionMode(); |
1015 |
> |
threadAssertSame(result, v1); |
1016 |
> |
threadAssertNull(t); |
1017 |
> |
a.getAndIncrement(); |
1018 |
> |
throw ex; |
1019 |
> |
}); |
1020 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1021 |
> |
|
1022 |
> |
checkCompletedWithWrappedException(g, ex); |
1023 |
> |
checkCompletedNormally(f, v1); |
1024 |
> |
assertEquals(1, a.get()); |
1025 |
> |
}} |
1026 |
> |
|
1027 |
> |
/** |
1028 |
> |
* If a whenComplete action throws an exception when triggered by |
1029 |
> |
* a source completion that also throws an exception, the source |
1030 |
> |
* exception takes precedence (unlike handle) |
1031 |
> |
*/ |
1032 |
> |
public void testWhenComplete_sourceFailedActionFailed() { |
1033 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1034 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1035 |
> |
{ |
1036 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1037 |
> |
final CFException ex1 = new CFException(); |
1038 |
> |
final CFException ex2 = new CFException(); |
1039 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1040 |
> |
|
1041 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1042 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1043 |
> |
(f, |
1044 |
> |
(Integer result, Throwable t) -> { |
1045 |
> |
m.checkExecutionMode(); |
1046 |
> |
threadAssertSame(t, ex1); |
1047 |
> |
threadAssertNull(result); |
1048 |
> |
a.getAndIncrement(); |
1049 |
> |
throw ex2; |
1050 |
> |
}); |
1051 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1052 |
> |
|
1053 |
> |
checkCompletedWithWrappedException(g, ex1); |
1054 |
> |
checkCompletedExceptionally(f, ex1); |
1055 |
> |
if (testImplementationDetails) { |
1056 |
> |
assertEquals(1, ex1.getSuppressed().length); |
1057 |
> |
assertSame(ex2, ex1.getSuppressed()[0]); |
1058 |
> |
} |
1059 |
|
assertEquals(1, a.get()); |
1060 |
|
}} |
1061 |
|
|
1070 |
|
{ |
1071 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1072 |
|
final AtomicInteger a = new AtomicInteger(0); |
1073 |
< |
if (!createIncomplete) f.complete(v1); |
1073 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1074 |
|
final CompletableFuture<Integer> g = m.handle |
1075 |
|
(f, |
1076 |
< |
(Integer x, Throwable t) -> { |
1077 |
< |
threadAssertSame(x, v1); |
1076 |
> |
(Integer result, Throwable t) -> { |
1077 |
> |
m.checkExecutionMode(); |
1078 |
> |
threadAssertSame(result, v1); |
1079 |
|
threadAssertNull(t); |
1080 |
|
a.getAndIncrement(); |
1081 |
|
return inc(v1); |
1082 |
|
}); |
1083 |
< |
if (createIncomplete) f.complete(v1); |
1083 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1084 |
|
|
1085 |
|
checkCompletedNormally(g, inc(v1)); |
1086 |
|
checkCompletedNormally(f, v1); |
1102 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1103 |
|
final CompletableFuture<Integer> g = m.handle |
1104 |
|
(f, |
1105 |
< |
(Integer x, Throwable t) -> { |
1106 |
< |
threadAssertNull(x); |
1105 |
> |
(Integer result, Throwable t) -> { |
1106 |
> |
m.checkExecutionMode(); |
1107 |
> |
threadAssertNull(result); |
1108 |
|
threadAssertSame(t, ex); |
1109 |
|
a.getAndIncrement(); |
1110 |
|
return v1; |
1112 |
|
if (createIncomplete) f.completeExceptionally(ex); |
1113 |
|
|
1114 |
|
checkCompletedNormally(g, v1); |
1115 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1115 |
> |
checkCompletedExceptionally(f, ex); |
1116 |
|
assertEquals(1, a.get()); |
1117 |
|
}} |
1118 |
|
|
1131 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1132 |
|
final CompletableFuture<Integer> g = m.handle |
1133 |
|
(f, |
1134 |
< |
(Integer x, Throwable t) -> { |
1135 |
< |
threadAssertNull(x); |
1134 |
> |
(Integer result, Throwable t) -> { |
1135 |
> |
m.checkExecutionMode(); |
1136 |
> |
threadAssertNull(result); |
1137 |
|
threadAssertTrue(t instanceof CancellationException); |
1138 |
|
a.getAndIncrement(); |
1139 |
|
return v1; |
1146 |
|
}} |
1147 |
|
|
1148 |
|
/** |
1149 |
< |
* handle result completes exceptionally if action does |
1149 |
> |
* If a "handle action" throws an exception when triggered by |
1150 |
> |
* a normal completion, it completes exceptionally |
1151 |
|
*/ |
1152 |
< |
public void testHandle_sourceFailedActionFailed() { |
1152 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1153 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1154 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1155 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1156 |
|
{ |
1157 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1158 |
|
final AtomicInteger a = new AtomicInteger(0); |
1159 |
< |
final CFException ex1 = new CFException(); |
1160 |
< |
final CFException ex2 = new CFException(); |
868 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
1159 |
> |
final CFException ex = new CFException(); |
1160 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1161 |
|
final CompletableFuture<Integer> g = m.handle |
1162 |
|
(f, |
1163 |
< |
(Integer x, Throwable t) -> { |
1164 |
< |
threadAssertNull(x); |
1165 |
< |
threadAssertSame(ex1, t); |
1163 |
> |
(Integer result, Throwable t) -> { |
1164 |
> |
m.checkExecutionMode(); |
1165 |
> |
threadAssertSame(result, v1); |
1166 |
> |
threadAssertNull(t); |
1167 |
|
a.getAndIncrement(); |
1168 |
< |
throw ex2; |
1168 |
> |
throw ex; |
1169 |
|
}); |
1170 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
1170 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1171 |
|
|
1172 |
< |
checkCompletedWithWrappedCFException(g, ex2); |
1173 |
< |
checkCompletedWithWrappedCFException(f, ex1); |
1172 |
> |
checkCompletedWithWrappedException(g, ex); |
1173 |
> |
checkCompletedNormally(f, v1); |
1174 |
|
assertEquals(1, a.get()); |
1175 |
|
}} |
1176 |
|
|
1177 |
< |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1178 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1177 |
> |
/** |
1178 |
> |
* If a "handle action" throws an exception when triggered by |
1179 |
> |
* a source completion that also throws an exception, the action |
1180 |
> |
* exception takes precedence (unlike whenComplete) |
1181 |
> |
*/ |
1182 |
> |
public void testHandle_sourceFailedActionFailed() { |
1183 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1184 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1184 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1185 |
|
{ |
889 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1186 |
|
final AtomicInteger a = new AtomicInteger(0); |
1187 |
< |
final CFException ex = new CFException(); |
1188 |
< |
if (!createIncomplete) f.complete(v1); |
1187 |
> |
final CFException ex1 = new CFException(); |
1188 |
> |
final CFException ex2 = new CFException(); |
1189 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1190 |
> |
|
1191 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1192 |
|
final CompletableFuture<Integer> g = m.handle |
1193 |
|
(f, |
1194 |
< |
(Integer x, Throwable t) -> { |
1195 |
< |
threadAssertSame(x, v1); |
1196 |
< |
threadAssertNull(t); |
1194 |
> |
(Integer result, Throwable t) -> { |
1195 |
> |
m.checkExecutionMode(); |
1196 |
> |
threadAssertNull(result); |
1197 |
> |
threadAssertSame(ex1, t); |
1198 |
|
a.getAndIncrement(); |
1199 |
< |
throw ex; |
1199 |
> |
throw ex2; |
1200 |
|
}); |
1201 |
< |
if (createIncomplete) f.complete(v1); |
1201 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1202 |
|
|
1203 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1204 |
< |
checkCompletedNormally(f, v1); |
1203 |
> |
checkCompletedWithWrappedException(g, ex2); |
1204 |
> |
checkCompletedExceptionally(f, ex1); |
1205 |
|
assertEquals(1, a.get()); |
1206 |
|
}} |
1207 |
|
|
1208 |
|
/** |
1209 |
|
* runAsync completes after running Runnable |
1210 |
|
*/ |
1211 |
< |
public void testRunAsync() { |
1212 |
< |
Noop r = new Noop(); |
1213 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
1214 |
< |
assertNull(f.join()); |
1215 |
< |
assertEquals(1, r.invocationCount); |
1216 |
< |
checkCompletedNormally(f, null); |
1217 |
< |
} |
1218 |
< |
|
1219 |
< |
/** |
920 |
< |
* runAsync with executor completes after running Runnable |
921 |
< |
*/ |
922 |
< |
public void testRunAsync2() { |
923 |
< |
Noop r = new Noop(); |
924 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
925 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
1211 |
> |
public void testRunAsync_normalCompletion() { |
1212 |
> |
ExecutionMode[] executionModes = { |
1213 |
> |
ExecutionMode.ASYNC, |
1214 |
> |
ExecutionMode.EXECUTOR, |
1215 |
> |
}; |
1216 |
> |
for (ExecutionMode m : executionModes) |
1217 |
> |
{ |
1218 |
> |
final Noop r = new Noop(m); |
1219 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1220 |
|
assertNull(f.join()); |
927 |
– |
assertEquals(1, r.invocationCount); |
1221 |
|
checkCompletedNormally(f, null); |
1222 |
< |
assertEquals(1, exec.count.get()); |
1223 |
< |
} |
1222 |
> |
r.assertInvoked(); |
1223 |
> |
}} |
1224 |
|
|
1225 |
|
/** |
1226 |
|
* failing runAsync completes exceptionally after running Runnable |
1227 |
|
*/ |
1228 |
< |
public void testRunAsync3() { |
1229 |
< |
FailingRunnable r = new FailingRunnable(); |
1230 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
1231 |
< |
checkCompletedWithWrappedCFException(f); |
1232 |
< |
assertEquals(1, r.invocationCount); |
1233 |
< |
} |
1228 |
> |
public void testRunAsync_exceptionalCompletion() { |
1229 |
> |
ExecutionMode[] executionModes = { |
1230 |
> |
ExecutionMode.ASYNC, |
1231 |
> |
ExecutionMode.EXECUTOR, |
1232 |
> |
}; |
1233 |
> |
for (ExecutionMode m : executionModes) |
1234 |
> |
{ |
1235 |
> |
final FailingRunnable r = new FailingRunnable(m); |
1236 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1237 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1238 |
> |
r.assertInvoked(); |
1239 |
> |
}} |
1240 |
|
|
1241 |
< |
/** |
1242 |
< |
* supplyAsync completes with result of supplier |
1243 |
< |
*/ |
1244 |
< |
public void testSupplyAsync() { |
1245 |
< |
CompletableFuture<Integer> f; |
1246 |
< |
f = CompletableFuture.supplyAsync(supplyOne); |
1247 |
< |
assertEquals(f.join(), one); |
1248 |
< |
checkCompletedNormally(f, one); |
1241 |
> |
@SuppressWarnings("FutureReturnValueIgnored") |
1242 |
> |
public void testRunAsync_rejectingExecutor() { |
1243 |
> |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1244 |
> |
try { |
1245 |
> |
CompletableFuture.runAsync(() -> {}, e); |
1246 |
> |
shouldThrow(); |
1247 |
> |
} catch (Throwable t) { |
1248 |
> |
assertSame(e.ex, t); |
1249 |
> |
} |
1250 |
> |
|
1251 |
> |
assertEquals(1, e.count.get()); |
1252 |
|
} |
1253 |
|
|
1254 |
|
/** |
1255 |
< |
* supplyAsync with executor completes with result of supplier |
1255 |
> |
* supplyAsync completes with result of supplier |
1256 |
|
*/ |
1257 |
< |
public void testSupplyAsync2() { |
1258 |
< |
CompletableFuture<Integer> f; |
1259 |
< |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
1260 |
< |
assertEquals(f.join(), one); |
1261 |
< |
checkCompletedNormally(f, one); |
1262 |
< |
} |
1257 |
> |
public void testSupplyAsync_normalCompletion() { |
1258 |
> |
ExecutionMode[] executionModes = { |
1259 |
> |
ExecutionMode.ASYNC, |
1260 |
> |
ExecutionMode.EXECUTOR, |
1261 |
> |
}; |
1262 |
> |
for (ExecutionMode m : executionModes) |
1263 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1264 |
> |
{ |
1265 |
> |
final IntegerSupplier r = new IntegerSupplier(m, v1); |
1266 |
> |
final CompletableFuture<Integer> f = m.supplyAsync(r); |
1267 |
> |
assertSame(v1, f.join()); |
1268 |
> |
checkCompletedNormally(f, v1); |
1269 |
> |
r.assertInvoked(); |
1270 |
> |
}} |
1271 |
|
|
1272 |
|
/** |
1273 |
|
* Failing supplyAsync completes exceptionally |
1274 |
|
*/ |
1275 |
< |
public void testSupplyAsync3() { |
1276 |
< |
FailingSupplier r = new FailingSupplier(); |
1277 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r); |
1278 |
< |
checkCompletedWithWrappedCFException(f); |
1279 |
< |
assertEquals(1, r.invocationCount); |
1275 |
> |
public void testSupplyAsync_exceptionalCompletion() { |
1276 |
> |
ExecutionMode[] executionModes = { |
1277 |
> |
ExecutionMode.ASYNC, |
1278 |
> |
ExecutionMode.EXECUTOR, |
1279 |
> |
}; |
1280 |
> |
for (ExecutionMode m : executionModes) |
1281 |
> |
{ |
1282 |
> |
FailingSupplier r = new FailingSupplier(m); |
1283 |
> |
CompletableFuture<Integer> f = m.supplyAsync(r); |
1284 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1285 |
> |
r.assertInvoked(); |
1286 |
> |
}} |
1287 |
> |
|
1288 |
> |
@SuppressWarnings("FutureReturnValueIgnored") |
1289 |
> |
public void testSupplyAsync_rejectingExecutor() { |
1290 |
> |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1291 |
> |
try { |
1292 |
> |
CompletableFuture.supplyAsync(() -> null, e); |
1293 |
> |
shouldThrow(); |
1294 |
> |
} catch (Throwable t) { |
1295 |
> |
assertSame(e.ex, t); |
1296 |
> |
} |
1297 |
> |
|
1298 |
> |
assertEquals(1, e.count.get()); |
1299 |
|
} |
1300 |
|
|
1301 |
|
// seq completion methods |
1305 |
|
*/ |
1306 |
|
public void testThenRun_normalCompletion() { |
1307 |
|
for (ExecutionMode m : ExecutionMode.values()) |
979 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1308 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1309 |
|
{ |
1310 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1311 |
< |
final Noop r = new Noop(); |
1312 |
< |
if (!createIncomplete) f.complete(v1); |
985 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
986 |
< |
if (createIncomplete) { |
987 |
< |
checkIncomplete(g); |
988 |
< |
f.complete(v1); |
989 |
< |
} |
1311 |
> |
final Noop[] rs = new Noop[6]; |
1312 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1313 |
|
|
1314 |
< |
checkCompletedNormally(g, null); |
1314 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1315 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1316 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1317 |
> |
checkIncomplete(h0); |
1318 |
> |
checkIncomplete(h1); |
1319 |
> |
checkIncomplete(h2); |
1320 |
> |
assertTrue(f.complete(v1)); |
1321 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1322 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1323 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1324 |
> |
|
1325 |
> |
checkCompletedNormally(h0, null); |
1326 |
> |
checkCompletedNormally(h1, null); |
1327 |
> |
checkCompletedNormally(h2, null); |
1328 |
> |
checkCompletedNormally(h3, null); |
1329 |
> |
checkCompletedNormally(h4, null); |
1330 |
> |
checkCompletedNormally(h5, null); |
1331 |
|
checkCompletedNormally(f, v1); |
1332 |
< |
assertEquals(1, r.invocationCount); |
1332 |
> |
for (Noop r : rs) r.assertInvoked(); |
1333 |
|
}} |
1334 |
|
|
1335 |
|
/** |
1338 |
|
*/ |
1339 |
|
public void testThenRun_exceptionalCompletion() { |
1340 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1002 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1341 |
|
{ |
1342 |
|
final CFException ex = new CFException(); |
1343 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1344 |
< |
final Noop r = new Noop(); |
1345 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1008 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1009 |
< |
if (createIncomplete) { |
1010 |
< |
checkIncomplete(g); |
1011 |
< |
f.completeExceptionally(ex); |
1012 |
< |
} |
1344 |
> |
final Noop[] rs = new Noop[6]; |
1345 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1346 |
|
|
1347 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1348 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1349 |
< |
assertEquals(0, r.invocationCount); |
1347 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1348 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1349 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1350 |
> |
checkIncomplete(h0); |
1351 |
> |
checkIncomplete(h1); |
1352 |
> |
checkIncomplete(h2); |
1353 |
> |
assertTrue(f.completeExceptionally(ex)); |
1354 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1355 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1356 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1357 |
> |
|
1358 |
> |
checkCompletedWithWrappedException(h0, ex); |
1359 |
> |
checkCompletedWithWrappedException(h1, ex); |
1360 |
> |
checkCompletedWithWrappedException(h2, ex); |
1361 |
> |
checkCompletedWithWrappedException(h3, ex); |
1362 |
> |
checkCompletedWithWrappedException(h4, ex); |
1363 |
> |
checkCompletedWithWrappedException(h5, ex); |
1364 |
> |
checkCompletedExceptionally(f, ex); |
1365 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1366 |
|
}} |
1367 |
|
|
1368 |
|
/** |
1370 |
|
*/ |
1371 |
|
public void testThenRun_sourceCancelled() { |
1372 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1024 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1373 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1374 |
|
{ |
1375 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1376 |
< |
final Noop r = new Noop(); |
1377 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1030 |
< |
final CompletableFuture<Void> g = f.thenRun(r); |
1031 |
< |
if (createIncomplete) { |
1032 |
< |
checkIncomplete(g); |
1033 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1034 |
< |
} |
1376 |
> |
final Noop[] rs = new Noop[6]; |
1377 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1378 |
|
|
1379 |
< |
checkCompletedWithWrappedCancellationException(g); |
1379 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1380 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1381 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1382 |
> |
checkIncomplete(h0); |
1383 |
> |
checkIncomplete(h1); |
1384 |
> |
checkIncomplete(h2); |
1385 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1386 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1387 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1388 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1389 |
> |
|
1390 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1391 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1392 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1393 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1394 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1395 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1396 |
|
checkCancelled(f); |
1397 |
< |
assertEquals(0, r.invocationCount); |
1397 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1398 |
|
}} |
1399 |
|
|
1400 |
|
/** |
1402 |
|
*/ |
1403 |
|
public void testThenRun_actionFailed() { |
1404 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1046 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1405 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1406 |
|
{ |
1407 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1408 |
< |
final FailingRunnable r = new FailingRunnable(); |
1409 |
< |
if (!createIncomplete) f.complete(v1); |
1052 |
< |
final CompletableFuture<Void> g = f.thenRun(r); |
1053 |
< |
if (createIncomplete) { |
1054 |
< |
checkIncomplete(g); |
1055 |
< |
f.complete(v1); |
1056 |
< |
} |
1408 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1409 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1410 |
|
|
1411 |
< |
checkCompletedWithWrappedCFException(g); |
1411 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1412 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1413 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1414 |
> |
assertTrue(f.complete(v1)); |
1415 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1416 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1417 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1418 |
> |
|
1419 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1420 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1421 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1422 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1423 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
1424 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
1425 |
|
checkCompletedNormally(f, v1); |
1426 |
|
}} |
1427 |
|
|
1430 |
|
*/ |
1431 |
|
public void testThenApply_normalCompletion() { |
1432 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1067 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1433 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1434 |
|
{ |
1435 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1436 |
< |
final IncFunction r = new IncFunction(); |
1437 |
< |
if (!createIncomplete) f.complete(v1); |
1073 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1074 |
< |
if (createIncomplete) { |
1075 |
< |
checkIncomplete(g); |
1076 |
< |
f.complete(v1); |
1077 |
< |
} |
1436 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1437 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1438 |
|
|
1439 |
< |
checkCompletedNormally(g, inc(v1)); |
1439 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1440 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1441 |
> |
checkIncomplete(h0); |
1442 |
> |
checkIncomplete(h1); |
1443 |
> |
assertTrue(f.complete(v1)); |
1444 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1445 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1446 |
> |
|
1447 |
> |
checkCompletedNormally(h0, inc(v1)); |
1448 |
> |
checkCompletedNormally(h1, inc(v1)); |
1449 |
> |
checkCompletedNormally(h2, inc(v1)); |
1450 |
> |
checkCompletedNormally(h3, inc(v1)); |
1451 |
|
checkCompletedNormally(f, v1); |
1452 |
< |
assertEquals(1, r.invocationCount); |
1452 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1453 |
|
}} |
1454 |
|
|
1455 |
|
/** |
1458 |
|
*/ |
1459 |
|
public void testThenApply_exceptionalCompletion() { |
1460 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1090 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1461 |
|
{ |
1462 |
|
final CFException ex = new CFException(); |
1463 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1464 |
< |
final IncFunction r = new IncFunction(); |
1465 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1096 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1097 |
< |
if (createIncomplete) { |
1098 |
< |
checkIncomplete(g); |
1099 |
< |
f.completeExceptionally(ex); |
1100 |
< |
} |
1464 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1465 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1466 |
|
|
1467 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1468 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1469 |
< |
assertEquals(0, r.invocationCount); |
1467 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1468 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1469 |
> |
assertTrue(f.completeExceptionally(ex)); |
1470 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1471 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1472 |
> |
|
1473 |
> |
checkCompletedWithWrappedException(h0, ex); |
1474 |
> |
checkCompletedWithWrappedException(h1, ex); |
1475 |
> |
checkCompletedWithWrappedException(h2, ex); |
1476 |
> |
checkCompletedWithWrappedException(h3, ex); |
1477 |
> |
checkCompletedExceptionally(f, ex); |
1478 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1479 |
|
}} |
1480 |
|
|
1481 |
|
/** |
1483 |
|
*/ |
1484 |
|
public void testThenApply_sourceCancelled() { |
1485 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1112 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1486 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1487 |
|
{ |
1488 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1489 |
< |
final IncFunction r = new IncFunction(); |
1490 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1118 |
< |
final CompletableFuture<Integer> g = f.thenApply(r); |
1119 |
< |
if (createIncomplete) { |
1120 |
< |
checkIncomplete(g); |
1121 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1122 |
< |
} |
1489 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1490 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1491 |
|
|
1492 |
< |
checkCompletedWithWrappedCancellationException(g); |
1492 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1493 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1494 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1495 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1496 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1497 |
> |
|
1498 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1499 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1500 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1501 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1502 |
|
checkCancelled(f); |
1503 |
< |
assertEquals(0, r.invocationCount); |
1503 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1504 |
|
}} |
1505 |
|
|
1506 |
|
/** |
1508 |
|
*/ |
1509 |
|
public void testThenApply_actionFailed() { |
1510 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1134 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1511 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1512 |
|
{ |
1513 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1514 |
< |
final FailingFunction r = new FailingFunction(); |
1515 |
< |
if (!createIncomplete) f.complete(v1); |
1140 |
< |
final CompletableFuture<Integer> g = f.thenApply(r); |
1141 |
< |
if (createIncomplete) { |
1142 |
< |
checkIncomplete(g); |
1143 |
< |
f.complete(v1); |
1144 |
< |
} |
1514 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1515 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1516 |
|
|
1517 |
< |
checkCompletedWithWrappedCFException(g); |
1517 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1518 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1519 |
> |
assertTrue(f.complete(v1)); |
1520 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1521 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1522 |
> |
|
1523 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1524 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1525 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1526 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1527 |
|
checkCompletedNormally(f, v1); |
1528 |
|
}} |
1529 |
|
|
1532 |
|
*/ |
1533 |
|
public void testThenAccept_normalCompletion() { |
1534 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1155 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1535 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1536 |
|
{ |
1537 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1538 |
< |
final IncAction r = new IncAction(); |
1539 |
< |
if (!createIncomplete) f.complete(v1); |
1161 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1162 |
< |
if (createIncomplete) { |
1163 |
< |
checkIncomplete(g); |
1164 |
< |
f.complete(v1); |
1165 |
< |
} |
1538 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1539 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1540 |
|
|
1541 |
< |
checkCompletedNormally(g, null); |
1541 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1542 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1543 |
> |
checkIncomplete(h0); |
1544 |
> |
checkIncomplete(h1); |
1545 |
> |
assertTrue(f.complete(v1)); |
1546 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1547 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1548 |
> |
|
1549 |
> |
checkCompletedNormally(h0, null); |
1550 |
> |
checkCompletedNormally(h1, null); |
1551 |
> |
checkCompletedNormally(h2, null); |
1552 |
> |
checkCompletedNormally(h3, null); |
1553 |
|
checkCompletedNormally(f, v1); |
1554 |
< |
assertEquals(1, r.invocationCount); |
1170 |
< |
assertEquals(inc(v1), r.value); |
1554 |
> |
for (NoopConsumer r : rs) r.assertValue(v1); |
1555 |
|
}} |
1556 |
|
|
1557 |
|
/** |
1560 |
|
*/ |
1561 |
|
public void testThenAccept_exceptionalCompletion() { |
1562 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1179 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1563 |
|
{ |
1564 |
|
final CFException ex = new CFException(); |
1565 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1566 |
< |
final IncAction r = new IncAction(); |
1567 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1185 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1186 |
< |
if (createIncomplete) { |
1187 |
< |
checkIncomplete(g); |
1188 |
< |
f.completeExceptionally(ex); |
1189 |
< |
} |
1566 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1567 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1568 |
|
|
1569 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1570 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1571 |
< |
assertEquals(0, r.invocationCount); |
1569 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1570 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1571 |
> |
assertTrue(f.completeExceptionally(ex)); |
1572 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1573 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1574 |
> |
|
1575 |
> |
checkCompletedWithWrappedException(h0, ex); |
1576 |
> |
checkCompletedWithWrappedException(h1, ex); |
1577 |
> |
checkCompletedWithWrappedException(h2, ex); |
1578 |
> |
checkCompletedWithWrappedException(h3, ex); |
1579 |
> |
checkCompletedExceptionally(f, ex); |
1580 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1581 |
|
}} |
1582 |
|
|
1583 |
|
/** |
1584 |
< |
* thenAccept result completes exceptionally if action does |
1584 |
> |
* thenAccept result completes exceptionally if source cancelled |
1585 |
|
*/ |
1586 |
< |
public void testThenAccept_actionFailed() { |
1586 |
> |
public void testThenAccept_sourceCancelled() { |
1587 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1588 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
1202 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1588 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1589 |
|
{ |
1590 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1591 |
< |
final FailingConsumer r = new FailingConsumer(); |
1592 |
< |
if (!createIncomplete) f.complete(v1); |
1207 |
< |
final CompletableFuture<Void> g = f.thenAccept(r); |
1208 |
< |
if (createIncomplete) { |
1209 |
< |
checkIncomplete(g); |
1210 |
< |
f.complete(v1); |
1211 |
< |
} |
1591 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1592 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1593 |
|
|
1594 |
< |
checkCompletedWithWrappedCFException(g); |
1595 |
< |
checkCompletedNormally(f, v1); |
1594 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1595 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1596 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1597 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1598 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1599 |
> |
|
1600 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1601 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1602 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1603 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1604 |
> |
checkCancelled(f); |
1605 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1606 |
|
}} |
1607 |
|
|
1608 |
|
/** |
1609 |
< |
* thenAccept result completes exceptionally if source cancelled |
1609 |
> |
* thenAccept result completes exceptionally if action does |
1610 |
|
*/ |
1611 |
< |
public void testThenAccept_sourceCancelled() { |
1611 |
> |
public void testThenAccept_actionFailed() { |
1612 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1613 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
1223 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1613 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1614 |
|
{ |
1615 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1616 |
< |
final IncAction r = new IncAction(); |
1617 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1228 |
< |
final CompletableFuture<Void> g = f.thenAccept(r); |
1229 |
< |
if (createIncomplete) { |
1230 |
< |
checkIncomplete(g); |
1231 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1232 |
< |
} |
1616 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1617 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1618 |
|
|
1619 |
< |
checkCompletedWithWrappedCancellationException(g); |
1620 |
< |
checkCancelled(f); |
1621 |
< |
assertEquals(0, r.invocationCount); |
1619 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1620 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1621 |
> |
assertTrue(f.complete(v1)); |
1622 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1623 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1624 |
> |
|
1625 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1626 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1627 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1628 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1629 |
> |
checkCompletedNormally(f, v1); |
1630 |
|
}} |
1631 |
|
|
1632 |
|
/** |
1635 |
|
*/ |
1636 |
|
public void testThenCombine_normalCompletion() { |
1637 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1245 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1638 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1639 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1640 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1641 |
|
{ |
1642 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1643 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1644 |
< |
final SubtractFunction r = new SubtractFunction(); |
1644 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1645 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1646 |
|
|
1647 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1648 |
< |
if (!createIncomplete) |
1649 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1650 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1651 |
< |
if (createIncomplete) { |
1652 |
< |
checkIncomplete(h); |
1653 |
< |
assertEquals(0, r.invocationCount); |
1654 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1655 |
< |
} |
1647 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1648 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1649 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1650 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1651 |
> |
|
1652 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1653 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1654 |
> |
assertTrue(fst.complete(w1)); |
1655 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1656 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1657 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1658 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1659 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1660 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1661 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1662 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1663 |
> |
assertTrue(snd.complete(w2)); |
1664 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1665 |
> |
|
1666 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1667 |
> |
checkCompletedNormally(h2, subtract(v1, v2)); |
1668 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1669 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1670 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1671 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1672 |
|
|
1264 |
– |
checkCompletedNormally(h, subtract(v1, v2)); |
1673 |
|
checkCompletedNormally(f, v1); |
1674 |
|
checkCompletedNormally(g, v2); |
1267 |
– |
assertEquals(1, r.invocationCount); |
1675 |
|
}} |
1676 |
|
|
1677 |
|
/** |
1678 |
|
* thenCombine result completes exceptionally after exceptional |
1679 |
|
* completion of either source |
1680 |
|
*/ |
1681 |
< |
public void testThenCombine_exceptionalCompletion() { |
1681 |
> |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
1682 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1276 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1683 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1684 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1685 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1686 |
|
{ |
1687 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1688 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1689 |
|
final CFException ex = new CFException(); |
1690 |
< |
final SubtractFunction r = new SubtractFunction(); |
1691 |
< |
|
1692 |
< |
(fFirst ? f : g).complete(v1); |
1693 |
< |
if (!createIncomplete) |
1694 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1695 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1696 |
< |
if (createIncomplete) { |
1697 |
< |
checkIncomplete(h); |
1698 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1699 |
< |
} |
1700 |
< |
|
1701 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1702 |
< |
assertEquals(0, r.invocationCount); |
1703 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1704 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1705 |
< |
}} |
1706 |
< |
|
1707 |
< |
/** |
1708 |
< |
* thenCombine result completes exceptionally if action does |
1709 |
< |
*/ |
1710 |
< |
public void testThenCombine_actionFailed() { |
1711 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1712 |
< |
for (boolean fFirst : new boolean[] { true, false }) |
1713 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1714 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1715 |
< |
{ |
1716 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1717 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1718 |
< |
final FailingBiFunction r = new FailingBiFunction(); |
1312 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1313 |
< |
|
1314 |
< |
if (fFirst) { |
1315 |
< |
f.complete(v1); |
1316 |
< |
g.complete(v2); |
1317 |
< |
} else { |
1318 |
< |
g.complete(v2); |
1319 |
< |
f.complete(v1); |
1320 |
< |
} |
1321 |
< |
|
1322 |
< |
checkCompletedWithWrappedCFException(h); |
1323 |
< |
checkCompletedNormally(f, v1); |
1324 |
< |
checkCompletedNormally(g, v2); |
1690 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1691 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1692 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1693 |
> |
|
1694 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1695 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1696 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1697 |
> |
() -> fst.completeExceptionally(ex) : |
1698 |
> |
() -> fst.complete(v1); |
1699 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1700 |
> |
() -> snd.complete(v1) : |
1701 |
> |
() -> snd.completeExceptionally(ex); |
1702 |
> |
|
1703 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1704 |
> |
assertTrue(complete1.call()); |
1705 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1706 |
> |
checkIncomplete(h1); |
1707 |
> |
checkIncomplete(h2); |
1708 |
> |
assertTrue(complete2.call()); |
1709 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1710 |
> |
|
1711 |
> |
checkCompletedWithWrappedException(h1, ex); |
1712 |
> |
checkCompletedWithWrappedException(h2, ex); |
1713 |
> |
checkCompletedWithWrappedException(h3, ex); |
1714 |
> |
r1.assertNotInvoked(); |
1715 |
> |
r2.assertNotInvoked(); |
1716 |
> |
r3.assertNotInvoked(); |
1717 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1718 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1719 |
|
}} |
1720 |
|
|
1721 |
|
/** |
1722 |
|
* thenCombine result completes exceptionally if either source cancelled |
1723 |
|
*/ |
1724 |
< |
public void testThenCombine_sourceCancelled() { |
1724 |
> |
public void testThenCombine_sourceCancelled() throws Throwable { |
1725 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1726 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1333 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1727 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1728 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1729 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1730 |
|
{ |
1731 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1732 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1733 |
< |
final SubtractFunction r = new SubtractFunction(); |
1734 |
< |
|
1735 |
< |
(fFirst ? f : g).complete(v1); |
1736 |
< |
if (!createIncomplete) |
1737 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1738 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1739 |
< |
if (createIncomplete) { |
1740 |
< |
checkIncomplete(h); |
1741 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1742 |
< |
} |
1743 |
< |
|
1744 |
< |
checkCompletedWithWrappedCancellationException(h); |
1745 |
< |
checkCancelled(!fFirst ? f : g); |
1746 |
< |
assertEquals(0, r.invocationCount); |
1747 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1733 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1734 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1735 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1736 |
> |
|
1737 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1738 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1739 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1740 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1741 |
> |
() -> fst.complete(v1); |
1742 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1743 |
> |
() -> snd.complete(v1) : |
1744 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1745 |
> |
|
1746 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1747 |
> |
assertTrue(complete1.call()); |
1748 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1749 |
> |
checkIncomplete(h1); |
1750 |
> |
checkIncomplete(h2); |
1751 |
> |
assertTrue(complete2.call()); |
1752 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1753 |
> |
|
1754 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1755 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1756 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1757 |
> |
r1.assertNotInvoked(); |
1758 |
> |
r2.assertNotInvoked(); |
1759 |
> |
r3.assertNotInvoked(); |
1760 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1761 |
> |
checkCancelled(failFirst ? fst : snd); |
1762 |
|
}} |
1763 |
|
|
1764 |
|
/** |
1765 |
< |
* thenAcceptBoth result completes normally after normal |
1358 |
< |
* completion of sources |
1765 |
> |
* thenCombine result completes exceptionally if action does |
1766 |
|
*/ |
1767 |
< |
public void testThenAcceptBoth_normalCompletion1() { |
1361 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1362 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1363 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1364 |
< |
{ |
1365 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1366 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1367 |
< |
final SubtractAction r = new SubtractAction(); |
1368 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1369 |
< |
|
1370 |
< |
f.complete(v1); |
1371 |
< |
checkIncomplete(h); |
1372 |
< |
assertEquals(0, r.invocationCount); |
1373 |
< |
g.complete(v2); |
1374 |
< |
|
1375 |
< |
checkCompletedNormally(h, null); |
1376 |
< |
assertEquals(subtract(v1, v2), r.value); |
1377 |
< |
checkCompletedNormally(f, v1); |
1378 |
< |
checkCompletedNormally(g, v2); |
1379 |
< |
}} |
1380 |
< |
|
1381 |
< |
public void testThenAcceptBoth_normalCompletion2() { |
1382 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1383 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1384 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1385 |
< |
{ |
1386 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1387 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1388 |
< |
final SubtractAction r = new SubtractAction(); |
1389 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1390 |
< |
|
1391 |
< |
g.complete(v2); |
1392 |
< |
checkIncomplete(h); |
1393 |
< |
assertEquals(0, r.invocationCount); |
1394 |
< |
f.complete(v1); |
1395 |
< |
|
1396 |
< |
checkCompletedNormally(h, null); |
1397 |
< |
assertEquals(subtract(v1, v2), r.value); |
1398 |
< |
checkCompletedNormally(f, v1); |
1399 |
< |
checkCompletedNormally(g, v2); |
1400 |
< |
}} |
1401 |
< |
|
1402 |
< |
public void testThenAcceptBoth_normalCompletion3() { |
1767 |
> |
public void testThenCombine_actionFailed() { |
1768 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1769 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1770 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1771 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1772 |
|
{ |
1773 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1774 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1775 |
< |
final SubtractAction r = new SubtractAction(); |
1776 |
< |
|
1777 |
< |
g.complete(v2); |
1778 |
< |
f.complete(v1); |
1779 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1780 |
< |
|
1781 |
< |
checkCompletedNormally(h, null); |
1782 |
< |
assertEquals(subtract(v1, v2), r.value); |
1775 |
> |
final FailingBiFunction r1 = new FailingBiFunction(m); |
1776 |
> |
final FailingBiFunction r2 = new FailingBiFunction(m); |
1777 |
> |
final FailingBiFunction r3 = new FailingBiFunction(m); |
1778 |
> |
|
1779 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1780 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1781 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1782 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1783 |
> |
|
1784 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1785 |
> |
assertTrue(fst.complete(w1)); |
1786 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1787 |
> |
assertTrue(snd.complete(w2)); |
1788 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1789 |
> |
|
1790 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1791 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1792 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1793 |
> |
r1.assertInvoked(); |
1794 |
> |
r2.assertInvoked(); |
1795 |
> |
r3.assertInvoked(); |
1796 |
|
checkCompletedNormally(f, v1); |
1797 |
|
checkCompletedNormally(g, v2); |
1798 |
|
}} |
1799 |
|
|
1800 |
< |
public void testThenAcceptBoth_normalCompletion4() { |
1800 |
> |
/** |
1801 |
> |
* thenAcceptBoth result completes normally after normal |
1802 |
> |
* completion of sources |
1803 |
> |
*/ |
1804 |
> |
public void testThenAcceptBoth_normalCompletion() { |
1805 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1806 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1807 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1808 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1809 |
|
{ |
1810 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1811 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1812 |
< |
final SubtractAction r = new SubtractAction(); |
1813 |
< |
|
1814 |
< |
f.complete(v1); |
1815 |
< |
g.complete(v2); |
1816 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1817 |
< |
|
1818 |
< |
checkCompletedNormally(h, null); |
1819 |
< |
assertEquals(subtract(v1, v2), r.value); |
1812 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1813 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1814 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1815 |
> |
|
1816 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1817 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1818 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1819 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1820 |
> |
|
1821 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1822 |
> |
assertTrue(fst.complete(w1)); |
1823 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1824 |
> |
checkIncomplete(h1); |
1825 |
> |
checkIncomplete(h2); |
1826 |
> |
r1.assertNotInvoked(); |
1827 |
> |
r2.assertNotInvoked(); |
1828 |
> |
assertTrue(snd.complete(w2)); |
1829 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1830 |
> |
|
1831 |
> |
checkCompletedNormally(h1, null); |
1832 |
> |
checkCompletedNormally(h2, null); |
1833 |
> |
checkCompletedNormally(h3, null); |
1834 |
> |
r1.assertValue(subtract(v1, v2)); |
1835 |
> |
r2.assertValue(subtract(v1, v2)); |
1836 |
> |
r3.assertValue(subtract(v1, v2)); |
1837 |
|
checkCompletedNormally(f, v1); |
1838 |
|
checkCompletedNormally(g, v2); |
1839 |
|
}} |
1842 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1843 |
|
* completion of either source |
1844 |
|
*/ |
1845 |
< |
public void testThenAcceptBoth_exceptionalCompletion1() { |
1445 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1446 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1447 |
< |
{ |
1448 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1449 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1450 |
< |
final SubtractAction r = new SubtractAction(); |
1451 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1452 |
< |
final CFException ex = new CFException(); |
1453 |
< |
|
1454 |
< |
f.completeExceptionally(ex); |
1455 |
< |
checkIncomplete(h); |
1456 |
< |
g.complete(v1); |
1457 |
< |
|
1458 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1459 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1460 |
< |
assertEquals(0, r.invocationCount); |
1461 |
< |
checkCompletedNormally(g, v1); |
1462 |
< |
}} |
1463 |
< |
|
1464 |
< |
public void testThenAcceptBoth_exceptionalCompletion2() { |
1845 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1846 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1847 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1848 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1849 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1850 |
|
{ |
1851 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1852 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1470 |
– |
final SubtractAction r = new SubtractAction(); |
1471 |
– |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1853 |
|
final CFException ex = new CFException(); |
1854 |
< |
|
1855 |
< |
g.completeExceptionally(ex); |
1856 |
< |
checkIncomplete(h); |
1857 |
< |
f.complete(v1); |
1858 |
< |
|
1859 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1860 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1861 |
< |
assertEquals(0, r.invocationCount); |
1862 |
< |
checkCompletedNormally(f, v1); |
1854 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1855 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1856 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1857 |
> |
|
1858 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1859 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1860 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1861 |
> |
() -> fst.completeExceptionally(ex) : |
1862 |
> |
() -> fst.complete(v1); |
1863 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1864 |
> |
() -> snd.complete(v1) : |
1865 |
> |
() -> snd.completeExceptionally(ex); |
1866 |
> |
|
1867 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1868 |
> |
assertTrue(complete1.call()); |
1869 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1870 |
> |
checkIncomplete(h1); |
1871 |
> |
checkIncomplete(h2); |
1872 |
> |
assertTrue(complete2.call()); |
1873 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1874 |
> |
|
1875 |
> |
checkCompletedWithWrappedException(h1, ex); |
1876 |
> |
checkCompletedWithWrappedException(h2, ex); |
1877 |
> |
checkCompletedWithWrappedException(h3, ex); |
1878 |
> |
r1.assertNotInvoked(); |
1879 |
> |
r2.assertNotInvoked(); |
1880 |
> |
r3.assertNotInvoked(); |
1881 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1882 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1883 |
|
}} |
1884 |
|
|
1885 |
< |
public void testThenAcceptBoth_exceptionalCompletion3() { |
1885 |
> |
/** |
1886 |
> |
* thenAcceptBoth result completes exceptionally if either source cancelled |
1887 |
> |
*/ |
1888 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1889 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1890 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1891 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1892 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1893 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1894 |
|
{ |
1895 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1896 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1897 |
< |
final SubtractAction r = new SubtractAction(); |
1898 |
< |
final CFException ex = new CFException(); |
1899 |
< |
|
1900 |
< |
g.completeExceptionally(ex); |
1901 |
< |
f.complete(v1); |
1902 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1903 |
< |
|
1904 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1905 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1906 |
< |
assertEquals(0, r.invocationCount); |
1907 |
< |
checkCompletedNormally(f, v1); |
1908 |
< |
}} |
1909 |
< |
|
1910 |
< |
public void testThenAcceptBoth_exceptionalCompletion4() { |
1911 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1912 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1913 |
< |
{ |
1914 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1915 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1916 |
< |
final SubtractAction r = new SubtractAction(); |
1917 |
< |
final CFException ex = new CFException(); |
1918 |
< |
|
1919 |
< |
f.completeExceptionally(ex); |
1920 |
< |
g.complete(v1); |
1921 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1922 |
< |
|
1923 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1924 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1925 |
< |
assertEquals(0, r.invocationCount); |
1519 |
< |
checkCompletedNormally(g, v1); |
1897 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1898 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1899 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1900 |
> |
|
1901 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1902 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1903 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1904 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1905 |
> |
() -> fst.complete(v1); |
1906 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1907 |
> |
() -> snd.complete(v1) : |
1908 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1909 |
> |
|
1910 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1911 |
> |
assertTrue(complete1.call()); |
1912 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1913 |
> |
checkIncomplete(h1); |
1914 |
> |
checkIncomplete(h2); |
1915 |
> |
assertTrue(complete2.call()); |
1916 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1917 |
> |
|
1918 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1919 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1920 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1921 |
> |
r1.assertNotInvoked(); |
1922 |
> |
r2.assertNotInvoked(); |
1923 |
> |
r3.assertNotInvoked(); |
1924 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1925 |
> |
checkCancelled(failFirst ? fst : snd); |
1926 |
|
}} |
1927 |
|
|
1928 |
|
/** |
1929 |
|
* thenAcceptBoth result completes exceptionally if action does |
1930 |
|
*/ |
1931 |
< |
public void testThenAcceptBoth_actionFailed1() { |
1526 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1527 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1528 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1529 |
< |
{ |
1530 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1531 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1532 |
< |
final FailingBiConsumer r = new FailingBiConsumer(); |
1533 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1534 |
< |
|
1535 |
< |
f.complete(v1); |
1536 |
< |
checkIncomplete(h); |
1537 |
< |
g.complete(v2); |
1538 |
< |
|
1539 |
< |
checkCompletedWithWrappedCFException(h); |
1540 |
< |
checkCompletedNormally(f, v1); |
1541 |
< |
checkCompletedNormally(g, v2); |
1542 |
< |
}} |
1543 |
< |
|
1544 |
< |
public void testThenAcceptBoth_actionFailed2() { |
1931 |
> |
public void testThenAcceptBoth_actionFailed() { |
1932 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1933 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1934 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1935 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1936 |
|
{ |
1937 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1938 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1939 |
< |
final FailingBiConsumer r = new FailingBiConsumer(); |
1940 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1941 |
< |
|
1942 |
< |
g.complete(v2); |
1943 |
< |
checkIncomplete(h); |
1944 |
< |
f.complete(v1); |
1945 |
< |
|
1946 |
< |
checkCompletedWithWrappedCFException(h); |
1939 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1940 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1941 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1942 |
> |
|
1943 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1944 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1945 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1946 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1947 |
> |
|
1948 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1949 |
> |
assertTrue(fst.complete(w1)); |
1950 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1951 |
> |
assertTrue(snd.complete(w2)); |
1952 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1953 |
> |
|
1954 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1955 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1956 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1957 |
> |
r1.assertInvoked(); |
1958 |
> |
r2.assertInvoked(); |
1959 |
> |
r3.assertInvoked(); |
1960 |
|
checkCompletedNormally(f, v1); |
1961 |
|
checkCompletedNormally(g, v2); |
1962 |
|
}} |
1963 |
|
|
1964 |
|
/** |
1564 |
– |
* thenAcceptBoth result completes exceptionally if either source cancelled |
1565 |
– |
*/ |
1566 |
– |
public void testThenAcceptBoth_sourceCancelled1() { |
1567 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1568 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1569 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1570 |
– |
{ |
1571 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1572 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1573 |
– |
final SubtractAction r = new SubtractAction(); |
1574 |
– |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1575 |
– |
|
1576 |
– |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1577 |
– |
checkIncomplete(h); |
1578 |
– |
g.complete(v1); |
1579 |
– |
|
1580 |
– |
checkCompletedWithWrappedCancellationException(h); |
1581 |
– |
checkCancelled(f); |
1582 |
– |
assertEquals(0, r.invocationCount); |
1583 |
– |
checkCompletedNormally(g, v1); |
1584 |
– |
}} |
1585 |
– |
|
1586 |
– |
public void testThenAcceptBoth_sourceCancelled2() { |
1587 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1588 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1589 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1590 |
– |
{ |
1591 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1592 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1593 |
– |
final SubtractAction r = new SubtractAction(); |
1594 |
– |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1595 |
– |
|
1596 |
– |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1597 |
– |
checkIncomplete(h); |
1598 |
– |
f.complete(v1); |
1599 |
– |
|
1600 |
– |
checkCompletedWithWrappedCancellationException(h); |
1601 |
– |
checkCancelled(g); |
1602 |
– |
assertEquals(0, r.invocationCount); |
1603 |
– |
checkCompletedNormally(f, v1); |
1604 |
– |
}} |
1605 |
– |
|
1606 |
– |
public void testThenAcceptBoth_sourceCancelled3() { |
1607 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1608 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1609 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1610 |
– |
{ |
1611 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1612 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1613 |
– |
final SubtractAction r = new SubtractAction(); |
1614 |
– |
|
1615 |
– |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1616 |
– |
f.complete(v1); |
1617 |
– |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1618 |
– |
|
1619 |
– |
checkCompletedWithWrappedCancellationException(h); |
1620 |
– |
checkCancelled(g); |
1621 |
– |
assertEquals(0, r.invocationCount); |
1622 |
– |
checkCompletedNormally(f, v1); |
1623 |
– |
}} |
1624 |
– |
|
1625 |
– |
public void testThenAcceptBoth_sourceCancelled4() { |
1626 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1627 |
– |
for (boolean mayInterruptIfRunning : 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 SubtractAction r = new SubtractAction(); |
1633 |
– |
|
1634 |
– |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1635 |
– |
g.complete(v1); |
1636 |
– |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1637 |
– |
|
1638 |
– |
checkCompletedWithWrappedCancellationException(h); |
1639 |
– |
checkCancelled(f); |
1640 |
– |
assertEquals(0, r.invocationCount); |
1641 |
– |
checkCompletedNormally(g, v1); |
1642 |
– |
}} |
1643 |
– |
|
1644 |
– |
/** |
1965 |
|
* runAfterBoth result completes normally after normal |
1966 |
|
* completion of sources |
1967 |
|
*/ |
1968 |
< |
public void testRunAfterBoth_normalCompletion1() { |
1649 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1650 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1651 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1652 |
< |
{ |
1653 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1654 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1655 |
< |
final Noop r = new Noop(); |
1656 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1657 |
< |
|
1658 |
< |
f.complete(v1); |
1659 |
< |
checkIncomplete(h); |
1660 |
< |
assertEquals(0, r.invocationCount); |
1661 |
< |
g.complete(v2); |
1662 |
< |
|
1663 |
< |
checkCompletedNormally(h, null); |
1664 |
< |
assertEquals(1, r.invocationCount); |
1665 |
< |
checkCompletedNormally(f, v1); |
1666 |
< |
checkCompletedNormally(g, v2); |
1667 |
< |
}} |
1668 |
< |
|
1669 |
< |
public void testRunAfterBoth_normalCompletion2() { |
1670 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1671 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1672 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1673 |
< |
{ |
1674 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1675 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1676 |
< |
final Noop r = new Noop(); |
1677 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1678 |
< |
|
1679 |
< |
g.complete(v2); |
1680 |
< |
checkIncomplete(h); |
1681 |
< |
assertEquals(0, r.invocationCount); |
1682 |
< |
f.complete(v1); |
1683 |
< |
|
1684 |
< |
checkCompletedNormally(h, null); |
1685 |
< |
assertEquals(1, r.invocationCount); |
1686 |
< |
checkCompletedNormally(f, v1); |
1687 |
< |
checkCompletedNormally(g, v2); |
1688 |
< |
}} |
1689 |
< |
|
1690 |
< |
public void testRunAfterBoth_normalCompletion3() { |
1691 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1692 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1693 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1694 |
< |
{ |
1695 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1696 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1697 |
< |
final Noop r = new Noop(); |
1698 |
< |
|
1699 |
< |
g.complete(v2); |
1700 |
< |
f.complete(v1); |
1701 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1702 |
< |
|
1703 |
< |
checkCompletedNormally(h, null); |
1704 |
< |
assertEquals(1, r.invocationCount); |
1705 |
< |
checkCompletedNormally(f, v1); |
1706 |
< |
checkCompletedNormally(g, v2); |
1707 |
< |
}} |
1708 |
< |
|
1709 |
< |
public void testRunAfterBoth_normalCompletion4() { |
1968 |
> |
public void testRunAfterBoth_normalCompletion() { |
1969 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1970 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1971 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1972 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1973 |
|
{ |
1974 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1975 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1976 |
< |
final Noop r = new Noop(); |
1977 |
< |
|
1978 |
< |
f.complete(v1); |
1979 |
< |
g.complete(v2); |
1980 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1981 |
< |
|
1982 |
< |
checkCompletedNormally(h, null); |
1983 |
< |
assertEquals(1, r.invocationCount); |
1976 |
> |
final Noop r1 = new Noop(m); |
1977 |
> |
final Noop r2 = new Noop(m); |
1978 |
> |
final Noop r3 = new Noop(m); |
1979 |
> |
|
1980 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1981 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1982 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1983 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1984 |
> |
|
1985 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1986 |
> |
assertTrue(fst.complete(w1)); |
1987 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1988 |
> |
checkIncomplete(h1); |
1989 |
> |
checkIncomplete(h2); |
1990 |
> |
r1.assertNotInvoked(); |
1991 |
> |
r2.assertNotInvoked(); |
1992 |
> |
assertTrue(snd.complete(w2)); |
1993 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1994 |
> |
|
1995 |
> |
checkCompletedNormally(h1, null); |
1996 |
> |
checkCompletedNormally(h2, null); |
1997 |
> |
checkCompletedNormally(h3, null); |
1998 |
> |
r1.assertInvoked(); |
1999 |
> |
r2.assertInvoked(); |
2000 |
> |
r3.assertInvoked(); |
2001 |
|
checkCompletedNormally(f, v1); |
2002 |
|
checkCompletedNormally(g, v2); |
2003 |
|
}} |
2006 |
|
* runAfterBoth result completes exceptionally after exceptional |
2007 |
|
* completion of either source |
2008 |
|
*/ |
2009 |
< |
public void testRunAfterBoth_exceptionalCompletion1() { |
1733 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1734 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1735 |
< |
{ |
1736 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1737 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1738 |
< |
final Noop r = new Noop(); |
1739 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1740 |
< |
final CFException ex = new CFException(); |
1741 |
< |
|
1742 |
< |
f.completeExceptionally(ex); |
1743 |
< |
checkIncomplete(h); |
1744 |
< |
g.complete(v1); |
1745 |
< |
|
1746 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1747 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1748 |
< |
assertEquals(0, r.invocationCount); |
1749 |
< |
checkCompletedNormally(g, v1); |
1750 |
< |
}} |
1751 |
< |
|
1752 |
< |
public void testRunAfterBoth_exceptionalCompletion2() { |
1753 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1754 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1755 |
< |
{ |
1756 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1757 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1758 |
< |
final Noop r = new Noop(); |
1759 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1760 |
< |
final CFException ex = new CFException(); |
1761 |
< |
|
1762 |
< |
g.completeExceptionally(ex); |
1763 |
< |
checkIncomplete(h); |
1764 |
< |
f.complete(v1); |
1765 |
< |
|
1766 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1767 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1768 |
< |
assertEquals(0, r.invocationCount); |
1769 |
< |
checkCompletedNormally(f, v1); |
1770 |
< |
}} |
1771 |
< |
|
1772 |
< |
public void testRunAfterBoth_exceptionalCompletion3() { |
2009 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
2010 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2011 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2012 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
2013 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2014 |
|
{ |
2015 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2016 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1778 |
– |
final Noop r = new Noop(); |
2017 |
|
final CFException ex = new CFException(); |
2018 |
< |
|
2019 |
< |
g.completeExceptionally(ex); |
2020 |
< |
f.complete(v1); |
2021 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2022 |
< |
|
2023 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2024 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2025 |
< |
assertEquals(0, r.invocationCount); |
2026 |
< |
checkCompletedNormally(f, v1); |
2018 |
> |
final Noop r1 = new Noop(m); |
2019 |
> |
final Noop r2 = new Noop(m); |
2020 |
> |
final Noop r3 = new Noop(m); |
2021 |
> |
|
2022 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2023 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2024 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2025 |
> |
() -> fst.completeExceptionally(ex) : |
2026 |
> |
() -> fst.complete(v1); |
2027 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2028 |
> |
() -> snd.complete(v1) : |
2029 |
> |
() -> snd.completeExceptionally(ex); |
2030 |
> |
|
2031 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2032 |
> |
assertTrue(complete1.call()); |
2033 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2034 |
> |
checkIncomplete(h1); |
2035 |
> |
checkIncomplete(h2); |
2036 |
> |
assertTrue(complete2.call()); |
2037 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2038 |
> |
|
2039 |
> |
checkCompletedWithWrappedException(h1, ex); |
2040 |
> |
checkCompletedWithWrappedException(h2, ex); |
2041 |
> |
checkCompletedWithWrappedException(h3, ex); |
2042 |
> |
r1.assertNotInvoked(); |
2043 |
> |
r2.assertNotInvoked(); |
2044 |
> |
r3.assertNotInvoked(); |
2045 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2046 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
2047 |
|
}} |
2048 |
|
|
2049 |
< |
public void testRunAfterBoth_exceptionalCompletion4() { |
2049 |
> |
/** |
2050 |
> |
* runAfterBoth result completes exceptionally if either source cancelled |
2051 |
> |
*/ |
2052 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
2053 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2054 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2055 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2056 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
2057 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2058 |
|
{ |
2059 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2060 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2061 |
< |
final Noop r = new Noop(); |
2062 |
< |
final CFException ex = new CFException(); |
2063 |
< |
|
2064 |
< |
f.completeExceptionally(ex); |
2065 |
< |
g.complete(v1); |
2066 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2067 |
< |
|
2068 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2069 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2070 |
< |
assertEquals(0, r.invocationCount); |
2071 |
< |
checkCompletedNormally(g, v1); |
2061 |
> |
final Noop r1 = new Noop(m); |
2062 |
> |
final Noop r2 = new Noop(m); |
2063 |
> |
final Noop r3 = new Noop(m); |
2064 |
> |
|
2065 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2066 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2067 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2068 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
2069 |
> |
() -> fst.complete(v1); |
2070 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2071 |
> |
() -> snd.complete(v1) : |
2072 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
2073 |
> |
|
2074 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2075 |
> |
assertTrue(complete1.call()); |
2076 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2077 |
> |
checkIncomplete(h1); |
2078 |
> |
checkIncomplete(h2); |
2079 |
> |
assertTrue(complete2.call()); |
2080 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2081 |
> |
|
2082 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2083 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2084 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2085 |
> |
r1.assertNotInvoked(); |
2086 |
> |
r2.assertNotInvoked(); |
2087 |
> |
r3.assertNotInvoked(); |
2088 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2089 |
> |
checkCancelled(failFirst ? fst : snd); |
2090 |
|
}} |
2091 |
|
|
2092 |
|
/** |
2093 |
|
* runAfterBoth result completes exceptionally if action does |
2094 |
|
*/ |
2095 |
< |
public void testRunAfterBoth_actionFailed1() { |
1814 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1815 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1816 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1817 |
< |
{ |
1818 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1819 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1820 |
< |
final FailingRunnable r = new FailingRunnable(); |
1821 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1822 |
< |
|
1823 |
< |
f.complete(v1); |
1824 |
< |
checkIncomplete(h); |
1825 |
< |
g.complete(v2); |
1826 |
< |
|
1827 |
< |
checkCompletedWithWrappedCFException(h); |
1828 |
< |
checkCompletedNormally(f, v1); |
1829 |
< |
checkCompletedNormally(g, v2); |
1830 |
< |
}} |
1831 |
< |
|
1832 |
< |
public void testRunAfterBoth_actionFailed2() { |
2095 |
> |
public void testRunAfterBoth_actionFailed() { |
2096 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2097 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2098 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2099 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2100 |
|
{ |
2101 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2102 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2103 |
< |
final FailingRunnable r = new FailingRunnable(); |
2104 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2105 |
< |
|
2106 |
< |
g.complete(v2); |
2107 |
< |
checkIncomplete(h); |
2108 |
< |
f.complete(v1); |
2109 |
< |
|
2110 |
< |
checkCompletedWithWrappedCFException(h); |
2103 |
> |
final FailingRunnable r1 = new FailingRunnable(m); |
2104 |
> |
final FailingRunnable r2 = new FailingRunnable(m); |
2105 |
> |
final FailingRunnable r3 = new FailingRunnable(m); |
2106 |
> |
|
2107 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2108 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2109 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2110 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2111 |
> |
|
2112 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2113 |
> |
assertTrue(fst.complete(w1)); |
2114 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2115 |
> |
assertTrue(snd.complete(w2)); |
2116 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2117 |
> |
|
2118 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
2119 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
2120 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
2121 |
> |
r1.assertInvoked(); |
2122 |
> |
r2.assertInvoked(); |
2123 |
> |
r3.assertInvoked(); |
2124 |
|
checkCompletedNormally(f, v1); |
2125 |
|
checkCompletedNormally(g, v2); |
2126 |
|
}} |
2127 |
|
|
2128 |
|
/** |
1852 |
– |
* runAfterBoth result completes exceptionally if either source cancelled |
1853 |
– |
*/ |
1854 |
– |
public void testRunAfterBoth_sourceCancelled1() { |
1855 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1856 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1857 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1858 |
– |
{ |
1859 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1860 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1861 |
– |
final Noop r = new Noop(); |
1862 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1863 |
– |
|
1864 |
– |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1865 |
– |
checkIncomplete(h); |
1866 |
– |
g.complete(v1); |
1867 |
– |
|
1868 |
– |
checkCompletedWithWrappedCancellationException(h); |
1869 |
– |
checkCancelled(f); |
1870 |
– |
assertEquals(0, r.invocationCount); |
1871 |
– |
checkCompletedNormally(g, v1); |
1872 |
– |
}} |
1873 |
– |
|
1874 |
– |
public void testRunAfterBoth_sourceCancelled2() { |
1875 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1876 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1877 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1878 |
– |
{ |
1879 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1880 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1881 |
– |
final Noop r = new Noop(); |
1882 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1883 |
– |
|
1884 |
– |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1885 |
– |
checkIncomplete(h); |
1886 |
– |
f.complete(v1); |
1887 |
– |
|
1888 |
– |
checkCompletedWithWrappedCancellationException(h); |
1889 |
– |
checkCancelled(g); |
1890 |
– |
assertEquals(0, r.invocationCount); |
1891 |
– |
checkCompletedNormally(f, v1); |
1892 |
– |
}} |
1893 |
– |
|
1894 |
– |
public void testRunAfterBoth_sourceCancelled3() { |
1895 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1896 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1897 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1898 |
– |
{ |
1899 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1900 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1901 |
– |
final Noop r = new Noop(); |
1902 |
– |
|
1903 |
– |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1904 |
– |
f.complete(v1); |
1905 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1906 |
– |
|
1907 |
– |
checkCompletedWithWrappedCancellationException(h); |
1908 |
– |
checkCancelled(g); |
1909 |
– |
assertEquals(0, r.invocationCount); |
1910 |
– |
checkCompletedNormally(f, v1); |
1911 |
– |
}} |
1912 |
– |
|
1913 |
– |
public void testRunAfterBoth_sourceCancelled4() { |
1914 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1915 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1916 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1917 |
– |
{ |
1918 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1919 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1920 |
– |
final Noop r = new Noop(); |
1921 |
– |
|
1922 |
– |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1923 |
– |
g.complete(v1); |
1924 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1925 |
– |
|
1926 |
– |
checkCompletedWithWrappedCancellationException(h); |
1927 |
– |
checkCancelled(f); |
1928 |
– |
assertEquals(0, r.invocationCount); |
1929 |
– |
checkCompletedNormally(g, v1); |
1930 |
– |
}} |
1931 |
– |
|
1932 |
– |
/** |
2129 |
|
* applyToEither result completes normally after normal completion |
2130 |
|
* of either source |
2131 |
|
*/ |
2132 |
< |
public void testApplyToEither_normalCompletion1() { |
2132 |
> |
public void testApplyToEither_normalCompletion() { |
2133 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2134 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2135 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2136 |
|
{ |
2137 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2138 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2139 |
< |
final IncFunction r = new IncFunction(); |
2140 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2139 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2140 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2141 |
|
|
2142 |
< |
f.complete(v1); |
2143 |
< |
checkCompletedNormally(h, inc(v1)); |
2142 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2143 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2144 |
> |
checkIncomplete(h0); |
2145 |
> |
checkIncomplete(h1); |
2146 |
> |
rs[0].assertNotInvoked(); |
2147 |
> |
rs[1].assertNotInvoked(); |
2148 |
> |
f.complete(v1); |
2149 |
> |
checkCompletedNormally(h0, inc(v1)); |
2150 |
> |
checkCompletedNormally(h1, inc(v1)); |
2151 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2152 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2153 |
> |
checkCompletedNormally(h2, inc(v1)); |
2154 |
> |
checkCompletedNormally(h3, inc(v1)); |
2155 |
|
g.complete(v2); |
2156 |
|
|
2157 |
< |
checkCompletedNormally(f, v1); |
2158 |
< |
checkCompletedNormally(g, v2); |
2159 |
< |
checkCompletedNormally(h, inc(v1)); |
2160 |
< |
}} |
2161 |
< |
|
2162 |
< |
public void testApplyToEither_normalCompletion2() { |
2163 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2164 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2165 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1959 |
< |
{ |
1960 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1961 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1962 |
< |
final IncFunction r = new IncFunction(); |
1963 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1964 |
< |
|
1965 |
< |
g.complete(v2); |
1966 |
< |
checkCompletedNormally(h, inc(v2)); |
1967 |
< |
f.complete(v1); |
1968 |
< |
|
1969 |
< |
checkCompletedNormally(f, v1); |
1970 |
< |
checkCompletedNormally(g, v2); |
1971 |
< |
checkCompletedNormally(h, inc(v2)); |
1972 |
< |
}} |
1973 |
< |
|
1974 |
< |
public void testApplyToEither_normalCompletion3() { |
1975 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1976 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1977 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1978 |
< |
{ |
1979 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1980 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1981 |
< |
final IncFunction r = new IncFunction(); |
1982 |
< |
|
1983 |
< |
f.complete(v1); |
1984 |
< |
g.complete(v2); |
1985 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2157 |
> |
// unspecified behavior - both source completions available |
2158 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2159 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2160 |
> |
rs[4].assertValue(h4.join()); |
2161 |
> |
rs[5].assertValue(h5.join()); |
2162 |
> |
assertTrue(Objects.equals(inc(v1), h4.join()) || |
2163 |
> |
Objects.equals(inc(v2), h4.join())); |
2164 |
> |
assertTrue(Objects.equals(inc(v1), h5.join()) || |
2165 |
> |
Objects.equals(inc(v2), h5.join())); |
2166 |
|
|
2167 |
|
checkCompletedNormally(f, v1); |
2168 |
|
checkCompletedNormally(g, v2); |
2169 |
< |
|
2170 |
< |
// unspecified behavior |
2171 |
< |
assertTrue(Objects.equals(h.join(), inc(v1)) || |
2172 |
< |
Objects.equals(h.join(), inc(v2))); |
2173 |
< |
assertEquals(1, r.invocationCount); |
2169 |
> |
checkCompletedNormally(h0, inc(v1)); |
2170 |
> |
checkCompletedNormally(h1, inc(v1)); |
2171 |
> |
checkCompletedNormally(h2, inc(v1)); |
2172 |
> |
checkCompletedNormally(h3, inc(v1)); |
2173 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); |
2174 |
|
}} |
2175 |
|
|
2176 |
|
/** |
2177 |
|
* applyToEither result completes exceptionally after exceptional |
2178 |
|
* completion of either source |
2179 |
|
*/ |
2180 |
< |
public void testApplyToEither_exceptionalCompletion1() { |
2180 |
> |
public void testApplyToEither_exceptionalCompletion() { |
2181 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2182 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2183 |
|
{ |
2184 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2185 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2006 |
– |
final IncFunction r = new IncFunction(); |
2007 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2186 |
|
final CFException ex = new CFException(); |
2187 |
+ |
final IncFunction[] rs = new IncFunction[6]; |
2188 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2189 |
|
|
2190 |
+ |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2191 |
+ |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2192 |
+ |
checkIncomplete(h0); |
2193 |
+ |
checkIncomplete(h1); |
2194 |
+ |
rs[0].assertNotInvoked(); |
2195 |
+ |
rs[1].assertNotInvoked(); |
2196 |
|
f.completeExceptionally(ex); |
2197 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2197 |
> |
checkCompletedWithWrappedException(h0, ex); |
2198 |
> |
checkCompletedWithWrappedException(h1, ex); |
2199 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2200 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2201 |
> |
checkCompletedWithWrappedException(h2, ex); |
2202 |
> |
checkCompletedWithWrappedException(h3, ex); |
2203 |
|
g.complete(v1); |
2204 |
|
|
2205 |
< |
assertEquals(0, r.invocationCount); |
2205 |
> |
// unspecified behavior - both source completions available |
2206 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2207 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2208 |
> |
try { |
2209 |
> |
assertEquals(inc(v1), h4.join()); |
2210 |
> |
rs[4].assertValue(inc(v1)); |
2211 |
> |
} catch (CompletionException ok) { |
2212 |
> |
checkCompletedWithWrappedException(h4, ex); |
2213 |
> |
rs[4].assertNotInvoked(); |
2214 |
> |
} |
2215 |
> |
try { |
2216 |
> |
assertEquals(inc(v1), h5.join()); |
2217 |
> |
rs[5].assertValue(inc(v1)); |
2218 |
> |
} catch (CompletionException ok) { |
2219 |
> |
checkCompletedWithWrappedException(h5, ex); |
2220 |
> |
rs[5].assertNotInvoked(); |
2221 |
> |
} |
2222 |
> |
|
2223 |
> |
checkCompletedExceptionally(f, ex); |
2224 |
|
checkCompletedNormally(g, v1); |
2225 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2226 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2225 |
> |
checkCompletedWithWrappedException(h0, ex); |
2226 |
> |
checkCompletedWithWrappedException(h1, ex); |
2227 |
> |
checkCompletedWithWrappedException(h2, ex); |
2228 |
> |
checkCompletedWithWrappedException(h3, ex); |
2229 |
> |
checkCompletedWithWrappedException(h4, ex); |
2230 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2231 |
|
}} |
2232 |
|
|
2233 |
|
public void testApplyToEither_exceptionalCompletion2() { |
2234 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2235 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2236 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2237 |
|
{ |
2238 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2239 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2026 |
– |
final IncFunction r = new IncFunction(); |
2027 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2028 |
– |
final CFException ex = new CFException(); |
2029 |
– |
|
2030 |
– |
g.completeExceptionally(ex); |
2031 |
– |
checkCompletedWithWrappedCFException(h, ex); |
2032 |
– |
f.complete(v1); |
2033 |
– |
|
2034 |
– |
assertEquals(0, r.invocationCount); |
2035 |
– |
checkCompletedNormally(f, v1); |
2036 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2037 |
– |
checkCompletedWithWrappedCFException(h, ex); |
2038 |
– |
}} |
2039 |
– |
|
2040 |
– |
public void testApplyToEither_exceptionalCompletion3() { |
2041 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2042 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2043 |
– |
{ |
2044 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2045 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2046 |
– |
final IncFunction r = new IncFunction(); |
2240 |
|
final CFException ex = new CFException(); |
2241 |
+ |
final IncFunction[] rs = new IncFunction[6]; |
2242 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2243 |
|
|
2244 |
< |
g.completeExceptionally(ex); |
2245 |
< |
f.complete(v1); |
2246 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2244 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2245 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2246 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2247 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2248 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2249 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2250 |
|
|
2251 |
< |
// unspecified behavior |
2054 |
< |
Integer v; |
2251 |
> |
// unspecified behavior - both source completions available |
2252 |
|
try { |
2253 |
< |
assertEquals(inc(v1), h.join()); |
2254 |
< |
assertEquals(1, r.invocationCount); |
2253 |
> |
assertEquals(inc(v1), h0.join()); |
2254 |
> |
rs[0].assertValue(inc(v1)); |
2255 |
|
} catch (CompletionException ok) { |
2256 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2257 |
< |
assertEquals(0, r.invocationCount); |
2256 |
> |
checkCompletedWithWrappedException(h0, ex); |
2257 |
> |
rs[0].assertNotInvoked(); |
2258 |
|
} |
2062 |
– |
|
2063 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2064 |
– |
checkCompletedNormally(f, v1); |
2065 |
– |
}} |
2066 |
– |
|
2067 |
– |
public void testApplyToEither_exceptionalCompletion4() { |
2068 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2069 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2070 |
– |
{ |
2071 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2072 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2073 |
– |
final IncFunction r = new IncFunction(); |
2074 |
– |
final CFException ex = new CFException(); |
2075 |
– |
|
2076 |
– |
f.completeExceptionally(ex); |
2077 |
– |
g.complete(v1); |
2078 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2079 |
– |
|
2080 |
– |
// unspecified behavior |
2081 |
– |
Integer v; |
2259 |
|
try { |
2260 |
< |
assertEquals(inc(v1), h.join()); |
2261 |
< |
assertEquals(1, r.invocationCount); |
2260 |
> |
assertEquals(inc(v1), h1.join()); |
2261 |
> |
rs[1].assertValue(inc(v1)); |
2262 |
|
} catch (CompletionException ok) { |
2263 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2264 |
< |
assertEquals(0, r.invocationCount); |
2263 |
> |
checkCompletedWithWrappedException(h1, ex); |
2264 |
> |
rs[1].assertNotInvoked(); |
2265 |
> |
} |
2266 |
> |
try { |
2267 |
> |
assertEquals(inc(v1), h2.join()); |
2268 |
> |
rs[2].assertValue(inc(v1)); |
2269 |
> |
} catch (CompletionException ok) { |
2270 |
> |
checkCompletedWithWrappedException(h2, ex); |
2271 |
> |
rs[2].assertNotInvoked(); |
2272 |
> |
} |
2273 |
> |
try { |
2274 |
> |
assertEquals(inc(v1), h3.join()); |
2275 |
> |
rs[3].assertValue(inc(v1)); |
2276 |
> |
} catch (CompletionException ok) { |
2277 |
> |
checkCompletedWithWrappedException(h3, ex); |
2278 |
> |
rs[3].assertNotInvoked(); |
2279 |
|
} |
2280 |
|
|
2090 |
– |
checkCompletedWithWrappedCFException(f, ex); |
2091 |
– |
checkCompletedNormally(g, v1); |
2092 |
– |
}} |
2093 |
– |
|
2094 |
– |
/** |
2095 |
– |
* applyToEither result completes exceptionally if action does |
2096 |
– |
*/ |
2097 |
– |
public void testApplyToEither_actionFailed1() { |
2098 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2099 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2100 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2101 |
– |
{ |
2102 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2103 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2104 |
– |
final FailingFunction r = new FailingFunction(); |
2105 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2106 |
– |
|
2107 |
– |
f.complete(v1); |
2108 |
– |
checkCompletedWithWrappedCFException(h); |
2109 |
– |
g.complete(v2); |
2110 |
– |
checkCompletedNormally(f, v1); |
2111 |
– |
checkCompletedNormally(g, v2); |
2112 |
– |
}} |
2113 |
– |
|
2114 |
– |
public void testApplyToEither_actionFailed2() { |
2115 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2116 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2117 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2118 |
– |
{ |
2119 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2120 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2121 |
– |
final FailingFunction r = new FailingFunction(); |
2122 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2123 |
– |
|
2124 |
– |
g.complete(v2); |
2125 |
– |
checkCompletedWithWrappedCFException(h); |
2126 |
– |
f.complete(v1); |
2281 |
|
checkCompletedNormally(f, v1); |
2282 |
< |
checkCompletedNormally(g, v2); |
2282 |
> |
checkCompletedExceptionally(g, ex); |
2283 |
|
}} |
2284 |
|
|
2285 |
|
/** |
2286 |
|
* applyToEither result completes exceptionally if either source cancelled |
2287 |
|
*/ |
2288 |
< |
public void testApplyToEither_sourceCancelled1() { |
2288 |
> |
public void testApplyToEither_sourceCancelled() { |
2289 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2290 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2291 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2292 |
|
{ |
2293 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2294 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2295 |
< |
final IncFunction r = new IncFunction(); |
2296 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2295 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2296 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2297 |
|
|
2298 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2299 |
< |
checkCompletedWithWrappedCancellationException(h); |
2298 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2299 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2300 |
> |
checkIncomplete(h0); |
2301 |
> |
checkIncomplete(h1); |
2302 |
> |
rs[0].assertNotInvoked(); |
2303 |
> |
rs[1].assertNotInvoked(); |
2304 |
> |
f.cancel(mayInterruptIfRunning); |
2305 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2306 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2307 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2308 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2309 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2310 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2311 |
|
g.complete(v1); |
2312 |
|
|
2313 |
+ |
// unspecified behavior - both source completions available |
2314 |
+ |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2315 |
+ |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2316 |
+ |
try { |
2317 |
+ |
assertEquals(inc(v1), h4.join()); |
2318 |
+ |
rs[4].assertValue(inc(v1)); |
2319 |
+ |
} catch (CompletionException ok) { |
2320 |
+ |
checkCompletedWithWrappedCancellationException(h4); |
2321 |
+ |
rs[4].assertNotInvoked(); |
2322 |
+ |
} |
2323 |
+ |
try { |
2324 |
+ |
assertEquals(inc(v1), h5.join()); |
2325 |
+ |
rs[5].assertValue(inc(v1)); |
2326 |
+ |
} catch (CompletionException ok) { |
2327 |
+ |
checkCompletedWithWrappedCancellationException(h5); |
2328 |
+ |
rs[5].assertNotInvoked(); |
2329 |
+ |
} |
2330 |
+ |
|
2331 |
|
checkCancelled(f); |
2149 |
– |
assertEquals(0, r.invocationCount); |
2332 |
|
checkCompletedNormally(g, v1); |
2333 |
< |
checkCompletedWithWrappedCancellationException(h); |
2333 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2334 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2335 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2336 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2337 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2338 |
|
}} |
2339 |
|
|
2340 |
|
public void testApplyToEither_sourceCancelled2() { |
2341 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2342 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2343 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2344 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2345 |
|
{ |
2346 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2347 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2348 |
< |
final IncFunction r = new IncFunction(); |
2349 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2163 |
< |
|
2164 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2165 |
< |
checkCompletedWithWrappedCancellationException(h); |
2166 |
< |
f.complete(v1); |
2167 |
< |
|
2168 |
< |
checkCancelled(g); |
2169 |
< |
assertEquals(0, r.invocationCount); |
2170 |
< |
checkCompletedNormally(f, v1); |
2171 |
< |
checkCompletedWithWrappedCancellationException(h); |
2172 |
< |
}} |
2173 |
< |
|
2174 |
< |
public void testApplyToEither_sourceCancelled3() { |
2175 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2176 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2177 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2178 |
< |
{ |
2179 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2180 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2181 |
< |
final IncFunction r = new IncFunction(); |
2348 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2349 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2350 |
|
|
2351 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2352 |
< |
f.complete(v1); |
2353 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2351 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2352 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2353 |
> |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2354 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2355 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2356 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2357 |
|
|
2358 |
< |
// unspecified behavior |
2188 |
< |
Integer v; |
2358 |
> |
// unspecified behavior - both source completions available |
2359 |
|
try { |
2360 |
< |
assertEquals(inc(v1), h.join()); |
2361 |
< |
assertEquals(1, r.invocationCount); |
2360 |
> |
assertEquals(inc(v1), h0.join()); |
2361 |
> |
rs[0].assertValue(inc(v1)); |
2362 |
|
} catch (CompletionException ok) { |
2363 |
< |
checkCompletedWithWrappedCancellationException(h); |
2364 |
< |
assertEquals(0, r.invocationCount); |
2363 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2364 |
> |
rs[0].assertNotInvoked(); |
2365 |
|
} |
2196 |
– |
|
2197 |
– |
checkCancelled(g); |
2198 |
– |
checkCompletedNormally(f, v1); |
2199 |
– |
}} |
2200 |
– |
|
2201 |
– |
public void testApplyToEither_sourceCancelled4() { |
2202 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2203 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2204 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2205 |
– |
{ |
2206 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2207 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2208 |
– |
final IncFunction r = new IncFunction(); |
2209 |
– |
|
2210 |
– |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2211 |
– |
g.complete(v1); |
2212 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2213 |
– |
|
2214 |
– |
// unspecified behavior |
2215 |
– |
Integer v; |
2366 |
|
try { |
2367 |
< |
assertEquals(inc(v1), h.join()); |
2368 |
< |
assertEquals(1, r.invocationCount); |
2367 |
> |
assertEquals(inc(v1), h1.join()); |
2368 |
> |
rs[1].assertValue(inc(v1)); |
2369 |
|
} catch (CompletionException ok) { |
2370 |
< |
checkCompletedWithWrappedCancellationException(h); |
2371 |
< |
assertEquals(0, r.invocationCount); |
2370 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2371 |
> |
rs[1].assertNotInvoked(); |
2372 |
> |
} |
2373 |
> |
try { |
2374 |
> |
assertEquals(inc(v1), h2.join()); |
2375 |
> |
rs[2].assertValue(inc(v1)); |
2376 |
> |
} catch (CompletionException ok) { |
2377 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2378 |
> |
rs[2].assertNotInvoked(); |
2379 |
> |
} |
2380 |
> |
try { |
2381 |
> |
assertEquals(inc(v1), h3.join()); |
2382 |
> |
rs[3].assertValue(inc(v1)); |
2383 |
> |
} catch (CompletionException ok) { |
2384 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2385 |
> |
rs[3].assertNotInvoked(); |
2386 |
|
} |
2387 |
|
|
2388 |
< |
checkCancelled(f); |
2389 |
< |
checkCompletedNormally(g, v1); |
2388 |
> |
checkCompletedNormally(f, v1); |
2389 |
> |
checkCancelled(g); |
2390 |
|
}} |
2391 |
|
|
2392 |
|
/** |
2393 |
< |
* acceptEither result completes normally after normal completion |
2230 |
< |
* of either source |
2393 |
> |
* applyToEither result completes exceptionally if action does |
2394 |
|
*/ |
2395 |
< |
public void testAcceptEither_normalCompletion1() { |
2395 |
> |
public void testApplyToEither_actionFailed() { |
2396 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2397 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2398 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2399 |
|
{ |
2400 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2401 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2402 |
< |
final IncAction r = new IncAction(); |
2403 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2402 |
> |
final FailingFunction[] rs = new FailingFunction[6]; |
2403 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
2404 |
|
|
2405 |
+ |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2406 |
+ |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2407 |
|
f.complete(v1); |
2408 |
< |
checkCompletedNormally(h, null); |
2409 |
< |
assertEquals(inc(v1), r.value); |
2410 |
< |
g.complete(v2); |
2411 |
< |
|
2412 |
< |
checkCompletedNormally(f, v1); |
2413 |
< |
checkCompletedNormally(g, v2); |
2414 |
< |
checkCompletedNormally(h, null); |
2250 |
< |
}} |
2251 |
< |
|
2252 |
< |
public void testAcceptEither_normalCompletion2() { |
2253 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2254 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2255 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
2256 |
< |
{ |
2257 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2258 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2259 |
< |
final IncAction r = new IncAction(); |
2260 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2408 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2409 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2410 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2411 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2412 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2413 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2414 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2415 |
|
|
2416 |
|
g.complete(v2); |
2417 |
< |
checkCompletedNormally(h, null); |
2418 |
< |
assertEquals(inc(v2), r.value); |
2419 |
< |
f.complete(v1); |
2417 |
> |
|
2418 |
> |
// unspecified behavior - both source completions available |
2419 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2420 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2421 |
> |
|
2422 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2423 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2424 |
> |
Objects.equals(v2, rs[4].value)); |
2425 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2426 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2427 |
> |
Objects.equals(v2, rs[5].value)); |
2428 |
|
|
2429 |
|
checkCompletedNormally(f, v1); |
2430 |
|
checkCompletedNormally(g, v2); |
2269 |
– |
checkCompletedNormally(h, null); |
2431 |
|
}} |
2432 |
|
|
2433 |
< |
public void testAcceptEither_normalCompletion3() { |
2433 |
> |
/** |
2434 |
> |
* acceptEither result completes normally after normal completion |
2435 |
> |
* of either source |
2436 |
> |
*/ |
2437 |
> |
public void testAcceptEither_normalCompletion() { |
2438 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2439 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2440 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2441 |
|
{ |
2442 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2443 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2444 |
< |
final IncAction r = new IncAction(); |
2444 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2445 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2446 |
|
|
2447 |
< |
f.complete(v1); |
2447 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2448 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2449 |
> |
checkIncomplete(h0); |
2450 |
> |
checkIncomplete(h1); |
2451 |
> |
rs[0].assertNotInvoked(); |
2452 |
> |
rs[1].assertNotInvoked(); |
2453 |
> |
f.complete(v1); |
2454 |
> |
checkCompletedNormally(h0, null); |
2455 |
> |
checkCompletedNormally(h1, null); |
2456 |
> |
rs[0].assertValue(v1); |
2457 |
> |
rs[1].assertValue(v1); |
2458 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2459 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2460 |
> |
checkCompletedNormally(h2, null); |
2461 |
> |
checkCompletedNormally(h3, null); |
2462 |
> |
rs[2].assertValue(v1); |
2463 |
> |
rs[3].assertValue(v1); |
2464 |
|
g.complete(v2); |
2283 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2465 |
|
|
2466 |
< |
checkCompletedNormally(h, null); |
2466 |
> |
// unspecified behavior - both source completions available |
2467 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2468 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2469 |
> |
checkCompletedNormally(h4, null); |
2470 |
> |
checkCompletedNormally(h5, null); |
2471 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2472 |
> |
Objects.equals(v2, rs[4].value)); |
2473 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2474 |
> |
Objects.equals(v2, rs[5].value)); |
2475 |
> |
|
2476 |
|
checkCompletedNormally(f, v1); |
2477 |
|
checkCompletedNormally(g, v2); |
2478 |
< |
|
2479 |
< |
// unspecified behavior |
2480 |
< |
assertTrue(Objects.equals(r.value, inc(v1)) || |
2481 |
< |
Objects.equals(r.value, inc(v2))); |
2478 |
> |
checkCompletedNormally(h0, null); |
2479 |
> |
checkCompletedNormally(h1, null); |
2480 |
> |
checkCompletedNormally(h2, null); |
2481 |
> |
checkCompletedNormally(h3, null); |
2482 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2483 |
|
}} |
2484 |
|
|
2485 |
|
/** |
2486 |
|
* acceptEither result completes exceptionally after exceptional |
2487 |
|
* completion of either source |
2488 |
|
*/ |
2489 |
< |
public void testAcceptEither_exceptionalCompletion1() { |
2489 |
> |
public void testAcceptEither_exceptionalCompletion() { |
2490 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2491 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2492 |
|
{ |
2493 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2494 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2304 |
– |
final IncAction r = new IncAction(); |
2305 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2495 |
|
final CFException ex = new CFException(); |
2496 |
+ |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2497 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2498 |
|
|
2499 |
+ |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2500 |
+ |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2501 |
+ |
checkIncomplete(h0); |
2502 |
+ |
checkIncomplete(h1); |
2503 |
+ |
rs[0].assertNotInvoked(); |
2504 |
+ |
rs[1].assertNotInvoked(); |
2505 |
|
f.completeExceptionally(ex); |
2506 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2506 |
> |
checkCompletedWithWrappedException(h0, ex); |
2507 |
> |
checkCompletedWithWrappedException(h1, ex); |
2508 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2509 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2510 |
> |
checkCompletedWithWrappedException(h2, ex); |
2511 |
> |
checkCompletedWithWrappedException(h3, ex); |
2512 |
> |
|
2513 |
|
g.complete(v1); |
2514 |
|
|
2515 |
< |
assertEquals(0, r.invocationCount); |
2515 |
> |
// unspecified behavior - both source completions available |
2516 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2517 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2518 |
> |
try { |
2519 |
> |
assertNull(h4.join()); |
2520 |
> |
rs[4].assertValue(v1); |
2521 |
> |
} catch (CompletionException ok) { |
2522 |
> |
checkCompletedWithWrappedException(h4, ex); |
2523 |
> |
rs[4].assertNotInvoked(); |
2524 |
> |
} |
2525 |
> |
try { |
2526 |
> |
assertNull(h5.join()); |
2527 |
> |
rs[5].assertValue(v1); |
2528 |
> |
} catch (CompletionException ok) { |
2529 |
> |
checkCompletedWithWrappedException(h5, ex); |
2530 |
> |
rs[5].assertNotInvoked(); |
2531 |
> |
} |
2532 |
> |
|
2533 |
> |
checkCompletedExceptionally(f, ex); |
2534 |
|
checkCompletedNormally(g, v1); |
2535 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2536 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2535 |
> |
checkCompletedWithWrappedException(h0, ex); |
2536 |
> |
checkCompletedWithWrappedException(h1, ex); |
2537 |
> |
checkCompletedWithWrappedException(h2, ex); |
2538 |
> |
checkCompletedWithWrappedException(h3, ex); |
2539 |
> |
checkCompletedWithWrappedException(h4, ex); |
2540 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2541 |
|
}} |
2542 |
|
|
2543 |
|
public void testAcceptEither_exceptionalCompletion2() { |
2544 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2545 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2546 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2547 |
|
{ |
2548 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2549 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2324 |
– |
final IncAction r = new IncAction(); |
2325 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2326 |
– |
final CFException ex = new CFException(); |
2327 |
– |
|
2328 |
– |
g.completeExceptionally(ex); |
2329 |
– |
checkCompletedWithWrappedCFException(h, ex); |
2330 |
– |
f.complete(v1); |
2331 |
– |
|
2332 |
– |
assertEquals(0, r.invocationCount); |
2333 |
– |
checkCompletedNormally(f, v1); |
2334 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2335 |
– |
checkCompletedWithWrappedCFException(h, ex); |
2336 |
– |
}} |
2337 |
– |
|
2338 |
– |
public void testAcceptEither_exceptionalCompletion3() { |
2339 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2340 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2341 |
– |
{ |
2342 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2343 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2344 |
– |
final IncAction r = new IncAction(); |
2550 |
|
final CFException ex = new CFException(); |
2551 |
+ |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2552 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2553 |
|
|
2554 |
< |
g.completeExceptionally(ex); |
2555 |
< |
f.complete(v1); |
2556 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2554 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2555 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2556 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2557 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2558 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2559 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2560 |
|
|
2561 |
< |
// unspecified behavior |
2352 |
< |
Integer v; |
2561 |
> |
// unspecified behavior - both source completions available |
2562 |
|
try { |
2563 |
< |
assertNull(h.join()); |
2564 |
< |
assertEquals(1, r.invocationCount); |
2356 |
< |
assertEquals(inc(v1), r.value); |
2563 |
> |
assertNull(h0.join()); |
2564 |
> |
rs[0].assertValue(v1); |
2565 |
|
} catch (CompletionException ok) { |
2566 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2567 |
< |
assertEquals(0, r.invocationCount); |
2566 |
> |
checkCompletedWithWrappedException(h0, ex); |
2567 |
> |
rs[0].assertNotInvoked(); |
2568 |
|
} |
2361 |
– |
|
2362 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2363 |
– |
checkCompletedNormally(f, v1); |
2364 |
– |
}} |
2365 |
– |
|
2366 |
– |
public void testAcceptEither_exceptionalCompletion4() { |
2367 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2368 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2369 |
– |
{ |
2370 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2371 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2372 |
– |
final IncAction r = new IncAction(); |
2373 |
– |
final CFException ex = new CFException(); |
2374 |
– |
|
2375 |
– |
f.completeExceptionally(ex); |
2376 |
– |
g.complete(v1); |
2377 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2378 |
– |
|
2379 |
– |
// unspecified behavior |
2380 |
– |
Integer v; |
2569 |
|
try { |
2570 |
< |
assertNull(h.join()); |
2571 |
< |
assertEquals(1, r.invocationCount); |
2384 |
< |
assertEquals(inc(v1), r.value); |
2570 |
> |
assertNull(h1.join()); |
2571 |
> |
rs[1].assertValue(v1); |
2572 |
|
} catch (CompletionException ok) { |
2573 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2574 |
< |
assertEquals(0, r.invocationCount); |
2573 |
> |
checkCompletedWithWrappedException(h1, ex); |
2574 |
> |
rs[1].assertNotInvoked(); |
2575 |
> |
} |
2576 |
> |
try { |
2577 |
> |
assertNull(h2.join()); |
2578 |
> |
rs[2].assertValue(v1); |
2579 |
> |
} catch (CompletionException ok) { |
2580 |
> |
checkCompletedWithWrappedException(h2, ex); |
2581 |
> |
rs[2].assertNotInvoked(); |
2582 |
> |
} |
2583 |
> |
try { |
2584 |
> |
assertNull(h3.join()); |
2585 |
> |
rs[3].assertValue(v1); |
2586 |
> |
} catch (CompletionException ok) { |
2587 |
> |
checkCompletedWithWrappedException(h3, ex); |
2588 |
> |
rs[3].assertNotInvoked(); |
2589 |
|
} |
2590 |
|
|
2390 |
– |
checkCompletedWithWrappedCFException(f, ex); |
2391 |
– |
checkCompletedNormally(g, v1); |
2392 |
– |
}} |
2393 |
– |
|
2394 |
– |
/** |
2395 |
– |
* acceptEither result completes exceptionally if action does |
2396 |
– |
*/ |
2397 |
– |
public void testAcceptEither_actionFailed1() { |
2398 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2399 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2400 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2401 |
– |
{ |
2402 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2403 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2404 |
– |
final FailingConsumer r = new FailingConsumer(); |
2405 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2406 |
– |
|
2407 |
– |
f.complete(v1); |
2408 |
– |
checkCompletedWithWrappedCFException(h); |
2409 |
– |
g.complete(v2); |
2410 |
– |
checkCompletedNormally(f, v1); |
2411 |
– |
checkCompletedNormally(g, v2); |
2412 |
– |
}} |
2413 |
– |
|
2414 |
– |
public void testAcceptEither_actionFailed2() { |
2415 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2416 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2417 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2418 |
– |
{ |
2419 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2420 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2421 |
– |
final FailingConsumer r = new FailingConsumer(); |
2422 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2423 |
– |
|
2424 |
– |
g.complete(v2); |
2425 |
– |
checkCompletedWithWrappedCFException(h); |
2426 |
– |
f.complete(v1); |
2591 |
|
checkCompletedNormally(f, v1); |
2592 |
< |
checkCompletedNormally(g, v2); |
2592 |
> |
checkCompletedExceptionally(g, ex); |
2593 |
|
}} |
2594 |
|
|
2595 |
|
/** |
2596 |
|
* acceptEither result completes exceptionally if either source cancelled |
2597 |
|
*/ |
2598 |
< |
public void testAcceptEither_sourceCancelled1() { |
2435 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2436 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2437 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2438 |
< |
{ |
2439 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2440 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2441 |
< |
final IncAction r = new IncAction(); |
2442 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2443 |
< |
|
2444 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2445 |
< |
checkCompletedWithWrappedCancellationException(h); |
2446 |
< |
g.complete(v1); |
2447 |
< |
|
2448 |
< |
checkCancelled(f); |
2449 |
< |
assertEquals(0, r.invocationCount); |
2450 |
< |
checkCompletedNormally(g, v1); |
2451 |
< |
checkCompletedWithWrappedCancellationException(h); |
2452 |
< |
}} |
2453 |
< |
|
2454 |
< |
public void testAcceptEither_sourceCancelled2() { |
2598 |
> |
public void testAcceptEither_sourceCancelled() { |
2599 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2600 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2601 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2602 |
|
{ |
2603 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2604 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2605 |
< |
final IncAction r = new IncAction(); |
2606 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2605 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2606 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2607 |
|
|
2608 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2609 |
< |
checkCompletedWithWrappedCancellationException(h); |
2610 |
< |
f.complete(v1); |
2611 |
< |
|
2612 |
< |
checkCancelled(g); |
2613 |
< |
assertEquals(0, r.invocationCount); |
2614 |
< |
checkCompletedNormally(f, v1); |
2615 |
< |
checkCompletedWithWrappedCancellationException(h); |
2616 |
< |
}} |
2617 |
< |
|
2618 |
< |
public void testAcceptEither_sourceCancelled3() { |
2619 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2620 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2477 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2478 |
< |
{ |
2479 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2480 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2481 |
< |
final IncAction r = new IncAction(); |
2608 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2609 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2610 |
> |
checkIncomplete(h0); |
2611 |
> |
checkIncomplete(h1); |
2612 |
> |
rs[0].assertNotInvoked(); |
2613 |
> |
rs[1].assertNotInvoked(); |
2614 |
> |
f.cancel(mayInterruptIfRunning); |
2615 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2616 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2617 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2618 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2619 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2620 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2621 |
|
|
2622 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2484 |
< |
f.complete(v1); |
2485 |
< |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2622 |
> |
g.complete(v1); |
2623 |
|
|
2624 |
< |
// unspecified behavior |
2625 |
< |
Integer v; |
2624 |
> |
// unspecified behavior - both source completions available |
2625 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2626 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2627 |
|
try { |
2628 |
< |
assertNull(h.join()); |
2629 |
< |
assertEquals(1, r.invocationCount); |
2492 |
< |
assertEquals(inc(v1), r.value); |
2628 |
> |
assertNull(h4.join()); |
2629 |
> |
rs[4].assertValue(v1); |
2630 |
|
} catch (CompletionException ok) { |
2631 |
< |
checkCompletedWithWrappedCancellationException(h); |
2632 |
< |
assertEquals(0, r.invocationCount); |
2631 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2632 |
> |
rs[4].assertNotInvoked(); |
2633 |
|
} |
2497 |
– |
|
2498 |
– |
checkCancelled(g); |
2499 |
– |
checkCompletedNormally(f, v1); |
2500 |
– |
}} |
2501 |
– |
|
2502 |
– |
public void testAcceptEither_sourceCancelled4() { |
2503 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2504 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2505 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2506 |
– |
{ |
2507 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2508 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2509 |
– |
final IncAction r = new IncAction(); |
2510 |
– |
|
2511 |
– |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2512 |
– |
g.complete(v1); |
2513 |
– |
final CompletableFuture<Void> h = m.acceptEither(f, g, r); |
2514 |
– |
|
2515 |
– |
// unspecified behavior |
2516 |
– |
Integer v; |
2634 |
|
try { |
2635 |
< |
assertNull(h.join()); |
2636 |
< |
assertEquals(1, r.invocationCount); |
2520 |
< |
assertEquals(inc(v1), r.value); |
2635 |
> |
assertNull(h5.join()); |
2636 |
> |
rs[5].assertValue(v1); |
2637 |
|
} catch (CompletionException ok) { |
2638 |
< |
checkCompletedWithWrappedCancellationException(h); |
2639 |
< |
assertEquals(0, r.invocationCount); |
2638 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2639 |
> |
rs[5].assertNotInvoked(); |
2640 |
|
} |
2641 |
|
|
2642 |
|
checkCancelled(f); |
2643 |
|
checkCompletedNormally(g, v1); |
2644 |
+ |
checkCompletedWithWrappedCancellationException(h0); |
2645 |
+ |
checkCompletedWithWrappedCancellationException(h1); |
2646 |
+ |
checkCompletedWithWrappedCancellationException(h2); |
2647 |
+ |
checkCompletedWithWrappedCancellationException(h3); |
2648 |
+ |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2649 |
|
}} |
2650 |
|
|
2651 |
|
/** |
2652 |
< |
* runAfterEither result completes normally after normal completion |
2532 |
< |
* of either source |
2652 |
> |
* acceptEither result completes exceptionally if action does |
2653 |
|
*/ |
2654 |
< |
public void testRunAfterEither_normalCompletion1() { |
2654 |
> |
public void testAcceptEither_actionFailed() { |
2655 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2656 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2657 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2658 |
|
{ |
2659 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2660 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2661 |
< |
final Noop r = new Noop(); |
2662 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2661 |
> |
final FailingConsumer[] rs = new FailingConsumer[6]; |
2662 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
2663 |
|
|
2664 |
+ |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2665 |
+ |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2666 |
|
f.complete(v1); |
2667 |
< |
checkCompletedNormally(h, null); |
2668 |
< |
assertEquals(1, r.invocationCount); |
2667 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2668 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2669 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2670 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2671 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2672 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2673 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2674 |
> |
|
2675 |
|
g.complete(v2); |
2676 |
|
|
2677 |
+ |
// unspecified behavior - both source completions available |
2678 |
+ |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2679 |
+ |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2680 |
+ |
|
2681 |
+ |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2682 |
+ |
assertTrue(Objects.equals(v1, rs[4].value) || |
2683 |
+ |
Objects.equals(v2, rs[4].value)); |
2684 |
+ |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2685 |
+ |
assertTrue(Objects.equals(v1, rs[5].value) || |
2686 |
+ |
Objects.equals(v2, rs[5].value)); |
2687 |
+ |
|
2688 |
|
checkCompletedNormally(f, v1); |
2689 |
|
checkCompletedNormally(g, v2); |
2551 |
– |
checkCompletedNormally(h, null); |
2552 |
– |
assertEquals(1, r.invocationCount); |
2690 |
|
}} |
2691 |
|
|
2692 |
< |
public void testRunAfterEither_normalCompletion2() { |
2692 |
> |
/** |
2693 |
> |
* runAfterEither result completes normally after normal completion |
2694 |
> |
* of either source |
2695 |
> |
*/ |
2696 |
> |
public void testRunAfterEither_normalCompletion() { |
2697 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2698 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2699 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2700 |
+ |
for (boolean pushNop : new boolean[] { true, false }) |
2701 |
|
{ |
2702 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2703 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2704 |
< |
final Noop r = new Noop(); |
2705 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2704 |
> |
final Noop[] rs = new Noop[6]; |
2705 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2706 |
|
|
2707 |
< |
g.complete(v2); |
2708 |
< |
checkCompletedNormally(h, null); |
2709 |
< |
assertEquals(1, r.invocationCount); |
2710 |
< |
f.complete(v1); |
2711 |
< |
|
2712 |
< |
checkCompletedNormally(f, v1); |
2713 |
< |
checkCompletedNormally(g, v2); |
2714 |
< |
checkCompletedNormally(h, null); |
2715 |
< |
assertEquals(1, r.invocationCount); |
2716 |
< |
}} |
2717 |
< |
|
2718 |
< |
public void testRunAfterEither_normalCompletion3() { |
2719 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2720 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2721 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
2722 |
< |
{ |
2723 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2724 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2725 |
< |
final Noop r = new Noop(); |
2707 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2708 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2709 |
> |
checkIncomplete(h0); |
2710 |
> |
checkIncomplete(h1); |
2711 |
> |
rs[0].assertNotInvoked(); |
2712 |
> |
rs[1].assertNotInvoked(); |
2713 |
> |
if (pushNop) { // ad hoc test of intra-completion interference |
2714 |
> |
m.thenRun(f, () -> {}); |
2715 |
> |
m.thenRun(g, () -> {}); |
2716 |
> |
} |
2717 |
> |
f.complete(v1); |
2718 |
> |
checkCompletedNormally(h0, null); |
2719 |
> |
checkCompletedNormally(h1, null); |
2720 |
> |
rs[0].assertInvoked(); |
2721 |
> |
rs[1].assertInvoked(); |
2722 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2723 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2724 |
> |
checkCompletedNormally(h2, null); |
2725 |
> |
checkCompletedNormally(h3, null); |
2726 |
> |
rs[2].assertInvoked(); |
2727 |
> |
rs[3].assertInvoked(); |
2728 |
|
|
2585 |
– |
f.complete(v1); |
2729 |
|
g.complete(v2); |
2587 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2730 |
|
|
2731 |
< |
checkCompletedNormally(h, null); |
2731 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2732 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2733 |
> |
|
2734 |
|
checkCompletedNormally(f, v1); |
2735 |
|
checkCompletedNormally(g, v2); |
2736 |
< |
assertEquals(1, r.invocationCount); |
2736 |
> |
checkCompletedNormally(h0, null); |
2737 |
> |
checkCompletedNormally(h1, null); |
2738 |
> |
checkCompletedNormally(h2, null); |
2739 |
> |
checkCompletedNormally(h3, null); |
2740 |
> |
checkCompletedNormally(h4, null); |
2741 |
> |
checkCompletedNormally(h5, null); |
2742 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2743 |
|
}} |
2744 |
|
|
2745 |
|
/** |
2746 |
|
* runAfterEither result completes exceptionally after exceptional |
2747 |
|
* completion of either source |
2748 |
|
*/ |
2749 |
< |
public void testRunAfterEither_exceptionalCompletion1() { |
2749 |
> |
public void testRunAfterEither_exceptionalCompletion() { |
2750 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2751 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2752 |
|
{ |
2753 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2754 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2605 |
– |
final Noop r = new Noop(); |
2606 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2755 |
|
final CFException ex = new CFException(); |
2756 |
+ |
final Noop[] rs = new Noop[6]; |
2757 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2758 |
|
|
2759 |
< |
f.completeExceptionally(ex); |
2760 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2761 |
< |
g.complete(v1); |
2759 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2760 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2761 |
> |
checkIncomplete(h0); |
2762 |
> |
checkIncomplete(h1); |
2763 |
> |
rs[0].assertNotInvoked(); |
2764 |
> |
rs[1].assertNotInvoked(); |
2765 |
> |
assertTrue(f.completeExceptionally(ex)); |
2766 |
> |
checkCompletedWithWrappedException(h0, ex); |
2767 |
> |
checkCompletedWithWrappedException(h1, ex); |
2768 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2769 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2770 |
> |
checkCompletedWithWrappedException(h2, ex); |
2771 |
> |
checkCompletedWithWrappedException(h3, ex); |
2772 |
> |
|
2773 |
> |
assertTrue(g.complete(v1)); |
2774 |
> |
|
2775 |
> |
// unspecified behavior - both source completions available |
2776 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2777 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2778 |
> |
try { |
2779 |
> |
assertNull(h4.join()); |
2780 |
> |
rs[4].assertInvoked(); |
2781 |
> |
} catch (CompletionException ok) { |
2782 |
> |
checkCompletedWithWrappedException(h4, ex); |
2783 |
> |
rs[4].assertNotInvoked(); |
2784 |
> |
} |
2785 |
> |
try { |
2786 |
> |
assertNull(h5.join()); |
2787 |
> |
rs[5].assertInvoked(); |
2788 |
> |
} catch (CompletionException ok) { |
2789 |
> |
checkCompletedWithWrappedException(h5, ex); |
2790 |
> |
rs[5].assertNotInvoked(); |
2791 |
> |
} |
2792 |
|
|
2793 |
< |
assertEquals(0, r.invocationCount); |
2793 |
> |
checkCompletedExceptionally(f, ex); |
2794 |
|
checkCompletedNormally(g, v1); |
2795 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2796 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2795 |
> |
checkCompletedWithWrappedException(h0, ex); |
2796 |
> |
checkCompletedWithWrappedException(h1, ex); |
2797 |
> |
checkCompletedWithWrappedException(h2, ex); |
2798 |
> |
checkCompletedWithWrappedException(h3, ex); |
2799 |
> |
checkCompletedWithWrappedException(h4, ex); |
2800 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2801 |
|
}} |
2802 |
|
|
2803 |
|
public void testRunAfterEither_exceptionalCompletion2() { |
2804 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2805 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2806 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2807 |
|
{ |
2808 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2809 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2625 |
– |
final Noop r = new Noop(); |
2626 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2627 |
– |
final CFException ex = new CFException(); |
2628 |
– |
|
2629 |
– |
g.completeExceptionally(ex); |
2630 |
– |
checkCompletedWithWrappedCFException(h, ex); |
2631 |
– |
f.complete(v1); |
2632 |
– |
|
2633 |
– |
assertEquals(0, r.invocationCount); |
2634 |
– |
checkCompletedNormally(f, v1); |
2635 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2636 |
– |
checkCompletedWithWrappedCFException(h, ex); |
2637 |
– |
}} |
2638 |
– |
|
2639 |
– |
public void testRunAfterEither_exceptionalCompletion3() { |
2640 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2641 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2642 |
– |
{ |
2643 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2644 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2645 |
– |
final Noop r = new Noop(); |
2810 |
|
final CFException ex = new CFException(); |
2811 |
+ |
final Noop[] rs = new Noop[6]; |
2812 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2813 |
|
|
2814 |
< |
g.completeExceptionally(ex); |
2815 |
< |
f.complete(v1); |
2816 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2814 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2815 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2816 |
> |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2817 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2818 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2819 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2820 |
|
|
2821 |
< |
// unspecified behavior |
2653 |
< |
Integer v; |
2821 |
> |
// unspecified behavior - both source completions available |
2822 |
|
try { |
2823 |
< |
assertNull(h.join()); |
2824 |
< |
assertEquals(1, r.invocationCount); |
2823 |
> |
assertNull(h0.join()); |
2824 |
> |
rs[0].assertInvoked(); |
2825 |
|
} catch (CompletionException ok) { |
2826 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2827 |
< |
assertEquals(0, r.invocationCount); |
2826 |
> |
checkCompletedWithWrappedException(h0, ex); |
2827 |
> |
rs[0].assertNotInvoked(); |
2828 |
|
} |
2661 |
– |
|
2662 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2663 |
– |
checkCompletedNormally(f, v1); |
2664 |
– |
}} |
2665 |
– |
|
2666 |
– |
public void testRunAfterEither_exceptionalCompletion4() { |
2667 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2668 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2669 |
– |
{ |
2670 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2671 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2672 |
– |
final Noop r = new Noop(); |
2673 |
– |
final CFException ex = new CFException(); |
2674 |
– |
|
2675 |
– |
f.completeExceptionally(ex); |
2676 |
– |
g.complete(v1); |
2677 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2678 |
– |
|
2679 |
– |
// unspecified behavior |
2680 |
– |
Integer v; |
2829 |
|
try { |
2830 |
< |
assertNull(h.join()); |
2831 |
< |
assertEquals(1, r.invocationCount); |
2830 |
> |
assertNull(h1.join()); |
2831 |
> |
rs[1].assertInvoked(); |
2832 |
|
} catch (CompletionException ok) { |
2833 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2834 |
< |
assertEquals(0, r.invocationCount); |
2833 |
> |
checkCompletedWithWrappedException(h1, ex); |
2834 |
> |
rs[1].assertNotInvoked(); |
2835 |
> |
} |
2836 |
> |
try { |
2837 |
> |
assertNull(h2.join()); |
2838 |
> |
rs[2].assertInvoked(); |
2839 |
> |
} catch (CompletionException ok) { |
2840 |
> |
checkCompletedWithWrappedException(h2, ex); |
2841 |
> |
rs[2].assertNotInvoked(); |
2842 |
> |
} |
2843 |
> |
try { |
2844 |
> |
assertNull(h3.join()); |
2845 |
> |
rs[3].assertInvoked(); |
2846 |
> |
} catch (CompletionException ok) { |
2847 |
> |
checkCompletedWithWrappedException(h3, ex); |
2848 |
> |
rs[3].assertNotInvoked(); |
2849 |
|
} |
2850 |
|
|
2689 |
– |
checkCompletedWithWrappedCFException(f, ex); |
2690 |
– |
checkCompletedNormally(g, v1); |
2691 |
– |
}} |
2692 |
– |
|
2693 |
– |
/** |
2694 |
– |
* runAfterEither result completes exceptionally if action does |
2695 |
– |
*/ |
2696 |
– |
public void testRunAfterEither_actionFailed1() { |
2697 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2698 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2699 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2700 |
– |
{ |
2701 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2702 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2703 |
– |
final FailingRunnable r = new FailingRunnable(); |
2704 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2705 |
– |
|
2706 |
– |
f.complete(v1); |
2707 |
– |
checkCompletedWithWrappedCFException(h); |
2708 |
– |
g.complete(v2); |
2709 |
– |
checkCompletedNormally(f, v1); |
2710 |
– |
checkCompletedNormally(g, v2); |
2711 |
– |
}} |
2712 |
– |
|
2713 |
– |
public void testRunAfterEither_actionFailed2() { |
2714 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2715 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2716 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
2717 |
– |
{ |
2718 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2719 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2720 |
– |
final FailingRunnable r = new FailingRunnable(); |
2721 |
– |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2722 |
– |
|
2723 |
– |
g.complete(v2); |
2724 |
– |
checkCompletedWithWrappedCFException(h); |
2725 |
– |
f.complete(v1); |
2851 |
|
checkCompletedNormally(f, v1); |
2852 |
< |
checkCompletedNormally(g, v2); |
2852 |
> |
checkCompletedExceptionally(g, ex); |
2853 |
|
}} |
2854 |
|
|
2855 |
|
/** |
2856 |
|
* runAfterEither result completes exceptionally if either source cancelled |
2857 |
|
*/ |
2858 |
< |
public void testRunAfterEither_sourceCancelled1() { |
2858 |
> |
public void testRunAfterEither_sourceCancelled() { |
2859 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2860 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2861 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2862 |
|
{ |
2863 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2864 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2865 |
< |
final Noop r = new Noop(); |
2866 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2742 |
< |
|
2743 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2744 |
< |
checkCompletedWithWrappedCancellationException(h); |
2745 |
< |
g.complete(v1); |
2746 |
< |
|
2747 |
< |
checkCancelled(f); |
2748 |
< |
assertEquals(0, r.invocationCount); |
2749 |
< |
checkCompletedNormally(g, v1); |
2750 |
< |
checkCompletedWithWrappedCancellationException(h); |
2751 |
< |
}} |
2865 |
> |
final Noop[] rs = new Noop[6]; |
2866 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2867 |
|
|
2868 |
< |
public void testRunAfterEither_sourceCancelled2() { |
2869 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2870 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2871 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2872 |
< |
{ |
2873 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2874 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2875 |
< |
final Noop r = new Noop(); |
2876 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2877 |
< |
|
2878 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2879 |
< |
checkCompletedWithWrappedCancellationException(h); |
2880 |
< |
f.complete(v1); |
2881 |
< |
|
2882 |
< |
checkCancelled(g); |
2883 |
< |
assertEquals(0, r.invocationCount); |
2884 |
< |
checkCompletedNormally(f, v1); |
2885 |
< |
checkCompletedWithWrappedCancellationException(h); |
2886 |
< |
}} |
2887 |
< |
|
2888 |
< |
public void testRunAfterEither_sourceCancelled3() { |
2889 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2890 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2891 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2892 |
< |
{ |
2893 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2779 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2780 |
< |
final Noop r = new Noop(); |
2781 |
< |
|
2782 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2783 |
< |
f.complete(v1); |
2784 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2785 |
< |
|
2786 |
< |
// unspecified behavior |
2787 |
< |
Integer v; |
2868 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2869 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2870 |
> |
checkIncomplete(h0); |
2871 |
> |
checkIncomplete(h1); |
2872 |
> |
rs[0].assertNotInvoked(); |
2873 |
> |
rs[1].assertNotInvoked(); |
2874 |
> |
f.cancel(mayInterruptIfRunning); |
2875 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2876 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2877 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2878 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2879 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2880 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2881 |
> |
|
2882 |
> |
assertTrue(g.complete(v1)); |
2883 |
> |
|
2884 |
> |
// unspecified behavior - both source completions available |
2885 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2886 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2887 |
> |
try { |
2888 |
> |
assertNull(h4.join()); |
2889 |
> |
rs[4].assertInvoked(); |
2890 |
> |
} catch (CompletionException ok) { |
2891 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2892 |
> |
rs[4].assertNotInvoked(); |
2893 |
> |
} |
2894 |
|
try { |
2895 |
< |
assertNull(h.join()); |
2896 |
< |
assertEquals(1, r.invocationCount); |
2895 |
> |
assertNull(h5.join()); |
2896 |
> |
rs[5].assertInvoked(); |
2897 |
|
} catch (CompletionException ok) { |
2898 |
< |
checkCompletedWithWrappedCancellationException(h); |
2899 |
< |
assertEquals(0, r.invocationCount); |
2898 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2899 |
> |
rs[5].assertNotInvoked(); |
2900 |
|
} |
2901 |
|
|
2902 |
< |
checkCancelled(g); |
2903 |
< |
checkCompletedNormally(f, v1); |
2902 |
> |
checkCancelled(f); |
2903 |
> |
checkCompletedNormally(g, v1); |
2904 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2905 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2906 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2907 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2908 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2909 |
|
}} |
2910 |
|
|
2911 |
< |
public void testRunAfterEither_sourceCancelled4() { |
2911 |
> |
/** |
2912 |
> |
* runAfterEither result completes exceptionally if action does |
2913 |
> |
*/ |
2914 |
> |
public void testRunAfterEither_actionFailed() { |
2915 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2802 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2916 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2917 |
+ |
for (Integer v2 : new Integer[] { 2, null }) |
2918 |
|
{ |
2919 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2920 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2921 |
< |
final Noop r = new Noop(); |
2922 |
< |
|
2809 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2810 |
< |
g.complete(v1); |
2811 |
< |
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2921 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
2922 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
2923 |
|
|
2924 |
< |
// unspecified behavior |
2925 |
< |
Integer v; |
2926 |
< |
try { |
2927 |
< |
assertNull(h.join()); |
2928 |
< |
assertEquals(1, r.invocationCount); |
2929 |
< |
} catch (CompletionException ok) { |
2930 |
< |
checkCompletedWithWrappedCancellationException(h); |
2931 |
< |
assertEquals(0, r.invocationCount); |
2932 |
< |
} |
2924 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2925 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2926 |
> |
assertTrue(f.complete(v1)); |
2927 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2928 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2929 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2930 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2931 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2932 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2933 |
> |
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2934 |
> |
assertTrue(g.complete(v2)); |
2935 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2936 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2937 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2938 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2939 |
|
|
2940 |
< |
checkCancelled(f); |
2941 |
< |
checkCompletedNormally(g, v1); |
2940 |
> |
checkCompletedNormally(f, v1); |
2941 |
> |
checkCompletedNormally(g, v2); |
2942 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2943 |
|
}} |
2944 |
|
|
2945 |
|
/** |
2951 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2952 |
|
{ |
2953 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2954 |
< |
final CompletableFutureInc r = new CompletableFutureInc(); |
2955 |
< |
if (!createIncomplete) f.complete(v1); |
2956 |
< |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2957 |
< |
if (createIncomplete) f.complete(v1); |
2954 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2955 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2956 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2957 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2958 |
|
|
2959 |
|
checkCompletedNormally(g, inc(v1)); |
2960 |
|
checkCompletedNormally(f, v1); |
2961 |
< |
assertEquals(1, r.invocationCount); |
2961 |
> |
r.assertValue(v1); |
2962 |
|
}} |
2963 |
|
|
2964 |
|
/** |
2970 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
2971 |
|
{ |
2972 |
|
final CFException ex = new CFException(); |
2973 |
< |
final CompletableFutureInc r = new CompletableFutureInc(); |
2973 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2974 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2975 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
2976 |
< |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2976 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2977 |
|
if (createIncomplete) f.completeExceptionally(ex); |
2978 |
|
|
2979 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2980 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2981 |
< |
assertEquals(0, r.invocationCount); |
2979 |
> |
checkCompletedWithWrappedException(g, ex); |
2980 |
> |
checkCompletedExceptionally(f, ex); |
2981 |
> |
r.assertNotInvoked(); |
2982 |
|
}} |
2983 |
|
|
2984 |
|
/** |
2991 |
|
{ |
2992 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2993 |
|
final FailingCompletableFutureFunction r |
2994 |
< |
= new FailingCompletableFutureFunction(); |
2995 |
< |
if (!createIncomplete) f.complete(v1); |
2996 |
< |
final CompletableFuture<Integer> g = f.thenCompose(r); |
2997 |
< |
if (createIncomplete) f.complete(v1); |
2994 |
> |
= new FailingCompletableFutureFunction(m); |
2995 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2996 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2997 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2998 |
|
|
2999 |
< |
checkCompletedWithWrappedCFException(g); |
2999 |
> |
checkCompletedWithWrappedException(g, r.ex); |
3000 |
|
checkCompletedNormally(f, v1); |
3001 |
|
}} |
3002 |
|
|
3009 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3010 |
|
{ |
3011 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3012 |
< |
final CompletableFutureInc r = new CompletableFutureInc(); |
3012 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
3013 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
3014 |
< |
final CompletableFuture<Integer> g = f.thenCompose(r); |
3014 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
3015 |
|
if (createIncomplete) { |
3016 |
|
checkIncomplete(g); |
3017 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
3021 |
|
checkCancelled(f); |
3022 |
|
}} |
3023 |
|
|
3024 |
+ |
/** |
3025 |
+ |
* thenCompose result completes exceptionally if the result of the action does |
3026 |
+ |
*/ |
3027 |
+ |
public void testThenCompose_actionReturnsFailingFuture() { |
3028 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
3029 |
+ |
for (int order = 0; order < 6; order++) |
3030 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3031 |
+ |
{ |
3032 |
+ |
final CFException ex = new CFException(); |
3033 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3034 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3035 |
+ |
final CompletableFuture<Integer> h; |
3036 |
+ |
// Test all permutations of orders |
3037 |
+ |
switch (order) { |
3038 |
+ |
case 0: |
3039 |
+ |
assertTrue(f.complete(v1)); |
3040 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3041 |
+ |
h = m.thenCompose(f, (x -> g)); |
3042 |
+ |
break; |
3043 |
+ |
case 1: |
3044 |
+ |
assertTrue(f.complete(v1)); |
3045 |
+ |
h = m.thenCompose(f, (x -> g)); |
3046 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3047 |
+ |
break; |
3048 |
+ |
case 2: |
3049 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3050 |
+ |
assertTrue(f.complete(v1)); |
3051 |
+ |
h = m.thenCompose(f, (x -> g)); |
3052 |
+ |
break; |
3053 |
+ |
case 3: |
3054 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3055 |
+ |
h = m.thenCompose(f, (x -> g)); |
3056 |
+ |
assertTrue(f.complete(v1)); |
3057 |
+ |
break; |
3058 |
+ |
case 4: |
3059 |
+ |
h = m.thenCompose(f, (x -> g)); |
3060 |
+ |
assertTrue(f.complete(v1)); |
3061 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3062 |
+ |
break; |
3063 |
+ |
case 5: |
3064 |
+ |
h = m.thenCompose(f, (x -> g)); |
3065 |
+ |
assertTrue(f.complete(v1)); |
3066 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3067 |
+ |
break; |
3068 |
+ |
default: throw new AssertionError(); |
3069 |
+ |
} |
3070 |
+ |
|
3071 |
+ |
checkCompletedExceptionally(g, ex); |
3072 |
+ |
checkCompletedWithWrappedException(h, ex); |
3073 |
+ |
checkCompletedNormally(f, v1); |
3074 |
+ |
}} |
3075 |
+ |
|
3076 |
|
// other static methods |
3077 |
|
|
3078 |
|
/** |
3089 |
|
* when all components complete normally |
3090 |
|
*/ |
3091 |
|
public void testAllOf_normal() throws Exception { |
3092 |
< |
for (int k = 1; k < 20; ++k) { |
3093 |
< |
CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3094 |
< |
for (int i = 0; i < k; ++i) |
3092 |
> |
for (int k = 1; k < 10; k++) { |
3093 |
> |
CompletableFuture<Integer>[] fs |
3094 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3095 |
> |
for (int i = 0; i < k; i++) |
3096 |
|
fs[i] = new CompletableFuture<>(); |
3097 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3098 |
< |
for (int i = 0; i < k; ++i) { |
3098 |
> |
for (int i = 0; i < k; i++) { |
3099 |
|
checkIncomplete(f); |
3100 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3101 |
|
fs[i].complete(one); |
3105 |
|
} |
3106 |
|
} |
3107 |
|
|
3108 |
+ |
public void testAllOf_normal_backwards() throws Exception { |
3109 |
+ |
for (int k = 1; k < 10; k++) { |
3110 |
+ |
CompletableFuture<Integer>[] fs |
3111 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3112 |
+ |
for (int i = 0; i < k; i++) |
3113 |
+ |
fs[i] = new CompletableFuture<>(); |
3114 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3115 |
+ |
for (int i = k - 1; i >= 0; i--) { |
3116 |
+ |
checkIncomplete(f); |
3117 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3118 |
+ |
fs[i].complete(one); |
3119 |
+ |
} |
3120 |
+ |
checkCompletedNormally(f, null); |
3121 |
+ |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
3122 |
+ |
} |
3123 |
+ |
} |
3124 |
+ |
|
3125 |
+ |
public void testAllOf_exceptional() throws Exception { |
3126 |
+ |
for (int k = 1; k < 10; k++) { |
3127 |
+ |
CompletableFuture<Integer>[] fs |
3128 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3129 |
+ |
CFException ex = new CFException(); |
3130 |
+ |
for (int i = 0; i < k; i++) |
3131 |
+ |
fs[i] = new CompletableFuture<>(); |
3132 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3133 |
+ |
for (int i = 0; i < k; i++) { |
3134 |
+ |
checkIncomplete(f); |
3135 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3136 |
+ |
if (i != k / 2) { |
3137 |
+ |
fs[i].complete(i); |
3138 |
+ |
checkCompletedNormally(fs[i], i); |
3139 |
+ |
} else { |
3140 |
+ |
fs[i].completeExceptionally(ex); |
3141 |
+ |
checkCompletedExceptionally(fs[i], ex); |
3142 |
+ |
} |
3143 |
+ |
} |
3144 |
+ |
checkCompletedWithWrappedException(f, ex); |
3145 |
+ |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3146 |
+ |
} |
3147 |
+ |
} |
3148 |
+ |
|
3149 |
|
/** |
3150 |
|
* anyOf(no component futures) returns an incomplete future |
3151 |
|
*/ |
3152 |
|
public void testAnyOf_empty() throws Exception { |
3153 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3154 |
+ |
{ |
3155 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3156 |
|
checkIncomplete(f); |
3157 |
< |
} |
3157 |
> |
|
3158 |
> |
f.complete(v1); |
3159 |
> |
checkCompletedNormally(f, v1); |
3160 |
> |
}} |
3161 |
|
|
3162 |
|
/** |
3163 |
|
* anyOf returns a future completed normally with a value when |
3164 |
|
* a component future does |
3165 |
|
*/ |
3166 |
|
public void testAnyOf_normal() throws Exception { |
3167 |
< |
for (int k = 0; k < 10; ++k) { |
3167 |
> |
for (int k = 0; k < 10; k++) { |
3168 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3169 |
< |
for (int i = 0; i < k; ++i) |
3169 |
> |
for (int i = 0; i < k; i++) |
3170 |
|
fs[i] = new CompletableFuture<>(); |
3171 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3172 |
|
checkIncomplete(f); |
3173 |
< |
for (int i = 0; i < k; ++i) { |
3174 |
< |
fs[i].complete(one); |
3175 |
< |
checkCompletedNormally(f, one); |
3176 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
3173 |
> |
for (int i = 0; i < k; i++) { |
3174 |
> |
fs[i].complete(i); |
3175 |
> |
checkCompletedNormally(f, 0); |
3176 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3177 |
> |
assertTrue(0 <= x && x <= i); |
3178 |
> |
} |
3179 |
> |
} |
3180 |
> |
} |
3181 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3182 |
> |
for (int k = 0; k < 10; k++) { |
3183 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3184 |
> |
for (int i = 0; i < k; i++) |
3185 |
> |
fs[i] = new CompletableFuture<>(); |
3186 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3187 |
> |
checkIncomplete(f); |
3188 |
> |
for (int i = k - 1; i >= 0; i--) { |
3189 |
> |
fs[i].complete(i); |
3190 |
> |
checkCompletedNormally(f, k - 1); |
3191 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3192 |
> |
assertTrue(i <= x && x <= k - 1); |
3193 |
|
} |
3194 |
|
} |
3195 |
|
} |
3198 |
|
* anyOf result completes exceptionally when any component does. |
3199 |
|
*/ |
3200 |
|
public void testAnyOf_exceptional() throws Exception { |
3201 |
< |
for (int k = 0; k < 10; ++k) { |
3201 |
> |
for (int k = 0; k < 10; k++) { |
3202 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3203 |
< |
for (int i = 0; i < k; ++i) |
3203 |
> |
CFException[] exs = new CFException[k]; |
3204 |
> |
for (int i = 0; i < k; i++) { |
3205 |
|
fs[i] = new CompletableFuture<>(); |
3206 |
+ |
exs[i] = new CFException(); |
3207 |
+ |
} |
3208 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3209 |
|
checkIncomplete(f); |
3210 |
< |
for (int i = 0; i < k; ++i) { |
3211 |
< |
fs[i].completeExceptionally(new CFException()); |
3212 |
< |
checkCompletedWithWrappedCFException(f); |
3210 |
> |
for (int i = 0; i < k; i++) { |
3211 |
> |
fs[i].completeExceptionally(exs[i]); |
3212 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3213 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3214 |
> |
} |
3215 |
> |
} |
3216 |
> |
} |
3217 |
> |
|
3218 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3219 |
> |
for (int k = 0; k < 10; k++) { |
3220 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3221 |
> |
CFException[] exs = new CFException[k]; |
3222 |
> |
for (int i = 0; i < k; i++) { |
3223 |
> |
fs[i] = new CompletableFuture<>(); |
3224 |
> |
exs[i] = new CFException(); |
3225 |
> |
} |
3226 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3227 |
> |
checkIncomplete(f); |
3228 |
> |
for (int i = k - 1; i >= 0; i--) { |
3229 |
> |
fs[i].completeExceptionally(exs[i]); |
3230 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3231 |
|
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3232 |
|
} |
3233 |
|
} |
3236 |
|
/** |
3237 |
|
* Completion methods throw NullPointerException with null arguments |
3238 |
|
*/ |
3239 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
3240 |
|
public void testNPE() { |
3241 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3242 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3243 |
|
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2989 |
– |
CompletableFuture<?> h; |
3244 |
|
ThreadExecutor exec = new ThreadExecutor(); |
3245 |
|
|
3246 |
|
Runnable[] throwingActions = { |
3247 |
|
() -> CompletableFuture.supplyAsync(null), |
3248 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3249 |
< |
() -> CompletableFuture.supplyAsync(supplyOne, null), |
3249 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3250 |
|
|
3251 |
|
() -> CompletableFuture.runAsync(null), |
3252 |
|
() -> CompletableFuture.runAsync(null, exec), |
3256 |
|
|
3257 |
|
() -> f.thenApply(null), |
3258 |
|
() -> f.thenApplyAsync(null), |
3259 |
< |
() -> f.thenApplyAsync((x) -> x, null), |
3259 |
> |
() -> f.thenApplyAsync(x -> x, null), |
3260 |
|
() -> f.thenApplyAsync(null, exec), |
3261 |
|
|
3262 |
|
() -> f.thenAccept(null), |
3263 |
|
() -> f.thenAcceptAsync(null), |
3264 |
< |
() -> f.thenAcceptAsync((x) -> {} , null), |
3264 |
> |
() -> f.thenAcceptAsync(x -> {} , null), |
3265 |
|
() -> f.thenAcceptAsync(null, exec), |
3266 |
|
|
3267 |
|
() -> f.thenRun(null), |
3296 |
|
() -> f.applyToEither(g, null), |
3297 |
|
() -> f.applyToEitherAsync(g, null), |
3298 |
|
() -> f.applyToEitherAsync(g, null, exec), |
3299 |
< |
() -> f.applyToEither(nullFuture, (x) -> x), |
3300 |
< |
() -> f.applyToEitherAsync(nullFuture, (x) -> x), |
3301 |
< |
() -> f.applyToEitherAsync(nullFuture, (x) -> x, exec), |
3302 |
< |
() -> f.applyToEitherAsync(g, (x) -> x, null), |
3299 |
> |
() -> f.applyToEither(nullFuture, x -> x), |
3300 |
> |
() -> f.applyToEitherAsync(nullFuture, x -> x), |
3301 |
> |
() -> f.applyToEitherAsync(nullFuture, x -> x, exec), |
3302 |
> |
() -> f.applyToEitherAsync(g, x -> x, null), |
3303 |
|
|
3304 |
|
() -> f.acceptEither(g, null), |
3305 |
|
() -> f.acceptEitherAsync(g, null), |
3306 |
|
() -> f.acceptEitherAsync(g, null, exec), |
3307 |
< |
() -> f.acceptEither(nullFuture, (x) -> {}), |
3308 |
< |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}), |
3309 |
< |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec), |
3310 |
< |
() -> f.acceptEitherAsync(g, (x) -> {}, null), |
3307 |
> |
() -> f.acceptEither(nullFuture, x -> {}), |
3308 |
> |
() -> f.acceptEitherAsync(nullFuture, x -> {}), |
3309 |
> |
() -> f.acceptEitherAsync(nullFuture, x -> {}, exec), |
3310 |
> |
() -> f.acceptEitherAsync(g, x -> {}, null), |
3311 |
|
|
3312 |
|
() -> f.runAfterEither(g, null), |
3313 |
|
() -> f.runAfterEitherAsync(g, null), |
3319 |
|
|
3320 |
|
() -> f.thenCompose(null), |
3321 |
|
() -> f.thenComposeAsync(null), |
3322 |
< |
() -> f.thenComposeAsync(new CompletableFutureInc(), null), |
3322 |
> |
() -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null), |
3323 |
|
() -> f.thenComposeAsync(null, exec), |
3324 |
|
|
3325 |
|
() -> f.exceptionally(null), |
3337 |
|
() -> CompletableFuture.anyOf(null, f), |
3338 |
|
|
3339 |
|
() -> f.obtrudeException(null), |
3340 |
+ |
|
3341 |
+ |
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3342 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
3343 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null), |
3344 |
+ |
|
3345 |
+ |
() -> f.orTimeout(1L, null), |
3346 |
+ |
() -> f.completeOnTimeout(42, 1L, null), |
3347 |
+ |
|
3348 |
+ |
() -> CompletableFuture.failedFuture(null), |
3349 |
+ |
() -> CompletableFuture.failedStage(null), |
3350 |
|
}; |
3351 |
|
|
3352 |
|
assertThrows(NullPointerException.class, throwingActions); |
3354 |
|
} |
3355 |
|
|
3356 |
|
/** |
3357 |
+ |
* Test submissions to an executor that rejects all tasks. |
3358 |
+ |
*/ |
3359 |
+ |
public void testRejectingExecutor() { |
3360 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3361 |
+ |
{ |
3362 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3363 |
+ |
|
3364 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3365 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3366 |
+ |
|
3367 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3368 |
+ |
|
3369 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3370 |
+ |
srcs.add(complete); |
3371 |
+ |
srcs.add(incomplete); |
3372 |
+ |
|
3373 |
+ |
for (CompletableFuture<Integer> src : srcs) { |
3374 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3375 |
+ |
fs.add(src.thenRunAsync(() -> {}, e)); |
3376 |
+ |
fs.add(src.thenAcceptAsync(z -> {}, e)); |
3377 |
+ |
fs.add(src.thenApplyAsync(z -> z, e)); |
3378 |
+ |
|
3379 |
+ |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3380 |
+ |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3381 |
+ |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3382 |
+ |
|
3383 |
+ |
fs.add(src.applyToEitherAsync(src, z -> z, e)); |
3384 |
+ |
fs.add(src.acceptEitherAsync(src, z -> {}, e)); |
3385 |
+ |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3386 |
+ |
|
3387 |
+ |
fs.add(src.thenComposeAsync(z -> null, e)); |
3388 |
+ |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3389 |
+ |
fs.add(src.handleAsync((z, t) -> null, e)); |
3390 |
+ |
|
3391 |
+ |
for (CompletableFuture<?> future : fs) { |
3392 |
+ |
if (src.isDone()) |
3393 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3394 |
+ |
else |
3395 |
+ |
checkIncomplete(future); |
3396 |
+ |
} |
3397 |
+ |
futures.addAll(fs); |
3398 |
+ |
} |
3399 |
+ |
|
3400 |
+ |
{ |
3401 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3402 |
+ |
|
3403 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3404 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3405 |
+ |
|
3406 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3407 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3408 |
+ |
|
3409 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3410 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3411 |
+ |
|
3412 |
+ |
for (CompletableFuture<?> future : fs) |
3413 |
+ |
checkIncomplete(future); |
3414 |
+ |
futures.addAll(fs); |
3415 |
+ |
} |
3416 |
+ |
|
3417 |
+ |
{ |
3418 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3419 |
+ |
|
3420 |
+ |
fs.add(complete.applyToEitherAsync(incomplete, z -> z, e)); |
3421 |
+ |
fs.add(incomplete.applyToEitherAsync(complete, z -> z, e)); |
3422 |
+ |
|
3423 |
+ |
fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e)); |
3424 |
+ |
fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e)); |
3425 |
+ |
|
3426 |
+ |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3427 |
+ |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3428 |
+ |
|
3429 |
+ |
for (CompletableFuture<?> future : fs) |
3430 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3431 |
+ |
futures.addAll(fs); |
3432 |
+ |
} |
3433 |
+ |
|
3434 |
+ |
incomplete.complete(v); |
3435 |
+ |
|
3436 |
+ |
for (CompletableFuture<?> future : futures) |
3437 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3438 |
+ |
|
3439 |
+ |
assertEquals(futures.size(), e.count.get()); |
3440 |
+ |
}} |
3441 |
+ |
|
3442 |
+ |
/** |
3443 |
+ |
* Test submissions to an executor that rejects all tasks, but |
3444 |
+ |
* should never be invoked because the dependent future is |
3445 |
+ |
* explicitly completed. |
3446 |
+ |
*/ |
3447 |
+ |
public void testRejectingExecutorNeverInvoked() { |
3448 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3449 |
+ |
{ |
3450 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3451 |
+ |
|
3452 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3453 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3454 |
+ |
|
3455 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3456 |
+ |
|
3457 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3458 |
+ |
srcs.add(complete); |
3459 |
+ |
srcs.add(incomplete); |
3460 |
+ |
|
3461 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3462 |
+ |
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3463 |
+ |
fs.add(incomplete.thenAcceptAsync(z -> {}, e)); |
3464 |
+ |
fs.add(incomplete.thenApplyAsync(z -> z, e)); |
3465 |
+ |
|
3466 |
+ |
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3467 |
+ |
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3468 |
+ |
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3469 |
+ |
|
3470 |
+ |
fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e)); |
3471 |
+ |
fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e)); |
3472 |
+ |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3473 |
+ |
|
3474 |
+ |
fs.add(incomplete.thenComposeAsync(z -> null, e)); |
3475 |
+ |
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3476 |
+ |
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3477 |
+ |
|
3478 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3479 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3480 |
+ |
|
3481 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3482 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3483 |
+ |
|
3484 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3485 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3486 |
+ |
|
3487 |
+ |
for (CompletableFuture<?> future : fs) |
3488 |
+ |
checkIncomplete(future); |
3489 |
+ |
|
3490 |
+ |
for (CompletableFuture<?> future : fs) |
3491 |
+ |
future.complete(null); |
3492 |
+ |
|
3493 |
+ |
incomplete.complete(v); |
3494 |
+ |
|
3495 |
+ |
for (CompletableFuture<?> future : fs) |
3496 |
+ |
checkCompletedNormally(future, null); |
3497 |
+ |
|
3498 |
+ |
assertEquals(0, e.count.get()); |
3499 |
+ |
}} |
3500 |
+ |
|
3501 |
+ |
/** |
3502 |
|
* toCompletableFuture returns this CompletableFuture. |
3503 |
|
*/ |
3504 |
|
public void testToCompletableFuture() { |
3506 |
|
assertSame(f, f.toCompletableFuture()); |
3507 |
|
} |
3508 |
|
|
3509 |
+ |
// jdk9 |
3510 |
+ |
|
3511 |
|
/** |
3512 |
< |
* whenComplete action executes on normal completion, propagating |
3102 |
< |
* source result. |
3512 |
> |
* newIncompleteFuture returns an incomplete CompletableFuture |
3513 |
|
*/ |
3514 |
< |
public void testWhenComplete_normalCompletion1() { |
3105 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
3106 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
3514 |
> |
public void testNewIncompleteFuture() { |
3515 |
|
for (Integer v1 : new Integer[] { 1, null }) |
3516 |
|
{ |
3517 |
< |
final AtomicInteger a = new AtomicInteger(0); |
3518 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3519 |
< |
if (!createIncomplete) f.complete(v1); |
3520 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
3521 |
< |
(f, |
3114 |
< |
(Integer x, Throwable t) -> { |
3115 |
< |
threadAssertSame(x, v1); |
3116 |
< |
threadAssertNull(t); |
3117 |
< |
a.getAndIncrement(); |
3118 |
< |
}); |
3119 |
< |
if (createIncomplete) f.complete(v1); |
3120 |
< |
|
3121 |
< |
checkCompletedNormally(g, v1); |
3517 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3518 |
> |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3519 |
> |
checkIncomplete(f); |
3520 |
> |
checkIncomplete(g); |
3521 |
> |
f.complete(v1); |
3522 |
|
checkCompletedNormally(f, v1); |
3523 |
< |
assertEquals(1, a.get()); |
3523 |
> |
checkIncomplete(g); |
3524 |
> |
g.complete(v1); |
3525 |
> |
checkCompletedNormally(g, v1); |
3526 |
> |
assertSame(g.getClass(), CompletableFuture.class); |
3527 |
|
}} |
3528 |
|
|
3529 |
|
/** |
3530 |
< |
* whenComplete action executes on exceptional completion, propagating |
3128 |
< |
* source result. |
3530 |
> |
* completedStage returns a completed CompletionStage |
3531 |
|
*/ |
3532 |
< |
public void testWhenComplete_exceptionalCompletion() { |
3533 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
3532 |
> |
public void testCompletedStage() { |
3533 |
> |
AtomicInteger x = new AtomicInteger(0); |
3534 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3535 |
> |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3536 |
> |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3537 |
> |
assertEquals(x.get(), 1); |
3538 |
> |
assertNull(r.get()); |
3539 |
> |
} |
3540 |
> |
|
3541 |
> |
/** |
3542 |
> |
* defaultExecutor by default returns the commonPool if |
3543 |
> |
* it supports more than one thread. |
3544 |
> |
*/ |
3545 |
> |
public void testDefaultExecutor() { |
3546 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3547 |
> |
Executor e = f.defaultExecutor(); |
3548 |
> |
Executor c = ForkJoinPool.commonPool(); |
3549 |
> |
if (ForkJoinPool.getCommonPoolParallelism() > 1) |
3550 |
> |
assertSame(e, c); |
3551 |
> |
else |
3552 |
> |
assertNotSame(e, c); |
3553 |
> |
} |
3554 |
> |
|
3555 |
> |
/** |
3556 |
> |
* failedFuture returns a CompletableFuture completed |
3557 |
> |
* exceptionally with the given Exception |
3558 |
> |
*/ |
3559 |
> |
public void testFailedFuture() { |
3560 |
> |
CFException ex = new CFException(); |
3561 |
> |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3562 |
> |
checkCompletedExceptionally(f, ex); |
3563 |
> |
} |
3564 |
> |
|
3565 |
> |
/** |
3566 |
> |
* failedFuture(null) throws NPE |
3567 |
> |
*/ |
3568 |
> |
public void testFailedFuture_null() { |
3569 |
> |
try { |
3570 |
> |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3571 |
> |
shouldThrow(); |
3572 |
> |
} catch (NullPointerException success) {} |
3573 |
> |
} |
3574 |
> |
|
3575 |
> |
/** |
3576 |
> |
* copy returns a CompletableFuture that is completed normally, |
3577 |
> |
* with the same value, when source is. |
3578 |
> |
*/ |
3579 |
> |
public void testCopy_normalCompletion() { |
3580 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
3581 |
|
for (Integer v1 : new Integer[] { 1, null }) |
3582 |
|
{ |
3583 |
< |
final AtomicInteger a = new AtomicInteger(0); |
3584 |
< |
final CFException ex = new CFException(); |
3585 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3583 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3584 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
3585 |
> |
CompletableFuture<Integer> g = f.copy(); |
3586 |
> |
if (createIncomplete) { |
3587 |
> |
checkIncomplete(f); |
3588 |
> |
checkIncomplete(g); |
3589 |
> |
assertTrue(f.complete(v1)); |
3590 |
> |
} |
3591 |
> |
checkCompletedNormally(f, v1); |
3592 |
> |
checkCompletedNormally(g, v1); |
3593 |
> |
}} |
3594 |
> |
|
3595 |
> |
/** |
3596 |
> |
* copy returns a CompletableFuture that is completed exceptionally |
3597 |
> |
* when source is. |
3598 |
> |
*/ |
3599 |
> |
public void testCopy_exceptionalCompletion() { |
3600 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3601 |
> |
{ |
3602 |
> |
CFException ex = new CFException(); |
3603 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3604 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
3605 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
3606 |
< |
(f, |
3607 |
< |
(Integer x, Throwable t) -> { |
3608 |
< |
threadAssertNull(x); |
3609 |
< |
threadAssertSame(t, ex); |
3610 |
< |
a.getAndIncrement(); |
3611 |
< |
}); |
3612 |
< |
if (createIncomplete) f.completeExceptionally(ex); |
3147 |
< |
checkCompletedWithWrappedCFException(f, ex); |
3148 |
< |
checkCompletedWithWrappedCFException(g, ex); |
3149 |
< |
assertEquals(1, a.get()); |
3605 |
> |
CompletableFuture<Integer> g = f.copy(); |
3606 |
> |
if (createIncomplete) { |
3607 |
> |
checkIncomplete(f); |
3608 |
> |
checkIncomplete(g); |
3609 |
> |
f.completeExceptionally(ex); |
3610 |
> |
} |
3611 |
> |
checkCompletedExceptionally(f, ex); |
3612 |
> |
checkCompletedWithWrappedException(g, ex); |
3613 |
|
}} |
3614 |
|
|
3615 |
|
/** |
3616 |
< |
* whenComplete action executes on cancelled source, propagating |
3154 |
< |
* CancellationException. |
3616 |
> |
* Completion of a copy does not complete its source. |
3617 |
|
*/ |
3618 |
< |
public void testWhenComplete_sourceCancelled() { |
3618 |
> |
public void testCopy_oneWayPropagation() { |
3619 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3620 |
> |
assertTrue(f.copy().complete(1)); |
3621 |
> |
assertTrue(f.copy().complete(null)); |
3622 |
> |
assertTrue(f.copy().cancel(true)); |
3623 |
> |
assertTrue(f.copy().cancel(false)); |
3624 |
> |
assertTrue(f.copy().completeExceptionally(new CFException())); |
3625 |
> |
checkIncomplete(f); |
3626 |
> |
} |
3627 |
> |
|
3628 |
> |
/** |
3629 |
> |
* minimalCompletionStage returns a CompletableFuture that is |
3630 |
> |
* completed normally, with the same value, when source is. |
3631 |
> |
*/ |
3632 |
> |
public void testMinimalCompletionStage() { |
3633 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3634 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3635 |
> |
AtomicInteger x = new AtomicInteger(0); |
3636 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3637 |
> |
checkIncomplete(f); |
3638 |
> |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3639 |
> |
f.complete(1); |
3640 |
> |
checkCompletedNormally(f, 1); |
3641 |
> |
assertEquals(x.get(), 1); |
3642 |
> |
assertNull(r.get()); |
3643 |
> |
} |
3644 |
> |
|
3645 |
> |
/** |
3646 |
> |
* minimalCompletionStage returns a CompletableFuture that is |
3647 |
> |
* completed exceptionally when source is. |
3648 |
> |
*/ |
3649 |
> |
public void testMinimalCompletionStage2() { |
3650 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3651 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3652 |
> |
AtomicInteger x = new AtomicInteger(0); |
3653 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3654 |
> |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3655 |
> |
checkIncomplete(f); |
3656 |
> |
CFException ex = new CFException(); |
3657 |
> |
f.completeExceptionally(ex); |
3658 |
> |
checkCompletedExceptionally(f, ex); |
3659 |
> |
assertEquals(x.get(), 0); |
3660 |
> |
assertEquals(r.get().getCause(), ex); |
3661 |
> |
} |
3662 |
> |
|
3663 |
> |
/** |
3664 |
> |
* failedStage returns a CompletionStage completed |
3665 |
> |
* exceptionally with the given Exception |
3666 |
> |
*/ |
3667 |
> |
public void testFailedStage() { |
3668 |
> |
CFException ex = new CFException(); |
3669 |
> |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3670 |
> |
AtomicInteger x = new AtomicInteger(0); |
3671 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3672 |
> |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3673 |
> |
assertEquals(x.get(), 0); |
3674 |
> |
assertEquals(r.get(), ex); |
3675 |
> |
} |
3676 |
> |
|
3677 |
> |
/** |
3678 |
> |
* completeAsync completes with value of given supplier |
3679 |
> |
*/ |
3680 |
> |
public void testCompleteAsync() { |
3681 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3682 |
> |
{ |
3683 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3684 |
> |
f.completeAsync(() -> v1); |
3685 |
> |
f.join(); |
3686 |
> |
checkCompletedNormally(f, v1); |
3687 |
> |
}} |
3688 |
> |
|
3689 |
> |
/** |
3690 |
> |
* completeAsync completes exceptionally if given supplier throws |
3691 |
> |
*/ |
3692 |
> |
public void testCompleteAsync2() { |
3693 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3694 |
> |
CFException ex = new CFException(); |
3695 |
> |
f.completeAsync(() -> { throw ex; }); |
3696 |
> |
try { |
3697 |
> |
f.join(); |
3698 |
> |
shouldThrow(); |
3699 |
> |
} catch (CompletionException success) {} |
3700 |
> |
checkCompletedWithWrappedException(f, ex); |
3701 |
> |
} |
3702 |
> |
|
3703 |
> |
/** |
3704 |
> |
* completeAsync with given executor completes with value of given supplier |
3705 |
> |
*/ |
3706 |
> |
public void testCompleteAsync3() { |
3707 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3708 |
> |
{ |
3709 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3710 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3711 |
> |
f.completeAsync(() -> v1, executor); |
3712 |
> |
assertSame(v1, f.join()); |
3713 |
> |
checkCompletedNormally(f, v1); |
3714 |
> |
assertEquals(1, executor.count.get()); |
3715 |
> |
}} |
3716 |
> |
|
3717 |
> |
/** |
3718 |
> |
* completeAsync with given executor completes exceptionally if |
3719 |
> |
* given supplier throws |
3720 |
> |
*/ |
3721 |
> |
public void testCompleteAsync4() { |
3722 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3723 |
> |
CFException ex = new CFException(); |
3724 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3725 |
> |
f.completeAsync(() -> { throw ex; }, executor); |
3726 |
> |
try { |
3727 |
> |
f.join(); |
3728 |
> |
shouldThrow(); |
3729 |
> |
} catch (CompletionException success) {} |
3730 |
> |
checkCompletedWithWrappedException(f, ex); |
3731 |
> |
assertEquals(1, executor.count.get()); |
3732 |
> |
} |
3733 |
> |
|
3734 |
> |
/** |
3735 |
> |
* orTimeout completes with TimeoutException if not complete |
3736 |
> |
*/ |
3737 |
> |
public void testOrTimeout_timesOut() { |
3738 |
> |
long timeoutMillis = timeoutMillis(); |
3739 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3740 |
> |
long startTime = System.nanoTime(); |
3741 |
> |
assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); |
3742 |
> |
checkCompletedWithTimeoutException(f); |
3743 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3744 |
> |
} |
3745 |
> |
|
3746 |
> |
/** |
3747 |
> |
* orTimeout completes normally if completed before timeout |
3748 |
> |
*/ |
3749 |
> |
public void testOrTimeout_completed() { |
3750 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3751 |
> |
{ |
3752 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3753 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3754 |
> |
long startTime = System.nanoTime(); |
3755 |
> |
f.complete(v1); |
3756 |
> |
assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3757 |
> |
assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3758 |
> |
g.complete(v1); |
3759 |
> |
checkCompletedNormally(f, v1); |
3760 |
> |
checkCompletedNormally(g, v1); |
3761 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3762 |
> |
}} |
3763 |
> |
|
3764 |
> |
/** |
3765 |
> |
* completeOnTimeout completes with given value if not complete |
3766 |
> |
*/ |
3767 |
> |
public void testCompleteOnTimeout_timesOut() { |
3768 |
> |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3769 |
> |
() -> testCompleteOnTimeout_timesOut(null)); |
3770 |
> |
} |
3771 |
> |
|
3772 |
> |
/** |
3773 |
> |
* completeOnTimeout completes with given value if not complete |
3774 |
> |
*/ |
3775 |
> |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3776 |
> |
long timeoutMillis = timeoutMillis(); |
3777 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3778 |
> |
long startTime = System.nanoTime(); |
3779 |
> |
assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); |
3780 |
> |
assertSame(v, f.join()); |
3781 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3782 |
> |
f.complete(99); // should have no effect |
3783 |
> |
checkCompletedNormally(f, v); |
3784 |
> |
} |
3785 |
> |
|
3786 |
> |
/** |
3787 |
> |
* completeOnTimeout has no effect if completed within timeout |
3788 |
> |
*/ |
3789 |
> |
public void testCompleteOnTimeout_completed() { |
3790 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3791 |
> |
{ |
3792 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3793 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3794 |
> |
long startTime = System.nanoTime(); |
3795 |
> |
f.complete(v1); |
3796 |
> |
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3797 |
> |
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3798 |
> |
g.complete(v1); |
3799 |
> |
checkCompletedNormally(f, v1); |
3800 |
> |
checkCompletedNormally(g, v1); |
3801 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3802 |
> |
}} |
3803 |
> |
|
3804 |
> |
/** |
3805 |
> |
* delayedExecutor returns an executor that delays submission |
3806 |
> |
*/ |
3807 |
> |
public void testDelayedExecutor() { |
3808 |
> |
testInParallel(() -> testDelayedExecutor(null, null), |
3809 |
> |
() -> testDelayedExecutor(null, 1), |
3810 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3811 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3812 |
> |
} |
3813 |
> |
|
3814 |
> |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3815 |
> |
long timeoutMillis = timeoutMillis(); |
3816 |
> |
// Use an "unreasonably long" long timeout to catch lingering threads |
3817 |
> |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3818 |
> |
final Executor delayer, longDelayer; |
3819 |
> |
if (executor == null) { |
3820 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3821 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3822 |
> |
} else { |
3823 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3824 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3825 |
> |
} |
3826 |
> |
long startTime = System.nanoTime(); |
3827 |
> |
CompletableFuture<Integer> f = |
3828 |
> |
CompletableFuture.supplyAsync(() -> v, delayer); |
3829 |
> |
CompletableFuture<Integer> g = |
3830 |
> |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3831 |
> |
|
3832 |
> |
assertNull(g.getNow(null)); |
3833 |
> |
|
3834 |
> |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3835 |
> |
long millisElapsed = millisElapsedSince(startTime); |
3836 |
> |
assertTrue(millisElapsed >= timeoutMillis); |
3837 |
> |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3838 |
> |
|
3839 |
> |
checkCompletedNormally(f, v); |
3840 |
> |
|
3841 |
> |
checkIncomplete(g); |
3842 |
> |
assertTrue(g.cancel(true)); |
3843 |
> |
} |
3844 |
> |
|
3845 |
> |
//--- tests of implementation details; not part of official tck --- |
3846 |
> |
|
3847 |
> |
Object resultOf(CompletableFuture<?> f) { |
3848 |
> |
SecurityManager sm = System.getSecurityManager(); |
3849 |
> |
if (sm != null) { |
3850 |
> |
try { |
3851 |
> |
System.setSecurityManager(null); |
3852 |
> |
} catch (SecurityException giveUp) { |
3853 |
> |
return "Reflection not available"; |
3854 |
> |
} |
3855 |
> |
} |
3856 |
> |
|
3857 |
> |
try { |
3858 |
> |
java.lang.reflect.Field resultField |
3859 |
> |
= CompletableFuture.class.getDeclaredField("result"); |
3860 |
> |
resultField.setAccessible(true); |
3861 |
> |
return resultField.get(f); |
3862 |
> |
} catch (Throwable t) { |
3863 |
> |
throw new AssertionError(t); |
3864 |
> |
} finally { |
3865 |
> |
if (sm != null) System.setSecurityManager(sm); |
3866 |
> |
} |
3867 |
> |
} |
3868 |
> |
|
3869 |
> |
public void testExceptionPropagationReusesResultObject() { |
3870 |
> |
if (!testImplementationDetails) return; |
3871 |
|
for (ExecutionMode m : ExecutionMode.values()) |
3158 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3159 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
3872 |
|
{ |
3873 |
< |
final AtomicInteger a = new AtomicInteger(0); |
3874 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3875 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
3164 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
3165 |
< |
(f, |
3166 |
< |
(Integer x, Throwable t) -> { |
3167 |
< |
threadAssertNull(x); |
3168 |
< |
threadAssertTrue(t instanceof CancellationException); |
3169 |
< |
a.getAndIncrement(); |
3170 |
< |
}); |
3171 |
< |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
3873 |
> |
final CFException ex = new CFException(); |
3874 |
> |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3875 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3876 |
|
|
3877 |
< |
//try { g.join(); } catch (Throwable t) { throw new Error(t); } |
3878 |
< |
checkCompletedWithWrappedCancellationException(g); |
3879 |
< |
checkCancelled(f); |
3880 |
< |
assertEquals(1, a.get()); |
3877 |
> |
final Runnable noopRunnable = new Noop(m); |
3878 |
> |
final Consumer<Integer> noopConsumer = new NoopConsumer(m); |
3879 |
> |
final Function<Integer, Integer> incFunction = new IncFunction(m); |
3880 |
> |
|
3881 |
> |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3882 |
> |
= new ArrayList<>(); |
3883 |
> |
|
3884 |
> |
funs.add(y -> m.thenRun(y, noopRunnable)); |
3885 |
> |
funs.add(y -> m.thenAccept(y, noopConsumer)); |
3886 |
> |
funs.add(y -> m.thenApply(y, incFunction)); |
3887 |
> |
|
3888 |
> |
funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable)); |
3889 |
> |
funs.add(y -> m.acceptEither(y, incomplete, noopConsumer)); |
3890 |
> |
funs.add(y -> m.applyToEither(y, incomplete, incFunction)); |
3891 |
> |
|
3892 |
> |
funs.add(y -> m.runAfterBoth(y, v42, noopRunnable)); |
3893 |
> |
funs.add(y -> m.runAfterBoth(v42, y, noopRunnable)); |
3894 |
> |
funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3895 |
> |
funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3896 |
> |
funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3897 |
> |
funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3898 |
> |
|
3899 |
> |
funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3900 |
> |
|
3901 |
> |
funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m))); |
3902 |
> |
|
3903 |
> |
funs.add(y -> CompletableFuture.allOf(y)); |
3904 |
> |
funs.add(y -> CompletableFuture.allOf(y, v42)); |
3905 |
> |
funs.add(y -> CompletableFuture.allOf(v42, y)); |
3906 |
> |
funs.add(y -> CompletableFuture.anyOf(y)); |
3907 |
> |
funs.add(y -> CompletableFuture.anyOf(y, incomplete)); |
3908 |
> |
funs.add(y -> CompletableFuture.anyOf(incomplete, y)); |
3909 |
> |
|
3910 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3911 |
> |
fun : funs) { |
3912 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3913 |
> |
f.completeExceptionally(ex); |
3914 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3915 |
> |
checkCompletedWithWrappedException(src, ex); |
3916 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3917 |
> |
checkCompletedWithWrappedException(dep, ex); |
3918 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3919 |
> |
} |
3920 |
> |
|
3921 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3922 |
> |
fun : funs) { |
3923 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3924 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3925 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3926 |
> |
f.completeExceptionally(ex); |
3927 |
> |
checkCompletedWithWrappedException(src, ex); |
3928 |
> |
checkCompletedWithWrappedException(dep, ex); |
3929 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3930 |
> |
} |
3931 |
> |
|
3932 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3933 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3934 |
> |
fun : funs) { |
3935 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3936 |
> |
f.cancel(mayInterruptIfRunning); |
3937 |
> |
checkCancelled(f); |
3938 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3939 |
> |
checkCompletedWithWrappedCancellationException(src); |
3940 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3941 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3942 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3943 |
> |
} |
3944 |
> |
|
3945 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3946 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3947 |
> |
fun : funs) { |
3948 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3949 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3950 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3951 |
> |
f.cancel(mayInterruptIfRunning); |
3952 |
> |
checkCancelled(f); |
3953 |
> |
checkCompletedWithWrappedCancellationException(src); |
3954 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3955 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3956 |
> |
} |
3957 |
|
}} |
3958 |
|
|
3959 |
|
/** |
3960 |
< |
* If a whenComplete action throws an exception when triggered by |
3961 |
< |
* a normal completion, it completes exceptionally |
3960 |
> |
* Minimal completion stages throw UOE for most non-CompletionStage methods |
3961 |
> |
*/ |
3962 |
> |
public void testMinimalCompletionStage_minimality() { |
3963 |
> |
if (!testImplementationDetails) return; |
3964 |
> |
Function<Method, String> toSignature = |
3965 |
> |
method -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3966 |
> |
Predicate<Method> isNotStatic = |
3967 |
> |
method -> (method.getModifiers() & Modifier.STATIC) == 0; |
3968 |
> |
List<Method> minimalMethods = |
3969 |
> |
Stream.of(Object.class, CompletionStage.class) |
3970 |
> |
.flatMap(klazz -> Stream.of(klazz.getMethods())) |
3971 |
> |
.filter(isNotStatic) |
3972 |
> |
.collect(Collectors.toList()); |
3973 |
> |
// Methods from CompletableFuture permitted NOT to throw UOE |
3974 |
> |
String[] signatureWhitelist = { |
3975 |
> |
"newIncompleteFuture[]", |
3976 |
> |
"defaultExecutor[]", |
3977 |
> |
"minimalCompletionStage[]", |
3978 |
> |
"copy[]", |
3979 |
> |
}; |
3980 |
> |
Set<String> permittedMethodSignatures = |
3981 |
> |
Stream.concat(minimalMethods.stream().map(toSignature), |
3982 |
> |
Stream.of(signatureWhitelist)) |
3983 |
> |
.collect(Collectors.toSet()); |
3984 |
> |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3985 |
> |
.filter(isNotStatic) |
3986 |
> |
.filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3987 |
> |
.collect(Collectors.toList()); |
3988 |
> |
|
3989 |
> |
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
3990 |
> |
CompletionStage<Integer> min = |
3991 |
> |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3992 |
> |
stages.add(min); |
3993 |
> |
stages.add(min.thenApply(x -> x)); |
3994 |
> |
stages.add(CompletableFuture.completedStage(1)); |
3995 |
> |
stages.add(CompletableFuture.failedStage(new CFException())); |
3996 |
> |
|
3997 |
> |
List<Method> bugs = new ArrayList<>(); |
3998 |
> |
for (Method method : allMethods) { |
3999 |
> |
Class<?>[] parameterTypes = method.getParameterTypes(); |
4000 |
> |
Object[] args = new Object[parameterTypes.length]; |
4001 |
> |
// Manufacture boxed primitives for primitive params |
4002 |
> |
for (int i = 0; i < args.length; i++) { |
4003 |
> |
Class<?> type = parameterTypes[i]; |
4004 |
> |
if (parameterTypes[i] == boolean.class) |
4005 |
> |
args[i] = false; |
4006 |
> |
else if (parameterTypes[i] == int.class) |
4007 |
> |
args[i] = 0; |
4008 |
> |
else if (parameterTypes[i] == long.class) |
4009 |
> |
args[i] = 0L; |
4010 |
> |
} |
4011 |
> |
for (CompletionStage<Integer> stage : stages) { |
4012 |
> |
try { |
4013 |
> |
method.invoke(stage, args); |
4014 |
> |
bugs.add(method); |
4015 |
> |
} |
4016 |
> |
catch (java.lang.reflect.InvocationTargetException expected) { |
4017 |
> |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
4018 |
> |
bugs.add(method); |
4019 |
> |
// expected.getCause().printStackTrace(); |
4020 |
> |
} |
4021 |
> |
} |
4022 |
> |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
4023 |
> |
} |
4024 |
> |
} |
4025 |
> |
if (!bugs.isEmpty()) |
4026 |
> |
throw new Error("Methods did not throw UOE: " + bugs); |
4027 |
> |
} |
4028 |
> |
|
4029 |
> |
/** |
4030 |
> |
* minimalStage.toCompletableFuture() returns a CompletableFuture that |
4031 |
> |
* is completed normally, with the same value, when source is. |
4032 |
|
*/ |
4033 |
< |
public void testWhenComplete_actionFailed() { |
4033 |
> |
public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() { |
4034 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
3185 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
4035 |
|
for (Integer v1 : new Integer[] { 1, null }) |
4036 |
|
{ |
4037 |
< |
final AtomicInteger a = new AtomicInteger(0); |
4038 |
< |
final CFException ex = new CFException(); |
4039 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
4040 |
< |
if (!createIncomplete) f.complete(v1); |
4041 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
4042 |
< |
(f, |
4043 |
< |
(Integer x, Throwable t) -> { |
4044 |
< |
threadAssertSame(x, v1); |
4045 |
< |
threadAssertNull(t); |
3197 |
< |
a.getAndIncrement(); |
3198 |
< |
throw ex; |
3199 |
< |
}); |
3200 |
< |
if (createIncomplete) f.complete(v1); |
4037 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4038 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4039 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4040 |
> |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4041 |
> |
if (createIncomplete) { |
4042 |
> |
checkIncomplete(f); |
4043 |
> |
checkIncomplete(g); |
4044 |
> |
assertTrue(f.complete(v1)); |
4045 |
> |
} |
4046 |
|
checkCompletedNormally(f, v1); |
4047 |
< |
checkCompletedWithWrappedCFException(g, ex); |
3203 |
< |
assertEquals(1, a.get()); |
4047 |
> |
checkCompletedNormally(g, v1); |
4048 |
|
}} |
4049 |
|
|
4050 |
|
/** |
4051 |
< |
* If a whenComplete action throws an exception when triggered by |
4052 |
< |
* a source completion that also throws an exception, the source |
3209 |
< |
* exception takes precedence. |
4051 |
> |
* minimalStage.toCompletableFuture() returns a CompletableFuture that |
4052 |
> |
* is completed exceptionally when source is. |
4053 |
|
*/ |
4054 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
4054 |
> |
public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() { |
4055 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
4056 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
4056 |
> |
{ |
4057 |
> |
CFException ex = new CFException(); |
4058 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4059 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4060 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
4061 |
> |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4062 |
> |
if (createIncomplete) { |
4063 |
> |
checkIncomplete(f); |
4064 |
> |
checkIncomplete(g); |
4065 |
> |
f.completeExceptionally(ex); |
4066 |
> |
} |
4067 |
> |
checkCompletedExceptionally(f, ex); |
4068 |
> |
checkCompletedWithWrappedException(g, ex); |
4069 |
> |
}} |
4070 |
> |
|
4071 |
> |
/** |
4072 |
> |
* minimalStage.toCompletableFuture() gives mutable CompletableFuture |
4073 |
> |
*/ |
4074 |
> |
public void testMinimalCompletionStage_toCompletableFuture_mutable() { |
4075 |
|
for (Integer v1 : new Integer[] { 1, null }) |
4076 |
|
{ |
4077 |
< |
final AtomicInteger a = new AtomicInteger(0); |
4078 |
< |
final CFException ex1 = new CFException(); |
4079 |
< |
final CFException ex2 = new CFException(); |
4080 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
4077 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4078 |
> |
CompletionStage minimal = f.minimalCompletionStage(); |
4079 |
> |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4080 |
> |
assertTrue(g.complete(v1)); |
4081 |
> |
checkCompletedNormally(g, v1); |
4082 |
> |
checkIncomplete(f); |
4083 |
> |
checkIncomplete(minimal.toCompletableFuture()); |
4084 |
> |
}} |
4085 |
|
|
4086 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
4087 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
4088 |
< |
(f, |
4089 |
< |
(Integer x, Throwable t) -> { |
4090 |
< |
threadAssertSame(t, ex1); |
4091 |
< |
threadAssertNull(x); |
4092 |
< |
a.getAndIncrement(); |
4093 |
< |
throw ex2; |
4094 |
< |
}); |
4095 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
4086 |
> |
/** |
4087 |
> |
* minimalStage.toCompletableFuture().join() awaits completion |
4088 |
> |
*/ |
4089 |
> |
public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception { |
4090 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
4091 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
4092 |
> |
{ |
4093 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4094 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4095 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4096 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
4097 |
> |
assertEquals(v1, minimal.toCompletableFuture().join()); |
4098 |
> |
assertEquals(v1, minimal.toCompletableFuture().get()); |
4099 |
> |
checkCompletedNormally(minimal.toCompletableFuture(), v1); |
4100 |
> |
}} |
4101 |
|
|
4102 |
< |
checkCompletedWithWrappedCFException(f, ex1); |
4103 |
< |
checkCompletedWithWrappedCFException(g, ex1); |
4104 |
< |
assertEquals(1, a.get()); |
4102 |
> |
/** |
4103 |
> |
* Completion of a toCompletableFuture copy of a minimal stage |
4104 |
> |
* does not complete its source. |
4105 |
> |
*/ |
4106 |
> |
public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() { |
4107 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4108 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
4109 |
> |
assertTrue(g.toCompletableFuture().complete(1)); |
4110 |
> |
assertTrue(g.toCompletableFuture().complete(null)); |
4111 |
> |
assertTrue(g.toCompletableFuture().cancel(true)); |
4112 |
> |
assertTrue(g.toCompletableFuture().cancel(false)); |
4113 |
> |
assertTrue(g.toCompletableFuture().completeExceptionally(new CFException())); |
4114 |
> |
checkIncomplete(g.toCompletableFuture()); |
4115 |
> |
f.complete(1); |
4116 |
> |
checkCompletedNormally(g.toCompletableFuture(), 1); |
4117 |
> |
} |
4118 |
> |
|
4119 |
> |
/** Demo utility method for external reliable toCompletableFuture */ |
4120 |
> |
static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) { |
4121 |
> |
CompletableFuture<T> f = new CompletableFuture<>(); |
4122 |
> |
stage.handle((T t, Throwable ex) -> { |
4123 |
> |
if (ex != null) f.completeExceptionally(ex); |
4124 |
> |
else f.complete(t); |
4125 |
> |
return null; |
4126 |
> |
}); |
4127 |
> |
return f; |
4128 |
> |
} |
4129 |
> |
|
4130 |
> |
/** Demo utility method to join a CompletionStage */ |
4131 |
> |
static <T> T join(CompletionStage<T> stage) { |
4132 |
> |
return toCompletableFuture(stage).join(); |
4133 |
> |
} |
4134 |
> |
|
4135 |
> |
/** |
4136 |
> |
* Joining a minimal stage "by hand" works |
4137 |
> |
*/ |
4138 |
> |
public void testMinimalCompletionStage_join_by_hand() { |
4139 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
4140 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
4141 |
> |
{ |
4142 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4143 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4144 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
4145 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4146 |
> |
minimal.thenAccept(x -> g.complete(x)); |
4147 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
4148 |
> |
g.join(); |
4149 |
> |
checkCompletedNormally(g, v1); |
4150 |
> |
checkCompletedNormally(f, v1); |
4151 |
> |
assertEquals(v1, join(minimal)); |
4152 |
|
}} |
4153 |
|
|
4154 |
+ |
static class Monad { |
4155 |
+ |
static class ZeroException extends RuntimeException { |
4156 |
+ |
public ZeroException() { super("monadic zero"); } |
4157 |
+ |
} |
4158 |
+ |
// "return", "unit" |
4159 |
+ |
static <T> CompletableFuture<T> unit(T value) { |
4160 |
+ |
return completedFuture(value); |
4161 |
+ |
} |
4162 |
+ |
// monadic zero ? |
4163 |
+ |
static <T> CompletableFuture<T> zero() { |
4164 |
+ |
return failedFuture(new ZeroException()); |
4165 |
+ |
} |
4166 |
+ |
// >=> |
4167 |
+ |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
4168 |
+ |
(Function<T, CompletableFuture<U>> f, |
4169 |
+ |
Function<U, CompletableFuture<V>> g) { |
4170 |
+ |
return x -> f.apply(x).thenCompose(g); |
4171 |
+ |
} |
4172 |
+ |
|
4173 |
+ |
static void assertZero(CompletableFuture<?> f) { |
4174 |
+ |
try { |
4175 |
+ |
f.getNow(null); |
4176 |
+ |
throw new AssertionFailedError("should throw"); |
4177 |
+ |
} catch (CompletionException success) { |
4178 |
+ |
assertTrue(success.getCause() instanceof ZeroException); |
4179 |
+ |
} |
4180 |
+ |
} |
4181 |
+ |
|
4182 |
+ |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
4183 |
+ |
CompletableFuture<T> g) { |
4184 |
+ |
T fval = null, gval = null; |
4185 |
+ |
Throwable fex = null, gex = null; |
4186 |
+ |
|
4187 |
+ |
try { fval = f.get(); } |
4188 |
+ |
catch (ExecutionException ex) { fex = ex.getCause(); } |
4189 |
+ |
catch (Throwable ex) { fex = ex; } |
4190 |
+ |
|
4191 |
+ |
try { gval = g.get(); } |
4192 |
+ |
catch (ExecutionException ex) { gex = ex.getCause(); } |
4193 |
+ |
catch (Throwable ex) { gex = ex; } |
4194 |
+ |
|
4195 |
+ |
if (fex != null || gex != null) |
4196 |
+ |
assertSame(fex.getClass(), gex.getClass()); |
4197 |
+ |
else |
4198 |
+ |
assertEquals(fval, gval); |
4199 |
+ |
} |
4200 |
+ |
|
4201 |
+ |
static class PlusFuture<T> extends CompletableFuture<T> { |
4202 |
+ |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
4203 |
+ |
} |
4204 |
+ |
|
4205 |
+ |
/** Implements "monadic plus". */ |
4206 |
+ |
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
4207 |
+ |
CompletableFuture<? extends T> g) { |
4208 |
+ |
PlusFuture<T> plus = new PlusFuture<T>(); |
4209 |
+ |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
4210 |
+ |
try { |
4211 |
+ |
if (ex == null) { |
4212 |
+ |
if (plus.complete(result)) |
4213 |
+ |
if (plus.firstFailure.get() != null) |
4214 |
+ |
plus.firstFailure.set(null); |
4215 |
+ |
} |
4216 |
+ |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
4217 |
+ |
if (plus.isDone()) |
4218 |
+ |
plus.firstFailure.set(null); |
4219 |
+ |
} |
4220 |
+ |
else { |
4221 |
+ |
// first failure has precedence |
4222 |
+ |
Throwable first = plus.firstFailure.getAndSet(null); |
4223 |
+ |
|
4224 |
+ |
// may fail with "Self-suppression not permitted" |
4225 |
+ |
try { first.addSuppressed(ex); } |
4226 |
+ |
catch (Exception ignored) {} |
4227 |
+ |
|
4228 |
+ |
plus.completeExceptionally(first); |
4229 |
+ |
} |
4230 |
+ |
} catch (Throwable unexpected) { |
4231 |
+ |
plus.completeExceptionally(unexpected); |
4232 |
+ |
} |
4233 |
+ |
}; |
4234 |
+ |
f.whenComplete(action); |
4235 |
+ |
g.whenComplete(action); |
4236 |
+ |
return plus; |
4237 |
+ |
} |
4238 |
+ |
} |
4239 |
+ |
|
4240 |
+ |
/** |
4241 |
+ |
* CompletableFuture is an additive monad - sort of. |
4242 |
+ |
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
4243 |
+ |
*/ |
4244 |
+ |
public void testAdditiveMonad() throws Throwable { |
4245 |
+ |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
4246 |
+ |
CompletableFuture<Long> zero = Monad.zero(); |
4247 |
+ |
|
4248 |
+ |
// Some mutually non-commutative functions |
4249 |
+ |
Function<Long, CompletableFuture<Long>> triple |
4250 |
+ |
= x -> Monad.unit(3 * x); |
4251 |
+ |
Function<Long, CompletableFuture<Long>> inc |
4252 |
+ |
= x -> Monad.unit(x + 1); |
4253 |
+ |
|
4254 |
+ |
// unit is a right identity: m >>= unit === m |
4255 |
+ |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
4256 |
+ |
inc.apply(5L)); |
4257 |
+ |
// unit is a left identity: (unit x) >>= f === f x |
4258 |
+ |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
4259 |
+ |
inc.apply(5L)); |
4260 |
+ |
|
4261 |
+ |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
4262 |
+ |
Monad.assertFutureEquals( |
4263 |
+ |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
4264 |
+ |
unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple))); |
4265 |
+ |
|
4266 |
+ |
// The case for CompletableFuture as an additive monad is weaker... |
4267 |
+ |
|
4268 |
+ |
// zero is a monadic zero |
4269 |
+ |
Monad.assertZero(zero); |
4270 |
+ |
|
4271 |
+ |
// left zero: zero >>= f === zero |
4272 |
+ |
Monad.assertZero(zero.thenCompose(inc)); |
4273 |
+ |
// right zero: f >>= (\x -> zero) === zero |
4274 |
+ |
Monad.assertZero(inc.apply(5L).thenCompose(x -> zero)); |
4275 |
+ |
|
4276 |
+ |
// f plus zero === f |
4277 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4278 |
+ |
Monad.plus(Monad.unit(5L), zero)); |
4279 |
+ |
// zero plus f === f |
4280 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4281 |
+ |
Monad.plus(zero, Monad.unit(5L))); |
4282 |
+ |
// zero plus zero === zero |
4283 |
+ |
Monad.assertZero(Monad.plus(zero, zero)); |
4284 |
+ |
{ |
4285 |
+ |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
4286 |
+ |
Monad.unit(8L)); |
4287 |
+ |
// non-determinism |
4288 |
+ |
assertTrue(f.get() == 5L || f.get() == 8L); |
4289 |
+ |
} |
4290 |
+ |
|
4291 |
+ |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
4292 |
+ |
// f plus godot === f (doesn't wait for godot) |
4293 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4294 |
+ |
Monad.plus(Monad.unit(5L), godot)); |
4295 |
+ |
// godot plus f === f (doesn't wait for godot) |
4296 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4297 |
+ |
Monad.plus(godot, Monad.unit(5L))); |
4298 |
+ |
} |
4299 |
+ |
|
4300 |
+ |
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4301 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4302 |
+ |
public void testRecursiveChains() throws Throwable { |
4303 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
4304 |
+ |
for (boolean addDeadEnds : new boolean[] { true, false }) |
4305 |
+ |
{ |
4306 |
+ |
final int val = 42; |
4307 |
+ |
final int n = expensiveTests ? 1_000 : 2; |
4308 |
+ |
CompletableFuture<Integer> head = new CompletableFuture<>(); |
4309 |
+ |
CompletableFuture<Integer> tail = head; |
4310 |
+ |
for (int i = 0; i < n; i++) { |
4311 |
+ |
if (addDeadEnds) m.thenApply(tail, v -> v + 1); |
4312 |
+ |
tail = m.thenApply(tail, v -> v + 1); |
4313 |
+ |
if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1); |
4314 |
+ |
tail = m.applyToEither(tail, tail, v -> v + 1); |
4315 |
+ |
if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1); |
4316 |
+ |
tail = m.thenCombine(tail, tail, (v, w) -> v + 1); |
4317 |
+ |
} |
4318 |
+ |
head.complete(val); |
4319 |
+ |
assertEquals(val + 3 * n, (int) tail.join()); |
4320 |
+ |
}} |
4321 |
+ |
|
4322 |
+ |
/** |
4323 |
+ |
* A single CompletableFuture with many dependents. |
4324 |
+ |
* A demo of scalability - runtime is O(n). |
4325 |
+ |
*/ |
4326 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4327 |
+ |
public void testManyDependents() throws Throwable { |
4328 |
+ |
final int n = expensiveTests ? 1_000_000 : 10; |
4329 |
+ |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4330 |
+ |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
4331 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
4332 |
+ |
for (int i = 0; i < n; i++) { |
4333 |
+ |
head.thenRun(() -> count.getAndIncrement()); |
4334 |
+ |
head.thenAccept(x -> count.getAndIncrement()); |
4335 |
+ |
head.thenApply(x -> count.getAndIncrement()); |
4336 |
+ |
|
4337 |
+ |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
4338 |
+ |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
4339 |
+ |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
4340 |
+ |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
4341 |
+ |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
4342 |
+ |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
4343 |
+ |
|
4344 |
+ |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
4345 |
+ |
head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement()); |
4346 |
+ |
head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement()); |
4347 |
+ |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
4348 |
+ |
new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement()); |
4349 |
+ |
new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement()); |
4350 |
+ |
} |
4351 |
+ |
head.complete(null); |
4352 |
+ |
assertEquals(5 * 3 * n, count.get()); |
4353 |
+ |
} |
4354 |
+ |
|
4355 |
+ |
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4356 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4357 |
+ |
public void testCoCompletionGarbageRetention() throws Throwable { |
4358 |
+ |
final int n = expensiveTests ? 1_000_000 : 10; |
4359 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4360 |
+ |
CompletableFuture<Integer> f; |
4361 |
+ |
for (int i = 0; i < n; i++) { |
4362 |
+ |
f = new CompletableFuture<>(); |
4363 |
+ |
f.runAfterEither(incomplete, () -> {}); |
4364 |
+ |
f.complete(null); |
4365 |
+ |
|
4366 |
+ |
f = new CompletableFuture<>(); |
4367 |
+ |
f.acceptEither(incomplete, x -> {}); |
4368 |
+ |
f.complete(null); |
4369 |
+ |
|
4370 |
+ |
f = new CompletableFuture<>(); |
4371 |
+ |
f.applyToEither(incomplete, x -> x); |
4372 |
+ |
f.complete(null); |
4373 |
+ |
|
4374 |
+ |
f = new CompletableFuture<>(); |
4375 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4376 |
+ |
f.complete(null); |
4377 |
+ |
} |
4378 |
+ |
|
4379 |
+ |
for (int i = 0; i < n; i++) { |
4380 |
+ |
f = new CompletableFuture<>(); |
4381 |
+ |
incomplete.runAfterEither(f, () -> {}); |
4382 |
+ |
f.complete(null); |
4383 |
+ |
|
4384 |
+ |
f = new CompletableFuture<>(); |
4385 |
+ |
incomplete.acceptEither(f, x -> {}); |
4386 |
+ |
f.complete(null); |
4387 |
+ |
|
4388 |
+ |
f = new CompletableFuture<>(); |
4389 |
+ |
incomplete.applyToEither(f, x -> x); |
4390 |
+ |
f.complete(null); |
4391 |
+ |
|
4392 |
+ |
f = new CompletableFuture<>(); |
4393 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4394 |
+ |
f.complete(null); |
4395 |
+ |
} |
4396 |
+ |
} |
4397 |
+ |
|
4398 |
+ |
/** |
4399 |
+ |
* Reproduction recipe for: |
4400 |
+ |
* 8160402: Garbage retention with CompletableFuture.anyOf |
4401 |
+ |
* cvs update -D '2016-05-01' ./src/main/java/util/concurrent/CompletableFuture.java && ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck; cvs update -A |
4402 |
+ |
*/ |
4403 |
+ |
public void testAnyOfGarbageRetention() throws Throwable { |
4404 |
+ |
for (Integer v : new Integer[] { 1, null }) |
4405 |
+ |
{ |
4406 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4407 |
+ |
CompletableFuture<Integer>[] fs |
4408 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4409 |
+ |
for (int i = 0; i < fs.length; i++) |
4410 |
+ |
fs[i] = new CompletableFuture<>(); |
4411 |
+ |
fs[fs.length - 1].complete(v); |
4412 |
+ |
for (int i = 0; i < n; i++) |
4413 |
+ |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4414 |
+ |
}} |
4415 |
+ |
|
4416 |
+ |
/** |
4417 |
+ |
* Checks for garbage retention with allOf. |
4418 |
+ |
* |
4419 |
+ |
* As of 2016-07, fails with OOME: |
4420 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck |
4421 |
+ |
*/ |
4422 |
+ |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4423 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4424 |
+ |
CompletableFuture<Integer>[] fs |
4425 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4426 |
+ |
for (int i = 0; i < fs.length; i++) |
4427 |
+ |
fs[i] = new CompletableFuture<>(); |
4428 |
+ |
for (int i = 0; i < n; i++) |
4429 |
+ |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4430 |
+ |
} |
4431 |
+ |
|
4432 |
+ |
/** |
4433 |
+ |
* Checks for garbage retention when a dependent future is |
4434 |
+ |
* cancelled and garbage-collected. |
4435 |
+ |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4436 |
+ |
* |
4437 |
+ |
* As of 2016-07, fails with OOME: |
4438 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck |
4439 |
+ |
*/ |
4440 |
+ |
public void testCancelledGarbageRetention() throws Throwable { |
4441 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4442 |
+ |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4443 |
+ |
for (int i = 0; i < n; i++) |
4444 |
+ |
assertTrue(neverCompleted.thenRun(() -> {}).cancel(true)); |
4445 |
+ |
} |
4446 |
+ |
|
4447 |
+ |
/** |
4448 |
+ |
* Checks for garbage retention when MinimalStage.toCompletableFuture() |
4449 |
+ |
* is invoked many times. |
4450 |
+ |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4451 |
+ |
* |
4452 |
+ |
* As of 2016-07, fails with OOME: |
4453 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck |
4454 |
+ |
*/ |
4455 |
+ |
public void testToCompletableFutureGarbageRetention() throws Throwable { |
4456 |
+ |
final int n = expensiveTests ? 900_000 : 10; |
4457 |
+ |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4458 |
+ |
CompletionStage minimal = neverCompleted.minimalCompletionStage(); |
4459 |
+ |
for (int i = 0; i < n; i++) |
4460 |
+ |
assertTrue(minimal.toCompletableFuture().cancel(true)); |
4461 |
+ |
} |
4462 |
+ |
|
4463 |
+ |
// static <U> U join(CompletionStage<U> stage) { |
4464 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
4465 |
+ |
// stage.whenComplete((v, ex) -> { |
4466 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
4467 |
+ |
// }); |
4468 |
+ |
// return f.join(); |
4469 |
+ |
// } |
4470 |
+ |
|
4471 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
4472 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
4473 |
+ |
// stage.whenComplete((v, ex) -> { |
4474 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
4475 |
+ |
// }); |
4476 |
+ |
// return f.isDone(); |
4477 |
+ |
// } |
4478 |
+ |
|
4479 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
4480 |
+ |
// return stage.toCompletableFuture().copy().join(); |
4481 |
+ |
// } |
4482 |
+ |
|
4483 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
4484 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
4485 |
+ |
// } |
4486 |
+ |
|
4487 |
|
} |