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.45 by jsr166, Sun May 29 07:01:17 2011 UTC vs.
Revision 1.53 by jsr166, Fri May 15 18:21:19 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.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);
# Line 402 | Line 422 | public class ThreadPoolExecutorTest exte
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
456       */
457      public void testIsTerminated() throws InterruptedException {
# Line 601 | Line 651 | public class ThreadPoolExecutorTest exte
651       */
652      public void testConstructor1() {
653          try {
654 <            new ThreadPoolExecutor(-1, 1,
605 <                                   LONG_DELAY_MS, MILLISECONDS,
654 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
655                                     new ArrayBlockingQueue<Runnable>(10));
656              shouldThrow();
657          } catch (IllegalArgumentException success) {}
# Line 613 | Line 662 | public class ThreadPoolExecutorTest exte
662       */
663      public void testConstructor2() {
664          try {
665 <            new ThreadPoolExecutor(1, -1,
617 <                                   LONG_DELAY_MS, MILLISECONDS,
665 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
666                                     new ArrayBlockingQueue<Runnable>(10));
667              shouldThrow();
668          } catch (IllegalArgumentException success) {}
# Line 625 | Line 673 | public class ThreadPoolExecutorTest exte
673       */
674      public void testConstructor3() {
675          try {
676 <            new ThreadPoolExecutor(1, 0,
629 <                                   LONG_DELAY_MS, MILLISECONDS,
676 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
677                                     new ArrayBlockingQueue<Runnable>(10));
678              shouldThrow();
679          } catch (IllegalArgumentException success) {}
# Line 637 | Line 684 | public class ThreadPoolExecutorTest exte
684       */
685      public void testConstructor4() {
686          try {
687 <            new ThreadPoolExecutor(1, 2,
641 <                                   -1L, MILLISECONDS,
687 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
688                                     new ArrayBlockingQueue<Runnable>(10));
689              shouldThrow();
690          } catch (IllegalArgumentException success) {}
# Line 649 | Line 695 | public class ThreadPoolExecutorTest exte
695       */
696      public void testConstructor5() {
697          try {
698 <            new ThreadPoolExecutor(2, 1,
653 <                                   LONG_DELAY_MS, MILLISECONDS,
698 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
699                                     new ArrayBlockingQueue<Runnable>(10));
700              shouldThrow();
701          } catch (IllegalArgumentException success) {}
# Line 661 | Line 706 | public class ThreadPoolExecutorTest exte
706       */
707      public void testConstructorNullPointerException() {
708          try {
709 <            new ThreadPoolExecutor(1, 2,
665 <                                   LONG_DELAY_MS, MILLISECONDS,
709 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
710                                     (BlockingQueue) null);
711              shouldThrow();
712          } catch (NullPointerException success) {}
# Line 673 | Line 717 | public class ThreadPoolExecutorTest exte
717       */
718      public void testConstructor6() {
719          try {
720 <            new ThreadPoolExecutor(-1, 1,
677 <                                   LONG_DELAY_MS, MILLISECONDS,
720 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
721                                     new ArrayBlockingQueue<Runnable>(10),
722                                     new SimpleThreadFactory());
723              shouldThrow();
# Line 686 | Line 729 | public class ThreadPoolExecutorTest exte
729       */
730      public void testConstructor7() {
731          try {
732 <            new ThreadPoolExecutor(1, -1,
690 <                                   LONG_DELAY_MS, MILLISECONDS,
732 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
733                                     new ArrayBlockingQueue<Runnable>(10),
734                                     new SimpleThreadFactory());
735              shouldThrow();
# Line 699 | Line 741 | public class ThreadPoolExecutorTest exte
741       */
742      public void testConstructor8() {
743          try {
744 <            new ThreadPoolExecutor(1, 0,
703 <                                   LONG_DELAY_MS, MILLISECONDS,
744 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
745                                     new ArrayBlockingQueue<Runnable>(10),
746                                     new SimpleThreadFactory());
747              shouldThrow();
# Line 712 | Line 753 | public class ThreadPoolExecutorTest exte
753       */
754      public void testConstructor9() {
755          try {
756 <            new ThreadPoolExecutor(1, 2,
716 <                                   -1L, MILLISECONDS,
756 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
757                                     new ArrayBlockingQueue<Runnable>(10),
758                                     new SimpleThreadFactory());
759              shouldThrow();
# Line 725 | Line 765 | public class ThreadPoolExecutorTest exte
765       */
766      public void testConstructor10() {
767          try {
768 <            new ThreadPoolExecutor(2, 1,
729 <                                   LONG_DELAY_MS, MILLISECONDS,
768 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
769                                     new ArrayBlockingQueue<Runnable>(10),
770                                     new SimpleThreadFactory());
771              shouldThrow();
# Line 738 | Line 777 | public class ThreadPoolExecutorTest exte
777       */
778      public void testConstructorNullPointerException2() {
779          try {
780 <            new ThreadPoolExecutor(1, 2,
742 <                                   LONG_DELAY_MS, MILLISECONDS,
780 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
781                                     (BlockingQueue) null,
782                                     new SimpleThreadFactory());
783              shouldThrow();
# Line 751 | Line 789 | public class ThreadPoolExecutorTest exte
789       */
790      public void testConstructorNullPointerException3() {
791          try {
792 <            new ThreadPoolExecutor(1, 2,
755 <                                   LONG_DELAY_MS, MILLISECONDS,
792 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
793                                     new ArrayBlockingQueue<Runnable>(10),
794                                     (ThreadFactory) null);
795              shouldThrow();
# Line 764 | Line 801 | public class ThreadPoolExecutorTest exte
801       */
802      public void testConstructor11() {
803          try {
804 <            new ThreadPoolExecutor(-1, 1,
768 <                                   LONG_DELAY_MS, MILLISECONDS,
804 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
805                                     new ArrayBlockingQueue<Runnable>(10),
806                                     new NoOpREHandler());
807              shouldThrow();
# Line 777 | Line 813 | public class ThreadPoolExecutorTest exte
813       */
814      public void testConstructor12() {
815          try {
816 <            new ThreadPoolExecutor(1, -1,
781 <                                   LONG_DELAY_MS, MILLISECONDS,
816 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
817                                     new ArrayBlockingQueue<Runnable>(10),
818                                     new NoOpREHandler());
819              shouldThrow();
# Line 790 | Line 825 | public class ThreadPoolExecutorTest exte
825       */
826      public void testConstructor13() {
827          try {
828 <            new ThreadPoolExecutor(1, 0,
794 <                                   LONG_DELAY_MS, MILLISECONDS,
828 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
829                                     new ArrayBlockingQueue<Runnable>(10),
830                                     new NoOpREHandler());
831              shouldThrow();
# Line 803 | Line 837 | public class ThreadPoolExecutorTest exte
837       */
838      public void testConstructor14() {
839          try {
840 <            new ThreadPoolExecutor(1, 2,
807 <                                   -1L, MILLISECONDS,
840 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
841                                     new ArrayBlockingQueue<Runnable>(10),
842                                     new NoOpREHandler());
843              shouldThrow();
# Line 816 | Line 849 | public class ThreadPoolExecutorTest exte
849       */
850      public void testConstructor15() {
851          try {
852 <            new ThreadPoolExecutor(2, 1,
820 <                                   LONG_DELAY_MS, MILLISECONDS,
852 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
853                                     new ArrayBlockingQueue<Runnable>(10),
854                                     new NoOpREHandler());
855              shouldThrow();
# Line 829 | Line 861 | public class ThreadPoolExecutorTest exte
861       */
862      public void testConstructorNullPointerException4() {
863          try {
864 <            new ThreadPoolExecutor(1, 2,
833 <                                   LONG_DELAY_MS, MILLISECONDS,
864 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
865                                     (BlockingQueue) null,
866                                     new NoOpREHandler());
867              shouldThrow();
# Line 842 | Line 873 | public class ThreadPoolExecutorTest exte
873       */
874      public void testConstructorNullPointerException5() {
875          try {
876 <            new ThreadPoolExecutor(1, 2,
846 <                                   LONG_DELAY_MS, MILLISECONDS,
876 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
877                                     new ArrayBlockingQueue<Runnable>(10),
878                                     (RejectedExecutionHandler) null);
879              shouldThrow();
# Line 855 | Line 885 | public class ThreadPoolExecutorTest exte
885       */
886      public void testConstructor16() {
887          try {
888 <            new ThreadPoolExecutor(-1, 1,
859 <                                   LONG_DELAY_MS, MILLISECONDS,
888 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
889                                     new ArrayBlockingQueue<Runnable>(10),
890                                     new SimpleThreadFactory(),
891                                     new NoOpREHandler());
# Line 869 | Line 898 | public class ThreadPoolExecutorTest exte
898       */
899      public void testConstructor17() {
900          try {
901 <            new ThreadPoolExecutor(1, -1,
873 <                                   LONG_DELAY_MS, MILLISECONDS,
901 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
902                                     new ArrayBlockingQueue<Runnable>(10),
903                                     new SimpleThreadFactory(),
904                                     new NoOpREHandler());
# Line 883 | Line 911 | public class ThreadPoolExecutorTest exte
911       */
912      public void testConstructor18() {
913          try {
914 <            new ThreadPoolExecutor(1, 0,
887 <                                   LONG_DELAY_MS, MILLISECONDS,
914 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
915                                     new ArrayBlockingQueue<Runnable>(10),
916                                     new SimpleThreadFactory(),
917                                     new NoOpREHandler());
# Line 897 | Line 924 | public class ThreadPoolExecutorTest exte
924       */
925      public void testConstructor19() {
926          try {
927 <            new ThreadPoolExecutor(1, 2,
901 <                                   -1L, MILLISECONDS,
927 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
928                                     new ArrayBlockingQueue<Runnable>(10),
929                                     new SimpleThreadFactory(),
930                                     new NoOpREHandler());
# Line 911 | Line 937 | public class ThreadPoolExecutorTest exte
937       */
938      public void testConstructor20() {
939          try {
940 <            new ThreadPoolExecutor(2, 1,
915 <                                   LONG_DELAY_MS, MILLISECONDS,
940 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
941                                     new ArrayBlockingQueue<Runnable>(10),
942                                     new SimpleThreadFactory(),
943                                     new NoOpREHandler());
# Line 925 | Line 950 | public class ThreadPoolExecutorTest exte
950       */
951      public void testConstructorNullPointerException6() {
952          try {
953 <            new ThreadPoolExecutor(1, 2,
929 <                                   LONG_DELAY_MS, MILLISECONDS,
953 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
954                                     (BlockingQueue) null,
955                                     new SimpleThreadFactory(),
956                                     new NoOpREHandler());
# Line 939 | Line 963 | public class ThreadPoolExecutorTest exte
963       */
964      public void testConstructorNullPointerException7() {
965          try {
966 <            new ThreadPoolExecutor(1, 2,
943 <                                   LONG_DELAY_MS, MILLISECONDS,
966 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
967                                     new ArrayBlockingQueue<Runnable>(10),
968                                     new SimpleThreadFactory(),
969                                     (RejectedExecutionHandler) null);
# Line 953 | Line 976 | public class ThreadPoolExecutorTest exte
976       */
977      public void testConstructorNullPointerException8() {
978          try {
979 <            new ThreadPoolExecutor(1, 2,
957 <                                   LONG_DELAY_MS, MILLISECONDS,
979 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
980                                     new ArrayBlockingQueue<Runnable>(10),
981                                     (ThreadFactory) null,
982                                     new NoOpREHandler());
# Line 1242 | Line 1264 | public class ThreadPoolExecutorTest exte
1264       */
1265      public void testExecuteNull() {
1266          ThreadPoolExecutor p =
1267 <            new ThreadPoolExecutor(1, 2,
1246 <                                   LONG_DELAY_MS, MILLISECONDS,
1267 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1268                                     new ArrayBlockingQueue<Runnable>(10));
1269          try {
1270              p.execute(null);
# Line 1345 | Line 1366 | public class ThreadPoolExecutorTest exte
1366          ExtendedTPE p = new ExtendedTPE();
1367          try {
1368              final CountDownLatch done = new CountDownLatch(1);
1369 <            final CheckedRunnable task = new CheckedRunnable() {
1369 >            p.execute(new CheckedRunnable() {
1370                  public void realRun() {
1371                      done.countDown();
1372 <                }};
1352 <            p.execute(task);
1372 >                }});
1373              await(p.afterCalled);
1374              assertEquals(0, done.getCount());
1375              assertTrue(p.afterCalled());
# Line 1977 | Line 1997 | public class ThreadPoolExecutorTest exte
1997              // enough time to run all tasks
1998              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
1999          } finally {
2000 <            p.shutdown();
2000 >            joinPool(p);
2001          }
2002      }
2003  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines