ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
(Generate patch)

Comparing jsr166/src/test/tck/RecursiveTaskTest.java (file contents):
Revision 1.20 by jsr166, Sun Nov 21 08:25:10 2010 UTC vs.
Revision 1.36 by jsr166, Mon May 29 19:15:02 2017 UTC

# Line 1 | Line 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/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import junit.framework.*;
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 >
9 > import java.util.HashSet;
10   import java.util.concurrent.CancellationException;
11   import java.util.concurrent.ExecutionException;
12   import java.util.concurrent.ForkJoinPool;
13 < import java.util.concurrent.ForkJoinWorkerThread;
13 > import java.util.concurrent.ForkJoinTask;
14   import java.util.concurrent.RecursiveTask;
13 import java.util.concurrent.TimeUnit;
15   import java.util.concurrent.TimeoutException;
16 < import static java.util.concurrent.TimeUnit.SECONDS;
17 < import java.util.HashSet;
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());
23 >        main(suite(), args);
24      }
25      public static Test suite() {
26          return new TestSuite(RecursiveTaskTest.class);
# Line 39 | Line 41 | public class RecursiveTaskTest extends J
41      }
42  
43      private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) {
44 <        try {
44 >        try (PoolCleaner cleaner = cleaner(pool)) {
45              checkNotDone(a);
46  
47              T result = pool.invoke(a);
48  
49              checkCompletedNormally(a, result);
50              return result;
49        } finally {
50            joinPool(pool);
51          }
52      }
53  
# Line 59 | Line 59 | public class RecursiveTaskTest extends J
59          assertNull(a.getException());
60          assertNull(a.getRawResult());
61  
62 <        if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) {
62 >        if (! ForkJoinTask.inForkJoinPool()) {
63              Thread.currentThread().interrupt();
64              try {
65                  a.get();
# Line 69 | Line 69 | public class RecursiveTaskTest extends J
69  
70              Thread.currentThread().interrupt();
71              try {
72 <                a.get(5L, SECONDS);
72 >                a.get(randomTimeout(), randomTimeUnit());
73                  shouldThrow();
74              } catch (InterruptedException success) {
75              } catch (Throwable fail) { threadUnexpectedException(fail); }
76          }
77  
78          try {
79 <            a.get(0L, SECONDS);
79 >            a.get(randomExpiredTimeout(), randomTimeUnit());
80              shouldThrow();
81          } catch (TimeoutException success) {
82          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 90 | Line 90 | public class RecursiveTaskTest extends J
90          assertNull(a.getException());
91          assertSame(expected, a.getRawResult());
92          assertSame(expected, a.join());
93 +        assertFalse(a.cancel(false));
94 +        assertFalse(a.cancel(true));
95          try {
96              assertSame(expected, a.get());
97 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
96 <        try {
97 <            assertSame(expected, a.get(5L, SECONDS));
97 >            assertSame(expected, a.get(randomTimeout(), randomTimeUnit()));
98          } catch (Throwable fail) { threadUnexpectedException(fail); }
99      }
100  
# Line 139 | Line 139 | public class RecursiveTaskTest extends J
139          } catch (Throwable fail) { threadUnexpectedException(fail); }
140  
141          try {
142 <            a.get(5L, SECONDS);
142 >            a.get(randomTimeout(), randomTimeUnit());
143              shouldThrow();
144          } catch (CancellationException success) {
145          } catch (Throwable fail) { threadUnexpectedException(fail); }
146      }
147  
148 <    void checkTaskThrew(RecursiveTask a, Throwable t) {
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, a.getException());
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, expected);
162 >            assertSame(t.getClass(), expected.getClass());
163          }
164  
165          try {
166              a.get();
167              shouldThrow();
168          } catch (ExecutionException success) {
169 <            assertSame(t, success.getCause());
169 >            assertSame(t.getClass(), success.getCause().getClass());
170          } catch (Throwable fail) { threadUnexpectedException(fail); }
171  
172          try {
173 <            a.get(5L, SECONDS);
173 >            a.get(randomTimeout(), randomTimeUnit());
174              shouldThrow();
175          } catch (ExecutionException success) {
176 <            assertSame(t, success.getCause());
176 >            assertSame(t.getClass(), success.getCause().getClass());
177          } catch (Throwable fail) { threadUnexpectedException(fail); }
178      }
179  
180 <    static final class FJException extends RuntimeException {
181 <        FJException() { super(); }
180 >    public static final class FJException extends RuntimeException {
181 >        public FJException() { super(); }
182      }
183  
184 <    // An invalid return value for Fib
184 >    /** An invalid return value for Fib. */
185      static final Integer NoResult = Integer.valueOf(-17);
186  
187 <    // A simple recursive task for testing
187 >    /** A simple recursive task for testing. */
188      final class FibTask extends CheckedRecursiveTask<Integer> {
189          final int number;
190          FibTask(int n) { number = n; }
# Line 200 | Line 202 | public class RecursiveTaskTest extends J
202          }
203      }
204  
205 <    // A recursive action failing in base case
205 >    /** A recursive action failing in base case. */
206      final class FailingFibTask extends RecursiveTask<Integer> {
207          final int number;
208          int result;
# Line 289 | Line 291 | public class RecursiveTaskTest extends J
291              public Integer realCompute() throws Exception {
292                  FibTask f = new FibTask(8);
293                  assertSame(f, f.fork());
294 <                Integer r = f.get(5L, SECONDS);
294 >                Integer r = f.get(LONG_DELAY_MS, MILLISECONDS);
295                  assertEquals(21, (int) r);
296                  checkCompletedNormally(f, r);
297                  return r;
# Line 314 | Line 316 | public class RecursiveTaskTest extends J
316          assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
317      }
318  
317
319      /**
320       * helpQuiesce returns when tasks are complete.
321       * getQueuedTaskCount returns 0 when quiescent
# Line 324 | Line 325 | public class RecursiveTaskTest extends J
325              public Integer realCompute() {
326                  FibTask f = new FibTask(8);
327                  assertSame(f, f.fork());
328 <                f.helpQuiesce();
328 >                helpQuiesce();
329 >                while (!f.isDone()) // wait out race
330 >                    ;
331                  assertEquals(0, getQueuedTaskCount());
332                  checkCompletedNormally(f, 21);
333                  return NoResult;
# Line 332 | Line 335 | public class RecursiveTaskTest extends J
335          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
336      }
337  
335
338      /**
339       * invoke task throws exception when task completes abnormally
340       */
# Line 344 | Line 346 | public class RecursiveTaskTest extends J
346                      f.invoke();
347                      shouldThrow();
348                  } catch (FJException success) {
349 <                    checkTaskThrew(f, success);
349 >                    checkCompletedAbnormally(f, success);
350                  }
351                  return NoResult;
352              }};
# Line 360 | Line 362 | public class RecursiveTaskTest extends J
362                  FailingFibTask f = new FailingFibTask(8);
363                  f.quietlyInvoke();
364                  assertTrue(f.getException() instanceof FJException);
365 <                checkTaskThrew(f, f.getException());
365 >                checkCompletedAbnormally(f, f.getException());
366                  return NoResult;
367              }};
368          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
# Line 378 | Line 380 | public class RecursiveTaskTest extends J
380                      Integer r = f.join();
381                      shouldThrow();
382                  } catch (FJException success) {
383 <                    checkTaskThrew(f, success);
383 >                    checkCompletedAbnormally(f, success);
384                  }
385                  return NoResult;
386              }};
# Line 397 | Line 399 | public class RecursiveTaskTest extends J
399                      Integer r = f.get();
400                      shouldThrow();
401                  } catch (ExecutionException success) {
402 <                    checkTaskThrew(f, success.getCause());
402 >                    Throwable cause = success.getCause();
403 >                    assertTrue(cause instanceof FJException);
404 >                    checkCompletedAbnormally(f, cause);
405                  }
406                  return NoResult;
407              }};
# Line 413 | Line 417 | public class RecursiveTaskTest extends J
417                  FailingFibTask f = new FailingFibTask(8);
418                  assertSame(f, f.fork());
419                  try {
420 <                    Integer r = f.get(5L, SECONDS);
420 >                    Integer r = f.get(LONG_DELAY_MS, MILLISECONDS);
421                      shouldThrow();
422                  } catch (ExecutionException success) {
423 <                    checkTaskThrew(f, success.getCause());
423 >                    Throwable cause = success.getCause();
424 >                    assertTrue(cause instanceof FJException);
425 >                    checkCompletedAbnormally(f, cause);
426                  }
427                  return NoResult;
428              }};
# Line 433 | Line 439 | public class RecursiveTaskTest extends J
439                  assertSame(f, f.fork());
440                  f.quietlyJoin();
441                  assertTrue(f.getException() instanceof FJException);
442 <                checkTaskThrew(f, f.getException());
442 >                checkCompletedAbnormally(f, f.getException());
443                  return NoResult;
444              }};
445          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
# Line 508 | Line 514 | public class RecursiveTaskTest extends J
514                  assertTrue(f.cancel(true));
515                  assertSame(f, f.fork());
516                  try {
517 <                    Integer r = f.get(5L, SECONDS);
517 >                    Integer r = f.get(LONG_DELAY_MS, MILLISECONDS);
518                      shouldThrow();
519                  } catch (CancellationException success) {
520                      checkCancelled(f);
# Line 577 | Line 583 | public class RecursiveTaskTest extends J
583      public void testInForkJoinPool2() {
584          RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
585              public Integer realCompute() {
586 <                assertTrue(!inForkJoinPool());
586 >                assertFalse(inForkJoinPool());
587                  return NoResult;
588              }};
589          assertSame(NoResult, a.invoke());
# Line 594 | Line 600 | public class RecursiveTaskTest extends J
600                  return NoResult;
601              }
602          };
603 <        a.invoke();
603 >        assertSame(NoResult, a.invoke());
604      }
605  
606      /**
# Line 633 | Line 639 | public class RecursiveTaskTest extends J
639                          f.invoke();
640                          shouldThrow();
641                      } catch (FJException success) {
642 <                        checkTaskThrew(f, success);
642 >                        checkCompletedAbnormally(f, success);
643                      }
644                      f.reinitialize();
645                      checkNotDone(f);
# Line 655 | Line 661 | public class RecursiveTaskTest extends J
661                      Integer r = f.invoke();
662                      shouldThrow();
663                  } catch (FJException success) {
664 <                    checkTaskThrew(f, success);
664 >                    checkCompletedAbnormally(f, success);
665                  }
666                  return NoResult;
667              }};
# Line 687 | Line 693 | public class RecursiveTaskTest extends J
693                  FibTask f = new FibTask(8);
694                  FibTask g = new FibTask(9);
695                  invokeAll(f, g);
696 <                checkCompletesNormally(f, 21);
697 <                checkCompletesNormally(g, 34);
696 >                checkCompletedNormally(f, 21);
697 >                checkCompletedNormally(g, 34);
698                  return NoResult;
699              }};
700          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
# Line 702 | Line 708 | public class RecursiveTaskTest extends J
708              public Integer realCompute() {
709                  FibTask f = new FibTask(8);
710                  invokeAll(f);
711 <                checkCompletesNormally(f, 21);
711 >                checkCompletedNormally(f, 21);
712                  return NoResult;
713              }};
714          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
# Line 718 | Line 724 | public class RecursiveTaskTest extends J
724                  FibTask g = new FibTask(9);
725                  FibTask h = new FibTask(7);
726                  invokeAll(f, g, h);
727 <                checkCompletesNormally(f, 21);
728 <                checkCompletesNormally(g, 34);
729 <                checkCompletesNormally(h, 13);
727 >                assertTrue(f.isDone());
728 >                assertTrue(g.isDone());
729 >                assertTrue(h.isDone());
730 >                checkCompletedNormally(f, 21);
731 >                checkCompletedNormally(g, 34);
732 >                checkCompletedNormally(h, 13);
733                  return NoResult;
734              }};
735          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
# Line 740 | Line 749 | public class RecursiveTaskTest extends J
749                  set.add(g);
750                  set.add(h);
751                  invokeAll(set);
752 <                checkCompletesNormally(f, 21);
753 <                checkCompletesNormally(g, 34);
754 <                checkCompletesNormally(h, 13);
752 >                assertTrue(f.isDone());
753 >                assertTrue(g.isDone());
754 >                assertTrue(h.isDone());
755 >                checkCompletedNormally(f, 21);
756 >                checkCompletedNormally(g, 34);
757 >                checkCompletedNormally(h, 13);
758                  return NoResult;
759              }};
760          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
761      }
762  
751
763      /**
764       * invokeAll(tasks) with any null task throws NPE
765       */
# Line 779 | Line 790 | public class RecursiveTaskTest extends J
790                      invokeAll(f, g);
791                      shouldThrow();
792                  } catch (FJException success) {
793 <                    checkTaskThrew(g, success);
793 >                    checkCompletedAbnormally(g, success);
794                  }
795                  return NoResult;
796              }};
# Line 797 | Line 808 | public class RecursiveTaskTest extends J
808                      invokeAll(g);
809                      shouldThrow();
810                  } catch (FJException success) {
811 <                    checkTaskThrew(g, success);
811 >                    checkCompletedAbnormally(g, success);
812                  }
813                  return NoResult;
814              }};
# Line 817 | Line 828 | public class RecursiveTaskTest extends J
828                      invokeAll(f, g, h);
829                      shouldThrow();
830                  } catch (FJException success) {
831 <                    checkTaskThrew(g, success);
831 >                    checkCompletedAbnormally(g, success);
832                  }
833                  return NoResult;
834              }};
# Line 841 | Line 852 | public class RecursiveTaskTest extends J
852                      invokeAll(set);
853                      shouldThrow();
854                  } catch (FJException success) {
855 <                    checkTaskThrew(f, success);
855 >                    checkCompletedAbnormally(f, success);
856                  }
857                  return NoResult;
858              }};
# Line 883 | Line 894 | public class RecursiveTaskTest extends J
894                  assertSame(f, f.fork());
895                  assertTrue(getSurplusQueuedTaskCount() > 0);
896                  helpQuiesce();
897 +                assertEquals(0, getSurplusQueuedTaskCount());
898                  checkCompletedNormally(f, 21);
899                  checkCompletedNormally(g, 34);
900                  checkCompletedNormally(h, 13);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines