ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.38
Committed: Sat May 21 06:24:33 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.37: +27 -18 lines
Log Message:
various test improvements

File Contents

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