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.33 by jsr166, Thu Dec 20 04:58:53 2012 UTC vs.
Revision 1.37 by jsr166, Sun Apr 21 06:54:11 2013 UTC

# Line 7 | Line 7
7   */
8  
9   import junit.framework.*;
10 import java.security.Permission;
10   import java.util.concurrent.Callable;
11   import java.util.concurrent.CancellationException;
12   import java.util.concurrent.CountDownLatch;
# Line 38 | 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();
44            int savedSetCount = pf.setCount();
45            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 69 | 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 462 | Line 481 | public class FutureTaskTest extends JSR1
481                      try {
482                          pleaseCancel.countDown();
483                          delay(LONG_DELAY_MS);
484 <                    } finally { throw new RuntimeException(); }
484 >                        shouldThrow();
485 >                    } catch (Throwable t) {
486 >                        assertTrue(t instanceof InterruptedException);
487 >                    }
488 >                    throw new RuntimeException();
489                  }});
490  
491          Thread t = newStartedThread(task);
# Line 587 | Line 610 | public class FutureTaskTest extends JSR1
610       * CancellationException
611       */
612      public void testTimedGet_Cancellation() {
613 <        for (final boolean mayInterruptIfRunning :
614 <                 new boolean[] { true, false }) {
615 <            final CountDownLatch pleaseCancel = new CountDownLatch(3);
616 <            final CountDownLatch cancelled = new CountDownLatch(1);
617 <            final PublicFutureTask task =
618 <                new PublicFutureTask(new CheckedCallable<Object>() {
619 <                    public Object realCall() throws InterruptedException {
620 <                        pleaseCancel.countDown();
621 <                        if (mayInterruptIfRunning) {
622 <                            try {
623 <                                delay(2*LONG_DELAY_MS);
624 <                            } catch (InterruptedException success) {}
625 <                        } else {
626 <                            await(cancelled);
627 <                        }
628 <                        return two;
629 <                    }});
613 >        testTimedGet_Cancellation(false);
614 >    }
615 >    public void testTimedGet_Cancellation_interrupt() {
616 >        testTimedGet_Cancellation(true);
617 >    }
618 >    public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) {
619 >        final CountDownLatch pleaseCancel = new CountDownLatch(3);
620 >        final CountDownLatch cancelled = new CountDownLatch(1);
621 >        final Callable<Object> callable =
622 >            new CheckedCallable<Object>() {
623 >            public Object realCall() throws InterruptedException {
624 >                pleaseCancel.countDown();
625 >                if (mayInterruptIfRunning) {
626 >                    try {
627 >                        delay(2*LONG_DELAY_MS);
628 >                    } catch (InterruptedException success) {}
629 >                } else {
630 >                    await(cancelled);
631 >                }
632 >                return two;
633 >            }};
634 >        final PublicFutureTask task = new PublicFutureTask(callable);
635  
636 <            Thread t1 = new ThreadShouldThrow(CancellationException.class) {
636 >        Thread t1 = new ThreadShouldThrow(CancellationException.class) {
637                  public void realRun() throws Exception {
638                      pleaseCancel.countDown();
639                      task.get();
640                  }};
641 <            Thread t2 = new ThreadShouldThrow(CancellationException.class) {
641 >        Thread t2 = new ThreadShouldThrow(CancellationException.class) {
642                  public void realRun() throws Exception {
643                      pleaseCancel.countDown();
644                      task.get(2*LONG_DELAY_MS, MILLISECONDS);
645                  }};
646 <            t1.start();
647 <            t2.start();
648 <            Thread t3 = newStartedThread(task);
649 <            await(pleaseCancel);
650 <            checkIsRunning(task);
651 <            task.cancel(mayInterruptIfRunning);
652 <            checkCancelled(task);
653 <            awaitTermination(t1);
654 <            awaitTermination(t2);
655 <            cancelled.countDown();
656 <            awaitTermination(t3);
657 <            assertEquals(1, task.runCount());
658 <            assertEquals(1, task.setCount());
659 <            assertEquals(0, task.setExceptionCount());
660 <            tryToConfuseDoneTask(task);
661 <            checkCancelled(task);
634 <        }
646 >        t1.start();
647 >        t2.start();
648 >        Thread t3 = newStartedThread(task);
649 >        await(pleaseCancel);
650 >        checkIsRunning(task);
651 >        task.cancel(mayInterruptIfRunning);
652 >        checkCancelled(task);
653 >        awaitTermination(t1);
654 >        awaitTermination(t2);
655 >        cancelled.countDown();
656 >        awaitTermination(t3);
657 >        assertEquals(1, task.runCount());
658 >        assertEquals(1, task.setCount());
659 >        assertEquals(0, task.setExceptionCount());
660 >        tryToConfuseDoneTask(task);
661 >        checkCancelled(task);
662      }
663  
664      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines