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.79 by jsr166, Sun May 14 04:02:06 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 613 | Line 626 | public class LinkedBlockingDequeTest ext
626                  pleaseTake.countDown();
627                  q.put(86);
628  
629 +                Thread.currentThread().interrupt();
630 +                try {
631 +                    q.put(99);
632 +                    shouldThrow();
633 +                } catch (InterruptedException success) {}
634 +                assertFalse(Thread.interrupted());
635 +
636                  pleaseInterrupt.countDown();
637                  try {
638                      q.put(99);
# Line 622 | Line 642 | public class LinkedBlockingDequeTest ext
642              }});
643  
644          await(pleaseTake);
645 <        assertEquals(q.remainingCapacity(), 0);
645 >        assertEquals(0, q.remainingCapacity());
646          assertEquals(0, q.take());
647  
648          await(pleaseInterrupt);
649 <        assertThreadStaysAlive(t);
649 >        assertThreadBlocks(t, Thread.State.WAITING);
650          t.interrupt();
651          awaitTermination(t);
652 <        assertEquals(q.remainingCapacity(), 0);
652 >        assertEquals(0, q.remainingCapacity());
653      }
654  
655      /**
# Line 650 | Line 670 | public class LinkedBlockingDequeTest ext
670                      q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
671                      shouldThrow();
672                  } catch (InterruptedException success) {}
673 +                assertFalse(Thread.interrupted());
674              }});
675  
676          await(pleaseInterrupt);
677 <        assertThreadStaysAlive(t);
677 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
678          t.interrupt();
679          awaitTermination(t);
680      }
# Line 676 | Line 697 | public class LinkedBlockingDequeTest ext
697          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
698          Thread t = newStartedThread(new CheckedRunnable() {
699              public void realRun() throws InterruptedException {
700 <                for (int i = 0; i < SIZE; ++i) {
680 <                    assertEquals(i, q.take());
681 <                }
700 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
701  
702                  Thread.currentThread().interrupt();
703                  try {
# Line 696 | Line 715 | public class LinkedBlockingDequeTest ext
715              }});
716  
717          await(pleaseInterrupt);
718 <        assertThreadStaysAlive(t);
718 >        assertThreadBlocks(t, Thread.State.WAITING);
719          t.interrupt();
720          awaitTermination(t);
721      }
# Line 745 | Line 764 | public class LinkedBlockingDequeTest ext
764       */
765      public void testInterruptedTimedPoll() throws InterruptedException {
766          final BlockingQueue<Integer> q = populatedDeque(SIZE);
767 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
767 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
768          Thread t = newStartedThread(new CheckedRunnable() {
769              public void realRun() throws InterruptedException {
770 <                for (int i = 0; i < SIZE; ++i) {
771 <                    long t0 = System.nanoTime();
770 >                long startTime = System.nanoTime();
771 >                for (int i = 0; i < SIZE; i++)
772                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
773 <                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
774 <                }
775 <                long t0 = System.nanoTime();
776 <                aboutToWait.countDown();
773 >
774 >                Thread.currentThread().interrupt();
775 >                try {
776 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
777 >                    shouldThrow();
778 >                } catch (InterruptedException success) {}
779 >                assertFalse(Thread.interrupted());
780 >
781 >                pleaseInterrupt.countDown();
782                  try {
783 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
783 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
784                      shouldThrow();
785 <                } catch (InterruptedException success) {
786 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
787 <                }
785 >                } catch (InterruptedException success) {}
786 >                assertFalse(Thread.interrupted());
787 >
788 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
789              }});
790  
791 <        aboutToWait.await();
792 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
791 >        await(pleaseInterrupt);
792 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
793          t.interrupt();
794 <        awaitTermination(t, MEDIUM_DELAY_MS);
794 >        awaitTermination(t);
795          checkEmpty(q);
796      }
797  
# Line 787 | Line 812 | public class LinkedBlockingDequeTest ext
812      public void testPutFirst() throws InterruptedException {
813          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
814          for (int i = 0; i < SIZE; ++i) {
815 <            Integer I = new Integer(i);
816 <            q.putFirst(I);
817 <            assertTrue(q.contains(I));
815 >            Integer x = new Integer(i);
816 >            q.putFirst(x);
817 >            assertTrue(q.contains(x));
818          }
819          assertEquals(0, q.remainingCapacity());
820      }
# Line 823 | Line 848 | public class LinkedBlockingDequeTest ext
848              }});
849  
850          await(pleaseInterrupt);
851 <        assertThreadStaysAlive(t);
851 >        assertThreadBlocks(t, Thread.State.WAITING);
852          t.interrupt();
853          awaitTermination(t);
854          assertEquals(SIZE, q.size());
# Line 854 | Line 879 | public class LinkedBlockingDequeTest ext
879              }});
880  
881          await(pleaseTake);
882 <        assertEquals(q.remainingCapacity(), 0);
882 >        assertEquals(0, q.remainingCapacity());
883          assertEquals(capacity - 1, q.take());
884  
885          await(pleaseInterrupt);
886 <        assertThreadStaysAlive(t);
886 >        assertThreadBlocks(t, Thread.State.WAITING);
887          t.interrupt();
888          awaitTermination(t);
889 <        assertEquals(q.remainingCapacity(), 0);
889 >        assertEquals(0, q.remainingCapacity());
890      }
891  
892      /**
# Line 882 | Line 907 | public class LinkedBlockingDequeTest ext
907                      q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
908                      shouldThrow();
909                  } catch (InterruptedException success) {}
910 +                assertFalse(Thread.interrupted());
911              }});
912  
913          await(pleaseInterrupt);
914 <        assertThreadStaysAlive(t);
914 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
915          t.interrupt();
916          awaitTermination(t);
917      }
# Line 917 | Line 943 | public class LinkedBlockingDequeTest ext
943              }});
944  
945          await(threadStarted);
946 <        assertThreadStaysAlive(t);
946 >        assertThreadBlocks(t, Thread.State.WAITING);
947          t.interrupt();
948          awaitTermination(t);
949      }
# Line 958 | Line 984 | public class LinkedBlockingDequeTest ext
984              }});
985  
986          await(threadStarted);
987 <        assertThreadStaysAlive(t);
987 >        assertThreadBlocks(t, Thread.State.WAITING);
988          t.interrupt();
989          awaitTermination(t);
990      }
# Line 990 | Line 1016 | public class LinkedBlockingDequeTest ext
1016          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1017          Thread t = newStartedThread(new CheckedRunnable() {
1018              public void realRun() throws InterruptedException {
1019 <                for (int i = 0; i < SIZE; ++i) {
994 <                    assertEquals(i, q.takeFirst());
995 <                }
1019 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst());
1020  
1021                  Thread.currentThread().interrupt();
1022                  try {
# Line 1010 | Line 1034 | public class LinkedBlockingDequeTest ext
1034              }});
1035  
1036          await(pleaseInterrupt);
1037 <        assertThreadStaysAlive(t);
1037 >        assertThreadBlocks(t, Thread.State.WAITING);
1038          t.interrupt();
1039          awaitTermination(t);
1040      }
# Line 1047 | Line 1071 | public class LinkedBlockingDequeTest ext
1071       * returning timeout status
1072       */
1073      public void testInterruptedTimedPollFirst() throws InterruptedException {
1074 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1075          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1076          Thread t = newStartedThread(new CheckedRunnable() {
1077              public void realRun() throws InterruptedException {
1078 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1079 <                for (int i = 0; i < SIZE; ++i) {
1078 >                long startTime = System.nanoTime();
1079 >                for (int i = 0; i < SIZE; i++)
1080                      assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1056                }
1081  
1082                  Thread.currentThread().interrupt();
1083                  try {
1084 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1084 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1085                      shouldThrow();
1086                  } catch (InterruptedException success) {}
1087                  assertFalse(Thread.interrupted());
1088  
1089                  pleaseInterrupt.countDown();
1090                  try {
1091 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1091 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1092                      shouldThrow();
1093                  } catch (InterruptedException success) {}
1094                  assertFalse(Thread.interrupted());
1095 +
1096 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1097              }});
1098  
1099          await(pleaseInterrupt);
1100 <        assertThreadStaysAlive(t);
1100 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1101          t.interrupt();
1102          awaitTermination(t);
1103      }
# Line 1104 | Line 1130 | public class LinkedBlockingDequeTest ext
1130                      q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1131                      shouldThrow();
1132                  } catch (InterruptedException success) {}
1133 +                assertFalse(Thread.interrupted());
1134                  assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1135              }});
1136  
# Line 1112 | Line 1139 | public class LinkedBlockingDequeTest ext
1139          assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1140          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1141          barrier.await();
1142 <        assertThreadStaysAlive(t);
1142 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1143          t.interrupt();
1144          awaitTermination(t);
1145      }
# Line 1134 | Line 1161 | public class LinkedBlockingDequeTest ext
1161      public void testPutLast() throws InterruptedException {
1162          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1163          for (int i = 0; i < SIZE; ++i) {
1164 <            Integer I = new Integer(i);
1165 <            q.putLast(I);
1166 <            assertTrue(q.contains(I));
1164 >            Integer x = new Integer(i);
1165 >            q.putLast(x);
1166 >            assertTrue(q.contains(x));
1167          }
1168          assertEquals(0, q.remainingCapacity());
1169      }
# Line 1170 | Line 1197 | public class LinkedBlockingDequeTest ext
1197              }});
1198  
1199          await(pleaseInterrupt);
1200 <        assertThreadStaysAlive(t);
1200 >        assertThreadBlocks(t, Thread.State.WAITING);
1201          t.interrupt();
1202          awaitTermination(t);
1203          assertEquals(SIZE, q.size());
# Line 1201 | Line 1228 | public class LinkedBlockingDequeTest ext
1228              }});
1229  
1230          await(pleaseTake);
1231 <        assertEquals(q.remainingCapacity(), 0);
1231 >        assertEquals(0, q.remainingCapacity());
1232          assertEquals(0, q.take());
1233  
1234          await(pleaseInterrupt);
1235 <        assertThreadStaysAlive(t);
1235 >        assertThreadBlocks(t, Thread.State.WAITING);
1236          t.interrupt();
1237          awaitTermination(t);
1238 <        assertEquals(q.remainingCapacity(), 0);
1238 >        assertEquals(0, q.remainingCapacity());
1239      }
1240  
1241      /**
# Line 1232 | Line 1259 | public class LinkedBlockingDequeTest ext
1259              }});
1260  
1261          await(pleaseInterrupt);
1262 <        assertThreadStaysAlive(t);
1262 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1263          t.interrupt();
1264          awaitTermination(t);
1265      }
# Line 1243 | Line 1270 | public class LinkedBlockingDequeTest ext
1270      public void testTakeLast() throws InterruptedException {
1271          LinkedBlockingDeque q = populatedDeque(SIZE);
1272          for (int i = 0; i < SIZE; ++i) {
1273 <            assertEquals(SIZE-i-1, q.takeLast());
1273 >            assertEquals(SIZE - i - 1, q.takeLast());
1274          }
1275      }
1276  
# Line 1255 | Line 1282 | public class LinkedBlockingDequeTest ext
1282          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1283          Thread t = newStartedThread(new CheckedRunnable() {
1284              public void realRun() throws InterruptedException {
1285 <                for (int i = 0; i < SIZE; ++i) {
1286 <                    assertEquals(SIZE-i-1, q.takeLast());
1260 <                }
1285 >                for (int i = 0; i < SIZE; i++)
1286 >                    assertEquals(SIZE - i - 1, q.takeLast());
1287  
1288                  Thread.currentThread().interrupt();
1289                  try {
# Line 1275 | Line 1301 | public class LinkedBlockingDequeTest ext
1301              }});
1302  
1303          await(pleaseInterrupt);
1304 <        assertThreadStaysAlive(t);
1304 >        assertThreadBlocks(t, Thread.State.WAITING);
1305          t.interrupt();
1306          awaitTermination(t);
1307      }
# Line 1286 | Line 1312 | public class LinkedBlockingDequeTest ext
1312      public void testTimedPollLast0() throws InterruptedException {
1313          LinkedBlockingDeque q = populatedDeque(SIZE);
1314          for (int i = 0; i < SIZE; ++i) {
1315 <            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1315 >            assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
1316          }
1317          assertNull(q.pollLast(0, MILLISECONDS));
1318      }
# Line 1298 | Line 1324 | public class LinkedBlockingDequeTest ext
1324          LinkedBlockingDeque q = populatedDeque(SIZE);
1325          for (int i = 0; i < SIZE; ++i) {
1326              long startTime = System.nanoTime();
1327 <            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1327 >            assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1328              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1329          }
1330          long startTime = System.nanoTime();
# Line 1312 | Line 1338 | public class LinkedBlockingDequeTest ext
1338       * returning timeout status
1339       */
1340      public void testInterruptedTimedPollLast() throws InterruptedException {
1341 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1342          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1343          Thread t = newStartedThread(new CheckedRunnable() {
1344              public void realRun() throws InterruptedException {
1345 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1346 <                for (int i = 0; i < SIZE; ++i) {
1347 <                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1348 <                }
1345 >                long startTime = System.nanoTime();
1346 >                for (int i = 0; i < SIZE; i++)
1347 >                    assertEquals(SIZE - i - 1,
1348 >                                 q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1349  
1350                  Thread.currentThread().interrupt();
1351                  try {
# Line 1333 | Line 1360 | public class LinkedBlockingDequeTest ext
1360                      shouldThrow();
1361                  } catch (InterruptedException success) {}
1362                  assertFalse(Thread.interrupted());
1363 +
1364 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1365              }});
1366  
1367          await(pleaseInterrupt);
1368 <        assertThreadStaysAlive(t);
1368 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1369          t.interrupt();
1370          awaitTermination(t);
1371 +        checkEmpty(q);
1372      }
1373  
1374      /**
# Line 1371 | Line 1401 | public class LinkedBlockingDequeTest ext
1401                      shouldThrow();
1402                  } catch (InterruptedException success) {}
1403                  assertFalse(Thread.interrupted());
1404 +
1405 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1406              }});
1407  
1408          barrier.await();
# Line 1379 | Line 1411 | public class LinkedBlockingDequeTest ext
1411          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1412  
1413          barrier.await();
1414 <        assertThreadStaysAlive(t);
1414 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1415          t.interrupt();
1416          awaitTermination(t);
1417      }
# Line 1400 | Line 1432 | public class LinkedBlockingDequeTest ext
1432      }
1433  
1434      /**
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    /**
1435       * contains(x) reports true when elements added but not yet removed
1436       */
1437      public void testContains() {
# Line 1476 | Line 1487 | public class LinkedBlockingDequeTest ext
1487                  assertTrue(changed);
1488  
1489              assertTrue(q.containsAll(p));
1490 <            assertEquals(SIZE-i, q.size());
1490 >            assertEquals(SIZE - i, q.size());
1491              p.remove();
1492          }
1493      }
# Line 1489 | Line 1500 | public class LinkedBlockingDequeTest ext
1500              LinkedBlockingDeque q = populatedDeque(SIZE);
1501              LinkedBlockingDeque p = populatedDeque(i);
1502              assertTrue(q.removeAll(p));
1503 <            assertEquals(SIZE-i, q.size());
1503 >            assertEquals(SIZE - i, q.size());
1504              for (int j = 0; j < i; ++j) {
1505 <                Integer I = (Integer)(p.remove());
1506 <                assertFalse(q.contains(I));
1505 >                Integer x = (Integer)(p.remove());
1506 >                assertFalse(q.contains(x));
1507              }
1508          }
1509      }
# Line 1500 | Line 1511 | public class LinkedBlockingDequeTest ext
1511      /**
1512       * toArray contains all elements in FIFO order
1513       */
1514 <    public void testToArray() throws InterruptedException{
1514 >    public void testToArray() throws InterruptedException {
1515          LinkedBlockingDeque q = populatedDeque(SIZE);
1516          Object[] o = q.toArray();
1517          for (int i = 0; i < o.length; i++)
# Line 1536 | Line 1547 | public class LinkedBlockingDequeTest ext
1547      public void testIterator() throws InterruptedException {
1548          LinkedBlockingDeque q = populatedDeque(SIZE);
1549          Iterator it = q.iterator();
1550 <        while (it.hasNext()) {
1550 >        int i;
1551 >        for (i = 0; it.hasNext(); i++)
1552 >            assertTrue(q.contains(it.next()));
1553 >        assertEquals(i, SIZE);
1554 >        assertIteratorExhausted(it);
1555 >
1556 >        it = q.iterator();
1557 >        for (i = 0; it.hasNext(); i++)
1558              assertEquals(it.next(), q.take());
1559 <        }
1559 >        assertEquals(i, SIZE);
1560 >        assertIteratorExhausted(it);
1561 >    }
1562 >
1563 >    /**
1564 >     * iterator of empty collection has no elements
1565 >     */
1566 >    public void testEmptyIterator() {
1567 >        Deque c = new LinkedBlockingDeque();
1568 >        assertIteratorExhausted(c.iterator());
1569 >        assertIteratorExhausted(c.descendingIterator());
1570      }
1571  
1572      /**
# Line 1671 | Line 1699 | public class LinkedBlockingDequeTest ext
1699          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1700          q.add(one);
1701          q.add(two);
1674        ExecutorService executor = Executors.newFixedThreadPool(2);
1702          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1703 <        executor.execute(new CheckedRunnable() {
1704 <            public void realRun() throws InterruptedException {
1705 <                assertFalse(q.offer(three));
1706 <                threadsStarted.await();
1707 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1708 <                assertEquals(0, q.remainingCapacity());
1709 <            }});
1710 <
1711 <        executor.execute(new CheckedRunnable() {
1712 <            public void realRun() throws InterruptedException {
1713 <                threadsStarted.await();
1714 <                assertSame(one, q.take());
1715 <            }});
1716 <
1717 <        joinPool(executor);
1703 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1704 >        try (PoolCleaner cleaner = cleaner(executor)) {
1705 >            executor.execute(new CheckedRunnable() {
1706 >                public void realRun() throws InterruptedException {
1707 >                    assertFalse(q.offer(three));
1708 >                    threadsStarted.await();
1709 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1710 >                    assertEquals(0, q.remainingCapacity());
1711 >                }});
1712 >
1713 >            executor.execute(new CheckedRunnable() {
1714 >                public void realRun() throws InterruptedException {
1715 >                    threadsStarted.await();
1716 >                    assertSame(one, q.take());
1717 >                }});
1718 >        }
1719      }
1720  
1721      /**
# Line 1696 | Line 1724 | public class LinkedBlockingDequeTest ext
1724      public void testPollInExecutor() {
1725          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1726          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1727 <        ExecutorService executor = Executors.newFixedThreadPool(2);
1728 <        executor.execute(new CheckedRunnable() {
1729 <            public void realRun() throws InterruptedException {
1730 <                assertNull(q.poll());
1731 <                threadsStarted.await();
1732 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1733 <                checkEmpty(q);
1734 <            }});
1735 <
1736 <        executor.execute(new CheckedRunnable() {
1737 <            public void realRun() throws InterruptedException {
1738 <                threadsStarted.await();
1739 <                q.put(one);
1740 <            }});
1741 <
1742 <        joinPool(executor);
1727 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1728 >        try (PoolCleaner cleaner = cleaner(executor)) {
1729 >            executor.execute(new CheckedRunnable() {
1730 >                public void realRun() throws InterruptedException {
1731 >                    assertNull(q.poll());
1732 >                    threadsStarted.await();
1733 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1734 >                    checkEmpty(q);
1735 >                }});
1736 >
1737 >            executor.execute(new CheckedRunnable() {
1738 >                public void realRun() throws InterruptedException {
1739 >                    threadsStarted.await();
1740 >                    q.put(one);
1741 >                }});
1742 >        }
1743      }
1744  
1745      /**
1746       * A deserialized serialized deque has same elements in same order
1747       */
1748      public void testSerialization() throws Exception {
1749 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1749 >        Queue x = populatedDeque(SIZE);
1750 >        Queue y = serialClone(x);
1751  
1752 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1753 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1754 <        out.writeObject(q);
1755 <        out.close();
1756 <
1757 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1758 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1759 <        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1760 <        assertEquals(q.size(), r.size());
1732 <        while (!q.isEmpty())
1733 <            assertEquals(q.remove(), r.remove());
1752 >        assertNotSame(y, x);
1753 >        assertEquals(x.size(), y.size());
1754 >        assertEquals(x.toString(), y.toString());
1755 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1756 >        while (!x.isEmpty()) {
1757 >            assertFalse(y.isEmpty());
1758 >            assertEquals(x.remove(), y.remove());
1759 >        }
1760 >        assertTrue(y.isEmpty());
1761      }
1762  
1763      /**
# Line 1740 | Line 1767 | public class LinkedBlockingDequeTest ext
1767          LinkedBlockingDeque q = populatedDeque(SIZE);
1768          ArrayList l = new ArrayList();
1769          q.drainTo(l);
1770 <        assertEquals(q.size(), 0);
1771 <        assertEquals(l.size(), SIZE);
1770 >        assertEquals(0, q.size());
1771 >        assertEquals(SIZE, l.size());
1772          for (int i = 0; i < SIZE; ++i)
1773              assertEquals(l.get(i), new Integer(i));
1774          q.add(zero);
# Line 1751 | Line 1778 | public class LinkedBlockingDequeTest ext
1778          assertTrue(q.contains(one));
1779          l.clear();
1780          q.drainTo(l);
1781 <        assertEquals(q.size(), 0);
1782 <        assertEquals(l.size(), 2);
1781 >        assertEquals(0, q.size());
1782 >        assertEquals(2, l.size());
1783          for (int i = 0; i < 2; ++i)
1784              assertEquals(l.get(i), new Integer(i));
1785      }
# Line 1764 | Line 1791 | public class LinkedBlockingDequeTest ext
1791          final LinkedBlockingDeque q = populatedDeque(SIZE);
1792          Thread t = new Thread(new CheckedRunnable() {
1793              public void realRun() throws InterruptedException {
1794 <                q.put(new Integer(SIZE+1));
1794 >                q.put(new Integer(SIZE + 1));
1795              }});
1796  
1797          t.start();
# Line 1788 | Line 1815 | public class LinkedBlockingDequeTest ext
1815              ArrayList l = new ArrayList();
1816              q.drainTo(l, i);
1817              int k = (i < SIZE) ? i : SIZE;
1818 <            assertEquals(l.size(), k);
1819 <            assertEquals(q.size(), SIZE-k);
1818 >            assertEquals(k, l.size());
1819 >            assertEquals(SIZE - k, q.size());
1820              for (int j = 0; j < k; ++j)
1821                  assertEquals(l.get(j), new Integer(j));
1822 <            while (q.poll() != null) ;
1822 >            do {} while (q.poll() != null);
1823 >        }
1824 >    }
1825 >
1826 >    /**
1827 >     * remove(null), contains(null) always return false
1828 >     */
1829 >    public void testNeverContainsNull() {
1830 >        Deque<?>[] qs = {
1831 >            new LinkedBlockingDeque<Object>(),
1832 >            populatedDeque(2),
1833 >        };
1834 >
1835 >        for (Deque<?> q : qs) {
1836 >            assertFalse(q.contains(null));
1837 >            assertFalse(q.remove(null));
1838 >            assertFalse(q.removeFirstOccurrence(null));
1839 >            assertFalse(q.removeLastOccurrence(null));
1840          }
1841      }
1842  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines