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.62 by jsr166, Tue Oct 6 00:03:55 2015 UTC vs.
Revision 1.84 by jsr166, Sun Aug 11 22:29:27 2019 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 >        if (randomBoolean()) 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 >        if (randomBoolean()) 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 646 | Line 663 | public class LinkedBlockingDequeTest ext
663                  q.put(new Object());
664                  q.put(new Object());
665                  long startTime = System.nanoTime();
666 +
667                  assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
668                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
669 +
670 +                Thread.currentThread().interrupt();
671 +                try {
672 +                    q.offer(new Object(), randomTimeout(), randomTimeUnit());
673 +                    shouldThrow();
674 +                } catch (InterruptedException success) {}
675 +                assertFalse(Thread.interrupted());
676 +
677                  pleaseInterrupt.countDown();
678                  try {
679 <                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
679 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
680                      shouldThrow();
681                  } catch (InterruptedException success) {}
682 +                assertFalse(Thread.interrupted());
683 +
684 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
685              }});
686  
687          await(pleaseInterrupt);
688 <        assertThreadStaysAlive(t);
688 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
689          t.interrupt();
690          awaitTermination(t);
691      }
# Line 679 | Line 708 | public class LinkedBlockingDequeTest ext
708          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
709          Thread t = newStartedThread(new CheckedRunnable() {
710              public void realRun() throws InterruptedException {
711 <                for (int i = 0; i < SIZE; ++i) {
683 <                    assertEquals(i, q.take());
684 <                }
711 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
712  
713                  Thread.currentThread().interrupt();
714                  try {
# Line 699 | Line 726 | public class LinkedBlockingDequeTest ext
726              }});
727  
728          await(pleaseInterrupt);
729 <        assertThreadStaysAlive(t);
729 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
730          t.interrupt();
731          awaitTermination(t);
732      }
# Line 748 | Line 775 | public class LinkedBlockingDequeTest ext
775       */
776      public void testInterruptedTimedPoll() throws InterruptedException {
777          final BlockingQueue<Integer> q = populatedDeque(SIZE);
778 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
778 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
779          Thread t = newStartedThread(new CheckedRunnable() {
780              public void realRun() throws InterruptedException {
781                  long startTime = System.nanoTime();
782 <                for (int i = 0; i < SIZE; ++i) {
782 >                for (int i = 0; i < SIZE; i++)
783                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
784 <                }
785 <                aboutToWait.countDown();
784 >
785 >                Thread.currentThread().interrupt();
786 >                try {
787 >                    q.poll(randomTimeout(), randomTimeUnit());
788 >                    shouldThrow();
789 >                } catch (InterruptedException success) {}
790 >                assertFalse(Thread.interrupted());
791 >
792 >                pleaseInterrupt.countDown();
793                  try {
794                      q.poll(LONG_DELAY_MS, MILLISECONDS);
795                      shouldThrow();
796 <                } catch (InterruptedException success) {
797 <                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
798 <                }
796 >                } catch (InterruptedException success) {}
797 >                assertFalse(Thread.interrupted());
798 >
799 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
800              }});
801  
802 <        aboutToWait.await();
803 <        waitForThreadToEnterWaitState(t, LONG_DELAY_MS);
802 >        await(pleaseInterrupt);
803 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
804          t.interrupt();
805          awaitTermination(t);
806          checkEmpty(q);
# Line 824 | Line 859 | public class LinkedBlockingDequeTest ext
859              }});
860  
861          await(pleaseInterrupt);
862 <        assertThreadStaysAlive(t);
862 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
863          t.interrupt();
864          awaitTermination(t);
865          assertEquals(SIZE, q.size());
# Line 859 | Line 894 | public class LinkedBlockingDequeTest ext
894          assertEquals(capacity - 1, q.take());
895  
896          await(pleaseInterrupt);
897 <        assertThreadStaysAlive(t);
897 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
898          t.interrupt();
899          awaitTermination(t);
900          assertEquals(0, q.remainingCapacity());
# Line 868 | Line 903 | public class LinkedBlockingDequeTest ext
903      /**
904       * timed offerFirst times out if full and elements not taken
905       */
906 <    public void testTimedOfferFirst() throws InterruptedException {
906 >    public void testTimedOfferFirst() {
907          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
908          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
909          Thread t = newStartedThread(new CheckedRunnable() {
# Line 876 | Line 911 | public class LinkedBlockingDequeTest ext
911                  q.putFirst(new Object());
912                  q.putFirst(new Object());
913                  long startTime = System.nanoTime();
914 +
915                  assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
916                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
917 +
918 +                Thread.currentThread().interrupt();
919 +                try {
920 +                    q.offerFirst(new Object(), randomTimeout(), randomTimeUnit());
921 +                    shouldThrow();
922 +                } catch (InterruptedException success) {}
923 +                assertFalse(Thread.interrupted());
924 +
925                  pleaseInterrupt.countDown();
926                  try {
927 <                    q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
927 >                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
928                      shouldThrow();
929                  } catch (InterruptedException success) {}
930 +                assertFalse(Thread.interrupted());
931 +
932 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
933              }});
934  
935          await(pleaseInterrupt);
936 <        assertThreadStaysAlive(t);
936 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
937          t.interrupt();
938          awaitTermination(t);
939      }
# Line 918 | Line 965 | public class LinkedBlockingDequeTest ext
965              }});
966  
967          await(threadStarted);
968 <        assertThreadStaysAlive(t);
968 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
969          t.interrupt();
970          awaitTermination(t);
971      }
# Line 959 | Line 1006 | public class LinkedBlockingDequeTest ext
1006              }});
1007  
1008          await(threadStarted);
1009 <        assertThreadStaysAlive(t);
1009 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1010          t.interrupt();
1011          awaitTermination(t);
1012      }
# Line 991 | Line 1038 | public class LinkedBlockingDequeTest ext
1038          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1039          Thread t = newStartedThread(new CheckedRunnable() {
1040              public void realRun() throws InterruptedException {
1041 <                for (int i = 0; i < SIZE; ++i) {
995 <                    assertEquals(i, q.takeFirst());
996 <                }
1041 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst());
1042  
1043                  Thread.currentThread().interrupt();
1044                  try {
# Line 1011 | Line 1056 | public class LinkedBlockingDequeTest ext
1056              }});
1057  
1058          await(pleaseInterrupt);
1059 <        assertThreadStaysAlive(t);
1059 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1060          t.interrupt();
1061          awaitTermination(t);
1062      }
# Line 1053 | Line 1098 | public class LinkedBlockingDequeTest ext
1098          Thread t = newStartedThread(new CheckedRunnable() {
1099              public void realRun() throws InterruptedException {
1100                  long startTime = System.nanoTime();
1101 <                for (int i = 0; i < SIZE; ++i) {
1101 >                for (int i = 0; i < SIZE; i++)
1102                      assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1058                }
1103  
1104                  Thread.currentThread().interrupt();
1105                  try {
1106 <                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1106 >                    q.pollFirst(randomTimeout(), randomTimeUnit());
1107                      shouldThrow();
1108                  } catch (InterruptedException success) {}
1109                  assertFalse(Thread.interrupted());
# Line 1070 | Line 1114 | public class LinkedBlockingDequeTest ext
1114                      shouldThrow();
1115                  } catch (InterruptedException success) {}
1116                  assertFalse(Thread.interrupted());
1117 +
1118                  assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1119              }});
1120  
1121          await(pleaseInterrupt);
1122 <        assertThreadStaysAlive(t);
1122 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1123          t.interrupt();
1124          awaitTermination(t);
1125      }
# Line 1098 | Line 1143 | public class LinkedBlockingDequeTest ext
1143  
1144                  Thread.currentThread().interrupt();
1145                  try {
1146 <                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1146 >                    q.pollFirst(randomTimeout(), randomTimeUnit());
1147                      shouldThrow();
1148                  } catch (InterruptedException success) {}
1149  
# Line 1107 | Line 1152 | public class LinkedBlockingDequeTest ext
1152                      q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1153                      shouldThrow();
1154                  } catch (InterruptedException success) {}
1155 +                assertFalse(Thread.interrupted());
1156 +
1157                  assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1158              }});
1159  
# Line 1115 | Line 1162 | public class LinkedBlockingDequeTest ext
1162          assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1163          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1164          barrier.await();
1165 <        assertThreadStaysAlive(t);
1165 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1166          t.interrupt();
1167          awaitTermination(t);
1168      }
# Line 1173 | Line 1220 | public class LinkedBlockingDequeTest ext
1220              }});
1221  
1222          await(pleaseInterrupt);
1223 <        assertThreadStaysAlive(t);
1223 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1224          t.interrupt();
1225          awaitTermination(t);
1226          assertEquals(SIZE, q.size());
# Line 1195 | Line 1242 | public class LinkedBlockingDequeTest ext
1242                  pleaseTake.countDown();
1243                  q.putLast(86);
1244  
1245 +                Thread.currentThread().interrupt();
1246 +                try {
1247 +                    q.putLast(99);
1248 +                    shouldThrow();
1249 +                } catch (InterruptedException success) {}
1250 +                assertFalse(Thread.interrupted());
1251 +
1252                  pleaseInterrupt.countDown();
1253                  try {
1254                      q.putLast(99);
# Line 1208 | Line 1262 | public class LinkedBlockingDequeTest ext
1262          assertEquals(0, q.take());
1263  
1264          await(pleaseInterrupt);
1265 <        assertThreadStaysAlive(t);
1265 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1266          t.interrupt();
1267          awaitTermination(t);
1268          assertEquals(0, q.remainingCapacity());
# Line 1217 | Line 1271 | public class LinkedBlockingDequeTest ext
1271      /**
1272       * timed offerLast times out if full and elements not taken
1273       */
1274 <    public void testTimedOfferLast() throws InterruptedException {
1274 >    public void testTimedOfferLast() {
1275          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1276          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1277          Thread t = newStartedThread(new CheckedRunnable() {
# Line 1225 | Line 1279 | public class LinkedBlockingDequeTest ext
1279                  q.putLast(new Object());
1280                  q.putLast(new Object());
1281                  long startTime = System.nanoTime();
1282 +
1283                  assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1284                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1285 +
1286 +                Thread.currentThread().interrupt();
1287 +                try {
1288 +                    q.offerLast(new Object(), randomTimeout(), randomTimeUnit());
1289 +                    shouldThrow();
1290 +                } catch (InterruptedException success) {}
1291 +
1292                  pleaseInterrupt.countDown();
1293                  try {
1294 <                    q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1294 >                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1295                      shouldThrow();
1296                  } catch (InterruptedException success) {}
1297 +
1298 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1299              }});
1300  
1301          await(pleaseInterrupt);
1302 <        assertThreadStaysAlive(t);
1302 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1303          t.interrupt();
1304          awaitTermination(t);
1305      }
# Line 1258 | Line 1322 | public class LinkedBlockingDequeTest ext
1322          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1323          Thread t = newStartedThread(new CheckedRunnable() {
1324              public void realRun() throws InterruptedException {
1325 <                for (int i = 0; i < SIZE; ++i) {
1325 >                for (int i = 0; i < SIZE; i++)
1326                      assertEquals(SIZE - i - 1, q.takeLast());
1263                }
1327  
1328                  Thread.currentThread().interrupt();
1329                  try {
# Line 1278 | Line 1341 | public class LinkedBlockingDequeTest ext
1341              }});
1342  
1343          await(pleaseInterrupt);
1344 <        assertThreadStaysAlive(t);
1344 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1345          t.interrupt();
1346          awaitTermination(t);
1347      }
# Line 1320 | Line 1383 | public class LinkedBlockingDequeTest ext
1383          Thread t = newStartedThread(new CheckedRunnable() {
1384              public void realRun() throws InterruptedException {
1385                  long startTime = System.nanoTime();
1386 <                for (int i = 0; i < SIZE; ++i) {
1386 >                for (int i = 0; i < SIZE; i++)
1387                      assertEquals(SIZE - i - 1,
1388                                   q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1326                }
1389  
1390                  Thread.currentThread().interrupt();
1391                  try {
1392 <                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1392 >                    q.pollLast(randomTimeout(), randomTimeUnit());
1393                      shouldThrow();
1394                  } catch (InterruptedException success) {}
1395                  assertFalse(Thread.interrupted());
# Line 1343 | Line 1405 | public class LinkedBlockingDequeTest ext
1405              }});
1406  
1407          await(pleaseInterrupt);
1408 <        assertThreadStaysAlive(t);
1408 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1409          t.interrupt();
1410          awaitTermination(t);
1411          checkEmpty(q);
# Line 1368 | Line 1430 | public class LinkedBlockingDequeTest ext
1430  
1431                  Thread.currentThread().interrupt();
1432                  try {
1433 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1433 >                    q.poll(randomTimeout(), randomTimeUnit());
1434                      shouldThrow();
1435                  } catch (InterruptedException success) {}
1436                  assertFalse(Thread.interrupted());
# Line 1379 | Line 1441 | public class LinkedBlockingDequeTest ext
1441                      shouldThrow();
1442                  } catch (InterruptedException success) {}
1443                  assertFalse(Thread.interrupted());
1444 +
1445 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1446              }});
1447  
1448          barrier.await();
# Line 1387 | Line 1451 | public class LinkedBlockingDequeTest ext
1451          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1452  
1453          barrier.await();
1454 <        assertThreadStaysAlive(t);
1454 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1455          t.interrupt();
1456          awaitTermination(t);
1457      }
# Line 1489 | Line 1553 | public class LinkedBlockingDequeTest ext
1553       */
1554      public void testToArray() throws InterruptedException {
1555          LinkedBlockingDeque q = populatedDeque(SIZE);
1556 <        Object[] o = q.toArray();
1557 <        for (int i = 0; i < o.length; i++)
1558 <            assertSame(o[i], q.poll());
1556 >        Object[] a = q.toArray();
1557 >        assertSame(Object[].class, a.getClass());
1558 >        for (Object o : a)
1559 >            assertSame(o, q.poll());
1560 >        assertTrue(q.isEmpty());
1561      }
1562  
1563      /**
# Line 1502 | Line 1568 | public class LinkedBlockingDequeTest ext
1568          Integer[] ints = new Integer[SIZE];
1569          Integer[] array = q.toArray(ints);
1570          assertSame(ints, array);
1571 <        for (int i = 0; i < ints.length; i++)
1572 <            assertSame(ints[i], q.remove());
1571 >        for (Integer o : ints)
1572 >            assertSame(o, q.remove());
1573 >        assertTrue(q.isEmpty());
1574      }
1575  
1576      /**
# Line 1719 | Line 1786 | public class LinkedBlockingDequeTest ext
1786      }
1787  
1788      /**
1789 <     * A deserialized serialized deque has same elements in same order
1789 >     * A deserialized/reserialized deque has same elements in same order
1790       */
1791      public void testSerialization() throws Exception {
1792          Queue x = populatedDeque(SIZE);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines