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.41 by jsr166, Tue May 31 16:16:24 2011 UTC vs.
Revision 1.64 by jsr166, Sat Aug 6 17:02:49 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 29 | Line 32 | public class LinkedBlockingDequeTest ext
32  
33      public static class Bounded extends BlockingQueueTest {
34          protected BlockingQueue emptyCollection() {
35 <            return new LinkedBlockingDeque(20);
35 >            return new LinkedBlockingDeque(SIZE);
36          }
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 44 | Line 47 | public class LinkedBlockingDequeTest ext
47      }
48  
49      /**
50 <     * Create a deque of given size containing consecutive
50 >     * Returns a new deque of given size containing consecutive
51       * Integers 0 ... n.
52       */
53      private LinkedBlockingDeque<Integer> populatedDeque(int n) {
# Line 79 | Line 82 | public class LinkedBlockingDequeTest ext
82      public void testSize() {
83          LinkedBlockingDeque q = populatedDeque(SIZE);
84          for (int i = 0; i < SIZE; ++i) {
85 <            assertEquals(SIZE-i, q.size());
85 >            assertEquals(SIZE - i, q.size());
86              q.removeFirst();
87          }
88          for (int i = 0; i < SIZE; ++i) {
# Line 144 | Line 147 | public class LinkedBlockingDequeTest ext
147       */
148      public void testPollLast() {
149          LinkedBlockingDeque q = populatedDeque(SIZE);
150 <        for (int i = SIZE-1; i >= 0; --i) {
150 >        for (int i = SIZE - 1; i >= 0; --i) {
151              assertEquals(i, q.pollLast());
152          }
153          assertNull(q.pollLast());
# Line 183 | Line 186 | public class LinkedBlockingDequeTest ext
186       */
187      public void testPeekLast() {
188          LinkedBlockingDeque q = populatedDeque(SIZE);
189 <        for (int i = SIZE-1; i >= 0; --i) {
189 >        for (int i = SIZE - 1; i >= 0; --i) {
190              assertEquals(i, q.peekLast());
191              assertEquals(i, q.pollLast());
192              assertTrue(q.peekLast() == null ||
# Line 213 | Line 216 | public class LinkedBlockingDequeTest ext
216       */
217      public void testLastElement() {
218          LinkedBlockingDeque q = populatedDeque(SIZE);
219 <        for (int i = SIZE-1; i >= 0; --i) {
219 >        for (int i = SIZE - 1; i >= 0; --i) {
220              assertEquals(i, q.getLast());
221              assertEquals(i, q.pollLast());
222          }
# Line 273 | Line 276 | public class LinkedBlockingDequeTest ext
276       */
277      public void testRemoveFirstOccurrence() {
278          LinkedBlockingDeque q = populatedDeque(SIZE);
279 <        for (int i = 1; i < SIZE; i+=2) {
279 >        for (int i = 1; i < SIZE; i += 2) {
280              assertTrue(q.removeFirstOccurrence(new Integer(i)));
281          }
282 <        for (int i = 0; i < SIZE; i+=2) {
282 >        for (int i = 0; i < SIZE; i += 2) {
283              assertTrue(q.removeFirstOccurrence(new Integer(i)));
284 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
284 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
285          }
286          assertTrue(q.isEmpty());
287      }
# Line 288 | Line 291 | public class LinkedBlockingDequeTest ext
291       */
292      public void testRemoveLastOccurrence() {
293          LinkedBlockingDeque q = populatedDeque(SIZE);
294 <        for (int i = 1; i < SIZE; i+=2) {
294 >        for (int i = 1; i < SIZE; i += 2) {
295              assertTrue(q.removeLastOccurrence(new Integer(i)));
296          }
297 <        for (int i = 0; i < SIZE; i+=2) {
297 >        for (int i = 0; i < SIZE; i += 2) {
298              assertTrue(q.removeLastOccurrence(new Integer(i)));
299 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
299 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
300          }
301          assertTrue(q.isEmpty());
302      }
# Line 364 | Line 367 | public class LinkedBlockingDequeTest ext
367       */
368      public void testConstructor5() {
369          Integer[] ints = new Integer[SIZE];
370 <        for (int i = 0; i < SIZE-1; ++i)
370 >        for (int i = 0; i < SIZE - 1; ++i)
371              ints[i] = i;
372          Collection<Integer> elements = Arrays.asList(ints);
373          try {
# Line 404 | Line 407 | public class LinkedBlockingDequeTest ext
407       * remainingCapacity decreases on add, increases on remove
408       */
409      public void testRemainingCapacity() {
410 <        LinkedBlockingDeque q = populatedDeque(SIZE);
410 >        BlockingQueue q = populatedDeque(SIZE);
411          for (int i = 0; i < SIZE; ++i) {
412              assertEquals(i, q.remainingCapacity());
413 <            assertEquals(SIZE-i, q.size());
414 <            q.remove();
413 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
414 >            assertEquals(i, q.remove());
415          }
416          for (int i = 0; i < SIZE; ++i) {
417 <            assertEquals(SIZE-i, q.remainingCapacity());
418 <            assertEquals(i, q.size());
419 <            q.add(new Integer(i));
417 >            assertEquals(SIZE - i, q.remainingCapacity());
418 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
419 >            assertTrue(q.add(i));
420          }
421      }
422  
# Line 421 | Line 424 | public class LinkedBlockingDequeTest ext
424       * push(null) throws NPE
425       */
426      public void testPushNull() {
427 +        LinkedBlockingDeque q = new LinkedBlockingDeque(1);
428          try {
425            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
429              q.push(null);
430              shouldThrow();
431          } catch (NullPointerException success) {}
# Line 432 | Line 435 | public class LinkedBlockingDequeTest ext
435       * push succeeds if not full; throws ISE if full
436       */
437      public void testPush() {
438 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
439 +        for (int i = 0; i < SIZE; ++i) {
440 +            Integer x = new Integer(i);
441 +            q.push(x);
442 +            assertEquals(x, q.peek());
443 +        }
444 +        assertEquals(0, q.remainingCapacity());
445          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());
446              q.push(new Integer(SIZE));
447              shouldThrow();
448          } catch (IllegalStateException success) {}
# Line 510 | Line 513 | public class LinkedBlockingDequeTest ext
513      public void testAddAll3() {
514          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
515          Integer[] ints = new Integer[SIZE];
516 <        for (int i = 0; i < SIZE-1; ++i)
516 >        for (int i = 0; i < SIZE - 1; ++i)
517              ints[i] = new Integer(i);
518          Collection<Integer> elements = Arrays.asList(ints);
519          try {
# Line 555 | Line 558 | public class LinkedBlockingDequeTest ext
558      public void testPut() throws InterruptedException {
559          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
560          for (int i = 0; i < SIZE; ++i) {
561 <            Integer I = new Integer(i);
562 <            q.put(I);
563 <            assertTrue(q.contains(I));
561 >            Integer x = new Integer(i);
562 >            q.put(x);
563 >            assertTrue(q.contains(x));
564          }
565          assertEquals(0, q.remainingCapacity());
566      }
# Line 622 | Line 625 | public class LinkedBlockingDequeTest ext
625              }});
626  
627          await(pleaseTake);
628 <        assertEquals(q.remainingCapacity(), 0);
628 >        assertEquals(0, q.remainingCapacity());
629          assertEquals(0, q.take());
630  
631          await(pleaseInterrupt);
632          assertThreadStaysAlive(t);
633          t.interrupt();
634          awaitTermination(t);
635 <        assertEquals(q.remainingCapacity(), 0);
635 >        assertEquals(0, q.remainingCapacity());
636      }
637  
638      /**
# Line 748 | Line 751 | public class LinkedBlockingDequeTest ext
751          final CountDownLatch aboutToWait = new CountDownLatch(1);
752          Thread t = newStartedThread(new CheckedRunnable() {
753              public void realRun() throws InterruptedException {
754 +                long startTime = System.nanoTime();
755                  for (int i = 0; i < SIZE; ++i) {
752                    long t0 = System.nanoTime();
756                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
754                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
757                  }
756                long t0 = System.nanoTime();
758                  aboutToWait.countDown();
759                  try {
760 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
760 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
761                      shouldThrow();
762                  } catch (InterruptedException success) {
763 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
763 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
764                  }
765              }});
766  
767          aboutToWait.await();
768 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
768 >        waitForThreadToEnterWaitState(t);
769          t.interrupt();
770 <        awaitTermination(t, MEDIUM_DELAY_MS);
770 >        awaitTermination(t);
771          checkEmpty(q);
772      }
773  
# Line 787 | Line 788 | public class LinkedBlockingDequeTest ext
788      public void testPutFirst() throws InterruptedException {
789          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
790          for (int i = 0; i < SIZE; ++i) {
791 <            Integer I = new Integer(i);
792 <            q.putFirst(I);
793 <            assertTrue(q.contains(I));
791 >            Integer x = new Integer(i);
792 >            q.putFirst(x);
793 >            assertTrue(q.contains(x));
794          }
795          assertEquals(0, q.remainingCapacity());
796      }
# Line 854 | Line 855 | public class LinkedBlockingDequeTest ext
855              }});
856  
857          await(pleaseTake);
858 <        assertEquals(q.remainingCapacity(), 0);
858 >        assertEquals(0, q.remainingCapacity());
859          assertEquals(capacity - 1, q.take());
860  
861          await(pleaseInterrupt);
862          assertThreadStaysAlive(t);
863          t.interrupt();
864          awaitTermination(t);
865 <        assertEquals(q.remainingCapacity(), 0);
865 >        assertEquals(0, q.remainingCapacity());
866      }
867  
868      /**
# Line 1047 | Line 1048 | public class LinkedBlockingDequeTest ext
1048       * returning timeout status
1049       */
1050      public void testInterruptedTimedPollFirst() throws InterruptedException {
1051 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1052          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1053          Thread t = newStartedThread(new CheckedRunnable() {
1054              public void realRun() throws InterruptedException {
1055 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1055 >                long startTime = System.nanoTime();
1056                  for (int i = 0; i < SIZE; ++i) {
1057                      assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1058                  }
1059  
1060                  Thread.currentThread().interrupt();
1061                  try {
1062 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1062 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1063                      shouldThrow();
1064                  } catch (InterruptedException success) {}
1065                  assertFalse(Thread.interrupted());
1066  
1067                  pleaseInterrupt.countDown();
1068                  try {
1069 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1069 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1070                      shouldThrow();
1071                  } catch (InterruptedException success) {}
1072                  assertFalse(Thread.interrupted());
1073 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1074              }});
1075  
1076          await(pleaseInterrupt);
# Line 1134 | Line 1137 | public class LinkedBlockingDequeTest ext
1137      public void testPutLast() throws InterruptedException {
1138          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1139          for (int i = 0; i < SIZE; ++i) {
1140 <            Integer I = new Integer(i);
1141 <            q.putLast(I);
1142 <            assertTrue(q.contains(I));
1140 >            Integer x = new Integer(i);
1141 >            q.putLast(x);
1142 >            assertTrue(q.contains(x));
1143          }
1144          assertEquals(0, q.remainingCapacity());
1145      }
# Line 1201 | Line 1204 | public class LinkedBlockingDequeTest ext
1204              }});
1205  
1206          await(pleaseTake);
1207 <        assertEquals(q.remainingCapacity(), 0);
1207 >        assertEquals(0, q.remainingCapacity());
1208          assertEquals(0, q.take());
1209  
1210          await(pleaseInterrupt);
1211          assertThreadStaysAlive(t);
1212          t.interrupt();
1213          awaitTermination(t);
1214 <        assertEquals(q.remainingCapacity(), 0);
1214 >        assertEquals(0, q.remainingCapacity());
1215      }
1216  
1217      /**
# Line 1243 | Line 1246 | public class LinkedBlockingDequeTest ext
1246      public void testTakeLast() throws InterruptedException {
1247          LinkedBlockingDeque q = populatedDeque(SIZE);
1248          for (int i = 0; i < SIZE; ++i) {
1249 <            assertEquals(SIZE-i-1, q.takeLast());
1249 >            assertEquals(SIZE - i - 1, q.takeLast());
1250          }
1251      }
1252  
# Line 1256 | Line 1259 | public class LinkedBlockingDequeTest ext
1259          Thread t = newStartedThread(new CheckedRunnable() {
1260              public void realRun() throws InterruptedException {
1261                  for (int i = 0; i < SIZE; ++i) {
1262 <                    assertEquals(SIZE-i-1, q.takeLast());
1262 >                    assertEquals(SIZE - i - 1, q.takeLast());
1263                  }
1264  
1265                  Thread.currentThread().interrupt();
# Line 1286 | Line 1289 | public class LinkedBlockingDequeTest ext
1289      public void testTimedPollLast0() throws InterruptedException {
1290          LinkedBlockingDeque q = populatedDeque(SIZE);
1291          for (int i = 0; i < SIZE; ++i) {
1292 <            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1292 >            assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
1293          }
1294          assertNull(q.pollLast(0, MILLISECONDS));
1295      }
# Line 1298 | Line 1301 | public class LinkedBlockingDequeTest ext
1301          LinkedBlockingDeque q = populatedDeque(SIZE);
1302          for (int i = 0; i < SIZE; ++i) {
1303              long startTime = System.nanoTime();
1304 <            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1304 >            assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1305              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1306          }
1307          long startTime = System.nanoTime();
# Line 1312 | Line 1315 | public class LinkedBlockingDequeTest ext
1315       * returning timeout status
1316       */
1317      public void testInterruptedTimedPollLast() throws InterruptedException {
1318 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1319          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1320          Thread t = newStartedThread(new CheckedRunnable() {
1321              public void realRun() throws InterruptedException {
1322 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1322 >                long startTime = System.nanoTime();
1323                  for (int i = 0; i < SIZE; ++i) {
1324 <                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1324 >                    assertEquals(SIZE - i - 1,
1325 >                                 q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1326                  }
1327  
1328                  Thread.currentThread().interrupt();
# Line 1333 | Line 1338 | public class LinkedBlockingDequeTest ext
1338                      shouldThrow();
1339                  } catch (InterruptedException success) {}
1340                  assertFalse(Thread.interrupted());
1341 +
1342 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1343              }});
1344  
1345          await(pleaseInterrupt);
1346          assertThreadStaysAlive(t);
1347          t.interrupt();
1348          awaitTermination(t);
1349 +        checkEmpty(q);
1350      }
1351  
1352      /**
# Line 1371 | Line 1379 | public class LinkedBlockingDequeTest ext
1379                      shouldThrow();
1380                  } catch (InterruptedException success) {}
1381                  assertFalse(Thread.interrupted());
1382 +
1383 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1384              }});
1385  
1386          barrier.await();
# Line 1400 | Line 1410 | public class LinkedBlockingDequeTest ext
1410      }
1411  
1412      /**
1403     * remove(x) removes x and returns true if present
1404     */
1405    public void testRemoveElement() {
1406        LinkedBlockingDeque q = populatedDeque(SIZE);
1407        for (int i = 1; i < SIZE; i+=2) {
1408            assertTrue(q.contains(i));
1409            assertTrue(q.remove(i));
1410            assertFalse(q.contains(i));
1411            assertTrue(q.contains(i-1));
1412        }
1413        for (int i = 0; i < SIZE; i+=2) {
1414            assertTrue(q.contains(i));
1415            assertTrue(q.remove(i));
1416            assertFalse(q.contains(i));
1417            assertFalse(q.remove(i+1));
1418            assertFalse(q.contains(i+1));
1419        }
1420        assertTrue(q.isEmpty());
1421    }
1422
1423    /**
1413       * contains(x) reports true when elements added but not yet removed
1414       */
1415      public void testContains() {
# Line 1476 | Line 1465 | public class LinkedBlockingDequeTest ext
1465                  assertTrue(changed);
1466  
1467              assertTrue(q.containsAll(p));
1468 <            assertEquals(SIZE-i, q.size());
1468 >            assertEquals(SIZE - i, q.size());
1469              p.remove();
1470          }
1471      }
# Line 1489 | Line 1478 | public class LinkedBlockingDequeTest ext
1478              LinkedBlockingDeque q = populatedDeque(SIZE);
1479              LinkedBlockingDeque p = populatedDeque(i);
1480              assertTrue(q.removeAll(p));
1481 <            assertEquals(SIZE-i, q.size());
1481 >            assertEquals(SIZE - i, q.size());
1482              for (int j = 0; j < i; ++j) {
1483 <                Integer I = (Integer)(p.remove());
1484 <                assertFalse(q.contains(I));
1483 >                Integer x = (Integer)(p.remove());
1484 >                assertFalse(q.contains(x));
1485              }
1486          }
1487      }
# Line 1500 | Line 1489 | public class LinkedBlockingDequeTest ext
1489      /**
1490       * toArray contains all elements in FIFO order
1491       */
1492 <    public void testToArray() throws InterruptedException{
1492 >    public void testToArray() throws InterruptedException {
1493          LinkedBlockingDeque q = populatedDeque(SIZE);
1494          Object[] o = q.toArray();
1495          for (int i = 0; i < o.length; i++)
# Line 1536 | Line 1525 | public class LinkedBlockingDequeTest ext
1525      public void testIterator() throws InterruptedException {
1526          LinkedBlockingDeque q = populatedDeque(SIZE);
1527          Iterator it = q.iterator();
1528 <        while (it.hasNext()) {
1528 >        int i;
1529 >        for (i = 0; it.hasNext(); i++)
1530 >            assertTrue(q.contains(it.next()));
1531 >        assertEquals(i, SIZE);
1532 >        assertIteratorExhausted(it);
1533 >
1534 >        it = q.iterator();
1535 >        for (i = 0; it.hasNext(); i++)
1536              assertEquals(it.next(), q.take());
1537 <        }
1537 >        assertEquals(i, SIZE);
1538 >        assertIteratorExhausted(it);
1539 >    }
1540 >
1541 >    /**
1542 >     * iterator of empty collection has no elements
1543 >     */
1544 >    public void testEmptyIterator() {
1545 >        Deque c = new LinkedBlockingDeque();
1546 >        assertIteratorExhausted(c.iterator());
1547 >        assertIteratorExhausted(c.descendingIterator());
1548      }
1549  
1550      /**
# Line 1671 | Line 1677 | public class LinkedBlockingDequeTest ext
1677          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1678          q.add(one);
1679          q.add(two);
1674        ExecutorService executor = Executors.newFixedThreadPool(2);
1680          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1681 <        executor.execute(new CheckedRunnable() {
1682 <            public void realRun() throws InterruptedException {
1683 <                assertFalse(q.offer(three));
1684 <                threadsStarted.await();
1685 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1686 <                assertEquals(0, q.remainingCapacity());
1687 <            }});
1688 <
1689 <        executor.execute(new CheckedRunnable() {
1690 <            public void realRun() throws InterruptedException {
1691 <                threadsStarted.await();
1692 <                assertSame(one, q.take());
1693 <            }});
1694 <
1695 <        joinPool(executor);
1681 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1682 >        try (PoolCleaner cleaner = cleaner(executor)) {
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      }
1698  
1699      /**
# Line 1696 | Line 1702 | public class LinkedBlockingDequeTest ext
1702      public void testPollInExecutor() {
1703          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1704          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1705 <        ExecutorService executor = Executors.newFixedThreadPool(2);
1706 <        executor.execute(new CheckedRunnable() {
1707 <            public void realRun() throws InterruptedException {
1708 <                assertNull(q.poll());
1709 <                threadsStarted.await();
1710 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1711 <                checkEmpty(q);
1712 <            }});
1713 <
1714 <        executor.execute(new CheckedRunnable() {
1715 <            public void realRun() throws InterruptedException {
1716 <                threadsStarted.await();
1717 <                q.put(one);
1718 <            }});
1719 <
1720 <        joinPool(executor);
1705 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1706 >        try (PoolCleaner cleaner = cleaner(executor)) {
1707 >            executor.execute(new CheckedRunnable() {
1708 >                public void realRun() throws InterruptedException {
1709 >                    assertNull(q.poll());
1710 >                    threadsStarted.await();
1711 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1712 >                    checkEmpty(q);
1713 >                }});
1714 >
1715 >            executor.execute(new CheckedRunnable() {
1716 >                public void realRun() throws InterruptedException {
1717 >                    threadsStarted.await();
1718 >                    q.put(one);
1719 >                }});
1720 >        }
1721      }
1722  
1723      /**
# Line 1721 | Line 1727 | public class LinkedBlockingDequeTest ext
1727          Queue x = populatedDeque(SIZE);
1728          Queue y = serialClone(x);
1729  
1730 <        assertTrue(x != y);
1730 >        assertNotSame(y, x);
1731          assertEquals(x.size(), y.size());
1732          assertEquals(x.toString(), y.toString());
1733          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
# Line 1739 | Line 1745 | public class LinkedBlockingDequeTest ext
1745          LinkedBlockingDeque q = populatedDeque(SIZE);
1746          ArrayList l = new ArrayList();
1747          q.drainTo(l);
1748 <        assertEquals(q.size(), 0);
1749 <        assertEquals(l.size(), SIZE);
1748 >        assertEquals(0, q.size());
1749 >        assertEquals(SIZE, l.size());
1750          for (int i = 0; i < SIZE; ++i)
1751              assertEquals(l.get(i), new Integer(i));
1752          q.add(zero);
# Line 1750 | Line 1756 | public class LinkedBlockingDequeTest ext
1756          assertTrue(q.contains(one));
1757          l.clear();
1758          q.drainTo(l);
1759 <        assertEquals(q.size(), 0);
1760 <        assertEquals(l.size(), 2);
1759 >        assertEquals(0, q.size());
1760 >        assertEquals(2, l.size());
1761          for (int i = 0; i < 2; ++i)
1762              assertEquals(l.get(i), new Integer(i));
1763      }
# Line 1763 | Line 1769 | public class LinkedBlockingDequeTest ext
1769          final LinkedBlockingDeque q = populatedDeque(SIZE);
1770          Thread t = new Thread(new CheckedRunnable() {
1771              public void realRun() throws InterruptedException {
1772 <                q.put(new Integer(SIZE+1));
1772 >                q.put(new Integer(SIZE + 1));
1773              }});
1774  
1775          t.start();
# Line 1787 | Line 1793 | public class LinkedBlockingDequeTest ext
1793              ArrayList l = new ArrayList();
1794              q.drainTo(l, i);
1795              int k = (i < SIZE) ? i : SIZE;
1796 <            assertEquals(l.size(), k);
1797 <            assertEquals(q.size(), SIZE-k);
1796 >            assertEquals(k, l.size());
1797 >            assertEquals(SIZE - k, q.size());
1798              for (int j = 0; j < k; ++j)
1799                  assertEquals(l.get(j), new Integer(j));
1800 <            while (q.poll() != null) ;
1800 >            do {} while (q.poll() != null);
1801 >        }
1802 >    }
1803 >
1804 >    /**
1805 >     * remove(null), contains(null) always return false
1806 >     */
1807 >    public void testNeverContainsNull() {
1808 >        Deque<?>[] qs = {
1809 >            new LinkedBlockingDeque<Object>(),
1810 >            populatedDeque(2),
1811 >        };
1812 >
1813 >        for (Deque<?> q : qs) {
1814 >            assertFalse(q.contains(null));
1815 >            assertFalse(q.remove(null));
1816 >            assertFalse(q.removeFirstOccurrence(null));
1817 >            assertFalse(q.removeLastOccurrence(null));
1818          }
1819      }
1820  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines