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.60 by jsr166, Sun May 24 01:42:14 2015 UTC vs.
Revision 1.79 by jsr166, Sun May 14 04:02:06 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 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 653 | Line 670 | public class LinkedBlockingDequeTest ext
670                      q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
671                      shouldThrow();
672                  } catch (InterruptedException success) {}
673 +                assertFalse(Thread.interrupted());
674              }});
675  
676          await(pleaseInterrupt);
677 <        assertThreadStaysAlive(t);
677 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
678          t.interrupt();
679          awaitTermination(t);
680      }
# Line 679 | Line 697 | public class LinkedBlockingDequeTest ext
697          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
698          Thread t = newStartedThread(new CheckedRunnable() {
699              public void realRun() throws InterruptedException {
700 <                for (int i = 0; i < SIZE; ++i) {
683 <                    assertEquals(i, q.take());
684 <                }
700 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
701  
702                  Thread.currentThread().interrupt();
703                  try {
# Line 699 | Line 715 | public class LinkedBlockingDequeTest ext
715              }});
716  
717          await(pleaseInterrupt);
718 <        assertThreadStaysAlive(t);
718 >        assertThreadBlocks(t, Thread.State.WAITING);
719          t.interrupt();
720          awaitTermination(t);
721      }
# Line 748 | Line 764 | public class LinkedBlockingDequeTest ext
764       */
765      public void testInterruptedTimedPoll() throws InterruptedException {
766          final BlockingQueue<Integer> q = populatedDeque(SIZE);
767 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
767 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
768          Thread t = newStartedThread(new CheckedRunnable() {
769              public void realRun() throws InterruptedException {
770 <                for (int i = 0; i < SIZE; ++i) {
771 <                    long t0 = System.nanoTime();
770 >                long startTime = System.nanoTime();
771 >                for (int i = 0; i < SIZE; i++)
772                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
773 <                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
774 <                }
775 <                long t0 = System.nanoTime();
776 <                aboutToWait.countDown();
773 >
774 >                Thread.currentThread().interrupt();
775 >                try {
776 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
777 >                    shouldThrow();
778 >                } catch (InterruptedException success) {}
779 >                assertFalse(Thread.interrupted());
780 >
781 >                pleaseInterrupt.countDown();
782                  try {
783 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
783 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
784                      shouldThrow();
785 <                } catch (InterruptedException success) {
786 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
787 <                }
785 >                } catch (InterruptedException success) {}
786 >                assertFalse(Thread.interrupted());
787 >
788 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
789              }});
790  
791 <        aboutToWait.await();
792 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
791 >        await(pleaseInterrupt);
792 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
793          t.interrupt();
794 <        awaitTermination(t, MEDIUM_DELAY_MS);
794 >        awaitTermination(t);
795          checkEmpty(q);
796      }
797  
# Line 826 | Line 848 | public class LinkedBlockingDequeTest ext
848              }});
849  
850          await(pleaseInterrupt);
851 <        assertThreadStaysAlive(t);
851 >        assertThreadBlocks(t, Thread.State.WAITING);
852          t.interrupt();
853          awaitTermination(t);
854          assertEquals(SIZE, q.size());
# Line 861 | Line 883 | public class LinkedBlockingDequeTest ext
883          assertEquals(capacity - 1, q.take());
884  
885          await(pleaseInterrupt);
886 <        assertThreadStaysAlive(t);
886 >        assertThreadBlocks(t, Thread.State.WAITING);
887          t.interrupt();
888          awaitTermination(t);
889          assertEquals(0, q.remainingCapacity());
# Line 885 | Line 907 | public class LinkedBlockingDequeTest ext
907                      q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
908                      shouldThrow();
909                  } catch (InterruptedException success) {}
910 +                assertFalse(Thread.interrupted());
911              }});
912  
913          await(pleaseInterrupt);
914 <        assertThreadStaysAlive(t);
914 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
915          t.interrupt();
916          awaitTermination(t);
917      }
# Line 920 | Line 943 | public class LinkedBlockingDequeTest ext
943              }});
944  
945          await(threadStarted);
946 <        assertThreadStaysAlive(t);
946 >        assertThreadBlocks(t, Thread.State.WAITING);
947          t.interrupt();
948          awaitTermination(t);
949      }
# Line 961 | Line 984 | public class LinkedBlockingDequeTest ext
984              }});
985  
986          await(threadStarted);
987 <        assertThreadStaysAlive(t);
987 >        assertThreadBlocks(t, Thread.State.WAITING);
988          t.interrupt();
989          awaitTermination(t);
990      }
# Line 993 | Line 1016 | public class LinkedBlockingDequeTest ext
1016          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1017          Thread t = newStartedThread(new CheckedRunnable() {
1018              public void realRun() throws InterruptedException {
1019 <                for (int i = 0; i < SIZE; ++i) {
997 <                    assertEquals(i, q.takeFirst());
998 <                }
1019 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst());
1020  
1021                  Thread.currentThread().interrupt();
1022                  try {
# Line 1013 | Line 1034 | public class LinkedBlockingDequeTest ext
1034              }});
1035  
1036          await(pleaseInterrupt);
1037 <        assertThreadStaysAlive(t);
1037 >        assertThreadBlocks(t, Thread.State.WAITING);
1038          t.interrupt();
1039          awaitTermination(t);
1040      }
# Line 1050 | Line 1071 | public class LinkedBlockingDequeTest ext
1071       * returning timeout status
1072       */
1073      public void testInterruptedTimedPollFirst() throws InterruptedException {
1074 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1075          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1076          Thread t = newStartedThread(new CheckedRunnable() {
1077              public void realRun() throws InterruptedException {
1078 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1079 <                for (int i = 0; i < SIZE; ++i) {
1078 >                long startTime = System.nanoTime();
1079 >                for (int i = 0; i < SIZE; i++)
1080                      assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1059                }
1081  
1082                  Thread.currentThread().interrupt();
1083                  try {
1084 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1084 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1085                      shouldThrow();
1086                  } catch (InterruptedException success) {}
1087                  assertFalse(Thread.interrupted());
# Line 1071 | Line 1092 | public class LinkedBlockingDequeTest ext
1092                      shouldThrow();
1093                  } catch (InterruptedException success) {}
1094                  assertFalse(Thread.interrupted());
1095 +
1096 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1097              }});
1098  
1099          await(pleaseInterrupt);
1100 <        assertThreadStaysAlive(t);
1100 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1101          t.interrupt();
1102          awaitTermination(t);
1103      }
# Line 1107 | Line 1130 | public class LinkedBlockingDequeTest ext
1130                      q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1131                      shouldThrow();
1132                  } catch (InterruptedException success) {}
1133 +                assertFalse(Thread.interrupted());
1134                  assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1135              }});
1136  
# Line 1115 | Line 1139 | public class LinkedBlockingDequeTest ext
1139          assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1140          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1141          barrier.await();
1142 <        assertThreadStaysAlive(t);
1142 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1143          t.interrupt();
1144          awaitTermination(t);
1145      }
# Line 1173 | Line 1197 | public class LinkedBlockingDequeTest ext
1197              }});
1198  
1199          await(pleaseInterrupt);
1200 <        assertThreadStaysAlive(t);
1200 >        assertThreadBlocks(t, Thread.State.WAITING);
1201          t.interrupt();
1202          awaitTermination(t);
1203          assertEquals(SIZE, q.size());
# Line 1208 | Line 1232 | public class LinkedBlockingDequeTest ext
1232          assertEquals(0, q.take());
1233  
1234          await(pleaseInterrupt);
1235 <        assertThreadStaysAlive(t);
1235 >        assertThreadBlocks(t, Thread.State.WAITING);
1236          t.interrupt();
1237          awaitTermination(t);
1238          assertEquals(0, q.remainingCapacity());
# Line 1235 | Line 1259 | public class LinkedBlockingDequeTest ext
1259              }});
1260  
1261          await(pleaseInterrupt);
1262 <        assertThreadStaysAlive(t);
1262 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1263          t.interrupt();
1264          awaitTermination(t);
1265      }
# Line 1258 | Line 1282 | public class LinkedBlockingDequeTest ext
1282          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1283          Thread t = newStartedThread(new CheckedRunnable() {
1284              public void realRun() throws InterruptedException {
1285 <                for (int i = 0; i < SIZE; ++i) {
1285 >                for (int i = 0; i < SIZE; i++)
1286                      assertEquals(SIZE - i - 1, q.takeLast());
1263                }
1287  
1288                  Thread.currentThread().interrupt();
1289                  try {
# Line 1278 | Line 1301 | public class LinkedBlockingDequeTest ext
1301              }});
1302  
1303          await(pleaseInterrupt);
1304 <        assertThreadStaysAlive(t);
1304 >        assertThreadBlocks(t, Thread.State.WAITING);
1305          t.interrupt();
1306          awaitTermination(t);
1307      }
# Line 1315 | Line 1338 | public class LinkedBlockingDequeTest ext
1338       * returning timeout status
1339       */
1340      public void testInterruptedTimedPollLast() throws InterruptedException {
1341 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1342          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1343          Thread t = newStartedThread(new CheckedRunnable() {
1344              public void realRun() throws InterruptedException {
1345 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1346 <                for (int i = 0; i < SIZE; ++i) {
1345 >                long startTime = System.nanoTime();
1346 >                for (int i = 0; i < SIZE; i++)
1347                      assertEquals(SIZE - i - 1,
1348                                   q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1325                }
1349  
1350                  Thread.currentThread().interrupt();
1351                  try {
# Line 1337 | Line 1360 | public class LinkedBlockingDequeTest ext
1360                      shouldThrow();
1361                  } catch (InterruptedException success) {}
1362                  assertFalse(Thread.interrupted());
1363 +
1364 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1365              }});
1366  
1367          await(pleaseInterrupt);
1368 <        assertThreadStaysAlive(t);
1368 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1369          t.interrupt();
1370          awaitTermination(t);
1371 +        checkEmpty(q);
1372      }
1373  
1374      /**
# Line 1375 | Line 1401 | public class LinkedBlockingDequeTest ext
1401                      shouldThrow();
1402                  } catch (InterruptedException success) {}
1403                  assertFalse(Thread.interrupted());
1404 +
1405 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1406              }});
1407  
1408          barrier.await();
# Line 1383 | Line 1411 | public class LinkedBlockingDequeTest ext
1411          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1412  
1413          barrier.await();
1414 <        assertThreadStaysAlive(t);
1414 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1415          t.interrupt();
1416          awaitTermination(t);
1417      }
# Line 1671 | Line 1699 | public class LinkedBlockingDequeTest ext
1699          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1700          q.add(one);
1701          q.add(two);
1674        ExecutorService executor = Executors.newFixedThreadPool(2);
1702          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1703 <        executor.execute(new CheckedRunnable() {
1704 <            public void realRun() throws InterruptedException {
1705 <                assertFalse(q.offer(three));
1706 <                threadsStarted.await();
1707 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1708 <                assertEquals(0, q.remainingCapacity());
1709 <            }});
1710 <
1711 <        executor.execute(new CheckedRunnable() {
1712 <            public void realRun() throws InterruptedException {
1713 <                threadsStarted.await();
1714 <                assertSame(one, q.take());
1715 <            }});
1716 <
1717 <        joinPool(executor);
1703 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1704 >        try (PoolCleaner cleaner = cleaner(executor)) {
1705 >            executor.execute(new CheckedRunnable() {
1706 >                public void realRun() throws InterruptedException {
1707 >                    assertFalse(q.offer(three));
1708 >                    threadsStarted.await();
1709 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1710 >                    assertEquals(0, q.remainingCapacity());
1711 >                }});
1712 >
1713 >            executor.execute(new CheckedRunnable() {
1714 >                public void realRun() throws InterruptedException {
1715 >                    threadsStarted.await();
1716 >                    assertSame(one, q.take());
1717 >                }});
1718 >        }
1719      }
1720  
1721      /**
# Line 1696 | Line 1724 | public class LinkedBlockingDequeTest ext
1724      public void testPollInExecutor() {
1725          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1726          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1727 <        ExecutorService executor = Executors.newFixedThreadPool(2);
1728 <        executor.execute(new CheckedRunnable() {
1729 <            public void realRun() throws InterruptedException {
1730 <                assertNull(q.poll());
1731 <                threadsStarted.await();
1732 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1733 <                checkEmpty(q);
1734 <            }});
1735 <
1736 <        executor.execute(new CheckedRunnable() {
1737 <            public void realRun() throws InterruptedException {
1738 <                threadsStarted.await();
1739 <                q.put(one);
1740 <            }});
1741 <
1742 <        joinPool(executor);
1727 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1728 >        try (PoolCleaner cleaner = cleaner(executor)) {
1729 >            executor.execute(new CheckedRunnable() {
1730 >                public void realRun() throws InterruptedException {
1731 >                    assertNull(q.poll());
1732 >                    threadsStarted.await();
1733 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1734 >                    checkEmpty(q);
1735 >                }});
1736 >
1737 >            executor.execute(new CheckedRunnable() {
1738 >                public void realRun() throws InterruptedException {
1739 >                    threadsStarted.await();
1740 >                    q.put(one);
1741 >                }});
1742 >        }
1743      }
1744  
1745      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines