ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.49
Committed: Tue May 31 16:16:23 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.48: +12 -13 lines
Log Message:
use serialClone in serialization tests; update imports

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 import junit.framework.*;
10 import java.util.Arrays;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Iterator;
14 import java.util.NoSuchElementException;
15 import java.util.Queue;
16 import java.util.concurrent.ArrayBlockingQueue;
17 import java.util.concurrent.BlockingQueue;
18 import java.util.concurrent.CountDownLatch;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ExecutorService;
21 import static java.util.concurrent.TimeUnit.MILLISECONDS;
22
23 public class ArrayBlockingQueueTest extends JSR166TestCase {
24
25 public static class Fair extends BlockingQueueTest {
26 protected BlockingQueue emptyCollection() {
27 return new ArrayBlockingQueue(20, true);
28 }
29 }
30
31 public static class NonFair extends BlockingQueueTest {
32 protected BlockingQueue emptyCollection() {
33 return new ArrayBlockingQueue(20, false);
34 }
35 }
36
37 public static void main(String[] args) {
38 junit.textui.TestRunner.run(suite());
39 }
40
41 public static Test suite() {
42 return newTestSuite(ArrayBlockingQueueTest.class,
43 new Fair().testSuite(),
44 new NonFair().testSuite());
45 }
46
47 /**
48 * Create a queue of given size containing consecutive
49 * Integers 0 ... n.
50 */
51 private ArrayBlockingQueue<Integer> populatedQueue(int n) {
52 ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
53 assertTrue(q.isEmpty());
54 for (int i = 0; i < n; i++)
55 assertTrue(q.offer(new Integer(i)));
56 assertFalse(q.isEmpty());
57 assertEquals(0, q.remainingCapacity());
58 assertEquals(n, q.size());
59 return q;
60 }
61
62 /**
63 * A new queue has the indicated capacity
64 */
65 public void testConstructor1() {
66 assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
67 }
68
69 /**
70 * Constructor throws IAE if capacity argument nonpositive
71 */
72 public void testConstructor2() {
73 try {
74 new ArrayBlockingQueue(0);
75 shouldThrow();
76 } catch (IllegalArgumentException success) {}
77 }
78
79 /**
80 * Initializing from null Collection throws NPE
81 */
82 public void testConstructor3() {
83 try {
84 new ArrayBlockingQueue(1, true, null);
85 shouldThrow();
86 } catch (NullPointerException success) {}
87 }
88
89 /**
90 * Initializing from Collection of null elements throws NPE
91 */
92 public void testConstructor4() {
93 Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
94 try {
95 new ArrayBlockingQueue(SIZE, false, elements);
96 shouldThrow();
97 } catch (NullPointerException success) {}
98 }
99
100 /**
101 * Initializing from Collection with some null elements throws NPE
102 */
103 public void testConstructor5() {
104 Integer[] ints = new Integer[SIZE];
105 for (int i = 0; i < SIZE-1; ++i)
106 ints[i] = i;
107 Collection<Integer> elements = Arrays.asList(ints);
108 try {
109 new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
110 shouldThrow();
111 } catch (NullPointerException success) {}
112 }
113
114 /**
115 * Initializing from too large collection throws IAE
116 */
117 public void testConstructor6() {
118 Integer[] ints = new Integer[SIZE];
119 for (int i = 0; i < SIZE; ++i)
120 ints[i] = i;
121 Collection<Integer> elements = Arrays.asList(ints);
122 try {
123 new ArrayBlockingQueue(SIZE - 1, false, elements);
124 shouldThrow();
125 } catch (IllegalArgumentException success) {}
126 }
127
128 /**
129 * Queue contains all elements of collection used to initialize
130 */
131 public void testConstructor7() {
132 Integer[] ints = new Integer[SIZE];
133 for (int i = 0; i < SIZE; ++i)
134 ints[i] = i;
135 Collection<Integer> elements = Arrays.asList(ints);
136 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, elements);
137 for (int i = 0; i < SIZE; ++i)
138 assertEquals(ints[i], q.poll());
139 }
140
141 /**
142 * Queue transitions from empty to full when elements added
143 */
144 public void testEmptyFull() {
145 ArrayBlockingQueue q = new ArrayBlockingQueue(2);
146 assertTrue(q.isEmpty());
147 assertEquals(2, q.remainingCapacity());
148 q.add(one);
149 assertFalse(q.isEmpty());
150 q.add(two);
151 assertFalse(q.isEmpty());
152 assertEquals(0, q.remainingCapacity());
153 assertFalse(q.offer(three));
154 }
155
156 /**
157 * remainingCapacity decreases on add, increases on remove
158 */
159 public void testRemainingCapacity() {
160 ArrayBlockingQueue q = populatedQueue(SIZE);
161 for (int i = 0; i < SIZE; ++i) {
162 assertEquals(i, q.remainingCapacity());
163 assertEquals(SIZE-i, q.size());
164 q.remove();
165 }
166 for (int i = 0; i < SIZE; ++i) {
167 assertEquals(SIZE-i, q.remainingCapacity());
168 assertEquals(i, q.size());
169 q.add(new Integer(i));
170 }
171 }
172
173 /**
174 * Offer succeeds if not full; fails if full
175 */
176 public void testOffer() {
177 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
178 assertTrue(q.offer(zero));
179 assertFalse(q.offer(one));
180 }
181
182 /**
183 * add succeeds if not full; throws ISE if full
184 */
185 public void testAdd() {
186 try {
187 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
188 for (int i = 0; i < SIZE; ++i) {
189 assertTrue(q.add(new Integer(i)));
190 }
191 assertEquals(0, q.remainingCapacity());
192 q.add(new Integer(SIZE));
193 shouldThrow();
194 } catch (IllegalStateException success) {}
195 }
196
197 /**
198 * addAll(this) throws IAE
199 */
200 public void testAddAllSelf() {
201 try {
202 ArrayBlockingQueue q = populatedQueue(SIZE);
203 q.addAll(q);
204 shouldThrow();
205 } catch (IllegalArgumentException success) {}
206 }
207
208 /**
209 * addAll of a collection with any null elements throws NPE after
210 * possibly adding some elements
211 */
212 public void testAddAll3() {
213 try {
214 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
215 Integer[] ints = new Integer[SIZE];
216 for (int i = 0; i < SIZE-1; ++i)
217 ints[i] = new Integer(i);
218 q.addAll(Arrays.asList(ints));
219 shouldThrow();
220 } catch (NullPointerException success) {}
221 }
222
223 /**
224 * addAll throws ISE if not enough room
225 */
226 public void testAddAll4() {
227 try {
228 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
229 Integer[] ints = new Integer[SIZE];
230 for (int i = 0; i < SIZE; ++i)
231 ints[i] = new Integer(i);
232 q.addAll(Arrays.asList(ints));
233 shouldThrow();
234 } catch (IllegalStateException success) {}
235 }
236
237 /**
238 * Queue contains all elements, in traversal order, of successful addAll
239 */
240 public void testAddAll5() {
241 Integer[] empty = new Integer[0];
242 Integer[] ints = new Integer[SIZE];
243 for (int i = 0; i < SIZE; ++i)
244 ints[i] = new Integer(i);
245 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
246 assertFalse(q.addAll(Arrays.asList(empty)));
247 assertTrue(q.addAll(Arrays.asList(ints)));
248 for (int i = 0; i < SIZE; ++i)
249 assertEquals(ints[i], q.poll());
250 }
251
252 /**
253 * all elements successfully put are contained
254 */
255 public void testPut() throws InterruptedException {
256 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
257 for (int i = 0; i < SIZE; ++i) {
258 Integer I = new Integer(i);
259 q.put(I);
260 assertTrue(q.contains(I));
261 }
262 assertEquals(0, q.remainingCapacity());
263 }
264
265 /**
266 * put blocks interruptibly if full
267 */
268 public void testBlockingPut() throws InterruptedException {
269 final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
270 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
271 Thread t = newStartedThread(new CheckedRunnable() {
272 public void realRun() throws InterruptedException {
273 for (int i = 0; i < SIZE; ++i)
274 q.put(i);
275 assertEquals(SIZE, q.size());
276 assertEquals(0, q.remainingCapacity());
277
278 Thread.currentThread().interrupt();
279 try {
280 q.put(99);
281 shouldThrow();
282 } catch (InterruptedException success) {}
283 assertFalse(Thread.interrupted());
284
285 pleaseInterrupt.countDown();
286 try {
287 q.put(99);
288 shouldThrow();
289 } catch (InterruptedException success) {}
290 assertFalse(Thread.interrupted());
291 }});
292
293 await(pleaseInterrupt);
294 assertThreadStaysAlive(t);
295 t.interrupt();
296 awaitTermination(t);
297 assertEquals(SIZE, q.size());
298 assertEquals(0, q.remainingCapacity());
299 }
300
301 /**
302 * put blocks interruptibly waiting for take when full
303 */
304 public void testPutWithTake() throws InterruptedException {
305 final int capacity = 2;
306 final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
307 final CountDownLatch pleaseTake = new CountDownLatch(1);
308 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
309 Thread t = newStartedThread(new CheckedRunnable() {
310 public void realRun() throws InterruptedException {
311 for (int i = 0; i < capacity; i++)
312 q.put(i);
313 pleaseTake.countDown();
314 q.put(86);
315
316 pleaseInterrupt.countDown();
317 try {
318 q.put(99);
319 shouldThrow();
320 } catch (InterruptedException success) {}
321 assertFalse(Thread.interrupted());
322 }});
323
324 await(pleaseTake);
325 assertEquals(q.remainingCapacity(), 0);
326 assertEquals(0, q.take());
327
328 await(pleaseInterrupt);
329 assertThreadStaysAlive(t);
330 t.interrupt();
331 awaitTermination(t);
332 assertEquals(q.remainingCapacity(), 0);
333 }
334
335 /**
336 * timed offer times out if full and elements not taken
337 */
338 public void testTimedOffer() throws InterruptedException {
339 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
340 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
341 Thread t = newStartedThread(new CheckedRunnable() {
342 public void realRun() throws InterruptedException {
343 q.put(new Object());
344 q.put(new Object());
345 long startTime = System.nanoTime();
346 assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
347 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
348 pleaseInterrupt.countDown();
349 try {
350 q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
351 shouldThrow();
352 } catch (InterruptedException success) {}
353 }});
354
355 await(pleaseInterrupt);
356 assertThreadStaysAlive(t);
357 t.interrupt();
358 awaitTermination(t);
359 }
360
361 /**
362 * take retrieves elements in FIFO order
363 */
364 public void testTake() throws InterruptedException {
365 ArrayBlockingQueue q = populatedQueue(SIZE);
366 for (int i = 0; i < SIZE; ++i) {
367 assertEquals(i, q.take());
368 }
369 }
370
371 /**
372 * Take removes existing elements until empty, then blocks interruptibly
373 */
374 public void testBlockingTake() throws InterruptedException {
375 final ArrayBlockingQueue q = populatedQueue(SIZE);
376 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
377 Thread t = newStartedThread(new CheckedRunnable() {
378 public void realRun() throws InterruptedException {
379 for (int i = 0; i < SIZE; ++i) {
380 assertEquals(i, q.take());
381 }
382
383 Thread.currentThread().interrupt();
384 try {
385 q.take();
386 shouldThrow();
387 } catch (InterruptedException success) {}
388 assertFalse(Thread.interrupted());
389
390 pleaseInterrupt.countDown();
391 try {
392 q.take();
393 shouldThrow();
394 } catch (InterruptedException success) {}
395 assertFalse(Thread.interrupted());
396 }});
397
398 await(pleaseInterrupt);
399 assertThreadStaysAlive(t);
400 t.interrupt();
401 awaitTermination(t);
402 }
403
404 /**
405 * poll succeeds unless empty
406 */
407 public void testPoll() {
408 ArrayBlockingQueue q = populatedQueue(SIZE);
409 for (int i = 0; i < SIZE; ++i) {
410 assertEquals(i, q.poll());
411 }
412 assertNull(q.poll());
413 }
414
415 /**
416 * timed poll with zero timeout succeeds when non-empty, else times out
417 */
418 public void testTimedPoll0() throws InterruptedException {
419 ArrayBlockingQueue q = populatedQueue(SIZE);
420 for (int i = 0; i < SIZE; ++i) {
421 assertEquals(i, q.poll(0, MILLISECONDS));
422 }
423 assertNull(q.poll(0, MILLISECONDS));
424 checkEmpty(q);
425 }
426
427 /**
428 * timed poll with nonzero timeout succeeds when non-empty, else times out
429 */
430 public void testTimedPoll() throws InterruptedException {
431 ArrayBlockingQueue q = populatedQueue(SIZE);
432 for (int i = 0; i < SIZE; ++i) {
433 long startTime = System.nanoTime();
434 assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
435 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
436 }
437 long startTime = System.nanoTime();
438 assertNull(q.poll(timeoutMillis(), MILLISECONDS));
439 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
440 checkEmpty(q);
441 }
442
443 /**
444 * Interrupted timed poll throws InterruptedException instead of
445 * returning timeout status
446 */
447 public void testInterruptedTimedPoll() throws InterruptedException {
448 final BlockingQueue<Integer> q = populatedQueue(SIZE);
449 final CountDownLatch aboutToWait = new CountDownLatch(1);
450 Thread t = newStartedThread(new CheckedRunnable() {
451 public void realRun() throws InterruptedException {
452 for (int i = 0; i < SIZE; ++i) {
453 long t0 = System.nanoTime();
454 assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
455 assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
456 }
457 long t0 = System.nanoTime();
458 aboutToWait.countDown();
459 try {
460 q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
461 shouldThrow();
462 } catch (InterruptedException success) {
463 assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
464 }
465 }});
466
467 aboutToWait.await();
468 waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
469 t.interrupt();
470 awaitTermination(t, MEDIUM_DELAY_MS);
471 checkEmpty(q);
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(incompatible array type) throws ArrayStoreException
633 */
634 public void testToArray1_BadArg() {
635 ArrayBlockingQueue q = populatedQueue(SIZE);
636 try {
637 q.toArray(new String[10]);
638 shouldThrow();
639 } catch (ArrayStoreException success) {}
640 }
641
642 /**
643 * iterator iterates through all elements
644 */
645 public void testIterator() throws InterruptedException {
646 ArrayBlockingQueue q = populatedQueue(SIZE);
647 Iterator it = q.iterator();
648 while (it.hasNext()) {
649 assertEquals(it.next(), q.take());
650 }
651 }
652
653 /**
654 * iterator.remove removes current element
655 */
656 public void testIteratorRemove() {
657 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
658 q.add(two);
659 q.add(one);
660 q.add(three);
661
662 Iterator it = q.iterator();
663 it.next();
664 it.remove();
665
666 it = q.iterator();
667 assertSame(it.next(), one);
668 assertSame(it.next(), three);
669 assertFalse(it.hasNext());
670 }
671
672 /**
673 * iterator ordering is FIFO
674 */
675 public void testIteratorOrdering() {
676 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
677 q.add(one);
678 q.add(two);
679 q.add(three);
680
681 assertEquals("queue should be full", 0, q.remainingCapacity());
682
683 int k = 0;
684 for (Iterator it = q.iterator(); it.hasNext();) {
685 assertEquals(++k, it.next());
686 }
687 assertEquals(3, k);
688 }
689
690 /**
691 * Modifications do not cause iterators to fail
692 */
693 public void testWeaklyConsistentIteration() {
694 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
695 q.add(one);
696 q.add(two);
697 q.add(three);
698 for (Iterator it = q.iterator(); it.hasNext();) {
699 q.remove();
700 it.next();
701 }
702 assertEquals(0, q.size());
703 }
704
705 /**
706 * toString contains toStrings of elements
707 */
708 public void testToString() {
709 ArrayBlockingQueue q = populatedQueue(SIZE);
710 String s = q.toString();
711 for (int i = 0; i < SIZE; ++i) {
712 assertTrue(s.contains(String.valueOf(i)));
713 }
714 }
715
716 /**
717 * offer transfers elements across Executor tasks
718 */
719 public void testOfferInExecutor() {
720 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
721 q.add(one);
722 q.add(two);
723 ExecutorService executor = Executors.newFixedThreadPool(2);
724 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
725 executor.execute(new CheckedRunnable() {
726 public void realRun() throws InterruptedException {
727 assertFalse(q.offer(three));
728 threadsStarted.await();
729 assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
730 assertEquals(0, q.remainingCapacity());
731 }});
732
733 executor.execute(new CheckedRunnable() {
734 public void realRun() throws InterruptedException {
735 threadsStarted.await();
736 assertEquals(0, q.remainingCapacity());
737 assertSame(one, q.take());
738 }});
739
740 joinPool(executor);
741 }
742
743 /**
744 * timed poll retrieves elements across Executor threads
745 */
746 public void testPollInExecutor() {
747 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
748 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
749 ExecutorService executor = Executors.newFixedThreadPool(2);
750 executor.execute(new CheckedRunnable() {
751 public void realRun() throws InterruptedException {
752 assertNull(q.poll());
753 threadsStarted.await();
754 assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
755 checkEmpty(q);
756 }});
757
758 executor.execute(new CheckedRunnable() {
759 public void realRun() throws InterruptedException {
760 threadsStarted.await();
761 q.put(one);
762 }});
763
764 joinPool(executor);
765 }
766
767 /**
768 * A deserialized serialized queue has same elements in same order
769 */
770 public void testSerialization() throws Exception {
771 Queue x = populatedQueue(SIZE);
772 Queue y = serialClone(x);
773
774 assertTrue(x != y);
775 assertEquals(x.size(), y.size());
776 assertEquals(x.toString(), y.toString());
777 assertTrue(Arrays.equals(x.toArray(), y.toArray()));
778 while (!x.isEmpty()) {
779 assertFalse(y.isEmpty());
780 assertEquals(x.remove(), y.remove());
781 }
782 assertTrue(y.isEmpty());
783 }
784
785 /**
786 * drainTo(c) empties queue into another collection c
787 */
788 public void testDrainTo() {
789 ArrayBlockingQueue q = populatedQueue(SIZE);
790 ArrayList l = new ArrayList();
791 q.drainTo(l);
792 assertEquals(q.size(), 0);
793 assertEquals(l.size(), SIZE);
794 for (int i = 0; i < SIZE; ++i)
795 assertEquals(l.get(i), new Integer(i));
796 q.add(zero);
797 q.add(one);
798 assertFalse(q.isEmpty());
799 assertTrue(q.contains(zero));
800 assertTrue(q.contains(one));
801 l.clear();
802 q.drainTo(l);
803 assertEquals(q.size(), 0);
804 assertEquals(l.size(), 2);
805 for (int i = 0; i < 2; ++i)
806 assertEquals(l.get(i), new Integer(i));
807 }
808
809 /**
810 * drainTo empties full queue, unblocking a waiting put.
811 */
812 public void testDrainToWithActivePut() throws InterruptedException {
813 final ArrayBlockingQueue q = populatedQueue(SIZE);
814 Thread t = new Thread(new CheckedRunnable() {
815 public void realRun() throws InterruptedException {
816 q.put(new Integer(SIZE+1));
817 }});
818
819 t.start();
820 ArrayList l = new ArrayList();
821 q.drainTo(l);
822 assertTrue(l.size() >= SIZE);
823 for (int i = 0; i < SIZE; ++i)
824 assertEquals(l.get(i), new Integer(i));
825 t.join();
826 assertTrue(q.size() + l.size() >= SIZE);
827 }
828
829 /**
830 * drainTo(c, n) empties first min(n, size) elements of queue into c
831 */
832 public void testDrainToN() {
833 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
834 for (int i = 0; i < SIZE + 2; ++i) {
835 for (int j = 0; j < SIZE; j++)
836 assertTrue(q.offer(new Integer(j)));
837 ArrayList l = new ArrayList();
838 q.drainTo(l, i);
839 int k = (i < SIZE) ? i : SIZE;
840 assertEquals(l.size(), k);
841 assertEquals(q.size(), SIZE-k);
842 for (int j = 0; j < k; ++j)
843 assertEquals(l.get(j), new Integer(j));
844 while (q.poll() != null) ;
845 }
846 }
847
848 }