ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.46
Committed: Sat May 21 06:24:33 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.45: +9 -6 lines
Log Message:
various test improvements

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/publicdomain/zero/1.0/
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 delay(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 delay(SHORT_DELAY_MS);
355 assertEquals(q.remainingCapacity(), 0);
356 assertEquals(0, q.take());
357 delay(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 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
369 Thread t = newStartedThread(new CheckedRunnable() {
370 public void realRun() throws InterruptedException {
371 q.put(new Object());
372 q.put(new Object());
373 long startTime = System.nanoTime();
374 assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
375 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
376 pleaseInterrupt.countDown();
377 try {
378 q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
379 shouldThrow();
380 } catch (InterruptedException success) {}
381 }});
382
383 await(pleaseInterrupt);
384 t.interrupt();
385 awaitTermination(t);
386 }
387
388 /**
389 * take retrieves elements in FIFO order
390 */
391 public void testTake() throws InterruptedException {
392 ArrayBlockingQueue q = populatedQueue(SIZE);
393 for (int i = 0; i < SIZE; ++i) {
394 assertEquals(i, q.take());
395 }
396 }
397
398 /**
399 * Take removes existing elements until empty, then blocks interruptibly
400 */
401 public void testBlockingTake() throws InterruptedException {
402 final ArrayBlockingQueue q = populatedQueue(SIZE);
403 Thread t = new Thread(new CheckedRunnable() {
404 public void realRun() throws InterruptedException {
405 for (int i = 0; i < SIZE; ++i) {
406 assertEquals(i, q.take());
407 }
408 try {
409 q.take();
410 shouldThrow();
411 } catch (InterruptedException success) {}
412 }});
413
414 t.start();
415 delay(SHORT_DELAY_MS);
416 t.interrupt();
417 t.join();
418 }
419
420
421 /**
422 * poll succeeds unless empty
423 */
424 public void testPoll() {
425 ArrayBlockingQueue q = populatedQueue(SIZE);
426 for (int i = 0; i < SIZE; ++i) {
427 assertEquals(i, q.poll());
428 }
429 assertNull(q.poll());
430 }
431
432 /**
433 * timed poll with zero timeout succeeds when non-empty, else times out
434 */
435 public void testTimedPoll0() throws InterruptedException {
436 ArrayBlockingQueue q = populatedQueue(SIZE);
437 for (int i = 0; i < SIZE; ++i) {
438 assertEquals(i, q.poll(0, MILLISECONDS));
439 }
440 assertNull(q.poll(0, MILLISECONDS));
441 }
442
443 /**
444 * timed poll with nonzero timeout succeeds when non-empty, else times out
445 */
446 public void testTimedPoll() throws InterruptedException {
447 ArrayBlockingQueue q = populatedQueue(SIZE);
448 for (int i = 0; i < SIZE; ++i) {
449 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
450 }
451 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
452 }
453
454 /**
455 * Interrupted timed poll throws InterruptedException instead of
456 * returning timeout status
457 */
458 public void testInterruptedTimedPoll() throws InterruptedException {
459 final BlockingQueue<Integer> q = populatedQueue(SIZE);
460 final CountDownLatch aboutToWait = new CountDownLatch(1);
461 Thread t = newStartedThread(new CheckedRunnable() {
462 public void realRun() throws InterruptedException {
463 for (int i = 0; i < SIZE; ++i) {
464 long t0 = System.nanoTime();
465 assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
466 assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
467 }
468 long t0 = System.nanoTime();
469 aboutToWait.countDown();
470 try {
471 q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
472 shouldThrow();
473 } catch (InterruptedException success) {
474 assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
475 }
476 }});
477
478 aboutToWait.await();
479 waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
480 t.interrupt();
481 awaitTermination(t, MEDIUM_DELAY_MS);
482 checkEmpty(q);
483 }
484
485 /**
486 * peek returns next element, or null if empty
487 */
488 public void testPeek() {
489 ArrayBlockingQueue q = populatedQueue(SIZE);
490 for (int i = 0; i < SIZE; ++i) {
491 assertEquals(i, q.peek());
492 assertEquals(i, q.poll());
493 assertTrue(q.peek() == null ||
494 !q.peek().equals(i));
495 }
496 assertNull(q.peek());
497 }
498
499 /**
500 * element returns next element, or throws NSEE if empty
501 */
502 public void testElement() {
503 ArrayBlockingQueue q = populatedQueue(SIZE);
504 for (int i = 0; i < SIZE; ++i) {
505 assertEquals(i, q.element());
506 assertEquals(i, q.poll());
507 }
508 try {
509 q.element();
510 shouldThrow();
511 } catch (NoSuchElementException success) {}
512 }
513
514 /**
515 * remove removes next element, or throws NSEE if empty
516 */
517 public void testRemove() {
518 ArrayBlockingQueue q = populatedQueue(SIZE);
519 for (int i = 0; i < SIZE; ++i) {
520 assertEquals(i, q.remove());
521 }
522 try {
523 q.remove();
524 shouldThrow();
525 } catch (NoSuchElementException success) {}
526 }
527
528 /**
529 * remove(x) removes x and returns true if present
530 */
531 public void testRemoveElement() {
532 ArrayBlockingQueue q = populatedQueue(SIZE);
533 for (int i = 1; i < SIZE; i+=2) {
534 assertTrue(q.remove(new Integer(i)));
535 }
536 for (int i = 0; i < SIZE; i+=2) {
537 assertTrue(q.remove(new Integer(i)));
538 assertFalse(q.remove(new Integer(i+1)));
539 }
540 assertTrue(q.isEmpty());
541 }
542
543 /**
544 * contains(x) reports true when elements added but not yet removed
545 */
546 public void testContains() {
547 ArrayBlockingQueue q = populatedQueue(SIZE);
548 for (int i = 0; i < SIZE; ++i) {
549 assertTrue(q.contains(new Integer(i)));
550 assertEquals(i, q.poll());
551 assertFalse(q.contains(new Integer(i)));
552 }
553 }
554
555 /**
556 * clear removes all elements
557 */
558 public void testClear() {
559 ArrayBlockingQueue q = populatedQueue(SIZE);
560 q.clear();
561 assertTrue(q.isEmpty());
562 assertEquals(0, q.size());
563 assertEquals(SIZE, q.remainingCapacity());
564 q.add(one);
565 assertFalse(q.isEmpty());
566 assertTrue(q.contains(one));
567 q.clear();
568 assertTrue(q.isEmpty());
569 }
570
571 /**
572 * containsAll(c) is true when c contains a subset of elements
573 */
574 public void testContainsAll() {
575 ArrayBlockingQueue q = populatedQueue(SIZE);
576 ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
577 for (int i = 0; i < SIZE; ++i) {
578 assertTrue(q.containsAll(p));
579 assertFalse(p.containsAll(q));
580 p.add(new Integer(i));
581 }
582 assertTrue(p.containsAll(q));
583 }
584
585 /**
586 * retainAll(c) retains only those elements of c and reports true if changed
587 */
588 public void testRetainAll() {
589 ArrayBlockingQueue q = populatedQueue(SIZE);
590 ArrayBlockingQueue p = populatedQueue(SIZE);
591 for (int i = 0; i < SIZE; ++i) {
592 boolean changed = q.retainAll(p);
593 if (i == 0)
594 assertFalse(changed);
595 else
596 assertTrue(changed);
597
598 assertTrue(q.containsAll(p));
599 assertEquals(SIZE-i, q.size());
600 p.remove();
601 }
602 }
603
604 /**
605 * removeAll(c) removes only those elements of c and reports true if changed
606 */
607 public void testRemoveAll() {
608 for (int i = 1; i < SIZE; ++i) {
609 ArrayBlockingQueue q = populatedQueue(SIZE);
610 ArrayBlockingQueue p = populatedQueue(i);
611 assertTrue(q.removeAll(p));
612 assertEquals(SIZE-i, q.size());
613 for (int j = 0; j < i; ++j) {
614 Integer I = (Integer)(p.remove());
615 assertFalse(q.contains(I));
616 }
617 }
618 }
619
620 /**
621 * toArray contains all elements in FIFO order
622 */
623 public void testToArray() {
624 ArrayBlockingQueue q = populatedQueue(SIZE);
625 Object[] o = q.toArray();
626 for (int i = 0; i < o.length; i++)
627 assertSame(o[i], q.poll());
628 }
629
630 /**
631 * toArray(a) contains all elements in FIFO order
632 */
633 public void testToArray2() {
634 ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
635 Integer[] ints = new Integer[SIZE];
636 Integer[] array = q.toArray(ints);
637 assertSame(ints, array);
638 for (int i = 0; i < ints.length; i++)
639 assertSame(ints[i], q.poll());
640 }
641
642 /**
643 * toArray(null) throws NullPointerException
644 */
645 public void testToArray_NullArg() {
646 ArrayBlockingQueue q = populatedQueue(SIZE);
647 try {
648 q.toArray(null);
649 shouldThrow();
650 } catch (NullPointerException success) {}
651 }
652
653 /**
654 * toArray(incompatible array type) throws ArrayStoreException
655 */
656 public void testToArray1_BadArg() {
657 ArrayBlockingQueue q = populatedQueue(SIZE);
658 try {
659 q.toArray(new String[10]);
660 shouldThrow();
661 } catch (ArrayStoreException success) {}
662 }
663
664
665 /**
666 * iterator iterates through all elements
667 */
668 public void testIterator() throws InterruptedException {
669 ArrayBlockingQueue q = populatedQueue(SIZE);
670 Iterator it = q.iterator();
671 while (it.hasNext()) {
672 assertEquals(it.next(), q.take());
673 }
674 }
675
676 /**
677 * iterator.remove removes current element
678 */
679 public void testIteratorRemove() {
680 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
681 q.add(two);
682 q.add(one);
683 q.add(three);
684
685 Iterator it = q.iterator();
686 it.next();
687 it.remove();
688
689 it = q.iterator();
690 assertSame(it.next(), one);
691 assertSame(it.next(), three);
692 assertFalse(it.hasNext());
693 }
694
695 /**
696 * iterator ordering is FIFO
697 */
698 public void testIteratorOrdering() {
699 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
700 q.add(one);
701 q.add(two);
702 q.add(three);
703
704 assertEquals("queue should be full", 0, q.remainingCapacity());
705
706 int k = 0;
707 for (Iterator it = q.iterator(); it.hasNext();) {
708 assertEquals(++k, it.next());
709 }
710 assertEquals(3, k);
711 }
712
713 /**
714 * Modifications do not cause iterators to fail
715 */
716 public void testWeaklyConsistentIteration() {
717 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
718 q.add(one);
719 q.add(two);
720 q.add(three);
721 for (Iterator it = q.iterator(); it.hasNext();) {
722 q.remove();
723 it.next();
724 }
725 assertEquals(0, q.size());
726 }
727
728
729 /**
730 * toString contains toStrings of elements
731 */
732 public void testToString() {
733 ArrayBlockingQueue q = populatedQueue(SIZE);
734 String s = q.toString();
735 for (int i = 0; i < SIZE; ++i) {
736 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
737 }
738 }
739
740
741 /**
742 * offer transfers elements across Executor tasks
743 */
744 public void testOfferInExecutor() {
745 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
746 q.add(one);
747 q.add(two);
748 ExecutorService executor = Executors.newFixedThreadPool(2);
749 executor.execute(new CheckedRunnable() {
750 public void realRun() throws InterruptedException {
751 assertFalse(q.offer(three));
752 assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
753 assertEquals(0, q.remainingCapacity());
754 }});
755
756 executor.execute(new CheckedRunnable() {
757 public void realRun() throws InterruptedException {
758 delay(SMALL_DELAY_MS);
759 assertSame(one, q.take());
760 }});
761
762 joinPool(executor);
763 }
764
765 /**
766 * poll retrieves elements across Executor threads
767 */
768 public void testPollInExecutor() {
769 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
770 ExecutorService executor = Executors.newFixedThreadPool(2);
771 executor.execute(new CheckedRunnable() {
772 public void realRun() throws InterruptedException {
773 assertNull(q.poll());
774 assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
775 assertTrue(q.isEmpty());
776 }});
777
778 executor.execute(new CheckedRunnable() {
779 public void realRun() throws InterruptedException {
780 delay(SMALL_DELAY_MS);
781 q.put(one);
782 }});
783
784 joinPool(executor);
785 }
786
787 /**
788 * A deserialized serialized queue has same elements in same order
789 */
790 public void testSerialization() throws Exception {
791 ArrayBlockingQueue q = populatedQueue(SIZE);
792
793 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
794 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
795 out.writeObject(q);
796 out.close();
797
798 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
799 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
800 ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
801 assertEquals(q.size(), r.size());
802 while (!q.isEmpty())
803 assertEquals(q.remove(), r.remove());
804 }
805
806 /**
807 * drainTo(null) throws NPE
808 */
809 public void testDrainToNull() {
810 ArrayBlockingQueue q = populatedQueue(SIZE);
811 try {
812 q.drainTo(null);
813 shouldThrow();
814 } catch (NullPointerException success) {}
815 }
816
817 /**
818 * drainTo(this) throws IAE
819 */
820 public void testDrainToSelf() {
821 ArrayBlockingQueue q = populatedQueue(SIZE);
822 try {
823 q.drainTo(q);
824 shouldThrow();
825 } catch (IllegalArgumentException success) {}
826 }
827
828 /**
829 * drainTo(c) empties queue into another collection c
830 */
831 public void testDrainTo() {
832 ArrayBlockingQueue q = populatedQueue(SIZE);
833 ArrayList l = new ArrayList();
834 q.drainTo(l);
835 assertEquals(q.size(), 0);
836 assertEquals(l.size(), SIZE);
837 for (int i = 0; i < SIZE; ++i)
838 assertEquals(l.get(i), new Integer(i));
839 q.add(zero);
840 q.add(one);
841 assertFalse(q.isEmpty());
842 assertTrue(q.contains(zero));
843 assertTrue(q.contains(one));
844 l.clear();
845 q.drainTo(l);
846 assertEquals(q.size(), 0);
847 assertEquals(l.size(), 2);
848 for (int i = 0; i < 2; ++i)
849 assertEquals(l.get(i), new Integer(i));
850 }
851
852 /**
853 * drainTo empties full queue, unblocking a waiting put.
854 */
855 public void testDrainToWithActivePut() throws InterruptedException {
856 final ArrayBlockingQueue q = populatedQueue(SIZE);
857 Thread t = new Thread(new CheckedRunnable() {
858 public void realRun() throws InterruptedException {
859 q.put(new Integer(SIZE+1));
860 }});
861
862 t.start();
863 ArrayList l = new ArrayList();
864 q.drainTo(l);
865 assertTrue(l.size() >= SIZE);
866 for (int i = 0; i < SIZE; ++i)
867 assertEquals(l.get(i), new Integer(i));
868 t.join();
869 assertTrue(q.size() + l.size() >= SIZE);
870 }
871
872 /**
873 * drainTo(null, n) throws NPE
874 */
875 public void testDrainToNullN() {
876 ArrayBlockingQueue q = populatedQueue(SIZE);
877 try {
878 q.drainTo(null, 0);
879 shouldThrow();
880 } catch (NullPointerException success) {}
881 }
882
883 /**
884 * drainTo(this, n) throws IAE
885 */
886 public void testDrainToSelfN() {
887 ArrayBlockingQueue q = populatedQueue(SIZE);
888 try {
889 q.drainTo(q, 0);
890 shouldThrow();
891 } catch (IllegalArgumentException success) {}
892 }
893
894 /**
895 * drainTo(c, n) empties first min(n, size) elements of queue into c
896 */
897 public void testDrainToN() {
898 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
899 for (int i = 0; i < SIZE + 2; ++i) {
900 for (int j = 0; j < SIZE; j++)
901 assertTrue(q.offer(new Integer(j)));
902 ArrayList l = new ArrayList();
903 q.drainTo(l, i);
904 int k = (i < SIZE) ? i : SIZE;
905 assertEquals(l.size(), k);
906 assertEquals(q.size(), SIZE-k);
907 for (int j = 0; j < k; ++j)
908 assertEquals(l.get(j), new Integer(j));
909 while (q.poll() != null) ;
910 }
911 }
912
913 }