ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
Revision: 1.6
Committed: Sun Oct 5 23:00:40 2003 UTC (20 years, 6 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.5: +152 -0 lines
Log Message:
Added tests and documentation

File Contents

# Content
1 /*
2 * Written by members of JCP JSR-166 Expert Group and released to the
3 * public domain. Use, modify, and redistribute this code in any way
4 * without acknowledgement. Other contributors include Andrew Wright,
5 * Jeffrey Hayes, Pat Fischer, Mike Judd.
6 */
7
8 import junit.framework.*;
9 import java.util.*;
10 import java.util.concurrent.*;
11 import java.io.*;
12
13 public class LinkedBlockingQueueTest extends JSR166TestCase {
14
15 public static void main(String[] args) {
16 junit.textui.TestRunner.run (suite());
17 }
18
19 public static Test suite() {
20 return new TestSuite(LinkedBlockingQueueTest.class);
21 }
22
23
24 /**
25 * Create a queue of given size containing consecutive
26 * Integers 0 ... n.
27 */
28 private LinkedBlockingQueue populatedQueue(int n) {
29 LinkedBlockingQueue q = new LinkedBlockingQueue(n);
30 assertTrue(q.isEmpty());
31 for(int i = 0; i < n; i++)
32 assertTrue(q.offer(new Integer(i)));
33 assertFalse(q.isEmpty());
34 assertEquals(0, q.remainingCapacity());
35 assertEquals(n, q.size());
36 return q;
37 }
38
39 /**
40 * A new queue has the indicated capacity, or Integer.MAX_VALUE if
41 * none given
42 */
43 public void testConstructor1() {
44 assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity());
45 assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity());
46 }
47
48 /**
49 * Constructor throws IAE if capacity argument nonpositive
50 */
51 public void testConstructor2() {
52 try {
53 LinkedBlockingQueue q = new LinkedBlockingQueue(0);
54 shouldThrow();
55 }
56 catch (IllegalArgumentException success) {}
57 }
58
59 /**
60 * Initializing from null Collection throws NPE
61 */
62 public void testConstructor3() {
63 try {
64 LinkedBlockingQueue q = new LinkedBlockingQueue(null);
65 shouldThrow();
66 }
67 catch (NullPointerException success) {}
68 }
69
70 /**
71 * Initializing from Collection of null elements throws NPE
72 */
73 public void testConstructor4() {
74 try {
75 Integer[] ints = new Integer[SIZE];
76 LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
77 shouldThrow();
78 }
79 catch (NullPointerException success) {}
80 }
81
82 /**
83 * Initializing from Collection with some null elements throws NPE
84 */
85 public void testConstructor5() {
86 try {
87 Integer[] ints = new Integer[SIZE];
88 for (int i = 0; i < SIZE-1; ++i)
89 ints[i] = new Integer(i);
90 LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
91 shouldThrow();
92 }
93 catch (NullPointerException success) {}
94 }
95
96 /**
97 * Queue contains all elements of collection used to initialize
98 */
99 public void testConstructor6() {
100 try {
101 Integer[] ints = new Integer[SIZE];
102 for (int i = 0; i < SIZE; ++i)
103 ints[i] = new Integer(i);
104 LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
105 for (int i = 0; i < SIZE; ++i)
106 assertEquals(ints[i], q.poll());
107 }
108 finally {}
109 }
110
111 /**
112 * Queue transitions from empty to full when elements added
113 */
114 public void testEmptyFull() {
115 LinkedBlockingQueue q = new LinkedBlockingQueue(2);
116 assertTrue(q.isEmpty());
117 assertEquals("should have room for 2", 2, q.remainingCapacity());
118 q.add(one);
119 assertFalse(q.isEmpty());
120 q.add(two);
121 assertFalse(q.isEmpty());
122 assertEquals(0, q.remainingCapacity());
123 assertFalse(q.offer(three));
124 }
125
126 /**
127 * remainingCapacity decreases on add, increases on remove
128 */
129 public void testRemainingCapacity() {
130 LinkedBlockingQueue q = populatedQueue(SIZE);
131 for (int i = 0; i < SIZE; ++i) {
132 assertEquals(i, q.remainingCapacity());
133 assertEquals(SIZE-i, q.size());
134 q.remove();
135 }
136 for (int i = 0; i < SIZE; ++i) {
137 assertEquals(SIZE-i, q.remainingCapacity());
138 assertEquals(i, q.size());
139 q.add(new Integer(i));
140 }
141 }
142
143 /**
144 * offer(null) throws NPE
145 */
146 public void testOfferNull() {
147 try {
148 LinkedBlockingQueue q = new LinkedBlockingQueue(1);
149 q.offer(null);
150 shouldThrow();
151 } catch (NullPointerException success) { }
152 }
153
154 /**
155 * add(null) throws NPE
156 */
157 public void testAddNull() {
158 try {
159 LinkedBlockingQueue q = new LinkedBlockingQueue(1);
160 q.add(null);
161 shouldThrow();
162 } catch (NullPointerException success) { }
163 }
164
165 /**
166 * Offer succeeds if not full; fails if full
167 */
168 public void testOffer() {
169 LinkedBlockingQueue q = new LinkedBlockingQueue(1);
170 assertTrue(q.offer(zero));
171 assertFalse(q.offer(one));
172 }
173
174 /**
175 * add succeeds if not full; throws ISE if full
176 */
177 public void testAdd() {
178 try {
179 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
180 for (int i = 0; i < SIZE; ++i) {
181 assertTrue(q.add(new Integer(i)));
182 }
183 assertEquals(0, q.remainingCapacity());
184 q.add(new Integer(SIZE));
185 } catch (IllegalStateException success){
186 }
187 }
188
189 /**
190 * addAll(null) throws NPE
191 */
192 public void testAddAll1() {
193 try {
194 LinkedBlockingQueue q = new LinkedBlockingQueue(1);
195 q.addAll(null);
196 shouldThrow();
197 }
198 catch (NullPointerException success) {}
199 }
200
201 /**
202 * addAll(this) throws IAE
203 */
204 public void testAddAllSelf() {
205 try {
206 LinkedBlockingQueue q = populatedQueue(SIZE);
207 q.addAll(q);
208 shouldThrow();
209 }
210 catch (IllegalArgumentException success) {}
211 }
212
213 /**
214 * addAll of a collection with null elements throws NPE
215 */
216 public void testAddAll2() {
217 try {
218 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
219 Integer[] ints = new Integer[SIZE];
220 q.addAll(Arrays.asList(ints));
221 shouldThrow();
222 }
223 catch (NullPointerException success) {}
224 }
225 /**
226 * addAll of a collection with any null elements throws NPE after
227 * possibly adding some elements
228 */
229 public void testAddAll3() {
230 try {
231 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
232 Integer[] ints = new Integer[SIZE];
233 for (int i = 0; i < SIZE-1; ++i)
234 ints[i] = new Integer(i);
235 q.addAll(Arrays.asList(ints));
236 shouldThrow();
237 }
238 catch (NullPointerException success) {}
239 }
240 /**
241 * addAll throws ISE if not enough room
242 */
243 public void testAddAll4() {
244 try {
245 LinkedBlockingQueue q = new LinkedBlockingQueue(1);
246 Integer[] ints = new Integer[SIZE];
247 for (int i = 0; i < SIZE; ++i)
248 ints[i] = new Integer(i);
249 q.addAll(Arrays.asList(ints));
250 shouldThrow();
251 }
252 catch (IllegalStateException success) {}
253 }
254 /**
255 * Queue contains all elements, in traversal order, of successful addAll
256 */
257 public void testAddAll5() {
258 try {
259 Integer[] empty = new Integer[0];
260 Integer[] ints = new Integer[SIZE];
261 for (int i = 0; i < SIZE; ++i)
262 ints[i] = new Integer(i);
263 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
264 assertFalse(q.addAll(Arrays.asList(empty)));
265 assertTrue(q.addAll(Arrays.asList(ints)));
266 for (int i = 0; i < SIZE; ++i)
267 assertEquals(ints[i], q.poll());
268 }
269 finally {}
270 }
271
272 /**
273 * put(null) throws NPE
274 */
275 public void testPutNull() {
276 try {
277 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
278 q.put(null);
279 shouldThrow();
280 }
281 catch (NullPointerException success){
282 }
283 catch (InterruptedException ie) {
284 unexpectedException();
285 }
286 }
287
288 /**
289 * all elements successfully put are contained
290 */
291 public void testPut() {
292 try {
293 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
294 for (int i = 0; i < SIZE; ++i) {
295 Integer I = new Integer(i);
296 q.put(I);
297 assertTrue(q.contains(I));
298 }
299 assertEquals(0, q.remainingCapacity());
300 }
301 catch (InterruptedException ie) {
302 unexpectedException();
303 }
304 }
305
306 /**
307 * put blocks interruptibly if full
308 */
309 public void testBlockingPut() {
310 Thread t = new Thread(new Runnable() {
311 public void run() {
312 int added = 0;
313 try {
314 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
315 for (int i = 0; i < SIZE; ++i) {
316 q.put(new Integer(i));
317 ++added;
318 }
319 q.put(new Integer(SIZE));
320 threadShouldThrow();
321 } catch (InterruptedException ie){
322 threadAssertEquals(added, SIZE);
323 }
324 }});
325 t.start();
326 try {
327 Thread.sleep(SHORT_DELAY_MS);
328 t.interrupt();
329 t.join();
330 }
331 catch (InterruptedException ie) {
332 unexpectedException();
333 }
334 }
335
336 /**
337 * put blocks waiting for take when full
338 */
339 public void testPutWithTake() {
340 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
341 Thread t = new Thread(new Runnable() {
342 public void run() {
343 int added = 0;
344 try {
345 q.put(new Object());
346 ++added;
347 q.put(new Object());
348 ++added;
349 q.put(new Object());
350 ++added;
351 q.put(new Object());
352 ++added;
353 threadShouldThrow();
354 } catch (InterruptedException e){
355 threadAssertTrue(added >= 2);
356 }
357 }
358 });
359 try {
360 t.start();
361 Thread.sleep(SHORT_DELAY_MS);
362 q.take();
363 t.interrupt();
364 t.join();
365 } catch (Exception e){
366 unexpectedException();
367 }
368 }
369
370 /**
371 * timed offer times out if full and elements not taken
372 */
373 public void testTimedOffer() {
374 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
375 Thread t = new Thread(new Runnable() {
376 public void run() {
377 try {
378 q.put(new Object());
379 q.put(new Object());
380 threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
381 q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
382 threadShouldThrow();
383 } catch (InterruptedException success){}
384 }
385 });
386
387 try {
388 t.start();
389 Thread.sleep(SMALL_DELAY_MS);
390 t.interrupt();
391 t.join();
392 } catch (Exception e){
393 unexpectedException();
394 }
395 }
396
397 /**
398 * take retrieves elements in FIFO order
399 */
400 public void testTake() {
401 try {
402 LinkedBlockingQueue q = populatedQueue(SIZE);
403 for (int i = 0; i < SIZE; ++i) {
404 assertEquals(i, ((Integer)q.take()).intValue());
405 }
406 } catch (InterruptedException e){
407 unexpectedException();
408 }
409 }
410
411 /**
412 * take blocks interruptibly when empty
413 */
414 public void testTakeFromEmpty() {
415 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
416 Thread t = new Thread(new Runnable() {
417 public void run() {
418 try {
419 q.take();
420 threadShouldThrow();
421 } catch (InterruptedException success){ }
422 }
423 });
424 try {
425 t.start();
426 Thread.sleep(SHORT_DELAY_MS);
427 t.interrupt();
428 t.join();
429 } catch (Exception e){
430 unexpectedException();
431 }
432 }
433
434 /**
435 * Take removes existing elements until empty, then blocks interruptibly
436 */
437 public void testBlockingTake() {
438 Thread t = new Thread(new Runnable() {
439 public void run() {
440 try {
441 LinkedBlockingQueue q = populatedQueue(SIZE);
442 for (int i = 0; i < SIZE; ++i) {
443 assertEquals(i, ((Integer)q.take()).intValue());
444 }
445 q.take();
446 threadShouldThrow();
447 } catch (InterruptedException success){
448 }
449 }});
450 t.start();
451 try {
452 Thread.sleep(SHORT_DELAY_MS);
453 t.interrupt();
454 t.join();
455 }
456 catch (InterruptedException ie) {
457 unexpectedException();
458 }
459 }
460
461
462 /**
463 * poll succeeds unless empty
464 */
465 public void testPoll() {
466 LinkedBlockingQueue q = populatedQueue(SIZE);
467 for (int i = 0; i < SIZE; ++i) {
468 assertEquals(i, ((Integer)q.poll()).intValue());
469 }
470 assertNull(q.poll());
471 }
472
473 /**
474 * timed pool with zero timeout succeeds when non-empty, else times out
475 */
476 public void testTimedPoll0() {
477 try {
478 LinkedBlockingQueue q = populatedQueue(SIZE);
479 for (int i = 0; i < SIZE; ++i) {
480 assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
481 }
482 assertNull(q.poll(0, TimeUnit.MILLISECONDS));
483 } catch (InterruptedException e){
484 unexpectedException();
485 }
486 }
487
488 /**
489 * timed pool with nonzero timeout succeeds when non-empty, else times out
490 */
491 public void testTimedPoll() {
492 try {
493 LinkedBlockingQueue q = populatedQueue(SIZE);
494 for (int i = 0; i < SIZE; ++i) {
495 assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
496 }
497 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
498 } catch (InterruptedException e){
499 unexpectedException();
500 }
501 }
502
503 /**
504 * Interrupted timed poll throws InterruptedException instead of
505 * returning timeout status
506 */
507 public void testInterruptedTimedPoll() {
508 Thread t = new Thread(new Runnable() {
509 public void run() {
510 try {
511 LinkedBlockingQueue q = populatedQueue(SIZE);
512 for (int i = 0; i < SIZE; ++i) {
513 threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
514 }
515 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
516 } catch (InterruptedException success){
517 }
518 }});
519 t.start();
520 try {
521 Thread.sleep(SHORT_DELAY_MS);
522 t.interrupt();
523 t.join();
524 }
525 catch (InterruptedException ie) {
526 unexpectedException();
527 }
528 }
529
530 /**
531 * timed poll before a delayed offer fails; after offer succeeds;
532 * on interruption throws
533 */
534 public void testTimedPollWithOffer() {
535 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
536 Thread t = new Thread(new Runnable() {
537 public void run() {
538 try {
539 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
540 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
541 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
542 threadShouldThrow();
543 } catch (InterruptedException success) { }
544 }
545 });
546 try {
547 t.start();
548 Thread.sleep(SMALL_DELAY_MS);
549 assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
550 t.interrupt();
551 t.join();
552 } catch (Exception e){
553 unexpectedException();
554 }
555 }
556
557 /**
558 * peek returns next element, or null if empty
559 */
560 public void testPeek() {
561 LinkedBlockingQueue q = populatedQueue(SIZE);
562 for (int i = 0; i < SIZE; ++i) {
563 assertEquals(i, ((Integer)q.peek()).intValue());
564 q.poll();
565 assertTrue(q.peek() == null ||
566 i != ((Integer)q.peek()).intValue());
567 }
568 assertNull(q.peek());
569 }
570
571 /**
572 * element returns next element, or throws NSEE if empty
573 */
574 public void testElement() {
575 LinkedBlockingQueue q = populatedQueue(SIZE);
576 for (int i = 0; i < SIZE; ++i) {
577 assertEquals(i, ((Integer)q.element()).intValue());
578 q.poll();
579 }
580 try {
581 q.element();
582 shouldThrow();
583 }
584 catch (NoSuchElementException success) {}
585 }
586
587 /**
588 * remove removes next element, or throws NSEE if empty
589 */
590 public void testRemove() {
591 LinkedBlockingQueue q = populatedQueue(SIZE);
592 for (int i = 0; i < SIZE; ++i) {
593 assertEquals(i, ((Integer)q.remove()).intValue());
594 }
595 try {
596 q.remove();
597 shouldThrow();
598 } catch (NoSuchElementException success){
599 }
600 }
601
602 /**
603 * remove(x) removes x and returns true if present
604 */
605 public void testRemoveElement() {
606 LinkedBlockingQueue q = populatedQueue(SIZE);
607 for (int i = 1; i < SIZE; i+=2) {
608 assertTrue(q.remove(new Integer(i)));
609 }
610 for (int i = 0; i < SIZE; i+=2) {
611 assertTrue(q.remove(new Integer(i)));
612 assertFalse(q.remove(new Integer(i+1)));
613 }
614 assertTrue(q.isEmpty());
615 }
616
617 /**
618 * contains(x) reports true when elements added but not yet removed
619 */
620 public void testContains() {
621 LinkedBlockingQueue q = populatedQueue(SIZE);
622 for (int i = 0; i < SIZE; ++i) {
623 assertTrue(q.contains(new Integer(i)));
624 q.poll();
625 assertFalse(q.contains(new Integer(i)));
626 }
627 }
628
629 /**
630 * clear removes all elements
631 */
632 public void testClear() {
633 LinkedBlockingQueue q = populatedQueue(SIZE);
634 q.clear();
635 assertTrue(q.isEmpty());
636 assertEquals(0, q.size());
637 assertEquals(SIZE, q.remainingCapacity());
638 q.add(one);
639 assertFalse(q.isEmpty());
640 q.clear();
641 assertTrue(q.isEmpty());
642 }
643
644 /**
645 * containsAll(c) is true when c contains a subset of elements
646 */
647 public void testContainsAll() {
648 LinkedBlockingQueue q = populatedQueue(SIZE);
649 LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
650 for (int i = 0; i < SIZE; ++i) {
651 assertTrue(q.containsAll(p));
652 assertFalse(p.containsAll(q));
653 p.add(new Integer(i));
654 }
655 assertTrue(p.containsAll(q));
656 }
657
658 /**
659 * retainAll(c) retains only those elements of c and reports true if changed
660 */
661 public void testRetainAll() {
662 LinkedBlockingQueue q = populatedQueue(SIZE);
663 LinkedBlockingQueue p = populatedQueue(SIZE);
664 for (int i = 0; i < SIZE; ++i) {
665 boolean changed = q.retainAll(p);
666 if (i == 0)
667 assertFalse(changed);
668 else
669 assertTrue(changed);
670
671 assertTrue(q.containsAll(p));
672 assertEquals(SIZE-i, q.size());
673 p.remove();
674 }
675 }
676
677 /**
678 * removeAll(c) removes only those elements of c and reports true if changed
679 */
680 public void testRemoveAll() {
681 for (int i = 1; i < SIZE; ++i) {
682 LinkedBlockingQueue q = populatedQueue(SIZE);
683 LinkedBlockingQueue p = populatedQueue(i);
684 assertTrue(q.removeAll(p));
685 assertEquals(SIZE-i, q.size());
686 for (int j = 0; j < i; ++j) {
687 Integer I = (Integer)(p.remove());
688 assertFalse(q.contains(I));
689 }
690 }
691 }
692
693 /**
694 * toArray contains all elements
695 */
696 public void testToArray() {
697 LinkedBlockingQueue q = populatedQueue(SIZE);
698 Object[] o = q.toArray();
699 try {
700 for(int i = 0; i < o.length; i++)
701 assertEquals(o[i], q.take());
702 } catch (InterruptedException e){
703 unexpectedException();
704 }
705 }
706
707 /**
708 * toArray(a) contains all elements
709 */
710 public void testToArray2() {
711 LinkedBlockingQueue q = populatedQueue(SIZE);
712 Integer[] ints = new Integer[SIZE];
713 ints = (Integer[])q.toArray(ints);
714 try {
715 for(int i = 0; i < ints.length; i++)
716 assertEquals(ints[i], q.take());
717 } catch (InterruptedException e){
718 unexpectedException();
719 }
720 }
721
722 /**
723 * toArray(null) throws NPE
724 */
725 public void testToArray_BadArg() {
726 try {
727 LinkedBlockingQueue q = populatedQueue(SIZE);
728 Object o[] = q.toArray(null);
729 shouldThrow();
730 } catch(NullPointerException success){}
731 }
732
733 /**
734 * toArray with incompatable array type throws CCE
735 */
736 public void testToArray1_BadArg() {
737 try {
738 LinkedBlockingQueue q = populatedQueue(SIZE);
739 Object o[] = q.toArray(new String[10] );
740 shouldThrow();
741 } catch(ArrayStoreException success){}
742 }
743
744
745 /**
746 * iterator iterates through all elements
747 */
748 public void testIterator() {
749 LinkedBlockingQueue q = populatedQueue(SIZE);
750 Iterator it = q.iterator();
751 try {
752 while(it.hasNext()){
753 assertEquals(it.next(), q.take());
754 }
755 } catch (InterruptedException e){
756 unexpectedException();
757 }
758 }
759
760 /**
761 * iterator.remove removes current element
762 */
763 public void testIteratorRemove () {
764 final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
765 q.add(two);
766 q.add(one);
767 q.add(three);
768
769 Iterator it = q.iterator();
770 it.next();
771 it.remove();
772
773 it = q.iterator();
774 assertEquals(it.next(), one);
775 assertEquals(it.next(), three);
776 assertFalse(it.hasNext());
777 }
778
779
780 /**
781 * iterator ordering is FIFO
782 */
783 public void testIteratorOrdering() {
784 final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
785 q.add(one);
786 q.add(two);
787 q.add(three);
788 assertEquals(0, q.remainingCapacity());
789 int k = 0;
790 for (Iterator it = q.iterator(); it.hasNext();) {
791 int i = ((Integer)(it.next())).intValue();
792 assertEquals(++k, i);
793 }
794 assertEquals(3, k);
795 }
796
797 /**
798 * Modifications do not cause iterators to fail
799 */
800 public void testWeaklyConsistentIteration () {
801 final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
802 q.add(one);
803 q.add(two);
804 q.add(three);
805 try {
806 for (Iterator it = q.iterator(); it.hasNext();) {
807 q.remove();
808 it.next();
809 }
810 }
811 catch (ConcurrentModificationException e) {
812 unexpectedException();
813 }
814 assertEquals(0, q.size());
815 }
816
817
818 /**
819 * toString contains toStrings of elements
820 */
821 public void testToString() {
822 LinkedBlockingQueue q = populatedQueue(SIZE);
823 String s = q.toString();
824 for (int i = 0; i < SIZE; ++i) {
825 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
826 }
827 }
828
829
830 /**
831 * offer transfers elements across Executor tasks
832 */
833 public void testOfferInExecutor() {
834 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
835 q.add(one);
836 q.add(two);
837 ExecutorService executor = Executors.newFixedThreadPool(2);
838 executor.execute(new Runnable() {
839 public void run() {
840 threadAssertFalse(q.offer(three));
841 try {
842 threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
843 threadAssertEquals(0, q.remainingCapacity());
844 }
845 catch (InterruptedException e) {
846 threadUnexpectedException();
847 }
848 }
849 });
850
851 executor.execute(new Runnable() {
852 public void run() {
853 try {
854 Thread.sleep(SMALL_DELAY_MS);
855 threadAssertEquals(one, q.take());
856 }
857 catch (InterruptedException e) {
858 threadUnexpectedException();
859 }
860 }
861 });
862
863 joinPool(executor);
864 }
865
866 /**
867 * poll retrieves elements across Executor threads
868 */
869 public void testPollInExecutor() {
870 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
871 ExecutorService executor = Executors.newFixedThreadPool(2);
872 executor.execute(new Runnable() {
873 public void run() {
874 threadAssertNull(q.poll());
875 try {
876 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
877 threadAssertTrue(q.isEmpty());
878 }
879 catch (InterruptedException e) {
880 threadUnexpectedException();
881 }
882 }
883 });
884
885 executor.execute(new Runnable() {
886 public void run() {
887 try {
888 Thread.sleep(SMALL_DELAY_MS);
889 q.put(one);
890 }
891 catch (InterruptedException e) {
892 threadUnexpectedException();
893 }
894 }
895 });
896
897 joinPool(executor);
898 }
899
900 /**
901 * A deserialized serialized queue has same elements in same order
902 */
903 public void testSerialization() {
904 LinkedBlockingQueue q = populatedQueue(SIZE);
905
906 try {
907 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
908 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
909 out.writeObject(q);
910 out.close();
911
912 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
913 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
914 LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
915 assertEquals(q.size(), r.size());
916 while (!q.isEmpty())
917 assertEquals(q.remove(), r.remove());
918 } catch(Exception e){
919 unexpectedException();
920 }
921 }
922
923 /**
924 * drainTo(null) throws NPE
925 */
926 public void testDrainToNull() {
927 LinkedBlockingQueue q = populatedQueue(SIZE);
928 try {
929 q.drainTo(null);
930 shouldThrow();
931 } catch(NullPointerException success) {
932 }
933 }
934
935 /**
936 * drainTo(this) throws IAE
937 */
938 public void testDrainToSelf() {
939 LinkedBlockingQueue q = populatedQueue(SIZE);
940 try {
941 q.drainTo(q);
942 shouldThrow();
943 } catch(IllegalArgumentException success) {
944 }
945 }
946
947 /**
948 * drainTo(c) empties queue into another collection c
949 */
950 public void testDrainTo() {
951 LinkedBlockingQueue q = populatedQueue(SIZE);
952 ArrayList l = new ArrayList();
953 q.drainTo(l);
954 assertEquals(q.size(), 0);
955 assertEquals(l.size(), SIZE);
956 for (int i = 0; i < SIZE; ++i)
957 assertEquals(l.get(i), new Integer(i));
958 }
959
960 /**
961 * drainTo empties full queue, unblocking a waiting put.
962 */
963 public void testDrainToWithActivePut() {
964 final LinkedBlockingQueue q = populatedQueue(SIZE);
965 Thread t = new Thread(new Runnable() {
966 public void run() {
967 try {
968 q.put(new Integer(SIZE+1));
969 } catch (InterruptedException ie){
970 threadUnexpectedException();
971 }
972 }
973 });
974 try {
975 t.start();
976 ArrayList l = new ArrayList();
977 q.drainTo(l);
978 assertTrue(l.size() >= SIZE);
979 for (int i = 0; i < SIZE; ++i)
980 assertEquals(l.get(i), new Integer(i));
981 t.join();
982 assertTrue(q.size() + l.size() == SIZE+1);
983 } catch(Exception e){
984 unexpectedException();
985 }
986 }
987
988 /**
989 * drainTo(null, n) throws NPE
990 */
991 public void testDrainToNullN() {
992 LinkedBlockingQueue q = populatedQueue(SIZE);
993 try {
994 q.drainTo(null, 0);
995 shouldThrow();
996 } catch(NullPointerException success) {
997 }
998 }
999
1000 /**
1001 * drainTo(this, n) throws IAE
1002 */
1003 public void testDrainToSelfN() {
1004 LinkedBlockingQueue q = populatedQueue(SIZE);
1005 try {
1006 q.drainTo(q, 0);
1007 shouldThrow();
1008 } catch(IllegalArgumentException success) {
1009 }
1010 }
1011
1012 /**
1013 * drainTo(c, n) empties first max {n, size} elements of queue into c
1014 */
1015 public void testDrainToN() {
1016 for (int i = 0; i < SIZE + 2; ++i) {
1017 LinkedBlockingQueue q = populatedQueue(SIZE);
1018 ArrayList l = new ArrayList();
1019 q.drainTo(l, i);
1020 int k = (i < SIZE)? i : SIZE;
1021 assertEquals(q.size(), SIZE-k);
1022 assertEquals(l.size(), k);
1023 for (int j = 0; j < k; ++j)
1024 assertEquals(l.get(j), new Integer(j));
1025 }
1026 }
1027
1028 }