1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
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 |
|
/** |
38 |
|
* Create a deque of given size containing consecutive |
39 |
|
* Integers 0 ... n. |
40 |
|
*/ |
41 |
< |
private LinkedBlockingDeque populatedDeque(int n) { |
42 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(n); |
41 |
> |
private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
42 |
> |
LinkedBlockingDeque<Integer> q = |
43 |
> |
new LinkedBlockingDeque<Integer>(n); |
44 |
|
assertTrue(q.isEmpty()); |
45 |
|
for (int i = 0; i < n; i++) |
46 |
|
assertTrue(q.offer(new Integer(i))); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* pollFirst succeeds unless empty |
112 |
> |
* pollFirst succeeds unless empty |
113 |
|
*/ |
114 |
|
public void testPollFirst() { |
115 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
116 |
|
for (int i = 0; i < SIZE; ++i) { |
117 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
117 |
> |
assertEquals(i, q.pollFirst()); |
118 |
|
} |
119 |
|
assertNull(q.pollFirst()); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* pollLast succeeds unless empty |
123 |
> |
* pollLast succeeds unless empty |
124 |
|
*/ |
125 |
|
public void testPollLast() { |
126 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
127 |
|
for (int i = SIZE-1; i >= 0; --i) { |
128 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
128 |
> |
assertEquals(i, q.pollLast()); |
129 |
|
} |
130 |
|
assertNull(q.pollLast()); |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
< |
* peekFirst returns next element, or null if empty |
134 |
> |
* peekFirst returns next element, or null if empty |
135 |
|
*/ |
136 |
|
public void testPeekFirst() { |
137 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
138 |
|
for (int i = 0; i < SIZE; ++i) { |
139 |
< |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
140 |
< |
q.pollFirst(); |
139 |
> |
assertEquals(i, q.peekFirst()); |
140 |
> |
assertEquals(i, q.pollFirst()); |
141 |
|
assertTrue(q.peekFirst() == null || |
142 |
< |
i != ((Integer)q.peekFirst()).intValue()); |
142 |
> |
!q.peekFirst().equals(i)); |
143 |
|
} |
144 |
|
assertNull(q.peekFirst()); |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
< |
* peek returns next element, or null if empty |
148 |
> |
* peek returns next element, or null if empty |
149 |
|
*/ |
150 |
|
public void testPeek() { |
151 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
152 |
|
for (int i = 0; i < SIZE; ++i) { |
153 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
154 |
< |
q.pollFirst(); |
153 |
> |
assertEquals(i, q.peek()); |
154 |
> |
assertEquals(i, q.pollFirst()); |
155 |
|
assertTrue(q.peek() == null || |
156 |
< |
i != ((Integer)q.peek()).intValue()); |
156 |
> |
!q.peek().equals(i)); |
157 |
|
} |
158 |
|
assertNull(q.peek()); |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
< |
* peekLast returns next element, or null if empty |
162 |
> |
* peekLast returns next element, or null if empty |
163 |
|
*/ |
164 |
|
public void testPeekLast() { |
165 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
166 |
|
for (int i = SIZE-1; i >= 0; --i) { |
167 |
< |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
168 |
< |
q.pollLast(); |
167 |
> |
assertEquals(i, q.peekLast()); |
168 |
> |
assertEquals(i, q.pollLast()); |
169 |
|
assertTrue(q.peekLast() == null || |
170 |
< |
i != ((Integer)q.peekLast()).intValue()); |
170 |
> |
!q.peekLast().equals(i)); |
171 |
|
} |
172 |
|
assertNull(q.peekLast()); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
< |
* getFirst returns next getFirst, or throws NSEE if empty |
176 |
> |
* getFirst() returns first element, or throws NSEE if empty |
177 |
|
*/ |
178 |
|
public void testFirstElement() { |
179 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
180 |
|
for (int i = 0; i < SIZE; ++i) { |
181 |
< |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
182 |
< |
q.pollFirst(); |
181 |
> |
assertEquals(i, q.getFirst()); |
182 |
> |
assertEquals(i, q.pollFirst()); |
183 |
|
} |
184 |
|
try { |
185 |
|
q.getFirst(); |
186 |
|
shouldThrow(); |
187 |
|
} catch (NoSuchElementException success) {} |
188 |
+ |
assertNull(q.peekFirst()); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
< |
* getLast returns next element, or throws NSEE if empty |
192 |
> |
* getLast() returns last element, or throws NSEE if empty |
193 |
|
*/ |
194 |
|
public void testLastElement() { |
195 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
196 |
|
for (int i = SIZE-1; i >= 0; --i) { |
197 |
< |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
198 |
< |
q.pollLast(); |
197 |
> |
assertEquals(i, q.getLast()); |
198 |
> |
assertEquals(i, q.pollLast()); |
199 |
|
} |
200 |
|
try { |
201 |
|
q.getLast(); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
< |
* removeFirst removes next element, or throws NSEE if empty |
208 |
> |
* removeFirst() removes first element, or throws NSEE if empty |
209 |
|
*/ |
210 |
|
public void testRemoveFirst() { |
211 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
212 |
|
for (int i = 0; i < SIZE; ++i) { |
213 |
< |
assertEquals(i, ((Integer)q.removeFirst()).intValue()); |
213 |
> |
assertEquals(i, q.removeFirst()); |
214 |
|
} |
215 |
|
try { |
216 |
|
q.removeFirst(); |
217 |
|
shouldThrow(); |
218 |
|
} catch (NoSuchElementException success) {} |
219 |
+ |
assertNull(q.peekFirst()); |
220 |
+ |
} |
221 |
+ |
|
222 |
+ |
/** |
223 |
+ |
* removeLast() removes last element, or throws NSEE if empty |
224 |
+ |
*/ |
225 |
+ |
public void testRemoveLast() { |
226 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
227 |
+ |
for (int i = SIZE - 1; i >= 0; --i) { |
228 |
+ |
assertEquals(i, q.removeLast()); |
229 |
+ |
} |
230 |
+ |
try { |
231 |
+ |
q.removeLast(); |
232 |
+ |
shouldThrow(); |
233 |
+ |
} catch (NoSuchElementException success) {} |
234 |
+ |
assertNull(q.peekLast()); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
238 |
< |
* remove removes next element, or throws NSEE if empty |
238 |
> |
* remove removes next element, or throws NSEE if empty |
239 |
|
*/ |
240 |
|
public void testRemove() { |
241 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
242 |
|
for (int i = 0; i < SIZE; ++i) { |
243 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
243 |
> |
assertEquals(i, q.remove()); |
244 |
|
} |
245 |
|
try { |
246 |
|
q.remove(); |
285 |
|
LinkedBlockingDeque q = populatedDeque(3); |
286 |
|
q.pollLast(); |
287 |
|
q.addFirst(four); |
288 |
< |
assertEquals(four,q.peekFirst()); |
288 |
> |
assertSame(four, q.peekFirst()); |
289 |
|
} |
290 |
|
|
291 |
|
/** |
295 |
|
LinkedBlockingDeque q = populatedDeque(3); |
296 |
|
q.pollLast(); |
297 |
|
q.addLast(four); |
298 |
< |
assertEquals(four,q.peekLast()); |
298 |
> |
assertSame(four, q.peekLast()); |
299 |
|
} |
300 |
|
|
301 |
|
|
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
< |
* Constructor throws IAE if capacity argument nonpositive |
312 |
> |
* Constructor throws IAE if capacity argument nonpositive |
313 |
|
*/ |
314 |
|
public void testConstructor2() { |
315 |
|
try { |
356 |
|
* Deque contains all elements of collection used to initialize |
357 |
|
*/ |
358 |
|
public void testConstructor6() { |
359 |
< |
try { |
360 |
< |
Integer[] ints = new Integer[SIZE]; |
361 |
< |
for (int i = 0; i < SIZE; ++i) |
362 |
< |
ints[i] = new Integer(i); |
363 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
364 |
< |
for (int i = 0; i < SIZE; ++i) |
332 |
< |
assertEquals(ints[i], q.poll()); |
333 |
< |
} |
334 |
< |
finally {} |
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) |
364 |
> |
assertEquals(ints[i], q.poll()); |
365 |
|
} |
366 |
|
|
367 |
|
/** |
453 |
|
LinkedBlockingDeque q = populatedDeque(3); |
454 |
|
q.pollLast(); |
455 |
|
q.push(four); |
456 |
< |
assertEquals(four,q.peekFirst()); |
456 |
> |
assertSame(four, q.peekFirst()); |
457 |
|
} |
458 |
|
|
459 |
|
|
460 |
|
/** |
461 |
< |
* pop removes next element, or throws NSEE if empty |
461 |
> |
* pop removes next element, or throws NSEE if empty |
462 |
|
*/ |
463 |
|
public void testPop() { |
464 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
465 |
|
for (int i = 0; i < SIZE; ++i) { |
466 |
< |
assertEquals(i, ((Integer)q.pop()).intValue()); |
466 |
> |
assertEquals(i, q.pop()); |
467 |
|
} |
468 |
|
try { |
469 |
|
q.pop(); |
529 |
|
shouldThrow(); |
530 |
|
} catch (NullPointerException success) {} |
531 |
|
} |
532 |
+ |
|
533 |
|
/** |
534 |
|
* addAll of a collection with any null elements throws NPE after |
535 |
|
* possibly adding some elements |
544 |
|
shouldThrow(); |
545 |
|
} catch (NullPointerException success) {} |
546 |
|
} |
547 |
+ |
|
548 |
|
/** |
549 |
|
* addAll throws ISE if not enough room |
550 |
|
*/ |
558 |
|
shouldThrow(); |
559 |
|
} catch (IllegalStateException success) {} |
560 |
|
} |
561 |
+ |
|
562 |
|
/** |
563 |
|
* Deque contains all elements, in traversal order, of successful addAll |
564 |
|
*/ |
565 |
|
public void testAddAll5() { |
566 |
< |
try { |
567 |
< |
Integer[] empty = new Integer[0]; |
568 |
< |
Integer[] ints = new Integer[SIZE]; |
569 |
< |
for (int i = 0; i < SIZE; ++i) |
570 |
< |
ints[i] = new Integer(i); |
571 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
572 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
573 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
574 |
< |
for (int i = 0; i < SIZE; ++i) |
542 |
< |
assertEquals(ints[i], q.poll()); |
543 |
< |
} |
544 |
< |
finally {} |
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) |
574 |
> |
assertEquals(ints[i], q.poll()); |
575 |
|
} |
576 |
|
|
577 |
|
|
603 |
|
* put blocks interruptibly if full |
604 |
|
*/ |
605 |
|
public void testBlockingPut() throws InterruptedException { |
606 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
607 |
|
Thread t = new Thread(new CheckedRunnable() { |
608 |
< |
public void realRun() { |
609 |
< |
int added = 0; |
608 |
> |
public void realRun() throws InterruptedException { |
609 |
> |
for (int i = 0; i < SIZE; ++i) |
610 |
> |
q.put(i); |
611 |
> |
assertEquals(SIZE, q.size()); |
612 |
> |
assertEquals(0, q.remainingCapacity()); |
613 |
|
try { |
614 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
615 |
< |
for (int i = 0; i < SIZE; ++i) { |
616 |
< |
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 |
< |
} |
614 |
> |
q.put(99); |
615 |
> |
shouldThrow(); |
616 |
> |
} catch (InterruptedException success) {} |
617 |
|
}}); |
618 |
|
|
619 |
|
t.start(); |
620 |
< |
Thread.sleep(SHORT_DELAY_MS); |
620 |
> |
delay(SHORT_DELAY_MS); |
621 |
|
t.interrupt(); |
622 |
|
t.join(); |
623 |
+ |
assertEquals(SIZE, q.size()); |
624 |
+ |
assertEquals(0, q.remainingCapacity()); |
625 |
|
} |
626 |
|
|
627 |
|
/** |
628 |
|
* put blocks waiting for take when full |
629 |
|
*/ |
630 |
|
public void testPutWithTake() throws InterruptedException { |
631 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
631 |
> |
final int capacity = 2; |
632 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
633 |
|
Thread t = new Thread(new CheckedRunnable() { |
634 |
< |
public void realRun() { |
635 |
< |
int added = 0; |
634 |
> |
public void realRun() throws InterruptedException { |
635 |
> |
for (int i = 0; i < capacity + 1; i++) |
636 |
> |
q.put(i); |
637 |
|
try { |
638 |
< |
q.put(new Object()); |
639 |
< |
++added; |
640 |
< |
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 |
< |
} |
638 |
> |
q.put(99); |
639 |
> |
shouldThrow(); |
640 |
> |
} catch (InterruptedException success) {} |
641 |
|
}}); |
642 |
|
|
643 |
|
t.start(); |
644 |
< |
Thread.sleep(SHORT_DELAY_MS); |
645 |
< |
q.take(); |
644 |
> |
delay(SHORT_DELAY_MS); |
645 |
> |
assertEquals(q.remainingCapacity(), 0); |
646 |
> |
assertEquals(0, q.take()); |
647 |
> |
delay(SHORT_DELAY_MS); |
648 |
|
t.interrupt(); |
649 |
|
t.join(); |
650 |
+ |
assertEquals(q.remainingCapacity(), 0); |
651 |
|
} |
652 |
|
|
653 |
|
/** |
655 |
|
*/ |
656 |
|
public void testTimedOffer() throws InterruptedException { |
657 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
658 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
658 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
659 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
660 |
|
public void realRun() throws InterruptedException { |
661 |
|
q.put(new Object()); |
662 |
|
q.put(new Object()); |
663 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
664 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
665 |
< |
}}; |
663 |
> |
long startTime = System.nanoTime(); |
664 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
665 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
666 |
> |
pleaseInterrupt.countDown(); |
667 |
> |
try { |
668 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
669 |
> |
shouldThrow(); |
670 |
> |
} catch (InterruptedException success) {} |
671 |
> |
}}); |
672 |
|
|
673 |
< |
t.start(); |
642 |
< |
Thread.sleep(SMALL_DELAY_MS); |
673 |
> |
await(pleaseInterrupt); |
674 |
|
t.interrupt(); |
675 |
< |
t.join(); |
675 |
> |
awaitTermination(t); |
676 |
|
} |
677 |
|
|
678 |
|
/** |
681 |
|
public void testTake() throws InterruptedException { |
682 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
683 |
|
for (int i = 0; i < SIZE; ++i) { |
684 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
684 |
> |
assertEquals(i, q.take()); |
685 |
|
} |
686 |
|
} |
687 |
|
|
688 |
|
/** |
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 |
– |
/** |
689 |
|
* Take removes existing elements until empty, then blocks interruptibly |
690 |
|
*/ |
691 |
|
public void testBlockingTake() throws InterruptedException { |
692 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
692 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
693 |
> |
Thread t = new Thread(new CheckedRunnable() { |
694 |
|
public void realRun() throws InterruptedException { |
679 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
695 |
|
for (int i = 0; i < SIZE; ++i) { |
696 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
696 |
> |
assertEquals(i, q.take()); |
697 |
|
} |
698 |
< |
q.take(); |
699 |
< |
}}; |
698 |
> |
try { |
699 |
> |
q.take(); |
700 |
> |
shouldThrow(); |
701 |
> |
} catch (InterruptedException success) {} |
702 |
> |
}}); |
703 |
|
|
704 |
|
t.start(); |
705 |
< |
Thread.sleep(SHORT_DELAY_MS); |
705 |
> |
delay(SHORT_DELAY_MS); |
706 |
|
t.interrupt(); |
707 |
|
t.join(); |
708 |
|
} |
714 |
|
public void testPoll() { |
715 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
716 |
|
for (int i = 0; i < SIZE; ++i) { |
717 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
717 |
> |
assertEquals(i, q.poll()); |
718 |
|
} |
719 |
|
assertNull(q.poll()); |
720 |
|
} |
725 |
|
public void testTimedPoll0() throws InterruptedException { |
726 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
727 |
|
for (int i = 0; i < SIZE; ++i) { |
728 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
728 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
729 |
|
} |
730 |
|
assertNull(q.poll(0, MILLISECONDS)); |
731 |
|
} |
736 |
|
public void testTimedPoll() throws InterruptedException { |
737 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
738 |
|
for (int i = 0; i < SIZE; ++i) { |
739 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
739 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
740 |
|
} |
741 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
742 |
|
} |
746 |
|
* returning timeout status |
747 |
|
*/ |
748 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
749 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
749 |
> |
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
750 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
751 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
752 |
|
public void realRun() throws InterruptedException { |
733 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
753 |
|
for (int i = 0; i < SIZE; ++i) { |
754 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
754 |
> |
long t0 = System.nanoTime(); |
755 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
756 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
757 |
|
} |
758 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
759 |
< |
}}; |
760 |
< |
|
761 |
< |
t.start(); |
762 |
< |
Thread.sleep(SHORT_DELAY_MS); |
763 |
< |
t.interrupt(); |
764 |
< |
t.join(); |
765 |
< |
} |
766 |
< |
|
746 |
< |
/** |
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 |
> |
long t0 = System.nanoTime(); |
759 |
> |
aboutToWait.countDown(); |
760 |
> |
try { |
761 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
762 |
> |
shouldThrow(); |
763 |
> |
} catch (InterruptedException success) { |
764 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
765 |
> |
} |
766 |
> |
}}); |
767 |
|
|
768 |
< |
t.start(); |
769 |
< |
Thread.sleep(SMALL_DELAY_MS); |
761 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
768 |
> |
aboutToWait.await(); |
769 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
770 |
|
t.interrupt(); |
771 |
< |
t.join(); |
771 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
772 |
> |
checkEmpty(q); |
773 |
|
} |
774 |
|
|
766 |
– |
|
775 |
|
/** |
776 |
|
* putFirst(null) throws NPE |
777 |
|
*/ |
778 |
< |
public void testPutFirstNull() throws InterruptedException { |
778 |
> |
public void testPutFirstNull() throws InterruptedException { |
779 |
|
try { |
780 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
781 |
|
q.putFirst(null); |
782 |
|
shouldThrow(); |
783 |
|
} catch (NullPointerException success) {} |
784 |
< |
} |
784 |
> |
} |
785 |
|
|
786 |
|
/** |
787 |
|
* all elements successfully putFirst are contained |
788 |
|
*/ |
789 |
< |
public void testPutFirst() throws InterruptedException { |
790 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
791 |
< |
for (int i = 0; i < SIZE; ++i) { |
792 |
< |
Integer I = new Integer(i); |
793 |
< |
q.putFirst(I); |
794 |
< |
assertTrue(q.contains(I)); |
795 |
< |
} |
796 |
< |
assertEquals(0, q.remainingCapacity()); |
789 |
> |
public void testPutFirst() throws InterruptedException { |
790 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
791 |
> |
for (int i = 0; i < SIZE; ++i) { |
792 |
> |
Integer I = new Integer(i); |
793 |
> |
q.putFirst(I); |
794 |
> |
assertTrue(q.contains(I)); |
795 |
> |
} |
796 |
> |
assertEquals(0, q.remainingCapacity()); |
797 |
|
} |
798 |
|
|
799 |
|
/** |
800 |
|
* putFirst blocks interruptibly if full |
801 |
|
*/ |
802 |
|
public void testBlockingPutFirst() throws InterruptedException { |
803 |
< |
Thread t = new Thread(new Runnable() { |
804 |
< |
public void run() { |
805 |
< |
int added = 0; |
806 |
< |
try { |
807 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
808 |
< |
for (int i = 0; i < SIZE; ++i) { |
809 |
< |
q.putFirst(new Integer(i)); |
810 |
< |
++added; |
811 |
< |
} |
812 |
< |
q.putFirst(new Integer(SIZE)); |
813 |
< |
threadShouldThrow(); |
814 |
< |
} catch (InterruptedException success) { |
807 |
< |
threadAssertEquals(added, SIZE); |
808 |
< |
} |
809 |
< |
}}); |
803 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
804 |
> |
Thread t = new Thread(new CheckedRunnable() { |
805 |
> |
public void realRun() throws InterruptedException { |
806 |
> |
for (int i = 0; i < SIZE; ++i) |
807 |
> |
q.putFirst(i); |
808 |
> |
assertEquals(SIZE, q.size()); |
809 |
> |
assertEquals(0, q.remainingCapacity()); |
810 |
> |
try { |
811 |
> |
q.putFirst(99); |
812 |
> |
shouldThrow(); |
813 |
> |
} catch (InterruptedException success) {} |
814 |
> |
}}); |
815 |
|
|
816 |
|
t.start(); |
817 |
< |
Thread.sleep(SHORT_DELAY_MS); |
817 |
> |
delay(SHORT_DELAY_MS); |
818 |
|
t.interrupt(); |
819 |
|
t.join(); |
820 |
+ |
assertEquals(SIZE, q.size()); |
821 |
+ |
assertEquals(0, q.remainingCapacity()); |
822 |
|
} |
823 |
|
|
824 |
|
/** |
825 |
|
* putFirst blocks waiting for take when full |
826 |
|
*/ |
827 |
|
public void testPutFirstWithTake() throws InterruptedException { |
828 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
829 |
< |
Thread t = new Thread(new Runnable() { |
830 |
< |
public void run() { |
831 |
< |
int added = 0; |
832 |
< |
try { |
833 |
< |
q.putFirst(new Object()); |
834 |
< |
++added; |
835 |
< |
q.putFirst(new Object()); |
836 |
< |
++added; |
837 |
< |
q.putFirst(new Object()); |
838 |
< |
++added; |
832 |
< |
q.putFirst(new Object()); |
833 |
< |
++added; |
834 |
< |
threadShouldThrow(); |
835 |
< |
} catch (InterruptedException success) { |
836 |
< |
threadAssertTrue(added >= 2); |
837 |
< |
} |
838 |
< |
} |
839 |
< |
}); |
828 |
> |
final int capacity = 2; |
829 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
830 |
> |
Thread t = new Thread(new CheckedRunnable() { |
831 |
> |
public void realRun() throws InterruptedException { |
832 |
> |
for (int i = 0; i < capacity + 1; i++) |
833 |
> |
q.putFirst(i); |
834 |
> |
try { |
835 |
> |
q.putFirst(99); |
836 |
> |
shouldThrow(); |
837 |
> |
} catch (InterruptedException success) {} |
838 |
> |
}}); |
839 |
|
|
840 |
|
t.start(); |
841 |
< |
Thread.sleep(SHORT_DELAY_MS); |
842 |
< |
q.take(); |
841 |
> |
delay(SHORT_DELAY_MS); |
842 |
> |
assertEquals(q.remainingCapacity(), 0); |
843 |
> |
assertEquals(capacity - 1, q.take()); |
844 |
> |
delay(SHORT_DELAY_MS); |
845 |
|
t.interrupt(); |
846 |
|
t.join(); |
847 |
+ |
assertEquals(q.remainingCapacity(), 0); |
848 |
|
} |
849 |
|
|
850 |
|
/** |
852 |
|
*/ |
853 |
|
public void testTimedOfferFirst() throws InterruptedException { |
854 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
855 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
855 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
856 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
857 |
|
public void realRun() throws InterruptedException { |
858 |
|
q.putFirst(new Object()); |
859 |
|
q.putFirst(new Object()); |
860 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
861 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
862 |
< |
}}; |
860 |
> |
long startTime = System.nanoTime(); |
861 |
> |
assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS)); |
862 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
863 |
> |
pleaseInterrupt.countDown(); |
864 |
> |
try { |
865 |
> |
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
866 |
> |
shouldThrow(); |
867 |
> |
} catch (InterruptedException success) {} |
868 |
> |
}}); |
869 |
|
|
870 |
< |
t.start(); |
862 |
< |
Thread.sleep(SMALL_DELAY_MS); |
870 |
> |
await(pleaseInterrupt); |
871 |
|
t.interrupt(); |
872 |
< |
t.join(); |
872 |
> |
awaitTermination(t); |
873 |
|
} |
874 |
|
|
875 |
|
/** |
878 |
|
public void testTakeFirst() throws InterruptedException { |
879 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
880 |
|
for (int i = 0; i < SIZE; ++i) { |
881 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
881 |
> |
assertEquals(i, q.takeFirst()); |
882 |
|
} |
883 |
|
} |
884 |
|
|
893 |
|
}}; |
894 |
|
|
895 |
|
t.start(); |
896 |
< |
Thread.sleep(SHORT_DELAY_MS); |
896 |
> |
delay(SHORT_DELAY_MS); |
897 |
|
t.interrupt(); |
898 |
|
t.join(); |
899 |
|
} |
902 |
|
* TakeFirst removes existing elements until empty, then blocks interruptibly |
903 |
|
*/ |
904 |
|
public void testBlockingTakeFirst() throws InterruptedException { |
905 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
905 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
906 |
> |
Thread t = new Thread(new CheckedRunnable() { |
907 |
|
public void realRun() throws InterruptedException { |
908 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
909 |
< |
for (int i = 0; i < SIZE; ++i) { |
910 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
911 |
< |
} |
912 |
< |
q.takeFirst(); |
913 |
< |
}}; |
908 |
> |
for (int i = 0; i < SIZE; ++i) |
909 |
> |
assertEquals(i, q.takeFirst()); |
910 |
> |
try { |
911 |
> |
q.takeFirst(); |
912 |
> |
shouldThrow(); |
913 |
> |
} catch (InterruptedException success) {} |
914 |
> |
}}); |
915 |
|
|
916 |
|
t.start(); |
917 |
< |
Thread.sleep(SHORT_DELAY_MS); |
917 |
> |
delay(SHORT_DELAY_MS); |
918 |
|
t.interrupt(); |
919 |
|
t.join(); |
920 |
|
} |
926 |
|
public void testTimedPollFirst0() throws InterruptedException { |
927 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
928 |
|
for (int i = 0; i < SIZE; ++i) { |
929 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue()); |
929 |
> |
assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
930 |
|
} |
931 |
|
assertNull(q.pollFirst(0, MILLISECONDS)); |
932 |
|
} |
937 |
|
public void testTimedPollFirst() throws InterruptedException { |
938 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
939 |
|
for (int i = 0; i < SIZE; ++i) { |
940 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
940 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
941 |
|
} |
942 |
|
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
943 |
|
} |
947 |
|
* returning timeout status |
948 |
|
*/ |
949 |
|
public void testInterruptedTimedPollFirst() throws InterruptedException { |
950 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
950 |
> |
Thread t = new Thread(new CheckedRunnable() { |
951 |
|
public void realRun() throws InterruptedException { |
952 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
953 |
|
for (int i = 0; i < SIZE; ++i) { |
954 |
< |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
954 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
955 |
|
} |
956 |
< |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
957 |
< |
}}; |
956 |
> |
try { |
957 |
> |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
958 |
> |
shouldThrow(); |
959 |
> |
} catch (InterruptedException success) {} |
960 |
> |
}}); |
961 |
|
|
962 |
|
t.start(); |
963 |
< |
Thread.sleep(SHORT_DELAY_MS); |
963 |
> |
delay(SHORT_DELAY_MS); |
964 |
|
t.interrupt(); |
965 |
|
t.join(); |
966 |
|
} |
967 |
|
|
968 |
|
/** |
969 |
< |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
970 |
< |
* on interruption throws |
969 |
> |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
970 |
> |
* on interruption throws |
971 |
|
*/ |
972 |
|
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
973 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
974 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
974 |
> |
Thread t = new Thread(new CheckedRunnable() { |
975 |
|
public void realRun() throws InterruptedException { |
976 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
977 |
< |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
978 |
< |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
979 |
< |
}}; |
976 |
> |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
977 |
> |
assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
978 |
> |
try { |
979 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
980 |
> |
shouldThrow(); |
981 |
> |
} catch (InterruptedException success) {} |
982 |
> |
}}); |
983 |
|
|
984 |
|
t.start(); |
985 |
< |
Thread.sleep(SMALL_DELAY_MS); |
985 |
> |
delay(SMALL_DELAY_MS); |
986 |
|
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
987 |
|
t.interrupt(); |
988 |
|
t.join(); |
991 |
|
/** |
992 |
|
* putLast(null) throws NPE |
993 |
|
*/ |
994 |
< |
public void testPutLastNull() throws InterruptedException { |
994 |
> |
public void testPutLastNull() throws InterruptedException { |
995 |
|
try { |
996 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
997 |
|
q.putLast(null); |
998 |
|
shouldThrow(); |
999 |
|
} catch (NullPointerException success) {} |
1000 |
< |
} |
1000 |
> |
} |
1001 |
|
|
1002 |
|
/** |
1003 |
|
* all elements successfully putLast are contained |
1004 |
|
*/ |
1005 |
< |
public void testPutLast() throws InterruptedException { |
1006 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1007 |
< |
for (int i = 0; i < SIZE; ++i) { |
1008 |
< |
Integer I = new Integer(i); |
1009 |
< |
q.putLast(I); |
1010 |
< |
assertTrue(q.contains(I)); |
1011 |
< |
} |
1012 |
< |
assertEquals(0, q.remainingCapacity()); |
1005 |
> |
public void testPutLast() throws InterruptedException { |
1006 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1007 |
> |
for (int i = 0; i < SIZE; ++i) { |
1008 |
> |
Integer I = new Integer(i); |
1009 |
> |
q.putLast(I); |
1010 |
> |
assertTrue(q.contains(I)); |
1011 |
> |
} |
1012 |
> |
assertEquals(0, q.remainingCapacity()); |
1013 |
|
} |
1014 |
|
|
1015 |
|
/** |
1016 |
|
* putLast blocks interruptibly if full |
1017 |
|
*/ |
1018 |
|
public void testBlockingPutLast() throws InterruptedException { |
1019 |
< |
Thread t = new Thread(new Runnable() { |
1020 |
< |
public void run() { |
1021 |
< |
int added = 0; |
1022 |
< |
try { |
1023 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1024 |
< |
for (int i = 0; i < SIZE; ++i) { |
1025 |
< |
q.putLast(new Integer(i)); |
1026 |
< |
++added; |
1027 |
< |
} |
1028 |
< |
q.putLast(new Integer(SIZE)); |
1029 |
< |
threadShouldThrow(); |
1030 |
< |
} catch (InterruptedException success) { |
1031 |
< |
threadAssertEquals(added, SIZE); |
1016 |
< |
} |
1017 |
< |
}}); |
1019 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1020 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1021 |
> |
public void realRun() throws InterruptedException { |
1022 |
> |
for (int i = 0; i < SIZE; ++i) |
1023 |
> |
q.putLast(i); |
1024 |
> |
assertEquals(SIZE, q.size()); |
1025 |
> |
assertEquals(0, q.remainingCapacity()); |
1026 |
> |
try { |
1027 |
> |
q.putLast(99); |
1028 |
> |
shouldThrow(); |
1029 |
> |
} catch (InterruptedException success) {} |
1030 |
> |
}}); |
1031 |
> |
|
1032 |
|
t.start(); |
1033 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1033 |
> |
delay(SHORT_DELAY_MS); |
1034 |
|
t.interrupt(); |
1035 |
|
t.join(); |
1036 |
+ |
assertEquals(SIZE, q.size()); |
1037 |
+ |
assertEquals(0, q.remainingCapacity()); |
1038 |
|
} |
1039 |
|
|
1040 |
|
/** |
1041 |
|
* putLast blocks waiting for take when full |
1042 |
|
*/ |
1043 |
|
public void testPutLastWithTake() throws InterruptedException { |
1044 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1045 |
< |
Thread t = new Thread(new Runnable() { |
1046 |
< |
public void run() { |
1047 |
< |
int added = 0; |
1048 |
< |
try { |
1049 |
< |
q.putLast(new Object()); |
1050 |
< |
++added; |
1051 |
< |
q.putLast(new Object()); |
1052 |
< |
++added; |
1053 |
< |
q.putLast(new Object()); |
1054 |
< |
++added; |
1039 |
< |
q.putLast(new Object()); |
1040 |
< |
++added; |
1041 |
< |
threadShouldThrow(); |
1042 |
< |
} catch (InterruptedException success) { |
1043 |
< |
threadAssertTrue(added >= 2); |
1044 |
< |
} |
1045 |
< |
} |
1046 |
< |
}); |
1044 |
> |
final int capacity = 2; |
1045 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1046 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1047 |
> |
public void realRun() throws InterruptedException { |
1048 |
> |
for (int i = 0; i < capacity + 1; i++) |
1049 |
> |
q.putLast(i); |
1050 |
> |
try { |
1051 |
> |
q.putLast(99); |
1052 |
> |
shouldThrow(); |
1053 |
> |
} catch (InterruptedException success) {} |
1054 |
> |
}}); |
1055 |
|
|
1056 |
|
t.start(); |
1057 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1058 |
< |
q.take(); |
1057 |
> |
delay(SHORT_DELAY_MS); |
1058 |
> |
assertEquals(q.remainingCapacity(), 0); |
1059 |
> |
assertEquals(0, q.take()); |
1060 |
> |
delay(SHORT_DELAY_MS); |
1061 |
|
t.interrupt(); |
1062 |
|
t.join(); |
1063 |
+ |
assertEquals(q.remainingCapacity(), 0); |
1064 |
|
} |
1065 |
|
|
1066 |
|
/** |
1068 |
|
*/ |
1069 |
|
public void testTimedOfferLast() throws InterruptedException { |
1070 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1071 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1071 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1072 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1073 |
|
public void realRun() throws InterruptedException { |
1074 |
|
q.putLast(new Object()); |
1075 |
|
q.putLast(new Object()); |
1076 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1077 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1078 |
< |
}}; |
1076 |
> |
long startTime = System.nanoTime(); |
1077 |
> |
assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS)); |
1078 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1079 |
> |
pleaseInterrupt.countDown(); |
1080 |
> |
try { |
1081 |
> |
q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
1082 |
> |
shouldThrow(); |
1083 |
> |
} catch (InterruptedException success) {} |
1084 |
> |
}}); |
1085 |
|
|
1086 |
< |
t.start(); |
1069 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1086 |
> |
await(pleaseInterrupt); |
1087 |
|
t.interrupt(); |
1088 |
< |
t.join(); |
1088 |
> |
awaitTermination(t); |
1089 |
|
} |
1090 |
|
|
1091 |
|
/** |
1094 |
|
public void testTakeLast() throws InterruptedException { |
1095 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1096 |
|
for (int i = 0; i < SIZE; ++i) { |
1097 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1097 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1098 |
|
} |
1099 |
|
} |
1100 |
|
|
1109 |
|
}}; |
1110 |
|
|
1111 |
|
t.start(); |
1112 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1112 |
> |
delay(SHORT_DELAY_MS); |
1113 |
|
t.interrupt(); |
1114 |
|
t.join(); |
1115 |
|
} |
1118 |
|
* TakeLast removes existing elements until empty, then blocks interruptibly |
1119 |
|
*/ |
1120 |
|
public void testBlockingTakeLast() throws InterruptedException { |
1121 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1121 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1122 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1123 |
|
public void realRun() throws InterruptedException { |
1124 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1125 |
< |
for (int i = 0; i < SIZE; ++i) { |
1126 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1127 |
< |
} |
1128 |
< |
q.takeLast(); |
1129 |
< |
}}; |
1124 |
> |
for (int i = 0; i < SIZE; ++i) |
1125 |
> |
assertEquals(SIZE - 1 - i, q.takeLast()); |
1126 |
> |
try { |
1127 |
> |
q.takeLast(); |
1128 |
> |
shouldThrow(); |
1129 |
> |
} catch (InterruptedException success) {} |
1130 |
> |
}}); |
1131 |
|
|
1132 |
|
t.start(); |
1133 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1133 |
> |
delay(SHORT_DELAY_MS); |
1134 |
|
t.interrupt(); |
1135 |
|
t.join(); |
1136 |
|
} |
1137 |
|
|
1119 |
– |
|
1138 |
|
/** |
1139 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1140 |
|
*/ |
1141 |
|
public void testTimedPollLast0() throws InterruptedException { |
1142 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1143 |
|
for (int i = 0; i < SIZE; ++i) { |
1144 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue()); |
1144 |
> |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1145 |
|
} |
1146 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1147 |
|
} |
1152 |
|
public void testTimedPollLast() throws InterruptedException { |
1153 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1154 |
|
for (int i = 0; i < SIZE; ++i) { |
1155 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1155 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1156 |
|
} |
1157 |
|
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1158 |
|
} |
1162 |
|
* returning timeout status |
1163 |
|
*/ |
1164 |
|
public void testInterruptedTimedPollLast() throws InterruptedException { |
1165 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1165 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1166 |
|
public void realRun() throws InterruptedException { |
1167 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1168 |
|
for (int i = 0; i < SIZE; ++i) { |
1169 |
< |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1169 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1170 |
|
} |
1171 |
< |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1172 |
< |
}}; |
1171 |
> |
try { |
1172 |
> |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1173 |
> |
shouldThrow(); |
1174 |
> |
} catch (InterruptedException success) {} |
1175 |
> |
}}); |
1176 |
|
|
1177 |
|
t.start(); |
1178 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1178 |
> |
delay(SHORT_DELAY_MS); |
1179 |
|
t.interrupt(); |
1180 |
|
t.join(); |
1181 |
|
} |
1182 |
|
|
1183 |
|
/** |
1184 |
< |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1185 |
< |
* on interruption throws |
1184 |
> |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1185 |
> |
* on interruption throws |
1186 |
|
*/ |
1187 |
|
public void testTimedPollWithOfferLast() throws InterruptedException { |
1188 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1197 |
|
}}); |
1198 |
|
|
1199 |
|
t.start(); |
1200 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1200 |
> |
delay(SMALL_DELAY_MS); |
1201 |
|
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1202 |
|
t.interrupt(); |
1203 |
|
t.join(); |
1210 |
|
public void testElement() { |
1211 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1212 |
|
for (int i = 0; i < SIZE; ++i) { |
1213 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
1213 |
> |
assertEquals(i, q.element()); |
1214 |
|
q.poll(); |
1215 |
|
} |
1216 |
|
try { |
1225 |
|
public void testRemoveElement() { |
1226 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1227 |
|
for (int i = 1; i < SIZE; i+=2) { |
1228 |
< |
assertTrue(q.remove(new Integer(i))); |
1228 |
> |
assertTrue(q.contains(i)); |
1229 |
> |
assertTrue(q.remove(i)); |
1230 |
> |
assertFalse(q.contains(i)); |
1231 |
> |
assertTrue(q.contains(i-1)); |
1232 |
|
} |
1233 |
|
for (int i = 0; i < SIZE; i+=2) { |
1234 |
< |
assertTrue(q.remove(new Integer(i))); |
1235 |
< |
assertFalse(q.remove(new Integer(i+1))); |
1234 |
> |
assertTrue(q.contains(i)); |
1235 |
> |
assertTrue(q.remove(i)); |
1236 |
> |
assertFalse(q.contains(i)); |
1237 |
> |
assertFalse(q.remove(i+1)); |
1238 |
> |
assertFalse(q.contains(i+1)); |
1239 |
|
} |
1240 |
|
assertTrue(q.isEmpty()); |
1241 |
|
} |
1318 |
|
} |
1319 |
|
|
1320 |
|
/** |
1321 |
< |
* toArray contains all elements |
1321 |
> |
* toArray contains all elements in FIFO order |
1322 |
|
*/ |
1323 |
|
public void testToArray() throws InterruptedException{ |
1324 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1325 |
|
Object[] o = q.toArray(); |
1326 |
|
for (int i = 0; i < o.length; i++) |
1327 |
< |
assertEquals(o[i], q.take()); |
1327 |
> |
assertSame(o[i], q.poll()); |
1328 |
|
} |
1329 |
|
|
1330 |
|
/** |
1331 |
< |
* toArray(a) contains all elements |
1331 |
> |
* toArray(a) contains all elements in FIFO order |
1332 |
|
*/ |
1333 |
< |
public void testToArray2() throws InterruptedException { |
1334 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1333 |
> |
public void testToArray2() { |
1334 |
> |
LinkedBlockingDeque<Integer> q = populatedDeque(SIZE); |
1335 |
|
Integer[] ints = new Integer[SIZE]; |
1336 |
< |
ints = (Integer[])q.toArray(ints); |
1336 |
> |
Integer[] array = q.toArray(ints); |
1337 |
> |
assertSame(ints, array); |
1338 |
|
for (int i = 0; i < ints.length; i++) |
1339 |
< |
assertEquals(ints[i], q.take()); |
1339 |
> |
assertSame(ints[i], q.remove()); |
1340 |
|
} |
1341 |
|
|
1342 |
|
/** |
1343 |
< |
* toArray(null) throws NPE |
1343 |
> |
* toArray(null) throws NullPointerException |
1344 |
|
*/ |
1345 |
< |
public void testToArray_BadArg() { |
1345 |
> |
public void testToArray_NullArg() { |
1346 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1347 |
|
try { |
1348 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1320 |
< |
Object o[] = q.toArray(null); |
1348 |
> |
q.toArray(null); |
1349 |
|
shouldThrow(); |
1350 |
|
} catch (NullPointerException success) {} |
1351 |
|
} |
1352 |
|
|
1353 |
|
/** |
1354 |
< |
* toArray with incompatible array type throws CCE |
1354 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
1355 |
|
*/ |
1356 |
|
public void testToArray1_BadArg() { |
1357 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1358 |
|
try { |
1359 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1331 |
< |
Object o[] = q.toArray(new String[10] ); |
1359 |
> |
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(); |
1380 |
< |
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); |
1426 |
|
|
1427 |
|
|
1428 |
|
/** |
1429 |
< |
* Descending iterator iterates through all elements |
1429 |
> |
* Descending iterator iterates through all elements |
1430 |
|
*/ |
1431 |
|
public void testDescendingIterator() { |
1432 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1445 |
|
} |
1446 |
|
|
1447 |
|
/** |
1448 |
< |
* Descending iterator ordering is reverse FIFO |
1448 |
> |
* Descending iterator ordering is reverse FIFO |
1449 |
|
*/ |
1450 |
|
public void testDescendingIteratorOrdering() { |
1451 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
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(); |
1432 |
< |
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()); |
1520 |
> |
delay(SMALL_DELAY_MS); |
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() { |
1541 |
|
public void realRun() throws InterruptedException { |
1542 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1542 |
> |
delay(SMALL_DELAY_MS); |
1543 |
|
q.put(one); |
1544 |
|
}}); |
1545 |
|
|
1654 |
|
} |
1655 |
|
|
1656 |
|
/** |
1657 |
< |
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1657 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
1658 |
|
*/ |
1659 |
|
public void testDrainToN() { |
1660 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1663 |
|
assertTrue(q.offer(new Integer(j))); |
1664 |
|
ArrayList l = new ArrayList(); |
1665 |
|
q.drainTo(l, i); |
1666 |
< |
int k = (i < SIZE)? i : SIZE; |
1666 |
> |
int k = (i < SIZE) ? i : SIZE; |
1667 |
|
assertEquals(l.size(), k); |
1668 |
|
assertEquals(q.size(), SIZE-k); |
1669 |
|
for (int j = 0; j < k; ++j) |