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.10 by jsr166, Sat Nov 21 09:28:16 2009 UTC vs.
Revision 1.24 by jsr166, Wed Oct 6 07:49:22 2010 UTC

# Line 11 | Line 11 | import static java.util.concurrent.TimeU
11   import java.io.*;
12  
13   public class LinkedBlockingDequeTest extends JSR166TestCase {
14 +
15 +    public static class Unbounded extends BlockingQueueTest {
16 +        protected BlockingQueue emptyCollection() {
17 +            return new LinkedBlockingDeque();
18 +        }
19 +    }
20 +
21 +    public static class Bounded extends BlockingQueueTest {
22 +        protected BlockingQueue emptyCollection() {
23 +            return new LinkedBlockingDeque(20);
24 +        }
25 +    }
26 +
27      public static void main(String[] args) {
28 <        junit.textui.TestRunner.run (suite());
28 >        junit.textui.TestRunner.run(suite());
29      }
30  
31      public static Test suite() {
32 <        return new TestSuite(LinkedBlockingDequeTest.class);
32 >        return newTestSuite(LinkedBlockingDequeTest.class,
33 >                            new Unbounded().testSuite(),
34 >                            new Bounded().testSuite());
35      }
36  
37      /**
# Line 98 | Line 113 | public class LinkedBlockingDequeTest ext
113      public void testPollFirst() {
114          LinkedBlockingDeque q = populatedDeque(SIZE);
115          for (int i = 0; i < SIZE; ++i) {
116 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
116 >            assertEquals(i, q.pollFirst());
117          }
118          assertNull(q.pollFirst());
119      }
# Line 109 | Line 124 | public class LinkedBlockingDequeTest ext
124      public void testPollLast() {
125          LinkedBlockingDeque q = populatedDeque(SIZE);
126          for (int i = SIZE-1; i >= 0; --i) {
127 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
127 >            assertEquals(i, q.pollLast());
128          }
129          assertNull(q.pollLast());
130      }
# Line 120 | Line 135 | public class LinkedBlockingDequeTest ext
135      public void testPeekFirst() {
136          LinkedBlockingDeque q = populatedDeque(SIZE);
137          for (int i = 0; i < SIZE; ++i) {
138 <            assertEquals(i, ((Integer)q.peekFirst()).intValue());
139 <            q.pollFirst();
138 >            assertEquals(i, q.peekFirst());
139 >            assertEquals(i, q.pollFirst());
140              assertTrue(q.peekFirst() == null ||
141 <                       i != ((Integer)q.peekFirst()).intValue());
141 >                       !q.peekFirst().equals(i));
142          }
143          assertNull(q.peekFirst());
144      }
# Line 134 | Line 149 | public class LinkedBlockingDequeTest ext
149      public void testPeek() {
150          LinkedBlockingDeque q = populatedDeque(SIZE);
151          for (int i = 0; i < SIZE; ++i) {
152 <            assertEquals(i, ((Integer)q.peek()).intValue());
153 <            q.pollFirst();
152 >            assertEquals(i, q.peek());
153 >            assertEquals(i, q.pollFirst());
154              assertTrue(q.peek() == null ||
155 <                       i != ((Integer)q.peek()).intValue());
155 >                       !q.peek().equals(i));
156          }
157          assertNull(q.peek());
158      }
# Line 148 | Line 163 | public class LinkedBlockingDequeTest ext
163      public void testPeekLast() {
164          LinkedBlockingDeque q = populatedDeque(SIZE);
165          for (int i = SIZE-1; i >= 0; --i) {
166 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
167 <            q.pollLast();
166 >            assertEquals(i, q.peekLast());
167 >            assertEquals(i, q.pollLast());
168              assertTrue(q.peekLast() == null ||
169 <                       i != ((Integer)q.peekLast()).intValue());
169 >                       !q.peekLast().equals(i));
170          }
171          assertNull(q.peekLast());
172      }
173  
174      /**
175 <     * getFirst returns next getFirst, or throws NSEE if empty
175 >     * getFirst() returns first element, or throws NSEE if empty
176       */
177      public void testFirstElement() {
178          LinkedBlockingDeque q = populatedDeque(SIZE);
179          for (int i = 0; i < SIZE; ++i) {
180 <            assertEquals(i, ((Integer)q.getFirst()).intValue());
181 <            q.pollFirst();
180 >            assertEquals(i, q.getFirst());
181 >            assertEquals(i, q.pollFirst());
182          }
183          try {
184              q.getFirst();
185              shouldThrow();
186          } catch (NoSuchElementException success) {}
187 +        assertNull(q.peekFirst());
188      }
189  
190      /**
191 <     *  getLast returns next element, or throws NSEE if empty
191 >     *  getLast() returns last element, or throws NSEE if empty
192       */
193      public void testLastElement() {
194          LinkedBlockingDeque q = populatedDeque(SIZE);
195          for (int i = SIZE-1; i >= 0; --i) {
196 <            assertEquals(i, ((Integer)q.getLast()).intValue());
197 <            q.pollLast();
196 >            assertEquals(i, q.getLast());
197 >            assertEquals(i, q.pollLast());
198          }
199          try {
200              q.getLast();
# Line 188 | Line 204 | public class LinkedBlockingDequeTest ext
204      }
205  
206      /**
207 <     *  removeFirst removes next element, or throws NSEE if empty
207 >     * removeFirst() removes first element, or throws NSEE if empty
208       */
209      public void testRemoveFirst() {
210          LinkedBlockingDeque q = populatedDeque(SIZE);
211          for (int i = 0; i < SIZE; ++i) {
212 <            assertEquals(i, ((Integer)q.removeFirst()).intValue());
212 >            assertEquals(i, q.removeFirst());
213          }
214          try {
215              q.removeFirst();
216              shouldThrow();
217          } catch (NoSuchElementException success) {}
218 +        assertNull(q.peekFirst());
219 +    }
220 +
221 +    /**
222 +     * removeLast() removes last element, or throws NSEE if empty
223 +     */
224 +    public void testRemoveLast() {
225 +        LinkedBlockingDeque q = populatedDeque(SIZE);
226 +        for (int i = SIZE - 1; i >= 0; --i) {
227 +            assertEquals(i, q.removeLast());
228 +        }
229 +        try {
230 +            q.removeLast();
231 +            shouldThrow();
232 +        } catch (NoSuchElementException success) {}
233 +        assertNull(q.peekLast());
234      }
235  
236      /**
# Line 207 | Line 239 | public class LinkedBlockingDequeTest ext
239      public void testRemove() {
240          LinkedBlockingDeque q = populatedDeque(SIZE);
241          for (int i = 0; i < SIZE; ++i) {
242 <            assertEquals(i, ((Integer)q.remove()).intValue());
242 >            assertEquals(i, q.remove());
243          }
244          try {
245              q.remove();
# Line 252 | Line 284 | public class LinkedBlockingDequeTest ext
284          LinkedBlockingDeque q = populatedDeque(3);
285          q.pollLast();
286          q.addFirst(four);
287 <        assertEquals(four,q.peekFirst());
287 >        assertSame(four, q.peekFirst());
288      }
289  
290      /**
# Line 262 | Line 294 | public class LinkedBlockingDequeTest ext
294          LinkedBlockingDeque q = populatedDeque(3);
295          q.pollLast();
296          q.addLast(four);
297 <        assertEquals(four,q.peekLast());
297 >        assertSame(four, q.peekLast());
298      }
299  
300  
# Line 276 | Line 308 | public class LinkedBlockingDequeTest ext
308      }
309  
310      /**
311 <     * Constructor throws IAE if  capacity argument nonpositive
311 >     * Constructor throws IAE if capacity argument nonpositive
312       */
313      public void testConstructor2() {
314          try {
# Line 323 | Line 355 | public class LinkedBlockingDequeTest ext
355       * Deque contains all elements of collection used to initialize
356       */
357      public void testConstructor6() {
358 <        try {
359 <            Integer[] ints = new Integer[SIZE];
360 <            for (int i = 0; i < SIZE; ++i)
361 <                ints[i] = new Integer(i);
362 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
363 <            for (int i = 0; i < SIZE; ++i)
332 <                assertEquals(ints[i], q.poll());
333 <        }
334 <        finally {}
358 >        Integer[] ints = new Integer[SIZE];
359 >        for (int i = 0; i < SIZE; ++i)
360 >            ints[i] = new Integer(i);
361 >        LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
362 >        for (int i = 0; i < SIZE; ++i)
363 >            assertEquals(ints[i], q.poll());
364      }
365  
366      /**
# Line 423 | Line 452 | public class LinkedBlockingDequeTest ext
452          LinkedBlockingDeque q = populatedDeque(3);
453          q.pollLast();
454          q.push(four);
455 <        assertEquals(four,q.peekFirst());
455 >        assertSame(four, q.peekFirst());
456      }
457  
458  
# Line 433 | Line 462 | public class LinkedBlockingDequeTest ext
462      public void testPop() {
463          LinkedBlockingDeque q = populatedDeque(SIZE);
464          for (int i = 0; i < SIZE; ++i) {
465 <            assertEquals(i, ((Integer)q.pop()).intValue());
465 >            assertEquals(i, q.pop());
466          }
467          try {
468              q.pop();
# Line 499 | Line 528 | public class LinkedBlockingDequeTest ext
528              shouldThrow();
529          } catch (NullPointerException success) {}
530      }
531 +
532      /**
533       * addAll of a collection with any null elements throws NPE after
534       * possibly adding some elements
# Line 513 | Line 543 | public class LinkedBlockingDequeTest ext
543              shouldThrow();
544          } catch (NullPointerException success) {}
545      }
546 +
547      /**
548       * addAll throws ISE if not enough room
549       */
# Line 526 | Line 557 | public class LinkedBlockingDequeTest ext
557              shouldThrow();
558          } catch (IllegalStateException success) {}
559      }
560 +
561      /**
562       * Deque contains all elements, in traversal order, of successful addAll
563       */
564      public void testAddAll5() {
565 <        try {
566 <            Integer[] empty = new Integer[0];
567 <            Integer[] ints = new Integer[SIZE];
568 <            for (int i = 0; i < SIZE; ++i)
569 <                ints[i] = new Integer(i);
570 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
571 <            assertFalse(q.addAll(Arrays.asList(empty)));
572 <            assertTrue(q.addAll(Arrays.asList(ints)));
573 <            for (int i = 0; i < SIZE; ++i)
542 <                assertEquals(ints[i], q.poll());
543 <        }
544 <        finally {}
565 >        Integer[] empty = new Integer[0];
566 >        Integer[] ints = new Integer[SIZE];
567 >        for (int i = 0; i < SIZE; ++i)
568 >            ints[i] = new Integer(i);
569 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
570 >        assertFalse(q.addAll(Arrays.asList(empty)));
571 >        assertTrue(q.addAll(Arrays.asList(ints)));
572 >        for (int i = 0; i < SIZE; ++i)
573 >            assertEquals(ints[i], q.poll());
574      }
575  
576  
# Line 573 | Line 602 | public class LinkedBlockingDequeTest ext
602       * put blocks interruptibly if full
603       */
604      public void testBlockingPut() throws InterruptedException {
605 +        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
606          Thread t = new Thread(new CheckedRunnable() {
607 <            public void realRun() {
608 <                int added = 0;
607 >            public void realRun() throws InterruptedException {
608 >                for (int i = 0; i < SIZE; ++i)
609 >                    q.put(i);
610 >                assertEquals(SIZE, q.size());
611 >                assertEquals(0, q.remainingCapacity());
612                  try {
613 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
614 <                    for (int i = 0; i < SIZE; ++i) {
615 <                        q.put(new Integer(i));
583 <                        ++added;
584 <                    }
585 <                    q.put(new Integer(SIZE));
586 <                    threadShouldThrow();
587 <                } catch (InterruptedException success) {
588 <                    threadAssertEquals(added, SIZE);
589 <                }
613 >                    q.put(99);
614 >                    shouldThrow();
615 >                } catch (InterruptedException success) {}
616              }});
617  
618          t.start();
619          Thread.sleep(SHORT_DELAY_MS);
620          t.interrupt();
621          t.join();
622 +        assertEquals(SIZE, q.size());
623 +        assertEquals(0, q.remainingCapacity());
624      }
625  
626      /**
627       * put blocks waiting for take when full
628       */
629      public void testPutWithTake() throws InterruptedException {
630 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
630 >        final int capacity = 2;
631 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
632          Thread t = new Thread(new CheckedRunnable() {
633 <            public void realRun() {
634 <                int added = 0;
633 >            public void realRun() throws InterruptedException {
634 >                for (int i = 0; i < capacity + 1; i++)
635 >                    q.put(i);
636                  try {
637 <                    q.put(new Object());
638 <                    ++added;
639 <                    q.put(new Object());
610 <                    ++added;
611 <                    q.put(new Object());
612 <                    ++added;
613 <                    q.put(new Object());
614 <                    ++added;
615 <                    threadShouldThrow();
616 <                } catch (InterruptedException success) {
617 <                    threadAssertTrue(added >= 2);
618 <                }
637 >                    q.put(99);
638 >                    shouldThrow();
639 >                } catch (InterruptedException success) {}
640              }});
641  
642          t.start();
643          Thread.sleep(SHORT_DELAY_MS);
644 <        q.take();
644 >        assertEquals(q.remainingCapacity(), 0);
645 >        assertEquals(0, q.take());
646 >        Thread.sleep(SHORT_DELAY_MS);
647          t.interrupt();
648          t.join();
649 +        assertEquals(q.remainingCapacity(), 0);
650      }
651  
652      /**
# Line 630 | Line 654 | public class LinkedBlockingDequeTest ext
654       */
655      public void testTimedOffer() throws InterruptedException {
656          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
657 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
657 >        Thread t = new Thread(new CheckedRunnable() {
658              public void realRun() throws InterruptedException {
659                  q.put(new Object());
660                  q.put(new Object());
661 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
662 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
663 <            }};
661 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
662 >                try {
663 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
664 >                    shouldThrow();
665 >                } catch (InterruptedException success) {}
666 >            }});
667  
668          t.start();
669          Thread.sleep(SMALL_DELAY_MS);
# Line 650 | Line 677 | public class LinkedBlockingDequeTest ext
677      public void testTake() throws InterruptedException {
678          LinkedBlockingDeque q = populatedDeque(SIZE);
679          for (int i = 0; i < SIZE; ++i) {
680 <            assertEquals(i, ((Integer)q.take()).intValue());
680 >            assertEquals(i, q.take());
681          }
682      }
683  
# Line 674 | Line 701 | public class LinkedBlockingDequeTest ext
701       * Take removes existing elements until empty, then blocks interruptibly
702       */
703      public void testBlockingTake() throws InterruptedException {
704 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
704 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
705 >        Thread t = new Thread(new CheckedRunnable() {
706              public void realRun() throws InterruptedException {
679                LinkedBlockingDeque q = populatedDeque(SIZE);
707                  for (int i = 0; i < SIZE; ++i) {
708 <                    assertEquals(i, ((Integer)q.take()).intValue());
708 >                    assertEquals(i, q.take());
709                  }
710 <                q.take();
711 <            }};
710 >                try {
711 >                    q.take();
712 >                    shouldThrow();
713 >                } catch (InterruptedException success) {}
714 >            }});
715  
716          t.start();
717          Thread.sleep(SHORT_DELAY_MS);
# Line 696 | Line 726 | public class LinkedBlockingDequeTest ext
726      public void testPoll() {
727          LinkedBlockingDeque q = populatedDeque(SIZE);
728          for (int i = 0; i < SIZE; ++i) {
729 <            assertEquals(i, ((Integer)q.poll()).intValue());
729 >            assertEquals(i, q.poll());
730          }
731          assertNull(q.poll());
732      }
# Line 707 | Line 737 | public class LinkedBlockingDequeTest ext
737      public void testTimedPoll0() throws InterruptedException {
738          LinkedBlockingDeque q = populatedDeque(SIZE);
739          for (int i = 0; i < SIZE; ++i) {
740 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
740 >            assertEquals(i, q.poll(0, MILLISECONDS));
741          }
742          assertNull(q.poll(0, MILLISECONDS));
743      }
# Line 718 | Line 748 | public class LinkedBlockingDequeTest ext
748      public void testTimedPoll() throws InterruptedException {
749          LinkedBlockingDeque q = populatedDeque(SIZE);
750          for (int i = 0; i < SIZE; ++i) {
751 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
751 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
752          }
753          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
754      }
# Line 728 | Line 758 | public class LinkedBlockingDequeTest ext
758       * returning timeout status
759       */
760      public void testInterruptedTimedPoll() throws InterruptedException {
761 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
761 >        Thread t = new Thread(new CheckedRunnable() {
762              public void realRun() throws InterruptedException {
763                  LinkedBlockingDeque q = populatedDeque(SIZE);
764                  for (int i = 0; i < SIZE; ++i) {
765 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
765 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
766                  }
767 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
768 <            }};
767 >                try {
768 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
769 >                    shouldThrow();
770 >                } catch (InterruptedException success) {}
771 >            }});
772  
773          t.start();
774          Thread.sleep(SHORT_DELAY_MS);
# Line 744 | Line 777 | public class LinkedBlockingDequeTest ext
777      }
778  
779      /**
747     *  timed poll before a delayed offer fails; after offer succeeds;
748     *  on interruption throws
749     */
750    public void testTimedPollWithOffer() throws InterruptedException {
751        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
752        Thread t = new ThreadShouldThrow(InterruptedException.class) {
753            public void realRun() throws InterruptedException {
754                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
755                q.poll(LONG_DELAY_MS, MILLISECONDS);
756                q.poll(LONG_DELAY_MS, MILLISECONDS);
757            }};
758
759        t.start();
760        Thread.sleep(SMALL_DELAY_MS);
761        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
762        t.interrupt();
763        t.join();
764    }
765
766
767    /**
780       * putFirst(null) throws NPE
781       */
782       public void testPutFirstNull() throws InterruptedException {
# Line 792 | Line 804 | public class LinkedBlockingDequeTest ext
804       * putFirst blocks interruptibly if full
805       */
806      public void testBlockingPutFirst() throws InterruptedException {
807 <        Thread t = new Thread(new Runnable() {
808 <                public void run() {
809 <                    int added = 0;
810 <                    try {
811 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
812 <                        for (int i = 0; i < SIZE; ++i) {
813 <                            q.putFirst(new Integer(i));
814 <                            ++added;
815 <                        }
816 <                        q.putFirst(new Integer(SIZE));
817 <                        threadShouldThrow();
818 <                    } catch (InterruptedException success) {
807 <                        threadAssertEquals(added, SIZE);
808 <                    }
809 <                }});
807 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
808 >        Thread t = new Thread(new CheckedRunnable() {
809 >            public void realRun() throws InterruptedException {
810 >                for (int i = 0; i < SIZE; ++i)
811 >                    q.putFirst(i);
812 >                assertEquals(SIZE, q.size());
813 >                assertEquals(0, q.remainingCapacity());
814 >                try {
815 >                    q.putFirst(99);
816 >                    shouldThrow();
817 >                } catch (InterruptedException success) {}
818 >            }});
819  
820          t.start();
821          Thread.sleep(SHORT_DELAY_MS);
822          t.interrupt();
823          t.join();
824 +        assertEquals(SIZE, q.size());
825 +        assertEquals(0, q.remainingCapacity());
826      }
827  
828      /**
829       * putFirst blocks waiting for take when full
830       */
831      public void testPutFirstWithTake() throws InterruptedException {
832 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
833 <        Thread t = new Thread(new Runnable() {
834 <                public void run() {
835 <                    int added = 0;
836 <                    try {
837 <                        q.putFirst(new Object());
838 <                        ++added;
839 <                        q.putFirst(new Object());
840 <                        ++added;
841 <                        q.putFirst(new Object());
842 <                        ++added;
832 <                        q.putFirst(new Object());
833 <                        ++added;
834 <                        threadShouldThrow();
835 <                    } catch (InterruptedException success) {
836 <                        threadAssertTrue(added >= 2);
837 <                    }
838 <                }
839 <            });
832 >        final int capacity = 2;
833 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
834 >        Thread t = new Thread(new CheckedRunnable() {
835 >            public void realRun() throws InterruptedException {
836 >                for (int i = 0; i < capacity + 1; i++)
837 >                    q.putFirst(i);
838 >                try {
839 >                    q.putFirst(99);
840 >                    shouldThrow();
841 >                } catch (InterruptedException success) {}
842 >            }});
843  
844          t.start();
845          Thread.sleep(SHORT_DELAY_MS);
846 <        q.take();
846 >        assertEquals(q.remainingCapacity(), 0);
847 >        assertEquals(capacity - 1, q.take());
848 >        Thread.sleep(SHORT_DELAY_MS);
849          t.interrupt();
850          t.join();
851 +        assertEquals(q.remainingCapacity(), 0);
852      }
853  
854      /**
# Line 850 | Line 856 | public class LinkedBlockingDequeTest ext
856       */
857      public void testTimedOfferFirst() throws InterruptedException {
858          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
859 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
859 >        Thread t = new Thread(new CheckedRunnable() {
860              public void realRun() throws InterruptedException {
861                  q.putFirst(new Object());
862                  q.putFirst(new Object());
863 <                threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
864 <                q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
865 <            }};
863 >                assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
864 >                try {
865 >                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
866 >                    shouldThrow();
867 >                } catch (InterruptedException success) {}
868 >            }});
869  
870          t.start();
871          Thread.sleep(SMALL_DELAY_MS);
# Line 870 | Line 879 | public class LinkedBlockingDequeTest ext
879      public void testTakeFirst() throws InterruptedException {
880          LinkedBlockingDeque q = populatedDeque(SIZE);
881          for (int i = 0; i < SIZE; ++i) {
882 <            assertEquals(i, ((Integer)q.takeFirst()).intValue());
882 >            assertEquals(i, q.takeFirst());
883          }
884      }
885  
# Line 894 | Line 903 | public class LinkedBlockingDequeTest ext
903       * TakeFirst removes existing elements until empty, then blocks interruptibly
904       */
905      public void testBlockingTakeFirst() throws InterruptedException {
906 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
906 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
907 >        Thread t = new Thread(new CheckedRunnable() {
908              public void realRun() throws InterruptedException {
909 <                LinkedBlockingDeque q = populatedDeque(SIZE);
910 <                for (int i = 0; i < SIZE; ++i) {
911 <                    assertEquals(i, ((Integer)q.takeFirst()).intValue());
912 <                }
913 <                q.takeFirst();
914 <            }};
909 >                for (int i = 0; i < SIZE; ++i)
910 >                    assertEquals(i, q.takeFirst());
911 >                try {
912 >                    q.takeFirst();
913 >                    shouldThrow();
914 >                } catch (InterruptedException success) {}
915 >            }});
916  
917          t.start();
918          Thread.sleep(SHORT_DELAY_MS);
# Line 916 | Line 927 | public class LinkedBlockingDequeTest ext
927      public void testTimedPollFirst0() throws InterruptedException {
928          LinkedBlockingDeque q = populatedDeque(SIZE);
929          for (int i = 0; i < SIZE; ++i) {
930 <            assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
930 >            assertEquals(i, q.pollFirst(0, MILLISECONDS));
931          }
932          assertNull(q.pollFirst(0, MILLISECONDS));
933      }
# Line 927 | Line 938 | public class LinkedBlockingDequeTest ext
938      public void testTimedPollFirst() throws InterruptedException {
939          LinkedBlockingDeque q = populatedDeque(SIZE);
940          for (int i = 0; i < SIZE; ++i) {
941 <            assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
941 >            assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
942          }
943          assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
944      }
# Line 937 | Line 948 | public class LinkedBlockingDequeTest ext
948       * returning timeout status
949       */
950      public void testInterruptedTimedPollFirst() throws InterruptedException {
951 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
951 >        Thread t = new Thread(new CheckedRunnable() {
952              public void realRun() throws InterruptedException {
953                  LinkedBlockingDeque q = populatedDeque(SIZE);
954                  for (int i = 0; i < SIZE; ++i) {
955 <                    threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
955 >                    assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
956                  }
957 <                q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
958 <            }};
957 >                try {
958 >                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
959 >                    shouldThrow();
960 >                } catch (InterruptedException success) {}
961 >            }});
962  
963          t.start();
964          Thread.sleep(SHORT_DELAY_MS);
# Line 958 | Line 972 | public class LinkedBlockingDequeTest ext
972       */
973      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
974          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
975 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
975 >        Thread t = new Thread(new CheckedRunnable() {
976              public void realRun() throws InterruptedException {
977 <                threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
978 <                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
979 <                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
980 <            }};
977 >                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
978 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
979 >                try {
980 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
981 >                    shouldThrow();
982 >                } catch (InterruptedException success) {}
983 >            }});
984  
985          t.start();
986          Thread.sleep(SMALL_DELAY_MS);
# Line 1000 | Line 1017 | public class LinkedBlockingDequeTest ext
1017       * putLast blocks interruptibly if full
1018       */
1019      public void testBlockingPutLast() throws InterruptedException {
1020 <        Thread t = new Thread(new Runnable() {
1021 <                public void run() {
1022 <                    int added = 0;
1023 <                    try {
1024 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1025 <                        for (int i = 0; i < SIZE; ++i) {
1026 <                            q.putLast(new Integer(i));
1027 <                            ++added;
1028 <                        }
1029 <                        q.putLast(new Integer(SIZE));
1030 <                        threadShouldThrow();
1031 <                    } catch (InterruptedException success) {
1032 <                        threadAssertEquals(added, SIZE);
1016 <                    }
1017 <                }});
1020 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1021 >        Thread t = new Thread(new CheckedRunnable() {
1022 >            public void realRun() throws InterruptedException {
1023 >                for (int i = 0; i < SIZE; ++i)
1024 >                    q.putLast(i);
1025 >                assertEquals(SIZE, q.size());
1026 >                assertEquals(0, q.remainingCapacity());
1027 >                try {
1028 >                    q.putLast(99);
1029 >                    shouldThrow();
1030 >                } catch (InterruptedException success) {}
1031 >            }});
1032 >
1033          t.start();
1034          Thread.sleep(SHORT_DELAY_MS);
1035          t.interrupt();
1036          t.join();
1037 +        assertEquals(SIZE, q.size());
1038 +        assertEquals(0, q.remainingCapacity());
1039      }
1040  
1041      /**
1042       * putLast blocks waiting for take when full
1043       */
1044      public void testPutLastWithTake() throws InterruptedException {
1045 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1046 <        Thread t = new Thread(new Runnable() {
1047 <                public void run() {
1048 <                    int added = 0;
1049 <                    try {
1050 <                        q.putLast(new Object());
1051 <                        ++added;
1052 <                        q.putLast(new Object());
1053 <                        ++added;
1054 <                        q.putLast(new Object());
1055 <                        ++added;
1039 <                        q.putLast(new Object());
1040 <                        ++added;
1041 <                        threadShouldThrow();
1042 <                    } catch (InterruptedException success) {
1043 <                        threadAssertTrue(added >= 2);
1044 <                    }
1045 <                }
1046 <            });
1045 >        final int capacity = 2;
1046 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1047 >        Thread t = new Thread(new CheckedRunnable() {
1048 >            public void realRun() throws InterruptedException {
1049 >                for (int i = 0; i < capacity + 1; i++)
1050 >                    q.putLast(i);
1051 >                try {
1052 >                    q.putLast(99);
1053 >                    shouldThrow();
1054 >                } catch (InterruptedException success) {}
1055 >            }});
1056  
1057          t.start();
1058          Thread.sleep(SHORT_DELAY_MS);
1059 <        q.take();
1059 >        assertEquals(q.remainingCapacity(), 0);
1060 >        assertEquals(0, q.take());
1061 >        Thread.sleep(SHORT_DELAY_MS);
1062          t.interrupt();
1063          t.join();
1064 +        assertEquals(q.remainingCapacity(), 0);
1065      }
1066  
1067      /**
# Line 1057 | Line 1069 | public class LinkedBlockingDequeTest ext
1069       */
1070      public void testTimedOfferLast() throws InterruptedException {
1071          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1072 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1072 >        Thread t = new Thread(new CheckedRunnable() {
1073              public void realRun() throws InterruptedException {
1074                  q.putLast(new Object());
1075                  q.putLast(new Object());
1076 <                threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1077 <                q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1078 <            }};
1076 >                assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1077 >                try {
1078 >                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1079 >                    shouldThrow();
1080 >                } catch (InterruptedException success) {}
1081 >            }});
1082  
1083          t.start();
1084          Thread.sleep(SMALL_DELAY_MS);
# Line 1077 | Line 1092 | public class LinkedBlockingDequeTest ext
1092      public void testTakeLast() throws InterruptedException {
1093          LinkedBlockingDeque q = populatedDeque(SIZE);
1094          for (int i = 0; i < SIZE; ++i) {
1095 <            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1095 >            assertEquals(SIZE-i-1, q.takeLast());
1096          }
1097      }
1098  
# Line 1101 | Line 1116 | public class LinkedBlockingDequeTest ext
1116       * TakeLast removes existing elements until empty, then blocks interruptibly
1117       */
1118      public void testBlockingTakeLast() throws InterruptedException {
1119 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1119 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1120 >        Thread t = new Thread(new CheckedRunnable() {
1121              public void realRun() throws InterruptedException {
1122 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1123 <                for (int i = 0; i < SIZE; ++i) {
1124 <                    assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1125 <                }
1126 <                q.takeLast();
1127 <            }};
1122 >                for (int i = 0; i < SIZE; ++i)
1123 >                    assertEquals(SIZE - 1 - i, q.takeLast());
1124 >                try {
1125 >                    q.takeLast();
1126 >                    shouldThrow();
1127 >                } catch (InterruptedException success) {}
1128 >            }});
1129  
1130          t.start();
1131          Thread.sleep(SHORT_DELAY_MS);
# Line 1116 | Line 1133 | public class LinkedBlockingDequeTest ext
1133          t.join();
1134      }
1135  
1119
1136      /**
1137       * timed pollLast with zero timeout succeeds when non-empty, else times out
1138       */
1139      public void testTimedPollLast0() throws InterruptedException {
1140          LinkedBlockingDeque q = populatedDeque(SIZE);
1141          for (int i = 0; i < SIZE; ++i) {
1142 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1142 >            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1143          }
1144          assertNull(q.pollLast(0, MILLISECONDS));
1145      }
# Line 1134 | Line 1150 | public class LinkedBlockingDequeTest ext
1150      public void testTimedPollLast() throws InterruptedException {
1151          LinkedBlockingDeque q = populatedDeque(SIZE);
1152          for (int i = 0; i < SIZE; ++i) {
1153 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1153 >            assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1154          }
1155          assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1156      }
# Line 1144 | Line 1160 | public class LinkedBlockingDequeTest ext
1160       * returning timeout status
1161       */
1162      public void testInterruptedTimedPollLast() throws InterruptedException {
1163 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1163 >        Thread t = new Thread(new CheckedRunnable() {
1164              public void realRun() throws InterruptedException {
1165                  LinkedBlockingDeque q = populatedDeque(SIZE);
1166                  for (int i = 0; i < SIZE; ++i) {
1167 <                    threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1167 >                    assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1168                  }
1169 <                q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1170 <            }};
1169 >                try {
1170 >                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1171 >                    shouldThrow();
1172 >                } catch (InterruptedException success) {}
1173 >            }});
1174  
1175          t.start();
1176          Thread.sleep(SHORT_DELAY_MS);
# Line 1160 | Line 1179 | public class LinkedBlockingDequeTest ext
1179      }
1180  
1181      /**
1182 <     *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1183 <     *  on interruption throws
1182 >     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1183 >     * on interruption throws
1184       */
1185      public void testTimedPollWithOfferLast() throws InterruptedException {
1186          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
# Line 1189 | Line 1208 | public class LinkedBlockingDequeTest ext
1208      public void testElement() {
1209          LinkedBlockingDeque q = populatedDeque(SIZE);
1210          for (int i = 0; i < SIZE; ++i) {
1211 <            assertEquals(i, ((Integer)q.element()).intValue());
1211 >            assertEquals(i, q.element());
1212              q.poll();
1213          }
1214          try {
# Line 1315 | Line 1334 | public class LinkedBlockingDequeTest ext
1334       * toArray(null) throws NPE
1335       */
1336      public void testToArray_BadArg() {
1337 +        LinkedBlockingDeque q = populatedDeque(SIZE);
1338          try {
1319            LinkedBlockingDeque q = populatedDeque(SIZE);
1339              Object o[] = q.toArray(null);
1340              shouldThrow();
1341          } catch (NullPointerException success) {}
# Line 1326 | Line 1345 | public class LinkedBlockingDequeTest ext
1345       * toArray with incompatible array type throws CCE
1346       */
1347      public void testToArray1_BadArg() {
1348 +        LinkedBlockingDeque q = populatedDeque(SIZE);
1349          try {
1350 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1331 <            Object o[] = q.toArray(new String[10] );
1350 >            Object o[] = q.toArray(new String[10]);
1351              shouldThrow();
1352          } catch (ArrayStoreException success) {}
1353      }
# Line 1348 | Line 1367 | public class LinkedBlockingDequeTest ext
1367      /**
1368       * iterator.remove removes current element
1369       */
1370 <    public void testIteratorRemove () {
1370 >    public void testIteratorRemove() {
1371          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1372          q.add(two);
1373          q.add(one);
# Line 1359 | Line 1378 | public class LinkedBlockingDequeTest ext
1378          it.remove();
1379  
1380          it = q.iterator();
1381 <        assertEquals(it.next(), one);
1382 <        assertEquals(it.next(), three);
1381 >        assertSame(it.next(), one);
1382 >        assertSame(it.next(), three);
1383          assertFalse(it.hasNext());
1384      }
1385  
# Line 1376 | Line 1395 | public class LinkedBlockingDequeTest ext
1395          assertEquals(0, q.remainingCapacity());
1396          int k = 0;
1397          for (Iterator it = q.iterator(); it.hasNext();) {
1398 <            int i = ((Integer)(it.next())).intValue();
1380 <            assertEquals(++k, i);
1398 >            assertEquals(++k, it.next());
1399          }
1400          assertEquals(3, k);
1401      }
# Line 1385 | Line 1403 | public class LinkedBlockingDequeTest ext
1403      /**
1404       * Modifications do not cause iterators to fail
1405       */
1406 <    public void testWeaklyConsistentIteration () {
1406 >    public void testWeaklyConsistentIteration() {
1407          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1408          q.add(one);
1409          q.add(two);
# Line 1428 | Line 1446 | public class LinkedBlockingDequeTest ext
1446              q.add(new Integer(1));
1447              int k = 0;
1448              for (Iterator it = q.descendingIterator(); it.hasNext();) {
1449 <                int i = ((Integer)(it.next())).intValue();
1432 <                assertEquals(++k, i);
1449 >                assertEquals(++k, it.next());
1450              }
1451  
1452              assertEquals(3, k);
# Line 1442 | Line 1459 | public class LinkedBlockingDequeTest ext
1459      /**
1460       * descendingIterator.remove removes current element
1461       */
1462 <    public void testDescendingIteratorRemove () {
1462 >    public void testDescendingIteratorRemove() {
1463          final LinkedBlockingDeque q = new LinkedBlockingDeque();
1464          for (int iters = 0; iters < 100; ++iters) {
1465              q.add(new Integer(3));
# Line 1484 | Line 1501 | public class LinkedBlockingDequeTest ext
1501          ExecutorService executor = Executors.newFixedThreadPool(2);
1502          executor.execute(new CheckedRunnable() {
1503              public void realRun() throws InterruptedException {
1504 <                threadAssertFalse(q.offer(three));
1505 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1506 <                threadAssertEquals(0, q.remainingCapacity());
1504 >                assertFalse(q.offer(three));
1505 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1506 >                assertEquals(0, q.remainingCapacity());
1507              }});
1508  
1509          executor.execute(new CheckedRunnable() {
1510              public void realRun() throws InterruptedException {
1511                  Thread.sleep(SMALL_DELAY_MS);
1512 <                threadAssertEquals(one, q.take());
1512 >                assertSame(one, q.take());
1513              }});
1514  
1515          joinPool(executor);
# Line 1506 | Line 1523 | public class LinkedBlockingDequeTest ext
1523          ExecutorService executor = Executors.newFixedThreadPool(2);
1524          executor.execute(new CheckedRunnable() {
1525              public void realRun() throws InterruptedException {
1526 <                threadAssertNull(q.poll());
1527 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1528 <                threadAssertTrue(q.isEmpty());
1526 >                assertNull(q.poll());
1527 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1528 >                assertTrue(q.isEmpty());
1529              }});
1530  
1531          executor.execute(new CheckedRunnable() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines