11 |
|
import java.io.*; |
12 |
|
|
13 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
14 |
+ |
|
15 |
+ |
public static class Unbounded extends BlockingQueueTest { |
16 |
+ |
protected BlockingQueue emptyCollection() { |
17 |
+ |
return new LinkedBlockingDeque(); |
18 |
+ |
} |
19 |
+ |
} |
20 |
+ |
|
21 |
+ |
public static class Bounded extends BlockingQueueTest { |
22 |
+ |
protected BlockingQueue emptyCollection() { |
23 |
+ |
return new LinkedBlockingDeque(20); |
24 |
+ |
} |
25 |
+ |
} |
26 |
+ |
|
27 |
|
public static void main(String[] args) { |
28 |
< |
junit.textui.TestRunner.run (suite()); |
28 |
> |
junit.textui.TestRunner.run(suite()); |
29 |
|
} |
30 |
|
|
31 |
|
public static Test suite() { |
32 |
< |
return new TestSuite(LinkedBlockingDequeTest.class); |
32 |
> |
return newTestSuite(LinkedBlockingDequeTest.class, |
33 |
> |
new Unbounded().testSuite(), |
34 |
> |
new Bounded().testSuite()); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
108 |
|
} |
109 |
|
|
110 |
|
/** |
111 |
< |
* pollFirst succeeds unless empty |
111 |
> |
* pollFirst succeeds unless empty |
112 |
|
*/ |
113 |
|
public void testPollFirst() { |
114 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
115 |
|
for (int i = 0; i < SIZE; ++i) { |
116 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
116 |
> |
assertEquals(i, q.pollFirst()); |
117 |
|
} |
118 |
|
assertNull(q.pollFirst()); |
119 |
|
} |
120 |
|
|
121 |
|
/** |
122 |
< |
* pollLast succeeds unless empty |
122 |
> |
* pollLast succeeds unless empty |
123 |
|
*/ |
124 |
|
public void testPollLast() { |
125 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
126 |
|
for (int i = SIZE-1; i >= 0; --i) { |
127 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
127 |
> |
assertEquals(i, q.pollLast()); |
128 |
|
} |
129 |
|
assertNull(q.pollLast()); |
130 |
|
} |
131 |
|
|
132 |
|
/** |
133 |
< |
* peekFirst returns next element, or null if empty |
133 |
> |
* peekFirst returns next element, or null if empty |
134 |
|
*/ |
135 |
|
public void testPeekFirst() { |
136 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
137 |
|
for (int i = 0; i < SIZE; ++i) { |
138 |
< |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
139 |
< |
q.pollFirst(); |
138 |
> |
assertEquals(i, q.peekFirst()); |
139 |
> |
assertEquals(i, q.pollFirst()); |
140 |
|
assertTrue(q.peekFirst() == null || |
141 |
< |
i != ((Integer)q.peekFirst()).intValue()); |
141 |
> |
!q.peekFirst().equals(i)); |
142 |
|
} |
143 |
|
assertNull(q.peekFirst()); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
147 |
< |
* peek returns next element, or null if empty |
147 |
> |
* peek returns next element, or null if empty |
148 |
|
*/ |
149 |
|
public void testPeek() { |
150 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
151 |
|
for (int i = 0; i < SIZE; ++i) { |
152 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
153 |
< |
q.pollFirst(); |
152 |
> |
assertEquals(i, q.peek()); |
153 |
> |
assertEquals(i, q.pollFirst()); |
154 |
|
assertTrue(q.peek() == null || |
155 |
< |
i != ((Integer)q.peek()).intValue()); |
155 |
> |
!q.peek().equals(i)); |
156 |
|
} |
157 |
|
assertNull(q.peek()); |
158 |
|
} |
159 |
|
|
160 |
|
/** |
161 |
< |
* peekLast returns next element, or null if empty |
161 |
> |
* peekLast returns next element, or null if empty |
162 |
|
*/ |
163 |
|
public void testPeekLast() { |
164 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
165 |
|
for (int i = SIZE-1; i >= 0; --i) { |
166 |
< |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
167 |
< |
q.pollLast(); |
166 |
> |
assertEquals(i, q.peekLast()); |
167 |
> |
assertEquals(i, q.pollLast()); |
168 |
|
assertTrue(q.peekLast() == null || |
169 |
< |
i != ((Integer)q.peekLast()).intValue()); |
169 |
> |
!q.peekLast().equals(i)); |
170 |
|
} |
171 |
|
assertNull(q.peekLast()); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* getFirst returns next getFirst, or throws NSEE if empty |
175 |
> |
* getFirst() returns first element, or throws NSEE if empty |
176 |
|
*/ |
177 |
|
public void testFirstElement() { |
178 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
179 |
|
for (int i = 0; i < SIZE; ++i) { |
180 |
< |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
181 |
< |
q.pollFirst(); |
180 |
> |
assertEquals(i, q.getFirst()); |
181 |
> |
assertEquals(i, q.pollFirst()); |
182 |
|
} |
183 |
|
try { |
184 |
|
q.getFirst(); |
185 |
|
shouldThrow(); |
186 |
|
} catch (NoSuchElementException success) {} |
187 |
+ |
assertNull(q.peekFirst()); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* getLast returns next element, or throws NSEE if empty |
191 |
> |
* getLast() returns last element, or throws NSEE if empty |
192 |
|
*/ |
193 |
|
public void testLastElement() { |
194 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
195 |
|
for (int i = SIZE-1; i >= 0; --i) { |
196 |
< |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
197 |
< |
q.pollLast(); |
196 |
> |
assertEquals(i, q.getLast()); |
197 |
> |
assertEquals(i, q.pollLast()); |
198 |
|
} |
199 |
|
try { |
200 |
|
q.getLast(); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
< |
* removeFirst removes next element, or throws NSEE if empty |
207 |
> |
* removeFirst() removes first element, or throws NSEE if empty |
208 |
|
*/ |
209 |
|
public void testRemoveFirst() { |
210 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
211 |
|
for (int i = 0; i < SIZE; ++i) { |
212 |
< |
assertEquals(i, ((Integer)q.removeFirst()).intValue()); |
212 |
> |
assertEquals(i, q.removeFirst()); |
213 |
|
} |
214 |
|
try { |
215 |
|
q.removeFirst(); |
216 |
|
shouldThrow(); |
217 |
|
} catch (NoSuchElementException success) {} |
218 |
+ |
assertNull(q.peekFirst()); |
219 |
+ |
} |
220 |
+ |
|
221 |
+ |
/** |
222 |
+ |
* removeLast() removes last element, or throws NSEE if empty |
223 |
+ |
*/ |
224 |
+ |
public void testRemoveLast() { |
225 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
226 |
+ |
for (int i = SIZE - 1; i >= 0; --i) { |
227 |
+ |
assertEquals(i, q.removeLast()); |
228 |
+ |
} |
229 |
+ |
try { |
230 |
+ |
q.removeLast(); |
231 |
+ |
shouldThrow(); |
232 |
+ |
} catch (NoSuchElementException success) {} |
233 |
+ |
assertNull(q.peekLast()); |
234 |
|
} |
235 |
|
|
236 |
|
/** |
237 |
< |
* remove removes next element, or throws NSEE if empty |
237 |
> |
* remove removes next element, or throws NSEE if empty |
238 |
|
*/ |
239 |
|
public void testRemove() { |
240 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
241 |
|
for (int i = 0; i < SIZE; ++i) { |
242 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
242 |
> |
assertEquals(i, q.remove()); |
243 |
|
} |
244 |
|
try { |
245 |
|
q.remove(); |
284 |
|
LinkedBlockingDeque q = populatedDeque(3); |
285 |
|
q.pollLast(); |
286 |
|
q.addFirst(four); |
287 |
< |
assertEquals(four,q.peekFirst()); |
287 |
> |
assertSame(four, q.peekFirst()); |
288 |
|
} |
289 |
|
|
290 |
|
/** |
294 |
|
LinkedBlockingDeque q = populatedDeque(3); |
295 |
|
q.pollLast(); |
296 |
|
q.addLast(four); |
297 |
< |
assertEquals(four,q.peekLast()); |
297 |
> |
assertSame(four, q.peekLast()); |
298 |
|
} |
299 |
|
|
300 |
|
|
355 |
|
* Deque contains all elements of collection used to initialize |
356 |
|
*/ |
357 |
|
public void testConstructor6() { |
358 |
< |
try { |
359 |
< |
Integer[] ints = new Integer[SIZE]; |
360 |
< |
for (int i = 0; i < SIZE; ++i) |
361 |
< |
ints[i] = new Integer(i); |
362 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
363 |
< |
for (int i = 0; i < SIZE; ++i) |
332 |
< |
assertEquals(ints[i], q.poll()); |
333 |
< |
} |
334 |
< |
finally {} |
358 |
> |
Integer[] ints = new Integer[SIZE]; |
359 |
> |
for (int i = 0; i < SIZE; ++i) |
360 |
> |
ints[i] = new Integer(i); |
361 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
362 |
> |
for (int i = 0; i < SIZE; ++i) |
363 |
> |
assertEquals(ints[i], q.poll()); |
364 |
|
} |
365 |
|
|
366 |
|
/** |
452 |
|
LinkedBlockingDeque q = populatedDeque(3); |
453 |
|
q.pollLast(); |
454 |
|
q.push(four); |
455 |
< |
assertEquals(four,q.peekFirst()); |
455 |
> |
assertSame(four, q.peekFirst()); |
456 |
|
} |
457 |
|
|
458 |
|
|
459 |
|
/** |
460 |
< |
* pop removes next element, or throws NSEE if empty |
460 |
> |
* pop removes next element, or throws NSEE if empty |
461 |
|
*/ |
462 |
|
public void testPop() { |
463 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
464 |
|
for (int i = 0; i < SIZE; ++i) { |
465 |
< |
assertEquals(i, ((Integer)q.pop()).intValue()); |
465 |
> |
assertEquals(i, q.pop()); |
466 |
|
} |
467 |
|
try { |
468 |
|
q.pop(); |
528 |
|
shouldThrow(); |
529 |
|
} catch (NullPointerException success) {} |
530 |
|
} |
531 |
+ |
|
532 |
|
/** |
533 |
|
* addAll of a collection with any null elements throws NPE after |
534 |
|
* possibly adding some elements |
543 |
|
shouldThrow(); |
544 |
|
} catch (NullPointerException success) {} |
545 |
|
} |
546 |
+ |
|
547 |
|
/** |
548 |
|
* addAll throws ISE if not enough room |
549 |
|
*/ |
557 |
|
shouldThrow(); |
558 |
|
} catch (IllegalStateException success) {} |
559 |
|
} |
560 |
+ |
|
561 |
|
/** |
562 |
|
* Deque contains all elements, in traversal order, of successful addAll |
563 |
|
*/ |
564 |
|
public void testAddAll5() { |
565 |
< |
try { |
566 |
< |
Integer[] empty = new Integer[0]; |
567 |
< |
Integer[] ints = new Integer[SIZE]; |
568 |
< |
for (int i = 0; i < SIZE; ++i) |
569 |
< |
ints[i] = new Integer(i); |
570 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
571 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
572 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
573 |
< |
for (int i = 0; i < SIZE; ++i) |
542 |
< |
assertEquals(ints[i], q.poll()); |
543 |
< |
} |
544 |
< |
finally {} |
565 |
> |
Integer[] empty = new Integer[0]; |
566 |
> |
Integer[] ints = new Integer[SIZE]; |
567 |
> |
for (int i = 0; i < SIZE; ++i) |
568 |
> |
ints[i] = new Integer(i); |
569 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
570 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
571 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
572 |
> |
for (int i = 0; i < SIZE; ++i) |
573 |
> |
assertEquals(ints[i], q.poll()); |
574 |
|
} |
575 |
|
|
576 |
|
|
602 |
|
* put blocks interruptibly if full |
603 |
|
*/ |
604 |
|
public void testBlockingPut() throws InterruptedException { |
605 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
606 |
|
Thread t = new Thread(new CheckedRunnable() { |
607 |
< |
public void realRun() { |
608 |
< |
int added = 0; |
607 |
> |
public void realRun() throws InterruptedException { |
608 |
> |
for (int i = 0; i < SIZE; ++i) |
609 |
> |
q.put(i); |
610 |
> |
assertEquals(SIZE, q.size()); |
611 |
> |
assertEquals(0, q.remainingCapacity()); |
612 |
|
try { |
613 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
614 |
< |
for (int i = 0; i < SIZE; ++i) { |
615 |
< |
q.put(new Integer(i)); |
583 |
< |
++added; |
584 |
< |
} |
585 |
< |
q.put(new Integer(SIZE)); |
586 |
< |
threadShouldThrow(); |
587 |
< |
} catch (InterruptedException success) { |
588 |
< |
threadAssertEquals(added, SIZE); |
589 |
< |
} |
613 |
> |
q.put(99); |
614 |
> |
shouldThrow(); |
615 |
> |
} catch (InterruptedException success) {} |
616 |
|
}}); |
617 |
|
|
618 |
|
t.start(); |
619 |
|
Thread.sleep(SHORT_DELAY_MS); |
620 |
|
t.interrupt(); |
621 |
|
t.join(); |
622 |
+ |
assertEquals(SIZE, q.size()); |
623 |
+ |
assertEquals(0, q.remainingCapacity()); |
624 |
|
} |
625 |
|
|
626 |
|
/** |
627 |
|
* put blocks waiting for take when full |
628 |
|
*/ |
629 |
|
public void testPutWithTake() throws InterruptedException { |
630 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
630 |
> |
final int capacity = 2; |
631 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
632 |
|
Thread t = new Thread(new CheckedRunnable() { |
633 |
< |
public void realRun() { |
634 |
< |
int added = 0; |
633 |
> |
public void realRun() throws InterruptedException { |
634 |
> |
for (int i = 0; i < capacity + 1; i++) |
635 |
> |
q.put(i); |
636 |
|
try { |
637 |
< |
q.put(new Object()); |
638 |
< |
++added; |
639 |
< |
q.put(new Object()); |
610 |
< |
++added; |
611 |
< |
q.put(new Object()); |
612 |
< |
++added; |
613 |
< |
q.put(new Object()); |
614 |
< |
++added; |
615 |
< |
threadShouldThrow(); |
616 |
< |
} catch (InterruptedException success) { |
617 |
< |
threadAssertTrue(added >= 2); |
618 |
< |
} |
637 |
> |
q.put(99); |
638 |
> |
shouldThrow(); |
639 |
> |
} catch (InterruptedException success) {} |
640 |
|
}}); |
641 |
|
|
642 |
|
t.start(); |
643 |
|
Thread.sleep(SHORT_DELAY_MS); |
644 |
< |
q.take(); |
644 |
> |
assertEquals(q.remainingCapacity(), 0); |
645 |
> |
assertEquals(0, q.take()); |
646 |
> |
Thread.sleep(SHORT_DELAY_MS); |
647 |
|
t.interrupt(); |
648 |
|
t.join(); |
649 |
+ |
assertEquals(q.remainingCapacity(), 0); |
650 |
|
} |
651 |
|
|
652 |
|
/** |
654 |
|
*/ |
655 |
|
public void testTimedOffer() throws InterruptedException { |
656 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
657 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
657 |
> |
Thread t = new Thread(new CheckedRunnable() { |
658 |
|
public void realRun() throws InterruptedException { |
659 |
|
q.put(new Object()); |
660 |
|
q.put(new Object()); |
661 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
662 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
663 |
< |
}}; |
661 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
662 |
> |
try { |
663 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
664 |
> |
shouldThrow(); |
665 |
> |
} catch (InterruptedException success) {} |
666 |
> |
}}); |
667 |
|
|
668 |
|
t.start(); |
669 |
|
Thread.sleep(SMALL_DELAY_MS); |
677 |
|
public void testTake() throws InterruptedException { |
678 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
679 |
|
for (int i = 0; i < SIZE; ++i) { |
680 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
680 |
> |
assertEquals(i, q.take()); |
681 |
|
} |
682 |
|
} |
683 |
|
|
684 |
|
/** |
658 |
– |
* take blocks interruptibly when empty |
659 |
– |
*/ |
660 |
– |
public void testTakeFromEmpty() throws InterruptedException { |
661 |
– |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
662 |
– |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
663 |
– |
public void realRun() throws InterruptedException { |
664 |
– |
q.take(); |
665 |
– |
}}; |
666 |
– |
|
667 |
– |
t.start(); |
668 |
– |
Thread.sleep(SHORT_DELAY_MS); |
669 |
– |
t.interrupt(); |
670 |
– |
t.join(); |
671 |
– |
} |
672 |
– |
|
673 |
– |
/** |
685 |
|
* Take removes existing elements until empty, then blocks interruptibly |
686 |
|
*/ |
687 |
|
public void testBlockingTake() throws InterruptedException { |
688 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
688 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
689 |
> |
Thread t = new Thread(new CheckedRunnable() { |
690 |
|
public void realRun() throws InterruptedException { |
679 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
691 |
|
for (int i = 0; i < SIZE; ++i) { |
692 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
692 |
> |
assertEquals(i, q.take()); |
693 |
|
} |
694 |
< |
q.take(); |
695 |
< |
}}; |
694 |
> |
try { |
695 |
> |
q.take(); |
696 |
> |
shouldThrow(); |
697 |
> |
} catch (InterruptedException success) {} |
698 |
> |
}}); |
699 |
|
|
700 |
|
t.start(); |
701 |
|
Thread.sleep(SHORT_DELAY_MS); |
710 |
|
public void testPoll() { |
711 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
712 |
|
for (int i = 0; i < SIZE; ++i) { |
713 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
713 |
> |
assertEquals(i, q.poll()); |
714 |
|
} |
715 |
|
assertNull(q.poll()); |
716 |
|
} |
721 |
|
public void testTimedPoll0() throws InterruptedException { |
722 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
723 |
|
for (int i = 0; i < SIZE; ++i) { |
724 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
724 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
725 |
|
} |
726 |
|
assertNull(q.poll(0, MILLISECONDS)); |
727 |
|
} |
732 |
|
public void testTimedPoll() throws InterruptedException { |
733 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
734 |
|
for (int i = 0; i < SIZE; ++i) { |
735 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
735 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
736 |
|
} |
737 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
738 |
|
} |
742 |
|
* returning timeout status |
743 |
|
*/ |
744 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
745 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
745 |
> |
Thread t = new Thread(new CheckedRunnable() { |
746 |
|
public void realRun() throws InterruptedException { |
747 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
748 |
|
for (int i = 0; i < SIZE; ++i) { |
749 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
749 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
750 |
|
} |
751 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
752 |
< |
}}; |
751 |
> |
try { |
752 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
753 |
> |
shouldThrow(); |
754 |
> |
} catch (InterruptedException success) {} |
755 |
> |
}}); |
756 |
|
|
757 |
|
t.start(); |
758 |
|
Thread.sleep(SHORT_DELAY_MS); |
761 |
|
} |
762 |
|
|
763 |
|
/** |
747 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
748 |
– |
* on interruption throws |
749 |
– |
*/ |
750 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
751 |
– |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
752 |
– |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
753 |
– |
public void realRun() throws InterruptedException { |
754 |
– |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
755 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
756 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
757 |
– |
}}; |
758 |
– |
|
759 |
– |
t.start(); |
760 |
– |
Thread.sleep(SMALL_DELAY_MS); |
761 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
762 |
– |
t.interrupt(); |
763 |
– |
t.join(); |
764 |
– |
} |
765 |
– |
|
766 |
– |
|
767 |
– |
/** |
764 |
|
* putFirst(null) throws NPE |
765 |
|
*/ |
766 |
|
public void testPutFirstNull() throws InterruptedException { |
788 |
|
* putFirst blocks interruptibly if full |
789 |
|
*/ |
790 |
|
public void testBlockingPutFirst() throws InterruptedException { |
791 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
792 |
|
Thread t = new Thread(new CheckedRunnable() { |
793 |
< |
public void realRun() { |
794 |
< |
int added = 0; |
793 |
> |
public void realRun() throws InterruptedException { |
794 |
> |
for (int i = 0; i < SIZE; ++i) |
795 |
> |
q.putFirst(i); |
796 |
> |
assertEquals(SIZE, q.size()); |
797 |
> |
assertEquals(0, q.remainingCapacity()); |
798 |
|
try { |
799 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
800 |
< |
for (int i = 0; i < SIZE; ++i) { |
801 |
< |
q.putFirst(new Integer(i)); |
802 |
< |
++added; |
803 |
< |
} |
804 |
< |
q.putFirst(new Integer(SIZE)); |
805 |
< |
threadShouldThrow(); |
806 |
< |
} catch (InterruptedException success) { |
807 |
< |
threadAssertEquals(added, SIZE); |
808 |
< |
} |
799 |
> |
q.putFirst(99); |
800 |
> |
shouldThrow(); |
801 |
> |
} catch (InterruptedException success) {} |
802 |
|
}}); |
803 |
|
|
804 |
|
t.start(); |
805 |
|
Thread.sleep(SHORT_DELAY_MS); |
806 |
|
t.interrupt(); |
807 |
|
t.join(); |
808 |
+ |
assertEquals(SIZE, q.size()); |
809 |
+ |
assertEquals(0, q.remainingCapacity()); |
810 |
|
} |
811 |
|
|
812 |
|
/** |
813 |
|
* putFirst blocks waiting for take when full |
814 |
|
*/ |
815 |
|
public void testPutFirstWithTake() throws InterruptedException { |
816 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
816 |
> |
final int capacity = 2; |
817 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
818 |
|
Thread t = new Thread(new CheckedRunnable() { |
819 |
< |
public void realRun() { |
820 |
< |
int added = 0; |
819 |
> |
public void realRun() throws InterruptedException { |
820 |
> |
for (int i = 0; i < capacity + 1; i++) |
821 |
> |
q.putFirst(i); |
822 |
|
try { |
823 |
< |
q.putFirst(new Object()); |
824 |
< |
++added; |
825 |
< |
q.putFirst(new Object()); |
829 |
< |
++added; |
830 |
< |
q.putFirst(new Object()); |
831 |
< |
++added; |
832 |
< |
q.putFirst(new Object()); |
833 |
< |
++added; |
834 |
< |
threadShouldThrow(); |
835 |
< |
} catch (InterruptedException success) { |
836 |
< |
threadAssertTrue(added >= 2); |
837 |
< |
} |
823 |
> |
q.putFirst(99); |
824 |
> |
shouldThrow(); |
825 |
> |
} catch (InterruptedException success) {} |
826 |
|
}}); |
827 |
|
|
828 |
|
t.start(); |
829 |
|
Thread.sleep(SHORT_DELAY_MS); |
830 |
< |
q.take(); |
830 |
> |
assertEquals(q.remainingCapacity(), 0); |
831 |
> |
assertEquals(capacity - 1, q.take()); |
832 |
> |
Thread.sleep(SHORT_DELAY_MS); |
833 |
|
t.interrupt(); |
834 |
|
t.join(); |
835 |
+ |
assertEquals(q.remainingCapacity(), 0); |
836 |
|
} |
837 |
|
|
838 |
|
/** |
840 |
|
*/ |
841 |
|
public void testTimedOfferFirst() throws InterruptedException { |
842 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
843 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
843 |
> |
Thread t = new Thread(new CheckedRunnable() { |
844 |
|
public void realRun() throws InterruptedException { |
845 |
|
q.putFirst(new Object()); |
846 |
|
q.putFirst(new Object()); |
847 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
848 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
849 |
< |
}}; |
847 |
> |
assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
848 |
> |
try { |
849 |
> |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
850 |
> |
shouldThrow(); |
851 |
> |
} catch (InterruptedException success) {} |
852 |
> |
}}); |
853 |
|
|
854 |
|
t.start(); |
855 |
|
Thread.sleep(SMALL_DELAY_MS); |
863 |
|
public void testTakeFirst() throws InterruptedException { |
864 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
865 |
|
for (int i = 0; i < SIZE; ++i) { |
866 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
866 |
> |
assertEquals(i, q.takeFirst()); |
867 |
|
} |
868 |
|
} |
869 |
|
|
887 |
|
* TakeFirst removes existing elements until empty, then blocks interruptibly |
888 |
|
*/ |
889 |
|
public void testBlockingTakeFirst() throws InterruptedException { |
890 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
890 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
891 |
> |
Thread t = new Thread(new CheckedRunnable() { |
892 |
|
public void realRun() throws InterruptedException { |
893 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
894 |
< |
for (int i = 0; i < SIZE; ++i) { |
895 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
896 |
< |
} |
897 |
< |
q.takeFirst(); |
898 |
< |
}}; |
893 |
> |
for (int i = 0; i < SIZE; ++i) |
894 |
> |
assertEquals(i, q.takeFirst()); |
895 |
> |
try { |
896 |
> |
q.takeFirst(); |
897 |
> |
shouldThrow(); |
898 |
> |
} catch (InterruptedException success) {} |
899 |
> |
}}); |
900 |
|
|
901 |
|
t.start(); |
902 |
|
Thread.sleep(SHORT_DELAY_MS); |
911 |
|
public void testTimedPollFirst0() throws InterruptedException { |
912 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
913 |
|
for (int i = 0; i < SIZE; ++i) { |
914 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue()); |
914 |
> |
assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
915 |
|
} |
916 |
|
assertNull(q.pollFirst(0, MILLISECONDS)); |
917 |
|
} |
922 |
|
public void testTimedPollFirst() throws InterruptedException { |
923 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
924 |
|
for (int i = 0; i < SIZE; ++i) { |
925 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
925 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
926 |
|
} |
927 |
|
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
928 |
|
} |
932 |
|
* returning timeout status |
933 |
|
*/ |
934 |
|
public void testInterruptedTimedPollFirst() throws InterruptedException { |
935 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
935 |
> |
Thread t = new Thread(new CheckedRunnable() { |
936 |
|
public void realRun() throws InterruptedException { |
937 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
938 |
|
for (int i = 0; i < SIZE; ++i) { |
939 |
< |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
939 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
940 |
|
} |
941 |
< |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
942 |
< |
}}; |
941 |
> |
try { |
942 |
> |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
943 |
> |
shouldThrow(); |
944 |
> |
} catch (InterruptedException success) {} |
945 |
> |
}}); |
946 |
|
|
947 |
|
t.start(); |
948 |
|
Thread.sleep(SHORT_DELAY_MS); |
951 |
|
} |
952 |
|
|
953 |
|
/** |
954 |
< |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
955 |
< |
* on interruption throws |
954 |
> |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
955 |
> |
* on interruption throws |
956 |
|
*/ |
957 |
|
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
958 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
959 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
959 |
> |
Thread t = new Thread(new CheckedRunnable() { |
960 |
|
public void realRun() throws InterruptedException { |
961 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
962 |
< |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
963 |
< |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
964 |
< |
}}; |
961 |
> |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
962 |
> |
assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
963 |
> |
try { |
964 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
965 |
> |
shouldThrow(); |
966 |
> |
} catch (InterruptedException success) {} |
967 |
> |
}}); |
968 |
|
|
969 |
|
t.start(); |
970 |
|
Thread.sleep(SMALL_DELAY_MS); |
1001 |
|
* putLast blocks interruptibly if full |
1002 |
|
*/ |
1003 |
|
public void testBlockingPutLast() throws InterruptedException { |
1004 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1005 |
|
Thread t = new Thread(new CheckedRunnable() { |
1006 |
< |
public void realRun() { |
1007 |
< |
int added = 0; |
1006 |
> |
public void realRun() throws InterruptedException { |
1007 |
> |
for (int i = 0; i < SIZE; ++i) |
1008 |
> |
q.putLast(i); |
1009 |
> |
assertEquals(SIZE, q.size()); |
1010 |
> |
assertEquals(0, q.remainingCapacity()); |
1011 |
|
try { |
1012 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1013 |
< |
for (int i = 0; i < SIZE; ++i) { |
1014 |
< |
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 |
< |
} |
1012 |
> |
q.putLast(99); |
1013 |
> |
shouldThrow(); |
1014 |
> |
} catch (InterruptedException success) {} |
1015 |
|
}}); |
1016 |
|
|
1017 |
|
t.start(); |
1018 |
|
Thread.sleep(SHORT_DELAY_MS); |
1019 |
|
t.interrupt(); |
1020 |
|
t.join(); |
1021 |
+ |
assertEquals(SIZE, q.size()); |
1022 |
+ |
assertEquals(0, q.remainingCapacity()); |
1023 |
|
} |
1024 |
|
|
1025 |
|
/** |
1026 |
|
* putLast blocks waiting for take when full |
1027 |
|
*/ |
1028 |
|
public void testPutLastWithTake() throws InterruptedException { |
1029 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1029 |
> |
final int capacity = 2; |
1030 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1031 |
|
Thread t = new Thread(new CheckedRunnable() { |
1032 |
< |
public void realRun() { |
1033 |
< |
int added = 0; |
1032 |
> |
public void realRun() throws InterruptedException { |
1033 |
> |
for (int i = 0; i < capacity + 1; i++) |
1034 |
> |
q.putLast(i); |
1035 |
|
try { |
1036 |
< |
q.putLast(new Object()); |
1037 |
< |
++added; |
1038 |
< |
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 |
< |
} |
1036 |
> |
q.putLast(99); |
1037 |
> |
shouldThrow(); |
1038 |
> |
} catch (InterruptedException success) {} |
1039 |
|
}}); |
1040 |
|
|
1041 |
|
t.start(); |
1042 |
|
Thread.sleep(SHORT_DELAY_MS); |
1043 |
< |
q.take(); |
1043 |
> |
assertEquals(q.remainingCapacity(), 0); |
1044 |
> |
assertEquals(0, q.take()); |
1045 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1046 |
|
t.interrupt(); |
1047 |
|
t.join(); |
1048 |
+ |
assertEquals(q.remainingCapacity(), 0); |
1049 |
|
} |
1050 |
|
|
1051 |
|
/** |
1053 |
|
*/ |
1054 |
|
public void testTimedOfferLast() throws InterruptedException { |
1055 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1056 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1056 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1057 |
|
public void realRun() throws InterruptedException { |
1058 |
|
q.putLast(new Object()); |
1059 |
|
q.putLast(new Object()); |
1060 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1061 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1062 |
< |
}}; |
1060 |
> |
assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1061 |
> |
try { |
1062 |
> |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1063 |
> |
shouldThrow(); |
1064 |
> |
} catch (InterruptedException success) {} |
1065 |
> |
}}); |
1066 |
|
|
1067 |
|
t.start(); |
1068 |
|
Thread.sleep(SMALL_DELAY_MS); |
1076 |
|
public void testTakeLast() throws InterruptedException { |
1077 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1078 |
|
for (int i = 0; i < SIZE; ++i) { |
1079 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1079 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1080 |
|
} |
1081 |
|
} |
1082 |
|
|
1100 |
|
* TakeLast removes existing elements until empty, then blocks interruptibly |
1101 |
|
*/ |
1102 |
|
public void testBlockingTakeLast() throws InterruptedException { |
1103 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1103 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1104 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1105 |
|
public void realRun() throws InterruptedException { |
1106 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1107 |
< |
for (int i = 0; i < SIZE; ++i) { |
1108 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1109 |
< |
} |
1110 |
< |
q.takeLast(); |
1111 |
< |
}}; |
1106 |
> |
for (int i = 0; i < SIZE; ++i) |
1107 |
> |
assertEquals(SIZE - 1 - i, q.takeLast()); |
1108 |
> |
try { |
1109 |
> |
q.takeLast(); |
1110 |
> |
shouldThrow(); |
1111 |
> |
} catch (InterruptedException success) {} |
1112 |
> |
}}); |
1113 |
|
|
1114 |
|
t.start(); |
1115 |
|
Thread.sleep(SHORT_DELAY_MS); |
1117 |
|
t.join(); |
1118 |
|
} |
1119 |
|
|
1118 |
– |
|
1120 |
|
/** |
1121 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1122 |
|
*/ |
1123 |
|
public void testTimedPollLast0() throws InterruptedException { |
1124 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1125 |
|
for (int i = 0; i < SIZE; ++i) { |
1126 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue()); |
1126 |
> |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1127 |
|
} |
1128 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1129 |
|
} |
1134 |
|
public void testTimedPollLast() throws InterruptedException { |
1135 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1136 |
|
for (int i = 0; i < SIZE; ++i) { |
1137 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1137 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1138 |
|
} |
1139 |
|
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1140 |
|
} |
1144 |
|
* returning timeout status |
1145 |
|
*/ |
1146 |
|
public void testInterruptedTimedPollLast() throws InterruptedException { |
1147 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1147 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1148 |
|
public void realRun() throws InterruptedException { |
1149 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1150 |
|
for (int i = 0; i < SIZE; ++i) { |
1151 |
< |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1151 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1152 |
|
} |
1153 |
< |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1154 |
< |
}}; |
1153 |
> |
try { |
1154 |
> |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1155 |
> |
shouldThrow(); |
1156 |
> |
} catch (InterruptedException success) {} |
1157 |
> |
}}); |
1158 |
|
|
1159 |
|
t.start(); |
1160 |
|
Thread.sleep(SHORT_DELAY_MS); |
1163 |
|
} |
1164 |
|
|
1165 |
|
/** |
1166 |
< |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1167 |
< |
* on interruption throws |
1166 |
> |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1167 |
> |
* on interruption throws |
1168 |
|
*/ |
1169 |
|
public void testTimedPollWithOfferLast() throws InterruptedException { |
1170 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1192 |
|
public void testElement() { |
1193 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1194 |
|
for (int i = 0; i < SIZE; ++i) { |
1195 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
1195 |
> |
assertEquals(i, q.element()); |
1196 |
|
q.poll(); |
1197 |
|
} |
1198 |
|
try { |
1315 |
|
} |
1316 |
|
|
1317 |
|
/** |
1318 |
< |
* toArray(null) throws NPE |
1318 |
> |
* toArray(null) throws NullPointerException |
1319 |
|
*/ |
1320 |
< |
public void testToArray_BadArg() { |
1320 |
> |
public void testToArray_NullArg() { |
1321 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1322 |
|
try { |
1323 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1319 |
< |
Object o[] = q.toArray(null); |
1323 |
> |
q.toArray(null); |
1324 |
|
shouldThrow(); |
1325 |
|
} catch (NullPointerException success) {} |
1326 |
|
} |
1327 |
|
|
1328 |
|
/** |
1329 |
< |
* toArray with incompatible array type throws CCE |
1329 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
1330 |
|
*/ |
1331 |
|
public void testToArray1_BadArg() { |
1332 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1333 |
|
try { |
1334 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1330 |
< |
Object o[] = q.toArray(new String[10] ); |
1334 |
> |
q.toArray(new String[10]); |
1335 |
|
shouldThrow(); |
1336 |
|
} catch (ArrayStoreException success) {} |
1337 |
|
} |
1351 |
|
/** |
1352 |
|
* iterator.remove removes current element |
1353 |
|
*/ |
1354 |
< |
public void testIteratorRemove () { |
1354 |
> |
public void testIteratorRemove() { |
1355 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1356 |
|
q.add(two); |
1357 |
|
q.add(one); |
1362 |
|
it.remove(); |
1363 |
|
|
1364 |
|
it = q.iterator(); |
1365 |
< |
assertEquals(it.next(), one); |
1366 |
< |
assertEquals(it.next(), three); |
1365 |
> |
assertSame(it.next(), one); |
1366 |
> |
assertSame(it.next(), three); |
1367 |
|
assertFalse(it.hasNext()); |
1368 |
|
} |
1369 |
|
|
1379 |
|
assertEquals(0, q.remainingCapacity()); |
1380 |
|
int k = 0; |
1381 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
1382 |
< |
int i = ((Integer)(it.next())).intValue(); |
1379 |
< |
assertEquals(++k, i); |
1382 |
> |
assertEquals(++k, it.next()); |
1383 |
|
} |
1384 |
|
assertEquals(3, k); |
1385 |
|
} |
1387 |
|
/** |
1388 |
|
* Modifications do not cause iterators to fail |
1389 |
|
*/ |
1390 |
< |
public void testWeaklyConsistentIteration () { |
1390 |
> |
public void testWeaklyConsistentIteration() { |
1391 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1392 |
|
q.add(one); |
1393 |
|
q.add(two); |
1401 |
|
|
1402 |
|
|
1403 |
|
/** |
1404 |
< |
* Descending iterator iterates through all elements |
1404 |
> |
* Descending iterator iterates through all elements |
1405 |
|
*/ |
1406 |
|
public void testDescendingIterator() { |
1407 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1420 |
|
} |
1421 |
|
|
1422 |
|
/** |
1423 |
< |
* Descending iterator ordering is reverse FIFO |
1423 |
> |
* Descending iterator ordering is reverse FIFO |
1424 |
|
*/ |
1425 |
|
public void testDescendingIteratorOrdering() { |
1426 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1430 |
|
q.add(new Integer(1)); |
1431 |
|
int k = 0; |
1432 |
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1433 |
< |
int i = ((Integer)(it.next())).intValue(); |
1431 |
< |
assertEquals(++k, i); |
1433 |
> |
assertEquals(++k, it.next()); |
1434 |
|
} |
1435 |
|
|
1436 |
|
assertEquals(3, k); |
1443 |
|
/** |
1444 |
|
* descendingIterator.remove removes current element |
1445 |
|
*/ |
1446 |
< |
public void testDescendingIteratorRemove () { |
1446 |
> |
public void testDescendingIteratorRemove() { |
1447 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1448 |
|
for (int iters = 0; iters < 100; ++iters) { |
1449 |
|
q.add(new Integer(3)); |
1485 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1486 |
|
executor.execute(new CheckedRunnable() { |
1487 |
|
public void realRun() throws InterruptedException { |
1488 |
< |
threadAssertFalse(q.offer(three)); |
1489 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1490 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1488 |
> |
assertFalse(q.offer(three)); |
1489 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1490 |
> |
assertEquals(0, q.remainingCapacity()); |
1491 |
|
}}); |
1492 |
|
|
1493 |
|
executor.execute(new CheckedRunnable() { |
1494 |
|
public void realRun() throws InterruptedException { |
1495 |
|
Thread.sleep(SMALL_DELAY_MS); |
1496 |
< |
threadAssertEquals(one, q.take()); |
1496 |
> |
assertSame(one, q.take()); |
1497 |
|
}}); |
1498 |
|
|
1499 |
|
joinPool(executor); |
1507 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1508 |
|
executor.execute(new CheckedRunnable() { |
1509 |
|
public void realRun() throws InterruptedException { |
1510 |
< |
threadAssertNull(q.poll()); |
1511 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1512 |
< |
threadAssertTrue(q.isEmpty()); |
1510 |
> |
assertNull(q.poll()); |
1511 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1512 |
> |
assertTrue(q.isEmpty()); |
1513 |
|
}}); |
1514 |
|
|
1515 |
|
executor.execute(new CheckedRunnable() { |
1629 |
|
} |
1630 |
|
|
1631 |
|
/** |
1632 |
< |
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1632 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
1633 |
|
*/ |
1634 |
|
public void testDrainToN() { |
1635 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1638 |
|
assertTrue(q.offer(new Integer(j))); |
1639 |
|
ArrayList l = new ArrayList(); |
1640 |
|
q.drainTo(l, i); |
1641 |
< |
int k = (i < SIZE)? i : SIZE; |
1641 |
> |
int k = (i < SIZE) ? i : SIZE; |
1642 |
|
assertEquals(l.size(), k); |
1643 |
|
assertEquals(q.size(), SIZE-k); |
1644 |
|
for (int j = 0; j < k; ++j) |