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.21 by jsr166, Sun Nov 21 08:35:40 2010 UTC vs.
Revision 1.32 by jsr166, Sat Apr 25 04:55:31 2015 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.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.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 59 | Line 61 | public class RecursiveTaskTest extends J
61          assertNull(a.getException());
62          assertNull(a.getRawResult());
63  
64 <        if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) {
64 >        if (! ForkJoinTask.inForkJoinPool()) {
65              Thread.currentThread().interrupt();
66              try {
67                  a.get();
# Line 90 | Line 92 | public class RecursiveTaskTest extends J
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); }
# Line 145 | Line 149 | public class RecursiveTaskTest extends J
149          } catch (Throwable fail) { threadUnexpectedException(fail); }
150      }
151  
152 <    void checkTaskThrew(RecursiveTask a, Throwable t) {
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, a.getException());
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, expected);
166 >            assertSame(t.getClass(), expected.getClass());
167          }
168  
169          try {
170              a.get();
171              shouldThrow();
172          } catch (ExecutionException success) {
173 <            assertSame(t, success.getCause());
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, success.getCause());
180 >            assertSame(t.getClass(), success.getCause().getClass());
181          } catch (Throwable fail) { threadUnexpectedException(fail); }
182      }
183  
184 <    static final class FJException extends RuntimeException {
185 <        FJException() { super(); }
184 >    public static final class FJException extends RuntimeException {
185 >        public FJException() { super(); }
186      }
187  
188      // An invalid return value for Fib
# Line 314 | Line 320 | public class RecursiveTaskTest extends J
320          assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
321      }
322  
317
323      /**
324       * helpQuiesce returns when tasks are complete.
325       * getQueuedTaskCount returns 0 when quiescent
# Line 324 | Line 329 | public class RecursiveTaskTest extends J
329              public Integer realCompute() {
330                  FibTask f = new FibTask(8);
331                  assertSame(f, f.fork());
332 <                f.helpQuiesce();
332 >                helpQuiesce();
333                  assertEquals(0, getQueuedTaskCount());
334                  checkCompletedNormally(f, 21);
335                  return NoResult;
# Line 332 | Line 337 | public class RecursiveTaskTest extends J
337          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
338      }
339  
335
340      /**
341       * invoke task throws exception when task completes abnormally
342       */
# Line 344 | Line 348 | public class RecursiveTaskTest extends J
348                      f.invoke();
349                      shouldThrow();
350                  } catch (FJException success) {
351 <                    checkTaskThrew(f, success);
351 >                    checkCompletedAbnormally(f, success);
352                  }
353                  return NoResult;
354              }};
# Line 360 | Line 364 | public class RecursiveTaskTest extends J
364                  FailingFibTask f = new FailingFibTask(8);
365                  f.quietlyInvoke();
366                  assertTrue(f.getException() instanceof FJException);
367 <                checkTaskThrew(f, f.getException());
367 >                checkCompletedAbnormally(f, f.getException());
368                  return NoResult;
369              }};
370          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
# Line 378 | Line 382 | public class RecursiveTaskTest extends J
382                      Integer r = f.join();
383                      shouldThrow();
384                  } catch (FJException success) {
385 <                    checkTaskThrew(f, success);
385 >                    checkCompletedAbnormally(f, success);
386                  }
387                  return NoResult;
388              }};
# Line 397 | Line 401 | public class RecursiveTaskTest extends J
401                      Integer r = f.get();
402                      shouldThrow();
403                  } catch (ExecutionException success) {
404 <                    checkTaskThrew(f, success.getCause());
404 >                    Throwable cause = success.getCause();
405 >                    assertTrue(cause instanceof FJException);
406 >                    checkCompletedAbnormally(f, cause);
407                  }
408                  return NoResult;
409              }};
# Line 416 | Line 422 | public class RecursiveTaskTest extends J
422                      Integer r = f.get(5L, SECONDS);
423                      shouldThrow();
424                  } catch (ExecutionException success) {
425 <                    checkTaskThrew(f, success.getCause());
425 >                    Throwable cause = success.getCause();
426 >                    assertTrue(cause instanceof FJException);
427 >                    checkCompletedAbnormally(f, cause);
428                  }
429                  return NoResult;
430              }};
# Line 433 | Line 441 | public class RecursiveTaskTest extends J
441                  assertSame(f, f.fork());
442                  f.quietlyJoin();
443                  assertTrue(f.getException() instanceof FJException);
444 <                checkTaskThrew(f, f.getException());
444 >                checkCompletedAbnormally(f, f.getException());
445                  return NoResult;
446              }};
447          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
# Line 577 | Line 585 | public class RecursiveTaskTest extends J
585      public void testInForkJoinPool2() {
586          RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
587              public Integer realCompute() {
588 <                assertTrue(!inForkJoinPool());
588 >                assertFalse(inForkJoinPool());
589                  return NoResult;
590              }};
591          assertSame(NoResult, a.invoke());
# Line 594 | Line 602 | public class RecursiveTaskTest extends J
602                  return NoResult;
603              }
604          };
605 <        a.invoke();
605 >        assertSame(NoResult, a.invoke());
606      }
607  
608      /**
# Line 633 | Line 641 | public class RecursiveTaskTest extends J
641                          f.invoke();
642                          shouldThrow();
643                      } catch (FJException success) {
644 <                        checkTaskThrew(f, success);
644 >                        checkCompletedAbnormally(f, success);
645                      }
646                      f.reinitialize();
647                      checkNotDone(f);
# Line 655 | Line 663 | public class RecursiveTaskTest extends J
663                      Integer r = f.invoke();
664                      shouldThrow();
665                  } catch (FJException success) {
666 <                    checkTaskThrew(f, success);
666 >                    checkCompletedAbnormally(f, success);
667                  }
668                  return NoResult;
669              }};
# Line 754 | Line 762 | public class RecursiveTaskTest extends J
762          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
763      }
764  
757
765      /**
766       * invokeAll(tasks) with any null task throws NPE
767       */
# Line 785 | Line 792 | public class RecursiveTaskTest extends J
792                      invokeAll(f, g);
793                      shouldThrow();
794                  } catch (FJException success) {
795 <                    checkTaskThrew(g, success);
795 >                    checkCompletedAbnormally(g, success);
796                  }
797                  return NoResult;
798              }};
# Line 803 | Line 810 | public class RecursiveTaskTest extends J
810                      invokeAll(g);
811                      shouldThrow();
812                  } catch (FJException success) {
813 <                    checkTaskThrew(g, success);
813 >                    checkCompletedAbnormally(g, success);
814                  }
815                  return NoResult;
816              }};
# Line 823 | Line 830 | public class RecursiveTaskTest extends J
830                      invokeAll(f, g, h);
831                      shouldThrow();
832                  } catch (FJException success) {
833 <                    checkTaskThrew(g, success);
833 >                    checkCompletedAbnormally(g, success);
834                  }
835                  return NoResult;
836              }};
# Line 847 | Line 854 | public class RecursiveTaskTest extends J
854                      invokeAll(set);
855                      shouldThrow();
856                  } catch (FJException success) {
857 <                    checkTaskThrew(f, success);
857 >                    checkCompletedAbnormally(f, success);
858                  }
859                  return NoResult;
860              }};
# Line 889 | Line 896 | public class RecursiveTaskTest extends J
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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines