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.19 by jsr166, Sun Nov 21 08:25:10 2010 UTC vs.
Revision 1.30 by jsr166, Tue Mar 15 19:47:07 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.*;
8   import java.util.concurrent.CancellationException;
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 59 | 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 90 | Line 92 | public class RecursiveActionTest extends
92          assertNull(a.getException());
93          assertNull(a.getRawResult());
94          assertNull(a.join());
95 +        assertFalse(a.cancel(false));
96 +        assertFalse(a.cancel(true));
97          try {
98              assertNull(a.get());
99          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 125 | Line 129 | public class RecursiveActionTest extends
129          } catch (Throwable fail) { threadUnexpectedException(fail); }
130      }
131  
132 <    void checkTaskThrew(RecursiveAction a, Throwable t) {
132 >    void checkCompletedAbnormally(RecursiveAction a, Throwable t) {
133          assertTrue(a.isDone());
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));
141  
142          try {
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 243 | Line 250 | public class RecursiveActionTest extends
250      }
251  
252      /**
253 +     * join/quietlyJoin of a forked task succeeds in the presence of interrupts
254 +     */
255 +    public void testJoinIgnoresInterrupts() {
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 +                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();
271 +                f.cancel(true);
272 +                assertSame(f, f.fork());
273 +                myself.interrupt();
274 +                assertTrue(myself.isInterrupted());
275 +                try {
276 +                    f.join();
277 +                    shouldThrow();
278 +                } catch (CancellationException success) {
279 +                    Thread.interrupted();
280 +                    checkCancelled(f);
281 +                }
282 +
283 +                f.reinitialize();
284 +                f.completeExceptionally(new FJException());
285 +                assertSame(f, f.fork());
286 +                myself.interrupt();
287 +                assertTrue(myself.isInterrupted());
288 +                try {
289 +                    f.join();
290 +                    shouldThrow();
291 +                } catch (FJException success) {
292 +                    Thread.interrupted();
293 +                    checkCompletedAbnormally(f, success);
294 +                }
295 +
296 +                // test quietlyJoin()
297 +                f.reinitialize();
298 +                assertSame(f, f.fork());
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();
307 +                f.cancel(true);
308 +                assertSame(f, f.fork());
309 +                myself.interrupt();
310 +                assertTrue(myself.isInterrupted());
311 +                f.quietlyJoin();
312 +                Thread.interrupted();
313 +                checkCancelled(f);
314 +
315 +                f.reinitialize();
316 +                f.completeExceptionally(new FJException());
317 +                assertSame(f, f.fork());
318 +                myself.interrupt();
319 +                assertTrue(myself.isInterrupted());
320 +                f.quietlyJoin();
321 +                Thread.interrupted();
322 +                checkCompletedAbnormally(f, f.getException());
323 +            }};
324 +        testInvokeOnPool(mainPool(), a);
325 +        a.reinitialize();
326 +        testInvokeOnPool(singletonPool(), a);
327 +    }
328 +
329 +    /**
330 +     * join/quietlyJoin of a forked task when not in ForkJoinPool
331 +     * succeeds in the presence of interrupts
332 +     */
333 +    public void testJoinIgnoresInterruptsOutsideForkJoinPool() {
334 +        final SynchronousQueue<FibAction[]> sq =
335 +            new SynchronousQueue<FibAction[]>();
336 +        RecursiveAction a = new CheckedRecursiveAction() {
337 +            public void realCompute() throws InterruptedException {
338 +                FibAction[] fibActions = new FibAction[6];
339 +                for (int i = 0; i < fibActions.length; i++)
340 +                    fibActions[i] = new FibAction(8);
341 +
342 +                fibActions[1].cancel(false);
343 +                fibActions[2].completeExceptionally(new FJException());
344 +                fibActions[4].cancel(true);
345 +                fibActions[5].completeExceptionally(new FJException());
346 +
347 +                for (int i = 0; i < fibActions.length; i++)
348 +                    fibActions[i].fork();
349 +
350 +                sq.put(fibActions);
351 +
352 +                helpQuiesce();
353 +            }};
354 +
355 +        Runnable r = new CheckedRunnable() {
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(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 +                myself.interrupt();
374 +                assertTrue(myself.isInterrupted());
375 +                try {
376 +                    f.join();
377 +                    shouldThrow();
378 +                } catch (CancellationException success) {
379 +                    assertTrue(Thread.interrupted());
380 +                    checkCancelled(f);
381 +                }
382 +
383 +                f = fibActions[2];
384 +                myself.interrupt();
385 +                assertTrue(myself.isInterrupted());
386 +                try {
387 +                    f.join();
388 +                    shouldThrow();
389 +                } catch (FJException success) {
390 +                    assertTrue(Thread.interrupted());
391 +                    checkCompletedAbnormally(f, success);
392 +                }
393 +
394 +                // test quietlyJoin() ---------
395 +
396 +                f = fibActions[3];
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 +                myself.interrupt();
406 +                assertTrue(myself.isInterrupted());
407 +                f.quietlyJoin();
408 +                assertTrue(Thread.interrupted());
409 +                checkCancelled(f);
410 +
411 +                f = fibActions[5];
412 +                myself.interrupt();
413 +                assertTrue(myself.isInterrupted());
414 +                f.quietlyJoin();
415 +                assertTrue(Thread.interrupted());
416 +                assertTrue(f.getException() instanceof FJException);
417 +                checkCompletedAbnormally(f, f.getException());
418 +            }};
419 +
420 +        Thread t;
421 +
422 +        t = newStartedThread(r);
423 +        testInvokeOnPool(mainPool(), a);
424 +        awaitTermination(t, LONG_DELAY_MS);
425 +
426 +        a.reinitialize();
427 +        t = newStartedThread(r);
428 +        testInvokeOnPool(singletonPool(), a);
429 +        awaitTermination(t, LONG_DELAY_MS);
430 +    }
431 +
432 +    /**
433       * get of a forked task returns when task completes
434       */
435      public void testForkGet() {
# Line 333 | Line 520 | public class RecursiveActionTest extends
520                      f.invoke();
521                      shouldThrow();
522                  } catch (FJException success) {
523 <                    checkTaskThrew(f, success);
523 >                    checkCompletedAbnormally(f, success);
524                  }
525              }};
526          testInvokeOnPool(mainPool(), a);
# Line 348 | Line 535 | public class RecursiveActionTest extends
535                  FailingFibAction f = new FailingFibAction(8);
536                  f.quietlyInvoke();
537                  assertTrue(f.getException() instanceof FJException);
538 <                checkTaskThrew(f, f.getException());
538 >                checkCompletedAbnormally(f, f.getException());
539              }};
540          testInvokeOnPool(mainPool(), a);
541      }
# Line 365 | Line 552 | public class RecursiveActionTest extends
552                      f.join();
553                      shouldThrow();
554                  } catch (FJException success) {
555 <                    checkTaskThrew(f, success);
555 >                    checkCompletedAbnormally(f, success);
556                  }
557              }};
558          testInvokeOnPool(mainPool(), a);
# Line 383 | Line 570 | public class RecursiveActionTest extends
570                      f.get();
571                      shouldThrow();
572                  } catch (ExecutionException success) {
573 <                    checkTaskThrew(f, success.getCause());
573 >                    Throwable cause = success.getCause();
574 >                    assertTrue(cause instanceof FJException);
575 >                    checkCompletedAbnormally(f, cause);
576                  }
577              }};
578          testInvokeOnPool(mainPool(), a);
# Line 401 | Line 590 | public class RecursiveActionTest extends
590                      f.get(5L, TimeUnit.SECONDS);
591                      shouldThrow();
592                  } catch (ExecutionException success) {
593 <                    checkTaskThrew(f, success.getCause());
593 >                    Throwable cause = success.getCause();
594 >                    assertTrue(cause instanceof FJException);
595 >                    checkCompletedAbnormally(f, cause);
596                  }
597              }};
598          testInvokeOnPool(mainPool(), a);
# Line 417 | Line 608 | public class RecursiveActionTest extends
608                  assertSame(f, f.fork());
609                  f.quietlyJoin();
610                  assertTrue(f.getException() instanceof FJException);
611 <                checkTaskThrew(f, f.getException());
611 >                checkCompletedAbnormally(f, f.getException());
612              }};
613          testInvokeOnPool(mainPool(), a);
614      }
# Line 579 | Line 770 | public class RecursiveActionTest extends
770          RecursiveAction a = new CheckedRecursiveAction() {
771              public void realCompute() {
772                  ForkJoinWorkerThread w =
773 <                    (ForkJoinWorkerThread)(Thread.currentThread());
773 >                    (ForkJoinWorkerThread) Thread.currentThread();
774                  assertTrue(w.getPoolIndex() >= 0);
775 <                assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
775 >                // pool size can shrink after assigning index, so cannot check
776 >                // assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
777              }};
778          testInvokeOnPool(mainPool, a);
779      }
# Line 594 | Line 786 | public class RecursiveActionTest extends
786          RecursiveAction a = new CheckedRecursiveAction() {
787              public void realCompute() {
788                  setRawResult(null);
789 +                assertNull(getRawResult());
790              }};
791          assertNull(a.invoke());
792      }
# Line 630 | Line 823 | public class RecursiveActionTest extends
823                      f.invoke();
824                      shouldThrow();
825                  } catch (FJException success) {
826 <                    checkTaskThrew(f, success);
826 >                    checkCompletedAbnormally(f, success);
827                  }
828              }};
829          testInvokeOnPool(mainPool(), a);
# Line 692 | Line 885 | public class RecursiveActionTest extends
885                  FibAction g = new FibAction(9);
886                  FibAction h = new FibAction(7);
887                  invokeAll(f, g, h);
888 +                assertTrue(f.isDone());
889 +                assertTrue(g.isDone());
890 +                assertTrue(h.isDone());
891                  checkCompletedNormally(f);
892                  assertEquals(21, f.result);
893                  checkCompletedNormally(g);
# Line 716 | Line 912 | public class RecursiveActionTest extends
912                  set.add(g);
913                  set.add(h);
914                  invokeAll(set);
915 +                assertTrue(f.isDone());
916 +                assertTrue(g.isDone());
917 +                assertTrue(h.isDone());
918                  checkCompletedNormally(f);
919                  assertEquals(21, f.result);
920                  checkCompletedNormally(g);
# Line 756 | Line 955 | public class RecursiveActionTest extends
955                      invokeAll(f, g);
956                      shouldThrow();
957                  } catch (FJException success) {
958 <                    checkTaskThrew(g, success);
958 >                    checkCompletedAbnormally(g, success);
959                  }
960              }};
961          testInvokeOnPool(mainPool(), a);
# Line 773 | Line 972 | public class RecursiveActionTest extends
972                      invokeAll(g);
973                      shouldThrow();
974                  } catch (FJException success) {
975 <                    checkTaskThrew(g, success);
975 >                    checkCompletedAbnormally(g, success);
976                  }
977              }};
978          testInvokeOnPool(mainPool(), a);
# Line 792 | Line 991 | public class RecursiveActionTest extends
991                      invokeAll(f, g, h);
992                      shouldThrow();
993                  } catch (FJException success) {
994 <                    checkTaskThrew(g, success);
994 >                    checkCompletedAbnormally(g, success);
995                  }
996              }};
997          testInvokeOnPool(mainPool(), a);
# Line 815 | Line 1014 | public class RecursiveActionTest extends
1014                      invokeAll(set);
1015                      shouldThrow();
1016                  } catch (FJException success) {
1017 <                    checkTaskThrew(f, success);
1017 >                    checkCompletedAbnormally(f, success);
1018                  }
1019              }};
1020          testInvokeOnPool(mainPool(), a);
# Line 855 | Line 1054 | public class RecursiveActionTest extends
1054                  assertSame(f, f.fork());
1055                  assertTrue(getSurplusQueuedTaskCount() > 0);
1056                  helpQuiesce();
1057 +                assertEquals(0, getSurplusQueuedTaskCount());
1058                  checkCompletedNormally(f);
1059                  checkCompletedNormally(g);
1060                  checkCompletedNormally(h);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines