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

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.106 by jsr166, Tue Oct 6 05:12:16 2015 UTC vs.
Revision 1.112 by jsr166, Thu Oct 8 03:08:38 2015 UTC

# Line 101 | Line 101 | public class ThreadPoolExecutorTest exte
101       * thread becomes active
102       */
103      public void testGetActiveCount() throws InterruptedException {
104 +        final CountDownLatch done = new CountDownLatch(1);
105          final ThreadPoolExecutor p =
106              new ThreadPoolExecutor(2, 2,
107                                     LONG_DELAY_MS, MILLISECONDS,
108                                     new ArrayBlockingQueue<Runnable>(10));
109 <        try (PoolCleaner cleaner = cleaner(p)) {
109 >        try (PoolCleaner cleaner = cleaner(p, done)) {
110              final CountDownLatch threadStarted = new CountDownLatch(1);
110            final CountDownLatch done = new CountDownLatch(1);
111              assertEquals(0, p.getActiveCount());
112              p.execute(new CheckedRunnable() {
113                  public void realRun() throws InterruptedException {
# Line 115 | Line 115 | public class ThreadPoolExecutorTest exte
115                      assertEquals(1, p.getActiveCount());
116                      await(done);
117                  }});
118 <            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
118 >            await(threadStarted);
119              assertEquals(1, p.getActiveCount());
120            done.countDown();
120          }
121      }
122  
# Line 344 | Line 343 | public class ThreadPoolExecutorTest exte
343                          await(done);
344                          assertEquals(THREADS, p.getLargestPoolSize());
345                      }});
346 <            assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
346 >            await(threadsStarted);
347              assertEquals(THREADS, p.getLargestPoolSize());
348          }
349          assertEquals(THREADS, p.getLargestPoolSize());
# Line 387 | Line 386 | public class ThreadPoolExecutorTest exte
386                      assertEquals(1, p.getPoolSize());
387                      await(done);
388                  }});
389 <            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
389 >            await(threadStarted);
390              assertEquals(1, p.getPoolSize());
391          }
392      }
# Line 411 | Line 410 | public class ThreadPoolExecutorTest exte
410                      threadStarted.countDown();
411                      await(done);
412                  }});
413 <            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
413 >            await(threadStarted);
414              assertEquals(1, p.getTaskCount());
415              assertEquals(0, p.getCompletedTaskCount());
416              for (int i = 0; i < TASKS; i++) {
# Line 495 | Line 494 | public class ThreadPoolExecutorTest exte
494                      threadStarted.countDown();
495                      await(done);
496                  }});
497 <            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
497 >            await(threadStarted);
498              assertFalse(p.isTerminating());
499              done.countDown();
500              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 523 | Line 522 | public class ThreadPoolExecutorTest exte
522                      threadStarted.countDown();
523                      await(done);
524                  }});
525 <            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
525 >            await(threadStarted);
526              assertFalse(p.isTerminating());
527              done.countDown();
528              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 537 | Line 536 | public class ThreadPoolExecutorTest exte
536       * getQueue returns the work queue, which contains queued tasks
537       */
538      public void testGetQueue() throws InterruptedException {
539 +        final CountDownLatch done = new CountDownLatch(1);
540          final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
541          final ThreadPoolExecutor p =
542              new ThreadPoolExecutor(1, 1,
543                                     LONG_DELAY_MS, MILLISECONDS,
544                                     q);
545 <        try (PoolCleaner cleaner = cleaner(p)) {
545 >        try (PoolCleaner cleaner = cleaner(p, done)) {
546              final CountDownLatch threadStarted = new CountDownLatch(1);
547            final CountDownLatch done = new CountDownLatch(1);
547              FutureTask[] tasks = new FutureTask[5];
548              for (int i = 0; i < tasks.length; i++) {
549                  Callable task = new CheckedCallable<Boolean>() {
# Line 557 | Line 556 | public class ThreadPoolExecutorTest exte
556                  tasks[i] = new FutureTask(task);
557                  p.execute(tasks[i]);
558              }
559 <            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
559 >            await(threadStarted);
560              assertSame(q, p.getQueue());
561              assertFalse(q.contains(tasks[0]));
562              assertTrue(q.contains(tasks[tasks.length - 1]));
563              assertEquals(tasks.length - 1, q.size());
565            done.countDown();
564          }
565      }
566  
# Line 570 | Line 568 | public class ThreadPoolExecutorTest exte
568       * remove(task) removes queued task, and fails to remove active task
569       */
570      public void testRemove() throws InterruptedException {
571 +        final CountDownLatch done = new CountDownLatch(1);
572          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
573          final ThreadPoolExecutor p =
574              new ThreadPoolExecutor(1, 1,
575                                     LONG_DELAY_MS, MILLISECONDS,
576                                     q);
577 <        try (PoolCleaner cleaner = cleaner(p)) {
577 >        try (PoolCleaner cleaner = cleaner(p, done)) {
578              Runnable[] tasks = new Runnable[6];
579              final CountDownLatch threadStarted = new CountDownLatch(1);
581            final CountDownLatch done = new CountDownLatch(1);
580              for (int i = 0; i < tasks.length; i++) {
581                  tasks[i] = new CheckedRunnable() {
582                      public void realRun() throws InterruptedException {
# Line 587 | Line 585 | public class ThreadPoolExecutorTest exte
585                      }};
586                  p.execute(tasks[i]);
587              }
588 <            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
588 >            await(threadStarted);
589              assertFalse(p.remove(tasks[0]));
590              assertTrue(q.contains(tasks[4]));
591              assertTrue(q.contains(tasks[3]));
# Line 597 | Line 595 | public class ThreadPoolExecutorTest exte
595              assertTrue(q.contains(tasks[3]));
596              assertTrue(p.remove(tasks[3]));
597              assertFalse(q.contains(tasks[3]));
600            done.countDown();
598          }
599      }
600  
# Line 624 | Line 621 | public class ThreadPoolExecutorTest exte
621                  tasks[i] = new FutureTask(task);
622                  p.execute(tasks[i]);
623              }
624 <            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
624 >            await(threadStarted);
625              assertEquals(tasks.length, p.getTaskCount());
626              assertEquals(tasks.length - 1, q.size());
627              assertEquals(1L, p.getActiveCount());
# Line 662 | Line 659 | public class ThreadPoolExecutorTest exte
659          }};
660          for (int i = 0; i < count; i++)
661              p.execute(waiter);
662 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
662 >        await(threadsStarted);
663          assertEquals(poolSize, p.getActiveCount());
664          assertEquals(0, p.getCompletedTaskCount());
665          final List<Runnable> queuedTasks;
# Line 1053 | Line 1050 | public class ThreadPoolExecutorTest exte
1050       * execute throws RejectedExecutionException if saturated.
1051       */
1052      public void testSaturatedExecute() {
1053 +        final CountDownLatch done = new CountDownLatch(1);
1054          final ThreadPoolExecutor p =
1055              new ThreadPoolExecutor(1, 1,
1056                                     LONG_DELAY_MS, MILLISECONDS,
1057                                     new ArrayBlockingQueue<Runnable>(1));
1058 <        try (PoolCleaner cleaner = cleaner(p)) {
1061 <            final CountDownLatch done = new CountDownLatch(1);
1058 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1059              Runnable task = new CheckedRunnable() {
1060                  public void realRun() throws InterruptedException {
1061                      await(done);
# Line 1072 | Line 1069 | public class ThreadPoolExecutorTest exte
1069                  } catch (RejectedExecutionException success) {}
1070                  assertTrue(p.getTaskCount() <= 2);
1071              }
1075            done.countDown();
1072          }
1073      }
1074  
# Line 1080 | Line 1076 | public class ThreadPoolExecutorTest exte
1076       * submit(runnable) throws RejectedExecutionException if saturated.
1077       */
1078      public void testSaturatedSubmitRunnable() {
1079 +        final CountDownLatch done = new CountDownLatch(1);
1080          final ThreadPoolExecutor p =
1081              new ThreadPoolExecutor(1, 1,
1082                                     LONG_DELAY_MS, MILLISECONDS,
1083                                     new ArrayBlockingQueue<Runnable>(1));
1084 <        try (PoolCleaner cleaner = cleaner(p)) {
1088 <            final CountDownLatch done = new CountDownLatch(1);
1084 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1085              Runnable task = new CheckedRunnable() {
1086                  public void realRun() throws InterruptedException {
1087                      await(done);
# Line 1099 | Line 1095 | public class ThreadPoolExecutorTest exte
1095                  } catch (RejectedExecutionException success) {}
1096                  assertTrue(p.getTaskCount() <= 2);
1097              }
1102            done.countDown();
1098          }
1099      }
1100  
# Line 1107 | Line 1102 | public class ThreadPoolExecutorTest exte
1102       * submit(callable) throws RejectedExecutionException if saturated.
1103       */
1104      public void testSaturatedSubmitCallable() {
1105 +        final CountDownLatch done = new CountDownLatch(1);
1106          final ThreadPoolExecutor p =
1107              new ThreadPoolExecutor(1, 1,
1108                                     LONG_DELAY_MS, MILLISECONDS,
1109                                     new ArrayBlockingQueue<Runnable>(1));
1110 <        try (PoolCleaner cleaner = cleaner(p)) {
1115 <            final CountDownLatch done = new CountDownLatch(1);
1110 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1111              Runnable task = new CheckedRunnable() {
1112                  public void realRun() throws InterruptedException {
1113                      await(done);
# Line 1126 | Line 1121 | public class ThreadPoolExecutorTest exte
1121                  } catch (RejectedExecutionException success) {}
1122                  assertTrue(p.getTaskCount() <= 2);
1123              }
1129            done.countDown();
1124          }
1125      }
1126  
# Line 1163 | Line 1157 | public class ThreadPoolExecutorTest exte
1157       * executor using DiscardPolicy drops task if saturated.
1158       */
1159      public void testSaturatedExecute3() {
1160 +        final CountDownLatch done = new CountDownLatch(1);
1161          final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1162          for (int i = 0; i < tasks.length; ++i)
1163              tasks[i] = new TrackedNoOpRunnable();
# Line 1171 | Line 1166 | public class ThreadPoolExecutorTest exte
1166                            LONG_DELAY_MS, MILLISECONDS,
1167                            new ArrayBlockingQueue<Runnable>(1),
1168                            new ThreadPoolExecutor.DiscardPolicy());
1169 <        try (PoolCleaner cleaner = cleaner(p)) {
1175 <            final CountDownLatch done = new CountDownLatch(1);
1169 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1170              p.execute(awaiter(done));
1171  
1172              for (TrackedNoOpRunnable task : tasks)
1173                  p.execute(task);
1174              for (int i = 1; i < tasks.length; i++)
1175                  assertFalse(tasks[i].done);
1182            done.countDown();
1176          }
1177          for (int i = 1; i < tasks.length; i++)
1178              assertFalse(tasks[i].done);
# Line 1199 | Line 1192 | public class ThreadPoolExecutorTest exte
1192                                     LONG_DELAY_MS, MILLISECONDS,
1193                                     new ArrayBlockingQueue<Runnable>(1),
1194                                     new ThreadPoolExecutor.DiscardOldestPolicy());
1195 <        try (PoolCleaner cleaner = cleaner(p)) {
1195 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1196              assertEquals(LatchAwaiter.NEW, r1.state);
1197              assertEquals(LatchAwaiter.NEW, r2.state);
1198              assertEquals(LatchAwaiter.NEW, r3.state);
# Line 1209 | Line 1202 | public class ThreadPoolExecutorTest exte
1202              p.execute(r3);
1203              assertFalse(p.getQueue().contains(r2));
1204              assertTrue(p.getQueue().contains(r3));
1212            done.countDown();
1205          }
1206          assertEquals(LatchAwaiter.DONE, r1.state);
1207          assertEquals(LatchAwaiter.NEW, r2.state);
# Line 1735 | Line 1727 | public class ThreadPoolExecutorTest exte
1727                                     LONG_DELAY_MS, MILLISECONDS,
1728                                     new ArrayBlockingQueue<Runnable>(10));
1729          try (PoolCleaner cleaner = cleaner(e)) {
1730 +            long startTime = System.nanoTime();
1731              List<Callable<String>> l = new ArrayList<Callable<String>>();
1732              l.add(new NPETask());
1733              try {
1734 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1734 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1735                  shouldThrow();
1736              } catch (ExecutionException success) {
1737                  assertTrue(success.getCause() instanceof NullPointerException);
1738              }
1739 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1740          }
1741      }
1742  
# Line 1755 | Line 1749 | public class ThreadPoolExecutorTest exte
1749                                     LONG_DELAY_MS, MILLISECONDS,
1750                                     new ArrayBlockingQueue<Runnable>(10));
1751          try (PoolCleaner cleaner = cleaner(e)) {
1752 +            long startTime = System.nanoTime();
1753              List<Callable<String>> l = new ArrayList<Callable<String>>();
1754              l.add(new StringTask());
1755              l.add(new StringTask());
1756 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1756 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1757              assertSame(TEST_STRING, result);
1758 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1759          }
1760      }
1761  
# Line 1878 | Line 1874 | public class ThreadPoolExecutorTest exte
1874       * timed invokeAll(c) cancels tasks not completed by timeout
1875       */
1876      public void testTimedInvokeAll6() throws Exception {
1877 <        final ExecutorService e =
1878 <            new ThreadPoolExecutor(2, 2,
1879 <                                   LONG_DELAY_MS, MILLISECONDS,
1880 <                                   new ArrayBlockingQueue<Runnable>(10));
1881 <        try (PoolCleaner cleaner = cleaner(e)) {
1882 <            for (long timeout = timeoutMillis();;) {
1877 >        for (long timeout = timeoutMillis();;) {
1878 >            final CountDownLatch done = new CountDownLatch(1);
1879 >            final Callable<String> waiter = new CheckedCallable<String>() {
1880 >                public String realCall() {
1881 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1882 >                    catch (InterruptedException ok) {}
1883 >                    return "1"; }};
1884 >            final ExecutorService p =
1885 >                new ThreadPoolExecutor(2, 2,
1886 >                                       LONG_DELAY_MS, MILLISECONDS,
1887 >                                       new ArrayBlockingQueue<Runnable>(10));
1888 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1889                  List<Callable<String>> tasks = new ArrayList<>();
1890                  tasks.add(new StringTask("0"));
1891 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1891 >                tasks.add(waiter);
1892                  tasks.add(new StringTask("2"));
1893                  long startTime = System.nanoTime();
1894                  List<Future<String>> futures =
1895 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1895 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1896                  assertEquals(tasks.size(), futures.size());
1897                  assertTrue(millisElapsedSince(startTime) >= timeout);
1898                  for (Future future : futures)
# Line 2028 | Line 2030 | public class ThreadPoolExecutorTest exte
2030       * get(cancelled task) throws CancellationException
2031       */
2032      public void testGet_cancelled() throws Exception {
2033 +        final CountDownLatch done = new CountDownLatch(1);
2034          final ExecutorService e =
2035              new ThreadPoolExecutor(1, 1,
2036                                     LONG_DELAY_MS, MILLISECONDS,
2037                                     new LinkedBlockingQueue<Runnable>());
2038 <        try (PoolCleaner cleaner = cleaner(e)) {
2038 >        try (PoolCleaner cleaner = cleaner(e, done)) {
2039              final CountDownLatch blockerStarted = new CountDownLatch(1);
2037            final CountDownLatch done = new CountDownLatch(1);
2040              final List<Future<?>> futures = new ArrayList<>();
2041              for (int i = 0; i < 2; i++) {
2042                  Runnable r = new CheckedRunnable() { public void realRun()
# Line 2044 | Line 2046 | public class ThreadPoolExecutorTest exte
2046                  }};
2047                  futures.add(e.submit(r));
2048              }
2049 <            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2049 >            await(blockerStarted);
2050              for (Future<?> future : futures) future.cancel(false);
2051              for (Future<?> future : futures) {
2052                  try {
# Line 2058 | Line 2060 | public class ThreadPoolExecutorTest exte
2060                  assertTrue(future.isCancelled());
2061                  assertTrue(future.isDone());
2062              }
2061            done.countDown();
2063          }
2064      }
2065  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines