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.45 by jsr166, Sun May 14 00:56:43 2017 UTC vs.
Revision 1.53 by jsr166, Sun Jul 22 22:13:55 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 61 | Line 60 | public class FutureTaskTest extends JSR1
60              pf.run();
61              pf.runAndReset();
62              assertEquals(savedRunCount, pf.runCount());
63 +            Object r2 = null;
64              try {
65 <                assertSame(r, f.get());
65 >                r2 = f.get();
66              } catch (CancellationException t) {
67                  assertSame(exInfo, CancellationException.class);
68              } catch (ExecutionException t) {
# Line 70 | Line 70 | public class FutureTaskTest extends JSR1
70              } catch (Throwable t) {
71                  threadUnexpectedException(t);
72              }
73 +            if (exInfo == null)
74 +                assertSame(r, r2);
75              assertTrue(f.isDone());
76          }
77      }
# Line 102 | Line 104 | public class FutureTaskTest extends JSR1
104          }
105      }
106  
107 <    <T> void checkCompletedNormally(Future<T> f, T expected) {
107 >    <T> void checkCompletedNormally(Future<T> f, T expectedValue) {
108          checkIsDone(f);
109          assertFalse(f.isCancelled());
110  
111 +        T v1 = null, v2 = null;
112          try {
113 <            assertSame(expected, f.get());
114 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
112 <        try {
113 <            assertSame(expected, f.get(5L, SECONDS));
113 >            v1 = f.get();
114 >            v2 = f.get(randomTimeout(), randomTimeUnit());
115          } catch (Throwable fail) { threadUnexpectedException(fail); }
116 +        assertSame(expectedValue, v1);
117 +        assertSame(expectedValue, v2);
118      }
119  
120      void checkCancelled(Future<?> f) {
# Line 125 | Line 128 | public class FutureTaskTest extends JSR1
128          } catch (Throwable fail) { threadUnexpectedException(fail); }
129  
130          try {
131 <            f.get(5L, SECONDS);
131 >            f.get(randomTimeout(), randomTimeUnit());
132              shouldThrow();
133          } catch (CancellationException success) {
134          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 151 | Line 154 | public class FutureTaskTest extends JSR1
154          } catch (Throwable fail) { threadUnexpectedException(fail); }
155  
156          try {
157 <            f.get(5L, SECONDS);
157 >            f.get(randomTimeout(), randomTimeUnit());
158              shouldThrow();
159          } catch (ExecutionException success) {
160              assertSame(t, success.getCause());
# Line 461 | Line 464 | public class FutureTaskTest extends JSR1
464          try {
465              task.cancel(true);
466              shouldThrow();
467 <        } catch (SecurityException expected) {}
467 >        } catch (SecurityException success) {}
468  
469          // We failed to deliver the interrupt, but the world retains
470          // its sanity, as if we had done task.cancel(false)
# Line 837 | Line 840 | public class FutureTaskTest extends JSR1
840          }
841      }
842  
843 +    /**
844 +     * toString indicates current completion state
845 +     */
846 +    public void testToString_incomplete() {
847 +        FutureTask<String> f = new FutureTask<>(() -> "");
848 +        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
849 +        if (testImplementationDetails)
850 +            assertTrue(f.toString().startsWith(
851 +                               identityString(f) + "[Not completed, task ="));
852 +    }
853 +
854 +    public void testToString_normal() {
855 +        FutureTask<String> f = new FutureTask<>(() -> "");
856 +        f.run();
857 +        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
858 +        if (testImplementationDetails)
859 +            assertEquals(identityString(f) + "[Completed normally]",
860 +                         f.toString());
861 +    }
862 +
863 +    public void testToString_exception() {
864 +        FutureTask<String> f = new FutureTask<>(
865 +                () -> { throw new ArithmeticException(); });
866 +        f.run();
867 +        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
868 +        if (testImplementationDetails)
869 +            assertTrue(f.toString().startsWith(
870 +                               identityString(f) + "[Completed exceptionally: "));
871 +    }
872 +
873 +    public void testToString_cancelled() {
874 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
875 +            FutureTask<String> f = new FutureTask<>(() -> "");
876 +            assertTrue(f.cancel(mayInterruptIfRunning));
877 +            assertTrue(f.toString().matches(".*\\[.*Cancelled.*\\]"));
878 +            if (testImplementationDetails)
879 +                assertEquals(identityString(f) + "[Cancelled]",
880 +                             f.toString());
881 +        }
882 +    }
883 +
884   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines