ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck-jsr166e/RecursiveTaskTest.java
Revision: 1.1
Committed: Sun Jul 14 19:55:05 2013 UTC (10 years, 10 months ago) by jsr166
Branch: MAIN
Log Message:
backport jsr166e to run on jdk6; backport all applicable tck tests from tck to tck-jsr166e

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