ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/FutureTaskTest.java
(Generate patch)

Comparing jsr166/src/test/tck/FutureTaskTest.java (file contents):
Revision 1.35 by jsr166, Mon Jan 14 21:54:42 2013 UTC vs.
Revision 1.38 by jsr166, Wed Jun 19 05:54:45 2013 UTC

# Line 37 | Line 37 | public class FutureTaskTest extends JSR1
37              assertEquals(1, pf.doneCount());
38              assertFalse(pf.runAndReset());
39              assertEquals(1, pf.doneCount());
40 +            Object r = null; Object exInfo = null;
41 +            try {
42 +                r = f.get();
43 +            } catch (CancellationException t) {
44 +                exInfo = CancellationException.class;
45 +            } catch (ExecutionException t) {
46 +                exInfo = t.getCause();
47 +            } catch (Throwable t) {
48 +                threadUnexpectedException(t);
49 +            }
50  
51              // Check that run and runAndReset have no effect.
52              int savedRunCount = pf.runCount();
43            int savedSetCount = pf.setCount();
44            int savedSetExceptionCount = pf.setExceptionCount();
53              pf.run();
54              pf.runAndReset();
55              assertEquals(savedRunCount, pf.runCount());
56 <            assertEquals(savedSetCount, pf.setCount());
57 <            assertEquals(savedSetExceptionCount, pf.setExceptionCount());
56 >            try {
57 >                assertSame(r, f.get());
58 >            } catch (CancellationException t) {
59 >                assertSame(exInfo, CancellationException.class);
60 >            } catch (ExecutionException t) {
61 >                assertSame(exInfo, t.getCause());
62 >            } catch (Throwable t) {
63 >                threadUnexpectedException(t);
64 >            }
65              assertTrue(f.isDone());
66          }
67      }
# Line 68 | Line 83 | public class FutureTaskTest extends JSR1
83              FutureTask ft = (FutureTask<?>) f;
84              // Check that run methods do nothing
85              ft.run();
86 <            if (f instanceof PublicFutureTask)
87 <                assertFalse(((PublicFutureTask) f).runAndReset());
86 >            if (f instanceof PublicFutureTask) {
87 >                PublicFutureTask pf = (PublicFutureTask) f;
88 >                int savedRunCount = pf.runCount();
89 >                pf.run();
90 >                assertFalse(pf.runAndReset());
91 >                assertEquals(savedRunCount, pf.runCount());
92 >            }
93              checkNotDone(f);
94          }
95      }
# Line 461 | Line 481 | public class FutureTaskTest extends JSR1
481                      try {
482                          pleaseCancel.countDown();
483                          delay(LONG_DELAY_MS);
484 <                        shouldThrow();
485 <                    } catch (Throwable t) {
486 <                        assertTrue(t instanceof InterruptedException);
467 <                    }
484 >                        threadShouldThrow();
485 >                    } catch (InterruptedException success) {
486 >                    } catch (Throwable t) { threadUnexpectedException(t); }
487                      throw new RuntimeException();
488                  }});
489  
# Line 590 | Line 609 | public class FutureTaskTest extends JSR1
609       * CancellationException
610       */
611      public void testTimedGet_Cancellation() {
612 <        for (final boolean mayInterruptIfRunning :
613 <                 new boolean[] { true, false }) {
614 <            final CountDownLatch pleaseCancel = new CountDownLatch(3);
615 <            final CountDownLatch cancelled = new CountDownLatch(1);
616 <            final PublicFutureTask task =
617 <                new PublicFutureTask(new CheckedCallable<Object>() {
618 <                    public Object realCall() throws InterruptedException {
619 <                        pleaseCancel.countDown();
620 <                        if (mayInterruptIfRunning) {
621 <                            try {
622 <                                delay(2*LONG_DELAY_MS);
623 <                            } catch (InterruptedException success) {}
624 <                        } else {
625 <                            await(cancelled);
626 <                        }
627 <                        return two;
628 <                    }});
612 >        testTimedGet_Cancellation(false);
613 >    }
614 >    public void testTimedGet_Cancellation_interrupt() {
615 >        testTimedGet_Cancellation(true);
616 >    }
617 >    public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) {
618 >        final CountDownLatch pleaseCancel = new CountDownLatch(3);
619 >        final CountDownLatch cancelled = new CountDownLatch(1);
620 >        final Callable<Object> callable =
621 >            new CheckedCallable<Object>() {
622 >            public Object realCall() throws InterruptedException {
623 >                pleaseCancel.countDown();
624 >                if (mayInterruptIfRunning) {
625 >                    try {
626 >                        delay(2*LONG_DELAY_MS);
627 >                    } catch (InterruptedException success) {}
628 >                } else {
629 >                    await(cancelled);
630 >                }
631 >                return two;
632 >            }};
633 >        final PublicFutureTask task = new PublicFutureTask(callable);
634  
635 <            Thread t1 = new ThreadShouldThrow(CancellationException.class) {
635 >        Thread t1 = new ThreadShouldThrow(CancellationException.class) {
636                  public void realRun() throws Exception {
637                      pleaseCancel.countDown();
638                      task.get();
639                  }};
640 <            Thread t2 = new ThreadShouldThrow(CancellationException.class) {
640 >        Thread t2 = new ThreadShouldThrow(CancellationException.class) {
641                  public void realRun() throws Exception {
642                      pleaseCancel.countDown();
643                      task.get(2*LONG_DELAY_MS, MILLISECONDS);
644                  }};
645 <            t1.start();
646 <            t2.start();
647 <            Thread t3 = newStartedThread(task);
648 <            await(pleaseCancel);
649 <            checkIsRunning(task);
650 <            task.cancel(mayInterruptIfRunning);
651 <            checkCancelled(task);
652 <            awaitTermination(t1);
653 <            awaitTermination(t2);
654 <            cancelled.countDown();
655 <            awaitTermination(t3);
656 <            assertEquals(1, task.runCount());
657 <            assertEquals(1, task.setCount());
658 <            assertEquals(0, task.setExceptionCount());
659 <            tryToConfuseDoneTask(task);
660 <            checkCancelled(task);
637 <        }
645 >        t1.start();
646 >        t2.start();
647 >        Thread t3 = newStartedThread(task);
648 >        await(pleaseCancel);
649 >        checkIsRunning(task);
650 >        task.cancel(mayInterruptIfRunning);
651 >        checkCancelled(task);
652 >        awaitTermination(t1);
653 >        awaitTermination(t2);
654 >        cancelled.countDown();
655 >        awaitTermination(t3);
656 >        assertEquals(1, task.runCount());
657 >        assertEquals(1, task.setCount());
658 >        assertEquals(0, task.setExceptionCount());
659 >        tryToConfuseDoneTask(task);
660 >        checkCancelled(task);
661      }
662  
663      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines