ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java (file contents):
Revision 1.91 by jsr166, Tue Oct 6 05:30:44 2015 UTC vs.
Revision 1.95 by jsr166, Thu Oct 8 03:08:37 2015 UTC

# Line 248 | Line 248 | public class ThreadPoolExecutorSubclassT
248       * thread becomes active
249       */
250      public void testGetActiveCount() throws InterruptedException {
251 +        final CountDownLatch done = new CountDownLatch(1);
252          final ThreadPoolExecutor p =
253              new CustomTPE(2, 2,
254                            LONG_DELAY_MS, MILLISECONDS,
255                            new ArrayBlockingQueue<Runnable>(10));
256 <        try (PoolCleaner cleaner = cleaner(p)) {
256 >        try (PoolCleaner cleaner = cleaner(p, done)) {
257              final CountDownLatch threadStarted = new CountDownLatch(1);
257            final CountDownLatch done = new CountDownLatch(1);
258              assertEquals(0, p.getActiveCount());
259              p.execute(new CheckedRunnable() {
260                  public void realRun() throws InterruptedException {
# Line 264 | Line 264 | public class ThreadPoolExecutorSubclassT
264                  }});
265              await(threadStarted);
266              assertEquals(1, p.getActiveCount());
267            done.countDown();
267          }
268      }
269  
# Line 652 | Line 651 | public class ThreadPoolExecutorSubclassT
651       * getQueue returns the work queue, which contains queued tasks
652       */
653      public void testGetQueue() throws InterruptedException {
654 +        final CountDownLatch done = new CountDownLatch(1);
655          final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
656          final ThreadPoolExecutor p =
657              new CustomTPE(1, 1,
658                            LONG_DELAY_MS, MILLISECONDS,
659                            q);
660 <        try (PoolCleaner cleaner = cleaner(p)) {
660 >        try (PoolCleaner cleaner = cleaner(p, done)) {
661              final CountDownLatch threadStarted = new CountDownLatch(1);
662            final CountDownLatch done = new CountDownLatch(1);
662              FutureTask[] tasks = new FutureTask[5];
663              for (int i = 0; i < tasks.length; i++) {
664                  Callable task = new CheckedCallable<Boolean>() {
# Line 677 | Line 676 | public class ThreadPoolExecutorSubclassT
676              assertFalse(q.contains(tasks[0]));
677              assertTrue(q.contains(tasks[tasks.length - 1]));
678              assertEquals(tasks.length - 1, q.size());
680            done.countDown();
679          }
680      }
681  
# Line 685 | Line 683 | public class ThreadPoolExecutorSubclassT
683       * remove(task) removes queued task, and fails to remove active task
684       */
685      public void testRemove() throws InterruptedException {
686 +        final CountDownLatch done = new CountDownLatch(1);
687          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
688          final ThreadPoolExecutor p =
689              new CustomTPE(1, 1,
690                            LONG_DELAY_MS, MILLISECONDS,
691                            q);
692 <        try (PoolCleaner cleaner = cleaner(p)) {
692 >        try (PoolCleaner cleaner = cleaner(p, done)) {
693              Runnable[] tasks = new Runnable[6];
694              final CountDownLatch threadStarted = new CountDownLatch(1);
696            final CountDownLatch done = new CountDownLatch(1);
695              for (int i = 0; i < tasks.length; i++) {
696                  tasks[i] = new CheckedRunnable() {
697                      public void realRun() throws InterruptedException {
# Line 712 | Line 710 | public class ThreadPoolExecutorSubclassT
710              assertTrue(q.contains(tasks[3]));
711              assertTrue(p.remove(tasks[3]));
712              assertFalse(q.contains(tasks[3]));
715            done.countDown();
713          }
714      }
715  
# Line 1136 | Line 1133 | public class ThreadPoolExecutorSubclassT
1133       * execute throws RejectedExecutionException if saturated.
1134       */
1135      public void testSaturatedExecute() {
1136 +        final CountDownLatch done = new CountDownLatch(1);
1137          final ThreadPoolExecutor p =
1138              new CustomTPE(1, 1,
1139                            LONG_DELAY_MS, MILLISECONDS,
1140                            new ArrayBlockingQueue<Runnable>(1));
1141 <        try (PoolCleaner cleaner = cleaner(p)) {
1144 <            final CountDownLatch done = new CountDownLatch(1);
1141 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1142              Runnable task = new CheckedRunnable() {
1143                  public void realRun() throws InterruptedException {
1144                      await(done);
# Line 1155 | Line 1152 | public class ThreadPoolExecutorSubclassT
1152                  } catch (RejectedExecutionException success) {}
1153                  assertTrue(p.getTaskCount() <= 2);
1154              }
1158            done.countDown();
1155          }
1156      }
1157  
# Line 1163 | Line 1159 | public class ThreadPoolExecutorSubclassT
1159       * executor using CallerRunsPolicy runs task if saturated.
1160       */
1161      public void testSaturatedExecute2() {
1162 +        final CountDownLatch done = new CountDownLatch(1);
1163          final ThreadPoolExecutor p =
1164              new CustomTPE(1, 1,
1165                            LONG_DELAY_MS, MILLISECONDS,
1166                            new ArrayBlockingQueue<Runnable>(1),
1167                            new CustomTPE.CallerRunsPolicy());
1168 <        try (PoolCleaner cleaner = cleaner(p)) {
1172 <            final CountDownLatch done = new CountDownLatch(1);
1168 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1169              Runnable blocker = new CheckedRunnable() {
1170                  public void realRun() throws InterruptedException {
1171                      await(done);
# Line 1183 | Line 1179 | public class ThreadPoolExecutorSubclassT
1179              for (int i = 1; i < tasks.length; i++)
1180                  assertTrue(tasks[i].done);
1181              assertFalse(tasks[0].done); // waiting in queue
1186            done.countDown();
1182          }
1183      }
1184  
# Line 1194 | Line 1189 | public class ThreadPoolExecutorSubclassT
1189          final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1190          for (int i = 0; i < tasks.length; ++i)
1191              tasks[i] = new TrackedNoOpRunnable();
1192 +        final CountDownLatch done = new CountDownLatch(1);
1193          final ThreadPoolExecutor p =
1194              new CustomTPE(1, 1,
1195                            LONG_DELAY_MS, MILLISECONDS,
1196                            new ArrayBlockingQueue<Runnable>(1),
1197                            new CustomTPE.DiscardPolicy());
1198 <        try (PoolCleaner cleaner = cleaner(p)) {
1203 <            final CountDownLatch done = new CountDownLatch(1);
1198 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1199              p.execute(awaiter(done));
1200  
1201              for (TrackedNoOpRunnable task : tasks)
1202                  p.execute(task);
1203              for (int i = 1; i < tasks.length; i++)
1204                  assertFalse(tasks[i].done);
1210            done.countDown();
1205          }
1206          for (int i = 1; i < tasks.length; i++)
1207              assertFalse(tasks[i].done);
# Line 1227 | Line 1221 | public class ThreadPoolExecutorSubclassT
1221                            LONG_DELAY_MS, MILLISECONDS,
1222                            new ArrayBlockingQueue<Runnable>(1),
1223                            new CustomTPE.DiscardOldestPolicy());
1224 <        try (PoolCleaner cleaner = cleaner(p)) {
1224 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1225              assertEquals(LatchAwaiter.NEW, r1.state);
1226              assertEquals(LatchAwaiter.NEW, r2.state);
1227              assertEquals(LatchAwaiter.NEW, r3.state);
# Line 1237 | Line 1231 | public class ThreadPoolExecutorSubclassT
1231              p.execute(r3);
1232              assertFalse(p.getQueue().contains(r2));
1233              assertTrue(p.getQueue().contains(r3));
1240            done.countDown();
1234          }
1235          assertEquals(LatchAwaiter.DONE, r1.state);
1236          assertEquals(LatchAwaiter.NEW, r2.state);
# Line 1732 | Line 1725 | public class ThreadPoolExecutorSubclassT
1725                            LONG_DELAY_MS, MILLISECONDS,
1726                            new ArrayBlockingQueue<Runnable>(10));
1727          try (PoolCleaner cleaner = cleaner(e)) {
1728 +            long startTime = System.nanoTime();
1729              List<Callable<String>> l = new ArrayList<Callable<String>>();
1730              l.add(new NPETask());
1731              try {
1732 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1732 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1733                  shouldThrow();
1734              } catch (ExecutionException success) {
1735                  assertTrue(success.getCause() instanceof NullPointerException);
1736              }
1737 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1738          }
1739      }
1740  
# Line 1752 | Line 1747 | public class ThreadPoolExecutorSubclassT
1747                            LONG_DELAY_MS, MILLISECONDS,
1748                            new ArrayBlockingQueue<Runnable>(10));
1749          try (PoolCleaner cleaner = cleaner(e)) {
1750 +            long startTime = System.nanoTime();
1751              List<Callable<String>> l = new ArrayList<Callable<String>>();
1752              l.add(new StringTask());
1753              l.add(new StringTask());
1754 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1754 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1755              assertSame(TEST_STRING, result);
1756 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1757          }
1758      }
1759  
# Line 1875 | Line 1872 | public class ThreadPoolExecutorSubclassT
1872       * timed invokeAll(c) cancels tasks not completed by timeout
1873       */
1874      public void testTimedInvokeAll6() throws Exception {
1875 <        final ExecutorService e =
1876 <            new CustomTPE(2, 2,
1877 <                          LONG_DELAY_MS, MILLISECONDS,
1878 <                          new ArrayBlockingQueue<Runnable>(10));
1879 <        try (PoolCleaner cleaner = cleaner(e)) {
1880 <            for (long timeout = timeoutMillis();;) {
1875 >        for (long timeout = timeoutMillis();;) {
1876 >            final CountDownLatch done = new CountDownLatch(1);
1877 >            final Callable<String> waiter = new CheckedCallable<String>() {
1878 >                public String realCall() {
1879 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1880 >                    catch (InterruptedException ok) {}
1881 >                    return "1"; }};
1882 >            final ExecutorService p =
1883 >                new CustomTPE(2, 2,
1884 >                              LONG_DELAY_MS, MILLISECONDS,
1885 >                              new ArrayBlockingQueue<Runnable>(10));
1886 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1887                  List<Callable<String>> tasks = new ArrayList<>();
1888                  tasks.add(new StringTask("0"));
1889 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1889 >                tasks.add(waiter);
1890                  tasks.add(new StringTask("2"));
1891                  long startTime = System.nanoTime();
1892                  List<Future<String>> futures =
1893 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1893 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1894                  assertEquals(tasks.size(), futures.size());
1895                  assertTrue(millisElapsedSince(startTime) >= timeout);
1896                  for (Future future : futures)
# Line 1996 | Line 1999 | public class ThreadPoolExecutorSubclassT
1999       * (in part, a test of CustomTPE itself)
2000       */
2001      public void testGet_cancelled() throws Exception {
2002 +        final CountDownLatch done = new CountDownLatch(1);
2003          final ExecutorService e =
2004              new CustomTPE(1, 1,
2005                            LONG_DELAY_MS, MILLISECONDS,
2006                            new LinkedBlockingQueue<Runnable>());
2007 <        try (PoolCleaner cleaner = cleaner(e)) {
2007 >        try (PoolCleaner cleaner = cleaner(e, done)) {
2008              final CountDownLatch blockerStarted = new CountDownLatch(1);
2005            final CountDownLatch done = new CountDownLatch(1);
2009              final List<Future<?>> futures = new ArrayList<>();
2010              for (int i = 0; i < 2; i++) {
2011                  Runnable r = new CheckedRunnable() { public void realRun()
# Line 2026 | Line 2029 | public class ThreadPoolExecutorSubclassT
2029                  assertTrue(future.isCancelled());
2030                  assertTrue(future.isDone());
2031              }
2029            done.countDown();
2032          }
2033      }
2034  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines