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.27 by jsr166, Tue Mar 15 19:47:07 2011 UTC vs.
Revision 1.38 by jsr166, Wed Nov 8 02:21:43 2017 UTC

# Line 4 | Line 4
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.ForkJoinTask;
12 import java.util.concurrent.ForkJoinWorkerThread;
14   import java.util.concurrent.RecursiveTask;
14 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 40 | 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;
50        } finally {
51            joinPool(pool);
51          }
52      }
53  
# Line 70 | 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 95 | Line 94 | public class RecursiveTaskTest extends J
94          assertFalse(a.cancel(true));
95          try {
96              assertSame(expected, a.get());
97 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
99 <        try {
100 <            assertSame(expected, a.get(5L, SECONDS));
97 >            assertSame(expected, a.get(randomTimeout(), randomTimeUnit()));
98          } catch (Throwable fail) { threadUnexpectedException(fail); }
99      }
100  
# Line 142 | 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); }
# Line 173 | Line 170 | public class RecursiveTaskTest extends J
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.getClass(), success.getCause().getClass());
# Line 184 | Line 181 | public class RecursiveTaskTest extends J
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 205 | 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 294 | 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 319 | Line 316 | public class RecursiveTaskTest extends J
316          assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
317      }
318  
322
319      /**
320       * helpQuiesce returns when tasks are complete.
321       * getQueuedTaskCount returns 0 when quiescent
# Line 329 | 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 337 | Line 335 | public class RecursiveTaskTest extends J
335          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
336      }
337  
340
338      /**
339       * invoke task throws exception when task completes abnormally
340       */
# Line 420 | 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                      Throwable cause = success.getCause();
# Line 517 | 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 763 | Line 760 | public class RecursiveTaskTest extends J
760          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
761      }
762  
766
763      /**
764       * invokeAll(tasks) with any null task throws NPE
765       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines