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.20 by jsr166, Sun Nov 21 08:35:40 2010 UTC vs.
Revision 1.27 by jsr166, Tue Nov 23 08:48:23 2010 UTC

# Line 6 | Line 6
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());
138          assertNull(a.getRawResult());
139 +        assertFalse(a.cancel(false));
140 +        assertFalse(a.cancel(true));
141  
142          try {
143              a.join();
# Line 243 | Line 249 | public class RecursiveActionTest extends
249      }
250  
251      /**
252 +     * join/quietlyJoin of a forked task succeeds in the presence of interrupts
253 +     */
254 +    public void testJoinIgnoresInterrupts() {
255 +        RecursiveAction a = new CheckedRecursiveAction() {
256 +            public void realCompute() {
257 +                FibAction f = new FibAction(8);
258 +
259 +                // test join()
260 +                assertSame(f, f.fork());
261 +                Thread.currentThread().interrupt();
262 +                assertNull(f.join());
263 +                Thread.interrupted();
264 +                assertEquals(21, f.result);
265 +                checkCompletedNormally(f);
266 +
267 +                f.reinitialize();
268 +                f.cancel(true);
269 +                assertSame(f, f.fork());
270 +                Thread.currentThread().interrupt();
271 +                try {
272 +                    f.join();
273 +                    shouldThrow();
274 +                } catch (CancellationException success) {
275 +                    Thread.interrupted();
276 +                    checkCancelled(f);
277 +                }
278 +
279 +                f.reinitialize();
280 +                f.completeExceptionally(new FJException());
281 +                assertSame(f, f.fork());
282 +                Thread.currentThread().interrupt();
283 +                try {
284 +                    f.join();
285 +                    shouldThrow();
286 +                } catch (FJException success) {
287 +                    Thread.interrupted();
288 +                    checkCompletedAbnormally(f, success);
289 +                }
290 +
291 +                // test quietlyJoin()
292 +                f.reinitialize();
293 +                assertSame(f, f.fork());
294 +                Thread.currentThread().interrupt();
295 +                f.quietlyJoin();
296 +                Thread.interrupted();
297 +                assertEquals(21, f.result);
298 +                checkCompletedNormally(f);
299 +
300 +                f.reinitialize();
301 +                f.cancel(true);
302 +                assertSame(f, f.fork());
303 +                Thread.currentThread().interrupt();
304 +                f.quietlyJoin();
305 +                Thread.interrupted();
306 +                checkCancelled(f);
307 +
308 +                f.reinitialize();
309 +                f.completeExceptionally(new FJException());
310 +                assertSame(f, f.fork());
311 +                Thread.currentThread().interrupt();
312 +                f.quietlyJoin();
313 +                Thread.interrupted();
314 +                checkCompletedAbnormally(f, f.getException());
315 +            }};
316 +        testInvokeOnPool(mainPool(), a);
317 +        a.reinitialize();
318 +        testInvokeOnPool(singletonPool(), a);
319 +    }
320 +
321 +    /**
322 +     * join/quietlyJoin of a forked task when not in ForkJoinPool
323 +     * succeeds in the presence of interrupts
324 +     */
325 +    public void testJoinIgnoresInterruptsOutsideForkJoinPool() {
326 +        final SynchronousQueue<FibAction[]> sq =
327 +            new SynchronousQueue<FibAction[]>();
328 +        RecursiveAction a = new CheckedRecursiveAction() {
329 +            public void realCompute() throws InterruptedException {
330 +                FibAction[] fibActions = new FibAction[6];
331 +                for (int i = 0; i < fibActions.length; i++)
332 +                    fibActions[i] = new FibAction(8);
333 +
334 +                fibActions[1].cancel(false);
335 +                fibActions[2].completeExceptionally(new FJException());
336 +                fibActions[4].cancel(true);
337 +                fibActions[5].completeExceptionally(new FJException());
338 +
339 +                for (int i = 0; i < fibActions.length; i++)
340 +                    fibActions[i].fork();
341 +
342 +                sq.put(fibActions);
343 +
344 +                helpQuiesce();
345 +            }};
346 +
347 +        Runnable r = new CheckedRunnable() {
348 +            public void realRun() throws InterruptedException {
349 +                FibAction[] fibActions = sq.take();
350 +                FibAction f;
351 +
352 +                // test join() ------------
353 +
354 +                f = fibActions[0];
355 +                assertFalse(ForkJoinTask.inForkJoinPool());
356 +                Thread.currentThread().interrupt();
357 +                assertNull(f.join());
358 +                assertTrue(Thread.interrupted());
359 +                assertEquals(21, f.result);
360 +                checkCompletedNormally(f);
361 +
362 +                f = fibActions[1];
363 +                Thread.currentThread().interrupt();
364 +                try {
365 +                    f.join();
366 +                    shouldThrow();
367 +                } catch (CancellationException success) {
368 +                    assertTrue(Thread.interrupted());
369 +                    checkCancelled(f);
370 +                }
371 +
372 +                f = fibActions[2];
373 +                Thread.currentThread().interrupt();
374 +                try {
375 +                    f.join();
376 +                    shouldThrow();
377 +                } catch (FJException success) {
378 +                    assertTrue(Thread.interrupted());
379 +                    checkCompletedAbnormally(f, success);
380 +                }
381 +
382 +                // test quietlyJoin() ---------
383 +
384 +                f = fibActions[3];
385 +                Thread.currentThread().interrupt();
386 +                f.quietlyJoin();
387 +                assertTrue(Thread.interrupted());
388 +                assertEquals(21, f.result);
389 +                checkCompletedNormally(f);
390 +
391 +                f = fibActions[4];
392 +                Thread.currentThread().interrupt();
393 +                f.quietlyJoin();
394 +                assertTrue(Thread.interrupted());
395 +                checkCancelled(f);
396 +
397 +                f = fibActions[5];
398 +                Thread.currentThread().interrupt();
399 +                f.quietlyJoin();
400 +                assertTrue(Thread.interrupted());
401 +                assertTrue(f.getException() instanceof FJException);
402 +                checkCompletedAbnormally(f, f.getException());
403 +            }};
404 +
405 +        Thread t;
406 +
407 +        t = newStartedThread(r);
408 +        testInvokeOnPool(mainPool(), a);
409 +        awaitTermination(t, LONG_DELAY_MS);
410 +
411 +        a.reinitialize();
412 +        t = newStartedThread(r);
413 +        testInvokeOnPool(singletonPool(), a);
414 +        awaitTermination(t, LONG_DELAY_MS);
415 +    }
416 +
417 +    /**
418       * get of a forked task returns when task completes
419       */
420      public void testForkGet() {
# Line 333 | Line 505 | public class RecursiveActionTest extends
505                      f.invoke();
506                      shouldThrow();
507                  } catch (FJException success) {
508 <                    checkTaskThrew(f, success);
508 >                    checkCompletedAbnormally(f, success);
509                  }
510              }};
511          testInvokeOnPool(mainPool(), a);
# Line 348 | Line 520 | public class RecursiveActionTest extends
520                  FailingFibAction f = new FailingFibAction(8);
521                  f.quietlyInvoke();
522                  assertTrue(f.getException() instanceof FJException);
523 <                checkTaskThrew(f, f.getException());
523 >                checkCompletedAbnormally(f, f.getException());
524              }};
525          testInvokeOnPool(mainPool(), a);
526      }
# Line 365 | Line 537 | public class RecursiveActionTest extends
537                      f.join();
538                      shouldThrow();
539                  } catch (FJException success) {
540 <                    checkTaskThrew(f, success);
540 >                    checkCompletedAbnormally(f, success);
541                  }
542              }};
543          testInvokeOnPool(mainPool(), a);
# Line 383 | Line 555 | public class RecursiveActionTest extends
555                      f.get();
556                      shouldThrow();
557                  } catch (ExecutionException success) {
558 <                    checkTaskThrew(f, success.getCause());
558 >                    Throwable cause = success.getCause();
559 >                    assertTrue(cause instanceof FJException);
560 >                    checkCompletedAbnormally(f, cause);
561                  }
562              }};
563          testInvokeOnPool(mainPool(), a);
# Line 401 | Line 575 | public class RecursiveActionTest extends
575                      f.get(5L, TimeUnit.SECONDS);
576                      shouldThrow();
577                  } catch (ExecutionException success) {
578 <                    checkTaskThrew(f, success.getCause());
578 >                    Throwable cause = success.getCause();
579 >                    assertTrue(cause instanceof FJException);
580 >                    checkCompletedAbnormally(f, cause);
581                  }
582              }};
583          testInvokeOnPool(mainPool(), a);
# Line 417 | Line 593 | public class RecursiveActionTest extends
593                  assertSame(f, f.fork());
594                  f.quietlyJoin();
595                  assertTrue(f.getException() instanceof FJException);
596 <                checkTaskThrew(f, f.getException());
596 >                checkCompletedAbnormally(f, f.getException());
597              }};
598          testInvokeOnPool(mainPool(), a);
599      }
# Line 594 | Line 770 | public class RecursiveActionTest extends
770          RecursiveAction a = new CheckedRecursiveAction() {
771              public void realCompute() {
772                  setRawResult(null);
773 +                assertNull(getRawResult());
774              }};
775          assertNull(a.invoke());
776      }
# Line 630 | Line 807 | public class RecursiveActionTest extends
807                      f.invoke();
808                      shouldThrow();
809                  } catch (FJException success) {
810 <                    checkTaskThrew(f, success);
810 >                    checkCompletedAbnormally(f, success);
811                  }
812              }};
813          testInvokeOnPool(mainPool(), a);
# Line 762 | Line 939 | public class RecursiveActionTest extends
939                      invokeAll(f, g);
940                      shouldThrow();
941                  } catch (FJException success) {
942 <                    checkTaskThrew(g, success);
942 >                    checkCompletedAbnormally(g, success);
943                  }
944              }};
945          testInvokeOnPool(mainPool(), a);
# Line 779 | Line 956 | public class RecursiveActionTest extends
956                      invokeAll(g);
957                      shouldThrow();
958                  } catch (FJException success) {
959 <                    checkTaskThrew(g, success);
959 >                    checkCompletedAbnormally(g, success);
960                  }
961              }};
962          testInvokeOnPool(mainPool(), a);
# Line 798 | Line 975 | public class RecursiveActionTest extends
975                      invokeAll(f, g, h);
976                      shouldThrow();
977                  } catch (FJException success) {
978 <                    checkTaskThrew(g, success);
978 >                    checkCompletedAbnormally(g, success);
979                  }
980              }};
981          testInvokeOnPool(mainPool(), a);
# Line 821 | Line 998 | public class RecursiveActionTest extends
998                      invokeAll(set);
999                      shouldThrow();
1000                  } catch (FJException success) {
1001 <                    checkTaskThrew(f, success);
1001 >                    checkCompletedAbnormally(f, success);
1002                  }
1003              }};
1004          testInvokeOnPool(mainPool(), a);
# Line 861 | Line 1038 | public class RecursiveActionTest extends
1038                  assertSame(f, f.fork());
1039                  assertTrue(getSurplusQueuedTaskCount() > 0);
1040                  helpQuiesce();
1041 +                assertEquals(0, getSurplusQueuedTaskCount());
1042                  checkCompletedNormally(f);
1043                  checkCompletedNormally(g);
1044                  checkCompletedNormally(h);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines