ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.9
Committed: Sat Nov 21 08:37:39 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.8: +396 -664 lines
Log Message:
improve exception handling

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