98 |
|
public void testPollFirst() { |
99 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
100 |
|
for (int i = 0; i < SIZE; ++i) { |
101 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
101 |
> |
assertEquals(i, q.pollFirst()); |
102 |
|
} |
103 |
|
assertNull(q.pollFirst()); |
104 |
|
} |
109 |
|
public void testPollLast() { |
110 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
111 |
|
for (int i = SIZE-1; i >= 0; --i) { |
112 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
112 |
> |
assertEquals(i, q.pollLast()); |
113 |
|
} |
114 |
|
assertNull(q.pollLast()); |
115 |
|
} |
120 |
|
public void testPeekFirst() { |
121 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
122 |
|
for (int i = 0; i < SIZE; ++i) { |
123 |
< |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
124 |
< |
q.pollFirst(); |
123 |
> |
assertEquals(i, q.peekFirst()); |
124 |
> |
assertEquals(i, q.pollFirst()); |
125 |
|
assertTrue(q.peekFirst() == null || |
126 |
< |
i != ((Integer)q.peekFirst()).intValue()); |
126 |
> |
!q.peekFirst().equals(i)); |
127 |
|
} |
128 |
|
assertNull(q.peekFirst()); |
129 |
|
} |
134 |
|
public void testPeek() { |
135 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
136 |
|
for (int i = 0; i < SIZE; ++i) { |
137 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
138 |
< |
q.pollFirst(); |
137 |
> |
assertEquals(i, q.peek()); |
138 |
> |
assertEquals(i, q.pollFirst()); |
139 |
|
assertTrue(q.peek() == null || |
140 |
< |
i != ((Integer)q.peek()).intValue()); |
140 |
> |
!q.peek().equals(i)); |
141 |
|
} |
142 |
|
assertNull(q.peek()); |
143 |
|
} |
148 |
|
public void testPeekLast() { |
149 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
150 |
|
for (int i = SIZE-1; i >= 0; --i) { |
151 |
< |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
152 |
< |
q.pollLast(); |
151 |
> |
assertEquals(i, q.peekLast()); |
152 |
> |
assertEquals(i, q.pollLast()); |
153 |
|
assertTrue(q.peekLast() == null || |
154 |
< |
i != ((Integer)q.peekLast()).intValue()); |
154 |
> |
!q.peekLast().equals(i)); |
155 |
|
} |
156 |
|
assertNull(q.peekLast()); |
157 |
|
} |
162 |
|
public void testFirstElement() { |
163 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
164 |
|
for (int i = 0; i < SIZE; ++i) { |
165 |
< |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
166 |
< |
q.pollFirst(); |
165 |
> |
assertEquals(i, q.getFirst()); |
166 |
> |
assertEquals(i, q.pollFirst()); |
167 |
|
} |
168 |
|
try { |
169 |
|
q.getFirst(); |
178 |
|
public void testLastElement() { |
179 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
180 |
|
for (int i = SIZE-1; i >= 0; --i) { |
181 |
< |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
182 |
< |
q.pollLast(); |
181 |
> |
assertEquals(i, q.getLast()); |
182 |
> |
assertEquals(i, q.pollLast()); |
183 |
|
} |
184 |
|
try { |
185 |
|
q.getLast(); |
194 |
|
public void testRemoveFirst() { |
195 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
196 |
|
for (int i = 0; i < SIZE; ++i) { |
197 |
< |
assertEquals(i, ((Integer)q.removeFirst()).intValue()); |
197 |
> |
assertEquals(i, q.removeFirst()); |
198 |
|
} |
199 |
|
try { |
200 |
|
q.removeFirst(); |
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()); |
212 |
> |
assertEquals(i, q.removeLast()); |
213 |
|
} |
214 |
|
try { |
215 |
|
q.removeLast(); |
224 |
|
public void testRemove() { |
225 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
226 |
|
for (int i = 0; i < SIZE; ++i) { |
227 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
227 |
> |
assertEquals(i, q.remove()); |
228 |
|
} |
229 |
|
try { |
230 |
|
q.remove(); |
447 |
|
public void testPop() { |
448 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
449 |
|
for (int i = 0; i < SIZE; ++i) { |
450 |
< |
assertEquals(i, ((Integer)q.pop()).intValue()); |
450 |
> |
assertEquals(i, q.pop()); |
451 |
|
} |
452 |
|
try { |
453 |
|
q.pop(); |
540 |
|
shouldThrow(); |
541 |
|
} catch (IllegalStateException success) {} |
542 |
|
} |
543 |
+ |
|
544 |
|
/** |
545 |
|
* Deque contains all elements, in traversal order, of successful addAll |
546 |
|
*/ |
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)); |
594 |
< |
++added; |
595 |
< |
} |
596 |
< |
q.put(new Integer(SIZE)); |
597 |
< |
threadShouldThrow(); |
598 |
< |
} catch (InterruptedException success) { |
599 |
< |
threadAssertEquals(added, SIZE); |
600 |
< |
} |
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()); |
621 |
< |
++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 |
< |
} |
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 |
|
/** |
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); |
660 |
|
public void testTake() throws InterruptedException { |
661 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
662 |
|
for (int i = 0; i < SIZE; ++i) { |
663 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
663 |
> |
assertEquals(i, q.take()); |
664 |
|
} |
665 |
|
} |
666 |
|
|
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 { |
690 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
690 |
|
for (int i = 0; i < SIZE; ++i) { |
691 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
691 |
> |
assertEquals(i, q.take()); |
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); |
709 |
|
public void testPoll() { |
710 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
711 |
|
for (int i = 0; i < SIZE; ++i) { |
712 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
712 |
> |
assertEquals(i, q.poll()); |
713 |
|
} |
714 |
|
assertNull(q.poll()); |
715 |
|
} |
720 |
|
public void testTimedPoll0() throws InterruptedException { |
721 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
722 |
|
for (int i = 0; i < SIZE; ++i) { |
723 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
723 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
724 |
|
} |
725 |
|
assertNull(q.poll(0, MILLISECONDS)); |
726 |
|
} |
731 |
|
public void testTimedPoll() throws InterruptedException { |
732 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
733 |
|
for (int i = 0; i < SIZE; ++i) { |
734 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
734 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
735 |
|
} |
736 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
737 |
|
} |
745 |
|
public void realRun() throws InterruptedException { |
746 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
747 |
|
for (int i = 0; i < SIZE; ++i) { |
748 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
748 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
749 |
|
} |
750 |
|
try { |
751 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
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)); |
819 |
< |
++added; |
820 |
< |
} |
821 |
< |
q.putFirst(new Integer(SIZE)); |
822 |
< |
threadShouldThrow(); |
823 |
< |
} catch (InterruptedException success) { |
824 |
< |
threadAssertEquals(added, SIZE); |
825 |
< |
} |
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()); |
846 |
< |
++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 |
< |
} |
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 |
|
/** |
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); |
886 |
|
public void testTakeFirst() throws InterruptedException { |
887 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
888 |
|
for (int i = 0; i < SIZE; ++i) { |
889 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
889 |
> |
assertEquals(i, q.takeFirst()); |
890 |
|
} |
891 |
|
} |
892 |
|
|
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); |
934 |
|
public void testTimedPollFirst0() throws InterruptedException { |
935 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
936 |
|
for (int i = 0; i < SIZE; ++i) { |
937 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue()); |
937 |
> |
assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
938 |
|
} |
939 |
|
assertNull(q.pollFirst(0, MILLISECONDS)); |
940 |
|
} |
945 |
|
public void testTimedPollFirst() throws InterruptedException { |
946 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
947 |
|
for (int i = 0; i < SIZE; ++i) { |
948 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
948 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
949 |
|
} |
950 |
|
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
951 |
|
} |
959 |
|
public void realRun() throws InterruptedException { |
960 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
961 |
|
for (int i = 0; i < SIZE; ++i) { |
962 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
962 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
963 |
|
} |
964 |
|
try { |
965 |
|
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
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)); |
1032 |
< |
++added; |
1033 |
< |
} |
1034 |
< |
q.putLast(new Integer(SIZE)); |
1035 |
< |
threadShouldThrow(); |
1036 |
< |
} catch (InterruptedException success) { |
1037 |
< |
threadAssertEquals(added, SIZE); |
1038 |
< |
} |
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()); |
1059 |
< |
++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 |
< |
} |
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 |
|
/** |
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); |
1099 |
|
public void testTakeLast() throws InterruptedException { |
1100 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1101 |
|
for (int i = 0; i < SIZE; ++i) { |
1102 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1102 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1103 |
|
} |
1104 |
|
} |
1105 |
|
|
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); |
1140 |
|
t.join(); |
1141 |
|
} |
1142 |
|
|
1141 |
– |
|
1143 |
|
/** |
1144 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1145 |
|
*/ |
1146 |
|
public void testTimedPollLast0() throws InterruptedException { |
1147 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1148 |
|
for (int i = 0; i < SIZE; ++i) { |
1149 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue()); |
1149 |
> |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1150 |
|
} |
1151 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1152 |
|
} |
1157 |
|
public void testTimedPollLast() throws InterruptedException { |
1158 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1159 |
|
for (int i = 0; i < SIZE; ++i) { |
1160 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1160 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1161 |
|
} |
1162 |
|
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1163 |
|
} |
1171 |
|
public void realRun() throws InterruptedException { |
1172 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1173 |
|
for (int i = 0; i < SIZE; ++i) { |
1174 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1174 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1175 |
|
} |
1176 |
|
try { |
1177 |
|
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1215 |
|
public void testElement() { |
1216 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1217 |
|
for (int i = 0; i < SIZE; ++i) { |
1218 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
1218 |
> |
assertEquals(i, q.element()); |
1219 |
|
q.poll(); |
1220 |
|
} |
1221 |
|
try { |
1341 |
|
* toArray(null) throws NPE |
1342 |
|
*/ |
1343 |
|
public void testToArray_BadArg() { |
1344 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1345 |
|
try { |
1344 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1346 |
|
Object o[] = q.toArray(null); |
1347 |
|
shouldThrow(); |
1348 |
|
} catch (NullPointerException success) {} |
1352 |
|
* toArray with incompatible array type throws CCE |
1353 |
|
*/ |
1354 |
|
public void testToArray1_BadArg() { |
1355 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1356 |
|
try { |
1357 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1356 |
< |
Object o[] = q.toArray(new String[10] ); |
1357 |
> |
Object o[] = q.toArray(new String[10]); |
1358 |
|
shouldThrow(); |
1359 |
|
} catch (ArrayStoreException success) {} |
1360 |
|
} |
1402 |
|
assertEquals(0, q.remainingCapacity()); |
1403 |
|
int k = 0; |
1404 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
1405 |
< |
int i = ((Integer)(it.next())).intValue(); |
1405 |
< |
assertEquals(++k, i); |
1405 |
> |
assertEquals(++k, it.next()); |
1406 |
|
} |
1407 |
|
assertEquals(3, k); |
1408 |
|
} |
1453 |
|
q.add(new Integer(1)); |
1454 |
|
int k = 0; |
1455 |
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1456 |
< |
int i = ((Integer)(it.next())).intValue(); |
1457 |
< |
assertEquals(++k, i); |
1456 |
> |
assertEquals(++k, it.next()); |
1457 |
|
} |
1458 |
|
|
1459 |
|
assertEquals(3, k); |
1508 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1509 |
|
executor.execute(new CheckedRunnable() { |
1510 |
|
public void realRun() throws InterruptedException { |
1511 |
< |
threadAssertFalse(q.offer(three)); |
1512 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1513 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1511 |
> |
assertFalse(q.offer(three)); |
1512 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1513 |
> |
assertEquals(0, q.remainingCapacity()); |
1514 |
|
}}); |
1515 |
|
|
1516 |
|
executor.execute(new CheckedRunnable() { |
1517 |
|
public void realRun() throws InterruptedException { |
1518 |
|
Thread.sleep(SMALL_DELAY_MS); |
1519 |
< |
threadAssertEquals(one, q.take()); |
1519 |
> |
assertSame(one, q.take()); |
1520 |
|
}}); |
1521 |
|
|
1522 |
|
joinPool(executor); |
1530 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1531 |
|
executor.execute(new CheckedRunnable() { |
1532 |
|
public void realRun() throws InterruptedException { |
1533 |
< |
threadAssertNull(q.poll()); |
1534 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1535 |
< |
threadAssertTrue(q.isEmpty()); |
1533 |
> |
assertNull(q.poll()); |
1534 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1535 |
> |
assertTrue(q.isEmpty()); |
1536 |
|
}}); |
1537 |
|
|
1538 |
|
executor.execute(new CheckedRunnable() { |