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.29 by jsr166, Fri May 27 19:35:24 2011 UTC vs.
Revision 1.34 by jsr166, Wed Dec 31 20:28:55 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 37 | Line 58 | public class ThreadPoolExecutorSubclassT
58          CustomTask(final Runnable r, final V res) {
59              if (r == null) throw new NullPointerException();
60              callable = new Callable<V>() {
61 <            public V call() throws Exception { r.run(); return res; }};
61 >                public V call() throws Exception { r.run(); return res; }};
62          }
63          public boolean isDone() {
64              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 161 | 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 284 | 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 1223 | 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 1233 | 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 1655 | 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();
1663 <            assertTrue(f1.isDone());
1664 <            assertTrue(f2.isDone());
1665 <            assertTrue(f3.isDone());
1666 <            assertFalse(f1.isCancelled());
1667 <            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          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines