ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.65
Committed: Sun Oct 16 20:44:18 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.64: +3 -1 lines
Log Message:
improve populatedFoo methods

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