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

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