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.21 by jsr166, Sun Nov 21 19:06:53 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 132 | Line 136 | public class RecursiveActionTest extends
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 598 | 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      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines