ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.88
Committed: Tue Jan 26 13:33:06 2021 UTC (3 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.87: +319 -328 lines
Log Message:
Replace Integer with Item class

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