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