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.50 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;
10 < import java.util.concurrent.atomic.*;
11 < import junit.framework.*;
12 < import java.util.*;
10 > import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 >
12 > import java.util.ArrayList;
13 > import java.util.List;
14 > import java.util.concurrent.ArrayBlockingQueue;
15 > import java.util.concurrent.BlockingQueue;
16 > import java.util.concurrent.Callable;
17 > import java.util.concurrent.CountDownLatch;
18 > import java.util.concurrent.ExecutionException;
19 > import java.util.concurrent.Executors;
20 > import java.util.concurrent.ExecutorService;
21 > import java.util.concurrent.Future;
22 > import java.util.concurrent.FutureTask;
23 > import java.util.concurrent.LinkedBlockingQueue;
24 > import java.util.concurrent.RejectedExecutionException;
25 > import java.util.concurrent.RejectedExecutionHandler;
26 > import java.util.concurrent.SynchronousQueue;
27 > import java.util.concurrent.ThreadFactory;
28 > import java.util.concurrent.ThreadPoolExecutor;
29 > import java.util.concurrent.TimeUnit;
30 >
31 > import junit.framework.Test;
32 > import junit.framework.TestSuite;
33  
34   public class ThreadPoolExecutorTest extends JSR166TestCase {
35      public static void main(String[] args) {
# Line 21 | Line 40 | public class ThreadPoolExecutorTest exte
40      }
41  
42      static class ExtendedTPE extends ThreadPoolExecutor {
43 <        volatile boolean beforeCalled = false;
44 <        volatile boolean afterCalled = false;
45 <        volatile boolean terminatedCalled = false;
43 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
44 >        final CountDownLatch afterCalled = new CountDownLatch(1);
45 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
46 >
47          public ExtendedTPE() {
48              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
49          }
50          protected void beforeExecute(Thread t, Runnable r) {
51 <            beforeCalled = true;
51 >            beforeCalled.countDown();
52          }
53          protected void afterExecute(Runnable r, Throwable t) {
54 <            afterCalled = true;
54 >            afterCalled.countDown();
55          }
56          protected void terminated() {
57 <            terminatedCalled = true;
57 >            terminatedCalled.countDown();
58 >        }
59 >
60 >        public boolean beforeCalled() {
61 >            return beforeCalled.getCount() == 0;
62 >        }
63 >        public boolean afterCalled() {
64 >            return afterCalled.getCount() == 0;
65 >        }
66 >        public boolean terminatedCalled() {
67 >            return terminatedCalled.getCount() == 0;
68          }
69      }
70  
# Line 149 | Line 179 | public class ThreadPoolExecutorTest exte
179                      threadProceed.await();
180                      threadDone.countDown();
181                  }});
182 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
182 >            await(threadStarted);
183              assertEquals(0, p.getCompletedTaskCount());
184              threadProceed.countDown();
185              threadDone.await();
186 <            delay(SHORT_DELAY_MS);
187 <            assertEquals(1, p.getCompletedTaskCount());
186 >            long startTime = System.nanoTime();
187 >            while (p.getCompletedTaskCount() != 1) {
188 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
189 >                    fail("timed out");
190 >                Thread.yield();
191 >            }
192          } finally {
193              joinPool(p);
194          }
# Line 387 | Line 421 | public class ThreadPoolExecutorTest exte
421      }
422  
423      /**
424 +     * awaitTermination on a non-shutdown pool times out
425 +     */
426 +    public void testAwaitTermination_timesOut() throws InterruptedException {
427 +        final ThreadPoolExecutor p =
428 +            new ThreadPoolExecutor(1, 1,
429 +                                   LONG_DELAY_MS, MILLISECONDS,
430 +                                   new ArrayBlockingQueue<Runnable>(10));
431 +        assertFalse(p.isTerminated());
432 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
433 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
434 +        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
435 +        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
436 +        assertFalse(p.awaitTermination(0L, NANOSECONDS));
437 +        assertFalse(p.awaitTermination(0L, MILLISECONDS));
438 +        long timeoutNanos = 999999L;
439 +        long startTime = System.nanoTime();
440 +        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
441 +        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
442 +        assertFalse(p.isTerminated());
443 +        startTime = System.nanoTime();
444 +        long timeoutMillis = timeoutMillis();
445 +        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
446 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
447 +        assertFalse(p.isTerminated());
448 +        p.shutdown();
449 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
450 +        assertTrue(p.isTerminated());
451 +    }
452 +
453 +    /**
454       * isTerminated is false before termination, true after
455       */
456      public void testIsTerminated() throws InterruptedException {
# Line 1319 | Line 1383 | public class ThreadPoolExecutorTest exte
1383      public void testTerminated() {
1384          ExtendedTPE p = new ExtendedTPE();
1385          try { p.shutdown(); } catch (SecurityException ok) { return; }
1386 <        assertTrue(p.terminatedCalled);
1386 >        assertTrue(p.terminatedCalled());
1387          joinPool(p);
1388      }
1389  
# Line 1329 | Line 1393 | public class ThreadPoolExecutorTest exte
1393      public void testBeforeAfter() throws InterruptedException {
1394          ExtendedTPE p = new ExtendedTPE();
1395          try {
1396 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1397 <            p.execute(r);
1398 <            delay(SHORT_DELAY_MS);
1399 <            assertTrue(r.done);
1400 <            assertTrue(p.beforeCalled);
1401 <            assertTrue(p.afterCalled);
1396 >            final CountDownLatch done = new CountDownLatch(1);
1397 >            p.execute(new CheckedRunnable() {
1398 >                public void realRun() {
1399 >                    done.countDown();
1400 >                }});
1401 >            await(p.afterCalled);
1402 >            assertEquals(0, done.getCount());
1403 >            assertTrue(p.afterCalled());
1404 >            assertTrue(p.beforeCalled());
1405              try { p.shutdown(); } catch (SecurityException ok) { return; }
1406          } finally {
1407              joinPool(p);
# Line 1831 | Line 1898 | public class ThreadPoolExecutorTest exte
1898              l.add(new StringTask());
1899              List<Future<String>> futures =
1900                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1901 <            assertEquals(3, futures.size());
1902 <            Iterator<Future<String>> it = futures.iterator();
1903 <            Future<String> f1 = it.next();
1904 <            Future<String> f2 = it.next();
1905 <            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());
1901 >            assertEquals(l.size(), futures.size());
1902 >            for (Future future : futures)
1903 >                assertTrue(future.isDone());
1904 >            assertFalse(futures.get(0).isCancelled());
1905 >            assertTrue(futures.get(1).isCancelled());
1906          } finally {
1907              joinPool(e);
1908          }
# Line 1963 | Line 2025 | public class ThreadPoolExecutorTest exte
2025              // enough time to run all tasks
2026              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2027          } finally {
2028 <            p.shutdown();
2028 >            joinPool(p);
2029          }
2030      }
2031  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines