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.40 by jsr166, Wed Dec 31 19:05:42 2014 UTC vs.
Revision 1.51 by jsr166, Sun Jan 7 22:59:18 2018 UTC

# Line 8 | Line 8
8  
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10   import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 import static java.util.concurrent.TimeUnit.SECONDS;
11  
12   import java.util.ArrayList;
13   import java.util.List;
# Line 30 | Line 29 | import junit.framework.TestSuite;
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 108 | Line 107 | public class FutureTaskTest extends JSR1
107  
108          try {
109              assertSame(expected, f.get());
110 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
112 <        try {
113 <            assertSame(expected, f.get(5L, SECONDS));
110 >            assertSame(expected, f.get(randomTimeout(), randomTimeUnit()));
111          } catch (Throwable fail) { threadUnexpectedException(fail); }
112      }
113  
# Line 125 | 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 135 | 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 151 | 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 267 | 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 284 | 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 417 | 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 486 | 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 {
490                        pleaseCancel.countDown();
489                          delay(LONG_DELAY_MS);
490                          threadShouldThrow();
491                      } catch (InterruptedException success) {
# Line 836 | Line 834 | public class FutureTaskTest extends JSR1
834          }
835      }
836  
837 +    /**
838 +     * toString indicates current completion state
839 +     */
840 +    public void testToString_incomplete() {
841 +        FutureTask<String> f = new FutureTask<>(() -> "");
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<>(() -> "");
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<>(
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<>(() -> "");
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