ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.39
Committed: Fri May 27 20:07:24 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.38: +340 -128 lines
Log Message:
performance and robustness improvements to queue tests

File Contents

# Content
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/publicdomain/zero/1.0/
5 */
6
7 import junit.framework.*;
8 import java.util.*;
9 import java.util.concurrent.*;
10 import static java.util.concurrent.TimeUnit.MILLISECONDS;
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());
29 }
30
31 public static Test suite() {
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<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)));
47 assertFalse(q.isEmpty());
48 assertEquals(0, q.remainingCapacity());
49 assertEquals(n, q.size());
50 return q;
51 }
52
53 /**
54 * isEmpty is true before add, false after
55 */
56 public void testEmpty() {
57 LinkedBlockingDeque q = new LinkedBlockingDeque();
58 assertTrue(q.isEmpty());
59 q.add(new Integer(1));
60 assertFalse(q.isEmpty());
61 q.add(new Integer(2));
62 q.removeFirst();
63 q.removeFirst();
64 assertTrue(q.isEmpty());
65 }
66
67 /**
68 * size changes when elements added and removed
69 */
70 public void testSize() {
71 LinkedBlockingDeque q = populatedDeque(SIZE);
72 for (int i = 0; i < SIZE; ++i) {
73 assertEquals(SIZE-i, q.size());
74 q.removeFirst();
75 }
76 for (int i = 0; i < SIZE; ++i) {
77 assertEquals(i, q.size());
78 q.add(new Integer(i));
79 }
80 }
81
82 /**
83 * offer(null) throws NPE
84 */
85 public void testOfferFirstNull() {
86 try {
87 LinkedBlockingDeque q = new LinkedBlockingDeque();
88 q.offerFirst(null);
89 shouldThrow();
90 } catch (NullPointerException success) {}
91 }
92
93 /**
94 * OfferFirst succeeds
95 */
96 public void testOfferFirst() {
97 LinkedBlockingDeque q = new LinkedBlockingDeque();
98 assertTrue(q.offerFirst(new Integer(0)));
99 assertTrue(q.offerFirst(new Integer(1)));
100 }
101
102 /**
103 * OfferLast succeeds
104 */
105 public void testOfferLast() {
106 LinkedBlockingDeque q = new LinkedBlockingDeque();
107 assertTrue(q.offerLast(new Integer(0)));
108 assertTrue(q.offerLast(new Integer(1)));
109 }
110
111 /**
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, q.pollFirst());
118 }
119 assertNull(q.pollFirst());
120 }
121
122 /**
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, q.pollLast());
129 }
130 assertNull(q.pollLast());
131 }
132
133 /**
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, q.peekFirst());
140 assertEquals(i, q.pollFirst());
141 assertTrue(q.peekFirst() == null ||
142 !q.peekFirst().equals(i));
143 }
144 assertNull(q.peekFirst());
145 }
146
147 /**
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, q.peek());
154 assertEquals(i, q.pollFirst());
155 assertTrue(q.peek() == null ||
156 !q.peek().equals(i));
157 }
158 assertNull(q.peek());
159 }
160
161 /**
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, q.peekLast());
168 assertEquals(i, q.pollLast());
169 assertTrue(q.peekLast() == null ||
170 !q.peekLast().equals(i));
171 }
172 assertNull(q.peekLast());
173 }
174
175 /**
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, 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 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, q.getLast());
198 assertEquals(i, q.pollLast());
199 }
200 try {
201 q.getLast();
202 shouldThrow();
203 } catch (NoSuchElementException success) {}
204 assertNull(q.peekLast());
205 }
206
207 /**
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, 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
239 */
240 public void testRemove() {
241 LinkedBlockingDeque q = populatedDeque(SIZE);
242 for (int i = 0; i < SIZE; ++i) {
243 assertEquals(i, q.remove());
244 }
245 try {
246 q.remove();
247 shouldThrow();
248 } catch (NoSuchElementException success) {}
249 }
250
251 /**
252 * removeFirstOccurrence(x) removes x and returns true if present
253 */
254 public void testRemoveFirstOccurrence() {
255 LinkedBlockingDeque q = populatedDeque(SIZE);
256 for (int i = 1; i < SIZE; i+=2) {
257 assertTrue(q.removeFirstOccurrence(new Integer(i)));
258 }
259 for (int i = 0; i < SIZE; i+=2) {
260 assertTrue(q.removeFirstOccurrence(new Integer(i)));
261 assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
262 }
263 assertTrue(q.isEmpty());
264 }
265
266 /**
267 * removeLastOccurrence(x) removes x and returns true if present
268 */
269 public void testRemoveLastOccurrence() {
270 LinkedBlockingDeque q = populatedDeque(SIZE);
271 for (int i = 1; i < SIZE; i+=2) {
272 assertTrue(q.removeLastOccurrence(new Integer(i)));
273 }
274 for (int i = 0; i < SIZE; i+=2) {
275 assertTrue(q.removeLastOccurrence(new Integer(i)));
276 assertFalse(q.removeLastOccurrence(new Integer(i+1)));
277 }
278 assertTrue(q.isEmpty());
279 }
280
281 /**
282 * peekFirst returns element inserted with addFirst
283 */
284 public void testAddFirst() {
285 LinkedBlockingDeque q = populatedDeque(3);
286 q.pollLast();
287 q.addFirst(four);
288 assertSame(four, q.peekFirst());
289 }
290
291 /**
292 * peekLast returns element inserted with addLast
293 */
294 public void testAddLast() {
295 LinkedBlockingDeque q = populatedDeque(3);
296 q.pollLast();
297 q.addLast(four);
298 assertSame(four, q.peekLast());
299 }
300
301 /**
302 * A new deque has the indicated capacity, or Integer.MAX_VALUE if
303 * none given
304 */
305 public void testConstructor1() {
306 assertEquals(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity());
307 assertEquals(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity());
308 }
309
310 /**
311 * Constructor throws IAE if capacity argument nonpositive
312 */
313 public void testConstructor2() {
314 try {
315 LinkedBlockingDeque q = new LinkedBlockingDeque(0);
316 shouldThrow();
317 } catch (IllegalArgumentException success) {}
318 }
319
320 /**
321 * Initializing from null Collection throws NPE
322 */
323 public void testConstructor3() {
324 try {
325 LinkedBlockingDeque q = new LinkedBlockingDeque(null);
326 shouldThrow();
327 } catch (NullPointerException success) {}
328 }
329
330 /**
331 * Initializing from Collection of null elements throws NPE
332 */
333 public void testConstructor4() {
334 try {
335 Integer[] ints = new Integer[SIZE];
336 LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
337 shouldThrow();
338 } catch (NullPointerException success) {}
339 }
340
341 /**
342 * Initializing from Collection with some null elements throws NPE
343 */
344 public void testConstructor5() {
345 try {
346 Integer[] ints = new Integer[SIZE];
347 for (int i = 0; i < SIZE-1; ++i)
348 ints[i] = new Integer(i);
349 LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
350 shouldThrow();
351 } catch (NullPointerException success) {}
352 }
353
354 /**
355 * Deque contains all elements of collection used to initialize
356 */
357 public void testConstructor6() {
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 /**
367 * Deque transitions from empty to full when elements added
368 */
369 public void testEmptyFull() {
370 LinkedBlockingDeque q = new LinkedBlockingDeque(2);
371 assertTrue(q.isEmpty());
372 assertEquals("should have room for 2", 2, q.remainingCapacity());
373 q.add(one);
374 assertFalse(q.isEmpty());
375 q.add(two);
376 assertFalse(q.isEmpty());
377 assertEquals(0, q.remainingCapacity());
378 assertFalse(q.offer(three));
379 }
380
381 /**
382 * remainingCapacity decreases on add, increases on remove
383 */
384 public void testRemainingCapacity() {
385 LinkedBlockingDeque q = populatedDeque(SIZE);
386 for (int i = 0; i < SIZE; ++i) {
387 assertEquals(i, q.remainingCapacity());
388 assertEquals(SIZE-i, q.size());
389 q.remove();
390 }
391 for (int i = 0; i < SIZE; ++i) {
392 assertEquals(SIZE-i, q.remainingCapacity());
393 assertEquals(i, q.size());
394 q.add(new Integer(i));
395 }
396 }
397
398 /**
399 * offer(null) throws NPE
400 */
401 public void testOfferNull() {
402 try {
403 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
404 q.offer(null);
405 shouldThrow();
406 } catch (NullPointerException success) {}
407 }
408
409 /**
410 * add(null) throws NPE
411 */
412 public void testAddNull() {
413 try {
414 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
415 q.add(null);
416 shouldThrow();
417 } catch (NullPointerException success) {}
418 }
419
420 /**
421 * push(null) throws NPE
422 */
423 public void testPushNull() {
424 try {
425 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
426 q.push(null);
427 shouldThrow();
428 } catch (NullPointerException success) {}
429 }
430
431 /**
432 * push succeeds if not full; throws ISE if full
433 */
434 public void testPush() {
435 try {
436 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
437 for (int i = 0; i < SIZE; ++i) {
438 Integer I = new Integer(i);
439 q.push(I);
440 assertEquals(I, q.peek());
441 }
442 assertEquals(0, q.remainingCapacity());
443 q.push(new Integer(SIZE));
444 shouldThrow();
445 } catch (IllegalStateException success) {}
446 }
447
448 /**
449 * peekFirst returns element inserted with push
450 */
451 public void testPushWithPeek() {
452 LinkedBlockingDeque q = populatedDeque(3);
453 q.pollLast();
454 q.push(four);
455 assertSame(four, q.peekFirst());
456 }
457
458 /**
459 * pop removes next element, or throws NSEE if empty
460 */
461 public void testPop() {
462 LinkedBlockingDeque q = populatedDeque(SIZE);
463 for (int i = 0; i < SIZE; ++i) {
464 assertEquals(i, q.pop());
465 }
466 try {
467 q.pop();
468 shouldThrow();
469 } catch (NoSuchElementException success) {}
470 }
471
472 /**
473 * Offer succeeds if not full; fails if full
474 */
475 public void testOffer() {
476 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
477 assertTrue(q.offer(zero));
478 assertFalse(q.offer(one));
479 }
480
481 /**
482 * add succeeds if not full; throws ISE if full
483 */
484 public void testAdd() {
485 try {
486 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
487 for (int i = 0; i < SIZE; ++i) {
488 assertTrue(q.add(new Integer(i)));
489 }
490 assertEquals(0, q.remainingCapacity());
491 q.add(new Integer(SIZE));
492 shouldThrow();
493 } catch (IllegalStateException success) {}
494 }
495
496 /**
497 * addAll(null) throws NPE
498 */
499 public void testAddAll1() {
500 try {
501 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
502 q.addAll(null);
503 shouldThrow();
504 } catch (NullPointerException success) {}
505 }
506
507 /**
508 * addAll(this) throws IAE
509 */
510 public void testAddAllSelf() {
511 try {
512 LinkedBlockingDeque q = populatedDeque(SIZE);
513 q.addAll(q);
514 shouldThrow();
515 } catch (IllegalArgumentException success) {}
516 }
517
518 /**
519 * addAll of a collection with null elements throws NPE
520 */
521 public void testAddAll2() {
522 try {
523 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
524 Integer[] ints = new Integer[SIZE];
525 q.addAll(Arrays.asList(ints));
526 shouldThrow();
527 } catch (NullPointerException success) {}
528 }
529
530 /**
531 * addAll of a collection with any null elements throws NPE after
532 * possibly adding some elements
533 */
534 public void testAddAll3() {
535 try {
536 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
537 Integer[] ints = new Integer[SIZE];
538 for (int i = 0; i < SIZE-1; ++i)
539 ints[i] = new Integer(i);
540 q.addAll(Arrays.asList(ints));
541 shouldThrow();
542 } catch (NullPointerException success) {}
543 }
544
545 /**
546 * addAll throws ISE if not enough room
547 */
548 public void testAddAll4() {
549 try {
550 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
551 Integer[] ints = new Integer[SIZE];
552 for (int i = 0; i < SIZE; ++i)
553 ints[i] = new Integer(i);
554 q.addAll(Arrays.asList(ints));
555 shouldThrow();
556 } catch (IllegalStateException success) {}
557 }
558
559 /**
560 * Deque contains all elements, in traversal order, of successful addAll
561 */
562 public void testAddAll5() {
563 Integer[] empty = new Integer[0];
564 Integer[] ints = new Integer[SIZE];
565 for (int i = 0; i < SIZE; ++i)
566 ints[i] = new Integer(i);
567 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
568 assertFalse(q.addAll(Arrays.asList(empty)));
569 assertTrue(q.addAll(Arrays.asList(ints)));
570 for (int i = 0; i < SIZE; ++i)
571 assertEquals(ints[i], q.poll());
572 }
573
574 /**
575 * put(null) throws NPE
576 */
577 public void testPutNull() throws InterruptedException {
578 try {
579 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
580 q.put(null);
581 shouldThrow();
582 } catch (NullPointerException success) {}
583 }
584
585 /**
586 * all elements successfully put are contained
587 */
588 public void testPut() throws InterruptedException {
589 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
590 for (int i = 0; i < SIZE; ++i) {
591 Integer I = new Integer(i);
592 q.put(I);
593 assertTrue(q.contains(I));
594 }
595 assertEquals(0, q.remainingCapacity());
596 }
597
598 /**
599 * put blocks interruptibly if full
600 */
601 public void testBlockingPut() throws InterruptedException {
602 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
603 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
604 Thread t = newStartedThread(new CheckedRunnable() {
605 public void realRun() throws InterruptedException {
606 for (int i = 0; i < SIZE; ++i)
607 q.put(i);
608 assertEquals(SIZE, q.size());
609 assertEquals(0, q.remainingCapacity());
610
611 Thread.currentThread().interrupt();
612 try {
613 q.put(99);
614 shouldThrow();
615 } catch (InterruptedException success) {}
616 assertFalse(Thread.interrupted());
617
618 pleaseInterrupt.countDown();
619 try {
620 q.put(99);
621 shouldThrow();
622 } catch (InterruptedException success) {}
623 assertFalse(Thread.interrupted());
624 }});
625
626 await(pleaseInterrupt);
627 assertThreadStaysAlive(t);
628 t.interrupt();
629 awaitTermination(t);
630 assertEquals(SIZE, q.size());
631 assertEquals(0, q.remainingCapacity());
632 }
633
634 /**
635 * put blocks interruptibly waiting for take when full
636 */
637 public void testPutWithTake() throws InterruptedException {
638 final int capacity = 2;
639 final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
640 final CountDownLatch pleaseTake = new CountDownLatch(1);
641 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
642 Thread t = newStartedThread(new CheckedRunnable() {
643 public void realRun() throws InterruptedException {
644 for (int i = 0; i < capacity; i++)
645 q.put(i);
646 pleaseTake.countDown();
647 q.put(86);
648
649 pleaseInterrupt.countDown();
650 try {
651 q.put(99);
652 shouldThrow();
653 } catch (InterruptedException success) {}
654 assertFalse(Thread.interrupted());
655 }});
656
657 await(pleaseTake);
658 assertEquals(q.remainingCapacity(), 0);
659 assertEquals(0, q.take());
660
661 await(pleaseInterrupt);
662 assertThreadStaysAlive(t);
663 t.interrupt();
664 awaitTermination(t);
665 assertEquals(q.remainingCapacity(), 0);
666 }
667
668 /**
669 * timed offer times out if full and elements not taken
670 */
671 public void testTimedOffer() throws InterruptedException {
672 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
673 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
674 Thread t = newStartedThread(new CheckedRunnable() {
675 public void realRun() throws InterruptedException {
676 q.put(new Object());
677 q.put(new Object());
678 long startTime = System.nanoTime();
679 assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
680 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
681 pleaseInterrupt.countDown();
682 try {
683 q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
684 shouldThrow();
685 } catch (InterruptedException success) {}
686 }});
687
688 await(pleaseInterrupt);
689 assertThreadStaysAlive(t);
690 t.interrupt();
691 awaitTermination(t);
692 }
693
694 /**
695 * take retrieves elements in FIFO order
696 */
697 public void testTake() throws InterruptedException {
698 LinkedBlockingDeque q = populatedDeque(SIZE);
699 for (int i = 0; i < SIZE; ++i) {
700 assertEquals(i, q.take());
701 }
702 }
703
704 /**
705 * take removes existing elements until empty, then blocks interruptibly
706 */
707 public void testBlockingTake() throws InterruptedException {
708 final LinkedBlockingDeque q = populatedDeque(SIZE);
709 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
710 Thread t = newStartedThread(new CheckedRunnable() {
711 public void realRun() throws InterruptedException {
712 for (int i = 0; i < SIZE; ++i) {
713 assertEquals(i, q.take());
714 }
715
716 Thread.currentThread().interrupt();
717 try {
718 q.take();
719 shouldThrow();
720 } catch (InterruptedException success) {}
721 assertFalse(Thread.interrupted());
722
723 pleaseInterrupt.countDown();
724 try {
725 q.take();
726 shouldThrow();
727 } catch (InterruptedException success) {}
728 assertFalse(Thread.interrupted());
729 }});
730
731 await(pleaseInterrupt);
732 assertThreadStaysAlive(t);
733 t.interrupt();
734 awaitTermination(t);
735 }
736
737 /**
738 * poll succeeds unless empty
739 */
740 public void testPoll() {
741 LinkedBlockingDeque q = populatedDeque(SIZE);
742 for (int i = 0; i < SIZE; ++i) {
743 assertEquals(i, q.poll());
744 }
745 assertNull(q.poll());
746 }
747
748 /**
749 * timed poll with zero timeout succeeds when non-empty, else times out
750 */
751 public void testTimedPoll0() throws InterruptedException {
752 LinkedBlockingDeque q = populatedDeque(SIZE);
753 for (int i = 0; i < SIZE; ++i) {
754 assertEquals(i, q.poll(0, MILLISECONDS));
755 }
756 assertNull(q.poll(0, MILLISECONDS));
757 }
758
759 /**
760 * timed poll with nonzero timeout succeeds when non-empty, else times out
761 */
762 public void testTimedPoll() throws InterruptedException {
763 LinkedBlockingDeque q = populatedDeque(SIZE);
764 for (int i = 0; i < SIZE; ++i) {
765 long startTime = System.nanoTime();
766 assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
767 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
768 }
769 long startTime = System.nanoTime();
770 assertNull(q.poll(timeoutMillis(), MILLISECONDS));
771 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
772 checkEmpty(q);
773 }
774
775 /**
776 * Interrupted timed poll throws InterruptedException instead of
777 * returning timeout status
778 */
779 public void testInterruptedTimedPoll() throws InterruptedException {
780 final BlockingQueue<Integer> q = populatedDeque(SIZE);
781 final CountDownLatch aboutToWait = new CountDownLatch(1);
782 Thread t = newStartedThread(new CheckedRunnable() {
783 public void realRun() throws InterruptedException {
784 for (int i = 0; i < SIZE; ++i) {
785 long t0 = System.nanoTime();
786 assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
787 assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
788 }
789 long t0 = System.nanoTime();
790 aboutToWait.countDown();
791 try {
792 q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
793 shouldThrow();
794 } catch (InterruptedException success) {
795 assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
796 }
797 }});
798
799 aboutToWait.await();
800 waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
801 t.interrupt();
802 awaitTermination(t, MEDIUM_DELAY_MS);
803 checkEmpty(q);
804 }
805
806 /**
807 * putFirst(null) throws NPE
808 */
809 public void testPutFirstNull() throws InterruptedException {
810 try {
811 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
812 q.putFirst(null);
813 shouldThrow();
814 } catch (NullPointerException success) {}
815 }
816
817 /**
818 * all elements successfully putFirst are contained
819 */
820 public void testPutFirst() throws InterruptedException {
821 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
822 for (int i = 0; i < SIZE; ++i) {
823 Integer I = new Integer(i);
824 q.putFirst(I);
825 assertTrue(q.contains(I));
826 }
827 assertEquals(0, q.remainingCapacity());
828 }
829
830 /**
831 * putFirst blocks interruptibly if full
832 */
833 public void testBlockingPutFirst() throws InterruptedException {
834 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
835 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
836 Thread t = newStartedThread(new CheckedRunnable() {
837 public void realRun() throws InterruptedException {
838 for (int i = 0; i < SIZE; ++i)
839 q.putFirst(i);
840 assertEquals(SIZE, q.size());
841 assertEquals(0, q.remainingCapacity());
842
843 Thread.currentThread().interrupt();
844 try {
845 q.putFirst(99);
846 shouldThrow();
847 } catch (InterruptedException success) {}
848 assertFalse(Thread.interrupted());
849
850 pleaseInterrupt.countDown();
851 try {
852 q.putFirst(99);
853 shouldThrow();
854 } catch (InterruptedException success) {}
855 assertFalse(Thread.interrupted());
856 }});
857
858 await(pleaseInterrupt);
859 assertThreadStaysAlive(t);
860 t.interrupt();
861 awaitTermination(t);
862 assertEquals(SIZE, q.size());
863 assertEquals(0, q.remainingCapacity());
864 }
865
866 /**
867 * putFirst blocks interruptibly waiting for take when full
868 */
869 public void testPutFirstWithTake() throws InterruptedException {
870 final int capacity = 2;
871 final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
872 final CountDownLatch pleaseTake = new CountDownLatch(1);
873 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
874 Thread t = newStartedThread(new CheckedRunnable() {
875 public void realRun() throws InterruptedException {
876 for (int i = 0; i < capacity; i++)
877 q.putFirst(i);
878 pleaseTake.countDown();
879 q.putFirst(86);
880
881 pleaseInterrupt.countDown();
882 try {
883 q.putFirst(99);
884 shouldThrow();
885 } catch (InterruptedException success) {}
886 assertFalse(Thread.interrupted());
887 }});
888
889 await(pleaseTake);
890 assertEquals(q.remainingCapacity(), 0);
891 assertEquals(capacity - 1, q.take());
892
893 await(pleaseInterrupt);
894 assertThreadStaysAlive(t);
895 t.interrupt();
896 awaitTermination(t);
897 assertEquals(q.remainingCapacity(), 0);
898 }
899
900 /**
901 * timed offerFirst times out if full and elements not taken
902 */
903 public void testTimedOfferFirst() throws InterruptedException {
904 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
905 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
906 Thread t = newStartedThread(new CheckedRunnable() {
907 public void realRun() throws InterruptedException {
908 q.putFirst(new Object());
909 q.putFirst(new Object());
910 long startTime = System.nanoTime();
911 assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
912 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
913 pleaseInterrupt.countDown();
914 try {
915 q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
916 shouldThrow();
917 } catch (InterruptedException success) {}
918 }});
919
920 await(pleaseInterrupt);
921 assertThreadStaysAlive(t);
922 t.interrupt();
923 awaitTermination(t);
924 }
925
926 /**
927 * take retrieves elements in FIFO order
928 */
929 public void testTakeFirst() throws InterruptedException {
930 LinkedBlockingDeque q = populatedDeque(SIZE);
931 for (int i = 0; i < SIZE; ++i) {
932 assertEquals(i, q.takeFirst());
933 }
934 }
935
936 /**
937 * takeFirst() blocks interruptibly when empty
938 */
939 public void testTakeFirstFromEmptyBlocksInterruptibly() {
940 final BlockingDeque q = new LinkedBlockingDeque();
941 final CountDownLatch threadStarted = new CountDownLatch(1);
942 Thread t = newStartedThread(new CheckedRunnable() {
943 public void realRun() {
944 threadStarted.countDown();
945 try {
946 q.takeFirst();
947 shouldThrow();
948 } catch (InterruptedException success) {}
949 assertFalse(Thread.interrupted());
950 }});
951
952 await(threadStarted);
953 assertThreadStaysAlive(t);
954 t.interrupt();
955 awaitTermination(t);
956 }
957
958 /**
959 * takeFirst() throws InterruptedException immediately if interrupted
960 * before waiting
961 */
962 public void testTakeFirstFromEmptyAfterInterrupt() {
963 final BlockingDeque q = new LinkedBlockingDeque();
964 Thread t = newStartedThread(new CheckedRunnable() {
965 public void realRun() {
966 Thread.currentThread().interrupt();
967 try {
968 q.takeFirst();
969 shouldThrow();
970 } catch (InterruptedException success) {}
971 assertFalse(Thread.interrupted());
972 }});
973
974 awaitTermination(t);
975 }
976
977 /**
978 * takeLast() blocks interruptibly when empty
979 */
980 public void testTakeLastFromEmptyBlocksInterruptibly() {
981 final BlockingDeque q = new LinkedBlockingDeque();
982 final CountDownLatch threadStarted = new CountDownLatch(1);
983 Thread t = newStartedThread(new CheckedRunnable() {
984 public void realRun() {
985 threadStarted.countDown();
986 try {
987 q.takeLast();
988 shouldThrow();
989 } catch (InterruptedException success) {}
990 assertFalse(Thread.interrupted());
991 }});
992
993 await(threadStarted);
994 assertThreadStaysAlive(t);
995 t.interrupt();
996 awaitTermination(t);
997 }
998
999 /**
1000 * takeLast() throws InterruptedException immediately if interrupted
1001 * before waiting
1002 */
1003 public void testTakeLastFromEmptyAfterInterrupt() {
1004 final BlockingDeque q = new LinkedBlockingDeque();
1005 Thread t = newStartedThread(new CheckedRunnable() {
1006 public void realRun() {
1007 Thread.currentThread().interrupt();
1008 try {
1009 q.takeLast();
1010 shouldThrow();
1011 } catch (InterruptedException success) {}
1012 assertFalse(Thread.interrupted());
1013 }});
1014
1015 awaitTermination(t);
1016 }
1017
1018 /**
1019 * takeFirst removes existing elements until empty, then blocks interruptibly
1020 */
1021 public void testBlockingTakeFirst() throws InterruptedException {
1022 final LinkedBlockingDeque q = populatedDeque(SIZE);
1023 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1024 Thread t = newStartedThread(new CheckedRunnable() {
1025 public void realRun() throws InterruptedException {
1026 for (int i = 0; i < SIZE; ++i) {
1027 assertEquals(i, q.takeFirst());
1028 }
1029
1030 Thread.currentThread().interrupt();
1031 try {
1032 q.takeFirst();
1033 shouldThrow();
1034 } catch (InterruptedException success) {}
1035 assertFalse(Thread.interrupted());
1036
1037 pleaseInterrupt.countDown();
1038 try {
1039 q.takeFirst();
1040 shouldThrow();
1041 } catch (InterruptedException success) {}
1042 assertFalse(Thread.interrupted());
1043 }});
1044
1045 await(pleaseInterrupt);
1046 assertThreadStaysAlive(t);
1047 t.interrupt();
1048 awaitTermination(t);
1049 }
1050
1051 /**
1052 * timed pollFirst with zero timeout succeeds when non-empty, else times out
1053 */
1054 public void testTimedPollFirst0() throws InterruptedException {
1055 LinkedBlockingDeque q = populatedDeque(SIZE);
1056 for (int i = 0; i < SIZE; ++i) {
1057 assertEquals(i, q.pollFirst(0, MILLISECONDS));
1058 }
1059 assertNull(q.pollFirst(0, MILLISECONDS));
1060 }
1061
1062 /**
1063 * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
1064 */
1065 public void testTimedPollFirst() throws InterruptedException {
1066 LinkedBlockingDeque q = populatedDeque(SIZE);
1067 for (int i = 0; i < SIZE; ++i) {
1068 long startTime = System.nanoTime();
1069 assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1070 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1071 }
1072 long startTime = System.nanoTime();
1073 assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1074 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1075 checkEmpty(q);
1076 }
1077
1078 /**
1079 * Interrupted timed pollFirst throws InterruptedException instead of
1080 * returning timeout status
1081 */
1082 public void testInterruptedTimedPollFirst() throws InterruptedException {
1083 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1084 Thread t = newStartedThread(new CheckedRunnable() {
1085 public void realRun() throws InterruptedException {
1086 LinkedBlockingDeque q = populatedDeque(SIZE);
1087 for (int i = 0; i < SIZE; ++i) {
1088 assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1089 }
1090
1091 Thread.currentThread().interrupt();
1092 try {
1093 q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1094 shouldThrow();
1095 } catch (InterruptedException success) {}
1096 assertFalse(Thread.interrupted());
1097
1098 pleaseInterrupt.countDown();
1099 try {
1100 q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1101 shouldThrow();
1102 } catch (InterruptedException success) {}
1103 assertFalse(Thread.interrupted());
1104 }});
1105
1106 await(pleaseInterrupt);
1107 assertThreadStaysAlive(t);
1108 t.interrupt();
1109 awaitTermination(t);
1110 }
1111
1112 /**
1113 * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
1114 * on interruption throws
1115 */
1116 public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
1117 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1118 final CheckedBarrier barrier = new CheckedBarrier(2);
1119 Thread t = newStartedThread(new CheckedRunnable() {
1120 public void realRun() throws InterruptedException {
1121 long startTime = System.nanoTime();
1122 assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1123 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1124
1125 barrier.await();
1126
1127 assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1128
1129 Thread.currentThread().interrupt();
1130 try {
1131 q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1132 shouldThrow();
1133 } catch (InterruptedException success) {}
1134
1135 barrier.await();
1136 try {
1137 q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1138 shouldThrow();
1139 } catch (InterruptedException success) {}
1140 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1141 }});
1142
1143 barrier.await();
1144 long startTime = System.nanoTime();
1145 assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1146 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1147 barrier.await();
1148 assertThreadStaysAlive(t);
1149 t.interrupt();
1150 awaitTermination(t);
1151 }
1152
1153 /**
1154 * putLast(null) throws NPE
1155 */
1156 public void testPutLastNull() throws InterruptedException {
1157 try {
1158 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1159 q.putLast(null);
1160 shouldThrow();
1161 } catch (NullPointerException success) {}
1162 }
1163
1164 /**
1165 * all elements successfully putLast are contained
1166 */
1167 public void testPutLast() throws InterruptedException {
1168 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1169 for (int i = 0; i < SIZE; ++i) {
1170 Integer I = new Integer(i);
1171 q.putLast(I);
1172 assertTrue(q.contains(I));
1173 }
1174 assertEquals(0, q.remainingCapacity());
1175 }
1176
1177 /**
1178 * putLast blocks interruptibly if full
1179 */
1180 public void testBlockingPutLast() throws InterruptedException {
1181 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1182 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1183 Thread t = newStartedThread(new CheckedRunnable() {
1184 public void realRun() throws InterruptedException {
1185 for (int i = 0; i < SIZE; ++i)
1186 q.putLast(i);
1187 assertEquals(SIZE, q.size());
1188 assertEquals(0, q.remainingCapacity());
1189
1190 Thread.currentThread().interrupt();
1191 try {
1192 q.putLast(99);
1193 shouldThrow();
1194 } catch (InterruptedException success) {}
1195 assertFalse(Thread.interrupted());
1196
1197 pleaseInterrupt.countDown();
1198 try {
1199 q.putLast(99);
1200 shouldThrow();
1201 } catch (InterruptedException success) {}
1202 assertFalse(Thread.interrupted());
1203 }});
1204
1205 await(pleaseInterrupt);
1206 assertThreadStaysAlive(t);
1207 t.interrupt();
1208 awaitTermination(t);
1209 assertEquals(SIZE, q.size());
1210 assertEquals(0, q.remainingCapacity());
1211 }
1212
1213 /**
1214 * putLast blocks interruptibly waiting for take when full
1215 */
1216 public void testPutLastWithTake() throws InterruptedException {
1217 final int capacity = 2;
1218 final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1219 final CountDownLatch pleaseTake = new CountDownLatch(1);
1220 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1221 Thread t = newStartedThread(new CheckedRunnable() {
1222 public void realRun() throws InterruptedException {
1223 for (int i = 0; i < capacity; i++)
1224 q.putLast(i);
1225 pleaseTake.countDown();
1226 q.putLast(86);
1227
1228 pleaseInterrupt.countDown();
1229 try {
1230 q.putLast(99);
1231 shouldThrow();
1232 } catch (InterruptedException success) {}
1233 assertFalse(Thread.interrupted());
1234 }});
1235
1236 await(pleaseTake);
1237 assertEquals(q.remainingCapacity(), 0);
1238 assertEquals(0, q.take());
1239
1240 await(pleaseInterrupt);
1241 assertThreadStaysAlive(t);
1242 t.interrupt();
1243 awaitTermination(t);
1244 assertEquals(q.remainingCapacity(), 0);
1245 }
1246
1247 /**
1248 * timed offerLast times out if full and elements not taken
1249 */
1250 public void testTimedOfferLast() throws InterruptedException {
1251 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1252 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1253 Thread t = newStartedThread(new CheckedRunnable() {
1254 public void realRun() throws InterruptedException {
1255 q.putLast(new Object());
1256 q.putLast(new Object());
1257 long startTime = System.nanoTime();
1258 assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1259 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1260 pleaseInterrupt.countDown();
1261 try {
1262 q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1263 shouldThrow();
1264 } catch (InterruptedException success) {}
1265 }});
1266
1267 await(pleaseInterrupt);
1268 assertThreadStaysAlive(t);
1269 t.interrupt();
1270 awaitTermination(t);
1271 }
1272
1273 /**
1274 * takeLast retrieves elements in FIFO order
1275 */
1276 public void testTakeLast() throws InterruptedException {
1277 LinkedBlockingDeque q = populatedDeque(SIZE);
1278 for (int i = 0; i < SIZE; ++i) {
1279 assertEquals(SIZE-i-1, q.takeLast());
1280 }
1281 }
1282
1283 /**
1284 * takeLast removes existing elements until empty, then blocks interruptibly
1285 */
1286 public void testBlockingTakeLast() throws InterruptedException {
1287 final LinkedBlockingDeque q = populatedDeque(SIZE);
1288 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1289 Thread t = newStartedThread(new CheckedRunnable() {
1290 public void realRun() throws InterruptedException {
1291 for (int i = 0; i < SIZE; ++i) {
1292 assertEquals(SIZE-i-1, q.takeLast());
1293 }
1294
1295 Thread.currentThread().interrupt();
1296 try {
1297 q.takeLast();
1298 shouldThrow();
1299 } catch (InterruptedException success) {}
1300 assertFalse(Thread.interrupted());
1301
1302 pleaseInterrupt.countDown();
1303 try {
1304 q.takeLast();
1305 shouldThrow();
1306 } catch (InterruptedException success) {}
1307 assertFalse(Thread.interrupted());
1308 }});
1309
1310 await(pleaseInterrupt);
1311 assertThreadStaysAlive(t);
1312 t.interrupt();
1313 awaitTermination(t);
1314 }
1315
1316 /**
1317 * timed pollLast with zero timeout succeeds when non-empty, else times out
1318 */
1319 public void testTimedPollLast0() throws InterruptedException {
1320 LinkedBlockingDeque q = populatedDeque(SIZE);
1321 for (int i = 0; i < SIZE; ++i) {
1322 assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1323 }
1324 assertNull(q.pollLast(0, MILLISECONDS));
1325 }
1326
1327 /**
1328 * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1329 */
1330 public void testTimedPollLast() throws InterruptedException {
1331 LinkedBlockingDeque q = populatedDeque(SIZE);
1332 for (int i = 0; i < SIZE; ++i) {
1333 long startTime = System.nanoTime();
1334 assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1335 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1336 }
1337 long startTime = System.nanoTime();
1338 assertNull(q.pollLast(timeoutMillis(), MILLISECONDS));
1339 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1340 checkEmpty(q);
1341 }
1342
1343 /**
1344 * Interrupted timed pollLast throws InterruptedException instead of
1345 * returning timeout status
1346 */
1347 public void testInterruptedTimedPollLast() throws InterruptedException {
1348 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1349 Thread t = newStartedThread(new CheckedRunnable() {
1350 public void realRun() throws InterruptedException {
1351 LinkedBlockingDeque q = populatedDeque(SIZE);
1352 for (int i = 0; i < SIZE; ++i) {
1353 assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1354 }
1355
1356 Thread.currentThread().interrupt();
1357 try {
1358 q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1359 shouldThrow();
1360 } catch (InterruptedException success) {}
1361 assertFalse(Thread.interrupted());
1362
1363 pleaseInterrupt.countDown();
1364 try {
1365 q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1366 shouldThrow();
1367 } catch (InterruptedException success) {}
1368 assertFalse(Thread.interrupted());
1369 }});
1370
1371 await(pleaseInterrupt);
1372 assertThreadStaysAlive(t);
1373 t.interrupt();
1374 awaitTermination(t);
1375 }
1376
1377 /**
1378 * timed poll before a delayed offerLast fails; after offerLast succeeds;
1379 * on interruption throws
1380 */
1381 public void testTimedPollWithOfferLast() throws InterruptedException {
1382 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1383 final CheckedBarrier barrier = new CheckedBarrier(2);
1384 Thread t = newStartedThread(new CheckedRunnable() {
1385 public void realRun() throws InterruptedException {
1386 long startTime = System.nanoTime();
1387 assertNull(q.poll(timeoutMillis(), MILLISECONDS));
1388 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1389
1390 barrier.await();
1391
1392 assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1393
1394 Thread.currentThread().interrupt();
1395 try {
1396 q.poll(LONG_DELAY_MS, MILLISECONDS);
1397 shouldThrow();
1398 } catch (InterruptedException success) {}
1399 assertFalse(Thread.interrupted());
1400
1401 barrier.await();
1402 try {
1403 q.poll(LONG_DELAY_MS, MILLISECONDS);
1404 shouldThrow();
1405 } catch (InterruptedException success) {}
1406 assertFalse(Thread.interrupted());
1407 }});
1408
1409 barrier.await();
1410 long startTime = System.nanoTime();
1411 assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS));
1412 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1413
1414 barrier.await();
1415 assertThreadStaysAlive(t);
1416 t.interrupt();
1417 awaitTermination(t);
1418 }
1419
1420 /**
1421 * element returns next element, or throws NSEE if empty
1422 */
1423 public void testElement() {
1424 LinkedBlockingDeque q = populatedDeque(SIZE);
1425 for (int i = 0; i < SIZE; ++i) {
1426 assertEquals(i, q.element());
1427 q.poll();
1428 }
1429 try {
1430 q.element();
1431 shouldThrow();
1432 } catch (NoSuchElementException success) {}
1433 }
1434
1435 /**
1436 * remove(x) removes x and returns true if present
1437 */
1438 public void testRemoveElement() {
1439 LinkedBlockingDeque q = populatedDeque(SIZE);
1440 for (int i = 1; i < SIZE; i+=2) {
1441 assertTrue(q.contains(i));
1442 assertTrue(q.remove(i));
1443 assertFalse(q.contains(i));
1444 assertTrue(q.contains(i-1));
1445 }
1446 for (int i = 0; i < SIZE; i+=2) {
1447 assertTrue(q.contains(i));
1448 assertTrue(q.remove(i));
1449 assertFalse(q.contains(i));
1450 assertFalse(q.remove(i+1));
1451 assertFalse(q.contains(i+1));
1452 }
1453 assertTrue(q.isEmpty());
1454 }
1455
1456 /**
1457 * contains(x) reports true when elements added but not yet removed
1458 */
1459 public void testContains() {
1460 LinkedBlockingDeque q = populatedDeque(SIZE);
1461 for (int i = 0; i < SIZE; ++i) {
1462 assertTrue(q.contains(new Integer(i)));
1463 q.poll();
1464 assertFalse(q.contains(new Integer(i)));
1465 }
1466 }
1467
1468 /**
1469 * clear removes all elements
1470 */
1471 public void testClear() {
1472 LinkedBlockingDeque q = populatedDeque(SIZE);
1473 q.clear();
1474 assertTrue(q.isEmpty());
1475 assertEquals(0, q.size());
1476 assertEquals(SIZE, q.remainingCapacity());
1477 q.add(one);
1478 assertFalse(q.isEmpty());
1479 assertTrue(q.contains(one));
1480 q.clear();
1481 assertTrue(q.isEmpty());
1482 }
1483
1484 /**
1485 * containsAll(c) is true when c contains a subset of elements
1486 */
1487 public void testContainsAll() {
1488 LinkedBlockingDeque q = populatedDeque(SIZE);
1489 LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE);
1490 for (int i = 0; i < SIZE; ++i) {
1491 assertTrue(q.containsAll(p));
1492 assertFalse(p.containsAll(q));
1493 p.add(new Integer(i));
1494 }
1495 assertTrue(p.containsAll(q));
1496 }
1497
1498 /**
1499 * retainAll(c) retains only those elements of c and reports true if changed
1500 */
1501 public void testRetainAll() {
1502 LinkedBlockingDeque q = populatedDeque(SIZE);
1503 LinkedBlockingDeque p = populatedDeque(SIZE);
1504 for (int i = 0; i < SIZE; ++i) {
1505 boolean changed = q.retainAll(p);
1506 if (i == 0)
1507 assertFalse(changed);
1508 else
1509 assertTrue(changed);
1510
1511 assertTrue(q.containsAll(p));
1512 assertEquals(SIZE-i, q.size());
1513 p.remove();
1514 }
1515 }
1516
1517 /**
1518 * removeAll(c) removes only those elements of c and reports true if changed
1519 */
1520 public void testRemoveAll() {
1521 for (int i = 1; i < SIZE; ++i) {
1522 LinkedBlockingDeque q = populatedDeque(SIZE);
1523 LinkedBlockingDeque p = populatedDeque(i);
1524 assertTrue(q.removeAll(p));
1525 assertEquals(SIZE-i, q.size());
1526 for (int j = 0; j < i; ++j) {
1527 Integer I = (Integer)(p.remove());
1528 assertFalse(q.contains(I));
1529 }
1530 }
1531 }
1532
1533 /**
1534 * toArray contains all elements in FIFO order
1535 */
1536 public void testToArray() throws InterruptedException{
1537 LinkedBlockingDeque q = populatedDeque(SIZE);
1538 Object[] o = q.toArray();
1539 for (int i = 0; i < o.length; i++)
1540 assertSame(o[i], q.poll());
1541 }
1542
1543 /**
1544 * toArray(a) contains all elements in FIFO order
1545 */
1546 public void testToArray2() {
1547 LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1548 Integer[] ints = new Integer[SIZE];
1549 Integer[] array = q.toArray(ints);
1550 assertSame(ints, array);
1551 for (int i = 0; i < ints.length; i++)
1552 assertSame(ints[i], q.remove());
1553 }
1554
1555 /**
1556 * toArray(null) throws NullPointerException
1557 */
1558 public void testToArray_NullArg() {
1559 LinkedBlockingDeque q = populatedDeque(SIZE);
1560 try {
1561 q.toArray(null);
1562 shouldThrow();
1563 } catch (NullPointerException success) {}
1564 }
1565
1566 /**
1567 * toArray(incompatible array type) throws ArrayStoreException
1568 */
1569 public void testToArray1_BadArg() {
1570 LinkedBlockingDeque q = populatedDeque(SIZE);
1571 try {
1572 q.toArray(new String[10]);
1573 shouldThrow();
1574 } catch (ArrayStoreException success) {}
1575 }
1576
1577 /**
1578 * iterator iterates through all elements
1579 */
1580 public void testIterator() throws InterruptedException {
1581 LinkedBlockingDeque q = populatedDeque(SIZE);
1582 Iterator it = q.iterator();
1583 while (it.hasNext()) {
1584 assertEquals(it.next(), q.take());
1585 }
1586 }
1587
1588 /**
1589 * iterator.remove removes current element
1590 */
1591 public void testIteratorRemove() {
1592 final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1593 q.add(two);
1594 q.add(one);
1595 q.add(three);
1596
1597 Iterator it = q.iterator();
1598 it.next();
1599 it.remove();
1600
1601 it = q.iterator();
1602 assertSame(it.next(), one);
1603 assertSame(it.next(), three);
1604 assertFalse(it.hasNext());
1605 }
1606
1607 /**
1608 * iterator ordering is FIFO
1609 */
1610 public void testIteratorOrdering() {
1611 final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1612 q.add(one);
1613 q.add(two);
1614 q.add(three);
1615 assertEquals(0, q.remainingCapacity());
1616 int k = 0;
1617 for (Iterator it = q.iterator(); it.hasNext();) {
1618 assertEquals(++k, it.next());
1619 }
1620 assertEquals(3, k);
1621 }
1622
1623 /**
1624 * Modifications do not cause iterators to fail
1625 */
1626 public void testWeaklyConsistentIteration() {
1627 final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1628 q.add(one);
1629 q.add(two);
1630 q.add(three);
1631 for (Iterator it = q.iterator(); it.hasNext();) {
1632 q.remove();
1633 it.next();
1634 }
1635 assertEquals(0, q.size());
1636 }
1637
1638 /**
1639 * Descending iterator iterates through all elements
1640 */
1641 public void testDescendingIterator() {
1642 LinkedBlockingDeque q = populatedDeque(SIZE);
1643 int i = 0;
1644 Iterator it = q.descendingIterator();
1645 while (it.hasNext()) {
1646 assertTrue(q.contains(it.next()));
1647 ++i;
1648 }
1649 assertEquals(i, SIZE);
1650 assertFalse(it.hasNext());
1651 try {
1652 it.next();
1653 shouldThrow();
1654 } catch (NoSuchElementException success) {}
1655 }
1656
1657 /**
1658 * Descending iterator ordering is reverse FIFO
1659 */
1660 public void testDescendingIteratorOrdering() {
1661 final LinkedBlockingDeque q = new LinkedBlockingDeque();
1662 for (int iters = 0; iters < 100; ++iters) {
1663 q.add(new Integer(3));
1664 q.add(new Integer(2));
1665 q.add(new Integer(1));
1666 int k = 0;
1667 for (Iterator it = q.descendingIterator(); it.hasNext();) {
1668 assertEquals(++k, it.next());
1669 }
1670
1671 assertEquals(3, k);
1672 q.remove();
1673 q.remove();
1674 q.remove();
1675 }
1676 }
1677
1678 /**
1679 * descendingIterator.remove removes current element
1680 */
1681 public void testDescendingIteratorRemove() {
1682 final LinkedBlockingDeque q = new LinkedBlockingDeque();
1683 for (int iters = 0; iters < 100; ++iters) {
1684 q.add(new Integer(3));
1685 q.add(new Integer(2));
1686 q.add(new Integer(1));
1687 Iterator it = q.descendingIterator();
1688 assertEquals(it.next(), new Integer(1));
1689 it.remove();
1690 assertEquals(it.next(), new Integer(2));
1691 it = q.descendingIterator();
1692 assertEquals(it.next(), new Integer(2));
1693 assertEquals(it.next(), new Integer(3));
1694 it.remove();
1695 assertFalse(it.hasNext());
1696 q.remove();
1697 }
1698 }
1699
1700 /**
1701 * toString contains toStrings of elements
1702 */
1703 public void testToString() {
1704 LinkedBlockingDeque q = populatedDeque(SIZE);
1705 String s = q.toString();
1706 for (int i = 0; i < SIZE; ++i) {
1707 assertTrue(s.contains(String.valueOf(i)));
1708 }
1709 }
1710
1711 /**
1712 * offer transfers elements across Executor tasks
1713 */
1714 public void testOfferInExecutor() {
1715 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1716 q.add(one);
1717 q.add(two);
1718 ExecutorService executor = Executors.newFixedThreadPool(2);
1719 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1720 executor.execute(new CheckedRunnable() {
1721 public void realRun() throws InterruptedException {
1722 assertFalse(q.offer(three));
1723 threadsStarted.await();
1724 assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1725 assertEquals(0, q.remainingCapacity());
1726 }});
1727
1728 executor.execute(new CheckedRunnable() {
1729 public void realRun() throws InterruptedException {
1730 threadsStarted.await();
1731 assertSame(one, q.take());
1732 }});
1733
1734 joinPool(executor);
1735 }
1736
1737 /**
1738 * timed poll retrieves elements across Executor threads
1739 */
1740 public void testPollInExecutor() {
1741 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1742 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1743 ExecutorService executor = Executors.newFixedThreadPool(2);
1744 executor.execute(new CheckedRunnable() {
1745 public void realRun() throws InterruptedException {
1746 assertNull(q.poll());
1747 threadsStarted.await();
1748 assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1749 checkEmpty(q);
1750 }});
1751
1752 executor.execute(new CheckedRunnable() {
1753 public void realRun() throws InterruptedException {
1754 threadsStarted.await();
1755 q.put(one);
1756 }});
1757
1758 joinPool(executor);
1759 }
1760
1761 /**
1762 * A deserialized serialized deque has same elements in same order
1763 */
1764 public void testSerialization() throws Exception {
1765 LinkedBlockingDeque q = populatedDeque(SIZE);
1766
1767 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1768 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1769 out.writeObject(q);
1770 out.close();
1771
1772 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1773 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1774 LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1775 assertEquals(q.size(), r.size());
1776 while (!q.isEmpty())
1777 assertEquals(q.remove(), r.remove());
1778 }
1779
1780 /**
1781 * drainTo(null) throws NPE
1782 */
1783 public void testDrainToNull() {
1784 LinkedBlockingDeque q = populatedDeque(SIZE);
1785 try {
1786 q.drainTo(null);
1787 shouldThrow();
1788 } catch (NullPointerException success) {}
1789 }
1790
1791 /**
1792 * drainTo(this) throws IAE
1793 */
1794 public void testDrainToSelf() {
1795 LinkedBlockingDeque q = populatedDeque(SIZE);
1796 try {
1797 q.drainTo(q);
1798 shouldThrow();
1799 } catch (IllegalArgumentException success) {}
1800 }
1801
1802 /**
1803 * drainTo(c) empties deque into another collection c
1804 */
1805 public void testDrainTo() {
1806 LinkedBlockingDeque q = populatedDeque(SIZE);
1807 ArrayList l = new ArrayList();
1808 q.drainTo(l);
1809 assertEquals(q.size(), 0);
1810 assertEquals(l.size(), SIZE);
1811 for (int i = 0; i < SIZE; ++i)
1812 assertEquals(l.get(i), new Integer(i));
1813 q.add(zero);
1814 q.add(one);
1815 assertFalse(q.isEmpty());
1816 assertTrue(q.contains(zero));
1817 assertTrue(q.contains(one));
1818 l.clear();
1819 q.drainTo(l);
1820 assertEquals(q.size(), 0);
1821 assertEquals(l.size(), 2);
1822 for (int i = 0; i < 2; ++i)
1823 assertEquals(l.get(i), new Integer(i));
1824 }
1825
1826 /**
1827 * drainTo empties full deque, unblocking a waiting put.
1828 */
1829 public void testDrainToWithActivePut() throws InterruptedException {
1830 final LinkedBlockingDeque q = populatedDeque(SIZE);
1831 Thread t = new Thread(new CheckedRunnable() {
1832 public void realRun() throws InterruptedException {
1833 q.put(new Integer(SIZE+1));
1834 }});
1835
1836 t.start();
1837 ArrayList l = new ArrayList();
1838 q.drainTo(l);
1839 assertTrue(l.size() >= SIZE);
1840 for (int i = 0; i < SIZE; ++i)
1841 assertEquals(l.get(i), new Integer(i));
1842 t.join();
1843 assertTrue(q.size() + l.size() >= SIZE);
1844 }
1845
1846 /**
1847 * drainTo(null, n) throws NPE
1848 */
1849 public void testDrainToNullN() {
1850 LinkedBlockingDeque q = populatedDeque(SIZE);
1851 try {
1852 q.drainTo(null, 0);
1853 shouldThrow();
1854 } catch (NullPointerException success) {}
1855 }
1856
1857 /**
1858 * drainTo(this, n) throws IAE
1859 */
1860 public void testDrainToSelfN() {
1861 LinkedBlockingDeque q = populatedDeque(SIZE);
1862 try {
1863 q.drainTo(q, 0);
1864 shouldThrow();
1865 } catch (IllegalArgumentException success) {}
1866 }
1867
1868 /**
1869 * drainTo(c, n) empties first min(n, size) elements of queue into c
1870 */
1871 public void testDrainToN() {
1872 LinkedBlockingDeque q = new LinkedBlockingDeque();
1873 for (int i = 0; i < SIZE + 2; ++i) {
1874 for (int j = 0; j < SIZE; j++)
1875 assertTrue(q.offer(new Integer(j)));
1876 ArrayList l = new ArrayList();
1877 q.drainTo(l, i);
1878 int k = (i < SIZE) ? i : SIZE;
1879 assertEquals(l.size(), k);
1880 assertEquals(q.size(), SIZE-k);
1881 for (int j = 0; j < k; ++j)
1882 assertEquals(l.get(j), new Integer(j));
1883 while (q.poll() != null) ;
1884 }
1885 }
1886
1887 }