ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.22
Committed: Wed Aug 25 01:44:48 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.21: +6 -4 lines
Log Message:
whitespace

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/licenses/publicdomain
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 public static void main(String[] args) {
15 junit.textui.TestRunner.run(suite());
16 }
17
18 public static Test suite() {
19 return new TestSuite(LinkedBlockingDequeTest.class);
20 }
21
22 /**
23 * Create a deque of given size containing consecutive
24 * Integers 0 ... n.
25 */
26 private LinkedBlockingDeque populatedDeque(int n) {
27 LinkedBlockingDeque q = new LinkedBlockingDeque(n);
28 assertTrue(q.isEmpty());
29 for (int i = 0; i < n; i++)
30 assertTrue(q.offer(new Integer(i)));
31 assertFalse(q.isEmpty());
32 assertEquals(0, q.remainingCapacity());
33 assertEquals(n, q.size());
34 return q;
35 }
36
37 /**
38 * isEmpty is true before add, false after
39 */
40 public void testEmpty() {
41 LinkedBlockingDeque q = new LinkedBlockingDeque();
42 assertTrue(q.isEmpty());
43 q.add(new Integer(1));
44 assertFalse(q.isEmpty());
45 q.add(new Integer(2));
46 q.removeFirst();
47 q.removeFirst();
48 assertTrue(q.isEmpty());
49 }
50
51 /**
52 * size changes when elements added and removed
53 */
54 public void testSize() {
55 LinkedBlockingDeque q = populatedDeque(SIZE);
56 for (int i = 0; i < SIZE; ++i) {
57 assertEquals(SIZE-i, q.size());
58 q.removeFirst();
59 }
60 for (int i = 0; i < SIZE; ++i) {
61 assertEquals(i, q.size());
62 q.add(new Integer(i));
63 }
64 }
65
66 /**
67 * offer(null) throws NPE
68 */
69 public void testOfferFirstNull() {
70 try {
71 LinkedBlockingDeque q = new LinkedBlockingDeque();
72 q.offerFirst(null);
73 shouldThrow();
74 } catch (NullPointerException success) {}
75 }
76
77 /**
78 * OfferFirst succeeds
79 */
80 public void testOfferFirst() {
81 LinkedBlockingDeque q = new LinkedBlockingDeque();
82 assertTrue(q.offerFirst(new Integer(0)));
83 assertTrue(q.offerFirst(new Integer(1)));
84 }
85
86 /**
87 * OfferLast succeeds
88 */
89 public void testOfferLast() {
90 LinkedBlockingDeque q = new LinkedBlockingDeque();
91 assertTrue(q.offerLast(new Integer(0)));
92 assertTrue(q.offerLast(new Integer(1)));
93 }
94
95 /**
96 * pollFirst succeeds unless empty
97 */
98 public void testPollFirst() {
99 LinkedBlockingDeque q = populatedDeque(SIZE);
100 for (int i = 0; i < SIZE; ++i) {
101 assertEquals(i, q.pollFirst());
102 }
103 assertNull(q.pollFirst());
104 }
105
106 /**
107 * pollLast succeeds unless empty
108 */
109 public void testPollLast() {
110 LinkedBlockingDeque q = populatedDeque(SIZE);
111 for (int i = SIZE-1; i >= 0; --i) {
112 assertEquals(i, q.pollLast());
113 }
114 assertNull(q.pollLast());
115 }
116
117 /**
118 * peekFirst returns next element, or null if empty
119 */
120 public void testPeekFirst() {
121 LinkedBlockingDeque q = populatedDeque(SIZE);
122 for (int i = 0; i < SIZE; ++i) {
123 assertEquals(i, q.peekFirst());
124 assertEquals(i, q.pollFirst());
125 assertTrue(q.peekFirst() == null ||
126 !q.peekFirst().equals(i));
127 }
128 assertNull(q.peekFirst());
129 }
130
131 /**
132 * peek returns next element, or null if empty
133 */
134 public void testPeek() {
135 LinkedBlockingDeque q = populatedDeque(SIZE);
136 for (int i = 0; i < SIZE; ++i) {
137 assertEquals(i, q.peek());
138 assertEquals(i, q.pollFirst());
139 assertTrue(q.peek() == null ||
140 !q.peek().equals(i));
141 }
142 assertNull(q.peek());
143 }
144
145 /**
146 * peekLast returns next element, or null if empty
147 */
148 public void testPeekLast() {
149 LinkedBlockingDeque q = populatedDeque(SIZE);
150 for (int i = SIZE-1; i >= 0; --i) {
151 assertEquals(i, q.peekLast());
152 assertEquals(i, q.pollLast());
153 assertTrue(q.peekLast() == null ||
154 !q.peekLast().equals(i));
155 }
156 assertNull(q.peekLast());
157 }
158
159 /**
160 * getFirst() returns first element, or throws NSEE if empty
161 */
162 public void testFirstElement() {
163 LinkedBlockingDeque q = populatedDeque(SIZE);
164 for (int i = 0; i < SIZE; ++i) {
165 assertEquals(i, q.getFirst());
166 assertEquals(i, q.pollFirst());
167 }
168 try {
169 q.getFirst();
170 shouldThrow();
171 } catch (NoSuchElementException success) {}
172 assertNull(q.peekFirst());
173 }
174
175 /**
176 * getLast() returns last element, or throws NSEE if empty
177 */
178 public void testLastElement() {
179 LinkedBlockingDeque q = populatedDeque(SIZE);
180 for (int i = SIZE-1; i >= 0; --i) {
181 assertEquals(i, q.getLast());
182 assertEquals(i, q.pollLast());
183 }
184 try {
185 q.getLast();
186 shouldThrow();
187 } catch (NoSuchElementException success) {}
188 assertNull(q.peekLast());
189 }
190
191 /**
192 * removeFirst() removes first element, or throws NSEE if empty
193 */
194 public void testRemoveFirst() {
195 LinkedBlockingDeque q = populatedDeque(SIZE);
196 for (int i = 0; i < SIZE; ++i) {
197 assertEquals(i, q.removeFirst());
198 }
199 try {
200 q.removeFirst();
201 shouldThrow();
202 } catch (NoSuchElementException success) {}
203 assertNull(q.peekFirst());
204 }
205
206 /**
207 * removeLast() removes last element, or throws NSEE if empty
208 */
209 public void testRemoveLast() {
210 LinkedBlockingDeque q = populatedDeque(SIZE);
211 for (int i = SIZE - 1; i >= 0; --i) {
212 assertEquals(i, q.removeLast());
213 }
214 try {
215 q.removeLast();
216 shouldThrow();
217 } catch (NoSuchElementException success) {}
218 assertNull(q.peekLast());
219 }
220
221 /**
222 * remove removes next element, or throws NSEE if empty
223 */
224 public void testRemove() {
225 LinkedBlockingDeque q = populatedDeque(SIZE);
226 for (int i = 0; i < SIZE; ++i) {
227 assertEquals(i, q.remove());
228 }
229 try {
230 q.remove();
231 shouldThrow();
232 } catch (NoSuchElementException success) {}
233 }
234
235 /**
236 * removeFirstOccurrence(x) removes x and returns true if present
237 */
238 public void testRemoveFirstOccurrence() {
239 LinkedBlockingDeque q = populatedDeque(SIZE);
240 for (int i = 1; i < SIZE; i+=2) {
241 assertTrue(q.removeFirstOccurrence(new Integer(i)));
242 }
243 for (int i = 0; i < SIZE; i+=2) {
244 assertTrue(q.removeFirstOccurrence(new Integer(i)));
245 assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
246 }
247 assertTrue(q.isEmpty());
248 }
249
250 /**
251 * removeLastOccurrence(x) removes x and returns true if present
252 */
253 public void testRemoveLastOccurrence() {
254 LinkedBlockingDeque q = populatedDeque(SIZE);
255 for (int i = 1; i < SIZE; i+=2) {
256 assertTrue(q.removeLastOccurrence(new Integer(i)));
257 }
258 for (int i = 0; i < SIZE; i+=2) {
259 assertTrue(q.removeLastOccurrence(new Integer(i)));
260 assertFalse(q.removeLastOccurrence(new Integer(i+1)));
261 }
262 assertTrue(q.isEmpty());
263 }
264
265 /**
266 * peekFirst returns element inserted with addFirst
267 */
268 public void testAddFirst() {
269 LinkedBlockingDeque q = populatedDeque(3);
270 q.pollLast();
271 q.addFirst(four);
272 assertSame(four, q.peekFirst());
273 }
274
275 /**
276 * peekLast returns element inserted with addLast
277 */
278 public void testAddLast() {
279 LinkedBlockingDeque q = populatedDeque(3);
280 q.pollLast();
281 q.addLast(four);
282 assertSame(four, q.peekLast());
283 }
284
285
286 /**
287 * A new deque has the indicated capacity, or Integer.MAX_VALUE if
288 * none given
289 */
290 public void testConstructor1() {
291 assertEquals(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity());
292 assertEquals(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity());
293 }
294
295 /**
296 * Constructor throws IAE if capacity argument nonpositive
297 */
298 public void testConstructor2() {
299 try {
300 LinkedBlockingDeque q = new LinkedBlockingDeque(0);
301 shouldThrow();
302 } catch (IllegalArgumentException success) {}
303 }
304
305 /**
306 * Initializing from null Collection throws NPE
307 */
308 public void testConstructor3() {
309 try {
310 LinkedBlockingDeque q = new LinkedBlockingDeque(null);
311 shouldThrow();
312 } catch (NullPointerException success) {}
313 }
314
315 /**
316 * Initializing from Collection of null elements throws NPE
317 */
318 public void testConstructor4() {
319 try {
320 Integer[] ints = new Integer[SIZE];
321 LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
322 shouldThrow();
323 } catch (NullPointerException success) {}
324 }
325
326 /**
327 * Initializing from Collection with some null elements throws NPE
328 */
329 public void testConstructor5() {
330 try {
331 Integer[] ints = new Integer[SIZE];
332 for (int i = 0; i < SIZE-1; ++i)
333 ints[i] = new Integer(i);
334 LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
335 shouldThrow();
336 } catch (NullPointerException success) {}
337 }
338
339 /**
340 * Deque contains all elements of collection used to initialize
341 */
342 public void testConstructor6() {
343 Integer[] ints = new Integer[SIZE];
344 for (int i = 0; i < SIZE; ++i)
345 ints[i] = new Integer(i);
346 LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
347 for (int i = 0; i < SIZE; ++i)
348 assertEquals(ints[i], q.poll());
349 }
350
351 /**
352 * Deque transitions from empty to full when elements added
353 */
354 public void testEmptyFull() {
355 LinkedBlockingDeque q = new LinkedBlockingDeque(2);
356 assertTrue(q.isEmpty());
357 assertEquals("should have room for 2", 2, q.remainingCapacity());
358 q.add(one);
359 assertFalse(q.isEmpty());
360 q.add(two);
361 assertFalse(q.isEmpty());
362 assertEquals(0, q.remainingCapacity());
363 assertFalse(q.offer(three));
364 }
365
366 /**
367 * remainingCapacity decreases on add, increases on remove
368 */
369 public void testRemainingCapacity() {
370 LinkedBlockingDeque q = populatedDeque(SIZE);
371 for (int i = 0; i < SIZE; ++i) {
372 assertEquals(i, q.remainingCapacity());
373 assertEquals(SIZE-i, q.size());
374 q.remove();
375 }
376 for (int i = 0; i < SIZE; ++i) {
377 assertEquals(SIZE-i, q.remainingCapacity());
378 assertEquals(i, q.size());
379 q.add(new Integer(i));
380 }
381 }
382
383 /**
384 * offer(null) throws NPE
385 */
386 public void testOfferNull() {
387 try {
388 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
389 q.offer(null);
390 shouldThrow();
391 } catch (NullPointerException success) {}
392 }
393
394 /**
395 * add(null) throws NPE
396 */
397 public void testAddNull() {
398 try {
399 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
400 q.add(null);
401 shouldThrow();
402 } catch (NullPointerException success) {}
403 }
404
405 /**
406 * push(null) throws NPE
407 */
408 public void testPushNull() {
409 try {
410 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
411 q.push(null);
412 shouldThrow();
413 } catch (NullPointerException success) {}
414 }
415
416 /**
417 * push succeeds if not full; throws ISE if full
418 */
419 public void testPush() {
420 try {
421 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
422 for (int i = 0; i < SIZE; ++i) {
423 Integer I = new Integer(i);
424 q.push(I);
425 assertEquals(I, q.peek());
426 }
427 assertEquals(0, q.remainingCapacity());
428 q.push(new Integer(SIZE));
429 shouldThrow();
430 } catch (IllegalStateException success) {}
431 }
432
433 /**
434 * peekFirst returns element inserted with push
435 */
436 public void testPushWithPeek() {
437 LinkedBlockingDeque q = populatedDeque(3);
438 q.pollLast();
439 q.push(four);
440 assertSame(four, q.peekFirst());
441 }
442
443
444 /**
445 * pop removes next element, or throws NSEE if empty
446 */
447 public void testPop() {
448 LinkedBlockingDeque q = populatedDeque(SIZE);
449 for (int i = 0; i < SIZE; ++i) {
450 assertEquals(i, q.pop());
451 }
452 try {
453 q.pop();
454 shouldThrow();
455 } catch (NoSuchElementException success) {}
456 }
457
458
459 /**
460 * Offer succeeds if not full; fails if full
461 */
462 public void testOffer() {
463 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
464 assertTrue(q.offer(zero));
465 assertFalse(q.offer(one));
466 }
467
468 /**
469 * add succeeds if not full; throws ISE if full
470 */
471 public void testAdd() {
472 try {
473 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
474 for (int i = 0; i < SIZE; ++i) {
475 assertTrue(q.add(new Integer(i)));
476 }
477 assertEquals(0, q.remainingCapacity());
478 q.add(new Integer(SIZE));
479 shouldThrow();
480 } catch (IllegalStateException success) {}
481 }
482
483 /**
484 * addAll(null) throws NPE
485 */
486 public void testAddAll1() {
487 try {
488 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
489 q.addAll(null);
490 shouldThrow();
491 } catch (NullPointerException success) {}
492 }
493
494 /**
495 * addAll(this) throws IAE
496 */
497 public void testAddAllSelf() {
498 try {
499 LinkedBlockingDeque q = populatedDeque(SIZE);
500 q.addAll(q);
501 shouldThrow();
502 } catch (IllegalArgumentException success) {}
503 }
504
505 /**
506 * addAll of a collection with null elements throws NPE
507 */
508 public void testAddAll2() {
509 try {
510 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
511 Integer[] ints = new Integer[SIZE];
512 q.addAll(Arrays.asList(ints));
513 shouldThrow();
514 } catch (NullPointerException success) {}
515 }
516
517 /**
518 * addAll of a collection with any null elements throws NPE after
519 * possibly adding some elements
520 */
521 public void testAddAll3() {
522 try {
523 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
524 Integer[] ints = new Integer[SIZE];
525 for (int i = 0; i < SIZE-1; ++i)
526 ints[i] = new Integer(i);
527 q.addAll(Arrays.asList(ints));
528 shouldThrow();
529 } catch (NullPointerException success) {}
530 }
531
532 /**
533 * addAll throws ISE if not enough room
534 */
535 public void testAddAll4() {
536 try {
537 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
538 Integer[] ints = new Integer[SIZE];
539 for (int i = 0; i < SIZE; ++i)
540 ints[i] = new Integer(i);
541 q.addAll(Arrays.asList(ints));
542 shouldThrow();
543 } catch (IllegalStateException success) {}
544 }
545
546 /**
547 * Deque contains all elements, in traversal order, of successful addAll
548 */
549 public void testAddAll5() {
550 Integer[] empty = new Integer[0];
551 Integer[] ints = new Integer[SIZE];
552 for (int i = 0; i < SIZE; ++i)
553 ints[i] = new Integer(i);
554 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
555 assertFalse(q.addAll(Arrays.asList(empty)));
556 assertTrue(q.addAll(Arrays.asList(ints)));
557 for (int i = 0; i < SIZE; ++i)
558 assertEquals(ints[i], q.poll());
559 }
560
561
562 /**
563 * put(null) throws NPE
564 */
565 public void testPutNull() throws InterruptedException {
566 try {
567 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
568 q.put(null);
569 shouldThrow();
570 } catch (NullPointerException success) {}
571 }
572
573 /**
574 * all elements successfully put are contained
575 */
576 public void testPut() throws InterruptedException {
577 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
578 for (int i = 0; i < SIZE; ++i) {
579 Integer I = new Integer(i);
580 q.put(I);
581 assertTrue(q.contains(I));
582 }
583 assertEquals(0, q.remainingCapacity());
584 }
585
586 /**
587 * put blocks interruptibly if full
588 */
589 public void testBlockingPut() throws InterruptedException {
590 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
591 Thread t = new Thread(new CheckedRunnable() {
592 public void realRun() throws InterruptedException {
593 for (int i = 0; i < SIZE; ++i)
594 q.put(i);
595 assertEquals(SIZE, q.size());
596 assertEquals(0, q.remainingCapacity());
597 try {
598 q.put(99);
599 shouldThrow();
600 } catch (InterruptedException success) {}
601 }});
602
603 t.start();
604 Thread.sleep(SHORT_DELAY_MS);
605 t.interrupt();
606 t.join();
607 assertEquals(SIZE, q.size());
608 assertEquals(0, q.remainingCapacity());
609 }
610
611 /**
612 * put blocks waiting for take when full
613 */
614 public void testPutWithTake() throws InterruptedException {
615 final int capacity = 2;
616 final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
617 Thread t = new Thread(new CheckedRunnable() {
618 public void realRun() throws InterruptedException {
619 for (int i = 0; i < capacity + 1; i++)
620 q.put(i);
621 try {
622 q.put(99);
623 shouldThrow();
624 } catch (InterruptedException success) {}
625 }});
626
627 t.start();
628 Thread.sleep(SHORT_DELAY_MS);
629 assertEquals(q.remainingCapacity(), 0);
630 assertEquals(0, q.take());
631 Thread.sleep(SHORT_DELAY_MS);
632 t.interrupt();
633 t.join();
634 assertEquals(q.remainingCapacity(), 0);
635 }
636
637 /**
638 * timed offer times out if full and elements not taken
639 */
640 public void testTimedOffer() throws InterruptedException {
641 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
642 Thread t = new Thread(new CheckedRunnable() {
643 public void realRun() throws InterruptedException {
644 q.put(new Object());
645 q.put(new Object());
646 assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
647 try {
648 q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
649 shouldThrow();
650 } catch (InterruptedException success) {}
651 }});
652
653 t.start();
654 Thread.sleep(SMALL_DELAY_MS);
655 t.interrupt();
656 t.join();
657 }
658
659 /**
660 * take retrieves elements in FIFO order
661 */
662 public void testTake() throws InterruptedException {
663 LinkedBlockingDeque q = populatedDeque(SIZE);
664 for (int i = 0; i < SIZE; ++i) {
665 assertEquals(i, q.take());
666 }
667 }
668
669 /**
670 * take blocks interruptibly when empty
671 */
672 public void testTakeFromEmpty() throws InterruptedException {
673 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
674 Thread t = new ThreadShouldThrow(InterruptedException.class) {
675 public void realRun() throws InterruptedException {
676 q.take();
677 }};
678
679 t.start();
680 Thread.sleep(SHORT_DELAY_MS);
681 t.interrupt();
682 t.join();
683 }
684
685 /**
686 * Take removes existing elements until empty, then blocks interruptibly
687 */
688 public void testBlockingTake() throws InterruptedException {
689 final LinkedBlockingDeque q = populatedDeque(SIZE);
690 Thread t = new Thread(new CheckedRunnable() {
691 public void realRun() throws InterruptedException {
692 for (int i = 0; i < SIZE; ++i) {
693 assertEquals(i, q.take());
694 }
695 try {
696 q.take();
697 shouldThrow();
698 } catch (InterruptedException success) {}
699 }});
700
701 t.start();
702 Thread.sleep(SHORT_DELAY_MS);
703 t.interrupt();
704 t.join();
705 }
706
707
708 /**
709 * poll succeeds unless empty
710 */
711 public void testPoll() {
712 LinkedBlockingDeque q = populatedDeque(SIZE);
713 for (int i = 0; i < SIZE; ++i) {
714 assertEquals(i, q.poll());
715 }
716 assertNull(q.poll());
717 }
718
719 /**
720 * timed poll with zero timeout succeeds when non-empty, else times out
721 */
722 public void testTimedPoll0() throws InterruptedException {
723 LinkedBlockingDeque q = populatedDeque(SIZE);
724 for (int i = 0; i < SIZE; ++i) {
725 assertEquals(i, q.poll(0, MILLISECONDS));
726 }
727 assertNull(q.poll(0, MILLISECONDS));
728 }
729
730 /**
731 * timed poll with nonzero timeout succeeds when non-empty, else times out
732 */
733 public void testTimedPoll() throws InterruptedException {
734 LinkedBlockingDeque q = populatedDeque(SIZE);
735 for (int i = 0; i < SIZE; ++i) {
736 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
737 }
738 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
739 }
740
741 /**
742 * Interrupted timed poll throws InterruptedException instead of
743 * returning timeout status
744 */
745 public void testInterruptedTimedPoll() throws InterruptedException {
746 Thread t = new Thread(new CheckedRunnable() {
747 public void realRun() throws InterruptedException {
748 LinkedBlockingDeque q = populatedDeque(SIZE);
749 for (int i = 0; i < SIZE; ++i) {
750 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
751 }
752 try {
753 q.poll(SMALL_DELAY_MS, MILLISECONDS);
754 shouldThrow();
755 } catch (InterruptedException success) {}
756 }});
757
758 t.start();
759 Thread.sleep(SHORT_DELAY_MS);
760 t.interrupt();
761 t.join();
762 }
763
764 /**
765 * timed poll before a delayed offer fails; after offer succeeds;
766 * on interruption throws
767 */
768 public void testTimedPollWithOffer() throws InterruptedException {
769 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
770 Thread t = new Thread(new CheckedRunnable() {
771 public void realRun() throws InterruptedException {
772 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
773 assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
774 try {
775 q.poll(LONG_DELAY_MS, MILLISECONDS);
776 shouldThrow();
777 } catch (InterruptedException success) {}
778 }});
779
780 t.start();
781 Thread.sleep(SMALL_DELAY_MS);
782 assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
783 t.interrupt();
784 t.join();
785 }
786
787
788 /**
789 * putFirst(null) throws NPE
790 */
791 public void testPutFirstNull() throws InterruptedException {
792 try {
793 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
794 q.putFirst(null);
795 shouldThrow();
796 } catch (NullPointerException success) {}
797 }
798
799 /**
800 * all elements successfully putFirst are contained
801 */
802 public void testPutFirst() throws InterruptedException {
803 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
804 for (int i = 0; i < SIZE; ++i) {
805 Integer I = new Integer(i);
806 q.putFirst(I);
807 assertTrue(q.contains(I));
808 }
809 assertEquals(0, q.remainingCapacity());
810 }
811
812 /**
813 * putFirst blocks interruptibly if full
814 */
815 public void testBlockingPutFirst() throws InterruptedException {
816 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
817 Thread t = new Thread(new CheckedRunnable() {
818 public void realRun() throws InterruptedException {
819 for (int i = 0; i < SIZE; ++i)
820 q.putFirst(i);
821 assertEquals(SIZE, q.size());
822 assertEquals(0, q.remainingCapacity());
823 try {
824 q.putFirst(99);
825 shouldThrow();
826 } catch (InterruptedException success) {}
827 }});
828
829 t.start();
830 Thread.sleep(SHORT_DELAY_MS);
831 t.interrupt();
832 t.join();
833 assertEquals(SIZE, q.size());
834 assertEquals(0, q.remainingCapacity());
835 }
836
837 /**
838 * putFirst blocks waiting for take when full
839 */
840 public void testPutFirstWithTake() throws InterruptedException {
841 final int capacity = 2;
842 final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
843 Thread t = new Thread(new CheckedRunnable() {
844 public void realRun() throws InterruptedException {
845 for (int i = 0; i < capacity + 1; i++)
846 q.putFirst(i);
847 try {
848 q.putFirst(99);
849 shouldThrow();
850 } catch (InterruptedException success) {}
851 }});
852
853 t.start();
854 Thread.sleep(SHORT_DELAY_MS);
855 assertEquals(q.remainingCapacity(), 0);
856 assertEquals(capacity - 1, q.take());
857 Thread.sleep(SHORT_DELAY_MS);
858 t.interrupt();
859 t.join();
860 assertEquals(q.remainingCapacity(), 0);
861 }
862
863 /**
864 * timed offerFirst times out if full and elements not taken
865 */
866 public void testTimedOfferFirst() throws InterruptedException {
867 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
868 Thread t = new Thread(new CheckedRunnable() {
869 public void realRun() throws InterruptedException {
870 q.putFirst(new Object());
871 q.putFirst(new Object());
872 assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
873 try {
874 q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
875 shouldThrow();
876 } catch (InterruptedException success) {}
877 }});
878
879 t.start();
880 Thread.sleep(SMALL_DELAY_MS);
881 t.interrupt();
882 t.join();
883 }
884
885 /**
886 * take retrieves elements in FIFO order
887 */
888 public void testTakeFirst() throws InterruptedException {
889 LinkedBlockingDeque q = populatedDeque(SIZE);
890 for (int i = 0; i < SIZE; ++i) {
891 assertEquals(i, q.takeFirst());
892 }
893 }
894
895 /**
896 * takeFirst blocks interruptibly when empty
897 */
898 public void testTakeFirstFromEmpty() throws InterruptedException {
899 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
900 Thread t = new ThreadShouldThrow(InterruptedException.class) {
901 public void realRun() throws InterruptedException {
902 q.takeFirst();
903 }};
904
905 t.start();
906 Thread.sleep(SHORT_DELAY_MS);
907 t.interrupt();
908 t.join();
909 }
910
911 /**
912 * TakeFirst removes existing elements until empty, then blocks interruptibly
913 */
914 public void testBlockingTakeFirst() throws InterruptedException {
915 final LinkedBlockingDeque q = populatedDeque(SIZE);
916 Thread t = new Thread(new CheckedRunnable() {
917 public void realRun() throws InterruptedException {
918 for (int i = 0; i < SIZE; ++i)
919 assertEquals(i, q.takeFirst());
920 try {
921 q.takeFirst();
922 shouldThrow();
923 } catch (InterruptedException success) {}
924 }});
925
926 t.start();
927 Thread.sleep(SHORT_DELAY_MS);
928 t.interrupt();
929 t.join();
930 }
931
932
933 /**
934 * timed pollFirst with zero timeout succeeds when non-empty, else times out
935 */
936 public void testTimedPollFirst0() throws InterruptedException {
937 LinkedBlockingDeque q = populatedDeque(SIZE);
938 for (int i = 0; i < SIZE; ++i) {
939 assertEquals(i, q.pollFirst(0, MILLISECONDS));
940 }
941 assertNull(q.pollFirst(0, MILLISECONDS));
942 }
943
944 /**
945 * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
946 */
947 public void testTimedPollFirst() throws InterruptedException {
948 LinkedBlockingDeque q = populatedDeque(SIZE);
949 for (int i = 0; i < SIZE; ++i) {
950 assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
951 }
952 assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
953 }
954
955 /**
956 * Interrupted timed pollFirst throws InterruptedException instead of
957 * returning timeout status
958 */
959 public void testInterruptedTimedPollFirst() throws InterruptedException {
960 Thread t = new Thread(new CheckedRunnable() {
961 public void realRun() throws InterruptedException {
962 LinkedBlockingDeque q = populatedDeque(SIZE);
963 for (int i = 0; i < SIZE; ++i) {
964 assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
965 }
966 try {
967 q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
968 shouldThrow();
969 } catch (InterruptedException success) {}
970 }});
971
972 t.start();
973 Thread.sleep(SHORT_DELAY_MS);
974 t.interrupt();
975 t.join();
976 }
977
978 /**
979 * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
980 * on interruption throws
981 */
982 public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
983 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
984 Thread t = new Thread(new CheckedRunnable() {
985 public void realRun() throws InterruptedException {
986 assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
987 assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
988 try {
989 q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
990 shouldThrow();
991 } catch (InterruptedException success) {}
992 }});
993
994 t.start();
995 Thread.sleep(SMALL_DELAY_MS);
996 assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
997 t.interrupt();
998 t.join();
999 }
1000
1001 /**
1002 * putLast(null) throws NPE
1003 */
1004 public void testPutLastNull() throws InterruptedException {
1005 try {
1006 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1007 q.putLast(null);
1008 shouldThrow();
1009 } catch (NullPointerException success) {}
1010 }
1011
1012 /**
1013 * all elements successfully putLast are contained
1014 */
1015 public void testPutLast() throws InterruptedException {
1016 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1017 for (int i = 0; i < SIZE; ++i) {
1018 Integer I = new Integer(i);
1019 q.putLast(I);
1020 assertTrue(q.contains(I));
1021 }
1022 assertEquals(0, q.remainingCapacity());
1023 }
1024
1025 /**
1026 * putLast blocks interruptibly if full
1027 */
1028 public void testBlockingPutLast() throws InterruptedException {
1029 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1030 Thread t = new Thread(new CheckedRunnable() {
1031 public void realRun() throws InterruptedException {
1032 for (int i = 0; i < SIZE; ++i)
1033 q.putLast(i);
1034 assertEquals(SIZE, q.size());
1035 assertEquals(0, q.remainingCapacity());
1036 try {
1037 q.putLast(99);
1038 shouldThrow();
1039 } catch (InterruptedException success) {}
1040 }});
1041
1042 t.start();
1043 Thread.sleep(SHORT_DELAY_MS);
1044 t.interrupt();
1045 t.join();
1046 assertEquals(SIZE, q.size());
1047 assertEquals(0, q.remainingCapacity());
1048 }
1049
1050 /**
1051 * putLast blocks waiting for take when full
1052 */
1053 public void testPutLastWithTake() throws InterruptedException {
1054 final int capacity = 2;
1055 final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1056 Thread t = new Thread(new CheckedRunnable() {
1057 public void realRun() throws InterruptedException {
1058 for (int i = 0; i < capacity + 1; i++)
1059 q.putLast(i);
1060 try {
1061 q.putLast(99);
1062 shouldThrow();
1063 } catch (InterruptedException success) {}
1064 }});
1065
1066 t.start();
1067 Thread.sleep(SHORT_DELAY_MS);
1068 assertEquals(q.remainingCapacity(), 0);
1069 assertEquals(0, q.take());
1070 Thread.sleep(SHORT_DELAY_MS);
1071 t.interrupt();
1072 t.join();
1073 assertEquals(q.remainingCapacity(), 0);
1074 }
1075
1076 /**
1077 * timed offerLast times out if full and elements not taken
1078 */
1079 public void testTimedOfferLast() throws InterruptedException {
1080 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1081 Thread t = new Thread(new CheckedRunnable() {
1082 public void realRun() throws InterruptedException {
1083 q.putLast(new Object());
1084 q.putLast(new Object());
1085 assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1086 try {
1087 q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1088 shouldThrow();
1089 } catch (InterruptedException success) {}
1090 }});
1091
1092 t.start();
1093 Thread.sleep(SMALL_DELAY_MS);
1094 t.interrupt();
1095 t.join();
1096 }
1097
1098 /**
1099 * takeLast retrieves elements in FIFO order
1100 */
1101 public void testTakeLast() throws InterruptedException {
1102 LinkedBlockingDeque q = populatedDeque(SIZE);
1103 for (int i = 0; i < SIZE; ++i) {
1104 assertEquals(SIZE-i-1, q.takeLast());
1105 }
1106 }
1107
1108 /**
1109 * takeLast blocks interruptibly when empty
1110 */
1111 public void testTakeLastFromEmpty() throws InterruptedException {
1112 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1113 Thread t = new ThreadShouldThrow(InterruptedException.class) {
1114 public void realRun() throws InterruptedException {
1115 q.takeLast();
1116 }};
1117
1118 t.start();
1119 Thread.sleep(SHORT_DELAY_MS);
1120 t.interrupt();
1121 t.join();
1122 }
1123
1124 /**
1125 * TakeLast removes existing elements until empty, then blocks interruptibly
1126 */
1127 public void testBlockingTakeLast() throws InterruptedException {
1128 final LinkedBlockingDeque q = populatedDeque(SIZE);
1129 Thread t = new Thread(new CheckedRunnable() {
1130 public void realRun() throws InterruptedException {
1131 for (int i = 0; i < SIZE; ++i)
1132 assertEquals(SIZE - 1 - i, q.takeLast());
1133 try {
1134 q.takeLast();
1135 shouldThrow();
1136 } catch (InterruptedException success) {}
1137 }});
1138
1139 t.start();
1140 Thread.sleep(SHORT_DELAY_MS);
1141 t.interrupt();
1142 t.join();
1143 }
1144
1145 /**
1146 * timed pollLast with zero timeout succeeds when non-empty, else times out
1147 */
1148 public void testTimedPollLast0() throws InterruptedException {
1149 LinkedBlockingDeque q = populatedDeque(SIZE);
1150 for (int i = 0; i < SIZE; ++i) {
1151 assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1152 }
1153 assertNull(q.pollLast(0, MILLISECONDS));
1154 }
1155
1156 /**
1157 * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1158 */
1159 public void testTimedPollLast() throws InterruptedException {
1160 LinkedBlockingDeque q = populatedDeque(SIZE);
1161 for (int i = 0; i < SIZE; ++i) {
1162 assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1163 }
1164 assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1165 }
1166
1167 /**
1168 * Interrupted timed pollLast throws InterruptedException instead of
1169 * returning timeout status
1170 */
1171 public void testInterruptedTimedPollLast() throws InterruptedException {
1172 Thread t = new Thread(new CheckedRunnable() {
1173 public void realRun() throws InterruptedException {
1174 LinkedBlockingDeque q = populatedDeque(SIZE);
1175 for (int i = 0; i < SIZE; ++i) {
1176 assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1177 }
1178 try {
1179 q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1180 shouldThrow();
1181 } catch (InterruptedException success) {}
1182 }});
1183
1184 t.start();
1185 Thread.sleep(SHORT_DELAY_MS);
1186 t.interrupt();
1187 t.join();
1188 }
1189
1190 /**
1191 * timed poll before a delayed offerLast fails; after offerLast succeeds;
1192 * on interruption throws
1193 */
1194 public void testTimedPollWithOfferLast() throws InterruptedException {
1195 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1196 Thread t = new Thread(new CheckedRunnable() {
1197 public void realRun() throws InterruptedException {
1198 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1199 assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1200 try {
1201 q.poll(LONG_DELAY_MS, MILLISECONDS);
1202 shouldThrow();
1203 } catch (InterruptedException success) {}
1204 }});
1205
1206 t.start();
1207 Thread.sleep(SMALL_DELAY_MS);
1208 assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1209 t.interrupt();
1210 t.join();
1211 }
1212
1213
1214 /**
1215 * element returns next element, or throws NSEE if empty
1216 */
1217 public void testElement() {
1218 LinkedBlockingDeque q = populatedDeque(SIZE);
1219 for (int i = 0; i < SIZE; ++i) {
1220 assertEquals(i, q.element());
1221 q.poll();
1222 }
1223 try {
1224 q.element();
1225 shouldThrow();
1226 } catch (NoSuchElementException success) {}
1227 }
1228
1229 /**
1230 * remove(x) removes x and returns true if present
1231 */
1232 public void testRemoveElement() {
1233 LinkedBlockingDeque q = populatedDeque(SIZE);
1234 for (int i = 1; i < SIZE; i+=2) {
1235 assertTrue(q.remove(new Integer(i)));
1236 }
1237 for (int i = 0; i < SIZE; i+=2) {
1238 assertTrue(q.remove(new Integer(i)));
1239 assertFalse(q.remove(new Integer(i+1)));
1240 }
1241 assertTrue(q.isEmpty());
1242 }
1243
1244 /**
1245 * contains(x) reports true when elements added but not yet removed
1246 */
1247 public void testContains() {
1248 LinkedBlockingDeque q = populatedDeque(SIZE);
1249 for (int i = 0; i < SIZE; ++i) {
1250 assertTrue(q.contains(new Integer(i)));
1251 q.poll();
1252 assertFalse(q.contains(new Integer(i)));
1253 }
1254 }
1255
1256 /**
1257 * clear removes all elements
1258 */
1259 public void testClear() {
1260 LinkedBlockingDeque q = populatedDeque(SIZE);
1261 q.clear();
1262 assertTrue(q.isEmpty());
1263 assertEquals(0, q.size());
1264 assertEquals(SIZE, q.remainingCapacity());
1265 q.add(one);
1266 assertFalse(q.isEmpty());
1267 assertTrue(q.contains(one));
1268 q.clear();
1269 assertTrue(q.isEmpty());
1270 }
1271
1272 /**
1273 * containsAll(c) is true when c contains a subset of elements
1274 */
1275 public void testContainsAll() {
1276 LinkedBlockingDeque q = populatedDeque(SIZE);
1277 LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE);
1278 for (int i = 0; i < SIZE; ++i) {
1279 assertTrue(q.containsAll(p));
1280 assertFalse(p.containsAll(q));
1281 p.add(new Integer(i));
1282 }
1283 assertTrue(p.containsAll(q));
1284 }
1285
1286 /**
1287 * retainAll(c) retains only those elements of c and reports true if changed
1288 */
1289 public void testRetainAll() {
1290 LinkedBlockingDeque q = populatedDeque(SIZE);
1291 LinkedBlockingDeque p = populatedDeque(SIZE);
1292 for (int i = 0; i < SIZE; ++i) {
1293 boolean changed = q.retainAll(p);
1294 if (i == 0)
1295 assertFalse(changed);
1296 else
1297 assertTrue(changed);
1298
1299 assertTrue(q.containsAll(p));
1300 assertEquals(SIZE-i, q.size());
1301 p.remove();
1302 }
1303 }
1304
1305 /**
1306 * removeAll(c) removes only those elements of c and reports true if changed
1307 */
1308 public void testRemoveAll() {
1309 for (int i = 1; i < SIZE; ++i) {
1310 LinkedBlockingDeque q = populatedDeque(SIZE);
1311 LinkedBlockingDeque p = populatedDeque(i);
1312 assertTrue(q.removeAll(p));
1313 assertEquals(SIZE-i, q.size());
1314 for (int j = 0; j < i; ++j) {
1315 Integer I = (Integer)(p.remove());
1316 assertFalse(q.contains(I));
1317 }
1318 }
1319 }
1320
1321 /**
1322 * toArray contains all elements
1323 */
1324 public void testToArray() throws InterruptedException{
1325 LinkedBlockingDeque q = populatedDeque(SIZE);
1326 Object[] o = q.toArray();
1327 for (int i = 0; i < o.length; i++)
1328 assertEquals(o[i], q.take());
1329 }
1330
1331 /**
1332 * toArray(a) contains all elements
1333 */
1334 public void testToArray2() throws InterruptedException {
1335 LinkedBlockingDeque q = populatedDeque(SIZE);
1336 Integer[] ints = new Integer[SIZE];
1337 ints = (Integer[])q.toArray(ints);
1338 for (int i = 0; i < ints.length; i++)
1339 assertEquals(ints[i], q.take());
1340 }
1341
1342 /**
1343 * toArray(null) throws NPE
1344 */
1345 public void testToArray_BadArg() {
1346 LinkedBlockingDeque q = populatedDeque(SIZE);
1347 try {
1348 Object o[] = q.toArray(null);
1349 shouldThrow();
1350 } catch (NullPointerException success) {}
1351 }
1352
1353 /**
1354 * toArray with incompatible array type throws CCE
1355 */
1356 public void testToArray1_BadArg() {
1357 LinkedBlockingDeque q = populatedDeque(SIZE);
1358 try {
1359 Object o[] = 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 Thread.sleep(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 Thread.sleep(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 max {n, size} elements of deque 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 }