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.40 by jsr166, Mon May 30 22:43:20 2011 UTC vs.
Revision 1.74 by jsr166, Sat May 13 22:49:01 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;
16   import java.util.concurrent.BlockingDeque;
17   import java.util.concurrent.BlockingQueue;
18   import java.util.concurrent.CountDownLatch;
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;
23 < import java.io.*;
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() {
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 653 | Line 666 | public class LinkedBlockingDequeTest ext
666              }});
667  
668          await(pleaseInterrupt);
669 <        assertThreadStaysAlive(t);
669 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
670          t.interrupt();
671          awaitTermination(t);
672      }
# Line 676 | Line 689 | public class LinkedBlockingDequeTest ext
689          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
690          Thread t = newStartedThread(new CheckedRunnable() {
691              public void realRun() throws InterruptedException {
692 <                for (int i = 0; i < SIZE; ++i) {
680 <                    assertEquals(i, q.take());
681 <                }
692 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
693  
694                  Thread.currentThread().interrupt();
695                  try {
# Line 696 | Line 707 | public class LinkedBlockingDequeTest ext
707              }});
708  
709          await(pleaseInterrupt);
710 <        assertThreadStaysAlive(t);
710 >        assertThreadBlocks(t, Thread.State.WAITING);
711          t.interrupt();
712          awaitTermination(t);
713      }
# Line 748 | Line 759 | public class LinkedBlockingDequeTest ext
759          final CountDownLatch aboutToWait = new CountDownLatch(1);
760          Thread t = newStartedThread(new CheckedRunnable() {
761              public void realRun() throws InterruptedException {
762 +                long startTime = System.nanoTime();
763                  for (int i = 0; i < SIZE; ++i) {
752                    long t0 = System.nanoTime();
764                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
754                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
765                  }
756                long t0 = System.nanoTime();
766                  aboutToWait.countDown();
767                  try {
768 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
768 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
769                      shouldThrow();
770                  } catch (InterruptedException success) {
771 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
771 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
772                  }
773              }});
774  
775 <        aboutToWait.await();
776 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
775 >        await(aboutToWait);
776 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
777          t.interrupt();
778 <        awaitTermination(t, MEDIUM_DELAY_MS);
778 >        awaitTermination(t);
779          checkEmpty(q);
780      }
781  
# Line 787 | Line 796 | public class LinkedBlockingDequeTest ext
796      public void testPutFirst() throws InterruptedException {
797          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
798          for (int i = 0; i < SIZE; ++i) {
799 <            Integer I = new Integer(i);
800 <            q.putFirst(I);
801 <            assertTrue(q.contains(I));
799 >            Integer x = new Integer(i);
800 >            q.putFirst(x);
801 >            assertTrue(q.contains(x));
802          }
803          assertEquals(0, q.remainingCapacity());
804      }
# Line 823 | Line 832 | public class LinkedBlockingDequeTest ext
832              }});
833  
834          await(pleaseInterrupt);
835 <        assertThreadStaysAlive(t);
835 >        assertThreadBlocks(t, Thread.State.WAITING);
836          t.interrupt();
837          awaitTermination(t);
838          assertEquals(SIZE, q.size());
# Line 854 | Line 863 | public class LinkedBlockingDequeTest ext
863              }});
864  
865          await(pleaseTake);
866 <        assertEquals(q.remainingCapacity(), 0);
866 >        assertEquals(0, q.remainingCapacity());
867          assertEquals(capacity - 1, q.take());
868  
869          await(pleaseInterrupt);
870 <        assertThreadStaysAlive(t);
870 >        assertThreadBlocks(t, Thread.State.WAITING);
871          t.interrupt();
872          awaitTermination(t);
873 <        assertEquals(q.remainingCapacity(), 0);
873 >        assertEquals(0, q.remainingCapacity());
874      }
875  
876      /**
# Line 885 | Line 894 | public class LinkedBlockingDequeTest ext
894              }});
895  
896          await(pleaseInterrupt);
897 <        assertThreadStaysAlive(t);
897 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
898          t.interrupt();
899          awaitTermination(t);
900      }
# Line 917 | Line 926 | public class LinkedBlockingDequeTest ext
926              }});
927  
928          await(threadStarted);
929 <        assertThreadStaysAlive(t);
929 >        assertThreadBlocks(t, Thread.State.WAITING);
930          t.interrupt();
931          awaitTermination(t);
932      }
# Line 958 | Line 967 | public class LinkedBlockingDequeTest ext
967              }});
968  
969          await(threadStarted);
970 <        assertThreadStaysAlive(t);
970 >        assertThreadBlocks(t, Thread.State.WAITING);
971          t.interrupt();
972          awaitTermination(t);
973      }
# Line 990 | Line 999 | public class LinkedBlockingDequeTest ext
999          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1000          Thread t = newStartedThread(new CheckedRunnable() {
1001              public void realRun() throws InterruptedException {
1002 <                for (int i = 0; i < SIZE; ++i) {
994 <                    assertEquals(i, q.takeFirst());
995 <                }
1002 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst());
1003  
1004                  Thread.currentThread().interrupt();
1005                  try {
# Line 1010 | Line 1017 | public class LinkedBlockingDequeTest ext
1017              }});
1018  
1019          await(pleaseInterrupt);
1020 <        assertThreadStaysAlive(t);
1020 >        assertThreadBlocks(t, Thread.State.WAITING);
1021          t.interrupt();
1022          awaitTermination(t);
1023      }
# Line 1047 | Line 1054 | public class LinkedBlockingDequeTest ext
1054       * returning timeout status
1055       */
1056      public void testInterruptedTimedPollFirst() throws InterruptedException {
1057 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1058          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1059          Thread t = newStartedThread(new CheckedRunnable() {
1060              public void realRun() throws InterruptedException {
1061 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1061 >                long startTime = System.nanoTime();
1062                  for (int i = 0; i < SIZE; ++i) {
1063                      assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1064                  }
1065  
1066                  Thread.currentThread().interrupt();
1067                  try {
1068 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1068 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1069                      shouldThrow();
1070                  } catch (InterruptedException success) {}
1071                  assertFalse(Thread.interrupted());
1072  
1073                  pleaseInterrupt.countDown();
1074                  try {
1075 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1075 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1076                      shouldThrow();
1077                  } catch (InterruptedException success) {}
1078                  assertFalse(Thread.interrupted());
1079 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1080              }});
1081  
1082          await(pleaseInterrupt);
1083 <        assertThreadStaysAlive(t);
1083 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1084          t.interrupt();
1085          awaitTermination(t);
1086      }
# Line 1134 | Line 1143 | public class LinkedBlockingDequeTest ext
1143      public void testPutLast() throws InterruptedException {
1144          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1145          for (int i = 0; i < SIZE; ++i) {
1146 <            Integer I = new Integer(i);
1147 <            q.putLast(I);
1148 <            assertTrue(q.contains(I));
1146 >            Integer x = new Integer(i);
1147 >            q.putLast(x);
1148 >            assertTrue(q.contains(x));
1149          }
1150          assertEquals(0, q.remainingCapacity());
1151      }
# Line 1170 | Line 1179 | public class LinkedBlockingDequeTest ext
1179              }});
1180  
1181          await(pleaseInterrupt);
1182 <        assertThreadStaysAlive(t);
1182 >        assertThreadBlocks(t, Thread.State.WAITING);
1183          t.interrupt();
1184          awaitTermination(t);
1185          assertEquals(SIZE, q.size());
# Line 1201 | Line 1210 | public class LinkedBlockingDequeTest ext
1210              }});
1211  
1212          await(pleaseTake);
1213 <        assertEquals(q.remainingCapacity(), 0);
1213 >        assertEquals(0, q.remainingCapacity());
1214          assertEquals(0, q.take());
1215  
1216          await(pleaseInterrupt);
1217 <        assertThreadStaysAlive(t);
1217 >        assertThreadBlocks(t, Thread.State.WAITING);
1218          t.interrupt();
1219          awaitTermination(t);
1220 <        assertEquals(q.remainingCapacity(), 0);
1220 >        assertEquals(0, q.remainingCapacity());
1221      }
1222  
1223      /**
# Line 1232 | Line 1241 | public class LinkedBlockingDequeTest ext
1241              }});
1242  
1243          await(pleaseInterrupt);
1244 <        assertThreadStaysAlive(t);
1244 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1245          t.interrupt();
1246          awaitTermination(t);
1247      }
# Line 1243 | Line 1252 | public class LinkedBlockingDequeTest ext
1252      public void testTakeLast() throws InterruptedException {
1253          LinkedBlockingDeque q = populatedDeque(SIZE);
1254          for (int i = 0; i < SIZE; ++i) {
1255 <            assertEquals(SIZE-i-1, q.takeLast());
1255 >            assertEquals(SIZE - i - 1, q.takeLast());
1256          }
1257      }
1258  
# Line 1255 | Line 1264 | public class LinkedBlockingDequeTest ext
1264          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1265          Thread t = newStartedThread(new CheckedRunnable() {
1266              public void realRun() throws InterruptedException {
1267 <                for (int i = 0; i < SIZE; ++i) {
1268 <                    assertEquals(SIZE-i-1, q.takeLast());
1260 <                }
1267 >                for (int i = 0; i < SIZE; i++)
1268 >                    assertEquals(SIZE - i - 1, q.takeLast());
1269  
1270                  Thread.currentThread().interrupt();
1271                  try {
# Line 1275 | Line 1283 | public class LinkedBlockingDequeTest ext
1283              }});
1284  
1285          await(pleaseInterrupt);
1286 <        assertThreadStaysAlive(t);
1286 >        assertThreadBlocks(t, Thread.State.WAITING);
1287          t.interrupt();
1288          awaitTermination(t);
1289      }
# Line 1286 | Line 1294 | public class LinkedBlockingDequeTest ext
1294      public void testTimedPollLast0() throws InterruptedException {
1295          LinkedBlockingDeque q = populatedDeque(SIZE);
1296          for (int i = 0; i < SIZE; ++i) {
1297 <            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1297 >            assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
1298          }
1299          assertNull(q.pollLast(0, MILLISECONDS));
1300      }
# Line 1298 | Line 1306 | public class LinkedBlockingDequeTest ext
1306          LinkedBlockingDeque q = populatedDeque(SIZE);
1307          for (int i = 0; i < SIZE; ++i) {
1308              long startTime = System.nanoTime();
1309 <            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1309 >            assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1310              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1311          }
1312          long startTime = System.nanoTime();
# Line 1312 | Line 1320 | public class LinkedBlockingDequeTest ext
1320       * returning timeout status
1321       */
1322      public void testInterruptedTimedPollLast() throws InterruptedException {
1323 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1324          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1325          Thread t = newStartedThread(new CheckedRunnable() {
1326              public void realRun() throws InterruptedException {
1327 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1327 >                long startTime = System.nanoTime();
1328                  for (int i = 0; i < SIZE; ++i) {
1329 <                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1329 >                    assertEquals(SIZE - i - 1,
1330 >                                 q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1331                  }
1332  
1333                  Thread.currentThread().interrupt();
# Line 1333 | Line 1343 | public class LinkedBlockingDequeTest ext
1343                      shouldThrow();
1344                  } catch (InterruptedException success) {}
1345                  assertFalse(Thread.interrupted());
1346 +
1347 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1348              }});
1349  
1350          await(pleaseInterrupt);
1351 <        assertThreadStaysAlive(t);
1351 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1352          t.interrupt();
1353          awaitTermination(t);
1354 +        checkEmpty(q);
1355      }
1356  
1357      /**
# Line 1371 | Line 1384 | public class LinkedBlockingDequeTest ext
1384                      shouldThrow();
1385                  } catch (InterruptedException success) {}
1386                  assertFalse(Thread.interrupted());
1387 +
1388 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1389              }});
1390  
1391          barrier.await();
# Line 1379 | Line 1394 | public class LinkedBlockingDequeTest ext
1394          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1395  
1396          barrier.await();
1397 <        assertThreadStaysAlive(t);
1397 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1398          t.interrupt();
1399          awaitTermination(t);
1400      }
# Line 1400 | Line 1415 | public class LinkedBlockingDequeTest ext
1415      }
1416  
1417      /**
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    /**
1418       * contains(x) reports true when elements added but not yet removed
1419       */
1420      public void testContains() {
# Line 1476 | Line 1470 | public class LinkedBlockingDequeTest ext
1470                  assertTrue(changed);
1471  
1472              assertTrue(q.containsAll(p));
1473 <            assertEquals(SIZE-i, q.size());
1473 >            assertEquals(SIZE - i, q.size());
1474              p.remove();
1475          }
1476      }
# Line 1489 | Line 1483 | public class LinkedBlockingDequeTest ext
1483              LinkedBlockingDeque q = populatedDeque(SIZE);
1484              LinkedBlockingDeque p = populatedDeque(i);
1485              assertTrue(q.removeAll(p));
1486 <            assertEquals(SIZE-i, q.size());
1486 >            assertEquals(SIZE - i, q.size());
1487              for (int j = 0; j < i; ++j) {
1488 <                Integer I = (Integer)(p.remove());
1489 <                assertFalse(q.contains(I));
1488 >                Integer x = (Integer)(p.remove());
1489 >                assertFalse(q.contains(x));
1490              }
1491          }
1492      }
# Line 1500 | Line 1494 | public class LinkedBlockingDequeTest ext
1494      /**
1495       * toArray contains all elements in FIFO order
1496       */
1497 <    public void testToArray() throws InterruptedException{
1497 >    public void testToArray() throws InterruptedException {
1498          LinkedBlockingDeque q = populatedDeque(SIZE);
1499          Object[] o = q.toArray();
1500          for (int i = 0; i < o.length; i++)
# Line 1536 | Line 1530 | public class LinkedBlockingDequeTest ext
1530      public void testIterator() throws InterruptedException {
1531          LinkedBlockingDeque q = populatedDeque(SIZE);
1532          Iterator it = q.iterator();
1533 <        while (it.hasNext()) {
1533 >        int i;
1534 >        for (i = 0; it.hasNext(); i++)
1535 >            assertTrue(q.contains(it.next()));
1536 >        assertEquals(i, SIZE);
1537 >        assertIteratorExhausted(it);
1538 >
1539 >        it = q.iterator();
1540 >        for (i = 0; it.hasNext(); i++)
1541              assertEquals(it.next(), q.take());
1542 <        }
1542 >        assertEquals(i, SIZE);
1543 >        assertIteratorExhausted(it);
1544 >    }
1545 >
1546 >    /**
1547 >     * iterator of empty collection has no elements
1548 >     */
1549 >    public void testEmptyIterator() {
1550 >        Deque c = new LinkedBlockingDeque();
1551 >        assertIteratorExhausted(c.iterator());
1552 >        assertIteratorExhausted(c.descendingIterator());
1553      }
1554  
1555      /**
# Line 1671 | Line 1682 | public class LinkedBlockingDequeTest ext
1682          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1683          q.add(one);
1684          q.add(two);
1674        ExecutorService executor = Executors.newFixedThreadPool(2);
1685          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1686 <        executor.execute(new CheckedRunnable() {
1687 <            public void realRun() throws InterruptedException {
1688 <                assertFalse(q.offer(three));
1689 <                threadsStarted.await();
1690 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1691 <                assertEquals(0, q.remainingCapacity());
1692 <            }});
1693 <
1694 <        executor.execute(new CheckedRunnable() {
1695 <            public void realRun() throws InterruptedException {
1696 <                threadsStarted.await();
1697 <                assertSame(one, q.take());
1698 <            }});
1699 <
1700 <        joinPool(executor);
1686 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1687 >        try (PoolCleaner cleaner = cleaner(executor)) {
1688 >            executor.execute(new CheckedRunnable() {
1689 >                public void realRun() throws InterruptedException {
1690 >                    assertFalse(q.offer(three));
1691 >                    threadsStarted.await();
1692 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1693 >                    assertEquals(0, q.remainingCapacity());
1694 >                }});
1695 >
1696 >            executor.execute(new CheckedRunnable() {
1697 >                public void realRun() throws InterruptedException {
1698 >                    threadsStarted.await();
1699 >                    assertSame(one, q.take());
1700 >                }});
1701 >        }
1702      }
1703  
1704      /**
# Line 1696 | Line 1707 | public class LinkedBlockingDequeTest ext
1707      public void testPollInExecutor() {
1708          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1709          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1710 <        ExecutorService executor = Executors.newFixedThreadPool(2);
1711 <        executor.execute(new CheckedRunnable() {
1712 <            public void realRun() throws InterruptedException {
1713 <                assertNull(q.poll());
1714 <                threadsStarted.await();
1715 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1716 <                checkEmpty(q);
1717 <            }});
1718 <
1719 <        executor.execute(new CheckedRunnable() {
1720 <            public void realRun() throws InterruptedException {
1721 <                threadsStarted.await();
1722 <                q.put(one);
1723 <            }});
1724 <
1725 <        joinPool(executor);
1710 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1711 >        try (PoolCleaner cleaner = cleaner(executor)) {
1712 >            executor.execute(new CheckedRunnable() {
1713 >                public void realRun() throws InterruptedException {
1714 >                    assertNull(q.poll());
1715 >                    threadsStarted.await();
1716 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1717 >                    checkEmpty(q);
1718 >                }});
1719 >
1720 >            executor.execute(new CheckedRunnable() {
1721 >                public void realRun() throws InterruptedException {
1722 >                    threadsStarted.await();
1723 >                    q.put(one);
1724 >                }});
1725 >        }
1726      }
1727  
1728      /**
1729       * A deserialized serialized deque has same elements in same order
1730       */
1731      public void testSerialization() throws Exception {
1732 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1732 >        Queue x = populatedDeque(SIZE);
1733 >        Queue y = serialClone(x);
1734  
1735 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1736 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1737 <        out.writeObject(q);
1738 <        out.close();
1739 <
1740 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1741 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1742 <        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1743 <        assertEquals(q.size(), r.size());
1732 <        while (!q.isEmpty())
1733 <            assertEquals(q.remove(), r.remove());
1735 >        assertNotSame(y, x);
1736 >        assertEquals(x.size(), y.size());
1737 >        assertEquals(x.toString(), y.toString());
1738 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1739 >        while (!x.isEmpty()) {
1740 >            assertFalse(y.isEmpty());
1741 >            assertEquals(x.remove(), y.remove());
1742 >        }
1743 >        assertTrue(y.isEmpty());
1744      }
1745  
1746      /**
# Line 1740 | Line 1750 | public class LinkedBlockingDequeTest ext
1750          LinkedBlockingDeque q = populatedDeque(SIZE);
1751          ArrayList l = new ArrayList();
1752          q.drainTo(l);
1753 <        assertEquals(q.size(), 0);
1754 <        assertEquals(l.size(), SIZE);
1753 >        assertEquals(0, q.size());
1754 >        assertEquals(SIZE, l.size());
1755          for (int i = 0; i < SIZE; ++i)
1756              assertEquals(l.get(i), new Integer(i));
1757          q.add(zero);
# Line 1751 | Line 1761 | public class LinkedBlockingDequeTest ext
1761          assertTrue(q.contains(one));
1762          l.clear();
1763          q.drainTo(l);
1764 <        assertEquals(q.size(), 0);
1765 <        assertEquals(l.size(), 2);
1764 >        assertEquals(0, q.size());
1765 >        assertEquals(2, l.size());
1766          for (int i = 0; i < 2; ++i)
1767              assertEquals(l.get(i), new Integer(i));
1768      }
# Line 1764 | Line 1774 | public class LinkedBlockingDequeTest ext
1774          final LinkedBlockingDeque q = populatedDeque(SIZE);
1775          Thread t = new Thread(new CheckedRunnable() {
1776              public void realRun() throws InterruptedException {
1777 <                q.put(new Integer(SIZE+1));
1777 >                q.put(new Integer(SIZE + 1));
1778              }});
1779  
1780          t.start();
# Line 1788 | Line 1798 | public class LinkedBlockingDequeTest ext
1798              ArrayList l = new ArrayList();
1799              q.drainTo(l, i);
1800              int k = (i < SIZE) ? i : SIZE;
1801 <            assertEquals(l.size(), k);
1802 <            assertEquals(q.size(), SIZE-k);
1801 >            assertEquals(k, l.size());
1802 >            assertEquals(SIZE - k, q.size());
1803              for (int j = 0; j < k; ++j)
1804                  assertEquals(l.get(j), new Integer(j));
1805 <            while (q.poll() != null) ;
1805 >            do {} while (q.poll() != null);
1806 >        }
1807 >    }
1808 >
1809 >    /**
1810 >     * remove(null), contains(null) always return false
1811 >     */
1812 >    public void testNeverContainsNull() {
1813 >        Deque<?>[] qs = {
1814 >            new LinkedBlockingDeque<Object>(),
1815 >            populatedDeque(2),
1816 >        };
1817 >
1818 >        for (Deque<?> q : qs) {
1819 >            assertFalse(q.contains(null));
1820 >            assertFalse(q.remove(null));
1821 >            assertFalse(q.removeFirstOccurrence(null));
1822 >            assertFalse(q.removeLastOccurrence(null));
1823          }
1824      }
1825  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines