ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
(Generate patch)

Comparing jsr166/src/test/tck/RecursiveActionTest.java (file contents):
Revision 1.26 by jsr166, Tue Nov 23 07:38:40 2010 UTC vs.
Revision 1.32 by dl, Tue May 31 10:28:06 2011 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.*;
# Line 9 | Line 9 | import java.util.concurrent.Cancellation
9   import java.util.concurrent.SynchronousQueue;
10   import java.util.concurrent.ExecutionException;
11   import java.util.concurrent.ForkJoinPool;
12 + import java.util.concurrent.ForkJoinTask;
13   import java.util.concurrent.ForkJoinWorkerThread;
14   import java.util.concurrent.RecursiveAction;
15   import java.util.concurrent.TimeUnit;
# Line 60 | Line 61 | public class RecursiveActionTest extends
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 133 | Line 134 | public class RecursiveActionTest extends
134          assertFalse(a.isCancelled());
135          assertFalse(a.isCompletedNormally());
136          assertTrue(a.isCompletedAbnormally());
137 <        assertSame(t, a.getException());
137 >        assertSame(t.getClass(), a.getException().getClass());
138          assertNull(a.getRawResult());
139          assertFalse(a.cancel(false));
140          assertFalse(a.cancel(true));
# Line 142 | Line 143 | public class RecursiveActionTest extends
143              a.join();
144              shouldThrow();
145          } catch (Throwable expected) {
146 <            assertSame(t, expected);
146 >            assertSame(expected.getClass(), t.getClass());
147          }
148  
149          try {
150              a.get();
151              shouldThrow();
152          } catch (ExecutionException success) {
153 <            assertSame(t, success.getCause());
153 >            assertSame(t.getClass(), success.getCause().getClass());
154          } catch (Throwable fail) { threadUnexpectedException(fail); }
155  
156          try {
157              a.get(5L, SECONDS);
158              shouldThrow();
159          } catch (ExecutionException success) {
160 <            assertSame(t, success.getCause());
160 >            assertSame(t.getClass(), success.getCause().getClass());
161          } catch (Throwable fail) { threadUnexpectedException(fail); }
162      }
163  
164 <    static final class FJException extends RuntimeException {
165 <        FJException() { super(); }
164 >    public static final class FJException extends RuntimeException {
165 >        public FJException() { super(); }
166 >        public FJException(Throwable cause) { super(cause); }
167      }
168  
169      // A simple recursive action for testing
# Line 254 | Line 256 | public class RecursiveActionTest extends
256          RecursiveAction a = new CheckedRecursiveAction() {
257              public void realCompute() {
258                  FibAction f = new FibAction(8);
259 +                final Thread myself = Thread.currentThread();
260  
261                  // test join()
262                  assertSame(f, f.fork());
263 <                Thread.currentThread().interrupt();
263 >                myself.interrupt();
264 >                assertTrue(myself.isInterrupted());
265                  assertNull(f.join());
266                  Thread.interrupted();
267                  assertEquals(21, f.result);
268                  checkCompletedNormally(f);
269  
270 <                f.reinitialize();
270 >                f = new FibAction(8);
271                  f.cancel(true);
272                  assertSame(f, f.fork());
273 <                Thread.currentThread().interrupt();
273 >                myself.interrupt();
274 >                assertTrue(myself.isInterrupted());
275                  try {
276                      f.join();
277                      shouldThrow();
# Line 275 | Line 280 | public class RecursiveActionTest extends
280                      checkCancelled(f);
281                  }
282  
283 <                f.reinitialize();
283 >                f = new FibAction(8);
284                  f.completeExceptionally(new FJException());
285                  assertSame(f, f.fork());
286 <                Thread.currentThread().interrupt();
286 >                myself.interrupt();
287 >                assertTrue(myself.isInterrupted());
288                  try {
289                      f.join();
290                      shouldThrow();
# Line 288 | Line 294 | public class RecursiveActionTest extends
294                  }
295  
296                  // test quietlyJoin()
297 <                f.reinitialize();
297 >                f = new FibAction(8);
298                  assertSame(f, f.fork());
299 <                Thread.currentThread().interrupt();
299 >                myself.interrupt();
300 >                assertTrue(myself.isInterrupted());
301                  f.quietlyJoin();
302                  Thread.interrupted();
303                  assertEquals(21, f.result);
304                  checkCompletedNormally(f);
305  
306 <                f.reinitialize();
306 >                f = new FibAction(8);
307                  f.cancel(true);
308                  assertSame(f, f.fork());
309 <                Thread.currentThread().interrupt();
309 >                myself.interrupt();
310 >                assertTrue(myself.isInterrupted());
311                  f.quietlyJoin();
312                  Thread.interrupted();
313                  checkCancelled(f);
314  
315 <                f.reinitialize();
315 >                f = new FibAction(8);
316                  f.completeExceptionally(new FJException());
317                  assertSame(f, f.fork());
318 <                Thread.currentThread().interrupt();
318 >                myself.interrupt();
319 >                assertTrue(myself.isInterrupted());
320                  f.quietlyJoin();
321                  Thread.interrupted();
322                  checkCompletedAbnormally(f, f.getException());
# Line 347 | Line 356 | public class RecursiveActionTest extends
356              public void realRun() throws InterruptedException {
357                  FibAction[] fibActions = sq.take();
358                  FibAction f;
359 +                final Thread myself = Thread.currentThread();
360  
361                  // test join() ------------
362  
363                  f = fibActions[0];
364 <                assertFalse(f.inForkJoinPool());
365 <                Thread.currentThread().interrupt();
364 >                assertFalse(ForkJoinTask.inForkJoinPool());
365 >                myself.interrupt();
366 >                assertTrue(myself.isInterrupted());
367                  assertNull(f.join());
368                  assertTrue(Thread.interrupted());
369                  assertEquals(21, f.result);
370                  checkCompletedNormally(f);
371  
372                  f = fibActions[1];
373 <                Thread.currentThread().interrupt();
373 >                myself.interrupt();
374 >                assertTrue(myself.isInterrupted());
375                  try {
376                      f.join();
377                      shouldThrow();
# Line 369 | Line 381 | public class RecursiveActionTest extends
381                  }
382  
383                  f = fibActions[2];
384 <                Thread.currentThread().interrupt();
384 >                myself.interrupt();
385 >                assertTrue(myself.isInterrupted());
386                  try {
387                      f.join();
388                      shouldThrow();
# Line 381 | Line 394 | public class RecursiveActionTest extends
394                  // test quietlyJoin() ---------
395  
396                  f = fibActions[3];
397 <                Thread.currentThread().interrupt();
397 >                myself.interrupt();
398 >                assertTrue(myself.isInterrupted());
399                  f.quietlyJoin();
400                  assertTrue(Thread.interrupted());
401                  assertEquals(21, f.result);
402                  checkCompletedNormally(f);
403  
404                  f = fibActions[4];
405 <                Thread.currentThread().interrupt();
405 >                myself.interrupt();
406 >                assertTrue(myself.isInterrupted());
407                  f.quietlyJoin();
408                  assertTrue(Thread.interrupted());
409                  checkCancelled(f);
410  
411                  f = fibActions[5];
412 <                Thread.currentThread().interrupt();
412 >                myself.interrupt();
413 >                assertTrue(myself.isInterrupted());
414                  f.quietlyJoin();
415                  assertTrue(Thread.interrupted());
416                  assertTrue(f.getException() instanceof FJException);
# Line 474 | Line 490 | public class RecursiveActionTest extends
490          testInvokeOnPool(mainPool(), a);
491      }
492  
477
493      /**
494       * helpQuiesce returns when tasks are complete.
495       * getQueuedTaskCount returns 0 when quiescent
# Line 484 | Line 499 | public class RecursiveActionTest extends
499              public void realCompute() {
500                  FibAction f = new FibAction(8);
501                  assertSame(f, f.fork());
502 <                f.helpQuiesce();
502 >                helpQuiesce();
503                  assertEquals(21, f.result);
504                  assertEquals(0, getQueuedTaskCount());
505                  checkCompletedNormally(f);
# Line 492 | Line 507 | public class RecursiveActionTest extends
507          testInvokeOnPool(mainPool(), a);
508      }
509  
495
510      /**
511       * invoke task throws exception when task completes abnormally
512       */
# Line 754 | Line 768 | public class RecursiveActionTest extends
768          RecursiveAction a = new CheckedRecursiveAction() {
769              public void realCompute() {
770                  ForkJoinWorkerThread w =
771 <                    (ForkJoinWorkerThread)(Thread.currentThread());
771 >                    (ForkJoinWorkerThread) Thread.currentThread();
772                  assertTrue(w.getPoolIndex() >= 0);
773 <                assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
773 >                // pool size can shrink after assigning index, so cannot check
774 >                // assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
775              }};
776          testInvokeOnPool(mainPool, a);
777      }
778  
764
779      /**
780       * setRawResult(null) succeeds
781       */
# Line 908 | Line 922 | public class RecursiveActionTest extends
922          testInvokeOnPool(mainPool(), a);
923      }
924  
911
925      /**
926       * invokeAll(tasks) with any null task throws NPE
927       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines