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.43 by jsr166, Tue Oct 25 20:29:12 2011 UTC vs.
Revision 1.77 by jsr166, Sun May 14 00:56:43 2017 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() {
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 <     * Create a deque of given size containing consecutive
59 <     * Integers 0 ... n.
58 >     * Returns a new deque of given size containing consecutive
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 56 | 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 79 | Line 92 | public class LinkedBlockingDequeTest ext
92      public void testSize() {
93          LinkedBlockingDeque q = populatedDeque(SIZE);
94          for (int i = 0; i < SIZE; ++i) {
95 <            assertEquals(SIZE-i, q.size());
95 >            assertEquals(SIZE - i, q.size());
96              q.removeFirst();
97          }
98          for (int i = 0; i < SIZE; ++i) {
# Line 144 | Line 157 | public class LinkedBlockingDequeTest ext
157       */
158      public void testPollLast() {
159          LinkedBlockingDeque q = populatedDeque(SIZE);
160 <        for (int i = SIZE-1; i >= 0; --i) {
160 >        for (int i = SIZE - 1; i >= 0; --i) {
161              assertEquals(i, q.pollLast());
162          }
163          assertNull(q.pollLast());
# Line 183 | Line 196 | public class LinkedBlockingDequeTest ext
196       */
197      public void testPeekLast() {
198          LinkedBlockingDeque q = populatedDeque(SIZE);
199 <        for (int i = SIZE-1; i >= 0; --i) {
199 >        for (int i = SIZE - 1; i >= 0; --i) {
200              assertEquals(i, q.peekLast());
201              assertEquals(i, q.pollLast());
202              assertTrue(q.peekLast() == null ||
# Line 213 | Line 226 | public class LinkedBlockingDequeTest ext
226       */
227      public void testLastElement() {
228          LinkedBlockingDeque q = populatedDeque(SIZE);
229 <        for (int i = SIZE-1; i >= 0; --i) {
229 >        for (int i = SIZE - 1; i >= 0; --i) {
230              assertEquals(i, q.getLast());
231              assertEquals(i, q.pollLast());
232          }
# Line 273 | Line 286 | public class LinkedBlockingDequeTest ext
286       */
287      public void testRemoveFirstOccurrence() {
288          LinkedBlockingDeque q = populatedDeque(SIZE);
289 <        for (int i = 1; i < SIZE; i+=2) {
289 >        for (int i = 1; i < SIZE; i += 2) {
290              assertTrue(q.removeFirstOccurrence(new Integer(i)));
291          }
292 <        for (int i = 0; i < SIZE; i+=2) {
292 >        for (int i = 0; i < SIZE; i += 2) {
293              assertTrue(q.removeFirstOccurrence(new Integer(i)));
294 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
294 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
295          }
296          assertTrue(q.isEmpty());
297      }
# Line 288 | Line 301 | public class LinkedBlockingDequeTest ext
301       */
302      public void testRemoveLastOccurrence() {
303          LinkedBlockingDeque q = populatedDeque(SIZE);
304 <        for (int i = 1; i < SIZE; i+=2) {
304 >        for (int i = 1; i < SIZE; i += 2) {
305              assertTrue(q.removeLastOccurrence(new Integer(i)));
306          }
307 <        for (int i = 0; i < SIZE; i+=2) {
307 >        for (int i = 0; i < SIZE; i += 2) {
308              assertTrue(q.removeLastOccurrence(new Integer(i)));
309 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
309 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
310          }
311          assertTrue(q.isEmpty());
312      }
# Line 364 | Line 377 | public class LinkedBlockingDequeTest ext
377       */
378      public void testConstructor5() {
379          Integer[] ints = new Integer[SIZE];
380 <        for (int i = 0; i < SIZE-1; ++i)
380 >        for (int i = 0; i < SIZE - 1; ++i)
381              ints[i] = i;
382          Collection<Integer> elements = Arrays.asList(ints);
383          try {
# Line 404 | Line 417 | public class LinkedBlockingDequeTest ext
417       * remainingCapacity decreases on add, increases on remove
418       */
419      public void testRemainingCapacity() {
420 <        LinkedBlockingDeque q = populatedDeque(SIZE);
420 >        BlockingQueue q = populatedDeque(SIZE);
421          for (int i = 0; i < SIZE; ++i) {
422              assertEquals(i, q.remainingCapacity());
423 <            assertEquals(SIZE-i, q.size());
424 <            q.remove();
423 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
424 >            assertEquals(i, q.remove());
425          }
426          for (int i = 0; i < SIZE; ++i) {
427 <            assertEquals(SIZE-i, q.remainingCapacity());
428 <            assertEquals(i, q.size());
429 <            q.add(new Integer(i));
427 >            assertEquals(SIZE - i, q.remainingCapacity());
428 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
429 >            assertTrue(q.add(i));
430          }
431      }
432  
# Line 421 | Line 434 | public class LinkedBlockingDequeTest ext
434       * push(null) throws NPE
435       */
436      public void testPushNull() {
437 +        LinkedBlockingDeque q = new LinkedBlockingDeque(1);
438          try {
425            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
439              q.push(null);
440              shouldThrow();
441          } catch (NullPointerException success) {}
# Line 432 | Line 445 | public class LinkedBlockingDequeTest ext
445       * push succeeds if not full; throws ISE if full
446       */
447      public void testPush() {
448 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
449 +        for (int i = 0; i < SIZE; ++i) {
450 +            Integer x = new Integer(i);
451 +            q.push(x);
452 +            assertEquals(x, q.peek());
453 +        }
454 +        assertEquals(0, q.remainingCapacity());
455          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());
456              q.push(new Integer(SIZE));
457              shouldThrow();
458          } catch (IllegalStateException success) {}
# Line 510 | Line 523 | public class LinkedBlockingDequeTest ext
523      public void testAddAll3() {
524          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
525          Integer[] ints = new Integer[SIZE];
526 <        for (int i = 0; i < SIZE-1; ++i)
526 >        for (int i = 0; i < SIZE - 1; ++i)
527              ints[i] = new Integer(i);
528          Collection<Integer> elements = Arrays.asList(ints);
529          try {
# Line 555 | Line 568 | public class LinkedBlockingDequeTest ext
568      public void testPut() throws InterruptedException {
569          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
570          for (int i = 0; i < SIZE; ++i) {
571 <            Integer I = new Integer(i);
572 <            q.put(I);
573 <            assertTrue(q.contains(I));
571 >            Integer x = new Integer(i);
572 >            q.put(x);
573 >            assertTrue(q.contains(x));
574          }
575          assertEquals(0, q.remainingCapacity());
576      }
# Line 591 | 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 622 | Line 635 | public class LinkedBlockingDequeTest ext
635              }});
636  
637          await(pleaseTake);
638 <        assertEquals(q.remainingCapacity(), 0);
638 >        assertEquals(0, q.remainingCapacity());
639          assertEquals(0, q.take());
640  
641          await(pleaseInterrupt);
642 <        assertThreadStaysAlive(t);
642 >        assertThreadBlocks(t, Thread.State.WAITING);
643          t.interrupt();
644          awaitTermination(t);
645 <        assertEquals(q.remainingCapacity(), 0);
645 >        assertEquals(0, q.remainingCapacity());
646      }
647  
648      /**
# Line 650 | Line 663 | public class LinkedBlockingDequeTest ext
663                      q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
664                      shouldThrow();
665                  } catch (InterruptedException success) {}
666 +                assertFalse(Thread.interrupted());
667              }});
668  
669          await(pleaseInterrupt);
670 <        assertThreadStaysAlive(t);
670 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
671          t.interrupt();
672          awaitTermination(t);
673      }
# Line 676 | Line 690 | public class LinkedBlockingDequeTest ext
690          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
691          Thread t = newStartedThread(new CheckedRunnable() {
692              public void realRun() throws InterruptedException {
693 <                for (int i = 0; i < SIZE; ++i) {
680 <                    assertEquals(i, q.take());
681 <                }
693 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
694  
695                  Thread.currentThread().interrupt();
696                  try {
# Line 696 | Line 708 | public class LinkedBlockingDequeTest ext
708              }});
709  
710          await(pleaseInterrupt);
711 <        assertThreadStaysAlive(t);
711 >        assertThreadBlocks(t, Thread.State.WAITING);
712          t.interrupt();
713          awaitTermination(t);
714      }
# Line 745 | Line 757 | public class LinkedBlockingDequeTest ext
757       */
758      public void testInterruptedTimedPoll() throws InterruptedException {
759          final BlockingQueue<Integer> q = populatedDeque(SIZE);
760 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
760 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
761          Thread t = newStartedThread(new CheckedRunnable() {
762              public void realRun() throws InterruptedException {
763 +                long startTime = System.nanoTime();
764                  for (int i = 0; i < SIZE; ++i) {
752                    long t0 = System.nanoTime();
765                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
754                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
766                  }
767 <                long t0 = System.nanoTime();
768 <                aboutToWait.countDown();
767 >
768 >                pleaseInterrupt.countDown();
769                  try {
770 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
770 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
771                      shouldThrow();
772 <                } catch (InterruptedException success) {
773 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
774 <                }
772 >                } catch (InterruptedException success) {}
773 >                assertFalse(Thread.interrupted());
774 >
775 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
776              }});
777  
778 <        aboutToWait.await();
779 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
778 >        await(pleaseInterrupt);
779 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
780          t.interrupt();
781 <        awaitTermination(t, MEDIUM_DELAY_MS);
781 >        awaitTermination(t);
782          checkEmpty(q);
783      }
784  
# Line 787 | Line 799 | public class LinkedBlockingDequeTest ext
799      public void testPutFirst() throws InterruptedException {
800          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
801          for (int i = 0; i < SIZE; ++i) {
802 <            Integer I = new Integer(i);
803 <            q.putFirst(I);
804 <            assertTrue(q.contains(I));
802 >            Integer x = new Integer(i);
803 >            q.putFirst(x);
804 >            assertTrue(q.contains(x));
805          }
806          assertEquals(0, q.remainingCapacity());
807      }
# Line 823 | Line 835 | public class LinkedBlockingDequeTest ext
835              }});
836  
837          await(pleaseInterrupt);
838 <        assertThreadStaysAlive(t);
838 >        assertThreadBlocks(t, Thread.State.WAITING);
839          t.interrupt();
840          awaitTermination(t);
841          assertEquals(SIZE, q.size());
# Line 854 | Line 866 | public class LinkedBlockingDequeTest ext
866              }});
867  
868          await(pleaseTake);
869 <        assertEquals(q.remainingCapacity(), 0);
869 >        assertEquals(0, q.remainingCapacity());
870          assertEquals(capacity - 1, q.take());
871  
872          await(pleaseInterrupt);
873 <        assertThreadStaysAlive(t);
873 >        assertThreadBlocks(t, Thread.State.WAITING);
874          t.interrupt();
875          awaitTermination(t);
876 <        assertEquals(q.remainingCapacity(), 0);
876 >        assertEquals(0, q.remainingCapacity());
877      }
878  
879      /**
# Line 882 | Line 894 | public class LinkedBlockingDequeTest ext
894                      q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
895                      shouldThrow();
896                  } catch (InterruptedException success) {}
897 +                assertFalse(Thread.interrupted());
898              }});
899  
900          await(pleaseInterrupt);
901 <        assertThreadStaysAlive(t);
901 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
902          t.interrupt();
903          awaitTermination(t);
904      }
# Line 917 | Line 930 | public class LinkedBlockingDequeTest ext
930              }});
931  
932          await(threadStarted);
933 <        assertThreadStaysAlive(t);
933 >        assertThreadBlocks(t, Thread.State.WAITING);
934          t.interrupt();
935          awaitTermination(t);
936      }
# Line 958 | Line 971 | public class LinkedBlockingDequeTest ext
971              }});
972  
973          await(threadStarted);
974 <        assertThreadStaysAlive(t);
974 >        assertThreadBlocks(t, Thread.State.WAITING);
975          t.interrupt();
976          awaitTermination(t);
977      }
# Line 990 | Line 1003 | public class LinkedBlockingDequeTest ext
1003          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1004          Thread t = newStartedThread(new CheckedRunnable() {
1005              public void realRun() throws InterruptedException {
1006 <                for (int i = 0; i < SIZE; ++i) {
994 <                    assertEquals(i, q.takeFirst());
995 <                }
1006 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst());
1007  
1008                  Thread.currentThread().interrupt();
1009                  try {
# Line 1010 | Line 1021 | public class LinkedBlockingDequeTest ext
1021              }});
1022  
1023          await(pleaseInterrupt);
1024 <        assertThreadStaysAlive(t);
1024 >        assertThreadBlocks(t, Thread.State.WAITING);
1025          t.interrupt();
1026          awaitTermination(t);
1027      }
# Line 1047 | Line 1058 | public class LinkedBlockingDequeTest ext
1058       * returning timeout status
1059       */
1060      public void testInterruptedTimedPollFirst() throws InterruptedException {
1061 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1062          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1063          Thread t = newStartedThread(new CheckedRunnable() {
1064              public void realRun() throws InterruptedException {
1065 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1065 >                long startTime = System.nanoTime();
1066                  for (int i = 0; i < SIZE; ++i) {
1067                      assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1068                  }
1069  
1070                  Thread.currentThread().interrupt();
1071                  try {
1072 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1072 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1073                      shouldThrow();
1074                  } catch (InterruptedException success) {}
1075                  assertFalse(Thread.interrupted());
1076  
1077                  pleaseInterrupt.countDown();
1078                  try {
1079 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1079 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1080                      shouldThrow();
1081                  } catch (InterruptedException success) {}
1082                  assertFalse(Thread.interrupted());
1083 +
1084 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1085              }});
1086  
1087          await(pleaseInterrupt);
1088 <        assertThreadStaysAlive(t);
1088 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1089          t.interrupt();
1090          awaitTermination(t);
1091      }
# Line 1104 | Line 1118 | public class LinkedBlockingDequeTest ext
1118                      q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1119                      shouldThrow();
1120                  } catch (InterruptedException success) {}
1121 +                assertFalse(Thread.interrupted());
1122                  assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1123              }});
1124  
# Line 1112 | Line 1127 | public class LinkedBlockingDequeTest ext
1127          assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1128          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1129          barrier.await();
1130 <        assertThreadStaysAlive(t);
1130 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1131          t.interrupt();
1132          awaitTermination(t);
1133      }
# Line 1134 | Line 1149 | public class LinkedBlockingDequeTest ext
1149      public void testPutLast() throws InterruptedException {
1150          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1151          for (int i = 0; i < SIZE; ++i) {
1152 <            Integer I = new Integer(i);
1153 <            q.putLast(I);
1154 <            assertTrue(q.contains(I));
1152 >            Integer x = new Integer(i);
1153 >            q.putLast(x);
1154 >            assertTrue(q.contains(x));
1155          }
1156          assertEquals(0, q.remainingCapacity());
1157      }
# Line 1170 | Line 1185 | public class LinkedBlockingDequeTest ext
1185              }});
1186  
1187          await(pleaseInterrupt);
1188 <        assertThreadStaysAlive(t);
1188 >        assertThreadBlocks(t, Thread.State.WAITING);
1189          t.interrupt();
1190          awaitTermination(t);
1191          assertEquals(SIZE, q.size());
# Line 1201 | Line 1216 | public class LinkedBlockingDequeTest ext
1216              }});
1217  
1218          await(pleaseTake);
1219 <        assertEquals(q.remainingCapacity(), 0);
1219 >        assertEquals(0, q.remainingCapacity());
1220          assertEquals(0, q.take());
1221  
1222          await(pleaseInterrupt);
1223 <        assertThreadStaysAlive(t);
1223 >        assertThreadBlocks(t, Thread.State.WAITING);
1224          t.interrupt();
1225          awaitTermination(t);
1226 <        assertEquals(q.remainingCapacity(), 0);
1226 >        assertEquals(0, q.remainingCapacity());
1227      }
1228  
1229      /**
# Line 1232 | Line 1247 | public class LinkedBlockingDequeTest ext
1247              }});
1248  
1249          await(pleaseInterrupt);
1250 <        assertThreadStaysAlive(t);
1250 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1251          t.interrupt();
1252          awaitTermination(t);
1253      }
# Line 1243 | Line 1258 | public class LinkedBlockingDequeTest ext
1258      public void testTakeLast() throws InterruptedException {
1259          LinkedBlockingDeque q = populatedDeque(SIZE);
1260          for (int i = 0; i < SIZE; ++i) {
1261 <            assertEquals(SIZE-i-1, q.takeLast());
1261 >            assertEquals(SIZE - i - 1, q.takeLast());
1262          }
1263      }
1264  
# Line 1255 | Line 1270 | public class LinkedBlockingDequeTest ext
1270          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1271          Thread t = newStartedThread(new CheckedRunnable() {
1272              public void realRun() throws InterruptedException {
1273 <                for (int i = 0; i < SIZE; ++i) {
1274 <                    assertEquals(SIZE-i-1, q.takeLast());
1260 <                }
1273 >                for (int i = 0; i < SIZE; i++)
1274 >                    assertEquals(SIZE - i - 1, q.takeLast());
1275  
1276                  Thread.currentThread().interrupt();
1277                  try {
# Line 1275 | Line 1289 | public class LinkedBlockingDequeTest ext
1289              }});
1290  
1291          await(pleaseInterrupt);
1292 <        assertThreadStaysAlive(t);
1292 >        assertThreadBlocks(t, Thread.State.WAITING);
1293          t.interrupt();
1294          awaitTermination(t);
1295      }
# Line 1286 | Line 1300 | public class LinkedBlockingDequeTest ext
1300      public void testTimedPollLast0() throws InterruptedException {
1301          LinkedBlockingDeque q = populatedDeque(SIZE);
1302          for (int i = 0; i < SIZE; ++i) {
1303 <            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1303 >            assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
1304          }
1305          assertNull(q.pollLast(0, MILLISECONDS));
1306      }
# Line 1298 | Line 1312 | public class LinkedBlockingDequeTest ext
1312          LinkedBlockingDeque q = populatedDeque(SIZE);
1313          for (int i = 0; i < SIZE; ++i) {
1314              long startTime = System.nanoTime();
1315 <            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1315 >            assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1316              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1317          }
1318          long startTime = System.nanoTime();
# Line 1312 | Line 1326 | public class LinkedBlockingDequeTest ext
1326       * returning timeout status
1327       */
1328      public void testInterruptedTimedPollLast() throws InterruptedException {
1329 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1330          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1331          Thread t = newStartedThread(new CheckedRunnable() {
1332              public void realRun() throws InterruptedException {
1333 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1333 >                long startTime = System.nanoTime();
1334                  for (int i = 0; i < SIZE; ++i) {
1335 <                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1335 >                    assertEquals(SIZE - i - 1,
1336 >                                 q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1337                  }
1338  
1339                  Thread.currentThread().interrupt();
# Line 1333 | Line 1349 | public class LinkedBlockingDequeTest ext
1349                      shouldThrow();
1350                  } catch (InterruptedException success) {}
1351                  assertFalse(Thread.interrupted());
1352 +
1353 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1354              }});
1355  
1356          await(pleaseInterrupt);
1357 <        assertThreadStaysAlive(t);
1357 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1358          t.interrupt();
1359          awaitTermination(t);
1360 +        checkEmpty(q);
1361      }
1362  
1363      /**
# Line 1371 | Line 1390 | public class LinkedBlockingDequeTest ext
1390                      shouldThrow();
1391                  } catch (InterruptedException success) {}
1392                  assertFalse(Thread.interrupted());
1393 +
1394 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1395              }});
1396  
1397          barrier.await();
# Line 1379 | Line 1400 | public class LinkedBlockingDequeTest ext
1400          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1401  
1402          barrier.await();
1403 <        assertThreadStaysAlive(t);
1403 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1404          t.interrupt();
1405          awaitTermination(t);
1406      }
# Line 1455 | Line 1476 | public class LinkedBlockingDequeTest ext
1476                  assertTrue(changed);
1477  
1478              assertTrue(q.containsAll(p));
1479 <            assertEquals(SIZE-i, q.size());
1479 >            assertEquals(SIZE - i, q.size());
1480              p.remove();
1481          }
1482      }
# Line 1468 | Line 1489 | public class LinkedBlockingDequeTest ext
1489              LinkedBlockingDeque q = populatedDeque(SIZE);
1490              LinkedBlockingDeque p = populatedDeque(i);
1491              assertTrue(q.removeAll(p));
1492 <            assertEquals(SIZE-i, q.size());
1492 >            assertEquals(SIZE - i, q.size());
1493              for (int j = 0; j < i; ++j) {
1494 <                Integer I = (Integer)(p.remove());
1495 <                assertFalse(q.contains(I));
1494 >                Integer x = (Integer)(p.remove());
1495 >                assertFalse(q.contains(x));
1496              }
1497          }
1498      }
# Line 1515 | Line 1536 | public class LinkedBlockingDequeTest ext
1536      public void testIterator() throws InterruptedException {
1537          LinkedBlockingDeque q = populatedDeque(SIZE);
1538          Iterator it = q.iterator();
1539 <        while (it.hasNext()) {
1539 >        int i;
1540 >        for (i = 0; it.hasNext(); i++)
1541 >            assertTrue(q.contains(it.next()));
1542 >        assertEquals(i, SIZE);
1543 >        assertIteratorExhausted(it);
1544 >
1545 >        it = q.iterator();
1546 >        for (i = 0; it.hasNext(); i++)
1547              assertEquals(it.next(), q.take());
1548 <        }
1548 >        assertEquals(i, SIZE);
1549 >        assertIteratorExhausted(it);
1550 >    }
1551 >
1552 >    /**
1553 >     * iterator of empty collection has no elements
1554 >     */
1555 >    public void testEmptyIterator() {
1556 >        Deque c = new LinkedBlockingDeque();
1557 >        assertIteratorExhausted(c.iterator());
1558 >        assertIteratorExhausted(c.descendingIterator());
1559      }
1560  
1561      /**
# Line 1650 | Line 1688 | public class LinkedBlockingDequeTest ext
1688          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1689          q.add(one);
1690          q.add(two);
1653        ExecutorService executor = Executors.newFixedThreadPool(2);
1691          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1692 <        executor.execute(new CheckedRunnable() {
1693 <            public void realRun() throws InterruptedException {
1694 <                assertFalse(q.offer(three));
1695 <                threadsStarted.await();
1696 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1697 <                assertEquals(0, q.remainingCapacity());
1698 <            }});
1699 <
1700 <        executor.execute(new CheckedRunnable() {
1701 <            public void realRun() throws InterruptedException {
1702 <                threadsStarted.await();
1703 <                assertSame(one, q.take());
1704 <            }});
1705 <
1706 <        joinPool(executor);
1692 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1693 >        try (PoolCleaner cleaner = cleaner(executor)) {
1694 >            executor.execute(new CheckedRunnable() {
1695 >                public void realRun() throws InterruptedException {
1696 >                    assertFalse(q.offer(three));
1697 >                    threadsStarted.await();
1698 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1699 >                    assertEquals(0, q.remainingCapacity());
1700 >                }});
1701 >
1702 >            executor.execute(new CheckedRunnable() {
1703 >                public void realRun() throws InterruptedException {
1704 >                    threadsStarted.await();
1705 >                    assertSame(one, q.take());
1706 >                }});
1707 >        }
1708      }
1709  
1710      /**
# Line 1675 | Line 1713 | public class LinkedBlockingDequeTest ext
1713      public void testPollInExecutor() {
1714          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1715          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1716 <        ExecutorService executor = Executors.newFixedThreadPool(2);
1717 <        executor.execute(new CheckedRunnable() {
1718 <            public void realRun() throws InterruptedException {
1719 <                assertNull(q.poll());
1720 <                threadsStarted.await();
1721 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1722 <                checkEmpty(q);
1723 <            }});
1724 <
1725 <        executor.execute(new CheckedRunnable() {
1726 <            public void realRun() throws InterruptedException {
1727 <                threadsStarted.await();
1728 <                q.put(one);
1729 <            }});
1730 <
1731 <        joinPool(executor);
1716 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1717 >        try (PoolCleaner cleaner = cleaner(executor)) {
1718 >            executor.execute(new CheckedRunnable() {
1719 >                public void realRun() throws InterruptedException {
1720 >                    assertNull(q.poll());
1721 >                    threadsStarted.await();
1722 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1723 >                    checkEmpty(q);
1724 >                }});
1725 >
1726 >            executor.execute(new CheckedRunnable() {
1727 >                public void realRun() throws InterruptedException {
1728 >                    threadsStarted.await();
1729 >                    q.put(one);
1730 >                }});
1731 >        }
1732      }
1733  
1734      /**
# Line 1700 | Line 1738 | public class LinkedBlockingDequeTest ext
1738          Queue x = populatedDeque(SIZE);
1739          Queue y = serialClone(x);
1740  
1741 <        assertTrue(x != y);
1741 >        assertNotSame(y, x);
1742          assertEquals(x.size(), y.size());
1743          assertEquals(x.toString(), y.toString());
1744          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
# Line 1718 | Line 1756 | public class LinkedBlockingDequeTest ext
1756          LinkedBlockingDeque q = populatedDeque(SIZE);
1757          ArrayList l = new ArrayList();
1758          q.drainTo(l);
1759 <        assertEquals(q.size(), 0);
1760 <        assertEquals(l.size(), SIZE);
1759 >        assertEquals(0, q.size());
1760 >        assertEquals(SIZE, l.size());
1761          for (int i = 0; i < SIZE; ++i)
1762              assertEquals(l.get(i), new Integer(i));
1763          q.add(zero);
# Line 1729 | Line 1767 | public class LinkedBlockingDequeTest ext
1767          assertTrue(q.contains(one));
1768          l.clear();
1769          q.drainTo(l);
1770 <        assertEquals(q.size(), 0);
1771 <        assertEquals(l.size(), 2);
1770 >        assertEquals(0, q.size());
1771 >        assertEquals(2, l.size());
1772          for (int i = 0; i < 2; ++i)
1773              assertEquals(l.get(i), new Integer(i));
1774      }
# Line 1742 | Line 1780 | public class LinkedBlockingDequeTest ext
1780          final LinkedBlockingDeque q = populatedDeque(SIZE);
1781          Thread t = new Thread(new CheckedRunnable() {
1782              public void realRun() throws InterruptedException {
1783 <                q.put(new Integer(SIZE+1));
1783 >                q.put(new Integer(SIZE + 1));
1784              }});
1785  
1786          t.start();
# Line 1766 | Line 1804 | public class LinkedBlockingDequeTest ext
1804              ArrayList l = new ArrayList();
1805              q.drainTo(l, i);
1806              int k = (i < SIZE) ? i : SIZE;
1807 <            assertEquals(l.size(), k);
1808 <            assertEquals(q.size(), SIZE-k);
1807 >            assertEquals(k, l.size());
1808 >            assertEquals(SIZE - k, q.size());
1809              for (int j = 0; j < k; ++j)
1810                  assertEquals(l.get(j), new Integer(j));
1811 <            while (q.poll() != null) ;
1811 >            do {} while (q.poll() != null);
1812 >        }
1813 >    }
1814 >
1815 >    /**
1816 >     * remove(null), contains(null) always return false
1817 >     */
1818 >    public void testNeverContainsNull() {
1819 >        Deque<?>[] qs = {
1820 >            new LinkedBlockingDeque<Object>(),
1821 >            populatedDeque(2),
1822 >        };
1823 >
1824 >        for (Deque<?> q : qs) {
1825 >            assertFalse(q.contains(null));
1826 >            assertFalse(q.remove(null));
1827 >            assertFalse(q.removeFirstOccurrence(null));
1828 >            assertFalse(q.removeLastOccurrence(null));
1829          }
1830      }
1831  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines