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.43 by jsr166, Sat May 7 19:49:37 2011 UTC vs.
Revision 1.47 by jsr166, Tue May 31 16:16:24 2011 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 + import junit.framework.*;
10   import java.util.concurrent.*;
11   import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 import java.util.concurrent.atomic.*;
12 import junit.framework.*;
12   import java.util.*;
13  
14   public class ThreadPoolExecutorTest extends JSR166TestCase {
# Line 21 | Line 20 | public class ThreadPoolExecutorTest exte
20      }
21  
22      static class ExtendedTPE extends ThreadPoolExecutor {
23 <        volatile boolean beforeCalled = false;
24 <        volatile boolean afterCalled = false;
25 <        volatile boolean terminatedCalled = false;
23 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
24 >        final CountDownLatch afterCalled = new CountDownLatch(1);
25 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
26 >
27          public ExtendedTPE() {
28              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
29          }
30          protected void beforeExecute(Thread t, Runnable r) {
31 <            beforeCalled = true;
31 >            beforeCalled.countDown();
32          }
33          protected void afterExecute(Runnable r, Throwable t) {
34 <            afterCalled = true;
34 >            afterCalled.countDown();
35          }
36          protected void terminated() {
37 <            terminatedCalled = true;
37 >            terminatedCalled.countDown();
38 >        }
39 >
40 >        public boolean beforeCalled() {
41 >            return beforeCalled.getCount() == 0;
42 >        }
43 >        public boolean afterCalled() {
44 >            return afterCalled.getCount() == 0;
45 >        }
46 >        public boolean terminatedCalled() {
47 >            return terminatedCalled.getCount() == 0;
48          }
49      }
50  
# Line 46 | Line 56 | public class ThreadPoolExecutorTest exte
56          }
57      }
58  
49
59      /**
60       * execute successfully executes a runnable
61       */
# Line 150 | Line 159 | public class ThreadPoolExecutorTest exte
159                      threadProceed.await();
160                      threadDone.countDown();
161                  }});
162 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
162 >            await(threadStarted);
163              assertEquals(0, p.getCompletedTaskCount());
164              threadProceed.countDown();
165              threadDone.await();
166 <            delay(SHORT_DELAY_MS);
167 <            assertEquals(1, p.getCompletedTaskCount());
166 >            long startTime = System.nanoTime();
167 >            while (p.getCompletedTaskCount() != 1) {
168 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
169 >                    fail("timed out");
170 >                Thread.yield();
171 >            }
172          } finally {
173              joinPool(p);
174          }
# Line 185 | Line 198 | public class ThreadPoolExecutorTest exte
198          joinPool(p);
199      }
200  
188
201      /**
202       * getThreadFactory returns factory in constructor if not set
203       */
# Line 215 | Line 227 | public class ThreadPoolExecutorTest exte
227          joinPool(p);
228      }
229  
218
230      /**
231       * setThreadFactory(null) throws NPE
232       */
# Line 262 | Line 273 | public class ThreadPoolExecutorTest exte
273          joinPool(p);
274      }
275  
265
276      /**
277       * setRejectedExecutionHandler(null) throws NPE
278       */
# Line 280 | Line 290 | public class ThreadPoolExecutorTest exte
290          }
291      }
292  
283
293      /**
294       * getLargestPoolSize increases, but doesn't overestimate, when
295       * multiple threads active
# Line 391 | Line 400 | public class ThreadPoolExecutorTest exte
400          joinPool(p);
401      }
402  
394
403      /**
404       * isTerminated is false before termination, true after
405       */
# Line 587 | Line 595 | public class ThreadPoolExecutorTest exte
595  
596      // Exception Tests
597  
590
598      /**
599       * Constructor throws if corePoolSize argument is less than zero
600       */
# Line 660 | Line 667 | public class ThreadPoolExecutorTest exte
667          } catch (NullPointerException success) {}
668      }
669  
663
664
670      /**
671       * Constructor throws if corePoolSize argument is less than zero
672       */
# Line 753 | Line 758 | public class ThreadPoolExecutorTest exte
758          } catch (NullPointerException success) {}
759      }
760  
756
761      /**
762       * Constructor throws if corePoolSize argument is less than zero
763       */
# Line 845 | Line 849 | public class ThreadPoolExecutorTest exte
849          } catch (NullPointerException success) {}
850      }
851  
848
852      /**
853       * Constructor throws if corePoolSize argument is less than zero
854       */
# Line 1212 | Line 1215 | public class ThreadPoolExecutorTest exte
1215          }
1216      }
1217  
1215
1218      /**
1219       * execute using DiscardOldestPolicy drops task on shutdown
1220       */
# Line 1234 | Line 1236 | public class ThreadPoolExecutorTest exte
1236          }
1237      }
1238  
1237
1239      /**
1240       * execute(null) throws NPE
1241       */
# Line 1307 | Line 1308 | public class ThreadPoolExecutorTest exte
1308          joinPool(p);
1309      }
1310  
1310
1311      /**
1312       * setKeepAliveTime throws IllegalArgumentException
1313       * when given a negative value
# Line 1333 | Line 1333 | public class ThreadPoolExecutorTest exte
1333      public void testTerminated() {
1334          ExtendedTPE p = new ExtendedTPE();
1335          try { p.shutdown(); } catch (SecurityException ok) { return; }
1336 <        assertTrue(p.terminatedCalled);
1336 >        assertTrue(p.terminatedCalled());
1337          joinPool(p);
1338      }
1339  
# Line 1343 | Line 1343 | public class ThreadPoolExecutorTest exte
1343      public void testBeforeAfter() throws InterruptedException {
1344          ExtendedTPE p = new ExtendedTPE();
1345          try {
1346 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1347 <            p.execute(r);
1348 <            delay(SHORT_DELAY_MS);
1349 <            assertTrue(r.done);
1350 <            assertTrue(p.beforeCalled);
1351 <            assertTrue(p.afterCalled);
1346 >            final CountDownLatch done = new CountDownLatch(1);
1347 >            final CheckedRunnable task = new CheckedRunnable() {
1348 >                public void realRun() {
1349 >                    done.countDown();
1350 >                }};
1351 >            p.execute(task);
1352 >            await(p.afterCalled);
1353 >            assertEquals(0, done.getCount());
1354 >            assertTrue(p.afterCalled());
1355 >            assertTrue(p.beforeCalled());
1356              try { p.shutdown(); } catch (SecurityException ok) { return; }
1357          } finally {
1358              joinPool(p);
# Line 1406 | Line 1410 | public class ThreadPoolExecutorTest exte
1410          }
1411      }
1412  
1409
1413      /**
1414       * invokeAny(null) throws NPE
1415       */
# Line 1600 | Line 1603 | public class ThreadPoolExecutorTest exte
1603          }
1604      }
1605  
1603
1604
1606      /**
1607       * timed invokeAny(null) throws NPE
1608       */
# Line 1848 | Line 1849 | public class ThreadPoolExecutorTest exte
1849              l.add(new StringTask());
1850              List<Future<String>> futures =
1851                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1852 <            assertEquals(3, futures.size());
1853 <            Iterator<Future<String>> it = futures.iterator();
1854 <            Future<String> f1 = it.next();
1855 <            Future<String> f2 = it.next();
1856 <            Future<String> f3 = it.next();
1856 <            assertTrue(f1.isDone());
1857 <            assertTrue(f2.isDone());
1858 <            assertTrue(f3.isDone());
1859 <            assertFalse(f1.isCancelled());
1860 <            assertTrue(f2.isCancelled());
1852 >            assertEquals(l.size(), futures.size());
1853 >            for (Future future : futures)
1854 >                assertTrue(future.isDone());
1855 >            assertFalse(futures.get(0).isCancelled());
1856 >            assertTrue(futures.get(1).isCancelled());
1857          } finally {
1858              joinPool(e);
1859          }
# Line 1903 | Line 1899 | public class ThreadPoolExecutorTest exte
1899       * allowCoreThreadTimeOut(true) causes idle threads to time out
1900       */
1901      public void testAllowCoreThreadTimeOut_true() throws Exception {
1902 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1903          final ThreadPoolExecutor p =
1904              new ThreadPoolExecutor(2, 10,
1905 <                                   SHORT_DELAY_MS, MILLISECONDS,
1905 >                                   coreThreadTimeOut, MILLISECONDS,
1906                                     new ArrayBlockingQueue<Runnable>(10));
1907          final CountDownLatch threadStarted = new CountDownLatch(1);
1908          try {
1909              p.allowCoreThreadTimeOut(true);
1910              p.execute(new CheckedRunnable() {
1911 <                public void realRun() throws InterruptedException {
1911 >                public void realRun() {
1912                      threadStarted.countDown();
1913                      assertEquals(1, p.getPoolSize());
1914                  }});
1915 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1916 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1917 <                if (p.getPoolSize() == 0)
1918 <                    break;
1919 <                delay(10);
1920 <            }
1915 >            await(threadStarted);
1916 >            delay(coreThreadTimeOut);
1917 >            long startTime = System.nanoTime();
1918 >            while (p.getPoolSize() > 0
1919 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1920 >                Thread.yield();
1921 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1922              assertEquals(0, p.getPoolSize());
1923          } finally {
1924              joinPool(p);
# Line 1931 | Line 1929 | public class ThreadPoolExecutorTest exte
1929       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1930       */
1931      public void testAllowCoreThreadTimeOut_false() throws Exception {
1932 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1933          final ThreadPoolExecutor p =
1934              new ThreadPoolExecutor(2, 10,
1935 <                                   SHORT_DELAY_MS, MILLISECONDS,
1935 >                                   coreThreadTimeOut, MILLISECONDS,
1936                                     new ArrayBlockingQueue<Runnable>(10));
1937          final CountDownLatch threadStarted = new CountDownLatch(1);
1938          try {
# Line 1943 | Line 1942 | public class ThreadPoolExecutorTest exte
1942                      threadStarted.countDown();
1943                      assertTrue(p.getPoolSize() >= 1);
1944                  }});
1945 <            delay(SMALL_DELAY_MS);
1945 >            delay(2 * coreThreadTimeOut);
1946              assertTrue(p.getPoolSize() >= 1);
1947          } finally {
1948              joinPool(p);
# Line 1977 | Line 1976 | public class ThreadPoolExecutorTest exte
1976              // enough time to run all tasks
1977              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
1978          } finally {
1979 <            p.shutdown();
1979 >            joinPool(p);
1980          }
1981      }
1982  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines