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.64 by jsr166, Sat Aug 6 17:02:49 2016 UTC vs.
Revision 1.82 by jsr166, Fri Aug 4 03:30:21 2017 UTC

# Line 41 | Line 41 | public class LinkedBlockingDequeTest ext
41      }
42  
43      public static Test suite() {
44 +        class Implementation implements CollectionImplementation {
45 +            public Class<?> klazz() { return LinkedBlockingDeque.class; }
46 +            public Collection emptyCollection() { return new LinkedBlockingDeque(); }
47 +            public Object makeElement(int i) { return i; }
48 +            public boolean isConcurrent() { return true; }
49 +            public boolean permitsNulls() { return false; }
50 +        }
51          return newTestSuite(LinkedBlockingDequeTest.class,
52                              new Unbounded().testSuite(),
53 <                            new Bounded().testSuite());
53 >                            new Bounded().testSuite(),
54 >                            CollectionTest.testSuite(new Implementation()));
55      }
56  
57      /**
58       * Returns a new deque of given size containing consecutive
59 <     * Integers 0 ... n.
59 >     * Integers 0 ... n - 1.
60       */
61 <    private LinkedBlockingDeque<Integer> populatedDeque(int n) {
61 >    private static LinkedBlockingDeque<Integer> populatedDeque(int n) {
62          LinkedBlockingDeque<Integer> q =
63              new LinkedBlockingDeque<Integer>(n);
64          assertTrue(q.isEmpty());
# Line 59 | Line 67 | public class LinkedBlockingDequeTest ext
67          assertFalse(q.isEmpty());
68          assertEquals(0, q.remainingCapacity());
69          assertEquals(n, q.size());
70 +        assertEquals((Integer) 0, q.peekFirst());
71 +        assertEquals((Integer) (n - 1), q.peekLast());
72          return q;
73      }
74  
# Line 432 | Line 442 | public class LinkedBlockingDequeTest ext
442      }
443  
444      /**
445 <     * push succeeds if not full; throws ISE if full
445 >     * push succeeds if not full; throws IllegalStateException if full
446       */
447      public void testPush() {
448          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
# Line 482 | Line 492 | public class LinkedBlockingDequeTest ext
492      }
493  
494      /**
495 <     * add succeeds if not full; throws ISE if full
495 >     * add succeeds if not full; throws IllegalStateException if full
496       */
497      public void testAdd() {
498          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
# Line 496 | Line 506 | public class LinkedBlockingDequeTest ext
506      }
507  
508      /**
509 <     * addAll(this) throws IAE
509 >     * addAll(this) throws IllegalArgumentException
510       */
511      public void testAddAllSelf() {
512          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 594 | Line 604 | public class LinkedBlockingDequeTest ext
604              }});
605  
606          await(pleaseInterrupt);
607 <        assertThreadStaysAlive(t);
607 >        assertThreadBlocks(t, Thread.State.WAITING);
608          t.interrupt();
609          awaitTermination(t);
610          assertEquals(SIZE, q.size());
# Line 616 | Line 626 | public class LinkedBlockingDequeTest ext
626                  pleaseTake.countDown();
627                  q.put(86);
628  
629 +                Thread.currentThread().interrupt();
630 +                try {
631 +                    q.put(99);
632 +                    shouldThrow();
633 +                } catch (InterruptedException success) {}
634 +                assertFalse(Thread.interrupted());
635 +
636                  pleaseInterrupt.countDown();
637                  try {
638                      q.put(99);
# Line 629 | Line 646 | public class LinkedBlockingDequeTest ext
646          assertEquals(0, q.take());
647  
648          await(pleaseInterrupt);
649 <        assertThreadStaysAlive(t);
649 >        assertThreadBlocks(t, Thread.State.WAITING);
650          t.interrupt();
651          awaitTermination(t);
652          assertEquals(0, q.remainingCapacity());
# Line 638 | Line 655 | public class LinkedBlockingDequeTest ext
655      /**
656       * timed offer times out if full and elements not taken
657       */
658 <    public void testTimedOffer() throws InterruptedException {
658 >    public void testTimedOffer() {
659          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
660          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
661          Thread t = newStartedThread(new CheckedRunnable() {
# Line 648 | Line 665 | public class LinkedBlockingDequeTest ext
665                  long startTime = System.nanoTime();
666                  assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
667                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
668 +
669 +                Thread.currentThread().interrupt();
670 +                try {
671 +                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
672 +                    shouldThrow();
673 +                } catch (InterruptedException success) {}
674 +                assertFalse(Thread.interrupted());
675 +
676                  pleaseInterrupt.countDown();
677                  try {
678                      q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
679                      shouldThrow();
680                  } catch (InterruptedException success) {}
681 +                assertFalse(Thread.interrupted());
682              }});
683  
684          await(pleaseInterrupt);
685 <        assertThreadStaysAlive(t);
685 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
686          t.interrupt();
687          awaitTermination(t);
688      }
# Line 679 | Line 705 | public class LinkedBlockingDequeTest ext
705          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
706          Thread t = newStartedThread(new CheckedRunnable() {
707              public void realRun() throws InterruptedException {
708 <                for (int i = 0; i < SIZE; ++i) {
683 <                    assertEquals(i, q.take());
684 <                }
708 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
709  
710                  Thread.currentThread().interrupt();
711                  try {
# Line 699 | Line 723 | public class LinkedBlockingDequeTest ext
723              }});
724  
725          await(pleaseInterrupt);
726 <        assertThreadStaysAlive(t);
726 >        assertThreadBlocks(t, Thread.State.WAITING);
727          t.interrupt();
728          awaitTermination(t);
729      }
# Line 748 | Line 772 | public class LinkedBlockingDequeTest ext
772       */
773      public void testInterruptedTimedPoll() throws InterruptedException {
774          final BlockingQueue<Integer> q = populatedDeque(SIZE);
775 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
775 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
776          Thread t = newStartedThread(new CheckedRunnable() {
777              public void realRun() throws InterruptedException {
778                  long startTime = System.nanoTime();
779 <                for (int i = 0; i < SIZE; ++i) {
779 >                for (int i = 0; i < SIZE; i++)
780                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
781 <                }
782 <                aboutToWait.countDown();
781 >
782 >                Thread.currentThread().interrupt();
783 >                try {
784 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
785 >                    shouldThrow();
786 >                } catch (InterruptedException success) {}
787 >                assertFalse(Thread.interrupted());
788 >
789 >                pleaseInterrupt.countDown();
790                  try {
791                      q.poll(LONG_DELAY_MS, MILLISECONDS);
792                      shouldThrow();
793 <                } catch (InterruptedException success) {
794 <                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
795 <                }
793 >                } catch (InterruptedException success) {}
794 >                assertFalse(Thread.interrupted());
795 >
796 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
797              }});
798  
799 <        aboutToWait.await();
800 <        waitForThreadToEnterWaitState(t);
799 >        await(pleaseInterrupt);
800 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
801          t.interrupt();
802          awaitTermination(t);
803          checkEmpty(q);
# Line 824 | Line 856 | public class LinkedBlockingDequeTest ext
856              }});
857  
858          await(pleaseInterrupt);
859 <        assertThreadStaysAlive(t);
859 >        assertThreadBlocks(t, Thread.State.WAITING);
860          t.interrupt();
861          awaitTermination(t);
862          assertEquals(SIZE, q.size());
# Line 859 | Line 891 | public class LinkedBlockingDequeTest ext
891          assertEquals(capacity - 1, q.take());
892  
893          await(pleaseInterrupt);
894 <        assertThreadStaysAlive(t);
894 >        assertThreadBlocks(t, Thread.State.WAITING);
895          t.interrupt();
896          awaitTermination(t);
897          assertEquals(0, q.remainingCapacity());
# Line 868 | Line 900 | public class LinkedBlockingDequeTest ext
900      /**
901       * timed offerFirst times out if full and elements not taken
902       */
903 <    public void testTimedOfferFirst() throws InterruptedException {
903 >    public void testTimedOfferFirst() {
904          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
905          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
906          Thread t = newStartedThread(new CheckedRunnable() {
# Line 878 | Line 910 | public class LinkedBlockingDequeTest ext
910                  long startTime = System.nanoTime();
911                  assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
912                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
913 +
914 +                Thread.currentThread().interrupt();
915 +                try {
916 +                    q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
917 +                    shouldThrow();
918 +                } catch (InterruptedException success) {}
919 +                assertFalse(Thread.interrupted());
920 +
921                  pleaseInterrupt.countDown();
922                  try {
923                      q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
924                      shouldThrow();
925                  } catch (InterruptedException success) {}
926 +                assertFalse(Thread.interrupted());
927              }});
928  
929          await(pleaseInterrupt);
930 <        assertThreadStaysAlive(t);
930 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
931          t.interrupt();
932          awaitTermination(t);
933      }
# Line 918 | Line 959 | public class LinkedBlockingDequeTest ext
959              }});
960  
961          await(threadStarted);
962 <        assertThreadStaysAlive(t);
962 >        assertThreadBlocks(t, Thread.State.WAITING);
963          t.interrupt();
964          awaitTermination(t);
965      }
# Line 959 | Line 1000 | public class LinkedBlockingDequeTest ext
1000              }});
1001  
1002          await(threadStarted);
1003 <        assertThreadStaysAlive(t);
1003 >        assertThreadBlocks(t, Thread.State.WAITING);
1004          t.interrupt();
1005          awaitTermination(t);
1006      }
# Line 991 | Line 1032 | public class LinkedBlockingDequeTest ext
1032          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1033          Thread t = newStartedThread(new CheckedRunnable() {
1034              public void realRun() throws InterruptedException {
1035 <                for (int i = 0; i < SIZE; ++i) {
995 <                    assertEquals(i, q.takeFirst());
996 <                }
1035 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst());
1036  
1037                  Thread.currentThread().interrupt();
1038                  try {
# Line 1011 | Line 1050 | public class LinkedBlockingDequeTest ext
1050              }});
1051  
1052          await(pleaseInterrupt);
1053 <        assertThreadStaysAlive(t);
1053 >        assertThreadBlocks(t, Thread.State.WAITING);
1054          t.interrupt();
1055          awaitTermination(t);
1056      }
# Line 1053 | Line 1092 | public class LinkedBlockingDequeTest ext
1092          Thread t = newStartedThread(new CheckedRunnable() {
1093              public void realRun() throws InterruptedException {
1094                  long startTime = System.nanoTime();
1095 <                for (int i = 0; i < SIZE; ++i) {
1095 >                for (int i = 0; i < SIZE; i++)
1096                      assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1058                }
1097  
1098                  Thread.currentThread().interrupt();
1099                  try {
# Line 1070 | Line 1108 | public class LinkedBlockingDequeTest ext
1108                      shouldThrow();
1109                  } catch (InterruptedException success) {}
1110                  assertFalse(Thread.interrupted());
1111 +
1112                  assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1113              }});
1114  
1115          await(pleaseInterrupt);
1116 <        assertThreadStaysAlive(t);
1116 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1117          t.interrupt();
1118          awaitTermination(t);
1119      }
# Line 1107 | Line 1146 | public class LinkedBlockingDequeTest ext
1146                      q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1147                      shouldThrow();
1148                  } catch (InterruptedException success) {}
1149 +                assertFalse(Thread.interrupted());
1150                  assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1151              }});
1152  
# Line 1115 | Line 1155 | public class LinkedBlockingDequeTest ext
1155          assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1156          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1157          barrier.await();
1158 <        assertThreadStaysAlive(t);
1158 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1159          t.interrupt();
1160          awaitTermination(t);
1161      }
# Line 1173 | Line 1213 | public class LinkedBlockingDequeTest ext
1213              }});
1214  
1215          await(pleaseInterrupt);
1216 <        assertThreadStaysAlive(t);
1216 >        assertThreadBlocks(t, Thread.State.WAITING);
1217          t.interrupt();
1218          awaitTermination(t);
1219          assertEquals(SIZE, q.size());
# Line 1195 | Line 1235 | public class LinkedBlockingDequeTest ext
1235                  pleaseTake.countDown();
1236                  q.putLast(86);
1237  
1238 +                Thread.currentThread().interrupt();
1239 +                try {
1240 +                    q.putLast(99);
1241 +                    shouldThrow();
1242 +                } catch (InterruptedException success) {}
1243 +                assertFalse(Thread.interrupted());
1244 +
1245                  pleaseInterrupt.countDown();
1246                  try {
1247                      q.putLast(99);
# Line 1208 | Line 1255 | public class LinkedBlockingDequeTest ext
1255          assertEquals(0, q.take());
1256  
1257          await(pleaseInterrupt);
1258 <        assertThreadStaysAlive(t);
1258 >        assertThreadBlocks(t, Thread.State.WAITING);
1259          t.interrupt();
1260          awaitTermination(t);
1261          assertEquals(0, q.remainingCapacity());
# Line 1217 | Line 1264 | public class LinkedBlockingDequeTest ext
1264      /**
1265       * timed offerLast times out if full and elements not taken
1266       */
1267 <    public void testTimedOfferLast() throws InterruptedException {
1267 >    public void testTimedOfferLast() {
1268          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1269          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1270          Thread t = newStartedThread(new CheckedRunnable() {
# Line 1227 | Line 1274 | public class LinkedBlockingDequeTest ext
1274                  long startTime = System.nanoTime();
1275                  assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1276                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1277 +
1278 +                Thread.currentThread().interrupt();
1279 +                try {
1280 +                    q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1281 +                    shouldThrow();
1282 +                } catch (InterruptedException success) {}
1283 +
1284                  pleaseInterrupt.countDown();
1285                  try {
1286                      q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
# Line 1235 | Line 1289 | public class LinkedBlockingDequeTest ext
1289              }});
1290  
1291          await(pleaseInterrupt);
1292 <        assertThreadStaysAlive(t);
1292 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1293          t.interrupt();
1294          awaitTermination(t);
1295      }
# Line 1258 | Line 1312 | public class LinkedBlockingDequeTest ext
1312          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1313          Thread t = newStartedThread(new CheckedRunnable() {
1314              public void realRun() throws InterruptedException {
1315 <                for (int i = 0; i < SIZE; ++i) {
1315 >                for (int i = 0; i < SIZE; i++)
1316                      assertEquals(SIZE - i - 1, q.takeLast());
1263                }
1317  
1318                  Thread.currentThread().interrupt();
1319                  try {
# Line 1278 | Line 1331 | public class LinkedBlockingDequeTest ext
1331              }});
1332  
1333          await(pleaseInterrupt);
1334 <        assertThreadStaysAlive(t);
1334 >        assertThreadBlocks(t, Thread.State.WAITING);
1335          t.interrupt();
1336          awaitTermination(t);
1337      }
# Line 1320 | Line 1373 | public class LinkedBlockingDequeTest ext
1373          Thread t = newStartedThread(new CheckedRunnable() {
1374              public void realRun() throws InterruptedException {
1375                  long startTime = System.nanoTime();
1376 <                for (int i = 0; i < SIZE; ++i) {
1376 >                for (int i = 0; i < SIZE; i++)
1377                      assertEquals(SIZE - i - 1,
1378                                   q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1326                }
1379  
1380                  Thread.currentThread().interrupt();
1381                  try {
# Line 1343 | Line 1395 | public class LinkedBlockingDequeTest ext
1395              }});
1396  
1397          await(pleaseInterrupt);
1398 <        assertThreadStaysAlive(t);
1398 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1399          t.interrupt();
1400          awaitTermination(t);
1401          checkEmpty(q);
# Line 1389 | Line 1441 | public class LinkedBlockingDequeTest ext
1441          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1442  
1443          barrier.await();
1444 <        assertThreadStaysAlive(t);
1444 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1445          t.interrupt();
1446          awaitTermination(t);
1447      }
# Line 1721 | Line 1773 | public class LinkedBlockingDequeTest ext
1773      }
1774  
1775      /**
1776 <     * A deserialized serialized deque has same elements in same order
1776 >     * A deserialized/reserialized deque has same elements in same order
1777       */
1778      public void testSerialization() throws Exception {
1779          Queue x = populatedDeque(SIZE);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines