5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
< |
import junit.framework.*; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
> |
|
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.List; |
13 |
> |
import java.util.Objects; |
14 |
|
import java.util.concurrent.Callable; |
10 |
– |
import java.util.concurrent.Executor; |
11 |
– |
import java.util.concurrent.ExecutorService; |
12 |
– |
import java.util.concurrent.Executors; |
15 |
|
import java.util.concurrent.CancellationException; |
14 |
– |
import java.util.concurrent.CountDownLatch; |
15 |
– |
import java.util.concurrent.ExecutionException; |
16 |
– |
import java.util.concurrent.Future; |
16 |
|
import java.util.concurrent.CompletableFuture; |
17 |
|
import java.util.concurrent.CompletionException; |
18 |
|
import java.util.concurrent.CompletionStage; |
19 |
+ |
import java.util.concurrent.ExecutionException; |
20 |
+ |
import java.util.concurrent.Executor; |
21 |
+ |
import java.util.concurrent.ForkJoinPool; |
22 |
+ |
import java.util.concurrent.ForkJoinTask; |
23 |
|
import java.util.concurrent.TimeoutException; |
24 |
+ |
import java.util.concurrent.TimeUnit; |
25 |
|
import java.util.concurrent.atomic.AtomicInteger; |
26 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
23 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
24 |
< |
import java.util.*; |
25 |
< |
import java.util.function.Supplier; |
26 |
< |
import java.util.function.Consumer; |
26 |
> |
import java.util.concurrent.atomic.AtomicReference; |
27 |
|
import java.util.function.BiConsumer; |
28 |
– |
import java.util.function.Function; |
28 |
|
import java.util.function.BiFunction; |
29 |
+ |
import java.util.function.Consumer; |
30 |
+ |
import java.util.function.Function; |
31 |
+ |
import java.util.function.Supplier; |
32 |
+ |
|
33 |
+ |
import junit.framework.Test; |
34 |
+ |
import junit.framework.TestSuite; |
35 |
|
|
36 |
|
public class CompletableFutureTest extends JSR166TestCase { |
37 |
|
|
38 |
|
public static void main(String[] args) { |
39 |
< |
junit.textui.TestRunner.run(suite()); |
39 |
> |
main(suite(), args); |
40 |
|
} |
41 |
|
public static Test suite() { |
42 |
|
return new TestSuite(CompletableFutureTest.class); |
47 |
|
void checkIncomplete(CompletableFuture<?> f) { |
48 |
|
assertFalse(f.isDone()); |
49 |
|
assertFalse(f.isCancelled()); |
50 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
50 |
> |
assertTrue(f.toString().contains("Not completed")); |
51 |
|
try { |
52 |
|
assertNull(f.getNow(null)); |
53 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
60 |
|
} |
61 |
|
|
62 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
63 |
< |
try { |
64 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
60 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
63 |
> |
checkTimedGet(f, value); |
64 |
> |
|
65 |
|
try { |
66 |
|
assertEquals(value, f.join()); |
67 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
78 |
|
} |
79 |
|
|
80 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
81 |
+ |
long startTime = System.nanoTime(); |
82 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
83 |
|
try { |
84 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
84 |
> |
f.get(timeoutMillis, MILLISECONDS); |
85 |
|
shouldThrow(); |
86 |
|
} catch (ExecutionException success) { |
87 |
|
assertTrue(success.getCause() instanceof CFException); |
88 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
89 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
90 |
+ |
|
91 |
|
try { |
92 |
|
f.join(); |
93 |
|
shouldThrow(); |
111 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
112 |
|
} |
113 |
|
|
114 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f, |
115 |
< |
CFException ex) { |
114 |
> |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
115 |
> |
Throwable ex) { |
116 |
> |
long startTime = System.nanoTime(); |
117 |
> |
long timeoutMillis = LONG_DELAY_MS; |
118 |
|
try { |
119 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
119 |
> |
f.get(timeoutMillis, MILLISECONDS); |
120 |
|
shouldThrow(); |
121 |
|
} catch (ExecutionException success) { |
122 |
|
assertSame(ex, success.getCause()); |
123 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
124 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
125 |
+ |
|
126 |
|
try { |
127 |
|
f.join(); |
128 |
|
shouldThrow(); |
141 |
|
} catch (ExecutionException success) { |
142 |
|
assertSame(ex, success.getCause()); |
143 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
144 |
+ |
|
145 |
+ |
assertTrue(f.isDone()); |
146 |
+ |
assertFalse(f.isCancelled()); |
147 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
148 |
+ |
} |
149 |
+ |
|
150 |
+ |
<U> void checkCompletedExceptionallyWithTimeout(CompletableFuture<U> f) { |
151 |
+ |
long startTime = System.nanoTime(); |
152 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
153 |
+ |
try { |
154 |
+ |
f.get(timeoutMillis, MILLISECONDS); |
155 |
+ |
shouldThrow(); |
156 |
+ |
} catch (ExecutionException ex) { |
157 |
+ |
assertTrue(ex.getCause() instanceof TimeoutException); |
158 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
159 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
160 |
+ |
|
161 |
+ |
try { |
162 |
+ |
f.join(); |
163 |
+ |
shouldThrow(); |
164 |
+ |
} catch (Throwable ex) { |
165 |
+ |
assertTrue(ex.getCause() instanceof TimeoutException); |
166 |
+ |
} |
167 |
+ |
|
168 |
+ |
try { |
169 |
+ |
f.getNow(null); |
170 |
+ |
shouldThrow(); |
171 |
+ |
} catch (Throwable ex) { |
172 |
+ |
assertTrue(ex.getCause() instanceof TimeoutException); |
173 |
+ |
} |
174 |
+ |
|
175 |
+ |
try { |
176 |
+ |
f.get(); |
177 |
+ |
shouldThrow(); |
178 |
+ |
} catch (ExecutionException ex) { |
179 |
+ |
assertTrue(ex.getCause() instanceof TimeoutException); |
180 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
181 |
+ |
|
182 |
|
assertTrue(f.isDone()); |
183 |
|
assertFalse(f.isCancelled()); |
184 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
185 |
|
} |
186 |
|
|
187 |
+ |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
188 |
+ |
Throwable ex) { |
189 |
+ |
checkCompletedExceptionallyWithRootCause(f, ex); |
190 |
+ |
try { |
191 |
+ |
CompletableFuture<Throwable> spy = f.handle |
192 |
+ |
((U u, Throwable t) -> t); |
193 |
+ |
assertTrue(spy.join() instanceof CompletionException); |
194 |
+ |
assertSame(ex, spy.join().getCause()); |
195 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
196 |
+ |
} |
197 |
+ |
|
198 |
+ |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
199 |
+ |
checkCompletedExceptionallyWithRootCause(f, ex); |
200 |
+ |
try { |
201 |
+ |
CompletableFuture<Throwable> spy = f.handle |
202 |
+ |
((U u, Throwable t) -> t); |
203 |
+ |
assertSame(ex, spy.join()); |
204 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
205 |
+ |
} |
206 |
+ |
|
207 |
|
void checkCancelled(CompletableFuture<?> f) { |
208 |
+ |
long startTime = System.nanoTime(); |
209 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
210 |
|
try { |
211 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
211 |
> |
f.get(timeoutMillis, MILLISECONDS); |
212 |
|
shouldThrow(); |
213 |
|
} catch (CancellationException success) { |
214 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
215 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
216 |
+ |
|
217 |
|
try { |
218 |
|
f.join(); |
219 |
|
shouldThrow(); |
234 |
|
} |
235 |
|
|
236 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
237 |
+ |
long startTime = System.nanoTime(); |
238 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
239 |
|
try { |
240 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
240 |
> |
f.get(timeoutMillis, MILLISECONDS); |
241 |
|
shouldThrow(); |
242 |
|
} catch (ExecutionException success) { |
243 |
|
assertTrue(success.getCause() instanceof CancellationException); |
244 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
245 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
246 |
+ |
|
247 |
|
try { |
248 |
|
f.join(); |
249 |
|
shouldThrow(); |
282 |
|
* isCancelled, join, get, and getNow |
283 |
|
*/ |
284 |
|
public void testComplete() { |
285 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
286 |
+ |
{ |
287 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
288 |
|
checkIncomplete(f); |
289 |
< |
f.complete(one); |
290 |
< |
checkCompletedNormally(f, one); |
291 |
< |
} |
289 |
> |
assertTrue(f.complete(v1)); |
290 |
> |
assertFalse(f.complete(v1)); |
291 |
> |
checkCompletedNormally(f, v1); |
292 |
> |
}} |
293 |
|
|
294 |
|
/** |
295 |
|
* completeExceptionally completes exceptionally, as indicated by |
297 |
|
*/ |
298 |
|
public void testCompleteExceptionally() { |
299 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
300 |
+ |
CFException ex = new CFException(); |
301 |
|
checkIncomplete(f); |
302 |
< |
f.completeExceptionally(new CFException()); |
303 |
< |
checkCompletedWithWrappedCFException(f); |
302 |
> |
f.completeExceptionally(ex); |
303 |
> |
checkCompletedExceptionally(f, ex); |
304 |
|
} |
305 |
|
|
306 |
|
/** |
308 |
|
* methods isDone, isCancelled, join, get, and getNow |
309 |
|
*/ |
310 |
|
public void testCancel() { |
311 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
312 |
+ |
{ |
313 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
314 |
|
checkIncomplete(f); |
315 |
< |
assertTrue(f.cancel(true)); |
315 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
316 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
317 |
> |
assertTrue(f.cancel(!mayInterruptIfRunning)); |
318 |
|
checkCancelled(f); |
319 |
< |
} |
319 |
> |
}} |
320 |
|
|
321 |
|
/** |
322 |
|
* obtrudeValue forces completion with given value |
324 |
|
public void testObtrudeValue() { |
325 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
326 |
|
checkIncomplete(f); |
327 |
< |
f.complete(one); |
327 |
> |
assertTrue(f.complete(one)); |
328 |
|
checkCompletedNormally(f, one); |
329 |
|
f.obtrudeValue(three); |
330 |
|
checkCompletedNormally(f, three); |
333 |
|
f = new CompletableFuture<>(); |
334 |
|
f.obtrudeValue(three); |
335 |
|
checkCompletedNormally(f, three); |
336 |
+ |
f.obtrudeValue(null); |
337 |
+ |
checkCompletedNormally(f, null); |
338 |
|
f = new CompletableFuture<>(); |
339 |
|
f.completeExceptionally(new CFException()); |
340 |
|
f.obtrudeValue(four); |
345 |
|
* obtrudeException forces completion with given exception |
346 |
|
*/ |
347 |
|
public void testObtrudeException() { |
348 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
349 |
< |
checkIncomplete(f); |
350 |
< |
f.complete(one); |
351 |
< |
checkCompletedNormally(f, one); |
352 |
< |
f.obtrudeException(new CFException()); |
265 |
< |
checkCompletedWithWrappedCFException(f); |
348 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
349 |
> |
{ |
350 |
> |
CFException ex; |
351 |
> |
CompletableFuture<Integer> f; |
352 |
> |
|
353 |
|
f = new CompletableFuture<>(); |
354 |
< |
f.obtrudeException(new CFException()); |
355 |
< |
checkCompletedWithWrappedCFException(f); |
354 |
> |
assertTrue(f.complete(v1)); |
355 |
> |
for (int i = 0; i < 2; i++) { |
356 |
> |
f.obtrudeException(ex = new CFException()); |
357 |
> |
checkCompletedExceptionally(f, ex); |
358 |
> |
} |
359 |
> |
|
360 |
> |
f = new CompletableFuture<>(); |
361 |
> |
for (int i = 0; i < 2; i++) { |
362 |
> |
f.obtrudeException(ex = new CFException()); |
363 |
> |
checkCompletedExceptionally(f, ex); |
364 |
> |
} |
365 |
> |
|
366 |
|
f = new CompletableFuture<>(); |
367 |
+ |
f.completeExceptionally(ex = new CFException()); |
368 |
+ |
f.obtrudeValue(v1); |
369 |
+ |
checkCompletedNormally(f, v1); |
370 |
+ |
f.obtrudeException(ex = new CFException()); |
371 |
+ |
checkCompletedExceptionally(f, ex); |
372 |
|
f.completeExceptionally(new CFException()); |
373 |
< |
f.obtrudeValue(four); |
374 |
< |
checkCompletedNormally(f, four); |
375 |
< |
f.obtrudeException(new CFException()); |
376 |
< |
checkCompletedWithWrappedCFException(f); |
275 |
< |
} |
373 |
> |
checkCompletedExceptionally(f, ex); |
374 |
> |
assertFalse(f.complete(v1)); |
375 |
> |
checkCompletedExceptionally(f, ex); |
376 |
> |
}} |
377 |
|
|
378 |
|
/** |
379 |
|
* getNumberOfDependents returns number of dependent tasks |
380 |
|
*/ |
381 |
|
public void testGetNumberOfDependents() { |
382 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
383 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
384 |
+ |
{ |
385 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
386 |
< |
assertEquals(f.getNumberOfDependents(), 0); |
387 |
< |
CompletableFuture g = f.thenRun(new Noop()); |
388 |
< |
assertEquals(f.getNumberOfDependents(), 1); |
389 |
< |
assertEquals(g.getNumberOfDependents(), 0); |
390 |
< |
CompletableFuture h = f.thenRun(new Noop()); |
391 |
< |
assertEquals(f.getNumberOfDependents(), 2); |
392 |
< |
f.complete(1); |
386 |
> |
assertEquals(0, f.getNumberOfDependents()); |
387 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
388 |
> |
assertEquals(1, f.getNumberOfDependents()); |
389 |
> |
assertEquals(0, g.getNumberOfDependents()); |
390 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
391 |
> |
assertEquals(2, f.getNumberOfDependents()); |
392 |
> |
assertEquals(0, h.getNumberOfDependents()); |
393 |
> |
assertTrue(f.complete(v1)); |
394 |
|
checkCompletedNormally(g, null); |
395 |
< |
assertEquals(f.getNumberOfDependents(), 0); |
396 |
< |
assertEquals(g.getNumberOfDependents(), 0); |
397 |
< |
} |
395 |
> |
checkCompletedNormally(h, null); |
396 |
> |
assertEquals(0, f.getNumberOfDependents()); |
397 |
> |
assertEquals(0, g.getNumberOfDependents()); |
398 |
> |
assertEquals(0, h.getNumberOfDependents()); |
399 |
> |
}} |
400 |
|
|
401 |
|
/** |
402 |
|
* toString indicates current completion state |
407 |
|
f = new CompletableFuture<String>(); |
408 |
|
assertTrue(f.toString().contains("[Not completed]")); |
409 |
|
|
410 |
< |
f.complete("foo"); |
410 |
> |
assertTrue(f.complete("foo")); |
411 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
412 |
|
|
413 |
|
f = new CompletableFuture<String>(); |
414 |
< |
f.completeExceptionally(new IndexOutOfBoundsException()); |
414 |
> |
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
415 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
416 |
+ |
|
417 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
418 |
+ |
f = new CompletableFuture<String>(); |
419 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
420 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
421 |
+ |
} |
422 |
|
} |
423 |
|
|
424 |
|
/** |
429 |
|
checkCompletedNormally(f, "test"); |
430 |
|
} |
431 |
|
|
432 |
< |
// Choose non-commutative actions for better coverage |
432 |
> |
abstract class CheckedAction { |
433 |
> |
int invocationCount = 0; |
434 |
> |
final ExecutionMode m; |
435 |
> |
CheckedAction(ExecutionMode m) { this.m = m; } |
436 |
> |
void invoked() { |
437 |
> |
m.checkExecutionMode(); |
438 |
> |
assertEquals(0, invocationCount++); |
439 |
> |
} |
440 |
> |
void assertNotInvoked() { assertEquals(0, invocationCount); } |
441 |
> |
void assertInvoked() { assertEquals(1, invocationCount); } |
442 |
> |
} |
443 |
> |
|
444 |
> |
abstract class CheckedIntegerAction extends CheckedAction { |
445 |
> |
Integer value; |
446 |
> |
CheckedIntegerAction(ExecutionMode m) { super(m); } |
447 |
> |
void assertValue(Integer expected) { |
448 |
> |
assertInvoked(); |
449 |
> |
assertEquals(expected, value); |
450 |
> |
} |
451 |
> |
} |
452 |
> |
|
453 |
> |
class IntegerSupplier extends CheckedAction |
454 |
> |
implements Supplier<Integer> |
455 |
> |
{ |
456 |
> |
final Integer value; |
457 |
> |
IntegerSupplier(ExecutionMode m, Integer value) { |
458 |
> |
super(m); |
459 |
> |
this.value = value; |
460 |
> |
} |
461 |
> |
public Integer get() { |
462 |
> |
invoked(); |
463 |
> |
return value; |
464 |
> |
} |
465 |
> |
} |
466 |
> |
|
467 |
> |
// A function that handles and produces null values as well. |
468 |
> |
static Integer inc(Integer x) { |
469 |
> |
return (x == null) ? null : x + 1; |
470 |
> |
} |
471 |
|
|
472 |
< |
// A non-commutative function that handles null values as well, |
473 |
< |
// and produces null values occasionally. |
474 |
< |
public static Integer subtract(Integer x, Integer y) { |
472 |
> |
class NoopConsumer extends CheckedIntegerAction |
473 |
> |
implements Consumer<Integer> |
474 |
> |
{ |
475 |
> |
NoopConsumer(ExecutionMode m) { super(m); } |
476 |
> |
public void accept(Integer x) { |
477 |
> |
invoked(); |
478 |
> |
value = x; |
479 |
> |
} |
480 |
> |
} |
481 |
> |
|
482 |
> |
class IncFunction extends CheckedIntegerAction |
483 |
> |
implements Function<Integer,Integer> |
484 |
> |
{ |
485 |
> |
IncFunction(ExecutionMode m) { super(m); } |
486 |
> |
public Integer apply(Integer x) { |
487 |
> |
invoked(); |
488 |
> |
return value = inc(x); |
489 |
> |
} |
490 |
> |
} |
491 |
> |
|
492 |
> |
// Choose non-commutative actions for better coverage |
493 |
> |
// A non-commutative function that handles and produces null values as well. |
494 |
> |
static Integer subtract(Integer x, Integer y) { |
495 |
|
return (x == null && y == null) ? null : |
496 |
|
((x == null) ? 42 : x.intValue()) |
497 |
|
- ((y == null) ? 99 : y.intValue()); |
498 |
|
} |
499 |
|
|
500 |
< |
static final Supplier<Integer> supplyOne = |
501 |
< |
() -> Integer.valueOf(1); |
502 |
< |
static final Function<Integer, Integer> inc = |
503 |
< |
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
333 |
< |
static final BiFunction<Integer, Integer, Integer> subtract = |
334 |
< |
(Integer x, Integer y) -> subtract(x, y); |
335 |
< |
static final class IncAction implements Consumer<Integer> { |
336 |
< |
int value; |
337 |
< |
public void accept(Integer x) { value = x.intValue() + 1; } |
338 |
< |
} |
339 |
< |
static final class SubtractAction implements BiConsumer<Integer, Integer> { |
340 |
< |
int invocationCount = 0; |
341 |
< |
Integer value; |
342 |
< |
// Check this action was invoked exactly once when result is computed. |
343 |
< |
public boolean ran() { return invocationCount == 1; } |
500 |
> |
class SubtractAction extends CheckedIntegerAction |
501 |
> |
implements BiConsumer<Integer, Integer> |
502 |
> |
{ |
503 |
> |
SubtractAction(ExecutionMode m) { super(m); } |
504 |
|
public void accept(Integer x, Integer y) { |
505 |
< |
invocationCount++; |
505 |
> |
invoked(); |
506 |
|
value = subtract(x, y); |
507 |
|
} |
508 |
|
} |
509 |
< |
static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> { |
510 |
< |
int invocationCount = 0; |
511 |
< |
Integer value; |
512 |
< |
// Check this action was invoked exactly once when result is computed. |
513 |
< |
public boolean ran() { return invocationCount == 1; } |
509 |
> |
|
510 |
> |
class SubtractFunction extends CheckedIntegerAction |
511 |
> |
implements BiFunction<Integer, Integer, Integer> |
512 |
> |
{ |
513 |
> |
SubtractFunction(ExecutionMode m) { super(m); } |
514 |
|
public Integer apply(Integer x, Integer y) { |
515 |
< |
invocationCount++; |
515 |
> |
invoked(); |
516 |
|
return value = subtract(x, y); |
517 |
|
} |
518 |
|
} |
519 |
< |
static final class Noop implements Runnable { |
520 |
< |
boolean ran; |
521 |
< |
public void run() { ran = true; } |
519 |
> |
|
520 |
> |
class Noop extends CheckedAction implements Runnable { |
521 |
> |
Noop(ExecutionMode m) { super(m); } |
522 |
> |
public void run() { |
523 |
> |
invoked(); |
524 |
> |
} |
525 |
|
} |
526 |
|
|
527 |
< |
static final class FailingSupplier implements Supplier<Integer> { |
528 |
< |
boolean ran; |
529 |
< |
public Integer get() { ran = true; throw new CFException(); } |
527 |
> |
class FailingSupplier extends CheckedAction |
528 |
> |
implements Supplier<Integer> |
529 |
> |
{ |
530 |
> |
FailingSupplier(ExecutionMode m) { super(m); } |
531 |
> |
public Integer get() { |
532 |
> |
invoked(); |
533 |
> |
throw new CFException(); |
534 |
> |
} |
535 |
|
} |
536 |
< |
static final class FailingConsumer implements Consumer<Integer> { |
537 |
< |
boolean ran; |
538 |
< |
public void accept(Integer x) { ran = true; throw new CFException(); } |
536 |
> |
|
537 |
> |
class FailingConsumer extends CheckedIntegerAction |
538 |
> |
implements Consumer<Integer> |
539 |
> |
{ |
540 |
> |
FailingConsumer(ExecutionMode m) { super(m); } |
541 |
> |
public void accept(Integer x) { |
542 |
> |
invoked(); |
543 |
> |
value = x; |
544 |
> |
throw new CFException(); |
545 |
> |
} |
546 |
|
} |
547 |
< |
static final class FailingBiConsumer implements BiConsumer<Integer, Integer> { |
548 |
< |
boolean ran; |
549 |
< |
public void accept(Integer x, Integer y) { ran = true; throw new CFException(); } |
547 |
> |
|
548 |
> |
class FailingBiConsumer extends CheckedIntegerAction |
549 |
> |
implements BiConsumer<Integer, Integer> |
550 |
> |
{ |
551 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); } |
552 |
> |
public void accept(Integer x, Integer y) { |
553 |
> |
invoked(); |
554 |
> |
value = subtract(x, y); |
555 |
> |
throw new CFException(); |
556 |
> |
} |
557 |
|
} |
558 |
< |
static final class FailingFunction implements Function<Integer, Integer> { |
559 |
< |
boolean ran; |
560 |
< |
public Integer apply(Integer x) { ran = true; throw new CFException(); } |
558 |
> |
|
559 |
> |
class FailingFunction extends CheckedIntegerAction |
560 |
> |
implements Function<Integer, Integer> |
561 |
> |
{ |
562 |
> |
FailingFunction(ExecutionMode m) { super(m); } |
563 |
> |
public Integer apply(Integer x) { |
564 |
> |
invoked(); |
565 |
> |
value = x; |
566 |
> |
throw new CFException(); |
567 |
> |
} |
568 |
|
} |
569 |
< |
static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> { |
570 |
< |
boolean ran; |
571 |
< |
public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); } |
569 |
> |
|
570 |
> |
class FailingBiFunction extends CheckedIntegerAction |
571 |
> |
implements BiFunction<Integer, Integer, Integer> |
572 |
> |
{ |
573 |
> |
FailingBiFunction(ExecutionMode m) { super(m); } |
574 |
> |
public Integer apply(Integer x, Integer y) { |
575 |
> |
invoked(); |
576 |
> |
value = subtract(x, y); |
577 |
> |
throw new CFException(); |
578 |
> |
} |
579 |
|
} |
580 |
< |
static final class FailingNoop implements Runnable { |
581 |
< |
boolean ran; |
582 |
< |
public void run() { ran = true; throw new CFException(); } |
580 |
> |
|
581 |
> |
class FailingRunnable extends CheckedAction implements Runnable { |
582 |
> |
FailingRunnable(ExecutionMode m) { super(m); } |
583 |
> |
public void run() { |
584 |
> |
invoked(); |
585 |
> |
throw new CFException(); |
586 |
> |
} |
587 |
|
} |
588 |
|
|
589 |
< |
static final class CompletableFutureInc |
590 |
< |
implements Function<Integer, CompletableFuture<Integer>> { |
591 |
< |
boolean ran; |
589 |
> |
class CompletableFutureInc extends CheckedIntegerAction |
590 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
591 |
> |
{ |
592 |
> |
CompletableFutureInc(ExecutionMode m) { super(m); } |
593 |
|
public CompletableFuture<Integer> apply(Integer x) { |
594 |
< |
ran = true; |
594 |
> |
invoked(); |
595 |
> |
value = x; |
596 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
597 |
< |
f.complete(Integer.valueOf(x.intValue() + 1)); |
597 |
> |
assertTrue(f.complete(inc(x))); |
598 |
|
return f; |
599 |
|
} |
600 |
|
} |
601 |
|
|
602 |
< |
static final class FailingCompletableFutureFunction |
603 |
< |
implements Function<Integer, CompletableFuture<Integer>> { |
604 |
< |
boolean ran; |
602 |
> |
class FailingCompletableFutureFunction extends CheckedIntegerAction |
603 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
604 |
> |
{ |
605 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); } |
606 |
|
public CompletableFuture<Integer> apply(Integer x) { |
607 |
< |
ran = true; throw new CFException(); |
607 |
> |
invoked(); |
608 |
> |
value = x; |
609 |
> |
throw new CFException(); |
610 |
|
} |
611 |
|
} |
612 |
|
|
613 |
|
// Used for explicit executor tests |
614 |
|
static final class ThreadExecutor implements Executor { |
615 |
< |
AtomicInteger count = new AtomicInteger(0); |
615 |
> |
final AtomicInteger count = new AtomicInteger(0); |
616 |
> |
static final ThreadGroup tg = new ThreadGroup("ThreadExecutor"); |
617 |
> |
static boolean startedCurrentThread() { |
618 |
> |
return Thread.currentThread().getThreadGroup() == tg; |
619 |
> |
} |
620 |
|
|
621 |
|
public void execute(Runnable r) { |
622 |
|
count.getAndIncrement(); |
623 |
< |
new Thread(r).start(); |
623 |
> |
new Thread(tg, r).start(); |
624 |
|
} |
625 |
|
} |
626 |
|
|
627 |
< |
static final class ExceptionToInteger implements Function<Throwable, Integer> { |
628 |
< |
public Integer apply(Throwable x) { return Integer.valueOf(3); } |
420 |
< |
} |
421 |
< |
|
422 |
< |
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
423 |
< |
boolean ran; |
424 |
< |
public Integer apply(Integer x, Throwable t) { |
425 |
< |
ran = true; |
426 |
< |
return (t == null) ? two : three; |
427 |
< |
} |
428 |
< |
} |
627 |
> |
static final boolean defaultExecutorIsCommonPool |
628 |
> |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
629 |
|
|
630 |
|
/** |
631 |
|
* Permits the testing of parallel code for the 3 different |
632 |
< |
* execution modes without repeating all the testing code. |
632 |
> |
* execution modes without copy/pasting all the test methods. |
633 |
|
*/ |
634 |
|
enum ExecutionMode { |
635 |
< |
DEFAULT { |
635 |
> |
SYNC { |
636 |
> |
public void checkExecutionMode() { |
637 |
> |
assertFalse(ThreadExecutor.startedCurrentThread()); |
638 |
> |
assertNull(ForkJoinTask.getPool()); |
639 |
> |
} |
640 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
641 |
> |
throw new UnsupportedOperationException(); |
642 |
> |
} |
643 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
644 |
> |
throw new UnsupportedOperationException(); |
645 |
> |
} |
646 |
> |
public <T> CompletableFuture<Void> thenRun |
647 |
> |
(CompletableFuture<T> f, Runnable a) { |
648 |
> |
return f.thenRun(a); |
649 |
> |
} |
650 |
> |
public <T> CompletableFuture<Void> thenAccept |
651 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
652 |
> |
return f.thenAccept(a); |
653 |
> |
} |
654 |
> |
public <T,U> CompletableFuture<U> thenApply |
655 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
656 |
> |
return f.thenApply(a); |
657 |
> |
} |
658 |
> |
public <T,U> CompletableFuture<U> thenCompose |
659 |
> |
(CompletableFuture<T> f, |
660 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
661 |
> |
return f.thenCompose(a); |
662 |
> |
} |
663 |
> |
public <T,U> CompletableFuture<U> handle |
664 |
> |
(CompletableFuture<T> f, |
665 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
666 |
> |
return f.handle(a); |
667 |
> |
} |
668 |
> |
public <T> CompletableFuture<T> whenComplete |
669 |
> |
(CompletableFuture<T> f, |
670 |
> |
BiConsumer<? super T,? super Throwable> a) { |
671 |
> |
return f.whenComplete(a); |
672 |
> |
} |
673 |
|
public <T,U> CompletableFuture<Void> runAfterBoth |
674 |
|
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
675 |
|
return f.runAfterBoth(g, a); |
686 |
|
BiFunction<? super T,? super U,? extends V> a) { |
687 |
|
return f.thenCombine(g, a); |
688 |
|
} |
689 |
+ |
public <T> CompletableFuture<Void> runAfterEither |
690 |
+ |
(CompletableFuture<T> f, |
691 |
+ |
CompletionStage<?> g, |
692 |
+ |
java.lang.Runnable a) { |
693 |
+ |
return f.runAfterEither(g, a); |
694 |
+ |
} |
695 |
+ |
public <T> CompletableFuture<Void> acceptEither |
696 |
+ |
(CompletableFuture<T> f, |
697 |
+ |
CompletionStage<? extends T> g, |
698 |
+ |
Consumer<? super T> a) { |
699 |
+ |
return f.acceptEither(g, a); |
700 |
+ |
} |
701 |
+ |
public <T,U> CompletableFuture<U> applyToEither |
702 |
+ |
(CompletableFuture<T> f, |
703 |
+ |
CompletionStage<? extends T> g, |
704 |
+ |
Function<? super T,U> a) { |
705 |
+ |
return f.applyToEither(g, a); |
706 |
+ |
} |
707 |
|
}, |
708 |
|
|
709 |
< |
// /** Experimental way to do more testing */ |
710 |
< |
// REVERSE_DEFAULT { |
711 |
< |
// public <T,U> CompletableFuture<Void> runAfterBoth |
712 |
< |
// (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
713 |
< |
// return g.runAfterBoth(f, a); |
714 |
< |
// } |
715 |
< |
// public <T,U> CompletableFuture<Void> thenAcceptBoth |
716 |
< |
// (CompletableFuture<T> f, |
717 |
< |
// CompletionStage<? extends U> g, |
718 |
< |
// BiConsumer<? super T,? super U> a) { |
719 |
< |
// return DEFAULT.thenAcceptBoth(f, g, a); |
720 |
< |
// } |
721 |
< |
// }, |
722 |
< |
|
723 |
< |
DEFAULT_ASYNC { |
709 |
> |
ASYNC { |
710 |
> |
public void checkExecutionMode() { |
711 |
> |
assertEquals(defaultExecutorIsCommonPool, |
712 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
713 |
> |
} |
714 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
715 |
> |
return CompletableFuture.runAsync(a); |
716 |
> |
} |
717 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
718 |
> |
return CompletableFuture.supplyAsync(a); |
719 |
> |
} |
720 |
> |
public <T> CompletableFuture<Void> thenRun |
721 |
> |
(CompletableFuture<T> f, Runnable a) { |
722 |
> |
return f.thenRunAsync(a); |
723 |
> |
} |
724 |
> |
public <T> CompletableFuture<Void> thenAccept |
725 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
726 |
> |
return f.thenAcceptAsync(a); |
727 |
> |
} |
728 |
> |
public <T,U> CompletableFuture<U> thenApply |
729 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
730 |
> |
return f.thenApplyAsync(a); |
731 |
> |
} |
732 |
> |
public <T,U> CompletableFuture<U> thenCompose |
733 |
> |
(CompletableFuture<T> f, |
734 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
735 |
> |
return f.thenComposeAsync(a); |
736 |
> |
} |
737 |
> |
public <T,U> CompletableFuture<U> handle |
738 |
> |
(CompletableFuture<T> f, |
739 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
740 |
> |
return f.handleAsync(a); |
741 |
> |
} |
742 |
> |
public <T> CompletableFuture<T> whenComplete |
743 |
> |
(CompletableFuture<T> f, |
744 |
> |
BiConsumer<? super T,? super Throwable> a) { |
745 |
> |
return f.whenCompleteAsync(a); |
746 |
> |
} |
747 |
|
public <T,U> CompletableFuture<Void> runAfterBoth |
748 |
|
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
749 |
|
return f.runAfterBothAsync(g, a); |
760 |
|
BiFunction<? super T,? super U,? extends V> a) { |
761 |
|
return f.thenCombineAsync(g, a); |
762 |
|
} |
763 |
+ |
public <T> CompletableFuture<Void> runAfterEither |
764 |
+ |
(CompletableFuture<T> f, |
765 |
+ |
CompletionStage<?> g, |
766 |
+ |
java.lang.Runnable a) { |
767 |
+ |
return f.runAfterEitherAsync(g, a); |
768 |
+ |
} |
769 |
+ |
public <T> CompletableFuture<Void> acceptEither |
770 |
+ |
(CompletableFuture<T> f, |
771 |
+ |
CompletionStage<? extends T> g, |
772 |
+ |
Consumer<? super T> a) { |
773 |
+ |
return f.acceptEitherAsync(g, a); |
774 |
+ |
} |
775 |
+ |
public <T,U> CompletableFuture<U> applyToEither |
776 |
+ |
(CompletableFuture<T> f, |
777 |
+ |
CompletionStage<? extends T> g, |
778 |
+ |
Function<? super T,U> a) { |
779 |
+ |
return f.applyToEitherAsync(g, a); |
780 |
+ |
} |
781 |
|
}, |
782 |
|
|
487 |
– |
// REVERSE_DEFAULT_ASYNC { |
488 |
– |
// public <T,U> CompletableFuture<Void> runAfterBoth |
489 |
– |
// (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
490 |
– |
// return f.runAfterBothAsync(g, a); |
491 |
– |
// } |
492 |
– |
// public <T,U> CompletableFuture<Void> thenAcceptBoth |
493 |
– |
// (CompletableFuture<T> f, |
494 |
– |
// CompletionStage<? extends U> g, |
495 |
– |
// BiConsumer<? super T,? super U> a) { |
496 |
– |
// return DEFAULT_ASYNC.thenAcceptBoth(f, g, a); |
497 |
– |
// } |
498 |
– |
// }, |
499 |
– |
|
783 |
|
EXECUTOR { |
784 |
+ |
public void checkExecutionMode() { |
785 |
+ |
assertTrue(ThreadExecutor.startedCurrentThread()); |
786 |
+ |
} |
787 |
+ |
public CompletableFuture<Void> runAsync(Runnable a) { |
788 |
+ |
return CompletableFuture.runAsync(a, new ThreadExecutor()); |
789 |
+ |
} |
790 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
791 |
+ |
return CompletableFuture.supplyAsync(a, new ThreadExecutor()); |
792 |
+ |
} |
793 |
+ |
public <T> CompletableFuture<Void> thenRun |
794 |
+ |
(CompletableFuture<T> f, Runnable a) { |
795 |
+ |
return f.thenRunAsync(a, new ThreadExecutor()); |
796 |
+ |
} |
797 |
+ |
public <T> CompletableFuture<Void> thenAccept |
798 |
+ |
(CompletableFuture<T> f, Consumer<? super T> a) { |
799 |
+ |
return f.thenAcceptAsync(a, new ThreadExecutor()); |
800 |
+ |
} |
801 |
+ |
public <T,U> CompletableFuture<U> thenApply |
802 |
+ |
(CompletableFuture<T> f, Function<? super T,U> a) { |
803 |
+ |
return f.thenApplyAsync(a, new ThreadExecutor()); |
804 |
+ |
} |
805 |
+ |
public <T,U> CompletableFuture<U> thenCompose |
806 |
+ |
(CompletableFuture<T> f, |
807 |
+ |
Function<? super T,? extends CompletionStage<U>> a) { |
808 |
+ |
return f.thenComposeAsync(a, new ThreadExecutor()); |
809 |
+ |
} |
810 |
+ |
public <T,U> CompletableFuture<U> handle |
811 |
+ |
(CompletableFuture<T> f, |
812 |
+ |
BiFunction<? super T,Throwable,? extends U> a) { |
813 |
+ |
return f.handleAsync(a, new ThreadExecutor()); |
814 |
+ |
} |
815 |
+ |
public <T> CompletableFuture<T> whenComplete |
816 |
+ |
(CompletableFuture<T> f, |
817 |
+ |
BiConsumer<? super T,? super Throwable> a) { |
818 |
+ |
return f.whenCompleteAsync(a, new ThreadExecutor()); |
819 |
+ |
} |
820 |
|
public <T,U> CompletableFuture<Void> runAfterBoth |
821 |
|
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
822 |
|
return f.runAfterBothAsync(g, a, new ThreadExecutor()); |
833 |
|
BiFunction<? super T,? super U,? extends V> a) { |
834 |
|
return f.thenCombineAsync(g, a, new ThreadExecutor()); |
835 |
|
} |
836 |
+ |
public <T> CompletableFuture<Void> runAfterEither |
837 |
+ |
(CompletableFuture<T> f, |
838 |
+ |
CompletionStage<?> g, |
839 |
+ |
java.lang.Runnable a) { |
840 |
+ |
return f.runAfterEitherAsync(g, a, new ThreadExecutor()); |
841 |
+ |
} |
842 |
+ |
public <T> CompletableFuture<Void> acceptEither |
843 |
+ |
(CompletableFuture<T> f, |
844 |
+ |
CompletionStage<? extends T> g, |
845 |
+ |
Consumer<? super T> a) { |
846 |
+ |
return f.acceptEitherAsync(g, a, new ThreadExecutor()); |
847 |
+ |
} |
848 |
+ |
public <T,U> CompletableFuture<U> applyToEither |
849 |
+ |
(CompletableFuture<T> f, |
850 |
+ |
CompletionStage<? extends T> g, |
851 |
+ |
Function<? super T,U> a) { |
852 |
+ |
return f.applyToEitherAsync(g, a, new ThreadExecutor()); |
853 |
+ |
} |
854 |
|
}; |
855 |
|
|
856 |
+ |
public abstract void checkExecutionMode(); |
857 |
+ |
public abstract CompletableFuture<Void> runAsync(Runnable a); |
858 |
+ |
public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); |
859 |
+ |
public abstract <T> CompletableFuture<Void> thenRun |
860 |
+ |
(CompletableFuture<T> f, Runnable a); |
861 |
+ |
public abstract <T> CompletableFuture<Void> thenAccept |
862 |
+ |
(CompletableFuture<T> f, Consumer<? super T> a); |
863 |
+ |
public abstract <T,U> CompletableFuture<U> thenApply |
864 |
+ |
(CompletableFuture<T> f, Function<? super T,U> a); |
865 |
+ |
public abstract <T,U> CompletableFuture<U> thenCompose |
866 |
+ |
(CompletableFuture<T> f, |
867 |
+ |
Function<? super T,? extends CompletionStage<U>> a); |
868 |
+ |
public abstract <T,U> CompletableFuture<U> handle |
869 |
+ |
(CompletableFuture<T> f, |
870 |
+ |
BiFunction<? super T,Throwable,? extends U> a); |
871 |
+ |
public abstract <T> CompletableFuture<T> whenComplete |
872 |
+ |
(CompletableFuture<T> f, |
873 |
+ |
BiConsumer<? super T,? super Throwable> a); |
874 |
|
public abstract <T,U> CompletableFuture<Void> runAfterBoth |
875 |
|
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a); |
876 |
|
public abstract <T,U> CompletableFuture<Void> thenAcceptBoth |
881 |
|
(CompletableFuture<T> f, |
882 |
|
CompletionStage<? extends U> g, |
883 |
|
BiFunction<? super T,? super U,? extends V> a); |
884 |
+ |
public abstract <T> CompletableFuture<Void> runAfterEither |
885 |
+ |
(CompletableFuture<T> f, |
886 |
+ |
CompletionStage<?> g, |
887 |
+ |
java.lang.Runnable a); |
888 |
+ |
public abstract <T> CompletableFuture<Void> acceptEither |
889 |
+ |
(CompletableFuture<T> f, |
890 |
+ |
CompletionStage<? extends T> g, |
891 |
+ |
Consumer<? super T> a); |
892 |
+ |
public abstract <T,U> CompletableFuture<U> applyToEither |
893 |
+ |
(CompletableFuture<T> f, |
894 |
+ |
CompletionStage<? extends T> g, |
895 |
+ |
Function<? super T,U> a); |
896 |
|
} |
897 |
|
|
898 |
|
/** |
899 |
< |
* exceptionally action completes with function value on source |
900 |
< |
* exception; otherwise with source value |
899 |
> |
* exceptionally action is not invoked when source completes |
900 |
> |
* normally, and source result is propagated |
901 |
|
*/ |
902 |
< |
public void testExceptionally() { |
903 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
904 |
< |
ExceptionToInteger r = new ExceptionToInteger(); |
905 |
< |
CompletableFuture<Integer> g = f.exceptionally(r); |
906 |
< |
f.completeExceptionally(new CFException()); |
907 |
< |
checkCompletedNormally(g, three); |
902 |
> |
public void testExceptionally_normalCompletion() { |
903 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
904 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
905 |
> |
{ |
906 |
> |
final AtomicInteger a = new AtomicInteger(0); |
907 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
908 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
909 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
910 |
> |
((Throwable t) -> { |
911 |
> |
// Should not be called |
912 |
> |
a.getAndIncrement(); |
913 |
> |
throw new AssertionError(); |
914 |
> |
}); |
915 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
916 |
|
|
917 |
< |
f = new CompletableFuture<>(); |
918 |
< |
r = new ExceptionToInteger(); |
919 |
< |
g = f.exceptionally(r); |
920 |
< |
f.complete(one); |
546 |
< |
checkCompletedNormally(g, one); |
547 |
< |
} |
917 |
> |
checkCompletedNormally(g, v1); |
918 |
> |
checkCompletedNormally(f, v1); |
919 |
> |
assertEquals(0, a.get()); |
920 |
> |
}} |
921 |
|
|
922 |
|
/** |
923 |
< |
* handle action completes normally with function value on either |
924 |
< |
* normal or exceptional completion of source |
923 |
> |
* exceptionally action completes with function value on source |
924 |
> |
* exception |
925 |
|
*/ |
926 |
< |
public void testHandle() { |
927 |
< |
CompletableFuture<Integer> f, g; |
928 |
< |
IntegerHandler r; |
929 |
< |
|
930 |
< |
f = new CompletableFuture<>(); |
931 |
< |
f.completeExceptionally(new CFException()); |
932 |
< |
g = f.handle(r = new IntegerHandler()); |
933 |
< |
assertTrue(r.ran); |
934 |
< |
checkCompletedNormally(g, three); |
935 |
< |
|
936 |
< |
f = new CompletableFuture<>(); |
937 |
< |
g = f.handle(r = new IntegerHandler()); |
938 |
< |
assertFalse(r.ran); |
939 |
< |
f.completeExceptionally(new CFException()); |
940 |
< |
checkCompletedNormally(g, three); |
941 |
< |
assertTrue(r.ran); |
926 |
> |
public void testExceptionally_exceptionalCompletion() { |
927 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
928 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
929 |
> |
{ |
930 |
> |
final AtomicInteger a = new AtomicInteger(0); |
931 |
> |
final CFException ex = new CFException(); |
932 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
933 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
934 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
935 |
> |
((Throwable t) -> { |
936 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
937 |
> |
threadAssertSame(t, ex); |
938 |
> |
a.getAndIncrement(); |
939 |
> |
return v1; |
940 |
> |
}); |
941 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
942 |
|
|
943 |
< |
f = new CompletableFuture<>(); |
944 |
< |
f.complete(one); |
945 |
< |
g = f.handle(r = new IntegerHandler()); |
573 |
< |
assertTrue(r.ran); |
574 |
< |
checkCompletedNormally(g, two); |
943 |
> |
checkCompletedNormally(g, v1); |
944 |
> |
assertEquals(1, a.get()); |
945 |
> |
}} |
946 |
|
|
947 |
< |
f = new CompletableFuture<>(); |
948 |
< |
g = f.handle(r = new IntegerHandler()); |
949 |
< |
assertFalse(r.ran); |
950 |
< |
f.complete(one); |
951 |
< |
assertTrue(r.ran); |
952 |
< |
checkCompletedNormally(g, two); |
953 |
< |
} |
947 |
> |
public void testExceptionally_exceptionalCompletionActionFailed() { |
948 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
949 |
> |
{ |
950 |
> |
final AtomicInteger a = new AtomicInteger(0); |
951 |
> |
final CFException ex1 = new CFException(); |
952 |
> |
final CFException ex2 = new CFException(); |
953 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
954 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
955 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
956 |
> |
((Throwable t) -> { |
957 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
958 |
> |
threadAssertSame(t, ex1); |
959 |
> |
a.getAndIncrement(); |
960 |
> |
throw ex2; |
961 |
> |
}); |
962 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
963 |
> |
|
964 |
> |
checkCompletedWithWrappedException(g, ex2); |
965 |
> |
assertEquals(1, a.get()); |
966 |
> |
}} |
967 |
|
|
968 |
|
/** |
969 |
< |
* runAsync completes after running Runnable |
969 |
> |
* whenComplete action executes on normal completion, propagating |
970 |
> |
* source result. |
971 |
|
*/ |
972 |
< |
public void testRunAsync() { |
973 |
< |
Noop r = new Noop(); |
974 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
975 |
< |
assertNull(f.join()); |
976 |
< |
assertTrue(r.ran); |
977 |
< |
checkCompletedNormally(f, null); |
978 |
< |
} |
972 |
> |
public void testWhenComplete_normalCompletion1() { |
973 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
974 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
975 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
976 |
> |
{ |
977 |
> |
final AtomicInteger a = new AtomicInteger(0); |
978 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
979 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
980 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
981 |
> |
(f, |
982 |
> |
(Integer x, Throwable t) -> { |
983 |
> |
m.checkExecutionMode(); |
984 |
> |
threadAssertSame(x, v1); |
985 |
> |
threadAssertNull(t); |
986 |
> |
a.getAndIncrement(); |
987 |
> |
}); |
988 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
989 |
|
|
990 |
< |
/** |
991 |
< |
* runAsync with executor completes after running Runnable |
992 |
< |
*/ |
993 |
< |
public void testRunAsync2() { |
599 |
< |
Noop r = new Noop(); |
600 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
601 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
602 |
< |
assertNull(f.join()); |
603 |
< |
assertTrue(r.ran); |
604 |
< |
checkCompletedNormally(f, null); |
605 |
< |
assertEquals(1, exec.count.get()); |
606 |
< |
} |
990 |
> |
checkCompletedNormally(g, v1); |
991 |
> |
checkCompletedNormally(f, v1); |
992 |
> |
assertEquals(1, a.get()); |
993 |
> |
}} |
994 |
|
|
995 |
|
/** |
996 |
< |
* failing runAsync completes exceptionally after running Runnable |
996 |
> |
* whenComplete action executes on exceptional completion, propagating |
997 |
> |
* source result. |
998 |
|
*/ |
999 |
< |
public void testRunAsync3() { |
1000 |
< |
FailingNoop r = new FailingNoop(); |
1001 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
1002 |
< |
checkCompletedWithWrappedCFException(f); |
1003 |
< |
assertTrue(r.ran); |
1004 |
< |
} |
999 |
> |
public void testWhenComplete_exceptionalCompletion() { |
1000 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1001 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1002 |
> |
{ |
1003 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1004 |
> |
final CFException ex = new CFException(); |
1005 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1006 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
1007 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1008 |
> |
(f, |
1009 |
> |
(Integer x, Throwable t) -> { |
1010 |
> |
m.checkExecutionMode(); |
1011 |
> |
threadAssertNull(x); |
1012 |
> |
threadAssertSame(t, ex); |
1013 |
> |
a.getAndIncrement(); |
1014 |
> |
}); |
1015 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
1016 |
> |
|
1017 |
> |
checkCompletedWithWrappedException(g, ex); |
1018 |
> |
checkCompletedExceptionally(f, ex); |
1019 |
> |
assertEquals(1, a.get()); |
1020 |
> |
}} |
1021 |
|
|
1022 |
|
/** |
1023 |
< |
* supplyAsync completes with result of supplier |
1023 |
> |
* whenComplete action executes on cancelled source, propagating |
1024 |
> |
* CancellationException. |
1025 |
|
*/ |
1026 |
< |
public void testSupplyAsync() { |
1027 |
< |
CompletableFuture<Integer> f; |
1028 |
< |
f = CompletableFuture.supplyAsync(supplyOne); |
1029 |
< |
assertEquals(f.join(), one); |
1030 |
< |
checkCompletedNormally(f, one); |
1031 |
< |
} |
1026 |
> |
public void testWhenComplete_sourceCancelled() { |
1027 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1028 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1029 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1030 |
> |
{ |
1031 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1032 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1033 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1034 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1035 |
> |
(f, |
1036 |
> |
(Integer x, Throwable t) -> { |
1037 |
> |
m.checkExecutionMode(); |
1038 |
> |
threadAssertNull(x); |
1039 |
> |
threadAssertTrue(t instanceof CancellationException); |
1040 |
> |
a.getAndIncrement(); |
1041 |
> |
}); |
1042 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1043 |
|
|
1044 |
< |
/** |
1045 |
< |
* supplyAsync with executor completes with result of supplier |
1046 |
< |
*/ |
1047 |
< |
public void testSupplyAsync2() { |
632 |
< |
CompletableFuture<Integer> f; |
633 |
< |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
634 |
< |
assertEquals(f.join(), one); |
635 |
< |
checkCompletedNormally(f, one); |
636 |
< |
} |
1044 |
> |
checkCompletedWithWrappedCancellationException(g); |
1045 |
> |
checkCancelled(f); |
1046 |
> |
assertEquals(1, a.get()); |
1047 |
> |
}} |
1048 |
|
|
1049 |
|
/** |
1050 |
< |
* Failing supplyAsync completes exceptionally |
1050 |
> |
* If a whenComplete action throws an exception when triggered by |
1051 |
> |
* a normal completion, it completes exceptionally |
1052 |
|
*/ |
1053 |
< |
public void testSupplyAsync3() { |
1054 |
< |
FailingSupplier r = new FailingSupplier(); |
1055 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r); |
1056 |
< |
checkCompletedWithWrappedCFException(f); |
1057 |
< |
assertTrue(r.ran); |
1058 |
< |
} |
1053 |
> |
public void testWhenComplete_actionFailed() { |
1054 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1055 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1056 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1057 |
> |
{ |
1058 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1059 |
> |
final CFException ex = new CFException(); |
1060 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1061 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1062 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1063 |
> |
(f, |
1064 |
> |
(Integer x, Throwable t) -> { |
1065 |
> |
m.checkExecutionMode(); |
1066 |
> |
threadAssertSame(x, v1); |
1067 |
> |
threadAssertNull(t); |
1068 |
> |
a.getAndIncrement(); |
1069 |
> |
throw ex; |
1070 |
> |
}); |
1071 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1072 |
|
|
1073 |
< |
// seq completion methods |
1073 |
> |
checkCompletedWithWrappedException(g, ex); |
1074 |
> |
checkCompletedNormally(f, v1); |
1075 |
> |
assertEquals(1, a.get()); |
1076 |
> |
}} |
1077 |
|
|
1078 |
|
/** |
1079 |
< |
* thenRun result completes normally after normal completion of source |
1079 |
> |
* If a whenComplete action throws an exception when triggered by |
1080 |
> |
* a source completion that also throws an exception, the source |
1081 |
> |
* exception takes precedence. |
1082 |
|
*/ |
1083 |
< |
public void testThenRun() { |
1084 |
< |
CompletableFuture<Integer> f; |
1085 |
< |
CompletableFuture<Void> g; |
1086 |
< |
Noop r; |
1087 |
< |
|
1088 |
< |
f = new CompletableFuture<>(); |
1089 |
< |
g = f.thenRun(r = new Noop()); |
1090 |
< |
f.complete(null); |
1091 |
< |
checkCompletedNormally(g, null); |
1092 |
< |
assertTrue(r.ran); |
1093 |
< |
|
1094 |
< |
f = new CompletableFuture<>(); |
1095 |
< |
f.complete(null); |
1096 |
< |
g = f.thenRun(r = new Noop()); |
1097 |
< |
checkCompletedNormally(g, null); |
1098 |
< |
assertTrue(r.ran); |
1099 |
< |
} |
1083 |
> |
public void testWhenComplete_actionFailedSourceFailed() { |
1084 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1085 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1086 |
> |
{ |
1087 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1088 |
> |
final CFException ex1 = new CFException(); |
1089 |
> |
final CFException ex2 = new CFException(); |
1090 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1091 |
> |
|
1092 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1093 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1094 |
> |
(f, |
1095 |
> |
(Integer x, Throwable t) -> { |
1096 |
> |
m.checkExecutionMode(); |
1097 |
> |
threadAssertSame(t, ex1); |
1098 |
> |
threadAssertNull(x); |
1099 |
> |
a.getAndIncrement(); |
1100 |
> |
throw ex2; |
1101 |
> |
}); |
1102 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1103 |
> |
|
1104 |
> |
checkCompletedWithWrappedException(g, ex1); |
1105 |
> |
checkCompletedExceptionally(f, ex1); |
1106 |
> |
assertEquals(1, a.get()); |
1107 |
> |
}} |
1108 |
|
|
1109 |
|
/** |
1110 |
< |
* thenRun result completes exceptionally after exceptional |
1110 |
> |
* handle action completes normally with function value on normal |
1111 |
|
* completion of source |
1112 |
|
*/ |
1113 |
< |
public void testThenRun2() { |
1114 |
< |
CompletableFuture<Integer> f; |
1115 |
< |
CompletableFuture<Void> g; |
1116 |
< |
Noop r; |
1117 |
< |
|
1118 |
< |
f = new CompletableFuture<>(); |
1119 |
< |
g = f.thenRun(r = new Noop()); |
1120 |
< |
f.completeExceptionally(new CFException()); |
1121 |
< |
checkCompletedWithWrappedCFException(g); |
1122 |
< |
assertFalse(r.ran); |
1113 |
> |
public void testHandle_normalCompletion() { |
1114 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1115 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1116 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1117 |
> |
{ |
1118 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1119 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1120 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1121 |
> |
final CompletableFuture<Integer> g = m.handle |
1122 |
> |
(f, |
1123 |
> |
(Integer x, Throwable t) -> { |
1124 |
> |
m.checkExecutionMode(); |
1125 |
> |
threadAssertSame(x, v1); |
1126 |
> |
threadAssertNull(t); |
1127 |
> |
a.getAndIncrement(); |
1128 |
> |
return inc(v1); |
1129 |
> |
}); |
1130 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1131 |
|
|
1132 |
< |
f = new CompletableFuture<>(); |
1133 |
< |
f.completeExceptionally(new CFException()); |
1134 |
< |
g = f.thenRun(r = new Noop()); |
1135 |
< |
checkCompletedWithWrappedCFException(g); |
690 |
< |
assertFalse(r.ran); |
691 |
< |
} |
1132 |
> |
checkCompletedNormally(g, inc(v1)); |
1133 |
> |
checkCompletedNormally(f, v1); |
1134 |
> |
assertEquals(1, a.get()); |
1135 |
> |
}} |
1136 |
|
|
1137 |
|
/** |
1138 |
< |
* thenRun result completes exceptionally if action does |
1138 |
> |
* handle action completes normally with function value on |
1139 |
> |
* exceptional completion of source |
1140 |
|
*/ |
1141 |
< |
public void testThenRun3() { |
1142 |
< |
CompletableFuture<Integer> f; |
1143 |
< |
CompletableFuture<Void> g; |
1144 |
< |
FailingNoop r; |
1145 |
< |
|
1146 |
< |
f = new CompletableFuture<>(); |
1147 |
< |
g = f.thenRun(r = new FailingNoop()); |
1148 |
< |
f.complete(null); |
1149 |
< |
checkCompletedWithWrappedCFException(g); |
1141 |
> |
public void testHandle_exceptionalCompletion() { |
1142 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1143 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1144 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1145 |
> |
{ |
1146 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1147 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1148 |
> |
final CFException ex = new CFException(); |
1149 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
1150 |
> |
final CompletableFuture<Integer> g = m.handle |
1151 |
> |
(f, |
1152 |
> |
(Integer x, Throwable t) -> { |
1153 |
> |
m.checkExecutionMode(); |
1154 |
> |
threadAssertNull(x); |
1155 |
> |
threadAssertSame(t, ex); |
1156 |
> |
a.getAndIncrement(); |
1157 |
> |
return v1; |
1158 |
> |
}); |
1159 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
1160 |
|
|
1161 |
< |
f = new CompletableFuture<>(); |
1162 |
< |
f.complete(null); |
1163 |
< |
g = f.thenRun(r = new FailingNoop()); |
1164 |
< |
checkCompletedWithWrappedCFException(g); |
710 |
< |
} |
1161 |
> |
checkCompletedNormally(g, v1); |
1162 |
> |
checkCompletedExceptionally(f, ex); |
1163 |
> |
assertEquals(1, a.get()); |
1164 |
> |
}} |
1165 |
|
|
1166 |
|
/** |
1167 |
< |
* thenRun result completes exceptionally if source cancelled |
1167 |
> |
* handle action completes normally with function value on |
1168 |
> |
* cancelled source |
1169 |
|
*/ |
1170 |
< |
public void testThenRun4() { |
1171 |
< |
CompletableFuture<Integer> f; |
1172 |
< |
CompletableFuture<Void> g; |
1173 |
< |
Noop r; |
1174 |
< |
|
1175 |
< |
f = new CompletableFuture<>(); |
1176 |
< |
g = f.thenRun(r = new Noop()); |
1177 |
< |
assertTrue(f.cancel(true)); |
1178 |
< |
checkCompletedWithWrappedCancellationException(g); |
1170 |
> |
public void testHandle_sourceCancelled() { |
1171 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1172 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1173 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1174 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1175 |
> |
{ |
1176 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1177 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1178 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1179 |
> |
final CompletableFuture<Integer> g = m.handle |
1180 |
> |
(f, |
1181 |
> |
(Integer x, Throwable t) -> { |
1182 |
> |
m.checkExecutionMode(); |
1183 |
> |
threadAssertNull(x); |
1184 |
> |
threadAssertTrue(t instanceof CancellationException); |
1185 |
> |
a.getAndIncrement(); |
1186 |
> |
return v1; |
1187 |
> |
}); |
1188 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1189 |
|
|
1190 |
< |
f = new CompletableFuture<>(); |
1191 |
< |
assertTrue(f.cancel(true)); |
1192 |
< |
g = f.thenRun(r = new Noop()); |
1193 |
< |
checkCompletedWithWrappedCancellationException(g); |
729 |
< |
} |
1190 |
> |
checkCompletedNormally(g, v1); |
1191 |
> |
checkCancelled(f); |
1192 |
> |
assertEquals(1, a.get()); |
1193 |
> |
}} |
1194 |
|
|
1195 |
|
/** |
1196 |
< |
* thenApply result completes normally after normal completion of source |
1196 |
> |
* handle result completes exceptionally if action does |
1197 |
|
*/ |
1198 |
< |
public void testThenApply() { |
1199 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1200 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
1201 |
< |
f.complete(one); |
1202 |
< |
checkCompletedNormally(g, two); |
1203 |
< |
} |
1198 |
> |
public void testHandle_sourceFailedActionFailed() { |
1199 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1200 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1201 |
> |
{ |
1202 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1203 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1204 |
> |
final CFException ex1 = new CFException(); |
1205 |
> |
final CFException ex2 = new CFException(); |
1206 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1207 |
> |
final CompletableFuture<Integer> g = m.handle |
1208 |
> |
(f, |
1209 |
> |
(Integer x, Throwable t) -> { |
1210 |
> |
m.checkExecutionMode(); |
1211 |
> |
threadAssertNull(x); |
1212 |
> |
threadAssertSame(ex1, t); |
1213 |
> |
a.getAndIncrement(); |
1214 |
> |
throw ex2; |
1215 |
> |
}); |
1216 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1217 |
> |
|
1218 |
> |
checkCompletedWithWrappedException(g, ex2); |
1219 |
> |
checkCompletedExceptionally(f, ex1); |
1220 |
> |
assertEquals(1, a.get()); |
1221 |
> |
}} |
1222 |
|
|
1223 |
< |
/** |
1224 |
< |
* thenApply result completes exceptionally after exceptional |
1225 |
< |
* completion of source |
1226 |
< |
*/ |
1227 |
< |
public void testThenApply2() { |
1228 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1229 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
1230 |
< |
f.completeExceptionally(new CFException()); |
1231 |
< |
checkCompletedWithWrappedCFException(g); |
1232 |
< |
} |
1223 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1224 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1225 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1226 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1227 |
> |
{ |
1228 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1229 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1230 |
> |
final CFException ex = new CFException(); |
1231 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1232 |
> |
final CompletableFuture<Integer> g = m.handle |
1233 |
> |
(f, |
1234 |
> |
(Integer x, Throwable t) -> { |
1235 |
> |
m.checkExecutionMode(); |
1236 |
> |
threadAssertSame(x, v1); |
1237 |
> |
threadAssertNull(t); |
1238 |
> |
a.getAndIncrement(); |
1239 |
> |
throw ex; |
1240 |
> |
}); |
1241 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1242 |
|
|
1243 |
< |
/** |
1244 |
< |
* thenApply result completes exceptionally if action does |
1245 |
< |
*/ |
1246 |
< |
public void testThenApply3() { |
756 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
757 |
< |
CompletableFuture<Integer> g = f.thenApply(new FailingFunction()); |
758 |
< |
f.complete(one); |
759 |
< |
checkCompletedWithWrappedCFException(g); |
760 |
< |
} |
1243 |
> |
checkCompletedWithWrappedException(g, ex); |
1244 |
> |
checkCompletedNormally(f, v1); |
1245 |
> |
assertEquals(1, a.get()); |
1246 |
> |
}} |
1247 |
|
|
1248 |
|
/** |
1249 |
< |
* thenApply result completes exceptionally if source cancelled |
1249 |
> |
* runAsync completes after running Runnable |
1250 |
|
*/ |
1251 |
< |
public void testThenApply4() { |
1252 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1253 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
1254 |
< |
assertTrue(f.cancel(true)); |
1255 |
< |
checkCompletedWithWrappedCancellationException(g); |
1256 |
< |
} |
1251 |
> |
public void testRunAsync_normalCompletion() { |
1252 |
> |
ExecutionMode[] executionModes = { |
1253 |
> |
ExecutionMode.ASYNC, |
1254 |
> |
ExecutionMode.EXECUTOR, |
1255 |
> |
}; |
1256 |
> |
for (ExecutionMode m : executionModes) |
1257 |
> |
{ |
1258 |
> |
final Noop r = new Noop(m); |
1259 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1260 |
> |
assertNull(f.join()); |
1261 |
> |
checkCompletedNormally(f, null); |
1262 |
> |
r.assertInvoked(); |
1263 |
> |
}} |
1264 |
|
|
1265 |
|
/** |
1266 |
< |
* thenAccept result completes normally after normal completion of source |
1266 |
> |
* failing runAsync completes exceptionally after running Runnable |
1267 |
|
*/ |
1268 |
< |
public void testThenAccept() { |
1269 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1270 |
< |
IncAction r = new IncAction(); |
1271 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1272 |
< |
f.complete(one); |
1273 |
< |
checkCompletedNormally(g, null); |
1274 |
< |
assertEquals(r.value, 2); |
1275 |
< |
} |
1268 |
> |
public void testRunAsync_exceptionalCompletion() { |
1269 |
> |
ExecutionMode[] executionModes = { |
1270 |
> |
ExecutionMode.ASYNC, |
1271 |
> |
ExecutionMode.EXECUTOR, |
1272 |
> |
}; |
1273 |
> |
for (ExecutionMode m : executionModes) |
1274 |
> |
{ |
1275 |
> |
final FailingRunnable r = new FailingRunnable(m); |
1276 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1277 |
> |
checkCompletedWithWrappedCFException(f); |
1278 |
> |
r.assertInvoked(); |
1279 |
> |
}} |
1280 |
|
|
1281 |
|
/** |
1282 |
< |
* thenAccept result completes exceptionally after exceptional |
786 |
< |
* completion of source |
1282 |
> |
* supplyAsync completes with result of supplier |
1283 |
|
*/ |
1284 |
< |
public void testThenAccept2() { |
1285 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1286 |
< |
IncAction r = new IncAction(); |
1287 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1288 |
< |
f.completeExceptionally(new CFException()); |
1289 |
< |
checkCompletedWithWrappedCFException(g); |
1290 |
< |
} |
1284 |
> |
public void testSupplyAsync_normalCompletion() { |
1285 |
> |
ExecutionMode[] executionModes = { |
1286 |
> |
ExecutionMode.ASYNC, |
1287 |
> |
ExecutionMode.EXECUTOR, |
1288 |
> |
}; |
1289 |
> |
for (ExecutionMode m : executionModes) |
1290 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1291 |
> |
{ |
1292 |
> |
final IntegerSupplier r = new IntegerSupplier(m, v1); |
1293 |
> |
final CompletableFuture<Integer> f = m.supplyAsync(r); |
1294 |
> |
assertSame(v1, f.join()); |
1295 |
> |
checkCompletedNormally(f, v1); |
1296 |
> |
r.assertInvoked(); |
1297 |
> |
}} |
1298 |
|
|
1299 |
|
/** |
1300 |
< |
* thenAccept result completes exceptionally if action does |
1300 |
> |
* Failing supplyAsync completes exceptionally |
1301 |
|
*/ |
1302 |
< |
public void testThenAccept3() { |
1303 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1304 |
< |
FailingConsumer r = new FailingConsumer(); |
1305 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1306 |
< |
f.complete(one); |
1307 |
< |
checkCompletedWithWrappedCFException(g); |
1308 |
< |
assertTrue(r.ran); |
1309 |
< |
} |
1302 |
> |
public void testSupplyAsync_exceptionalCompletion() { |
1303 |
> |
ExecutionMode[] executionModes = { |
1304 |
> |
ExecutionMode.ASYNC, |
1305 |
> |
ExecutionMode.EXECUTOR, |
1306 |
> |
}; |
1307 |
> |
for (ExecutionMode m : executionModes) |
1308 |
> |
{ |
1309 |
> |
FailingSupplier r = new FailingSupplier(m); |
1310 |
> |
CompletableFuture<Integer> f = m.supplyAsync(r); |
1311 |
> |
checkCompletedWithWrappedCFException(f); |
1312 |
> |
r.assertInvoked(); |
1313 |
> |
}} |
1314 |
|
|
1315 |
< |
/** |
809 |
< |
* thenAccept result completes exceptionally if source cancelled |
810 |
< |
*/ |
811 |
< |
public void testThenAccept4() { |
812 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
813 |
< |
IncAction r = new IncAction(); |
814 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
815 |
< |
assertTrue(f.cancel(true)); |
816 |
< |
checkCompletedWithWrappedCancellationException(g); |
817 |
< |
} |
1315 |
> |
// seq completion methods |
1316 |
|
|
1317 |
|
/** |
1318 |
< |
* thenCombine result completes normally after normal completion |
821 |
< |
* of sources |
1318 |
> |
* thenRun result completes normally after normal completion of source |
1319 |
|
*/ |
1320 |
< |
public void testThenCombine_normalCompletion1() { |
1320 |
> |
public void testThenRun_normalCompletion() { |
1321 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1322 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1323 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
827 |
< |
|
1323 |
> |
{ |
1324 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1325 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1326 |
< |
final SubtractFunction r = new SubtractFunction(); |
831 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1325 |
> |
final Noop[] rs = new Noop[6]; |
1326 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1327 |
|
|
1328 |
< |
f.complete(v1); |
1329 |
< |
checkIncomplete(h); |
1330 |
< |
assertFalse(r.ran()); |
1331 |
< |
g.complete(v2); |
1332 |
< |
|
1333 |
< |
checkCompletedNormally(h, subtract(v1, v2)); |
1328 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1329 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1330 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1331 |
> |
checkIncomplete(h0); |
1332 |
> |
checkIncomplete(h1); |
1333 |
> |
checkIncomplete(h2); |
1334 |
> |
assertTrue(f.complete(v1)); |
1335 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1336 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1337 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1338 |
> |
|
1339 |
> |
checkCompletedNormally(h0, null); |
1340 |
> |
checkCompletedNormally(h1, null); |
1341 |
> |
checkCompletedNormally(h2, null); |
1342 |
> |
checkCompletedNormally(h3, null); |
1343 |
> |
checkCompletedNormally(h4, null); |
1344 |
> |
checkCompletedNormally(h5, null); |
1345 |
|
checkCompletedNormally(f, v1); |
1346 |
< |
checkCompletedNormally(g, v2); |
1347 |
< |
} |
842 |
< |
} |
1346 |
> |
for (Noop r : rs) r.assertInvoked(); |
1347 |
> |
}} |
1348 |
|
|
1349 |
< |
public void testThenCombine_normalCompletion2() { |
1349 |
> |
/** |
1350 |
> |
* thenRun result completes exceptionally after exceptional |
1351 |
> |
* completion of source |
1352 |
> |
*/ |
1353 |
> |
public void testThenRun_exceptionalCompletion() { |
1354 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1355 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1356 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
848 |
< |
|
1355 |
> |
{ |
1356 |
> |
final CFException ex = new CFException(); |
1357 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1358 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1359 |
< |
final SubtractFunction r = new SubtractFunction(); |
852 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
853 |
< |
|
854 |
< |
g.complete(v2); |
855 |
< |
checkIncomplete(h); |
856 |
< |
assertFalse(r.ran()); |
857 |
< |
f.complete(v1); |
1358 |
> |
final Noop[] rs = new Noop[6]; |
1359 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1360 |
|
|
1361 |
< |
checkCompletedNormally(h, subtract(v1, v2)); |
1362 |
< |
checkCompletedNormally(f, v1); |
1363 |
< |
checkCompletedNormally(g, v2); |
1364 |
< |
} |
1365 |
< |
} |
1361 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1362 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1363 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1364 |
> |
checkIncomplete(h0); |
1365 |
> |
checkIncomplete(h1); |
1366 |
> |
checkIncomplete(h2); |
1367 |
> |
assertTrue(f.completeExceptionally(ex)); |
1368 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1369 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1370 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1371 |
> |
|
1372 |
> |
checkCompletedWithWrappedException(h0, ex); |
1373 |
> |
checkCompletedWithWrappedException(h1, ex); |
1374 |
> |
checkCompletedWithWrappedException(h2, ex); |
1375 |
> |
checkCompletedWithWrappedException(h3, ex); |
1376 |
> |
checkCompletedWithWrappedException(h4, ex); |
1377 |
> |
checkCompletedWithWrappedException(h5, ex); |
1378 |
> |
checkCompletedExceptionally(f, ex); |
1379 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1380 |
> |
}} |
1381 |
|
|
1382 |
< |
public void testThenCombine_normalCompletion3() { |
1382 |
> |
/** |
1383 |
> |
* thenRun result completes exceptionally if source cancelled |
1384 |
> |
*/ |
1385 |
> |
public void testThenRun_sourceCancelled() { |
1386 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1387 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1388 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
869 |
< |
|
1387 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1388 |
> |
{ |
1389 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1390 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1391 |
< |
final SubtractFunction r = new SubtractFunction(); |
873 |
< |
|
874 |
< |
g.complete(v2); |
875 |
< |
f.complete(v1); |
876 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1390 |
> |
final Noop[] rs = new Noop[6]; |
1391 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1392 |
|
|
1393 |
< |
checkCompletedNormally(h, subtract(v1, v2)); |
1394 |
< |
checkCompletedNormally(f, v1); |
1395 |
< |
checkCompletedNormally(g, v2); |
1396 |
< |
} |
1397 |
< |
} |
1393 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1394 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1395 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1396 |
> |
checkIncomplete(h0); |
1397 |
> |
checkIncomplete(h1); |
1398 |
> |
checkIncomplete(h2); |
1399 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1400 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1401 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1402 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1403 |
> |
|
1404 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1405 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1406 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1407 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1408 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1409 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1410 |
> |
checkCancelled(f); |
1411 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1412 |
> |
}} |
1413 |
|
|
1414 |
< |
public void testThenCombine_normalCompletion4() { |
1414 |
> |
/** |
1415 |
> |
* thenRun result completes exceptionally if action does |
1416 |
> |
*/ |
1417 |
> |
public void testThenRun_actionFailed() { |
1418 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1419 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1420 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
888 |
< |
|
1420 |
> |
{ |
1421 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1422 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1423 |
< |
final SubtractFunction r = new SubtractFunction(); |
1422 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1423 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1424 |
|
|
1425 |
< |
f.complete(v1); |
1426 |
< |
g.complete(v2); |
1427 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1428 |
< |
|
1429 |
< |
checkCompletedNormally(h, subtract(v1, v2)); |
1425 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1426 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1427 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1428 |
> |
assertTrue(f.complete(v1)); |
1429 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1430 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1431 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1432 |
> |
|
1433 |
> |
checkCompletedWithWrappedCFException(h0); |
1434 |
> |
checkCompletedWithWrappedCFException(h1); |
1435 |
> |
checkCompletedWithWrappedCFException(h2); |
1436 |
> |
checkCompletedWithWrappedCFException(h3); |
1437 |
> |
checkCompletedWithWrappedCFException(h4); |
1438 |
> |
checkCompletedWithWrappedCFException(h5); |
1439 |
|
checkCompletedNormally(f, v1); |
1440 |
< |
checkCompletedNormally(g, v2); |
900 |
< |
} |
901 |
< |
} |
1440 |
> |
}} |
1441 |
|
|
1442 |
|
/** |
1443 |
< |
* thenCombine result completes exceptionally after exceptional |
905 |
< |
* completion of either source |
1443 |
> |
* thenApply result completes normally after normal completion of source |
1444 |
|
*/ |
1445 |
< |
public void testThenCombine_exceptionalCompletion1() { |
908 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
909 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
910 |
< |
|
911 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
912 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
913 |
< |
final SubtractFunction r = new SubtractFunction(); |
914 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
915 |
< |
final CFException ex = new CFException(); |
916 |
< |
|
917 |
< |
f.completeExceptionally(ex); |
918 |
< |
checkIncomplete(h); |
919 |
< |
g.complete(v1); |
920 |
< |
|
921 |
< |
checkCompletedWithWrappedCFException(h, ex); |
922 |
< |
checkCompletedWithWrappedCFException(f, ex); |
923 |
< |
assertFalse(r.ran()); |
924 |
< |
checkCompletedNormally(g, v1); |
925 |
< |
} |
926 |
< |
} |
927 |
< |
|
928 |
< |
public void testThenCombine_exceptionalCompletion2() { |
1445 |
> |
public void testThenApply_normalCompletion() { |
1446 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1447 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
1448 |
< |
|
1447 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1448 |
> |
{ |
1449 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1450 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1451 |
< |
final SubtractFunction r = new SubtractFunction(); |
935 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
936 |
< |
final CFException ex = new CFException(); |
937 |
< |
|
938 |
< |
g.completeExceptionally(ex); |
939 |
< |
checkIncomplete(h); |
940 |
< |
f.complete(v1); |
1450 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1451 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1452 |
|
|
1453 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1454 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1455 |
< |
assertFalse(r.ran()); |
1453 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1454 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1455 |
> |
checkIncomplete(h0); |
1456 |
> |
checkIncomplete(h1); |
1457 |
> |
assertTrue(f.complete(v1)); |
1458 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1459 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1460 |
> |
|
1461 |
> |
checkCompletedNormally(h0, inc(v1)); |
1462 |
> |
checkCompletedNormally(h1, inc(v1)); |
1463 |
> |
checkCompletedNormally(h2, inc(v1)); |
1464 |
> |
checkCompletedNormally(h3, inc(v1)); |
1465 |
|
checkCompletedNormally(f, v1); |
1466 |
< |
} |
1467 |
< |
} |
1466 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1467 |
> |
}} |
1468 |
|
|
1469 |
< |
public void testThenCombine_exceptionalCompletion3() { |
1469 |
> |
/** |
1470 |
> |
* thenApply result completes exceptionally after exceptional |
1471 |
> |
* completion of source |
1472 |
> |
*/ |
1473 |
> |
public void testThenApply_exceptionalCompletion() { |
1474 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1475 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
952 |
< |
|
953 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
954 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
955 |
< |
final SubtractFunction r = new SubtractFunction(); |
1475 |
> |
{ |
1476 |
|
final CFException ex = new CFException(); |
1477 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1478 |
+ |
final IncFunction[] rs = new IncFunction[4]; |
1479 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1480 |
|
|
1481 |
< |
g.completeExceptionally(ex); |
1482 |
< |
f.complete(v1); |
1483 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1484 |
< |
|
1485 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1486 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1487 |
< |
assertFalse(r.ran()); |
1488 |
< |
checkCompletedNormally(f, v1); |
1489 |
< |
} |
1490 |
< |
} |
1481 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1482 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1483 |
> |
assertTrue(f.completeExceptionally(ex)); |
1484 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1485 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1486 |
> |
|
1487 |
> |
checkCompletedWithWrappedException(h0, ex); |
1488 |
> |
checkCompletedWithWrappedException(h1, ex); |
1489 |
> |
checkCompletedWithWrappedException(h2, ex); |
1490 |
> |
checkCompletedWithWrappedException(h3, ex); |
1491 |
> |
checkCompletedExceptionally(f, ex); |
1492 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1493 |
> |
}} |
1494 |
|
|
1495 |
< |
public void testThenCombine_exceptionalCompletion4() { |
1495 |
> |
/** |
1496 |
> |
* thenApply result completes exceptionally if source cancelled |
1497 |
> |
*/ |
1498 |
> |
public void testThenApply_sourceCancelled() { |
1499 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1500 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
1501 |
< |
|
1500 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1501 |
> |
{ |
1502 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1503 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1504 |
< |
final SubtractFunction r = new SubtractFunction(); |
976 |
< |
final CFException ex = new CFException(); |
1503 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1504 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1505 |
|
|
1506 |
< |
f.completeExceptionally(ex); |
1507 |
< |
g.complete(v1); |
1508 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1506 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1507 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1508 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1509 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1510 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1511 |
|
|
1512 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1513 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1514 |
< |
assertFalse(r.ran()); |
1515 |
< |
checkCompletedNormally(g, v1); |
1516 |
< |
} |
1517 |
< |
} |
1512 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1513 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1514 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1515 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1516 |
> |
checkCancelled(f); |
1517 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1518 |
> |
}} |
1519 |
|
|
1520 |
|
/** |
1521 |
< |
* thenCombine result completes exceptionally if action does |
1521 |
> |
* thenApply result completes exceptionally if action does |
1522 |
|
*/ |
1523 |
< |
public void testThenCombine_actionFailed1() { |
1523 |
> |
public void testThenApply_actionFailed() { |
1524 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1525 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1526 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
996 |
< |
|
1526 |
> |
{ |
1527 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1528 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1529 |
< |
final FailingBiFunction r = new FailingBiFunction(); |
1000 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1528 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1529 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1530 |
|
|
1531 |
< |
f.complete(v1); |
1532 |
< |
checkIncomplete(h); |
1533 |
< |
g.complete(v2); |
1534 |
< |
|
1535 |
< |
checkCompletedWithWrappedCFException(h); |
1531 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1532 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1533 |
> |
assertTrue(f.complete(v1)); |
1534 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1535 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1536 |
> |
|
1537 |
> |
checkCompletedWithWrappedCFException(h0); |
1538 |
> |
checkCompletedWithWrappedCFException(h1); |
1539 |
> |
checkCompletedWithWrappedCFException(h2); |
1540 |
> |
checkCompletedWithWrappedCFException(h3); |
1541 |
|
checkCompletedNormally(f, v1); |
1542 |
< |
checkCompletedNormally(g, v2); |
1009 |
< |
} |
1010 |
< |
} |
1542 |
> |
}} |
1543 |
|
|
1544 |
< |
public void testThenCombine_actionFailed2() { |
1544 |
> |
/** |
1545 |
> |
* thenAccept result completes normally after normal completion of source |
1546 |
> |
*/ |
1547 |
> |
public void testThenAccept_normalCompletion() { |
1548 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1549 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1550 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
1016 |
< |
|
1550 |
> |
{ |
1551 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1552 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1553 |
< |
final FailingBiFunction r = new FailingBiFunction(); |
1020 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1021 |
< |
|
1022 |
< |
g.complete(v2); |
1023 |
< |
checkIncomplete(h); |
1024 |
< |
f.complete(v1); |
1552 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1553 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1554 |
|
|
1555 |
< |
checkCompletedWithWrappedCFException(h); |
1555 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1556 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1557 |
> |
checkIncomplete(h0); |
1558 |
> |
checkIncomplete(h1); |
1559 |
> |
assertTrue(f.complete(v1)); |
1560 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1561 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1562 |
> |
|
1563 |
> |
checkCompletedNormally(h0, null); |
1564 |
> |
checkCompletedNormally(h1, null); |
1565 |
> |
checkCompletedNormally(h2, null); |
1566 |
> |
checkCompletedNormally(h3, null); |
1567 |
|
checkCompletedNormally(f, v1); |
1568 |
< |
checkCompletedNormally(g, v2); |
1569 |
< |
} |
1030 |
< |
} |
1568 |
> |
for (NoopConsumer r : rs) r.assertValue(v1); |
1569 |
> |
}} |
1570 |
|
|
1571 |
|
/** |
1572 |
< |
* thenCombine result completes exceptionally if either source cancelled |
1572 |
> |
* thenAccept result completes exceptionally after exceptional |
1573 |
> |
* completion of source |
1574 |
|
*/ |
1575 |
< |
public void testThenCombine_sourceCancelled1() { |
1575 |
> |
public void testThenAccept_exceptionalCompletion() { |
1576 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1577 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1578 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
1039 |
< |
|
1577 |
> |
{ |
1578 |
> |
final CFException ex = new CFException(); |
1579 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1580 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1581 |
< |
final SubtractFunction r = new SubtractFunction(); |
1043 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1580 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1581 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1582 |
|
|
1583 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1584 |
< |
checkIncomplete(h); |
1585 |
< |
g.complete(v1); |
1583 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1584 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1585 |
> |
assertTrue(f.completeExceptionally(ex)); |
1586 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1587 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1588 |
> |
|
1589 |
> |
checkCompletedWithWrappedException(h0, ex); |
1590 |
> |
checkCompletedWithWrappedException(h1, ex); |
1591 |
> |
checkCompletedWithWrappedException(h2, ex); |
1592 |
> |
checkCompletedWithWrappedException(h3, ex); |
1593 |
> |
checkCompletedExceptionally(f, ex); |
1594 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1595 |
> |
}} |
1596 |
|
|
1597 |
< |
checkCompletedWithWrappedCancellationException(h); |
1598 |
< |
checkCancelled(f); |
1599 |
< |
assertFalse(r.ran()); |
1600 |
< |
checkCompletedNormally(g, v1); |
1053 |
< |
} |
1054 |
< |
} |
1055 |
< |
|
1056 |
< |
public void testThenCombine_sourceCancelled2() { |
1597 |
> |
/** |
1598 |
> |
* thenAccept result completes exceptionally if source cancelled |
1599 |
> |
*/ |
1600 |
> |
public void testThenAccept_sourceCancelled() { |
1601 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1602 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1603 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
1060 |
< |
|
1603 |
> |
{ |
1604 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1605 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1606 |
< |
final SubtractFunction r = new SubtractFunction(); |
1064 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1605 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1606 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1607 |
|
|
1608 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1609 |
< |
checkIncomplete(h); |
1610 |
< |
f.complete(v1); |
1608 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1609 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1610 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1611 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1612 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1613 |
|
|
1614 |
< |
checkCompletedWithWrappedCancellationException(h); |
1615 |
< |
checkCancelled(g); |
1616 |
< |
assertFalse(r.ran()); |
1617 |
< |
checkCompletedNormally(f, v1); |
1618 |
< |
} |
1619 |
< |
} |
1614 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1615 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1616 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1617 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1618 |
> |
checkCancelled(f); |
1619 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1620 |
> |
}} |
1621 |
|
|
1622 |
< |
public void testThenCombine_sourceCancelled3() { |
1622 |
> |
/** |
1623 |
> |
* thenAccept result completes exceptionally if action does |
1624 |
> |
*/ |
1625 |
> |
public void testThenAccept_actionFailed() { |
1626 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1627 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1628 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
1081 |
< |
|
1627 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1628 |
> |
{ |
1629 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1630 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1631 |
< |
final SubtractFunction r = new SubtractFunction(); |
1630 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1631 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1632 |
|
|
1633 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1634 |
< |
f.complete(v1); |
1635 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1636 |
< |
|
1637 |
< |
checkCompletedWithWrappedCancellationException(h); |
1638 |
< |
checkCancelled(g); |
1639 |
< |
assertFalse(r.ran()); |
1633 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1634 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1635 |
> |
assertTrue(f.complete(v1)); |
1636 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1637 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1638 |
> |
|
1639 |
> |
checkCompletedWithWrappedCFException(h0); |
1640 |
> |
checkCompletedWithWrappedCFException(h1); |
1641 |
> |
checkCompletedWithWrappedCFException(h2); |
1642 |
> |
checkCompletedWithWrappedCFException(h3); |
1643 |
|
checkCompletedNormally(f, v1); |
1644 |
< |
} |
1095 |
< |
} |
1644 |
> |
}} |
1645 |
|
|
1646 |
< |
public void testThenCombine_sourceCancelled4() { |
1646 |
> |
/** |
1647 |
> |
* thenCombine result completes normally after normal completion |
1648 |
> |
* of sources |
1649 |
> |
*/ |
1650 |
> |
public void testThenCombine_normalCompletion() { |
1651 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1652 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1653 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
1654 |
< |
|
1652 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1653 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1654 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1655 |
> |
{ |
1656 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1657 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1658 |
< |
final SubtractFunction r = new SubtractFunction(); |
1658 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1659 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1660 |
|
|
1661 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1662 |
< |
g.complete(v1); |
1663 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1661 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1662 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1663 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1664 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1665 |
> |
|
1666 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1667 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1668 |
> |
assertTrue(fst.complete(w1)); |
1669 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1670 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1671 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1672 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1673 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1674 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1675 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1676 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1677 |
> |
assertTrue(snd.complete(w2)); |
1678 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1679 |
> |
|
1680 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1681 |
> |
checkCompletedNormally(h2, subtract(v1, v2)); |
1682 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1683 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1684 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1685 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1686 |
|
|
1687 |
< |
checkCompletedWithWrappedCancellationException(h); |
1688 |
< |
checkCancelled(f); |
1689 |
< |
assertFalse(r.ran()); |
1113 |
< |
checkCompletedNormally(g, v1); |
1114 |
< |
} |
1115 |
< |
} |
1687 |
> |
checkCompletedNormally(f, v1); |
1688 |
> |
checkCompletedNormally(g, v2); |
1689 |
> |
}} |
1690 |
|
|
1691 |
|
/** |
1692 |
< |
* thenAcceptBoth result completes normally after normal |
1693 |
< |
* completion of sources |
1692 |
> |
* thenCombine result completes exceptionally after exceptional |
1693 |
> |
* completion of either source |
1694 |
|
*/ |
1695 |
< |
public void testThenAcceptBoth_normalCompletion1() { |
1695 |
> |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
1696 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1697 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1698 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1699 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1700 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
1125 |
< |
|
1700 |
> |
{ |
1701 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1702 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1703 |
< |
final SubtractAction r = new SubtractAction(); |
1704 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1705 |
< |
|
1706 |
< |
f.complete(v1); |
1707 |
< |
checkIncomplete(h); |
1708 |
< |
assertFalse(r.ran()); |
1709 |
< |
g.complete(v2); |
1710 |
< |
|
1711 |
< |
checkCompletedNormally(h, null); |
1712 |
< |
assertEquals(r.value, subtract(v1, v2)); |
1713 |
< |
checkCompletedNormally(f, v1); |
1714 |
< |
checkCompletedNormally(g, v2); |
1715 |
< |
} |
1716 |
< |
} |
1703 |
> |
final CFException ex = new CFException(); |
1704 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1705 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1706 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1707 |
> |
|
1708 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1709 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1710 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1711 |
> |
() -> fst.completeExceptionally(ex) : |
1712 |
> |
() -> fst.complete(v1); |
1713 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1714 |
> |
() -> snd.complete(v1) : |
1715 |
> |
() -> snd.completeExceptionally(ex); |
1716 |
> |
|
1717 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1718 |
> |
assertTrue(complete1.call()); |
1719 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1720 |
> |
checkIncomplete(h1); |
1721 |
> |
checkIncomplete(h2); |
1722 |
> |
assertTrue(complete2.call()); |
1723 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1724 |
> |
|
1725 |
> |
checkCompletedWithWrappedException(h1, ex); |
1726 |
> |
checkCompletedWithWrappedException(h2, ex); |
1727 |
> |
checkCompletedWithWrappedException(h3, ex); |
1728 |
> |
r1.assertNotInvoked(); |
1729 |
> |
r2.assertNotInvoked(); |
1730 |
> |
r3.assertNotInvoked(); |
1731 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1732 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1733 |
> |
}} |
1734 |
|
|
1735 |
< |
public void testThenAcceptBoth_normalCompletion2() { |
1735 |
> |
/** |
1736 |
> |
* thenCombine result completes exceptionally if either source cancelled |
1737 |
> |
*/ |
1738 |
> |
public void testThenCombine_sourceCancelled() throws Throwable { |
1739 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1740 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1741 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1742 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1743 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1744 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
1147 |
< |
|
1744 |
> |
{ |
1745 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1746 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1747 |
< |
final SubtractAction r = new SubtractAction(); |
1748 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1747 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1748 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1749 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1750 |
> |
|
1751 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1752 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1753 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1754 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1755 |
> |
() -> fst.complete(v1); |
1756 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1757 |
> |
() -> snd.complete(v1) : |
1758 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1759 |
> |
|
1760 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1761 |
> |
assertTrue(complete1.call()); |
1762 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1763 |
> |
checkIncomplete(h1); |
1764 |
> |
checkIncomplete(h2); |
1765 |
> |
assertTrue(complete2.call()); |
1766 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1767 |
> |
|
1768 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1769 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1770 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1771 |
> |
r1.assertNotInvoked(); |
1772 |
> |
r2.assertNotInvoked(); |
1773 |
> |
r3.assertNotInvoked(); |
1774 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1775 |
> |
checkCancelled(failFirst ? fst : snd); |
1776 |
> |
}} |
1777 |
|
|
1778 |
< |
g.complete(v2); |
1779 |
< |
checkIncomplete(h); |
1780 |
< |
assertFalse(r.ran()); |
1781 |
< |
f.complete(v1); |
1157 |
< |
|
1158 |
< |
checkCompletedNormally(h, null); |
1159 |
< |
assertEquals(r.value, subtract(v1, v2)); |
1160 |
< |
checkCompletedNormally(f, v1); |
1161 |
< |
checkCompletedNormally(g, v2); |
1162 |
< |
} |
1163 |
< |
} |
1164 |
< |
|
1165 |
< |
public void testThenAcceptBoth_normalCompletion3() { |
1778 |
> |
/** |
1779 |
> |
* thenCombine result completes exceptionally if action does |
1780 |
> |
*/ |
1781 |
> |
public void testThenCombine_actionFailed() { |
1782 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1783 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1784 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1785 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
1786 |
< |
|
1785 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1786 |
> |
{ |
1787 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1788 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1789 |
< |
final SubtractAction r = new SubtractAction(); |
1790 |
< |
|
1791 |
< |
g.complete(v2); |
1792 |
< |
f.complete(v1); |
1793 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1794 |
< |
|
1795 |
< |
checkCompletedNormally(h, null); |
1796 |
< |
assertEquals(r.value, subtract(v1, v2)); |
1789 |
> |
final FailingBiFunction r1 = new FailingBiFunction(m); |
1790 |
> |
final FailingBiFunction r2 = new FailingBiFunction(m); |
1791 |
> |
final FailingBiFunction r3 = new FailingBiFunction(m); |
1792 |
> |
|
1793 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1794 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1795 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1796 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1797 |
> |
|
1798 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1799 |
> |
assertTrue(fst.complete(w1)); |
1800 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1801 |
> |
assertTrue(snd.complete(w2)); |
1802 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1803 |
> |
|
1804 |
> |
checkCompletedWithWrappedCFException(h1); |
1805 |
> |
checkCompletedWithWrappedCFException(h2); |
1806 |
> |
checkCompletedWithWrappedCFException(h3); |
1807 |
> |
r1.assertInvoked(); |
1808 |
> |
r2.assertInvoked(); |
1809 |
> |
r3.assertInvoked(); |
1810 |
|
checkCompletedNormally(f, v1); |
1811 |
|
checkCompletedNormally(g, v2); |
1812 |
< |
} |
1183 |
< |
} |
1812 |
> |
}} |
1813 |
|
|
1814 |
< |
public void testThenAcceptBoth_normalCompletion4() { |
1814 |
> |
/** |
1815 |
> |
* thenAcceptBoth result completes normally after normal |
1816 |
> |
* completion of sources |
1817 |
> |
*/ |
1818 |
> |
public void testThenAcceptBoth_normalCompletion() { |
1819 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1820 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1821 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1822 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
1823 |
< |
|
1822 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1823 |
> |
{ |
1824 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1825 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1826 |
< |
final SubtractAction r = new SubtractAction(); |
1827 |
< |
|
1828 |
< |
f.complete(v1); |
1829 |
< |
g.complete(v2); |
1830 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1831 |
< |
|
1832 |
< |
checkCompletedNormally(h, null); |
1833 |
< |
assertEquals(r.value, subtract(v1, v2)); |
1826 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1827 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1828 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1829 |
> |
|
1830 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1831 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1832 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1833 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1834 |
> |
|
1835 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1836 |
> |
assertTrue(fst.complete(w1)); |
1837 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1838 |
> |
checkIncomplete(h1); |
1839 |
> |
checkIncomplete(h2); |
1840 |
> |
r1.assertNotInvoked(); |
1841 |
> |
r2.assertNotInvoked(); |
1842 |
> |
assertTrue(snd.complete(w2)); |
1843 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1844 |
> |
|
1845 |
> |
checkCompletedNormally(h1, null); |
1846 |
> |
checkCompletedNormally(h2, null); |
1847 |
> |
checkCompletedNormally(h3, null); |
1848 |
> |
r1.assertValue(subtract(v1, v2)); |
1849 |
> |
r2.assertValue(subtract(v1, v2)); |
1850 |
> |
r3.assertValue(subtract(v1, v2)); |
1851 |
|
checkCompletedNormally(f, v1); |
1852 |
|
checkCompletedNormally(g, v2); |
1853 |
< |
} |
1203 |
< |
} |
1853 |
> |
}} |
1854 |
|
|
1855 |
|
/** |
1856 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1857 |
|
* completion of either source |
1858 |
|
*/ |
1859 |
< |
public void testThenAcceptBoth_exceptionalCompletion1() { |
1859 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1860 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1861 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
1862 |
< |
|
1861 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1862 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1863 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1864 |
> |
{ |
1865 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1866 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1215 |
– |
final SubtractAction r = new SubtractAction(); |
1216 |
– |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1867 |
|
final CFException ex = new CFException(); |
1868 |
+ |
final SubtractAction r1 = new SubtractAction(m); |
1869 |
+ |
final SubtractAction r2 = new SubtractAction(m); |
1870 |
+ |
final SubtractAction r3 = new SubtractAction(m); |
1871 |
+ |
|
1872 |
+ |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1873 |
+ |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1874 |
+ |
final Callable<Boolean> complete1 = failFirst ? |
1875 |
+ |
() -> fst.completeExceptionally(ex) : |
1876 |
+ |
() -> fst.complete(v1); |
1877 |
+ |
final Callable<Boolean> complete2 = failFirst ? |
1878 |
+ |
() -> snd.complete(v1) : |
1879 |
+ |
() -> snd.completeExceptionally(ex); |
1880 |
+ |
|
1881 |
+ |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1882 |
+ |
assertTrue(complete1.call()); |
1883 |
+ |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1884 |
+ |
checkIncomplete(h1); |
1885 |
+ |
checkIncomplete(h2); |
1886 |
+ |
assertTrue(complete2.call()); |
1887 |
+ |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1888 |
+ |
|
1889 |
+ |
checkCompletedWithWrappedException(h1, ex); |
1890 |
+ |
checkCompletedWithWrappedException(h2, ex); |
1891 |
+ |
checkCompletedWithWrappedException(h3, ex); |
1892 |
+ |
r1.assertNotInvoked(); |
1893 |
+ |
r2.assertNotInvoked(); |
1894 |
+ |
r3.assertNotInvoked(); |
1895 |
+ |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1896 |
+ |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1897 |
+ |
}} |
1898 |
|
|
1899 |
< |
f.completeExceptionally(ex); |
1900 |
< |
checkIncomplete(h); |
1901 |
< |
g.complete(v1); |
1902 |
< |
|
1223 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1224 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1225 |
< |
assertFalse(r.ran()); |
1226 |
< |
checkCompletedNormally(g, v1); |
1227 |
< |
} |
1228 |
< |
} |
1229 |
< |
|
1230 |
< |
public void testThenAcceptBoth_exceptionalCompletion2() { |
1899 |
> |
/** |
1900 |
> |
* thenAcceptBoth result completes exceptionally if either source cancelled |
1901 |
> |
*/ |
1902 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1903 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1904 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
1905 |
< |
|
1904 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1905 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1906 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1907 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1908 |
> |
{ |
1909 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1910 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1911 |
< |
final SubtractAction r = new SubtractAction(); |
1912 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1913 |
< |
final CFException ex = new CFException(); |
1914 |
< |
|
1915 |
< |
g.completeExceptionally(ex); |
1916 |
< |
checkIncomplete(h); |
1917 |
< |
f.complete(v1); |
1911 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1912 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1913 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1914 |
> |
|
1915 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1916 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1917 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1918 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1919 |
> |
() -> fst.complete(v1); |
1920 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1921 |
> |
() -> snd.complete(v1) : |
1922 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1923 |
> |
|
1924 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1925 |
> |
assertTrue(complete1.call()); |
1926 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1927 |
> |
checkIncomplete(h1); |
1928 |
> |
checkIncomplete(h2); |
1929 |
> |
assertTrue(complete2.call()); |
1930 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1931 |
> |
|
1932 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1933 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1934 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1935 |
> |
r1.assertNotInvoked(); |
1936 |
> |
r2.assertNotInvoked(); |
1937 |
> |
r3.assertNotInvoked(); |
1938 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1939 |
> |
checkCancelled(failFirst ? fst : snd); |
1940 |
> |
}} |
1941 |
|
|
1942 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1943 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1944 |
< |
assertFalse(r.ran()); |
1942 |
> |
/** |
1943 |
> |
* thenAcceptBoth result completes exceptionally if action does |
1944 |
> |
*/ |
1945 |
> |
public void testThenAcceptBoth_actionFailed() { |
1946 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1947 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1948 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1949 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1950 |
> |
{ |
1951 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1952 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1953 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1954 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1955 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1956 |
> |
|
1957 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1958 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1959 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1960 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1961 |
> |
|
1962 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1963 |
> |
assertTrue(fst.complete(w1)); |
1964 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1965 |
> |
assertTrue(snd.complete(w2)); |
1966 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1967 |
> |
|
1968 |
> |
checkCompletedWithWrappedCFException(h1); |
1969 |
> |
checkCompletedWithWrappedCFException(h2); |
1970 |
> |
checkCompletedWithWrappedCFException(h3); |
1971 |
> |
r1.assertInvoked(); |
1972 |
> |
r2.assertInvoked(); |
1973 |
> |
r3.assertInvoked(); |
1974 |
|
checkCompletedNormally(f, v1); |
1975 |
< |
} |
1976 |
< |
} |
1975 |
> |
checkCompletedNormally(g, v2); |
1976 |
> |
}} |
1977 |
|
|
1978 |
< |
public void testThenAcceptBoth_exceptionalCompletion3() { |
1978 |
> |
/** |
1979 |
> |
* runAfterBoth result completes normally after normal |
1980 |
> |
* completion of sources |
1981 |
> |
*/ |
1982 |
> |
public void testRunAfterBoth_normalCompletion() { |
1983 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1984 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
1985 |
< |
|
1984 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1985 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1986 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1987 |
> |
{ |
1988 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1989 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1990 |
< |
final SubtractAction r = new SubtractAction(); |
1991 |
< |
final CFException ex = new CFException(); |
1992 |
< |
|
1993 |
< |
g.completeExceptionally(ex); |
1994 |
< |
f.complete(v1); |
1995 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1996 |
< |
|
1997 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1998 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1999 |
< |
assertFalse(r.ran()); |
1990 |
> |
final Noop r1 = new Noop(m); |
1991 |
> |
final Noop r2 = new Noop(m); |
1992 |
> |
final Noop r3 = new Noop(m); |
1993 |
> |
|
1994 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1995 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1996 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1997 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1998 |
> |
|
1999 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2000 |
> |
assertTrue(fst.complete(w1)); |
2001 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2002 |
> |
checkIncomplete(h1); |
2003 |
> |
checkIncomplete(h2); |
2004 |
> |
r1.assertNotInvoked(); |
2005 |
> |
r2.assertNotInvoked(); |
2006 |
> |
assertTrue(snd.complete(w2)); |
2007 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2008 |
> |
|
2009 |
> |
checkCompletedNormally(h1, null); |
2010 |
> |
checkCompletedNormally(h2, null); |
2011 |
> |
checkCompletedNormally(h3, null); |
2012 |
> |
r1.assertInvoked(); |
2013 |
> |
r2.assertInvoked(); |
2014 |
> |
r3.assertInvoked(); |
2015 |
|
checkCompletedNormally(f, v1); |
2016 |
< |
} |
2017 |
< |
} |
2016 |
> |
checkCompletedNormally(g, v2); |
2017 |
> |
}} |
2018 |
|
|
2019 |
< |
public void testThenAcceptBoth_exceptionalCompletion4() { |
2019 |
> |
/** |
2020 |
> |
* runAfterBoth result completes exceptionally after exceptional |
2021 |
> |
* completion of either source |
2022 |
> |
*/ |
2023 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
2024 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2025 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
2026 |
< |
|
2025 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2026 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
2027 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2028 |
> |
{ |
2029 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2030 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1277 |
– |
final SubtractAction r = new SubtractAction(); |
2031 |
|
final CFException ex = new CFException(); |
2032 |
< |
|
2033 |
< |
f.completeExceptionally(ex); |
2034 |
< |
g.complete(v1); |
2035 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
2036 |
< |
|
2037 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2038 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2039 |
< |
assertFalse(r.ran()); |
2040 |
< |
checkCompletedNormally(g, v1); |
2041 |
< |
} |
2042 |
< |
} |
2032 |
> |
final Noop r1 = new Noop(m); |
2033 |
> |
final Noop r2 = new Noop(m); |
2034 |
> |
final Noop r3 = new Noop(m); |
2035 |
> |
|
2036 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2037 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2038 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2039 |
> |
() -> fst.completeExceptionally(ex) : |
2040 |
> |
() -> fst.complete(v1); |
2041 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2042 |
> |
() -> snd.complete(v1) : |
2043 |
> |
() -> snd.completeExceptionally(ex); |
2044 |
> |
|
2045 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2046 |
> |
assertTrue(complete1.call()); |
2047 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2048 |
> |
checkIncomplete(h1); |
2049 |
> |
checkIncomplete(h2); |
2050 |
> |
assertTrue(complete2.call()); |
2051 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2052 |
> |
|
2053 |
> |
checkCompletedWithWrappedException(h1, ex); |
2054 |
> |
checkCompletedWithWrappedException(h2, ex); |
2055 |
> |
checkCompletedWithWrappedException(h3, ex); |
2056 |
> |
r1.assertNotInvoked(); |
2057 |
> |
r2.assertNotInvoked(); |
2058 |
> |
r3.assertNotInvoked(); |
2059 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2060 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
2061 |
> |
}} |
2062 |
|
|
2063 |
|
/** |
2064 |
< |
* thenAcceptBoth result completes exceptionally if action does |
2064 |
> |
* runAfterBoth result completes exceptionally if either source cancelled |
2065 |
|
*/ |
2066 |
< |
public void testThenAcceptBoth_actionFailed1() { |
2066 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
2067 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2068 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2069 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2070 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
2071 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2072 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
1298 |
< |
|
2072 |
> |
{ |
2073 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2074 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2075 |
< |
final FailingBiConsumer r = new FailingBiConsumer(); |
2076 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
2075 |
> |
final Noop r1 = new Noop(m); |
2076 |
> |
final Noop r2 = new Noop(m); |
2077 |
> |
final Noop r3 = new Noop(m); |
2078 |
> |
|
2079 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2080 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2081 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2082 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
2083 |
> |
() -> fst.complete(v1); |
2084 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2085 |
> |
() -> snd.complete(v1) : |
2086 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
2087 |
> |
|
2088 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2089 |
> |
assertTrue(complete1.call()); |
2090 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2091 |
> |
checkIncomplete(h1); |
2092 |
> |
checkIncomplete(h2); |
2093 |
> |
assertTrue(complete2.call()); |
2094 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2095 |
> |
|
2096 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2097 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2098 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2099 |
> |
r1.assertNotInvoked(); |
2100 |
> |
r2.assertNotInvoked(); |
2101 |
> |
r3.assertNotInvoked(); |
2102 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2103 |
> |
checkCancelled(failFirst ? fst : snd); |
2104 |
> |
}} |
2105 |
|
|
2106 |
< |
f.complete(v1); |
2107 |
< |
checkIncomplete(h); |
2108 |
< |
g.complete(v2); |
2109 |
< |
|
2110 |
< |
checkCompletedWithWrappedCFException(h); |
2106 |
> |
/** |
2107 |
> |
* runAfterBoth result completes exceptionally if action does |
2108 |
> |
*/ |
2109 |
> |
public void testRunAfterBoth_actionFailed() { |
2110 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2111 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2112 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2113 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2114 |
> |
{ |
2115 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2116 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2117 |
> |
final FailingRunnable r1 = new FailingRunnable(m); |
2118 |
> |
final FailingRunnable r2 = new FailingRunnable(m); |
2119 |
> |
final FailingRunnable r3 = new FailingRunnable(m); |
2120 |
> |
|
2121 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2122 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2123 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2124 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2125 |
> |
|
2126 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2127 |
> |
assertTrue(fst.complete(w1)); |
2128 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2129 |
> |
assertTrue(snd.complete(w2)); |
2130 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2131 |
> |
|
2132 |
> |
checkCompletedWithWrappedCFException(h1); |
2133 |
> |
checkCompletedWithWrappedCFException(h2); |
2134 |
> |
checkCompletedWithWrappedCFException(h3); |
2135 |
> |
r1.assertInvoked(); |
2136 |
> |
r2.assertInvoked(); |
2137 |
> |
r3.assertInvoked(); |
2138 |
|
checkCompletedNormally(f, v1); |
2139 |
|
checkCompletedNormally(g, v2); |
2140 |
< |
} |
1312 |
< |
} |
2140 |
> |
}} |
2141 |
|
|
2142 |
< |
public void testThenAcceptBoth_actionFailed2() { |
2142 |
> |
/** |
2143 |
> |
* applyToEither result completes normally after normal completion |
2144 |
> |
* of either source |
2145 |
> |
*/ |
2146 |
> |
public void testApplyToEither_normalCompletion() { |
2147 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2148 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2149 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
2150 |
< |
|
2149 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2150 |
> |
{ |
2151 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2152 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2153 |
< |
final FailingBiConsumer r = new FailingBiConsumer(); |
2154 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
2153 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2154 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2155 |
|
|
2156 |
+ |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2157 |
+ |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2158 |
+ |
checkIncomplete(h0); |
2159 |
+ |
checkIncomplete(h1); |
2160 |
+ |
rs[0].assertNotInvoked(); |
2161 |
+ |
rs[1].assertNotInvoked(); |
2162 |
+ |
f.complete(v1); |
2163 |
+ |
checkCompletedNormally(h0, inc(v1)); |
2164 |
+ |
checkCompletedNormally(h1, inc(v1)); |
2165 |
+ |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2166 |
+ |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2167 |
+ |
checkCompletedNormally(h2, inc(v1)); |
2168 |
+ |
checkCompletedNormally(h3, inc(v1)); |
2169 |
|
g.complete(v2); |
1325 |
– |
checkIncomplete(h); |
1326 |
– |
f.complete(v1); |
2170 |
|
|
2171 |
< |
checkCompletedWithWrappedCFException(h); |
2171 |
> |
// unspecified behavior - both source completions available |
2172 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2173 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2174 |
> |
rs[4].assertValue(h4.join()); |
2175 |
> |
rs[5].assertValue(h5.join()); |
2176 |
> |
assertTrue(Objects.equals(inc(v1), h4.join()) || |
2177 |
> |
Objects.equals(inc(v2), h4.join())); |
2178 |
> |
assertTrue(Objects.equals(inc(v1), h5.join()) || |
2179 |
> |
Objects.equals(inc(v2), h5.join())); |
2180 |
> |
|
2181 |
|
checkCompletedNormally(f, v1); |
2182 |
|
checkCompletedNormally(g, v2); |
2183 |
< |
} |
2184 |
< |
} |
2183 |
> |
checkCompletedNormally(h0, inc(v1)); |
2184 |
> |
checkCompletedNormally(h1, inc(v1)); |
2185 |
> |
checkCompletedNormally(h2, inc(v1)); |
2186 |
> |
checkCompletedNormally(h3, inc(v1)); |
2187 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); |
2188 |
> |
}} |
2189 |
|
|
2190 |
|
/** |
2191 |
< |
* thenAcceptBoth result completes exceptionally if either source cancelled |
2191 |
> |
* applyToEither result completes exceptionally after exceptional |
2192 |
> |
* completion of either source |
2193 |
|
*/ |
2194 |
< |
public void testThenAcceptBoth_sourceCancelled1() { |
2194 |
> |
public void testApplyToEither_exceptionalCompletion() { |
2195 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2196 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2197 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
1341 |
< |
|
2196 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2197 |
> |
{ |
2198 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2199 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2200 |
< |
final SubtractAction r = new SubtractAction(); |
2201 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
2200 |
> |
final CFException ex = new CFException(); |
2201 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2202 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2203 |
|
|
2204 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2205 |
< |
checkIncomplete(h); |
2204 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2205 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2206 |
> |
checkIncomplete(h0); |
2207 |
> |
checkIncomplete(h1); |
2208 |
> |
rs[0].assertNotInvoked(); |
2209 |
> |
rs[1].assertNotInvoked(); |
2210 |
> |
f.completeExceptionally(ex); |
2211 |
> |
checkCompletedWithWrappedException(h0, ex); |
2212 |
> |
checkCompletedWithWrappedException(h1, ex); |
2213 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2214 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2215 |
> |
checkCompletedWithWrappedException(h2, ex); |
2216 |
> |
checkCompletedWithWrappedException(h3, ex); |
2217 |
|
g.complete(v1); |
2218 |
|
|
2219 |
< |
checkCompletedWithWrappedCancellationException(h); |
2220 |
< |
checkCancelled(f); |
2221 |
< |
assertFalse(r.ran()); |
2222 |
< |
checkCompletedNormally(g, v1); |
2219 |
> |
// unspecified behavior - both source completions available |
2220 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2221 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2222 |
> |
try { |
2223 |
> |
assertEquals(inc(v1), h4.join()); |
2224 |
> |
rs[4].assertValue(inc(v1)); |
2225 |
> |
} catch (CompletionException ok) { |
2226 |
> |
checkCompletedWithWrappedException(h4, ex); |
2227 |
> |
rs[4].assertNotInvoked(); |
2228 |
> |
} |
2229 |
> |
try { |
2230 |
> |
assertEquals(inc(v1), h5.join()); |
2231 |
> |
rs[5].assertValue(inc(v1)); |
2232 |
> |
} catch (CompletionException ok) { |
2233 |
> |
checkCompletedWithWrappedException(h5, ex); |
2234 |
> |
rs[5].assertNotInvoked(); |
2235 |
|
} |
1356 |
– |
} |
2236 |
|
|
2237 |
< |
public void testThenAcceptBoth_sourceCancelled2() { |
2238 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2239 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2240 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
2237 |
> |
checkCompletedExceptionally(f, ex); |
2238 |
> |
checkCompletedNormally(g, v1); |
2239 |
> |
checkCompletedWithWrappedException(h0, ex); |
2240 |
> |
checkCompletedWithWrappedException(h1, ex); |
2241 |
> |
checkCompletedWithWrappedException(h2, ex); |
2242 |
> |
checkCompletedWithWrappedException(h3, ex); |
2243 |
> |
checkCompletedWithWrappedException(h4, ex); |
2244 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2245 |
> |
}} |
2246 |
|
|
2247 |
+ |
public void testApplyToEither_exceptionalCompletion2() { |
2248 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
2249 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2250 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
2251 |
+ |
{ |
2252 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2253 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2254 |
< |
final SubtractAction r = new SubtractAction(); |
2255 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
2254 |
> |
final CFException ex = new CFException(); |
2255 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2256 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2257 |
|
|
2258 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2259 |
< |
checkIncomplete(h); |
2260 |
< |
f.complete(v1); |
2258 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2259 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2260 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2261 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2262 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2263 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2264 |
|
|
2265 |
< |
checkCompletedWithWrappedCancellationException(h); |
2266 |
< |
checkCancelled(g); |
2267 |
< |
assertFalse(r.ran()); |
2268 |
< |
checkCompletedNormally(f, v1); |
2265 |
> |
// unspecified behavior - both source completions available |
2266 |
> |
try { |
2267 |
> |
assertEquals(inc(v1), h0.join()); |
2268 |
> |
rs[0].assertValue(inc(v1)); |
2269 |
> |
} catch (CompletionException ok) { |
2270 |
> |
checkCompletedWithWrappedException(h0, ex); |
2271 |
> |
rs[0].assertNotInvoked(); |
2272 |
> |
} |
2273 |
> |
try { |
2274 |
> |
assertEquals(inc(v1), h1.join()); |
2275 |
> |
rs[1].assertValue(inc(v1)); |
2276 |
> |
} catch (CompletionException ok) { |
2277 |
> |
checkCompletedWithWrappedException(h1, ex); |
2278 |
> |
rs[1].assertNotInvoked(); |
2279 |
> |
} |
2280 |
> |
try { |
2281 |
> |
assertEquals(inc(v1), h2.join()); |
2282 |
> |
rs[2].assertValue(inc(v1)); |
2283 |
> |
} catch (CompletionException ok) { |
2284 |
> |
checkCompletedWithWrappedException(h2, ex); |
2285 |
> |
rs[2].assertNotInvoked(); |
2286 |
> |
} |
2287 |
> |
try { |
2288 |
> |
assertEquals(inc(v1), h3.join()); |
2289 |
> |
rs[3].assertValue(inc(v1)); |
2290 |
> |
} catch (CompletionException ok) { |
2291 |
> |
checkCompletedWithWrappedException(h3, ex); |
2292 |
> |
rs[3].assertNotInvoked(); |
2293 |
|
} |
1377 |
– |
} |
1378 |
– |
|
1379 |
– |
public void testThenAcceptBoth_sourceCancelled3() { |
1380 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1381 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1382 |
– |
for (Integer v1 : new Integer[] { 1, null }) { |
1383 |
– |
|
1384 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1385 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1386 |
– |
final SubtractAction r = new SubtractAction(); |
1387 |
– |
|
1388 |
– |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1389 |
– |
f.complete(v1); |
1390 |
– |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
2294 |
|
|
1392 |
– |
checkCompletedWithWrappedCancellationException(h); |
1393 |
– |
checkCancelled(g); |
1394 |
– |
assertFalse(r.ran()); |
2295 |
|
checkCompletedNormally(f, v1); |
2296 |
< |
} |
2297 |
< |
} |
2296 |
> |
checkCompletedExceptionally(g, ex); |
2297 |
> |
}} |
2298 |
|
|
2299 |
< |
public void testThenAcceptBoth_sourceCancelled4() { |
2299 |
> |
/** |
2300 |
> |
* applyToEither result completes exceptionally if either source cancelled |
2301 |
> |
*/ |
2302 |
> |
public void testApplyToEither_sourceCancelled() { |
2303 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2304 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2305 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
2306 |
< |
|
2305 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2306 |
> |
{ |
2307 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2308 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2309 |
< |
final SubtractAction r = new SubtractAction(); |
2309 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2310 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2311 |
|
|
2312 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2312 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2313 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2314 |
> |
checkIncomplete(h0); |
2315 |
> |
checkIncomplete(h1); |
2316 |
> |
rs[0].assertNotInvoked(); |
2317 |
> |
rs[1].assertNotInvoked(); |
2318 |
> |
f.cancel(mayInterruptIfRunning); |
2319 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2320 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2321 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2322 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2323 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2324 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2325 |
|
g.complete(v1); |
1410 |
– |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
2326 |
|
|
2327 |
< |
checkCompletedWithWrappedCancellationException(h); |
2327 |
> |
// unspecified behavior - both source completions available |
2328 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2329 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2330 |
> |
try { |
2331 |
> |
assertEquals(inc(v1), h4.join()); |
2332 |
> |
rs[4].assertValue(inc(v1)); |
2333 |
> |
} catch (CompletionException ok) { |
2334 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2335 |
> |
rs[4].assertNotInvoked(); |
2336 |
> |
} |
2337 |
> |
try { |
2338 |
> |
assertEquals(inc(v1), h5.join()); |
2339 |
> |
rs[5].assertValue(inc(v1)); |
2340 |
> |
} catch (CompletionException ok) { |
2341 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2342 |
> |
rs[5].assertNotInvoked(); |
2343 |
> |
} |
2344 |
> |
|
2345 |
|
checkCancelled(f); |
1414 |
– |
assertFalse(r.ran()); |
2346 |
|
checkCompletedNormally(g, v1); |
2347 |
< |
} |
2348 |
< |
} |
2347 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2348 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2349 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2350 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2351 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2352 |
> |
}} |
2353 |
|
|
2354 |
< |
/** |
1420 |
< |
* runAfterBoth result completes normally after normal |
1421 |
< |
* completion of sources |
1422 |
< |
*/ |
1423 |
< |
public void testRunAfterBoth_normalCompletion1() { |
2354 |
> |
public void testApplyToEither_sourceCancelled2() { |
2355 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2356 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2357 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2358 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2359 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
1427 |
< |
|
2359 |
> |
{ |
2360 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2361 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2362 |
< |
final Noop r = new Noop(); |
2363 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2362 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2363 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2364 |
|
|
2365 |
< |
f.complete(v1); |
2366 |
< |
checkIncomplete(h); |
2367 |
< |
assertFalse(r.ran); |
2368 |
< |
g.complete(v2); |
2365 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2366 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2367 |
> |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2368 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2369 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2370 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2371 |
|
|
2372 |
< |
checkCompletedNormally(h, null); |
2373 |
< |
assertTrue(r.ran); |
2374 |
< |
checkCompletedNormally(f, v1); |
2375 |
< |
checkCompletedNormally(g, v2); |
2372 |
> |
// unspecified behavior - both source completions available |
2373 |
> |
try { |
2374 |
> |
assertEquals(inc(v1), h0.join()); |
2375 |
> |
rs[0].assertValue(inc(v1)); |
2376 |
> |
} catch (CompletionException ok) { |
2377 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2378 |
> |
rs[0].assertNotInvoked(); |
2379 |
> |
} |
2380 |
> |
try { |
2381 |
> |
assertEquals(inc(v1), h1.join()); |
2382 |
> |
rs[1].assertValue(inc(v1)); |
2383 |
> |
} catch (CompletionException ok) { |
2384 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2385 |
> |
rs[1].assertNotInvoked(); |
2386 |
> |
} |
2387 |
> |
try { |
2388 |
> |
assertEquals(inc(v1), h2.join()); |
2389 |
> |
rs[2].assertValue(inc(v1)); |
2390 |
> |
} catch (CompletionException ok) { |
2391 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2392 |
> |
rs[2].assertNotInvoked(); |
2393 |
> |
} |
2394 |
> |
try { |
2395 |
> |
assertEquals(inc(v1), h3.join()); |
2396 |
> |
rs[3].assertValue(inc(v1)); |
2397 |
> |
} catch (CompletionException ok) { |
2398 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2399 |
> |
rs[3].assertNotInvoked(); |
2400 |
|
} |
1443 |
– |
} |
1444 |
– |
|
1445 |
– |
public void testRunAfterBoth_normalCompletion2() { |
1446 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1447 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1448 |
– |
for (Integer v2 : new Integer[] { 2, null }) { |
1449 |
– |
|
1450 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1451 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1452 |
– |
final Noop r = new Noop(); |
1453 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1454 |
– |
|
1455 |
– |
g.complete(v2); |
1456 |
– |
checkIncomplete(h); |
1457 |
– |
assertFalse(r.ran); |
1458 |
– |
f.complete(v1); |
2401 |
|
|
1460 |
– |
checkCompletedNormally(h, null); |
1461 |
– |
assertTrue(r.ran); |
2402 |
|
checkCompletedNormally(f, v1); |
2403 |
< |
checkCompletedNormally(g, v2); |
2404 |
< |
} |
1465 |
< |
} |
2403 |
> |
checkCancelled(g); |
2404 |
> |
}} |
2405 |
|
|
2406 |
< |
public void testRunAfterBoth_normalCompletion3() { |
2406 |
> |
/** |
2407 |
> |
* applyToEither result completes exceptionally if action does |
2408 |
> |
*/ |
2409 |
> |
public void testApplyToEither_actionFailed() { |
2410 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2411 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2412 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
2413 |
< |
|
2412 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2413 |
> |
{ |
2414 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2415 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2416 |
< |
final Noop r = new Noop(); |
2416 |
> |
final FailingFunction[] rs = new FailingFunction[6]; |
2417 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
2418 |
> |
|
2419 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2420 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2421 |
> |
f.complete(v1); |
2422 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2423 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2424 |
> |
checkCompletedWithWrappedCFException(h0); |
2425 |
> |
checkCompletedWithWrappedCFException(h1); |
2426 |
> |
checkCompletedWithWrappedCFException(h2); |
2427 |
> |
checkCompletedWithWrappedCFException(h3); |
2428 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2429 |
|
|
2430 |
|
g.complete(v2); |
1477 |
– |
f.complete(v1); |
1478 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2431 |
|
|
2432 |
< |
checkCompletedNormally(h, null); |
2433 |
< |
assertTrue(r.ran); |
2432 |
> |
// unspecified behavior - both source completions available |
2433 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2434 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2435 |
> |
|
2436 |
> |
checkCompletedWithWrappedCFException(h4); |
2437 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2438 |
> |
Objects.equals(v2, rs[4].value)); |
2439 |
> |
checkCompletedWithWrappedCFException(h5); |
2440 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2441 |
> |
Objects.equals(v2, rs[5].value)); |
2442 |
> |
|
2443 |
|
checkCompletedNormally(f, v1); |
2444 |
|
checkCompletedNormally(g, v2); |
2445 |
< |
} |
1485 |
< |
} |
2445 |
> |
}} |
2446 |
|
|
2447 |
< |
public void testRunAfterBoth_normalCompletion4() { |
2447 |
> |
/** |
2448 |
> |
* acceptEither result completes normally after normal completion |
2449 |
> |
* of either source |
2450 |
> |
*/ |
2451 |
> |
public void testAcceptEither_normalCompletion() { |
2452 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2453 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2454 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
2455 |
< |
|
2454 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2455 |
> |
{ |
2456 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2457 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2458 |
< |
final Noop r = new Noop(); |
2458 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2459 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2460 |
|
|
2461 |
< |
f.complete(v1); |
2461 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2462 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2463 |
> |
checkIncomplete(h0); |
2464 |
> |
checkIncomplete(h1); |
2465 |
> |
rs[0].assertNotInvoked(); |
2466 |
> |
rs[1].assertNotInvoked(); |
2467 |
> |
f.complete(v1); |
2468 |
> |
checkCompletedNormally(h0, null); |
2469 |
> |
checkCompletedNormally(h1, null); |
2470 |
> |
rs[0].assertValue(v1); |
2471 |
> |
rs[1].assertValue(v1); |
2472 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2473 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2474 |
> |
checkCompletedNormally(h2, null); |
2475 |
> |
checkCompletedNormally(h3, null); |
2476 |
> |
rs[2].assertValue(v1); |
2477 |
> |
rs[3].assertValue(v1); |
2478 |
|
g.complete(v2); |
1498 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2479 |
|
|
2480 |
< |
checkCompletedNormally(h, null); |
2481 |
< |
assertTrue(r.ran); |
2480 |
> |
// unspecified behavior - both source completions available |
2481 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2482 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2483 |
> |
checkCompletedNormally(h4, null); |
2484 |
> |
checkCompletedNormally(h5, null); |
2485 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2486 |
> |
Objects.equals(v2, rs[4].value)); |
2487 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2488 |
> |
Objects.equals(v2, rs[5].value)); |
2489 |
> |
|
2490 |
|
checkCompletedNormally(f, v1); |
2491 |
|
checkCompletedNormally(g, v2); |
2492 |
< |
} |
2493 |
< |
} |
2492 |
> |
checkCompletedNormally(h0, null); |
2493 |
> |
checkCompletedNormally(h1, null); |
2494 |
> |
checkCompletedNormally(h2, null); |
2495 |
> |
checkCompletedNormally(h3, null); |
2496 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2497 |
> |
}} |
2498 |
|
|
2499 |
|
/** |
2500 |
< |
* runAfterBoth result completes exceptionally after exceptional |
2500 |
> |
* acceptEither result completes exceptionally after exceptional |
2501 |
|
* completion of either source |
2502 |
|
*/ |
2503 |
< |
public void testRunAfterBoth_exceptionalCompletion1() { |
2503 |
> |
public void testAcceptEither_exceptionalCompletion() { |
2504 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2505 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
2506 |
< |
|
2505 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2506 |
> |
{ |
2507 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2508 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1517 |
– |
final Noop r = new Noop(); |
1518 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2509 |
|
final CFException ex = new CFException(); |
2510 |
+ |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2511 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2512 |
|
|
2513 |
+ |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2514 |
+ |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2515 |
+ |
checkIncomplete(h0); |
2516 |
+ |
checkIncomplete(h1); |
2517 |
+ |
rs[0].assertNotInvoked(); |
2518 |
+ |
rs[1].assertNotInvoked(); |
2519 |
|
f.completeExceptionally(ex); |
2520 |
< |
checkIncomplete(h); |
2520 |
> |
checkCompletedWithWrappedException(h0, ex); |
2521 |
> |
checkCompletedWithWrappedException(h1, ex); |
2522 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2523 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2524 |
> |
checkCompletedWithWrappedException(h2, ex); |
2525 |
> |
checkCompletedWithWrappedException(h3, ex); |
2526 |
> |
|
2527 |
|
g.complete(v1); |
2528 |
|
|
2529 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2530 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2531 |
< |
assertFalse(r.ran); |
2532 |
< |
checkCompletedNormally(g, v1); |
2529 |
> |
// unspecified behavior - both source completions available |
2530 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2531 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2532 |
> |
try { |
2533 |
> |
assertNull(h4.join()); |
2534 |
> |
rs[4].assertValue(v1); |
2535 |
> |
} catch (CompletionException ok) { |
2536 |
> |
checkCompletedWithWrappedException(h4, ex); |
2537 |
> |
rs[4].assertNotInvoked(); |
2538 |
> |
} |
2539 |
> |
try { |
2540 |
> |
assertNull(h5.join()); |
2541 |
> |
rs[5].assertValue(v1); |
2542 |
> |
} catch (CompletionException ok) { |
2543 |
> |
checkCompletedWithWrappedException(h5, ex); |
2544 |
> |
rs[5].assertNotInvoked(); |
2545 |
|
} |
1530 |
– |
} |
2546 |
|
|
2547 |
< |
public void testRunAfterBoth_exceptionalCompletion2() { |
2548 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2549 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
2547 |
> |
checkCompletedExceptionally(f, ex); |
2548 |
> |
checkCompletedNormally(g, v1); |
2549 |
> |
checkCompletedWithWrappedException(h0, ex); |
2550 |
> |
checkCompletedWithWrappedException(h1, ex); |
2551 |
> |
checkCompletedWithWrappedException(h2, ex); |
2552 |
> |
checkCompletedWithWrappedException(h3, ex); |
2553 |
> |
checkCompletedWithWrappedException(h4, ex); |
2554 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2555 |
> |
}} |
2556 |
|
|
2557 |
+ |
public void testAcceptEither_exceptionalCompletion2() { |
2558 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
2559 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2560 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
2561 |
+ |
{ |
2562 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2563 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1538 |
– |
final Noop r = new Noop(); |
1539 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2564 |
|
final CFException ex = new CFException(); |
2565 |
+ |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2566 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2567 |
|
|
2568 |
< |
g.completeExceptionally(ex); |
2569 |
< |
checkIncomplete(h); |
2570 |
< |
f.complete(v1); |
2568 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2569 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2570 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2571 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2572 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2573 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2574 |
|
|
2575 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2576 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2577 |
< |
assertFalse(r.ran); |
2578 |
< |
checkCompletedNormally(f, v1); |
2575 |
> |
// unspecified behavior - both source completions available |
2576 |
> |
try { |
2577 |
> |
assertEquals(null, h0.join()); |
2578 |
> |
rs[0].assertValue(v1); |
2579 |
> |
} catch (CompletionException ok) { |
2580 |
> |
checkCompletedWithWrappedException(h0, ex); |
2581 |
> |
rs[0].assertNotInvoked(); |
2582 |
> |
} |
2583 |
> |
try { |
2584 |
> |
assertEquals(null, h1.join()); |
2585 |
> |
rs[1].assertValue(v1); |
2586 |
> |
} catch (CompletionException ok) { |
2587 |
> |
checkCompletedWithWrappedException(h1, ex); |
2588 |
> |
rs[1].assertNotInvoked(); |
2589 |
> |
} |
2590 |
> |
try { |
2591 |
> |
assertEquals(null, h2.join()); |
2592 |
> |
rs[2].assertValue(v1); |
2593 |
> |
} catch (CompletionException ok) { |
2594 |
> |
checkCompletedWithWrappedException(h2, ex); |
2595 |
> |
rs[2].assertNotInvoked(); |
2596 |
> |
} |
2597 |
> |
try { |
2598 |
> |
assertEquals(null, h3.join()); |
2599 |
> |
rs[3].assertValue(v1); |
2600 |
> |
} catch (CompletionException ok) { |
2601 |
> |
checkCompletedWithWrappedException(h3, ex); |
2602 |
> |
rs[3].assertNotInvoked(); |
2603 |
|
} |
1551 |
– |
} |
1552 |
– |
|
1553 |
– |
public void testRunAfterBoth_exceptionalCompletion3() { |
1554 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1555 |
– |
for (Integer v1 : new Integer[] { 1, null }) { |
1556 |
– |
|
1557 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1558 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1559 |
– |
final Noop r = new Noop(); |
1560 |
– |
final CFException ex = new CFException(); |
1561 |
– |
|
1562 |
– |
g.completeExceptionally(ex); |
1563 |
– |
f.complete(v1); |
1564 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2604 |
|
|
1566 |
– |
checkCompletedWithWrappedCFException(h, ex); |
1567 |
– |
checkCompletedWithWrappedCFException(g, ex); |
1568 |
– |
assertFalse(r.ran); |
2605 |
|
checkCompletedNormally(f, v1); |
2606 |
< |
} |
2607 |
< |
} |
2606 |
> |
checkCompletedExceptionally(g, ex); |
2607 |
> |
}} |
2608 |
|
|
2609 |
< |
public void testRunAfterBoth_exceptionalCompletion4() { |
2609 |
> |
/** |
2610 |
> |
* acceptEither result completes exceptionally if either source cancelled |
2611 |
> |
*/ |
2612 |
> |
public void testAcceptEither_sourceCancelled() { |
2613 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2614 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
2615 |
< |
|
2614 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2615 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2616 |
> |
{ |
2617 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2618 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2619 |
< |
final Noop r = new Noop(); |
2620 |
< |
final CFException ex = new CFException(); |
2619 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2620 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2621 |
> |
|
2622 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2623 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2624 |
> |
checkIncomplete(h0); |
2625 |
> |
checkIncomplete(h1); |
2626 |
> |
rs[0].assertNotInvoked(); |
2627 |
> |
rs[1].assertNotInvoked(); |
2628 |
> |
f.cancel(mayInterruptIfRunning); |
2629 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2630 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2631 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2632 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2633 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2634 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2635 |
|
|
1582 |
– |
f.completeExceptionally(ex); |
2636 |
|
g.complete(v1); |
1584 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2637 |
|
|
2638 |
< |
checkCompletedWithWrappedCFException(h, ex); |
2639 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2640 |
< |
assertFalse(r.ran); |
2641 |
< |
checkCompletedNormally(g, v1); |
2638 |
> |
// unspecified behavior - both source completions available |
2639 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2640 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2641 |
> |
try { |
2642 |
> |
assertNull(h4.join()); |
2643 |
> |
rs[4].assertValue(v1); |
2644 |
> |
} catch (CompletionException ok) { |
2645 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2646 |
> |
rs[4].assertNotInvoked(); |
2647 |
> |
} |
2648 |
> |
try { |
2649 |
> |
assertNull(h5.join()); |
2650 |
> |
rs[5].assertValue(v1); |
2651 |
> |
} catch (CompletionException ok) { |
2652 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2653 |
> |
rs[5].assertNotInvoked(); |
2654 |
|
} |
2655 |
< |
} |
2655 |
> |
|
2656 |
> |
checkCancelled(f); |
2657 |
> |
checkCompletedNormally(g, v1); |
2658 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2659 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2660 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2661 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2662 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2663 |
> |
}} |
2664 |
|
|
2665 |
|
/** |
2666 |
< |
* runAfterBoth result completes exceptionally if action does |
2666 |
> |
* acceptEither result completes exceptionally if action does |
2667 |
|
*/ |
2668 |
< |
public void testRunAfterBoth_actionFailed1() { |
2668 |
> |
public void testAcceptEither_actionFailed() { |
2669 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2670 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2671 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
2672 |
< |
|
2671 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2672 |
> |
{ |
2673 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2674 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2675 |
< |
final FailingNoop r = new FailingNoop(); |
2676 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2675 |
> |
final FailingConsumer[] rs = new FailingConsumer[6]; |
2676 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
2677 |
> |
|
2678 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2679 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2680 |
> |
f.complete(v1); |
2681 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2682 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2683 |
> |
checkCompletedWithWrappedCFException(h0); |
2684 |
> |
checkCompletedWithWrappedCFException(h1); |
2685 |
> |
checkCompletedWithWrappedCFException(h2); |
2686 |
> |
checkCompletedWithWrappedCFException(h3); |
2687 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2688 |
|
|
1606 |
– |
f.complete(v1); |
1607 |
– |
checkIncomplete(h); |
2689 |
|
g.complete(v2); |
2690 |
|
|
2691 |
< |
checkCompletedWithWrappedCFException(h); |
2691 |
> |
// unspecified behavior - both source completions available |
2692 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2693 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2694 |
> |
|
2695 |
> |
checkCompletedWithWrappedCFException(h4); |
2696 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2697 |
> |
Objects.equals(v2, rs[4].value)); |
2698 |
> |
checkCompletedWithWrappedCFException(h5); |
2699 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2700 |
> |
Objects.equals(v2, rs[5].value)); |
2701 |
> |
|
2702 |
|
checkCompletedNormally(f, v1); |
2703 |
|
checkCompletedNormally(g, v2); |
2704 |
< |
} |
1614 |
< |
} |
2704 |
> |
}} |
2705 |
|
|
2706 |
< |
public void testRunAfterBoth_actionFailed2() { |
2706 |
> |
/** |
2707 |
> |
* runAfterEither result completes normally after normal completion |
2708 |
> |
* of either source |
2709 |
> |
*/ |
2710 |
> |
public void testRunAfterEither_normalCompletion() { |
2711 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2712 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2713 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
2714 |
< |
|
2713 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2714 |
> |
{ |
2715 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2716 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2717 |
< |
final FailingNoop r = new FailingNoop(); |
2718 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2717 |
> |
final Noop[] rs = new Noop[6]; |
2718 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2719 |
> |
|
2720 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2721 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2722 |
> |
checkIncomplete(h0); |
2723 |
> |
checkIncomplete(h1); |
2724 |
> |
rs[0].assertNotInvoked(); |
2725 |
> |
rs[1].assertNotInvoked(); |
2726 |
> |
f.complete(v1); |
2727 |
> |
checkCompletedNormally(h0, null); |
2728 |
> |
checkCompletedNormally(h1, null); |
2729 |
> |
rs[0].assertInvoked(); |
2730 |
> |
rs[1].assertInvoked(); |
2731 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2732 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2733 |
> |
checkCompletedNormally(h2, null); |
2734 |
> |
checkCompletedNormally(h3, null); |
2735 |
> |
rs[2].assertInvoked(); |
2736 |
> |
rs[3].assertInvoked(); |
2737 |
|
|
2738 |
|
g.complete(v2); |
1627 |
– |
checkIncomplete(h); |
1628 |
– |
f.complete(v1); |
2739 |
|
|
2740 |
< |
checkCompletedWithWrappedCFException(h); |
2740 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2741 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2742 |
> |
|
2743 |
|
checkCompletedNormally(f, v1); |
2744 |
|
checkCompletedNormally(g, v2); |
2745 |
< |
} |
2746 |
< |
} |
2745 |
> |
checkCompletedNormally(h0, null); |
2746 |
> |
checkCompletedNormally(h1, null); |
2747 |
> |
checkCompletedNormally(h2, null); |
2748 |
> |
checkCompletedNormally(h3, null); |
2749 |
> |
checkCompletedNormally(h4, null); |
2750 |
> |
checkCompletedNormally(h5, null); |
2751 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2752 |
> |
}} |
2753 |
|
|
2754 |
|
/** |
2755 |
< |
* runAfterBoth result completes exceptionally if either source cancelled |
2755 |
> |
* runAfterEither result completes exceptionally after exceptional |
2756 |
> |
* completion of either source |
2757 |
|
*/ |
2758 |
< |
public void testRunAfterBoth_sourceCancelled1() { |
2758 |
> |
public void testRunAfterEither_exceptionalCompletion() { |
2759 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2760 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2761 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
1643 |
< |
|
2760 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2761 |
> |
{ |
2762 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2763 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2764 |
< |
final Noop r = new Noop(); |
2765 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2764 |
> |
final CFException ex = new CFException(); |
2765 |
> |
final Noop[] rs = new Noop[6]; |
2766 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2767 |
|
|
2768 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2769 |
< |
checkIncomplete(h); |
2770 |
< |
g.complete(v1); |
2768 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2769 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2770 |
> |
checkIncomplete(h0); |
2771 |
> |
checkIncomplete(h1); |
2772 |
> |
rs[0].assertNotInvoked(); |
2773 |
> |
rs[1].assertNotInvoked(); |
2774 |
> |
assertTrue(f.completeExceptionally(ex)); |
2775 |
> |
checkCompletedWithWrappedException(h0, ex); |
2776 |
> |
checkCompletedWithWrappedException(h1, ex); |
2777 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2778 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2779 |
> |
checkCompletedWithWrappedException(h2, ex); |
2780 |
> |
checkCompletedWithWrappedException(h3, ex); |
2781 |
> |
|
2782 |
> |
assertTrue(g.complete(v1)); |
2783 |
> |
|
2784 |
> |
// unspecified behavior - both source completions available |
2785 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2786 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2787 |
> |
try { |
2788 |
> |
assertNull(h4.join()); |
2789 |
> |
rs[4].assertInvoked(); |
2790 |
> |
} catch (CompletionException ok) { |
2791 |
> |
checkCompletedWithWrappedException(h4, ex); |
2792 |
> |
rs[4].assertNotInvoked(); |
2793 |
> |
} |
2794 |
> |
try { |
2795 |
> |
assertNull(h5.join()); |
2796 |
> |
rs[5].assertInvoked(); |
2797 |
> |
} catch (CompletionException ok) { |
2798 |
> |
checkCompletedWithWrappedException(h5, ex); |
2799 |
> |
rs[5].assertNotInvoked(); |
2800 |
> |
} |
2801 |
|
|
2802 |
< |
checkCompletedWithWrappedCancellationException(h); |
1654 |
< |
checkCancelled(f); |
1655 |
< |
assertFalse(r.ran); |
2802 |
> |
checkCompletedExceptionally(f, ex); |
2803 |
|
checkCompletedNormally(g, v1); |
2804 |
< |
} |
2805 |
< |
} |
2804 |
> |
checkCompletedWithWrappedException(h0, ex); |
2805 |
> |
checkCompletedWithWrappedException(h1, ex); |
2806 |
> |
checkCompletedWithWrappedException(h2, ex); |
2807 |
> |
checkCompletedWithWrappedException(h3, ex); |
2808 |
> |
checkCompletedWithWrappedException(h4, ex); |
2809 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2810 |
> |
}} |
2811 |
|
|
2812 |
< |
public void testRunAfterBoth_sourceCancelled2() { |
2812 |
> |
public void testRunAfterEither_exceptionalCompletion2() { |
2813 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2814 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2815 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
2816 |
< |
|
2814 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2815 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2816 |
> |
{ |
2817 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2818 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2819 |
< |
final Noop r = new Noop(); |
2820 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2819 |
> |
final CFException ex = new CFException(); |
2820 |
> |
final Noop[] rs = new Noop[6]; |
2821 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2822 |
|
|
2823 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2824 |
< |
checkIncomplete(h); |
2825 |
< |
f.complete(v1); |
2823 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2824 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2825 |
> |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2826 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2827 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2828 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2829 |
|
|
2830 |
< |
checkCompletedWithWrappedCancellationException(h); |
2831 |
< |
checkCancelled(g); |
2832 |
< |
assertFalse(r.ran); |
2833 |
< |
checkCompletedNormally(f, v1); |
2830 |
> |
// unspecified behavior - both source completions available |
2831 |
> |
try { |
2832 |
> |
assertEquals(null, h0.join()); |
2833 |
> |
rs[0].assertInvoked(); |
2834 |
> |
} catch (CompletionException ok) { |
2835 |
> |
checkCompletedWithWrappedException(h0, ex); |
2836 |
> |
rs[0].assertNotInvoked(); |
2837 |
> |
} |
2838 |
> |
try { |
2839 |
> |
assertEquals(null, h1.join()); |
2840 |
> |
rs[1].assertInvoked(); |
2841 |
> |
} catch (CompletionException ok) { |
2842 |
> |
checkCompletedWithWrappedException(h1, ex); |
2843 |
> |
rs[1].assertNotInvoked(); |
2844 |
> |
} |
2845 |
> |
try { |
2846 |
> |
assertEquals(null, h2.join()); |
2847 |
> |
rs[2].assertInvoked(); |
2848 |
> |
} catch (CompletionException ok) { |
2849 |
> |
checkCompletedWithWrappedException(h2, ex); |
2850 |
> |
rs[2].assertNotInvoked(); |
2851 |
> |
} |
2852 |
> |
try { |
2853 |
> |
assertEquals(null, h3.join()); |
2854 |
> |
rs[3].assertInvoked(); |
2855 |
> |
} catch (CompletionException ok) { |
2856 |
> |
checkCompletedWithWrappedException(h3, ex); |
2857 |
> |
rs[3].assertNotInvoked(); |
2858 |
|
} |
1679 |
– |
} |
1680 |
– |
|
1681 |
– |
public void testRunAfterBoth_sourceCancelled3() { |
1682 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1683 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1684 |
– |
for (Integer v1 : new Integer[] { 1, null }) { |
1685 |
– |
|
1686 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1687 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1688 |
– |
final Noop r = new Noop(); |
1689 |
– |
|
1690 |
– |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1691 |
– |
f.complete(v1); |
1692 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2859 |
|
|
1694 |
– |
checkCompletedWithWrappedCancellationException(h); |
1695 |
– |
checkCancelled(g); |
1696 |
– |
assertFalse(r.ran); |
2860 |
|
checkCompletedNormally(f, v1); |
2861 |
< |
} |
2862 |
< |
} |
2861 |
> |
checkCompletedExceptionally(g, ex); |
2862 |
> |
}} |
2863 |
|
|
2864 |
< |
public void testRunAfterBoth_sourceCancelled4() { |
2864 |
> |
/** |
2865 |
> |
* runAfterEither result completes exceptionally if either source cancelled |
2866 |
> |
*/ |
2867 |
> |
public void testRunAfterEither_sourceCancelled() { |
2868 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2869 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2870 |
< |
for (Integer v1 : new Integer[] { 1, null }) { |
2871 |
< |
|
2870 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2871 |
> |
{ |
2872 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2873 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2874 |
< |
final Noop r = new Noop(); |
2874 |
> |
final Noop[] rs = new Noop[6]; |
2875 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2876 |
|
|
2877 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2878 |
< |
g.complete(v1); |
2879 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2877 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2878 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2879 |
> |
checkIncomplete(h0); |
2880 |
> |
checkIncomplete(h1); |
2881 |
> |
rs[0].assertNotInvoked(); |
2882 |
> |
rs[1].assertNotInvoked(); |
2883 |
> |
f.cancel(mayInterruptIfRunning); |
2884 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2885 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2886 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2887 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2888 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2889 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2890 |
> |
|
2891 |
> |
assertTrue(g.complete(v1)); |
2892 |
> |
|
2893 |
> |
// unspecified behavior - both source completions available |
2894 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2895 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2896 |
> |
try { |
2897 |
> |
assertNull(h4.join()); |
2898 |
> |
rs[4].assertInvoked(); |
2899 |
> |
} catch (CompletionException ok) { |
2900 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2901 |
> |
rs[4].assertNotInvoked(); |
2902 |
> |
} |
2903 |
> |
try { |
2904 |
> |
assertNull(h5.join()); |
2905 |
> |
rs[5].assertInvoked(); |
2906 |
> |
} catch (CompletionException ok) { |
2907 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2908 |
> |
rs[5].assertNotInvoked(); |
2909 |
> |
} |
2910 |
|
|
1714 |
– |
checkCompletedWithWrappedCancellationException(h); |
2911 |
|
checkCancelled(f); |
1716 |
– |
assertFalse(r.ran); |
2912 |
|
checkCompletedNormally(g, v1); |
2913 |
< |
} |
2914 |
< |
} |
2915 |
< |
|
2916 |
< |
/** |
2917 |
< |
* applyToEither result completes normally after normal completion |
2918 |
< |
* of either source |
1724 |
< |
*/ |
1725 |
< |
public void testApplyToEither() { |
1726 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1727 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1728 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
1729 |
< |
f.complete(one); |
1730 |
< |
checkCompletedNormally(g, two); |
1731 |
< |
f2.complete(one); |
1732 |
< |
checkCompletedNormally(g, two); |
1733 |
< |
|
1734 |
< |
f = new CompletableFuture<>(); |
1735 |
< |
f.complete(one); |
1736 |
< |
f2 = new CompletableFuture<>(); |
1737 |
< |
g = f.applyToEither(f2, inc); |
1738 |
< |
checkCompletedNormally(g, two); |
1739 |
< |
} |
1740 |
< |
|
1741 |
< |
/** |
1742 |
< |
* applyToEither result completes exceptionally after exceptional |
1743 |
< |
* completion of either source |
1744 |
< |
*/ |
1745 |
< |
public void testApplyToEither2() { |
1746 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1747 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1748 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
1749 |
< |
f.completeExceptionally(new CFException()); |
1750 |
< |
f2.complete(one); |
1751 |
< |
checkCompletedWithWrappedCFException(g); |
1752 |
< |
|
1753 |
< |
f = new CompletableFuture<>(); |
1754 |
< |
f2 = new CompletableFuture<>(); |
1755 |
< |
f2.completeExceptionally(new CFException()); |
1756 |
< |
g = f.applyToEither(f2, inc); |
1757 |
< |
checkCompletedWithWrappedCFException(g); |
1758 |
< |
} |
1759 |
< |
|
1760 |
< |
/** |
1761 |
< |
* applyToEither result completes exceptionally if action does |
1762 |
< |
*/ |
1763 |
< |
public void testApplyToEither3() { |
1764 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1765 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1766 |
< |
FailingFunction r = new FailingFunction(); |
1767 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, r); |
1768 |
< |
f2.complete(two); |
1769 |
< |
checkCompletedWithWrappedCFException(g); |
1770 |
< |
} |
1771 |
< |
|
1772 |
< |
/** |
1773 |
< |
* applyToEither result completes exceptionally if either source cancelled |
1774 |
< |
*/ |
1775 |
< |
public void testApplyToEither4() { |
1776 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1777 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1778 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
1779 |
< |
assertTrue(f.cancel(true)); |
1780 |
< |
checkCompletedWithWrappedCancellationException(g); |
1781 |
< |
f = new CompletableFuture<>(); |
1782 |
< |
f2 = new CompletableFuture<>(); |
1783 |
< |
assertTrue(f2.cancel(true)); |
1784 |
< |
checkCompletedWithWrappedCancellationException(g); |
1785 |
< |
} |
1786 |
< |
|
1787 |
< |
/** |
1788 |
< |
* acceptEither result completes normally after normal completion |
1789 |
< |
* of either source |
1790 |
< |
*/ |
1791 |
< |
public void testAcceptEither() { |
1792 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1793 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1794 |
< |
IncAction r = new IncAction(); |
1795 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
1796 |
< |
f.complete(one); |
1797 |
< |
checkCompletedNormally(g, null); |
1798 |
< |
f2.complete(one); |
1799 |
< |
checkCompletedNormally(g, null); |
1800 |
< |
assertEquals(r.value, 2); |
1801 |
< |
|
1802 |
< |
r = new IncAction(); |
1803 |
< |
f = new CompletableFuture<>(); |
1804 |
< |
f.complete(one); |
1805 |
< |
f2 = new CompletableFuture<>(); |
1806 |
< |
g = f.acceptEither(f2, r); |
1807 |
< |
checkCompletedNormally(g, null); |
1808 |
< |
assertEquals(r.value, 2); |
1809 |
< |
} |
1810 |
< |
|
1811 |
< |
/** |
1812 |
< |
* acceptEither result completes exceptionally after exceptional |
1813 |
< |
* completion of either source |
1814 |
< |
*/ |
1815 |
< |
public void testAcceptEither2() { |
1816 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1817 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1818 |
< |
IncAction r = new IncAction(); |
1819 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
1820 |
< |
f.completeExceptionally(new CFException()); |
1821 |
< |
f2.complete(one); |
1822 |
< |
checkCompletedWithWrappedCFException(g); |
1823 |
< |
|
1824 |
< |
r = new IncAction(); |
1825 |
< |
f = new CompletableFuture<>(); |
1826 |
< |
f2 = new CompletableFuture<>(); |
1827 |
< |
f2.completeExceptionally(new CFException()); |
1828 |
< |
g = f.acceptEither(f2, r); |
1829 |
< |
checkCompletedWithWrappedCFException(g); |
1830 |
< |
} |
1831 |
< |
|
1832 |
< |
/** |
1833 |
< |
* acceptEither result completes exceptionally if action does |
1834 |
< |
*/ |
1835 |
< |
public void testAcceptEither3() { |
1836 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1837 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1838 |
< |
FailingConsumer r = new FailingConsumer(); |
1839 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
1840 |
< |
f2.complete(two); |
1841 |
< |
checkCompletedWithWrappedCFException(g); |
1842 |
< |
} |
1843 |
< |
|
1844 |
< |
/** |
1845 |
< |
* acceptEither result completes exceptionally if either source cancelled |
1846 |
< |
*/ |
1847 |
< |
public void testAcceptEither4() { |
1848 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1849 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1850 |
< |
IncAction r = new IncAction(); |
1851 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
1852 |
< |
assertTrue(f.cancel(true)); |
1853 |
< |
checkCompletedWithWrappedCancellationException(g); |
1854 |
< |
f = new CompletableFuture<>(); |
1855 |
< |
f2 = new CompletableFuture<>(); |
1856 |
< |
assertTrue(f2.cancel(true)); |
1857 |
< |
checkCompletedWithWrappedCancellationException(g); |
1858 |
< |
} |
1859 |
< |
|
1860 |
< |
/** |
1861 |
< |
* runAfterEither result completes normally after normal completion |
1862 |
< |
* of either source |
1863 |
< |
*/ |
1864 |
< |
public void testRunAfterEither() { |
1865 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1866 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1867 |
< |
Noop r = new Noop(); |
1868 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
1869 |
< |
f.complete(one); |
1870 |
< |
checkCompletedNormally(g, null); |
1871 |
< |
f2.complete(one); |
1872 |
< |
checkCompletedNormally(g, null); |
1873 |
< |
assertTrue(r.ran); |
1874 |
< |
|
1875 |
< |
r = new Noop(); |
1876 |
< |
f = new CompletableFuture<>(); |
1877 |
< |
f.complete(one); |
1878 |
< |
f2 = new CompletableFuture<>(); |
1879 |
< |
g = f.runAfterEither(f2, r); |
1880 |
< |
checkCompletedNormally(g, null); |
1881 |
< |
assertTrue(r.ran); |
1882 |
< |
} |
1883 |
< |
|
1884 |
< |
/** |
1885 |
< |
* runAfterEither result completes exceptionally after exceptional |
1886 |
< |
* completion of either source |
1887 |
< |
*/ |
1888 |
< |
public void testRunAfterEither2() { |
1889 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1890 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1891 |
< |
Noop r = new Noop(); |
1892 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
1893 |
< |
f.completeExceptionally(new CFException()); |
1894 |
< |
f2.complete(one); |
1895 |
< |
checkCompletedWithWrappedCFException(g); |
1896 |
< |
|
1897 |
< |
r = new Noop(); |
1898 |
< |
f = new CompletableFuture<>(); |
1899 |
< |
f2 = new CompletableFuture<>(); |
1900 |
< |
f2.completeExceptionally(new CFException()); |
1901 |
< |
g = f.runAfterEither(f2, r); |
1902 |
< |
checkCompletedWithWrappedCFException(g); |
1903 |
< |
} |
2913 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2914 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2915 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2916 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2917 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2918 |
> |
}} |
2919 |
|
|
2920 |
|
/** |
2921 |
|
* runAfterEither result completes exceptionally if action does |
2922 |
|
*/ |
2923 |
< |
public void testRunAfterEither3() { |
2924 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2925 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2926 |
< |
FailingNoop r = new FailingNoop(); |
2927 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
2928 |
< |
f2.complete(two); |
2929 |
< |
checkCompletedWithWrappedCFException(g); |
2930 |
< |
} |
2923 |
> |
public void testRunAfterEither_actionFailed() { |
2924 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2925 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2926 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2927 |
> |
{ |
2928 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2929 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2930 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
2931 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
2932 |
|
|
2933 |
< |
/** |
2934 |
< |
* runAfterEither result completes exceptionally if either source cancelled |
2935 |
< |
*/ |
2936 |
< |
public void testRunAfterEither4() { |
2937 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2938 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2939 |
< |
Noop r = new Noop(); |
2940 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
2941 |
< |
assertTrue(f.cancel(true)); |
2942 |
< |
checkCompletedWithWrappedCancellationException(g); |
2943 |
< |
f = new CompletableFuture<>(); |
2944 |
< |
f2 = new CompletableFuture<>(); |
2945 |
< |
assertTrue(f2.cancel(true)); |
2946 |
< |
checkCompletedWithWrappedCancellationException(g); |
2947 |
< |
} |
2933 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2934 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2935 |
> |
assertTrue(f.complete(v1)); |
2936 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2937 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2938 |
> |
checkCompletedWithWrappedCFException(h0); |
2939 |
> |
checkCompletedWithWrappedCFException(h1); |
2940 |
> |
checkCompletedWithWrappedCFException(h2); |
2941 |
> |
checkCompletedWithWrappedCFException(h3); |
2942 |
> |
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2943 |
> |
assertTrue(g.complete(v2)); |
2944 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2945 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2946 |
> |
checkCompletedWithWrappedCFException(h4); |
2947 |
> |
checkCompletedWithWrappedCFException(h5); |
2948 |
> |
|
2949 |
> |
checkCompletedNormally(f, v1); |
2950 |
> |
checkCompletedNormally(g, v2); |
2951 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2952 |
> |
}} |
2953 |
|
|
2954 |
|
/** |
2955 |
|
* thenCompose result completes normally after normal completion of source |
2956 |
|
*/ |
2957 |
< |
public void testThenCompose() { |
2958 |
< |
CompletableFuture<Integer> f, g; |
2959 |
< |
CompletableFutureInc r; |
2960 |
< |
|
2961 |
< |
f = new CompletableFuture<>(); |
2962 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2963 |
< |
f.complete(one); |
2964 |
< |
checkCompletedNormally(g, two); |
2965 |
< |
assertTrue(r.ran); |
2957 |
> |
public void testThenCompose_normalCompletion() { |
2958 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2959 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2960 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2961 |
> |
{ |
2962 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2963 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2964 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2965 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2966 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2967 |
|
|
2968 |
< |
f = new CompletableFuture<>(); |
2969 |
< |
f.complete(one); |
2970 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2971 |
< |
checkCompletedNormally(g, two); |
1950 |
< |
assertTrue(r.ran); |
1951 |
< |
} |
2968 |
> |
checkCompletedNormally(g, inc(v1)); |
2969 |
> |
checkCompletedNormally(f, v1); |
2970 |
> |
r.assertValue(v1); |
2971 |
> |
}} |
2972 |
|
|
2973 |
|
/** |
2974 |
|
* thenCompose result completes exceptionally after exceptional |
2975 |
|
* completion of source |
2976 |
|
*/ |
2977 |
< |
public void testThenCompose2() { |
2978 |
< |
CompletableFuture<Integer> f, g; |
2979 |
< |
CompletableFutureInc r; |
2980 |
< |
|
2981 |
< |
f = new CompletableFuture<>(); |
2982 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2983 |
< |
f.completeExceptionally(new CFException()); |
2984 |
< |
checkCompletedWithWrappedCFException(g); |
2985 |
< |
|
2986 |
< |
f = new CompletableFuture<>(); |
2987 |
< |
f.completeExceptionally(new CFException()); |
2988 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2989 |
< |
checkCompletedWithWrappedCFException(g); |
2990 |
< |
} |
2977 |
> |
public void testThenCompose_exceptionalCompletion() { |
2978 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2979 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2980 |
> |
{ |
2981 |
> |
final CFException ex = new CFException(); |
2982 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2983 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2984 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
2985 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2986 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
2987 |
> |
|
2988 |
> |
checkCompletedWithWrappedException(g, ex); |
2989 |
> |
checkCompletedExceptionally(f, ex); |
2990 |
> |
r.assertNotInvoked(); |
2991 |
> |
}} |
2992 |
|
|
2993 |
|
/** |
2994 |
|
* thenCompose result completes exceptionally if action does |
2995 |
|
*/ |
2996 |
< |
public void testThenCompose3() { |
2997 |
< |
CompletableFuture<Integer> f, g; |
2998 |
< |
FailingCompletableFutureFunction r; |
2999 |
< |
|
3000 |
< |
f = new CompletableFuture<>(); |
3001 |
< |
g = f.thenCompose(r = new FailingCompletableFutureFunction()); |
3002 |
< |
f.complete(one); |
3003 |
< |
checkCompletedWithWrappedCFException(g); |
2996 |
> |
public void testThenCompose_actionFailed() { |
2997 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2998 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2999 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3000 |
> |
{ |
3001 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3002 |
> |
final FailingCompletableFutureFunction r |
3003 |
> |
= new FailingCompletableFutureFunction(m); |
3004 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
3005 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
3006 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
3007 |
|
|
1984 |
– |
f = new CompletableFuture<>(); |
1985 |
– |
f.complete(one); |
1986 |
– |
g = f.thenCompose(r = new FailingCompletableFutureFunction()); |
3008 |
|
checkCompletedWithWrappedCFException(g); |
3009 |
< |
} |
3009 |
> |
checkCompletedNormally(f, v1); |
3010 |
> |
}} |
3011 |
|
|
3012 |
|
/** |
3013 |
|
* thenCompose result completes exceptionally if source cancelled |
3014 |
|
*/ |
3015 |
< |
public void testThenCompose4() { |
3016 |
< |
CompletableFuture<Integer> f, g; |
3017 |
< |
CompletableFutureInc r; |
3018 |
< |
|
3019 |
< |
f = new CompletableFuture<>(); |
3020 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
3021 |
< |
assertTrue(f.cancel(true)); |
3022 |
< |
checkCompletedWithWrappedCancellationException(g); |
3023 |
< |
|
3024 |
< |
f = new CompletableFuture<>(); |
3025 |
< |
assertTrue(f.cancel(true)); |
3026 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
3027 |
< |
checkCompletedWithWrappedCancellationException(g); |
2006 |
< |
} |
2007 |
< |
|
2008 |
< |
// asyncs |
2009 |
< |
|
2010 |
< |
/** |
2011 |
< |
* thenRunAsync result completes normally after normal completion of source |
2012 |
< |
*/ |
2013 |
< |
public void testThenRunAsync() { |
2014 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2015 |
< |
Noop r = new Noop(); |
2016 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
2017 |
< |
f.complete(null); |
2018 |
< |
checkCompletedNormally(g, null); |
2019 |
< |
|
2020 |
< |
// reordered version |
2021 |
< |
f = new CompletableFuture<>(); |
2022 |
< |
f.complete(null); |
2023 |
< |
r = new Noop(); |
2024 |
< |
g = f.thenRunAsync(r); |
2025 |
< |
checkCompletedNormally(g, null); |
2026 |
< |
} |
2027 |
< |
|
2028 |
< |
/** |
2029 |
< |
* thenRunAsync result completes exceptionally after exceptional |
2030 |
< |
* completion of source |
2031 |
< |
*/ |
2032 |
< |
public void testThenRunAsync2() { |
2033 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2034 |
< |
Noop r = new Noop(); |
2035 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
2036 |
< |
f.completeExceptionally(new CFException()); |
2037 |
< |
try { |
2038 |
< |
g.join(); |
2039 |
< |
shouldThrow(); |
2040 |
< |
} catch (CompletionException success) {} |
2041 |
< |
checkCompletedWithWrappedCFException(g); |
2042 |
< |
} |
2043 |
< |
|
2044 |
< |
/** |
2045 |
< |
* thenRunAsync result completes exceptionally if action does |
2046 |
< |
*/ |
2047 |
< |
public void testThenRunAsync3() { |
2048 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2049 |
< |
FailingNoop r = new FailingNoop(); |
2050 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
2051 |
< |
f.complete(null); |
2052 |
< |
checkCompletedWithWrappedCFException(g); |
2053 |
< |
} |
2054 |
< |
|
2055 |
< |
/** |
2056 |
< |
* thenRunAsync result completes exceptionally if source cancelled |
2057 |
< |
*/ |
2058 |
< |
public void testThenRunAsync4() { |
2059 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2060 |
< |
Noop r = new Noop(); |
2061 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
2062 |
< |
assertTrue(f.cancel(true)); |
2063 |
< |
checkCompletedWithWrappedCancellationException(g); |
2064 |
< |
} |
2065 |
< |
|
2066 |
< |
/** |
2067 |
< |
* thenApplyAsync result completes normally after normal completion of source |
2068 |
< |
*/ |
2069 |
< |
public void testThenApplyAsync() { |
2070 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2071 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
2072 |
< |
f.complete(one); |
2073 |
< |
checkCompletedNormally(g, two); |
2074 |
< |
} |
2075 |
< |
|
2076 |
< |
/** |
2077 |
< |
* thenApplyAsync result completes exceptionally after exceptional |
2078 |
< |
* completion of source |
2079 |
< |
*/ |
2080 |
< |
public void testThenApplyAsync2() { |
2081 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2082 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
2083 |
< |
f.completeExceptionally(new CFException()); |
2084 |
< |
checkCompletedWithWrappedCFException(g); |
2085 |
< |
} |
2086 |
< |
|
2087 |
< |
/** |
2088 |
< |
* thenApplyAsync result completes exceptionally if action does |
2089 |
< |
*/ |
2090 |
< |
public void testThenApplyAsync3() { |
2091 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2092 |
< |
FailingFunction r = new FailingFunction(); |
2093 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(r); |
2094 |
< |
f.complete(null); |
2095 |
< |
checkCompletedWithWrappedCFException(g); |
2096 |
< |
} |
2097 |
< |
|
2098 |
< |
/** |
2099 |
< |
* thenApplyAsync result completes exceptionally if source cancelled |
2100 |
< |
*/ |
2101 |
< |
public void testThenApplyAsync4() { |
2102 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2103 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
2104 |
< |
assertTrue(f.cancel(true)); |
2105 |
< |
checkCompletedWithWrappedCancellationException(g); |
2106 |
< |
} |
2107 |
< |
|
2108 |
< |
/** |
2109 |
< |
* thenAcceptAsync result completes normally after normal |
2110 |
< |
* completion of source |
2111 |
< |
*/ |
2112 |
< |
public void testThenAcceptAsync() { |
2113 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2114 |
< |
IncAction r = new IncAction(); |
2115 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
2116 |
< |
f.complete(one); |
2117 |
< |
checkCompletedNormally(g, null); |
2118 |
< |
assertEquals(r.value, 2); |
2119 |
< |
} |
2120 |
< |
|
2121 |
< |
/** |
2122 |
< |
* thenAcceptAsync result completes exceptionally after exceptional |
2123 |
< |
* completion of source |
2124 |
< |
*/ |
2125 |
< |
public void testThenAcceptAsync2() { |
2126 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2127 |
< |
IncAction r = new IncAction(); |
2128 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
2129 |
< |
f.completeExceptionally(new CFException()); |
2130 |
< |
checkCompletedWithWrappedCFException(g); |
2131 |
< |
} |
2132 |
< |
|
2133 |
< |
/** |
2134 |
< |
* thenAcceptAsync result completes exceptionally if action does |
2135 |
< |
*/ |
2136 |
< |
public void testThenAcceptAsync3() { |
2137 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2138 |
< |
FailingConsumer r = new FailingConsumer(); |
2139 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
2140 |
< |
f.complete(null); |
2141 |
< |
checkCompletedWithWrappedCFException(g); |
2142 |
< |
} |
2143 |
< |
|
2144 |
< |
/** |
2145 |
< |
* thenAcceptAsync result completes exceptionally if source cancelled |
2146 |
< |
*/ |
2147 |
< |
public void testThenAcceptAsync4() { |
2148 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2149 |
< |
IncAction r = new IncAction(); |
2150 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
2151 |
< |
assertTrue(f.cancel(true)); |
2152 |
< |
checkCompletedWithWrappedCancellationException(g); |
2153 |
< |
} |
2154 |
< |
|
2155 |
< |
/** |
2156 |
< |
* applyToEitherAsync result completes normally after normal |
2157 |
< |
* completion of sources |
2158 |
< |
*/ |
2159 |
< |
public void testApplyToEitherAsync() { |
2160 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2161 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2162 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
2163 |
< |
f.complete(one); |
2164 |
< |
checkCompletedNormally(g, two); |
2165 |
< |
|
2166 |
< |
f = new CompletableFuture<>(); |
2167 |
< |
f.complete(one); |
2168 |
< |
f2 = new CompletableFuture<>(); |
2169 |
< |
g = f.applyToEitherAsync(f2, inc); |
2170 |
< |
checkCompletedNormally(g, two); |
2171 |
< |
} |
2172 |
< |
|
2173 |
< |
/** |
2174 |
< |
* applyToEitherAsync result completes exceptionally after exceptional |
2175 |
< |
* completion of source |
2176 |
< |
*/ |
2177 |
< |
public void testApplyToEitherAsync2() { |
2178 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2179 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2180 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
2181 |
< |
f.completeExceptionally(new CFException()); |
2182 |
< |
checkCompletedWithWrappedCFException(g); |
2183 |
< |
|
2184 |
< |
f = new CompletableFuture<>(); |
2185 |
< |
f2 = new CompletableFuture<>(); |
2186 |
< |
f2.completeExceptionally(new CFException()); |
2187 |
< |
g = f.applyToEitherAsync(f2, inc); |
2188 |
< |
f.complete(one); |
2189 |
< |
checkCompletedWithWrappedCFException(g); |
2190 |
< |
} |
2191 |
< |
|
2192 |
< |
/** |
2193 |
< |
* applyToEitherAsync result completes exceptionally if action does |
2194 |
< |
*/ |
2195 |
< |
public void testApplyToEitherAsync3() { |
2196 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2197 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2198 |
< |
FailingFunction r = new FailingFunction(); |
2199 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r); |
2200 |
< |
f.complete(one); |
2201 |
< |
checkCompletedWithWrappedCFException(g); |
2202 |
< |
} |
2203 |
< |
|
2204 |
< |
/** |
2205 |
< |
* applyToEitherAsync result completes exceptionally if either source cancelled |
2206 |
< |
*/ |
2207 |
< |
public void testApplyToEitherAsync4() { |
2208 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2209 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2210 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
2211 |
< |
assertTrue(f.cancel(true)); |
2212 |
< |
checkCompletedWithWrappedCancellationException(g); |
2213 |
< |
|
2214 |
< |
f = new CompletableFuture<>(); |
2215 |
< |
f2 = new CompletableFuture<>(); |
2216 |
< |
assertTrue(f2.cancel(true)); |
2217 |
< |
g = f.applyToEitherAsync(f2, inc); |
2218 |
< |
checkCompletedWithWrappedCancellationException(g); |
2219 |
< |
} |
2220 |
< |
|
2221 |
< |
/** |
2222 |
< |
* acceptEitherAsync result completes normally after normal |
2223 |
< |
* completion of sources |
2224 |
< |
*/ |
2225 |
< |
public void testAcceptEitherAsync() { |
2226 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2227 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2228 |
< |
IncAction r = new IncAction(); |
2229 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
2230 |
< |
f.complete(one); |
2231 |
< |
checkCompletedNormally(g, null); |
2232 |
< |
assertEquals(r.value, 2); |
2233 |
< |
|
2234 |
< |
r = new IncAction(); |
2235 |
< |
f = new CompletableFuture<>(); |
2236 |
< |
f.complete(one); |
2237 |
< |
f2 = new CompletableFuture<>(); |
2238 |
< |
g = f.acceptEitherAsync(f2, r); |
2239 |
< |
checkCompletedNormally(g, null); |
2240 |
< |
assertEquals(r.value, 2); |
2241 |
< |
} |
2242 |
< |
|
2243 |
< |
/** |
2244 |
< |
* acceptEitherAsync result completes exceptionally after exceptional |
2245 |
< |
* completion of source |
2246 |
< |
*/ |
2247 |
< |
public void testAcceptEitherAsync2() { |
2248 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2249 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2250 |
< |
IncAction r = new IncAction(); |
2251 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
2252 |
< |
f.completeExceptionally(new CFException()); |
2253 |
< |
checkCompletedWithWrappedCFException(g); |
2254 |
< |
|
2255 |
< |
r = new IncAction(); |
2256 |
< |
f = new CompletableFuture<>(); |
2257 |
< |
f2 = new CompletableFuture<>(); |
2258 |
< |
f2.completeExceptionally(new CFException()); |
2259 |
< |
g = f.acceptEitherAsync(f2, r); |
2260 |
< |
f.complete(one); |
2261 |
< |
checkCompletedWithWrappedCFException(g); |
2262 |
< |
} |
2263 |
< |
|
2264 |
< |
/** |
2265 |
< |
* acceptEitherAsync result completes exceptionally if action does |
2266 |
< |
*/ |
2267 |
< |
public void testAcceptEitherAsync3() { |
2268 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2269 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2270 |
< |
FailingConsumer r = new FailingConsumer(); |
2271 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
2272 |
< |
f.complete(one); |
2273 |
< |
checkCompletedWithWrappedCFException(g); |
2274 |
< |
} |
2275 |
< |
|
2276 |
< |
/** |
2277 |
< |
* acceptEitherAsync result completes exceptionally if either |
2278 |
< |
* source cancelled |
2279 |
< |
*/ |
2280 |
< |
public void testAcceptEitherAsync4() { |
2281 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2282 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2283 |
< |
IncAction r = new IncAction(); |
2284 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
2285 |
< |
assertTrue(f.cancel(true)); |
2286 |
< |
checkCompletedWithWrappedCancellationException(g); |
2287 |
< |
|
2288 |
< |
r = new IncAction(); |
2289 |
< |
f = new CompletableFuture<>(); |
2290 |
< |
f2 = new CompletableFuture<>(); |
2291 |
< |
assertTrue(f2.cancel(true)); |
2292 |
< |
g = f.acceptEitherAsync(f2, r); |
2293 |
< |
checkCompletedWithWrappedCancellationException(g); |
2294 |
< |
} |
2295 |
< |
|
2296 |
< |
/** |
2297 |
< |
* runAfterEitherAsync result completes normally after normal |
2298 |
< |
* completion of sources |
2299 |
< |
*/ |
2300 |
< |
public void testRunAfterEitherAsync() { |
2301 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2302 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2303 |
< |
Noop r = new Noop(); |
2304 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
2305 |
< |
f.complete(one); |
2306 |
< |
checkCompletedNormally(g, null); |
2307 |
< |
assertTrue(r.ran); |
2308 |
< |
|
2309 |
< |
r = new Noop(); |
2310 |
< |
f = new CompletableFuture<>(); |
2311 |
< |
f.complete(one); |
2312 |
< |
f2 = new CompletableFuture<>(); |
2313 |
< |
g = f.runAfterEitherAsync(f2, r); |
2314 |
< |
checkCompletedNormally(g, null); |
2315 |
< |
assertTrue(r.ran); |
2316 |
< |
} |
2317 |
< |
|
2318 |
< |
/** |
2319 |
< |
* runAfterEitherAsync result completes exceptionally after exceptional |
2320 |
< |
* completion of source |
2321 |
< |
*/ |
2322 |
< |
public void testRunAfterEitherAsync2() { |
2323 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2324 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2325 |
< |
Noop r = new Noop(); |
2326 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
2327 |
< |
f.completeExceptionally(new CFException()); |
2328 |
< |
checkCompletedWithWrappedCFException(g); |
2329 |
< |
|
2330 |
< |
r = new Noop(); |
2331 |
< |
f = new CompletableFuture<>(); |
2332 |
< |
f2 = new CompletableFuture<>(); |
2333 |
< |
f2.completeExceptionally(new CFException()); |
2334 |
< |
g = f.runAfterEitherAsync(f2, r); |
2335 |
< |
f.complete(one); |
2336 |
< |
checkCompletedWithWrappedCFException(g); |
2337 |
< |
} |
2338 |
< |
|
2339 |
< |
/** |
2340 |
< |
* runAfterEitherAsync result completes exceptionally if action does |
2341 |
< |
*/ |
2342 |
< |
public void testRunAfterEitherAsync3() { |
2343 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2344 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2345 |
< |
FailingNoop r = new FailingNoop(); |
2346 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
2347 |
< |
f.complete(one); |
2348 |
< |
checkCompletedWithWrappedCFException(g); |
2349 |
< |
} |
2350 |
< |
|
2351 |
< |
/** |
2352 |
< |
* runAfterEitherAsync result completes exceptionally if either |
2353 |
< |
* source cancelled |
2354 |
< |
*/ |
2355 |
< |
public void testRunAfterEitherAsync4() { |
2356 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2357 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2358 |
< |
Noop r = new Noop(); |
2359 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
2360 |
< |
assertTrue(f.cancel(true)); |
2361 |
< |
checkCompletedWithWrappedCancellationException(g); |
2362 |
< |
|
2363 |
< |
r = new Noop(); |
2364 |
< |
f = new CompletableFuture<>(); |
2365 |
< |
f2 = new CompletableFuture<>(); |
2366 |
< |
assertTrue(f2.cancel(true)); |
2367 |
< |
g = f.runAfterEitherAsync(f2, r); |
2368 |
< |
checkCompletedWithWrappedCancellationException(g); |
2369 |
< |
} |
2370 |
< |
|
2371 |
< |
/** |
2372 |
< |
* thenComposeAsync result completes normally after normal |
2373 |
< |
* completion of source |
2374 |
< |
*/ |
2375 |
< |
public void testThenComposeAsync() { |
2376 |
< |
CompletableFuture<Integer> f, g; |
2377 |
< |
CompletableFutureInc r; |
2378 |
< |
|
2379 |
< |
f = new CompletableFuture<>(); |
2380 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
2381 |
< |
f.complete(one); |
2382 |
< |
checkCompletedNormally(g, two); |
2383 |
< |
|
2384 |
< |
f = new CompletableFuture<>(); |
2385 |
< |
f.complete(one); |
2386 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
2387 |
< |
checkCompletedNormally(g, two); |
2388 |
< |
} |
2389 |
< |
|
2390 |
< |
/** |
2391 |
< |
* thenComposeAsync result completes exceptionally after |
2392 |
< |
* exceptional completion of source |
2393 |
< |
*/ |
2394 |
< |
public void testThenComposeAsync2() { |
2395 |
< |
CompletableFuture<Integer> f, g; |
2396 |
< |
CompletableFutureInc r; |
2397 |
< |
|
2398 |
< |
f = new CompletableFuture<>(); |
2399 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
2400 |
< |
f.completeExceptionally(new CFException()); |
2401 |
< |
checkCompletedWithWrappedCFException(g); |
2402 |
< |
assertFalse(r.ran); |
2403 |
< |
|
2404 |
< |
f = new CompletableFuture<>(); |
2405 |
< |
f.completeExceptionally(new CFException()); |
2406 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
2407 |
< |
checkCompletedWithWrappedCFException(g); |
2408 |
< |
assertFalse(r.ran); |
2409 |
< |
} |
2410 |
< |
|
2411 |
< |
/** |
2412 |
< |
* thenComposeAsync result completes exceptionally if action does |
2413 |
< |
*/ |
2414 |
< |
public void testThenComposeAsync3() { |
2415 |
< |
CompletableFuture<Integer> f, g; |
2416 |
< |
FailingCompletableFutureFunction r; |
2417 |
< |
|
2418 |
< |
f = new CompletableFuture<>(); |
2419 |
< |
g = f.thenComposeAsync(r = new FailingCompletableFutureFunction()); |
2420 |
< |
f.complete(one); |
2421 |
< |
checkCompletedWithWrappedCFException(g); |
2422 |
< |
|
2423 |
< |
f = new CompletableFuture<>(); |
2424 |
< |
f.complete(one); |
2425 |
< |
g = f.thenComposeAsync(r = new FailingCompletableFutureFunction()); |
2426 |
< |
checkCompletedWithWrappedCFException(g); |
2427 |
< |
} |
2428 |
< |
|
2429 |
< |
/** |
2430 |
< |
* thenComposeAsync result completes exceptionally if source cancelled |
2431 |
< |
*/ |
2432 |
< |
public void testThenComposeAsync4() { |
2433 |
< |
CompletableFuture<Integer> f, g; |
2434 |
< |
CompletableFutureInc r; |
2435 |
< |
|
2436 |
< |
f = new CompletableFuture<>(); |
2437 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
2438 |
< |
assertTrue(f.cancel(true)); |
2439 |
< |
checkCompletedWithWrappedCancellationException(g); |
2440 |
< |
|
2441 |
< |
f = new CompletableFuture<>(); |
2442 |
< |
assertTrue(f.cancel(true)); |
2443 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
2444 |
< |
checkCompletedWithWrappedCancellationException(g); |
2445 |
< |
} |
2446 |
< |
|
2447 |
< |
// async with explicit executors |
2448 |
< |
|
2449 |
< |
/** |
2450 |
< |
* thenRunAsync result completes normally after normal completion of source |
2451 |
< |
*/ |
2452 |
< |
public void testThenRunAsyncE() { |
2453 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2454 |
< |
Noop r = new Noop(); |
2455 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
2456 |
< |
f.complete(null); |
2457 |
< |
checkCompletedNormally(g, null); |
2458 |
< |
|
2459 |
< |
// reordered version |
2460 |
< |
f = new CompletableFuture<>(); |
2461 |
< |
f.complete(null); |
2462 |
< |
r = new Noop(); |
2463 |
< |
g = f.thenRunAsync(r, new ThreadExecutor()); |
2464 |
< |
checkCompletedNormally(g, null); |
2465 |
< |
} |
2466 |
< |
|
2467 |
< |
/** |
2468 |
< |
* thenRunAsync result completes exceptionally after exceptional |
2469 |
< |
* completion of source |
2470 |
< |
*/ |
2471 |
< |
public void testThenRunAsync2E() { |
2472 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2473 |
< |
Noop r = new Noop(); |
2474 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
2475 |
< |
f.completeExceptionally(new CFException()); |
2476 |
< |
try { |
2477 |
< |
g.join(); |
2478 |
< |
shouldThrow(); |
2479 |
< |
} catch (CompletionException success) {} |
2480 |
< |
checkCompletedWithWrappedCFException(g); |
2481 |
< |
} |
2482 |
< |
|
2483 |
< |
/** |
2484 |
< |
* thenRunAsync result completes exceptionally if action does |
2485 |
< |
*/ |
2486 |
< |
public void testThenRunAsync3E() { |
2487 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2488 |
< |
FailingNoop r = new FailingNoop(); |
2489 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
2490 |
< |
f.complete(null); |
2491 |
< |
checkCompletedWithWrappedCFException(g); |
2492 |
< |
} |
2493 |
< |
|
2494 |
< |
/** |
2495 |
< |
* thenRunAsync result completes exceptionally if source cancelled |
2496 |
< |
*/ |
2497 |
< |
public void testThenRunAsync4E() { |
2498 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2499 |
< |
Noop r = new Noop(); |
2500 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
2501 |
< |
assertTrue(f.cancel(true)); |
2502 |
< |
checkCompletedWithWrappedCancellationException(g); |
2503 |
< |
} |
2504 |
< |
|
2505 |
< |
/** |
2506 |
< |
* thenApplyAsync result completes normally after normal completion of source |
2507 |
< |
*/ |
2508 |
< |
public void testThenApplyAsyncE() { |
2509 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2510 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
2511 |
< |
f.complete(one); |
2512 |
< |
checkCompletedNormally(g, two); |
2513 |
< |
} |
2514 |
< |
|
2515 |
< |
/** |
2516 |
< |
* thenApplyAsync result completes exceptionally after exceptional |
2517 |
< |
* completion of source |
2518 |
< |
*/ |
2519 |
< |
public void testThenApplyAsync2E() { |
2520 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2521 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
2522 |
< |
f.completeExceptionally(new CFException()); |
2523 |
< |
checkCompletedWithWrappedCFException(g); |
2524 |
< |
} |
2525 |
< |
|
2526 |
< |
/** |
2527 |
< |
* thenApplyAsync result completes exceptionally if action does |
2528 |
< |
*/ |
2529 |
< |
public void testThenApplyAsync3E() { |
2530 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2531 |
< |
FailingFunction r = new FailingFunction(); |
2532 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor()); |
2533 |
< |
f.complete(null); |
2534 |
< |
checkCompletedWithWrappedCFException(g); |
2535 |
< |
} |
2536 |
< |
|
2537 |
< |
/** |
2538 |
< |
* thenApplyAsync result completes exceptionally if source cancelled |
2539 |
< |
*/ |
2540 |
< |
public void testThenApplyAsync4E() { |
2541 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2542 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
2543 |
< |
assertTrue(f.cancel(true)); |
2544 |
< |
checkCompletedWithWrappedCancellationException(g); |
2545 |
< |
} |
2546 |
< |
|
2547 |
< |
/** |
2548 |
< |
* thenAcceptAsync result completes normally after normal |
2549 |
< |
* completion of source |
2550 |
< |
*/ |
2551 |
< |
public void testThenAcceptAsyncE() { |
2552 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2553 |
< |
IncAction r = new IncAction(); |
2554 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
2555 |
< |
f.complete(one); |
2556 |
< |
checkCompletedNormally(g, null); |
2557 |
< |
assertEquals(r.value, 2); |
2558 |
< |
} |
2559 |
< |
|
2560 |
< |
/** |
2561 |
< |
* thenAcceptAsync result completes exceptionally after exceptional |
2562 |
< |
* completion of source |
2563 |
< |
*/ |
2564 |
< |
public void testThenAcceptAsync2E() { |
2565 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2566 |
< |
IncAction r = new IncAction(); |
2567 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
2568 |
< |
f.completeExceptionally(new CFException()); |
2569 |
< |
checkCompletedWithWrappedCFException(g); |
2570 |
< |
} |
2571 |
< |
|
2572 |
< |
/** |
2573 |
< |
* thenAcceptAsync result completes exceptionally if action does |
2574 |
< |
*/ |
2575 |
< |
public void testThenAcceptAsync3E() { |
2576 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2577 |
< |
FailingConsumer r = new FailingConsumer(); |
2578 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
2579 |
< |
f.complete(null); |
2580 |
< |
checkCompletedWithWrappedCFException(g); |
2581 |
< |
} |
2582 |
< |
|
2583 |
< |
/** |
2584 |
< |
* thenAcceptAsync result completes exceptionally if source cancelled |
2585 |
< |
*/ |
2586 |
< |
public void testThenAcceptAsync4E() { |
2587 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2588 |
< |
IncAction r = new IncAction(); |
2589 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
2590 |
< |
assertTrue(f.cancel(true)); |
2591 |
< |
checkCompletedWithWrappedCancellationException(g); |
2592 |
< |
} |
2593 |
< |
|
2594 |
< |
/** |
2595 |
< |
* applyToEitherAsync result completes normally after normal |
2596 |
< |
* completion of sources |
2597 |
< |
*/ |
2598 |
< |
public void testApplyToEitherAsyncE() { |
2599 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2600 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2601 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2602 |
< |
f.complete(one); |
2603 |
< |
checkCompletedNormally(g, two); |
2604 |
< |
|
2605 |
< |
f = new CompletableFuture<>(); |
2606 |
< |
f.complete(one); |
2607 |
< |
f2 = new CompletableFuture<>(); |
2608 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2609 |
< |
checkCompletedNormally(g, two); |
2610 |
< |
} |
2611 |
< |
|
2612 |
< |
/** |
2613 |
< |
* applyToEitherAsync result completes exceptionally after exceptional |
2614 |
< |
* completion of source |
2615 |
< |
*/ |
2616 |
< |
public void testApplyToEitherAsync2E() { |
2617 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2618 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2619 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2620 |
< |
f.completeExceptionally(new CFException()); |
2621 |
< |
checkCompletedWithWrappedCFException(g); |
2622 |
< |
|
2623 |
< |
f = new CompletableFuture<>(); |
2624 |
< |
f2 = new CompletableFuture<>(); |
2625 |
< |
f2.completeExceptionally(new CFException()); |
2626 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2627 |
< |
f.complete(one); |
2628 |
< |
checkCompletedWithWrappedCFException(g); |
2629 |
< |
} |
2630 |
< |
|
2631 |
< |
/** |
2632 |
< |
* applyToEitherAsync result completes exceptionally if action does |
2633 |
< |
*/ |
2634 |
< |
public void testApplyToEitherAsync3E() { |
2635 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2636 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2637 |
< |
FailingFunction r = new FailingFunction(); |
2638 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor()); |
2639 |
< |
f.complete(one); |
2640 |
< |
checkCompletedWithWrappedCFException(g); |
2641 |
< |
} |
2642 |
< |
|
2643 |
< |
/** |
2644 |
< |
* applyToEitherAsync result completes exceptionally if either source cancelled |
2645 |
< |
*/ |
2646 |
< |
public void testApplyToEitherAsync4E() { |
2647 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2648 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2649 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2650 |
< |
assertTrue(f.cancel(true)); |
2651 |
< |
checkCompletedWithWrappedCancellationException(g); |
2652 |
< |
|
2653 |
< |
f = new CompletableFuture<>(); |
2654 |
< |
f2 = new CompletableFuture<>(); |
2655 |
< |
assertTrue(f2.cancel(true)); |
2656 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2657 |
< |
checkCompletedWithWrappedCancellationException(g); |
2658 |
< |
} |
2659 |
< |
|
2660 |
< |
/** |
2661 |
< |
* acceptEitherAsync result completes normally after normal |
2662 |
< |
* completion of sources |
2663 |
< |
*/ |
2664 |
< |
public void testAcceptEitherAsyncE() { |
2665 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2666 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2667 |
< |
IncAction r = new IncAction(); |
2668 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2669 |
< |
f.complete(one); |
2670 |
< |
checkCompletedNormally(g, null); |
2671 |
< |
assertEquals(r.value, 2); |
2672 |
< |
|
2673 |
< |
r = new IncAction(); |
2674 |
< |
f = new CompletableFuture<>(); |
2675 |
< |
f.complete(one); |
2676 |
< |
f2 = new CompletableFuture<>(); |
2677 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2678 |
< |
checkCompletedNormally(g, null); |
2679 |
< |
assertEquals(r.value, 2); |
2680 |
< |
} |
2681 |
< |
|
2682 |
< |
/** |
2683 |
< |
* acceptEitherAsync result completes exceptionally after exceptional |
2684 |
< |
* completion of source |
2685 |
< |
*/ |
2686 |
< |
public void testAcceptEitherAsync2E() { |
2687 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2688 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2689 |
< |
IncAction r = new IncAction(); |
2690 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2691 |
< |
f.completeExceptionally(new CFException()); |
2692 |
< |
checkCompletedWithWrappedCFException(g); |
2693 |
< |
|
2694 |
< |
r = new IncAction(); |
2695 |
< |
f = new CompletableFuture<>(); |
2696 |
< |
f2 = new CompletableFuture<>(); |
2697 |
< |
f2.completeExceptionally(new CFException()); |
2698 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2699 |
< |
f.complete(one); |
2700 |
< |
checkCompletedWithWrappedCFException(g); |
2701 |
< |
} |
2702 |
< |
|
2703 |
< |
/** |
2704 |
< |
* acceptEitherAsync result completes exceptionally if action does |
2705 |
< |
*/ |
2706 |
< |
public void testAcceptEitherAsync3E() { |
2707 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2708 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2709 |
< |
FailingConsumer r = new FailingConsumer(); |
2710 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2711 |
< |
f.complete(one); |
2712 |
< |
checkCompletedWithWrappedCFException(g); |
2713 |
< |
} |
2714 |
< |
|
2715 |
< |
/** |
2716 |
< |
* acceptEitherAsync result completes exceptionally if either |
2717 |
< |
* source cancelled |
2718 |
< |
*/ |
2719 |
< |
public void testAcceptEitherAsync4E() { |
2720 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2721 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2722 |
< |
IncAction r = new IncAction(); |
2723 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2724 |
< |
assertTrue(f.cancel(true)); |
2725 |
< |
checkCompletedWithWrappedCancellationException(g); |
2726 |
< |
|
2727 |
< |
r = new IncAction(); |
2728 |
< |
f = new CompletableFuture<>(); |
2729 |
< |
f2 = new CompletableFuture<>(); |
2730 |
< |
assertTrue(f2.cancel(true)); |
2731 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2732 |
< |
checkCompletedWithWrappedCancellationException(g); |
2733 |
< |
} |
2734 |
< |
|
2735 |
< |
/** |
2736 |
< |
* runAfterEitherAsync result completes normally after normal |
2737 |
< |
* completion of sources |
2738 |
< |
*/ |
2739 |
< |
public void testRunAfterEitherAsyncE() { |
2740 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2741 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2742 |
< |
Noop r = new Noop(); |
2743 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2744 |
< |
f.complete(one); |
2745 |
< |
checkCompletedNormally(g, null); |
2746 |
< |
assertTrue(r.ran); |
2747 |
< |
|
2748 |
< |
r = new Noop(); |
2749 |
< |
f = new CompletableFuture<>(); |
2750 |
< |
f.complete(one); |
2751 |
< |
f2 = new CompletableFuture<>(); |
2752 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2753 |
< |
checkCompletedNormally(g, null); |
2754 |
< |
assertTrue(r.ran); |
2755 |
< |
} |
2756 |
< |
|
2757 |
< |
/** |
2758 |
< |
* runAfterEitherAsync result completes exceptionally after exceptional |
2759 |
< |
* completion of source |
2760 |
< |
*/ |
2761 |
< |
public void testRunAfterEitherAsync2E() { |
2762 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2763 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2764 |
< |
Noop r = new Noop(); |
2765 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2766 |
< |
f.completeExceptionally(new CFException()); |
2767 |
< |
checkCompletedWithWrappedCFException(g); |
2768 |
< |
|
2769 |
< |
r = new Noop(); |
2770 |
< |
f = new CompletableFuture<>(); |
2771 |
< |
f2 = new CompletableFuture<>(); |
2772 |
< |
f2.completeExceptionally(new CFException()); |
2773 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2774 |
< |
f.complete(one); |
2775 |
< |
checkCompletedWithWrappedCFException(g); |
2776 |
< |
} |
2777 |
< |
|
2778 |
< |
/** |
2779 |
< |
* runAfterEitherAsync result completes exceptionally if action does |
2780 |
< |
*/ |
2781 |
< |
public void testRunAfterEitherAsync3E() { |
2782 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2783 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2784 |
< |
FailingNoop r = new FailingNoop(); |
2785 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2786 |
< |
f.complete(one); |
2787 |
< |
checkCompletedWithWrappedCFException(g); |
2788 |
< |
} |
2789 |
< |
|
2790 |
< |
/** |
2791 |
< |
* runAfterEitherAsync result completes exceptionally if either |
2792 |
< |
* source cancelled |
2793 |
< |
*/ |
2794 |
< |
public void testRunAfterEitherAsync4E() { |
2795 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2796 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2797 |
< |
Noop r = new Noop(); |
2798 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2799 |
< |
assertTrue(f.cancel(true)); |
2800 |
< |
checkCompletedWithWrappedCancellationException(g); |
3015 |
> |
public void testThenCompose_sourceCancelled() { |
3016 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3017 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3018 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3019 |
> |
{ |
3020 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3021 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
3022 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
3023 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
3024 |
> |
if (createIncomplete) { |
3025 |
> |
checkIncomplete(g); |
3026 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
3027 |
> |
} |
3028 |
|
|
2802 |
– |
r = new Noop(); |
2803 |
– |
f = new CompletableFuture<>(); |
2804 |
– |
f2 = new CompletableFuture<>(); |
2805 |
– |
assertTrue(f2.cancel(true)); |
2806 |
– |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
3029 |
|
checkCompletedWithWrappedCancellationException(g); |
3030 |
< |
} |
3031 |
< |
|
2810 |
< |
/** |
2811 |
< |
* thenComposeAsync result completes normally after normal |
2812 |
< |
* completion of source |
2813 |
< |
*/ |
2814 |
< |
public void testThenComposeAsyncE() { |
2815 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2816 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
2817 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2818 |
< |
f.complete(one); |
2819 |
< |
checkCompletedNormally(g, two); |
2820 |
< |
} |
2821 |
< |
|
2822 |
< |
/** |
2823 |
< |
* thenComposeAsync result completes exceptionally after |
2824 |
< |
* exceptional completion of source |
2825 |
< |
*/ |
2826 |
< |
public void testThenComposeAsync2E() { |
2827 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2828 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
2829 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2830 |
< |
f.completeExceptionally(new CFException()); |
2831 |
< |
checkCompletedWithWrappedCFException(g); |
2832 |
< |
} |
3030 |
> |
checkCancelled(f); |
3031 |
> |
}} |
3032 |
|
|
3033 |
|
/** |
3034 |
< |
* thenComposeAsync result completes exceptionally if action does |
3034 |
> |
* thenCompose result completes exceptionally if the result of the action does |
3035 |
|
*/ |
3036 |
< |
public void testThenComposeAsync3E() { |
3037 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3038 |
< |
FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); |
3039 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
3040 |
< |
f.complete(one); |
3041 |
< |
checkCompletedWithWrappedCFException(g); |
3042 |
< |
} |
3036 |
> |
public void testThenCompose_actionReturnsFailingFuture() { |
3037 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3038 |
> |
for (int order = 0; order < 6; order++) |
3039 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3040 |
> |
{ |
3041 |
> |
final CFException ex = new CFException(); |
3042 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3043 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3044 |
> |
final CompletableFuture<Integer> h; |
3045 |
> |
// Test all permutations of orders |
3046 |
> |
switch (order) { |
3047 |
> |
case 0: |
3048 |
> |
assertTrue(f.complete(v1)); |
3049 |
> |
assertTrue(g.completeExceptionally(ex)); |
3050 |
> |
h = m.thenCompose(f, (x -> g)); |
3051 |
> |
break; |
3052 |
> |
case 1: |
3053 |
> |
assertTrue(f.complete(v1)); |
3054 |
> |
h = m.thenCompose(f, (x -> g)); |
3055 |
> |
assertTrue(g.completeExceptionally(ex)); |
3056 |
> |
break; |
3057 |
> |
case 2: |
3058 |
> |
assertTrue(g.completeExceptionally(ex)); |
3059 |
> |
assertTrue(f.complete(v1)); |
3060 |
> |
h = m.thenCompose(f, (x -> g)); |
3061 |
> |
break; |
3062 |
> |
case 3: |
3063 |
> |
assertTrue(g.completeExceptionally(ex)); |
3064 |
> |
h = m.thenCompose(f, (x -> g)); |
3065 |
> |
assertTrue(f.complete(v1)); |
3066 |
> |
break; |
3067 |
> |
case 4: |
3068 |
> |
h = m.thenCompose(f, (x -> g)); |
3069 |
> |
assertTrue(f.complete(v1)); |
3070 |
> |
assertTrue(g.completeExceptionally(ex)); |
3071 |
> |
break; |
3072 |
> |
case 5: |
3073 |
> |
h = m.thenCompose(f, (x -> g)); |
3074 |
> |
assertTrue(f.complete(v1)); |
3075 |
> |
assertTrue(g.completeExceptionally(ex)); |
3076 |
> |
break; |
3077 |
> |
default: throw new AssertionError(); |
3078 |
> |
} |
3079 |
|
|
3080 |
< |
/** |
3081 |
< |
* thenComposeAsync result completes exceptionally if source cancelled |
3082 |
< |
*/ |
3083 |
< |
public void testThenComposeAsync4E() { |
2849 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2850 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
2851 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2852 |
< |
assertTrue(f.cancel(true)); |
2853 |
< |
checkCompletedWithWrappedCancellationException(g); |
2854 |
< |
} |
3080 |
> |
checkCompletedExceptionally(g, ex); |
3081 |
> |
checkCompletedWithWrappedException(h, ex); |
3082 |
> |
checkCompletedNormally(f, v1); |
3083 |
> |
}} |
3084 |
|
|
3085 |
|
// other static methods |
3086 |
|
|
3098 |
|
* when all components complete normally |
3099 |
|
*/ |
3100 |
|
public void testAllOf_normal() throws Exception { |
3101 |
< |
for (int k = 1; k < 20; ++k) { |
3102 |
< |
CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3103 |
< |
for (int i = 0; i < k; ++i) |
3101 |
> |
for (int k = 1; k < 10; k++) { |
3102 |
> |
CompletableFuture<Integer>[] fs |
3103 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3104 |
> |
for (int i = 0; i < k; i++) |
3105 |
|
fs[i] = new CompletableFuture<>(); |
3106 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3107 |
< |
for (int i = 0; i < k; ++i) { |
3107 |
> |
for (int i = 0; i < k; i++) { |
3108 |
|
checkIncomplete(f); |
3109 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3110 |
|
fs[i].complete(one); |
3114 |
|
} |
3115 |
|
} |
3116 |
|
|
3117 |
+ |
public void testAllOf_backwards() throws Exception { |
3118 |
+ |
for (int k = 1; k < 10; k++) { |
3119 |
+ |
CompletableFuture<Integer>[] fs |
3120 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3121 |
+ |
for (int i = 0; i < k; i++) |
3122 |
+ |
fs[i] = new CompletableFuture<>(); |
3123 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3124 |
+ |
for (int i = k - 1; i >= 0; i--) { |
3125 |
+ |
checkIncomplete(f); |
3126 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3127 |
+ |
fs[i].complete(one); |
3128 |
+ |
} |
3129 |
+ |
checkCompletedNormally(f, null); |
3130 |
+ |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
3131 |
+ |
} |
3132 |
+ |
} |
3133 |
+ |
|
3134 |
+ |
public void testAllOf_exceptional() throws Exception { |
3135 |
+ |
for (int k = 1; k < 10; k++) { |
3136 |
+ |
CompletableFuture<Integer>[] fs |
3137 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3138 |
+ |
CFException ex = new CFException(); |
3139 |
+ |
for (int i = 0; i < k; i++) |
3140 |
+ |
fs[i] = new CompletableFuture<>(); |
3141 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3142 |
+ |
for (int i = 0; i < k; i++) { |
3143 |
+ |
checkIncomplete(f); |
3144 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3145 |
+ |
if (i != k/2) { |
3146 |
+ |
fs[i].complete(i); |
3147 |
+ |
checkCompletedNormally(fs[i], i); |
3148 |
+ |
} else { |
3149 |
+ |
fs[i].completeExceptionally(ex); |
3150 |
+ |
checkCompletedExceptionally(fs[i], ex); |
3151 |
+ |
} |
3152 |
+ |
} |
3153 |
+ |
checkCompletedWithWrappedException(f, ex); |
3154 |
+ |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3155 |
+ |
} |
3156 |
+ |
} |
3157 |
+ |
|
3158 |
|
/** |
3159 |
|
* anyOf(no component futures) returns an incomplete future |
3160 |
|
*/ |
3161 |
|
public void testAnyOf_empty() throws Exception { |
3162 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3163 |
+ |
{ |
3164 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3165 |
|
checkIncomplete(f); |
3166 |
< |
} |
3166 |
> |
|
3167 |
> |
f.complete(v1); |
3168 |
> |
checkCompletedNormally(f, v1); |
3169 |
> |
}} |
3170 |
|
|
3171 |
|
/** |
3172 |
|
* anyOf returns a future completed normally with a value when |
3173 |
|
* a component future does |
3174 |
|
*/ |
3175 |
|
public void testAnyOf_normal() throws Exception { |
3176 |
< |
for (int k = 0; k < 10; ++k) { |
3176 |
> |
for (int k = 0; k < 10; k++) { |
3177 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3178 |
< |
for (int i = 0; i < k; ++i) |
3178 |
> |
for (int i = 0; i < k; i++) |
3179 |
|
fs[i] = new CompletableFuture<>(); |
3180 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3181 |
|
checkIncomplete(f); |
3182 |
< |
for (int i = 0; i < k; ++i) { |
3183 |
< |
fs[i].complete(one); |
3184 |
< |
checkCompletedNormally(f, one); |
3185 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
3182 |
> |
for (int i = 0; i < k; i++) { |
3183 |
> |
fs[i].complete(i); |
3184 |
> |
checkCompletedNormally(f, 0); |
3185 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3186 |
> |
assertTrue(0 <= x && x <= i); |
3187 |
> |
} |
3188 |
> |
} |
3189 |
> |
} |
3190 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3191 |
> |
for (int k = 0; k < 10; k++) { |
3192 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3193 |
> |
for (int i = 0; i < k; i++) |
3194 |
> |
fs[i] = new CompletableFuture<>(); |
3195 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3196 |
> |
checkIncomplete(f); |
3197 |
> |
for (int i = k - 1; i >= 0; i--) { |
3198 |
> |
fs[i].complete(i); |
3199 |
> |
checkCompletedNormally(f, k - 1); |
3200 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3201 |
> |
assertTrue(i <= x && x <= k - 1); |
3202 |
|
} |
3203 |
|
} |
3204 |
|
} |
3207 |
|
* anyOf result completes exceptionally when any component does. |
3208 |
|
*/ |
3209 |
|
public void testAnyOf_exceptional() throws Exception { |
3210 |
< |
for (int k = 0; k < 10; ++k) { |
3210 |
> |
for (int k = 0; k < 10; k++) { |
3211 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3212 |
< |
for (int i = 0; i < k; ++i) |
3212 |
> |
CFException[] exs = new CFException[k]; |
3213 |
> |
for (int i = 0; i < k; i++) { |
3214 |
|
fs[i] = new CompletableFuture<>(); |
3215 |
+ |
exs[i] = new CFException(); |
3216 |
+ |
} |
3217 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3218 |
|
checkIncomplete(f); |
3219 |
< |
for (int i = 0; i < k; ++i) { |
3220 |
< |
fs[i].completeExceptionally(new CFException()); |
3221 |
< |
checkCompletedWithWrappedCFException(f); |
3219 |
> |
for (int i = 0; i < k; i++) { |
3220 |
> |
fs[i].completeExceptionally(exs[i]); |
3221 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3222 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3223 |
> |
} |
3224 |
> |
} |
3225 |
> |
} |
3226 |
> |
|
3227 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3228 |
> |
for (int k = 0; k < 10; k++) { |
3229 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3230 |
> |
CFException[] exs = new CFException[k]; |
3231 |
> |
for (int i = 0; i < k; i++) { |
3232 |
> |
fs[i] = new CompletableFuture<>(); |
3233 |
> |
exs[i] = new CFException(); |
3234 |
> |
} |
3235 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3236 |
> |
checkIncomplete(f); |
3237 |
> |
for (int i = k - 1; i >= 0; i--) { |
3238 |
> |
fs[i].completeExceptionally(exs[i]); |
3239 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3240 |
|
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3241 |
|
} |
3242 |
|
} |
3249 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3250 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3251 |
|
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2939 |
– |
CompletableFuture<?> h; |
3252 |
|
ThreadExecutor exec = new ThreadExecutor(); |
3253 |
|
|
3254 |
|
Runnable[] throwingActions = { |
3255 |
|
() -> CompletableFuture.supplyAsync(null), |
3256 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3257 |
< |
() -> CompletableFuture.supplyAsync(supplyOne, null), |
3257 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3258 |
|
|
3259 |
|
() -> CompletableFuture.runAsync(null), |
3260 |
|
() -> CompletableFuture.runAsync(null, exec), |
3327 |
|
|
3328 |
|
() -> f.thenCompose(null), |
3329 |
|
() -> f.thenComposeAsync(null), |
3330 |
< |
() -> f.thenComposeAsync(new CompletableFutureInc(), null), |
3330 |
> |
() -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null), |
3331 |
|
() -> f.thenComposeAsync(null, exec), |
3332 |
|
|
3333 |
|
() -> f.exceptionally(null), |
3359 |
|
assertSame(f, f.toCompletableFuture()); |
3360 |
|
} |
3361 |
|
|
3362 |
+ |
// jdk9 |
3363 |
+ |
|
3364 |
|
/** |
3365 |
< |
* whenComplete action executes on normal completion, propagating |
3052 |
< |
* source result. |
3365 |
> |
* newIncompleteFuture returns an incomplete CompletableFuture |
3366 |
|
*/ |
3367 |
< |
public void testWhenComplete1() { |
3055 |
< |
final AtomicInteger a = new AtomicInteger(); |
3367 |
> |
public void testNewIncompleteFuture() { |
3368 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3369 |
< |
CompletableFuture<Integer> g = |
3370 |
< |
f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); |
3371 |
< |
f.complete(three); |
3060 |
< |
checkCompletedNormally(f, three); |
3061 |
< |
checkCompletedNormally(g, three); |
3062 |
< |
assertEquals(a.get(), 1); |
3369 |
> |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3370 |
> |
checkIncomplete(f); |
3371 |
> |
checkIncomplete(g); |
3372 |
|
} |
3373 |
|
|
3374 |
|
/** |
3375 |
< |
* whenComplete action executes on exceptional completion, propagating |
3067 |
< |
* source result. |
3375 |
> |
* completedStage returns a completed CompletionStage |
3376 |
|
*/ |
3377 |
< |
public void testWhenComplete2() { |
3378 |
< |
final AtomicInteger a = new AtomicInteger(); |
3379 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3380 |
< |
CompletableFuture<Integer> g = |
3381 |
< |
f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); |
3382 |
< |
f.completeExceptionally(new CFException()); |
3383 |
< |
assertTrue(f.isCompletedExceptionally()); |
3076 |
< |
assertTrue(g.isCompletedExceptionally()); |
3077 |
< |
assertEquals(a.get(), 1); |
3377 |
> |
public void testCompletedStage() { |
3378 |
> |
AtomicInteger x = new AtomicInteger(); |
3379 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3380 |
> |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3381 |
> |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3382 |
> |
assertEquals(x.get(), 1); |
3383 |
> |
assertNull(r.get()); |
3384 |
|
} |
3385 |
|
|
3386 |
|
/** |
3387 |
< |
* If a whenComplete action throws an exception when triggered by |
3388 |
< |
* a normal completion, it completes exceptionally |
3387 |
> |
* defaultExecutor by default returns the commonPool if |
3388 |
> |
* it supports more than one thread. |
3389 |
|
*/ |
3390 |
< |
public void testWhenComplete3() { |
3390 |
> |
public void testDefaultExecutor() { |
3391 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3392 |
< |
CompletableFuture<Integer> g = |
3393 |
< |
f.whenComplete((Integer x, Throwable t) -> |
3394 |
< |
{ throw new CFException(); } ); |
3395 |
< |
f.complete(three); |
3396 |
< |
checkCompletedNormally(f, three); |
3397 |
< |
assertTrue(g.isCompletedExceptionally()); |
3092 |
< |
checkCompletedWithWrappedCFException(g); |
3392 |
> |
Executor e = f.defaultExecutor(); |
3393 |
> |
Executor c = ForkJoinPool.commonPool(); |
3394 |
> |
if (ForkJoinPool.getCommonPoolParallelism() > 1) |
3395 |
> |
assertSame(e, c); |
3396 |
> |
else |
3397 |
> |
assertNotSame(e, c); |
3398 |
|
} |
3399 |
|
|
3400 |
|
/** |
3401 |
< |
* whenCompleteAsync action executes on normal completion, propagating |
3402 |
< |
* source result. |
3401 |
> |
* failedFuture returns a CompletableFuture completed |
3402 |
> |
* exceptionally with the given Exception |
3403 |
> |
*/ |
3404 |
> |
public void testFailedFuture() { |
3405 |
> |
CFException ex = new CFException(); |
3406 |
> |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3407 |
> |
checkCompletedExceptionallyWithRootCause(f, ex); |
3408 |
> |
} |
3409 |
> |
|
3410 |
> |
/** |
3411 |
> |
* failedFuture(null) throws NPE |
3412 |
> |
*/ |
3413 |
> |
public void testFailedFuture2() { |
3414 |
> |
try { |
3415 |
> |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3416 |
> |
shouldThrow(); |
3417 |
> |
} catch (NullPointerException success) {} |
3418 |
> |
} |
3419 |
> |
|
3420 |
> |
/** |
3421 |
> |
* copy returns a CompletableFuture that is completed normally, |
3422 |
> |
* with the same value, when source is. |
3423 |
|
*/ |
3424 |
< |
public void testWhenCompleteAsync1() { |
3100 |
< |
final AtomicInteger a = new AtomicInteger(); |
3424 |
> |
public void testCopy() { |
3425 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3426 |
< |
CompletableFuture<Integer> g = |
3427 |
< |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); |
3428 |
< |
f.complete(three); |
3429 |
< |
checkCompletedNormally(f, three); |
3430 |
< |
checkCompletedNormally(g, three); |
3431 |
< |
assertEquals(a.get(), 1); |
3426 |
> |
CompletableFuture<Integer> g = f.copy(); |
3427 |
> |
checkIncomplete(f); |
3428 |
> |
checkIncomplete(g); |
3429 |
> |
f.complete(1); |
3430 |
> |
checkCompletedNormally(f, 1); |
3431 |
> |
checkCompletedNormally(g, 1); |
3432 |
|
} |
3433 |
|
|
3434 |
|
/** |
3435 |
< |
* whenCompleteAsync action executes on exceptional completion, propagating |
3436 |
< |
* source result. |
3435 |
> |
* copy returns a CompletableFuture that is completed exceptionally |
3436 |
> |
* when source is. |
3437 |
|
*/ |
3438 |
< |
public void testWhenCompleteAsync2() { |
3115 |
< |
final AtomicInteger a = new AtomicInteger(); |
3438 |
> |
public void testCopy2() { |
3439 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3440 |
< |
CompletableFuture<Integer> g = |
3441 |
< |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); |
3442 |
< |
f.completeExceptionally(new CFException()); |
3443 |
< |
checkCompletedWithWrappedCFException(f); |
3440 |
> |
CompletableFuture<Integer> g = f.copy(); |
3441 |
> |
checkIncomplete(f); |
3442 |
> |
checkIncomplete(g); |
3443 |
> |
CFException ex = new CFException(); |
3444 |
> |
f.completeExceptionally(ex); |
3445 |
> |
checkCompletedExceptionally(f, ex); |
3446 |
|
checkCompletedWithWrappedCFException(g); |
3447 |
|
} |
3448 |
|
|
3449 |
|
/** |
3450 |
< |
* If a whenCompleteAsync action throws an exception when |
3451 |
< |
* triggered by a normal completion, it completes exceptionally |
3450 |
> |
* minimalCompletionStage returns a CompletableFuture that is |
3451 |
> |
* completed normally, with the same value, when source is. |
3452 |
|
*/ |
3453 |
< |
public void testWhenCompleteAsync3() { |
3453 |
> |
public void testMinimalCompletionStage() { |
3454 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3455 |
< |
CompletableFuture<Integer> g = |
3456 |
< |
f.whenCompleteAsync((Integer x, Throwable t) -> |
3457 |
< |
{ throw new CFException(); } ); |
3458 |
< |
f.complete(three); |
3459 |
< |
checkCompletedNormally(f, three); |
3460 |
< |
checkCompletedWithWrappedCFException(g); |
3455 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3456 |
> |
AtomicInteger x = new AtomicInteger(); |
3457 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3458 |
> |
checkIncomplete(f); |
3459 |
> |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3460 |
> |
f.complete(1); |
3461 |
> |
checkCompletedNormally(f, 1); |
3462 |
> |
assertEquals(x.get(), 1); |
3463 |
> |
assertNull(r.get()); |
3464 |
|
} |
3465 |
|
|
3466 |
|
/** |
3467 |
< |
* whenCompleteAsync action executes on normal completion, propagating |
3468 |
< |
* source result. |
3467 |
> |
* minimalCompletionStage returns a CompletableFuture that is |
3468 |
> |
* completed exceptionally when source is. |
3469 |
|
*/ |
3470 |
< |
public void testWhenCompleteAsync1e() { |
3143 |
< |
final AtomicInteger a = new AtomicInteger(); |
3144 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
3470 |
> |
public void testMinimalCompletionStage2() { |
3471 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3472 |
< |
CompletableFuture<Integer> g = |
3473 |
< |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), |
3474 |
< |
exec); |
3475 |
< |
f.complete(three); |
3476 |
< |
checkCompletedNormally(f, three); |
3477 |
< |
checkCompletedNormally(g, three); |
3478 |
< |
assertEquals(a.get(), 1); |
3472 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3473 |
> |
AtomicInteger x = new AtomicInteger(); |
3474 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3475 |
> |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3476 |
> |
checkIncomplete(f); |
3477 |
> |
CFException ex = new CFException(); |
3478 |
> |
f.completeExceptionally(ex); |
3479 |
> |
checkCompletedExceptionally(f, ex); |
3480 |
> |
assertEquals(x.get(), 0); |
3481 |
> |
assertEquals(r.get().getCause(), ex); |
3482 |
|
} |
3483 |
|
|
3484 |
|
/** |
3485 |
< |
* whenCompleteAsync action executes on exceptional completion, propagating |
3486 |
< |
* source result. |
3485 |
> |
* failedStage returns a CompletionStage completed |
3486 |
> |
* exceptionally with the given Exception |
3487 |
|
*/ |
3488 |
< |
public void testWhenCompleteAsync2e() { |
3489 |
< |
final AtomicInteger a = new AtomicInteger(); |
3490 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
3488 |
> |
public void testFailedStage() { |
3489 |
> |
CFException ex = new CFException(); |
3490 |
> |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3491 |
> |
AtomicInteger x = new AtomicInteger(); |
3492 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3493 |
> |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3494 |
> |
assertEquals(x.get(), 0); |
3495 |
> |
assertEquals(r.get().getCause(), ex); |
3496 |
> |
} |
3497 |
> |
|
3498 |
> |
/** |
3499 |
> |
* completeAsync completes with value of given supplier |
3500 |
> |
*/ |
3501 |
> |
public void testCompleteAsync() { |
3502 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3503 |
< |
CompletableFuture<Integer> g = |
3504 |
< |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), |
3505 |
< |
exec); |
3166 |
< |
f.completeExceptionally(new CFException()); |
3167 |
< |
checkCompletedWithWrappedCFException(f); |
3168 |
< |
checkCompletedWithWrappedCFException(g); |
3503 |
> |
f.completeAsync(() -> 1); |
3504 |
> |
f.join(); |
3505 |
> |
checkCompletedNormally(f, 1); |
3506 |
|
} |
3507 |
|
|
3508 |
|
/** |
3509 |
< |
* If a whenCompleteAsync action throws an exception when triggered |
3173 |
< |
* by a normal completion, it completes exceptionally |
3509 |
> |
* completeAsync completes exceptionally if given supplier throws |
3510 |
|
*/ |
3511 |
< |
public void testWhenCompleteAsync3e() { |
3176 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
3511 |
> |
public void testCompleteAsync2() { |
3512 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3513 |
< |
CompletableFuture<Integer> g = |
3514 |
< |
f.whenCompleteAsync((Integer x, Throwable t) -> |
3515 |
< |
{ throw new CFException(); }, |
3516 |
< |
exec); |
3517 |
< |
f.complete(three); |
3518 |
< |
checkCompletedNormally(f, three); |
3519 |
< |
checkCompletedWithWrappedCFException(g); |
3513 |
> |
CFException ex = new CFException(); |
3514 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3515 |
> |
try { |
3516 |
> |
f.join(); |
3517 |
> |
shouldThrow(); |
3518 |
> |
} catch (Exception success) {} |
3519 |
> |
checkCompletedWithWrappedCFException(f); |
3520 |
|
} |
3521 |
|
|
3522 |
|
/** |
3523 |
< |
* handleAsync action completes normally with function value on |
3189 |
< |
* either normal or exceptional completion of source |
3523 |
> |
* completeAsync with given executor completes with value of given supplier |
3524 |
|
*/ |
3525 |
< |
public void testHandleAsync() { |
3526 |
< |
CompletableFuture<Integer> f, g; |
3527 |
< |
IntegerHandler r; |
3525 |
> |
public void testCompleteAsync3() { |
3526 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3527 |
> |
f.completeAsync(() -> 1, new ThreadExecutor()); |
3528 |
> |
f.join(); |
3529 |
> |
checkCompletedNormally(f, 1); |
3530 |
> |
} |
3531 |
|
|
3532 |
< |
f = new CompletableFuture<>(); |
3533 |
< |
g = f.handleAsync(r = new IntegerHandler()); |
3534 |
< |
assertFalse(r.ran); |
3535 |
< |
f.completeExceptionally(new CFException()); |
3532 |
> |
/** |
3533 |
> |
* completeAsync with given executor completes exceptionally if |
3534 |
> |
* given supplier throws |
3535 |
> |
*/ |
3536 |
> |
public void testCompleteAsync4() { |
3537 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3538 |
> |
CFException ex = new CFException(); |
3539 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor()); |
3540 |
> |
try { |
3541 |
> |
f.join(); |
3542 |
> |
shouldThrow(); |
3543 |
> |
} catch (Exception success) {} |
3544 |
|
checkCompletedWithWrappedCFException(f); |
3545 |
< |
checkCompletedNormally(g, three); |
3201 |
< |
assertTrue(r.ran); |
3545 |
> |
} |
3546 |
|
|
3547 |
< |
f = new CompletableFuture<>(); |
3548 |
< |
g = f.handleAsync(r = new IntegerHandler()); |
3549 |
< |
assertFalse(r.ran); |
3550 |
< |
f.completeExceptionally(new CFException()); |
3551 |
< |
checkCompletedWithWrappedCFException(f); |
3552 |
< |
checkCompletedNormally(g, three); |
3553 |
< |
assertTrue(r.ran); |
3547 |
> |
/** |
3548 |
> |
* orTimeout completes with TimeoutException if not complete |
3549 |
> |
*/ |
3550 |
> |
public void testOrTimeout() { |
3551 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3552 |
> |
f.orTimeout(SHORT_DELAY_MS, MILLISECONDS); |
3553 |
> |
checkCompletedExceptionallyWithTimeout(f); |
3554 |
> |
} |
3555 |
|
|
3556 |
< |
f = new CompletableFuture<>(); |
3557 |
< |
g = f.handleAsync(r = new IntegerHandler()); |
3558 |
< |
assertFalse(r.ran); |
3559 |
< |
f.complete(one); |
3560 |
< |
checkCompletedNormally(f, one); |
3561 |
< |
checkCompletedNormally(g, two); |
3562 |
< |
assertTrue(r.ran); |
3556 |
> |
/** |
3557 |
> |
* orTimeout completes normally if completed before timeout |
3558 |
> |
*/ |
3559 |
> |
public void testOrTimeout2() { |
3560 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3561 |
> |
f.complete(1); |
3562 |
> |
f.orTimeout(SHORT_DELAY_MS, MILLISECONDS); |
3563 |
> |
checkCompletedNormally(f, 1); |
3564 |
> |
} |
3565 |
|
|
3566 |
< |
f = new CompletableFuture<>(); |
3567 |
< |
g = f.handleAsync(r = new IntegerHandler()); |
3568 |
< |
assertFalse(r.ran); |
3569 |
< |
f.complete(one); |
3570 |
< |
checkCompletedNormally(f, one); |
3571 |
< |
checkCompletedNormally(g, two); |
3572 |
< |
assertTrue(r.ran); |
3566 |
> |
/** |
3567 |
> |
* completeOnTimeout completes with given value if not complete |
3568 |
> |
*/ |
3569 |
> |
public void testCompleteOnTimeout() { |
3570 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3571 |
> |
f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS); |
3572 |
> |
f.join(); |
3573 |
> |
checkCompletedNormally(f, -1); |
3574 |
|
} |
3575 |
|
|
3576 |
|
/** |
3577 |
< |
* handleAsync action with Executor completes normally with |
3230 |
< |
* function value on either normal or exceptional completion of |
3231 |
< |
* source |
3577 |
> |
* completeOnTimeout has no effect if completed within timeout |
3578 |
|
*/ |
3579 |
< |
public void testHandleAsync2() { |
3580 |
< |
CompletableFuture<Integer> f, g; |
3581 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
3582 |
< |
IntegerHandler r; |
3579 |
> |
public void testCompleteOnTimeout2() { |
3580 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3581 |
> |
f.complete(1); |
3582 |
> |
f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS); |
3583 |
> |
checkCompletedNormally(f, 1); |
3584 |
> |
} |
3585 |
|
|
3586 |
< |
f = new CompletableFuture<>(); |
3587 |
< |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3588 |
< |
assertFalse(r.ran); |
3589 |
< |
f.completeExceptionally(new CFException()); |
3590 |
< |
checkCompletedWithWrappedCFException(f); |
3591 |
< |
checkCompletedNormally(g, three); |
3592 |
< |
assertTrue(r.ran); |
3586 |
> |
/** |
3587 |
> |
* delayedExecutor returns an executor that delays submission |
3588 |
> |
*/ |
3589 |
> |
public void testDelayedExecutor() { |
3590 |
> |
long timeoutMillis = SMALL_DELAY_MS; |
3591 |
> |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3592 |
> |
MILLISECONDS); |
3593 |
> |
long startTime = System.nanoTime(); |
3594 |
> |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3595 |
> |
assertNull(f.getNow(null)); |
3596 |
> |
try { |
3597 |
> |
assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS)); |
3598 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
3599 |
> |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3600 |
> |
checkCompletedNormally(f, 1); |
3601 |
> |
} |
3602 |
|
|
3603 |
< |
f = new CompletableFuture<>(); |
3604 |
< |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3605 |
< |
assertFalse(r.ran); |
3606 |
< |
f.completeExceptionally(new CFException()); |
3607 |
< |
checkCompletedWithWrappedCFException(f); |
3608 |
< |
checkCompletedNormally(g, three); |
3609 |
< |
assertTrue(r.ran); |
3603 |
> |
/** |
3604 |
> |
* delayedExecutor for a given executor returns an executor that |
3605 |
> |
* delays submission |
3606 |
> |
*/ |
3607 |
> |
public void testDelayedExecutor2() { |
3608 |
> |
long timeoutMillis = SMALL_DELAY_MS; |
3609 |
> |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3610 |
> |
MILLISECONDS, |
3611 |
> |
new ThreadExecutor()); |
3612 |
> |
long startTime = System.nanoTime(); |
3613 |
> |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3614 |
> |
assertNull(f.getNow(null)); |
3615 |
> |
try { |
3616 |
> |
assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS)); |
3617 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
3618 |
> |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3619 |
> |
checkCompletedNormally(f, 1); |
3620 |
> |
} |
3621 |
|
|
3622 |
< |
f = new CompletableFuture<>(); |
3255 |
< |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3256 |
< |
assertFalse(r.ran); |
3257 |
< |
f.complete(one); |
3258 |
< |
checkCompletedNormally(f, one); |
3259 |
< |
checkCompletedNormally(g, two); |
3260 |
< |
assertTrue(r.ran); |
3622 |
> |
//--- tests of implementation details; not part of official tck --- |
3623 |
|
|
3624 |
< |
f = new CompletableFuture<>(); |
3625 |
< |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3626 |
< |
assertFalse(r.ran); |
3627 |
< |
f.complete(one); |
3628 |
< |
checkCompletedNormally(f, one); |
3629 |
< |
checkCompletedNormally(g, two); |
3630 |
< |
assertTrue(r.ran); |
3624 |
> |
Object resultOf(CompletableFuture<?> f) { |
3625 |
> |
try { |
3626 |
> |
java.lang.reflect.Field resultField |
3627 |
> |
= CompletableFuture.class.getDeclaredField("result"); |
3628 |
> |
resultField.setAccessible(true); |
3629 |
> |
return resultField.get(f); |
3630 |
> |
} catch (Throwable t) { throw new AssertionError(t); } |
3631 |
|
} |
3632 |
|
|
3633 |
+ |
public void testExceptionPropagationReusesResultObject() { |
3634 |
+ |
if (!testImplementationDetails) return; |
3635 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
3636 |
+ |
{ |
3637 |
+ |
final CFException ex = new CFException(); |
3638 |
+ |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3639 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3640 |
+ |
|
3641 |
+ |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3642 |
+ |
= new ArrayList<>(); |
3643 |
+ |
|
3644 |
+ |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3645 |
+ |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3646 |
+ |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3647 |
+ |
|
3648 |
+ |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3649 |
+ |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3650 |
+ |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3651 |
+ |
|
3652 |
+ |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3653 |
+ |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3654 |
+ |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3655 |
+ |
|
3656 |
+ |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3657 |
+ |
|
3658 |
+ |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3659 |
+ |
|
3660 |
+ |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3661 |
+ |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3662 |
+ |
|
3663 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3664 |
+ |
fun : funs) { |
3665 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3666 |
+ |
f.completeExceptionally(ex); |
3667 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3668 |
+ |
checkCompletedWithWrappedException(src, ex); |
3669 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3670 |
+ |
checkCompletedWithWrappedException(dep, ex); |
3671 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3672 |
+ |
} |
3673 |
+ |
|
3674 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3675 |
+ |
fun : funs) { |
3676 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3677 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3678 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3679 |
+ |
f.completeExceptionally(ex); |
3680 |
+ |
checkCompletedWithWrappedException(src, ex); |
3681 |
+ |
checkCompletedWithWrappedException(dep, ex); |
3682 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3683 |
+ |
} |
3684 |
+ |
|
3685 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3686 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3687 |
+ |
fun : funs) { |
3688 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3689 |
+ |
f.cancel(mayInterruptIfRunning); |
3690 |
+ |
checkCancelled(f); |
3691 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3692 |
+ |
checkCompletedWithWrappedCancellationException(src); |
3693 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3694 |
+ |
checkCompletedWithWrappedCancellationException(dep); |
3695 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3696 |
+ |
} |
3697 |
+ |
|
3698 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3699 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3700 |
+ |
fun : funs) { |
3701 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3702 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3703 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3704 |
+ |
f.cancel(mayInterruptIfRunning); |
3705 |
+ |
checkCancelled(f); |
3706 |
+ |
checkCompletedWithWrappedCancellationException(src); |
3707 |
+ |
checkCompletedWithWrappedCancellationException(dep); |
3708 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3709 |
+ |
} |
3710 |
+ |
}} |
3711 |
+ |
|
3712 |
|
} |