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.
9 package tests.api.java.util.concurrent; // android-added
11 import junit.framework.*;
13 import java.util.concurrent.*;
14 import static java.util.concurrent.TimeUnit.MILLISECONDS;
17 public class ArrayBlockingQueueTest extends JSR166TestCase {
18 public static Test suite() {
19 return new TestSuite(ArrayBlockingQueueTest.class);
23 * Create a queue of given size containing consecutive
26 private ArrayBlockingQueue populatedQueue(int n) {
27 ArrayBlockingQueue q = new ArrayBlockingQueue(n);
28 assertTrue(q.isEmpty());
29 for (int i = 0; i < n; i++)
30 assertTrue(q.offer(new Integer(i)));
31 assertFalse(q.isEmpty());
32 assertEquals(0, q.remainingCapacity());
33 assertEquals(n, q.size());
38 * A new queue has the indicated capacity
40 public void testConstructor1() {
41 assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
45 * Constructor throws IAE if capacity argument nonpositive
47 public void testConstructor2() {
49 ArrayBlockingQueue q = new ArrayBlockingQueue(0);
51 } catch (IllegalArgumentException success) {}
55 * Initializing from null Collection throws NPE
57 public void testConstructor3() {
59 ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
61 } catch (NullPointerException success) {}
65 * Initializing from Collection of null elements throws NPE
67 public void testConstructor4() {
69 Integer[] ints = new Integer[SIZE];
70 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
72 } catch (NullPointerException success) {}
76 * Initializing from Collection with some null elements throws NPE
78 public void testConstructor5() {
80 Integer[] ints = new Integer[SIZE];
81 for (int i = 0; i < SIZE-1; ++i)
82 ints[i] = new Integer(i);
83 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
85 } catch (NullPointerException success) {}
89 * Initializing from too large collection throws IAE
91 public void testConstructor6() {
93 Integer[] ints = new Integer[SIZE];
94 for (int i = 0; i < SIZE; ++i)
95 ints[i] = new Integer(i);
96 ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
98 } catch (IllegalArgumentException success) {}
102 * Queue contains all elements of collection used to initialize
104 public void testConstructor7() {
105 Integer[] ints = new Integer[SIZE];
106 for (int i = 0; i < SIZE; ++i)
107 ints[i] = new Integer(i);
108 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
109 for (int i = 0; i < SIZE; ++i)
110 assertEquals(ints[i], q.poll());
114 * Queue transitions from empty to full when elements added
116 public void testEmptyFull() {
117 ArrayBlockingQueue q = new ArrayBlockingQueue(2);
118 assertTrue(q.isEmpty());
119 assertEquals(2, q.remainingCapacity());
121 assertFalse(q.isEmpty());
123 assertFalse(q.isEmpty());
124 assertEquals(0, q.remainingCapacity());
125 assertFalse(q.offer(three));
129 * remainingCapacity decreases on add, increases on remove
131 public void testRemainingCapacity() {
132 ArrayBlockingQueue q = populatedQueue(SIZE);
133 for (int i = 0; i < SIZE; ++i) {
134 assertEquals(i, q.remainingCapacity());
135 assertEquals(SIZE-i, q.size());
138 for (int i = 0; i < SIZE; ++i) {
139 assertEquals(SIZE-i, q.remainingCapacity());
140 assertEquals(i, q.size());
141 q.add(new Integer(i));
146 * offer(null) throws NPE
148 public void testOfferNull() {
150 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
153 } catch (NullPointerException success) {}
157 * add(null) throws NPE
159 public void testAddNull() {
161 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
164 } catch (NullPointerException success) {}
168 * Offer succeeds if not full; fails if full
170 public void testOffer() {
171 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
172 assertTrue(q.offer(zero));
173 assertFalse(q.offer(one));
177 * add succeeds if not full; throws ISE if full
179 public void testAdd() {
181 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
182 for (int i = 0; i < SIZE; ++i) {
183 assertTrue(q.add(new Integer(i)));
185 assertEquals(0, q.remainingCapacity());
186 q.add(new Integer(SIZE));
188 } catch (IllegalStateException success) {}
192 * addAll(null) throws NPE
194 public void testAddAll1() {
196 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
199 } catch (NullPointerException success) {}
203 * addAll(this) throws IAE
205 public void testAddAllSelf() {
207 ArrayBlockingQueue q = populatedQueue(SIZE);
210 } catch (IllegalArgumentException success) {}
215 * addAll of a collection with null elements throws NPE
217 public void testAddAll2() {
219 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
220 Integer[] ints = new Integer[SIZE];
221 q.addAll(Arrays.asList(ints));
223 } catch (NullPointerException success) {}
226 * addAll of a collection with any null elements throws NPE after
227 * possibly adding some elements
229 public void testAddAll3() {
231 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
232 Integer[] ints = new Integer[SIZE];
233 for (int i = 0; i < SIZE-1; ++i)
234 ints[i] = new Integer(i);
235 q.addAll(Arrays.asList(ints));
237 } catch (NullPointerException success) {}
240 * addAll throws ISE if not enough room
242 public void testAddAll4() {
244 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
245 Integer[] ints = new Integer[SIZE];
246 for (int i = 0; i < SIZE; ++i)
247 ints[i] = new Integer(i);
248 q.addAll(Arrays.asList(ints));
250 } catch (IllegalStateException success) {}
253 * Queue contains all elements, in traversal order, of successful addAll
255 public void testAddAll5() {
256 Integer[] empty = new Integer[0];
257 Integer[] ints = new Integer[SIZE];
258 for (int i = 0; i < SIZE; ++i)
259 ints[i] = new Integer(i);
260 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
261 assertFalse(q.addAll(Arrays.asList(empty)));
262 assertTrue(q.addAll(Arrays.asList(ints)));
263 for (int i = 0; i < SIZE; ++i)
264 assertEquals(ints[i], q.poll());
268 * put(null) throws NPE
270 public void testPutNull() throws InterruptedException {
272 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
275 } catch (NullPointerException success) {}
279 * all elements successfully put are contained
281 public void testPut() throws InterruptedException {
282 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
283 for (int i = 0; i < SIZE; ++i) {
284 Integer I = new Integer(i);
286 assertTrue(q.contains(I));
288 assertEquals(0, q.remainingCapacity());
292 * put blocks interruptibly if full
294 public void testBlockingPut() throws InterruptedException {
295 final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
296 Thread t = new Thread(new CheckedRunnable() {
297 public void realRun() throws InterruptedException {
298 for (int i = 0; i < SIZE; ++i)
300 assertEquals(SIZE, q.size());
301 assertEquals(0, q.remainingCapacity());
305 } catch (InterruptedException success) {}
309 Thread.sleep(SHORT_DELAY_MS);
312 assertEquals(SIZE, q.size());
313 assertEquals(0, q.remainingCapacity());
317 * put blocks waiting for take when full
319 public void testPutWithTake() throws InterruptedException {
320 final int capacity = 2;
321 final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
322 Thread t = new Thread(new CheckedRunnable() {
323 public void realRun() throws InterruptedException {
324 for (int i = 0; i < capacity + 1; i++)
329 } catch (InterruptedException success) {}
333 Thread.sleep(SHORT_DELAY_MS);
334 assertEquals(q.remainingCapacity(), 0);
335 assertEquals(0, q.take());
336 Thread.sleep(SHORT_DELAY_MS);
339 assertEquals(q.remainingCapacity(), 0);
343 * timed offer times out if full and elements not taken
345 public void testTimedOffer() throws InterruptedException {
346 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
347 Thread t = new Thread(new CheckedRunnable() {
348 public void realRun() throws InterruptedException {
351 assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
353 q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
355 } catch (InterruptedException success) {}
359 Thread.sleep(SHORT_DELAY_MS);
365 * take retrieves elements in FIFO order
367 public void testTake() throws InterruptedException {
368 ArrayBlockingQueue q = populatedQueue(SIZE);
369 for (int i = 0; i < SIZE; ++i) {
370 assertEquals(i, q.take());
375 * take blocks interruptibly when empty
377 public void testTakeFromEmpty() throws InterruptedException {
378 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
379 Thread t = new ThreadShouldThrow(InterruptedException.class) {
380 public void realRun() throws InterruptedException {
385 Thread.sleep(SHORT_DELAY_MS);
391 * Take removes existing elements until empty, then blocks interruptibly
393 public void testBlockingTake() throws InterruptedException {
394 final ArrayBlockingQueue q = populatedQueue(SIZE);
395 Thread t = new Thread(new CheckedRunnable() {
396 public void realRun() throws InterruptedException {
397 for (int i = 0; i < SIZE; ++i) {
398 assertEquals(i, q.take());
403 } catch (InterruptedException success) {}
407 Thread.sleep(SHORT_DELAY_MS);
414 * poll succeeds unless empty
416 public void testPoll() {
417 ArrayBlockingQueue q = populatedQueue(SIZE);
418 for (int i = 0; i < SIZE; ++i) {
419 assertEquals(i, q.poll());
421 assertNull(q.poll());
425 * timed pool with zero timeout succeeds when non-empty, else times out
427 public void testTimedPoll0() throws InterruptedException {
428 ArrayBlockingQueue q = populatedQueue(SIZE);
429 for (int i = 0; i < SIZE; ++i) {
430 assertEquals(i, q.poll(0, MILLISECONDS));
432 assertNull(q.poll(0, MILLISECONDS));
436 * timed pool with nonzero timeout succeeds when non-empty, else times out
438 public void testTimedPoll() throws InterruptedException {
439 ArrayBlockingQueue q = populatedQueue(SIZE);
440 for (int i = 0; i < SIZE; ++i) {
441 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
443 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
447 * Interrupted timed poll throws InterruptedException instead of
448 * returning timeout status
450 public void testInterruptedTimedPoll() throws InterruptedException {
451 Thread t = new Thread(new CheckedRunnable() {
452 public void realRun() throws InterruptedException {
453 ArrayBlockingQueue q = populatedQueue(SIZE);
454 for (int i = 0; i < SIZE; ++i) {
455 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));;
458 q.poll(SMALL_DELAY_MS, MILLISECONDS);
460 } catch (InterruptedException success) {}
464 Thread.sleep(SHORT_DELAY_MS);
470 * timed poll before a delayed offer fails; after offer succeeds;
471 * on interruption throws
473 public void testTimedPollWithOffer() throws InterruptedException {
474 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
475 Thread t = new Thread(new CheckedRunnable() {
476 public void realRun() throws InterruptedException {
477 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
478 assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
480 q.poll(LONG_DELAY_MS, MILLISECONDS);
482 } catch (InterruptedException success) {}
486 Thread.sleep(SMALL_DELAY_MS);
487 assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
494 * peek returns next element, or null if empty
496 public void testPeek() {
497 ArrayBlockingQueue q = populatedQueue(SIZE);
498 for (int i = 0; i < SIZE; ++i) {
499 assertEquals(i, q.peek());
500 assertEquals(i, q.poll());
501 assertTrue(q.peek() == null ||
502 !q.peek().equals(i));
504 assertNull(q.peek());
508 * element returns next element, or throws NSEE if empty
510 public void testElement() {
511 ArrayBlockingQueue q = populatedQueue(SIZE);
512 for (int i = 0; i < SIZE; ++i) {
513 assertEquals(i, q.element());
514 assertEquals(i, q.poll());
519 } catch (NoSuchElementException success) {}
523 * remove removes next element, or throws NSEE if empty
525 public void testRemove() {
526 ArrayBlockingQueue q = populatedQueue(SIZE);
527 for (int i = 0; i < SIZE; ++i) {
528 assertEquals(i, q.remove());
533 } catch (NoSuchElementException success) {}
537 * remove(x) removes x and returns true if present
539 public void testRemoveElement() {
540 ArrayBlockingQueue q = populatedQueue(SIZE);
541 for (int i = 1; i < SIZE; i+=2) {
542 assertTrue(q.remove(new Integer(i)));
544 for (int i = 0; i < SIZE; i+=2) {
545 assertTrue(q.remove(new Integer(i)));
546 assertFalse(q.remove(new Integer(i+1)));
548 assertTrue(q.isEmpty());
552 * contains(x) reports true when elements added but not yet removed
554 public void testContains() {
555 ArrayBlockingQueue q = populatedQueue(SIZE);
556 for (int i = 0; i < SIZE; ++i) {
557 assertTrue(q.contains(new Integer(i)));
558 assertEquals(i, q.poll());
559 assertFalse(q.contains(new Integer(i)));
564 * clear removes all elements
566 public void testClear() {
567 ArrayBlockingQueue q = populatedQueue(SIZE);
569 assertTrue(q.isEmpty());
570 assertEquals(0, q.size());
571 assertEquals(SIZE, q.remainingCapacity());
573 assertFalse(q.isEmpty());
574 assertTrue(q.contains(one));
576 assertTrue(q.isEmpty());
580 * containsAll(c) is true when c contains a subset of elements
582 public void testContainsAll() {
583 ArrayBlockingQueue q = populatedQueue(SIZE);
584 ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
585 for (int i = 0; i < SIZE; ++i) {
586 assertTrue(q.containsAll(p));
587 assertFalse(p.containsAll(q));
588 p.add(new Integer(i));
590 assertTrue(p.containsAll(q));
594 * retainAll(c) retains only those elements of c and reports true if changed
596 public void testRetainAll() {
597 ArrayBlockingQueue q = populatedQueue(SIZE);
598 ArrayBlockingQueue p = populatedQueue(SIZE);
599 for (int i = 0; i < SIZE; ++i) {
600 boolean changed = q.retainAll(p);
602 assertFalse(changed);
606 assertTrue(q.containsAll(p));
607 assertEquals(SIZE-i, q.size());
613 * removeAll(c) removes only those elements of c and reports true if changed
615 public void testRemoveAll() {
616 for (int i = 1; i < SIZE; ++i) {
617 ArrayBlockingQueue q = populatedQueue(SIZE);
618 ArrayBlockingQueue p = populatedQueue(i);
619 assertTrue(q.removeAll(p));
620 assertEquals(SIZE-i, q.size());
621 for (int j = 0; j < i; ++j) {
622 Integer I = (Integer)(p.remove());
623 assertFalse(q.contains(I));
629 * toArray contains all elements
631 public void testToArray() throws InterruptedException {
632 ArrayBlockingQueue q = populatedQueue(SIZE);
633 Object[] o = q.toArray();
634 for (int i = 0; i < o.length; i++)
635 assertEquals(o[i], q.take());
639 * toArray(a) contains all elements
641 public void testToArray2() throws InterruptedException {
642 ArrayBlockingQueue q = populatedQueue(SIZE);
643 Integer[] ints = new Integer[SIZE];
644 ints = (Integer[])q.toArray(ints);
645 for (int i = 0; i < ints.length; i++)
646 assertEquals(ints[i], q.take());
650 * toArray(null) throws NPE
652 public void testToArray_BadArg() {
653 ArrayBlockingQueue q = populatedQueue(SIZE);
655 Object o[] = q.toArray(null);
657 } catch (NullPointerException success) {}
661 * toArray with incompatible array type throws CCE
663 public void testToArray1_BadArg() {
664 ArrayBlockingQueue q = populatedQueue(SIZE);
666 Object o[] = q.toArray(new String[10]);
668 } catch (ArrayStoreException success) {}
673 * iterator iterates through all elements
675 public void testIterator() throws InterruptedException {
676 ArrayBlockingQueue q = populatedQueue(SIZE);
677 Iterator it = q.iterator();
678 while (it.hasNext()) {
679 assertEquals(it.next(), q.take());
684 * iterator.remove removes current element
686 public void testIteratorRemove () {
687 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
692 Iterator it = q.iterator();
697 assertSame(it.next(), one);
698 assertSame(it.next(), three);
699 assertFalse(it.hasNext());
703 * iterator ordering is FIFO
705 public void testIteratorOrdering() {
706 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
711 assertEquals("queue should be full", 0, q.remainingCapacity());
714 for (Iterator it = q.iterator(); it.hasNext();) {
715 assertEquals(++k, it.next());
721 * Modifications do not cause iterators to fail
723 public void testWeaklyConsistentIteration () {
724 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
728 for (Iterator it = q.iterator(); it.hasNext();) {
732 assertEquals(0, q.size());
737 * toString contains toStrings of elements
739 public void testToString() {
740 ArrayBlockingQueue q = populatedQueue(SIZE);
741 String s = q.toString();
742 for (int i = 0; i < SIZE; ++i) {
743 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
749 * offer transfers elements across Executor tasks
751 public void testOfferInExecutor() {
752 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
755 ExecutorService executor = Executors.newFixedThreadPool(2);
756 executor.execute(new CheckedRunnable() {
757 public void realRun() throws InterruptedException {
758 assertFalse(q.offer(three));
759 assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
760 assertEquals(0, q.remainingCapacity());
763 executor.execute(new CheckedRunnable() {
764 public void realRun() throws InterruptedException {
765 Thread.sleep(SMALL_DELAY_MS);
766 assertSame(one, q.take());
773 * poll retrieves elements across Executor threads
775 public void testPollInExecutor() {
776 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
777 ExecutorService executor = Executors.newFixedThreadPool(2);
778 executor.execute(new CheckedRunnable() {
779 public void realRun() throws InterruptedException {
780 assertNull(q.poll());
781 assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
782 assertTrue(q.isEmpty());
785 executor.execute(new CheckedRunnable() {
786 public void realRun() throws InterruptedException {
787 Thread.sleep(SMALL_DELAY_MS);
795 * A deserialized serialized queue has same elements in same order
797 public void testSerialization() throws Exception {
798 ArrayBlockingQueue q = populatedQueue(SIZE);
800 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
801 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
805 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
806 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
807 ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
808 assertEquals(q.size(), r.size());
810 assertEquals(q.remove(), r.remove());
814 * drainTo(null) throws NPE
816 public void testDrainToNull() {
817 ArrayBlockingQueue q = populatedQueue(SIZE);
821 } catch (NullPointerException success) {}
825 * drainTo(this) throws IAE
827 public void testDrainToSelf() {
828 ArrayBlockingQueue q = populatedQueue(SIZE);
832 } catch (IllegalArgumentException success) {}
836 * drainTo(c) empties queue into another collection c
838 public void testDrainTo() {
839 ArrayBlockingQueue q = populatedQueue(SIZE);
840 ArrayList l = new ArrayList();
842 assertEquals(q.size(), 0);
843 assertEquals(l.size(), SIZE);
844 for (int i = 0; i < SIZE; ++i)
845 assertEquals(l.get(i), new Integer(i));
848 assertFalse(q.isEmpty());
849 assertTrue(q.contains(zero));
850 assertTrue(q.contains(one));
853 assertEquals(q.size(), 0);
854 assertEquals(l.size(), 2);
855 for (int i = 0; i < 2; ++i)
856 assertEquals(l.get(i), new Integer(i));
860 * drainTo empties full queue, unblocking a waiting put.
862 public void testDrainToWithActivePut() throws InterruptedException {
863 final ArrayBlockingQueue q = populatedQueue(SIZE);
864 Thread t = new Thread(new CheckedRunnable() {
865 public void realRun() throws InterruptedException {
866 q.put(new Integer(SIZE+1));
870 ArrayList l = new ArrayList();
872 assertTrue(l.size() >= SIZE);
873 for (int i = 0; i < SIZE; ++i)
874 assertEquals(l.get(i), new Integer(i));
876 assertTrue(q.size() + l.size() >= SIZE);
880 * drainTo(null, n) throws NPE
882 public void testDrainToNullN() {
883 ArrayBlockingQueue q = populatedQueue(SIZE);
887 } catch (NullPointerException success) {}
891 * drainTo(this, n) throws IAE
893 public void testDrainToSelfN() {
894 ArrayBlockingQueue q = populatedQueue(SIZE);
898 } catch (IllegalArgumentException success) {}
902 * drainTo(c, n) empties first max {n, size} elements of queue into c
904 public void testDrainToN() {
905 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
906 for (int i = 0; i < SIZE + 2; ++i) {
907 for (int j = 0; j < SIZE; j++)
908 assertTrue(q.offer(new Integer(j)));
909 ArrayList l = new ArrayList();
911 int k = (i < SIZE)? i : SIZE;
912 assertEquals(l.size(), k);
913 assertEquals(q.size(), SIZE-k);
914 for (int j = 0; j < k; ++j)
915 assertEquals(l.get(j), new Integer(j));
916 while (q.poll() != null) ;