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.58 by jsr166, Fri May 15 18:21:19 2015 UTC vs.
Revision 1.65 by jsr166, Sun Oct 16 20:44:18 2016 UTC

# Line 48 | Line 48 | public class LinkedBlockingDequeTest ext
48  
49      /**
50       * Returns a new deque of given size containing consecutive
51 <     * Integers 0 ... n.
51 >     * Integers 0 ... n - 1.
52       */
53      private LinkedBlockingDeque<Integer> populatedDeque(int n) {
54          LinkedBlockingDeque<Integer> q =
# Line 59 | Line 59 | public class LinkedBlockingDequeTest ext
59          assertFalse(q.isEmpty());
60          assertEquals(0, q.remainingCapacity());
61          assertEquals(n, q.size());
62 +        assertEquals((Integer) 0, q.peekFirst());
63 +        assertEquals((Integer) (n - 1), q.peekLast());
64          return q;
65      }
66  
# Line 82 | Line 84 | public class LinkedBlockingDequeTest ext
84      public void testSize() {
85          LinkedBlockingDeque q = populatedDeque(SIZE);
86          for (int i = 0; i < SIZE; ++i) {
87 <            assertEquals(SIZE-i, q.size());
87 >            assertEquals(SIZE - i, q.size());
88              q.removeFirst();
89          }
90          for (int i = 0; i < SIZE; ++i) {
# Line 147 | Line 149 | public class LinkedBlockingDequeTest ext
149       */
150      public void testPollLast() {
151          LinkedBlockingDeque q = populatedDeque(SIZE);
152 <        for (int i = SIZE-1; i >= 0; --i) {
152 >        for (int i = SIZE - 1; i >= 0; --i) {
153              assertEquals(i, q.pollLast());
154          }
155          assertNull(q.pollLast());
# Line 186 | Line 188 | public class LinkedBlockingDequeTest ext
188       */
189      public void testPeekLast() {
190          LinkedBlockingDeque q = populatedDeque(SIZE);
191 <        for (int i = SIZE-1; i >= 0; --i) {
191 >        for (int i = SIZE - 1; i >= 0; --i) {
192              assertEquals(i, q.peekLast());
193              assertEquals(i, q.pollLast());
194              assertTrue(q.peekLast() == null ||
# Line 216 | Line 218 | public class LinkedBlockingDequeTest ext
218       */
219      public void testLastElement() {
220          LinkedBlockingDeque q = populatedDeque(SIZE);
221 <        for (int i = SIZE-1; i >= 0; --i) {
221 >        for (int i = SIZE - 1; i >= 0; --i) {
222              assertEquals(i, q.getLast());
223              assertEquals(i, q.pollLast());
224          }
# Line 281 | Line 283 | public class LinkedBlockingDequeTest ext
283          }
284          for (int i = 0; i < SIZE; i += 2) {
285              assertTrue(q.removeFirstOccurrence(new Integer(i)));
286 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
286 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
287          }
288          assertTrue(q.isEmpty());
289      }
# Line 296 | Line 298 | public class LinkedBlockingDequeTest ext
298          }
299          for (int i = 0; i < SIZE; i += 2) {
300              assertTrue(q.removeLastOccurrence(new Integer(i)));
301 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
301 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
302          }
303          assertTrue(q.isEmpty());
304      }
# Line 367 | Line 369 | public class LinkedBlockingDequeTest ext
369       */
370      public void testConstructor5() {
371          Integer[] ints = new Integer[SIZE];
372 <        for (int i = 0; i < SIZE-1; ++i)
372 >        for (int i = 0; i < SIZE - 1; ++i)
373              ints[i] = i;
374          Collection<Integer> elements = Arrays.asList(ints);
375          try {
# Line 414 | Line 416 | public class LinkedBlockingDequeTest ext
416              assertEquals(i, q.remove());
417          }
418          for (int i = 0; i < SIZE; ++i) {
419 <            assertEquals(SIZE-i, q.remainingCapacity());
419 >            assertEquals(SIZE - i, q.remainingCapacity());
420              assertEquals(SIZE, q.size() + q.remainingCapacity());
421              assertTrue(q.add(i));
422          }
# Line 513 | Line 515 | public class LinkedBlockingDequeTest ext
515      public void testAddAll3() {
516          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
517          Integer[] ints = new Integer[SIZE];
518 <        for (int i = 0; i < SIZE-1; ++i)
518 >        for (int i = 0; i < SIZE - 1; ++i)
519              ints[i] = new Integer(i);
520          Collection<Integer> elements = Arrays.asList(ints);
521          try {
# Line 751 | Line 753 | public class LinkedBlockingDequeTest ext
753          final CountDownLatch aboutToWait = new CountDownLatch(1);
754          Thread t = newStartedThread(new CheckedRunnable() {
755              public void realRun() throws InterruptedException {
756 +                long startTime = System.nanoTime();
757                  for (int i = 0; i < SIZE; ++i) {
755                    long t0 = System.nanoTime();
758                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
757                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
759                  }
759                long t0 = System.nanoTime();
760                  aboutToWait.countDown();
761                  try {
762 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
762 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
763                      shouldThrow();
764                  } catch (InterruptedException success) {
765 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
765 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
766                  }
767              }});
768  
769          aboutToWait.await();
770 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
770 >        waitForThreadToEnterWaitState(t);
771          t.interrupt();
772 <        awaitTermination(t, MEDIUM_DELAY_MS);
772 >        awaitTermination(t);
773          checkEmpty(q);
774      }
775  
# Line 1050 | Line 1050 | public class LinkedBlockingDequeTest ext
1050       * returning timeout status
1051       */
1052      public void testInterruptedTimedPollFirst() throws InterruptedException {
1053 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1054          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1055          Thread t = newStartedThread(new CheckedRunnable() {
1056              public void realRun() throws InterruptedException {
1057 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1057 >                long startTime = System.nanoTime();
1058                  for (int i = 0; i < SIZE; ++i) {
1059                      assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1060                  }
1061  
1062                  Thread.currentThread().interrupt();
1063                  try {
1064 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1064 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1065                      shouldThrow();
1066                  } catch (InterruptedException success) {}
1067                  assertFalse(Thread.interrupted());
# Line 1071 | Line 1072 | public class LinkedBlockingDequeTest ext
1072                      shouldThrow();
1073                  } catch (InterruptedException success) {}
1074                  assertFalse(Thread.interrupted());
1075 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1076              }});
1077  
1078          await(pleaseInterrupt);
# Line 1246 | Line 1248 | public class LinkedBlockingDequeTest ext
1248      public void testTakeLast() throws InterruptedException {
1249          LinkedBlockingDeque q = populatedDeque(SIZE);
1250          for (int i = 0; i < SIZE; ++i) {
1251 <            assertEquals(SIZE-i-1, q.takeLast());
1251 >            assertEquals(SIZE - i - 1, q.takeLast());
1252          }
1253      }
1254  
# Line 1259 | Line 1261 | public class LinkedBlockingDequeTest ext
1261          Thread t = newStartedThread(new CheckedRunnable() {
1262              public void realRun() throws InterruptedException {
1263                  for (int i = 0; i < SIZE; ++i) {
1264 <                    assertEquals(SIZE-i-1, q.takeLast());
1264 >                    assertEquals(SIZE - i - 1, q.takeLast());
1265                  }
1266  
1267                  Thread.currentThread().interrupt();
# Line 1289 | Line 1291 | public class LinkedBlockingDequeTest ext
1291      public void testTimedPollLast0() throws InterruptedException {
1292          LinkedBlockingDeque q = populatedDeque(SIZE);
1293          for (int i = 0; i < SIZE; ++i) {
1294 <            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1294 >            assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
1295          }
1296          assertNull(q.pollLast(0, MILLISECONDS));
1297      }
# Line 1301 | Line 1303 | public class LinkedBlockingDequeTest ext
1303          LinkedBlockingDeque q = populatedDeque(SIZE);
1304          for (int i = 0; i < SIZE; ++i) {
1305              long startTime = System.nanoTime();
1306 <            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1306 >            assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1307              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1308          }
1309          long startTime = System.nanoTime();
# Line 1315 | Line 1317 | public class LinkedBlockingDequeTest ext
1317       * returning timeout status
1318       */
1319      public void testInterruptedTimedPollLast() throws InterruptedException {
1320 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1321          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1322          Thread t = newStartedThread(new CheckedRunnable() {
1323              public void realRun() throws InterruptedException {
1324 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1324 >                long startTime = System.nanoTime();
1325                  for (int i = 0; i < SIZE; ++i) {
1326 <                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1326 >                    assertEquals(SIZE - i - 1,
1327 >                                 q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1328                  }
1329  
1330                  Thread.currentThread().interrupt();
# Line 1336 | Line 1340 | public class LinkedBlockingDequeTest ext
1340                      shouldThrow();
1341                  } catch (InterruptedException success) {}
1342                  assertFalse(Thread.interrupted());
1343 +
1344 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1345              }});
1346  
1347          await(pleaseInterrupt);
1348          assertThreadStaysAlive(t);
1349          t.interrupt();
1350          awaitTermination(t);
1351 +        checkEmpty(q);
1352      }
1353  
1354      /**
# Line 1374 | Line 1381 | public class LinkedBlockingDequeTest ext
1381                      shouldThrow();
1382                  } catch (InterruptedException success) {}
1383                  assertFalse(Thread.interrupted());
1384 +
1385 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1386              }});
1387  
1388          barrier.await();
# Line 1458 | Line 1467 | public class LinkedBlockingDequeTest ext
1467                  assertTrue(changed);
1468  
1469              assertTrue(q.containsAll(p));
1470 <            assertEquals(SIZE-i, q.size());
1470 >            assertEquals(SIZE - i, q.size());
1471              p.remove();
1472          }
1473      }
# Line 1471 | Line 1480 | public class LinkedBlockingDequeTest ext
1480              LinkedBlockingDeque q = populatedDeque(SIZE);
1481              LinkedBlockingDeque p = populatedDeque(i);
1482              assertTrue(q.removeAll(p));
1483 <            assertEquals(SIZE-i, q.size());
1483 >            assertEquals(SIZE - i, q.size());
1484              for (int j = 0; j < i; ++j) {
1485                  Integer x = (Integer)(p.remove());
1486                  assertFalse(q.contains(x));
# Line 1670 | Line 1679 | public class LinkedBlockingDequeTest ext
1679          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1680          q.add(one);
1681          q.add(two);
1673        ExecutorService executor = Executors.newFixedThreadPool(2);
1682          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1683 <        executor.execute(new CheckedRunnable() {
1684 <            public void realRun() throws InterruptedException {
1685 <                assertFalse(q.offer(three));
1686 <                threadsStarted.await();
1687 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1688 <                assertEquals(0, q.remainingCapacity());
1689 <            }});
1690 <
1691 <        executor.execute(new CheckedRunnable() {
1692 <            public void realRun() throws InterruptedException {
1693 <                threadsStarted.await();
1694 <                assertSame(one, q.take());
1695 <            }});
1696 <
1697 <        joinPool(executor);
1683 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1684 >        try (PoolCleaner cleaner = cleaner(executor)) {
1685 >            executor.execute(new CheckedRunnable() {
1686 >                public void realRun() throws InterruptedException {
1687 >                    assertFalse(q.offer(three));
1688 >                    threadsStarted.await();
1689 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1690 >                    assertEquals(0, q.remainingCapacity());
1691 >                }});
1692 >
1693 >            executor.execute(new CheckedRunnable() {
1694 >                public void realRun() throws InterruptedException {
1695 >                    threadsStarted.await();
1696 >                    assertSame(one, q.take());
1697 >                }});
1698 >        }
1699      }
1700  
1701      /**
# Line 1695 | Line 1704 | public class LinkedBlockingDequeTest ext
1704      public void testPollInExecutor() {
1705          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1706          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1707 <        ExecutorService executor = Executors.newFixedThreadPool(2);
1708 <        executor.execute(new CheckedRunnable() {
1709 <            public void realRun() throws InterruptedException {
1710 <                assertNull(q.poll());
1711 <                threadsStarted.await();
1712 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1713 <                checkEmpty(q);
1714 <            }});
1715 <
1716 <        executor.execute(new CheckedRunnable() {
1717 <            public void realRun() throws InterruptedException {
1718 <                threadsStarted.await();
1719 <                q.put(one);
1720 <            }});
1721 <
1722 <        joinPool(executor);
1707 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1708 >        try (PoolCleaner cleaner = cleaner(executor)) {
1709 >            executor.execute(new CheckedRunnable() {
1710 >                public void realRun() throws InterruptedException {
1711 >                    assertNull(q.poll());
1712 >                    threadsStarted.await();
1713 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1714 >                    checkEmpty(q);
1715 >                }});
1716 >
1717 >            executor.execute(new CheckedRunnable() {
1718 >                public void realRun() throws InterruptedException {
1719 >                    threadsStarted.await();
1720 >                    q.put(one);
1721 >                }});
1722 >        }
1723      }
1724  
1725      /**
# Line 1762 | Line 1771 | public class LinkedBlockingDequeTest ext
1771          final LinkedBlockingDeque q = populatedDeque(SIZE);
1772          Thread t = new Thread(new CheckedRunnable() {
1773              public void realRun() throws InterruptedException {
1774 <                q.put(new Integer(SIZE+1));
1774 >                q.put(new Integer(SIZE + 1));
1775              }});
1776  
1777          t.start();
# Line 1787 | Line 1796 | public class LinkedBlockingDequeTest ext
1796              q.drainTo(l, i);
1797              int k = (i < SIZE) ? i : SIZE;
1798              assertEquals(k, l.size());
1799 <            assertEquals(SIZE-k, q.size());
1799 >            assertEquals(SIZE - k, q.size());
1800              for (int j = 0; j < k; ++j)
1801                  assertEquals(l.get(j), new Integer(j));
1802              do {} while (q.poll() != null);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines