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.15 by jsr166, Sat Nov 21 21:12:55 2009 UTC vs.
Revision 1.39 by jsr166, Fri May 27 20:07:24 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import junit.framework.*;
# 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      /**
38       * Create a deque of given size containing consecutive
39       * Integers 0 ... n.
40       */
41 <    private LinkedBlockingDeque populatedDeque(int n) {
42 <        LinkedBlockingDeque q = new LinkedBlockingDeque(n);
41 >    private LinkedBlockingDeque<Integer> populatedDeque(int n) {
42 >        LinkedBlockingDeque<Integer> q =
43 >            new LinkedBlockingDeque<Integer>(n);
44          assertTrue(q.isEmpty());
45          for (int i = 0; i < n; i++)
46              assertTrue(q.offer(new Integer(i)));
# Line 93 | Line 109 | public class LinkedBlockingDequeTest ext
109      }
110  
111      /**
112 <     *  pollFirst succeeds unless empty
112 >     * pollFirst succeeds unless empty
113       */
114      public void testPollFirst() {
115          LinkedBlockingDeque q = populatedDeque(SIZE);
116          for (int i = 0; i < SIZE; ++i) {
117 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
117 >            assertEquals(i, q.pollFirst());
118          }
119          assertNull(q.pollFirst());
120      }
121  
122      /**
123 <     *  pollLast succeeds unless empty
123 >     * pollLast succeeds unless empty
124       */
125      public void testPollLast() {
126          LinkedBlockingDeque q = populatedDeque(SIZE);
127          for (int i = SIZE-1; i >= 0; --i) {
128 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
128 >            assertEquals(i, q.pollLast());
129          }
130          assertNull(q.pollLast());
131      }
132  
133      /**
134 <     *  peekFirst returns next element, or null if empty
134 >     * peekFirst returns next element, or null if empty
135       */
136      public void testPeekFirst() {
137          LinkedBlockingDeque q = populatedDeque(SIZE);
138          for (int i = 0; i < SIZE; ++i) {
139 <            assertEquals(i, ((Integer)q.peekFirst()).intValue());
140 <            q.pollFirst();
139 >            assertEquals(i, q.peekFirst());
140 >            assertEquals(i, q.pollFirst());
141              assertTrue(q.peekFirst() == null ||
142 <                       i != ((Integer)q.peekFirst()).intValue());
142 >                       !q.peekFirst().equals(i));
143          }
144          assertNull(q.peekFirst());
145      }
146  
147      /**
148 <     *  peek returns next element, or null if empty
148 >     * peek returns next element, or null if empty
149       */
150      public void testPeek() {
151          LinkedBlockingDeque q = populatedDeque(SIZE);
152          for (int i = 0; i < SIZE; ++i) {
153 <            assertEquals(i, ((Integer)q.peek()).intValue());
154 <            q.pollFirst();
153 >            assertEquals(i, q.peek());
154 >            assertEquals(i, q.pollFirst());
155              assertTrue(q.peek() == null ||
156 <                       i != ((Integer)q.peek()).intValue());
156 >                       !q.peek().equals(i));
157          }
158          assertNull(q.peek());
159      }
160  
161      /**
162 <     *  peekLast returns next element, or null if empty
162 >     * peekLast returns next element, or null if empty
163       */
164      public void testPeekLast() {
165          LinkedBlockingDeque q = populatedDeque(SIZE);
166          for (int i = SIZE-1; i >= 0; --i) {
167 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
168 <            q.pollLast();
167 >            assertEquals(i, q.peekLast());
168 >            assertEquals(i, q.pollLast());
169              assertTrue(q.peekLast() == null ||
170 <                       i != ((Integer)q.peekLast()).intValue());
170 >                       !q.peekLast().equals(i));
171          }
172          assertNull(q.peekLast());
173      }
174  
175      /**
176 <     * getFirst returns next getFirst, or throws NSEE if empty
176 >     * getFirst() returns first element, or throws NSEE if empty
177       */
178      public void testFirstElement() {
179          LinkedBlockingDeque q = populatedDeque(SIZE);
180          for (int i = 0; i < SIZE; ++i) {
181 <            assertEquals(i, ((Integer)q.getFirst()).intValue());
182 <            q.pollFirst();
181 >            assertEquals(i, q.getFirst());
182 >            assertEquals(i, q.pollFirst());
183          }
184          try {
185              q.getFirst();
# Line 173 | Line 189 | public class LinkedBlockingDequeTest ext
189      }
190  
191      /**
192 <     *  getLast returns next element, or throws NSEE if empty
192 >     * getLast() returns last element, or throws NSEE if empty
193       */
194      public void testLastElement() {
195          LinkedBlockingDeque q = populatedDeque(SIZE);
196          for (int i = SIZE-1; i >= 0; --i) {
197 <            assertEquals(i, ((Integer)q.getLast()).intValue());
198 <            q.pollLast();
197 >            assertEquals(i, q.getLast());
198 >            assertEquals(i, q.pollLast());
199          }
200          try {
201              q.getLast();
# Line 189 | Line 205 | public class LinkedBlockingDequeTest ext
205      }
206  
207      /**
208 <     *  removeFirst removes next element, or throws NSEE if empty
208 >     * removeFirst() removes first element, or throws NSEE if empty
209       */
210      public void testRemoveFirst() {
211          LinkedBlockingDeque q = populatedDeque(SIZE);
212          for (int i = 0; i < SIZE; ++i) {
213 <            assertEquals(i, ((Integer)q.removeFirst()).intValue());
213 >            assertEquals(i, q.removeFirst());
214          }
215          try {
216              q.removeFirst();
# Line 204 | Line 220 | public class LinkedBlockingDequeTest ext
220      }
221  
222      /**
223 <     *  removeLast removes last element, or throws NSEE if empty
223 >     * removeLast() removes last element, or throws NSEE if empty
224       */
225      public void testRemoveLast() {
226          LinkedBlockingDeque q = populatedDeque(SIZE);
227          for (int i = SIZE - 1; i >= 0; --i) {
228 <            assertEquals(i, ((Integer)q.removeLast()).intValue());
228 >            assertEquals(i, q.removeLast());
229          }
230          try {
231              q.removeLast();
# Line 219 | Line 235 | public class LinkedBlockingDequeTest ext
235      }
236  
237      /**
238 <     *  remove removes next element, or throws NSEE if empty
238 >     * remove removes next element, or throws NSEE if empty
239       */
240      public void testRemove() {
241          LinkedBlockingDeque q = populatedDeque(SIZE);
242          for (int i = 0; i < SIZE; ++i) {
243 <            assertEquals(i, ((Integer)q.remove()).intValue());
243 >            assertEquals(i, q.remove());
244          }
245          try {
246              q.remove();
# Line 269 | Line 285 | public class LinkedBlockingDequeTest ext
285          LinkedBlockingDeque q = populatedDeque(3);
286          q.pollLast();
287          q.addFirst(four);
288 <        assertEquals(four,q.peekFirst());
288 >        assertSame(four, q.peekFirst());
289      }
290  
291      /**
# Line 279 | Line 295 | public class LinkedBlockingDequeTest ext
295          LinkedBlockingDeque q = populatedDeque(3);
296          q.pollLast();
297          q.addLast(four);
298 <        assertEquals(four,q.peekLast());
298 >        assertSame(four, q.peekLast());
299      }
300  
285
301      /**
302       * A new deque has the indicated capacity, or Integer.MAX_VALUE if
303       * none given
# Line 437 | 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  
443
458      /**
459 <     *  pop removes next element, or throws NSEE if empty
459 >     * pop removes next element, or throws NSEE if empty
460       */
461      public void testPop() {
462          LinkedBlockingDeque q = populatedDeque(SIZE);
463          for (int i = 0; i < SIZE; ++i) {
464 <            assertEquals(i, ((Integer)q.pop()).intValue());
464 >            assertEquals(i, q.pop());
465          }
466          try {
467              q.pop();
# Line 455 | Line 469 | public class LinkedBlockingDequeTest ext
469          } catch (NoSuchElementException success) {}
470      }
471  
458
472      /**
473       * Offer succeeds if not full; fails if full
474       */
# Line 513 | Line 526 | public class LinkedBlockingDequeTest ext
526              shouldThrow();
527          } catch (NullPointerException success) {}
528      }
529 +
530      /**
531       * addAll of a collection with any null elements throws NPE after
532       * possibly adding some elements
# Line 527 | Line 541 | public class LinkedBlockingDequeTest ext
541              shouldThrow();
542          } catch (NullPointerException success) {}
543      }
544 +
545      /**
546       * addAll throws ISE if not enough room
547       */
# Line 540 | Line 555 | public class LinkedBlockingDequeTest ext
555              shouldThrow();
556          } catch (IllegalStateException success) {}
557      }
558 +
559      /**
560       * Deque contains all elements, in traversal order, of successful addAll
561       */
# Line 555 | Line 571 | public class LinkedBlockingDequeTest ext
571              assertEquals(ints[i], q.poll());
572      }
573  
558
574      /**
575       * put(null) throws NPE
576       */
# Line 584 | Line 599 | public class LinkedBlockingDequeTest ext
599       * put blocks interruptibly if full
600       */
601      public void testBlockingPut() throws InterruptedException {
602 <        Thread t = new Thread(new CheckedRunnable() {
603 <            public void realRun() {
604 <                int added = 0;
602 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
603 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
604 >        Thread t = newStartedThread(new CheckedRunnable() {
605 >            public void realRun() throws InterruptedException {
606 >                for (int i = 0; i < SIZE; ++i)
607 >                    q.put(i);
608 >                assertEquals(SIZE, q.size());
609 >                assertEquals(0, q.remainingCapacity());
610 >
611 >                Thread.currentThread().interrupt();
612                  try {
613 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
614 <                    for (int i = 0; i < SIZE; ++i) {
615 <                        q.put(new Integer(i));
616 <                        ++added;
617 <                    }
618 <                    q.put(new Integer(SIZE));
619 <                    threadShouldThrow();
620 <                } catch (InterruptedException success) {
621 <                    threadAssertEquals(added, SIZE);
622 <                }
613 >                    q.put(99);
614 >                    shouldThrow();
615 >                } catch (InterruptedException success) {}
616 >                assertFalse(Thread.interrupted());
617 >
618 >                pleaseInterrupt.countDown();
619 >                try {
620 >                    q.put(99);
621 >                    shouldThrow();
622 >                } catch (InterruptedException success) {}
623 >                assertFalse(Thread.interrupted());
624              }});
625  
626 <        t.start();
627 <        Thread.sleep(SHORT_DELAY_MS);
626 >        await(pleaseInterrupt);
627 >        assertThreadStaysAlive(t);
628          t.interrupt();
629 <        t.join();
629 >        awaitTermination(t);
630 >        assertEquals(SIZE, q.size());
631 >        assertEquals(0, q.remainingCapacity());
632      }
633  
634      /**
635 <     * put blocks waiting for take when full
635 >     * put blocks interruptibly waiting for take when full
636       */
637      public void testPutWithTake() throws InterruptedException {
638 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
639 <        Thread t = new Thread(new CheckedRunnable() {
640 <            public void realRun() {
641 <                int added = 0;
638 >        final int capacity = 2;
639 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
640 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
641 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
642 >        Thread t = newStartedThread(new CheckedRunnable() {
643 >            public void realRun() throws InterruptedException {
644 >                for (int i = 0; i < capacity; i++)
645 >                    q.put(i);
646 >                pleaseTake.countDown();
647 >                q.put(86);
648 >
649 >                pleaseInterrupt.countDown();
650                  try {
651 <                    q.put(new Object());
652 <                    ++added;
653 <                    q.put(new Object());
654 <                    ++added;
622 <                    q.put(new Object());
623 <                    ++added;
624 <                    q.put(new Object());
625 <                    ++added;
626 <                    threadShouldThrow();
627 <                } catch (InterruptedException success) {
628 <                    threadAssertTrue(added >= 2);
629 <                }
651 >                    q.put(99);
652 >                    shouldThrow();
653 >                } catch (InterruptedException success) {}
654 >                assertFalse(Thread.interrupted());
655              }});
656  
657 <        t.start();
658 <        Thread.sleep(SHORT_DELAY_MS);
659 <        q.take();
657 >        await(pleaseTake);
658 >        assertEquals(q.remainingCapacity(), 0);
659 >        assertEquals(0, q.take());
660 >
661 >        await(pleaseInterrupt);
662 >        assertThreadStaysAlive(t);
663          t.interrupt();
664 <        t.join();
664 >        awaitTermination(t);
665 >        assertEquals(q.remainingCapacity(), 0);
666      }
667  
668      /**
# Line 641 | Line 670 | public class LinkedBlockingDequeTest ext
670       */
671      public void testTimedOffer() throws InterruptedException {
672          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
673 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
673 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
674 >        Thread t = newStartedThread(new CheckedRunnable() {
675              public void realRun() throws InterruptedException {
676                  q.put(new Object());
677                  q.put(new Object());
678 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
679 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
680 <            }};
678 >                long startTime = System.nanoTime();
679 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
680 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
681 >                pleaseInterrupt.countDown();
682 >                try {
683 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
684 >                    shouldThrow();
685 >                } catch (InterruptedException success) {}
686 >            }});
687  
688 <        t.start();
689 <        Thread.sleep(SMALL_DELAY_MS);
688 >        await(pleaseInterrupt);
689 >        assertThreadStaysAlive(t);
690          t.interrupt();
691 <        t.join();
691 >        awaitTermination(t);
692      }
693  
694      /**
# Line 661 | Line 697 | public class LinkedBlockingDequeTest ext
697      public void testTake() throws InterruptedException {
698          LinkedBlockingDeque q = populatedDeque(SIZE);
699          for (int i = 0; i < SIZE; ++i) {
700 <            assertEquals(i, ((Integer)q.take()).intValue());
700 >            assertEquals(i, q.take());
701          }
702      }
703  
704      /**
705 <     * take blocks interruptibly when empty
670 <     */
671 <    public void testTakeFromEmpty() throws InterruptedException {
672 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
673 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
674 <            public void realRun() throws InterruptedException {
675 <                q.take();
676 <            }};
677 <
678 <        t.start();
679 <        Thread.sleep(SHORT_DELAY_MS);
680 <        t.interrupt();
681 <        t.join();
682 <    }
683 <
684 <    /**
685 <     * Take removes existing elements until empty, then blocks interruptibly
705 >     * take removes existing elements until empty, then blocks interruptibly
706       */
707      public void testBlockingTake() throws InterruptedException {
708 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
708 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
709 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
710 >        Thread t = newStartedThread(new CheckedRunnable() {
711              public void realRun() throws InterruptedException {
690                LinkedBlockingDeque q = populatedDeque(SIZE);
712                  for (int i = 0; i < SIZE; ++i) {
713 <                    assertEquals(i, ((Integer)q.take()).intValue());
713 >                    assertEquals(i, q.take());
714                  }
694                q.take();
695            }};
715  
716 <        t.start();
717 <        Thread.sleep(SHORT_DELAY_MS);
716 >                Thread.currentThread().interrupt();
717 >                try {
718 >                    q.take();
719 >                    shouldThrow();
720 >                } catch (InterruptedException success) {}
721 >                assertFalse(Thread.interrupted());
722 >
723 >                pleaseInterrupt.countDown();
724 >                try {
725 >                    q.take();
726 >                    shouldThrow();
727 >                } catch (InterruptedException success) {}
728 >                assertFalse(Thread.interrupted());
729 >            }});
730 >
731 >        await(pleaseInterrupt);
732 >        assertThreadStaysAlive(t);
733          t.interrupt();
734 <        t.join();
734 >        awaitTermination(t);
735      }
736  
703
737      /**
738       * poll succeeds unless empty
739       */
740      public void testPoll() {
741          LinkedBlockingDeque q = populatedDeque(SIZE);
742          for (int i = 0; i < SIZE; ++i) {
743 <            assertEquals(i, ((Integer)q.poll()).intValue());
743 >            assertEquals(i, q.poll());
744          }
745          assertNull(q.poll());
746      }
# Line 718 | Line 751 | public class LinkedBlockingDequeTest ext
751      public void testTimedPoll0() throws InterruptedException {
752          LinkedBlockingDeque q = populatedDeque(SIZE);
753          for (int i = 0; i < SIZE; ++i) {
754 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
754 >            assertEquals(i, q.poll(0, MILLISECONDS));
755          }
756          assertNull(q.poll(0, MILLISECONDS));
757      }
# Line 729 | Line 762 | public class LinkedBlockingDequeTest ext
762      public void testTimedPoll() throws InterruptedException {
763          LinkedBlockingDeque q = populatedDeque(SIZE);
764          for (int i = 0; i < SIZE; ++i) {
765 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
766 <        }
767 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
765 >            long startTime = System.nanoTime();
766 >            assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
767 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
768 >        }
769 >        long startTime = System.nanoTime();
770 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
771 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
772 >        checkEmpty(q);
773      }
774  
775      /**
# Line 739 | Line 777 | public class LinkedBlockingDequeTest ext
777       * returning timeout status
778       */
779      public void testInterruptedTimedPoll() throws InterruptedException {
780 <        Thread t = new Thread(new CheckedRunnable() {
780 >        final BlockingQueue<Integer> q = populatedDeque(SIZE);
781 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
782 >        Thread t = newStartedThread(new CheckedRunnable() {
783              public void realRun() throws InterruptedException {
744                LinkedBlockingDeque q = populatedDeque(SIZE);
784                  for (int i = 0; i < SIZE; ++i) {
785 <                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
785 >                    long t0 = System.nanoTime();
786 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
787 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
788                  }
789 +                long t0 = System.nanoTime();
790 +                aboutToWait.countDown();
791                  try {
792 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
750 <                    shouldThrow();
751 <                } catch (InterruptedException success) {}
752 <            }});
753 <
754 <        t.start();
755 <        Thread.sleep(SHORT_DELAY_MS);
756 <        t.interrupt();
757 <        t.join();
758 <    }
759 <
760 <    /**
761 <     *  timed poll before a delayed offer fails; after offer succeeds;
762 <     *  on interruption throws
763 <     */
764 <    public void testTimedPollWithOffer() throws InterruptedException {
765 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
766 <        Thread t = new Thread(new CheckedRunnable() {
767 <            public void realRun() throws InterruptedException {
768 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
769 <                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
770 <                try {
771 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
792 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
793                      shouldThrow();
794 <                } catch (InterruptedException success) {}
794 >                } catch (InterruptedException success) {
795 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
796 >                }
797              }});
798  
799 <        t.start();
800 <        Thread.sleep(SMALL_DELAY_MS);
778 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
799 >        aboutToWait.await();
800 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
801          t.interrupt();
802 <        t.join();
802 >        awaitTermination(t, MEDIUM_DELAY_MS);
803 >        checkEmpty(q);
804      }
805  
783
806      /**
807       * putFirst(null) throws NPE
808       */
809 <     public void testPutFirstNull() throws InterruptedException {
809 >    public void testPutFirstNull() throws InterruptedException {
810          try {
811              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
812              q.putFirst(null);
813              shouldThrow();
814          } catch (NullPointerException success) {}
815 <     }
815 >    }
816  
817      /**
818       * all elements successfully putFirst are contained
819       */
820 <     public void testPutFirst() throws InterruptedException {
821 <         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
822 <         for (int i = 0; i < SIZE; ++i) {
823 <             Integer I = new Integer(i);
824 <             q.putFirst(I);
825 <             assertTrue(q.contains(I));
826 <         }
827 <         assertEquals(0, q.remainingCapacity());
820 >    public void testPutFirst() throws InterruptedException {
821 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
822 >        for (int i = 0; i < SIZE; ++i) {
823 >            Integer I = new Integer(i);
824 >            q.putFirst(I);
825 >            assertTrue(q.contains(I));
826 >        }
827 >        assertEquals(0, q.remainingCapacity());
828      }
829  
830      /**
831       * putFirst blocks interruptibly if full
832       */
833      public void testBlockingPutFirst() throws InterruptedException {
834 <        Thread t = new Thread(new CheckedRunnable() {
835 <            public void realRun() {
836 <                int added = 0;
834 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
835 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
836 >        Thread t = newStartedThread(new CheckedRunnable() {
837 >            public void realRun() throws InterruptedException {
838 >                for (int i = 0; i < SIZE; ++i)
839 >                    q.putFirst(i);
840 >                assertEquals(SIZE, q.size());
841 >                assertEquals(0, q.remainingCapacity());
842 >
843 >                Thread.currentThread().interrupt();
844                  try {
845 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
846 <                    for (int i = 0; i < SIZE; ++i) {
847 <                        q.putFirst(new Integer(i));
848 <                        ++added;
849 <                    }
850 <                    q.putFirst(new Integer(SIZE));
851 <                    threadShouldThrow();
852 <                } catch (InterruptedException success) {
853 <                    threadAssertEquals(added, SIZE);
854 <                }
845 >                    q.putFirst(99);
846 >                    shouldThrow();
847 >                } catch (InterruptedException success) {}
848 >                assertFalse(Thread.interrupted());
849 >
850 >                pleaseInterrupt.countDown();
851 >                try {
852 >                    q.putFirst(99);
853 >                    shouldThrow();
854 >                } catch (InterruptedException success) {}
855 >                assertFalse(Thread.interrupted());
856              }});
857  
858 <        t.start();
859 <        Thread.sleep(SHORT_DELAY_MS);
858 >        await(pleaseInterrupt);
859 >        assertThreadStaysAlive(t);
860          t.interrupt();
861 <        t.join();
861 >        awaitTermination(t);
862 >        assertEquals(SIZE, q.size());
863 >        assertEquals(0, q.remainingCapacity());
864      }
865  
866      /**
867 <     * putFirst blocks waiting for take when full
867 >     * putFirst blocks interruptibly waiting for take when full
868       */
869      public void testPutFirstWithTake() throws InterruptedException {
870 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
871 <        Thread t = new Thread(new CheckedRunnable() {
872 <            public void realRun() {
873 <                int added = 0;
870 >        final int capacity = 2;
871 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
872 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
873 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
874 >        Thread t = newStartedThread(new CheckedRunnable() {
875 >            public void realRun() throws InterruptedException {
876 >                for (int i = 0; i < capacity; i++)
877 >                    q.putFirst(i);
878 >                pleaseTake.countDown();
879 >                q.putFirst(86);
880 >
881 >                pleaseInterrupt.countDown();
882                  try {
883 <                    q.putFirst(new Object());
884 <                    ++added;
885 <                    q.putFirst(new Object());
886 <                    ++added;
847 <                    q.putFirst(new Object());
848 <                    ++added;
849 <                    q.putFirst(new Object());
850 <                    ++added;
851 <                    threadShouldThrow();
852 <                } catch (InterruptedException success) {
853 <                    threadAssertTrue(added >= 2);
854 <                }
883 >                    q.putFirst(99);
884 >                    shouldThrow();
885 >                } catch (InterruptedException success) {}
886 >                assertFalse(Thread.interrupted());
887              }});
888  
889 <        t.start();
890 <        Thread.sleep(SHORT_DELAY_MS);
891 <        q.take();
889 >        await(pleaseTake);
890 >        assertEquals(q.remainingCapacity(), 0);
891 >        assertEquals(capacity - 1, q.take());
892 >
893 >        await(pleaseInterrupt);
894 >        assertThreadStaysAlive(t);
895          t.interrupt();
896 <        t.join();
896 >        awaitTermination(t);
897 >        assertEquals(q.remainingCapacity(), 0);
898      }
899  
900      /**
# Line 866 | Line 902 | public class LinkedBlockingDequeTest ext
902       */
903      public void testTimedOfferFirst() throws InterruptedException {
904          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
905 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
905 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
906 >        Thread t = newStartedThread(new CheckedRunnable() {
907              public void realRun() throws InterruptedException {
908                  q.putFirst(new Object());
909                  q.putFirst(new Object());
910 <                threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
911 <                q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
912 <            }};
910 >                long startTime = System.nanoTime();
911 >                assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
912 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
913 >                pleaseInterrupt.countDown();
914 >                try {
915 >                    q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
916 >                    shouldThrow();
917 >                } catch (InterruptedException success) {}
918 >            }});
919  
920 <        t.start();
921 <        Thread.sleep(SMALL_DELAY_MS);
920 >        await(pleaseInterrupt);
921 >        assertThreadStaysAlive(t);
922          t.interrupt();
923 <        t.join();
923 >        awaitTermination(t);
924      }
925  
926      /**
# Line 886 | Line 929 | public class LinkedBlockingDequeTest ext
929      public void testTakeFirst() throws InterruptedException {
930          LinkedBlockingDeque q = populatedDeque(SIZE);
931          for (int i = 0; i < SIZE; ++i) {
932 <            assertEquals(i, ((Integer)q.takeFirst()).intValue());
932 >            assertEquals(i, q.takeFirst());
933          }
934      }
935  
936      /**
937 <     * takeFirst blocks interruptibly when empty
937 >     * takeFirst() blocks interruptibly when empty
938       */
939 <    public void testTakeFirstFromEmpty() throws InterruptedException {
940 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
941 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
942 <            public void realRun() throws InterruptedException {
943 <                q.takeFirst();
944 <            }};
939 >    public void testTakeFirstFromEmptyBlocksInterruptibly() {
940 >        final BlockingDeque q = new LinkedBlockingDeque();
941 >        final CountDownLatch threadStarted = new CountDownLatch(1);
942 >        Thread t = newStartedThread(new CheckedRunnable() {
943 >            public void realRun() {
944 >                threadStarted.countDown();
945 >                try {
946 >                    q.takeFirst();
947 >                    shouldThrow();
948 >                } catch (InterruptedException success) {}
949 >                assertFalse(Thread.interrupted());
950 >            }});
951  
952 <        t.start();
953 <        Thread.sleep(SHORT_DELAY_MS);
952 >        await(threadStarted);
953 >        assertThreadStaysAlive(t);
954          t.interrupt();
955 <        t.join();
955 >        awaitTermination(t);
956 >    }
957 >
958 >    /**
959 >     * takeFirst() throws InterruptedException immediately if interrupted
960 >     * before waiting
961 >     */
962 >    public void testTakeFirstFromEmptyAfterInterrupt() {
963 >        final BlockingDeque q = new LinkedBlockingDeque();
964 >        Thread t = newStartedThread(new CheckedRunnable() {
965 >            public void realRun() {
966 >                Thread.currentThread().interrupt();
967 >                try {
968 >                    q.takeFirst();
969 >                    shouldThrow();
970 >                } catch (InterruptedException success) {}
971 >                assertFalse(Thread.interrupted());
972 >            }});
973 >
974 >        awaitTermination(t);
975 >    }
976 >
977 >    /**
978 >     * takeLast() blocks interruptibly when empty
979 >     */
980 >    public void testTakeLastFromEmptyBlocksInterruptibly() {
981 >        final BlockingDeque q = new LinkedBlockingDeque();
982 >        final CountDownLatch threadStarted = new CountDownLatch(1);
983 >        Thread t = newStartedThread(new CheckedRunnable() {
984 >            public void realRun() {
985 >                threadStarted.countDown();
986 >                try {
987 >                    q.takeLast();
988 >                    shouldThrow();
989 >                } catch (InterruptedException success) {}
990 >                assertFalse(Thread.interrupted());
991 >            }});
992 >
993 >        await(threadStarted);
994 >        assertThreadStaysAlive(t);
995 >        t.interrupt();
996 >        awaitTermination(t);
997      }
998  
999      /**
1000 <     * TakeFirst removes existing elements until empty, then blocks interruptibly
1000 >     * takeLast() throws InterruptedException immediately if interrupted
1001 >     * before waiting
1002 >     */
1003 >    public void testTakeLastFromEmptyAfterInterrupt() {
1004 >        final BlockingDeque q = new LinkedBlockingDeque();
1005 >        Thread t = newStartedThread(new CheckedRunnable() {
1006 >            public void realRun() {
1007 >                Thread.currentThread().interrupt();
1008 >                try {
1009 >                    q.takeLast();
1010 >                    shouldThrow();
1011 >                } catch (InterruptedException success) {}
1012 >                assertFalse(Thread.interrupted());
1013 >            }});
1014 >
1015 >        awaitTermination(t);
1016 >    }
1017 >
1018 >    /**
1019 >     * takeFirst removes existing elements until empty, then blocks interruptibly
1020       */
1021      public void testBlockingTakeFirst() throws InterruptedException {
1022 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1022 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1023 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1024 >        Thread t = newStartedThread(new CheckedRunnable() {
1025              public void realRun() throws InterruptedException {
915                LinkedBlockingDeque q = populatedDeque(SIZE);
1026                  for (int i = 0; i < SIZE; ++i) {
1027 <                    assertEquals(i, ((Integer)q.takeFirst()).intValue());
1027 >                    assertEquals(i, q.takeFirst());
1028                  }
919                q.takeFirst();
920            }};
1029  
1030 <        t.start();
1031 <        Thread.sleep(SHORT_DELAY_MS);
1030 >                Thread.currentThread().interrupt();
1031 >                try {
1032 >                    q.takeFirst();
1033 >                    shouldThrow();
1034 >                } catch (InterruptedException success) {}
1035 >                assertFalse(Thread.interrupted());
1036 >
1037 >                pleaseInterrupt.countDown();
1038 >                try {
1039 >                    q.takeFirst();
1040 >                    shouldThrow();
1041 >                } catch (InterruptedException success) {}
1042 >                assertFalse(Thread.interrupted());
1043 >            }});
1044 >
1045 >        await(pleaseInterrupt);
1046 >        assertThreadStaysAlive(t);
1047          t.interrupt();
1048 <        t.join();
1048 >        awaitTermination(t);
1049      }
1050  
928
1051      /**
1052       * timed pollFirst with zero timeout succeeds when non-empty, else times out
1053       */
1054      public void testTimedPollFirst0() throws InterruptedException {
1055          LinkedBlockingDeque q = populatedDeque(SIZE);
1056          for (int i = 0; i < SIZE; ++i) {
1057 <            assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
1057 >            assertEquals(i, q.pollFirst(0, MILLISECONDS));
1058          }
1059          assertNull(q.pollFirst(0, MILLISECONDS));
1060      }
# Line 943 | Line 1065 | public class LinkedBlockingDequeTest ext
1065      public void testTimedPollFirst() throws InterruptedException {
1066          LinkedBlockingDeque q = populatedDeque(SIZE);
1067          for (int i = 0; i < SIZE; ++i) {
1068 <            assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1069 <        }
1070 <        assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1068 >            long startTime = System.nanoTime();
1069 >            assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1070 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1071 >        }
1072 >        long startTime = System.nanoTime();
1073 >        assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1074 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1075 >        checkEmpty(q);
1076      }
1077  
1078      /**
# Line 953 | Line 1080 | public class LinkedBlockingDequeTest ext
1080       * returning timeout status
1081       */
1082      public void testInterruptedTimedPollFirst() throws InterruptedException {
1083 <        Thread t = new Thread(new CheckedRunnable() {
1083 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1084 >        Thread t = newStartedThread(new CheckedRunnable() {
1085              public void realRun() throws InterruptedException {
1086                  LinkedBlockingDeque q = populatedDeque(SIZE);
1087                  for (int i = 0; i < SIZE; ++i) {
1088 <                    assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1088 >                    assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1089                  }
1090 +
1091 +                Thread.currentThread().interrupt();
1092 +                try {
1093 +                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1094 +                    shouldThrow();
1095 +                } catch (InterruptedException success) {}
1096 +                assertFalse(Thread.interrupted());
1097 +
1098 +                pleaseInterrupt.countDown();
1099                  try {
1100                      q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1101                      shouldThrow();
1102                  } catch (InterruptedException success) {}
1103 +                assertFalse(Thread.interrupted());
1104              }});
1105  
1106 <        t.start();
1107 <        Thread.sleep(SHORT_DELAY_MS);
1106 >        await(pleaseInterrupt);
1107 >        assertThreadStaysAlive(t);
1108          t.interrupt();
1109 <        t.join();
1109 >        awaitTermination(t);
1110      }
1111  
1112      /**
1113 <     *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
1114 <     *  on interruption throws
1113 >     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
1114 >     * on interruption throws
1115       */
1116      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
1117          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1118 <        Thread t = new Thread(new CheckedRunnable() {
1118 >        final CheckedBarrier barrier = new CheckedBarrier(2);
1119 >        Thread t = newStartedThread(new CheckedRunnable() {
1120              public void realRun() throws InterruptedException {
1121 <                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1121 >                long startTime = System.nanoTime();
1122 >                assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1123 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1124 >
1125 >                barrier.await();
1126 >
1127                  assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1128 +
1129 +                Thread.currentThread().interrupt();
1130                  try {
1131                      q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1132                      shouldThrow();
1133                  } catch (InterruptedException success) {}
1134 +
1135 +                barrier.await();
1136 +                try {
1137 +                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1138 +                    shouldThrow();
1139 +                } catch (InterruptedException success) {}
1140 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1141              }});
1142  
1143 <        t.start();
1144 <        Thread.sleep(SMALL_DELAY_MS);
1145 <        assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
1143 >        barrier.await();
1144 >        long startTime = System.nanoTime();
1145 >        assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1146 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1147 >        barrier.await();
1148 >        assertThreadStaysAlive(t);
1149          t.interrupt();
1150 <        t.join();
1150 >        awaitTermination(t);
1151      }
1152  
1153      /**
1154       * putLast(null) throws NPE
1155       */
1156 <     public void testPutLastNull() throws InterruptedException {
1156 >    public void testPutLastNull() throws InterruptedException {
1157          try {
1158              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1159              q.putLast(null);
1160              shouldThrow();
1161          } catch (NullPointerException success) {}
1162 <     }
1162 >    }
1163  
1164      /**
1165       * all elements successfully putLast are contained
1166       */
1167 <     public void testPutLast() throws InterruptedException {
1168 <         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1169 <         for (int i = 0; i < SIZE; ++i) {
1170 <             Integer I = new Integer(i);
1171 <             q.putLast(I);
1172 <             assertTrue(q.contains(I));
1173 <         }
1174 <         assertEquals(0, q.remainingCapacity());
1167 >    public void testPutLast() throws InterruptedException {
1168 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1169 >        for (int i = 0; i < SIZE; ++i) {
1170 >            Integer I = new Integer(i);
1171 >            q.putLast(I);
1172 >            assertTrue(q.contains(I));
1173 >        }
1174 >        assertEquals(0, q.remainingCapacity());
1175      }
1176  
1177      /**
1178       * putLast blocks interruptibly if full
1179       */
1180      public void testBlockingPutLast() throws InterruptedException {
1181 <        Thread t = new Thread(new CheckedRunnable() {
1182 <            public void realRun() {
1183 <                int added = 0;
1181 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1182 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1183 >        Thread t = newStartedThread(new CheckedRunnable() {
1184 >            public void realRun() throws InterruptedException {
1185 >                for (int i = 0; i < SIZE; ++i)
1186 >                    q.putLast(i);
1187 >                assertEquals(SIZE, q.size());
1188 >                assertEquals(0, q.remainingCapacity());
1189 >
1190 >                Thread.currentThread().interrupt();
1191                  try {
1192 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1193 <                    for (int i = 0; i < SIZE; ++i) {
1194 <                        q.putLast(new Integer(i));
1195 <                        ++added;
1196 <                    }
1197 <                    q.putLast(new Integer(SIZE));
1198 <                    threadShouldThrow();
1199 <                } catch (InterruptedException success) {
1200 <                    threadAssertEquals(added, SIZE);
1201 <                }
1192 >                    q.putLast(99);
1193 >                    shouldThrow();
1194 >                } catch (InterruptedException success) {}
1195 >                assertFalse(Thread.interrupted());
1196 >
1197 >                pleaseInterrupt.countDown();
1198 >                try {
1199 >                    q.putLast(99);
1200 >                    shouldThrow();
1201 >                } catch (InterruptedException success) {}
1202 >                assertFalse(Thread.interrupted());
1203              }});
1204  
1205 <        t.start();
1206 <        Thread.sleep(SHORT_DELAY_MS);
1205 >        await(pleaseInterrupt);
1206 >        assertThreadStaysAlive(t);
1207          t.interrupt();
1208 <        t.join();
1208 >        awaitTermination(t);
1209 >        assertEquals(SIZE, q.size());
1210 >        assertEquals(0, q.remainingCapacity());
1211      }
1212  
1213      /**
1214 <     * putLast blocks waiting for take when full
1214 >     * putLast blocks interruptibly waiting for take when full
1215       */
1216      public void testPutLastWithTake() throws InterruptedException {
1217 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1218 <        Thread t = new Thread(new CheckedRunnable() {
1219 <            public void realRun() {
1220 <                int added = 0;
1217 >        final int capacity = 2;
1218 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1219 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
1220 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1221 >        Thread t = newStartedThread(new CheckedRunnable() {
1222 >            public void realRun() throws InterruptedException {
1223 >                for (int i = 0; i < capacity; i++)
1224 >                    q.putLast(i);
1225 >                pleaseTake.countDown();
1226 >                q.putLast(86);
1227 >
1228 >                pleaseInterrupt.countDown();
1229                  try {
1230 <                    q.putLast(new Object());
1231 <                    ++added;
1232 <                    q.putLast(new Object());
1233 <                    ++added;
1060 <                    q.putLast(new Object());
1061 <                    ++added;
1062 <                    q.putLast(new Object());
1063 <                    ++added;
1064 <                    threadShouldThrow();
1065 <                } catch (InterruptedException success) {
1066 <                    threadAssertTrue(added >= 2);
1067 <                }
1230 >                    q.putLast(99);
1231 >                    shouldThrow();
1232 >                } catch (InterruptedException success) {}
1233 >                assertFalse(Thread.interrupted());
1234              }});
1235  
1236 <        t.start();
1237 <        Thread.sleep(SHORT_DELAY_MS);
1238 <        q.take();
1236 >        await(pleaseTake);
1237 >        assertEquals(q.remainingCapacity(), 0);
1238 >        assertEquals(0, q.take());
1239 >
1240 >        await(pleaseInterrupt);
1241 >        assertThreadStaysAlive(t);
1242          t.interrupt();
1243 <        t.join();
1243 >        awaitTermination(t);
1244 >        assertEquals(q.remainingCapacity(), 0);
1245      }
1246  
1247      /**
# Line 1079 | Line 1249 | public class LinkedBlockingDequeTest ext
1249       */
1250      public void testTimedOfferLast() throws InterruptedException {
1251          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1252 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1252 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1253 >        Thread t = newStartedThread(new CheckedRunnable() {
1254              public void realRun() throws InterruptedException {
1255                  q.putLast(new Object());
1256                  q.putLast(new Object());
1257 <                threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1258 <                q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1259 <            }};
1257 >                long startTime = System.nanoTime();
1258 >                assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1259 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1260 >                pleaseInterrupt.countDown();
1261 >                try {
1262 >                    q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1263 >                    shouldThrow();
1264 >                } catch (InterruptedException success) {}
1265 >            }});
1266  
1267 <        t.start();
1268 <        Thread.sleep(SMALL_DELAY_MS);
1267 >        await(pleaseInterrupt);
1268 >        assertThreadStaysAlive(t);
1269          t.interrupt();
1270 <        t.join();
1270 >        awaitTermination(t);
1271      }
1272  
1273      /**
# Line 1099 | Line 1276 | public class LinkedBlockingDequeTest ext
1276      public void testTakeLast() throws InterruptedException {
1277          LinkedBlockingDeque q = populatedDeque(SIZE);
1278          for (int i = 0; i < SIZE; ++i) {
1279 <            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1279 >            assertEquals(SIZE-i-1, q.takeLast());
1280          }
1281      }
1282  
1283      /**
1284 <     * takeLast blocks interruptibly when empty
1108 <     */
1109 <    public void testTakeLastFromEmpty() throws InterruptedException {
1110 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1111 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1112 <            public void realRun() throws InterruptedException {
1113 <                q.takeLast();
1114 <            }};
1115 <
1116 <        t.start();
1117 <        Thread.sleep(SHORT_DELAY_MS);
1118 <        t.interrupt();
1119 <        t.join();
1120 <    }
1121 <
1122 <    /**
1123 <     * TakeLast removes existing elements until empty, then blocks interruptibly
1284 >     * takeLast removes existing elements until empty, then blocks interruptibly
1285       */
1286      public void testBlockingTakeLast() throws InterruptedException {
1287 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1287 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1288 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1289 >        Thread t = newStartedThread(new CheckedRunnable() {
1290              public void realRun() throws InterruptedException {
1128                LinkedBlockingDeque q = populatedDeque(SIZE);
1291                  for (int i = 0; i < SIZE; ++i) {
1292 <                    assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1292 >                    assertEquals(SIZE-i-1, q.takeLast());
1293                  }
1132                q.takeLast();
1133            }};
1294  
1295 <        t.start();
1296 <        Thread.sleep(SHORT_DELAY_MS);
1295 >                Thread.currentThread().interrupt();
1296 >                try {
1297 >                    q.takeLast();
1298 >                    shouldThrow();
1299 >                } catch (InterruptedException success) {}
1300 >                assertFalse(Thread.interrupted());
1301 >
1302 >                pleaseInterrupt.countDown();
1303 >                try {
1304 >                    q.takeLast();
1305 >                    shouldThrow();
1306 >                } catch (InterruptedException success) {}
1307 >                assertFalse(Thread.interrupted());
1308 >            }});
1309 >
1310 >        await(pleaseInterrupt);
1311 >        assertThreadStaysAlive(t);
1312          t.interrupt();
1313 <        t.join();
1313 >        awaitTermination(t);
1314      }
1315  
1141
1316      /**
1317       * timed pollLast with zero timeout succeeds when non-empty, else times out
1318       */
1319      public void testTimedPollLast0() throws InterruptedException {
1320          LinkedBlockingDeque q = populatedDeque(SIZE);
1321          for (int i = 0; i < SIZE; ++i) {
1322 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1322 >            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1323          }
1324          assertNull(q.pollLast(0, MILLISECONDS));
1325      }
# Line 1156 | Line 1330 | public class LinkedBlockingDequeTest ext
1330      public void testTimedPollLast() throws InterruptedException {
1331          LinkedBlockingDeque q = populatedDeque(SIZE);
1332          for (int i = 0; i < SIZE; ++i) {
1333 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1334 <        }
1335 <        assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1333 >            long startTime = System.nanoTime();
1334 >            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1335 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1336 >        }
1337 >        long startTime = System.nanoTime();
1338 >        assertNull(q.pollLast(timeoutMillis(), MILLISECONDS));
1339 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1340 >        checkEmpty(q);
1341      }
1342  
1343      /**
# Line 1166 | Line 1345 | public class LinkedBlockingDequeTest ext
1345       * returning timeout status
1346       */
1347      public void testInterruptedTimedPollLast() throws InterruptedException {
1348 <        Thread t = new Thread(new CheckedRunnable() {
1348 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1349 >        Thread t = newStartedThread(new CheckedRunnable() {
1350              public void realRun() throws InterruptedException {
1351                  LinkedBlockingDeque q = populatedDeque(SIZE);
1352                  for (int i = 0; i < SIZE; ++i) {
1353 <                    assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1353 >                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1354                  }
1355 +
1356 +                Thread.currentThread().interrupt();
1357                  try {
1358 <                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1358 >                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1359                      shouldThrow();
1360                  } catch (InterruptedException success) {}
1361 +                assertFalse(Thread.interrupted());
1362 +
1363 +                pleaseInterrupt.countDown();
1364 +                try {
1365 +                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1366 +                    shouldThrow();
1367 +                } catch (InterruptedException success) {}
1368 +                assertFalse(Thread.interrupted());
1369              }});
1370  
1371 <        t.start();
1372 <        Thread.sleep(SHORT_DELAY_MS);
1371 >        await(pleaseInterrupt);
1372 >        assertThreadStaysAlive(t);
1373          t.interrupt();
1374 <        t.join();
1374 >        awaitTermination(t);
1375      }
1376  
1377      /**
1378 <     *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1379 <     *  on interruption throws
1378 >     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1379 >     * on interruption throws
1380       */
1381      public void testTimedPollWithOfferLast() throws InterruptedException {
1382          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1383 <        Thread t = new Thread(new CheckedRunnable() {
1383 >        final CheckedBarrier barrier = new CheckedBarrier(2);
1384 >        Thread t = newStartedThread(new CheckedRunnable() {
1385              public void realRun() throws InterruptedException {
1386 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1386 >                long startTime = System.nanoTime();
1387 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
1388 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1389 >
1390 >                barrier.await();
1391 >
1392                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1393 +
1394 +                Thread.currentThread().interrupt();
1395                  try {
1396                      q.poll(LONG_DELAY_MS, MILLISECONDS);
1397                      shouldThrow();
1398                  } catch (InterruptedException success) {}
1399 +                assertFalse(Thread.interrupted());
1400 +
1401 +                barrier.await();
1402 +                try {
1403 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1404 +                    shouldThrow();
1405 +                } catch (InterruptedException success) {}
1406 +                assertFalse(Thread.interrupted());
1407              }});
1408  
1409 <        t.start();
1410 <        Thread.sleep(SMALL_DELAY_MS);
1411 <        assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1409 >        barrier.await();
1410 >        long startTime = System.nanoTime();
1411 >        assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS));
1412 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1413 >
1414 >        barrier.await();
1415 >        assertThreadStaysAlive(t);
1416          t.interrupt();
1417 <        t.join();
1417 >        awaitTermination(t);
1418      }
1419  
1210
1420      /**
1421       * element returns next element, or throws NSEE if empty
1422       */
1423      public void testElement() {
1424          LinkedBlockingDeque q = populatedDeque(SIZE);
1425          for (int i = 0; i < SIZE; ++i) {
1426 <            assertEquals(i, ((Integer)q.element()).intValue());
1426 >            assertEquals(i, q.element());
1427              q.poll();
1428          }
1429          try {
# Line 1229 | Line 1438 | public class LinkedBlockingDequeTest ext
1438      public void testRemoveElement() {
1439          LinkedBlockingDeque q = populatedDeque(SIZE);
1440          for (int i = 1; i < SIZE; i+=2) {
1441 <            assertTrue(q.remove(new Integer(i)));
1441 >            assertTrue(q.contains(i));
1442 >            assertTrue(q.remove(i));
1443 >            assertFalse(q.contains(i));
1444 >            assertTrue(q.contains(i-1));
1445          }
1446          for (int i = 0; i < SIZE; i+=2) {
1447 <            assertTrue(q.remove(new Integer(i)));
1448 <            assertFalse(q.remove(new Integer(i+1)));
1447 >            assertTrue(q.contains(i));
1448 >            assertTrue(q.remove(i));
1449 >            assertFalse(q.contains(i));
1450 >            assertFalse(q.remove(i+1));
1451 >            assertFalse(q.contains(i+1));
1452          }
1453          assertTrue(q.isEmpty());
1454      }
# Line 1316 | Line 1531 | public class LinkedBlockingDequeTest ext
1531      }
1532  
1533      /**
1534 <     * toArray contains all elements
1534 >     * toArray contains all elements in FIFO order
1535       */
1536      public void testToArray() throws InterruptedException{
1537          LinkedBlockingDeque q = populatedDeque(SIZE);
1538          Object[] o = q.toArray();
1539          for (int i = 0; i < o.length; i++)
1540 <            assertEquals(o[i], q.take());
1540 >            assertSame(o[i], q.poll());
1541      }
1542  
1543      /**
1544 <     * toArray(a) contains all elements
1544 >     * toArray(a) contains all elements in FIFO order
1545       */
1546 <    public void testToArray2() throws InterruptedException {
1547 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1546 >    public void testToArray2() {
1547 >        LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1548          Integer[] ints = new Integer[SIZE];
1549 <        ints = (Integer[])q.toArray(ints);
1549 >        Integer[] array = q.toArray(ints);
1550 >        assertSame(ints, array);
1551          for (int i = 0; i < ints.length; i++)
1552 <            assertEquals(ints[i], q.take());
1552 >            assertSame(ints[i], q.remove());
1553      }
1554  
1555      /**
1556 <     * toArray(null) throws NPE
1556 >     * toArray(null) throws NullPointerException
1557       */
1558 <    public void testToArray_BadArg() {
1558 >    public void testToArray_NullArg() {
1559 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1560          try {
1561 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1345 <            Object o[] = q.toArray(null);
1561 >            q.toArray(null);
1562              shouldThrow();
1563          } catch (NullPointerException success) {}
1564      }
1565  
1566      /**
1567 <     * toArray with incompatible array type throws CCE
1567 >     * toArray(incompatible array type) throws ArrayStoreException
1568       */
1569      public void testToArray1_BadArg() {
1570 +        LinkedBlockingDeque q = populatedDeque(SIZE);
1571          try {
1572 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1356 <            Object o[] = q.toArray(new String[10] );
1572 >            q.toArray(new String[10]);
1573              shouldThrow();
1574          } catch (ArrayStoreException success) {}
1575      }
1576  
1361
1577      /**
1578       * iterator iterates through all elements
1579       */
# Line 1373 | Line 1588 | public class LinkedBlockingDequeTest ext
1588      /**
1589       * iterator.remove removes current element
1590       */
1591 <    public void testIteratorRemove () {
1591 >    public void testIteratorRemove() {
1592          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1593          q.add(two);
1594          q.add(one);
# Line 1384 | Line 1599 | public class LinkedBlockingDequeTest ext
1599          it.remove();
1600  
1601          it = q.iterator();
1602 <        assertEquals(it.next(), one);
1603 <        assertEquals(it.next(), three);
1602 >        assertSame(it.next(), one);
1603 >        assertSame(it.next(), three);
1604          assertFalse(it.hasNext());
1605      }
1606  
1392
1607      /**
1608       * iterator ordering is FIFO
1609       */
# Line 1401 | Line 1615 | public class LinkedBlockingDequeTest ext
1615          assertEquals(0, q.remainingCapacity());
1616          int k = 0;
1617          for (Iterator it = q.iterator(); it.hasNext();) {
1618 <            int i = ((Integer)(it.next())).intValue();
1405 <            assertEquals(++k, i);
1618 >            assertEquals(++k, it.next());
1619          }
1620          assertEquals(3, k);
1621      }
# Line 1410 | Line 1623 | public class LinkedBlockingDequeTest ext
1623      /**
1624       * Modifications do not cause iterators to fail
1625       */
1626 <    public void testWeaklyConsistentIteration () {
1626 >    public void testWeaklyConsistentIteration() {
1627          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1628          q.add(one);
1629          q.add(two);
# Line 1422 | Line 1635 | public class LinkedBlockingDequeTest ext
1635          assertEquals(0, q.size());
1636      }
1637  
1425
1638      /**
1639 <     *  Descending iterator iterates through all elements
1639 >     * Descending iterator iterates through all elements
1640       */
1641      public void testDescendingIterator() {
1642          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 1443 | Line 1655 | public class LinkedBlockingDequeTest ext
1655      }
1656  
1657      /**
1658 <     *  Descending iterator ordering is reverse FIFO
1658 >     * Descending iterator ordering is reverse FIFO
1659       */
1660      public void testDescendingIteratorOrdering() {
1661          final LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1453 | Line 1665 | public class LinkedBlockingDequeTest ext
1665              q.add(new Integer(1));
1666              int k = 0;
1667              for (Iterator it = q.descendingIterator(); it.hasNext();) {
1668 <                int i = ((Integer)(it.next())).intValue();
1457 <                assertEquals(++k, i);
1668 >                assertEquals(++k, it.next());
1669              }
1670  
1671              assertEquals(3, k);
# Line 1467 | Line 1678 | public class LinkedBlockingDequeTest ext
1678      /**
1679       * descendingIterator.remove removes current element
1680       */
1681 <    public void testDescendingIteratorRemove () {
1681 >    public void testDescendingIteratorRemove() {
1682          final LinkedBlockingDeque q = new LinkedBlockingDeque();
1683          for (int iters = 0; iters < 100; ++iters) {
1684              q.add(new Integer(3));
# Line 1486 | Line 1697 | public class LinkedBlockingDequeTest ext
1697          }
1698      }
1699  
1489
1700      /**
1701       * toString contains toStrings of elements
1702       */
# Line 1494 | Line 1704 | public class LinkedBlockingDequeTest ext
1704          LinkedBlockingDeque q = populatedDeque(SIZE);
1705          String s = q.toString();
1706          for (int i = 0; i < SIZE; ++i) {
1707 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1707 >            assertTrue(s.contains(String.valueOf(i)));
1708          }
1709      }
1710  
1501
1711      /**
1712       * offer transfers elements across Executor tasks
1713       */
# Line 1507 | Line 1716 | public class LinkedBlockingDequeTest ext
1716          q.add(one);
1717          q.add(two);
1718          ExecutorService executor = Executors.newFixedThreadPool(2);
1719 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1720          executor.execute(new CheckedRunnable() {
1721              public void realRun() throws InterruptedException {
1722 <                threadAssertFalse(q.offer(three));
1723 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1724 <                threadAssertEquals(0, q.remainingCapacity());
1722 >                assertFalse(q.offer(three));
1723 >                threadsStarted.await();
1724 >                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1725 >                assertEquals(0, q.remainingCapacity());
1726              }});
1727  
1728          executor.execute(new CheckedRunnable() {
1729              public void realRun() throws InterruptedException {
1730 <                Thread.sleep(SMALL_DELAY_MS);
1731 <                threadAssertEquals(one, q.take());
1730 >                threadsStarted.await();
1731 >                assertSame(one, q.take());
1732              }});
1733  
1734          joinPool(executor);
1735      }
1736  
1737      /**
1738 <     * poll retrieves elements across Executor threads
1738 >     * timed poll retrieves elements across Executor threads
1739       */
1740      public void testPollInExecutor() {
1741          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1742 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1743          ExecutorService executor = Executors.newFixedThreadPool(2);
1744          executor.execute(new CheckedRunnable() {
1745              public void realRun() throws InterruptedException {
1746 <                threadAssertNull(q.poll());
1747 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1748 <                threadAssertTrue(q.isEmpty());
1746 >                assertNull(q.poll());
1747 >                threadsStarted.await();
1748 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1749 >                checkEmpty(q);
1750              }});
1751  
1752          executor.execute(new CheckedRunnable() {
1753              public void realRun() throws InterruptedException {
1754 <                Thread.sleep(SMALL_DELAY_MS);
1754 >                threadsStarted.await();
1755                  q.put(one);
1756              }});
1757  
# Line 1653 | Line 1866 | public class LinkedBlockingDequeTest ext
1866      }
1867  
1868      /**
1869 <     * drainTo(c, n) empties first max {n, size} elements of deque into c
1869 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
1870       */
1871      public void testDrainToN() {
1872          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1662 | Line 1875 | public class LinkedBlockingDequeTest ext
1875                  assertTrue(q.offer(new Integer(j)));
1876              ArrayList l = new ArrayList();
1877              q.drainTo(l, i);
1878 <            int k = (i < SIZE)? i : SIZE;
1878 >            int k = (i < SIZE) ? i : SIZE;
1879              assertEquals(l.size(), k);
1880              assertEquals(q.size(), SIZE-k);
1881              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines