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.24 by jsr166, Mon Nov 22 20:19:47 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 247 | Line 249 | public class RecursiveActionTest extends
249      }
250  
251      /**
252 <     * join/quietlyJoin of a forked task ignores interrupts
252 >     * join/quietlyJoin of a forked task succeeds in the presence of interrupts
253       */
254      public void testJoinIgnoresInterrupts() {
255          RecursiveAction a = new CheckedRecursiveAction() {
# Line 258 | Line 260 | public class RecursiveActionTest extends
260                  assertSame(f, f.fork());
261                  Thread.currentThread().interrupt();
262                  assertNull(f.join());
263 <                assertTrue(Thread.interrupted());
263 >                Thread.interrupted();
264                  assertEquals(21, f.result);
265                  checkCompletedNormally(f);
266  
# Line 270 | Line 272 | public class RecursiveActionTest extends
272                      f.join();
273                      shouldThrow();
274                  } catch (CancellationException success) {
275 <                    assertTrue(Thread.interrupted());
275 >                    Thread.interrupted();
276                      checkCancelled(f);
277                  }
278  
# Line 282 | Line 284 | public class RecursiveActionTest extends
284                      f.join();
285                      shouldThrow();
286                  } catch (FJException success) {
287 <                    assertTrue(Thread.interrupted());
287 >                    Thread.interrupted();
288                      checkCompletedAbnormally(f, success);
289                  }
290  
# Line 291 | Line 293 | public class RecursiveActionTest extends
293                  assertSame(f, f.fork());
294                  Thread.currentThread().interrupt();
295                  f.quietlyJoin();
296 <                assertTrue(Thread.interrupted());
296 >                Thread.interrupted();
297                  assertEquals(21, f.result);
298                  checkCompletedNormally(f);
299  
# Line 300 | Line 302 | public class RecursiveActionTest extends
302                  assertSame(f, f.fork());
303                  Thread.currentThread().interrupt();
304                  f.quietlyJoin();
305 <                assertTrue(Thread.interrupted());
305 >                Thread.interrupted();
306                  checkCancelled(f);
307  
308                  f.reinitialize();
# Line 308 | Line 310 | public class RecursiveActionTest extends
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      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines