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.27 by jsr166, Sat May 7 19:34:51 2011 UTC vs.
Revision 1.33 by jsr166, Wed Dec 31 19:05:43 2014 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 import java.util.concurrent.locks.*;
10  
11 < import junit.framework.*;
12 < import java.util.*;
11 > import java.util.ArrayList;
12 > import java.util.List;
13 > import java.util.concurrent.ArrayBlockingQueue;
14 > import java.util.concurrent.BlockingQueue;
15 > import java.util.concurrent.Callable;
16 > import java.util.concurrent.CountDownLatch;
17 > import java.util.concurrent.ExecutionException;
18 > import java.util.concurrent.Executors;
19 > import java.util.concurrent.ExecutorService;
20 > import java.util.concurrent.Future;
21 > import java.util.concurrent.FutureTask;
22 > import java.util.concurrent.LinkedBlockingQueue;
23 > import java.util.concurrent.RejectedExecutionException;
24 > import java.util.concurrent.RejectedExecutionHandler;
25 > import java.util.concurrent.RunnableFuture;
26 > import java.util.concurrent.SynchronousQueue;
27 > import java.util.concurrent.ThreadFactory;
28 > import java.util.concurrent.ThreadPoolExecutor;
29 > import java.util.concurrent.TimeoutException;
30 > import java.util.concurrent.TimeUnit;
31 > import java.util.concurrent.locks.Condition;
32 > import java.util.concurrent.locks.ReentrantLock;
33 >
34 > import junit.framework.Test;
35 > import junit.framework.TestSuite;
36  
37   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
38      public static void main(String[] args) {
# Line 115 | Line 136 | public class ThreadPoolExecutorSubclassT
136          }
137      }
138  
118
139      static class CustomTPE extends ThreadPoolExecutor {
140          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
141              return new CustomTask<V>(c);
# Line 162 | Line 182 | public class ThreadPoolExecutorSubclassT
182                workQueue, threadFactory, handler);
183          }
184  
185 <        volatile boolean beforeCalled = false;
186 <        volatile boolean afterCalled = false;
187 <        volatile boolean terminatedCalled = false;
185 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
186 >        final CountDownLatch afterCalled = new CountDownLatch(1);
187 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
188 >
189          public CustomTPE() {
190              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
191          }
192          protected void beforeExecute(Thread t, Runnable r) {
193 <            beforeCalled = true;
193 >            beforeCalled.countDown();
194          }
195          protected void afterExecute(Runnable r, Throwable t) {
196 <            afterCalled = true;
196 >            afterCalled.countDown();
197          }
198          protected void terminated() {
199 <            terminatedCalled = true;
199 >            terminatedCalled.countDown();
200          }
201  
202 +        public boolean beforeCalled() {
203 +            return beforeCalled.getCount() == 0;
204 +        }
205 +        public boolean afterCalled() {
206 +            return afterCalled.getCount() == 0;
207 +        }
208 +        public boolean terminatedCalled() {
209 +            return terminatedCalled.getCount() == 0;
210 +        }
211      }
212  
213      static class FailingThreadFactory implements ThreadFactory {
# Line 188 | Line 218 | public class ThreadPoolExecutorSubclassT
218          }
219      }
220  
191
221      /**
222       * execute successfully executes a runnable
223       */
# Line 286 | Line 315 | public class ThreadPoolExecutorSubclassT
315                      threadProceed.await();
316                      threadDone.countDown();
317                  }});
318 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
318 >            await(threadStarted);
319              assertEquals(0, p.getCompletedTaskCount());
320              threadProceed.countDown();
321              threadDone.await();
322 <            delay(SHORT_DELAY_MS);
323 <            assertEquals(1, p.getCompletedTaskCount());
322 >            long startTime = System.nanoTime();
323 >            while (p.getCompletedTaskCount() != 1) {
324 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
325 >                    fail("timed out");
326 >                Thread.yield();
327 >            }
328          } finally {
329              joinPool(p);
330          }
# Line 315 | Line 348 | public class ThreadPoolExecutorSubclassT
348          joinPool(p);
349      }
350  
318
351      /**
352       * getThreadFactory returns factory in constructor if not set
353       */
# Line 337 | Line 369 | public class ThreadPoolExecutorSubclassT
369          joinPool(p);
370      }
371  
340
372      /**
373       * setThreadFactory(null) throws NPE
374       */
# Line 374 | Line 405 | public class ThreadPoolExecutorSubclassT
405          joinPool(p);
406      }
407  
377
408      /**
409       * setRejectedExecutionHandler(null) throws NPE
410       */
# Line 389 | Line 419 | public class ThreadPoolExecutorSubclassT
419          }
420      }
421  
392
422      /**
423       * getLargestPoolSize increases, but doesn't overestimate, when
424       * multiple threads active
# Line 484 | Line 513 | public class ThreadPoolExecutorSubclassT
513      }
514  
515      /**
516 <     * isShutDown is false before shutdown, true after
516 >     * isShutdown is false before shutdown, true after
517       */
518      public void testIsShutdown() {
519  
# Line 495 | Line 524 | public class ThreadPoolExecutorSubclassT
524          joinPool(p);
525      }
526  
498
527      /**
528       * isTerminated is false before termination, true after
529       */
# Line 669 | Line 697 | public class ThreadPoolExecutorSubclassT
697      }
698  
699      /**
700 <     * shutDownNow returns a list containing tasks that were not run
700 >     * shutdownNow returns a list containing tasks that were not run
701       */
702      public void testShutdownNow() {
703          ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
# Line 689 | Line 717 | public class ThreadPoolExecutorSubclassT
717  
718      // Exception Tests
719  
692
720      /**
721       * Constructor throws if corePoolSize argument is less than zero
722       */
# Line 750 | Line 777 | public class ThreadPoolExecutorSubclassT
777          } catch (NullPointerException success) {}
778      }
779  
753
754
780      /**
781       * Constructor throws if corePoolSize argument is less than zero
782       */
# Line 823 | Line 848 | public class ThreadPoolExecutorSubclassT
848          } catch (NullPointerException success) {}
849      }
850  
826
851      /**
852       * Constructor throws if corePoolSize argument is less than zero
853       */
# Line 895 | Line 919 | public class ThreadPoolExecutorSubclassT
919          } catch (NullPointerException success) {}
920      }
921  
898
922      /**
923       * Constructor throws if corePoolSize argument is less than zero
924       */
# Line 981 | Line 1004 | public class ThreadPoolExecutorSubclassT
1004          } catch (NullPointerException success) {}
1005      }
1006  
984
1007      /**
1008       * execute throws RejectedExecutionException if saturated.
1009       */
# Line 1131 | Line 1153 | public class ThreadPoolExecutorSubclassT
1153          }
1154      }
1155  
1134
1156      /**
1157       * execute using DiscardOldestPolicy drops task on shutdown
1158       */
# Line 1149 | Line 1170 | public class ThreadPoolExecutorSubclassT
1170          }
1171      }
1172  
1152
1173      /**
1174       * execute(null) throws NPE
1175       */
# Line 1214 | Line 1234 | public class ThreadPoolExecutorSubclassT
1234          joinPool(p);
1235      }
1236  
1217
1237      /**
1238       * setKeepAliveTime throws IllegalArgumentException
1239       * when given a negative value
# Line 1239 | Line 1258 | public class ThreadPoolExecutorSubclassT
1258      public void testTerminated() {
1259          CustomTPE p = new CustomTPE();
1260          try { p.shutdown(); } catch (SecurityException ok) { return; }
1261 <        assertTrue(p.terminatedCalled);
1261 >        assertTrue(p.terminatedCalled());
1262          joinPool(p);
1263      }
1264  
# Line 1249 | Line 1268 | public class ThreadPoolExecutorSubclassT
1268      public void testBeforeAfter() throws InterruptedException {
1269          CustomTPE p = new CustomTPE();
1270          try {
1271 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1272 <            p.execute(r);
1273 <            delay(SHORT_DELAY_MS);
1274 <            assertTrue(r.done);
1275 <            assertTrue(p.beforeCalled);
1276 <            assertTrue(p.afterCalled);
1271 >            final CountDownLatch done = new CountDownLatch(1);
1272 >            p.execute(new CheckedRunnable() {
1273 >                public void realRun() {
1274 >                    done.countDown();
1275 >                }});
1276 >            await(p.afterCalled);
1277 >            assertEquals(0, done.getCount());
1278 >            assertTrue(p.afterCalled());
1279 >            assertTrue(p.beforeCalled());
1280              try { p.shutdown(); } catch (SecurityException ok) { return; }
1281          } finally {
1282              joinPool(p);
# Line 1303 | Line 1325 | public class ThreadPoolExecutorSubclassT
1325          }
1326      }
1327  
1306
1328      /**
1329       * invokeAny(null) throws NPE
1330       */
# Line 1465 | Line 1486 | public class ThreadPoolExecutorSubclassT
1486          }
1487      }
1488  
1468
1469
1489      /**
1490       * timed invokeAny(null) throws NPE
1491       */
# Line 1674 | Line 1693 | public class ThreadPoolExecutorSubclassT
1693              l.add(new StringTask());
1694              List<Future<String>> futures =
1695                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1696 <            assertEquals(3, futures.size());
1697 <            Iterator<Future<String>> it = futures.iterator();
1698 <            Future<String> f1 = it.next();
1699 <            Future<String> f2 = it.next();
1700 <            Future<String> f3 = it.next();
1682 <            assertTrue(f1.isDone());
1683 <            assertTrue(f2.isDone());
1684 <            assertTrue(f3.isDone());
1685 <            assertFalse(f1.isCancelled());
1686 <            assertTrue(f2.isCancelled());
1696 >            assertEquals(l.size(), futures.size());
1697 >            for (Future future : futures)
1698 >                assertTrue(future.isDone());
1699 >            assertFalse(futures.get(0).isCancelled());
1700 >            assertTrue(futures.get(1).isCancelled());
1701          } finally {
1702              joinPool(e);
1703          }
# Line 1726 | Line 1740 | public class ThreadPoolExecutorSubclassT
1740       * allowCoreThreadTimeOut(true) causes idle threads to time out
1741       */
1742      public void testAllowCoreThreadTimeOut_true() throws Exception {
1743 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1744          final ThreadPoolExecutor p =
1745              new CustomTPE(2, 10,
1746 <                          SHORT_DELAY_MS, MILLISECONDS,
1746 >                          coreThreadTimeOut, MILLISECONDS,
1747                            new ArrayBlockingQueue<Runnable>(10));
1748          final CountDownLatch threadStarted = new CountDownLatch(1);
1749          try {
# Line 1738 | Line 1753 | public class ThreadPoolExecutorSubclassT
1753                      threadStarted.countDown();
1754                      assertEquals(1, p.getPoolSize());
1755                  }});
1756 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1757 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1758 <                if (p.getPoolSize() == 0)
1759 <                    break;
1760 <                delay(10);
1761 <            }
1756 >            await(threadStarted);
1757 >            delay(coreThreadTimeOut);
1758 >            long startTime = System.nanoTime();
1759 >            while (p.getPoolSize() > 0
1760 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1761 >                Thread.yield();
1762 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1763              assertEquals(0, p.getPoolSize());
1764          } finally {
1765              joinPool(p);
# Line 1754 | Line 1770 | public class ThreadPoolExecutorSubclassT
1770       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1771       */
1772      public void testAllowCoreThreadTimeOut_false() throws Exception {
1773 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1774          final ThreadPoolExecutor p =
1775              new CustomTPE(2, 10,
1776 <                          SHORT_DELAY_MS, MILLISECONDS,
1776 >                          coreThreadTimeOut, MILLISECONDS,
1777                            new ArrayBlockingQueue<Runnable>(10));
1778          final CountDownLatch threadStarted = new CountDownLatch(1);
1779          try {
# Line 1766 | Line 1783 | public class ThreadPoolExecutorSubclassT
1783                      threadStarted.countDown();
1784                      assertTrue(p.getPoolSize() >= 1);
1785                  }});
1786 <            delay(SMALL_DELAY_MS);
1786 >            delay(2 * coreThreadTimeOut);
1787              assertTrue(p.getPoolSize() >= 1);
1788          } finally {
1789              joinPool(p);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines