ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.31
Committed: Wed Dec 31 19:05:43 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.30: +6 -3 lines
Log Message:
no wildcard imports

File Contents

# Content
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
7 import static java.util.concurrent.TimeUnit.SECONDS;
8
9 import java.util.HashSet;
10 import java.util.concurrent.CancellationException;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.ForkJoinPool;
13 import java.util.concurrent.ForkJoinTask;
14 import java.util.concurrent.RecursiveTask;
15 import java.util.concurrent.TimeoutException;
16
17 import junit.framework.Test;
18 import junit.framework.TestSuite;
19
20 public class RecursiveTaskTest extends JSR166TestCase {
21
22 public static void main(String[] args) {
23 junit.textui.TestRunner.run(suite());
24 }
25 public static Test suite() {
26 return new TestSuite(RecursiveTaskTest.class);
27 }
28
29 private static ForkJoinPool mainPool() {
30 return new ForkJoinPool();
31 }
32
33 private static ForkJoinPool singletonPool() {
34 return new ForkJoinPool(1);
35 }
36
37 private static ForkJoinPool asyncSingletonPool() {
38 return new ForkJoinPool(1,
39 ForkJoinPool.defaultForkJoinWorkerThreadFactory,
40 null, true);
41 }
42
43 private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) {
44 try {
45 checkNotDone(a);
46
47 T result = pool.invoke(a);
48
49 checkCompletedNormally(a, result);
50 return result;
51 } finally {
52 joinPool(pool);
53 }
54 }
55
56 void checkNotDone(RecursiveTask a) {
57 assertFalse(a.isDone());
58 assertFalse(a.isCompletedNormally());
59 assertFalse(a.isCompletedAbnormally());
60 assertFalse(a.isCancelled());
61 assertNull(a.getException());
62 assertNull(a.getRawResult());
63
64 if (! ForkJoinTask.inForkJoinPool()) {
65 Thread.currentThread().interrupt();
66 try {
67 a.get();
68 shouldThrow();
69 } catch (InterruptedException success) {
70 } catch (Throwable fail) { threadUnexpectedException(fail); }
71
72 Thread.currentThread().interrupt();
73 try {
74 a.get(5L, SECONDS);
75 shouldThrow();
76 } catch (InterruptedException success) {
77 } catch (Throwable fail) { threadUnexpectedException(fail); }
78 }
79
80 try {
81 a.get(0L, SECONDS);
82 shouldThrow();
83 } catch (TimeoutException success) {
84 } catch (Throwable fail) { threadUnexpectedException(fail); }
85 }
86
87 <T> void checkCompletedNormally(RecursiveTask<T> a, T expected) {
88 assertTrue(a.isDone());
89 assertFalse(a.isCancelled());
90 assertTrue(a.isCompletedNormally());
91 assertFalse(a.isCompletedAbnormally());
92 assertNull(a.getException());
93 assertSame(expected, a.getRawResult());
94 assertSame(expected, a.join());
95 assertFalse(a.cancel(false));
96 assertFalse(a.cancel(true));
97 try {
98 assertSame(expected, a.get());
99 } catch (Throwable fail) { threadUnexpectedException(fail); }
100 try {
101 assertSame(expected, a.get(5L, SECONDS));
102 } catch (Throwable fail) { threadUnexpectedException(fail); }
103 }
104
105 /**
106 * Waits for the task to complete, and checks that when it does,
107 * it will have an Integer result equals to the given int.
108 */
109 void checkCompletesNormally(RecursiveTask<Integer> a, int expected) {
110 Integer r = a.join();
111 assertEquals(expected, (int) r);
112 checkCompletedNormally(a, r);
113 }
114
115 /**
116 * Like checkCompletesNormally, but verifies that the task has
117 * already completed.
118 */
119 void checkCompletedNormally(RecursiveTask<Integer> a, int expected) {
120 Integer r = a.getRawResult();
121 assertEquals(expected, (int) r);
122 checkCompletedNormally(a, r);
123 }
124
125 void checkCancelled(RecursiveTask a) {
126 assertTrue(a.isDone());
127 assertTrue(a.isCancelled());
128 assertFalse(a.isCompletedNormally());
129 assertTrue(a.isCompletedAbnormally());
130 assertTrue(a.getException() instanceof CancellationException);
131 assertNull(a.getRawResult());
132
133 try {
134 a.join();
135 shouldThrow();
136 } catch (CancellationException success) {
137 } catch (Throwable fail) { threadUnexpectedException(fail); }
138
139 try {
140 a.get();
141 shouldThrow();
142 } catch (CancellationException success) {
143 } catch (Throwable fail) { threadUnexpectedException(fail); }
144
145 try {
146 a.get(5L, SECONDS);
147 shouldThrow();
148 } catch (CancellationException success) {
149 } catch (Throwable fail) { threadUnexpectedException(fail); }
150 }
151
152 void checkCompletedAbnormally(RecursiveTask a, Throwable t) {
153 assertTrue(a.isDone());
154 assertFalse(a.isCancelled());
155 assertFalse(a.isCompletedNormally());
156 assertTrue(a.isCompletedAbnormally());
157 assertSame(t.getClass(), a.getException().getClass());
158 assertNull(a.getRawResult());
159 assertFalse(a.cancel(false));
160 assertFalse(a.cancel(true));
161
162 try {
163 a.join();
164 shouldThrow();
165 } catch (Throwable expected) {
166 assertSame(t.getClass(), expected.getClass());
167 }
168
169 try {
170 a.get();
171 shouldThrow();
172 } catch (ExecutionException success) {
173 assertSame(t.getClass(), success.getCause().getClass());
174 } catch (Throwable fail) { threadUnexpectedException(fail); }
175
176 try {
177 a.get(5L, SECONDS);
178 shouldThrow();
179 } catch (ExecutionException success) {
180 assertSame(t.getClass(), success.getCause().getClass());
181 } catch (Throwable fail) { threadUnexpectedException(fail); }
182 }
183
184 public static final class FJException extends RuntimeException {
185 public FJException() { super(); }
186 }
187
188 // An invalid return value for Fib
189 static final Integer NoResult = Integer.valueOf(-17);
190
191 // A simple recursive task for testing
192 final class FibTask extends CheckedRecursiveTask<Integer> {
193 final int number;
194 FibTask(int n) { number = n; }
195 public Integer realCompute() {
196 int n = number;
197 if (n <= 1)
198 return n;
199 FibTask f1 = new FibTask(n - 1);
200 f1.fork();
201 return (new FibTask(n - 2)).compute() + f1.join();
202 }
203
204 public void publicSetRawResult(Integer result) {
205 setRawResult(result);
206 }
207 }
208
209 // A recursive action failing in base case
210 final class FailingFibTask extends RecursiveTask<Integer> {
211 final int number;
212 int result;
213 FailingFibTask(int n) { number = n; }
214 public Integer compute() {
215 int n = number;
216 if (n <= 1)
217 throw new FJException();
218 FailingFibTask f1 = new FailingFibTask(n - 1);
219 f1.fork();
220 return (new FibTask(n - 2)).compute() + f1.join();
221 }
222 }
223
224 /**
225 * invoke returns value when task completes normally.
226 * isCompletedAbnormally and isCancelled return false for normally
227 * completed tasks. getRawResult of a completed non-null task
228 * returns value;
229 */
230 public void testInvoke() {
231 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
232 public Integer realCompute() {
233 FibTask f = new FibTask(8);
234 Integer r = f.invoke();
235 assertEquals(21, (int) r);
236 checkCompletedNormally(f, r);
237 return r;
238 }};
239 assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
240 }
241
242 /**
243 * quietlyInvoke task returns when task completes normally.
244 * isCompletedAbnormally and isCancelled return false for normally
245 * completed tasks
246 */
247 public void testQuietlyInvoke() {
248 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
249 public Integer realCompute() {
250 FibTask f = new FibTask(8);
251 f.quietlyInvoke();
252 checkCompletedNormally(f, 21);
253 return NoResult;
254 }};
255 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
256 }
257
258 /**
259 * join of a forked task returns when task completes
260 */
261 public void testForkJoin() {
262 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
263 public Integer realCompute() {
264 FibTask f = new FibTask(8);
265 assertSame(f, f.fork());
266 Integer r = f.join();
267 assertEquals(21, (int) r);
268 checkCompletedNormally(f, r);
269 return r;
270 }};
271 assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
272 }
273
274 /**
275 * get of a forked task returns when task completes
276 */
277 public void testForkGet() {
278 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
279 public Integer realCompute() throws Exception {
280 FibTask f = new FibTask(8);
281 assertSame(f, f.fork());
282 Integer r = f.get();
283 assertEquals(21, (int) r);
284 checkCompletedNormally(f, r);
285 return r;
286 }};
287 assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
288 }
289
290 /**
291 * timed get of a forked task returns when task completes
292 */
293 public void testForkTimedGet() {
294 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
295 public Integer realCompute() throws Exception {
296 FibTask f = new FibTask(8);
297 assertSame(f, f.fork());
298 Integer r = f.get(5L, SECONDS);
299 assertEquals(21, (int) r);
300 checkCompletedNormally(f, r);
301 return r;
302 }};
303 assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
304 }
305
306 /**
307 * quietlyJoin of a forked task returns when task completes
308 */
309 public void testForkQuietlyJoin() {
310 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
311 public Integer realCompute() {
312 FibTask f = new FibTask(8);
313 assertSame(f, f.fork());
314 f.quietlyJoin();
315 Integer r = f.getRawResult();
316 assertEquals(21, (int) r);
317 checkCompletedNormally(f, r);
318 return r;
319 }};
320 assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
321 }
322
323 /**
324 * helpQuiesce returns when tasks are complete.
325 * getQueuedTaskCount returns 0 when quiescent
326 */
327 public void testForkHelpQuiesce() {
328 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
329 public Integer realCompute() {
330 FibTask f = new FibTask(8);
331 assertSame(f, f.fork());
332 helpQuiesce();
333 assertEquals(0, getQueuedTaskCount());
334 checkCompletedNormally(f, 21);
335 return NoResult;
336 }};
337 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
338 }
339
340 /**
341 * invoke task throws exception when task completes abnormally
342 */
343 public void testAbnormalInvoke() {
344 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
345 public Integer realCompute() {
346 FailingFibTask f = new FailingFibTask(8);
347 try {
348 f.invoke();
349 shouldThrow();
350 } catch (FJException success) {
351 checkCompletedAbnormally(f, success);
352 }
353 return NoResult;
354 }};
355 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
356 }
357
358 /**
359 * quietlyInvoke task returns when task completes abnormally
360 */
361 public void testAbnormalQuietlyInvoke() {
362 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
363 public Integer realCompute() {
364 FailingFibTask f = new FailingFibTask(8);
365 f.quietlyInvoke();
366 assertTrue(f.getException() instanceof FJException);
367 checkCompletedAbnormally(f, f.getException());
368 return NoResult;
369 }};
370 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
371 }
372
373 /**
374 * join of a forked task throws exception when task completes abnormally
375 */
376 public void testAbnormalForkJoin() {
377 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
378 public Integer realCompute() {
379 FailingFibTask f = new FailingFibTask(8);
380 assertSame(f, f.fork());
381 try {
382 Integer r = f.join();
383 shouldThrow();
384 } catch (FJException success) {
385 checkCompletedAbnormally(f, success);
386 }
387 return NoResult;
388 }};
389 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
390 }
391
392 /**
393 * get of a forked task throws exception when task completes abnormally
394 */
395 public void testAbnormalForkGet() {
396 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
397 public Integer realCompute() throws Exception {
398 FailingFibTask f = new FailingFibTask(8);
399 assertSame(f, f.fork());
400 try {
401 Integer r = f.get();
402 shouldThrow();
403 } catch (ExecutionException success) {
404 Throwable cause = success.getCause();
405 assertTrue(cause instanceof FJException);
406 checkCompletedAbnormally(f, cause);
407 }
408 return NoResult;
409 }};
410 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
411 }
412
413 /**
414 * timed get of a forked task throws exception when task completes abnormally
415 */
416 public void testAbnormalForkTimedGet() {
417 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
418 public Integer realCompute() throws Exception {
419 FailingFibTask f = new FailingFibTask(8);
420 assertSame(f, f.fork());
421 try {
422 Integer r = f.get(5L, SECONDS);
423 shouldThrow();
424 } catch (ExecutionException success) {
425 Throwable cause = success.getCause();
426 assertTrue(cause instanceof FJException);
427 checkCompletedAbnormally(f, cause);
428 }
429 return NoResult;
430 }};
431 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
432 }
433
434 /**
435 * quietlyJoin of a forked task returns when task completes abnormally
436 */
437 public void testAbnormalForkQuietlyJoin() {
438 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
439 public Integer realCompute() {
440 FailingFibTask f = new FailingFibTask(8);
441 assertSame(f, f.fork());
442 f.quietlyJoin();
443 assertTrue(f.getException() instanceof FJException);
444 checkCompletedAbnormally(f, f.getException());
445 return NoResult;
446 }};
447 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
448 }
449
450 /**
451 * invoke task throws exception when task cancelled
452 */
453 public void testCancelledInvoke() {
454 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
455 public Integer realCompute() {
456 FibTask f = new FibTask(8);
457 assertTrue(f.cancel(true));
458 try {
459 Integer r = f.invoke();
460 shouldThrow();
461 } catch (CancellationException success) {
462 checkCancelled(f);
463 }
464 return NoResult;
465 }};
466 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
467 }
468
469 /**
470 * join of a forked task throws exception when task cancelled
471 */
472 public void testCancelledForkJoin() {
473 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
474 public Integer realCompute() {
475 FibTask f = new FibTask(8);
476 assertTrue(f.cancel(true));
477 assertSame(f, f.fork());
478 try {
479 Integer r = f.join();
480 shouldThrow();
481 } catch (CancellationException success) {
482 checkCancelled(f);
483 }
484 return NoResult;
485 }};
486 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
487 }
488
489 /**
490 * get of a forked task throws exception when task cancelled
491 */
492 public void testCancelledForkGet() {
493 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
494 public Integer realCompute() throws Exception {
495 FibTask f = new FibTask(8);
496 assertTrue(f.cancel(true));
497 assertSame(f, f.fork());
498 try {
499 Integer r = f.get();
500 shouldThrow();
501 } catch (CancellationException success) {
502 checkCancelled(f);
503 }
504 return NoResult;
505 }};
506 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
507 }
508
509 /**
510 * timed get of a forked task throws exception when task cancelled
511 */
512 public void testCancelledForkTimedGet() {
513 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
514 public Integer realCompute() throws Exception {
515 FibTask f = new FibTask(8);
516 assertTrue(f.cancel(true));
517 assertSame(f, f.fork());
518 try {
519 Integer r = f.get(5L, SECONDS);
520 shouldThrow();
521 } catch (CancellationException success) {
522 checkCancelled(f);
523 }
524 return NoResult;
525 }};
526 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
527 }
528
529 /**
530 * quietlyJoin of a forked task returns when task cancelled
531 */
532 public void testCancelledForkQuietlyJoin() {
533 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
534 public Integer realCompute() {
535 FibTask f = new FibTask(8);
536 assertTrue(f.cancel(true));
537 assertSame(f, f.fork());
538 f.quietlyJoin();
539 checkCancelled(f);
540 return NoResult;
541 }};
542 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
543 }
544
545 /**
546 * getPool of executing task returns its pool
547 */
548 public void testGetPool() {
549 final ForkJoinPool mainPool = mainPool();
550 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
551 public Integer realCompute() {
552 assertSame(mainPool, getPool());
553 return NoResult;
554 }};
555 assertSame(NoResult, testInvokeOnPool(mainPool, a));
556 }
557
558 /**
559 * getPool of non-FJ task returns null
560 */
561 public void testGetPool2() {
562 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
563 public Integer realCompute() {
564 assertNull(getPool());
565 return NoResult;
566 }};
567 assertSame(NoResult, a.invoke());
568 }
569
570 /**
571 * inForkJoinPool of executing task returns true
572 */
573 public void testInForkJoinPool() {
574 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
575 public Integer realCompute() {
576 assertTrue(inForkJoinPool());
577 return NoResult;
578 }};
579 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
580 }
581
582 /**
583 * inForkJoinPool of non-FJ task returns false
584 */
585 public void testInForkJoinPool2() {
586 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
587 public Integer realCompute() {
588 assertFalse(inForkJoinPool());
589 return NoResult;
590 }};
591 assertSame(NoResult, a.invoke());
592 }
593
594 /**
595 * The value set by setRawResult is returned by getRawResult
596 */
597 public void testSetRawResult() {
598 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
599 public Integer realCompute() {
600 setRawResult(NoResult);
601 assertSame(NoResult, getRawResult());
602 return NoResult;
603 }
604 };
605 assertSame(NoResult, a.invoke());
606 }
607
608 /**
609 * A reinitialized normally completed task may be re-invoked
610 */
611 public void testReinitialize() {
612 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
613 public Integer realCompute() {
614 FibTask f = new FibTask(8);
615 checkNotDone(f);
616
617 for (int i = 0; i < 3; i++) {
618 Integer r = f.invoke();
619 assertEquals(21, (int) r);
620 checkCompletedNormally(f, r);
621 f.reinitialize();
622 f.publicSetRawResult(null);
623 checkNotDone(f);
624 }
625 return NoResult;
626 }};
627 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
628 }
629
630 /**
631 * A reinitialized abnormally completed task may be re-invoked
632 */
633 public void testReinitializeAbnormal() {
634 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
635 public Integer realCompute() {
636 FailingFibTask f = new FailingFibTask(8);
637 checkNotDone(f);
638
639 for (int i = 0; i < 3; i++) {
640 try {
641 f.invoke();
642 shouldThrow();
643 } catch (FJException success) {
644 checkCompletedAbnormally(f, success);
645 }
646 f.reinitialize();
647 checkNotDone(f);
648 }
649 return NoResult;
650 }};
651 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
652 }
653
654 /**
655 * invoke task throws exception after invoking completeExceptionally
656 */
657 public void testCompleteExceptionally() {
658 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
659 public Integer realCompute() {
660 FibTask f = new FibTask(8);
661 f.completeExceptionally(new FJException());
662 try {
663 Integer r = f.invoke();
664 shouldThrow();
665 } catch (FJException success) {
666 checkCompletedAbnormally(f, success);
667 }
668 return NoResult;
669 }};
670 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
671 }
672
673 /**
674 * invoke task suppresses execution invoking complete
675 */
676 public void testComplete() {
677 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
678 public Integer realCompute() {
679 FibTask f = new FibTask(8);
680 f.complete(NoResult);
681 Integer r = f.invoke();
682 assertSame(NoResult, r);
683 checkCompletedNormally(f, NoResult);
684 return r;
685 }};
686 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
687 }
688
689 /**
690 * invokeAll(t1, t2) invokes all task arguments
691 */
692 public void testInvokeAll2() {
693 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
694 public Integer realCompute() {
695 FibTask f = new FibTask(8);
696 FibTask g = new FibTask(9);
697 invokeAll(f, g);
698 checkCompletedNormally(f, 21);
699 checkCompletedNormally(g, 34);
700 return NoResult;
701 }};
702 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
703 }
704
705 /**
706 * invokeAll(tasks) with 1 argument invokes task
707 */
708 public void testInvokeAll1() {
709 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
710 public Integer realCompute() {
711 FibTask f = new FibTask(8);
712 invokeAll(f);
713 checkCompletedNormally(f, 21);
714 return NoResult;
715 }};
716 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
717 }
718
719 /**
720 * invokeAll(tasks) with > 2 argument invokes tasks
721 */
722 public void testInvokeAll3() {
723 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
724 public Integer realCompute() {
725 FibTask f = new FibTask(8);
726 FibTask g = new FibTask(9);
727 FibTask h = new FibTask(7);
728 invokeAll(f, g, h);
729 assertTrue(f.isDone());
730 assertTrue(g.isDone());
731 assertTrue(h.isDone());
732 checkCompletedNormally(f, 21);
733 checkCompletedNormally(g, 34);
734 checkCompletedNormally(h, 13);
735 return NoResult;
736 }};
737 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
738 }
739
740 /**
741 * invokeAll(collection) invokes all tasks in the collection
742 */
743 public void testInvokeAllCollection() {
744 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
745 public Integer realCompute() {
746 FibTask f = new FibTask(8);
747 FibTask g = new FibTask(9);
748 FibTask h = new FibTask(7);
749 HashSet set = new HashSet();
750 set.add(f);
751 set.add(g);
752 set.add(h);
753 invokeAll(set);
754 assertTrue(f.isDone());
755 assertTrue(g.isDone());
756 assertTrue(h.isDone());
757 checkCompletedNormally(f, 21);
758 checkCompletedNormally(g, 34);
759 checkCompletedNormally(h, 13);
760 return NoResult;
761 }};
762 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
763 }
764
765 /**
766 * invokeAll(tasks) with any null task throws NPE
767 */
768 public void testInvokeAllNPE() {
769 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
770 public Integer realCompute() {
771 FibTask f = new FibTask(8);
772 FibTask g = new FibTask(9);
773 FibTask h = null;
774 try {
775 invokeAll(f, g, h);
776 shouldThrow();
777 } catch (NullPointerException success) {}
778 return NoResult;
779 }};
780 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
781 }
782
783 /**
784 * invokeAll(t1, t2) throw exception if any task does
785 */
786 public void testAbnormalInvokeAll2() {
787 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
788 public Integer realCompute() {
789 FibTask f = new FibTask(8);
790 FailingFibTask g = new FailingFibTask(9);
791 try {
792 invokeAll(f, g);
793 shouldThrow();
794 } catch (FJException success) {
795 checkCompletedAbnormally(g, success);
796 }
797 return NoResult;
798 }};
799 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
800 }
801
802 /**
803 * invokeAll(tasks) with 1 argument throws exception if task does
804 */
805 public void testAbnormalInvokeAll1() {
806 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
807 public Integer realCompute() {
808 FailingFibTask g = new FailingFibTask(9);
809 try {
810 invokeAll(g);
811 shouldThrow();
812 } catch (FJException success) {
813 checkCompletedAbnormally(g, success);
814 }
815 return NoResult;
816 }};
817 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
818 }
819
820 /**
821 * invokeAll(tasks) with > 2 argument throws exception if any task does
822 */
823 public void testAbnormalInvokeAll3() {
824 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
825 public Integer realCompute() {
826 FibTask f = new FibTask(8);
827 FailingFibTask g = new FailingFibTask(9);
828 FibTask h = new FibTask(7);
829 try {
830 invokeAll(f, g, h);
831 shouldThrow();
832 } catch (FJException success) {
833 checkCompletedAbnormally(g, success);
834 }
835 return NoResult;
836 }};
837 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
838 }
839
840 /**
841 * invokeAll(collection) throws exception if any task does
842 */
843 public void testAbnormalInvokeAllCollection() {
844 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
845 public Integer realCompute() {
846 FailingFibTask f = new FailingFibTask(8);
847 FibTask g = new FibTask(9);
848 FibTask h = new FibTask(7);
849 HashSet set = new HashSet();
850 set.add(f);
851 set.add(g);
852 set.add(h);
853 try {
854 invokeAll(set);
855 shouldThrow();
856 } catch (FJException success) {
857 checkCompletedAbnormally(f, success);
858 }
859 return NoResult;
860 }};
861 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
862 }
863
864 /**
865 * tryUnfork returns true for most recent unexecuted task,
866 * and suppresses execution
867 */
868 public void testTryUnfork() {
869 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
870 public Integer realCompute() {
871 FibTask g = new FibTask(9);
872 assertSame(g, g.fork());
873 FibTask f = new FibTask(8);
874 assertSame(f, f.fork());
875 assertTrue(f.tryUnfork());
876 helpQuiesce();
877 checkNotDone(f);
878 checkCompletedNormally(g, 34);
879 return NoResult;
880 }};
881 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
882 }
883
884 /**
885 * getSurplusQueuedTaskCount returns > 0 when
886 * there are more tasks than threads
887 */
888 public void testGetSurplusQueuedTaskCount() {
889 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
890 public Integer realCompute() {
891 FibTask h = new FibTask(7);
892 assertSame(h, h.fork());
893 FibTask g = new FibTask(9);
894 assertSame(g, g.fork());
895 FibTask f = new FibTask(8);
896 assertSame(f, f.fork());
897 assertTrue(getSurplusQueuedTaskCount() > 0);
898 helpQuiesce();
899 assertEquals(0, getSurplusQueuedTaskCount());
900 checkCompletedNormally(f, 21);
901 checkCompletedNormally(g, 34);
902 checkCompletedNormally(h, 13);
903 return NoResult;
904 }};
905 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
906 }
907
908 /**
909 * peekNextLocalTask returns most recent unexecuted task.
910 */
911 public void testPeekNextLocalTask() {
912 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
913 public Integer realCompute() {
914 FibTask g = new FibTask(9);
915 assertSame(g, g.fork());
916 FibTask f = new FibTask(8);
917 assertSame(f, f.fork());
918 assertSame(f, peekNextLocalTask());
919 checkCompletesNormally(f, 21);
920 helpQuiesce();
921 checkCompletedNormally(g, 34);
922 return NoResult;
923 }};
924 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
925 }
926
927 /**
928 * pollNextLocalTask returns most recent unexecuted task
929 * without executing it
930 */
931 public void testPollNextLocalTask() {
932 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
933 public Integer realCompute() {
934 FibTask g = new FibTask(9);
935 assertSame(g, g.fork());
936 FibTask f = new FibTask(8);
937 assertSame(f, f.fork());
938 assertSame(f, pollNextLocalTask());
939 helpQuiesce();
940 checkNotDone(f);
941 checkCompletedNormally(g, 34);
942 return NoResult;
943 }};
944 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
945 }
946
947 /**
948 * pollTask returns an unexecuted task without executing it
949 */
950 public void testPollTask() {
951 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
952 public Integer realCompute() {
953 FibTask g = new FibTask(9);
954 assertSame(g, g.fork());
955 FibTask f = new FibTask(8);
956 assertSame(f, f.fork());
957 assertSame(f, pollTask());
958 helpQuiesce();
959 checkNotDone(f);
960 checkCompletedNormally(g, 34);
961 return NoResult;
962 }};
963 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
964 }
965
966 /**
967 * peekNextLocalTask returns least recent unexecuted task in async mode
968 */
969 public void testPeekNextLocalTaskAsync() {
970 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
971 public Integer realCompute() {
972 FibTask g = new FibTask(9);
973 assertSame(g, g.fork());
974 FibTask f = new FibTask(8);
975 assertSame(f, f.fork());
976 assertSame(g, peekNextLocalTask());
977 assertEquals(21, (int) f.join());
978 helpQuiesce();
979 checkCompletedNormally(f, 21);
980 checkCompletedNormally(g, 34);
981 return NoResult;
982 }};
983 assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
984 }
985
986 /**
987 * pollNextLocalTask returns least recent unexecuted task without
988 * executing it, in async mode
989 */
990 public void testPollNextLocalTaskAsync() {
991 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
992 public Integer realCompute() {
993 FibTask g = new FibTask(9);
994 assertSame(g, g.fork());
995 FibTask f = new FibTask(8);
996 assertSame(f, f.fork());
997 assertSame(g, pollNextLocalTask());
998 helpQuiesce();
999 checkCompletedNormally(f, 21);
1000 checkNotDone(g);
1001 return NoResult;
1002 }};
1003 assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
1004 }
1005
1006 /**
1007 * pollTask returns an unexecuted task without executing it, in
1008 * async mode
1009 */
1010 public void testPollTaskAsync() {
1011 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
1012 public Integer realCompute() {
1013 FibTask g = new FibTask(9);
1014 assertSame(g, g.fork());
1015 FibTask f = new FibTask(8);
1016 assertSame(f, f.fork());
1017 assertSame(g, pollTask());
1018 helpQuiesce();
1019 checkCompletedNormally(f, 21);
1020 checkNotDone(g);
1021 return NoResult;
1022 }};
1023 assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
1024 }
1025
1026 }