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.44 by jsr166, Fri May 27 19:35:24 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 149 | 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 1319 | 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 1329 | 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 1831 | 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();
1839 <            assertTrue(f1.isDone());
1840 <            assertTrue(f2.isDone());
1841 <            assertTrue(f3.isDone());
1842 <            assertFalse(f1.isCancelled());
1843 <            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 1963 | 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