ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.41
Committed: Fri Nov 5 00:17:22 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.40: +5 -4 lines
Log Message:
very small improvements to testToArray2

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