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.39 by jsr166, Sun Jul 22 21:13:32 2018 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); }
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());
93 <        try {
94 <            assertSame(expected, a.get());
95 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
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(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 112 | 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  
# Line 139 | 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 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);
177 >            a.get(randomTimeout(), randomTimeUnit());
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
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 200 | 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 289 | 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 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 >                while (!f.isDone()) // wait out race
334 >                    ;
335                  assertEquals(0, getQueuedTaskCount());
336                  checkCompletedNormally(f, 21);
337                  return NoResult;
# Line 332 | Line 339 | public class RecursiveTaskTest extends J
339          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
340      }
341  
335
342      /**
343       * invoke task throws exception when task completes abnormally
344       */
# Line 344 | Line 350 | public class RecursiveTaskTest extends J
350                      f.invoke();
351                      shouldThrow();
352                  } catch (FJException success) {
353 <                    checkTaskThrew(f, success);
353 >                    checkCompletedAbnormally(f, success);
354                  }
355                  return NoResult;
356              }};
# Line 360 | Line 366 | public class RecursiveTaskTest extends J
366                  FailingFibTask f = new FailingFibTask(8);
367                  f.quietlyInvoke();
368                  assertTrue(f.getException() instanceof FJException);
369 <                checkTaskThrew(f, f.getException());
369 >                checkCompletedAbnormally(f, f.getException());
370                  return NoResult;
371              }};
372          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
# Line 378 | Line 384 | public class RecursiveTaskTest extends J
384                      Integer r = f.join();
385                      shouldThrow();
386                  } catch (FJException success) {
387 <                    checkTaskThrew(f, success);
387 >                    checkCompletedAbnormally(f, success);
388                  }
389                  return NoResult;
390              }};
# Line 397 | Line 403 | public class RecursiveTaskTest extends J
403                      Integer r = f.get();
404                      shouldThrow();
405                  } catch (ExecutionException success) {
406 <                    checkTaskThrew(f, success.getCause());
406 >                    Throwable cause = success.getCause();
407 >                    assertTrue(cause instanceof FJException);
408 >                    checkCompletedAbnormally(f, cause);
409                  }
410                  return NoResult;
411              }};
# Line 413 | 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 >                    Integer r = f.get(LONG_DELAY_MS, MILLISECONDS);
425                      shouldThrow();
426                  } catch (ExecutionException success) {
427 <                    checkTaskThrew(f, success.getCause());
427 >                    Throwable cause = success.getCause();
428 >                    assertTrue(cause instanceof FJException);
429 >                    checkCompletedAbnormally(f, cause);
430                  }
431                  return NoResult;
432              }};
# Line 433 | Line 443 | public class RecursiveTaskTest extends J
443                  assertSame(f, f.fork());
444                  f.quietlyJoin();
445                  assertTrue(f.getException() instanceof FJException);
446 <                checkTaskThrew(f, f.getException());
446 >                checkCompletedAbnormally(f, f.getException());
447                  return NoResult;
448              }};
449          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
# Line 508 | 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 >                    Integer r = f.get(LONG_DELAY_MS, MILLISECONDS);
522                      shouldThrow();
523                  } catch (CancellationException success) {
524                      checkCancelled(f);
# Line 577 | Line 587 | public class RecursiveTaskTest extends J
587      public void testInForkJoinPool2() {
588          RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
589              public Integer realCompute() {
590 <                assertTrue(!inForkJoinPool());
590 >                assertFalse(inForkJoinPool());
591                  return NoResult;
592              }};
593          assertSame(NoResult, a.invoke());
# Line 594 | Line 604 | public class RecursiveTaskTest extends J
604                  return NoResult;
605              }
606          };
607 <        a.invoke();
607 >        assertSame(NoResult, a.invoke());
608      }
609  
610      /**
# Line 633 | Line 643 | public class RecursiveTaskTest extends J
643                          f.invoke();
644                          shouldThrow();
645                      } catch (FJException success) {
646 <                        checkTaskThrew(f, success);
646 >                        checkCompletedAbnormally(f, success);
647                      }
648                      f.reinitialize();
649                      checkNotDone(f);
# Line 655 | Line 665 | public class RecursiveTaskTest extends J
665                      Integer r = f.invoke();
666                      shouldThrow();
667                  } catch (FJException success) {
668 <                    checkTaskThrew(f, success);
668 >                    checkCompletedAbnormally(f, success);
669                  }
670                  return NoResult;
671              }};
# Line 754 | Line 764 | public class RecursiveTaskTest extends J
764          assertSame(NoResult, testInvokeOnPool(mainPool(), a));
765      }
766  
757
767      /**
768       * invokeAll(tasks) with any null task throws NPE
769       */
# Line 785 | Line 794 | public class RecursiveTaskTest extends J
794                      invokeAll(f, g);
795                      shouldThrow();
796                  } catch (FJException success) {
797 <                    checkTaskThrew(g, success);
797 >                    checkCompletedAbnormally(g, success);
798                  }
799                  return NoResult;
800              }};
# Line 803 | Line 812 | public class RecursiveTaskTest extends J
812                      invokeAll(g);
813                      shouldThrow();
814                  } catch (FJException success) {
815 <                    checkTaskThrew(g, success);
815 >                    checkCompletedAbnormally(g, success);
816                  }
817                  return NoResult;
818              }};
# Line 823 | Line 832 | public class RecursiveTaskTest extends J
832                      invokeAll(f, g, h);
833                      shouldThrow();
834                  } catch (FJException success) {
835 <                    checkTaskThrew(g, success);
835 >                    checkCompletedAbnormally(g, success);
836                  }
837                  return NoResult;
838              }};
# Line 847 | Line 856 | public class RecursiveTaskTest extends J
856                      invokeAll(set);
857                      shouldThrow();
858                  } catch (FJException success) {
859 <                    checkTaskThrew(f, success);
859 >                    checkCompletedAbnormally(f, success);
860                  }
861                  return NoResult;
862              }};
# Line 889 | Line 898 | public class RecursiveTaskTest extends J
898                  assertSame(f, f.fork());
899                  assertTrue(getSurplusQueuedTaskCount() > 0);
900                  helpQuiesce();
901 +                assertEquals(0, getSurplusQueuedTaskCount());
902                  checkCompletedNormally(f, 21);
903                  checkCompletedNormally(g, 34);
904                  checkCompletedNormally(h, 13);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines