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.41 by dl, Fri May 6 11:22:08 2011 UTC vs.
Revision 1.60 by jsr166, Sun Sep 27 18:50:50 2015 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 > 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.CancellationException;
19 > import java.util.concurrent.CountDownLatch;
20 > import java.util.concurrent.ExecutionException;
21 > import java.util.concurrent.Executors;
22 > import java.util.concurrent.ExecutorService;
23 > import java.util.concurrent.Future;
24 > import java.util.concurrent.FutureTask;
25 > import java.util.concurrent.LinkedBlockingQueue;
26 > import java.util.concurrent.RejectedExecutionException;
27 > import java.util.concurrent.RejectedExecutionHandler;
28 > import java.util.concurrent.SynchronousQueue;
29 > import java.util.concurrent.ThreadFactory;
30 > import java.util.concurrent.ThreadPoolExecutor;
31 > import java.util.concurrent.TimeUnit;
32 >
33 > import junit.framework.Test;
34 > import junit.framework.TestSuite;
35  
36   public class ThreadPoolExecutorTest extends JSR166TestCase {
37      public static void main(String[] args) {
38 <        junit.textui.TestRunner.run(suite());
38 >        main(suite(), args);
39      }
40      public static Test suite() {
41          return new TestSuite(ThreadPoolExecutorTest.class);
42      }
43  
44      static class ExtendedTPE extends ThreadPoolExecutor {
45 <        volatile boolean beforeCalled = false;
46 <        volatile boolean afterCalled = false;
47 <        volatile boolean terminatedCalled = false;
45 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
46 >        final CountDownLatch afterCalled = new CountDownLatch(1);
47 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
48 >
49          public ExtendedTPE() {
50              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
51          }
52          protected void beforeExecute(Thread t, Runnable r) {
53 <            beforeCalled = true;
53 >            beforeCalled.countDown();
54          }
55          protected void afterExecute(Runnable r, Throwable t) {
56 <            afterCalled = true;
56 >            afterCalled.countDown();
57          }
58          protected void terminated() {
59 <            terminatedCalled = true;
59 >            terminatedCalled.countDown();
60 >        }
61 >
62 >        public boolean beforeCalled() {
63 >            return beforeCalled.getCount() == 0;
64 >        }
65 >        public boolean afterCalled() {
66 >            return afterCalled.getCount() == 0;
67 >        }
68 >        public boolean terminatedCalled() {
69 >            return terminatedCalled.getCount() == 0;
70          }
71      }
72  
# Line 46 | Line 78 | public class ThreadPoolExecutorTest exte
78          }
79      }
80  
49
81      /**
82       * execute successfully executes a runnable
83       */
# Line 150 | Line 181 | public class ThreadPoolExecutorTest exte
181                      threadProceed.await();
182                      threadDone.countDown();
183                  }});
184 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
184 >            await(threadStarted);
185              assertEquals(0, p.getCompletedTaskCount());
186              threadProceed.countDown();
187              threadDone.await();
188 <            delay(SHORT_DELAY_MS);
189 <            assertEquals(1, p.getCompletedTaskCount());
188 >            long startTime = System.nanoTime();
189 >            while (p.getCompletedTaskCount() != 1) {
190 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
191 >                    fail("timed out");
192 >                Thread.yield();
193 >            }
194          } finally {
195              joinPool(p);
196          }
# Line 181 | Line 216 | public class ThreadPoolExecutorTest exte
216              new ThreadPoolExecutor(2, 2,
217                                     1000, MILLISECONDS,
218                                     new ArrayBlockingQueue<Runnable>(10));
219 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
219 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
220          joinPool(p);
221      }
222  
188
223      /**
224       * getThreadFactory returns factory in constructor if not set
225       */
# Line 215 | Line 249 | public class ThreadPoolExecutorTest exte
249          joinPool(p);
250      }
251  
218
252      /**
253       * setThreadFactory(null) throws NPE
254       */
# Line 262 | Line 295 | public class ThreadPoolExecutorTest exte
295          joinPool(p);
296      }
297  
265
298      /**
299       * setRejectedExecutionHandler(null) throws NPE
300       */
# Line 280 | Line 312 | public class ThreadPoolExecutorTest exte
312          }
313      }
314  
283
315      /**
316       * getLargestPoolSize increases, but doesn't overestimate, when
317       * multiple threads active
# Line 378 | Line 409 | public class ThreadPoolExecutorTest exte
409      }
410  
411      /**
412 <     * isShutDown is false before shutdown, true after
412 >     * isShutdown is false before shutdown, true after
413       */
414      public void testIsShutdown() {
415          final ThreadPoolExecutor p =
# Line 391 | Line 422 | public class ThreadPoolExecutorTest exte
422          joinPool(p);
423      }
424  
425 +    /**
426 +     * awaitTermination on a non-shutdown pool times out
427 +     */
428 +    public void testAwaitTermination_timesOut() throws InterruptedException {
429 +        final ThreadPoolExecutor p =
430 +            new ThreadPoolExecutor(1, 1,
431 +                                   LONG_DELAY_MS, MILLISECONDS,
432 +                                   new ArrayBlockingQueue<Runnable>(10));
433 +        assertFalse(p.isTerminated());
434 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
435 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
436 +        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
437 +        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
438 +        assertFalse(p.awaitTermination(0L, NANOSECONDS));
439 +        assertFalse(p.awaitTermination(0L, MILLISECONDS));
440 +        long timeoutNanos = 999999L;
441 +        long startTime = System.nanoTime();
442 +        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
443 +        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
444 +        assertFalse(p.isTerminated());
445 +        startTime = System.nanoTime();
446 +        long timeoutMillis = timeoutMillis();
447 +        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
448 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
449 +        assertFalse(p.isTerminated());
450 +        p.shutdown();
451 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
452 +        assertTrue(p.isTerminated());
453 +    }
454  
455      /**
456       * isTerminated is false before termination, true after
# Line 564 | Line 624 | public class ThreadPoolExecutorTest exte
624      }
625  
626      /**
627 <     * shutDownNow returns a list containing tasks that were not run
627 >     * shutdownNow returns a list containing tasks that were not run,
628 >     * and those tasks are drained from the queue
629       */
630 <    public void testShutDownNow() {
630 >    public void testShutdownNow() {
631          final ThreadPoolExecutor p =
632              new ThreadPoolExecutor(1, 1,
633                                     LONG_DELAY_MS, MILLISECONDS,
# Line 582 | Line 643 | public class ThreadPoolExecutorTest exte
643              } catch (SecurityException ok) { return; }
644          }
645          assertTrue(p.isShutdown());
646 +        assertTrue(p.getQueue().isEmpty());
647          assertTrue(l.size() <= 4);
648      }
649  
650      // Exception Tests
651  
590
652      /**
653       * Constructor throws if corePoolSize argument is less than zero
654       */
655      public void testConstructor1() {
656          try {
657 <            new ThreadPoolExecutor(-1, 1,
597 <                                   LONG_DELAY_MS, MILLISECONDS,
657 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
658                                     new ArrayBlockingQueue<Runnable>(10));
659              shouldThrow();
660          } catch (IllegalArgumentException success) {}
# Line 605 | Line 665 | public class ThreadPoolExecutorTest exte
665       */
666      public void testConstructor2() {
667          try {
668 <            new ThreadPoolExecutor(1, -1,
609 <                                   LONG_DELAY_MS, MILLISECONDS,
668 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
669                                     new ArrayBlockingQueue<Runnable>(10));
670              shouldThrow();
671          } catch (IllegalArgumentException success) {}
# Line 617 | Line 676 | public class ThreadPoolExecutorTest exte
676       */
677      public void testConstructor3() {
678          try {
679 <            new ThreadPoolExecutor(1, 0,
621 <                                   LONG_DELAY_MS, MILLISECONDS,
679 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
680                                     new ArrayBlockingQueue<Runnable>(10));
681              shouldThrow();
682          } catch (IllegalArgumentException success) {}
# Line 629 | Line 687 | public class ThreadPoolExecutorTest exte
687       */
688      public void testConstructor4() {
689          try {
690 <            new ThreadPoolExecutor(1, 2,
633 <                                   -1L, MILLISECONDS,
690 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
691                                     new ArrayBlockingQueue<Runnable>(10));
692              shouldThrow();
693          } catch (IllegalArgumentException success) {}
# Line 641 | Line 698 | public class ThreadPoolExecutorTest exte
698       */
699      public void testConstructor5() {
700          try {
701 <            new ThreadPoolExecutor(2, 1,
645 <                                   LONG_DELAY_MS, MILLISECONDS,
701 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
702                                     new ArrayBlockingQueue<Runnable>(10));
703              shouldThrow();
704          } catch (IllegalArgumentException success) {}
# Line 653 | Line 709 | public class ThreadPoolExecutorTest exte
709       */
710      public void testConstructorNullPointerException() {
711          try {
712 <            new ThreadPoolExecutor(1, 2,
657 <                                   LONG_DELAY_MS, MILLISECONDS,
712 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
713                                     (BlockingQueue) null);
714              shouldThrow();
715          } catch (NullPointerException success) {}
716      }
717  
663
664
718      /**
719       * Constructor throws if corePoolSize argument is less than zero
720       */
721      public void testConstructor6() {
722          try {
723 <            new ThreadPoolExecutor(-1, 1,
671 <                                   LONG_DELAY_MS, MILLISECONDS,
723 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
724                                     new ArrayBlockingQueue<Runnable>(10),
725                                     new SimpleThreadFactory());
726              shouldThrow();
# Line 680 | Line 732 | public class ThreadPoolExecutorTest exte
732       */
733      public void testConstructor7() {
734          try {
735 <            new ThreadPoolExecutor(1, -1,
684 <                                   LONG_DELAY_MS, MILLISECONDS,
735 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
736                                     new ArrayBlockingQueue<Runnable>(10),
737                                     new SimpleThreadFactory());
738              shouldThrow();
# Line 693 | Line 744 | public class ThreadPoolExecutorTest exte
744       */
745      public void testConstructor8() {
746          try {
747 <            new ThreadPoolExecutor(1, 0,
697 <                                   LONG_DELAY_MS, MILLISECONDS,
747 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
748                                     new ArrayBlockingQueue<Runnable>(10),
749                                     new SimpleThreadFactory());
750              shouldThrow();
# Line 706 | Line 756 | public class ThreadPoolExecutorTest exte
756       */
757      public void testConstructor9() {
758          try {
759 <            new ThreadPoolExecutor(1, 2,
710 <                                   -1L, MILLISECONDS,
759 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
760                                     new ArrayBlockingQueue<Runnable>(10),
761                                     new SimpleThreadFactory());
762              shouldThrow();
# Line 719 | Line 768 | public class ThreadPoolExecutorTest exte
768       */
769      public void testConstructor10() {
770          try {
771 <            new ThreadPoolExecutor(2, 1,
723 <                                   LONG_DELAY_MS, MILLISECONDS,
771 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
772                                     new ArrayBlockingQueue<Runnable>(10),
773                                     new SimpleThreadFactory());
774              shouldThrow();
# Line 732 | Line 780 | public class ThreadPoolExecutorTest exte
780       */
781      public void testConstructorNullPointerException2() {
782          try {
783 <            new ThreadPoolExecutor(1, 2,
736 <                                   LONG_DELAY_MS, MILLISECONDS,
783 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
784                                     (BlockingQueue) null,
785                                     new SimpleThreadFactory());
786              shouldThrow();
# Line 745 | Line 792 | public class ThreadPoolExecutorTest exte
792       */
793      public void testConstructorNullPointerException3() {
794          try {
795 <            new ThreadPoolExecutor(1, 2,
749 <                                   LONG_DELAY_MS, MILLISECONDS,
795 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
796                                     new ArrayBlockingQueue<Runnable>(10),
797                                     (ThreadFactory) null);
798              shouldThrow();
799          } catch (NullPointerException success) {}
800      }
801  
756
802      /**
803       * Constructor throws if corePoolSize argument is less than zero
804       */
805      public void testConstructor11() {
806          try {
807 <            new ThreadPoolExecutor(-1, 1,
763 <                                   LONG_DELAY_MS, MILLISECONDS,
807 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
808                                     new ArrayBlockingQueue<Runnable>(10),
809                                     new NoOpREHandler());
810              shouldThrow();
# Line 772 | Line 816 | public class ThreadPoolExecutorTest exte
816       */
817      public void testConstructor12() {
818          try {
819 <            new ThreadPoolExecutor(1, -1,
776 <                                   LONG_DELAY_MS, MILLISECONDS,
819 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
820                                     new ArrayBlockingQueue<Runnable>(10),
821                                     new NoOpREHandler());
822              shouldThrow();
# Line 785 | Line 828 | public class ThreadPoolExecutorTest exte
828       */
829      public void testConstructor13() {
830          try {
831 <            new ThreadPoolExecutor(1, 0,
789 <                                   LONG_DELAY_MS, MILLISECONDS,
831 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
832                                     new ArrayBlockingQueue<Runnable>(10),
833                                     new NoOpREHandler());
834              shouldThrow();
# Line 798 | Line 840 | public class ThreadPoolExecutorTest exte
840       */
841      public void testConstructor14() {
842          try {
843 <            new ThreadPoolExecutor(1, 2,
802 <                                   -1L, MILLISECONDS,
843 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
844                                     new ArrayBlockingQueue<Runnable>(10),
845                                     new NoOpREHandler());
846              shouldThrow();
# Line 811 | Line 852 | public class ThreadPoolExecutorTest exte
852       */
853      public void testConstructor15() {
854          try {
855 <            new ThreadPoolExecutor(2, 1,
815 <                                   LONG_DELAY_MS, MILLISECONDS,
855 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
856                                     new ArrayBlockingQueue<Runnable>(10),
857                                     new NoOpREHandler());
858              shouldThrow();
# Line 824 | Line 864 | public class ThreadPoolExecutorTest exte
864       */
865      public void testConstructorNullPointerException4() {
866          try {
867 <            new ThreadPoolExecutor(1, 2,
828 <                                   LONG_DELAY_MS, MILLISECONDS,
867 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
868                                     (BlockingQueue) null,
869                                     new NoOpREHandler());
870              shouldThrow();
# Line 837 | Line 876 | public class ThreadPoolExecutorTest exte
876       */
877      public void testConstructorNullPointerException5() {
878          try {
879 <            new ThreadPoolExecutor(1, 2,
841 <                                   LONG_DELAY_MS, MILLISECONDS,
879 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
880                                     new ArrayBlockingQueue<Runnable>(10),
881                                     (RejectedExecutionHandler) null);
882              shouldThrow();
883          } catch (NullPointerException success) {}
884      }
885  
848
886      /**
887       * Constructor throws if corePoolSize argument is less than zero
888       */
889      public void testConstructor16() {
890          try {
891 <            new ThreadPoolExecutor(-1, 1,
855 <                                   LONG_DELAY_MS, MILLISECONDS,
891 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
892                                     new ArrayBlockingQueue<Runnable>(10),
893                                     new SimpleThreadFactory(),
894                                     new NoOpREHandler());
# Line 865 | Line 901 | public class ThreadPoolExecutorTest exte
901       */
902      public void testConstructor17() {
903          try {
904 <            new ThreadPoolExecutor(1, -1,
869 <                                   LONG_DELAY_MS, MILLISECONDS,
904 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
905                                     new ArrayBlockingQueue<Runnable>(10),
906                                     new SimpleThreadFactory(),
907                                     new NoOpREHandler());
# Line 879 | Line 914 | public class ThreadPoolExecutorTest exte
914       */
915      public void testConstructor18() {
916          try {
917 <            new ThreadPoolExecutor(1, 0,
883 <                                   LONG_DELAY_MS, MILLISECONDS,
917 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
918                                     new ArrayBlockingQueue<Runnable>(10),
919                                     new SimpleThreadFactory(),
920                                     new NoOpREHandler());
# Line 893 | Line 927 | public class ThreadPoolExecutorTest exte
927       */
928      public void testConstructor19() {
929          try {
930 <            new ThreadPoolExecutor(1, 2,
897 <                                   -1L, MILLISECONDS,
930 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
931                                     new ArrayBlockingQueue<Runnable>(10),
932                                     new SimpleThreadFactory(),
933                                     new NoOpREHandler());
# Line 907 | Line 940 | public class ThreadPoolExecutorTest exte
940       */
941      public void testConstructor20() {
942          try {
943 <            new ThreadPoolExecutor(2, 1,
911 <                                   LONG_DELAY_MS, MILLISECONDS,
943 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
944                                     new ArrayBlockingQueue<Runnable>(10),
945                                     new SimpleThreadFactory(),
946                                     new NoOpREHandler());
# Line 921 | Line 953 | public class ThreadPoolExecutorTest exte
953       */
954      public void testConstructorNullPointerException6() {
955          try {
956 <            new ThreadPoolExecutor(1, 2,
925 <                                   LONG_DELAY_MS, MILLISECONDS,
956 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
957                                     (BlockingQueue) null,
958                                     new SimpleThreadFactory(),
959                                     new NoOpREHandler());
# Line 935 | Line 966 | public class ThreadPoolExecutorTest exte
966       */
967      public void testConstructorNullPointerException7() {
968          try {
969 <            new ThreadPoolExecutor(1, 2,
939 <                                   LONG_DELAY_MS, MILLISECONDS,
969 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
970                                     new ArrayBlockingQueue<Runnable>(10),
971                                     new SimpleThreadFactory(),
972                                     (RejectedExecutionHandler) null);
# Line 949 | Line 979 | public class ThreadPoolExecutorTest exte
979       */
980      public void testConstructorNullPointerException8() {
981          try {
982 <            new ThreadPoolExecutor(1, 2,
953 <                                   LONG_DELAY_MS, MILLISECONDS,
982 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
983                                     new ArrayBlockingQueue<Runnable>(10),
984                                     (ThreadFactory) null,
985                                     new NoOpREHandler());
# Line 964 | Line 993 | public class ThreadPoolExecutorTest exte
993      public void testInterruptedSubmit() throws InterruptedException {
994          final ThreadPoolExecutor p =
995              new ThreadPoolExecutor(1, 1,
996 <                                   60, TimeUnit.SECONDS,
996 >                                   60, SECONDS,
997                                     new ArrayBlockingQueue<Runnable>(10));
998  
999          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 1212 | Line 1241 | public class ThreadPoolExecutorTest exte
1241          }
1242      }
1243  
1215
1244      /**
1245       * execute using DiscardOldestPolicy drops task on shutdown
1246       */
# Line 1234 | Line 1262 | public class ThreadPoolExecutorTest exte
1262          }
1263      }
1264  
1237
1265      /**
1266       * execute(null) throws NPE
1267       */
1268      public void testExecuteNull() {
1269          ThreadPoolExecutor p =
1270 <            new ThreadPoolExecutor(1, 2,
1244 <                                   LONG_DELAY_MS, MILLISECONDS,
1270 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1271                                     new ArrayBlockingQueue<Runnable>(10));
1272          try {
1273              p.execute(null);
# Line 1307 | Line 1333 | public class ThreadPoolExecutorTest exte
1333          joinPool(p);
1334      }
1335  
1336 +    /**
1337 +     * Configuration changes that allow core pool size greater than
1338 +     * max pool size result in IllegalArgumentException.
1339 +     */
1340 +    public void testPoolSizeInvariants() {
1341 +        ThreadPoolExecutor p =
1342 +            new ThreadPoolExecutor(1, 1,
1343 +                                   LONG_DELAY_MS, MILLISECONDS,
1344 +                                   new ArrayBlockingQueue<Runnable>(10));
1345 +        for (int s = 1; s < 5; s++) {
1346 +            p.setMaximumPoolSize(s);
1347 +            p.setCorePoolSize(s);
1348 +            try {
1349 +                p.setMaximumPoolSize(s - 1);
1350 +                shouldThrow();
1351 +            } catch (IllegalArgumentException success) {}
1352 +            assertEquals(s, p.getCorePoolSize());
1353 +            assertEquals(s, p.getMaximumPoolSize());
1354 +            try {
1355 +                p.setCorePoolSize(s + 1);
1356 +                shouldThrow();
1357 +            } catch (IllegalArgumentException success) {}
1358 +            assertEquals(s, p.getCorePoolSize());
1359 +            assertEquals(s, p.getMaximumPoolSize());
1360 +        }
1361 +        joinPool(p);
1362 +    }
1363  
1364      /**
1365       * setKeepAliveTime throws IllegalArgumentException
# Line 1333 | Line 1386 | public class ThreadPoolExecutorTest exte
1386      public void testTerminated() {
1387          ExtendedTPE p = new ExtendedTPE();
1388          try { p.shutdown(); } catch (SecurityException ok) { return; }
1389 <        assertTrue(p.terminatedCalled);
1389 >        assertTrue(p.terminatedCalled());
1390          joinPool(p);
1391      }
1392  
# Line 1343 | Line 1396 | public class ThreadPoolExecutorTest exte
1396      public void testBeforeAfter() throws InterruptedException {
1397          ExtendedTPE p = new ExtendedTPE();
1398          try {
1399 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1400 <            p.execute(r);
1401 <            delay(SHORT_DELAY_MS);
1402 <            assertTrue(r.done);
1403 <            assertTrue(p.beforeCalled);
1404 <            assertTrue(p.afterCalled);
1399 >            final CountDownLatch done = new CountDownLatch(1);
1400 >            p.execute(new CheckedRunnable() {
1401 >                public void realRun() {
1402 >                    done.countDown();
1403 >                }});
1404 >            await(p.afterCalled);
1405 >            assertEquals(0, done.getCount());
1406 >            assertTrue(p.afterCalled());
1407 >            assertTrue(p.beforeCalled());
1408              try { p.shutdown(); } catch (SecurityException ok) { return; }
1409          } finally {
1410              joinPool(p);
# Line 1406 | Line 1462 | public class ThreadPoolExecutorTest exte
1462          }
1463      }
1464  
1409
1465      /**
1466       * invokeAny(null) throws NPE
1467       */
# Line 1600 | Line 1655 | public class ThreadPoolExecutorTest exte
1655          }
1656      }
1657  
1603
1604
1658      /**
1659       * timed invokeAny(null) throws NPE
1660       */
# Line 1842 | Line 1895 | public class ThreadPoolExecutorTest exte
1895                                     LONG_DELAY_MS, MILLISECONDS,
1896                                     new ArrayBlockingQueue<Runnable>(10));
1897          try {
1898 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1899 <            l.add(new StringTask());
1900 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1901 <            l.add(new StringTask());
1902 <            List<Future<String>> futures =
1903 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1904 <            assertEquals(3, futures.size());
1905 <            Iterator<Future<String>> it = futures.iterator();
1906 <            Future<String> f1 = it.next();
1907 <            Future<String> f2 = it.next();
1908 <            Future<String> f3 = it.next();
1909 <            assertTrue(f1.isDone());
1910 <            assertTrue(f2.isDone());
1911 <            assertTrue(f3.isDone());
1912 <            assertFalse(f1.isCancelled());
1913 <            assertTrue(f2.isCancelled());
1898 >            for (long timeout = timeoutMillis();;) {
1899 >                List<Callable<String>> tasks = new ArrayList<>();
1900 >                tasks.add(new StringTask("0"));
1901 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1902 >                tasks.add(new StringTask("2"));
1903 >                long startTime = System.nanoTime();
1904 >                List<Future<String>> futures =
1905 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1906 >                assertEquals(tasks.size(), futures.size());
1907 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1908 >                for (Future future : futures)
1909 >                    assertTrue(future.isDone());
1910 >                assertTrue(futures.get(1).isCancelled());
1911 >                try {
1912 >                    assertEquals("0", futures.get(0).get());
1913 >                    assertEquals("2", futures.get(2).get());
1914 >                    break;
1915 >                } catch (CancellationException retryWithLongerTimeout) {
1916 >                    timeout *= 2;
1917 >                    if (timeout >= LONG_DELAY_MS / 2)
1918 >                        fail("expected exactly one task to be cancelled");
1919 >                }
1920 >            }
1921          } finally {
1922              joinPool(e);
1923          }
# Line 1903 | Line 1963 | public class ThreadPoolExecutorTest exte
1963       * allowCoreThreadTimeOut(true) causes idle threads to time out
1964       */
1965      public void testAllowCoreThreadTimeOut_true() throws Exception {
1966 +        long keepAliveTime = timeoutMillis();
1967          final ThreadPoolExecutor p =
1968              new ThreadPoolExecutor(2, 10,
1969 <                                   SHORT_DELAY_MS, MILLISECONDS,
1969 >                                   keepAliveTime, MILLISECONDS,
1970                                     new ArrayBlockingQueue<Runnable>(10));
1971          final CountDownLatch threadStarted = new CountDownLatch(1);
1972          try {
1973              p.allowCoreThreadTimeOut(true);
1974              p.execute(new CheckedRunnable() {
1975 <                public void realRun() throws InterruptedException {
1975 >                public void realRun() {
1976                      threadStarted.countDown();
1977                      assertEquals(1, p.getPoolSize());
1978                  }});
1979 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1980 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1981 <                if (p.getPoolSize() == 0)
1982 <                    break;
1983 <                delay(10);
1984 <            }
1979 >            await(threadStarted);
1980 >            delay(keepAliveTime);
1981 >            long startTime = System.nanoTime();
1982 >            while (p.getPoolSize() > 0
1983 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1984 >                Thread.yield();
1985 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1986              assertEquals(0, p.getPoolSize());
1987          } finally {
1988              joinPool(p);
# Line 1931 | Line 1993 | public class ThreadPoolExecutorTest exte
1993       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1994       */
1995      public void testAllowCoreThreadTimeOut_false() throws Exception {
1996 +        long keepAliveTime = timeoutMillis();
1997          final ThreadPoolExecutor p =
1998              new ThreadPoolExecutor(2, 10,
1999 <                                   SHORT_DELAY_MS, MILLISECONDS,
1999 >                                   keepAliveTime, MILLISECONDS,
2000                                     new ArrayBlockingQueue<Runnable>(10));
2001          final CountDownLatch threadStarted = new CountDownLatch(1);
2002          try {
# Line 1943 | Line 2006 | public class ThreadPoolExecutorTest exte
2006                      threadStarted.countDown();
2007                      assertTrue(p.getPoolSize() >= 1);
2008                  }});
2009 <            delay(SMALL_DELAY_MS);
2009 >            delay(2 * keepAliveTime);
2010              assertTrue(p.getPoolSize() >= 1);
2011          } finally {
2012              joinPool(p);
# Line 1962 | Line 2025 | public class ThreadPoolExecutorTest exte
2025                  done.countDown();
2026              }};
2027          final ThreadPoolExecutor p =
2028 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2028 >            new ThreadPoolExecutor(1, 30,
2029 >                                   60, SECONDS,
2030                                     new ArrayBlockingQueue(30));
2031          try {
2032              for (int i = 0; i < nTasks; ++i) {
# Line 1977 | Line 2041 | public class ThreadPoolExecutorTest exte
2041              // enough time to run all tasks
2042              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2043          } finally {
2044 <            p.shutdown();
2044 >            joinPool(p);
2045          }
2046      }
2047  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines