7 |
|
|
8 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
+ |
import static java.util.concurrent.CompletableFuture.completedFuture; |
11 |
+ |
import static java.util.concurrent.CompletableFuture.failedFuture; |
12 |
+ |
|
13 |
+ |
import java.lang.reflect.Method; |
14 |
+ |
import java.lang.reflect.Modifier; |
15 |
+ |
|
16 |
+ |
import java.util.stream.Collectors; |
17 |
+ |
import java.util.stream.Stream; |
18 |
|
|
19 |
|
import java.util.ArrayList; |
20 |
+ |
import java.util.Arrays; |
21 |
|
import java.util.List; |
22 |
|
import java.util.Objects; |
23 |
+ |
import java.util.Set; |
24 |
|
import java.util.concurrent.Callable; |
25 |
|
import java.util.concurrent.CancellationException; |
26 |
|
import java.util.concurrent.CompletableFuture; |
38 |
|
import java.util.function.BiFunction; |
39 |
|
import java.util.function.Consumer; |
40 |
|
import java.util.function.Function; |
41 |
+ |
import java.util.function.Predicate; |
42 |
|
import java.util.function.Supplier; |
43 |
|
|
44 |
+ |
import junit.framework.AssertionFailedError; |
45 |
|
import junit.framework.Test; |
46 |
|
import junit.framework.TestSuite; |
47 |
|
|
89 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
90 |
|
} |
91 |
|
|
92 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
93 |
< |
long startTime = System.nanoTime(); |
94 |
< |
long timeoutMillis = LONG_DELAY_MS; |
95 |
< |
try { |
96 |
< |
f.get(timeoutMillis, MILLISECONDS); |
97 |
< |
shouldThrow(); |
98 |
< |
} catch (ExecutionException success) { |
99 |
< |
assertTrue(success.getCause() instanceof CFException); |
100 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
101 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
102 |
< |
|
103 |
< |
try { |
104 |
< |
f.join(); |
105 |
< |
shouldThrow(); |
106 |
< |
} catch (CompletionException success) { |
107 |
< |
assertTrue(success.getCause() instanceof CFException); |
108 |
< |
} |
97 |
< |
try { |
98 |
< |
f.getNow(null); |
99 |
< |
shouldThrow(); |
100 |
< |
} catch (CompletionException success) { |
101 |
< |
assertTrue(success.getCause() instanceof CFException); |
92 |
> |
/** |
93 |
> |
* Returns the "raw" internal exceptional completion of f, |
94 |
> |
* without any additional wrapping with CompletionException. |
95 |
> |
*/ |
96 |
> |
<U> Throwable exceptionalCompletion(CompletableFuture<U> f) { |
97 |
> |
// handle (and whenComplete) can distinguish between "direct" |
98 |
> |
// and "wrapped" exceptional completion |
99 |
> |
return f.handle((U u, Throwable t) -> t).join(); |
100 |
> |
} |
101 |
> |
|
102 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, |
103 |
> |
boolean wrapped, |
104 |
> |
Consumer<Throwable> checker) { |
105 |
> |
Throwable cause = exceptionalCompletion(f); |
106 |
> |
if (wrapped) { |
107 |
> |
assertTrue(cause instanceof CompletionException); |
108 |
> |
cause = cause.getCause(); |
109 |
|
} |
110 |
< |
try { |
104 |
< |
f.get(); |
105 |
< |
shouldThrow(); |
106 |
< |
} catch (ExecutionException success) { |
107 |
< |
assertTrue(success.getCause() instanceof CFException); |
108 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
109 |
< |
assertTrue(f.isDone()); |
110 |
< |
assertFalse(f.isCancelled()); |
111 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
112 |
< |
} |
110 |
> |
checker.accept(cause); |
111 |
|
|
114 |
– |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
115 |
– |
Throwable ex) { |
112 |
|
long startTime = System.nanoTime(); |
117 |
– |
long timeoutMillis = LONG_DELAY_MS; |
113 |
|
try { |
114 |
< |
f.get(timeoutMillis, MILLISECONDS); |
114 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
115 |
|
shouldThrow(); |
116 |
|
} catch (ExecutionException success) { |
117 |
< |
assertSame(ex, success.getCause()); |
117 |
> |
assertSame(cause, success.getCause()); |
118 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
119 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
119 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
120 |
|
|
121 |
|
try { |
122 |
|
f.join(); |
123 |
|
shouldThrow(); |
124 |
|
} catch (CompletionException success) { |
125 |
< |
assertSame(ex, success.getCause()); |
126 |
< |
} |
125 |
> |
assertSame(cause, success.getCause()); |
126 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
127 |
> |
|
128 |
|
try { |
129 |
|
f.getNow(null); |
130 |
|
shouldThrow(); |
131 |
|
} catch (CompletionException success) { |
132 |
< |
assertSame(ex, success.getCause()); |
133 |
< |
} |
132 |
> |
assertSame(cause, success.getCause()); |
133 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
> |
|
135 |
|
try { |
136 |
|
f.get(); |
137 |
|
shouldThrow(); |
138 |
|
} catch (ExecutionException success) { |
139 |
< |
assertSame(ex, success.getCause()); |
139 |
> |
assertSame(cause, success.getCause()); |
140 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
141 |
|
|
145 |
– |
assertTrue(f.isDone()); |
142 |
|
assertFalse(f.isCancelled()); |
143 |
+ |
assertTrue(f.isDone()); |
144 |
+ |
assertTrue(f.isCompletedExceptionally()); |
145 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
146 |
|
} |
147 |
|
|
148 |
< |
<U> void checkCompletedExceptionallyWithTimeout(CompletableFuture<U> f) { |
149 |
< |
long startTime = System.nanoTime(); |
150 |
< |
long timeoutMillis = LONG_DELAY_MS; |
151 |
< |
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 |
< |
} |
148 |
> |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
149 |
> |
checkCompletedExceptionally(f, true, |
150 |
> |
(t) -> assertTrue(t instanceof CFException)); |
151 |
> |
} |
152 |
|
|
153 |
< |
try { |
154 |
< |
f.get(); |
155 |
< |
shouldThrow(); |
156 |
< |
} catch (ExecutionException ex) { |
179 |
< |
assertTrue(ex.getCause() instanceof TimeoutException); |
180 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
153 |
> |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
154 |
> |
checkCompletedExceptionally(f, true, |
155 |
> |
(t) -> assertTrue(t instanceof CancellationException)); |
156 |
> |
} |
157 |
|
|
158 |
< |
assertTrue(f.isDone()); |
159 |
< |
assertFalse(f.isCancelled()); |
160 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
158 |
> |
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
159 |
> |
checkCompletedExceptionally(f, false, |
160 |
> |
(t) -> assertTrue(t instanceof TimeoutException)); |
161 |
|
} |
162 |
|
|
163 |
< |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
164 |
< |
Throwable ex) { |
165 |
< |
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); } |
163 |
> |
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
164 |
> |
Throwable ex) { |
165 |
> |
checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex)); |
166 |
|
} |
167 |
|
|
168 |
< |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
169 |
< |
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); } |
168 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
169 |
> |
checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex)); |
170 |
|
} |
171 |
|
|
172 |
|
void checkCancelled(CompletableFuture<?> f) { |
173 |
|
long startTime = System.nanoTime(); |
209 |
– |
long timeoutMillis = LONG_DELAY_MS; |
174 |
|
try { |
175 |
< |
f.get(timeoutMillis, MILLISECONDS); |
175 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
176 |
|
shouldThrow(); |
177 |
|
} catch (CancellationException success) { |
178 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
179 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
179 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
180 |
|
|
181 |
|
try { |
182 |
|
f.join(); |
191 |
|
shouldThrow(); |
192 |
|
} catch (CancellationException success) { |
193 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
230 |
– |
assertTrue(f.isDone()); |
231 |
– |
assertTrue(f.isCompletedExceptionally()); |
232 |
– |
assertTrue(f.isCancelled()); |
233 |
– |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
234 |
– |
} |
194 |
|
|
195 |
< |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
237 |
< |
long startTime = System.nanoTime(); |
238 |
< |
long timeoutMillis = LONG_DELAY_MS; |
239 |
< |
try { |
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); |
195 |
> |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
196 |
|
|
247 |
– |
try { |
248 |
– |
f.join(); |
249 |
– |
shouldThrow(); |
250 |
– |
} catch (CompletionException success) { |
251 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
252 |
– |
} |
253 |
– |
try { |
254 |
– |
f.getNow(null); |
255 |
– |
shouldThrow(); |
256 |
– |
} catch (CompletionException success) { |
257 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
258 |
– |
} |
259 |
– |
try { |
260 |
– |
f.get(); |
261 |
– |
shouldThrow(); |
262 |
– |
} catch (ExecutionException success) { |
263 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
264 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
197 |
|
assertTrue(f.isDone()); |
266 |
– |
assertFalse(f.isCancelled()); |
198 |
|
assertTrue(f.isCompletedExceptionally()); |
199 |
+ |
assertTrue(f.isCancelled()); |
200 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
201 |
|
} |
202 |
|
|
640 |
|
|
641 |
|
ASYNC { |
642 |
|
public void checkExecutionMode() { |
643 |
< |
assertEquals(defaultExecutorIsCommonPool, |
644 |
< |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
643 |
> |
// If tests are added that may run across different |
644 |
> |
// pools, this needs to be weakened to no-op. |
645 |
> |
ForkJoinPool p = ForkJoinTask.getPool(); |
646 |
> |
assertTrue(p == null || |
647 |
> |
(defaultExecutorIsCommonPool && |
648 |
> |
p == ForkJoinPool.commonPool())); |
649 |
|
} |
650 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
651 |
|
return CompletableFuture.runAsync(a); |
844 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
845 |
|
final CompletableFuture<Integer> g = f.exceptionally |
846 |
|
((Throwable t) -> { |
911 |
– |
// Should not be called |
847 |
|
a.getAndIncrement(); |
848 |
< |
throw new AssertionError(); |
848 |
> |
threadFail("should not be called"); |
849 |
> |
return null; // unreached |
850 |
|
}); |
851 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
852 |
|
|
880 |
|
assertEquals(1, a.get()); |
881 |
|
}} |
882 |
|
|
883 |
+ |
/** |
884 |
+ |
* If an "exceptionally action" throws an exception, it completes |
885 |
+ |
* exceptionally with that exception |
886 |
+ |
*/ |
887 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
888 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
889 |
|
{ |
902 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
903 |
|
|
904 |
|
checkCompletedWithWrappedException(g, ex2); |
905 |
+ |
checkCompletedExceptionally(f, ex1); |
906 |
|
assertEquals(1, a.get()); |
907 |
|
}} |
908 |
|
|
910 |
|
* whenComplete action executes on normal completion, propagating |
911 |
|
* source result. |
912 |
|
*/ |
913 |
< |
public void testWhenComplete_normalCompletion1() { |
913 |
> |
public void testWhenComplete_normalCompletion() { |
914 |
|
for (ExecutionMode m : ExecutionMode.values()) |
915 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
916 |
|
for (Integer v1 : new Integer[] { 1, null }) |
920 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
921 |
|
final CompletableFuture<Integer> g = m.whenComplete |
922 |
|
(f, |
923 |
< |
(Integer x, Throwable t) -> { |
923 |
> |
(Integer result, Throwable t) -> { |
924 |
|
m.checkExecutionMode(); |
925 |
< |
threadAssertSame(x, v1); |
925 |
> |
threadAssertSame(result, v1); |
926 |
|
threadAssertNull(t); |
927 |
|
a.getAndIncrement(); |
928 |
|
}); |
947 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
948 |
|
final CompletableFuture<Integer> g = m.whenComplete |
949 |
|
(f, |
950 |
< |
(Integer x, Throwable t) -> { |
950 |
> |
(Integer result, Throwable t) -> { |
951 |
|
m.checkExecutionMode(); |
952 |
< |
threadAssertNull(x); |
952 |
> |
threadAssertNull(result); |
953 |
|
threadAssertSame(t, ex); |
954 |
|
a.getAndIncrement(); |
955 |
|
}); |
974 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
975 |
|
final CompletableFuture<Integer> g = m.whenComplete |
976 |
|
(f, |
977 |
< |
(Integer x, Throwable t) -> { |
977 |
> |
(Integer result, Throwable t) -> { |
978 |
|
m.checkExecutionMode(); |
979 |
< |
threadAssertNull(x); |
979 |
> |
threadAssertNull(result); |
980 |
|
threadAssertTrue(t instanceof CancellationException); |
981 |
|
a.getAndIncrement(); |
982 |
|
}); |
991 |
|
* If a whenComplete action throws an exception when triggered by |
992 |
|
* a normal completion, it completes exceptionally |
993 |
|
*/ |
994 |
< |
public void testWhenComplete_actionFailed() { |
994 |
> |
public void testWhenComplete_sourceCompletedNormallyActionFailed() { |
995 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
996 |
|
for (ExecutionMode m : ExecutionMode.values()) |
997 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1002 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1003 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1004 |
|
(f, |
1005 |
< |
(Integer x, Throwable t) -> { |
1005 |
> |
(Integer result, Throwable t) -> { |
1006 |
|
m.checkExecutionMode(); |
1007 |
< |
threadAssertSame(x, v1); |
1007 |
> |
threadAssertSame(result, v1); |
1008 |
|
threadAssertNull(t); |
1009 |
|
a.getAndIncrement(); |
1010 |
|
throw ex; |
1019 |
|
/** |
1020 |
|
* If a whenComplete action throws an exception when triggered by |
1021 |
|
* a source completion that also throws an exception, the source |
1022 |
< |
* exception takes precedence. |
1022 |
> |
* exception takes precedence (unlike handle) |
1023 |
|
*/ |
1024 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
1024 |
> |
public void testWhenComplete_sourceFailedActionFailed() { |
1025 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1026 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1027 |
|
{ |
1033 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
1034 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1035 |
|
(f, |
1036 |
< |
(Integer x, Throwable t) -> { |
1036 |
> |
(Integer result, Throwable t) -> { |
1037 |
|
m.checkExecutionMode(); |
1038 |
|
threadAssertSame(t, ex1); |
1039 |
< |
threadAssertNull(x); |
1039 |
> |
threadAssertNull(result); |
1040 |
|
a.getAndIncrement(); |
1041 |
|
throw ex2; |
1042 |
|
}); |
1044 |
|
|
1045 |
|
checkCompletedWithWrappedException(g, ex1); |
1046 |
|
checkCompletedExceptionally(f, ex1); |
1047 |
+ |
if (testImplementationDetails) { |
1048 |
+ |
assertEquals(1, ex1.getSuppressed().length); |
1049 |
+ |
assertSame(ex2, ex1.getSuppressed()[0]); |
1050 |
+ |
} |
1051 |
|
assertEquals(1, a.get()); |
1052 |
|
}} |
1053 |
|
|
1065 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1066 |
|
final CompletableFuture<Integer> g = m.handle |
1067 |
|
(f, |
1068 |
< |
(Integer x, Throwable t) -> { |
1068 |
> |
(Integer result, Throwable t) -> { |
1069 |
|
m.checkExecutionMode(); |
1070 |
< |
threadAssertSame(x, v1); |
1070 |
> |
threadAssertSame(result, v1); |
1071 |
|
threadAssertNull(t); |
1072 |
|
a.getAndIncrement(); |
1073 |
|
return inc(v1); |
1094 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1095 |
|
final CompletableFuture<Integer> g = m.handle |
1096 |
|
(f, |
1097 |
< |
(Integer x, Throwable t) -> { |
1097 |
> |
(Integer result, Throwable t) -> { |
1098 |
|
m.checkExecutionMode(); |
1099 |
< |
threadAssertNull(x); |
1099 |
> |
threadAssertNull(result); |
1100 |
|
threadAssertSame(t, ex); |
1101 |
|
a.getAndIncrement(); |
1102 |
|
return v1; |
1123 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1124 |
|
final CompletableFuture<Integer> g = m.handle |
1125 |
|
(f, |
1126 |
< |
(Integer x, Throwable t) -> { |
1126 |
> |
(Integer result, Throwable t) -> { |
1127 |
|
m.checkExecutionMode(); |
1128 |
< |
threadAssertNull(x); |
1128 |
> |
threadAssertNull(result); |
1129 |
|
threadAssertTrue(t instanceof CancellationException); |
1130 |
|
a.getAndIncrement(); |
1131 |
|
return v1; |
1138 |
|
}} |
1139 |
|
|
1140 |
|
/** |
1141 |
< |
* handle result completes exceptionally if action does |
1141 |
> |
* If a "handle action" throws an exception when triggered by |
1142 |
> |
* a normal completion, it completes exceptionally |
1143 |
|
*/ |
1144 |
< |
public void testHandle_sourceFailedActionFailed() { |
1144 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1145 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1146 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1147 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1148 |
|
{ |
1149 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1150 |
|
final AtomicInteger a = new AtomicInteger(0); |
1151 |
< |
final CFException ex1 = new CFException(); |
1152 |
< |
final CFException ex2 = new CFException(); |
1206 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
1151 |
> |
final CFException ex = new CFException(); |
1152 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1153 |
|
final CompletableFuture<Integer> g = m.handle |
1154 |
|
(f, |
1155 |
< |
(Integer x, Throwable t) -> { |
1155 |
> |
(Integer result, Throwable t) -> { |
1156 |
|
m.checkExecutionMode(); |
1157 |
< |
threadAssertNull(x); |
1158 |
< |
threadAssertSame(ex1, t); |
1157 |
> |
threadAssertSame(result, v1); |
1158 |
> |
threadAssertNull(t); |
1159 |
|
a.getAndIncrement(); |
1160 |
< |
throw ex2; |
1160 |
> |
throw ex; |
1161 |
|
}); |
1162 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
1162 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1163 |
|
|
1164 |
< |
checkCompletedWithWrappedException(g, ex2); |
1165 |
< |
checkCompletedExceptionally(f, ex1); |
1164 |
> |
checkCompletedWithWrappedException(g, ex); |
1165 |
> |
checkCompletedNormally(f, v1); |
1166 |
|
assertEquals(1, a.get()); |
1167 |
|
}} |
1168 |
|
|
1169 |
< |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1170 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1169 |
> |
/** |
1170 |
> |
* If a "handle action" throws an exception when triggered by |
1171 |
> |
* a source completion that also throws an exception, the action |
1172 |
> |
* exception takes precedence (unlike whenComplete) |
1173 |
> |
*/ |
1174 |
> |
public void testHandle_sourceFailedActionFailed() { |
1175 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1176 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1176 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1177 |
|
{ |
1228 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1178 |
|
final AtomicInteger a = new AtomicInteger(0); |
1179 |
< |
final CFException ex = new CFException(); |
1180 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1179 |
> |
final CFException ex1 = new CFException(); |
1180 |
> |
final CFException ex2 = new CFException(); |
1181 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1182 |
> |
|
1183 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1184 |
|
final CompletableFuture<Integer> g = m.handle |
1185 |
|
(f, |
1186 |
< |
(Integer x, Throwable t) -> { |
1186 |
> |
(Integer result, Throwable t) -> { |
1187 |
|
m.checkExecutionMode(); |
1188 |
< |
threadAssertSame(x, v1); |
1189 |
< |
threadAssertNull(t); |
1188 |
> |
threadAssertNull(result); |
1189 |
> |
threadAssertSame(ex1, t); |
1190 |
|
a.getAndIncrement(); |
1191 |
< |
throw ex; |
1191 |
> |
throw ex2; |
1192 |
|
}); |
1193 |
< |
if (createIncomplete) assertTrue(f.complete(v1)); |
1193 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1194 |
|
|
1195 |
< |
checkCompletedWithWrappedException(g, ex); |
1196 |
< |
checkCompletedNormally(f, v1); |
1195 |
> |
checkCompletedWithWrappedException(g, ex2); |
1196 |
> |
checkCompletedExceptionally(f, ex1); |
1197 |
|
assertEquals(1, a.get()); |
1198 |
|
}} |
1199 |
|
|
3094 |
|
for (int i = 0; i < k; i++) { |
3095 |
|
checkIncomplete(f); |
3096 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3097 |
< |
if (i != k/2) { |
3097 |
> |
if (i != k / 2) { |
3098 |
|
fs[i].complete(i); |
3099 |
|
checkCompletedNormally(fs[i], i); |
3100 |
|
} else { |
3297 |
|
() -> CompletableFuture.anyOf(null, f), |
3298 |
|
|
3299 |
|
() -> f.obtrudeException(null), |
3300 |
+ |
|
3301 |
+ |
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3302 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3303 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null), |
3304 |
+ |
|
3305 |
+ |
() -> f.orTimeout(1L, null), |
3306 |
+ |
() -> f.completeOnTimeout(42, 1L, null), |
3307 |
+ |
|
3308 |
+ |
() -> CompletableFuture.failedFuture(null), |
3309 |
+ |
() -> CompletableFuture.failedStage(null), |
3310 |
|
}; |
3311 |
|
|
3312 |
|
assertThrows(NullPointerException.class, throwingActions); |
3327 |
|
* newIncompleteFuture returns an incomplete CompletableFuture |
3328 |
|
*/ |
3329 |
|
public void testNewIncompleteFuture() { |
3330 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3331 |
+ |
{ |
3332 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3333 |
|
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3334 |
|
checkIncomplete(f); |
3335 |
|
checkIncomplete(g); |
3336 |
< |
} |
3336 |
> |
f.complete(v1); |
3337 |
> |
checkCompletedNormally(f, v1); |
3338 |
> |
checkIncomplete(g); |
3339 |
> |
g.complete(v1); |
3340 |
> |
checkCompletedNormally(g, v1); |
3341 |
> |
assertSame(g.getClass(), CompletableFuture.class); |
3342 |
> |
}} |
3343 |
|
|
3344 |
|
/** |
3345 |
|
* completedStage returns a completed CompletionStage |
3346 |
|
*/ |
3347 |
|
public void testCompletedStage() { |
3348 |
< |
AtomicInteger x = new AtomicInteger(); |
3348 |
> |
AtomicInteger x = new AtomicInteger(0); |
3349 |
|
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3350 |
|
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3351 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3363 |
|
Executor c = ForkJoinPool.commonPool(); |
3364 |
|
if (ForkJoinPool.getCommonPoolParallelism() > 1) |
3365 |
|
assertSame(e, c); |
3366 |
+ |
else |
3367 |
+ |
assertNotSame(e, c); |
3368 |
|
} |
3369 |
|
|
3370 |
|
/** |
3374 |
|
public void testFailedFuture() { |
3375 |
|
CFException ex = new CFException(); |
3376 |
|
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3377 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
3377 |
> |
checkCompletedExceptionally(f, ex); |
3378 |
|
} |
3379 |
|
|
3380 |
|
/** |
3381 |
|
* failedFuture(null) throws NPE |
3382 |
|
*/ |
3383 |
< |
public void testFailedFuture2() { |
3383 |
> |
public void testFailedFuture_null() { |
3384 |
|
try { |
3385 |
|
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3386 |
|
shouldThrow(); |
3413 |
|
CFException ex = new CFException(); |
3414 |
|
f.completeExceptionally(ex); |
3415 |
|
checkCompletedExceptionally(f, ex); |
3416 |
< |
checkCompletedWithWrappedCFException(g); |
3416 |
> |
checkCompletedWithWrappedException(g, ex); |
3417 |
|
} |
3418 |
|
|
3419 |
|
/** |
3423 |
|
public void testMinimalCompletionStage() { |
3424 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3425 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3426 |
< |
AtomicInteger x = new AtomicInteger(); |
3426 |
> |
AtomicInteger x = new AtomicInteger(0); |
3427 |
|
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3428 |
|
checkIncomplete(f); |
3429 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3440 |
|
public void testMinimalCompletionStage2() { |
3441 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3442 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3443 |
< |
AtomicInteger x = new AtomicInteger(); |
3443 |
> |
AtomicInteger x = new AtomicInteger(0); |
3444 |
|
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3445 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3446 |
|
checkIncomplete(f); |
3458 |
|
public void testFailedStage() { |
3459 |
|
CFException ex = new CFException(); |
3460 |
|
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3461 |
< |
AtomicInteger x = new AtomicInteger(); |
3461 |
> |
AtomicInteger x = new AtomicInteger(0); |
3462 |
|
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3463 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3464 |
|
assertEquals(x.get(), 0); |
3465 |
< |
assertEquals(r.get().getCause(), ex); |
3465 |
> |
assertEquals(r.get(), ex); |
3466 |
|
} |
3467 |
|
|
3468 |
|
/** |
3469 |
|
* completeAsync completes with value of given supplier |
3470 |
|
*/ |
3471 |
|
public void testCompleteAsync() { |
3472 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3473 |
+ |
{ |
3474 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3475 |
< |
f.completeAsync(() -> 1); |
3475 |
> |
f.completeAsync(() -> v1); |
3476 |
|
f.join(); |
3477 |
< |
checkCompletedNormally(f, 1); |
3478 |
< |
} |
3477 |
> |
checkCompletedNormally(f, v1); |
3478 |
> |
}} |
3479 |
|
|
3480 |
|
/** |
3481 |
|
* completeAsync completes exceptionally if given supplier throws |
3487 |
|
try { |
3488 |
|
f.join(); |
3489 |
|
shouldThrow(); |
3490 |
< |
} catch (Exception success) {} |
3491 |
< |
checkCompletedWithWrappedCFException(f); |
3490 |
> |
} catch (CompletionException success) {} |
3491 |
> |
checkCompletedWithWrappedException(f, ex); |
3492 |
|
} |
3493 |
|
|
3494 |
|
/** |
3495 |
|
* completeAsync with given executor completes with value of given supplier |
3496 |
|
*/ |
3497 |
|
public void testCompleteAsync3() { |
3498 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3499 |
+ |
{ |
3500 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3501 |
< |
f.completeAsync(() -> 1, new ThreadExecutor()); |
3502 |
< |
f.join(); |
3503 |
< |
checkCompletedNormally(f, 1); |
3504 |
< |
} |
3501 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3502 |
> |
f.completeAsync(() -> v1, executor); |
3503 |
> |
assertSame(v1, f.join()); |
3504 |
> |
checkCompletedNormally(f, v1); |
3505 |
> |
assertEquals(1, executor.count.get()); |
3506 |
> |
}} |
3507 |
|
|
3508 |
|
/** |
3509 |
|
* completeAsync with given executor completes exceptionally if |
3512 |
|
public void testCompleteAsync4() { |
3513 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3514 |
|
CFException ex = new CFException(); |
3515 |
< |
f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor()); |
3515 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3516 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3517 |
|
try { |
3518 |
|
f.join(); |
3519 |
|
shouldThrow(); |
3520 |
< |
} catch (Exception success) {} |
3521 |
< |
checkCompletedWithWrappedCFException(f); |
3520 |
> |
} catch (CompletionException success) {} |
3521 |
> |
checkCompletedWithWrappedException(f, ex); |
3522 |
> |
assertEquals(1, executor.count.get()); |
3523 |
|
} |
3524 |
|
|
3525 |
|
/** |
3526 |
|
* orTimeout completes with TimeoutException if not complete |
3527 |
|
*/ |
3528 |
< |
public void testOrTimeout() { |
3528 |
> |
public void testOrTimeout_timesOut() { |
3529 |
> |
long timeoutMillis = timeoutMillis(); |
3530 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3531 |
< |
f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3532 |
< |
checkCompletedExceptionallyWithTimeout(f); |
3531 |
> |
long startTime = System.nanoTime(); |
3532 |
> |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3533 |
> |
checkCompletedWithTimeoutException(f); |
3534 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3535 |
|
} |
3536 |
|
|
3537 |
|
/** |
3538 |
|
* orTimeout completes normally if completed before timeout |
3539 |
|
*/ |
3540 |
< |
public void testOrTimeout2() { |
3540 |
> |
public void testOrTimeout_completed() { |
3541 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3542 |
> |
{ |
3543 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3544 |
< |
f.complete(1); |
3545 |
< |
f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3546 |
< |
checkCompletedNormally(f, 1); |
3547 |
< |
} |
3544 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3545 |
> |
long startTime = System.nanoTime(); |
3546 |
> |
f.complete(v1); |
3547 |
> |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3548 |
> |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3549 |
> |
g.complete(v1); |
3550 |
> |
checkCompletedNormally(f, v1); |
3551 |
> |
checkCompletedNormally(g, v1); |
3552 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3553 |
> |
}} |
3554 |
|
|
3555 |
|
/** |
3556 |
|
* completeOnTimeout completes with given value if not complete |
3557 |
|
*/ |
3558 |
< |
public void testCompleteOnTimeout() { |
3558 |
> |
public void testCompleteOnTimeout_timesOut() { |
3559 |
> |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3560 |
> |
() -> testCompleteOnTimeout_timesOut(null)); |
3561 |
> |
} |
3562 |
> |
|
3563 |
> |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3564 |
> |
long timeoutMillis = timeoutMillis(); |
3565 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3566 |
< |
f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3567 |
< |
f.join(); |
3568 |
< |
checkCompletedNormally(f, -1); |
3566 |
> |
long startTime = System.nanoTime(); |
3567 |
> |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3568 |
> |
assertSame(v, f.join()); |
3569 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3570 |
> |
f.complete(99); // should have no effect |
3571 |
> |
checkCompletedNormally(f, v); |
3572 |
|
} |
3573 |
|
|
3574 |
|
/** |
3575 |
|
* completeOnTimeout has no effect if completed within timeout |
3576 |
|
*/ |
3577 |
< |
public void testCompleteOnTimeout2() { |
3577 |
> |
public void testCompleteOnTimeout_completed() { |
3578 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3579 |
> |
{ |
3580 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3581 |
< |
f.complete(1); |
3582 |
< |
f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3583 |
< |
checkCompletedNormally(f, 1); |
3584 |
< |
} |
3581 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3582 |
> |
long startTime = System.nanoTime(); |
3583 |
> |
f.complete(v1); |
3584 |
> |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3585 |
> |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3586 |
> |
g.complete(v1); |
3587 |
> |
checkCompletedNormally(f, v1); |
3588 |
> |
checkCompletedNormally(g, v1); |
3589 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3590 |
> |
}} |
3591 |
|
|
3592 |
|
/** |
3593 |
|
* delayedExecutor returns an executor that delays submission |
3594 |
|
*/ |
3595 |
|
public void testDelayedExecutor() { |
3596 |
< |
long timeoutMillis = SMALL_DELAY_MS; |
3597 |
< |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3598 |
< |
MILLISECONDS); |
3596 |
> |
testInParallel(() -> testDelayedExecutor(null, null), |
3597 |
> |
() -> testDelayedExecutor(null, 1), |
3598 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3599 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3600 |
> |
} |
3601 |
> |
|
3602 |
> |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3603 |
> |
long timeoutMillis = timeoutMillis(); |
3604 |
> |
// Use an "unreasonably long" long timeout to catch lingering threads |
3605 |
> |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3606 |
> |
final Executor delayer, longDelayer; |
3607 |
> |
if (executor == null) { |
3608 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3609 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3610 |
> |
} else { |
3611 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3612 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3613 |
> |
} |
3614 |
|
long startTime = System.nanoTime(); |
3615 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3616 |
< |
assertNull(f.getNow(null)); |
3617 |
< |
try { |
3618 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
3619 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
3620 |
< |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3621 |
< |
checkCompletedNormally(f, 1); |
3622 |
< |
} |
3615 |
> |
CompletableFuture<Integer> f = |
3616 |
> |
CompletableFuture.supplyAsync(() -> v, delayer); |
3617 |
> |
CompletableFuture<Integer> g = |
3618 |
> |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3619 |
> |
|
3620 |
> |
assertNull(g.getNow(null)); |
3621 |
> |
|
3622 |
> |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3623 |
> |
long millisElapsed = millisElapsedSince(startTime); |
3624 |
> |
assertTrue(millisElapsed >= timeoutMillis); |
3625 |
> |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3626 |
|
|
3627 |
< |
/** |
3628 |
< |
* delayedExecutor for a given executor returns an executor that |
3629 |
< |
* delays submission |
3630 |
< |
*/ |
3605 |
< |
public void testDelayedExecutor2() { |
3606 |
< |
long timeoutMillis = SMALL_DELAY_MS; |
3607 |
< |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3608 |
< |
MILLISECONDS, |
3609 |
< |
new ThreadExecutor()); |
3610 |
< |
long startTime = System.nanoTime(); |
3611 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3612 |
< |
assertNull(f.getNow(null)); |
3613 |
< |
try { |
3614 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
3615 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
3616 |
< |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3617 |
< |
checkCompletedNormally(f, 1); |
3627 |
> |
checkCompletedNormally(f, v); |
3628 |
> |
|
3629 |
> |
checkIncomplete(g); |
3630 |
> |
assertTrue(g.cancel(true)); |
3631 |
|
} |
3632 |
|
|
3633 |
|
//--- tests of implementation details; not part of official tck --- |
3664 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3665 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3666 |
|
|
3667 |
< |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3667 |
> |
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3668 |
|
|
3669 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3670 |
|
|
3720 |
|
} |
3721 |
|
}} |
3722 |
|
|
3723 |
+ |
/** |
3724 |
+ |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3725 |
+ |
*/ |
3726 |
+ |
public void testMinimalCompletionStage_minimality() { |
3727 |
+ |
if (!testImplementationDetails) return; |
3728 |
+ |
Function<Method, String> toSignature = |
3729 |
+ |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3730 |
+ |
Predicate<Method> isNotStatic = |
3731 |
+ |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3732 |
+ |
List<Method> minimalMethods = |
3733 |
+ |
Stream.of(Object.class, CompletionStage.class) |
3734 |
+ |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3735 |
+ |
.filter(isNotStatic) |
3736 |
+ |
.collect(Collectors.toList()); |
3737 |
+ |
// Methods from CompletableFuture permitted NOT to throw UOE |
3738 |
+ |
String[] signatureWhitelist = { |
3739 |
+ |
"newIncompleteFuture[]", |
3740 |
+ |
"defaultExecutor[]", |
3741 |
+ |
"minimalCompletionStage[]", |
3742 |
+ |
"copy[]", |
3743 |
+ |
}; |
3744 |
+ |
Set<String> permittedMethodSignatures = |
3745 |
+ |
Stream.concat(minimalMethods.stream().map(toSignature), |
3746 |
+ |
Stream.of(signatureWhitelist)) |
3747 |
+ |
.collect(Collectors.toSet()); |
3748 |
+ |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3749 |
+ |
.filter(isNotStatic) |
3750 |
+ |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3751 |
+ |
.collect(Collectors.toList()); |
3752 |
+ |
|
3753 |
+ |
CompletionStage<Integer> minimalStage = |
3754 |
+ |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3755 |
+ |
|
3756 |
+ |
List<Method> bugs = new ArrayList<>(); |
3757 |
+ |
for (Method method : allMethods) { |
3758 |
+ |
Class<?>[] parameterTypes = method.getParameterTypes(); |
3759 |
+ |
Object[] args = new Object[parameterTypes.length]; |
3760 |
+ |
// Manufacture boxed primitives for primitive params |
3761 |
+ |
for (int i = 0; i < args.length; i++) { |
3762 |
+ |
Class<?> type = parameterTypes[i]; |
3763 |
+ |
if (parameterTypes[i] == boolean.class) |
3764 |
+ |
args[i] = false; |
3765 |
+ |
else if (parameterTypes[i] == int.class) |
3766 |
+ |
args[i] = 0; |
3767 |
+ |
else if (parameterTypes[i] == long.class) |
3768 |
+ |
args[i] = 0L; |
3769 |
+ |
} |
3770 |
+ |
try { |
3771 |
+ |
method.invoke(minimalStage, args); |
3772 |
+ |
bugs.add(method); |
3773 |
+ |
} |
3774 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3775 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3776 |
+ |
bugs.add(method); |
3777 |
+ |
// expected.getCause().printStackTrace(); |
3778 |
+ |
} |
3779 |
+ |
} |
3780 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3781 |
+ |
} |
3782 |
+ |
if (!bugs.isEmpty()) |
3783 |
+ |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
3784 |
+ |
} |
3785 |
+ |
|
3786 |
+ |
static class Monad { |
3787 |
+ |
static class ZeroException extends RuntimeException { |
3788 |
+ |
public ZeroException() { super("monadic zero"); } |
3789 |
+ |
} |
3790 |
+ |
// "return", "unit" |
3791 |
+ |
static <T> CompletableFuture<T> unit(T value) { |
3792 |
+ |
return completedFuture(value); |
3793 |
+ |
} |
3794 |
+ |
// monadic zero ? |
3795 |
+ |
static <T> CompletableFuture<T> zero() { |
3796 |
+ |
return failedFuture(new ZeroException()); |
3797 |
+ |
} |
3798 |
+ |
// >=> |
3799 |
+ |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
3800 |
+ |
(Function<T, CompletableFuture<U>> f, |
3801 |
+ |
Function<U, CompletableFuture<V>> g) { |
3802 |
+ |
return (x) -> f.apply(x).thenCompose(g); |
3803 |
+ |
} |
3804 |
+ |
|
3805 |
+ |
static void assertZero(CompletableFuture<?> f) { |
3806 |
+ |
try { |
3807 |
+ |
f.getNow(null); |
3808 |
+ |
throw new AssertionFailedError("should throw"); |
3809 |
+ |
} catch (CompletionException success) { |
3810 |
+ |
assertTrue(success.getCause() instanceof ZeroException); |
3811 |
+ |
} |
3812 |
+ |
} |
3813 |
+ |
|
3814 |
+ |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
3815 |
+ |
CompletableFuture<T> g) { |
3816 |
+ |
T fval = null, gval = null; |
3817 |
+ |
Throwable fex = null, gex = null; |
3818 |
+ |
|
3819 |
+ |
try { fval = f.get(); } |
3820 |
+ |
catch (ExecutionException ex) { fex = ex.getCause(); } |
3821 |
+ |
catch (Throwable ex) { fex = ex; } |
3822 |
+ |
|
3823 |
+ |
try { gval = g.get(); } |
3824 |
+ |
catch (ExecutionException ex) { gex = ex.getCause(); } |
3825 |
+ |
catch (Throwable ex) { gex = ex; } |
3826 |
+ |
|
3827 |
+ |
if (fex != null || gex != null) |
3828 |
+ |
assertSame(fex.getClass(), gex.getClass()); |
3829 |
+ |
else |
3830 |
+ |
assertEquals(fval, gval); |
3831 |
+ |
} |
3832 |
+ |
|
3833 |
+ |
static class PlusFuture<T> extends CompletableFuture<T> { |
3834 |
+ |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
3835 |
+ |
} |
3836 |
+ |
|
3837 |
+ |
/** Implements "monadic plus". */ |
3838 |
+ |
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
3839 |
+ |
CompletableFuture<? extends T> g) { |
3840 |
+ |
PlusFuture<T> plus = new PlusFuture<T>(); |
3841 |
+ |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3842 |
+ |
try { |
3843 |
+ |
if (ex == null) { |
3844 |
+ |
if (plus.complete(result)) |
3845 |
+ |
if (plus.firstFailure.get() != null) |
3846 |
+ |
plus.firstFailure.set(null); |
3847 |
+ |
} |
3848 |
+ |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3849 |
+ |
if (plus.isDone()) |
3850 |
+ |
plus.firstFailure.set(null); |
3851 |
+ |
} |
3852 |
+ |
else { |
3853 |
+ |
// first failure has precedence |
3854 |
+ |
Throwable first = plus.firstFailure.getAndSet(null); |
3855 |
+ |
|
3856 |
+ |
// may fail with "Self-suppression not permitted" |
3857 |
+ |
try { first.addSuppressed(ex); } |
3858 |
+ |
catch (Exception ignored) {} |
3859 |
+ |
|
3860 |
+ |
plus.completeExceptionally(first); |
3861 |
+ |
} |
3862 |
+ |
} catch (Throwable unexpected) { |
3863 |
+ |
plus.completeExceptionally(unexpected); |
3864 |
+ |
} |
3865 |
+ |
}; |
3866 |
+ |
f.whenComplete(action); |
3867 |
+ |
g.whenComplete(action); |
3868 |
+ |
return plus; |
3869 |
+ |
} |
3870 |
+ |
} |
3871 |
+ |
|
3872 |
+ |
/** |
3873 |
+ |
* CompletableFuture is an additive monad - sort of. |
3874 |
+ |
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
3875 |
+ |
*/ |
3876 |
+ |
public void testAdditiveMonad() throws Throwable { |
3877 |
+ |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
3878 |
+ |
CompletableFuture<Long> zero = Monad.zero(); |
3879 |
+ |
|
3880 |
+ |
// Some mutually non-commutative functions |
3881 |
+ |
Function<Long, CompletableFuture<Long>> triple |
3882 |
+ |
= (x) -> Monad.unit(3 * x); |
3883 |
+ |
Function<Long, CompletableFuture<Long>> inc |
3884 |
+ |
= (x) -> Monad.unit(x + 1); |
3885 |
+ |
|
3886 |
+ |
// unit is a right identity: m >>= unit === m |
3887 |
+ |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
3888 |
+ |
inc.apply(5L)); |
3889 |
+ |
// unit is a left identity: (unit x) >>= f === f x |
3890 |
+ |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
3891 |
+ |
inc.apply(5L)); |
3892 |
+ |
|
3893 |
+ |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
3894 |
+ |
Monad.assertFutureEquals( |
3895 |
+ |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
3896 |
+ |
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
3897 |
+ |
|
3898 |
+ |
// The case for CompletableFuture as an additive monad is weaker... |
3899 |
+ |
|
3900 |
+ |
// zero is a monadic zero |
3901 |
+ |
Monad.assertZero(zero); |
3902 |
+ |
|
3903 |
+ |
// left zero: zero >>= f === zero |
3904 |
+ |
Monad.assertZero(zero.thenCompose(inc)); |
3905 |
+ |
// right zero: f >>= (\x -> zero) === zero |
3906 |
+ |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
3907 |
+ |
|
3908 |
+ |
// f plus zero === f |
3909 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3910 |
+ |
Monad.plus(Monad.unit(5L), zero)); |
3911 |
+ |
// zero plus f === f |
3912 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3913 |
+ |
Monad.plus(zero, Monad.unit(5L))); |
3914 |
+ |
// zero plus zero === zero |
3915 |
+ |
Monad.assertZero(Monad.plus(zero, zero)); |
3916 |
+ |
{ |
3917 |
+ |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
3918 |
+ |
Monad.unit(8L)); |
3919 |
+ |
// non-determinism |
3920 |
+ |
assertTrue(f.get() == 5L || f.get() == 8L); |
3921 |
+ |
} |
3922 |
+ |
|
3923 |
+ |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
3924 |
+ |
// f plus godot === f (doesn't wait for godot) |
3925 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3926 |
+ |
Monad.plus(Monad.unit(5L), godot)); |
3927 |
+ |
// godot plus f === f (doesn't wait for godot) |
3928 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3929 |
+ |
Monad.plus(godot, Monad.unit(5L))); |
3930 |
+ |
} |
3931 |
+ |
|
3932 |
+ |
/** |
3933 |
+ |
* A single CompletableFuture with many dependents. |
3934 |
+ |
* A demo of scalability - runtime is O(n). |
3935 |
+ |
*/ |
3936 |
+ |
public void testManyDependents() throws Throwable { |
3937 |
+ |
final int n = 1_000; |
3938 |
+ |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
3939 |
+ |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
3940 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
3941 |
+ |
for (int i = 0; i < n; i++) { |
3942 |
+ |
head.thenRun(() -> count.getAndIncrement()); |
3943 |
+ |
head.thenAccept((x) -> count.getAndIncrement()); |
3944 |
+ |
head.thenApply((x) -> count.getAndIncrement()); |
3945 |
+ |
|
3946 |
+ |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
3947 |
+ |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
3948 |
+ |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
3949 |
+ |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
3950 |
+ |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
3951 |
+ |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
3952 |
+ |
|
3953 |
+ |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
3954 |
+ |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3955 |
+ |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3956 |
+ |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
3957 |
+ |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
3958 |
+ |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
3959 |
+ |
} |
3960 |
+ |
head.complete(null); |
3961 |
+ |
assertEquals(5 * 3 * n, count.get()); |
3962 |
+ |
} |
3963 |
+ |
|
3964 |
+ |
// static <U> U join(CompletionStage<U> stage) { |
3965 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3966 |
+ |
// stage.whenComplete((v, ex) -> { |
3967 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3968 |
+ |
// }); |
3969 |
+ |
// return f.join(); |
3970 |
+ |
// } |
3971 |
+ |
|
3972 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
3973 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3974 |
+ |
// stage.whenComplete((v, ex) -> { |
3975 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3976 |
+ |
// }); |
3977 |
+ |
// return f.isDone(); |
3978 |
+ |
// } |
3979 |
+ |
|
3980 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
3981 |
+ |
// return stage.toCompletableFuture().copy().join(); |
3982 |
+ |
// } |
3983 |
+ |
|
3984 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
3985 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
3986 |
+ |
// } |
3987 |
+ |
|
3988 |
|
} |