ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.39
Committed: Wed Jan 27 01:57:24 2021 UTC (3 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.38: +4 -4 lines
Log Message:
use diamond <> pervasively

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/publicdomain/zero/1.0/
5     */
6 jsr166 1.16
7     import static java.util.concurrent.TimeUnit.MILLISECONDS;
8    
9 jsr166 1.15 import java.util.HashSet;
10     import java.util.concurrent.CancellationException;
11     import java.util.concurrent.CountedCompleter;
12 dl 1.1 import java.util.concurrent.ExecutionException;
13     import java.util.concurrent.ForkJoinPool;
14     import java.util.concurrent.ForkJoinTask;
15     import java.util.concurrent.TimeoutException;
16 jsr166 1.8 import java.util.concurrent.atomic.AtomicInteger;
17     import java.util.concurrent.atomic.AtomicReference;
18 jsr166 1.16
19     import junit.framework.Test;
20     import junit.framework.TestSuite;
21 dl 1.1
22     public class CountedCompleterTest extends JSR166TestCase {
23    
24     public static void main(String[] args) {
25 jsr166 1.18 main(suite(), args);
26 dl 1.1 }
27    
28     public static Test suite() {
29     return new TestSuite(CountedCompleterTest.class);
30     }
31    
32     // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
33     static final int mainPoolSize =
34     Math.max(2, Runtime.getRuntime().availableProcessors());
35    
36     private static ForkJoinPool mainPool() {
37     return new ForkJoinPool(mainPoolSize);
38     }
39    
40     private static ForkJoinPool singletonPool() {
41     return new ForkJoinPool(1);
42     }
43    
44     private static ForkJoinPool asyncSingletonPool() {
45     return new ForkJoinPool(1,
46     ForkJoinPool.defaultForkJoinWorkerThreadFactory,
47     null, true);
48     }
49    
50 dl 1.38 private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask<?> a) {
51 jsr166 1.19 try (PoolCleaner cleaner = cleaner(pool)) {
52 dl 1.1 assertFalse(a.isDone());
53     assertFalse(a.isCompletedNormally());
54     assertFalse(a.isCompletedAbnormally());
55     assertFalse(a.isCancelled());
56     assertNull(a.getException());
57     assertNull(a.getRawResult());
58    
59     assertNull(pool.invoke(a));
60    
61     assertTrue(a.isDone());
62     assertTrue(a.isCompletedNormally());
63     assertFalse(a.isCompletedAbnormally());
64     assertFalse(a.isCancelled());
65     assertNull(a.getException());
66     assertNull(a.getRawResult());
67     }
68     }
69    
70 dl 1.38 void checkNotDone(CountedCompleter<?> a) {
71 dl 1.1 assertFalse(a.isDone());
72     assertFalse(a.isCompletedNormally());
73     assertFalse(a.isCompletedAbnormally());
74     assertFalse(a.isCancelled());
75     assertNull(a.getException());
76     assertNull(a.getRawResult());
77    
78     try {
79 jsr166 1.33 a.get(randomExpiredTimeout(), randomTimeUnit());
80 dl 1.1 shouldThrow();
81     } catch (TimeoutException success) {
82     } catch (Throwable fail) { threadUnexpectedException(fail); }
83     }
84    
85 jsr166 1.8 void checkCompletedNormally(CountedCompleter<?> a) {
86 dl 1.1 assertTrue(a.isDone());
87     assertFalse(a.isCancelled());
88     assertTrue(a.isCompletedNormally());
89     assertFalse(a.isCompletedAbnormally());
90     assertNull(a.getException());
91 jsr166 1.8 assertNull(a.getRawResult());
92 dl 1.1
93     {
94     Thread.currentThread().interrupt();
95 jsr166 1.20 long startTime = System.nanoTime();
96 jsr166 1.8 assertNull(a.join());
97 jsr166 1.33 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
98 dl 1.1 Thread.interrupted();
99     }
100    
101     {
102     Thread.currentThread().interrupt();
103 jsr166 1.20 long startTime = System.nanoTime();
104 dl 1.1 a.quietlyJoin(); // should be no-op
105 jsr166 1.33 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
106 dl 1.1 Thread.interrupted();
107     }
108    
109     assertFalse(a.cancel(false));
110     assertFalse(a.cancel(true));
111 jsr166 1.37
112     Object v1 = null, v2 = null;
113 dl 1.1 try {
114 jsr166 1.37 v1 = a.get();
115     v2 = a.get(randomTimeout(), randomTimeUnit());
116 jsr166 1.36 } catch (Throwable fail) { threadUnexpectedException(fail); }
117 jsr166 1.37 assertNull(v1);
118     assertNull(v2);
119 dl 1.1 }
120    
121 dl 1.38 void checkCancelled(CountedCompleter<?> a) {
122 dl 1.1 assertTrue(a.isDone());
123     assertTrue(a.isCancelled());
124     assertFalse(a.isCompletedNormally());
125     assertTrue(a.isCompletedAbnormally());
126     assertTrue(a.getException() instanceof CancellationException);
127     assertNull(a.getRawResult());
128     assertTrue(a.cancel(false));
129     assertTrue(a.cancel(true));
130    
131     try {
132     Thread.currentThread().interrupt();
133     a.join();
134     shouldThrow();
135     } catch (CancellationException success) {
136     } catch (Throwable fail) { threadUnexpectedException(fail); }
137     Thread.interrupted();
138    
139     {
140 jsr166 1.20 long startTime = System.nanoTime();
141 dl 1.1 a.quietlyJoin(); // should be no-op
142 jsr166 1.33 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
143 dl 1.1 }
144    
145     try {
146     a.get();
147     shouldThrow();
148     } catch (CancellationException success) {
149     } catch (Throwable fail) { threadUnexpectedException(fail); }
150    
151     try {
152 jsr166 1.33 a.get(randomTimeout(), randomTimeUnit());
153 dl 1.1 shouldThrow();
154     } catch (CancellationException success) {
155     } catch (Throwable fail) { threadUnexpectedException(fail); }
156     }
157    
158 dl 1.38 void checkCompletedAbnormally(CountedCompleter<?> a, Throwable t) {
159 dl 1.1 assertTrue(a.isDone());
160     assertFalse(a.isCancelled());
161     assertFalse(a.isCompletedNormally());
162     assertTrue(a.isCompletedAbnormally());
163     assertSame(t.getClass(), a.getException().getClass());
164     assertNull(a.getRawResult());
165     assertFalse(a.cancel(false));
166     assertFalse(a.cancel(true));
167    
168     try {
169     Thread.currentThread().interrupt();
170     a.join();
171     shouldThrow();
172     } catch (Throwable expected) {
173     assertSame(t.getClass(), expected.getClass());
174     }
175     Thread.interrupted();
176    
177     {
178 jsr166 1.20 long startTime = System.nanoTime();
179 dl 1.1 a.quietlyJoin(); // should be no-op
180 jsr166 1.33 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
181 dl 1.1 }
182    
183     try {
184     a.get();
185     shouldThrow();
186     } catch (ExecutionException success) {
187     assertSame(t.getClass(), success.getCause().getClass());
188     } catch (Throwable fail) { threadUnexpectedException(fail); }
189    
190     try {
191 jsr166 1.33 a.get(randomTimeout(), randomTimeUnit());
192 dl 1.1 shouldThrow();
193     } catch (ExecutionException success) {
194     assertSame(t.getClass(), success.getCause().getClass());
195     } catch (Throwable fail) { threadUnexpectedException(fail); }
196 jsr166 1.8
197     try {
198     a.invoke();
199     shouldThrow();
200 jsr166 1.17 } catch (Throwable success) {
201     assertSame(t, success);
202 jsr166 1.8 }
203 dl 1.1 }
204    
205     public static final class FJException extends RuntimeException {
206     FJException() { super(); }
207     }
208    
209 jsr166 1.8 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 jsr166 1.31 final AtomicReference<Object> rawResult = new AtomicReference<>(null);
215 jsr166 1.8 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 dl 1.38 CheckedCC(CountedCompleter<?> p) { super(p); }
222     CheckedCC(CountedCompleter<?> p, int n) { super(p, n); }
223 jsr166 1.8 abstract void realCompute();
224     public final void compute() {
225     computeN.incrementAndGet();
226     realCompute();
227     }
228 dl 1.38 public void onCompletion(CountedCompleter<?> caller) {
229 jsr166 1.8 onCompletionN.incrementAndGet();
230     super.onCompletion(caller);
231     }
232     public boolean onExceptionalCompletion(Throwable ex,
233 dl 1.38 CountedCompleter<?> caller) {
234 jsr166 1.8 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 jsr166 1.10 int pendingCount = getPendingCount();
255 jsr166 1.8 complete(rawResult);
256 jsr166 1.10 assertEquals(pendingCount, getPendingCount());
257 jsr166 1.8 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 dl 1.1 }
277     }
278    
279 jsr166 1.8 final class NoopCC extends CheckedCC {
280     NoopCC() { super(); }
281 dl 1.38 NoopCC(CountedCompleter<?> p) { super(p); }
282     NoopCC(CountedCompleter<?> p, int initialPendingCount) {
283 jsr166 1.21 super(p, initialPendingCount);
284     }
285 jsr166 1.8 protected void realCompute() {}
286     }
287    
288 dl 1.1 /**
289 jsr166 1.3 * A newly constructed CountedCompleter is not completed;
290 jsr166 1.10 * complete() causes completion. pendingCount is ignored.
291 dl 1.1 */
292     public void testComplete() {
293 jsr166 1.8 for (Object x : new Object[] { Boolean.TRUE, null }) {
294 jsr166 1.10 for (int pendingCount : new int[] { 0, 42 }) {
295     testComplete(new NoopCC(), x, pendingCount);
296     testComplete(new NoopCC(new NoopCC()), x, pendingCount);
297     }
298 jsr166 1.8 }
299 dl 1.1 }
300 jsr166 1.10 void testComplete(NoopCC cc, Object x, int pendingCount) {
301     cc.setPendingCount(pendingCount);
302     cc.checkCompletes(x);
303 jsr166 1.22 assertEquals(pendingCount, cc.getPendingCount());
304 jsr166 1.10 }
305 dl 1.1
306     /**
307     * completeExceptionally completes exceptionally
308     */
309     public void testCompleteExceptionally() {
310 jsr166 1.8 new NoopCC()
311     .checkCompletesExceptionally(new FJException());
312     new NoopCC(new NoopCC())
313     .checkCompletesExceptionally(new FJException());
314     }
315    
316     /**
317 jsr166 1.21 * completeExceptionally(null) surprisingly has the same effect as
318     * completeExceptionally(new RuntimeException())
319 jsr166 1.8 */
320     public void testCompleteExceptionally_null() {
321 jsr166 1.21 NoopCC a = new NoopCC();
322     a.completeExceptionally(null);
323 jsr166 1.8 try {
324 jsr166 1.21 a.invoke();
325 jsr166 1.8 shouldThrow();
326 jsr166 1.21 } catch (RuntimeException success) {
327     assertSame(success.getClass(), RuntimeException.class);
328     assertNull(success.getCause());
329     a.checkCompletedExceptionally(success);
330     }
331 dl 1.1 }
332    
333     /**
334     * setPendingCount sets the reported pending count
335     */
336     public void testSetPendingCount() {
337 jsr166 1.8 NoopCC a = new NoopCC();
338 dl 1.1 assertEquals(0, a.getPendingCount());
339 jsr166 1.25 int[] vals = {
340     -1, 0, 1,
341     Integer.MIN_VALUE,
342     Integer.MAX_VALUE,
343     };
344     for (int val : vals) {
345 jsr166 1.24 a.setPendingCount(val);
346     assertEquals(val, a.getPendingCount());
347     }
348 dl 1.1 }
349    
350     /**
351     * addToPendingCount adds to the reported pending count
352     */
353     public void testAddToPendingCount() {
354 jsr166 1.8 NoopCC a = new NoopCC();
355 dl 1.1 assertEquals(0, a.getPendingCount());
356     a.addToPendingCount(1);
357     assertEquals(1, a.getPendingCount());
358     a.addToPendingCount(27);
359     assertEquals(28, a.getPendingCount());
360 jsr166 1.26 a.addToPendingCount(-28);
361     assertEquals(0, a.getPendingCount());
362 dl 1.1 }
363    
364     /**
365     * decrementPendingCountUnlessZero decrements reported pending
366     * count unless zero
367     */
368 jsr166 1.21 public void testDecrementPendingCountUnlessZero() {
369     NoopCC a = new NoopCC(null, 2);
370     assertEquals(2, a.getPendingCount());
371     assertEquals(2, a.decrementPendingCountUnlessZero());
372 dl 1.1 assertEquals(1, a.getPendingCount());
373 jsr166 1.21 assertEquals(1, a.decrementPendingCountUnlessZero());
374 dl 1.1 assertEquals(0, a.getPendingCount());
375 jsr166 1.21 assertEquals(0, a.decrementPendingCountUnlessZero());
376 dl 1.1 assertEquals(0, a.getPendingCount());
377 jsr166 1.23 a.setPendingCount(-1);
378     assertEquals(-1, a.decrementPendingCountUnlessZero());
379     assertEquals(-2, a.getPendingCount());
380 dl 1.1 }
381    
382     /**
383 jsr166 1.8 * 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 dl 1.1 * getCompleter returns parent or null if at root
399     */
400     public void testGetCompleter() {
401 jsr166 1.8 NoopCC a = new NoopCC();
402 dl 1.1 assertNull(a.getCompleter());
403 dl 1.38 NoopCC b = new NoopCC(a);
404 jsr166 1.8 assertSame(a, b.getCompleter());
405 dl 1.38 NoopCC c = new NoopCC(b);
406 jsr166 1.8 assertSame(b, c.getCompleter());
407 dl 1.1 }
408 jsr166 1.3
409 dl 1.1 /**
410     * getRoot returns self if no parent, else parent's root
411     */
412     public void testGetRoot() {
413 jsr166 1.8 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 dl 1.1 }
420 jsr166 1.3
421 dl 1.1 /**
422 jsr166 1.8 * tryComplete decrements pending count unless zero, in which case
423     * causes completion
424 dl 1.1 */
425 jsr166 1.8 public void testTryComplete() {
426     NoopCC a = new NoopCC();
427 dl 1.1 assertEquals(0, a.getPendingCount());
428 jsr166 1.8 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 dl 1.1 a.tryComplete();
437 jsr166 1.8 assertEquals(0, a.computeN());
438     assertEquals(1, a.onCompletionN());
439     assertEquals(0, a.onExceptionalCompletionN());
440     assertEquals(0, a.setRawResultN());
441     checkCompletedNormally(a);
442 dl 1.1 }
443    
444     /**
445 jsr166 1.8 * propagateCompletion decrements pending count unless zero, in
446     * which case causes completion, without invoking onCompletion
447 dl 1.1 */
448     public void testPropagateCompletion() {
449 jsr166 1.8 NoopCC a = new NoopCC();
450 dl 1.1 assertEquals(0, a.getPendingCount());
451 jsr166 1.8 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 dl 1.1 a.propagateCompletion();
460 jsr166 1.8 assertEquals(0, a.computeN());
461     assertEquals(0, a.onCompletionN());
462     assertEquals(0, a.onExceptionalCompletionN());
463     assertEquals(0, a.setRawResultN());
464     checkCompletedNormally(a);
465 dl 1.1 }
466    
467     /**
468     * firstComplete returns this if pending count is zero else null
469     */
470     public void testFirstComplete() {
471 jsr166 1.8 NoopCC a = new NoopCC();
472 dl 1.1 a.setPendingCount(1);
473     assertNull(a.firstComplete());
474 jsr166 1.9 a.checkIncomplete();
475     assertSame(a, a.firstComplete());
476     a.checkIncomplete();
477 dl 1.1 }
478    
479     /**
480     * firstComplete.nextComplete returns parent if pending count is
481     * zero else null
482     */
483     public void testNextComplete() {
484 jsr166 1.8 NoopCC a = new NoopCC();
485     NoopCC b = new NoopCC(a);
486 dl 1.1 a.setPendingCount(1);
487     b.setPendingCount(1);
488     assertNull(b.firstComplete());
489 jsr166 1.9 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 dl 1.1 }
501    
502     /**
503 jsr166 1.27 * quietlyCompleteRoot completes root task and only root task
504 dl 1.1 */
505     public void testQuietlyCompleteRoot() {
506 jsr166 1.8 NoopCC a = new NoopCC();
507     NoopCC b = new NoopCC(a);
508     NoopCC c = new NoopCC(b);
509 dl 1.1 a.setPendingCount(1);
510     b.setPendingCount(1);
511 jsr166 1.8 c.setPendingCount(1);
512     c.quietlyCompleteRoot();
513 dl 1.1 assertTrue(a.isDone());
514     assertFalse(b.isDone());
515 jsr166 1.8 assertFalse(c.isDone());
516 dl 1.1 }
517 dl 1.2
518     // Invocation tests use some interdependent task classes
519     // to better test propagation etc
520    
521 jsr166 1.11 /**
522     * Version of Fibonacci with different classes for left vs right forks
523     */
524 jsr166 1.8 abstract class CCF extends CheckedCC {
525 dl 1.2 int number;
526     int rnumber;
527    
528 dl 1.38 public CCF(CountedCompleter<?> parent, int n) {
529 dl 1.2 super(parent, 1);
530     this.number = n;
531     }
532    
533 jsr166 1.8 protected final void realCompute() {
534 dl 1.2 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 jsr166 1.8 f.complete(null);
541 dl 1.2 }
542     }
543    
544 jsr166 1.8 final class LCCF extends CCF {
545     public LCCF(int n) { this(null, n); }
546 dl 1.38 public LCCF(CountedCompleter<?> parent, int n) {
547 dl 1.2 super(parent, n);
548     }
549 dl 1.38 public final void onCompletion(CountedCompleter<?> caller) {
550 jsr166 1.8 super.onCompletion(caller);
551 dl 1.2 CCF p = (CCF)getCompleter();
552     int n = number + rnumber;
553     if (p != null)
554     p.number = n;
555     else
556     number = n;
557     }
558     }
559 jsr166 1.8 final class RCCF extends CCF {
560 dl 1.38 public RCCF(CountedCompleter<?> parent, int n) {
561 dl 1.2 super(parent, n);
562     }
563 dl 1.38 public final void onCompletion(CountedCompleter<?> caller) {
564 jsr166 1.8 super.onCompletion(caller);
565 dl 1.2 CCF p = (CCF)getCompleter();
566     int n = number + rnumber;
567     if (p != null)
568     p.rnumber = n;
569     else
570     number = n;
571     }
572     }
573    
574     // Version of CCF with forced failure in left completions
575 jsr166 1.8 abstract class FailingCCF extends CheckedCC {
576 dl 1.2 int number;
577     int rnumber;
578    
579 dl 1.38 public FailingCCF(CountedCompleter<?> parent, int n) {
580 dl 1.2 super(parent, 1);
581     this.number = n;
582     }
583    
584 jsr166 1.8 protected final void realCompute() {
585 dl 1.2 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 jsr166 1.8 f.complete(null);
592 dl 1.2 }
593     }
594    
595 jsr166 1.8 final class LFCCF extends FailingCCF {
596     public LFCCF(int n) { this(null, n); }
597 dl 1.38 public LFCCF(CountedCompleter<?> parent, int n) {
598 dl 1.2 super(parent, n);
599     }
600 dl 1.38 public final void onCompletion(CountedCompleter<?> caller) {
601 jsr166 1.8 super.onCompletion(caller);
602 dl 1.2 FailingCCF p = (FailingCCF)getCompleter();
603     int n = number + rnumber;
604     if (p != null)
605     p.number = n;
606     else
607     number = n;
608     }
609     }
610 jsr166 1.8 final class RFCCF extends FailingCCF {
611 dl 1.38 public RFCCF(CountedCompleter<?> parent, int n) {
612 dl 1.2 super(parent, n);
613     }
614 dl 1.38 public final void onCompletion(CountedCompleter<?> caller) {
615 jsr166 1.8 super.onCompletion(caller);
616 dl 1.2 completeExceptionally(new FJException());
617     }
618     }
619 jsr166 1.3
620 dl 1.1 /**
621     * invoke returns when task completes normally.
622     * isCompletedAbnormally and isCancelled return false for normally
623     * completed tasks; getRawResult returns null.
624     */
625     public void testInvoke() {
626 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
627 jsr166 1.7 protected void realCompute() {
628 jsr166 1.8 CCF f = new LCCF(8);
629 dl 1.1 assertNull(f.invoke());
630     assertEquals(21, f.number);
631     checkCompletedNormally(f);
632     }};
633     testInvokeOnPool(mainPool(), a);
634     }
635    
636     /**
637     * quietlyInvoke task returns when task completes normally.
638     * isCompletedAbnormally and isCancelled return false for normally
639     * completed tasks
640     */
641     public void testQuietlyInvoke() {
642 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
643 jsr166 1.7 protected void realCompute() {
644 jsr166 1.8 CCF f = new LCCF(8);
645 dl 1.1 f.quietlyInvoke();
646     assertEquals(21, f.number);
647     checkCompletedNormally(f);
648     }};
649     testInvokeOnPool(mainPool(), a);
650     }
651    
652     /**
653     * join of a forked task returns when task completes
654     */
655     public void testForkJoin() {
656 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
657 jsr166 1.7 protected void realCompute() {
658 jsr166 1.8 CCF f = new LCCF(8);
659 dl 1.1 assertSame(f, f.fork());
660     assertNull(f.join());
661     assertEquals(21, f.number);
662     checkCompletedNormally(f);
663     }};
664     testInvokeOnPool(mainPool(), a);
665     }
666    
667     /**
668     * get of a forked task returns when task completes
669     */
670     public void testForkGet() {
671 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
672 jsr166 1.7 protected void realCompute() throws Exception {
673 jsr166 1.8 CCF f = new LCCF(8);
674 dl 1.1 assertSame(f, f.fork());
675     assertNull(f.get());
676     assertEquals(21, f.number);
677     checkCompletedNormally(f);
678     }};
679     testInvokeOnPool(mainPool(), a);
680     }
681    
682     /**
683     * timed get of a forked task returns when task completes
684     */
685     public void testForkTimedGet() {
686 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
687 jsr166 1.7 protected void realCompute() throws Exception {
688 jsr166 1.8 CCF f = new LCCF(8);
689 dl 1.1 assertSame(f, f.fork());
690     assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
691     assertEquals(21, f.number);
692     checkCompletedNormally(f);
693     }};
694     testInvokeOnPool(mainPool(), a);
695     }
696    
697     /**
698     * timed get with null time unit throws NPE
699     */
700     public void testForkTimedGetNPE() {
701 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
702 jsr166 1.7 protected void realCompute() throws Exception {
703 jsr166 1.8 CCF f = new LCCF(8);
704 dl 1.1 assertSame(f, f.fork());
705     try {
706 jsr166 1.33 f.get(randomTimeout(), null);
707 dl 1.1 shouldThrow();
708     } catch (NullPointerException success) {}
709     }};
710     testInvokeOnPool(mainPool(), a);
711     }
712    
713     /**
714     * quietlyJoin of a forked task returns when task completes
715     */
716     public void testForkQuietlyJoin() {
717 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
718 jsr166 1.7 protected void realCompute() {
719 jsr166 1.8 CCF f = new LCCF(8);
720 dl 1.1 assertSame(f, f.fork());
721     f.quietlyJoin();
722     assertEquals(21, f.number);
723     checkCompletedNormally(f);
724     }};
725     testInvokeOnPool(mainPool(), a);
726     }
727    
728     /**
729     * helpQuiesce returns when tasks are complete.
730     * getQueuedTaskCount returns 0 when quiescent
731     */
732     public void testForkHelpQuiesce() {
733 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
734 jsr166 1.7 protected void realCompute() {
735 jsr166 1.8 CCF f = new LCCF(8);
736 dl 1.1 assertSame(f, f.fork());
737     helpQuiesce();
738 dl 1.35 while (!f.isDone()) // wait out race
739     ;
740 dl 1.1 assertEquals(21, f.number);
741     assertEquals(0, getQueuedTaskCount());
742     checkCompletedNormally(f);
743     }};
744     testInvokeOnPool(mainPool(), a);
745     }
746    
747     /**
748     * invoke task throws exception when task completes abnormally
749     */
750     public void testAbnormalInvoke() {
751 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
752 jsr166 1.7 protected void realCompute() {
753 jsr166 1.8 FailingCCF f = new LFCCF(8);
754 dl 1.1 try {
755     f.invoke();
756     shouldThrow();
757     } catch (FJException success) {
758     checkCompletedAbnormally(f, success);
759     }
760     }};
761     testInvokeOnPool(mainPool(), a);
762     }
763    
764     /**
765     * quietlyInvoke task returns when task completes abnormally
766     */
767     public void testAbnormalQuietlyInvoke() {
768 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
769 jsr166 1.7 protected void realCompute() {
770 jsr166 1.8 FailingCCF f = new LFCCF(8);
771 dl 1.1 f.quietlyInvoke();
772     assertTrue(f.getException() instanceof FJException);
773     checkCompletedAbnormally(f, f.getException());
774     }};
775     testInvokeOnPool(mainPool(), a);
776     }
777    
778     /**
779     * join of a forked task throws exception when task completes abnormally
780     */
781     public void testAbnormalForkJoin() {
782 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
783 jsr166 1.7 protected void realCompute() {
784 jsr166 1.8 FailingCCF f = new LFCCF(8);
785 dl 1.1 assertSame(f, f.fork());
786     try {
787     f.join();
788     shouldThrow();
789     } catch (FJException success) {
790     checkCompletedAbnormally(f, success);
791     }
792     }};
793     testInvokeOnPool(mainPool(), a);
794     }
795    
796     /**
797     * get of a forked task throws exception when task completes abnormally
798     */
799     public void testAbnormalForkGet() {
800 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
801 jsr166 1.7 protected void realCompute() throws Exception {
802 jsr166 1.8 FailingCCF f = new LFCCF(8);
803 dl 1.1 assertSame(f, f.fork());
804     try {
805     f.get();
806     shouldThrow();
807     } catch (ExecutionException success) {
808     Throwable cause = success.getCause();
809     assertTrue(cause instanceof FJException);
810     checkCompletedAbnormally(f, cause);
811     }
812     }};
813     testInvokeOnPool(mainPool(), a);
814     }
815    
816     /**
817     * timed get of a forked task throws exception when task completes abnormally
818     */
819     public void testAbnormalForkTimedGet() {
820 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
821 jsr166 1.7 protected void realCompute() throws Exception {
822 jsr166 1.8 FailingCCF f = new LFCCF(8);
823 dl 1.1 assertSame(f, f.fork());
824     try {
825     f.get(LONG_DELAY_MS, MILLISECONDS);
826     shouldThrow();
827     } catch (ExecutionException success) {
828     Throwable cause = success.getCause();
829     assertTrue(cause instanceof FJException);
830     checkCompletedAbnormally(f, cause);
831     }
832     }};
833     testInvokeOnPool(mainPool(), a);
834     }
835    
836     /**
837     * quietlyJoin of a forked task returns when task completes abnormally
838     */
839     public void testAbnormalForkQuietlyJoin() {
840 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
841 jsr166 1.7 protected void realCompute() {
842 jsr166 1.8 FailingCCF f = new LFCCF(8);
843 dl 1.1 assertSame(f, f.fork());
844     f.quietlyJoin();
845     assertTrue(f.getException() instanceof FJException);
846     checkCompletedAbnormally(f, f.getException());
847     }};
848     testInvokeOnPool(mainPool(), a);
849     }
850    
851     /**
852     * invoke task throws exception when task cancelled
853     */
854     public void testCancelledInvoke() {
855 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
856 jsr166 1.7 protected void realCompute() {
857 jsr166 1.8 CCF f = new LCCF(8);
858 dl 1.1 assertTrue(f.cancel(true));
859     try {
860     f.invoke();
861     shouldThrow();
862     } catch (CancellationException success) {
863     checkCancelled(f);
864     }
865     }};
866     testInvokeOnPool(mainPool(), a);
867     }
868    
869     /**
870     * join of a forked task throws exception when task cancelled
871     */
872     public void testCancelledForkJoin() {
873 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
874 jsr166 1.7 protected void realCompute() {
875 jsr166 1.8 CCF f = new LCCF(8);
876 dl 1.1 assertTrue(f.cancel(true));
877     assertSame(f, f.fork());
878     try {
879     f.join();
880     shouldThrow();
881     } catch (CancellationException success) {
882     checkCancelled(f);
883     }
884     }};
885     testInvokeOnPool(mainPool(), a);
886     }
887    
888     /**
889     * get of a forked task throws exception when task cancelled
890     */
891     public void testCancelledForkGet() {
892 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
893 jsr166 1.7 protected void realCompute() throws Exception {
894 jsr166 1.8 CCF f = new LCCF(8);
895 dl 1.1 assertTrue(f.cancel(true));
896     assertSame(f, f.fork());
897     try {
898     f.get();
899     shouldThrow();
900     } catch (CancellationException success) {
901     checkCancelled(f);
902     }
903     }};
904     testInvokeOnPool(mainPool(), a);
905     }
906    
907     /**
908     * timed get of a forked task throws exception when task cancelled
909     */
910     public void testCancelledForkTimedGet() throws Exception {
911 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
912 jsr166 1.7 protected void realCompute() throws Exception {
913 jsr166 1.8 CCF f = new LCCF(8);
914 dl 1.1 assertTrue(f.cancel(true));
915     assertSame(f, f.fork());
916     try {
917     f.get(LONG_DELAY_MS, MILLISECONDS);
918     shouldThrow();
919     } catch (CancellationException success) {
920     checkCancelled(f);
921     }
922     }};
923     testInvokeOnPool(mainPool(), a);
924     }
925    
926     /**
927     * quietlyJoin of a forked task returns when task cancelled
928     */
929     public void testCancelledForkQuietlyJoin() {
930 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
931 jsr166 1.7 protected void realCompute() {
932 jsr166 1.8 CCF f = new LCCF(8);
933 dl 1.1 assertTrue(f.cancel(true));
934     assertSame(f, f.fork());
935     f.quietlyJoin();
936     checkCancelled(f);
937     }};
938     testInvokeOnPool(mainPool(), a);
939     }
940    
941     /**
942     * getPool of executing task returns its pool
943     */
944     public void testGetPool() {
945     final ForkJoinPool mainPool = mainPool();
946 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
947 jsr166 1.7 protected void realCompute() {
948 dl 1.1 assertSame(mainPool, getPool());
949     }};
950     testInvokeOnPool(mainPool, a);
951     }
952    
953     /**
954     * getPool of non-FJ task returns null
955     */
956     public void testGetPool2() {
957 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
958 jsr166 1.7 protected void realCompute() {
959 dl 1.1 assertNull(getPool());
960     }};
961     assertNull(a.invoke());
962     }
963    
964     /**
965     * inForkJoinPool of executing task returns true
966     */
967     public void testInForkJoinPool() {
968 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
969 jsr166 1.7 protected void realCompute() {
970 dl 1.1 assertTrue(inForkJoinPool());
971     }};
972     testInvokeOnPool(mainPool(), a);
973     }
974    
975     /**
976     * inForkJoinPool of non-FJ task returns false
977     */
978     public void testInForkJoinPool2() {
979 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
980 jsr166 1.7 protected void realCompute() {
981 dl 1.1 assertFalse(inForkJoinPool());
982     }};
983     assertNull(a.invoke());
984     }
985    
986     /**
987     * setRawResult(null) succeeds
988     */
989     public void testSetRawResult() {
990 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
991 jsr166 1.7 protected void realCompute() {
992 dl 1.1 setRawResult(null);
993     assertNull(getRawResult());
994     }};
995     assertNull(a.invoke());
996     }
997    
998     /**
999     * invoke task throws exception after invoking completeExceptionally
1000     */
1001     public void testCompleteExceptionally2() {
1002 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1003 jsr166 1.7 protected void realCompute() {
1004 jsr166 1.8 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 dl 1.1 }};
1011     testInvokeOnPool(mainPool(), a);
1012     }
1013    
1014     /**
1015     * invokeAll(t1, t2) invokes all task arguments
1016     */
1017     public void testInvokeAll2() {
1018 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1019 jsr166 1.7 protected void realCompute() {
1020 jsr166 1.8 CCF f = new LCCF(8);
1021     CCF g = new LCCF(9);
1022 dl 1.1 invokeAll(f, g);
1023     assertEquals(21, f.number);
1024     assertEquals(34, g.number);
1025     checkCompletedNormally(f);
1026     checkCompletedNormally(g);
1027     }};
1028     testInvokeOnPool(mainPool(), a);
1029     }
1030    
1031     /**
1032     * invokeAll(tasks) with 1 argument invokes task
1033     */
1034     public void testInvokeAll1() {
1035 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1036 jsr166 1.7 protected void realCompute() {
1037 jsr166 1.8 CCF f = new LCCF(8);
1038 dl 1.1 invokeAll(f);
1039     checkCompletedNormally(f);
1040     assertEquals(21, f.number);
1041     }};
1042     testInvokeOnPool(mainPool(), a);
1043     }
1044    
1045     /**
1046     * invokeAll(tasks) with > 2 argument invokes tasks
1047     */
1048     public void testInvokeAll3() {
1049 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1050 jsr166 1.7 protected void realCompute() {
1051 jsr166 1.8 CCF f = new LCCF(8);
1052     CCF g = new LCCF(9);
1053     CCF h = new LCCF(7);
1054 dl 1.1 invokeAll(f, g, h);
1055     assertEquals(21, f.number);
1056     assertEquals(34, g.number);
1057     assertEquals(13, h.number);
1058     checkCompletedNormally(f);
1059     checkCompletedNormally(g);
1060     checkCompletedNormally(h);
1061     }};
1062     testInvokeOnPool(mainPool(), a);
1063     }
1064    
1065     /**
1066     * invokeAll(collection) invokes all tasks in the collection
1067     */
1068     public void testInvokeAllCollection() {
1069 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1070 jsr166 1.7 protected void realCompute() {
1071 jsr166 1.8 CCF f = new LCCF(8);
1072     CCF g = new LCCF(9);
1073     CCF h = new LCCF(7);
1074 jsr166 1.39 HashSet<CCF> set = new HashSet<>();
1075 dl 1.1 set.add(f);
1076     set.add(g);
1077     set.add(h);
1078     invokeAll(set);
1079     assertEquals(21, f.number);
1080     assertEquals(34, g.number);
1081     assertEquals(13, h.number);
1082     checkCompletedNormally(f);
1083     checkCompletedNormally(g);
1084     checkCompletedNormally(h);
1085     }};
1086     testInvokeOnPool(mainPool(), a);
1087     }
1088    
1089     /**
1090     * invokeAll(tasks) with any null task throws NPE
1091     */
1092     public void testInvokeAllNPE() {
1093 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1094 jsr166 1.7 protected void realCompute() {
1095 jsr166 1.8 CCF f = new LCCF(8);
1096     CCF g = new LCCF(9);
1097 dl 1.1 CCF h = null;
1098     try {
1099     invokeAll(f, g, h);
1100     shouldThrow();
1101     } catch (NullPointerException success) {}
1102     }};
1103     testInvokeOnPool(mainPool(), a);
1104     }
1105    
1106     /**
1107     * invokeAll(t1, t2) throw exception if any task does
1108     */
1109     public void testAbnormalInvokeAll2() {
1110 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1111 jsr166 1.7 protected void realCompute() {
1112 jsr166 1.8 CCF f = new LCCF(8);
1113     FailingCCF g = new LFCCF(9);
1114 dl 1.1 try {
1115     invokeAll(f, g);
1116     shouldThrow();
1117     } catch (FJException success) {
1118     checkCompletedAbnormally(g, success);
1119     }
1120     }};
1121     testInvokeOnPool(mainPool(), a);
1122     }
1123    
1124     /**
1125     * invokeAll(tasks) with 1 argument throws exception if task does
1126     */
1127     public void testAbnormalInvokeAll1() {
1128 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1129 jsr166 1.7 protected void realCompute() {
1130 jsr166 1.8 FailingCCF g = new LFCCF(9);
1131 dl 1.1 try {
1132     invokeAll(g);
1133     shouldThrow();
1134     } catch (FJException success) {
1135     checkCompletedAbnormally(g, success);
1136     }
1137     }};
1138     testInvokeOnPool(mainPool(), a);
1139     }
1140    
1141     /**
1142     * invokeAll(tasks) with > 2 argument throws exception if any task does
1143     */
1144     public void testAbnormalInvokeAll3() {
1145 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1146 jsr166 1.7 protected void realCompute() {
1147 jsr166 1.8 CCF f = new LCCF(8);
1148     FailingCCF g = new LFCCF(9);
1149     CCF h = new LCCF(7);
1150 dl 1.1 try {
1151     invokeAll(f, g, h);
1152     shouldThrow();
1153     } catch (FJException success) {
1154     checkCompletedAbnormally(g, success);
1155     }
1156     }};
1157     testInvokeOnPool(mainPool(), a);
1158     }
1159    
1160     /**
1161 jsr166 1.13 * invokeAll(collection) throws exception if any task does
1162 dl 1.1 */
1163     public void testAbnormalInvokeAllCollection() {
1164 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1165 jsr166 1.7 protected void realCompute() {
1166 jsr166 1.8 FailingCCF f = new LFCCF(8);
1167     CCF g = new LCCF(9);
1168     CCF h = new LCCF(7);
1169 jsr166 1.39 HashSet<ForkJoinTask<?>> set = new HashSet<>();
1170 dl 1.1 set.add(f);
1171     set.add(g);
1172     set.add(h);
1173     try {
1174     invokeAll(set);
1175     shouldThrow();
1176     } catch (FJException success) {
1177     checkCompletedAbnormally(f, success);
1178     }
1179     }};
1180     testInvokeOnPool(mainPool(), a);
1181     }
1182    
1183     /**
1184     * tryUnfork returns true for most recent unexecuted task,
1185     * and suppresses execution
1186     */
1187     public void testTryUnfork() {
1188 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1189 jsr166 1.7 protected void realCompute() {
1190 jsr166 1.8 CCF g = new LCCF(9);
1191 dl 1.1 assertSame(g, g.fork());
1192 jsr166 1.8 CCF f = new LCCF(8);
1193 dl 1.1 assertSame(f, f.fork());
1194     assertTrue(f.tryUnfork());
1195     helpQuiesce();
1196     checkNotDone(f);
1197     checkCompletedNormally(g);
1198     }};
1199     testInvokeOnPool(singletonPool(), a);
1200     }
1201    
1202     /**
1203     * getSurplusQueuedTaskCount returns > 0 when
1204     * there are more tasks than threads
1205     */
1206     public void testGetSurplusQueuedTaskCount() {
1207 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1208 jsr166 1.7 protected void realCompute() {
1209 jsr166 1.8 CCF h = new LCCF(7);
1210 dl 1.1 assertSame(h, h.fork());
1211 jsr166 1.8 CCF g = new LCCF(9);
1212 dl 1.1 assertSame(g, g.fork());
1213 jsr166 1.8 CCF f = new LCCF(8);
1214 dl 1.1 assertSame(f, f.fork());
1215     assertTrue(getSurplusQueuedTaskCount() > 0);
1216     helpQuiesce();
1217     assertEquals(0, getSurplusQueuedTaskCount());
1218     checkCompletedNormally(f);
1219     checkCompletedNormally(g);
1220     checkCompletedNormally(h);
1221     }};
1222     testInvokeOnPool(singletonPool(), a);
1223     }
1224    
1225     /**
1226     * peekNextLocalTask returns most recent unexecuted task.
1227     */
1228     public void testPeekNextLocalTask() {
1229 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1230 jsr166 1.7 protected void realCompute() {
1231 jsr166 1.8 CCF g = new LCCF(9);
1232 dl 1.1 assertSame(g, g.fork());
1233 jsr166 1.8 CCF f = new LCCF(8);
1234 dl 1.1 assertSame(f, f.fork());
1235     assertSame(f, peekNextLocalTask());
1236     assertNull(f.join());
1237     checkCompletedNormally(f);
1238     helpQuiesce();
1239     checkCompletedNormally(g);
1240     }};
1241     testInvokeOnPool(singletonPool(), a);
1242     }
1243    
1244     /**
1245     * pollNextLocalTask returns most recent unexecuted task without
1246     * executing it
1247     */
1248     public void testPollNextLocalTask() {
1249 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1250 jsr166 1.7 protected void realCompute() {
1251 jsr166 1.8 CCF g = new LCCF(9);
1252 dl 1.1 assertSame(g, g.fork());
1253 jsr166 1.8 CCF f = new LCCF(8);
1254 dl 1.1 assertSame(f, f.fork());
1255     assertSame(f, pollNextLocalTask());
1256     helpQuiesce();
1257     checkNotDone(f);
1258     assertEquals(34, g.number);
1259     checkCompletedNormally(g);
1260     }};
1261     testInvokeOnPool(singletonPool(), a);
1262     }
1263    
1264     /**
1265     * pollTask returns an unexecuted task without executing it
1266     */
1267     public void testPollTask() {
1268 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1269 jsr166 1.7 protected void realCompute() {
1270 jsr166 1.8 CCF g = new LCCF(9);
1271 dl 1.1 assertSame(g, g.fork());
1272 jsr166 1.8 CCF f = new LCCF(8);
1273 dl 1.1 assertSame(f, f.fork());
1274     assertSame(f, pollTask());
1275     helpQuiesce();
1276     checkNotDone(f);
1277     checkCompletedNormally(g);
1278     }};
1279     testInvokeOnPool(singletonPool(), a);
1280     }
1281    
1282     /**
1283     * peekNextLocalTask returns least recent unexecuted task in async mode
1284     */
1285     public void testPeekNextLocalTaskAsync() {
1286 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1287 jsr166 1.7 protected void realCompute() {
1288 jsr166 1.8 CCF g = new LCCF(9);
1289 dl 1.1 assertSame(g, g.fork());
1290 jsr166 1.8 CCF f = new LCCF(8);
1291 dl 1.1 assertSame(f, f.fork());
1292     assertSame(g, peekNextLocalTask());
1293     assertNull(f.join());
1294     helpQuiesce();
1295     checkCompletedNormally(f);
1296     assertEquals(34, g.number);
1297     checkCompletedNormally(g);
1298     }};
1299     testInvokeOnPool(asyncSingletonPool(), a);
1300     }
1301    
1302     /**
1303     * pollNextLocalTask returns least recent unexecuted task without
1304     * executing it, in async mode
1305     */
1306     public void testPollNextLocalTaskAsync() {
1307 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1308 jsr166 1.7 protected void realCompute() {
1309 jsr166 1.8 CCF g = new LCCF(9);
1310 dl 1.1 assertSame(g, g.fork());
1311 jsr166 1.8 CCF f = new LCCF(8);
1312 dl 1.1 assertSame(f, f.fork());
1313     assertSame(g, pollNextLocalTask());
1314     helpQuiesce();
1315     assertEquals(21, f.number);
1316     checkCompletedNormally(f);
1317     checkNotDone(g);
1318     }};
1319     testInvokeOnPool(asyncSingletonPool(), a);
1320     }
1321    
1322     /**
1323     * pollTask returns an unexecuted task without executing it, in
1324     * async mode
1325     */
1326     public void testPollTaskAsync() {
1327 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1328 jsr166 1.7 protected void realCompute() {
1329 jsr166 1.8 CCF g = new LCCF(9);
1330 dl 1.1 assertSame(g, g.fork());
1331 jsr166 1.8 CCF f = new LCCF(8);
1332 dl 1.1 assertSame(f, f.fork());
1333     assertSame(g, pollTask());
1334     helpQuiesce();
1335     assertEquals(21, f.number);
1336     checkCompletedNormally(f);
1337     checkNotDone(g);
1338     }};
1339     testInvokeOnPool(asyncSingletonPool(), a);
1340     }
1341    
1342     // versions for singleton pools
1343    
1344     /**
1345     * invoke returns when task completes normally.
1346     * isCompletedAbnormally and isCancelled return false for normally
1347     * completed tasks; getRawResult returns null.
1348     */
1349     public void testInvokeSingleton() {
1350 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1351 jsr166 1.7 protected void realCompute() {
1352 jsr166 1.8 CCF f = new LCCF(8);
1353 dl 1.1 assertNull(f.invoke());
1354     assertEquals(21, f.number);
1355     checkCompletedNormally(f);
1356     }};
1357     testInvokeOnPool(singletonPool(), a);
1358     }
1359    
1360     /**
1361     * quietlyInvoke task returns when task completes normally.
1362     * isCompletedAbnormally and isCancelled return false for normally
1363     * completed tasks
1364     */
1365     public void testQuietlyInvokeSingleton() {
1366 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1367 jsr166 1.7 protected void realCompute() {
1368 jsr166 1.8 CCF f = new LCCF(8);
1369 dl 1.1 f.quietlyInvoke();
1370     assertEquals(21, f.number);
1371     checkCompletedNormally(f);
1372     }};
1373     testInvokeOnPool(singletonPool(), a);
1374     }
1375    
1376     /**
1377     * join of a forked task returns when task completes
1378     */
1379     public void testForkJoinSingleton() {
1380 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1381 jsr166 1.7 protected void realCompute() {
1382 jsr166 1.8 CCF f = new LCCF(8);
1383 dl 1.1 assertSame(f, f.fork());
1384     assertNull(f.join());
1385     assertEquals(21, f.number);
1386     checkCompletedNormally(f);
1387     }};
1388     testInvokeOnPool(singletonPool(), a);
1389     }
1390    
1391     /**
1392     * get of a forked task returns when task completes
1393     */
1394     public void testForkGetSingleton() {
1395 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1396 jsr166 1.7 protected void realCompute() throws Exception {
1397 jsr166 1.8 CCF f = new LCCF(8);
1398 dl 1.1 assertSame(f, f.fork());
1399     assertNull(f.get());
1400     assertEquals(21, f.number);
1401     checkCompletedNormally(f);
1402     }};
1403     testInvokeOnPool(singletonPool(), a);
1404     }
1405    
1406     /**
1407     * timed get of a forked task returns when task completes
1408     */
1409     public void testForkTimedGetSingleton() {
1410 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1411 jsr166 1.7 protected void realCompute() throws Exception {
1412 jsr166 1.8 CCF f = new LCCF(8);
1413 dl 1.1 assertSame(f, f.fork());
1414     assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1415     assertEquals(21, f.number);
1416     checkCompletedNormally(f);
1417     }};
1418     testInvokeOnPool(singletonPool(), a);
1419     }
1420    
1421     /**
1422     * timed get with null time unit throws NPE
1423     */
1424     public void testForkTimedGetNPESingleton() {
1425 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1426 jsr166 1.7 protected void realCompute() throws Exception {
1427 jsr166 1.8 CCF f = new LCCF(8);
1428 dl 1.1 assertSame(f, f.fork());
1429     try {
1430 jsr166 1.33 f.get(randomTimeout(), null);
1431 dl 1.1 shouldThrow();
1432     } catch (NullPointerException success) {}
1433     }};
1434     testInvokeOnPool(singletonPool(), a);
1435     }
1436    
1437     /**
1438     * quietlyJoin of a forked task returns when task completes
1439     */
1440     public void testForkQuietlyJoinSingleton() {
1441 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1442 jsr166 1.7 protected void realCompute() {
1443 jsr166 1.8 CCF f = new LCCF(8);
1444 dl 1.1 assertSame(f, f.fork());
1445     f.quietlyJoin();
1446     assertEquals(21, f.number);
1447     checkCompletedNormally(f);
1448     }};
1449     testInvokeOnPool(singletonPool(), a);
1450     }
1451    
1452     /**
1453     * helpQuiesce returns when tasks are complete.
1454     * getQueuedTaskCount returns 0 when quiescent
1455     */
1456     public void testForkHelpQuiesceSingleton() {
1457 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1458 jsr166 1.7 protected void realCompute() {
1459 jsr166 1.8 CCF f = new LCCF(8);
1460 dl 1.1 assertSame(f, f.fork());
1461     helpQuiesce();
1462     assertEquals(0, getQueuedTaskCount());
1463     assertEquals(21, f.number);
1464     checkCompletedNormally(f);
1465     }};
1466     testInvokeOnPool(singletonPool(), a);
1467     }
1468    
1469     /**
1470     * invoke task throws exception when task completes abnormally
1471     */
1472     public void testAbnormalInvokeSingleton() {
1473 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1474 jsr166 1.7 protected void realCompute() {
1475 jsr166 1.8 FailingCCF f = new LFCCF(8);
1476 dl 1.1 try {
1477     f.invoke();
1478     shouldThrow();
1479     } catch (FJException success) {
1480     checkCompletedAbnormally(f, success);
1481     }
1482     }};
1483     testInvokeOnPool(singletonPool(), a);
1484     }
1485    
1486     /**
1487     * quietlyInvoke task returns when task completes abnormally
1488     */
1489     public void testAbnormalQuietlyInvokeSingleton() {
1490 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1491 jsr166 1.7 protected void realCompute() {
1492 jsr166 1.8 FailingCCF f = new LFCCF(8);
1493 dl 1.1 f.quietlyInvoke();
1494     assertTrue(f.getException() instanceof FJException);
1495     checkCompletedAbnormally(f, f.getException());
1496     }};
1497     testInvokeOnPool(singletonPool(), a);
1498     }
1499    
1500     /**
1501     * join of a forked task throws exception when task completes abnormally
1502     */
1503     public void testAbnormalForkJoinSingleton() {
1504 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1505 jsr166 1.7 protected void realCompute() {
1506 jsr166 1.8 FailingCCF f = new LFCCF(8);
1507 dl 1.1 assertSame(f, f.fork());
1508     try {
1509     f.join();
1510     shouldThrow();
1511     } catch (FJException success) {
1512     checkCompletedAbnormally(f, success);
1513     }
1514     }};
1515     testInvokeOnPool(singletonPool(), a);
1516     }
1517    
1518     /**
1519     * get of a forked task throws exception when task completes abnormally
1520     */
1521     public void testAbnormalForkGetSingleton() {
1522 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1523 jsr166 1.7 protected void realCompute() throws Exception {
1524 jsr166 1.8 FailingCCF f = new LFCCF(8);
1525 dl 1.1 assertSame(f, f.fork());
1526     try {
1527     f.get();
1528     shouldThrow();
1529     } catch (ExecutionException success) {
1530     Throwable cause = success.getCause();
1531     assertTrue(cause instanceof FJException);
1532     checkCompletedAbnormally(f, cause);
1533     }
1534     }};
1535     testInvokeOnPool(singletonPool(), a);
1536     }
1537    
1538     /**
1539     * timed get of a forked task throws exception when task completes abnormally
1540     */
1541     public void testAbnormalForkTimedGetSingleton() {
1542 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1543 jsr166 1.7 protected void realCompute() throws Exception {
1544 jsr166 1.8 FailingCCF f = new LFCCF(8);
1545 dl 1.1 assertSame(f, f.fork());
1546     try {
1547     f.get(LONG_DELAY_MS, MILLISECONDS);
1548     shouldThrow();
1549     } catch (ExecutionException success) {
1550     Throwable cause = success.getCause();
1551     assertTrue(cause instanceof FJException);
1552     checkCompletedAbnormally(f, cause);
1553     }
1554     }};
1555     testInvokeOnPool(singletonPool(), a);
1556     }
1557    
1558     /**
1559     * quietlyJoin of a forked task returns when task completes abnormally
1560     */
1561     public void testAbnormalForkQuietlyJoinSingleton() {
1562 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1563 jsr166 1.7 protected void realCompute() {
1564 jsr166 1.8 FailingCCF f = new LFCCF(8);
1565 dl 1.1 assertSame(f, f.fork());
1566     f.quietlyJoin();
1567     assertTrue(f.getException() instanceof FJException);
1568     checkCompletedAbnormally(f, f.getException());
1569     }};
1570     testInvokeOnPool(singletonPool(), a);
1571     }
1572    
1573     /**
1574     * invoke task throws exception when task cancelled
1575     */
1576     public void testCancelledInvokeSingleton() {
1577 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1578 jsr166 1.7 protected void realCompute() {
1579 jsr166 1.8 CCF f = new LCCF(8);
1580 dl 1.1 assertTrue(f.cancel(true));
1581     try {
1582     f.invoke();
1583     shouldThrow();
1584     } catch (CancellationException success) {
1585     checkCancelled(f);
1586     }
1587     }};
1588     testInvokeOnPool(singletonPool(), a);
1589     }
1590    
1591     /**
1592     * join of a forked task throws exception when task cancelled
1593     */
1594     public void testCancelledForkJoinSingleton() {
1595 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1596 jsr166 1.7 protected void realCompute() {
1597 jsr166 1.8 CCF f = new LCCF(8);
1598 dl 1.1 assertTrue(f.cancel(true));
1599     assertSame(f, f.fork());
1600     try {
1601     f.join();
1602     shouldThrow();
1603     } catch (CancellationException success) {
1604     checkCancelled(f);
1605     }
1606     }};
1607     testInvokeOnPool(singletonPool(), a);
1608     }
1609    
1610     /**
1611     * get of a forked task throws exception when task cancelled
1612     */
1613     public void testCancelledForkGetSingleton() {
1614 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1615 jsr166 1.7 protected void realCompute() throws Exception {
1616 jsr166 1.8 CCF f = new LCCF(8);
1617 dl 1.1 assertTrue(f.cancel(true));
1618     assertSame(f, f.fork());
1619     try {
1620     f.get();
1621     shouldThrow();
1622     } catch (CancellationException success) {
1623     checkCancelled(f);
1624     }
1625     }};
1626     testInvokeOnPool(singletonPool(), a);
1627     }
1628    
1629     /**
1630     * timed get of a forked task throws exception when task cancelled
1631     */
1632     public void testCancelledForkTimedGetSingleton() throws Exception {
1633 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1634 jsr166 1.7 protected void realCompute() throws Exception {
1635 jsr166 1.8 CCF f = new LCCF(8);
1636 dl 1.1 assertTrue(f.cancel(true));
1637     assertSame(f, f.fork());
1638     try {
1639     f.get(LONG_DELAY_MS, MILLISECONDS);
1640     shouldThrow();
1641     } catch (CancellationException success) {
1642     checkCancelled(f);
1643     }
1644     }};
1645     testInvokeOnPool(singletonPool(), a);
1646     }
1647    
1648     /**
1649     * quietlyJoin of a forked task returns when task cancelled
1650     */
1651     public void testCancelledForkQuietlyJoinSingleton() {
1652 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1653 jsr166 1.7 protected void realCompute() {
1654 jsr166 1.8 CCF f = new LCCF(8);
1655 dl 1.1 assertTrue(f.cancel(true));
1656     assertSame(f, f.fork());
1657     f.quietlyJoin();
1658     checkCancelled(f);
1659     }};
1660     testInvokeOnPool(singletonPool(), a);
1661     }
1662    
1663     /**
1664     * invoke task throws exception after invoking completeExceptionally
1665     */
1666     public void testCompleteExceptionallySingleton() {
1667 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1668 jsr166 1.7 protected void realCompute() {
1669 jsr166 1.8 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 dl 1.1 }};
1676     testInvokeOnPool(singletonPool(), a);
1677     }
1678    
1679     /**
1680     * invokeAll(t1, t2) invokes all task arguments
1681     */
1682     public void testInvokeAll2Singleton() {
1683 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1684 jsr166 1.7 protected void realCompute() {
1685 jsr166 1.8 CCF f = new LCCF(8);
1686     CCF g = new LCCF(9);
1687 dl 1.1 invokeAll(f, g);
1688     assertEquals(21, f.number);
1689     assertEquals(34, g.number);
1690     checkCompletedNormally(f);
1691     checkCompletedNormally(g);
1692     }};
1693     testInvokeOnPool(singletonPool(), a);
1694     }
1695    
1696     /**
1697     * invokeAll(tasks) with 1 argument invokes task
1698     */
1699     public void testInvokeAll1Singleton() {
1700 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1701 jsr166 1.7 protected void realCompute() {
1702 jsr166 1.8 CCF f = new LCCF(8);
1703 dl 1.1 invokeAll(f);
1704     checkCompletedNormally(f);
1705     assertEquals(21, f.number);
1706     }};
1707     testInvokeOnPool(singletonPool(), a);
1708     }
1709    
1710     /**
1711     * invokeAll(tasks) with > 2 argument invokes tasks
1712     */
1713     public void testInvokeAll3Singleton() {
1714 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1715 jsr166 1.7 protected void realCompute() {
1716 jsr166 1.8 CCF f = new LCCF(8);
1717     CCF g = new LCCF(9);
1718     CCF h = new LCCF(7);
1719 dl 1.1 invokeAll(f, g, h);
1720     assertEquals(21, f.number);
1721     assertEquals(34, g.number);
1722     assertEquals(13, h.number);
1723     checkCompletedNormally(f);
1724     checkCompletedNormally(g);
1725     checkCompletedNormally(h);
1726     }};
1727     testInvokeOnPool(singletonPool(), a);
1728     }
1729    
1730     /**
1731     * invokeAll(collection) invokes all tasks in the collection
1732     */
1733     public void testInvokeAllCollectionSingleton() {
1734 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1735 jsr166 1.7 protected void realCompute() {
1736 jsr166 1.8 CCF f = new LCCF(8);
1737     CCF g = new LCCF(9);
1738     CCF h = new LCCF(7);
1739 jsr166 1.39 HashSet<ForkJoinTask<?>> set = new HashSet<>();
1740 dl 1.1 set.add(f);
1741     set.add(g);
1742     set.add(h);
1743     invokeAll(set);
1744     assertEquals(21, f.number);
1745     assertEquals(34, g.number);
1746     assertEquals(13, h.number);
1747     checkCompletedNormally(f);
1748     checkCompletedNormally(g);
1749     checkCompletedNormally(h);
1750     }};
1751     testInvokeOnPool(singletonPool(), a);
1752     }
1753    
1754     /**
1755     * invokeAll(tasks) with any null task throws NPE
1756     */
1757     public void testInvokeAllNPESingleton() {
1758 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1759 jsr166 1.7 protected void realCompute() {
1760 jsr166 1.8 CCF f = new LCCF(8);
1761     CCF g = new LCCF(9);
1762 dl 1.1 CCF h = null;
1763     try {
1764     invokeAll(f, g, h);
1765     shouldThrow();
1766     } catch (NullPointerException success) {}
1767     }};
1768     testInvokeOnPool(singletonPool(), a);
1769     }
1770    
1771     /**
1772     * invokeAll(t1, t2) throw exception if any task does
1773     */
1774     public void testAbnormalInvokeAll2Singleton() {
1775 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1776 jsr166 1.7 protected void realCompute() {
1777 jsr166 1.8 CCF f = new LCCF(8);
1778     FailingCCF g = new LFCCF(9);
1779 dl 1.1 try {
1780     invokeAll(f, g);
1781     shouldThrow();
1782     } catch (FJException success) {
1783     checkCompletedAbnormally(g, success);
1784     }
1785     }};
1786     testInvokeOnPool(singletonPool(), a);
1787     }
1788    
1789     /**
1790     * invokeAll(tasks) with 1 argument throws exception if task does
1791     */
1792     public void testAbnormalInvokeAll1Singleton() {
1793 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1794 jsr166 1.7 protected void realCompute() {
1795 jsr166 1.8 FailingCCF g = new LFCCF(9);
1796 dl 1.1 try {
1797     invokeAll(g);
1798     shouldThrow();
1799     } catch (FJException success) {
1800     checkCompletedAbnormally(g, success);
1801     }
1802     }};
1803     testInvokeOnPool(singletonPool(), a);
1804     }
1805    
1806     /**
1807     * invokeAll(tasks) with > 2 argument throws exception if any task does
1808     */
1809     public void testAbnormalInvokeAll3Singleton() {
1810 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1811 jsr166 1.7 protected void realCompute() {
1812 jsr166 1.8 CCF f = new LCCF(8);
1813     FailingCCF g = new LFCCF(9);
1814     CCF h = new LCCF(7);
1815 dl 1.1 try {
1816     invokeAll(f, g, h);
1817     shouldThrow();
1818     } catch (FJException success) {
1819     checkCompletedAbnormally(g, success);
1820     }
1821     }};
1822     testInvokeOnPool(singletonPool(), a);
1823     }
1824    
1825     /**
1826 jsr166 1.13 * invokeAll(collection) throws exception if any task does
1827 dl 1.1 */
1828     public void testAbnormalInvokeAllCollectionSingleton() {
1829 dl 1.38 CheckedRecursiveAction a = new CheckedRecursiveAction() {
1830 jsr166 1.7 protected void realCompute() {
1831 jsr166 1.8 FailingCCF f = new LFCCF(8);
1832     CCF g = new LCCF(9);
1833     CCF h = new LCCF(7);
1834 jsr166 1.39 HashSet<ForkJoinTask<?>> set = new HashSet<>();
1835 dl 1.1 set.add(f);
1836     set.add(g);
1837     set.add(h);
1838     try {
1839     invokeAll(set);
1840     shouldThrow();
1841     } catch (FJException success) {
1842     checkCompletedAbnormally(f, success);
1843     }
1844     }};
1845     testInvokeOnPool(singletonPool(), a);
1846     }
1847    
1848     }