12 |
|
|
13 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
17 |
|
|
18 |
|
public static Test suite() { |
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 |
|
} |
158 |
|
|
159 |
|
/** |
160 |
< |
* getFirst returns next getFirst, or throws NSEE if empty |
160 |
> |
* getFirst() returns first element, or throws NSEE if empty |
161 |
|
*/ |
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(); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
< |
* getLast returns next element, or throws NSEE if empty |
176 |
> |
* getLast() returns last element, or throws NSEE if empty |
177 |
|
*/ |
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(); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
< |
* removeFirst removes next element, or throws NSEE if empty |
192 |
> |
* removeFirst() removes first element, or throws NSEE if empty |
193 |
|
*/ |
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(); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
< |
* removeLast removes last element, or throws NSEE if empty |
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()); |
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(); |
269 |
|
LinkedBlockingDeque q = populatedDeque(3); |
270 |
|
q.pollLast(); |
271 |
|
q.addFirst(four); |
272 |
< |
assertEquals(four,q.peekFirst()); |
272 |
> |
assertSame(four, q.peekFirst()); |
273 |
|
} |
274 |
|
|
275 |
|
/** |
279 |
|
LinkedBlockingDeque q = populatedDeque(3); |
280 |
|
q.pollLast(); |
281 |
|
q.addLast(four); |
282 |
< |
assertEquals(four,q.peekLast()); |
282 |
> |
assertSame(four, q.peekLast()); |
283 |
|
} |
284 |
|
|
285 |
|
|
437 |
|
LinkedBlockingDeque q = populatedDeque(3); |
438 |
|
q.pollLast(); |
439 |
|
q.push(four); |
440 |
< |
assertEquals(four,q.peekFirst()); |
440 |
> |
assertSame(four, q.peekFirst()); |
441 |
|
} |
442 |
|
|
443 |
|
|
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(); |
513 |
|
shouldThrow(); |
514 |
|
} catch (NullPointerException success) {} |
515 |
|
} |
516 |
+ |
|
517 |
|
/** |
518 |
|
* addAll of a collection with any null elements throws NPE after |
519 |
|
* possibly adding some elements |
528 |
|
shouldThrow(); |
529 |
|
} catch (NullPointerException success) {} |
530 |
|
} |
531 |
+ |
|
532 |
|
/** |
533 |
|
* addAll throws ISE if not enough room |
534 |
|
*/ |
542 |
|
shouldThrow(); |
543 |
|
} catch (IllegalStateException success) {} |
544 |
|
} |
545 |
+ |
|
546 |
|
/** |
547 |
|
* Deque contains all elements, in traversal order, of successful addAll |
548 |
|
*/ |
587 |
|
* put blocks interruptibly if full |
588 |
|
*/ |
589 |
|
public void testBlockingPut() throws InterruptedException { |
590 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
591 |
|
Thread t = new Thread(new CheckedRunnable() { |
592 |
< |
public void realRun() { |
593 |
< |
int added = 0; |
592 |
> |
public void realRun() throws InterruptedException { |
593 |
> |
for (int i = 0; i < SIZE; ++i) |
594 |
> |
q.put(i); |
595 |
> |
assertEquals(SIZE, q.size()); |
596 |
> |
assertEquals(0, q.remainingCapacity()); |
597 |
|
try { |
598 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
599 |
< |
for (int i = 0; i < SIZE; ++i) { |
600 |
< |
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 |
< |
} |
598 |
> |
q.put(99); |
599 |
> |
shouldThrow(); |
600 |
> |
} catch (InterruptedException success) {} |
601 |
|
}}); |
602 |
|
|
603 |
|
t.start(); |
604 |
|
Thread.sleep(SHORT_DELAY_MS); |
605 |
|
t.interrupt(); |
606 |
|
t.join(); |
607 |
+ |
assertEquals(SIZE, q.size()); |
608 |
+ |
assertEquals(0, q.remainingCapacity()); |
609 |
|
} |
610 |
|
|
611 |
|
/** |
612 |
|
* put blocks waiting for take when full |
613 |
|
*/ |
614 |
|
public void testPutWithTake() throws InterruptedException { |
615 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
615 |
> |
final int capacity = 2; |
616 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
617 |
|
Thread t = new Thread(new CheckedRunnable() { |
618 |
< |
public void realRun() { |
619 |
< |
int added = 0; |
618 |
> |
public void realRun() throws InterruptedException { |
619 |
> |
for (int i = 0; i < capacity + 1; i++) |
620 |
> |
q.put(i); |
621 |
|
try { |
622 |
< |
q.put(new Object()); |
623 |
< |
++added; |
624 |
< |
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 |
< |
} |
622 |
> |
q.put(99); |
623 |
> |
shouldThrow(); |
624 |
> |
} catch (InterruptedException success) {} |
625 |
|
}}); |
626 |
|
|
627 |
|
t.start(); |
628 |
|
Thread.sleep(SHORT_DELAY_MS); |
629 |
< |
q.take(); |
629 |
> |
assertEquals(q.remainingCapacity(), 0); |
630 |
> |
assertEquals(0, q.take()); |
631 |
> |
Thread.sleep(SHORT_DELAY_MS); |
632 |
|
t.interrupt(); |
633 |
|
t.join(); |
634 |
+ |
assertEquals(q.remainingCapacity(), 0); |
635 |
|
} |
636 |
|
|
637 |
|
/** |
639 |
|
*/ |
640 |
|
public void testTimedOffer() throws InterruptedException { |
641 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
642 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
642 |
> |
Thread t = new Thread(new CheckedRunnable() { |
643 |
|
public void realRun() throws InterruptedException { |
644 |
|
q.put(new Object()); |
645 |
|
q.put(new Object()); |
646 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
647 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
648 |
< |
}}; |
646 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
647 |
> |
try { |
648 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
649 |
> |
shouldThrow(); |
650 |
> |
} catch (InterruptedException success) {} |
651 |
> |
}}); |
652 |
|
|
653 |
|
t.start(); |
654 |
|
Thread.sleep(SMALL_DELAY_MS); |
662 |
|
public void testTake() throws InterruptedException { |
663 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
664 |
|
for (int i = 0; i < SIZE; ++i) { |
665 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
665 |
> |
assertEquals(i, q.take()); |
666 |
|
} |
667 |
|
} |
668 |
|
|
686 |
|
* Take removes existing elements until empty, then blocks interruptibly |
687 |
|
*/ |
688 |
|
public void testBlockingTake() throws InterruptedException { |
689 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
689 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
690 |
> |
Thread t = new Thread(new CheckedRunnable() { |
691 |
|
public void realRun() throws InterruptedException { |
690 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
692 |
|
for (int i = 0; i < SIZE; ++i) { |
693 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
693 |
> |
assertEquals(i, q.take()); |
694 |
|
} |
695 |
< |
q.take(); |
696 |
< |
}}; |
695 |
> |
try { |
696 |
> |
q.take(); |
697 |
> |
shouldThrow(); |
698 |
> |
} catch (InterruptedException success) {} |
699 |
> |
}}); |
700 |
|
|
701 |
|
t.start(); |
702 |
|
Thread.sleep(SHORT_DELAY_MS); |
711 |
|
public void testPoll() { |
712 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
713 |
|
for (int i = 0; i < SIZE; ++i) { |
714 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
714 |
> |
assertEquals(i, q.poll()); |
715 |
|
} |
716 |
|
assertNull(q.poll()); |
717 |
|
} |
722 |
|
public void testTimedPoll0() throws InterruptedException { |
723 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
724 |
|
for (int i = 0; i < SIZE; ++i) { |
725 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
725 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
726 |
|
} |
727 |
|
assertNull(q.poll(0, MILLISECONDS)); |
728 |
|
} |
733 |
|
public void testTimedPoll() throws InterruptedException { |
734 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
735 |
|
for (int i = 0; i < SIZE; ++i) { |
736 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
736 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
737 |
|
} |
738 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
739 |
|
} |
747 |
|
public void realRun() throws InterruptedException { |
748 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
749 |
|
for (int i = 0; i < SIZE; ++i) { |
750 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
750 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
751 |
|
} |
752 |
|
try { |
753 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
769 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
770 |
|
Thread t = new Thread(new CheckedRunnable() { |
771 |
|
public void realRun() throws InterruptedException { |
768 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
769 |
– |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
772 |
|
try { |
773 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
774 |
+ |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
775 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
776 |
|
shouldThrow(); |
777 |
|
} catch (InterruptedException success) {} |
813 |
|
* putFirst blocks interruptibly if full |
814 |
|
*/ |
815 |
|
public void testBlockingPutFirst() throws InterruptedException { |
816 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
817 |
|
Thread t = new Thread(new CheckedRunnable() { |
818 |
< |
public void realRun() { |
819 |
< |
int added = 0; |
818 |
> |
public void realRun() throws InterruptedException { |
819 |
> |
for (int i = 0; i < SIZE; ++i) |
820 |
> |
q.putFirst(i); |
821 |
> |
assertEquals(SIZE, q.size()); |
822 |
> |
assertEquals(0, q.remainingCapacity()); |
823 |
|
try { |
824 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
825 |
< |
for (int i = 0; i < SIZE; ++i) { |
826 |
< |
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 |
< |
} |
824 |
> |
q.putFirst(99); |
825 |
> |
shouldThrow(); |
826 |
> |
} catch (InterruptedException success) {} |
827 |
|
}}); |
828 |
|
|
829 |
|
t.start(); |
830 |
|
Thread.sleep(SHORT_DELAY_MS); |
831 |
|
t.interrupt(); |
832 |
|
t.join(); |
833 |
+ |
assertEquals(SIZE, q.size()); |
834 |
+ |
assertEquals(0, q.remainingCapacity()); |
835 |
|
} |
836 |
|
|
837 |
|
/** |
838 |
|
* putFirst blocks waiting for take when full |
839 |
|
*/ |
840 |
|
public void testPutFirstWithTake() throws InterruptedException { |
841 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
841 |
> |
final int capacity = 2; |
842 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
843 |
|
Thread t = new Thread(new CheckedRunnable() { |
844 |
< |
public void realRun() { |
845 |
< |
int added = 0; |
844 |
> |
public void realRun() throws InterruptedException { |
845 |
> |
for (int i = 0; i < capacity + 1; i++) |
846 |
> |
q.putFirst(i); |
847 |
|
try { |
848 |
< |
q.putFirst(new Object()); |
849 |
< |
++added; |
850 |
< |
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 |
< |
} |
848 |
> |
q.putFirst(99); |
849 |
> |
shouldThrow(); |
850 |
> |
} catch (InterruptedException success) {} |
851 |
|
}}); |
852 |
|
|
853 |
|
t.start(); |
854 |
|
Thread.sleep(SHORT_DELAY_MS); |
855 |
< |
q.take(); |
855 |
> |
assertEquals(q.remainingCapacity(), 0); |
856 |
> |
assertEquals(capacity - 1, q.take()); |
857 |
> |
Thread.sleep(SHORT_DELAY_MS); |
858 |
|
t.interrupt(); |
859 |
|
t.join(); |
860 |
+ |
assertEquals(q.remainingCapacity(), 0); |
861 |
|
} |
862 |
|
|
863 |
|
/** |
865 |
|
*/ |
866 |
|
public void testTimedOfferFirst() throws InterruptedException { |
867 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
868 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
868 |
> |
Thread t = new Thread(new CheckedRunnable() { |
869 |
|
public void realRun() throws InterruptedException { |
870 |
|
q.putFirst(new Object()); |
871 |
|
q.putFirst(new Object()); |
872 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
873 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
874 |
< |
}}; |
872 |
> |
assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
873 |
> |
try { |
874 |
> |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
875 |
> |
shouldThrow(); |
876 |
> |
} catch (InterruptedException success) {} |
877 |
> |
}}); |
878 |
|
|
879 |
|
t.start(); |
880 |
|
Thread.sleep(SMALL_DELAY_MS); |
888 |
|
public void testTakeFirst() throws InterruptedException { |
889 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
890 |
|
for (int i = 0; i < SIZE; ++i) { |
891 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
891 |
> |
assertEquals(i, q.takeFirst()); |
892 |
|
} |
893 |
|
} |
894 |
|
|
912 |
|
* TakeFirst removes existing elements until empty, then blocks interruptibly |
913 |
|
*/ |
914 |
|
public void testBlockingTakeFirst() throws InterruptedException { |
915 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
915 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
916 |
> |
Thread t = new Thread(new CheckedRunnable() { |
917 |
|
public void realRun() throws InterruptedException { |
918 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
919 |
< |
for (int i = 0; i < SIZE; ++i) { |
920 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
921 |
< |
} |
922 |
< |
q.takeFirst(); |
923 |
< |
}}; |
918 |
> |
for (int i = 0; i < SIZE; ++i) |
919 |
> |
assertEquals(i, q.takeFirst()); |
920 |
> |
try { |
921 |
> |
q.takeFirst(); |
922 |
> |
shouldThrow(); |
923 |
> |
} catch (InterruptedException success) {} |
924 |
> |
}}); |
925 |
|
|
926 |
|
t.start(); |
927 |
|
Thread.sleep(SHORT_DELAY_MS); |
936 |
|
public void testTimedPollFirst0() throws InterruptedException { |
937 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
938 |
|
for (int i = 0; i < SIZE; ++i) { |
939 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue()); |
939 |
> |
assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
940 |
|
} |
941 |
|
assertNull(q.pollFirst(0, MILLISECONDS)); |
942 |
|
} |
947 |
|
public void testTimedPollFirst() throws InterruptedException { |
948 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
949 |
|
for (int i = 0; i < SIZE; ++i) { |
950 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
950 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
951 |
|
} |
952 |
|
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
953 |
|
} |
961 |
|
public void realRun() throws InterruptedException { |
962 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
963 |
|
for (int i = 0; i < SIZE; ++i) { |
964 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
964 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
965 |
|
} |
966 |
|
try { |
967 |
|
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1026 |
|
* putLast blocks interruptibly if full |
1027 |
|
*/ |
1028 |
|
public void testBlockingPutLast() throws InterruptedException { |
1029 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1030 |
|
Thread t = new Thread(new CheckedRunnable() { |
1031 |
< |
public void realRun() { |
1032 |
< |
int added = 0; |
1031 |
> |
public void realRun() throws InterruptedException { |
1032 |
> |
for (int i = 0; i < SIZE; ++i) |
1033 |
> |
q.putLast(i); |
1034 |
> |
assertEquals(SIZE, q.size()); |
1035 |
> |
assertEquals(0, q.remainingCapacity()); |
1036 |
|
try { |
1037 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1038 |
< |
for (int i = 0; i < SIZE; ++i) { |
1039 |
< |
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 |
< |
} |
1037 |
> |
q.putLast(99); |
1038 |
> |
shouldThrow(); |
1039 |
> |
} catch (InterruptedException success) {} |
1040 |
|
}}); |
1041 |
|
|
1042 |
|
t.start(); |
1043 |
|
Thread.sleep(SHORT_DELAY_MS); |
1044 |
|
t.interrupt(); |
1045 |
|
t.join(); |
1046 |
+ |
assertEquals(SIZE, q.size()); |
1047 |
+ |
assertEquals(0, q.remainingCapacity()); |
1048 |
|
} |
1049 |
|
|
1050 |
|
/** |
1051 |
|
* putLast blocks waiting for take when full |
1052 |
|
*/ |
1053 |
|
public void testPutLastWithTake() throws InterruptedException { |
1054 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1054 |
> |
final int capacity = 2; |
1055 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1056 |
|
Thread t = new Thread(new CheckedRunnable() { |
1057 |
< |
public void realRun() { |
1058 |
< |
int added = 0; |
1057 |
> |
public void realRun() throws InterruptedException { |
1058 |
> |
for (int i = 0; i < capacity + 1; i++) |
1059 |
> |
q.putLast(i); |
1060 |
|
try { |
1061 |
< |
q.putLast(new Object()); |
1062 |
< |
++added; |
1063 |
< |
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 |
< |
} |
1061 |
> |
q.putLast(99); |
1062 |
> |
shouldThrow(); |
1063 |
> |
} catch (InterruptedException success) {} |
1064 |
|
}}); |
1065 |
|
|
1066 |
|
t.start(); |
1067 |
|
Thread.sleep(SHORT_DELAY_MS); |
1068 |
< |
q.take(); |
1068 |
> |
assertEquals(q.remainingCapacity(), 0); |
1069 |
> |
assertEquals(0, q.take()); |
1070 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1071 |
|
t.interrupt(); |
1072 |
|
t.join(); |
1073 |
+ |
assertEquals(q.remainingCapacity(), 0); |
1074 |
|
} |
1075 |
|
|
1076 |
|
/** |
1078 |
|
*/ |
1079 |
|
public void testTimedOfferLast() throws InterruptedException { |
1080 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1081 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1081 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1082 |
|
public void realRun() throws InterruptedException { |
1083 |
|
q.putLast(new Object()); |
1084 |
|
q.putLast(new Object()); |
1085 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1086 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1087 |
< |
}}; |
1085 |
> |
assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1086 |
> |
try { |
1087 |
> |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1088 |
> |
shouldThrow(); |
1089 |
> |
} catch (InterruptedException success) {} |
1090 |
> |
}}); |
1091 |
|
|
1092 |
|
t.start(); |
1093 |
|
Thread.sleep(SMALL_DELAY_MS); |
1101 |
|
public void testTakeLast() throws InterruptedException { |
1102 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1103 |
|
for (int i = 0; i < SIZE; ++i) { |
1104 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1104 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1105 |
|
} |
1106 |
|
} |
1107 |
|
|
1125 |
|
* TakeLast removes existing elements until empty, then blocks interruptibly |
1126 |
|
*/ |
1127 |
|
public void testBlockingTakeLast() throws InterruptedException { |
1128 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1128 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1129 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1130 |
|
public void realRun() throws InterruptedException { |
1131 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1132 |
< |
for (int i = 0; i < SIZE; ++i) { |
1133 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1134 |
< |
} |
1135 |
< |
q.takeLast(); |
1136 |
< |
}}; |
1131 |
> |
for (int i = 0; i < SIZE; ++i) |
1132 |
> |
assertEquals(SIZE - 1 - i, q.takeLast()); |
1133 |
> |
try { |
1134 |
> |
q.takeLast(); |
1135 |
> |
shouldThrow(); |
1136 |
> |
} catch (InterruptedException success) {} |
1137 |
> |
}}); |
1138 |
|
|
1139 |
|
t.start(); |
1140 |
|
Thread.sleep(SHORT_DELAY_MS); |
1142 |
|
t.join(); |
1143 |
|
} |
1144 |
|
|
1141 |
– |
|
1145 |
|
/** |
1146 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1147 |
|
*/ |
1148 |
|
public void testTimedPollLast0() throws InterruptedException { |
1149 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1150 |
|
for (int i = 0; i < SIZE; ++i) { |
1151 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue()); |
1151 |
> |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1152 |
|
} |
1153 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1154 |
|
} |
1159 |
|
public void testTimedPollLast() throws InterruptedException { |
1160 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1161 |
|
for (int i = 0; i < SIZE; ++i) { |
1162 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1162 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1163 |
|
} |
1164 |
|
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1165 |
|
} |
1173 |
|
public void realRun() throws InterruptedException { |
1174 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1175 |
|
for (int i = 0; i < SIZE; ++i) { |
1176 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1176 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1177 |
|
} |
1178 |
|
try { |
1179 |
|
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1217 |
|
public void testElement() { |
1218 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1219 |
|
for (int i = 0; i < SIZE; ++i) { |
1220 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
1220 |
> |
assertEquals(i, q.element()); |
1221 |
|
q.poll(); |
1222 |
|
} |
1223 |
|
try { |
1343 |
|
* toArray(null) throws NPE |
1344 |
|
*/ |
1345 |
|
public void testToArray_BadArg() { |
1346 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1347 |
|
try { |
1344 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1348 |
|
Object o[] = q.toArray(null); |
1349 |
|
shouldThrow(); |
1350 |
|
} catch (NullPointerException success) {} |
1354 |
|
* toArray with incompatible array type throws CCE |
1355 |
|
*/ |
1356 |
|
public void testToArray1_BadArg() { |
1357 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1358 |
|
try { |
1359 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1356 |
< |
Object o[] = q.toArray(new String[10] ); |
1359 |
> |
Object o[] = q.toArray(new String[10]); |
1360 |
|
shouldThrow(); |
1361 |
|
} catch (ArrayStoreException success) {} |
1362 |
|
} |
1376 |
|
/** |
1377 |
|
* iterator.remove removes current element |
1378 |
|
*/ |
1379 |
< |
public void testIteratorRemove () { |
1379 |
> |
public void testIteratorRemove() { |
1380 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1381 |
|
q.add(two); |
1382 |
|
q.add(one); |
1387 |
|
it.remove(); |
1388 |
|
|
1389 |
|
it = q.iterator(); |
1390 |
< |
assertEquals(it.next(), one); |
1391 |
< |
assertEquals(it.next(), three); |
1390 |
> |
assertSame(it.next(), one); |
1391 |
> |
assertSame(it.next(), three); |
1392 |
|
assertFalse(it.hasNext()); |
1393 |
|
} |
1394 |
|
|
1404 |
|
assertEquals(0, q.remainingCapacity()); |
1405 |
|
int k = 0; |
1406 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
1407 |
< |
int i = ((Integer)(it.next())).intValue(); |
1405 |
< |
assertEquals(++k, i); |
1407 |
> |
assertEquals(++k, it.next()); |
1408 |
|
} |
1409 |
|
assertEquals(3, k); |
1410 |
|
} |
1412 |
|
/** |
1413 |
|
* Modifications do not cause iterators to fail |
1414 |
|
*/ |
1415 |
< |
public void testWeaklyConsistentIteration () { |
1415 |
> |
public void testWeaklyConsistentIteration() { |
1416 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1417 |
|
q.add(one); |
1418 |
|
q.add(two); |
1455 |
|
q.add(new Integer(1)); |
1456 |
|
int k = 0; |
1457 |
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1458 |
< |
int i = ((Integer)(it.next())).intValue(); |
1457 |
< |
assertEquals(++k, i); |
1458 |
> |
assertEquals(++k, it.next()); |
1459 |
|
} |
1460 |
|
|
1461 |
|
assertEquals(3, k); |
1468 |
|
/** |
1469 |
|
* descendingIterator.remove removes current element |
1470 |
|
*/ |
1471 |
< |
public void testDescendingIteratorRemove () { |
1471 |
> |
public void testDescendingIteratorRemove() { |
1472 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1473 |
|
for (int iters = 0; iters < 100; ++iters) { |
1474 |
|
q.add(new Integer(3)); |
1510 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1511 |
|
executor.execute(new CheckedRunnable() { |
1512 |
|
public void realRun() throws InterruptedException { |
1513 |
< |
threadAssertFalse(q.offer(three)); |
1514 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1515 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1513 |
> |
assertFalse(q.offer(three)); |
1514 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1515 |
> |
assertEquals(0, q.remainingCapacity()); |
1516 |
|
}}); |
1517 |
|
|
1518 |
|
executor.execute(new CheckedRunnable() { |
1519 |
|
public void realRun() throws InterruptedException { |
1520 |
|
Thread.sleep(SMALL_DELAY_MS); |
1521 |
< |
threadAssertEquals(one, q.take()); |
1521 |
> |
assertSame(one, q.take()); |
1522 |
|
}}); |
1523 |
|
|
1524 |
|
joinPool(executor); |
1532 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1533 |
|
executor.execute(new CheckedRunnable() { |
1534 |
|
public void realRun() throws InterruptedException { |
1535 |
< |
threadAssertNull(q.poll()); |
1536 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1537 |
< |
threadAssertTrue(q.isEmpty()); |
1535 |
> |
assertNull(q.poll()); |
1536 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1537 |
> |
assertTrue(q.isEmpty()); |
1538 |
|
}}); |
1539 |
|
|
1540 |
|
executor.execute(new CheckedRunnable() { |