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.25 by jsr166, Tue Nov 23 06:33:26 2010 UTC vs.
Revision 1.28 by jsr166, Wed Dec 1 22:37:51 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 253 | Line 255 | public class RecursiveActionTest extends
255          RecursiveAction a = new CheckedRecursiveAction() {
256              public void realCompute() {
257                  FibAction f = new FibAction(8);
258 +                final Thread myself = Thread.currentThread();
259  
260                  // test join()
261                  assertSame(f, f.fork());
262 <                Thread.currentThread().interrupt();
262 >                myself.interrupt();
263 >                assertTrue(myself.isInterrupted());
264                  assertNull(f.join());
265                  Thread.interrupted();
266                  assertEquals(21, f.result);
# Line 265 | Line 269 | public class RecursiveActionTest extends
269                  f.reinitialize();
270                  f.cancel(true);
271                  assertSame(f, f.fork());
272 <                Thread.currentThread().interrupt();
272 >                myself.interrupt();
273 >                assertTrue(myself.isInterrupted());
274                  try {
275                      f.join();
276                      shouldThrow();
# Line 277 | Line 282 | public class RecursiveActionTest extends
282                  f.reinitialize();
283                  f.completeExceptionally(new FJException());
284                  assertSame(f, f.fork());
285 <                Thread.currentThread().interrupt();
285 >                myself.interrupt();
286 >                assertTrue(myself.isInterrupted());
287                  try {
288                      f.join();
289                      shouldThrow();
# Line 289 | Line 295 | public class RecursiveActionTest extends
295                  // test quietlyJoin()
296                  f.reinitialize();
297                  assertSame(f, f.fork());
298 <                Thread.currentThread().interrupt();
298 >                myself.interrupt();
299 >                assertTrue(myself.isInterrupted());
300                  f.quietlyJoin();
301                  Thread.interrupted();
302                  assertEquals(21, f.result);
# Line 298 | Line 305 | public class RecursiveActionTest extends
305                  f.reinitialize();
306                  f.cancel(true);
307                  assertSame(f, f.fork());
308 <                Thread.currentThread().interrupt();
308 >                myself.interrupt();
309 >                assertTrue(myself.isInterrupted());
310                  f.quietlyJoin();
311                  Thread.interrupted();
312                  checkCancelled(f);
# Line 306 | Line 314 | public class RecursiveActionTest extends
314                  f.reinitialize();
315                  f.completeExceptionally(new FJException());
316                  assertSame(f, f.fork());
317 <                Thread.currentThread().interrupt();
317 >                myself.interrupt();
318 >                assertTrue(myself.isInterrupted());
319                  f.quietlyJoin();
320                  Thread.interrupted();
321                  checkCompletedAbnormally(f, f.getException());
# Line 317 | Line 326 | public class RecursiveActionTest extends
326      }
327  
328      /**
329 +     * join/quietlyJoin of a forked task when not in ForkJoinPool
330 +     * succeeds in the presence of interrupts
331 +     */
332 +    public void testJoinIgnoresInterruptsOutsideForkJoinPool() {
333 +        final SynchronousQueue<FibAction[]> sq =
334 +            new SynchronousQueue<FibAction[]>();
335 +        RecursiveAction a = new CheckedRecursiveAction() {
336 +            public void realCompute() throws InterruptedException {
337 +                FibAction[] fibActions = new FibAction[6];
338 +                for (int i = 0; i < fibActions.length; i++)
339 +                    fibActions[i] = new FibAction(8);
340 +
341 +                fibActions[1].cancel(false);
342 +                fibActions[2].completeExceptionally(new FJException());
343 +                fibActions[4].cancel(true);
344 +                fibActions[5].completeExceptionally(new FJException());
345 +
346 +                for (int i = 0; i < fibActions.length; i++)
347 +                    fibActions[i].fork();
348 +
349 +                sq.put(fibActions);
350 +
351 +                helpQuiesce();
352 +            }};
353 +
354 +        Runnable r = new CheckedRunnable() {
355 +            public void realRun() throws InterruptedException {
356 +                FibAction[] fibActions = sq.take();
357 +                FibAction f;
358 +                final Thread myself = Thread.currentThread();
359 +
360 +                // test join() ------------
361 +
362 +                f = fibActions[0];
363 +                assertFalse(ForkJoinTask.inForkJoinPool());
364 +                myself.interrupt();
365 +                assertTrue(myself.isInterrupted());
366 +                assertNull(f.join());
367 +                assertTrue(Thread.interrupted());
368 +                assertEquals(21, f.result);
369 +                checkCompletedNormally(f);
370 +
371 +                f = fibActions[1];
372 +                myself.interrupt();
373 +                assertTrue(myself.isInterrupted());
374 +                try {
375 +                    f.join();
376 +                    shouldThrow();
377 +                } catch (CancellationException success) {
378 +                    assertTrue(Thread.interrupted());
379 +                    checkCancelled(f);
380 +                }
381 +
382 +                f = fibActions[2];
383 +                myself.interrupt();
384 +                assertTrue(myself.isInterrupted());
385 +                try {
386 +                    f.join();
387 +                    shouldThrow();
388 +                } catch (FJException success) {
389 +                    assertTrue(Thread.interrupted());
390 +                    checkCompletedAbnormally(f, success);
391 +                }
392 +
393 +                // test quietlyJoin() ---------
394 +
395 +                f = fibActions[3];
396 +                myself.interrupt();
397 +                assertTrue(myself.isInterrupted());
398 +                f.quietlyJoin();
399 +                assertTrue(Thread.interrupted());
400 +                assertEquals(21, f.result);
401 +                checkCompletedNormally(f);
402 +
403 +                f = fibActions[4];
404 +                myself.interrupt();
405 +                assertTrue(myself.isInterrupted());
406 +                f.quietlyJoin();
407 +                assertTrue(Thread.interrupted());
408 +                checkCancelled(f);
409 +
410 +                f = fibActions[5];
411 +                myself.interrupt();
412 +                assertTrue(myself.isInterrupted());
413 +                f.quietlyJoin();
414 +                assertTrue(Thread.interrupted());
415 +                assertTrue(f.getException() instanceof FJException);
416 +                checkCompletedAbnormally(f, f.getException());
417 +            }};
418 +
419 +        Thread t;
420 +
421 +        t = newStartedThread(r);
422 +        testInvokeOnPool(mainPool(), a);
423 +        awaitTermination(t, LONG_DELAY_MS);
424 +
425 +        a.reinitialize();
426 +        t = newStartedThread(r);
427 +        testInvokeOnPool(singletonPool(), a);
428 +        awaitTermination(t, LONG_DELAY_MS);
429 +    }
430 +
431 +    /**
432       * get of a forked task returns when task completes
433       */
434      public void testForkGet() {
# Line 657 | Line 769 | public class RecursiveActionTest extends
769          RecursiveAction a = new CheckedRecursiveAction() {
770              public void realCompute() {
771                  ForkJoinWorkerThread w =
772 <                    (ForkJoinWorkerThread)(Thread.currentThread());
772 >                    (ForkJoinWorkerThread) Thread.currentThread();
773                  assertTrue(w.getPoolIndex() >= 0);
774                  assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
775              }};

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines