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.82 by jsr166, Sun Oct 4 07:23:20 2015 UTC vs.
Revision 1.94 by jsr166, Thu Oct 8 03:03:36 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 {
261                      threadStarted.countDown();
262                      assertEquals(1, p.getActiveCount());
263 <                    done.await();
263 >                    await(done);
264                  }});
265 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
265 >            await(threadStarted);
266              assertEquals(1, p.getActiveCount());
267            done.countDown();
267          }
268      }
269  
# Line 476 | Line 475 | public class ThreadPoolExecutorSubclassT
475       */
476      public void testGetLargestPoolSize() throws InterruptedException {
477          final int THREADS = 3;
478 +        final CountDownLatch done = new CountDownLatch(1);
479          final ThreadPoolExecutor p =
480              new CustomTPE(THREADS, THREADS,
481                            LONG_DELAY_MS, MILLISECONDS,
482                            new ArrayBlockingQueue<Runnable>(10));
483 <        try (PoolCleaner cleaner = cleaner(p)) {
484 <            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
485 <            final CountDownLatch done = new CountDownLatch(1);
483 >        try (PoolCleaner cleaner = cleaner(p, done)) {
484              assertEquals(0, p.getLargestPoolSize());
485 +            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
486              for (int i = 0; i < THREADS; i++)
487                  p.execute(new CheckedRunnable() {
488                      public void realRun() throws InterruptedException {
489                          threadsStarted.countDown();
490 <                        done.await();
490 >                        await(done);
491                          assertEquals(THREADS, p.getLargestPoolSize());
492                      }});
493 <            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
493 >            await(threadsStarted);
494              assertEquals(THREADS, p.getLargestPoolSize());
496            done.countDown();   // release pool
495          }
496          assertEquals(THREADS, p.getLargestPoolSize());
497      }
# Line 521 | Line 519 | public class ThreadPoolExecutorSubclassT
519       * become active
520       */
521      public void testGetPoolSize() throws InterruptedException {
522 +        final CountDownLatch done = new CountDownLatch(1);
523          final ThreadPoolExecutor p =
524              new CustomTPE(1, 1,
525                            LONG_DELAY_MS, MILLISECONDS,
526                            new ArrayBlockingQueue<Runnable>(10));
527 <        try (PoolCleaner cleaner = cleaner(p)) {
529 <            final CountDownLatch threadStarted = new CountDownLatch(1);
530 <            final CountDownLatch done = new CountDownLatch(1);
527 >        try (PoolCleaner cleaner = cleaner(p, done)) {
528              assertEquals(0, p.getPoolSize());
529 +            final CountDownLatch threadStarted = new CountDownLatch(1);
530              p.execute(new CheckedRunnable() {
531                  public void realRun() throws InterruptedException {
532                      threadStarted.countDown();
533                      assertEquals(1, p.getPoolSize());
534 <                    done.await();
534 >                    await(done);
535                  }});
536 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
536 >            await(threadStarted);
537              assertEquals(1, p.getPoolSize());
540            done.countDown();   // release pool
538          }
539      }
540  
# Line 545 | Line 542 | public class ThreadPoolExecutorSubclassT
542       * getTaskCount increases, but doesn't overestimate, when tasks submitted
543       */
544      public void testGetTaskCount() throws InterruptedException {
545 +        final int TASKS = 3;
546 +        final CountDownLatch done = new CountDownLatch(1);
547          final ThreadPoolExecutor p =
548              new CustomTPE(1, 1,
549                            LONG_DELAY_MS, MILLISECONDS,
550                            new ArrayBlockingQueue<Runnable>(10));
551 <        try (PoolCleaner cleaner = cleaner(p)) {
551 >        try (PoolCleaner cleaner = cleaner(p, done)) {
552              final CountDownLatch threadStarted = new CountDownLatch(1);
554            final CountDownLatch done = new CountDownLatch(1);
553              assertEquals(0, p.getTaskCount());
554 +            assertEquals(0, p.getCompletedTaskCount());
555              p.execute(new CheckedRunnable() {
556                  public void realRun() throws InterruptedException {
557                      threadStarted.countDown();
558 <                    assertEquals(1, p.getTaskCount());
560 <                    done.await();
558 >                    await(done);
559                  }});
560 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
560 >            await(threadStarted);
561              assertEquals(1, p.getTaskCount());
562 <            done.countDown();
562 >            assertEquals(0, p.getCompletedTaskCount());
563 >            for (int i = 0; i < TASKS; i++) {
564 >                assertEquals(1 + i, p.getTaskCount());
565 >                p.execute(new CheckedRunnable() {
566 >                    public void realRun() throws InterruptedException {
567 >                        threadStarted.countDown();
568 >                        assertEquals(1 + TASKS, p.getTaskCount());
569 >                        await(done);
570 >                    }});
571 >            }
572 >            assertEquals(1 + TASKS, p.getTaskCount());
573 >            assertEquals(0, p.getCompletedTaskCount());
574          }
575 +        assertEquals(1 + TASKS, p.getTaskCount());
576 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
577      }
578  
579      /**
# Line 596 | Line 607 | public class ThreadPoolExecutorSubclassT
607                  public void realRun() throws InterruptedException {
608                      assertFalse(p.isTerminating());
609                      threadStarted.countDown();
610 <                    done.await();
610 >                    await(done);
611                  }});
612 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
612 >            await(threadStarted);
613              assertFalse(p.isTerminating());
614              done.countDown();
615              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 624 | Line 635 | public class ThreadPoolExecutorSubclassT
635                  public void realRun() throws InterruptedException {
636                      assertFalse(p.isTerminating());
637                      threadStarted.countDown();
638 <                    done.await();
638 >                    await(done);
639                  }});
640 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
640 >            await(threadStarted);
641              assertFalse(p.isTerminating());
642              done.countDown();
643              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 640 | 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);
650            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>() {
665                      public Boolean realCall() throws InterruptedException {
666                          threadStarted.countDown();
667                          assertSame(q, p.getQueue());
668 <                        done.await();
668 >                        await(done);
669                          return Boolean.TRUE;
670                      }};
671                  tasks[i] = new FutureTask(task);
672                  p.execute(tasks[i]);
673              }
674 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
674 >            await(threadStarted);
675              assertSame(q, p.getQueue());
676              assertFalse(q.contains(tasks[0]));
677              assertTrue(q.contains(tasks[tasks.length - 1]));
678              assertEquals(tasks.length - 1, q.size());
668            done.countDown();
679          }
680      }
681  
# Line 673 | 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);
684            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 {
698                          threadStarted.countDown();
699 <                        done.await();
699 >                        await(done);
700                      }};
701                  p.execute(tasks[i]);
702              }
703 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
703 >            await(threadStarted);
704              assertFalse(p.remove(tasks[0]));
705              assertTrue(q.contains(tasks[4]));
706              assertTrue(q.contains(tasks[3]));
# Line 700 | Line 710 | public class ThreadPoolExecutorSubclassT
710              assertTrue(q.contains(tasks[3]));
711              assertTrue(p.remove(tasks[3]));
712              assertFalse(q.contains(tasks[3]));
703            done.countDown();
713          }
714      }
715  
# Line 715 | Line 724 | public class ThreadPoolExecutorSubclassT
724              new CustomTPE(1, 1,
725                            LONG_DELAY_MS, MILLISECONDS,
726                            q);
727 <        try (PoolCleaner cleaner = cleaner(p)) {
727 >        try (PoolCleaner cleaner = cleaner(p, done)) {
728              FutureTask[] tasks = new FutureTask[5];
729              for (int i = 0; i < tasks.length; i++) {
730                  Callable task = new CheckedCallable<Boolean>() {
731                      public Boolean realCall() throws InterruptedException {
732                          threadStarted.countDown();
733 <                        done.await();
733 >                        await(done);
734                          return Boolean.TRUE;
735                      }};
736                  tasks[i] = new FutureTask(task);
737                  p.execute(tasks[i]);
738              }
739 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
739 >            await(threadStarted);
740              assertEquals(tasks.length, p.getTaskCount());
741              assertEquals(tasks.length - 1, q.size());
742              assertEquals(1L, p.getActiveCount());
# Line 740 | Line 749 | public class ThreadPoolExecutorSubclassT
749              p.purge();         // Nothing to do
750              assertEquals(tasks.length - 3, q.size());
751              assertEquals(tasks.length - 2, p.getTaskCount());
743            done.countDown();
752          }
753      }
754  
# Line 756 | Line 764 | public class ThreadPoolExecutorSubclassT
764              new CustomTPE(poolSize, poolSize,
765                            LONG_DELAY_MS, MILLISECONDS,
766                            new ArrayBlockingQueue<Runnable>(10));
767 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
767 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
768          Runnable waiter = new CheckedRunnable() { public void realRun() {
769              threadsStarted.countDown();
770              try {
# Line 766 | Line 774 | public class ThreadPoolExecutorSubclassT
774          }};
775          for (int i = 0; i < count; i++)
776              p.execute(waiter);
777 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
777 >        await(threadsStarted);
778          assertEquals(poolSize, p.getActiveCount());
779          assertEquals(0, p.getCompletedTaskCount());
780          final List<Runnable> queuedTasks;
# Line 1125 | 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)) {
1133 <            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 <                    done.await();
1144 >                    await(done);
1145                  }};
1146              for (int i = 0; i < 2; ++i)
1147                  p.execute(task);
# Line 1144 | Line 1152 | public class ThreadPoolExecutorSubclassT
1152                  } catch (RejectedExecutionException success) {}
1153                  assertTrue(p.getTaskCount() <= 2);
1154              }
1147            done.countDown();
1155          }
1156      }
1157  
# Line 1152 | 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)) {
1161 <            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 <                    done.await();
1171 >                    await(done);
1172                  }};
1173              p.execute(blocker);
1174              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
# Line 1172 | 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
1175            done.countDown();
1182          }
1183      }
1184  
# Line 1183 | 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)) {
1192 <            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);
1199            done.countDown();
1205          }
1206          for (int i = 1; i < tasks.length; i++)
1207              assertFalse(tasks[i].done);
# Line 1216 | 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 1226 | Line 1231 | public class ThreadPoolExecutorSubclassT
1231              p.execute(r3);
1232              assertFalse(p.getQueue().contains(r2));
1233              assertTrue(p.getQueue().contains(r3));
1229            done.countDown();
1234          }
1235          assertEquals(LatchAwaiter.DONE, r1.state);
1236          assertEquals(LatchAwaiter.NEW, r2.state);
# Line 1721 | 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 1829 | Line 1835 | public class ThreadPoolExecutorSubclassT
1835              List<Callable<String>> l = new ArrayList<Callable<String>>();
1836              l.add(new NPETask());
1837              List<Future<String>> futures =
1838 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1838 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1839              assertEquals(1, futures.size());
1840              try {
1841                  futures.get(0).get();
# Line 1864 | Line 1870 | public class ThreadPoolExecutorSubclassT
1870       * timed invokeAll(c) cancels tasks not completed by timeout
1871       */
1872      public void testTimedInvokeAll6() throws Exception {
1873 <        final ExecutorService e =
1874 <            new CustomTPE(2, 2,
1875 <                          LONG_DELAY_MS, MILLISECONDS,
1876 <                          new ArrayBlockingQueue<Runnable>(10));
1877 <        try (PoolCleaner cleaner = cleaner(e)) {
1878 <            for (long timeout = timeoutMillis();;) {
1873 >        for (long timeout = timeoutMillis();;) {
1874 >            final CountDownLatch done = new CountDownLatch(1);
1875 >            final Callable<String> waiter = new CheckedCallable<String>() {
1876 >                public String realCall() {
1877 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1878 >                    catch (InterruptedException ok) {}
1879 >                    return "1"; }};
1880 >            final ExecutorService p =
1881 >                new CustomTPE(2, 2,
1882 >                              LONG_DELAY_MS, MILLISECONDS,
1883 >                              new ArrayBlockingQueue<Runnable>(10));
1884 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1885                  List<Callable<String>> tasks = new ArrayList<>();
1886                  tasks.add(new StringTask("0"));
1887 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1887 >                tasks.add(waiter);
1888                  tasks.add(new StringTask("2"));
1889                  long startTime = System.nanoTime();
1890                  List<Future<String>> futures =
1891 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1891 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1892                  assertEquals(tasks.size(), futures.size());
1893                  assertTrue(millisElapsedSince(startTime) >= timeout);
1894                  for (Future future : futures)
# Line 1985 | Line 1997 | public class ThreadPoolExecutorSubclassT
1997       * (in part, a test of CustomTPE itself)
1998       */
1999      public void testGet_cancelled() throws Exception {
2000 +        final CountDownLatch done = new CountDownLatch(1);
2001          final ExecutorService e =
2002              new CustomTPE(1, 1,
2003                            LONG_DELAY_MS, MILLISECONDS,
2004                            new LinkedBlockingQueue<Runnable>());
2005 <        try (PoolCleaner cleaner = cleaner(e)) {
2005 >        try (PoolCleaner cleaner = cleaner(e, done)) {
2006              final CountDownLatch blockerStarted = new CountDownLatch(1);
1994            final CountDownLatch done = new CountDownLatch(1);
2007              final List<Future<?>> futures = new ArrayList<>();
2008              for (int i = 0; i < 2; i++) {
2009                  Runnable r = new CheckedRunnable() { public void realRun()
# Line 2001 | Line 2013 | public class ThreadPoolExecutorSubclassT
2013                  }};
2014                  futures.add(e.submit(r));
2015              }
2016 <            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2016 >            await(blockerStarted);
2017              for (Future<?> future : futures) future.cancel(false);
2018              for (Future<?> future : futures) {
2019                  try {
# Line 2015 | Line 2027 | public class ThreadPoolExecutorSubclassT
2027                  assertTrue(future.isCancelled());
2028                  assertTrue(future.isDone());
2029              }
2018            done.countDown();
2030          }
2031      }
2032  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines