318 |
|
|
319 |
|
// Choose non-commutative actions for better coverage |
320 |
|
|
321 |
< |
// A non-commutative function that handles null values as well |
322 |
< |
public static int subtract(Integer x, Integer y) { |
323 |
< |
return ((x == null) ? 42 : x.intValue()) |
321 |
> |
// A non-commutative function that handles and produces null values as well. |
322 |
> |
static Integer subtract(Integer x, Integer y) { |
323 |
> |
return (x == null && y == null) ? null : |
324 |
> |
((x == null) ? 42 : x.intValue()) |
325 |
|
- ((y == null) ? 99 : y.intValue()); |
326 |
|
} |
327 |
|
|
328 |
+ |
// A function that handles and produces null values as well. |
329 |
+ |
static Integer inc(Integer x) { |
330 |
+ |
return (x == null) ? null : x + 1; |
331 |
+ |
} |
332 |
+ |
|
333 |
|
static final Supplier<Integer> supplyOne = |
334 |
|
() -> Integer.valueOf(1); |
335 |
|
static final Function<Integer, Integer> inc = |
337 |
|
static final BiFunction<Integer, Integer, Integer> subtract = |
338 |
|
(Integer x, Integer y) -> subtract(x, y); |
339 |
|
static final class IncAction implements Consumer<Integer> { |
340 |
< |
int value; |
341 |
< |
public void accept(Integer x) { value = x.intValue() + 1; } |
340 |
> |
int invocationCount = 0; |
341 |
> |
Integer value; |
342 |
> |
public boolean ran() { return invocationCount == 1; } |
343 |
> |
public void accept(Integer x) { |
344 |
> |
invocationCount++; |
345 |
> |
value = inc(x); |
346 |
> |
} |
347 |
> |
} |
348 |
> |
static final class IncFunction implements Function<Integer,Integer> { |
349 |
> |
int invocationCount = 0; |
350 |
> |
Integer value; |
351 |
> |
public boolean ran() { return invocationCount == 1; } |
352 |
> |
public Integer apply(Integer x) { |
353 |
> |
invocationCount++; |
354 |
> |
return value = inc(x); |
355 |
> |
} |
356 |
|
} |
357 |
|
static final class SubtractAction implements BiConsumer<Integer, Integer> { |
358 |
< |
volatile int invocationCount = 0; |
359 |
< |
int value; |
358 |
> |
int invocationCount = 0; |
359 |
> |
Integer value; |
360 |
|
// Check this action was invoked exactly once when result is computed. |
361 |
|
public boolean ran() { return invocationCount == 1; } |
362 |
|
public void accept(Integer x, Integer y) { |
365 |
|
} |
366 |
|
} |
367 |
|
static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> { |
368 |
< |
volatile int invocationCount = 0; |
369 |
< |
int value; |
368 |
> |
int invocationCount = 0; |
369 |
> |
Integer value; |
370 |
|
// Check this action was invoked exactly once when result is computed. |
371 |
|
public boolean ran() { return invocationCount == 1; } |
372 |
|
public Integer apply(Integer x, Integer y) { |
373 |
|
invocationCount++; |
374 |
< |
return subtract(x, y); |
374 |
> |
return value = subtract(x, y); |
375 |
|
} |
376 |
|
} |
377 |
|
static final class Noop implements Runnable { |
467 |
|
BiFunction<? super T,? super U,? extends V> a) { |
468 |
|
return f.thenCombine(g, a); |
469 |
|
} |
470 |
+ |
public <T,U> CompletableFuture<U> applyToEither |
471 |
+ |
(CompletableFuture<T> f, |
472 |
+ |
CompletionStage<? extends T> g, |
473 |
+ |
Function<? super T,U> a) { |
474 |
+ |
return f.applyToEither(g, a); |
475 |
+ |
} |
476 |
+ |
public <T> CompletableFuture<Void> acceptEither |
477 |
+ |
(CompletableFuture<T> f, |
478 |
+ |
CompletionStage<? extends T> g, |
479 |
+ |
Consumer<? super T> a) { |
480 |
+ |
return f.acceptEither(g, a); |
481 |
+ |
} |
482 |
+ |
public <T> CompletableFuture<Void> runAfterEither |
483 |
+ |
(CompletableFuture<T> f, |
484 |
+ |
CompletionStage<?> g, |
485 |
+ |
java.lang.Runnable a) { |
486 |
+ |
return f.runAfterEither(g, a); |
487 |
+ |
} |
488 |
+ |
public <T,U> CompletableFuture<U> thenCompose |
489 |
+ |
(CompletableFuture<T> f, |
490 |
+ |
Function<? super T,? extends CompletionStage<U>> a) { |
491 |
+ |
return f.thenCompose(a); |
492 |
+ |
} |
493 |
+ |
public <T> CompletableFuture<T> whenComplete |
494 |
+ |
(CompletableFuture<T> f, |
495 |
+ |
BiConsumer<? super T,? super Throwable> a) { |
496 |
+ |
return f.whenComplete(a); |
497 |
+ |
} |
498 |
|
}, |
499 |
|
|
500 |
|
// /** Experimental way to do more testing */ |
528 |
|
BiFunction<? super T,? super U,? extends V> a) { |
529 |
|
return f.thenCombineAsync(g, a); |
530 |
|
} |
531 |
+ |
public <T,U> CompletableFuture<U> applyToEither |
532 |
+ |
(CompletableFuture<T> f, |
533 |
+ |
CompletionStage<? extends T> g, |
534 |
+ |
Function<? super T,U> a) { |
535 |
+ |
return f.applyToEitherAsync(g, a); |
536 |
+ |
} |
537 |
+ |
public <T> CompletableFuture<Void> acceptEither |
538 |
+ |
(CompletableFuture<T> f, |
539 |
+ |
CompletionStage<? extends T> g, |
540 |
+ |
Consumer<? super T> a) { |
541 |
+ |
return f.acceptEitherAsync(g, a); |
542 |
+ |
} |
543 |
+ |
public <T> CompletableFuture<Void> runAfterEither |
544 |
+ |
(CompletableFuture<T> f, |
545 |
+ |
CompletionStage<?> g, |
546 |
+ |
java.lang.Runnable a) { |
547 |
+ |
return f.runAfterEitherAsync(g, a); |
548 |
+ |
} |
549 |
+ |
public <T,U> CompletableFuture<U> thenCompose |
550 |
+ |
(CompletableFuture<T> f, |
551 |
+ |
Function<? super T,? extends CompletionStage<U>> a) { |
552 |
+ |
return f.thenComposeAsync(a); |
553 |
+ |
} |
554 |
+ |
public <T> CompletableFuture<T> whenComplete |
555 |
+ |
(CompletableFuture<T> f, |
556 |
+ |
BiConsumer<? super T,? super Throwable> a) { |
557 |
+ |
return f.whenCompleteAsync(a); |
558 |
+ |
} |
559 |
|
}, |
560 |
|
|
561 |
|
// REVERSE_DEFAULT_ASYNC { |
588 |
|
BiFunction<? super T,? super U,? extends V> a) { |
589 |
|
return f.thenCombineAsync(g, a, new ThreadExecutor()); |
590 |
|
} |
591 |
+ |
public <T,U> CompletableFuture<U> applyToEither |
592 |
+ |
(CompletableFuture<T> f, |
593 |
+ |
CompletionStage<? extends T> g, |
594 |
+ |
Function<? super T,U> a) { |
595 |
+ |
return f.applyToEitherAsync(g, a, new ThreadExecutor()); |
596 |
+ |
} |
597 |
+ |
public <T> CompletableFuture<Void> acceptEither |
598 |
+ |
(CompletableFuture<T> f, |
599 |
+ |
CompletionStage<? extends T> g, |
600 |
+ |
Consumer<? super T> a) { |
601 |
+ |
return f.acceptEitherAsync(g, a, new ThreadExecutor()); |
602 |
+ |
} |
603 |
+ |
public <T> CompletableFuture<Void> runAfterEither |
604 |
+ |
(CompletableFuture<T> f, |
605 |
+ |
CompletionStage<?> g, |
606 |
+ |
java.lang.Runnable a) { |
607 |
+ |
return f.runAfterEitherAsync(g, a, new ThreadExecutor()); |
608 |
+ |
} |
609 |
+ |
public <T,U> CompletableFuture<U> thenCompose |
610 |
+ |
(CompletableFuture<T> f, |
611 |
+ |
Function<? super T,? extends CompletionStage<U>> a) { |
612 |
+ |
return f.thenComposeAsync(a, new ThreadExecutor()); |
613 |
+ |
} |
614 |
+ |
public <T> CompletableFuture<T> whenComplete |
615 |
+ |
(CompletableFuture<T> f, |
616 |
+ |
BiConsumer<? super T,? super Throwable> a) { |
617 |
+ |
return f.whenCompleteAsync(a, new ThreadExecutor()); |
618 |
+ |
} |
619 |
|
}; |
620 |
|
|
621 |
|
public abstract <T,U> CompletableFuture<Void> runAfterBoth |
628 |
|
(CompletableFuture<T> f, |
629 |
|
CompletionStage<? extends U> g, |
630 |
|
BiFunction<? super T,? super U,? extends V> a); |
631 |
+ |
public abstract <T,U> CompletableFuture<U> applyToEither |
632 |
+ |
(CompletableFuture<T> f, |
633 |
+ |
CompletionStage<? extends T> g, |
634 |
+ |
Function<? super T,U> a); |
635 |
+ |
public abstract <T> CompletableFuture<Void> acceptEither |
636 |
+ |
(CompletableFuture<T> f, |
637 |
+ |
CompletionStage<? extends T> g, |
638 |
+ |
Consumer<? super T> a); |
639 |
+ |
public abstract <T> CompletableFuture<Void> runAfterEither |
640 |
+ |
(CompletableFuture<T> f, |
641 |
+ |
CompletionStage<?> g, |
642 |
+ |
java.lang.Runnable a); |
643 |
+ |
public abstract <T,U> CompletableFuture<U> thenCompose |
644 |
+ |
(CompletableFuture<T> f, |
645 |
+ |
Function<? super T,? extends CompletionStage<U>> a); |
646 |
+ |
public abstract <T> CompletableFuture<T> whenComplete |
647 |
+ |
(CompletableFuture<T> f, |
648 |
+ |
BiConsumer<? super T,? super Throwable> a); |
649 |
+ |
|
650 |
+ |
|
651 |
|
} |
652 |
|
|
653 |
|
/** |
900 |
|
CompletableFuture<Void> g = f.thenAccept(r); |
901 |
|
f.complete(one); |
902 |
|
checkCompletedNormally(g, null); |
903 |
< |
assertEquals(r.value, 2); |
903 |
> |
assertEquals(r.value, (Integer) 2); |
904 |
|
} |
905 |
|
|
906 |
|
/** |
1054 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1055 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1056 |
|
final SubtractFunction r = new SubtractFunction(); |
1057 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract); |
1057 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1058 |
|
final CFException ex = new CFException(); |
1059 |
|
|
1060 |
|
g.completeExceptionally(ex); |
1099 |
|
|
1100 |
|
f.completeExceptionally(ex); |
1101 |
|
g.complete(v1); |
1102 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract); |
1102 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1103 |
|
|
1104 |
|
checkCompletedWithWrappedCFException(h, ex); |
1105 |
|
checkCompletedWithWrappedCFException(f, ex); |
1252 |
|
|
1253 |
|
f.complete(v1); |
1254 |
|
checkIncomplete(h); |
1255 |
< |
assertEquals(r.value, 0); |
1255 |
> |
assertFalse(r.ran()); |
1256 |
|
g.complete(v2); |
1257 |
|
|
1258 |
|
checkCompletedNormally(h, null); |
1274 |
|
|
1275 |
|
g.complete(v2); |
1276 |
|
checkIncomplete(h); |
1277 |
< |
assertEquals(r.value, 0); |
1277 |
> |
assertFalse(r.ran()); |
1278 |
|
f.complete(v1); |
1279 |
|
|
1280 |
|
checkCompletedNormally(h, null); |
1844 |
|
* applyToEither result completes normally after normal completion |
1845 |
|
* of either source |
1846 |
|
*/ |
1847 |
< |
public void testApplyToEither() { |
1848 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1849 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1850 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
1727 |
< |
f.complete(one); |
1728 |
< |
checkCompletedNormally(g, two); |
1729 |
< |
f2.complete(one); |
1730 |
< |
checkCompletedNormally(g, two); |
1847 |
> |
public void testApplyToEither_normalCompletion1() { |
1848 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1849 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1850 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
1851 |
|
|
1852 |
< |
f = new CompletableFuture<>(); |
1853 |
< |
f.complete(one); |
1854 |
< |
f2 = new CompletableFuture<>(); |
1855 |
< |
g = f.applyToEither(f2, inc); |
1856 |
< |
checkCompletedNormally(g, two); |
1852 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1853 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1854 |
> |
final IncFunction r = new IncFunction(); |
1855 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1856 |
> |
|
1857 |
> |
f.complete(v1); |
1858 |
> |
checkCompletedNormally(h, inc(v1)); |
1859 |
> |
g.complete(v2); |
1860 |
> |
|
1861 |
> |
checkCompletedNormally(f, v1); |
1862 |
> |
checkCompletedNormally(g, v2); |
1863 |
> |
checkCompletedNormally(h, inc(v1)); |
1864 |
> |
} |
1865 |
> |
} |
1866 |
> |
|
1867 |
> |
public void testApplyToEither_normalCompletion2() { |
1868 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1869 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1870 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
1871 |
> |
|
1872 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1873 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1874 |
> |
final IncFunction r = new IncFunction(); |
1875 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1876 |
> |
|
1877 |
> |
g.complete(v2); |
1878 |
> |
checkCompletedNormally(h, inc(v2)); |
1879 |
> |
f.complete(v1); |
1880 |
> |
|
1881 |
> |
checkCompletedNormally(f, v1); |
1882 |
> |
checkCompletedNormally(g, v2); |
1883 |
> |
checkCompletedNormally(h, inc(v2)); |
1884 |
> |
} |
1885 |
> |
} |
1886 |
> |
public void testApplyToEither_normalCompletion3() { |
1887 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1888 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1889 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
1890 |
> |
|
1891 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1892 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1893 |
> |
final IncFunction r = new IncFunction(); |
1894 |
> |
|
1895 |
> |
f.complete(v1); |
1896 |
> |
g.complete(v2); |
1897 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1898 |
> |
|
1899 |
> |
checkCompletedNormally(f, v1); |
1900 |
> |
checkCompletedNormally(g, v2); |
1901 |
> |
|
1902 |
> |
// unspecified behavior |
1903 |
> |
assertTrue(Objects.equals(h.join(), inc(v1)) || |
1904 |
> |
Objects.equals(h.join(), inc(v2))); |
1905 |
> |
} |
1906 |
|
} |
1907 |
|
|
1908 |
|
/** |
1909 |
|
* applyToEither result completes exceptionally after exceptional |
1910 |
|
* completion of either source |
1911 |
|
*/ |
1912 |
< |
public void testApplyToEither2() { |
1913 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1914 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1746 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
1747 |
< |
f.completeExceptionally(new CFException()); |
1748 |
< |
f2.complete(one); |
1749 |
< |
checkCompletedWithWrappedCFException(g); |
1912 |
> |
public void testApplyToEither_exceptionalCompletion1() { |
1913 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1914 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1915 |
|
|
1916 |
< |
f = new CompletableFuture<>(); |
1917 |
< |
f2 = new CompletableFuture<>(); |
1918 |
< |
f2.completeExceptionally(new CFException()); |
1919 |
< |
g = f.applyToEither(f2, inc); |
1920 |
< |
checkCompletedWithWrappedCFException(g); |
1916 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1917 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1918 |
> |
final IncFunction r = new IncFunction(); |
1919 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1920 |
> |
final CFException ex = new CFException(); |
1921 |
> |
|
1922 |
> |
f.completeExceptionally(ex); |
1923 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1924 |
> |
g.complete(v1); |
1925 |
> |
|
1926 |
> |
assertFalse(r.ran()); |
1927 |
> |
checkCompletedNormally(g, v1); |
1928 |
> |
checkCompletedWithWrappedCFException(f, ex); |
1929 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1930 |
> |
} |
1931 |
> |
} |
1932 |
> |
|
1933 |
> |
public void testApplyToEither_exceptionalCompletion2() { |
1934 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1935 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1936 |
> |
|
1937 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1938 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1939 |
> |
final IncFunction r = new IncFunction(); |
1940 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1941 |
> |
final CFException ex = new CFException(); |
1942 |
> |
|
1943 |
> |
g.completeExceptionally(ex); |
1944 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1945 |
> |
f.complete(v1); |
1946 |
> |
|
1947 |
> |
assertFalse(r.ran()); |
1948 |
> |
checkCompletedNormally(f, v1); |
1949 |
> |
checkCompletedWithWrappedCFException(g, ex); |
1950 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1951 |
> |
} |
1952 |
> |
} |
1953 |
> |
|
1954 |
> |
public void testApplyToEither_exceptionalCompletion3() { |
1955 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1956 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1957 |
> |
|
1958 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1959 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1960 |
> |
final IncFunction r = new IncFunction(); |
1961 |
> |
final CFException ex = new CFException(); |
1962 |
> |
|
1963 |
> |
g.completeExceptionally(ex); |
1964 |
> |
f.complete(v1); |
1965 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1966 |
> |
|
1967 |
> |
// unspecified behavior |
1968 |
> |
Integer v; |
1969 |
> |
try { |
1970 |
> |
assertEquals(h.join(), inc(v1)); |
1971 |
> |
assertTrue(r.ran()); |
1972 |
> |
} catch (CompletionException ok) { |
1973 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1974 |
> |
assertFalse(r.ran()); |
1975 |
> |
} |
1976 |
> |
|
1977 |
> |
checkCompletedWithWrappedCFException(g, ex); |
1978 |
> |
checkCompletedNormally(f, v1); |
1979 |
> |
} |
1980 |
> |
} |
1981 |
> |
|
1982 |
> |
public void testApplyToEither_exceptionalCompletion4() { |
1983 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1984 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1985 |
> |
|
1986 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1987 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1988 |
> |
final IncFunction r = new IncFunction(); |
1989 |
> |
final CFException ex = new CFException(); |
1990 |
> |
|
1991 |
> |
f.completeExceptionally(ex); |
1992 |
> |
g.complete(v1); |
1993 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1994 |
> |
|
1995 |
> |
// unspecified behavior |
1996 |
> |
Integer v; |
1997 |
> |
try { |
1998 |
> |
assertEquals(h.join(), inc(v1)); |
1999 |
> |
assertTrue(r.ran()); |
2000 |
> |
} catch (CompletionException ok) { |
2001 |
> |
checkCompletedWithWrappedCFException(h, ex); |
2002 |
> |
assertFalse(r.ran()); |
2003 |
> |
} |
2004 |
> |
|
2005 |
> |
checkCompletedWithWrappedCFException(f, ex); |
2006 |
> |
assertFalse(r.ran()); |
2007 |
> |
checkCompletedNormally(g, v1); |
2008 |
> |
} |
2009 |
|
} |
2010 |
|
|
2011 |
|
/** |
2012 |
|
* applyToEither result completes exceptionally if action does |
2013 |
|
*/ |
2014 |
< |
public void testApplyToEither3() { |
2015 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2016 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2017 |
< |
FailingFunction r = new FailingFunction(); |
2018 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, r); |
2019 |
< |
f2.complete(two); |
2020 |
< |
checkCompletedWithWrappedCFException(g); |
2014 |
> |
public void testApplyToEither_actionFailed1() { |
2015 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2016 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2017 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
2018 |
> |
|
2019 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2020 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2021 |
> |
final FailingFunction r = new FailingFunction(); |
2022 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2023 |
> |
|
2024 |
> |
f.complete(v1); |
2025 |
> |
checkCompletedWithWrappedCFException(h); |
2026 |
> |
g.complete(v2); |
2027 |
> |
checkCompletedNormally(f, v1); |
2028 |
> |
checkCompletedNormally(g, v2); |
2029 |
> |
} |
2030 |
> |
} |
2031 |
> |
|
2032 |
> |
public void testApplyToEither_actionFailed2() { |
2033 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2034 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2035 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
2036 |
> |
|
2037 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2038 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2039 |
> |
final FailingFunction r = new FailingFunction(); |
2040 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2041 |
> |
|
2042 |
> |
g.complete(v2); |
2043 |
> |
checkCompletedWithWrappedCFException(h); |
2044 |
> |
f.complete(v1); |
2045 |
> |
checkCompletedNormally(f, v1); |
2046 |
> |
checkCompletedNormally(g, v2); |
2047 |
> |
} |
2048 |
|
} |
2049 |
|
|
2050 |
|
/** |
2051 |
|
* applyToEither result completes exceptionally if either source cancelled |
2052 |
|
*/ |
2053 |
< |
public void testApplyToEither4() { |
2054 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2055 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2056 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
2057 |
< |
assertTrue(f.cancel(true)); |
2058 |
< |
checkCompletedWithWrappedCancellationException(g); |
2059 |
< |
f = new CompletableFuture<>(); |
2060 |
< |
f2 = new CompletableFuture<>(); |
2061 |
< |
assertTrue(f2.cancel(true)); |
2062 |
< |
checkCompletedWithWrappedCancellationException(g); |
2053 |
> |
public void testApplyToEither_sourceCancelled1() { |
2054 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2055 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2056 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
2057 |
> |
|
2058 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2059 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2060 |
> |
final IncFunction r = new IncFunction(); |
2061 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2062 |
> |
|
2063 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2064 |
> |
checkCompletedWithWrappedCancellationException(h); |
2065 |
> |
g.complete(v1); |
2066 |
> |
|
2067 |
> |
checkCancelled(f); |
2068 |
> |
assertFalse(r.ran()); |
2069 |
> |
checkCompletedNormally(g, v1); |
2070 |
> |
checkCompletedWithWrappedCancellationException(h); |
2071 |
> |
} |
2072 |
> |
} |
2073 |
> |
|
2074 |
> |
public void testApplyToEither_sourceCancelled2() { |
2075 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2076 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2077 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
2078 |
> |
|
2079 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2080 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2081 |
> |
final IncFunction r = new IncFunction(); |
2082 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2083 |
> |
|
2084 |
> |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2085 |
> |
checkCompletedWithWrappedCancellationException(h); |
2086 |
> |
f.complete(v1); |
2087 |
> |
|
2088 |
> |
checkCancelled(g); |
2089 |
> |
assertFalse(r.ran()); |
2090 |
> |
checkCompletedNormally(f, v1); |
2091 |
> |
checkCompletedWithWrappedCancellationException(h); |
2092 |
> |
} |
2093 |
> |
} |
2094 |
> |
|
2095 |
> |
public void testApplyToEither_sourceCancelled3() { |
2096 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2097 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2098 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
2099 |
> |
|
2100 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2101 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2102 |
> |
final IncFunction r = new IncFunction(); |
2103 |
> |
|
2104 |
> |
assertTrue(g.cancel(mayInterruptIfRunning)); |
2105 |
> |
f.complete(v1); |
2106 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2107 |
> |
|
2108 |
> |
// unspecified behavior |
2109 |
> |
Integer v; |
2110 |
> |
try { |
2111 |
> |
assertEquals(h.join(), inc(v1)); |
2112 |
> |
assertTrue(r.ran()); |
2113 |
> |
} catch (CompletionException ok) { |
2114 |
> |
checkCompletedWithWrappedCancellationException(h); |
2115 |
> |
assertFalse(r.ran()); |
2116 |
> |
} |
2117 |
> |
|
2118 |
> |
checkCancelled(g); |
2119 |
> |
checkCompletedNormally(f, v1); |
2120 |
> |
} |
2121 |
> |
} |
2122 |
> |
|
2123 |
> |
public void testApplyToEither_sourceCancelled4() { |
2124 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2125 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2126 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
2127 |
> |
|
2128 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2129 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2130 |
> |
final IncFunction r = new IncFunction(); |
2131 |
> |
|
2132 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2133 |
> |
g.complete(v1); |
2134 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2135 |
> |
|
2136 |
> |
// unspecified behavior |
2137 |
> |
Integer v; |
2138 |
> |
try { |
2139 |
> |
assertEquals(h.join(), inc(v1)); |
2140 |
> |
assertTrue(r.ran()); |
2141 |
> |
} catch (CompletionException ok) { |
2142 |
> |
checkCompletedWithWrappedCancellationException(h); |
2143 |
> |
assertFalse(r.ran()); |
2144 |
> |
} |
2145 |
> |
|
2146 |
> |
checkCancelled(f); |
2147 |
> |
checkCompletedNormally(g, v1); |
2148 |
> |
} |
2149 |
|
} |
2150 |
|
|
2151 |
|
/** |
2161 |
|
checkCompletedNormally(g, null); |
2162 |
|
f2.complete(one); |
2163 |
|
checkCompletedNormally(g, null); |
2164 |
< |
assertEquals(r.value, 2); |
2164 |
> |
assertEquals(r.value, (Integer) 2); |
2165 |
|
|
2166 |
|
r = new IncAction(); |
2167 |
|
f = new CompletableFuture<>(); |
2169 |
|
f2 = new CompletableFuture<>(); |
2170 |
|
g = f.acceptEither(f2, r); |
2171 |
|
checkCompletedNormally(g, null); |
2172 |
< |
assertEquals(r.value, 2); |
2172 |
> |
assertEquals(r.value, (Integer) 2); |
2173 |
|
} |
2174 |
|
|
2175 |
|
/** |
2479 |
|
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
2480 |
|
f.complete(one); |
2481 |
|
checkCompletedNormally(g, null); |
2482 |
< |
assertEquals(r.value, 2); |
2482 |
> |
assertEquals(r.value, (Integer) 2); |
2483 |
|
} |
2484 |
|
|
2485 |
|
/** |
2517 |
|
} |
2518 |
|
|
2519 |
|
/** |
2154 |
– |
* applyToEitherAsync result completes normally after normal |
2155 |
– |
* completion of sources |
2156 |
– |
*/ |
2157 |
– |
public void testApplyToEitherAsync() { |
2158 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2159 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2160 |
– |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
2161 |
– |
f.complete(one); |
2162 |
– |
checkCompletedNormally(g, two); |
2163 |
– |
|
2164 |
– |
f = new CompletableFuture<>(); |
2165 |
– |
f.complete(one); |
2166 |
– |
f2 = new CompletableFuture<>(); |
2167 |
– |
g = f.applyToEitherAsync(f2, inc); |
2168 |
– |
checkCompletedNormally(g, two); |
2169 |
– |
} |
2170 |
– |
|
2171 |
– |
/** |
2172 |
– |
* applyToEitherAsync result completes exceptionally after exceptional |
2173 |
– |
* completion of source |
2174 |
– |
*/ |
2175 |
– |
public void testApplyToEitherAsync2() { |
2176 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2177 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2178 |
– |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
2179 |
– |
f.completeExceptionally(new CFException()); |
2180 |
– |
checkCompletedWithWrappedCFException(g); |
2181 |
– |
|
2182 |
– |
f = new CompletableFuture<>(); |
2183 |
– |
f2 = new CompletableFuture<>(); |
2184 |
– |
f2.completeExceptionally(new CFException()); |
2185 |
– |
g = f.applyToEitherAsync(f2, inc); |
2186 |
– |
f.complete(one); |
2187 |
– |
checkCompletedWithWrappedCFException(g); |
2188 |
– |
} |
2189 |
– |
|
2190 |
– |
/** |
2191 |
– |
* applyToEitherAsync result completes exceptionally if action does |
2192 |
– |
*/ |
2193 |
– |
public void testApplyToEitherAsync3() { |
2194 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2195 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2196 |
– |
FailingFunction r = new FailingFunction(); |
2197 |
– |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r); |
2198 |
– |
f.complete(one); |
2199 |
– |
checkCompletedWithWrappedCFException(g); |
2200 |
– |
} |
2201 |
– |
|
2202 |
– |
/** |
2203 |
– |
* applyToEitherAsync result completes exceptionally if either source cancelled |
2204 |
– |
*/ |
2205 |
– |
public void testApplyToEitherAsync4() { |
2206 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2207 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2208 |
– |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
2209 |
– |
assertTrue(f.cancel(true)); |
2210 |
– |
checkCompletedWithWrappedCancellationException(g); |
2211 |
– |
|
2212 |
– |
f = new CompletableFuture<>(); |
2213 |
– |
f2 = new CompletableFuture<>(); |
2214 |
– |
assertTrue(f2.cancel(true)); |
2215 |
– |
g = f.applyToEitherAsync(f2, inc); |
2216 |
– |
checkCompletedWithWrappedCancellationException(g); |
2217 |
– |
} |
2218 |
– |
|
2219 |
– |
/** |
2520 |
|
* acceptEitherAsync result completes normally after normal |
2521 |
|
* completion of sources |
2522 |
|
*/ |
2527 |
|
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
2528 |
|
f.complete(one); |
2529 |
|
checkCompletedNormally(g, null); |
2530 |
< |
assertEquals(r.value, 2); |
2530 |
> |
assertEquals(r.value, (Integer) 2); |
2531 |
|
|
2532 |
|
r = new IncAction(); |
2533 |
|
f = new CompletableFuture<>(); |
2535 |
|
f2 = new CompletableFuture<>(); |
2536 |
|
g = f.acceptEitherAsync(f2, r); |
2537 |
|
checkCompletedNormally(g, null); |
2538 |
< |
assertEquals(r.value, 2); |
2538 |
> |
assertEquals(r.value, (Integer) 2); |
2539 |
|
} |
2540 |
|
|
2541 |
|
/** |
2852 |
|
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
2853 |
|
f.complete(one); |
2854 |
|
checkCompletedNormally(g, null); |
2855 |
< |
assertEquals(r.value, 2); |
2855 |
> |
assertEquals(r.value, (Integer) 2); |
2856 |
|
} |
2857 |
|
|
2858 |
|
/** |
2890 |
|
} |
2891 |
|
|
2892 |
|
/** |
2593 |
– |
* applyToEitherAsync result completes normally after normal |
2594 |
– |
* completion of sources |
2595 |
– |
*/ |
2596 |
– |
public void testApplyToEitherAsyncE() { |
2597 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2598 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2599 |
– |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2600 |
– |
f.complete(one); |
2601 |
– |
checkCompletedNormally(g, two); |
2602 |
– |
|
2603 |
– |
f = new CompletableFuture<>(); |
2604 |
– |
f.complete(one); |
2605 |
– |
f2 = new CompletableFuture<>(); |
2606 |
– |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2607 |
– |
checkCompletedNormally(g, two); |
2608 |
– |
} |
2609 |
– |
|
2610 |
– |
/** |
2611 |
– |
* applyToEitherAsync result completes exceptionally after exceptional |
2612 |
– |
* completion of source |
2613 |
– |
*/ |
2614 |
– |
public void testApplyToEitherAsync2E() { |
2615 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2616 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2617 |
– |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2618 |
– |
f.completeExceptionally(new CFException()); |
2619 |
– |
checkCompletedWithWrappedCFException(g); |
2620 |
– |
|
2621 |
– |
f = new CompletableFuture<>(); |
2622 |
– |
f2 = new CompletableFuture<>(); |
2623 |
– |
f2.completeExceptionally(new CFException()); |
2624 |
– |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2625 |
– |
f.complete(one); |
2626 |
– |
checkCompletedWithWrappedCFException(g); |
2627 |
– |
} |
2628 |
– |
|
2629 |
– |
/** |
2630 |
– |
* applyToEitherAsync result completes exceptionally if action does |
2631 |
– |
*/ |
2632 |
– |
public void testApplyToEitherAsync3E() { |
2633 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2634 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2635 |
– |
FailingFunction r = new FailingFunction(); |
2636 |
– |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor()); |
2637 |
– |
f.complete(one); |
2638 |
– |
checkCompletedWithWrappedCFException(g); |
2639 |
– |
} |
2640 |
– |
|
2641 |
– |
/** |
2642 |
– |
* applyToEitherAsync result completes exceptionally if either source cancelled |
2643 |
– |
*/ |
2644 |
– |
public void testApplyToEitherAsync4E() { |
2645 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2646 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2647 |
– |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2648 |
– |
assertTrue(f.cancel(true)); |
2649 |
– |
checkCompletedWithWrappedCancellationException(g); |
2650 |
– |
|
2651 |
– |
f = new CompletableFuture<>(); |
2652 |
– |
f2 = new CompletableFuture<>(); |
2653 |
– |
assertTrue(f2.cancel(true)); |
2654 |
– |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2655 |
– |
checkCompletedWithWrappedCancellationException(g); |
2656 |
– |
} |
2657 |
– |
|
2658 |
– |
/** |
2893 |
|
* acceptEitherAsync result completes normally after normal |
2894 |
|
* completion of sources |
2895 |
|
*/ |
2900 |
|
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2901 |
|
f.complete(one); |
2902 |
|
checkCompletedNormally(g, null); |
2903 |
< |
assertEquals(r.value, 2); |
2903 |
> |
assertEquals(r.value, (Integer) 2); |
2904 |
|
|
2905 |
|
r = new IncAction(); |
2906 |
|
f = new CompletableFuture<>(); |
2908 |
|
f2 = new CompletableFuture<>(); |
2909 |
|
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2910 |
|
checkCompletedNormally(g, null); |
2911 |
< |
assertEquals(r.value, 2); |
2911 |
> |
assertEquals(r.value, (Integer) 2); |
2912 |
|
} |
2913 |
|
|
2914 |
|
/** |