[cvs] / jsr166 / src / test / tck / FutureTaskTest.java Repository:
ViewVC logotype

Diff of /jsr166/src/test/tck/FutureTaskTest.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.39, Fri Aug 22 03:30:56 2014 UTC revision 1.47, Wed Aug 16 17:18:34 2017 UTC
# Line 6  Line 6 
6   * Pat Fisher, Mike Judd.   * Pat Fisher, Mike Judd.
7   */   */
8    
9  import junit.framework.*;  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;  import java.util.concurrent.Callable;
16  import java.util.concurrent.CancellationException;  import java.util.concurrent.CancellationException;
17  import java.util.concurrent.CountDownLatch;  import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;  
 import java.util.concurrent.Executors;  
18  import java.util.concurrent.ExecutionException;  import java.util.concurrent.ExecutionException;
19    import java.util.concurrent.Executors;
20    import java.util.concurrent.ExecutorService;
21  import java.util.concurrent.Future;  import java.util.concurrent.Future;
22  import java.util.concurrent.FutureTask;  import java.util.concurrent.FutureTask;
23  import java.util.concurrent.TimeoutException;  import java.util.concurrent.TimeoutException;
24  import java.util.concurrent.atomic.AtomicInteger;  import java.util.concurrent.atomic.AtomicInteger;
25  import static java.util.concurrent.TimeUnit.*;  
26  import java.util.*;  import junit.framework.Test;
27    import junit.framework.TestSuite;
28    
29  public class FutureTaskTest extends JSR166TestCase {  public class FutureTaskTest extends JSR166TestCase {
30    
31      public static void main(String[] args) {      public static void main(String[] args) {
32          junit.textui.TestRunner.run(suite());          main(suite(), args);
33      }      }
34      public static Test suite() {      public static Test suite() {
35          return new TestSuite(FutureTaskTest.class);          return new TestSuite(FutureTaskTest.class);
# Line 101  Line 107 
107    
108          try {          try {
109              assertSame(expected, f.get());              assertSame(expected, f.get());
110          } catch (Throwable fail) { threadUnexpectedException(fail); }              assertSame(expected, f.get(randomTimeout(), randomTimeUnit()));
         try {  
             assertSame(expected, f.get(5L, SECONDS));  
111          } catch (Throwable fail) { threadUnexpectedException(fail); }          } catch (Throwable fail) { threadUnexpectedException(fail); }
112      }      }
113    
# Line 118  Line 122 
122          } catch (Throwable fail) { threadUnexpectedException(fail); }          } catch (Throwable fail) { threadUnexpectedException(fail); }
123    
124          try {          try {
125              f.get(5L, SECONDS);              f.get(randomTimeout(), randomTimeUnit());
126              shouldThrow();              shouldThrow();
127          } catch (CancellationException success) {          } catch (CancellationException success) {
128          } catch (Throwable fail) { threadUnexpectedException(fail); }          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 128  Line 132 
132          pf.set(new Object());          pf.set(new Object());
133          pf.setException(new Error());          pf.setException(new Error());
134          for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {          for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
135              pf.cancel(true);              pf.cancel(mayInterruptIfRunning);
136          }          }
137      }      }
138    
# Line 144  Line 148 
148          } catch (Throwable fail) { threadUnexpectedException(fail); }          } catch (Throwable fail) { threadUnexpectedException(fail); }
149    
150          try {          try {
151              f.get(5L, SECONDS);              f.get(randomTimeout(), randomTimeUnit());
152              shouldThrow();              shouldThrow();
153          } catch (ExecutionException success) {          } catch (ExecutionException success) {
154              assertSame(t, success.getCause());              assertSame(t, success.getCause());
# Line 410  Line 414 
414                          delay(LONG_DELAY_MS);                          delay(LONG_DELAY_MS);
415                          shouldThrow();                          shouldThrow();
416                      } catch (InterruptedException success) {}                      } catch (InterruptedException success) {}
417                        assertFalse(Thread.interrupted());
418                  }});                  }});
419    
420          Thread t = newStartedThread(task);          Thread t = newStartedThread(task);
# Line 479  Line 484 
484          final PublicFutureTask task =          final PublicFutureTask task =
485              new PublicFutureTask(new Runnable() {              new PublicFutureTask(new Runnable() {
486                  public void run() {                  public void run() {
                     try {  
487                          pleaseCancel.countDown();                          pleaseCancel.countDown();
488                        try {
489                          delay(LONG_DELAY_MS);                          delay(LONG_DELAY_MS);
490                          threadShouldThrow();                          threadShouldThrow();
491                      } catch (InterruptedException success) {                      } catch (InterruptedException success) {
# Line 829  Line 834 
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  }  }

Legend:
Removed from v.1.39  
changed lines
  Added in v.1.47

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8