320 |
|
checkCompletedNormally(f, "test"); |
321 |
|
} |
322 |
|
|
323 |
< |
// Choose non-commutative actions for better coverage |
324 |
< |
|
325 |
< |
// A non-commutative function that handles and produces null values as well. |
326 |
< |
static Integer subtract(Integer x, Integer y) { |
327 |
< |
return (x == null && y == null) ? null : |
328 |
< |
((x == null) ? 42 : x.intValue()) |
329 |
< |
- ((y == null) ? 99 : y.intValue()); |
323 |
> |
static final class IntegerSupplier implements Supplier<Integer> { |
324 |
> |
final ExecutionMode m; |
325 |
> |
int invocationCount = 0; |
326 |
> |
final Integer value; |
327 |
> |
IntegerSupplier(ExecutionMode m, Integer value) { |
328 |
> |
this.m = m; |
329 |
> |
this.value = value; |
330 |
> |
} |
331 |
> |
public Integer get() { |
332 |
> |
m.checkExecutionMode(); |
333 |
> |
invocationCount++; |
334 |
> |
return value; |
335 |
> |
} |
336 |
|
} |
337 |
|
|
338 |
|
// A function that handles and produces null values as well. |
340 |
|
return (x == null) ? null : x + 1; |
341 |
|
} |
342 |
|
|
337 |
– |
static final Supplier<Integer> supplyOne = |
338 |
– |
() -> Integer.valueOf(1); |
339 |
– |
static final Function<Integer, Integer> inc = |
340 |
– |
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
341 |
– |
static final BiFunction<Integer, Integer, Integer> subtract = |
342 |
– |
(Integer x, Integer y) -> subtract(x, y); |
343 |
|
static final class IncAction implements Consumer<Integer> { |
344 |
|
int invocationCount = 0; |
345 |
|
Integer value; |
359 |
|
return value = inc(x); |
360 |
|
} |
361 |
|
} |
362 |
+ |
|
363 |
+ |
// Choose non-commutative actions for better coverage |
364 |
+ |
// A non-commutative function that handles and produces null values as well. |
365 |
+ |
static Integer subtract(Integer x, Integer y) { |
366 |
+ |
return (x == null && y == null) ? null : |
367 |
+ |
((x == null) ? 42 : x.intValue()) |
368 |
+ |
- ((y == null) ? 99 : y.intValue()); |
369 |
+ |
} |
370 |
+ |
|
371 |
|
static final class SubtractAction implements BiConsumer<Integer, Integer> { |
372 |
|
final ExecutionMode m; |
373 |
|
int invocationCount = 0; |
392 |
|
return value = subtract(x, y); |
393 |
|
} |
394 |
|
} |
395 |
+ |
|
396 |
|
static final class Noop implements Runnable { |
397 |
|
final ExecutionMode m; |
398 |
|
int invocationCount = 0; |
506 |
|
|
507 |
|
/** |
508 |
|
* Permits the testing of parallel code for the 3 different |
509 |
< |
* execution modes without repeating all the testing code. |
509 |
> |
* execution modes without copy/pasting all the test methods. |
510 |
|
*/ |
511 |
|
enum ExecutionMode { |
512 |
|
DEFAULT { |
513 |
|
public void checkExecutionMode() { |
514 |
+ |
assertFalse(ThreadExecutor.startedCurrentThread()); |
515 |
|
assertNull(ForkJoinTask.getPool()); |
516 |
|
} |
517 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
518 |
|
throw new UnsupportedOperationException(); |
519 |
|
} |
520 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
521 |
+ |
throw new UnsupportedOperationException(); |
522 |
+ |
} |
523 |
|
public <T> CompletableFuture<Void> thenRun |
524 |
|
(CompletableFuture<T> f, Runnable a) { |
525 |
|
return f.thenRun(a); |
591 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
592 |
|
return CompletableFuture.runAsync(a); |
593 |
|
} |
594 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
595 |
+ |
return CompletableFuture.supplyAsync(a); |
596 |
+ |
} |
597 |
|
public <T> CompletableFuture<Void> thenRun |
598 |
|
(CompletableFuture<T> f, Runnable a) { |
599 |
|
return f.thenRunAsync(a); |
664 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
665 |
|
return CompletableFuture.runAsync(a, new ThreadExecutor()); |
666 |
|
} |
667 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
668 |
+ |
return CompletableFuture.supplyAsync(a, new ThreadExecutor()); |
669 |
+ |
} |
670 |
|
public <T> CompletableFuture<Void> thenRun |
671 |
|
(CompletableFuture<T> f, Runnable a) { |
672 |
|
return f.thenRunAsync(a, new ThreadExecutor()); |
732 |
|
|
733 |
|
public abstract void checkExecutionMode(); |
734 |
|
public abstract CompletableFuture<Void> runAsync(Runnable a); |
735 |
+ |
public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); |
736 |
|
public abstract <T> CompletableFuture<Void> thenRun |
737 |
|
(CompletableFuture<T> f, Runnable a); |
738 |
|
public abstract <T> CompletableFuture<Void> thenAccept |
1019 |
|
/** |
1020 |
|
* supplyAsync completes with result of supplier |
1021 |
|
*/ |
1022 |
< |
public void testSupplyAsync() { |
1023 |
< |
CompletableFuture<Integer> f; |
1024 |
< |
f = CompletableFuture.supplyAsync(supplyOne); |
1025 |
< |
assertEquals(f.join(), one); |
1026 |
< |
checkCompletedNormally(f, one); |
1027 |
< |
} |
1028 |
< |
|
1029 |
< |
/** |
1030 |
< |
* supplyAsync with executor completes with result of supplier |
1031 |
< |
*/ |
1032 |
< |
public void testSupplyAsync2() { |
1033 |
< |
CompletableFuture<Integer> f; |
1034 |
< |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
1035 |
< |
assertEquals(f.join(), one); |
1015 |
< |
checkCompletedNormally(f, one); |
1016 |
< |
} |
1022 |
> |
public void testSupplyAsync_normalCompletion() { |
1023 |
> |
ExecutionMode[] executionModes = { |
1024 |
> |
ExecutionMode.ASYNC, |
1025 |
> |
ExecutionMode.EXECUTOR, |
1026 |
> |
}; |
1027 |
> |
for (ExecutionMode m : executionModes) |
1028 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1029 |
> |
{ |
1030 |
> |
final IntegerSupplier r = new IntegerSupplier(m, v1); |
1031 |
> |
final CompletableFuture<Integer> f = m.supplyAsync(r); |
1032 |
> |
assertSame(v1, f.join()); |
1033 |
> |
checkCompletedNormally(f, v1); |
1034 |
> |
assertEquals(1, r.invocationCount); |
1035 |
> |
}} |
1036 |
|
|
1037 |
|
/** |
1038 |
|
* Failing supplyAsync completes exceptionally |
1039 |
|
*/ |
1040 |
< |
public void testSupplyAsync3() { |
1041 |
< |
FailingSupplier r = new FailingSupplier(ExecutionMode.ASYNC); |
1042 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r); |
1040 |
> |
public void testSupplyAsync_exceptionalCompletion() { |
1041 |
> |
ExecutionMode[] executionModes = { |
1042 |
> |
ExecutionMode.ASYNC, |
1043 |
> |
ExecutionMode.EXECUTOR, |
1044 |
> |
}; |
1045 |
> |
for (ExecutionMode m : executionModes) |
1046 |
> |
{ |
1047 |
> |
FailingSupplier r = new FailingSupplier(m); |
1048 |
> |
CompletableFuture<Integer> f = m.supplyAsync(r); |
1049 |
|
checkCompletedWithWrappedCFException(f); |
1050 |
|
assertEquals(1, r.invocationCount); |
1051 |
< |
} |
1051 |
> |
}} |
1052 |
|
|
1053 |
|
// seq completion methods |
1054 |
|
|
2616 |
|
*/ |
2617 |
|
public void testAllOf_normal() throws Exception { |
2618 |
|
for (int k = 1; k < 20; ++k) { |
2619 |
< |
CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2619 |
> |
CompletableFuture<Integer>[] fs |
2620 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2621 |
|
for (int i = 0; i < k; ++i) |
2622 |
|
fs[i] = new CompletableFuture<>(); |
2623 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2631 |
|
} |
2632 |
|
} |
2633 |
|
|
2634 |
+ |
public void testAllOf_backwards() throws Exception { |
2635 |
+ |
for (int k = 1; k < 20; ++k) { |
2636 |
+ |
CompletableFuture<Integer>[] fs |
2637 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2638 |
+ |
for (int i = 0; i < k; ++i) |
2639 |
+ |
fs[i] = new CompletableFuture<>(); |
2640 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2641 |
+ |
for (int i = k - 1; i >= 0; i--) { |
2642 |
+ |
checkIncomplete(f); |
2643 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
2644 |
+ |
fs[i].complete(one); |
2645 |
+ |
} |
2646 |
+ |
checkCompletedNormally(f, null); |
2647 |
+ |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
2648 |
+ |
} |
2649 |
+ |
} |
2650 |
+ |
|
2651 |
|
/** |
2652 |
|
* anyOf(no component futures) returns an incomplete future |
2653 |
|
*/ |
2706 |
|
Runnable[] throwingActions = { |
2707 |
|
() -> CompletableFuture.supplyAsync(null), |
2708 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
2709 |
< |
() -> CompletableFuture.supplyAsync(supplyOne, null), |
2709 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
2710 |
|
|
2711 |
|
() -> CompletableFuture.runAsync(null), |
2712 |
|
() -> CompletableFuture.runAsync(null, exec), |