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.38 by jsr166, Wed Jun 19 05:54:45 2013 UTC vs.
Revision 1.49 by jsr166, Sun Oct 22 01:26:49 2017 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 > import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 >
12 > import java.util.ArrayList;
13 > import java.util.List;
14 > import java.util.NoSuchElementException;
15   import java.util.concurrent.Callable;
16   import java.util.concurrent.CancellationException;
17   import java.util.concurrent.CountDownLatch;
18   import java.util.concurrent.ExecutionException;
19 + import java.util.concurrent.Executors;
20 + import java.util.concurrent.ExecutorService;
21   import java.util.concurrent.Future;
22   import java.util.concurrent.FutureTask;
23   import java.util.concurrent.TimeoutException;
24   import java.util.concurrent.atomic.AtomicInteger;
25 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
26 < import static java.util.concurrent.TimeUnit.SECONDS;
27 < import java.util.*;
25 >
26 > import junit.framework.Test;
27 > import junit.framework.TestSuite;
28  
29   public class FutureTaskTest extends JSR166TestCase {
30  
31      public static void main(String[] args) {
32 <        junit.textui.TestRunner.run(suite());
32 >        main(suite(), args);
33      }
34      public static Test suite() {
35          return new TestSuite(FutureTaskTest.class);
# Line 44 | Line 51 | public class FutureTaskTest extends JSR1
51                  exInfo = CancellationException.class;
52              } catch (ExecutionException t) {
53                  exInfo = t.getCause();
54 <            } catch (Throwable t) {
54 >            } catch (Exception t) {
55                  threadUnexpectedException(t);
56              }
57  
# Line 59 | Line 66 | public class FutureTaskTest extends JSR1
66                  assertSame(exInfo, CancellationException.class);
67              } catch (ExecutionException t) {
68                  assertSame(exInfo, t.getCause());
69 <            } catch (Throwable t) {
69 >            } catch (Exception t) {
70                  threadUnexpectedException(t);
71              }
72              assertTrue(f.isDone());
# Line 100 | Line 107 | public class FutureTaskTest extends JSR1
107  
108          try {
109              assertSame(expected, f.get());
110 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
111 <        try {
105 <            assertSame(expected, f.get(5L, SECONDS));
106 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
110 >            assertSame(expected, f.get(randomTimeout(), randomTimeUnit()));
111 >        } catch (Exception fail) { threadUnexpectedException(fail); }
112      }
113  
114      void checkCancelled(Future<?> f) {
# Line 117 | Line 122 | public class FutureTaskTest extends JSR1
122          } catch (Throwable fail) { threadUnexpectedException(fail); }
123  
124          try {
125 <            f.get(5L, SECONDS);
125 >            f.get(randomTimeout(), randomTimeUnit());
126              shouldThrow();
127          } catch (CancellationException success) {
128          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 127 | Line 132 | public class FutureTaskTest extends JSR1
132          pf.set(new Object());
133          pf.setException(new Error());
134          for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
135 <            pf.cancel(true);
135 >            pf.cancel(mayInterruptIfRunning);
136          }
137      }
138  
# Line 143 | Line 148 | public class FutureTaskTest extends JSR1
148          } catch (Throwable fail) { threadUnexpectedException(fail); }
149  
150          try {
151 <            f.get(5L, SECONDS);
151 >            f.get(randomTimeout(), randomTimeUnit());
152              shouldThrow();
153          } catch (ExecutionException success) {
154              assertSame(t, success.getCause());
# Line 259 | Line 264 | public class FutureTaskTest extends JSR1
264          for (int i = 0; i < 3; i++) {
265              assertTrue(task.runAndReset());
266              checkNotDone(task);
267 <            assertEquals(i+1, task.runCount());
268 <            assertEquals(i+1, task.runAndResetCount());
267 >            assertEquals(i + 1, task.runCount());
268 >            assertEquals(i + 1, task.runAndResetCount());
269              assertEquals(0, task.setCount());
270              assertEquals(0, task.setExceptionCount());
271          }
# Line 276 | Line 281 | public class FutureTaskTest extends JSR1
281              for (int i = 0; i < 3; i++) {
282                  assertFalse(task.runAndReset());
283                  assertEquals(0, task.runCount());
284 <                assertEquals(i+1, task.runAndResetCount());
284 >                assertEquals(i + 1, task.runAndResetCount());
285                  assertEquals(0, task.setCount());
286                  assertEquals(0, task.setExceptionCount());
287              }
# Line 409 | Line 414 | public class FutureTaskTest extends JSR1
414                          delay(LONG_DELAY_MS);
415                          shouldThrow();
416                      } catch (InterruptedException success) {}
417 +                    assertFalse(Thread.interrupted());
418                  }});
419  
420          Thread t = newStartedThread(task);
# Line 478 | Line 484 | public class FutureTaskTest extends JSR1
484          final PublicFutureTask task =
485              new PublicFutureTask(new Runnable() {
486                  public void run() {
487 +                    pleaseCancel.countDown();
488                      try {
482                        pleaseCancel.countDown();
489                          delay(LONG_DELAY_MS);
490                          threadShouldThrow();
491                      } catch (InterruptedException success) {
# Line 801 | Line 807 | public class FutureTaskTest extends JSR1
807          }
808      }
809  
810 +    /**
811 +     * timed get with most negative timeout works correctly (i.e. no
812 +     * underflow bug)
813 +     */
814 +    public void testGet_NegativeInfinityTimeout() throws Exception {
815 +        final ExecutorService pool = Executors.newFixedThreadPool(10);
816 +        final Runnable nop = new Runnable() { public void run() {}};
817 +        final FutureTask<Void> task = new FutureTask<>(nop, null);
818 +        final List<Future<?>> futures = new ArrayList<>();
819 +        Runnable r = new Runnable() { public void run() {
820 +            for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) {
821 +                try {
822 +                    task.get(timeout, NANOSECONDS);
823 +                    shouldThrow();
824 +                } catch (TimeoutException success) {
825 +                } catch (Throwable fail) {threadUnexpectedException(fail);}}}};
826 +        for (int i = 0; i < 10; i++)
827 +            futures.add(pool.submit(r));
828 +        try {
829 +            joinPool(pool);
830 +            for (Future<?> future : futures)
831 +                checkCompletedNormally(future, null);
832 +        } finally {
833 +            task.run();         // last resort to help terminate
834 +        }
835 +    }
836 +
837 +    /**
838 +     * toString indicates current completion state
839 +     */
840 +    public void testToString_incomplete() {
841 +        FutureTask<String> f = new FutureTask<String>(() -> "");
842 +        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
843 +        if (testImplementationDetails)
844 +            assertTrue(f.toString().startsWith(
845 +                               identityString(f) + "[Not completed, task ="));
846 +    }
847 +
848 +    public void testToString_normal() {
849 +        FutureTask<String> f = new FutureTask<String>(() -> "");
850 +        f.run();
851 +        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
852 +        if (testImplementationDetails)
853 +            assertEquals(identityString(f) + "[Completed normally]",
854 +                         f.toString());
855 +    }
856 +
857 +    public void testToString_exception() {
858 +        FutureTask<String> f = new FutureTask<String>(
859 +                () -> { throw new ArithmeticException(); });
860 +        f.run();
861 +        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
862 +        if (testImplementationDetails)
863 +            assertTrue(f.toString().startsWith(
864 +                               identityString(f) + "[Completed exceptionally: "));
865 +    }
866 +
867 +    public void testToString_cancelled() {
868 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
869 +            FutureTask<String> f = new FutureTask<String>(() -> "");
870 +            assertTrue(f.cancel(mayInterruptIfRunning));
871 +            assertTrue(f.toString().matches(".*\\[.*Cancelled.*\\]"));
872 +            if (testImplementationDetails)
873 +                assertEquals(identityString(f) + "[Cancelled]",
874 +                             f.toString());
875 +        }
876 +    }
877 +
878   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines