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.49 by jsr166, Thu May 30 03:28:55 2013 UTC vs.
Revision 1.65 by jsr166, Sun Oct 16 20:44:18 2016 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import junit.framework.*;
8 < import java.util.Arrays;
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 >
9   import java.util.ArrayList;
10 + import java.util.Arrays;
11   import java.util.Collection;
12 + import java.util.Deque;
13   import java.util.Iterator;
14   import java.util.NoSuchElementException;
15   import java.util.Queue;
# Line 17 | Line 19 | import java.util.concurrent.CountDownLat
19   import java.util.concurrent.Executors;
20   import java.util.concurrent.ExecutorService;
21   import java.util.concurrent.LinkedBlockingDeque;
22 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
22 >
23 > import junit.framework.Test;
24  
25   public class LinkedBlockingDequeTest extends JSR166TestCase {
26  
# Line 34 | Line 37 | public class LinkedBlockingDequeTest ext
37      }
38  
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run(suite());
40 >        main(suite(), args);
41      }
42  
43      public static Test suite() {
# Line 45 | 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 56 | 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 79 | 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 144 | 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 183 | 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 213 | 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 273 | Line 278 | public class LinkedBlockingDequeTest ext
278       */
279      public void testRemoveFirstOccurrence() {
280          LinkedBlockingDeque q = populatedDeque(SIZE);
281 <        for (int i = 1; i < SIZE; i+=2) {
281 >        for (int i = 1; i < SIZE; i += 2) {
282              assertTrue(q.removeFirstOccurrence(new Integer(i)));
283          }
284 <        for (int i = 0; i < SIZE; i+=2) {
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 288 | Line 293 | public class LinkedBlockingDequeTest ext
293       */
294      public void testRemoveLastOccurrence() {
295          LinkedBlockingDeque q = populatedDeque(SIZE);
296 <        for (int i = 1; i < SIZE; i+=2) {
296 >        for (int i = 1; i < SIZE; i += 2) {
297              assertTrue(q.removeLastOccurrence(new Integer(i)));
298          }
299 <        for (int i = 0; i < SIZE; i+=2) {
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 364 | 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 404 | Line 409 | public class LinkedBlockingDequeTest ext
409       * remainingCapacity decreases on add, increases on remove
410       */
411      public void testRemainingCapacity() {
412 <        LinkedBlockingDeque q = populatedDeque(SIZE);
412 >        BlockingQueue q = populatedDeque(SIZE);
413          for (int i = 0; i < SIZE; ++i) {
414              assertEquals(i, q.remainingCapacity());
415 <            assertEquals(SIZE-i, q.size());
416 <            q.remove();
415 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
416 >            assertEquals(i, q.remove());
417          }
418          for (int i = 0; i < SIZE; ++i) {
419 <            assertEquals(SIZE-i, q.remainingCapacity());
420 <            assertEquals(i, q.size());
421 <            q.add(new Integer(i));
419 >            assertEquals(SIZE - i, q.remainingCapacity());
420 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
421 >            assertTrue(q.add(i));
422          }
423      }
424  
# Line 421 | Line 426 | public class LinkedBlockingDequeTest ext
426       * push(null) throws NPE
427       */
428      public void testPushNull() {
429 +        LinkedBlockingDeque q = new LinkedBlockingDeque(1);
430          try {
425            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
431              q.push(null);
432              shouldThrow();
433          } catch (NullPointerException success) {}
# Line 432 | Line 437 | public class LinkedBlockingDequeTest ext
437       * push succeeds if not full; throws ISE if full
438       */
439      public void testPush() {
440 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
441 +        for (int i = 0; i < SIZE; ++i) {
442 +            Integer x = new Integer(i);
443 +            q.push(x);
444 +            assertEquals(x, q.peek());
445 +        }
446 +        assertEquals(0, q.remainingCapacity());
447          try {
436            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
437            for (int i = 0; i < SIZE; ++i) {
438                Integer I = new Integer(i);
439                q.push(I);
440                assertEquals(I, q.peek());
441            }
442            assertEquals(0, q.remainingCapacity());
448              q.push(new Integer(SIZE));
449              shouldThrow();
450          } catch (IllegalStateException success) {}
# Line 510 | 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 555 | Line 560 | public class LinkedBlockingDequeTest ext
560      public void testPut() throws InterruptedException {
561          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
562          for (int i = 0; i < SIZE; ++i) {
563 <            Integer I = new Integer(i);
564 <            q.put(I);
565 <            assertTrue(q.contains(I));
563 >            Integer x = new Integer(i);
564 >            q.put(x);
565 >            assertTrue(q.contains(x));
566          }
567          assertEquals(0, q.remainingCapacity());
568      }
# Line 748 | 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) {
752                    long t0 = System.nanoTime();
758                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
754                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
759                  }
756                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 787 | Line 790 | public class LinkedBlockingDequeTest ext
790      public void testPutFirst() throws InterruptedException {
791          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
792          for (int i = 0; i < SIZE; ++i) {
793 <            Integer I = new Integer(i);
794 <            q.putFirst(I);
795 <            assertTrue(q.contains(I));
793 >            Integer x = new Integer(i);
794 >            q.putFirst(x);
795 >            assertTrue(q.contains(x));
796          }
797          assertEquals(0, q.remainingCapacity());
798      }
# Line 1047 | 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 1068 | 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 1134 | Line 1139 | public class LinkedBlockingDequeTest ext
1139      public void testPutLast() throws InterruptedException {
1140          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1141          for (int i = 0; i < SIZE; ++i) {
1142 <            Integer I = new Integer(i);
1143 <            q.putLast(I);
1144 <            assertTrue(q.contains(I));
1142 >            Integer x = new Integer(i);
1143 >            q.putLast(x);
1144 >            assertTrue(q.contains(x));
1145          }
1146          assertEquals(0, q.remainingCapacity());
1147      }
# Line 1243 | 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 1256 | 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 1286 | 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 1298 | 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 1312 | 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 1333 | 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 1371 | 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 1455 | 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 1468 | 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 I = (Integer)(p.remove());
1486 <                assertFalse(q.contains(I));
1485 >                Integer x = (Integer)(p.remove());
1486 >                assertFalse(q.contains(x));
1487              }
1488          }
1489      }
# Line 1515 | Line 1527 | public class LinkedBlockingDequeTest ext
1527      public void testIterator() throws InterruptedException {
1528          LinkedBlockingDeque q = populatedDeque(SIZE);
1529          Iterator it = q.iterator();
1530 <        while (it.hasNext()) {
1530 >        int i;
1531 >        for (i = 0; it.hasNext(); i++)
1532 >            assertTrue(q.contains(it.next()));
1533 >        assertEquals(i, SIZE);
1534 >        assertIteratorExhausted(it);
1535 >
1536 >        it = q.iterator();
1537 >        for (i = 0; it.hasNext(); i++)
1538              assertEquals(it.next(), q.take());
1539 <        }
1539 >        assertEquals(i, SIZE);
1540 >        assertIteratorExhausted(it);
1541 >    }
1542 >
1543 >    /**
1544 >     * iterator of empty collection has no elements
1545 >     */
1546 >    public void testEmptyIterator() {
1547 >        Deque c = new LinkedBlockingDeque();
1548 >        assertIteratorExhausted(c.iterator());
1549 >        assertIteratorExhausted(c.descendingIterator());
1550      }
1551  
1552      /**
# Line 1650 | Line 1679 | public class LinkedBlockingDequeTest ext
1679          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1680          q.add(one);
1681          q.add(two);
1653        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 1675 | 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 1742 | 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 1767 | 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 <            while (q.poll() != null) ;
1802 >            do {} while (q.poll() != null);
1803 >        }
1804 >    }
1805 >
1806 >    /**
1807 >     * remove(null), contains(null) always return false
1808 >     */
1809 >    public void testNeverContainsNull() {
1810 >        Deque<?>[] qs = {
1811 >            new LinkedBlockingDeque<Object>(),
1812 >            populatedDeque(2),
1813 >        };
1814 >
1815 >        for (Deque<?> q : qs) {
1816 >            assertFalse(q.contains(null));
1817 >            assertFalse(q.remove(null));
1818 >            assertFalse(q.removeFirstOccurrence(null));
1819 >            assertFalse(q.removeLastOccurrence(null));
1820          }
1821      }
1822  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines