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.30 by jsr166, Wed Dec 31 16:44:02 2014 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.*;
8   import java.util.concurrent.CancellationException;
9   import java.util.concurrent.ExecutionException;
10   import java.util.concurrent.ForkJoinPool;
11 < import java.util.concurrent.ForkJoinWorkerThread;
11 > import java.util.concurrent.ForkJoinTask;
12   import java.util.concurrent.RecursiveTask;
13 import java.util.concurrent.TimeUnit;
13   import java.util.concurrent.TimeoutException;
14   import static java.util.concurrent.TimeUnit.SECONDS;
15   import java.util.HashSet;
# Line 59 | Line 58 | public class RecursiveTaskTest extends J
58          assertNull(a.getException());
59          assertNull(a.getRawResult());
60  
61 <        if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) {
61 >        if (! ForkJoinTask.inForkJoinPool()) {
62              Thread.currentThread().interrupt();
63              try {
64                  a.get();
# Line 90 | Line 89 | public class RecursiveTaskTest extends J
89          assertNull(a.getException());
90          assertSame(expected, a.getRawResult());
91          assertSame(expected, a.join());
92 +        assertFalse(a.cancel(false));
93 +        assertFalse(a.cancel(true));
94          try {
95              assertSame(expected, a.get());
96          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 145 | Line 146 | public class RecursiveTaskTest extends J
146          } catch (Throwable fail) { threadUnexpectedException(fail); }
147      }
148  
149 <    void checkTaskThrew(RecursiveTask a, Throwable t) {
149 >    void checkCompletedAbnormally(RecursiveTask a, Throwable t) {
150          assertTrue(a.isDone());
151          assertFalse(a.isCancelled());
152          assertFalse(a.isCompletedNormally());
153          assertTrue(a.isCompletedAbnormally());
154 <        assertSame(t, a.getException());
154 >        assertSame(t.getClass(), a.getException().getClass());
155          assertNull(a.getRawResult());
156 +        assertFalse(a.cancel(false));
157 +        assertFalse(a.cancel(true));
158  
159          try {
160              a.join();
161              shouldThrow();
162          } catch (Throwable expected) {
163 <            assertSame(t, expected);
163 >            assertSame(t.getClass(), expected.getClass());
164          }
165  
166          try {
167              a.get();
168              shouldThrow();
169          } catch (ExecutionException success) {
170 <            assertSame(t, success.getCause());
170 >            assertSame(t.getClass(), success.getCause().getClass());
171          } catch (Throwable fail) { threadUnexpectedException(fail); }
172  
173          try {
174              a.get(5L, SECONDS);
175              shouldThrow();
176          } catch (ExecutionException success) {
177 <            assertSame(t, success.getCause());
177 >            assertSame(t.getClass(), success.getCause().getClass());
178          } catch (Throwable fail) { threadUnexpectedException(fail); }
179      }
180  
181 <    static final class FJException extends RuntimeException {
182 <        FJException() { super(); }
181 >    public static final class FJException extends RuntimeException {
182 >        public FJException() { super(); }
183      }
184  
185      // An invalid return value for Fib
# Line 314 | Line 317 | public class RecursiveTaskTest extends J
317          assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
318      }
319  
317
320      /**
321       * helpQuiesce returns when tasks are complete.
322       * getQueuedTaskCount returns 0 when quiescent
# Line 324 | Line 326 | public class RecursiveTaskTest extends J
326              public Integer realCompute() {
327                  FibTask f = new FibTask(8);
328                  assertSame(f, f.fork());
329 <                f.helpQuiesce();
329 >                helpQuiesce();
330                  assertEquals(0, getQueuedTaskCount());
331                  checkCompletedNormally(f, 21);
332                  return NoResult;
# Line 332 | Line 334 | public class RecursiveTaskTest extends J
334          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
335      }
336  
335
337      /**
338       * invoke task throws exception when task completes abnormally
339       */
# Line 344 | Line 345 | public class RecursiveTaskTest extends J
345                      f.invoke();
346                      shouldThrow();
347                  } catch (FJException success) {
348 <                    checkTaskThrew(f, success);
348 >                    checkCompletedAbnormally(f, success);
349                  }
350                  return NoResult;
351              }};
# Line 360 | Line 361 | public class RecursiveTaskTest extends J
361                  FailingFibTask f = new FailingFibTask(8);
362                  f.quietlyInvoke();
363                  assertTrue(f.getException() instanceof FJException);
364 <                checkTaskThrew(f, f.getException());
364 >                checkCompletedAbnormally(f, f.getException());
365                  return NoResult;
366              }};
367          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
# Line 378 | Line 379 | public class RecursiveTaskTest extends J
379                      Integer r = f.join();
380                      shouldThrow();
381                  } catch (FJException success) {
382 <                    checkTaskThrew(f, success);
382 >                    checkCompletedAbnormally(f, success);
383                  }
384                  return NoResult;
385              }};
# Line 397 | Line 398 | public class RecursiveTaskTest extends J
398                      Integer r = f.get();
399                      shouldThrow();
400                  } catch (ExecutionException success) {
401 <                    checkTaskThrew(f, success.getCause());
401 >                    Throwable cause = success.getCause();
402 >                    assertTrue(cause instanceof FJException);
403 >                    checkCompletedAbnormally(f, cause);
404                  }
405                  return NoResult;
406              }};
# Line 416 | Line 419 | public class RecursiveTaskTest extends J
419                      Integer r = f.get(5L, SECONDS);
420                      shouldThrow();
421                  } catch (ExecutionException success) {
422 <                    checkTaskThrew(f, success.getCause());
422 >                    Throwable cause = success.getCause();
423 >                    assertTrue(cause instanceof FJException);
424 >                    checkCompletedAbnormally(f, cause);
425                  }
426                  return NoResult;
427              }};
# Line 433 | Line 438 | public class RecursiveTaskTest extends J
438                  assertSame(f, f.fork());
439                  f.quietlyJoin();
440                  assertTrue(f.getException() instanceof FJException);
441 <                checkTaskThrew(f, f.getException());
441 >                checkCompletedAbnormally(f, f.getException());
442                  return NoResult;
443              }};
444          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
# Line 577 | Line 582 | public class RecursiveTaskTest extends J
582      public void testInForkJoinPool2() {
583          RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
584              public Integer realCompute() {
585 <                assertTrue(!inForkJoinPool());
585 >                assertFalse(inForkJoinPool());
586                  return NoResult;
587              }};
588          assertSame(NoResult, a.invoke());
# Line 594 | Line 599 | public class RecursiveTaskTest extends J
599                  return NoResult;
600              }
601          };
602 <        a.invoke();
602 >        assertSame(NoResult, a.invoke());
603      }
604  
605      /**
# Line 633 | Line 638 | public class RecursiveTaskTest extends J
638                          f.invoke();
639                          shouldThrow();
640                      } catch (FJException success) {
641 <                        checkTaskThrew(f, success);
641 >                        checkCompletedAbnormally(f, success);
642                      }
643                      f.reinitialize();
644                      checkNotDone(f);
# Line 655 | Line 660 | public class RecursiveTaskTest extends J
660                      Integer r = f.invoke();
661                      shouldThrow();
662                  } catch (FJException success) {
663 <                    checkTaskThrew(f, success);
663 >                    checkCompletedAbnormally(f, success);
664                  }
665                  return NoResult;
666              }};
# Line 754 | Line 759 | public class RecursiveTaskTest extends J
759          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
760      }
761  
757
762      /**
763       * invokeAll(tasks) with any null task throws NPE
764       */
# Line 785 | Line 789 | public class RecursiveTaskTest extends J
789                      invokeAll(f, g);
790                      shouldThrow();
791                  } catch (FJException success) {
792 <                    checkTaskThrew(g, success);
792 >                    checkCompletedAbnormally(g, success);
793                  }
794                  return NoResult;
795              }};
# Line 803 | Line 807 | public class RecursiveTaskTest extends J
807                      invokeAll(g);
808                      shouldThrow();
809                  } catch (FJException success) {
810 <                    checkTaskThrew(g, success);
810 >                    checkCompletedAbnormally(g, success);
811                  }
812                  return NoResult;
813              }};
# Line 823 | Line 827 | public class RecursiveTaskTest extends J
827                      invokeAll(f, g, h);
828                      shouldThrow();
829                  } catch (FJException success) {
830 <                    checkTaskThrew(g, success);
830 >                    checkCompletedAbnormally(g, success);
831                  }
832                  return NoResult;
833              }};
# Line 847 | Line 851 | public class RecursiveTaskTest extends J
851                      invokeAll(set);
852                      shouldThrow();
853                  } catch (FJException success) {
854 <                    checkTaskThrew(f, success);
854 >                    checkCompletedAbnormally(f, success);
855                  }
856                  return NoResult;
857              }};
# Line 889 | Line 893 | public class RecursiveTaskTest extends J
893                  assertSame(f, f.fork());
894                  assertTrue(getSurplusQueuedTaskCount() > 0);
895                  helpQuiesce();
896 +                assertEquals(0, getSurplusQueuedTaskCount());
897                  checkCompletedNormally(f, 21);
898                  checkCompletedNormally(g, 34);
899                  checkCompletedNormally(h, 13);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines