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.13 by jsr166, Sat Nov 21 19:11:53 2009 UTC vs.
Revision 1.17 by jsr166, Sun Nov 22 00:17:37 2009 UTC

# Line 169 | Line 169 | public class LinkedBlockingDequeTest ext
169              q.getFirst();
170              shouldThrow();
171          } catch (NoSuchElementException success) {}
172 +        assertNull(q.peekFirst());
173      }
174  
175      /**
# Line 199 | Line 200 | public class LinkedBlockingDequeTest ext
200              q.removeFirst();
201              shouldThrow();
202          } catch (NoSuchElementException success) {}
203 +        assertNull(q.peekFirst());
204 +    }
205 +
206 +    /**
207 +     *  removeLast removes last element, or throws NSEE if empty
208 +     */
209 +    public void testRemoveLast() {
210 +        LinkedBlockingDeque q = populatedDeque(SIZE);
211 +        for (int i = SIZE - 1; i >= 0; --i) {
212 +            assertEquals(i, ((Integer)q.removeLast()).intValue());
213 +        }
214 +        try {
215 +            q.removeLast();
216 +            shouldThrow();
217 +        } catch (NoSuchElementException success) {}
218 +        assertNull(q.peekLast());
219      }
220  
221      /**
# Line 523 | Line 540 | public class LinkedBlockingDequeTest ext
540              shouldThrow();
541          } catch (IllegalStateException success) {}
542      }
543 +
544      /**
545       * Deque contains all elements, in traversal order, of successful addAll
546       */
# Line 567 | Line 585 | public class LinkedBlockingDequeTest ext
585       * put blocks interruptibly if full
586       */
587      public void testBlockingPut() throws InterruptedException {
588 +        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
589          Thread t = new Thread(new CheckedRunnable() {
590 <            public void realRun() {
591 <                int added = 0;
590 >            public void realRun() throws InterruptedException {
591 >                for (int i = 0; i < SIZE; ++i)
592 >                    q.put(i);
593 >                assertEquals(SIZE, q.size());
594 >                assertEquals(0, q.remainingCapacity());
595                  try {
596 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
597 <                    for (int i = 0; i < SIZE; ++i) {
598 <                        q.put(new Integer(i));
577 <                        ++added;
578 <                    }
579 <                    q.put(new Integer(SIZE));
580 <                    threadShouldThrow();
581 <                } catch (InterruptedException success) {
582 <                    threadAssertEquals(added, SIZE);
583 <                }
596 >                    q.put(99);
597 >                    shouldThrow();
598 >                } catch (InterruptedException success) {}
599              }});
600  
601          t.start();
602          Thread.sleep(SHORT_DELAY_MS);
603          t.interrupt();
604          t.join();
605 +        assertEquals(SIZE, q.size());
606 +        assertEquals(0, q.remainingCapacity());
607      }
608  
609      /**
610       * put blocks waiting for take when full
611       */
612      public void testPutWithTake() throws InterruptedException {
613 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
613 >        final int capacity = 2;
614 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
615          Thread t = new Thread(new CheckedRunnable() {
616 <            public void realRun() {
617 <                int added = 0;
616 >            public void realRun() throws InterruptedException {
617 >                for (int i = 0; i < capacity + 1; i++)
618 >                    q.put(i);
619                  try {
620 <                    q.put(new Object());
621 <                    ++added;
622 <                    q.put(new Object());
604 <                    ++added;
605 <                    q.put(new Object());
606 <                    ++added;
607 <                    q.put(new Object());
608 <                    ++added;
609 <                    threadShouldThrow();
610 <                } catch (InterruptedException success) {
611 <                    threadAssertTrue(added >= 2);
612 <                }
620 >                    q.put(99);
621 >                    shouldThrow();
622 >                } catch (InterruptedException success) {}
623              }});
624  
625          t.start();
626          Thread.sleep(SHORT_DELAY_MS);
627 <        q.take();
627 >        assertEquals(q.remainingCapacity(), 0);
628 >        assertEquals(0, q.take());
629 >        Thread.sleep(SHORT_DELAY_MS);
630          t.interrupt();
631          t.join();
632 +        assertEquals(q.remainingCapacity(), 0);
633      }
634  
635      /**
# Line 624 | Line 637 | public class LinkedBlockingDequeTest ext
637       */
638      public void testTimedOffer() throws InterruptedException {
639          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
640 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
640 >        Thread t = new Thread(new CheckedRunnable() {
641              public void realRun() throws InterruptedException {
642                  q.put(new Object());
643                  q.put(new Object());
644 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
645 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
646 <            }};
644 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
645 >                try {
646 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
647 >                    shouldThrow();
648 >                } catch (InterruptedException success) {}
649 >            }});
650  
651          t.start();
652          Thread.sleep(SMALL_DELAY_MS);
# Line 668 | Line 684 | public class LinkedBlockingDequeTest ext
684       * Take removes existing elements until empty, then blocks interruptibly
685       */
686      public void testBlockingTake() throws InterruptedException {
687 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
687 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
688 >        Thread t = new Thread(new CheckedRunnable() {
689              public void realRun() throws InterruptedException {
673                LinkedBlockingDeque q = populatedDeque(SIZE);
690                  for (int i = 0; i < SIZE; ++i) {
691                      assertEquals(i, ((Integer)q.take()).intValue());
692                  }
693 <                q.take();
694 <            }};
693 >                try {
694 >                    q.take();
695 >                    shouldThrow();
696 >                } catch (InterruptedException success) {}
697 >            }});
698  
699          t.start();
700          Thread.sleep(SHORT_DELAY_MS);
# Line 746 | Line 765 | public class LinkedBlockingDequeTest ext
765       */
766      public void testTimedPollWithOffer() throws InterruptedException {
767          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
768 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
768 >        Thread t = new Thread(new CheckedRunnable() {
769              public void realRun() throws InterruptedException {
770 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
771 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
772 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
773 <            }};
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);
774 >                    shouldThrow();
775 >                } catch (InterruptedException success) {}
776 >            }});
777  
778          t.start();
779          Thread.sleep(SMALL_DELAY_MS);
# Line 789 | Line 811 | public class LinkedBlockingDequeTest ext
811       * putFirst blocks interruptibly if full
812       */
813      public void testBlockingPutFirst() throws InterruptedException {
814 +        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
815          Thread t = new Thread(new CheckedRunnable() {
816 <            public void realRun() {
817 <                int added = 0;
816 >            public void realRun() throws InterruptedException {
817 >                for (int i = 0; i < SIZE; ++i)
818 >                    q.putFirst(i);
819 >                assertEquals(SIZE, q.size());
820 >                assertEquals(0, q.remainingCapacity());
821                  try {
822 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
823 <                    for (int i = 0; i < SIZE; ++i) {
824 <                        q.putFirst(new Integer(i));
799 <                        ++added;
800 <                    }
801 <                    q.putFirst(new Integer(SIZE));
802 <                    threadShouldThrow();
803 <                } catch (InterruptedException success) {
804 <                    threadAssertEquals(added, SIZE);
805 <                }
822 >                    q.putFirst(99);
823 >                    shouldThrow();
824 >                } catch (InterruptedException success) {}
825              }});
826  
827          t.start();
828          Thread.sleep(SHORT_DELAY_MS);
829          t.interrupt();
830          t.join();
831 +        assertEquals(SIZE, q.size());
832 +        assertEquals(0, q.remainingCapacity());
833      }
834  
835      /**
836       * putFirst blocks waiting for take when full
837       */
838      public void testPutFirstWithTake() throws InterruptedException {
839 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
839 >        final int capacity = 2;
840 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
841          Thread t = new Thread(new CheckedRunnable() {
842 <            public void realRun() {
843 <                int added = 0;
842 >            public void realRun() throws InterruptedException {
843 >                for (int i = 0; i < capacity + 1; i++)
844 >                    q.putFirst(i);
845                  try {
846 <                    q.putFirst(new Object());
847 <                    ++added;
848 <                    q.putFirst(new Object());
826 <                    ++added;
827 <                    q.putFirst(new Object());
828 <                    ++added;
829 <                    q.putFirst(new Object());
830 <                    ++added;
831 <                    threadShouldThrow();
832 <                } catch (InterruptedException success) {
833 <                    threadAssertTrue(added >= 2);
834 <                }
846 >                    q.putFirst(99);
847 >                    shouldThrow();
848 >                } catch (InterruptedException success) {}
849              }});
850  
851          t.start();
852          Thread.sleep(SHORT_DELAY_MS);
853 <        q.take();
853 >        assertEquals(q.remainingCapacity(), 0);
854 >        assertEquals(capacity - 1, q.take());
855 >        Thread.sleep(SHORT_DELAY_MS);
856          t.interrupt();
857          t.join();
858 +        assertEquals(q.remainingCapacity(), 0);
859      }
860  
861      /**
# Line 846 | Line 863 | public class LinkedBlockingDequeTest ext
863       */
864      public void testTimedOfferFirst() throws InterruptedException {
865          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
866 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
866 >        Thread t = new Thread(new CheckedRunnable() {
867              public void realRun() throws InterruptedException {
868                  q.putFirst(new Object());
869                  q.putFirst(new Object());
870 <                threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
871 <                q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
872 <            }};
870 >                assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
871 >                try {
872 >                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
873 >                    shouldThrow();
874 >                } catch (InterruptedException success) {}
875 >            }});
876  
877          t.start();
878          Thread.sleep(SMALL_DELAY_MS);
# Line 890 | Line 910 | public class LinkedBlockingDequeTest ext
910       * TakeFirst removes existing elements until empty, then blocks interruptibly
911       */
912      public void testBlockingTakeFirst() throws InterruptedException {
913 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
913 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
914 >        Thread t = new Thread(new CheckedRunnable() {
915              public void realRun() throws InterruptedException {
916 <                LinkedBlockingDeque q = populatedDeque(SIZE);
917 <                for (int i = 0; i < SIZE; ++i) {
918 <                    assertEquals(i, ((Integer)q.takeFirst()).intValue());
919 <                }
920 <                q.takeFirst();
921 <            }};
916 >                for (int i = 0; i < SIZE; ++i)
917 >                    assertEquals(i, q.takeFirst());
918 >                try {
919 >                    q.takeFirst();
920 >                    shouldThrow();
921 >                } catch (InterruptedException success) {}
922 >            }});
923  
924          t.start();
925          Thread.sleep(SHORT_DELAY_MS);
# Line 957 | Line 979 | public class LinkedBlockingDequeTest ext
979       */
980      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
981          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
982 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
982 >        Thread t = new Thread(new CheckedRunnable() {
983              public void realRun() throws InterruptedException {
984 <                threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
985 <                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
986 <                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
987 <            }};
984 >                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
985 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
986 >                try {
987 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
988 >                    shouldThrow();
989 >                } catch (InterruptedException success) {}
990 >            }});
991  
992          t.start();
993          Thread.sleep(SMALL_DELAY_MS);
# Line 999 | Line 1024 | public class LinkedBlockingDequeTest ext
1024       * putLast blocks interruptibly if full
1025       */
1026      public void testBlockingPutLast() throws InterruptedException {
1027 +        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1028          Thread t = new Thread(new CheckedRunnable() {
1029 <            public void realRun() {
1030 <                int added = 0;
1029 >            public void realRun() throws InterruptedException {
1030 >                for (int i = 0; i < SIZE; ++i)
1031 >                    q.putLast(i);
1032 >                assertEquals(SIZE, q.size());
1033 >                assertEquals(0, q.remainingCapacity());
1034                  try {
1035 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1036 <                    for (int i = 0; i < SIZE; ++i) {
1037 <                        q.putLast(new Integer(i));
1009 <                        ++added;
1010 <                    }
1011 <                    q.putLast(new Integer(SIZE));
1012 <                    threadShouldThrow();
1013 <                } catch (InterruptedException success) {
1014 <                    threadAssertEquals(added, SIZE);
1015 <                }
1035 >                    q.putLast(99);
1036 >                    shouldThrow();
1037 >                } catch (InterruptedException success) {}
1038              }});
1039  
1040          t.start();
1041          Thread.sleep(SHORT_DELAY_MS);
1042          t.interrupt();
1043          t.join();
1044 +        assertEquals(SIZE, q.size());
1045 +        assertEquals(0, q.remainingCapacity());
1046      }
1047  
1048      /**
1049       * putLast blocks waiting for take when full
1050       */
1051      public void testPutLastWithTake() throws InterruptedException {
1052 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1052 >        final int capacity = 2;
1053 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1054          Thread t = new Thread(new CheckedRunnable() {
1055 <            public void realRun() {
1056 <                int added = 0;
1055 >            public void realRun() throws InterruptedException {
1056 >                for (int i = 0; i < capacity + 1; i++)
1057 >                    q.putLast(i);
1058                  try {
1059 <                    q.putLast(new Object());
1060 <                    ++added;
1061 <                    q.putLast(new Object());
1036 <                    ++added;
1037 <                    q.putLast(new Object());
1038 <                    ++added;
1039 <                    q.putLast(new Object());
1040 <                    ++added;
1041 <                    threadShouldThrow();
1042 <                } catch (InterruptedException success) {
1043 <                    threadAssertTrue(added >= 2);
1044 <                }
1059 >                    q.putLast(99);
1060 >                    shouldThrow();
1061 >                } catch (InterruptedException success) {}
1062              }});
1063  
1064          t.start();
1065          Thread.sleep(SHORT_DELAY_MS);
1066 <        q.take();
1066 >        assertEquals(q.remainingCapacity(), 0);
1067 >        assertEquals(0, q.take());
1068 >        Thread.sleep(SHORT_DELAY_MS);
1069          t.interrupt();
1070          t.join();
1071 +        assertEquals(q.remainingCapacity(), 0);
1072      }
1073  
1074      /**
# Line 1056 | Line 1076 | public class LinkedBlockingDequeTest ext
1076       */
1077      public void testTimedOfferLast() throws InterruptedException {
1078          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1079 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1079 >        Thread t = new Thread(new CheckedRunnable() {
1080              public void realRun() throws InterruptedException {
1081                  q.putLast(new Object());
1082                  q.putLast(new Object());
1083 <                threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1084 <                q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1085 <            }};
1083 >                assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1084 >                try {
1085 >                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1086 >                    shouldThrow();
1087 >                } catch (InterruptedException success) {}
1088 >            }});
1089  
1090          t.start();
1091          Thread.sleep(SMALL_DELAY_MS);
# Line 1100 | Line 1123 | public class LinkedBlockingDequeTest ext
1123       * TakeLast removes existing elements until empty, then blocks interruptibly
1124       */
1125      public void testBlockingTakeLast() throws InterruptedException {
1126 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1126 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1127 >        Thread t = new Thread(new CheckedRunnable() {
1128              public void realRun() throws InterruptedException {
1129 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1130 <                for (int i = 0; i < SIZE; ++i) {
1131 <                    assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1132 <                }
1133 <                q.takeLast();
1134 <            }};
1129 >                for (int i = 0; i < SIZE; ++i)
1130 >                    assertEquals(SIZE - 1 - i, q.takeLast());
1131 >                try {
1132 >                    q.takeLast();
1133 >                    shouldThrow();
1134 >                } catch (InterruptedException success) {}
1135 >            }});
1136  
1137          t.start();
1138          Thread.sleep(SHORT_DELAY_MS);
# Line 1115 | Line 1140 | public class LinkedBlockingDequeTest ext
1140          t.join();
1141      }
1142  
1118
1143      /**
1144       * timed pollLast with zero timeout succeeds when non-empty, else times out
1145       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines