ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
(Generate patch)

Comparing jsr166/src/test/tck/LinkedBlockingDequeTest.java (file contents):
Revision 1.38 by jsr166, Sat May 21 06:24:33 2011 UTC vs.
Revision 1.39 by jsr166, Fri May 27 20:07:24 2011 UTC

# Line 298 | Line 298 | public class LinkedBlockingDequeTest ext
298          assertSame(four, q.peekLast());
299      }
300  
301
301      /**
302       * A new deque has the indicated capacity, or Integer.MAX_VALUE if
303       * none given
# Line 456 | Line 455 | public class LinkedBlockingDequeTest ext
455          assertSame(four, q.peekFirst());
456      }
457  
459
458      /**
459       * pop removes next element, or throws NSEE if empty
460       */
# Line 471 | Line 469 | public class LinkedBlockingDequeTest ext
469          } catch (NoSuchElementException success) {}
470      }
471  
474
472      /**
473       * Offer succeeds if not full; fails if full
474       */
# Line 574 | Line 571 | public class LinkedBlockingDequeTest ext
571              assertEquals(ints[i], q.poll());
572      }
573  
577
574      /**
575       * put(null) throws NPE
576       */
# Line 604 | Line 600 | public class LinkedBlockingDequeTest ext
600       */
601      public void testBlockingPut() throws InterruptedException {
602          final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
603 <        Thread t = new Thread(new CheckedRunnable() {
603 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
604 >        Thread t = newStartedThread(new CheckedRunnable() {
605              public void realRun() throws InterruptedException {
606                  for (int i = 0; i < SIZE; ++i)
607                      q.put(i);
608                  assertEquals(SIZE, q.size());
609                  assertEquals(0, q.remainingCapacity());
610 +
611 +                Thread.currentThread().interrupt();
612                  try {
613                      q.put(99);
614                      shouldThrow();
615                  } catch (InterruptedException success) {}
616 +                assertFalse(Thread.interrupted());
617 +
618 +                pleaseInterrupt.countDown();
619 +                try {
620 +                    q.put(99);
621 +                    shouldThrow();
622 +                } catch (InterruptedException success) {}
623 +                assertFalse(Thread.interrupted());
624              }});
625  
626 <        t.start();
627 <        delay(SHORT_DELAY_MS);
626 >        await(pleaseInterrupt);
627 >        assertThreadStaysAlive(t);
628          t.interrupt();
629 <        t.join();
629 >        awaitTermination(t);
630          assertEquals(SIZE, q.size());
631          assertEquals(0, q.remainingCapacity());
632      }
633  
634      /**
635 <     * put blocks waiting for take when full
635 >     * put blocks interruptibly waiting for take when full
636       */
637      public void testPutWithTake() throws InterruptedException {
638          final int capacity = 2;
639          final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
640 <        Thread t = new Thread(new CheckedRunnable() {
640 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
641 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
642 >        Thread t = newStartedThread(new CheckedRunnable() {
643              public void realRun() throws InterruptedException {
644 <                for (int i = 0; i < capacity + 1; i++)
644 >                for (int i = 0; i < capacity; i++)
645                      q.put(i);
646 +                pleaseTake.countDown();
647 +                q.put(86);
648 +
649 +                pleaseInterrupt.countDown();
650                  try {
651                      q.put(99);
652                      shouldThrow();
653                  } catch (InterruptedException success) {}
654 +                assertFalse(Thread.interrupted());
655              }});
656  
657 <        t.start();
644 <        delay(SHORT_DELAY_MS);
657 >        await(pleaseTake);
658          assertEquals(q.remainingCapacity(), 0);
659          assertEquals(0, q.take());
660 <        delay(SHORT_DELAY_MS);
660 >
661 >        await(pleaseInterrupt);
662 >        assertThreadStaysAlive(t);
663          t.interrupt();
664 <        t.join();
664 >        awaitTermination(t);
665          assertEquals(q.remainingCapacity(), 0);
666      }
667  
# Line 671 | Line 686 | public class LinkedBlockingDequeTest ext
686              }});
687  
688          await(pleaseInterrupt);
689 +        assertThreadStaysAlive(t);
690          t.interrupt();
691          awaitTermination(t);
692      }
# Line 686 | Line 702 | public class LinkedBlockingDequeTest ext
702      }
703  
704      /**
705 <     * Take removes existing elements until empty, then blocks interruptibly
705 >     * take removes existing elements until empty, then blocks interruptibly
706       */
707      public void testBlockingTake() throws InterruptedException {
708          final LinkedBlockingDeque q = populatedDeque(SIZE);
709 <        Thread t = new Thread(new CheckedRunnable() {
709 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
710 >        Thread t = newStartedThread(new CheckedRunnable() {
711              public void realRun() throws InterruptedException {
712                  for (int i = 0; i < SIZE; ++i) {
713                      assertEquals(i, q.take());
714                  }
715 +
716 +                Thread.currentThread().interrupt();
717                  try {
718                      q.take();
719                      shouldThrow();
720                  } catch (InterruptedException success) {}
721 +                assertFalse(Thread.interrupted());
722 +
723 +                pleaseInterrupt.countDown();
724 +                try {
725 +                    q.take();
726 +                    shouldThrow();
727 +                } catch (InterruptedException success) {}
728 +                assertFalse(Thread.interrupted());
729              }});
730  
731 <        t.start();
732 <        delay(SHORT_DELAY_MS);
731 >        await(pleaseInterrupt);
732 >        assertThreadStaysAlive(t);
733          t.interrupt();
734 <        t.join();
734 >        awaitTermination(t);
735      }
736  
710
737      /**
738       * poll succeeds unless empty
739       */
# Line 736 | Line 762 | public class LinkedBlockingDequeTest ext
762      public void testTimedPoll() throws InterruptedException {
763          LinkedBlockingDeque q = populatedDeque(SIZE);
764          for (int i = 0; i < SIZE; ++i) {
765 <            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
766 <        }
767 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
765 >            long startTime = System.nanoTime();
766 >            assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
767 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
768 >        }
769 >        long startTime = System.nanoTime();
770 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
771 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
772 >        checkEmpty(q);
773      }
774  
775      /**
# Line 801 | Line 832 | public class LinkedBlockingDequeTest ext
832       */
833      public void testBlockingPutFirst() throws InterruptedException {
834          final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
835 <        Thread t = new Thread(new CheckedRunnable() {
835 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
836 >        Thread t = newStartedThread(new CheckedRunnable() {
837              public void realRun() throws InterruptedException {
838                  for (int i = 0; i < SIZE; ++i)
839                      q.putFirst(i);
840                  assertEquals(SIZE, q.size());
841                  assertEquals(0, q.remainingCapacity());
842 +
843 +                Thread.currentThread().interrupt();
844 +                try {
845 +                    q.putFirst(99);
846 +                    shouldThrow();
847 +                } catch (InterruptedException success) {}
848 +                assertFalse(Thread.interrupted());
849 +
850 +                pleaseInterrupt.countDown();
851                  try {
852                      q.putFirst(99);
853                      shouldThrow();
854                  } catch (InterruptedException success) {}
855 +                assertFalse(Thread.interrupted());
856              }});
857  
858 <        t.start();
859 <        delay(SHORT_DELAY_MS);
858 >        await(pleaseInterrupt);
859 >        assertThreadStaysAlive(t);
860          t.interrupt();
861 <        t.join();
861 >        awaitTermination(t);
862          assertEquals(SIZE, q.size());
863          assertEquals(0, q.remainingCapacity());
864      }
865  
866      /**
867 <     * putFirst blocks waiting for take when full
867 >     * putFirst blocks interruptibly waiting for take when full
868       */
869      public void testPutFirstWithTake() throws InterruptedException {
870          final int capacity = 2;
871          final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
872 <        Thread t = new Thread(new CheckedRunnable() {
872 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
873 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
874 >        Thread t = newStartedThread(new CheckedRunnable() {
875              public void realRun() throws InterruptedException {
876 <                for (int i = 0; i < capacity + 1; i++)
876 >                for (int i = 0; i < capacity; i++)
877                      q.putFirst(i);
878 +                pleaseTake.countDown();
879 +                q.putFirst(86);
880 +
881 +                pleaseInterrupt.countDown();
882                  try {
883                      q.putFirst(99);
884                      shouldThrow();
885                  } catch (InterruptedException success) {}
886 +                assertFalse(Thread.interrupted());
887              }});
888  
889 <        t.start();
841 <        delay(SHORT_DELAY_MS);
889 >        await(pleaseTake);
890          assertEquals(q.remainingCapacity(), 0);
891          assertEquals(capacity - 1, q.take());
892 <        delay(SHORT_DELAY_MS);
892 >
893 >        await(pleaseInterrupt);
894 >        assertThreadStaysAlive(t);
895          t.interrupt();
896 <        t.join();
896 >        awaitTermination(t);
897          assertEquals(q.remainingCapacity(), 0);
898      }
899  
# Line 868 | Line 918 | public class LinkedBlockingDequeTest ext
918              }});
919  
920          await(pleaseInterrupt);
921 +        assertThreadStaysAlive(t);
922          t.interrupt();
923          awaitTermination(t);
924      }
# Line 883 | Line 934 | public class LinkedBlockingDequeTest ext
934      }
935  
936      /**
937 <     * takeFirst blocks interruptibly when empty
937 >     * takeFirst() blocks interruptibly when empty
938       */
939 <    public void testTakeFirstFromEmpty() throws InterruptedException {
940 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
941 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
942 <            public void realRun() throws InterruptedException {
943 <                q.takeFirst();
944 <            }};
939 >    public void testTakeFirstFromEmptyBlocksInterruptibly() {
940 >        final BlockingDeque q = new LinkedBlockingDeque();
941 >        final CountDownLatch threadStarted = new CountDownLatch(1);
942 >        Thread t = newStartedThread(new CheckedRunnable() {
943 >            public void realRun() {
944 >                threadStarted.countDown();
945 >                try {
946 >                    q.takeFirst();
947 >                    shouldThrow();
948 >                } catch (InterruptedException success) {}
949 >                assertFalse(Thread.interrupted());
950 >            }});
951  
952 <        t.start();
953 <        delay(SHORT_DELAY_MS);
952 >        await(threadStarted);
953 >        assertThreadStaysAlive(t);
954          t.interrupt();
955 <        t.join();
955 >        awaitTermination(t);
956      }
957  
958      /**
959 <     * TakeFirst removes existing elements until empty, then blocks interruptibly
959 >     * takeFirst() throws InterruptedException immediately if interrupted
960 >     * before waiting
961 >     */
962 >    public void testTakeFirstFromEmptyAfterInterrupt() {
963 >        final BlockingDeque q = new LinkedBlockingDeque();
964 >        Thread t = newStartedThread(new CheckedRunnable() {
965 >            public void realRun() {
966 >                Thread.currentThread().interrupt();
967 >                try {
968 >                    q.takeFirst();
969 >                    shouldThrow();
970 >                } catch (InterruptedException success) {}
971 >                assertFalse(Thread.interrupted());
972 >            }});
973 >
974 >        awaitTermination(t);
975 >    }
976 >
977 >    /**
978 >     * takeLast() blocks interruptibly when empty
979 >     */
980 >    public void testTakeLastFromEmptyBlocksInterruptibly() {
981 >        final BlockingDeque q = new LinkedBlockingDeque();
982 >        final CountDownLatch threadStarted = new CountDownLatch(1);
983 >        Thread t = newStartedThread(new CheckedRunnable() {
984 >            public void realRun() {
985 >                threadStarted.countDown();
986 >                try {
987 >                    q.takeLast();
988 >                    shouldThrow();
989 >                } catch (InterruptedException success) {}
990 >                assertFalse(Thread.interrupted());
991 >            }});
992 >
993 >        await(threadStarted);
994 >        assertThreadStaysAlive(t);
995 >        t.interrupt();
996 >        awaitTermination(t);
997 >    }
998 >
999 >    /**
1000 >     * takeLast() throws InterruptedException immediately if interrupted
1001 >     * before waiting
1002 >     */
1003 >    public void testTakeLastFromEmptyAfterInterrupt() {
1004 >        final BlockingDeque q = new LinkedBlockingDeque();
1005 >        Thread t = newStartedThread(new CheckedRunnable() {
1006 >            public void realRun() {
1007 >                Thread.currentThread().interrupt();
1008 >                try {
1009 >                    q.takeLast();
1010 >                    shouldThrow();
1011 >                } catch (InterruptedException success) {}
1012 >                assertFalse(Thread.interrupted());
1013 >            }});
1014 >
1015 >        awaitTermination(t);
1016 >    }
1017 >
1018 >    /**
1019 >     * takeFirst removes existing elements until empty, then blocks interruptibly
1020       */
1021      public void testBlockingTakeFirst() throws InterruptedException {
1022          final LinkedBlockingDeque q = populatedDeque(SIZE);
1023 <        Thread t = new Thread(new CheckedRunnable() {
1023 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1024 >        Thread t = newStartedThread(new CheckedRunnable() {
1025              public void realRun() throws InterruptedException {
1026 <                for (int i = 0; i < SIZE; ++i)
1026 >                for (int i = 0; i < SIZE; ++i) {
1027                      assertEquals(i, q.takeFirst());
1028 +                }
1029 +
1030 +                Thread.currentThread().interrupt();
1031                  try {
1032                      q.takeFirst();
1033                      shouldThrow();
1034                  } catch (InterruptedException success) {}
1035 +                assertFalse(Thread.interrupted());
1036 +
1037 +                pleaseInterrupt.countDown();
1038 +                try {
1039 +                    q.takeFirst();
1040 +                    shouldThrow();
1041 +                } catch (InterruptedException success) {}
1042 +                assertFalse(Thread.interrupted());
1043              }});
1044  
1045 <        t.start();
1046 <        delay(SHORT_DELAY_MS);
1045 >        await(pleaseInterrupt);
1046 >        assertThreadStaysAlive(t);
1047          t.interrupt();
1048 <        t.join();
1048 >        awaitTermination(t);
1049      }
1050  
922
1051      /**
1052       * timed pollFirst with zero timeout succeeds when non-empty, else times out
1053       */
# Line 937 | Line 1065 | public class LinkedBlockingDequeTest ext
1065      public void testTimedPollFirst() throws InterruptedException {
1066          LinkedBlockingDeque q = populatedDeque(SIZE);
1067          for (int i = 0; i < SIZE; ++i) {
1068 <            assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1069 <        }
1070 <        assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1068 >            long startTime = System.nanoTime();
1069 >            assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1070 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1071 >        }
1072 >        long startTime = System.nanoTime();
1073 >        assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1074 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1075 >        checkEmpty(q);
1076      }
1077  
1078      /**
# Line 947 | Line 1080 | public class LinkedBlockingDequeTest ext
1080       * returning timeout status
1081       */
1082      public void testInterruptedTimedPollFirst() throws InterruptedException {
1083 <        Thread t = new Thread(new CheckedRunnable() {
1083 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1084 >        Thread t = newStartedThread(new CheckedRunnable() {
1085              public void realRun() throws InterruptedException {
1086                  LinkedBlockingDeque q = populatedDeque(SIZE);
1087                  for (int i = 0; i < SIZE; ++i) {
1088 <                    assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1088 >                    assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1089                  }
1090 +
1091 +                Thread.currentThread().interrupt();
1092                  try {
1093                      q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1094                      shouldThrow();
1095                  } catch (InterruptedException success) {}
1096 +                assertFalse(Thread.interrupted());
1097 +
1098 +                pleaseInterrupt.countDown();
1099 +                try {
1100 +                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1101 +                    shouldThrow();
1102 +                } catch (InterruptedException success) {}
1103 +                assertFalse(Thread.interrupted());
1104              }});
1105  
1106 <        t.start();
1107 <        delay(SHORT_DELAY_MS);
1106 >        await(pleaseInterrupt);
1107 >        assertThreadStaysAlive(t);
1108          t.interrupt();
1109 <        t.join();
1109 >        awaitTermination(t);
1110      }
1111  
1112      /**
# Line 971 | Line 1115 | public class LinkedBlockingDequeTest ext
1115       */
1116      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
1117          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1118 <        Thread t = new Thread(new CheckedRunnable() {
1118 >        final CheckedBarrier barrier = new CheckedBarrier(2);
1119 >        Thread t = newStartedThread(new CheckedRunnable() {
1120              public void realRun() throws InterruptedException {
1121 <                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1121 >                long startTime = System.nanoTime();
1122 >                assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1123 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1124 >
1125 >                barrier.await();
1126 >
1127                  assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1128 +
1129 +                Thread.currentThread().interrupt();
1130                  try {
1131                      q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1132                      shouldThrow();
1133                  } catch (InterruptedException success) {}
1134 +
1135 +                barrier.await();
1136 +                try {
1137 +                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1138 +                    shouldThrow();
1139 +                } catch (InterruptedException success) {}
1140 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1141              }});
1142  
1143 <        t.start();
1144 <        delay(SMALL_DELAY_MS);
1145 <        assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
1143 >        barrier.await();
1144 >        long startTime = System.nanoTime();
1145 >        assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1146 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1147 >        barrier.await();
1148 >        assertThreadStaysAlive(t);
1149          t.interrupt();
1150 <        t.join();
1150 >        awaitTermination(t);
1151      }
1152  
1153      /**
# Line 1017 | Line 1179 | public class LinkedBlockingDequeTest ext
1179       */
1180      public void testBlockingPutLast() throws InterruptedException {
1181          final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1182 <        Thread t = new Thread(new CheckedRunnable() {
1182 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1183 >        Thread t = newStartedThread(new CheckedRunnable() {
1184              public void realRun() throws InterruptedException {
1185                  for (int i = 0; i < SIZE; ++i)
1186                      q.putLast(i);
1187                  assertEquals(SIZE, q.size());
1188                  assertEquals(0, q.remainingCapacity());
1189 +
1190 +                Thread.currentThread().interrupt();
1191 +                try {
1192 +                    q.putLast(99);
1193 +                    shouldThrow();
1194 +                } catch (InterruptedException success) {}
1195 +                assertFalse(Thread.interrupted());
1196 +
1197 +                pleaseInterrupt.countDown();
1198                  try {
1199                      q.putLast(99);
1200                      shouldThrow();
1201                  } catch (InterruptedException success) {}
1202 +                assertFalse(Thread.interrupted());
1203              }});
1204  
1205 <        t.start();
1206 <        delay(SHORT_DELAY_MS);
1205 >        await(pleaseInterrupt);
1206 >        assertThreadStaysAlive(t);
1207          t.interrupt();
1208 <        t.join();
1208 >        awaitTermination(t);
1209          assertEquals(SIZE, q.size());
1210          assertEquals(0, q.remainingCapacity());
1211      }
1212  
1213      /**
1214 <     * putLast blocks waiting for take when full
1214 >     * putLast blocks interruptibly waiting for take when full
1215       */
1216      public void testPutLastWithTake() throws InterruptedException {
1217          final int capacity = 2;
1218          final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1219 <        Thread t = new Thread(new CheckedRunnable() {
1219 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
1220 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1221 >        Thread t = newStartedThread(new CheckedRunnable() {
1222              public void realRun() throws InterruptedException {
1223 <                for (int i = 0; i < capacity + 1; i++)
1223 >                for (int i = 0; i < capacity; i++)
1224                      q.putLast(i);
1225 +                pleaseTake.countDown();
1226 +                q.putLast(86);
1227 +
1228 +                pleaseInterrupt.countDown();
1229                  try {
1230                      q.putLast(99);
1231                      shouldThrow();
1232                  } catch (InterruptedException success) {}
1233 +                assertFalse(Thread.interrupted());
1234              }});
1235  
1236 <        t.start();
1057 <        delay(SHORT_DELAY_MS);
1236 >        await(pleaseTake);
1237          assertEquals(q.remainingCapacity(), 0);
1238          assertEquals(0, q.take());
1239 <        delay(SHORT_DELAY_MS);
1239 >
1240 >        await(pleaseInterrupt);
1241 >        assertThreadStaysAlive(t);
1242          t.interrupt();
1243 <        t.join();
1243 >        awaitTermination(t);
1244          assertEquals(q.remainingCapacity(), 0);
1245      }
1246  
# Line 1084 | Line 1265 | public class LinkedBlockingDequeTest ext
1265              }});
1266  
1267          await(pleaseInterrupt);
1268 +        assertThreadStaysAlive(t);
1269          t.interrupt();
1270          awaitTermination(t);
1271      }
# Line 1099 | Line 1281 | public class LinkedBlockingDequeTest ext
1281      }
1282  
1283      /**
1284 <     * takeLast blocks interruptibly when empty
1103 <     */
1104 <    public void testTakeLastFromEmpty() throws InterruptedException {
1105 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1106 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1107 <            public void realRun() throws InterruptedException {
1108 <                q.takeLast();
1109 <            }};
1110 <
1111 <        t.start();
1112 <        delay(SHORT_DELAY_MS);
1113 <        t.interrupt();
1114 <        t.join();
1115 <    }
1116 <
1117 <    /**
1118 <     * TakeLast removes existing elements until empty, then blocks interruptibly
1284 >     * takeLast removes existing elements until empty, then blocks interruptibly
1285       */
1286      public void testBlockingTakeLast() throws InterruptedException {
1287          final LinkedBlockingDeque q = populatedDeque(SIZE);
1288 <        Thread t = new Thread(new CheckedRunnable() {
1288 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1289 >        Thread t = newStartedThread(new CheckedRunnable() {
1290              public void realRun() throws InterruptedException {
1291 <                for (int i = 0; i < SIZE; ++i)
1292 <                    assertEquals(SIZE - 1 - i, q.takeLast());
1291 >                for (int i = 0; i < SIZE; ++i) {
1292 >                    assertEquals(SIZE-i-1, q.takeLast());
1293 >                }
1294 >
1295 >                Thread.currentThread().interrupt();
1296                  try {
1297                      q.takeLast();
1298                      shouldThrow();
1299                  } catch (InterruptedException success) {}
1300 +                assertFalse(Thread.interrupted());
1301 +
1302 +                pleaseInterrupt.countDown();
1303 +                try {
1304 +                    q.takeLast();
1305 +                    shouldThrow();
1306 +                } catch (InterruptedException success) {}
1307 +                assertFalse(Thread.interrupted());
1308              }});
1309  
1310 <        t.start();
1311 <        delay(SHORT_DELAY_MS);
1310 >        await(pleaseInterrupt);
1311 >        assertThreadStaysAlive(t);
1312          t.interrupt();
1313 <        t.join();
1313 >        awaitTermination(t);
1314      }
1315  
1316      /**
# Line 1152 | Line 1330 | public class LinkedBlockingDequeTest ext
1330      public void testTimedPollLast() throws InterruptedException {
1331          LinkedBlockingDeque q = populatedDeque(SIZE);
1332          for (int i = 0; i < SIZE; ++i) {
1333 <            assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1334 <        }
1335 <        assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1333 >            long startTime = System.nanoTime();
1334 >            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1335 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1336 >        }
1337 >        long startTime = System.nanoTime();
1338 >        assertNull(q.pollLast(timeoutMillis(), MILLISECONDS));
1339 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1340 >        checkEmpty(q);
1341      }
1342  
1343      /**
# Line 1162 | Line 1345 | public class LinkedBlockingDequeTest ext
1345       * returning timeout status
1346       */
1347      public void testInterruptedTimedPollLast() throws InterruptedException {
1348 <        Thread t = new Thread(new CheckedRunnable() {
1348 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1349 >        Thread t = newStartedThread(new CheckedRunnable() {
1350              public void realRun() throws InterruptedException {
1351                  LinkedBlockingDeque q = populatedDeque(SIZE);
1352                  for (int i = 0; i < SIZE; ++i) {
1353 <                    assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1353 >                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1354                  }
1355 +
1356 +                Thread.currentThread().interrupt();
1357                  try {
1358 <                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1358 >                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1359                      shouldThrow();
1360                  } catch (InterruptedException success) {}
1361 +                assertFalse(Thread.interrupted());
1362 +
1363 +                pleaseInterrupt.countDown();
1364 +                try {
1365 +                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1366 +                    shouldThrow();
1367 +                } catch (InterruptedException success) {}
1368 +                assertFalse(Thread.interrupted());
1369              }});
1370  
1371 <        t.start();
1372 <        delay(SHORT_DELAY_MS);
1371 >        await(pleaseInterrupt);
1372 >        assertThreadStaysAlive(t);
1373          t.interrupt();
1374 <        t.join();
1374 >        awaitTermination(t);
1375      }
1376  
1377      /**
# Line 1186 | Line 1380 | public class LinkedBlockingDequeTest ext
1380       */
1381      public void testTimedPollWithOfferLast() throws InterruptedException {
1382          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1383 <        Thread t = new Thread(new CheckedRunnable() {
1383 >        final CheckedBarrier barrier = new CheckedBarrier(2);
1384 >        Thread t = newStartedThread(new CheckedRunnable() {
1385              public void realRun() throws InterruptedException {
1386 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1386 >                long startTime = System.nanoTime();
1387 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
1388 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1389 >
1390 >                barrier.await();
1391 >
1392                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1393 +
1394 +                Thread.currentThread().interrupt();
1395                  try {
1396                      q.poll(LONG_DELAY_MS, MILLISECONDS);
1397                      shouldThrow();
1398                  } catch (InterruptedException success) {}
1399 +                assertFalse(Thread.interrupted());
1400 +
1401 +                barrier.await();
1402 +                try {
1403 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1404 +                    shouldThrow();
1405 +                } catch (InterruptedException success) {}
1406 +                assertFalse(Thread.interrupted());
1407              }});
1408  
1409 <        t.start();
1410 <        delay(SMALL_DELAY_MS);
1411 <        assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1409 >        barrier.await();
1410 >        long startTime = System.nanoTime();
1411 >        assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS));
1412 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1413 >
1414 >        barrier.await();
1415 >        assertThreadStaysAlive(t);
1416          t.interrupt();
1417 <        t.join();
1417 >        awaitTermination(t);
1418      }
1419  
1206
1420      /**
1421       * element returns next element, or throws NSEE if empty
1422       */
# Line 1361 | Line 1574 | public class LinkedBlockingDequeTest ext
1574          } catch (ArrayStoreException success) {}
1575      }
1576  
1364
1577      /**
1578       * iterator iterates through all elements
1579       */
# Line 1392 | Line 1604 | public class LinkedBlockingDequeTest ext
1604          assertFalse(it.hasNext());
1605      }
1606  
1395
1607      /**
1608       * iterator ordering is FIFO
1609       */
# Line 1424 | Line 1635 | public class LinkedBlockingDequeTest ext
1635          assertEquals(0, q.size());
1636      }
1637  
1427
1638      /**
1639       * Descending iterator iterates through all elements
1640       */
# Line 1487 | Line 1697 | public class LinkedBlockingDequeTest ext
1697          }
1698      }
1699  
1490
1700      /**
1701       * toString contains toStrings of elements
1702       */
# Line 1495 | Line 1704 | public class LinkedBlockingDequeTest ext
1704          LinkedBlockingDeque q = populatedDeque(SIZE);
1705          String s = q.toString();
1706          for (int i = 0; i < SIZE; ++i) {
1707 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1707 >            assertTrue(s.contains(String.valueOf(i)));
1708          }
1709      }
1710  
1502
1711      /**
1712       * offer transfers elements across Executor tasks
1713       */
# Line 1508 | Line 1716 | public class LinkedBlockingDequeTest ext
1716          q.add(one);
1717          q.add(two);
1718          ExecutorService executor = Executors.newFixedThreadPool(2);
1719 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1720          executor.execute(new CheckedRunnable() {
1721              public void realRun() throws InterruptedException {
1722                  assertFalse(q.offer(three));
1723 <                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1723 >                threadsStarted.await();
1724 >                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1725                  assertEquals(0, q.remainingCapacity());
1726              }});
1727  
1728          executor.execute(new CheckedRunnable() {
1729              public void realRun() throws InterruptedException {
1730 <                delay(SMALL_DELAY_MS);
1730 >                threadsStarted.await();
1731                  assertSame(one, q.take());
1732              }});
1733  
# Line 1525 | Line 1735 | public class LinkedBlockingDequeTest ext
1735      }
1736  
1737      /**
1738 <     * poll retrieves elements across Executor threads
1738 >     * timed poll retrieves elements across Executor threads
1739       */
1740      public void testPollInExecutor() {
1741          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1742 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1743          ExecutorService executor = Executors.newFixedThreadPool(2);
1744          executor.execute(new CheckedRunnable() {
1745              public void realRun() throws InterruptedException {
1746                  assertNull(q.poll());
1747 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1748 <                assertTrue(q.isEmpty());
1747 >                threadsStarted.await();
1748 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1749 >                checkEmpty(q);
1750              }});
1751  
1752          executor.execute(new CheckedRunnable() {
1753              public void realRun() throws InterruptedException {
1754 <                delay(SMALL_DELAY_MS);
1754 >                threadsStarted.await();
1755                  q.put(one);
1756              }});
1757  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines