ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.26
Committed: Sun Nov 22 18:57:16 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.25: +11 -12 lines
Log Message:
use autoboxing judiciously for readability

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