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.21 by jsr166, Wed Aug 25 00:07:03 2010 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());
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);
# Line 104 | Line 120 | public class LinkedBlockingDequeTest ext
120      }
121  
122      /**
123 <     *  pollLast succeeds unless empty
123 >     * pollLast succeeds unless empty
124       */
125      public void testPollLast() {
126          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 115 | Line 131 | public class LinkedBlockingDequeTest ext
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);
# Line 129 | Line 145 | public class LinkedBlockingDequeTest ext
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);
# Line 143 | Line 159 | public class LinkedBlockingDequeTest ext
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);
# Line 157 | Line 173 | public class LinkedBlockingDequeTest ext
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);
# 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);
# 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);
# 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);
# 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);
# Line 282 | Line 298 | public class LinkedBlockingDequeTest ext
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 440 | Line 455 | public class LinkedBlockingDequeTest ext
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);
# 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 556 | Line 571 | public class LinkedBlockingDequeTest ext
571              assertEquals(ints[i], q.poll());
572      }
573  
559
574      /**
575       * put(null) throws NPE
576       */
# Line 586 | Line 600 | public class LinkedBlockingDequeTest ext
600       */
601      public void testBlockingPut() throws InterruptedException {
602          final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
603 <        Thread t = new Thread(new CheckedRunnable() {
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                      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 int capacity = 2;
639          final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
640 <        Thread t = new Thread(new CheckedRunnable() {
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 + 1; i++)
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(99);
652                      shouldThrow();
653                  } catch (InterruptedException success) {}
654 +                assertFalse(Thread.interrupted());
655              }});
656  
657 <        t.start();
626 <        Thread.sleep(SHORT_DELAY_MS);
657 >        await(pleaseTake);
658          assertEquals(q.remainingCapacity(), 0);
659          assertEquals(0, q.take());
660 <        Thread.sleep(SHORT_DELAY_MS);
660 >
661 >        await(pleaseInterrupt);
662 >        assertThreadStaysAlive(t);
663          t.interrupt();
664 <        t.join();
664 >        awaitTermination(t);
665          assertEquals(q.remainingCapacity(), 0);
666      }
667  
# Line 637 | Line 670 | public class LinkedBlockingDequeTest ext
670       */
671      public void testTimedOffer() throws InterruptedException {
672          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
673 <        Thread t = new Thread(new CheckedRunnable() {
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 <                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
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(), LONG_DELAY_MS, MILLISECONDS);
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 665 | Line 702 | public class LinkedBlockingDequeTest ext
702      }
703  
704      /**
705 <     * take blocks interruptibly when empty
669 <     */
670 <    public void testTakeFromEmpty() throws InterruptedException {
671 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
672 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
673 <            public void realRun() throws InterruptedException {
674 <                q.take();
675 <            }};
676 <
677 <        t.start();
678 <        Thread.sleep(SHORT_DELAY_MS);
679 <        t.interrupt();
680 <        t.join();
681 <    }
682 <
683 <    /**
684 <     * 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          final LinkedBlockingDeque q = populatedDeque(SIZE);
709 <        Thread t = new Thread(new CheckedRunnable() {
709 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
710 >        Thread t = newStartedThread(new CheckedRunnable() {
711              public void realRun() throws InterruptedException {
712                  for (int i = 0; i < SIZE; ++i) {
713                      assertEquals(i, q.take());
714                  }
715 +
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 <        t.start();
732 <        Thread.sleep(SHORT_DELAY_MS);
731 >        await(pleaseInterrupt);
732 >        assertThreadStaysAlive(t);
733          t.interrupt();
734 <        t.join();
734 >        awaitTermination(t);
735      }
736  
705
737      /**
738       * poll succeeds unless empty
739       */
# Line 731 | 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, q.poll(SHORT_DELAY_MS, MILLISECONDS));
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 741 | 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 {
746                LinkedBlockingDeque q = populatedDeque(SIZE);
784                  for (int i = 0; i < SIZE; ++i) {
785 <                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
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);
752 <                    shouldThrow();
753 <                } catch (InterruptedException success) {}
754 <            }});
755 <
756 <        t.start();
757 <        Thread.sleep(SHORT_DELAY_MS);
758 <        t.interrupt();
759 <        t.join();
760 <    }
761 <
762 <    /**
763 <     *  timed poll before a delayed offer fails; after offer succeeds;
764 <     *  on interruption throws
765 <     */
766 <    public void testTimedPollWithOffer() throws InterruptedException {
767 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
768 <        Thread t = new Thread(new CheckedRunnable() {
769 <            public void realRun() throws InterruptedException {
770 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
771 <                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
772 <                try {
773 <                    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);
780 <        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  
785
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      /**
# Line 812 | Line 832 | public class LinkedBlockingDequeTest ext
832       */
833      public void testBlockingPutFirst() throws InterruptedException {
834          final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
835 <        Thread t = new Thread(new CheckedRunnable() {
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                      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 int capacity = 2;
871          final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
872 <        Thread t = new Thread(new CheckedRunnable() {
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 + 1; i++)
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(99);
884                      shouldThrow();
885                  } catch (InterruptedException success) {}
886 +                assertFalse(Thread.interrupted());
887              }});
888  
889 <        t.start();
852 <        Thread.sleep(SHORT_DELAY_MS);
889 >        await(pleaseTake);
890          assertEquals(q.remainingCapacity(), 0);
891          assertEquals(capacity - 1, q.take());
892 <        Thread.sleep(SHORT_DELAY_MS);
892 >
893 >        await(pleaseInterrupt);
894 >        assertThreadStaysAlive(t);
895          t.interrupt();
896 <        t.join();
896 >        awaitTermination(t);
897          assertEquals(q.remainingCapacity(), 0);
898      }
899  
# Line 863 | Line 902 | public class LinkedBlockingDequeTest ext
902       */
903      public void testTimedOfferFirst() throws InterruptedException {
904          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
905 <        Thread t = new Thread(new CheckedRunnable() {
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 <                assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
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(), LONG_DELAY_MS, MILLISECONDS);
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 891 | Line 934 | public class LinkedBlockingDequeTest ext
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 removes existing elements until empty, then blocks interruptibly
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 >     * 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          final LinkedBlockingDeque q = populatedDeque(SIZE);
1023 <        Thread t = new Thread(new CheckedRunnable() {
1023 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1024 >        Thread t = newStartedThread(new CheckedRunnable() {
1025              public void realRun() throws InterruptedException {
1026 <                for (int i = 0; i < SIZE; ++i)
1026 >                for (int i = 0; i < SIZE; ++i) {
1027                      assertEquals(i, q.takeFirst());
1028 +                }
1029 +
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 <        t.start();
1046 <        Thread.sleep(SHORT_DELAY_MS);
1045 >        await(pleaseInterrupt);
1046 >        assertThreadStaysAlive(t);
1047          t.interrupt();
1048 <        t.join();
1048 >        awaitTermination(t);
1049      }
1050  
930
1051      /**
1052       * timed pollFirst with zero timeout succeeds when non-empty, else times out
1053       */
# Line 945 | 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, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
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 955 | 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, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
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      /**
# Line 1025 | Line 1179 | public class LinkedBlockingDequeTest ext
1179       */
1180      public void testBlockingPutLast() throws InterruptedException {
1181          final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1182 <        Thread t = new Thread(new CheckedRunnable() {
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                      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 int capacity = 2;
1218          final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1219 <        Thread t = new Thread(new CheckedRunnable() {
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 + 1; i++)
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(99);
1231                      shouldThrow();
1232                  } catch (InterruptedException success) {}
1233 +                assertFalse(Thread.interrupted());
1234              }});
1235  
1236 <        t.start();
1065 <        Thread.sleep(SHORT_DELAY_MS);
1236 >        await(pleaseTake);
1237          assertEquals(q.remainingCapacity(), 0);
1238          assertEquals(0, q.take());
1239 <        Thread.sleep(SHORT_DELAY_MS);
1239 >
1240 >        await(pleaseInterrupt);
1241 >        assertThreadStaysAlive(t);
1242          t.interrupt();
1243 <        t.join();
1243 >        awaitTermination(t);
1244          assertEquals(q.remainingCapacity(), 0);
1245      }
1246  
# Line 1076 | Line 1249 | public class LinkedBlockingDequeTest ext
1249       */
1250      public void testTimedOfferLast() throws InterruptedException {
1251          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1252 <        Thread t = new Thread(new CheckedRunnable() {
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 <                assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
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(), LONG_DELAY_MS, MILLISECONDS);
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 1104 | Line 1281 | public class LinkedBlockingDequeTest ext
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          final LinkedBlockingDeque q = populatedDeque(SIZE);
1288 <        Thread t = new Thread(new CheckedRunnable() {
1288 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1289 >        Thread t = newStartedThread(new CheckedRunnable() {
1290              public void realRun() throws InterruptedException {
1291 <                for (int i = 0; i < SIZE; ++i)
1292 <                    assertEquals(SIZE - 1 - i, q.takeLast());
1291 >                for (int i = 0; i < SIZE; ++i) {
1292 >                    assertEquals(SIZE-i-1, q.takeLast());
1293 >                }
1294 >
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 <        t.start();
1311 <        Thread.sleep(SHORT_DELAY_MS);
1310 >        await(pleaseInterrupt);
1311 >        assertThreadStaysAlive(t);
1312          t.interrupt();
1313 <        t.join();
1313 >        awaitTermination(t);
1314      }
1315  
1316      /**
# Line 1157 | 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, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
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 1167 | 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, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
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  
1211
1420      /**
1421       * element returns next element, or throws NSEE if empty
1422       */
# Line 1230 | 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 1317 | 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 <            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 <            Object o[] = q.toArray(new String[10]);
1572 >            q.toArray(new String[10]);
1573              shouldThrow();
1574          } catch (ArrayStoreException success) {}
1575      }
1576  
1362
1577      /**
1578       * iterator iterates through all elements
1579       */
# Line 1390 | Line 1604 | public class LinkedBlockingDequeTest ext
1604          assertFalse(it.hasNext());
1605      }
1606  
1393
1607      /**
1608       * iterator ordering is FIFO
1609       */
# 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 1485 | Line 1697 | public class LinkedBlockingDequeTest ext
1697          }
1698      }
1699  
1488
1700      /**
1701       * toString contains toStrings of elements
1702       */
# Line 1493 | 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  
1500
1711      /**
1712       * offer transfers elements across Executor tasks
1713       */
# Line 1506 | 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                  assertFalse(q.offer(three));
1723 <                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
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);
1730 >                threadsStarted.await();
1731                  assertSame(one, q.take());
1732              }});
1733  
# Line 1523 | Line 1735 | public class LinkedBlockingDequeTest ext
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                  assertNull(q.poll());
1747 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1748 <                assertTrue(q.isEmpty());
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 1652 | 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 1661 | 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