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.37 by jsr166, Mon Oct 11 07:21:32 2010 UTC vs.
Revision 1.54 by jsr166, Fri Sep 4 19:35:46 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
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 > import static java.util.concurrent.TimeUnit.SECONDS;
12 >
13 > import java.util.ArrayList;
14 > import java.util.List;
15 > import java.util.concurrent.ArrayBlockingQueue;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.Callable;
18 > import java.util.concurrent.CountDownLatch;
19 > import java.util.concurrent.ExecutionException;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.Future;
23 > import java.util.concurrent.FutureTask;
24 > import java.util.concurrent.LinkedBlockingQueue;
25 > import java.util.concurrent.RejectedExecutionException;
26 > import java.util.concurrent.RejectedExecutionHandler;
27 > import java.util.concurrent.SynchronousQueue;
28 > import java.util.concurrent.ThreadFactory;
29 > import java.util.concurrent.ThreadPoolExecutor;
30 > import java.util.concurrent.TimeUnit;
31 >
32 > import junit.framework.Test;
33 > import junit.framework.TestSuite;
34  
35   public class ThreadPoolExecutorTest extends JSR166TestCase {
36      public static void main(String[] args) {
37 <        junit.textui.TestRunner.run(suite());
37 >        main(suite(), args);
38      }
39      public static Test suite() {
40          return new TestSuite(ThreadPoolExecutorTest.class);
41      }
42  
43      static class ExtendedTPE extends ThreadPoolExecutor {
44 <        volatile boolean beforeCalled = false;
45 <        volatile boolean afterCalled = false;
46 <        volatile boolean terminatedCalled = false;
44 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
45 >        final CountDownLatch afterCalled = new CountDownLatch(1);
46 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
47 >
48          public ExtendedTPE() {
49              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
50          }
51          protected void beforeExecute(Thread t, Runnable r) {
52 <            beforeCalled = true;
52 >            beforeCalled.countDown();
53          }
54          protected void afterExecute(Runnable r, Throwable t) {
55 <            afterCalled = true;
55 >            afterCalled.countDown();
56          }
57          protected void terminated() {
58 <            terminatedCalled = true;
58 >            terminatedCalled.countDown();
59 >        }
60 >
61 >        public boolean beforeCalled() {
62 >            return beforeCalled.getCount() == 0;
63 >        }
64 >        public boolean afterCalled() {
65 >            return afterCalled.getCount() == 0;
66 >        }
67 >        public boolean terminatedCalled() {
68 >            return terminatedCalled.getCount() == 0;
69          }
70      }
71  
# Line 46 | Line 77 | public class ThreadPoolExecutorTest exte
77          }
78      }
79  
49
80      /**
81       * execute successfully executes a runnable
82       */
# Line 150 | Line 180 | public class ThreadPoolExecutorTest exte
180                      threadProceed.await();
181                      threadDone.countDown();
182                  }});
183 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
183 >            await(threadStarted);
184              assertEquals(0, p.getCompletedTaskCount());
185              threadProceed.countDown();
186              threadDone.await();
187 <            Thread.sleep(SHORT_DELAY_MS);
188 <            assertEquals(1, p.getCompletedTaskCount());
187 >            long startTime = System.nanoTime();
188 >            while (p.getCompletedTaskCount() != 1) {
189 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
190 >                    fail("timed out");
191 >                Thread.yield();
192 >            }
193          } finally {
194              joinPool(p);
195          }
# Line 185 | Line 219 | public class ThreadPoolExecutorTest exte
219          joinPool(p);
220      }
221  
188
222      /**
223       * getThreadFactory returns factory in constructor if not set
224       */
# Line 215 | Line 248 | public class ThreadPoolExecutorTest exte
248          joinPool(p);
249      }
250  
218
251      /**
252       * setThreadFactory(null) throws NPE
253       */
# Line 262 | Line 294 | public class ThreadPoolExecutorTest exte
294          joinPool(p);
295      }
296  
265
297      /**
298       * setRejectedExecutionHandler(null) throws NPE
299       */
# Line 280 | Line 311 | public class ThreadPoolExecutorTest exte
311          }
312      }
313  
283
314      /**
315       * getLargestPoolSize increases, but doesn't overestimate, when
316       * multiple threads active
# Line 378 | Line 408 | public class ThreadPoolExecutorTest exte
408      }
409  
410      /**
411 <     * isShutDown is false before shutdown, true after
411 >     * isShutdown is false before shutdown, true after
412       */
413      public void testIsShutdown() {
414          final ThreadPoolExecutor p =
# Line 391 | Line 421 | public class ThreadPoolExecutorTest exte
421          joinPool(p);
422      }
423  
424 +    /**
425 +     * awaitTermination on a non-shutdown pool times out
426 +     */
427 +    public void testAwaitTermination_timesOut() throws InterruptedException {
428 +        final ThreadPoolExecutor p =
429 +            new ThreadPoolExecutor(1, 1,
430 +                                   LONG_DELAY_MS, MILLISECONDS,
431 +                                   new ArrayBlockingQueue<Runnable>(10));
432 +        assertFalse(p.isTerminated());
433 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
434 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
435 +        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
436 +        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
437 +        assertFalse(p.awaitTermination(0L, NANOSECONDS));
438 +        assertFalse(p.awaitTermination(0L, MILLISECONDS));
439 +        long timeoutNanos = 999999L;
440 +        long startTime = System.nanoTime();
441 +        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
442 +        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
443 +        assertFalse(p.isTerminated());
444 +        startTime = System.nanoTime();
445 +        long timeoutMillis = timeoutMillis();
446 +        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
447 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
448 +        assertFalse(p.isTerminated());
449 +        p.shutdown();
450 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
451 +        assertTrue(p.isTerminated());
452 +    }
453  
454      /**
455       * isTerminated is false before termination, true after
# Line 406 | Line 465 | public class ThreadPoolExecutorTest exte
465          try {
466              p.execute(new CheckedRunnable() {
467                  public void realRun() throws InterruptedException {
409                    threadStarted.countDown();
468                      assertFalse(p.isTerminated());
469 +                    threadStarted.countDown();
470                      done.await();
471                  }});
472              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
473 +            assertFalse(p.isTerminating());
474              done.countDown();
475          } finally {
476              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 433 | Line 493 | public class ThreadPoolExecutorTest exte
493              assertFalse(p.isTerminating());
494              p.execute(new CheckedRunnable() {
495                  public void realRun() throws InterruptedException {
436                    threadStarted.countDown();
496                      assertFalse(p.isTerminating());
497 +                    threadStarted.countDown();
498                      done.await();
499                  }});
500              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
# Line 563 | Line 623 | public class ThreadPoolExecutorTest exte
623      }
624  
625      /**
626 <     * shutDownNow returns a list containing tasks that were not run
626 >     * shutdownNow returns a list containing tasks that were not run
627       */
628 <    public void testShutDownNow() {
628 >    public void testShutdownNow() {
629          final ThreadPoolExecutor p =
630              new ThreadPoolExecutor(1, 1,
631                                     LONG_DELAY_MS, MILLISECONDS,
# Line 586 | Line 646 | public class ThreadPoolExecutorTest exte
646  
647      // Exception Tests
648  
589
649      /**
650       * Constructor throws if corePoolSize argument is less than zero
651       */
652      public void testConstructor1() {
653          try {
654 <            new ThreadPoolExecutor(-1, 1,
596 <                                   LONG_DELAY_MS, MILLISECONDS,
654 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
655                                     new ArrayBlockingQueue<Runnable>(10));
656              shouldThrow();
657          } catch (IllegalArgumentException success) {}
# Line 604 | Line 662 | public class ThreadPoolExecutorTest exte
662       */
663      public void testConstructor2() {
664          try {
665 <            new ThreadPoolExecutor(1, -1,
608 <                                   LONG_DELAY_MS, MILLISECONDS,
665 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
666                                     new ArrayBlockingQueue<Runnable>(10));
667              shouldThrow();
668          } catch (IllegalArgumentException success) {}
# Line 616 | Line 673 | public class ThreadPoolExecutorTest exte
673       */
674      public void testConstructor3() {
675          try {
676 <            new ThreadPoolExecutor(1, 0,
620 <                                   LONG_DELAY_MS, MILLISECONDS,
676 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
677                                     new ArrayBlockingQueue<Runnable>(10));
678              shouldThrow();
679          } catch (IllegalArgumentException success) {}
# Line 628 | Line 684 | public class ThreadPoolExecutorTest exte
684       */
685      public void testConstructor4() {
686          try {
687 <            new ThreadPoolExecutor(1, 2,
632 <                                   -1L, MILLISECONDS,
687 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
688                                     new ArrayBlockingQueue<Runnable>(10));
689              shouldThrow();
690          } catch (IllegalArgumentException success) {}
# Line 640 | Line 695 | public class ThreadPoolExecutorTest exte
695       */
696      public void testConstructor5() {
697          try {
698 <            new ThreadPoolExecutor(2, 1,
644 <                                   LONG_DELAY_MS, MILLISECONDS,
698 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
699                                     new ArrayBlockingQueue<Runnable>(10));
700              shouldThrow();
701          } catch (IllegalArgumentException success) {}
# Line 652 | Line 706 | public class ThreadPoolExecutorTest exte
706       */
707      public void testConstructorNullPointerException() {
708          try {
709 <            new ThreadPoolExecutor(1, 2,
656 <                                   LONG_DELAY_MS, MILLISECONDS,
709 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
710                                     (BlockingQueue) null);
711              shouldThrow();
712          } catch (NullPointerException success) {}
713      }
714  
662
663
715      /**
716       * Constructor throws if corePoolSize argument is less than zero
717       */
718      public void testConstructor6() {
719          try {
720 <            new ThreadPoolExecutor(-1, 1,
670 <                                   LONG_DELAY_MS, MILLISECONDS,
720 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
721                                     new ArrayBlockingQueue<Runnable>(10),
722                                     new SimpleThreadFactory());
723              shouldThrow();
# Line 679 | Line 729 | public class ThreadPoolExecutorTest exte
729       */
730      public void testConstructor7() {
731          try {
732 <            new ThreadPoolExecutor(1, -1,
683 <                                   LONG_DELAY_MS, MILLISECONDS,
732 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
733                                     new ArrayBlockingQueue<Runnable>(10),
734                                     new SimpleThreadFactory());
735              shouldThrow();
# Line 692 | Line 741 | public class ThreadPoolExecutorTest exte
741       */
742      public void testConstructor8() {
743          try {
744 <            new ThreadPoolExecutor(1, 0,
696 <                                   LONG_DELAY_MS, MILLISECONDS,
744 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
745                                     new ArrayBlockingQueue<Runnable>(10),
746                                     new SimpleThreadFactory());
747              shouldThrow();
# Line 705 | Line 753 | public class ThreadPoolExecutorTest exte
753       */
754      public void testConstructor9() {
755          try {
756 <            new ThreadPoolExecutor(1, 2,
709 <                                   -1L, MILLISECONDS,
756 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
757                                     new ArrayBlockingQueue<Runnable>(10),
758                                     new SimpleThreadFactory());
759              shouldThrow();
# Line 718 | Line 765 | public class ThreadPoolExecutorTest exte
765       */
766      public void testConstructor10() {
767          try {
768 <            new ThreadPoolExecutor(2, 1,
722 <                                   LONG_DELAY_MS, MILLISECONDS,
768 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
769                                     new ArrayBlockingQueue<Runnable>(10),
770                                     new SimpleThreadFactory());
771              shouldThrow();
# Line 731 | Line 777 | public class ThreadPoolExecutorTest exte
777       */
778      public void testConstructorNullPointerException2() {
779          try {
780 <            new ThreadPoolExecutor(1, 2,
735 <                                   LONG_DELAY_MS, MILLISECONDS,
780 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
781                                     (BlockingQueue) null,
782                                     new SimpleThreadFactory());
783              shouldThrow();
# Line 744 | Line 789 | public class ThreadPoolExecutorTest exte
789       */
790      public void testConstructorNullPointerException3() {
791          try {
792 <            new ThreadPoolExecutor(1, 2,
748 <                                   LONG_DELAY_MS, MILLISECONDS,
792 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
793                                     new ArrayBlockingQueue<Runnable>(10),
794                                     (ThreadFactory) null);
795              shouldThrow();
796          } catch (NullPointerException success) {}
797      }
798  
755
799      /**
800       * Constructor throws if corePoolSize argument is less than zero
801       */
802      public void testConstructor11() {
803          try {
804 <            new ThreadPoolExecutor(-1, 1,
762 <                                   LONG_DELAY_MS, MILLISECONDS,
804 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
805                                     new ArrayBlockingQueue<Runnable>(10),
806                                     new NoOpREHandler());
807              shouldThrow();
# Line 771 | Line 813 | public class ThreadPoolExecutorTest exte
813       */
814      public void testConstructor12() {
815          try {
816 <            new ThreadPoolExecutor(1, -1,
775 <                                   LONG_DELAY_MS, MILLISECONDS,
816 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
817                                     new ArrayBlockingQueue<Runnable>(10),
818                                     new NoOpREHandler());
819              shouldThrow();
# Line 784 | Line 825 | public class ThreadPoolExecutorTest exte
825       */
826      public void testConstructor13() {
827          try {
828 <            new ThreadPoolExecutor(1, 0,
788 <                                   LONG_DELAY_MS, MILLISECONDS,
828 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
829                                     new ArrayBlockingQueue<Runnable>(10),
830                                     new NoOpREHandler());
831              shouldThrow();
# Line 797 | Line 837 | public class ThreadPoolExecutorTest exte
837       */
838      public void testConstructor14() {
839          try {
840 <            new ThreadPoolExecutor(1, 2,
801 <                                   -1L, MILLISECONDS,
840 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
841                                     new ArrayBlockingQueue<Runnable>(10),
842                                     new NoOpREHandler());
843              shouldThrow();
# Line 810 | Line 849 | public class ThreadPoolExecutorTest exte
849       */
850      public void testConstructor15() {
851          try {
852 <            new ThreadPoolExecutor(2, 1,
814 <                                   LONG_DELAY_MS, MILLISECONDS,
852 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
853                                     new ArrayBlockingQueue<Runnable>(10),
854                                     new NoOpREHandler());
855              shouldThrow();
# Line 823 | Line 861 | public class ThreadPoolExecutorTest exte
861       */
862      public void testConstructorNullPointerException4() {
863          try {
864 <            new ThreadPoolExecutor(1, 2,
827 <                                   LONG_DELAY_MS, MILLISECONDS,
864 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
865                                     (BlockingQueue) null,
866                                     new NoOpREHandler());
867              shouldThrow();
# Line 836 | Line 873 | public class ThreadPoolExecutorTest exte
873       */
874      public void testConstructorNullPointerException5() {
875          try {
876 <            new ThreadPoolExecutor(1, 2,
840 <                                   LONG_DELAY_MS, MILLISECONDS,
876 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
877                                     new ArrayBlockingQueue<Runnable>(10),
878                                     (RejectedExecutionHandler) null);
879              shouldThrow();
880          } catch (NullPointerException success) {}
881      }
882  
847
883      /**
884       * Constructor throws if corePoolSize argument is less than zero
885       */
886      public void testConstructor16() {
887          try {
888 <            new ThreadPoolExecutor(-1, 1,
854 <                                   LONG_DELAY_MS, MILLISECONDS,
888 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
889                                     new ArrayBlockingQueue<Runnable>(10),
890                                     new SimpleThreadFactory(),
891                                     new NoOpREHandler());
# Line 864 | Line 898 | public class ThreadPoolExecutorTest exte
898       */
899      public void testConstructor17() {
900          try {
901 <            new ThreadPoolExecutor(1, -1,
868 <                                   LONG_DELAY_MS, MILLISECONDS,
901 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
902                                     new ArrayBlockingQueue<Runnable>(10),
903                                     new SimpleThreadFactory(),
904                                     new NoOpREHandler());
# Line 878 | Line 911 | public class ThreadPoolExecutorTest exte
911       */
912      public void testConstructor18() {
913          try {
914 <            new ThreadPoolExecutor(1, 0,
882 <                                   LONG_DELAY_MS, MILLISECONDS,
914 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
915                                     new ArrayBlockingQueue<Runnable>(10),
916                                     new SimpleThreadFactory(),
917                                     new NoOpREHandler());
# Line 892 | Line 924 | public class ThreadPoolExecutorTest exte
924       */
925      public void testConstructor19() {
926          try {
927 <            new ThreadPoolExecutor(1, 2,
896 <                                   -1L, MILLISECONDS,
927 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
928                                     new ArrayBlockingQueue<Runnable>(10),
929                                     new SimpleThreadFactory(),
930                                     new NoOpREHandler());
# Line 906 | Line 937 | public class ThreadPoolExecutorTest exte
937       */
938      public void testConstructor20() {
939          try {
940 <            new ThreadPoolExecutor(2, 1,
910 <                                   LONG_DELAY_MS, MILLISECONDS,
940 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
941                                     new ArrayBlockingQueue<Runnable>(10),
942                                     new SimpleThreadFactory(),
943                                     new NoOpREHandler());
# Line 920 | Line 950 | public class ThreadPoolExecutorTest exte
950       */
951      public void testConstructorNullPointerException6() {
952          try {
953 <            new ThreadPoolExecutor(1, 2,
924 <                                   LONG_DELAY_MS, MILLISECONDS,
953 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
954                                     (BlockingQueue) null,
955                                     new SimpleThreadFactory(),
956                                     new NoOpREHandler());
# Line 934 | Line 963 | public class ThreadPoolExecutorTest exte
963       */
964      public void testConstructorNullPointerException7() {
965          try {
966 <            new ThreadPoolExecutor(1, 2,
938 <                                   LONG_DELAY_MS, MILLISECONDS,
966 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
967                                     new ArrayBlockingQueue<Runnable>(10),
968                                     new SimpleThreadFactory(),
969                                     (RejectedExecutionHandler) null);
# Line 948 | Line 976 | public class ThreadPoolExecutorTest exte
976       */
977      public void testConstructorNullPointerException8() {
978          try {
979 <            new ThreadPoolExecutor(1, 2,
952 <                                   LONG_DELAY_MS, MILLISECONDS,
979 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
980                                     new ArrayBlockingQueue<Runnable>(10),
981                                     (ThreadFactory) null,
982                                     new NoOpREHandler());
# Line 1211 | Line 1238 | public class ThreadPoolExecutorTest exte
1238          }
1239      }
1240  
1214
1241      /**
1242       * execute using DiscardOldestPolicy drops task on shutdown
1243       */
# Line 1233 | Line 1259 | public class ThreadPoolExecutorTest exte
1259          }
1260      }
1261  
1236
1262      /**
1263       * execute(null) throws NPE
1264       */
1265      public void testExecuteNull() {
1266          ThreadPoolExecutor p =
1267 <            new ThreadPoolExecutor(1, 2,
1243 <                                   LONG_DELAY_MS, MILLISECONDS,
1267 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1268                                     new ArrayBlockingQueue<Runnable>(10));
1269          try {
1270              p.execute(null);
# Line 1306 | Line 1330 | public class ThreadPoolExecutorTest exte
1330          joinPool(p);
1331      }
1332  
1333 +    /**
1334 +     * Configuration changes that allow core pool size greater than
1335 +     * max pool size result in IllegalArgumentException.
1336 +     */
1337 +    public void testPoolSizeInvariants() {
1338 +        ThreadPoolExecutor p =
1339 +            new ThreadPoolExecutor(1, 1,
1340 +                                   LONG_DELAY_MS, MILLISECONDS,
1341 +                                   new ArrayBlockingQueue<Runnable>(10));
1342 +        for (int s = 1; s < 5; s++) {
1343 +            p.setMaximumPoolSize(s);
1344 +            p.setCorePoolSize(s);
1345 +            try {
1346 +                p.setMaximumPoolSize(s - 1);
1347 +                shouldThrow();
1348 +            } catch (IllegalArgumentException success) {}
1349 +            assertEquals(s, p.getCorePoolSize());
1350 +            assertEquals(s, p.getMaximumPoolSize());
1351 +            try {
1352 +                p.setCorePoolSize(s + 1);
1353 +                shouldThrow();
1354 +            } catch (IllegalArgumentException success) {}
1355 +            assertEquals(s, p.getCorePoolSize());
1356 +            assertEquals(s, p.getMaximumPoolSize());
1357 +        }
1358 +        joinPool(p);
1359 +    }
1360  
1361      /**
1362       * setKeepAliveTime throws IllegalArgumentException
# Line 1332 | 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 1342 | 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 <            Thread.sleep(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 1405 | Line 1459 | public class ThreadPoolExecutorTest exte
1459          }
1460      }
1461  
1408
1462      /**
1463       * invokeAny(null) throws NPE
1464       */
# Line 1599 | Line 1652 | public class ThreadPoolExecutorTest exte
1652          }
1653      }
1654  
1602
1603
1655      /**
1656       * timed invokeAny(null) throws NPE
1657       */
# Line 1847 | 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();
1855 <            assertTrue(f1.isDone());
1856 <            assertTrue(f2.isDone());
1857 <            assertTrue(f3.isDone());
1858 <            assertFalse(f1.isCancelled());
1859 <            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 1902 | Line 1948 | public class ThreadPoolExecutorTest exte
1948       * allowCoreThreadTimeOut(true) causes idle threads to time out
1949       */
1950      public void testAllowCoreThreadTimeOut_true() throws Exception {
1951 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1952          final ThreadPoolExecutor p =
1953              new ThreadPoolExecutor(2, 10,
1954 <                                   SHORT_DELAY_MS, MILLISECONDS,
1954 >                                   coreThreadTimeOut, MILLISECONDS,
1955                                     new ArrayBlockingQueue<Runnable>(10));
1956          final CountDownLatch threadStarted = new CountDownLatch(1);
1957          try {
1958              p.allowCoreThreadTimeOut(true);
1959              p.execute(new CheckedRunnable() {
1960 <                public void realRun() throws InterruptedException {
1960 >                public void realRun() {
1961                      threadStarted.countDown();
1962                      assertEquals(1, p.getPoolSize());
1963                  }});
1964 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1965 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1966 <                if (p.getPoolSize() == 0)
1967 <                    break;
1968 <                Thread.sleep(10);
1969 <            }
1964 >            await(threadStarted);
1965 >            delay(coreThreadTimeOut);
1966 >            long startTime = System.nanoTime();
1967 >            while (p.getPoolSize() > 0
1968 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1969 >                Thread.yield();
1970 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1971              assertEquals(0, p.getPoolSize());
1972          } finally {
1973              joinPool(p);
# Line 1930 | Line 1978 | public class ThreadPoolExecutorTest exte
1978       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1979       */
1980      public void testAllowCoreThreadTimeOut_false() throws Exception {
1981 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1982          final ThreadPoolExecutor p =
1983              new ThreadPoolExecutor(2, 10,
1984 <                                   SHORT_DELAY_MS, MILLISECONDS,
1984 >                                   coreThreadTimeOut, MILLISECONDS,
1985                                     new ArrayBlockingQueue<Runnable>(10));
1986          final CountDownLatch threadStarted = new CountDownLatch(1);
1987          try {
# Line 1942 | Line 1991 | public class ThreadPoolExecutorTest exte
1991                      threadStarted.countDown();
1992                      assertTrue(p.getPoolSize() >= 1);
1993                  }});
1994 <            Thread.sleep(SMALL_DELAY_MS);
1994 >            delay(2 * coreThreadTimeOut);
1995              assertTrue(p.getPoolSize() >= 1);
1996          } finally {
1997              joinPool(p);
# Line 1976 | 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