ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.16
Committed: Sat Nov 21 22:00:46 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.15: +22 -12 lines
Log Message:
reduce scope of check for IE in testTimedOffer*

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, ((Integer)q.pollFirst()).intValue());
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, ((Integer)q.pollLast()).intValue());
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, ((Integer)q.peekFirst()).intValue());
124 q.pollFirst();
125 assertTrue(q.peekFirst() == null ||
126 i != ((Integer)q.peekFirst()).intValue());
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, ((Integer)q.peek()).intValue());
138 q.pollFirst();
139 assertTrue(q.peek() == null ||
140 i != ((Integer)q.peek()).intValue());
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, ((Integer)q.peekLast()).intValue());
152 q.pollLast();
153 assertTrue(q.peekLast() == null ||
154 i != ((Integer)q.peekLast()).intValue());
155 }
156 assertNull(q.peekLast());
157 }
158
159 /**
160 * getFirst returns next getFirst, 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, ((Integer)q.getFirst()).intValue());
166 q.pollFirst();
167 }
168 try {
169 q.getFirst();
170 shouldThrow();
171 } catch (NoSuchElementException success) {}
172 assertNull(q.peekFirst());
173 }
174
175 /**
176 * getLast returns next 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, ((Integer)q.getLast()).intValue());
182 q.pollLast();
183 }
184 try {
185 q.getLast();
186 shouldThrow();
187 } catch (NoSuchElementException success) {}
188 assertNull(q.peekLast());
189 }
190
191 /**
192 * removeFirst removes next 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, ((Integer)q.removeFirst()).intValue());
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, ((Integer)q.removeLast()).intValue());
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, ((Integer)q.remove()).intValue());
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 assertEquals(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 assertEquals(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 assertEquals(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, ((Integer)q.pop()).intValue());
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 * addAll of a collection with any null elements throws NPE after
518 * possibly adding some elements
519 */
520 public void testAddAll3() {
521 try {
522 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
523 Integer[] ints = new Integer[SIZE];
524 for (int i = 0; i < SIZE-1; ++i)
525 ints[i] = new Integer(i);
526 q.addAll(Arrays.asList(ints));
527 shouldThrow();
528 } catch (NullPointerException success) {}
529 }
530 /**
531 * addAll throws ISE if not enough room
532 */
533 public void testAddAll4() {
534 try {
535 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
536 Integer[] ints = new Integer[SIZE];
537 for (int i = 0; i < SIZE; ++i)
538 ints[i] = new Integer(i);
539 q.addAll(Arrays.asList(ints));
540 shouldThrow();
541 } catch (IllegalStateException success) {}
542 }
543
544 /**
545 * Deque contains all elements, in traversal order, of successful addAll
546 */
547 public void testAddAll5() {
548 Integer[] empty = new Integer[0];
549 Integer[] ints = new Integer[SIZE];
550 for (int i = 0; i < SIZE; ++i)
551 ints[i] = new Integer(i);
552 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
553 assertFalse(q.addAll(Arrays.asList(empty)));
554 assertTrue(q.addAll(Arrays.asList(ints)));
555 for (int i = 0; i < SIZE; ++i)
556 assertEquals(ints[i], q.poll());
557 }
558
559
560 /**
561 * put(null) throws NPE
562 */
563 public void testPutNull() throws InterruptedException {
564 try {
565 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
566 q.put(null);
567 shouldThrow();
568 } catch (NullPointerException success) {}
569 }
570
571 /**
572 * all elements successfully put are contained
573 */
574 public void testPut() throws InterruptedException {
575 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
576 for (int i = 0; i < SIZE; ++i) {
577 Integer I = new Integer(i);
578 q.put(I);
579 assertTrue(q.contains(I));
580 }
581 assertEquals(0, q.remainingCapacity());
582 }
583
584 /**
585 * put blocks interruptibly if full
586 */
587 public void testBlockingPut() throws InterruptedException {
588 Thread t = new Thread(new CheckedRunnable() {
589 public void realRun() {
590 int added = 0;
591 try {
592 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
593 for (int i = 0; i < SIZE; ++i) {
594 q.put(new Integer(i));
595 ++added;
596 }
597 q.put(new Integer(SIZE));
598 threadShouldThrow();
599 } catch (InterruptedException success) {
600 threadAssertEquals(added, SIZE);
601 }
602 }});
603
604 t.start();
605 Thread.sleep(SHORT_DELAY_MS);
606 t.interrupt();
607 t.join();
608 }
609
610 /**
611 * put blocks waiting for take when full
612 */
613 public void testPutWithTake() throws InterruptedException {
614 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
615 Thread t = new Thread(new CheckedRunnable() {
616 public void realRun() {
617 int added = 0;
618 try {
619 q.put(new Object());
620 ++added;
621 q.put(new Object());
622 ++added;
623 q.put(new Object());
624 ++added;
625 q.put(new Object());
626 ++added;
627 threadShouldThrow();
628 } catch (InterruptedException success) {
629 threadAssertTrue(added >= 2);
630 }
631 }});
632
633 t.start();
634 Thread.sleep(SHORT_DELAY_MS);
635 q.take();
636 t.interrupt();
637 t.join();
638 }
639
640 /**
641 * timed offer times out if full and elements not taken
642 */
643 public void testTimedOffer() throws InterruptedException {
644 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
645 Thread t = new Thread(new CheckedRunnable() {
646 public void realRun() throws InterruptedException {
647 q.put(new Object());
648 q.put(new Object());
649 assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
650 try {
651 q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
652 shouldThrow();
653 } catch (InterruptedException success) {}
654 }});
655
656 t.start();
657 Thread.sleep(SMALL_DELAY_MS);
658 t.interrupt();
659 t.join();
660 }
661
662 /**
663 * take retrieves elements in FIFO order
664 */
665 public void testTake() throws InterruptedException {
666 LinkedBlockingDeque q = populatedDeque(SIZE);
667 for (int i = 0; i < SIZE; ++i) {
668 assertEquals(i, ((Integer)q.take()).intValue());
669 }
670 }
671
672 /**
673 * take blocks interruptibly when empty
674 */
675 public void testTakeFromEmpty() throws InterruptedException {
676 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
677 Thread t = new ThreadShouldThrow(InterruptedException.class) {
678 public void realRun() throws InterruptedException {
679 q.take();
680 }};
681
682 t.start();
683 Thread.sleep(SHORT_DELAY_MS);
684 t.interrupt();
685 t.join();
686 }
687
688 /**
689 * Take removes existing elements until empty, then blocks interruptibly
690 */
691 public void testBlockingTake() throws InterruptedException {
692 Thread t = new ThreadShouldThrow(InterruptedException.class) {
693 public void realRun() throws InterruptedException {
694 LinkedBlockingDeque q = populatedDeque(SIZE);
695 for (int i = 0; i < SIZE; ++i) {
696 assertEquals(i, ((Integer)q.take()).intValue());
697 }
698 q.take();
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, ((Integer)q.poll()).intValue());
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, ((Integer)q.poll(0, MILLISECONDS)).intValue());
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, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
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, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
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 Thread t = new Thread(new CheckedRunnable() {
817 public void realRun() {
818 int added = 0;
819 try {
820 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
821 for (int i = 0; i < SIZE; ++i) {
822 q.putFirst(new Integer(i));
823 ++added;
824 }
825 q.putFirst(new Integer(SIZE));
826 threadShouldThrow();
827 } catch (InterruptedException success) {
828 threadAssertEquals(added, SIZE);
829 }
830 }});
831
832 t.start();
833 Thread.sleep(SHORT_DELAY_MS);
834 t.interrupt();
835 t.join();
836 }
837
838 /**
839 * putFirst blocks waiting for take when full
840 */
841 public void testPutFirstWithTake() throws InterruptedException {
842 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
843 Thread t = new Thread(new CheckedRunnable() {
844 public void realRun() {
845 int added = 0;
846 try {
847 q.putFirst(new Object());
848 ++added;
849 q.putFirst(new Object());
850 ++added;
851 q.putFirst(new Object());
852 ++added;
853 q.putFirst(new Object());
854 ++added;
855 threadShouldThrow();
856 } catch (InterruptedException success) {
857 threadAssertTrue(added >= 2);
858 }
859 }});
860
861 t.start();
862 Thread.sleep(SHORT_DELAY_MS);
863 q.take();
864 t.interrupt();
865 t.join();
866 }
867
868 /**
869 * timed offerFirst times out if full and elements not taken
870 */
871 public void testTimedOfferFirst() throws InterruptedException {
872 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
873 Thread t = new Thread(new CheckedRunnable() {
874 public void realRun() throws InterruptedException {
875 q.putFirst(new Object());
876 q.putFirst(new Object());
877 assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
878 try {
879 q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
880 shouldThrow();
881 } catch (InterruptedException success) {}
882 }});
883
884 t.start();
885 Thread.sleep(SMALL_DELAY_MS);
886 t.interrupt();
887 t.join();
888 }
889
890 /**
891 * take retrieves elements in FIFO order
892 */
893 public void testTakeFirst() throws InterruptedException {
894 LinkedBlockingDeque q = populatedDeque(SIZE);
895 for (int i = 0; i < SIZE; ++i) {
896 assertEquals(i, ((Integer)q.takeFirst()).intValue());
897 }
898 }
899
900 /**
901 * takeFirst blocks interruptibly when empty
902 */
903 public void testTakeFirstFromEmpty() throws InterruptedException {
904 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
905 Thread t = new ThreadShouldThrow(InterruptedException.class) {
906 public void realRun() throws InterruptedException {
907 q.takeFirst();
908 }};
909
910 t.start();
911 Thread.sleep(SHORT_DELAY_MS);
912 t.interrupt();
913 t.join();
914 }
915
916 /**
917 * TakeFirst removes existing elements until empty, then blocks interruptibly
918 */
919 public void testBlockingTakeFirst() throws InterruptedException {
920 Thread t = new ThreadShouldThrow(InterruptedException.class) {
921 public void realRun() throws InterruptedException {
922 LinkedBlockingDeque q = populatedDeque(SIZE);
923 for (int i = 0; i < SIZE; ++i) {
924 assertEquals(i, ((Integer)q.takeFirst()).intValue());
925 }
926 q.takeFirst();
927 }};
928
929 t.start();
930 Thread.sleep(SHORT_DELAY_MS);
931 t.interrupt();
932 t.join();
933 }
934
935
936 /**
937 * timed pollFirst with zero timeout succeeds when non-empty, else times out
938 */
939 public void testTimedPollFirst0() throws InterruptedException {
940 LinkedBlockingDeque q = populatedDeque(SIZE);
941 for (int i = 0; i < SIZE; ++i) {
942 assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
943 }
944 assertNull(q.pollFirst(0, MILLISECONDS));
945 }
946
947 /**
948 * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
949 */
950 public void testTimedPollFirst() throws InterruptedException {
951 LinkedBlockingDeque q = populatedDeque(SIZE);
952 for (int i = 0; i < SIZE; ++i) {
953 assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
954 }
955 assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
956 }
957
958 /**
959 * Interrupted timed pollFirst throws InterruptedException instead of
960 * returning timeout status
961 */
962 public void testInterruptedTimedPollFirst() throws InterruptedException {
963 Thread t = new Thread(new CheckedRunnable() {
964 public void realRun() throws InterruptedException {
965 LinkedBlockingDeque q = populatedDeque(SIZE);
966 for (int i = 0; i < SIZE; ++i) {
967 assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
968 }
969 try {
970 q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
971 shouldThrow();
972 } catch (InterruptedException success) {}
973 }});
974
975 t.start();
976 Thread.sleep(SHORT_DELAY_MS);
977 t.interrupt();
978 t.join();
979 }
980
981 /**
982 * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
983 * on interruption throws
984 */
985 public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
986 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
987 Thread t = new Thread(new CheckedRunnable() {
988 public void realRun() throws InterruptedException {
989 assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
990 assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
991 try {
992 q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
993 shouldThrow();
994 } catch (InterruptedException success) {}
995 }});
996
997 t.start();
998 Thread.sleep(SMALL_DELAY_MS);
999 assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
1000 t.interrupt();
1001 t.join();
1002 }
1003
1004 /**
1005 * putLast(null) throws NPE
1006 */
1007 public void testPutLastNull() throws InterruptedException {
1008 try {
1009 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1010 q.putLast(null);
1011 shouldThrow();
1012 } catch (NullPointerException success) {}
1013 }
1014
1015 /**
1016 * all elements successfully putLast are contained
1017 */
1018 public void testPutLast() throws InterruptedException {
1019 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1020 for (int i = 0; i < SIZE; ++i) {
1021 Integer I = new Integer(i);
1022 q.putLast(I);
1023 assertTrue(q.contains(I));
1024 }
1025 assertEquals(0, q.remainingCapacity());
1026 }
1027
1028 /**
1029 * putLast blocks interruptibly if full
1030 */
1031 public void testBlockingPutLast() throws InterruptedException {
1032 Thread t = new Thread(new CheckedRunnable() {
1033 public void realRun() {
1034 int added = 0;
1035 try {
1036 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1037 for (int i = 0; i < SIZE; ++i) {
1038 q.putLast(new Integer(i));
1039 ++added;
1040 }
1041 q.putLast(new Integer(SIZE));
1042 threadShouldThrow();
1043 } catch (InterruptedException success) {
1044 threadAssertEquals(added, SIZE);
1045 }
1046 }});
1047
1048 t.start();
1049 Thread.sleep(SHORT_DELAY_MS);
1050 t.interrupt();
1051 t.join();
1052 }
1053
1054 /**
1055 * putLast blocks waiting for take when full
1056 */
1057 public void testPutLastWithTake() throws InterruptedException {
1058 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1059 Thread t = new Thread(new CheckedRunnable() {
1060 public void realRun() {
1061 int added = 0;
1062 try {
1063 q.putLast(new Object());
1064 ++added;
1065 q.putLast(new Object());
1066 ++added;
1067 q.putLast(new Object());
1068 ++added;
1069 q.putLast(new Object());
1070 ++added;
1071 threadShouldThrow();
1072 } catch (InterruptedException success) {
1073 threadAssertTrue(added >= 2);
1074 }
1075 }});
1076
1077 t.start();
1078 Thread.sleep(SHORT_DELAY_MS);
1079 q.take();
1080 t.interrupt();
1081 t.join();
1082 }
1083
1084 /**
1085 * timed offerLast times out if full and elements not taken
1086 */
1087 public void testTimedOfferLast() throws InterruptedException {
1088 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1089 Thread t = new Thread(new CheckedRunnable() {
1090 public void realRun() throws InterruptedException {
1091 q.putLast(new Object());
1092 q.putLast(new Object());
1093 assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1094 try {
1095 q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1096 shouldThrow();
1097 } catch (InterruptedException success) {}
1098 }});
1099
1100 t.start();
1101 Thread.sleep(SMALL_DELAY_MS);
1102 t.interrupt();
1103 t.join();
1104 }
1105
1106 /**
1107 * takeLast retrieves elements in FIFO order
1108 */
1109 public void testTakeLast() throws InterruptedException {
1110 LinkedBlockingDeque q = populatedDeque(SIZE);
1111 for (int i = 0; i < SIZE; ++i) {
1112 assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1113 }
1114 }
1115
1116 /**
1117 * takeLast blocks interruptibly when empty
1118 */
1119 public void testTakeLastFromEmpty() throws InterruptedException {
1120 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1121 Thread t = new ThreadShouldThrow(InterruptedException.class) {
1122 public void realRun() throws InterruptedException {
1123 q.takeLast();
1124 }};
1125
1126 t.start();
1127 Thread.sleep(SHORT_DELAY_MS);
1128 t.interrupt();
1129 t.join();
1130 }
1131
1132 /**
1133 * TakeLast removes existing elements until empty, then blocks interruptibly
1134 */
1135 public void testBlockingTakeLast() throws InterruptedException {
1136 Thread t = new ThreadShouldThrow(InterruptedException.class) {
1137 public void realRun() throws InterruptedException {
1138 LinkedBlockingDeque q = populatedDeque(SIZE);
1139 for (int i = 0; i < SIZE; ++i) {
1140 assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1141 }
1142 q.takeLast();
1143 }};
1144
1145 t.start();
1146 Thread.sleep(SHORT_DELAY_MS);
1147 t.interrupt();
1148 t.join();
1149 }
1150
1151
1152 /**
1153 * timed pollLast with zero timeout succeeds when non-empty, else times out
1154 */
1155 public void testTimedPollLast0() throws InterruptedException {
1156 LinkedBlockingDeque q = populatedDeque(SIZE);
1157 for (int i = 0; i < SIZE; ++i) {
1158 assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1159 }
1160 assertNull(q.pollLast(0, MILLISECONDS));
1161 }
1162
1163 /**
1164 * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1165 */
1166 public void testTimedPollLast() throws InterruptedException {
1167 LinkedBlockingDeque q = populatedDeque(SIZE);
1168 for (int i = 0; i < SIZE; ++i) {
1169 assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1170 }
1171 assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1172 }
1173
1174 /**
1175 * Interrupted timed pollLast throws InterruptedException instead of
1176 * returning timeout status
1177 */
1178 public void testInterruptedTimedPollLast() throws InterruptedException {
1179 Thread t = new Thread(new CheckedRunnable() {
1180 public void realRun() throws InterruptedException {
1181 LinkedBlockingDeque q = populatedDeque(SIZE);
1182 for (int i = 0; i < SIZE; ++i) {
1183 assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1184 }
1185 try {
1186 q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1187 shouldThrow();
1188 } catch (InterruptedException success) {}
1189 }});
1190
1191 t.start();
1192 Thread.sleep(SHORT_DELAY_MS);
1193 t.interrupt();
1194 t.join();
1195 }
1196
1197 /**
1198 * timed poll before a delayed offerLast fails; after offerLast succeeds;
1199 * on interruption throws
1200 */
1201 public void testTimedPollWithOfferLast() throws InterruptedException {
1202 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1203 Thread t = new Thread(new CheckedRunnable() {
1204 public void realRun() throws InterruptedException {
1205 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1206 assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1207 try {
1208 q.poll(LONG_DELAY_MS, MILLISECONDS);
1209 shouldThrow();
1210 } catch (InterruptedException success) {}
1211 }});
1212
1213 t.start();
1214 Thread.sleep(SMALL_DELAY_MS);
1215 assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1216 t.interrupt();
1217 t.join();
1218 }
1219
1220
1221 /**
1222 * element returns next element, or throws NSEE if empty
1223 */
1224 public void testElement() {
1225 LinkedBlockingDeque q = populatedDeque(SIZE);
1226 for (int i = 0; i < SIZE; ++i) {
1227 assertEquals(i, ((Integer)q.element()).intValue());
1228 q.poll();
1229 }
1230 try {
1231 q.element();
1232 shouldThrow();
1233 } catch (NoSuchElementException success) {}
1234 }
1235
1236 /**
1237 * remove(x) removes x and returns true if present
1238 */
1239 public void testRemoveElement() {
1240 LinkedBlockingDeque q = populatedDeque(SIZE);
1241 for (int i = 1; i < SIZE; i+=2) {
1242 assertTrue(q.remove(new Integer(i)));
1243 }
1244 for (int i = 0; i < SIZE; i+=2) {
1245 assertTrue(q.remove(new Integer(i)));
1246 assertFalse(q.remove(new Integer(i+1)));
1247 }
1248 assertTrue(q.isEmpty());
1249 }
1250
1251 /**
1252 * contains(x) reports true when elements added but not yet removed
1253 */
1254 public void testContains() {
1255 LinkedBlockingDeque q = populatedDeque(SIZE);
1256 for (int i = 0; i < SIZE; ++i) {
1257 assertTrue(q.contains(new Integer(i)));
1258 q.poll();
1259 assertFalse(q.contains(new Integer(i)));
1260 }
1261 }
1262
1263 /**
1264 * clear removes all elements
1265 */
1266 public void testClear() {
1267 LinkedBlockingDeque q = populatedDeque(SIZE);
1268 q.clear();
1269 assertTrue(q.isEmpty());
1270 assertEquals(0, q.size());
1271 assertEquals(SIZE, q.remainingCapacity());
1272 q.add(one);
1273 assertFalse(q.isEmpty());
1274 assertTrue(q.contains(one));
1275 q.clear();
1276 assertTrue(q.isEmpty());
1277 }
1278
1279 /**
1280 * containsAll(c) is true when c contains a subset of elements
1281 */
1282 public void testContainsAll() {
1283 LinkedBlockingDeque q = populatedDeque(SIZE);
1284 LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE);
1285 for (int i = 0; i < SIZE; ++i) {
1286 assertTrue(q.containsAll(p));
1287 assertFalse(p.containsAll(q));
1288 p.add(new Integer(i));
1289 }
1290 assertTrue(p.containsAll(q));
1291 }
1292
1293 /**
1294 * retainAll(c) retains only those elements of c and reports true if changed
1295 */
1296 public void testRetainAll() {
1297 LinkedBlockingDeque q = populatedDeque(SIZE);
1298 LinkedBlockingDeque p = populatedDeque(SIZE);
1299 for (int i = 0; i < SIZE; ++i) {
1300 boolean changed = q.retainAll(p);
1301 if (i == 0)
1302 assertFalse(changed);
1303 else
1304 assertTrue(changed);
1305
1306 assertTrue(q.containsAll(p));
1307 assertEquals(SIZE-i, q.size());
1308 p.remove();
1309 }
1310 }
1311
1312 /**
1313 * removeAll(c) removes only those elements of c and reports true if changed
1314 */
1315 public void testRemoveAll() {
1316 for (int i = 1; i < SIZE; ++i) {
1317 LinkedBlockingDeque q = populatedDeque(SIZE);
1318 LinkedBlockingDeque p = populatedDeque(i);
1319 assertTrue(q.removeAll(p));
1320 assertEquals(SIZE-i, q.size());
1321 for (int j = 0; j < i; ++j) {
1322 Integer I = (Integer)(p.remove());
1323 assertFalse(q.contains(I));
1324 }
1325 }
1326 }
1327
1328 /**
1329 * toArray contains all elements
1330 */
1331 public void testToArray() throws InterruptedException{
1332 LinkedBlockingDeque q = populatedDeque(SIZE);
1333 Object[] o = q.toArray();
1334 for (int i = 0; i < o.length; i++)
1335 assertEquals(o[i], q.take());
1336 }
1337
1338 /**
1339 * toArray(a) contains all elements
1340 */
1341 public void testToArray2() throws InterruptedException {
1342 LinkedBlockingDeque q = populatedDeque(SIZE);
1343 Integer[] ints = new Integer[SIZE];
1344 ints = (Integer[])q.toArray(ints);
1345 for (int i = 0; i < ints.length; i++)
1346 assertEquals(ints[i], q.take());
1347 }
1348
1349 /**
1350 * toArray(null) throws NPE
1351 */
1352 public void testToArray_BadArg() {
1353 try {
1354 LinkedBlockingDeque q = populatedDeque(SIZE);
1355 Object o[] = q.toArray(null);
1356 shouldThrow();
1357 } catch (NullPointerException success) {}
1358 }
1359
1360 /**
1361 * toArray with incompatible array type throws CCE
1362 */
1363 public void testToArray1_BadArg() {
1364 try {
1365 LinkedBlockingDeque q = populatedDeque(SIZE);
1366 Object o[] = q.toArray(new String[10] );
1367 shouldThrow();
1368 } catch (ArrayStoreException success) {}
1369 }
1370
1371
1372 /**
1373 * iterator iterates through all elements
1374 */
1375 public void testIterator() throws InterruptedException {
1376 LinkedBlockingDeque q = populatedDeque(SIZE);
1377 Iterator it = q.iterator();
1378 while (it.hasNext()) {
1379 assertEquals(it.next(), q.take());
1380 }
1381 }
1382
1383 /**
1384 * iterator.remove removes current element
1385 */
1386 public void testIteratorRemove () {
1387 final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1388 q.add(two);
1389 q.add(one);
1390 q.add(three);
1391
1392 Iterator it = q.iterator();
1393 it.next();
1394 it.remove();
1395
1396 it = q.iterator();
1397 assertEquals(it.next(), one);
1398 assertEquals(it.next(), three);
1399 assertFalse(it.hasNext());
1400 }
1401
1402
1403 /**
1404 * iterator ordering is FIFO
1405 */
1406 public void testIteratorOrdering() {
1407 final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1408 q.add(one);
1409 q.add(two);
1410 q.add(three);
1411 assertEquals(0, q.remainingCapacity());
1412 int k = 0;
1413 for (Iterator it = q.iterator(); it.hasNext();) {
1414 int i = ((Integer)(it.next())).intValue();
1415 assertEquals(++k, i);
1416 }
1417 assertEquals(3, k);
1418 }
1419
1420 /**
1421 * Modifications do not cause iterators to fail
1422 */
1423 public void testWeaklyConsistentIteration () {
1424 final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1425 q.add(one);
1426 q.add(two);
1427 q.add(three);
1428 for (Iterator it = q.iterator(); it.hasNext();) {
1429 q.remove();
1430 it.next();
1431 }
1432 assertEquals(0, q.size());
1433 }
1434
1435
1436 /**
1437 * Descending iterator iterates through all elements
1438 */
1439 public void testDescendingIterator() {
1440 LinkedBlockingDeque q = populatedDeque(SIZE);
1441 int i = 0;
1442 Iterator it = q.descendingIterator();
1443 while (it.hasNext()) {
1444 assertTrue(q.contains(it.next()));
1445 ++i;
1446 }
1447 assertEquals(i, SIZE);
1448 assertFalse(it.hasNext());
1449 try {
1450 it.next();
1451 shouldThrow();
1452 } catch (NoSuchElementException success) {}
1453 }
1454
1455 /**
1456 * Descending iterator ordering is reverse FIFO
1457 */
1458 public void testDescendingIteratorOrdering() {
1459 final LinkedBlockingDeque q = new LinkedBlockingDeque();
1460 for (int iters = 0; iters < 100; ++iters) {
1461 q.add(new Integer(3));
1462 q.add(new Integer(2));
1463 q.add(new Integer(1));
1464 int k = 0;
1465 for (Iterator it = q.descendingIterator(); it.hasNext();) {
1466 int i = ((Integer)(it.next())).intValue();
1467 assertEquals(++k, i);
1468 }
1469
1470 assertEquals(3, k);
1471 q.remove();
1472 q.remove();
1473 q.remove();
1474 }
1475 }
1476
1477 /**
1478 * descendingIterator.remove removes current element
1479 */
1480 public void testDescendingIteratorRemove () {
1481 final LinkedBlockingDeque q = new LinkedBlockingDeque();
1482 for (int iters = 0; iters < 100; ++iters) {
1483 q.add(new Integer(3));
1484 q.add(new Integer(2));
1485 q.add(new Integer(1));
1486 Iterator it = q.descendingIterator();
1487 assertEquals(it.next(), new Integer(1));
1488 it.remove();
1489 assertEquals(it.next(), new Integer(2));
1490 it = q.descendingIterator();
1491 assertEquals(it.next(), new Integer(2));
1492 assertEquals(it.next(), new Integer(3));
1493 it.remove();
1494 assertFalse(it.hasNext());
1495 q.remove();
1496 }
1497 }
1498
1499
1500 /**
1501 * toString contains toStrings of elements
1502 */
1503 public void testToString() {
1504 LinkedBlockingDeque q = populatedDeque(SIZE);
1505 String s = q.toString();
1506 for (int i = 0; i < SIZE; ++i) {
1507 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1508 }
1509 }
1510
1511
1512 /**
1513 * offer transfers elements across Executor tasks
1514 */
1515 public void testOfferInExecutor() {
1516 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1517 q.add(one);
1518 q.add(two);
1519 ExecutorService executor = Executors.newFixedThreadPool(2);
1520 executor.execute(new CheckedRunnable() {
1521 public void realRun() throws InterruptedException {
1522 threadAssertFalse(q.offer(three));
1523 threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1524 threadAssertEquals(0, q.remainingCapacity());
1525 }});
1526
1527 executor.execute(new CheckedRunnable() {
1528 public void realRun() throws InterruptedException {
1529 Thread.sleep(SMALL_DELAY_MS);
1530 threadAssertEquals(one, q.take());
1531 }});
1532
1533 joinPool(executor);
1534 }
1535
1536 /**
1537 * poll retrieves elements across Executor threads
1538 */
1539 public void testPollInExecutor() {
1540 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1541 ExecutorService executor = Executors.newFixedThreadPool(2);
1542 executor.execute(new CheckedRunnable() {
1543 public void realRun() throws InterruptedException {
1544 threadAssertNull(q.poll());
1545 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1546 threadAssertTrue(q.isEmpty());
1547 }});
1548
1549 executor.execute(new CheckedRunnable() {
1550 public void realRun() throws InterruptedException {
1551 Thread.sleep(SMALL_DELAY_MS);
1552 q.put(one);
1553 }});
1554
1555 joinPool(executor);
1556 }
1557
1558 /**
1559 * A deserialized serialized deque has same elements in same order
1560 */
1561 public void testSerialization() throws Exception {
1562 LinkedBlockingDeque q = populatedDeque(SIZE);
1563
1564 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1565 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1566 out.writeObject(q);
1567 out.close();
1568
1569 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1570 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1571 LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1572 assertEquals(q.size(), r.size());
1573 while (!q.isEmpty())
1574 assertEquals(q.remove(), r.remove());
1575 }
1576
1577 /**
1578 * drainTo(null) throws NPE
1579 */
1580 public void testDrainToNull() {
1581 LinkedBlockingDeque q = populatedDeque(SIZE);
1582 try {
1583 q.drainTo(null);
1584 shouldThrow();
1585 } catch (NullPointerException success) {}
1586 }
1587
1588 /**
1589 * drainTo(this) throws IAE
1590 */
1591 public void testDrainToSelf() {
1592 LinkedBlockingDeque q = populatedDeque(SIZE);
1593 try {
1594 q.drainTo(q);
1595 shouldThrow();
1596 } catch (IllegalArgumentException success) {}
1597 }
1598
1599 /**
1600 * drainTo(c) empties deque into another collection c
1601 */
1602 public void testDrainTo() {
1603 LinkedBlockingDeque q = populatedDeque(SIZE);
1604 ArrayList l = new ArrayList();
1605 q.drainTo(l);
1606 assertEquals(q.size(), 0);
1607 assertEquals(l.size(), SIZE);
1608 for (int i = 0; i < SIZE; ++i)
1609 assertEquals(l.get(i), new Integer(i));
1610 q.add(zero);
1611 q.add(one);
1612 assertFalse(q.isEmpty());
1613 assertTrue(q.contains(zero));
1614 assertTrue(q.contains(one));
1615 l.clear();
1616 q.drainTo(l);
1617 assertEquals(q.size(), 0);
1618 assertEquals(l.size(), 2);
1619 for (int i = 0; i < 2; ++i)
1620 assertEquals(l.get(i), new Integer(i));
1621 }
1622
1623 /**
1624 * drainTo empties full deque, unblocking a waiting put.
1625 */
1626 public void testDrainToWithActivePut() throws InterruptedException {
1627 final LinkedBlockingDeque q = populatedDeque(SIZE);
1628 Thread t = new Thread(new CheckedRunnable() {
1629 public void realRun() throws InterruptedException {
1630 q.put(new Integer(SIZE+1));
1631 }});
1632
1633 t.start();
1634 ArrayList l = new ArrayList();
1635 q.drainTo(l);
1636 assertTrue(l.size() >= SIZE);
1637 for (int i = 0; i < SIZE; ++i)
1638 assertEquals(l.get(i), new Integer(i));
1639 t.join();
1640 assertTrue(q.size() + l.size() >= SIZE);
1641 }
1642
1643 /**
1644 * drainTo(null, n) throws NPE
1645 */
1646 public void testDrainToNullN() {
1647 LinkedBlockingDeque q = populatedDeque(SIZE);
1648 try {
1649 q.drainTo(null, 0);
1650 shouldThrow();
1651 } catch (NullPointerException success) {}
1652 }
1653
1654 /**
1655 * drainTo(this, n) throws IAE
1656 */
1657 public void testDrainToSelfN() {
1658 LinkedBlockingDeque q = populatedDeque(SIZE);
1659 try {
1660 q.drainTo(q, 0);
1661 shouldThrow();
1662 } catch (IllegalArgumentException success) {}
1663 }
1664
1665 /**
1666 * drainTo(c, n) empties first max {n, size} elements of deque into c
1667 */
1668 public void testDrainToN() {
1669 LinkedBlockingDeque q = new LinkedBlockingDeque();
1670 for (int i = 0; i < SIZE + 2; ++i) {
1671 for (int j = 0; j < SIZE; j++)
1672 assertTrue(q.offer(new Integer(j)));
1673 ArrayList l = new ArrayList();
1674 q.drainTo(l, i);
1675 int k = (i < SIZE)? i : SIZE;
1676 assertEquals(l.size(), k);
1677 assertEquals(q.size(), SIZE-k);
1678 for (int j = 0; j < k; ++j)
1679 assertEquals(l.get(j), new Integer(j));
1680 while (q.poll() != null) ;
1681 }
1682 }
1683
1684 }