3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
< |
import java.util.concurrent.ExecutionException; |
6 |
> |
|
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
|
9 |
> |
import java.util.HashSet; |
10 |
|
import java.util.concurrent.CancellationException; |
11 |
+ |
import java.util.concurrent.CountedCompleter; |
12 |
+ |
import java.util.concurrent.ExecutionException; |
13 |
|
import java.util.concurrent.ForkJoinPool; |
14 |
|
import java.util.concurrent.ForkJoinTask; |
10 |
– |
import java.util.concurrent.CountedCompleter; |
11 |
– |
import java.util.concurrent.ForkJoinWorkerThread; |
12 |
– |
import java.util.concurrent.RecursiveAction; |
13 |
– |
import java.util.concurrent.TimeUnit; |
15 |
|
import java.util.concurrent.TimeoutException; |
16 |
< |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
17 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
18 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
19 |
< |
import java.util.HashSet; |
20 |
< |
import junit.framework.*; |
16 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
17 |
> |
import java.util.concurrent.atomic.AtomicReference; |
18 |
> |
|
19 |
> |
import junit.framework.Test; |
20 |
> |
import junit.framework.TestSuite; |
21 |
|
|
22 |
|
public class CountedCompleterTest extends JSR166TestCase { |
23 |
|
|
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run(suite()); |
25 |
> |
main(suite(), args); |
26 |
|
} |
27 |
|
|
28 |
|
public static Test suite() { |
33 |
|
static final int mainPoolSize = |
34 |
|
Math.max(2, Runtime.getRuntime().availableProcessors()); |
35 |
|
|
35 |
– |
/** |
36 |
– |
* Analog of CheckedRunnable for ForkJoinTasks |
37 |
– |
*/ |
38 |
– |
public abstract class CheckedFJTask extends RecursiveAction { |
39 |
– |
protected abstract void realCompute() throws Throwable; |
40 |
– |
|
41 |
– |
public final void compute() { |
42 |
– |
try { |
43 |
– |
realCompute(); |
44 |
– |
} catch (Throwable t) { |
45 |
– |
threadUnexpectedException(t); |
46 |
– |
} |
47 |
– |
} |
48 |
– |
} |
49 |
– |
|
36 |
|
private static ForkJoinPool mainPool() { |
37 |
|
return new ForkJoinPool(mainPoolSize); |
38 |
|
} |
47 |
|
null, true); |
48 |
|
} |
49 |
|
|
50 |
< |
private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) { |
51 |
< |
try { |
50 |
> |
private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask<?> a) { |
51 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
52 |
|
assertFalse(a.isDone()); |
53 |
|
assertFalse(a.isCompletedNormally()); |
54 |
|
assertFalse(a.isCompletedAbnormally()); |
64 |
|
assertFalse(a.isCancelled()); |
65 |
|
assertNull(a.getException()); |
66 |
|
assertNull(a.getRawResult()); |
81 |
– |
} finally { |
82 |
– |
joinPool(pool); |
67 |
|
} |
68 |
|
} |
69 |
|
|
70 |
< |
void checkNotDone(CountedCompleter a) { |
70 |
> |
void checkNotDone(CountedCompleter<?> a) { |
71 |
|
assertFalse(a.isDone()); |
72 |
|
assertFalse(a.isCompletedNormally()); |
73 |
|
assertFalse(a.isCompletedAbnormally()); |
76 |
|
assertNull(a.getRawResult()); |
77 |
|
|
78 |
|
try { |
79 |
< |
a.get(0L, SECONDS); |
79 |
> |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
80 |
|
shouldThrow(); |
81 |
|
} catch (TimeoutException success) { |
82 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
83 |
|
} |
84 |
|
|
85 |
< |
<T> void checkCompletedNormally(CountedCompleter<T> a) { |
102 |
< |
checkCompletedNormally(a, null); |
103 |
< |
} |
104 |
< |
|
105 |
< |
<T> void checkCompletedNormally(CountedCompleter<T> a, T expected) { |
85 |
> |
void checkCompletedNormally(CountedCompleter<?> a) { |
86 |
|
assertTrue(a.isDone()); |
87 |
|
assertFalse(a.isCancelled()); |
88 |
|
assertTrue(a.isCompletedNormally()); |
89 |
|
assertFalse(a.isCompletedAbnormally()); |
90 |
|
assertNull(a.getException()); |
91 |
< |
assertSame(expected, a.getRawResult()); |
91 |
> |
assertNull(a.getRawResult()); |
92 |
|
|
93 |
|
{ |
94 |
|
Thread.currentThread().interrupt(); |
95 |
< |
long t0 = System.nanoTime(); |
96 |
< |
assertSame(expected, a.join()); |
97 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
95 |
> |
long startTime = System.nanoTime(); |
96 |
> |
assertNull(a.join()); |
97 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
98 |
|
Thread.interrupted(); |
99 |
|
} |
100 |
|
|
101 |
|
{ |
102 |
|
Thread.currentThread().interrupt(); |
103 |
< |
long t0 = System.nanoTime(); |
103 |
> |
long startTime = System.nanoTime(); |
104 |
|
a.quietlyJoin(); // should be no-op |
105 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
105 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
106 |
|
Thread.interrupted(); |
107 |
|
} |
108 |
|
|
109 |
|
assertFalse(a.cancel(false)); |
110 |
|
assertFalse(a.cancel(true)); |
111 |
+ |
|
112 |
+ |
Object v1 = null, v2 = null; |
113 |
|
try { |
114 |
< |
assertSame(expected, a.get()); |
115 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
< |
try { |
135 |
< |
assertSame(expected, a.get(5L, SECONDS)); |
114 |
> |
v1 = a.get(); |
115 |
> |
v2 = a.get(randomTimeout(), randomTimeUnit()); |
116 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
117 |
+ |
assertNull(v1); |
118 |
+ |
assertNull(v2); |
119 |
|
} |
120 |
|
|
121 |
< |
void checkCancelled(CountedCompleter a) { |
121 |
> |
void checkCancelled(CountedCompleter<?> a) { |
122 |
|
assertTrue(a.isDone()); |
123 |
|
assertTrue(a.isCancelled()); |
124 |
|
assertFalse(a.isCompletedNormally()); |
137 |
|
Thread.interrupted(); |
138 |
|
|
139 |
|
{ |
140 |
< |
long t0 = System.nanoTime(); |
140 |
> |
long startTime = System.nanoTime(); |
141 |
|
a.quietlyJoin(); // should be no-op |
142 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
142 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
143 |
|
} |
144 |
|
|
145 |
|
try { |
149 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
150 |
|
|
151 |
|
try { |
152 |
< |
a.get(5L, SECONDS); |
152 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
153 |
|
shouldThrow(); |
154 |
|
} catch (CancellationException success) { |
155 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
156 |
|
} |
157 |
|
|
158 |
< |
void checkCompletedAbnormally(CountedCompleter a, Throwable t) { |
158 |
> |
void checkCompletedAbnormally(CountedCompleter<?> a, Throwable t) { |
159 |
|
assertTrue(a.isDone()); |
160 |
|
assertFalse(a.isCancelled()); |
161 |
|
assertFalse(a.isCompletedNormally()); |
175 |
|
Thread.interrupted(); |
176 |
|
|
177 |
|
{ |
178 |
< |
long t0 = System.nanoTime(); |
178 |
> |
long startTime = System.nanoTime(); |
179 |
|
a.quietlyJoin(); // should be no-op |
180 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
180 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
181 |
|
} |
182 |
|
|
183 |
|
try { |
188 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
189 |
|
|
190 |
|
try { |
191 |
< |
a.get(5L, SECONDS); |
191 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
192 |
|
shouldThrow(); |
193 |
|
} catch (ExecutionException success) { |
194 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
195 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
196 |
+ |
|
197 |
+ |
try { |
198 |
+ |
a.invoke(); |
199 |
+ |
shouldThrow(); |
200 |
+ |
} catch (Throwable success) { |
201 |
+ |
assertSame(t, success); |
202 |
+ |
} |
203 |
|
} |
204 |
|
|
205 |
|
public static final class FJException extends RuntimeException { |
206 |
|
FJException() { super(); } |
207 |
|
} |
208 |
|
|
209 |
< |
static final class NoopCountedCompleter extends CountedCompleter { |
210 |
< |
boolean post; // set true if onCompletion called |
211 |
< |
NoopCountedCompleter() { super(); } |
212 |
< |
NoopCountedCompleter(CountedCompleter p) { super(p); } |
213 |
< |
public void compute() {} |
214 |
< |
public final void onCompletion(CountedCompleter caller) { |
215 |
< |
post = true; |
209 |
> |
abstract class CheckedCC extends CountedCompleter<Object> { |
210 |
> |
final AtomicInteger computeN = new AtomicInteger(0); |
211 |
> |
final AtomicInteger onCompletionN = new AtomicInteger(0); |
212 |
> |
final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0); |
213 |
> |
final AtomicInteger setRawResultN = new AtomicInteger(0); |
214 |
> |
final AtomicReference<Object> rawResult = new AtomicReference<>(null); |
215 |
> |
int computeN() { return computeN.get(); } |
216 |
> |
int onCompletionN() { return onCompletionN.get(); } |
217 |
> |
int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); } |
218 |
> |
int setRawResultN() { return setRawResultN.get(); } |
219 |
> |
|
220 |
> |
CheckedCC() { super(); } |
221 |
> |
CheckedCC(CountedCompleter<?> p) { super(p); } |
222 |
> |
CheckedCC(CountedCompleter<?> p, int n) { super(p, n); } |
223 |
> |
abstract void realCompute(); |
224 |
> |
public final void compute() { |
225 |
> |
computeN.incrementAndGet(); |
226 |
> |
realCompute(); |
227 |
> |
} |
228 |
> |
public void onCompletion(CountedCompleter<?> caller) { |
229 |
> |
onCompletionN.incrementAndGet(); |
230 |
> |
super.onCompletion(caller); |
231 |
> |
} |
232 |
> |
public boolean onExceptionalCompletion(Throwable ex, |
233 |
> |
CountedCompleter<?> caller) { |
234 |
> |
onExceptionalCompletionN.incrementAndGet(); |
235 |
> |
assertNotNull(ex); |
236 |
> |
assertTrue(isCompletedAbnormally()); |
237 |
> |
assertTrue(super.onExceptionalCompletion(ex, caller)); |
238 |
> |
return true; |
239 |
> |
} |
240 |
> |
protected void setRawResult(Object t) { |
241 |
> |
setRawResultN.incrementAndGet(); |
242 |
> |
rawResult.set(t); |
243 |
> |
super.setRawResult(t); |
244 |
> |
} |
245 |
> |
void checkIncomplete() { |
246 |
> |
assertEquals(0, computeN()); |
247 |
> |
assertEquals(0, onCompletionN()); |
248 |
> |
assertEquals(0, onExceptionalCompletionN()); |
249 |
> |
assertEquals(0, setRawResultN()); |
250 |
> |
checkNotDone(this); |
251 |
> |
} |
252 |
> |
void checkCompletes(Object rawResult) { |
253 |
> |
checkIncomplete(); |
254 |
> |
int pendingCount = getPendingCount(); |
255 |
> |
complete(rawResult); |
256 |
> |
assertEquals(pendingCount, getPendingCount()); |
257 |
> |
assertEquals(0, computeN()); |
258 |
> |
assertEquals(1, onCompletionN()); |
259 |
> |
assertEquals(0, onExceptionalCompletionN()); |
260 |
> |
assertEquals(1, setRawResultN()); |
261 |
> |
assertSame(rawResult, this.rawResult.get()); |
262 |
> |
checkCompletedNormally(this); |
263 |
> |
} |
264 |
> |
void checkCompletesExceptionally(Throwable ex) { |
265 |
> |
checkIncomplete(); |
266 |
> |
completeExceptionally(ex); |
267 |
> |
checkCompletedExceptionally(ex); |
268 |
> |
} |
269 |
> |
void checkCompletedExceptionally(Throwable ex) { |
270 |
> |
assertEquals(0, computeN()); |
271 |
> |
assertEquals(0, onCompletionN()); |
272 |
> |
assertEquals(1, onExceptionalCompletionN()); |
273 |
> |
assertEquals(0, setRawResultN()); |
274 |
> |
assertNull(this.rawResult.get()); |
275 |
> |
checkCompletedAbnormally(this, ex); |
276 |
|
} |
277 |
|
} |
278 |
|
|
279 |
+ |
final class NoopCC extends CheckedCC { |
280 |
+ |
NoopCC() { super(); } |
281 |
+ |
NoopCC(CountedCompleter<?> p) { super(p); } |
282 |
+ |
NoopCC(CountedCompleter<?> p, int initialPendingCount) { |
283 |
+ |
super(p, initialPendingCount); |
284 |
+ |
} |
285 |
+ |
protected void realCompute() {} |
286 |
+ |
} |
287 |
+ |
|
288 |
|
/** |
289 |
|
* A newly constructed CountedCompleter is not completed; |
290 |
< |
* complete() causes completion. |
290 |
> |
* complete() causes completion. pendingCount is ignored. |
291 |
|
*/ |
292 |
|
public void testComplete() { |
293 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
294 |
< |
assertFalse(a.isDone()); |
295 |
< |
assertFalse(a.isCompletedNormally()); |
296 |
< |
assertFalse(a.isCompletedAbnormally()); |
297 |
< |
assertFalse(a.isCancelled()); |
298 |
< |
assertNull(a.getException()); |
299 |
< |
assertNull(a.getRawResult()); |
300 |
< |
assertFalse(a.post); |
301 |
< |
a.complete(null); |
302 |
< |
assertTrue(a.post); |
303 |
< |
assertTrue(a.isDone()); |
246 |
< |
assertTrue(a.isCompletedNormally()); |
247 |
< |
assertFalse(a.isCompletedAbnormally()); |
248 |
< |
assertFalse(a.isCancelled()); |
249 |
< |
assertNull(a.getException()); |
250 |
< |
assertNull(a.getRawResult()); |
293 |
> |
for (Object x : new Object[] { Boolean.TRUE, null }) { |
294 |
> |
for (int pendingCount : new int[] { 0, 42 }) { |
295 |
> |
testComplete(new NoopCC(), x, pendingCount); |
296 |
> |
testComplete(new NoopCC(new NoopCC()), x, pendingCount); |
297 |
> |
} |
298 |
> |
} |
299 |
> |
} |
300 |
> |
void testComplete(NoopCC cc, Object x, int pendingCount) { |
301 |
> |
cc.setPendingCount(pendingCount); |
302 |
> |
cc.checkCompletes(x); |
303 |
> |
assertEquals(pendingCount, cc.getPendingCount()); |
304 |
|
} |
305 |
|
|
306 |
|
/** |
307 |
|
* completeExceptionally completes exceptionally |
308 |
|
*/ |
309 |
|
public void testCompleteExceptionally() { |
310 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
311 |
< |
assertFalse(a.isDone()); |
312 |
< |
assertFalse(a.isCompletedNormally()); |
313 |
< |
assertFalse(a.isCompletedAbnormally()); |
314 |
< |
assertFalse(a.isCancelled()); |
315 |
< |
assertNull(a.getException()); |
316 |
< |
assertNull(a.getRawResult()); |
317 |
< |
assertFalse(a.post); |
318 |
< |
a.completeExceptionally(new FJException()); |
319 |
< |
assertFalse(a.post); |
320 |
< |
assertTrue(a.isDone()); |
321 |
< |
assertFalse(a.isCompletedNormally()); |
322 |
< |
assertTrue(a.isCompletedAbnormally()); |
323 |
< |
assertFalse(a.isCancelled()); |
324 |
< |
assertTrue(a.getException() instanceof FJException); |
325 |
< |
assertNull(a.getRawResult()); |
310 |
> |
new NoopCC() |
311 |
> |
.checkCompletesExceptionally(new FJException()); |
312 |
> |
new NoopCC(new NoopCC()) |
313 |
> |
.checkCompletesExceptionally(new FJException()); |
314 |
> |
} |
315 |
> |
|
316 |
> |
/** |
317 |
> |
* completeExceptionally(null) surprisingly has the same effect as |
318 |
> |
* completeExceptionally(new RuntimeException()) |
319 |
> |
*/ |
320 |
> |
public void testCompleteExceptionally_null() { |
321 |
> |
NoopCC a = new NoopCC(); |
322 |
> |
a.completeExceptionally(null); |
323 |
> |
try { |
324 |
> |
a.invoke(); |
325 |
> |
shouldThrow(); |
326 |
> |
} catch (RuntimeException success) { |
327 |
> |
assertSame(success.getClass(), RuntimeException.class); |
328 |
> |
assertNull(success.getCause()); |
329 |
> |
a.checkCompletedExceptionally(success); |
330 |
> |
} |
331 |
|
} |
332 |
|
|
333 |
|
/** |
334 |
|
* setPendingCount sets the reported pending count |
335 |
|
*/ |
336 |
|
public void testSetPendingCount() { |
337 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
337 |
> |
NoopCC a = new NoopCC(); |
338 |
|
assertEquals(0, a.getPendingCount()); |
339 |
< |
a.setPendingCount(1); |
340 |
< |
assertEquals(1, a.getPendingCount()); |
341 |
< |
a.setPendingCount(27); |
342 |
< |
assertEquals(27, a.getPendingCount()); |
339 |
> |
int[] vals = { |
340 |
> |
-1, 0, 1, |
341 |
> |
Integer.MIN_VALUE, |
342 |
> |
Integer.MAX_VALUE, |
343 |
> |
}; |
344 |
> |
for (int val : vals) { |
345 |
> |
a.setPendingCount(val); |
346 |
> |
assertEquals(val, a.getPendingCount()); |
347 |
> |
} |
348 |
|
} |
349 |
|
|
350 |
|
/** |
351 |
|
* addToPendingCount adds to the reported pending count |
352 |
|
*/ |
353 |
|
public void testAddToPendingCount() { |
354 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
354 |
> |
NoopCC a = new NoopCC(); |
355 |
|
assertEquals(0, a.getPendingCount()); |
356 |
|
a.addToPendingCount(1); |
357 |
|
assertEquals(1, a.getPendingCount()); |
358 |
|
a.addToPendingCount(27); |
359 |
|
assertEquals(28, a.getPendingCount()); |
360 |
+ |
a.addToPendingCount(-28); |
361 |
+ |
assertEquals(0, a.getPendingCount()); |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
|
* decrementPendingCountUnlessZero decrements reported pending |
366 |
|
* count unless zero |
367 |
|
*/ |
368 |
< |
public void testDecrementPendingCount() { |
369 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
370 |
< |
assertEquals(0, a.getPendingCount()); |
371 |
< |
a.addToPendingCount(1); |
368 |
> |
public void testDecrementPendingCountUnlessZero() { |
369 |
> |
NoopCC a = new NoopCC(null, 2); |
370 |
> |
assertEquals(2, a.getPendingCount()); |
371 |
> |
assertEquals(2, a.decrementPendingCountUnlessZero()); |
372 |
|
assertEquals(1, a.getPendingCount()); |
373 |
< |
a.decrementPendingCountUnlessZero(); |
373 |
> |
assertEquals(1, a.decrementPendingCountUnlessZero()); |
374 |
|
assertEquals(0, a.getPendingCount()); |
375 |
< |
a.decrementPendingCountUnlessZero(); |
375 |
> |
assertEquals(0, a.decrementPendingCountUnlessZero()); |
376 |
|
assertEquals(0, a.getPendingCount()); |
377 |
+ |
a.setPendingCount(-1); |
378 |
+ |
assertEquals(-1, a.decrementPendingCountUnlessZero()); |
379 |
+ |
assertEquals(-2, a.getPendingCount()); |
380 |
+ |
} |
381 |
+ |
|
382 |
+ |
/** |
383 |
+ |
* compareAndSetPendingCount compares and sets the reported |
384 |
+ |
* pending count |
385 |
+ |
*/ |
386 |
+ |
public void testCompareAndSetPendingCount() { |
387 |
+ |
NoopCC a = new NoopCC(); |
388 |
+ |
assertEquals(0, a.getPendingCount()); |
389 |
+ |
assertTrue(a.compareAndSetPendingCount(0, 1)); |
390 |
+ |
assertEquals(1, a.getPendingCount()); |
391 |
+ |
assertTrue(a.compareAndSetPendingCount(1, 2)); |
392 |
+ |
assertEquals(2, a.getPendingCount()); |
393 |
+ |
assertFalse(a.compareAndSetPendingCount(1, 3)); |
394 |
+ |
assertEquals(2, a.getPendingCount()); |
395 |
|
} |
396 |
|
|
397 |
|
/** |
398 |
|
* getCompleter returns parent or null if at root |
399 |
|
*/ |
400 |
|
public void testGetCompleter() { |
401 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
401 |
> |
NoopCC a = new NoopCC(); |
402 |
|
assertNull(a.getCompleter()); |
403 |
< |
CountedCompleter b = new NoopCountedCompleter(a); |
404 |
< |
assertEquals(a, b.getCompleter()); |
403 |
> |
NoopCC b = new NoopCC(a); |
404 |
> |
assertSame(a, b.getCompleter()); |
405 |
> |
NoopCC c = new NoopCC(b); |
406 |
> |
assertSame(b, c.getCompleter()); |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
|
* getRoot returns self if no parent, else parent's root |
411 |
|
*/ |
412 |
|
public void testGetRoot() { |
413 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
414 |
< |
NoopCountedCompleter b = new NoopCountedCompleter(a); |
415 |
< |
assertEquals(a, a.getRoot()); |
416 |
< |
assertEquals(a, b.getRoot()); |
413 |
> |
NoopCC a = new NoopCC(); |
414 |
> |
NoopCC b = new NoopCC(a); |
415 |
> |
NoopCC c = new NoopCC(b); |
416 |
> |
assertSame(a, a.getRoot()); |
417 |
> |
assertSame(a, b.getRoot()); |
418 |
> |
assertSame(a, c.getRoot()); |
419 |
|
} |
420 |
|
|
421 |
|
/** |
422 |
< |
* tryComplete causes completion if pending count is zero |
422 |
> |
* tryComplete decrements pending count unless zero, in which case |
423 |
> |
* causes completion |
424 |
|
*/ |
425 |
< |
public void testTryComplete1() { |
426 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
425 |
> |
public void testTryComplete() { |
426 |
> |
NoopCC a = new NoopCC(); |
427 |
|
assertEquals(0, a.getPendingCount()); |
428 |
+ |
int n = 3; |
429 |
+ |
a.setPendingCount(n); |
430 |
+ |
for (; n > 0; n--) { |
431 |
+ |
assertEquals(n, a.getPendingCount()); |
432 |
+ |
a.tryComplete(); |
433 |
+ |
a.checkIncomplete(); |
434 |
+ |
assertEquals(n - 1, a.getPendingCount()); |
435 |
+ |
} |
436 |
|
a.tryComplete(); |
437 |
< |
assertTrue(a.post); |
438 |
< |
assertTrue(a.isDone()); |
437 |
> |
assertEquals(0, a.computeN()); |
438 |
> |
assertEquals(1, a.onCompletionN()); |
439 |
> |
assertEquals(0, a.onExceptionalCompletionN()); |
440 |
> |
assertEquals(0, a.setRawResultN()); |
441 |
> |
checkCompletedNormally(a); |
442 |
|
} |
443 |
|
|
444 |
|
/** |
445 |
< |
* propagateCompletion causes completion without invoking |
446 |
< |
* onCompletion if pending count is zero |
445 |
> |
* propagateCompletion decrements pending count unless zero, in |
446 |
> |
* which case causes completion, without invoking onCompletion |
447 |
|
*/ |
448 |
|
public void testPropagateCompletion() { |
449 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
449 |
> |
NoopCC a = new NoopCC(); |
450 |
|
assertEquals(0, a.getPendingCount()); |
451 |
+ |
int n = 3; |
452 |
+ |
a.setPendingCount(n); |
453 |
+ |
for (; n > 0; n--) { |
454 |
+ |
assertEquals(n, a.getPendingCount()); |
455 |
+ |
a.propagateCompletion(); |
456 |
+ |
a.checkIncomplete(); |
457 |
+ |
assertEquals(n - 1, a.getPendingCount()); |
458 |
+ |
} |
459 |
|
a.propagateCompletion(); |
460 |
< |
assertFalse(a.post); |
461 |
< |
assertTrue(a.isDone()); |
462 |
< |
} |
463 |
< |
|
464 |
< |
/** |
358 |
< |
* tryComplete decrements pending count unless zero |
359 |
< |
*/ |
360 |
< |
public void testTryComplete2() { |
361 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
362 |
< |
assertEquals(0, a.getPendingCount()); |
363 |
< |
a.setPendingCount(1); |
364 |
< |
a.tryComplete(); |
365 |
< |
assertFalse(a.post); |
366 |
< |
assertFalse(a.isDone()); |
367 |
< |
assertEquals(0, a.getPendingCount()); |
368 |
< |
a.tryComplete(); |
369 |
< |
assertTrue(a.post); |
370 |
< |
assertTrue(a.isDone()); |
460 |
> |
assertEquals(0, a.computeN()); |
461 |
> |
assertEquals(0, a.onCompletionN()); |
462 |
> |
assertEquals(0, a.onExceptionalCompletionN()); |
463 |
> |
assertEquals(0, a.setRawResultN()); |
464 |
> |
checkCompletedNormally(a); |
465 |
|
} |
466 |
|
|
467 |
|
/** |
468 |
|
* firstComplete returns this if pending count is zero else null |
469 |
|
*/ |
470 |
|
public void testFirstComplete() { |
471 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
471 |
> |
NoopCC a = new NoopCC(); |
472 |
|
a.setPendingCount(1); |
473 |
|
assertNull(a.firstComplete()); |
474 |
< |
assertEquals(a, a.firstComplete()); |
474 |
> |
a.checkIncomplete(); |
475 |
> |
assertSame(a, a.firstComplete()); |
476 |
> |
a.checkIncomplete(); |
477 |
|
} |
478 |
|
|
479 |
|
/** |
481 |
|
* zero else null |
482 |
|
*/ |
483 |
|
public void testNextComplete() { |
484 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
485 |
< |
NoopCountedCompleter b = new NoopCountedCompleter(a); |
484 |
> |
NoopCC a = new NoopCC(); |
485 |
> |
NoopCC b = new NoopCC(a); |
486 |
|
a.setPendingCount(1); |
487 |
|
b.setPendingCount(1); |
488 |
|
assertNull(b.firstComplete()); |
489 |
< |
CountedCompleter c = b.firstComplete(); |
490 |
< |
assertEquals(b, c); |
491 |
< |
CountedCompleter d = c.nextComplete(); |
492 |
< |
assertNull(d); |
493 |
< |
CountedCompleter e = c.nextComplete(); |
494 |
< |
assertEquals(a, e); |
489 |
> |
assertSame(b, b.firstComplete()); |
490 |
> |
assertNull(b.nextComplete()); |
491 |
> |
a.checkIncomplete(); |
492 |
> |
b.checkIncomplete(); |
493 |
> |
assertSame(a, b.nextComplete()); |
494 |
> |
assertSame(a, b.nextComplete()); |
495 |
> |
a.checkIncomplete(); |
496 |
> |
b.checkIncomplete(); |
497 |
> |
assertNull(a.nextComplete()); |
498 |
> |
b.checkIncomplete(); |
499 |
> |
checkCompletedNormally(a); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
503 |
< |
* quietlyCompleteRoot completes root task |
503 |
> |
* quietlyCompleteRoot completes root task and only root task |
504 |
|
*/ |
505 |
|
public void testQuietlyCompleteRoot() { |
506 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
507 |
< |
NoopCountedCompleter b = new NoopCountedCompleter(a); |
506 |
> |
NoopCC a = new NoopCC(); |
507 |
> |
NoopCC b = new NoopCC(a); |
508 |
> |
NoopCC c = new NoopCC(b); |
509 |
|
a.setPendingCount(1); |
510 |
|
b.setPendingCount(1); |
511 |
< |
b.quietlyCompleteRoot(); |
511 |
> |
c.setPendingCount(1); |
512 |
> |
c.quietlyCompleteRoot(); |
513 |
|
assertTrue(a.isDone()); |
514 |
|
assertFalse(b.isDone()); |
515 |
+ |
assertFalse(c.isDone()); |
516 |
|
} |
517 |
|
|
518 |
|
// Invocation tests use some interdependent task classes |
519 |
|
// to better test propagation etc |
520 |
|
|
521 |
< |
|
522 |
< |
// Version of Fibonacci with different classes for left vs right forks |
523 |
< |
abstract static class CCF extends CountedCompleter { |
521 |
> |
/** |
522 |
> |
* Version of Fibonacci with different classes for left vs right forks |
523 |
> |
*/ |
524 |
> |
abstract class CCF extends CheckedCC { |
525 |
|
int number; |
526 |
|
int rnumber; |
527 |
|
|
528 |
< |
public CCF(CountedCompleter parent, int n) { |
528 |
> |
public CCF(CountedCompleter<?> parent, int n) { |
529 |
|
super(parent, 1); |
530 |
|
this.number = n; |
531 |
|
} |
532 |
|
|
533 |
< |
public final void compute() { |
429 |
< |
CountedCompleter p; |
533 |
> |
protected final void realCompute() { |
534 |
|
CCF f = this; |
535 |
|
int n = number; |
536 |
|
while (n >= 2) { |
537 |
|
new RCCF(f, n - 2).fork(); |
538 |
|
f = new LCCF(f, --n); |
539 |
|
} |
540 |
< |
f.number = n; |
437 |
< |
f.onCompletion(f); |
438 |
< |
if ((p = f.getCompleter()) != null) |
439 |
< |
p.tryComplete(); |
440 |
< |
else |
441 |
< |
f.quietlyComplete(); |
540 |
> |
f.complete(null); |
541 |
|
} |
542 |
|
} |
543 |
|
|
544 |
< |
static final class LCCF extends CCF { |
545 |
< |
public LCCF(CountedCompleter parent, int n) { |
544 |
> |
final class LCCF extends CCF { |
545 |
> |
public LCCF(int n) { this(null, n); } |
546 |
> |
public LCCF(CountedCompleter<?> parent, int n) { |
547 |
|
super(parent, n); |
548 |
|
} |
549 |
< |
public final void onCompletion(CountedCompleter caller) { |
549 |
> |
public final void onCompletion(CountedCompleter<?> caller) { |
550 |
> |
super.onCompletion(caller); |
551 |
|
CCF p = (CCF)getCompleter(); |
552 |
|
int n = number + rnumber; |
553 |
|
if (p != null) |
556 |
|
number = n; |
557 |
|
} |
558 |
|
} |
559 |
< |
static final class RCCF extends CCF { |
560 |
< |
public RCCF(CountedCompleter parent, int n) { |
559 |
> |
final class RCCF extends CCF { |
560 |
> |
public RCCF(CountedCompleter<?> parent, int n) { |
561 |
|
super(parent, n); |
562 |
|
} |
563 |
< |
public final void onCompletion(CountedCompleter caller) { |
563 |
> |
public final void onCompletion(CountedCompleter<?> caller) { |
564 |
> |
super.onCompletion(caller); |
565 |
|
CCF p = (CCF)getCompleter(); |
566 |
|
int n = number + rnumber; |
567 |
|
if (p != null) |
572 |
|
} |
573 |
|
|
574 |
|
// Version of CCF with forced failure in left completions |
575 |
< |
abstract static class FailingCCF extends CountedCompleter { |
575 |
> |
abstract class FailingCCF extends CheckedCC { |
576 |
|
int number; |
577 |
|
int rnumber; |
578 |
|
|
579 |
< |
public FailingCCF(CountedCompleter parent, int n) { |
579 |
> |
public FailingCCF(CountedCompleter<?> parent, int n) { |
580 |
|
super(parent, 1); |
581 |
|
this.number = n; |
582 |
|
} |
583 |
|
|
584 |
< |
public final void compute() { |
483 |
< |
CountedCompleter p; |
584 |
> |
protected final void realCompute() { |
585 |
|
FailingCCF f = this; |
586 |
|
int n = number; |
587 |
|
while (n >= 2) { |
588 |
|
new RFCCF(f, n - 2).fork(); |
589 |
|
f = new LFCCF(f, --n); |
590 |
|
} |
591 |
< |
f.number = n; |
491 |
< |
f.onCompletion(f); |
492 |
< |
if ((p = f.getCompleter()) != null) |
493 |
< |
p.tryComplete(); |
494 |
< |
else |
495 |
< |
f.quietlyComplete(); |
591 |
> |
f.complete(null); |
592 |
|
} |
593 |
|
} |
594 |
|
|
595 |
< |
static final class LFCCF extends FailingCCF { |
596 |
< |
public LFCCF(CountedCompleter parent, int n) { |
595 |
> |
final class LFCCF extends FailingCCF { |
596 |
> |
public LFCCF(int n) { this(null, n); } |
597 |
> |
public LFCCF(CountedCompleter<?> parent, int n) { |
598 |
|
super(parent, n); |
599 |
|
} |
600 |
< |
public final void onCompletion(CountedCompleter caller) { |
600 |
> |
public final void onCompletion(CountedCompleter<?> caller) { |
601 |
> |
super.onCompletion(caller); |
602 |
|
FailingCCF p = (FailingCCF)getCompleter(); |
603 |
|
int n = number + rnumber; |
604 |
|
if (p != null) |
607 |
|
number = n; |
608 |
|
} |
609 |
|
} |
610 |
< |
static final class RFCCF extends FailingCCF { |
611 |
< |
public RFCCF(CountedCompleter parent, int n) { |
610 |
> |
final class RFCCF extends FailingCCF { |
611 |
> |
public RFCCF(CountedCompleter<?> parent, int n) { |
612 |
|
super(parent, n); |
613 |
|
} |
614 |
< |
public final void onCompletion(CountedCompleter caller) { |
614 |
> |
public final void onCompletion(CountedCompleter<?> caller) { |
615 |
> |
super.onCompletion(caller); |
616 |
|
completeExceptionally(new FJException()); |
617 |
|
} |
618 |
|
} |
623 |
|
* completed tasks; getRawResult returns null. |
624 |
|
*/ |
625 |
|
public void testInvoke() { |
626 |
< |
ForkJoinTask a = new CheckedFJTask() { |
627 |
< |
public void realCompute() { |
628 |
< |
CCF f = new LCCF(null, 8); |
626 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
627 |
> |
protected void realCompute() { |
628 |
> |
CCF f = new LCCF(8); |
629 |
|
assertNull(f.invoke()); |
630 |
|
assertEquals(21, f.number); |
631 |
|
checkCompletedNormally(f); |
639 |
|
* completed tasks |
640 |
|
*/ |
641 |
|
public void testQuietlyInvoke() { |
642 |
< |
ForkJoinTask a = new CheckedFJTask() { |
643 |
< |
public void realCompute() { |
644 |
< |
CCF f = new LCCF(null, 8); |
642 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
643 |
> |
protected void realCompute() { |
644 |
> |
CCF f = new LCCF(8); |
645 |
|
f.quietlyInvoke(); |
646 |
|
assertEquals(21, f.number); |
647 |
|
checkCompletedNormally(f); |
653 |
|
* join of a forked task returns when task completes |
654 |
|
*/ |
655 |
|
public void testForkJoin() { |
656 |
< |
ForkJoinTask a = new CheckedFJTask() { |
657 |
< |
public void realCompute() { |
658 |
< |
CCF f = new LCCF(null, 8); |
656 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
657 |
> |
protected void realCompute() { |
658 |
> |
CCF f = new LCCF(8); |
659 |
|
assertSame(f, f.fork()); |
660 |
|
assertNull(f.join()); |
661 |
|
assertEquals(21, f.number); |
668 |
|
* get of a forked task returns when task completes |
669 |
|
*/ |
670 |
|
public void testForkGet() { |
671 |
< |
ForkJoinTask a = new CheckedFJTask() { |
672 |
< |
public void realCompute() throws Exception { |
673 |
< |
CCF f = new LCCF(null, 8); |
671 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
672 |
> |
protected void realCompute() throws Exception { |
673 |
> |
CCF f = new LCCF(8); |
674 |
|
assertSame(f, f.fork()); |
675 |
|
assertNull(f.get()); |
676 |
|
assertEquals(21, f.number); |
683 |
|
* timed get of a forked task returns when task completes |
684 |
|
*/ |
685 |
|
public void testForkTimedGet() { |
686 |
< |
ForkJoinTask a = new CheckedFJTask() { |
687 |
< |
public void realCompute() throws Exception { |
688 |
< |
CCF f = new LCCF(null, 8); |
686 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
687 |
> |
protected void realCompute() throws Exception { |
688 |
> |
CCF f = new LCCF(8); |
689 |
|
assertSame(f, f.fork()); |
690 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
691 |
|
assertEquals(21, f.number); |
698 |
|
* timed get with null time unit throws NPE |
699 |
|
*/ |
700 |
|
public void testForkTimedGetNPE() { |
701 |
< |
ForkJoinTask a = new CheckedFJTask() { |
702 |
< |
public void realCompute() throws Exception { |
703 |
< |
CCF f = new LCCF(null, 8); |
701 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
702 |
> |
protected void realCompute() throws Exception { |
703 |
> |
CCF f = new LCCF(8); |
704 |
|
assertSame(f, f.fork()); |
705 |
|
try { |
706 |
< |
f.get(5L, null); |
706 |
> |
f.get(randomTimeout(), null); |
707 |
|
shouldThrow(); |
708 |
|
} catch (NullPointerException success) {} |
709 |
|
}}; |
714 |
|
* quietlyJoin of a forked task returns when task completes |
715 |
|
*/ |
716 |
|
public void testForkQuietlyJoin() { |
717 |
< |
ForkJoinTask a = new CheckedFJTask() { |
718 |
< |
public void realCompute() { |
719 |
< |
CCF f = new LCCF(null, 8); |
717 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
718 |
> |
protected void realCompute() { |
719 |
> |
CCF f = new LCCF(8); |
720 |
|
assertSame(f, f.fork()); |
721 |
|
f.quietlyJoin(); |
722 |
|
assertEquals(21, f.number); |
730 |
|
* getQueuedTaskCount returns 0 when quiescent |
731 |
|
*/ |
732 |
|
public void testForkHelpQuiesce() { |
733 |
< |
ForkJoinTask a = new CheckedFJTask() { |
734 |
< |
public void realCompute() { |
735 |
< |
CCF f = new LCCF(null, 8); |
733 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
734 |
> |
protected void realCompute() { |
735 |
> |
CCF f = new LCCF(8); |
736 |
|
assertSame(f, f.fork()); |
737 |
|
helpQuiesce(); |
738 |
+ |
while (!f.isDone()) // wait out race |
739 |
+ |
; |
740 |
|
assertEquals(21, f.number); |
741 |
|
assertEquals(0, getQueuedTaskCount()); |
742 |
|
checkCompletedNormally(f); |
748 |
|
* invoke task throws exception when task completes abnormally |
749 |
|
*/ |
750 |
|
public void testAbnormalInvoke() { |
751 |
< |
ForkJoinTask a = new CheckedFJTask() { |
752 |
< |
public void realCompute() { |
753 |
< |
FailingCCF f = new LFCCF(null, 8); |
751 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
752 |
> |
protected void realCompute() { |
753 |
> |
FailingCCF f = new LFCCF(8); |
754 |
|
try { |
755 |
|
f.invoke(); |
756 |
|
shouldThrow(); |
765 |
|
* quietlyInvoke task returns when task completes abnormally |
766 |
|
*/ |
767 |
|
public void testAbnormalQuietlyInvoke() { |
768 |
< |
ForkJoinTask a = new CheckedFJTask() { |
769 |
< |
public void realCompute() { |
770 |
< |
FailingCCF f = new LFCCF(null, 8); |
768 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
769 |
> |
protected void realCompute() { |
770 |
> |
FailingCCF f = new LFCCF(8); |
771 |
|
f.quietlyInvoke(); |
772 |
|
assertTrue(f.getException() instanceof FJException); |
773 |
|
checkCompletedAbnormally(f, f.getException()); |
779 |
|
* join of a forked task throws exception when task completes abnormally |
780 |
|
*/ |
781 |
|
public void testAbnormalForkJoin() { |
782 |
< |
ForkJoinTask a = new CheckedFJTask() { |
783 |
< |
public void realCompute() { |
784 |
< |
FailingCCF f = new LFCCF(null, 8); |
782 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
783 |
> |
protected void realCompute() { |
784 |
> |
FailingCCF f = new LFCCF(8); |
785 |
|
assertSame(f, f.fork()); |
786 |
|
try { |
787 |
|
f.join(); |
797 |
|
* get of a forked task throws exception when task completes abnormally |
798 |
|
*/ |
799 |
|
public void testAbnormalForkGet() { |
800 |
< |
ForkJoinTask a = new CheckedFJTask() { |
801 |
< |
public void realCompute() throws Exception { |
802 |
< |
FailingCCF f = new LFCCF(null, 8); |
800 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
801 |
> |
protected void realCompute() throws Exception { |
802 |
> |
FailingCCF f = new LFCCF(8); |
803 |
|
assertSame(f, f.fork()); |
804 |
|
try { |
805 |
|
f.get(); |
817 |
|
* timed get of a forked task throws exception when task completes abnormally |
818 |
|
*/ |
819 |
|
public void testAbnormalForkTimedGet() { |
820 |
< |
ForkJoinTask a = new CheckedFJTask() { |
821 |
< |
public void realCompute() throws Exception { |
822 |
< |
FailingCCF f = new LFCCF(null, 8); |
820 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
821 |
> |
protected void realCompute() throws Exception { |
822 |
> |
FailingCCF f = new LFCCF(8); |
823 |
|
assertSame(f, f.fork()); |
824 |
|
try { |
825 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
837 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
838 |
|
*/ |
839 |
|
public void testAbnormalForkQuietlyJoin() { |
840 |
< |
ForkJoinTask a = new CheckedFJTask() { |
841 |
< |
public void realCompute() { |
842 |
< |
FailingCCF f = new LFCCF(null, 8); |
840 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
841 |
> |
protected void realCompute() { |
842 |
> |
FailingCCF f = new LFCCF(8); |
843 |
|
assertSame(f, f.fork()); |
844 |
|
f.quietlyJoin(); |
845 |
|
assertTrue(f.getException() instanceof FJException); |
852 |
|
* invoke task throws exception when task cancelled |
853 |
|
*/ |
854 |
|
public void testCancelledInvoke() { |
855 |
< |
ForkJoinTask a = new CheckedFJTask() { |
856 |
< |
public void realCompute() { |
857 |
< |
CCF f = new LCCF(null, 8); |
855 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
856 |
> |
protected void realCompute() { |
857 |
> |
CCF f = new LCCF(8); |
858 |
|
assertTrue(f.cancel(true)); |
859 |
|
try { |
860 |
|
f.invoke(); |
870 |
|
* join of a forked task throws exception when task cancelled |
871 |
|
*/ |
872 |
|
public void testCancelledForkJoin() { |
873 |
< |
ForkJoinTask a = new CheckedFJTask() { |
874 |
< |
public void realCompute() { |
875 |
< |
CCF f = new LCCF(null, 8); |
873 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
874 |
> |
protected void realCompute() { |
875 |
> |
CCF f = new LCCF(8); |
876 |
|
assertTrue(f.cancel(true)); |
877 |
|
assertSame(f, f.fork()); |
878 |
|
try { |
889 |
|
* get of a forked task throws exception when task cancelled |
890 |
|
*/ |
891 |
|
public void testCancelledForkGet() { |
892 |
< |
ForkJoinTask a = new CheckedFJTask() { |
893 |
< |
public void realCompute() throws Exception { |
894 |
< |
CCF f = new LCCF(null, 8); |
892 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
893 |
> |
protected void realCompute() throws Exception { |
894 |
> |
CCF f = new LCCF(8); |
895 |
|
assertTrue(f.cancel(true)); |
896 |
|
assertSame(f, f.fork()); |
897 |
|
try { |
908 |
|
* timed get of a forked task throws exception when task cancelled |
909 |
|
*/ |
910 |
|
public void testCancelledForkTimedGet() throws Exception { |
911 |
< |
ForkJoinTask a = new CheckedFJTask() { |
912 |
< |
public void realCompute() throws Exception { |
913 |
< |
CCF f = new LCCF(null, 8); |
911 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
912 |
> |
protected void realCompute() throws Exception { |
913 |
> |
CCF f = new LCCF(8); |
914 |
|
assertTrue(f.cancel(true)); |
915 |
|
assertSame(f, f.fork()); |
916 |
|
try { |
927 |
|
* quietlyJoin of a forked task returns when task cancelled |
928 |
|
*/ |
929 |
|
public void testCancelledForkQuietlyJoin() { |
930 |
< |
ForkJoinTask a = new CheckedFJTask() { |
931 |
< |
public void realCompute() { |
932 |
< |
CCF f = new LCCF(null, 8); |
930 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
931 |
> |
protected void realCompute() { |
932 |
> |
CCF f = new LCCF(8); |
933 |
|
assertTrue(f.cancel(true)); |
934 |
|
assertSame(f, f.fork()); |
935 |
|
f.quietlyJoin(); |
943 |
|
*/ |
944 |
|
public void testGetPool() { |
945 |
|
final ForkJoinPool mainPool = mainPool(); |
946 |
< |
ForkJoinTask a = new CheckedFJTask() { |
947 |
< |
public void realCompute() { |
946 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
947 |
> |
protected void realCompute() { |
948 |
|
assertSame(mainPool, getPool()); |
949 |
|
}}; |
950 |
|
testInvokeOnPool(mainPool, a); |
954 |
|
* getPool of non-FJ task returns null |
955 |
|
*/ |
956 |
|
public void testGetPool2() { |
957 |
< |
ForkJoinTask a = new CheckedFJTask() { |
958 |
< |
public void realCompute() { |
957 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
958 |
> |
protected void realCompute() { |
959 |
|
assertNull(getPool()); |
960 |
|
}}; |
961 |
|
assertNull(a.invoke()); |
965 |
|
* inForkJoinPool of executing task returns true |
966 |
|
*/ |
967 |
|
public void testInForkJoinPool() { |
968 |
< |
ForkJoinTask a = new CheckedFJTask() { |
969 |
< |
public void realCompute() { |
968 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
969 |
> |
protected void realCompute() { |
970 |
|
assertTrue(inForkJoinPool()); |
971 |
|
}}; |
972 |
|
testInvokeOnPool(mainPool(), a); |
976 |
|
* inForkJoinPool of non-FJ task returns false |
977 |
|
*/ |
978 |
|
public void testInForkJoinPool2() { |
979 |
< |
ForkJoinTask a = new CheckedFJTask() { |
980 |
< |
public void realCompute() { |
979 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
980 |
> |
protected void realCompute() { |
981 |
|
assertFalse(inForkJoinPool()); |
982 |
|
}}; |
983 |
|
assertNull(a.invoke()); |
987 |
|
* setRawResult(null) succeeds |
988 |
|
*/ |
989 |
|
public void testSetRawResult() { |
990 |
< |
ForkJoinTask a = new CheckedFJTask() { |
991 |
< |
public void realCompute() { |
990 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
991 |
> |
protected void realCompute() { |
992 |
|
setRawResult(null); |
993 |
|
assertNull(getRawResult()); |
994 |
|
}}; |
999 |
|
* invoke task throws exception after invoking completeExceptionally |
1000 |
|
*/ |
1001 |
|
public void testCompleteExceptionally2() { |
1002 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1003 |
< |
public void realCompute() { |
1004 |
< |
CCF f = new LCCF(null, 8); |
1005 |
< |
f.completeExceptionally(new FJException()); |
1006 |
< |
try { |
1007 |
< |
f.invoke(); |
1008 |
< |
shouldThrow(); |
1009 |
< |
} catch (FJException success) { |
909 |
< |
checkCompletedAbnormally(f, success); |
910 |
< |
} |
1002 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1003 |
> |
protected void realCompute() { |
1004 |
> |
CCF n = new LCCF(8); |
1005 |
> |
CCF f = new LCCF(n, 8); |
1006 |
> |
FJException ex = new FJException(); |
1007 |
> |
f.completeExceptionally(ex); |
1008 |
> |
f.checkCompletedExceptionally(ex); |
1009 |
> |
n.checkCompletedExceptionally(ex); |
1010 |
|
}}; |
1011 |
|
testInvokeOnPool(mainPool(), a); |
1012 |
|
} |
1015 |
|
* invokeAll(t1, t2) invokes all task arguments |
1016 |
|
*/ |
1017 |
|
public void testInvokeAll2() { |
1018 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1019 |
< |
public void realCompute() { |
1020 |
< |
CCF f = new LCCF(null, 8); |
1021 |
< |
CCF g = new LCCF(null, 9); |
1018 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1019 |
> |
protected void realCompute() { |
1020 |
> |
CCF f = new LCCF(8); |
1021 |
> |
CCF g = new LCCF(9); |
1022 |
|
invokeAll(f, g); |
1023 |
|
assertEquals(21, f.number); |
1024 |
|
assertEquals(34, g.number); |
1032 |
|
* invokeAll(tasks) with 1 argument invokes task |
1033 |
|
*/ |
1034 |
|
public void testInvokeAll1() { |
1035 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1036 |
< |
public void realCompute() { |
1037 |
< |
CCF f = new LCCF(null, 8); |
1035 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1036 |
> |
protected void realCompute() { |
1037 |
> |
CCF f = new LCCF(8); |
1038 |
|
invokeAll(f); |
1039 |
|
checkCompletedNormally(f); |
1040 |
|
assertEquals(21, f.number); |
1046 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
1047 |
|
*/ |
1048 |
|
public void testInvokeAll3() { |
1049 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1050 |
< |
public void realCompute() { |
1051 |
< |
CCF f = new LCCF(null, 8); |
1052 |
< |
CCF g = new LCCF(null, 9); |
1053 |
< |
CCF h = new LCCF(null, 7); |
1049 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1050 |
> |
protected void realCompute() { |
1051 |
> |
CCF f = new LCCF(8); |
1052 |
> |
CCF g = new LCCF(9); |
1053 |
> |
CCF h = new LCCF(7); |
1054 |
|
invokeAll(f, g, h); |
1055 |
|
assertEquals(21, f.number); |
1056 |
|
assertEquals(34, g.number); |
1066 |
|
* invokeAll(collection) invokes all tasks in the collection |
1067 |
|
*/ |
1068 |
|
public void testInvokeAllCollection() { |
1069 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1070 |
< |
public void realCompute() { |
1071 |
< |
CCF f = new LCCF(null, 8); |
1072 |
< |
CCF g = new LCCF(null, 9); |
1073 |
< |
CCF h = new LCCF(null, 7); |
1074 |
< |
HashSet set = new HashSet(); |
1069 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1070 |
> |
protected void realCompute() { |
1071 |
> |
CCF f = new LCCF(8); |
1072 |
> |
CCF g = new LCCF(9); |
1073 |
> |
CCF h = new LCCF(7); |
1074 |
> |
HashSet<CCF> set = new HashSet<>(); |
1075 |
|
set.add(f); |
1076 |
|
set.add(g); |
1077 |
|
set.add(h); |
1090 |
|
* invokeAll(tasks) with any null task throws NPE |
1091 |
|
*/ |
1092 |
|
public void testInvokeAllNPE() { |
1093 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1094 |
< |
public void realCompute() { |
1095 |
< |
CCF f = new LCCF(null, 8); |
1096 |
< |
CCF g = new LCCF(null, 9); |
1093 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1094 |
> |
protected void realCompute() { |
1095 |
> |
CCF f = new LCCF(8); |
1096 |
> |
CCF g = new LCCF(9); |
1097 |
|
CCF h = null; |
1098 |
|
try { |
1099 |
|
invokeAll(f, g, h); |
1107 |
|
* invokeAll(t1, t2) throw exception if any task does |
1108 |
|
*/ |
1109 |
|
public void testAbnormalInvokeAll2() { |
1110 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1111 |
< |
public void realCompute() { |
1112 |
< |
CCF f = new LCCF(null, 8); |
1113 |
< |
FailingCCF g = new LFCCF(null, 9); |
1110 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1111 |
> |
protected void realCompute() { |
1112 |
> |
CCF f = new LCCF(8); |
1113 |
> |
FailingCCF g = new LFCCF(9); |
1114 |
|
try { |
1115 |
|
invokeAll(f, g); |
1116 |
|
shouldThrow(); |
1125 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
1126 |
|
*/ |
1127 |
|
public void testAbnormalInvokeAll1() { |
1128 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1129 |
< |
public void realCompute() { |
1130 |
< |
FailingCCF g = new LFCCF(null, 9); |
1128 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1129 |
> |
protected void realCompute() { |
1130 |
> |
FailingCCF g = new LFCCF(9); |
1131 |
|
try { |
1132 |
|
invokeAll(g); |
1133 |
|
shouldThrow(); |
1142 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1143 |
|
*/ |
1144 |
|
public void testAbnormalInvokeAll3() { |
1145 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1146 |
< |
public void realCompute() { |
1147 |
< |
CCF f = new LCCF(null, 8); |
1148 |
< |
FailingCCF g = new LFCCF(null, 9); |
1149 |
< |
CCF h = new LCCF(null, 7); |
1145 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1146 |
> |
protected void realCompute() { |
1147 |
> |
CCF f = new LCCF(8); |
1148 |
> |
FailingCCF g = new LFCCF(9); |
1149 |
> |
CCF h = new LCCF(7); |
1150 |
|
try { |
1151 |
|
invokeAll(f, g, h); |
1152 |
|
shouldThrow(); |
1158 |
|
} |
1159 |
|
|
1160 |
|
/** |
1161 |
< |
* invokeAll(collection) throws exception if any task does |
1161 |
> |
* invokeAll(collection) throws exception if any task does |
1162 |
|
*/ |
1163 |
|
public void testAbnormalInvokeAllCollection() { |
1164 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1165 |
< |
public void realCompute() { |
1166 |
< |
FailingCCF f = new LFCCF(null, 8); |
1167 |
< |
CCF g = new LCCF(null, 9); |
1168 |
< |
CCF h = new LCCF(null, 7); |
1169 |
< |
HashSet set = new HashSet(); |
1164 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1165 |
> |
protected void realCompute() { |
1166 |
> |
FailingCCF f = new LFCCF(8); |
1167 |
> |
CCF g = new LCCF(9); |
1168 |
> |
CCF h = new LCCF(7); |
1169 |
> |
HashSet<ForkJoinTask<?>> set = new HashSet<>(); |
1170 |
|
set.add(f); |
1171 |
|
set.add(g); |
1172 |
|
set.add(h); |
1185 |
|
* and suppresses execution |
1186 |
|
*/ |
1187 |
|
public void testTryUnfork() { |
1188 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1189 |
< |
public void realCompute() { |
1190 |
< |
CCF g = new LCCF(null, 9); |
1188 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1189 |
> |
protected void realCompute() { |
1190 |
> |
CCF g = new LCCF(9); |
1191 |
|
assertSame(g, g.fork()); |
1192 |
< |
CCF f = new LCCF(null, 8); |
1192 |
> |
CCF f = new LCCF(8); |
1193 |
|
assertSame(f, f.fork()); |
1194 |
|
assertTrue(f.tryUnfork()); |
1195 |
|
helpQuiesce(); |
1204 |
|
* there are more tasks than threads |
1205 |
|
*/ |
1206 |
|
public void testGetSurplusQueuedTaskCount() { |
1207 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1208 |
< |
public void realCompute() { |
1209 |
< |
CCF h = new LCCF(null, 7); |
1207 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1208 |
> |
protected void realCompute() { |
1209 |
> |
CCF h = new LCCF(7); |
1210 |
|
assertSame(h, h.fork()); |
1211 |
< |
CCF g = new LCCF(null, 9); |
1211 |
> |
CCF g = new LCCF(9); |
1212 |
|
assertSame(g, g.fork()); |
1213 |
< |
CCF f = new LCCF(null, 8); |
1213 |
> |
CCF f = new LCCF(8); |
1214 |
|
assertSame(f, f.fork()); |
1215 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
1216 |
|
helpQuiesce(); |
1226 |
|
* peekNextLocalTask returns most recent unexecuted task. |
1227 |
|
*/ |
1228 |
|
public void testPeekNextLocalTask() { |
1229 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1230 |
< |
public void realCompute() { |
1231 |
< |
CCF g = new LCCF(null, 9); |
1229 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1230 |
> |
protected void realCompute() { |
1231 |
> |
CCF g = new LCCF(9); |
1232 |
|
assertSame(g, g.fork()); |
1233 |
< |
CCF f = new LCCF(null, 8); |
1233 |
> |
CCF f = new LCCF(8); |
1234 |
|
assertSame(f, f.fork()); |
1235 |
|
assertSame(f, peekNextLocalTask()); |
1236 |
|
assertNull(f.join()); |
1246 |
|
* executing it |
1247 |
|
*/ |
1248 |
|
public void testPollNextLocalTask() { |
1249 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1250 |
< |
public void realCompute() { |
1251 |
< |
CCF g = new LCCF(null, 9); |
1249 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1250 |
> |
protected void realCompute() { |
1251 |
> |
CCF g = new LCCF(9); |
1252 |
|
assertSame(g, g.fork()); |
1253 |
< |
CCF f = new LCCF(null, 8); |
1253 |
> |
CCF f = new LCCF(8); |
1254 |
|
assertSame(f, f.fork()); |
1255 |
|
assertSame(f, pollNextLocalTask()); |
1256 |
|
helpQuiesce(); |
1265 |
|
* pollTask returns an unexecuted task without executing it |
1266 |
|
*/ |
1267 |
|
public void testPollTask() { |
1268 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1269 |
< |
public void realCompute() { |
1270 |
< |
CCF g = new LCCF(null, 9); |
1268 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1269 |
> |
protected void realCompute() { |
1270 |
> |
CCF g = new LCCF(9); |
1271 |
|
assertSame(g, g.fork()); |
1272 |
< |
CCF f = new LCCF(null, 8); |
1272 |
> |
CCF f = new LCCF(8); |
1273 |
|
assertSame(f, f.fork()); |
1274 |
|
assertSame(f, pollTask()); |
1275 |
|
helpQuiesce(); |
1283 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
1284 |
|
*/ |
1285 |
|
public void testPeekNextLocalTaskAsync() { |
1286 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1287 |
< |
public void realCompute() { |
1288 |
< |
CCF g = new LCCF(null, 9); |
1286 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1287 |
> |
protected void realCompute() { |
1288 |
> |
CCF g = new LCCF(9); |
1289 |
|
assertSame(g, g.fork()); |
1290 |
< |
CCF f = new LCCF(null, 8); |
1290 |
> |
CCF f = new LCCF(8); |
1291 |
|
assertSame(f, f.fork()); |
1292 |
|
assertSame(g, peekNextLocalTask()); |
1293 |
|
assertNull(f.join()); |
1304 |
|
* executing it, in async mode |
1305 |
|
*/ |
1306 |
|
public void testPollNextLocalTaskAsync() { |
1307 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1308 |
< |
public void realCompute() { |
1309 |
< |
CCF g = new LCCF(null, 9); |
1307 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1308 |
> |
protected void realCompute() { |
1309 |
> |
CCF g = new LCCF(9); |
1310 |
|
assertSame(g, g.fork()); |
1311 |
< |
CCF f = new LCCF(null, 8); |
1311 |
> |
CCF f = new LCCF(8); |
1312 |
|
assertSame(f, f.fork()); |
1313 |
|
assertSame(g, pollNextLocalTask()); |
1314 |
|
helpQuiesce(); |
1324 |
|
* async mode |
1325 |
|
*/ |
1326 |
|
public void testPollTaskAsync() { |
1327 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1328 |
< |
public void realCompute() { |
1329 |
< |
CCF g = new LCCF(null, 9); |
1327 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1328 |
> |
protected void realCompute() { |
1329 |
> |
CCF g = new LCCF(9); |
1330 |
|
assertSame(g, g.fork()); |
1331 |
< |
CCF f = new LCCF(null, 8); |
1331 |
> |
CCF f = new LCCF(8); |
1332 |
|
assertSame(f, f.fork()); |
1333 |
|
assertSame(g, pollTask()); |
1334 |
|
helpQuiesce(); |
1347 |
|
* completed tasks; getRawResult returns null. |
1348 |
|
*/ |
1349 |
|
public void testInvokeSingleton() { |
1350 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1351 |
< |
public void realCompute() { |
1352 |
< |
CCF f = new LCCF(null, 8); |
1350 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1351 |
> |
protected void realCompute() { |
1352 |
> |
CCF f = new LCCF(8); |
1353 |
|
assertNull(f.invoke()); |
1354 |
|
assertEquals(21, f.number); |
1355 |
|
checkCompletedNormally(f); |
1363 |
|
* completed tasks |
1364 |
|
*/ |
1365 |
|
public void testQuietlyInvokeSingleton() { |
1366 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1367 |
< |
public void realCompute() { |
1368 |
< |
CCF f = new LCCF(null, 8); |
1366 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1367 |
> |
protected void realCompute() { |
1368 |
> |
CCF f = new LCCF(8); |
1369 |
|
f.quietlyInvoke(); |
1370 |
|
assertEquals(21, f.number); |
1371 |
|
checkCompletedNormally(f); |
1377 |
|
* join of a forked task returns when task completes |
1378 |
|
*/ |
1379 |
|
public void testForkJoinSingleton() { |
1380 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1381 |
< |
public void realCompute() { |
1382 |
< |
CCF f = new LCCF(null, 8); |
1380 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1381 |
> |
protected void realCompute() { |
1382 |
> |
CCF f = new LCCF(8); |
1383 |
|
assertSame(f, f.fork()); |
1384 |
|
assertNull(f.join()); |
1385 |
|
assertEquals(21, f.number); |
1392 |
|
* get of a forked task returns when task completes |
1393 |
|
*/ |
1394 |
|
public void testForkGetSingleton() { |
1395 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1396 |
< |
public void realCompute() throws Exception { |
1397 |
< |
CCF f = new LCCF(null, 8); |
1395 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1396 |
> |
protected void realCompute() throws Exception { |
1397 |
> |
CCF f = new LCCF(8); |
1398 |
|
assertSame(f, f.fork()); |
1399 |
|
assertNull(f.get()); |
1400 |
|
assertEquals(21, f.number); |
1407 |
|
* timed get of a forked task returns when task completes |
1408 |
|
*/ |
1409 |
|
public void testForkTimedGetSingleton() { |
1410 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1411 |
< |
public void realCompute() throws Exception { |
1412 |
< |
CCF f = new LCCF(null, 8); |
1410 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1411 |
> |
protected void realCompute() throws Exception { |
1412 |
> |
CCF f = new LCCF(8); |
1413 |
|
assertSame(f, f.fork()); |
1414 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1415 |
|
assertEquals(21, f.number); |
1422 |
|
* timed get with null time unit throws NPE |
1423 |
|
*/ |
1424 |
|
public void testForkTimedGetNPESingleton() { |
1425 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1426 |
< |
public void realCompute() throws Exception { |
1427 |
< |
CCF f = new LCCF(null, 8); |
1425 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1426 |
> |
protected void realCompute() throws Exception { |
1427 |
> |
CCF f = new LCCF(8); |
1428 |
|
assertSame(f, f.fork()); |
1429 |
|
try { |
1430 |
< |
f.get(5L, null); |
1430 |
> |
f.get(randomTimeout(), null); |
1431 |
|
shouldThrow(); |
1432 |
|
} catch (NullPointerException success) {} |
1433 |
|
}}; |
1438 |
|
* quietlyJoin of a forked task returns when task completes |
1439 |
|
*/ |
1440 |
|
public void testForkQuietlyJoinSingleton() { |
1441 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1442 |
< |
public void realCompute() { |
1443 |
< |
CCF f = new LCCF(null, 8); |
1441 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1442 |
> |
protected void realCompute() { |
1443 |
> |
CCF f = new LCCF(8); |
1444 |
|
assertSame(f, f.fork()); |
1445 |
|
f.quietlyJoin(); |
1446 |
|
assertEquals(21, f.number); |
1454 |
|
* getQueuedTaskCount returns 0 when quiescent |
1455 |
|
*/ |
1456 |
|
public void testForkHelpQuiesceSingleton() { |
1457 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1458 |
< |
public void realCompute() { |
1459 |
< |
CCF f = new LCCF(null, 8); |
1457 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1458 |
> |
protected void realCompute() { |
1459 |
> |
CCF f = new LCCF(8); |
1460 |
|
assertSame(f, f.fork()); |
1461 |
|
helpQuiesce(); |
1462 |
|
assertEquals(0, getQueuedTaskCount()); |
1470 |
|
* invoke task throws exception when task completes abnormally |
1471 |
|
*/ |
1472 |
|
public void testAbnormalInvokeSingleton() { |
1473 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1474 |
< |
public void realCompute() { |
1475 |
< |
FailingCCF f = new LFCCF(null, 8); |
1473 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1474 |
> |
protected void realCompute() { |
1475 |
> |
FailingCCF f = new LFCCF(8); |
1476 |
|
try { |
1477 |
|
f.invoke(); |
1478 |
|
shouldThrow(); |
1487 |
|
* quietlyInvoke task returns when task completes abnormally |
1488 |
|
*/ |
1489 |
|
public void testAbnormalQuietlyInvokeSingleton() { |
1490 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1491 |
< |
public void realCompute() { |
1492 |
< |
FailingCCF f = new LFCCF(null, 8); |
1490 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1491 |
> |
protected void realCompute() { |
1492 |
> |
FailingCCF f = new LFCCF(8); |
1493 |
|
f.quietlyInvoke(); |
1494 |
|
assertTrue(f.getException() instanceof FJException); |
1495 |
|
checkCompletedAbnormally(f, f.getException()); |
1501 |
|
* join of a forked task throws exception when task completes abnormally |
1502 |
|
*/ |
1503 |
|
public void testAbnormalForkJoinSingleton() { |
1504 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1505 |
< |
public void realCompute() { |
1506 |
< |
FailingCCF f = new LFCCF(null, 8); |
1504 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1505 |
> |
protected void realCompute() { |
1506 |
> |
FailingCCF f = new LFCCF(8); |
1507 |
|
assertSame(f, f.fork()); |
1508 |
|
try { |
1509 |
|
f.join(); |
1519 |
|
* get of a forked task throws exception when task completes abnormally |
1520 |
|
*/ |
1521 |
|
public void testAbnormalForkGetSingleton() { |
1522 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1523 |
< |
public void realCompute() throws Exception { |
1524 |
< |
FailingCCF f = new LFCCF(null, 8); |
1522 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1523 |
> |
protected void realCompute() throws Exception { |
1524 |
> |
FailingCCF f = new LFCCF(8); |
1525 |
|
assertSame(f, f.fork()); |
1526 |
|
try { |
1527 |
|
f.get(); |
1539 |
|
* timed get of a forked task throws exception when task completes abnormally |
1540 |
|
*/ |
1541 |
|
public void testAbnormalForkTimedGetSingleton() { |
1542 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1543 |
< |
public void realCompute() throws Exception { |
1544 |
< |
FailingCCF f = new LFCCF(null, 8); |
1542 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1543 |
> |
protected void realCompute() throws Exception { |
1544 |
> |
FailingCCF f = new LFCCF(8); |
1545 |
|
assertSame(f, f.fork()); |
1546 |
|
try { |
1547 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
1559 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
1560 |
|
*/ |
1561 |
|
public void testAbnormalForkQuietlyJoinSingleton() { |
1562 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1563 |
< |
public void realCompute() { |
1564 |
< |
FailingCCF f = new LFCCF(null, 8); |
1562 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1563 |
> |
protected void realCompute() { |
1564 |
> |
FailingCCF f = new LFCCF(8); |
1565 |
|
assertSame(f, f.fork()); |
1566 |
|
f.quietlyJoin(); |
1567 |
|
assertTrue(f.getException() instanceof FJException); |
1574 |
|
* invoke task throws exception when task cancelled |
1575 |
|
*/ |
1576 |
|
public void testCancelledInvokeSingleton() { |
1577 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1578 |
< |
public void realCompute() { |
1579 |
< |
CCF f = new LCCF(null, 8); |
1577 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1578 |
> |
protected void realCompute() { |
1579 |
> |
CCF f = new LCCF(8); |
1580 |
|
assertTrue(f.cancel(true)); |
1581 |
|
try { |
1582 |
|
f.invoke(); |
1592 |
|
* join of a forked task throws exception when task cancelled |
1593 |
|
*/ |
1594 |
|
public void testCancelledForkJoinSingleton() { |
1595 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1596 |
< |
public void realCompute() { |
1597 |
< |
CCF f = new LCCF(null, 8); |
1595 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1596 |
> |
protected void realCompute() { |
1597 |
> |
CCF f = new LCCF(8); |
1598 |
|
assertTrue(f.cancel(true)); |
1599 |
|
assertSame(f, f.fork()); |
1600 |
|
try { |
1611 |
|
* get of a forked task throws exception when task cancelled |
1612 |
|
*/ |
1613 |
|
public void testCancelledForkGetSingleton() { |
1614 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1615 |
< |
public void realCompute() throws Exception { |
1616 |
< |
CCF f = new LCCF(null, 8); |
1614 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1615 |
> |
protected void realCompute() throws Exception { |
1616 |
> |
CCF f = new LCCF(8); |
1617 |
|
assertTrue(f.cancel(true)); |
1618 |
|
assertSame(f, f.fork()); |
1619 |
|
try { |
1630 |
|
* timed get of a forked task throws exception when task cancelled |
1631 |
|
*/ |
1632 |
|
public void testCancelledForkTimedGetSingleton() throws Exception { |
1633 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1634 |
< |
public void realCompute() throws Exception { |
1635 |
< |
CCF f = new LCCF(null, 8); |
1633 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1634 |
> |
protected void realCompute() throws Exception { |
1635 |
> |
CCF f = new LCCF(8); |
1636 |
|
assertTrue(f.cancel(true)); |
1637 |
|
assertSame(f, f.fork()); |
1638 |
|
try { |
1649 |
|
* quietlyJoin of a forked task returns when task cancelled |
1650 |
|
*/ |
1651 |
|
public void testCancelledForkQuietlyJoinSingleton() { |
1652 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1653 |
< |
public void realCompute() { |
1654 |
< |
CCF f = new LCCF(null, 8); |
1652 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1653 |
> |
protected void realCompute() { |
1654 |
> |
CCF f = new LCCF(8); |
1655 |
|
assertTrue(f.cancel(true)); |
1656 |
|
assertSame(f, f.fork()); |
1657 |
|
f.quietlyJoin(); |
1664 |
|
* invoke task throws exception after invoking completeExceptionally |
1665 |
|
*/ |
1666 |
|
public void testCompleteExceptionallySingleton() { |
1667 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1668 |
< |
public void realCompute() { |
1669 |
< |
CCF f = new LCCF(null, 8); |
1670 |
< |
f.completeExceptionally(new FJException()); |
1671 |
< |
try { |
1672 |
< |
f.invoke(); |
1673 |
< |
shouldThrow(); |
1674 |
< |
} catch (FJException success) { |
1576 |
< |
checkCompletedAbnormally(f, success); |
1577 |
< |
} |
1667 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1668 |
> |
protected void realCompute() { |
1669 |
> |
CCF n = new LCCF(8); |
1670 |
> |
CCF f = new LCCF(n, 8); |
1671 |
> |
FJException ex = new FJException(); |
1672 |
> |
f.completeExceptionally(ex); |
1673 |
> |
f.checkCompletedExceptionally(ex); |
1674 |
> |
n.checkCompletedExceptionally(ex); |
1675 |
|
}}; |
1676 |
|
testInvokeOnPool(singletonPool(), a); |
1677 |
|
} |
1680 |
|
* invokeAll(t1, t2) invokes all task arguments |
1681 |
|
*/ |
1682 |
|
public void testInvokeAll2Singleton() { |
1683 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1684 |
< |
public void realCompute() { |
1685 |
< |
CCF f = new LCCF(null, 8); |
1686 |
< |
CCF g = new LCCF(null, 9); |
1683 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1684 |
> |
protected void realCompute() { |
1685 |
> |
CCF f = new LCCF(8); |
1686 |
> |
CCF g = new LCCF(9); |
1687 |
|
invokeAll(f, g); |
1688 |
|
assertEquals(21, f.number); |
1689 |
|
assertEquals(34, g.number); |
1697 |
|
* invokeAll(tasks) with 1 argument invokes task |
1698 |
|
*/ |
1699 |
|
public void testInvokeAll1Singleton() { |
1700 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1701 |
< |
public void realCompute() { |
1702 |
< |
CCF f = new LCCF(null, 8); |
1700 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1701 |
> |
protected void realCompute() { |
1702 |
> |
CCF f = new LCCF(8); |
1703 |
|
invokeAll(f); |
1704 |
|
checkCompletedNormally(f); |
1705 |
|
assertEquals(21, f.number); |
1711 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
1712 |
|
*/ |
1713 |
|
public void testInvokeAll3Singleton() { |
1714 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1715 |
< |
public void realCompute() { |
1716 |
< |
CCF f = new LCCF(null, 8); |
1717 |
< |
CCF g = new LCCF(null, 9); |
1718 |
< |
CCF h = new LCCF(null, 7); |
1714 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1715 |
> |
protected void realCompute() { |
1716 |
> |
CCF f = new LCCF(8); |
1717 |
> |
CCF g = new LCCF(9); |
1718 |
> |
CCF h = new LCCF(7); |
1719 |
|
invokeAll(f, g, h); |
1720 |
|
assertEquals(21, f.number); |
1721 |
|
assertEquals(34, g.number); |
1731 |
|
* invokeAll(collection) invokes all tasks in the collection |
1732 |
|
*/ |
1733 |
|
public void testInvokeAllCollectionSingleton() { |
1734 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1735 |
< |
public void realCompute() { |
1736 |
< |
CCF f = new LCCF(null, 8); |
1737 |
< |
CCF g = new LCCF(null, 9); |
1738 |
< |
CCF h = new LCCF(null, 7); |
1739 |
< |
HashSet set = new HashSet(); |
1734 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1735 |
> |
protected void realCompute() { |
1736 |
> |
CCF f = new LCCF(8); |
1737 |
> |
CCF g = new LCCF(9); |
1738 |
> |
CCF h = new LCCF(7); |
1739 |
> |
HashSet<ForkJoinTask<?>> set = new HashSet<>(); |
1740 |
|
set.add(f); |
1741 |
|
set.add(g); |
1742 |
|
set.add(h); |
1755 |
|
* invokeAll(tasks) with any null task throws NPE |
1756 |
|
*/ |
1757 |
|
public void testInvokeAllNPESingleton() { |
1758 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1759 |
< |
public void realCompute() { |
1760 |
< |
CCF f = new LCCF(null, 8); |
1761 |
< |
CCF g = new LCCF(null, 9); |
1758 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1759 |
> |
protected void realCompute() { |
1760 |
> |
CCF f = new LCCF(8); |
1761 |
> |
CCF g = new LCCF(9); |
1762 |
|
CCF h = null; |
1763 |
|
try { |
1764 |
|
invokeAll(f, g, h); |
1772 |
|
* invokeAll(t1, t2) throw exception if any task does |
1773 |
|
*/ |
1774 |
|
public void testAbnormalInvokeAll2Singleton() { |
1775 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1776 |
< |
public void realCompute() { |
1777 |
< |
CCF f = new LCCF(null, 8); |
1778 |
< |
FailingCCF g = new LFCCF(null, 9); |
1775 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1776 |
> |
protected void realCompute() { |
1777 |
> |
CCF f = new LCCF(8); |
1778 |
> |
FailingCCF g = new LFCCF(9); |
1779 |
|
try { |
1780 |
|
invokeAll(f, g); |
1781 |
|
shouldThrow(); |
1790 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
1791 |
|
*/ |
1792 |
|
public void testAbnormalInvokeAll1Singleton() { |
1793 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1794 |
< |
public void realCompute() { |
1795 |
< |
FailingCCF g = new LFCCF(null, 9); |
1793 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1794 |
> |
protected void realCompute() { |
1795 |
> |
FailingCCF g = new LFCCF(9); |
1796 |
|
try { |
1797 |
|
invokeAll(g); |
1798 |
|
shouldThrow(); |
1807 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1808 |
|
*/ |
1809 |
|
public void testAbnormalInvokeAll3Singleton() { |
1810 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1811 |
< |
public void realCompute() { |
1812 |
< |
CCF f = new LCCF(null, 8); |
1813 |
< |
FailingCCF g = new LFCCF(null, 9); |
1814 |
< |
CCF h = new LCCF(null, 7); |
1810 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1811 |
> |
protected void realCompute() { |
1812 |
> |
CCF f = new LCCF(8); |
1813 |
> |
FailingCCF g = new LFCCF(9); |
1814 |
> |
CCF h = new LCCF(7); |
1815 |
|
try { |
1816 |
|
invokeAll(f, g, h); |
1817 |
|
shouldThrow(); |
1823 |
|
} |
1824 |
|
|
1825 |
|
/** |
1826 |
< |
* invokeAll(collection) throws exception if any task does |
1826 |
> |
* invokeAll(collection) throws exception if any task does |
1827 |
|
*/ |
1828 |
|
public void testAbnormalInvokeAllCollectionSingleton() { |
1829 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1830 |
< |
public void realCompute() { |
1831 |
< |
FailingCCF f = new LFCCF(null, 8); |
1832 |
< |
CCF g = new LCCF(null, 9); |
1833 |
< |
CCF h = new LCCF(null, 7); |
1834 |
< |
HashSet set = new HashSet(); |
1829 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1830 |
> |
protected void realCompute() { |
1831 |
> |
FailingCCF f = new LFCCF(8); |
1832 |
> |
CCF g = new LCCF(9); |
1833 |
> |
CCF h = new LCCF(7); |
1834 |
> |
HashSet<ForkJoinTask<?>> set = new HashSet<>(); |
1835 |
|
set.add(f); |
1836 |
|
set.add(g); |
1837 |
|
set.add(h); |