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 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
> |
|
10 |
> |
import java.util.HashSet; |
11 |
|
import java.util.concurrent.CancellationException; |
12 |
+ |
import java.util.concurrent.CountedCompleter; |
13 |
+ |
import java.util.concurrent.ExecutionException; |
14 |
|
import java.util.concurrent.ForkJoinPool; |
15 |
|
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; |
16 |
|
import java.util.concurrent.TimeoutException; |
17 |
< |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
18 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
19 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
20 |
< |
import java.util.HashSet; |
21 |
< |
import junit.framework.*; |
17 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
18 |
> |
import java.util.concurrent.atomic.AtomicReference; |
19 |
> |
|
20 |
> |
import junit.framework.Test; |
21 |
> |
import junit.framework.TestSuite; |
22 |
|
|
23 |
|
public class CountedCompleterTest extends JSR166TestCase { |
24 |
|
|
25 |
|
public static void main(String[] args) { |
26 |
< |
junit.textui.TestRunner.run(suite()); |
26 |
> |
main(suite(), args); |
27 |
|
} |
28 |
|
|
29 |
|
public static Test suite() { |
34 |
|
static final int mainPoolSize = |
35 |
|
Math.max(2, Runtime.getRuntime().availableProcessors()); |
36 |
|
|
35 |
– |
/** |
36 |
– |
* Analog of CheckedRunnable for CountedCompleter |
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 |
– |
|
37 |
|
private static ForkJoinPool mainPool() { |
38 |
|
return new ForkJoinPool(mainPoolSize); |
39 |
|
} |
49 |
|
} |
50 |
|
|
51 |
|
private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) { |
52 |
< |
try { |
52 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
53 |
|
assertFalse(a.isDone()); |
54 |
|
assertFalse(a.isCompletedNormally()); |
55 |
|
assertFalse(a.isCompletedAbnormally()); |
65 |
|
assertFalse(a.isCancelled()); |
66 |
|
assertNull(a.getException()); |
67 |
|
assertNull(a.getRawResult()); |
81 |
– |
} finally { |
82 |
– |
joinPool(pool); |
68 |
|
} |
69 |
|
} |
70 |
|
|
83 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
84 |
|
} |
85 |
|
|
86 |
< |
<T> void checkCompletedNormally(CountedCompleter<T> a) { |
102 |
< |
checkCompletedNormally(a, null); |
103 |
< |
} |
104 |
< |
|
105 |
< |
<T> void checkCompletedNormally(CountedCompleter<T> a, T expected) { |
86 |
> |
void checkCompletedNormally(CountedCompleter<?> a) { |
87 |
|
assertTrue(a.isDone()); |
88 |
|
assertFalse(a.isCancelled()); |
89 |
|
assertTrue(a.isCompletedNormally()); |
90 |
|
assertFalse(a.isCompletedAbnormally()); |
91 |
|
assertNull(a.getException()); |
92 |
< |
assertSame(expected, a.getRawResult()); |
92 |
> |
assertNull(a.getRawResult()); |
93 |
|
|
94 |
|
{ |
95 |
|
Thread.currentThread().interrupt(); |
96 |
< |
long t0 = System.nanoTime(); |
97 |
< |
assertSame(expected, a.join()); |
98 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
96 |
> |
long startTime = System.nanoTime(); |
97 |
> |
assertNull(a.join()); |
98 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
99 |
|
Thread.interrupted(); |
100 |
|
} |
101 |
|
|
102 |
|
{ |
103 |
|
Thread.currentThread().interrupt(); |
104 |
< |
long t0 = System.nanoTime(); |
104 |
> |
long startTime = System.nanoTime(); |
105 |
|
a.quietlyJoin(); // should be no-op |
106 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
106 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
107 |
|
Thread.interrupted(); |
108 |
|
} |
109 |
|
|
110 |
|
assertFalse(a.cancel(false)); |
111 |
|
assertFalse(a.cancel(true)); |
112 |
|
try { |
113 |
< |
assertSame(expected, a.get()); |
113 |
> |
assertNull(a.get()); |
114 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
115 |
|
try { |
116 |
< |
assertSame(expected, a.get(5L, SECONDS)); |
116 |
> |
assertNull(a.get(5L, SECONDS)); |
117 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
118 |
|
} |
119 |
|
|
136 |
|
Thread.interrupted(); |
137 |
|
|
138 |
|
{ |
139 |
< |
long t0 = System.nanoTime(); |
139 |
> |
long startTime = System.nanoTime(); |
140 |
|
a.quietlyJoin(); // should be no-op |
141 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
141 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
142 |
|
} |
143 |
|
|
144 |
|
try { |
174 |
|
Thread.interrupted(); |
175 |
|
|
176 |
|
{ |
177 |
< |
long t0 = System.nanoTime(); |
177 |
> |
long startTime = System.nanoTime(); |
178 |
|
a.quietlyJoin(); // should be no-op |
179 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
179 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
180 |
|
} |
181 |
|
|
182 |
|
try { |
192 |
|
} catch (ExecutionException success) { |
193 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
194 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
195 |
+ |
|
196 |
+ |
try { |
197 |
+ |
a.invoke(); |
198 |
+ |
shouldThrow(); |
199 |
+ |
} catch (Throwable success) { |
200 |
+ |
assertSame(t, success); |
201 |
+ |
} |
202 |
|
} |
203 |
|
|
204 |
|
public static final class FJException extends RuntimeException { |
205 |
|
FJException() { super(); } |
206 |
|
} |
207 |
|
|
208 |
< |
static abstract class FailingCCF extends CountedCompleter { |
209 |
< |
int number; |
210 |
< |
int rnumber; |
211 |
< |
|
212 |
< |
public FailingCCF(CountedCompleter parent, int n) { |
213 |
< |
super(parent, 1); |
214 |
< |
this.number = n; |
215 |
< |
} |
216 |
< |
|
208 |
> |
abstract class CheckedCC extends CountedCompleter<Object> { |
209 |
> |
final AtomicInteger computeN = new AtomicInteger(0); |
210 |
> |
final AtomicInteger onCompletionN = new AtomicInteger(0); |
211 |
> |
final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0); |
212 |
> |
final AtomicInteger setRawResultN = new AtomicInteger(0); |
213 |
> |
final AtomicReference<Object> rawResult = new AtomicReference<Object>(null); |
214 |
> |
int computeN() { return computeN.get(); } |
215 |
> |
int onCompletionN() { return onCompletionN.get(); } |
216 |
> |
int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); } |
217 |
> |
int setRawResultN() { return setRawResultN.get(); } |
218 |
> |
|
219 |
> |
CheckedCC() { super(); } |
220 |
> |
CheckedCC(CountedCompleter p) { super(p); } |
221 |
> |
CheckedCC(CountedCompleter p, int n) { super(p, n); } |
222 |
> |
abstract void realCompute(); |
223 |
|
public final void compute() { |
224 |
< |
CountedCompleter p; |
225 |
< |
FailingCCF f = this; |
232 |
< |
int n = number; |
233 |
< |
while (n >= 2) { |
234 |
< |
new RFCCF(f, n - 2).fork(); |
235 |
< |
f = new LFCCF(f, --n); |
236 |
< |
} |
237 |
< |
f.number = n; |
238 |
< |
f.onCompletion(f); |
239 |
< |
if ((p = f.getCompleter()) != null) |
240 |
< |
p.tryComplete(); |
241 |
< |
else |
242 |
< |
f.quietlyComplete(); |
243 |
< |
} |
244 |
< |
} |
245 |
< |
|
246 |
< |
static final class LFCCF extends FailingCCF { |
247 |
< |
public LFCCF(CountedCompleter parent, int n) { |
248 |
< |
super(parent, n); |
224 |
> |
computeN.incrementAndGet(); |
225 |
> |
realCompute(); |
226 |
|
} |
227 |
< |
public final void onCompletion(CountedCompleter caller) { |
228 |
< |
FailingCCF p = (FailingCCF)getCompleter(); |
229 |
< |
int n = number + rnumber; |
253 |
< |
if (p != null) |
254 |
< |
p.number = n; |
255 |
< |
else |
256 |
< |
number = n; |
257 |
< |
} |
258 |
< |
} |
259 |
< |
static final class RFCCF extends FailingCCF { |
260 |
< |
public RFCCF(CountedCompleter parent, int n) { |
261 |
< |
super(parent, n); |
262 |
< |
} |
263 |
< |
public final void onCompletion(CountedCompleter caller) { |
264 |
< |
completeExceptionally(new FJException()); |
227 |
> |
public void onCompletion(CountedCompleter caller) { |
228 |
> |
onCompletionN.incrementAndGet(); |
229 |
> |
super.onCompletion(caller); |
230 |
|
} |
231 |
< |
} |
232 |
< |
|
233 |
< |
static abstract class CCF extends CountedCompleter { |
234 |
< |
int number; |
235 |
< |
int rnumber; |
236 |
< |
|
237 |
< |
public CCF(CountedCompleter parent, int n) { |
273 |
< |
super(parent, 1); |
274 |
< |
this.number = n; |
231 |
> |
public boolean onExceptionalCompletion(Throwable ex, |
232 |
> |
CountedCompleter caller) { |
233 |
> |
onExceptionalCompletionN.incrementAndGet(); |
234 |
> |
assertNotNull(ex); |
235 |
> |
assertTrue(isCompletedAbnormally()); |
236 |
> |
assertTrue(super.onExceptionalCompletion(ex, caller)); |
237 |
> |
return true; |
238 |
|
} |
239 |
< |
|
240 |
< |
public final void compute() { |
241 |
< |
CountedCompleter p; |
242 |
< |
CCF f = this; |
280 |
< |
int n = number; |
281 |
< |
while (n >= 2) { |
282 |
< |
new RCCF(f, n - 2).fork(); |
283 |
< |
f = new LCCF(f, --n); |
284 |
< |
} |
285 |
< |
f.number = n; |
286 |
< |
f.onCompletion(f); |
287 |
< |
if ((p = f.getCompleter()) != null) |
288 |
< |
p.tryComplete(); |
289 |
< |
else |
290 |
< |
f.quietlyComplete(); |
239 |
> |
protected void setRawResult(Object t) { |
240 |
> |
setRawResultN.incrementAndGet(); |
241 |
> |
rawResult.set(t); |
242 |
> |
super.setRawResult(t); |
243 |
|
} |
244 |
< |
} |
245 |
< |
|
246 |
< |
static final class LCCF extends CCF { |
247 |
< |
public LCCF(CountedCompleter parent, int n) { |
248 |
< |
super(parent, n); |
244 |
> |
void checkIncomplete() { |
245 |
> |
assertEquals(0, computeN()); |
246 |
> |
assertEquals(0, onCompletionN()); |
247 |
> |
assertEquals(0, onExceptionalCompletionN()); |
248 |
> |
assertEquals(0, setRawResultN()); |
249 |
> |
checkNotDone(this); |
250 |
|
} |
251 |
< |
public final void onCompletion(CountedCompleter caller) { |
252 |
< |
CCF p = (CCF)getCompleter(); |
253 |
< |
int n = number + rnumber; |
254 |
< |
if (p != null) |
255 |
< |
p.number = n; |
256 |
< |
else |
257 |
< |
number = n; |
251 |
> |
void checkCompletes(Object rawResult) { |
252 |
> |
checkIncomplete(); |
253 |
> |
int pendingCount = getPendingCount(); |
254 |
> |
complete(rawResult); |
255 |
> |
assertEquals(pendingCount, getPendingCount()); |
256 |
> |
assertEquals(0, computeN()); |
257 |
> |
assertEquals(1, onCompletionN()); |
258 |
> |
assertEquals(0, onExceptionalCompletionN()); |
259 |
> |
assertEquals(1, setRawResultN()); |
260 |
> |
assertSame(rawResult, this.rawResult.get()); |
261 |
> |
checkCompletedNormally(this); |
262 |
|
} |
263 |
< |
} |
264 |
< |
static final class RCCF extends CCF { |
265 |
< |
public RCCF(CountedCompleter parent, int n) { |
266 |
< |
super(parent, n); |
263 |
> |
void checkCompletesExceptionally(Throwable ex) { |
264 |
> |
checkIncomplete(); |
265 |
> |
completeExceptionally(ex); |
266 |
> |
checkCompletedExceptionally(ex); |
267 |
|
} |
268 |
< |
public final void onCompletion(CountedCompleter caller) { |
269 |
< |
CCF p = (CCF)getCompleter(); |
270 |
< |
int n = number + rnumber; |
271 |
< |
if (p != null) |
272 |
< |
p.rnumber = n; |
273 |
< |
else |
274 |
< |
number = n; |
268 |
> |
void checkCompletedExceptionally(Throwable ex) { |
269 |
> |
assertEquals(0, computeN()); |
270 |
> |
assertEquals(0, onCompletionN()); |
271 |
> |
assertEquals(1, onExceptionalCompletionN()); |
272 |
> |
assertEquals(0, setRawResultN()); |
273 |
> |
assertNull(this.rawResult.get()); |
274 |
> |
checkCompletedAbnormally(this, ex); |
275 |
|
} |
276 |
|
} |
277 |
|
|
278 |
< |
static final class NoopCountedCompleter extends CountedCompleter { |
279 |
< |
boolean post; // set true if onCompletion called |
280 |
< |
NoopCountedCompleter() { super(); } |
281 |
< |
NoopCountedCompleter(CountedCompleter p) { super(p); } |
282 |
< |
public void compute() {} |
326 |
< |
public final void onCompletion(CountedCompleter caller) { |
327 |
< |
post = true; |
278 |
> |
final class NoopCC extends CheckedCC { |
279 |
> |
NoopCC() { super(); } |
280 |
> |
NoopCC(CountedCompleter p) { super(p); } |
281 |
> |
NoopCC(CountedCompleter p, int initialPendingCount) { |
282 |
> |
super(p, initialPendingCount); |
283 |
|
} |
284 |
+ |
protected void realCompute() {} |
285 |
|
} |
286 |
|
|
287 |
|
/** |
288 |
< |
* A newly constructed CountedCompleter is not completed; |
289 |
< |
* complete() causes completion. |
288 |
> |
* A newly constructed CountedCompleter is not completed; |
289 |
> |
* complete() causes completion. pendingCount is ignored. |
290 |
|
*/ |
291 |
|
public void testComplete() { |
292 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
293 |
< |
assertFalse(a.isDone()); |
294 |
< |
assertFalse(a.isCompletedNormally()); |
295 |
< |
assertFalse(a.isCompletedAbnormally()); |
296 |
< |
assertFalse(a.isCancelled()); |
297 |
< |
assertNull(a.getException()); |
298 |
< |
assertNull(a.getRawResult()); |
299 |
< |
assertFalse(a.post); |
300 |
< |
a.complete(null); |
301 |
< |
assertTrue(a.post); |
302 |
< |
assertTrue(a.isDone()); |
347 |
< |
assertTrue(a.isCompletedNormally()); |
348 |
< |
assertFalse(a.isCompletedAbnormally()); |
349 |
< |
assertFalse(a.isCancelled()); |
350 |
< |
assertNull(a.getException()); |
351 |
< |
assertNull(a.getRawResult()); |
292 |
> |
for (Object x : new Object[] { Boolean.TRUE, null }) { |
293 |
> |
for (int pendingCount : new int[] { 0, 42 }) { |
294 |
> |
testComplete(new NoopCC(), x, pendingCount); |
295 |
> |
testComplete(new NoopCC(new NoopCC()), x, pendingCount); |
296 |
> |
} |
297 |
> |
} |
298 |
> |
} |
299 |
> |
void testComplete(NoopCC cc, Object x, int pendingCount) { |
300 |
> |
cc.setPendingCount(pendingCount); |
301 |
> |
cc.checkCompletes(x); |
302 |
> |
assertEquals(pendingCount, cc.getPendingCount()); |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
|
* completeExceptionally completes exceptionally |
307 |
|
*/ |
308 |
|
public void testCompleteExceptionally() { |
309 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
310 |
< |
assertFalse(a.isDone()); |
311 |
< |
assertFalse(a.isCompletedNormally()); |
312 |
< |
assertFalse(a.isCompletedAbnormally()); |
313 |
< |
assertFalse(a.isCancelled()); |
314 |
< |
assertNull(a.getException()); |
315 |
< |
assertNull(a.getRawResult()); |
316 |
< |
assertFalse(a.post); |
317 |
< |
a.completeExceptionally(new FJException()); |
318 |
< |
assertFalse(a.post); |
319 |
< |
assertTrue(a.isDone()); |
320 |
< |
assertFalse(a.isCompletedNormally()); |
321 |
< |
assertTrue(a.isCompletedAbnormally()); |
322 |
< |
assertFalse(a.isCancelled()); |
323 |
< |
assertTrue(a.getException() instanceof FJException); |
324 |
< |
assertNull(a.getRawResult()); |
309 |
> |
new NoopCC() |
310 |
> |
.checkCompletesExceptionally(new FJException()); |
311 |
> |
new NoopCC(new NoopCC()) |
312 |
> |
.checkCompletesExceptionally(new FJException()); |
313 |
> |
} |
314 |
> |
|
315 |
> |
/** |
316 |
> |
* completeExceptionally(null) surprisingly has the same effect as |
317 |
> |
* completeExceptionally(new RuntimeException()) |
318 |
> |
*/ |
319 |
> |
public void testCompleteExceptionally_null() { |
320 |
> |
NoopCC a = new NoopCC(); |
321 |
> |
a.completeExceptionally(null); |
322 |
> |
try { |
323 |
> |
a.invoke(); |
324 |
> |
shouldThrow(); |
325 |
> |
} catch (RuntimeException success) { |
326 |
> |
assertSame(success.getClass(), RuntimeException.class); |
327 |
> |
assertNull(success.getCause()); |
328 |
> |
a.checkCompletedExceptionally(success); |
329 |
> |
} |
330 |
|
} |
331 |
|
|
332 |
|
/** |
333 |
|
* setPendingCount sets the reported pending count |
334 |
|
*/ |
335 |
|
public void testSetPendingCount() { |
336 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
336 |
> |
NoopCC a = new NoopCC(); |
337 |
|
assertEquals(0, a.getPendingCount()); |
338 |
< |
a.setPendingCount(1); |
339 |
< |
assertEquals(1, a.getPendingCount()); |
340 |
< |
a.setPendingCount(27); |
341 |
< |
assertEquals(27, a.getPendingCount()); |
338 |
> |
int[] vals = { |
339 |
> |
-1, 0, 1, |
340 |
> |
Integer.MIN_VALUE, |
341 |
> |
Integer.MAX_VALUE, |
342 |
> |
}; |
343 |
> |
for (int val : vals) { |
344 |
> |
a.setPendingCount(val); |
345 |
> |
assertEquals(val, a.getPendingCount()); |
346 |
> |
} |
347 |
|
} |
348 |
|
|
349 |
|
/** |
350 |
|
* addToPendingCount adds to the reported pending count |
351 |
|
*/ |
352 |
|
public void testAddToPendingCount() { |
353 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
353 |
> |
NoopCC a = new NoopCC(); |
354 |
|
assertEquals(0, a.getPendingCount()); |
355 |
|
a.addToPendingCount(1); |
356 |
|
assertEquals(1, a.getPendingCount()); |
362 |
|
* decrementPendingCountUnlessZero decrements reported pending |
363 |
|
* count unless zero |
364 |
|
*/ |
365 |
< |
public void testDecrementPendingCount() { |
366 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
367 |
< |
assertEquals(0, a.getPendingCount()); |
368 |
< |
a.addToPendingCount(1); |
365 |
> |
public void testDecrementPendingCountUnlessZero() { |
366 |
> |
NoopCC a = new NoopCC(null, 2); |
367 |
> |
assertEquals(2, a.getPendingCount()); |
368 |
> |
assertEquals(2, a.decrementPendingCountUnlessZero()); |
369 |
|
assertEquals(1, a.getPendingCount()); |
370 |
< |
a.decrementPendingCountUnlessZero(); |
370 |
> |
assertEquals(1, a.decrementPendingCountUnlessZero()); |
371 |
|
assertEquals(0, a.getPendingCount()); |
372 |
< |
a.decrementPendingCountUnlessZero(); |
372 |
> |
assertEquals(0, a.decrementPendingCountUnlessZero()); |
373 |
> |
assertEquals(0, a.getPendingCount()); |
374 |
> |
a.setPendingCount(-1); |
375 |
> |
assertEquals(-1, a.decrementPendingCountUnlessZero()); |
376 |
> |
assertEquals(-2, a.getPendingCount()); |
377 |
> |
} |
378 |
> |
|
379 |
> |
/** |
380 |
> |
* compareAndSetPendingCount compares and sets the reported |
381 |
> |
* pending count |
382 |
> |
*/ |
383 |
> |
public void testCompareAndSetPendingCount() { |
384 |
> |
NoopCC a = new NoopCC(); |
385 |
|
assertEquals(0, a.getPendingCount()); |
386 |
+ |
assertTrue(a.compareAndSetPendingCount(0, 1)); |
387 |
+ |
assertEquals(1, a.getPendingCount()); |
388 |
+ |
assertTrue(a.compareAndSetPendingCount(1, 2)); |
389 |
+ |
assertEquals(2, a.getPendingCount()); |
390 |
+ |
assertFalse(a.compareAndSetPendingCount(1, 3)); |
391 |
+ |
assertEquals(2, a.getPendingCount()); |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
|
* getCompleter returns parent or null if at root |
396 |
|
*/ |
397 |
|
public void testGetCompleter() { |
398 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
398 |
> |
NoopCC a = new NoopCC(); |
399 |
|
assertNull(a.getCompleter()); |
400 |
< |
CountedCompleter b = new NoopCountedCompleter(a); |
401 |
< |
assertEquals(a, b.getCompleter()); |
400 |
> |
CountedCompleter b = new NoopCC(a); |
401 |
> |
assertSame(a, b.getCompleter()); |
402 |
> |
CountedCompleter c = new NoopCC(b); |
403 |
> |
assertSame(b, c.getCompleter()); |
404 |
|
} |
405 |
< |
|
405 |
> |
|
406 |
|
/** |
407 |
|
* getRoot returns self if no parent, else parent's root |
408 |
|
*/ |
409 |
|
public void testGetRoot() { |
410 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
411 |
< |
NoopCountedCompleter b = new NoopCountedCompleter(a); |
412 |
< |
assertEquals(a, a.getRoot()); |
413 |
< |
assertEquals(a, b.getRoot()); |
410 |
> |
NoopCC a = new NoopCC(); |
411 |
> |
NoopCC b = new NoopCC(a); |
412 |
> |
NoopCC c = new NoopCC(b); |
413 |
> |
assertSame(a, a.getRoot()); |
414 |
> |
assertSame(a, b.getRoot()); |
415 |
> |
assertSame(a, c.getRoot()); |
416 |
|
} |
417 |
< |
|
417 |
> |
|
418 |
|
/** |
419 |
< |
* tryComplete causes completion if pending count is zero |
419 |
> |
* tryComplete decrements pending count unless zero, in which case |
420 |
> |
* causes completion |
421 |
|
*/ |
422 |
< |
public void testTryComplete1() { |
423 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
422 |
> |
public void testTryComplete() { |
423 |
> |
NoopCC a = new NoopCC(); |
424 |
|
assertEquals(0, a.getPendingCount()); |
425 |
+ |
int n = 3; |
426 |
+ |
a.setPendingCount(n); |
427 |
+ |
for (; n > 0; n--) { |
428 |
+ |
assertEquals(n, a.getPendingCount()); |
429 |
+ |
a.tryComplete(); |
430 |
+ |
a.checkIncomplete(); |
431 |
+ |
assertEquals(n - 1, a.getPendingCount()); |
432 |
+ |
} |
433 |
|
a.tryComplete(); |
434 |
< |
assertTrue(a.post); |
435 |
< |
assertTrue(a.isDone()); |
434 |
> |
assertEquals(0, a.computeN()); |
435 |
> |
assertEquals(1, a.onCompletionN()); |
436 |
> |
assertEquals(0, a.onExceptionalCompletionN()); |
437 |
> |
assertEquals(0, a.setRawResultN()); |
438 |
> |
checkCompletedNormally(a); |
439 |
|
} |
440 |
|
|
441 |
|
/** |
442 |
< |
* propagateCompletion causes completion without invokein |
443 |
< |
* onCompletion if pending count is zero |
442 |
> |
* propagateCompletion decrements pending count unless zero, in |
443 |
> |
* which case causes completion, without invoking onCompletion |
444 |
|
*/ |
445 |
|
public void testPropagateCompletion() { |
446 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
446 |
> |
NoopCC a = new NoopCC(); |
447 |
|
assertEquals(0, a.getPendingCount()); |
448 |
+ |
int n = 3; |
449 |
+ |
a.setPendingCount(n); |
450 |
+ |
for (; n > 0; n--) { |
451 |
+ |
assertEquals(n, a.getPendingCount()); |
452 |
+ |
a.propagateCompletion(); |
453 |
+ |
a.checkIncomplete(); |
454 |
+ |
assertEquals(n - 1, a.getPendingCount()); |
455 |
+ |
} |
456 |
|
a.propagateCompletion(); |
457 |
< |
assertFalse(a.post); |
458 |
< |
assertTrue(a.isDone()); |
459 |
< |
} |
460 |
< |
|
461 |
< |
/** |
459 |
< |
* tryComplete decrments pending count unless zero |
460 |
< |
*/ |
461 |
< |
public void testTryComplete2() { |
462 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
463 |
< |
assertEquals(0, a.getPendingCount()); |
464 |
< |
a.setPendingCount(1); |
465 |
< |
a.tryComplete(); |
466 |
< |
assertFalse(a.post); |
467 |
< |
assertFalse(a.isDone()); |
468 |
< |
assertEquals(0, a.getPendingCount()); |
469 |
< |
a.tryComplete(); |
470 |
< |
assertTrue(a.post); |
471 |
< |
assertTrue(a.isDone()); |
457 |
> |
assertEquals(0, a.computeN()); |
458 |
> |
assertEquals(0, a.onCompletionN()); |
459 |
> |
assertEquals(0, a.onExceptionalCompletionN()); |
460 |
> |
assertEquals(0, a.setRawResultN()); |
461 |
> |
checkCompletedNormally(a); |
462 |
|
} |
463 |
|
|
464 |
|
/** |
465 |
|
* firstComplete returns this if pending count is zero else null |
466 |
|
*/ |
467 |
|
public void testFirstComplete() { |
468 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
468 |
> |
NoopCC a = new NoopCC(); |
469 |
|
a.setPendingCount(1); |
470 |
|
assertNull(a.firstComplete()); |
471 |
< |
assertEquals(a, a.firstComplete()); |
471 |
> |
a.checkIncomplete(); |
472 |
> |
assertSame(a, a.firstComplete()); |
473 |
> |
a.checkIncomplete(); |
474 |
|
} |
475 |
|
|
476 |
|
/** |
478 |
|
* zero else null |
479 |
|
*/ |
480 |
|
public void testNextComplete() { |
481 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
482 |
< |
NoopCountedCompleter b = new NoopCountedCompleter(a); |
481 |
> |
NoopCC a = new NoopCC(); |
482 |
> |
NoopCC b = new NoopCC(a); |
483 |
|
a.setPendingCount(1); |
484 |
|
b.setPendingCount(1); |
485 |
|
assertNull(b.firstComplete()); |
486 |
< |
CountedCompleter c = b.firstComplete(); |
487 |
< |
assertEquals(b, c); |
488 |
< |
CountedCompleter d = c.nextComplete(); |
489 |
< |
assertNull(d); |
490 |
< |
CountedCompleter e = c.nextComplete(); |
491 |
< |
assertEquals(a, e); |
486 |
> |
assertSame(b, b.firstComplete()); |
487 |
> |
assertNull(b.nextComplete()); |
488 |
> |
a.checkIncomplete(); |
489 |
> |
b.checkIncomplete(); |
490 |
> |
assertSame(a, b.nextComplete()); |
491 |
> |
assertSame(a, b.nextComplete()); |
492 |
> |
a.checkIncomplete(); |
493 |
> |
b.checkIncomplete(); |
494 |
> |
assertNull(a.nextComplete()); |
495 |
> |
b.checkIncomplete(); |
496 |
> |
checkCompletedNormally(a); |
497 |
|
} |
498 |
|
|
499 |
|
/** |
500 |
|
* quietlyCompleteRoot completes root task |
501 |
|
*/ |
502 |
|
public void testQuietlyCompleteRoot() { |
503 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
504 |
< |
NoopCountedCompleter b = new NoopCountedCompleter(a); |
503 |
> |
NoopCC a = new NoopCC(); |
504 |
> |
NoopCC b = new NoopCC(a); |
505 |
> |
NoopCC c = new NoopCC(b); |
506 |
|
a.setPendingCount(1); |
507 |
|
b.setPendingCount(1); |
508 |
< |
b.quietlyCompleteRoot(); |
508 |
> |
c.setPendingCount(1); |
509 |
> |
c.quietlyCompleteRoot(); |
510 |
|
assertTrue(a.isDone()); |
511 |
|
assertFalse(b.isDone()); |
512 |
+ |
assertFalse(c.isDone()); |
513 |
+ |
} |
514 |
+ |
|
515 |
+ |
// Invocation tests use some interdependent task classes |
516 |
+ |
// to better test propagation etc |
517 |
+ |
|
518 |
+ |
/** |
519 |
+ |
* Version of Fibonacci with different classes for left vs right forks |
520 |
+ |
*/ |
521 |
+ |
abstract class CCF extends CheckedCC { |
522 |
+ |
int number; |
523 |
+ |
int rnumber; |
524 |
+ |
|
525 |
+ |
public CCF(CountedCompleter parent, int n) { |
526 |
+ |
super(parent, 1); |
527 |
+ |
this.number = n; |
528 |
+ |
} |
529 |
+ |
|
530 |
+ |
protected final void realCompute() { |
531 |
+ |
CCF f = this; |
532 |
+ |
int n = number; |
533 |
+ |
while (n >= 2) { |
534 |
+ |
new RCCF(f, n - 2).fork(); |
535 |
+ |
f = new LCCF(f, --n); |
536 |
+ |
} |
537 |
+ |
f.complete(null); |
538 |
+ |
} |
539 |
+ |
} |
540 |
+ |
|
541 |
+ |
final class LCCF extends CCF { |
542 |
+ |
public LCCF(int n) { this(null, n); } |
543 |
+ |
public LCCF(CountedCompleter parent, int n) { |
544 |
+ |
super(parent, n); |
545 |
+ |
} |
546 |
+ |
public final void onCompletion(CountedCompleter caller) { |
547 |
+ |
super.onCompletion(caller); |
548 |
+ |
CCF p = (CCF)getCompleter(); |
549 |
+ |
int n = number + rnumber; |
550 |
+ |
if (p != null) |
551 |
+ |
p.number = n; |
552 |
+ |
else |
553 |
+ |
number = n; |
554 |
+ |
} |
555 |
+ |
} |
556 |
+ |
final class RCCF extends CCF { |
557 |
+ |
public RCCF(CountedCompleter parent, int n) { |
558 |
+ |
super(parent, n); |
559 |
+ |
} |
560 |
+ |
public final void onCompletion(CountedCompleter caller) { |
561 |
+ |
super.onCompletion(caller); |
562 |
+ |
CCF p = (CCF)getCompleter(); |
563 |
+ |
int n = number + rnumber; |
564 |
+ |
if (p != null) |
565 |
+ |
p.rnumber = n; |
566 |
+ |
else |
567 |
+ |
number = n; |
568 |
+ |
} |
569 |
+ |
} |
570 |
+ |
|
571 |
+ |
// Version of CCF with forced failure in left completions |
572 |
+ |
abstract class FailingCCF extends CheckedCC { |
573 |
+ |
int number; |
574 |
+ |
int rnumber; |
575 |
+ |
|
576 |
+ |
public FailingCCF(CountedCompleter parent, int n) { |
577 |
+ |
super(parent, 1); |
578 |
+ |
this.number = n; |
579 |
+ |
} |
580 |
+ |
|
581 |
+ |
protected final void realCompute() { |
582 |
+ |
FailingCCF f = this; |
583 |
+ |
int n = number; |
584 |
+ |
while (n >= 2) { |
585 |
+ |
new RFCCF(f, n - 2).fork(); |
586 |
+ |
f = new LFCCF(f, --n); |
587 |
+ |
} |
588 |
+ |
f.complete(null); |
589 |
+ |
} |
590 |
+ |
} |
591 |
+ |
|
592 |
+ |
final class LFCCF extends FailingCCF { |
593 |
+ |
public LFCCF(int n) { this(null, n); } |
594 |
+ |
public LFCCF(CountedCompleter parent, int n) { |
595 |
+ |
super(parent, n); |
596 |
+ |
} |
597 |
+ |
public final void onCompletion(CountedCompleter caller) { |
598 |
+ |
super.onCompletion(caller); |
599 |
+ |
FailingCCF p = (FailingCCF)getCompleter(); |
600 |
+ |
int n = number + rnumber; |
601 |
+ |
if (p != null) |
602 |
+ |
p.number = n; |
603 |
+ |
else |
604 |
+ |
number = n; |
605 |
+ |
} |
606 |
+ |
} |
607 |
+ |
final class RFCCF extends FailingCCF { |
608 |
+ |
public RFCCF(CountedCompleter parent, int n) { |
609 |
+ |
super(parent, n); |
610 |
+ |
} |
611 |
+ |
public final void onCompletion(CountedCompleter caller) { |
612 |
+ |
super.onCompletion(caller); |
613 |
+ |
completeExceptionally(new FJException()); |
614 |
+ |
} |
615 |
|
} |
616 |
< |
|
616 |
> |
|
617 |
|
/** |
618 |
|
* invoke returns when task completes normally. |
619 |
|
* isCompletedAbnormally and isCancelled return false for normally |
620 |
|
* completed tasks; getRawResult returns null. |
621 |
|
*/ |
622 |
|
public void testInvoke() { |
623 |
< |
ForkJoinTask a = new CheckedFJTask() { |
624 |
< |
public void realCompute() { |
625 |
< |
CCF f = new LCCF(null, 8); |
623 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
624 |
> |
protected void realCompute() { |
625 |
> |
CCF f = new LCCF(8); |
626 |
|
assertNull(f.invoke()); |
627 |
|
assertEquals(21, f.number); |
628 |
|
checkCompletedNormally(f); |
636 |
|
* completed tasks |
637 |
|
*/ |
638 |
|
public void testQuietlyInvoke() { |
639 |
< |
ForkJoinTask a = new CheckedFJTask() { |
640 |
< |
public void realCompute() { |
641 |
< |
CCF f = new LCCF(null, 8); |
639 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
640 |
> |
protected void realCompute() { |
641 |
> |
CCF f = new LCCF(8); |
642 |
|
f.quietlyInvoke(); |
643 |
|
assertEquals(21, f.number); |
644 |
|
checkCompletedNormally(f); |
650 |
|
* join of a forked task returns when task completes |
651 |
|
*/ |
652 |
|
public void testForkJoin() { |
653 |
< |
ForkJoinTask a = new CheckedFJTask() { |
654 |
< |
public void realCompute() { |
655 |
< |
CCF f = new LCCF(null, 8); |
653 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
654 |
> |
protected void realCompute() { |
655 |
> |
CCF f = new LCCF(8); |
656 |
|
assertSame(f, f.fork()); |
657 |
|
assertNull(f.join()); |
658 |
|
assertEquals(21, f.number); |
665 |
|
* get of a forked task returns when task completes |
666 |
|
*/ |
667 |
|
public void testForkGet() { |
668 |
< |
ForkJoinTask a = new CheckedFJTask() { |
669 |
< |
public void realCompute() throws Exception { |
670 |
< |
CCF f = new LCCF(null, 8); |
668 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
669 |
> |
protected void realCompute() throws Exception { |
670 |
> |
CCF f = new LCCF(8); |
671 |
|
assertSame(f, f.fork()); |
672 |
|
assertNull(f.get()); |
673 |
|
assertEquals(21, f.number); |
680 |
|
* timed get of a forked task returns when task completes |
681 |
|
*/ |
682 |
|
public void testForkTimedGet() { |
683 |
< |
ForkJoinTask a = new CheckedFJTask() { |
684 |
< |
public void realCompute() throws Exception { |
685 |
< |
CCF f = new LCCF(null, 8); |
683 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
684 |
> |
protected void realCompute() throws Exception { |
685 |
> |
CCF f = new LCCF(8); |
686 |
|
assertSame(f, f.fork()); |
687 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
688 |
|
assertEquals(21, f.number); |
695 |
|
* timed get with null time unit throws NPE |
696 |
|
*/ |
697 |
|
public void testForkTimedGetNPE() { |
698 |
< |
ForkJoinTask a = new CheckedFJTask() { |
699 |
< |
public void realCompute() throws Exception { |
700 |
< |
CCF f = new LCCF(null, 8); |
698 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
699 |
> |
protected void realCompute() throws Exception { |
700 |
> |
CCF f = new LCCF(8); |
701 |
|
assertSame(f, f.fork()); |
702 |
|
try { |
703 |
|
f.get(5L, null); |
711 |
|
* quietlyJoin of a forked task returns when task completes |
712 |
|
*/ |
713 |
|
public void testForkQuietlyJoin() { |
714 |
< |
ForkJoinTask a = new CheckedFJTask() { |
715 |
< |
public void realCompute() { |
716 |
< |
CCF f = new LCCF(null, 8); |
714 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
715 |
> |
protected void realCompute() { |
716 |
> |
CCF f = new LCCF(8); |
717 |
|
assertSame(f, f.fork()); |
718 |
|
f.quietlyJoin(); |
719 |
|
assertEquals(21, f.number); |
727 |
|
* getQueuedTaskCount returns 0 when quiescent |
728 |
|
*/ |
729 |
|
public void testForkHelpQuiesce() { |
730 |
< |
ForkJoinTask a = new CheckedFJTask() { |
731 |
< |
public void realCompute() { |
732 |
< |
CCF f = new LCCF(null, 8); |
730 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
731 |
> |
protected void realCompute() { |
732 |
> |
CCF f = new LCCF(8); |
733 |
|
assertSame(f, f.fork()); |
734 |
|
helpQuiesce(); |
735 |
|
assertEquals(21, f.number); |
743 |
|
* invoke task throws exception when task completes abnormally |
744 |
|
*/ |
745 |
|
public void testAbnormalInvoke() { |
746 |
< |
ForkJoinTask a = new CheckedFJTask() { |
747 |
< |
public void realCompute() { |
748 |
< |
FailingCCF f = new LFCCF(null, 8); |
746 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
747 |
> |
protected void realCompute() { |
748 |
> |
FailingCCF f = new LFCCF(8); |
749 |
|
try { |
750 |
|
f.invoke(); |
751 |
|
shouldThrow(); |
760 |
|
* quietlyInvoke task returns when task completes abnormally |
761 |
|
*/ |
762 |
|
public void testAbnormalQuietlyInvoke() { |
763 |
< |
ForkJoinTask a = new CheckedFJTask() { |
764 |
< |
public void realCompute() { |
765 |
< |
FailingCCF f = new LFCCF(null, 8); |
763 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
764 |
> |
protected void realCompute() { |
765 |
> |
FailingCCF f = new LFCCF(8); |
766 |
|
f.quietlyInvoke(); |
767 |
|
assertTrue(f.getException() instanceof FJException); |
768 |
|
checkCompletedAbnormally(f, f.getException()); |
774 |
|
* join of a forked task throws exception when task completes abnormally |
775 |
|
*/ |
776 |
|
public void testAbnormalForkJoin() { |
777 |
< |
ForkJoinTask a = new CheckedFJTask() { |
778 |
< |
public void realCompute() { |
779 |
< |
FailingCCF f = new LFCCF(null, 8); |
777 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
778 |
> |
protected void realCompute() { |
779 |
> |
FailingCCF f = new LFCCF(8); |
780 |
|
assertSame(f, f.fork()); |
781 |
|
try { |
782 |
|
f.join(); |
792 |
|
* get of a forked task throws exception when task completes abnormally |
793 |
|
*/ |
794 |
|
public void testAbnormalForkGet() { |
795 |
< |
ForkJoinTask a = new CheckedFJTask() { |
796 |
< |
public void realCompute() throws Exception { |
797 |
< |
FailingCCF f = new LFCCF(null, 8); |
795 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
796 |
> |
protected void realCompute() throws Exception { |
797 |
> |
FailingCCF f = new LFCCF(8); |
798 |
|
assertSame(f, f.fork()); |
799 |
|
try { |
800 |
|
f.get(); |
812 |
|
* timed get of a forked task throws exception when task completes abnormally |
813 |
|
*/ |
814 |
|
public void testAbnormalForkTimedGet() { |
815 |
< |
ForkJoinTask a = new CheckedFJTask() { |
816 |
< |
public void realCompute() throws Exception { |
817 |
< |
FailingCCF f = new LFCCF(null, 8); |
815 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
816 |
> |
protected void realCompute() throws Exception { |
817 |
> |
FailingCCF f = new LFCCF(8); |
818 |
|
assertSame(f, f.fork()); |
819 |
|
try { |
820 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
832 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
833 |
|
*/ |
834 |
|
public void testAbnormalForkQuietlyJoin() { |
835 |
< |
ForkJoinTask a = new CheckedFJTask() { |
836 |
< |
public void realCompute() { |
837 |
< |
FailingCCF f = new LFCCF(null, 8); |
835 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
836 |
> |
protected void realCompute() { |
837 |
> |
FailingCCF f = new LFCCF(8); |
838 |
|
assertSame(f, f.fork()); |
839 |
|
f.quietlyJoin(); |
840 |
|
assertTrue(f.getException() instanceof FJException); |
847 |
|
* invoke task throws exception when task cancelled |
848 |
|
*/ |
849 |
|
public void testCancelledInvoke() { |
850 |
< |
ForkJoinTask a = new CheckedFJTask() { |
851 |
< |
public void realCompute() { |
852 |
< |
CCF f = new LCCF(null, 8); |
850 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
851 |
> |
protected void realCompute() { |
852 |
> |
CCF f = new LCCF(8); |
853 |
|
assertTrue(f.cancel(true)); |
854 |
|
try { |
855 |
|
f.invoke(); |
865 |
|
* join of a forked task throws exception when task cancelled |
866 |
|
*/ |
867 |
|
public void testCancelledForkJoin() { |
868 |
< |
ForkJoinTask a = new CheckedFJTask() { |
869 |
< |
public void realCompute() { |
870 |
< |
CCF f = new LCCF(null, 8); |
868 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
869 |
> |
protected void realCompute() { |
870 |
> |
CCF f = new LCCF(8); |
871 |
|
assertTrue(f.cancel(true)); |
872 |
|
assertSame(f, f.fork()); |
873 |
|
try { |
884 |
|
* get of a forked task throws exception when task cancelled |
885 |
|
*/ |
886 |
|
public void testCancelledForkGet() { |
887 |
< |
ForkJoinTask a = new CheckedFJTask() { |
888 |
< |
public void realCompute() throws Exception { |
889 |
< |
CCF f = new LCCF(null, 8); |
887 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
888 |
> |
protected void realCompute() throws Exception { |
889 |
> |
CCF f = new LCCF(8); |
890 |
|
assertTrue(f.cancel(true)); |
891 |
|
assertSame(f, f.fork()); |
892 |
|
try { |
903 |
|
* timed get of a forked task throws exception when task cancelled |
904 |
|
*/ |
905 |
|
public void testCancelledForkTimedGet() throws Exception { |
906 |
< |
ForkJoinTask a = new CheckedFJTask() { |
907 |
< |
public void realCompute() throws Exception { |
908 |
< |
CCF f = new LCCF(null, 8); |
906 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
907 |
> |
protected void realCompute() throws Exception { |
908 |
> |
CCF f = new LCCF(8); |
909 |
|
assertTrue(f.cancel(true)); |
910 |
|
assertSame(f, f.fork()); |
911 |
|
try { |
922 |
|
* quietlyJoin of a forked task returns when task cancelled |
923 |
|
*/ |
924 |
|
public void testCancelledForkQuietlyJoin() { |
925 |
< |
ForkJoinTask a = new CheckedFJTask() { |
926 |
< |
public void realCompute() { |
927 |
< |
CCF f = new LCCF(null, 8); |
925 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
926 |
> |
protected void realCompute() { |
927 |
> |
CCF f = new LCCF(8); |
928 |
|
assertTrue(f.cancel(true)); |
929 |
|
assertSame(f, f.fork()); |
930 |
|
f.quietlyJoin(); |
938 |
|
*/ |
939 |
|
public void testGetPool() { |
940 |
|
final ForkJoinPool mainPool = mainPool(); |
941 |
< |
ForkJoinTask a = new CheckedFJTask() { |
942 |
< |
public void realCompute() { |
941 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
942 |
> |
protected void realCompute() { |
943 |
|
assertSame(mainPool, getPool()); |
944 |
|
}}; |
945 |
|
testInvokeOnPool(mainPool, a); |
949 |
|
* getPool of non-FJ task returns null |
950 |
|
*/ |
951 |
|
public void testGetPool2() { |
952 |
< |
ForkJoinTask a = new CheckedFJTask() { |
953 |
< |
public void realCompute() { |
952 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
953 |
> |
protected void realCompute() { |
954 |
|
assertNull(getPool()); |
955 |
|
}}; |
956 |
|
assertNull(a.invoke()); |
960 |
|
* inForkJoinPool of executing task returns true |
961 |
|
*/ |
962 |
|
public void testInForkJoinPool() { |
963 |
< |
ForkJoinTask a = new CheckedFJTask() { |
964 |
< |
public void realCompute() { |
963 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
964 |
> |
protected void realCompute() { |
965 |
|
assertTrue(inForkJoinPool()); |
966 |
|
}}; |
967 |
|
testInvokeOnPool(mainPool(), a); |
971 |
|
* inForkJoinPool of non-FJ task returns false |
972 |
|
*/ |
973 |
|
public void testInForkJoinPool2() { |
974 |
< |
ForkJoinTask a = new CheckedFJTask() { |
975 |
< |
public void realCompute() { |
974 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
975 |
> |
protected void realCompute() { |
976 |
|
assertFalse(inForkJoinPool()); |
977 |
|
}}; |
978 |
|
assertNull(a.invoke()); |
982 |
|
* setRawResult(null) succeeds |
983 |
|
*/ |
984 |
|
public void testSetRawResult() { |
985 |
< |
ForkJoinTask a = new CheckedFJTask() { |
986 |
< |
public void realCompute() { |
985 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
986 |
> |
protected void realCompute() { |
987 |
|
setRawResult(null); |
988 |
|
assertNull(getRawResult()); |
989 |
|
}}; |
994 |
|
* invoke task throws exception after invoking completeExceptionally |
995 |
|
*/ |
996 |
|
public void testCompleteExceptionally2() { |
997 |
< |
ForkJoinTask a = new CheckedFJTask() { |
998 |
< |
public void realCompute() { |
999 |
< |
CCF f = new LCCF(null, 8); |
1000 |
< |
f.completeExceptionally(new FJException()); |
1001 |
< |
try { |
1002 |
< |
f.invoke(); |
1003 |
< |
shouldThrow(); |
1004 |
< |
} catch (FJException success) { |
903 |
< |
checkCompletedAbnormally(f, success); |
904 |
< |
} |
997 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
998 |
> |
protected void realCompute() { |
999 |
> |
CCF n = new LCCF(8); |
1000 |
> |
CCF f = new LCCF(n, 8); |
1001 |
> |
FJException ex = new FJException(); |
1002 |
> |
f.completeExceptionally(ex); |
1003 |
> |
f.checkCompletedExceptionally(ex); |
1004 |
> |
n.checkCompletedExceptionally(ex); |
1005 |
|
}}; |
1006 |
|
testInvokeOnPool(mainPool(), a); |
1007 |
|
} |
1010 |
|
* invokeAll(t1, t2) invokes all task arguments |
1011 |
|
*/ |
1012 |
|
public void testInvokeAll2() { |
1013 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1014 |
< |
public void realCompute() { |
1015 |
< |
CCF f = new LCCF(null, 8); |
1016 |
< |
CCF g = new LCCF(null, 9); |
1013 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1014 |
> |
protected void realCompute() { |
1015 |
> |
CCF f = new LCCF(8); |
1016 |
> |
CCF g = new LCCF(9); |
1017 |
|
invokeAll(f, g); |
1018 |
|
assertEquals(21, f.number); |
1019 |
|
assertEquals(34, g.number); |
1027 |
|
* invokeAll(tasks) with 1 argument invokes task |
1028 |
|
*/ |
1029 |
|
public void testInvokeAll1() { |
1030 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1031 |
< |
public void realCompute() { |
1032 |
< |
CCF f = new LCCF(null, 8); |
1030 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1031 |
> |
protected void realCompute() { |
1032 |
> |
CCF f = new LCCF(8); |
1033 |
|
invokeAll(f); |
1034 |
|
checkCompletedNormally(f); |
1035 |
|
assertEquals(21, f.number); |
1041 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
1042 |
|
*/ |
1043 |
|
public void testInvokeAll3() { |
1044 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1045 |
< |
public void realCompute() { |
1046 |
< |
CCF f = new LCCF(null, 8); |
1047 |
< |
CCF g = new LCCF(null, 9); |
1048 |
< |
CCF h = new LCCF(null, 7); |
1044 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1045 |
> |
protected void realCompute() { |
1046 |
> |
CCF f = new LCCF(8); |
1047 |
> |
CCF g = new LCCF(9); |
1048 |
> |
CCF h = new LCCF(7); |
1049 |
|
invokeAll(f, g, h); |
1050 |
|
assertEquals(21, f.number); |
1051 |
|
assertEquals(34, g.number); |
1061 |
|
* invokeAll(collection) invokes all tasks in the collection |
1062 |
|
*/ |
1063 |
|
public void testInvokeAllCollection() { |
1064 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1065 |
< |
public void realCompute() { |
1066 |
< |
CCF f = new LCCF(null, 8); |
1067 |
< |
CCF g = new LCCF(null, 9); |
1068 |
< |
CCF h = new LCCF(null, 7); |
1064 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1065 |
> |
protected void realCompute() { |
1066 |
> |
CCF f = new LCCF(8); |
1067 |
> |
CCF g = new LCCF(9); |
1068 |
> |
CCF h = new LCCF(7); |
1069 |
|
HashSet set = new HashSet(); |
1070 |
|
set.add(f); |
1071 |
|
set.add(g); |
1085 |
|
* invokeAll(tasks) with any null task throws NPE |
1086 |
|
*/ |
1087 |
|
public void testInvokeAllNPE() { |
1088 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1089 |
< |
public void realCompute() { |
1090 |
< |
CCF f = new LCCF(null, 8); |
1091 |
< |
CCF g = new LCCF(null, 9); |
1088 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1089 |
> |
protected void realCompute() { |
1090 |
> |
CCF f = new LCCF(8); |
1091 |
> |
CCF g = new LCCF(9); |
1092 |
|
CCF h = null; |
1093 |
|
try { |
1094 |
|
invokeAll(f, g, h); |
1102 |
|
* invokeAll(t1, t2) throw exception if any task does |
1103 |
|
*/ |
1104 |
|
public void testAbnormalInvokeAll2() { |
1105 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1106 |
< |
public void realCompute() { |
1107 |
< |
CCF f = new LCCF(null, 8); |
1108 |
< |
FailingCCF g = new LFCCF(null, 9); |
1105 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1106 |
> |
protected void realCompute() { |
1107 |
> |
CCF f = new LCCF(8); |
1108 |
> |
FailingCCF g = new LFCCF(9); |
1109 |
|
try { |
1110 |
|
invokeAll(f, g); |
1111 |
|
shouldThrow(); |
1120 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
1121 |
|
*/ |
1122 |
|
public void testAbnormalInvokeAll1() { |
1123 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1124 |
< |
public void realCompute() { |
1125 |
< |
FailingCCF g = new LFCCF(null, 9); |
1123 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1124 |
> |
protected void realCompute() { |
1125 |
> |
FailingCCF g = new LFCCF(9); |
1126 |
|
try { |
1127 |
|
invokeAll(g); |
1128 |
|
shouldThrow(); |
1137 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1138 |
|
*/ |
1139 |
|
public void testAbnormalInvokeAll3() { |
1140 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1141 |
< |
public void realCompute() { |
1142 |
< |
CCF f = new LCCF(null, 8); |
1143 |
< |
FailingCCF g = new LFCCF(null, 9); |
1144 |
< |
CCF h = new LCCF(null, 7); |
1140 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1141 |
> |
protected void realCompute() { |
1142 |
> |
CCF f = new LCCF(8); |
1143 |
> |
FailingCCF g = new LFCCF(9); |
1144 |
> |
CCF h = new LCCF(7); |
1145 |
|
try { |
1146 |
|
invokeAll(f, g, h); |
1147 |
|
shouldThrow(); |
1153 |
|
} |
1154 |
|
|
1155 |
|
/** |
1156 |
< |
* invokeAll(collection) throws exception if any task does |
1156 |
> |
* invokeAll(collection) throws exception if any task does |
1157 |
|
*/ |
1158 |
|
public void testAbnormalInvokeAllCollection() { |
1159 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1160 |
< |
public void realCompute() { |
1161 |
< |
FailingCCF f = new LFCCF(null, 8); |
1162 |
< |
CCF g = new LCCF(null, 9); |
1163 |
< |
CCF h = new LCCF(null, 7); |
1159 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1160 |
> |
protected void realCompute() { |
1161 |
> |
FailingCCF f = new LFCCF(8); |
1162 |
> |
CCF g = new LCCF(9); |
1163 |
> |
CCF h = new LCCF(7); |
1164 |
|
HashSet set = new HashSet(); |
1165 |
|
set.add(f); |
1166 |
|
set.add(g); |
1180 |
|
* and suppresses execution |
1181 |
|
*/ |
1182 |
|
public void testTryUnfork() { |
1183 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1184 |
< |
public void realCompute() { |
1185 |
< |
CCF g = new LCCF(null, 9); |
1183 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1184 |
> |
protected void realCompute() { |
1185 |
> |
CCF g = new LCCF(9); |
1186 |
|
assertSame(g, g.fork()); |
1187 |
< |
CCF f = new LCCF(null, 8); |
1187 |
> |
CCF f = new LCCF(8); |
1188 |
|
assertSame(f, f.fork()); |
1189 |
|
assertTrue(f.tryUnfork()); |
1190 |
|
helpQuiesce(); |
1199 |
|
* there are more tasks than threads |
1200 |
|
*/ |
1201 |
|
public void testGetSurplusQueuedTaskCount() { |
1202 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1203 |
< |
public void realCompute() { |
1204 |
< |
CCF h = new LCCF(null, 7); |
1202 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1203 |
> |
protected void realCompute() { |
1204 |
> |
CCF h = new LCCF(7); |
1205 |
|
assertSame(h, h.fork()); |
1206 |
< |
CCF g = new LCCF(null, 9); |
1206 |
> |
CCF g = new LCCF(9); |
1207 |
|
assertSame(g, g.fork()); |
1208 |
< |
CCF f = new LCCF(null, 8); |
1208 |
> |
CCF f = new LCCF(8); |
1209 |
|
assertSame(f, f.fork()); |
1210 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
1211 |
|
helpQuiesce(); |
1221 |
|
* peekNextLocalTask returns most recent unexecuted task. |
1222 |
|
*/ |
1223 |
|
public void testPeekNextLocalTask() { |
1224 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1225 |
< |
public void realCompute() { |
1226 |
< |
CCF g = new LCCF(null, 9); |
1224 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1225 |
> |
protected void realCompute() { |
1226 |
> |
CCF g = new LCCF(9); |
1227 |
|
assertSame(g, g.fork()); |
1228 |
< |
CCF f = new LCCF(null, 8); |
1228 |
> |
CCF f = new LCCF(8); |
1229 |
|
assertSame(f, f.fork()); |
1230 |
|
assertSame(f, peekNextLocalTask()); |
1231 |
|
assertNull(f.join()); |
1241 |
|
* executing it |
1242 |
|
*/ |
1243 |
|
public void testPollNextLocalTask() { |
1244 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1245 |
< |
public void realCompute() { |
1246 |
< |
CCF g = new LCCF(null, 9); |
1244 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1245 |
> |
protected void realCompute() { |
1246 |
> |
CCF g = new LCCF(9); |
1247 |
|
assertSame(g, g.fork()); |
1248 |
< |
CCF f = new LCCF(null, 8); |
1248 |
> |
CCF f = new LCCF(8); |
1249 |
|
assertSame(f, f.fork()); |
1250 |
|
assertSame(f, pollNextLocalTask()); |
1251 |
|
helpQuiesce(); |
1260 |
|
* pollTask returns an unexecuted task without executing it |
1261 |
|
*/ |
1262 |
|
public void testPollTask() { |
1263 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1264 |
< |
public void realCompute() { |
1265 |
< |
CCF g = new LCCF(null, 9); |
1263 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1264 |
> |
protected void realCompute() { |
1265 |
> |
CCF g = new LCCF(9); |
1266 |
|
assertSame(g, g.fork()); |
1267 |
< |
CCF f = new LCCF(null, 8); |
1267 |
> |
CCF f = new LCCF(8); |
1268 |
|
assertSame(f, f.fork()); |
1269 |
|
assertSame(f, pollTask()); |
1270 |
|
helpQuiesce(); |
1278 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
1279 |
|
*/ |
1280 |
|
public void testPeekNextLocalTaskAsync() { |
1281 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1282 |
< |
public void realCompute() { |
1283 |
< |
CCF g = new LCCF(null, 9); |
1281 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1282 |
> |
protected void realCompute() { |
1283 |
> |
CCF g = new LCCF(9); |
1284 |
|
assertSame(g, g.fork()); |
1285 |
< |
CCF f = new LCCF(null, 8); |
1285 |
> |
CCF f = new LCCF(8); |
1286 |
|
assertSame(f, f.fork()); |
1287 |
|
assertSame(g, peekNextLocalTask()); |
1288 |
|
assertNull(f.join()); |
1299 |
|
* executing it, in async mode |
1300 |
|
*/ |
1301 |
|
public void testPollNextLocalTaskAsync() { |
1302 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1303 |
< |
public void realCompute() { |
1304 |
< |
CCF g = new LCCF(null, 9); |
1302 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1303 |
> |
protected void realCompute() { |
1304 |
> |
CCF g = new LCCF(9); |
1305 |
|
assertSame(g, g.fork()); |
1306 |
< |
CCF f = new LCCF(null, 8); |
1306 |
> |
CCF f = new LCCF(8); |
1307 |
|
assertSame(f, f.fork()); |
1308 |
|
assertSame(g, pollNextLocalTask()); |
1309 |
|
helpQuiesce(); |
1319 |
|
* async mode |
1320 |
|
*/ |
1321 |
|
public void testPollTaskAsync() { |
1322 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1323 |
< |
public void realCompute() { |
1324 |
< |
CCF g = new LCCF(null, 9); |
1322 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1323 |
> |
protected void realCompute() { |
1324 |
> |
CCF g = new LCCF(9); |
1325 |
|
assertSame(g, g.fork()); |
1326 |
< |
CCF f = new LCCF(null, 8); |
1326 |
> |
CCF f = new LCCF(8); |
1327 |
|
assertSame(f, f.fork()); |
1328 |
|
assertSame(g, pollTask()); |
1329 |
|
helpQuiesce(); |
1342 |
|
* completed tasks; getRawResult returns null. |
1343 |
|
*/ |
1344 |
|
public void testInvokeSingleton() { |
1345 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1346 |
< |
public void realCompute() { |
1347 |
< |
CCF f = new LCCF(null, 8); |
1345 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1346 |
> |
protected void realCompute() { |
1347 |
> |
CCF f = new LCCF(8); |
1348 |
|
assertNull(f.invoke()); |
1349 |
|
assertEquals(21, f.number); |
1350 |
|
checkCompletedNormally(f); |
1358 |
|
* completed tasks |
1359 |
|
*/ |
1360 |
|
public void testQuietlyInvokeSingleton() { |
1361 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1362 |
< |
public void realCompute() { |
1363 |
< |
CCF f = new LCCF(null, 8); |
1361 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1362 |
> |
protected void realCompute() { |
1363 |
> |
CCF f = new LCCF(8); |
1364 |
|
f.quietlyInvoke(); |
1365 |
|
assertEquals(21, f.number); |
1366 |
|
checkCompletedNormally(f); |
1372 |
|
* join of a forked task returns when task completes |
1373 |
|
*/ |
1374 |
|
public void testForkJoinSingleton() { |
1375 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1376 |
< |
public void realCompute() { |
1377 |
< |
CCF f = new LCCF(null, 8); |
1375 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1376 |
> |
protected void realCompute() { |
1377 |
> |
CCF f = new LCCF(8); |
1378 |
|
assertSame(f, f.fork()); |
1379 |
|
assertNull(f.join()); |
1380 |
|
assertEquals(21, f.number); |
1387 |
|
* get of a forked task returns when task completes |
1388 |
|
*/ |
1389 |
|
public void testForkGetSingleton() { |
1390 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1391 |
< |
public void realCompute() throws Exception { |
1392 |
< |
CCF f = new LCCF(null, 8); |
1390 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1391 |
> |
protected void realCompute() throws Exception { |
1392 |
> |
CCF f = new LCCF(8); |
1393 |
|
assertSame(f, f.fork()); |
1394 |
|
assertNull(f.get()); |
1395 |
|
assertEquals(21, f.number); |
1402 |
|
* timed get of a forked task returns when task completes |
1403 |
|
*/ |
1404 |
|
public void testForkTimedGetSingleton() { |
1405 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1406 |
< |
public void realCompute() throws Exception { |
1407 |
< |
CCF f = new LCCF(null, 8); |
1405 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1406 |
> |
protected void realCompute() throws Exception { |
1407 |
> |
CCF f = new LCCF(8); |
1408 |
|
assertSame(f, f.fork()); |
1409 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1410 |
|
assertEquals(21, f.number); |
1417 |
|
* timed get with null time unit throws NPE |
1418 |
|
*/ |
1419 |
|
public void testForkTimedGetNPESingleton() { |
1420 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1421 |
< |
public void realCompute() throws Exception { |
1422 |
< |
CCF f = new LCCF(null, 8); |
1420 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1421 |
> |
protected void realCompute() throws Exception { |
1422 |
> |
CCF f = new LCCF(8); |
1423 |
|
assertSame(f, f.fork()); |
1424 |
|
try { |
1425 |
|
f.get(5L, null); |
1433 |
|
* quietlyJoin of a forked task returns when task completes |
1434 |
|
*/ |
1435 |
|
public void testForkQuietlyJoinSingleton() { |
1436 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1437 |
< |
public void realCompute() { |
1438 |
< |
CCF f = new LCCF(null, 8); |
1436 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1437 |
> |
protected void realCompute() { |
1438 |
> |
CCF f = new LCCF(8); |
1439 |
|
assertSame(f, f.fork()); |
1440 |
|
f.quietlyJoin(); |
1441 |
|
assertEquals(21, f.number); |
1449 |
|
* getQueuedTaskCount returns 0 when quiescent |
1450 |
|
*/ |
1451 |
|
public void testForkHelpQuiesceSingleton() { |
1452 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1453 |
< |
public void realCompute() { |
1454 |
< |
CCF f = new LCCF(null, 8); |
1452 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1453 |
> |
protected void realCompute() { |
1454 |
> |
CCF f = new LCCF(8); |
1455 |
|
assertSame(f, f.fork()); |
1456 |
|
helpQuiesce(); |
1457 |
|
assertEquals(0, getQueuedTaskCount()); |
1465 |
|
* invoke task throws exception when task completes abnormally |
1466 |
|
*/ |
1467 |
|
public void testAbnormalInvokeSingleton() { |
1468 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1469 |
< |
public void realCompute() { |
1470 |
< |
FailingCCF f = new LFCCF(null, 8); |
1468 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1469 |
> |
protected void realCompute() { |
1470 |
> |
FailingCCF f = new LFCCF(8); |
1471 |
|
try { |
1472 |
|
f.invoke(); |
1473 |
|
shouldThrow(); |
1482 |
|
* quietlyInvoke task returns when task completes abnormally |
1483 |
|
*/ |
1484 |
|
public void testAbnormalQuietlyInvokeSingleton() { |
1485 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1486 |
< |
public void realCompute() { |
1487 |
< |
FailingCCF f = new LFCCF(null, 8); |
1485 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1486 |
> |
protected void realCompute() { |
1487 |
> |
FailingCCF f = new LFCCF(8); |
1488 |
|
f.quietlyInvoke(); |
1489 |
|
assertTrue(f.getException() instanceof FJException); |
1490 |
|
checkCompletedAbnormally(f, f.getException()); |
1496 |
|
* join of a forked task throws exception when task completes abnormally |
1497 |
|
*/ |
1498 |
|
public void testAbnormalForkJoinSingleton() { |
1499 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1500 |
< |
public void realCompute() { |
1501 |
< |
FailingCCF f = new LFCCF(null, 8); |
1499 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1500 |
> |
protected void realCompute() { |
1501 |
> |
FailingCCF f = new LFCCF(8); |
1502 |
|
assertSame(f, f.fork()); |
1503 |
|
try { |
1504 |
|
f.join(); |
1514 |
|
* get of a forked task throws exception when task completes abnormally |
1515 |
|
*/ |
1516 |
|
public void testAbnormalForkGetSingleton() { |
1517 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1518 |
< |
public void realCompute() throws Exception { |
1519 |
< |
FailingCCF f = new LFCCF(null, 8); |
1517 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1518 |
> |
protected void realCompute() throws Exception { |
1519 |
> |
FailingCCF f = new LFCCF(8); |
1520 |
|
assertSame(f, f.fork()); |
1521 |
|
try { |
1522 |
|
f.get(); |
1534 |
|
* timed get of a forked task throws exception when task completes abnormally |
1535 |
|
*/ |
1536 |
|
public void testAbnormalForkTimedGetSingleton() { |
1537 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1538 |
< |
public void realCompute() throws Exception { |
1539 |
< |
FailingCCF f = new LFCCF(null, 8); |
1537 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1538 |
> |
protected void realCompute() throws Exception { |
1539 |
> |
FailingCCF f = new LFCCF(8); |
1540 |
|
assertSame(f, f.fork()); |
1541 |
|
try { |
1542 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
1554 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
1555 |
|
*/ |
1556 |
|
public void testAbnormalForkQuietlyJoinSingleton() { |
1557 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1558 |
< |
public void realCompute() { |
1559 |
< |
FailingCCF f = new LFCCF(null, 8); |
1557 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1558 |
> |
protected void realCompute() { |
1559 |
> |
FailingCCF f = new LFCCF(8); |
1560 |
|
assertSame(f, f.fork()); |
1561 |
|
f.quietlyJoin(); |
1562 |
|
assertTrue(f.getException() instanceof FJException); |
1569 |
|
* invoke task throws exception when task cancelled |
1570 |
|
*/ |
1571 |
|
public void testCancelledInvokeSingleton() { |
1572 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1573 |
< |
public void realCompute() { |
1574 |
< |
CCF f = new LCCF(null, 8); |
1572 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1573 |
> |
protected void realCompute() { |
1574 |
> |
CCF f = new LCCF(8); |
1575 |
|
assertTrue(f.cancel(true)); |
1576 |
|
try { |
1577 |
|
f.invoke(); |
1587 |
|
* join of a forked task throws exception when task cancelled |
1588 |
|
*/ |
1589 |
|
public void testCancelledForkJoinSingleton() { |
1590 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1591 |
< |
public void realCompute() { |
1592 |
< |
CCF f = new LCCF(null, 8); |
1590 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1591 |
> |
protected void realCompute() { |
1592 |
> |
CCF f = new LCCF(8); |
1593 |
|
assertTrue(f.cancel(true)); |
1594 |
|
assertSame(f, f.fork()); |
1595 |
|
try { |
1606 |
|
* get of a forked task throws exception when task cancelled |
1607 |
|
*/ |
1608 |
|
public void testCancelledForkGetSingleton() { |
1609 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1610 |
< |
public void realCompute() throws Exception { |
1611 |
< |
CCF f = new LCCF(null, 8); |
1609 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1610 |
> |
protected void realCompute() throws Exception { |
1611 |
> |
CCF f = new LCCF(8); |
1612 |
|
assertTrue(f.cancel(true)); |
1613 |
|
assertSame(f, f.fork()); |
1614 |
|
try { |
1625 |
|
* timed get of a forked task throws exception when task cancelled |
1626 |
|
*/ |
1627 |
|
public void testCancelledForkTimedGetSingleton() throws Exception { |
1628 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1629 |
< |
public void realCompute() throws Exception { |
1630 |
< |
CCF f = new LCCF(null, 8); |
1628 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1629 |
> |
protected void realCompute() throws Exception { |
1630 |
> |
CCF f = new LCCF(8); |
1631 |
|
assertTrue(f.cancel(true)); |
1632 |
|
assertSame(f, f.fork()); |
1633 |
|
try { |
1644 |
|
* quietlyJoin of a forked task returns when task cancelled |
1645 |
|
*/ |
1646 |
|
public void testCancelledForkQuietlyJoinSingleton() { |
1647 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1648 |
< |
public void realCompute() { |
1649 |
< |
CCF f = new LCCF(null, 8); |
1647 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1648 |
> |
protected void realCompute() { |
1649 |
> |
CCF f = new LCCF(8); |
1650 |
|
assertTrue(f.cancel(true)); |
1651 |
|
assertSame(f, f.fork()); |
1652 |
|
f.quietlyJoin(); |
1659 |
|
* invoke task throws exception after invoking completeExceptionally |
1660 |
|
*/ |
1661 |
|
public void testCompleteExceptionallySingleton() { |
1662 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1663 |
< |
public void realCompute() { |
1664 |
< |
CCF f = new LCCF(null, 8); |
1665 |
< |
f.completeExceptionally(new FJException()); |
1666 |
< |
try { |
1667 |
< |
f.invoke(); |
1668 |
< |
shouldThrow(); |
1669 |
< |
} catch (FJException success) { |
1570 |
< |
checkCompletedAbnormally(f, success); |
1571 |
< |
} |
1662 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1663 |
> |
protected void realCompute() { |
1664 |
> |
CCF n = new LCCF(8); |
1665 |
> |
CCF f = new LCCF(n, 8); |
1666 |
> |
FJException ex = new FJException(); |
1667 |
> |
f.completeExceptionally(ex); |
1668 |
> |
f.checkCompletedExceptionally(ex); |
1669 |
> |
n.checkCompletedExceptionally(ex); |
1670 |
|
}}; |
1671 |
|
testInvokeOnPool(singletonPool(), a); |
1672 |
|
} |
1675 |
|
* invokeAll(t1, t2) invokes all task arguments |
1676 |
|
*/ |
1677 |
|
public void testInvokeAll2Singleton() { |
1678 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1679 |
< |
public void realCompute() { |
1680 |
< |
CCF f = new LCCF(null, 8); |
1681 |
< |
CCF g = new LCCF(null, 9); |
1678 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1679 |
> |
protected void realCompute() { |
1680 |
> |
CCF f = new LCCF(8); |
1681 |
> |
CCF g = new LCCF(9); |
1682 |
|
invokeAll(f, g); |
1683 |
|
assertEquals(21, f.number); |
1684 |
|
assertEquals(34, g.number); |
1692 |
|
* invokeAll(tasks) with 1 argument invokes task |
1693 |
|
*/ |
1694 |
|
public void testInvokeAll1Singleton() { |
1695 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1696 |
< |
public void realCompute() { |
1697 |
< |
CCF f = new LCCF(null, 8); |
1695 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1696 |
> |
protected void realCompute() { |
1697 |
> |
CCF f = new LCCF(8); |
1698 |
|
invokeAll(f); |
1699 |
|
checkCompletedNormally(f); |
1700 |
|
assertEquals(21, f.number); |
1706 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
1707 |
|
*/ |
1708 |
|
public void testInvokeAll3Singleton() { |
1709 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1710 |
< |
public void realCompute() { |
1711 |
< |
CCF f = new LCCF(null, 8); |
1712 |
< |
CCF g = new LCCF(null, 9); |
1713 |
< |
CCF h = new LCCF(null, 7); |
1709 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1710 |
> |
protected void realCompute() { |
1711 |
> |
CCF f = new LCCF(8); |
1712 |
> |
CCF g = new LCCF(9); |
1713 |
> |
CCF h = new LCCF(7); |
1714 |
|
invokeAll(f, g, h); |
1715 |
|
assertEquals(21, f.number); |
1716 |
|
assertEquals(34, g.number); |
1726 |
|
* invokeAll(collection) invokes all tasks in the collection |
1727 |
|
*/ |
1728 |
|
public void testInvokeAllCollectionSingleton() { |
1729 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1730 |
< |
public void realCompute() { |
1731 |
< |
CCF f = new LCCF(null, 8); |
1732 |
< |
CCF g = new LCCF(null, 9); |
1733 |
< |
CCF h = new LCCF(null, 7); |
1729 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1730 |
> |
protected void realCompute() { |
1731 |
> |
CCF f = new LCCF(8); |
1732 |
> |
CCF g = new LCCF(9); |
1733 |
> |
CCF h = new LCCF(7); |
1734 |
|
HashSet set = new HashSet(); |
1735 |
|
set.add(f); |
1736 |
|
set.add(g); |
1750 |
|
* invokeAll(tasks) with any null task throws NPE |
1751 |
|
*/ |
1752 |
|
public void testInvokeAllNPESingleton() { |
1753 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1754 |
< |
public void realCompute() { |
1755 |
< |
CCF f = new LCCF(null, 8); |
1756 |
< |
CCF g = new LCCF(null, 9); |
1753 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1754 |
> |
protected void realCompute() { |
1755 |
> |
CCF f = new LCCF(8); |
1756 |
> |
CCF g = new LCCF(9); |
1757 |
|
CCF h = null; |
1758 |
|
try { |
1759 |
|
invokeAll(f, g, h); |
1767 |
|
* invokeAll(t1, t2) throw exception if any task does |
1768 |
|
*/ |
1769 |
|
public void testAbnormalInvokeAll2Singleton() { |
1770 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1771 |
< |
public void realCompute() { |
1772 |
< |
CCF f = new LCCF(null, 8); |
1773 |
< |
FailingCCF g = new LFCCF(null, 9); |
1770 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1771 |
> |
protected void realCompute() { |
1772 |
> |
CCF f = new LCCF(8); |
1773 |
> |
FailingCCF g = new LFCCF(9); |
1774 |
|
try { |
1775 |
|
invokeAll(f, g); |
1776 |
|
shouldThrow(); |
1785 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
1786 |
|
*/ |
1787 |
|
public void testAbnormalInvokeAll1Singleton() { |
1788 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1789 |
< |
public void realCompute() { |
1790 |
< |
FailingCCF g = new LFCCF(null, 9); |
1788 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1789 |
> |
protected void realCompute() { |
1790 |
> |
FailingCCF g = new LFCCF(9); |
1791 |
|
try { |
1792 |
|
invokeAll(g); |
1793 |
|
shouldThrow(); |
1802 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1803 |
|
*/ |
1804 |
|
public void testAbnormalInvokeAll3Singleton() { |
1805 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1806 |
< |
public void realCompute() { |
1807 |
< |
CCF f = new LCCF(null, 8); |
1808 |
< |
FailingCCF g = new LFCCF(null, 9); |
1809 |
< |
CCF h = new LCCF(null, 7); |
1805 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1806 |
> |
protected void realCompute() { |
1807 |
> |
CCF f = new LCCF(8); |
1808 |
> |
FailingCCF g = new LFCCF(9); |
1809 |
> |
CCF h = new LCCF(7); |
1810 |
|
try { |
1811 |
|
invokeAll(f, g, h); |
1812 |
|
shouldThrow(); |
1818 |
|
} |
1819 |
|
|
1820 |
|
/** |
1821 |
< |
* invokeAll(collection) throws exception if any task does |
1821 |
> |
* invokeAll(collection) throws exception if any task does |
1822 |
|
*/ |
1823 |
|
public void testAbnormalInvokeAllCollectionSingleton() { |
1824 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1825 |
< |
public void realCompute() { |
1826 |
< |
FailingCCF f = new LFCCF(null, 8); |
1827 |
< |
CCF g = new LCCF(null, 9); |
1828 |
< |
CCF h = new LCCF(null, 7); |
1824 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1825 |
> |
protected void realCompute() { |
1826 |
> |
FailingCCF f = new LFCCF(8); |
1827 |
> |
CCF g = new LCCF(9); |
1828 |
> |
CCF h = new LCCF(7); |
1829 |
|
HashSet set = new HashSet(); |
1830 |
|
set.add(f); |
1831 |
|
set.add(g); |