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.42 by dl, Tue Jan 26 13:33:06 2021 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  
54 <    void checkNotDone(RecursiveTask a) {
54 >    void checkNotDone(RecursiveTask<?> a) {
55          assertFalse(a.isDone());
56          assertFalse(a.isCompletedNormally());
57          assertFalse(a.isCompletedAbnormally());
# 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); }
83      }
84  
85 <    <T> void checkCompletedNormally(RecursiveTask<T> a, T expected) {
85 >    <T> void checkCompletedNormally(RecursiveTask<T> a, T expectedValue) {
86          assertTrue(a.isDone());
87          assertFalse(a.isCancelled());
88          assertTrue(a.isCompletedNormally());
89          assertFalse(a.isCompletedAbnormally());
90          assertNull(a.getException());
91 <        assertSame(expected, a.getRawResult());
92 <        assertSame(expected, a.join());
91 >        assertSame(expectedValue, a.getRawResult());
92 >        assertSame(expectedValue, a.join());
93          assertFalse(a.cancel(false));
94          assertFalse(a.cancel(true));
95 +
96 +        T v1 = null, v2 = null;
97          try {
98 <            assertSame(expected, a.get());
99 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
99 <        try {
100 <            assertSame(expected, a.get(5L, SECONDS));
98 >            v1 = a.get();
99 >            v2 = a.get(randomTimeout(), randomTimeUnit());
100          } catch (Throwable fail) { threadUnexpectedException(fail); }
101 +        assertSame(expectedValue, v1);
102 +        assertSame(expectedValue, v2);
103      }
104  
105      /**
106       * Waits for the task to complete, and checks that when it does,
107       * it will have an Integer result equals to the given int.
108       */
109 <    void checkCompletesNormally(RecursiveTask<Integer> a, int expected) {
109 >    void checkCompletesNormally(RecursiveTask<Integer> a, int expectedValue) {
110          Integer r = a.join();
111 <        assertEquals(expected, (int) r);
111 >        assertEquals(expectedValue, (int) r);
112          checkCompletedNormally(a, r);
113      }
114  
# Line 115 | Line 116 | public class RecursiveTaskTest extends J
116       * Like checkCompletesNormally, but verifies that the task has
117       * already completed.
118       */
119 <    void checkCompletedNormally(RecursiveTask<Integer> a, int expected) {
119 >    void checkCompletedNormally(RecursiveTask<Integer> a, int expectedValue) {
120          Integer r = a.getRawResult();
121 <        assertEquals(expected, (int) r);
121 >        assertEquals(expectedValue, (int) r);
122          checkCompletedNormally(a, r);
123      }
124  
125 <    void checkCancelled(RecursiveTask a) {
125 >    void checkCancelled(RecursiveTask<?> a) {
126          assertTrue(a.isDone());
127          assertTrue(a.isCancelled());
128          assertFalse(a.isCompletedNormally());
# Line 142 | Line 143 | public class RecursiveTaskTest extends J
143          } catch (Throwable fail) { threadUnexpectedException(fail); }
144  
145          try {
146 <            a.get(5L, SECONDS);
146 >            a.get(randomTimeout(), randomTimeUnit());
147              shouldThrow();
148          } catch (CancellationException success) {
149          } catch (Throwable fail) { threadUnexpectedException(fail); }
150      }
151  
152 <    void checkCompletedAbnormally(RecursiveTask a, Throwable t) {
152 >    void checkCompletedAbnormally(RecursiveTask<?> a, Throwable t) {
153          assertTrue(a.isDone());
154          assertFalse(a.isCancelled());
155          assertFalse(a.isCompletedNormally());
# Line 173 | Line 174 | public class RecursiveTaskTest extends J
174          } catch (Throwable fail) { threadUnexpectedException(fail); }
175  
176          try {
177 <            a.get(5L, SECONDS);
177 >            a.get(randomTimeout(), randomTimeUnit());
178              shouldThrow();
179          } catch (ExecutionException success) {
180              assertSame(t.getClass(), success.getCause().getClass());
# Line 184 | Line 185 | public class RecursiveTaskTest extends J
185          public FJException() { super(); }
186      }
187  
188 <    // An invalid return value for Fib
188 >    /** An invalid return value for Fib. */
189      static final Integer NoResult = Integer.valueOf(-17);
190  
191 <    // A simple recursive task for testing
191 >    /** A simple recursive task for testing. */
192      final class FibTask extends CheckedRecursiveTask<Integer> {
193          final int number;
194          FibTask(int n) { number = n; }
# Line 197 | Line 198 | public class RecursiveTaskTest extends J
198                  return n;
199              FibTask f1 = new FibTask(n - 1);
200              f1.fork();
201 <            return (new FibTask(n - 2)).compute() + f1.join();
201 >            return new FibTask(n - 2).compute() + f1.join();
202          }
203  
204          public void publicSetRawResult(Integer result) {
# Line 205 | Line 206 | public class RecursiveTaskTest extends J
206          }
207      }
208  
209 <    // A recursive action failing in base case
209 >    /** A recursive action failing in base case. */
210      final class FailingFibTask extends RecursiveTask<Integer> {
211          final int number;
212          int result;
# Line 216 | Line 217 | public class RecursiveTaskTest extends J
217                  throw new FJException();
218              FailingFibTask f1 = new FailingFibTask(n - 1);
219              f1.fork();
220 <            return (new FibTask(n - 2)).compute() + f1.join();
220 >            return new FibTask(n - 2).compute() + f1.join();
221          }
222      }
223  
# Line 294 | Line 295 | public class RecursiveTaskTest extends J
295              public Integer realCompute() throws Exception {
296                  FibTask f = new FibTask(8);
297                  assertSame(f, f.fork());
298 <                Integer r = f.get(5L, SECONDS);
298 >                Integer r = f.get(LONG_DELAY_MS, MILLISECONDS);
299                  assertEquals(21, (int) r);
300                  checkCompletedNormally(f, r);
301                  return r;
# Line 319 | Line 320 | public class RecursiveTaskTest extends J
320          assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
321      }
322  
322
323      /**
324       * helpQuiesce returns when tasks are complete.
325       * getQueuedTaskCount returns 0 when quiescent
# Line 329 | 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 >                while (!f.isDone()) // wait out race
334 >                    ;
335                  assertEquals(0, getQueuedTaskCount());
336                  checkCompletedNormally(f, 21);
337                  return NoResult;
# Line 337 | Line 339 | public class RecursiveTaskTest extends J
339          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
340      }
341  
340
342      /**
343       * invoke task throws exception when task completes abnormally
344       */
# Line 380 | Line 381 | public class RecursiveTaskTest extends J
381                  FailingFibTask f = new FailingFibTask(8);
382                  assertSame(f, f.fork());
383                  try {
384 <                    Integer r = f.join();
384 >                    f.join();
385                      shouldThrow();
386                  } catch (FJException success) {
387                      checkCompletedAbnormally(f, success);
# Line 399 | Line 400 | public class RecursiveTaskTest extends J
400                  FailingFibTask f = new FailingFibTask(8);
401                  assertSame(f, f.fork());
402                  try {
403 <                    Integer r = f.get();
403 >                    f.get();
404                      shouldThrow();
405                  } catch (ExecutionException success) {
406                      Throwable cause = success.getCause();
# Line 420 | Line 421 | public class RecursiveTaskTest extends J
421                  FailingFibTask f = new FailingFibTask(8);
422                  assertSame(f, f.fork());
423                  try {
424 <                    Integer r = f.get(5L, SECONDS);
424 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
425                      shouldThrow();
426                  } catch (ExecutionException success) {
427                      Throwable cause = success.getCause();
# Line 457 | Line 458 | public class RecursiveTaskTest extends J
458                  FibTask f = new FibTask(8);
459                  assertTrue(f.cancel(true));
460                  try {
461 <                    Integer r = f.invoke();
461 >                    f.invoke();
462                      shouldThrow();
463                  } catch (CancellationException success) {
464                      checkCancelled(f);
# Line 477 | Line 478 | public class RecursiveTaskTest extends J
478                  assertTrue(f.cancel(true));
479                  assertSame(f, f.fork());
480                  try {
481 <                    Integer r = f.join();
481 >                    f.join();
482                      shouldThrow();
483                  } catch (CancellationException success) {
484                      checkCancelled(f);
# Line 497 | Line 498 | public class RecursiveTaskTest extends J
498                  assertTrue(f.cancel(true));
499                  assertSame(f, f.fork());
500                  try {
501 <                    Integer r = f.get();
501 >                    f.get();
502                      shouldThrow();
503                  } catch (CancellationException success) {
504                      checkCancelled(f);
# Line 517 | Line 518 | public class RecursiveTaskTest extends J
518                  assertTrue(f.cancel(true));
519                  assertSame(f, f.fork());
520                  try {
521 <                    Integer r = f.get(5L, SECONDS);
521 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
522                      shouldThrow();
523                  } catch (CancellationException success) {
524                      checkCancelled(f);
# Line 661 | Line 662 | public class RecursiveTaskTest extends J
662                  FibTask f = new FibTask(8);
663                  f.completeExceptionally(new FJException());
664                  try {
665 <                    Integer r = f.invoke();
665 >                    f.invoke();
666                      shouldThrow();
667                  } catch (FJException success) {
668                      checkCompletedAbnormally(f, success);
# Line 747 | Line 748 | public class RecursiveTaskTest extends J
748                  FibTask f = new FibTask(8);
749                  FibTask g = new FibTask(9);
750                  FibTask h = new FibTask(7);
751 <                HashSet set = new HashSet();
751 >                HashSet<ForkJoinTask<?>> set = new HashSet<ForkJoinTask<?>>();
752                  set.add(f);
753                  set.add(g);
754                  set.add(h);
# Line 763 | Line 764 | public class RecursiveTaskTest extends J
764          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
765      }
766  
766
767      /**
768       * invokeAll(tasks) with any null task throws NPE
769       */
# Line 848 | Line 848 | public class RecursiveTaskTest extends J
848                  FailingFibTask f = new FailingFibTask(8);
849                  FibTask g = new FibTask(9);
850                  FibTask h = new FibTask(7);
851 <                HashSet set = new HashSet();
851 >                HashSet<ForkJoinTask<?>> set = new HashSet<ForkJoinTask<?>>();
852                  set.add(f);
853                  set.add(g);
854                  set.add(h);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines