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.61 by jsr166, Mon Sep 28 02:41:29 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 > import java.util.concurrent.atomic.AtomicInteger;
33 >
34 > import junit.framework.Test;
35 > import junit.framework.TestSuite;
36  
37   public class ThreadPoolExecutorTest extends JSR166TestCase {
38      public static void main(String[] args) {
39 <        junit.textui.TestRunner.run(suite());
39 >        main(suite(), args);
40      }
41      public static Test suite() {
42          return new TestSuite(ThreadPoolExecutorTest.class);
43      }
44  
45      static class ExtendedTPE extends ThreadPoolExecutor {
46 <        volatile boolean beforeCalled = false;
47 <        volatile boolean afterCalled = false;
48 <        volatile boolean terminatedCalled = false;
46 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
47 >        final CountDownLatch afterCalled = new CountDownLatch(1);
48 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
49 >
50          public ExtendedTPE() {
51              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
52          }
53          protected void beforeExecute(Thread t, Runnable r) {
54 <            beforeCalled = true;
54 >            beforeCalled.countDown();
55          }
56          protected void afterExecute(Runnable r, Throwable t) {
57 <            afterCalled = true;
57 >            afterCalled.countDown();
58          }
59          protected void terminated() {
60 <            terminatedCalled = true;
60 >            terminatedCalled.countDown();
61 >        }
62 >
63 >        public boolean beforeCalled() {
64 >            return beforeCalled.getCount() == 0;
65 >        }
66 >        public boolean afterCalled() {
67 >            return afterCalled.getCount() == 0;
68 >        }
69 >        public boolean terminatedCalled() {
70 >            return terminatedCalled.getCount() == 0;
71          }
72      }
73  
# Line 46 | Line 79 | public class ThreadPoolExecutorTest exte
79          }
80      }
81  
49
82      /**
83       * execute successfully executes a runnable
84       */
# Line 150 | Line 182 | public class ThreadPoolExecutorTest exte
182                      threadProceed.await();
183                      threadDone.countDown();
184                  }});
185 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
185 >            await(threadStarted);
186              assertEquals(0, p.getCompletedTaskCount());
187              threadProceed.countDown();
188              threadDone.await();
189 <            delay(SHORT_DELAY_MS);
190 <            assertEquals(1, p.getCompletedTaskCount());
189 >            long startTime = System.nanoTime();
190 >            while (p.getCompletedTaskCount() != 1) {
191 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
192 >                    fail("timed out");
193 >                Thread.yield();
194 >            }
195          } finally {
196              joinPool(p);
197          }
# Line 181 | Line 217 | public class ThreadPoolExecutorTest exte
217              new ThreadPoolExecutor(2, 2,
218                                     1000, MILLISECONDS,
219                                     new ArrayBlockingQueue<Runnable>(10));
220 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
220 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
221          joinPool(p);
222      }
223  
188
224      /**
225       * getThreadFactory returns factory in constructor if not set
226       */
# Line 215 | Line 250 | public class ThreadPoolExecutorTest exte
250          joinPool(p);
251      }
252  
218
253      /**
254       * setThreadFactory(null) throws NPE
255       */
# Line 262 | Line 296 | public class ThreadPoolExecutorTest exte
296          joinPool(p);
297      }
298  
265
299      /**
300       * setRejectedExecutionHandler(null) throws NPE
301       */
# Line 280 | Line 313 | public class ThreadPoolExecutorTest exte
313          }
314      }
315  
283
316      /**
317       * getLargestPoolSize increases, but doesn't overestimate, when
318       * multiple threads active
# Line 378 | Line 410 | public class ThreadPoolExecutorTest exte
410      }
411  
412      /**
413 <     * isShutDown is false before shutdown, true after
413 >     * isShutdown is false before shutdown, true after
414       */
415      public void testIsShutdown() {
416          final ThreadPoolExecutor p =
# Line 391 | Line 423 | public class ThreadPoolExecutorTest exte
423          joinPool(p);
424      }
425  
426 +    /**
427 +     * awaitTermination on a non-shutdown pool times out
428 +     */
429 +    public void testAwaitTermination_timesOut() throws InterruptedException {
430 +        final ThreadPoolExecutor p =
431 +            new ThreadPoolExecutor(1, 1,
432 +                                   LONG_DELAY_MS, MILLISECONDS,
433 +                                   new ArrayBlockingQueue<Runnable>(10));
434 +        assertFalse(p.isTerminated());
435 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
436 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
437 +        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
438 +        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
439 +        assertFalse(p.awaitTermination(0L, NANOSECONDS));
440 +        assertFalse(p.awaitTermination(0L, MILLISECONDS));
441 +        long timeoutNanos = 999999L;
442 +        long startTime = System.nanoTime();
443 +        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
444 +        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
445 +        assertFalse(p.isTerminated());
446 +        startTime = System.nanoTime();
447 +        long timeoutMillis = timeoutMillis();
448 +        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
449 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
450 +        assertFalse(p.isTerminated());
451 +        p.shutdown();
452 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
453 +        assertTrue(p.isTerminated());
454 +    }
455  
456      /**
457       * isTerminated is false before termination, true after
# Line 564 | Line 625 | public class ThreadPoolExecutorTest exte
625      }
626  
627      /**
628 <     * shutDownNow returns a list containing tasks that were not run
628 >     * shutdownNow returns a list containing tasks that were not run,
629 >     * and those tasks are drained from the queue
630       */
631 <    public void testShutDownNow() {
631 >    public void testShutdownNow() throws InterruptedException {
632 >        final int poolSize = 2;
633 >        final int count = 5;
634 >        final AtomicInteger ran = new AtomicInteger(0);
635          final ThreadPoolExecutor p =
636 <            new ThreadPoolExecutor(1, 1,
636 >            new ThreadPoolExecutor(poolSize, poolSize,
637                                     LONG_DELAY_MS, MILLISECONDS,
638                                     new ArrayBlockingQueue<Runnable>(10));
639 <        List l;
640 <        try {
641 <            for (int i = 0; i < 5; i++)
577 <                p.execute(new MediumPossiblyInterruptedRunnable());
578 <        }
579 <        finally {
639 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
640 >        CheckedRunnable waiter = new CheckedRunnable() { public void realRun() {
641 >            threadsStarted.countDown();
642              try {
643 <                l = p.shutdownNow();
644 <            } catch (SecurityException ok) { return; }
643 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
644 >            } catch (InterruptedException success) {}
645 >            ran.getAndIncrement();
646 >        }};
647 >        for (int i = 0; i < count; i++)
648 >            p.execute(waiter);
649 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
650 >        final List<Runnable> queuedTasks;
651 >        try {
652 >            queuedTasks = p.shutdownNow();
653 >        } catch (SecurityException ok) {
654 >            return; // Allowed in case test doesn't have privs
655          }
656          assertTrue(p.isShutdown());
657 <        assertTrue(l.size() <= 4);
657 >        assertTrue(p.getQueue().isEmpty());
658 >        assertEquals(count - poolSize, queuedTasks.size());
659 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
660 >        assertTrue(p.isTerminated());
661 >        assertEquals(poolSize, ran.get());
662      }
663  
664      // Exception Tests
665  
590
666      /**
667       * Constructor throws if corePoolSize argument is less than zero
668       */
669      public void testConstructor1() {
670          try {
671 <            new ThreadPoolExecutor(-1, 1,
597 <                                   LONG_DELAY_MS, MILLISECONDS,
671 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
672                                     new ArrayBlockingQueue<Runnable>(10));
673              shouldThrow();
674          } catch (IllegalArgumentException success) {}
# Line 605 | Line 679 | public class ThreadPoolExecutorTest exte
679       */
680      public void testConstructor2() {
681          try {
682 <            new ThreadPoolExecutor(1, -1,
609 <                                   LONG_DELAY_MS, MILLISECONDS,
682 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
683                                     new ArrayBlockingQueue<Runnable>(10));
684              shouldThrow();
685          } catch (IllegalArgumentException success) {}
# Line 617 | Line 690 | public class ThreadPoolExecutorTest exte
690       */
691      public void testConstructor3() {
692          try {
693 <            new ThreadPoolExecutor(1, 0,
621 <                                   LONG_DELAY_MS, MILLISECONDS,
693 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
694                                     new ArrayBlockingQueue<Runnable>(10));
695              shouldThrow();
696          } catch (IllegalArgumentException success) {}
# Line 629 | Line 701 | public class ThreadPoolExecutorTest exte
701       */
702      public void testConstructor4() {
703          try {
704 <            new ThreadPoolExecutor(1, 2,
633 <                                   -1L, MILLISECONDS,
704 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
705                                     new ArrayBlockingQueue<Runnable>(10));
706              shouldThrow();
707          } catch (IllegalArgumentException success) {}
# Line 641 | Line 712 | public class ThreadPoolExecutorTest exte
712       */
713      public void testConstructor5() {
714          try {
715 <            new ThreadPoolExecutor(2, 1,
645 <                                   LONG_DELAY_MS, MILLISECONDS,
715 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
716                                     new ArrayBlockingQueue<Runnable>(10));
717              shouldThrow();
718          } catch (IllegalArgumentException success) {}
# Line 653 | Line 723 | public class ThreadPoolExecutorTest exte
723       */
724      public void testConstructorNullPointerException() {
725          try {
726 <            new ThreadPoolExecutor(1, 2,
657 <                                   LONG_DELAY_MS, MILLISECONDS,
726 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
727                                     (BlockingQueue) null);
728              shouldThrow();
729          } catch (NullPointerException success) {}
730      }
731  
663
664
732      /**
733       * Constructor throws if corePoolSize argument is less than zero
734       */
735      public void testConstructor6() {
736          try {
737 <            new ThreadPoolExecutor(-1, 1,
671 <                                   LONG_DELAY_MS, MILLISECONDS,
737 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
738                                     new ArrayBlockingQueue<Runnable>(10),
739                                     new SimpleThreadFactory());
740              shouldThrow();
# Line 680 | Line 746 | public class ThreadPoolExecutorTest exte
746       */
747      public void testConstructor7() {
748          try {
749 <            new ThreadPoolExecutor(1, -1,
684 <                                   LONG_DELAY_MS, MILLISECONDS,
749 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
750                                     new ArrayBlockingQueue<Runnable>(10),
751                                     new SimpleThreadFactory());
752              shouldThrow();
# Line 693 | Line 758 | public class ThreadPoolExecutorTest exte
758       */
759      public void testConstructor8() {
760          try {
761 <            new ThreadPoolExecutor(1, 0,
697 <                                   LONG_DELAY_MS, MILLISECONDS,
761 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
762                                     new ArrayBlockingQueue<Runnable>(10),
763                                     new SimpleThreadFactory());
764              shouldThrow();
# Line 706 | Line 770 | public class ThreadPoolExecutorTest exte
770       */
771      public void testConstructor9() {
772          try {
773 <            new ThreadPoolExecutor(1, 2,
710 <                                   -1L, MILLISECONDS,
773 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
774                                     new ArrayBlockingQueue<Runnable>(10),
775                                     new SimpleThreadFactory());
776              shouldThrow();
# Line 719 | Line 782 | public class ThreadPoolExecutorTest exte
782       */
783      public void testConstructor10() {
784          try {
785 <            new ThreadPoolExecutor(2, 1,
723 <                                   LONG_DELAY_MS, MILLISECONDS,
785 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
786                                     new ArrayBlockingQueue<Runnable>(10),
787                                     new SimpleThreadFactory());
788              shouldThrow();
# Line 732 | Line 794 | public class ThreadPoolExecutorTest exte
794       */
795      public void testConstructorNullPointerException2() {
796          try {
797 <            new ThreadPoolExecutor(1, 2,
736 <                                   LONG_DELAY_MS, MILLISECONDS,
797 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
798                                     (BlockingQueue) null,
799                                     new SimpleThreadFactory());
800              shouldThrow();
# Line 745 | Line 806 | public class ThreadPoolExecutorTest exte
806       */
807      public void testConstructorNullPointerException3() {
808          try {
809 <            new ThreadPoolExecutor(1, 2,
749 <                                   LONG_DELAY_MS, MILLISECONDS,
809 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
810                                     new ArrayBlockingQueue<Runnable>(10),
811                                     (ThreadFactory) null);
812              shouldThrow();
813          } catch (NullPointerException success) {}
814      }
815  
756
816      /**
817       * Constructor throws if corePoolSize argument is less than zero
818       */
819      public void testConstructor11() {
820          try {
821 <            new ThreadPoolExecutor(-1, 1,
763 <                                   LONG_DELAY_MS, MILLISECONDS,
821 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
822                                     new ArrayBlockingQueue<Runnable>(10),
823                                     new NoOpREHandler());
824              shouldThrow();
# Line 772 | Line 830 | public class ThreadPoolExecutorTest exte
830       */
831      public void testConstructor12() {
832          try {
833 <            new ThreadPoolExecutor(1, -1,
776 <                                   LONG_DELAY_MS, MILLISECONDS,
833 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
834                                     new ArrayBlockingQueue<Runnable>(10),
835                                     new NoOpREHandler());
836              shouldThrow();
# Line 785 | Line 842 | public class ThreadPoolExecutorTest exte
842       */
843      public void testConstructor13() {
844          try {
845 <            new ThreadPoolExecutor(1, 0,
789 <                                   LONG_DELAY_MS, MILLISECONDS,
845 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
846                                     new ArrayBlockingQueue<Runnable>(10),
847                                     new NoOpREHandler());
848              shouldThrow();
# Line 798 | Line 854 | public class ThreadPoolExecutorTest exte
854       */
855      public void testConstructor14() {
856          try {
857 <            new ThreadPoolExecutor(1, 2,
802 <                                   -1L, MILLISECONDS,
857 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
858                                     new ArrayBlockingQueue<Runnable>(10),
859                                     new NoOpREHandler());
860              shouldThrow();
# Line 811 | Line 866 | public class ThreadPoolExecutorTest exte
866       */
867      public void testConstructor15() {
868          try {
869 <            new ThreadPoolExecutor(2, 1,
815 <                                   LONG_DELAY_MS, MILLISECONDS,
869 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
870                                     new ArrayBlockingQueue<Runnable>(10),
871                                     new NoOpREHandler());
872              shouldThrow();
# Line 824 | Line 878 | public class ThreadPoolExecutorTest exte
878       */
879      public void testConstructorNullPointerException4() {
880          try {
881 <            new ThreadPoolExecutor(1, 2,
828 <                                   LONG_DELAY_MS, MILLISECONDS,
881 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
882                                     (BlockingQueue) null,
883                                     new NoOpREHandler());
884              shouldThrow();
# Line 837 | Line 890 | public class ThreadPoolExecutorTest exte
890       */
891      public void testConstructorNullPointerException5() {
892          try {
893 <            new ThreadPoolExecutor(1, 2,
841 <                                   LONG_DELAY_MS, MILLISECONDS,
893 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
894                                     new ArrayBlockingQueue<Runnable>(10),
895                                     (RejectedExecutionHandler) null);
896              shouldThrow();
897          } catch (NullPointerException success) {}
898      }
899  
848
900      /**
901       * Constructor throws if corePoolSize argument is less than zero
902       */
903      public void testConstructor16() {
904          try {
905 <            new ThreadPoolExecutor(-1, 1,
855 <                                   LONG_DELAY_MS, MILLISECONDS,
905 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
906                                     new ArrayBlockingQueue<Runnable>(10),
907                                     new SimpleThreadFactory(),
908                                     new NoOpREHandler());
# Line 865 | Line 915 | public class ThreadPoolExecutorTest exte
915       */
916      public void testConstructor17() {
917          try {
918 <            new ThreadPoolExecutor(1, -1,
869 <                                   LONG_DELAY_MS, MILLISECONDS,
918 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
919                                     new ArrayBlockingQueue<Runnable>(10),
920                                     new SimpleThreadFactory(),
921                                     new NoOpREHandler());
# Line 879 | Line 928 | public class ThreadPoolExecutorTest exte
928       */
929      public void testConstructor18() {
930          try {
931 <            new ThreadPoolExecutor(1, 0,
883 <                                   LONG_DELAY_MS, MILLISECONDS,
931 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
932                                     new ArrayBlockingQueue<Runnable>(10),
933                                     new SimpleThreadFactory(),
934                                     new NoOpREHandler());
# Line 893 | Line 941 | public class ThreadPoolExecutorTest exte
941       */
942      public void testConstructor19() {
943          try {
944 <            new ThreadPoolExecutor(1, 2,
897 <                                   -1L, MILLISECONDS,
944 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
945                                     new ArrayBlockingQueue<Runnable>(10),
946                                     new SimpleThreadFactory(),
947                                     new NoOpREHandler());
# Line 907 | Line 954 | public class ThreadPoolExecutorTest exte
954       */
955      public void testConstructor20() {
956          try {
957 <            new ThreadPoolExecutor(2, 1,
911 <                                   LONG_DELAY_MS, MILLISECONDS,
957 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
958                                     new ArrayBlockingQueue<Runnable>(10),
959                                     new SimpleThreadFactory(),
960                                     new NoOpREHandler());
# Line 921 | Line 967 | public class ThreadPoolExecutorTest exte
967       */
968      public void testConstructorNullPointerException6() {
969          try {
970 <            new ThreadPoolExecutor(1, 2,
925 <                                   LONG_DELAY_MS, MILLISECONDS,
970 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
971                                     (BlockingQueue) null,
972                                     new SimpleThreadFactory(),
973                                     new NoOpREHandler());
# Line 935 | Line 980 | public class ThreadPoolExecutorTest exte
980       */
981      public void testConstructorNullPointerException7() {
982          try {
983 <            new ThreadPoolExecutor(1, 2,
939 <                                   LONG_DELAY_MS, MILLISECONDS,
983 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
984                                     new ArrayBlockingQueue<Runnable>(10),
985                                     new SimpleThreadFactory(),
986                                     (RejectedExecutionHandler) null);
# Line 949 | Line 993 | public class ThreadPoolExecutorTest exte
993       */
994      public void testConstructorNullPointerException8() {
995          try {
996 <            new ThreadPoolExecutor(1, 2,
953 <                                   LONG_DELAY_MS, MILLISECONDS,
996 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
997                                     new ArrayBlockingQueue<Runnable>(10),
998                                     (ThreadFactory) null,
999                                     new NoOpREHandler());
# Line 964 | Line 1007 | public class ThreadPoolExecutorTest exte
1007      public void testInterruptedSubmit() throws InterruptedException {
1008          final ThreadPoolExecutor p =
1009              new ThreadPoolExecutor(1, 1,
1010 <                                   60, TimeUnit.SECONDS,
1010 >                                   60, SECONDS,
1011                                     new ArrayBlockingQueue<Runnable>(10));
1012  
1013          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 1212 | Line 1255 | public class ThreadPoolExecutorTest exte
1255          }
1256      }
1257  
1215
1258      /**
1259       * execute using DiscardOldestPolicy drops task on shutdown
1260       */
# Line 1234 | Line 1276 | public class ThreadPoolExecutorTest exte
1276          }
1277      }
1278  
1237
1279      /**
1280       * execute(null) throws NPE
1281       */
1282      public void testExecuteNull() {
1283          ThreadPoolExecutor p =
1284 <            new ThreadPoolExecutor(1, 2,
1244 <                                   LONG_DELAY_MS, MILLISECONDS,
1284 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1285                                     new ArrayBlockingQueue<Runnable>(10));
1286          try {
1287              p.execute(null);
# Line 1307 | Line 1347 | public class ThreadPoolExecutorTest exte
1347          joinPool(p);
1348      }
1349  
1350 +    /**
1351 +     * Configuration changes that allow core pool size greater than
1352 +     * max pool size result in IllegalArgumentException.
1353 +     */
1354 +    public void testPoolSizeInvariants() {
1355 +        ThreadPoolExecutor p =
1356 +            new ThreadPoolExecutor(1, 1,
1357 +                                   LONG_DELAY_MS, MILLISECONDS,
1358 +                                   new ArrayBlockingQueue<Runnable>(10));
1359 +        for (int s = 1; s < 5; s++) {
1360 +            p.setMaximumPoolSize(s);
1361 +            p.setCorePoolSize(s);
1362 +            try {
1363 +                p.setMaximumPoolSize(s - 1);
1364 +                shouldThrow();
1365 +            } catch (IllegalArgumentException success) {}
1366 +            assertEquals(s, p.getCorePoolSize());
1367 +            assertEquals(s, p.getMaximumPoolSize());
1368 +            try {
1369 +                p.setCorePoolSize(s + 1);
1370 +                shouldThrow();
1371 +            } catch (IllegalArgumentException success) {}
1372 +            assertEquals(s, p.getCorePoolSize());
1373 +            assertEquals(s, p.getMaximumPoolSize());
1374 +        }
1375 +        joinPool(p);
1376 +    }
1377  
1378      /**
1379       * setKeepAliveTime throws IllegalArgumentException
# Line 1333 | Line 1400 | public class ThreadPoolExecutorTest exte
1400      public void testTerminated() {
1401          ExtendedTPE p = new ExtendedTPE();
1402          try { p.shutdown(); } catch (SecurityException ok) { return; }
1403 <        assertTrue(p.terminatedCalled);
1403 >        assertTrue(p.terminatedCalled());
1404          joinPool(p);
1405      }
1406  
# Line 1343 | Line 1410 | public class ThreadPoolExecutorTest exte
1410      public void testBeforeAfter() throws InterruptedException {
1411          ExtendedTPE p = new ExtendedTPE();
1412          try {
1413 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1414 <            p.execute(r);
1415 <            delay(SHORT_DELAY_MS);
1416 <            assertTrue(r.done);
1417 <            assertTrue(p.beforeCalled);
1418 <            assertTrue(p.afterCalled);
1413 >            final CountDownLatch done = new CountDownLatch(1);
1414 >            p.execute(new CheckedRunnable() {
1415 >                public void realRun() {
1416 >                    done.countDown();
1417 >                }});
1418 >            await(p.afterCalled);
1419 >            assertEquals(0, done.getCount());
1420 >            assertTrue(p.afterCalled());
1421 >            assertTrue(p.beforeCalled());
1422              try { p.shutdown(); } catch (SecurityException ok) { return; }
1423          } finally {
1424              joinPool(p);
# Line 1406 | Line 1476 | public class ThreadPoolExecutorTest exte
1476          }
1477      }
1478  
1409
1479      /**
1480       * invokeAny(null) throws NPE
1481       */
# Line 1600 | Line 1669 | public class ThreadPoolExecutorTest exte
1669          }
1670      }
1671  
1603
1604
1672      /**
1673       * timed invokeAny(null) throws NPE
1674       */
# Line 1842 | Line 1909 | public class ThreadPoolExecutorTest exte
1909                                     LONG_DELAY_MS, MILLISECONDS,
1910                                     new ArrayBlockingQueue<Runnable>(10));
1911          try {
1912 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1913 <            l.add(new StringTask());
1914 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1915 <            l.add(new StringTask());
1916 <            List<Future<String>> futures =
1917 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1918 <            assertEquals(3, futures.size());
1919 <            Iterator<Future<String>> it = futures.iterator();
1920 <            Future<String> f1 = it.next();
1921 <            Future<String> f2 = it.next();
1922 <            Future<String> f3 = it.next();
1923 <            assertTrue(f1.isDone());
1924 <            assertTrue(f2.isDone());
1925 <            assertTrue(f3.isDone());
1926 <            assertFalse(f1.isCancelled());
1927 <            assertTrue(f2.isCancelled());
1912 >            for (long timeout = timeoutMillis();;) {
1913 >                List<Callable<String>> tasks = new ArrayList<>();
1914 >                tasks.add(new StringTask("0"));
1915 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1916 >                tasks.add(new StringTask("2"));
1917 >                long startTime = System.nanoTime();
1918 >                List<Future<String>> futures =
1919 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1920 >                assertEquals(tasks.size(), futures.size());
1921 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1922 >                for (Future future : futures)
1923 >                    assertTrue(future.isDone());
1924 >                assertTrue(futures.get(1).isCancelled());
1925 >                try {
1926 >                    assertEquals("0", futures.get(0).get());
1927 >                    assertEquals("2", futures.get(2).get());
1928 >                    break;
1929 >                } catch (CancellationException retryWithLongerTimeout) {
1930 >                    timeout *= 2;
1931 >                    if (timeout >= LONG_DELAY_MS / 2)
1932 >                        fail("expected exactly one task to be cancelled");
1933 >                }
1934 >            }
1935          } finally {
1936              joinPool(e);
1937          }
# Line 1903 | Line 1977 | public class ThreadPoolExecutorTest exte
1977       * allowCoreThreadTimeOut(true) causes idle threads to time out
1978       */
1979      public void testAllowCoreThreadTimeOut_true() throws Exception {
1980 +        long keepAliveTime = timeoutMillis();
1981          final ThreadPoolExecutor p =
1982              new ThreadPoolExecutor(2, 10,
1983 <                                   SHORT_DELAY_MS, MILLISECONDS,
1983 >                                   keepAliveTime, MILLISECONDS,
1984                                     new ArrayBlockingQueue<Runnable>(10));
1985          final CountDownLatch threadStarted = new CountDownLatch(1);
1986          try {
1987              p.allowCoreThreadTimeOut(true);
1988              p.execute(new CheckedRunnable() {
1989 <                public void realRun() throws InterruptedException {
1989 >                public void realRun() {
1990                      threadStarted.countDown();
1991                      assertEquals(1, p.getPoolSize());
1992                  }});
1993 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1994 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1995 <                if (p.getPoolSize() == 0)
1996 <                    break;
1997 <                delay(10);
1998 <            }
1993 >            await(threadStarted);
1994 >            delay(keepAliveTime);
1995 >            long startTime = System.nanoTime();
1996 >            while (p.getPoolSize() > 0
1997 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1998 >                Thread.yield();
1999 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
2000              assertEquals(0, p.getPoolSize());
2001          } finally {
2002              joinPool(p);
# Line 1931 | Line 2007 | public class ThreadPoolExecutorTest exte
2007       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2008       */
2009      public void testAllowCoreThreadTimeOut_false() throws Exception {
2010 +        long keepAliveTime = timeoutMillis();
2011          final ThreadPoolExecutor p =
2012              new ThreadPoolExecutor(2, 10,
2013 <                                   SHORT_DELAY_MS, MILLISECONDS,
2013 >                                   keepAliveTime, MILLISECONDS,
2014                                     new ArrayBlockingQueue<Runnable>(10));
2015          final CountDownLatch threadStarted = new CountDownLatch(1);
2016          try {
# Line 1943 | Line 2020 | public class ThreadPoolExecutorTest exte
2020                      threadStarted.countDown();
2021                      assertTrue(p.getPoolSize() >= 1);
2022                  }});
2023 <            delay(SMALL_DELAY_MS);
2023 >            delay(2 * keepAliveTime);
2024              assertTrue(p.getPoolSize() >= 1);
2025          } finally {
2026              joinPool(p);
# Line 1962 | Line 2039 | public class ThreadPoolExecutorTest exte
2039                  done.countDown();
2040              }};
2041          final ThreadPoolExecutor p =
2042 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2042 >            new ThreadPoolExecutor(1, 30,
2043 >                                   60, SECONDS,
2044                                     new ArrayBlockingQueue(30));
2045          try {
2046              for (int i = 0; i < nTasks; ++i) {
# Line 1977 | Line 2055 | public class ThreadPoolExecutorTest exte
2055              // enough time to run all tasks
2056              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2057          } finally {
2058 <            p.shutdown();
2058 >            joinPool(p);
2059          }
2060      }
2061  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines