OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / tests / api / java / util / concurrent / ArrayBlockingQueueTest.java
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 package tests.api.java.util.concurrent; // android-added
10
11 import junit.framework.*;
12 import java.util.*;
13 import java.util.concurrent.*;
14 import static java.util.concurrent.TimeUnit.MILLISECONDS;
15 import java.io.*;
16
17 public class ArrayBlockingQueueTest extends JSR166TestCase {
18     public static Test suite() {
19         return new TestSuite(ArrayBlockingQueueTest.class);
20     }
21
22     /**
23      * Create a queue of given size containing consecutive
24      * Integers 0 ... n.
25      */
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());
34         return q;
35     }
36
37     /**
38      * A new queue has the indicated capacity
39      */
40     public void testConstructor1() {
41         assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
42     }
43
44     /**
45      * Constructor throws IAE if capacity argument nonpositive
46      */
47     public void testConstructor2() {
48         try {
49             ArrayBlockingQueue q = new ArrayBlockingQueue(0);
50             shouldThrow();
51         } catch (IllegalArgumentException success) {}
52     }
53
54     /**
55      * Initializing from null Collection throws NPE
56      */
57     public void testConstructor3() {
58         try {
59             ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
60             shouldThrow();
61         } catch (NullPointerException success) {}
62     }
63
64     /**
65      * Initializing from Collection of null elements throws NPE
66      */
67     public void testConstructor4() {
68         try {
69             Integer[] ints = new Integer[SIZE];
70             ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
71             shouldThrow();
72         } catch (NullPointerException success) {}
73     }
74
75     /**
76      * Initializing from Collection with some null elements throws NPE
77      */
78     public void testConstructor5() {
79         try {
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));
84             shouldThrow();
85         } catch (NullPointerException success) {}
86     }
87
88     /**
89      * Initializing from too large collection throws IAE
90      */
91     public void testConstructor6() {
92         try {
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));
97             shouldThrow();
98         } catch (IllegalArgumentException success) {}
99     }
100
101     /**
102      * Queue contains all elements of collection used to initialize
103      */
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());
111     }
112
113     /**
114      * Queue transitions from empty to full when elements added
115      */
116     public void testEmptyFull() {
117         ArrayBlockingQueue q = new ArrayBlockingQueue(2);
118         assertTrue(q.isEmpty());
119         assertEquals(2, q.remainingCapacity());
120         q.add(one);
121         assertFalse(q.isEmpty());
122         q.add(two);
123         assertFalse(q.isEmpty());
124         assertEquals(0, q.remainingCapacity());
125         assertFalse(q.offer(three));
126     }
127
128     /**
129      * remainingCapacity decreases on add, increases on remove
130      */
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());
136             q.remove();
137         }
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));
142         }
143     }
144
145     /**
146      *  offer(null) throws NPE
147      */
148     public void testOfferNull() {
149         try {
150             ArrayBlockingQueue q = new ArrayBlockingQueue(1);
151             q.offer(null);
152             shouldThrow();
153         } catch (NullPointerException success) {}
154     }
155
156     /**
157      *  add(null) throws NPE
158      */
159     public void testAddNull() {
160         try {
161             ArrayBlockingQueue q = new ArrayBlockingQueue(1);
162             q.add(null);
163             shouldThrow();
164         } catch (NullPointerException success) {}
165     }
166
167     /**
168      * Offer succeeds if not full; fails if full
169      */
170     public void testOffer() {
171         ArrayBlockingQueue q = new ArrayBlockingQueue(1);
172         assertTrue(q.offer(zero));
173         assertFalse(q.offer(one));
174     }
175
176     /**
177      * add succeeds if not full; throws ISE if full
178      */
179     public void testAdd() {
180         try {
181             ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
182             for (int i = 0; i < SIZE; ++i) {
183                 assertTrue(q.add(new Integer(i)));
184             }
185             assertEquals(0, q.remainingCapacity());
186             q.add(new Integer(SIZE));
187             shouldThrow();
188         } catch (IllegalStateException success) {}
189     }
190
191     /**
192      *  addAll(null) throws NPE
193      */
194     public void testAddAll1() {
195         try {
196             ArrayBlockingQueue q = new ArrayBlockingQueue(1);
197             q.addAll(null);
198             shouldThrow();
199         } catch (NullPointerException success) {}
200     }
201
202     /**
203      * addAll(this) throws IAE
204      */
205     public void testAddAllSelf() {
206         try {
207             ArrayBlockingQueue q = populatedQueue(SIZE);
208             q.addAll(q);
209             shouldThrow();
210         } catch (IllegalArgumentException success) {}
211     }
212
213
214     /**
215      *  addAll of a collection with null elements throws NPE
216      */
217     public void testAddAll2() {
218         try {
219             ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
220             Integer[] ints = new Integer[SIZE];
221             q.addAll(Arrays.asList(ints));
222             shouldThrow();
223         } catch (NullPointerException success) {}
224     }
225     /**
226      * addAll of a collection with any null elements throws NPE after
227      * possibly adding some elements
228      */
229     public void testAddAll3() {
230         try {
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));
236             shouldThrow();
237         } catch (NullPointerException success) {}
238     }
239     /**
240      * addAll throws ISE if not enough room
241      */
242     public void testAddAll4() {
243         try {
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));
249             shouldThrow();
250         } catch (IllegalStateException success) {}
251     }
252     /**
253      * Queue contains all elements, in traversal order, of successful addAll
254      */
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());
265     }
266
267     /**
268      *  put(null) throws NPE
269      */
270     public void testPutNull() throws InterruptedException {
271         try {
272             ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
273             q.put(null);
274             shouldThrow();
275         } catch (NullPointerException success) {}
276      }
277
278     /**
279      * all elements successfully put are contained
280      */
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);
285             q.put(I);
286             assertTrue(q.contains(I));
287         }
288         assertEquals(0, q.remainingCapacity());
289     }
290
291     /**
292      * put blocks interruptibly if full
293      */
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)
299                     q.put(i);
300                 assertEquals(SIZE, q.size());
301                 assertEquals(0, q.remainingCapacity());
302                 try {
303                     q.put(99);
304                     shouldThrow();
305                 } catch (InterruptedException success) {}
306             }});
307
308         t.start();
309         Thread.sleep(SHORT_DELAY_MS);
310         t.interrupt();
311         t.join();
312         assertEquals(SIZE, q.size());
313         assertEquals(0, q.remainingCapacity());
314     }
315
316     /**
317      * put blocks waiting for take when full
318      */
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++)
325                     q.put(i);
326                 try {
327                     q.put(99);
328                     shouldThrow();
329                 } catch (InterruptedException success) {}
330             }});
331
332         t.start();
333         Thread.sleep(SHORT_DELAY_MS);
334         assertEquals(q.remainingCapacity(), 0);
335         assertEquals(0, q.take());
336         Thread.sleep(SHORT_DELAY_MS);
337         t.interrupt();
338         t.join();
339         assertEquals(q.remainingCapacity(), 0);
340     }
341
342     /**
343      * timed offer times out if full and elements not taken
344      */
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 {
349                 q.put(new Object());
350                 q.put(new Object());
351                 assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
352                 try {
353                     q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
354                     shouldThrow();
355                 } catch (InterruptedException success) {}
356             }});
357
358         t.start();
359         Thread.sleep(SHORT_DELAY_MS);
360         t.interrupt();
361         t.join();
362     }
363
364     /**
365      * take retrieves elements in FIFO order
366      */
367     public void testTake() throws InterruptedException {
368         ArrayBlockingQueue q = populatedQueue(SIZE);
369         for (int i = 0; i < SIZE; ++i) {
370             assertEquals(i, q.take());
371         }
372     }
373
374     /**
375      * take blocks interruptibly when empty
376      */
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 {
381                 q.take();
382             }};
383
384         t.start();
385         Thread.sleep(SHORT_DELAY_MS);
386         t.interrupt();
387         t.join();
388     }
389
390     /**
391      * Take removes existing elements until empty, then blocks interruptibly
392      */
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());
399                 }
400                 try {
401                     q.take();
402                     shouldThrow();
403                 } catch (InterruptedException success) {}
404             }});
405
406         t.start();
407         Thread.sleep(SHORT_DELAY_MS);
408         t.interrupt();
409         t.join();
410     }
411
412
413     /**
414      * poll succeeds unless empty
415      */
416     public void testPoll() {
417         ArrayBlockingQueue q = populatedQueue(SIZE);
418         for (int i = 0; i < SIZE; ++i) {
419             assertEquals(i, q.poll());
420         }
421         assertNull(q.poll());
422     }
423
424     /**
425      * timed pool with zero timeout succeeds when non-empty, else times out
426      */
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));
431         }
432         assertNull(q.poll(0, MILLISECONDS));
433     }
434
435     /**
436      * timed pool with nonzero timeout succeeds when non-empty, else times out
437      */
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));
442         }
443         assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
444     }
445
446     /**
447      * Interrupted timed poll throws InterruptedException instead of
448      * returning timeout status
449      */
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));;
456                 }
457                 try {
458                     q.poll(SMALL_DELAY_MS, MILLISECONDS);
459                     shouldThrow();
460                 } catch (InterruptedException success) {}
461             }});
462
463         t.start();
464         Thread.sleep(SHORT_DELAY_MS);
465         t.interrupt();
466         t.join();
467     }
468
469     /**
470      *  timed poll before a delayed offer fails; after offer succeeds;
471      *  on interruption throws
472      */
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));
479                 try {
480                     q.poll(LONG_DELAY_MS, MILLISECONDS);
481                     shouldThrow();
482                 } catch (InterruptedException success) {}
483             }});
484
485         t.start();
486         Thread.sleep(SMALL_DELAY_MS);
487         assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
488         t.interrupt();
489         t.join();
490     }
491
492
493     /**
494      * peek returns next element, or null if empty
495      */
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));
503         }
504         assertNull(q.peek());
505     }
506
507     /**
508      * element returns next element, or throws NSEE if empty
509      */
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());
515         }
516         try {
517             q.element();
518             shouldThrow();
519         } catch (NoSuchElementException success) {}
520     }
521
522     /**
523      * remove removes next element, or throws NSEE if empty
524      */
525     public void testRemove() {
526         ArrayBlockingQueue q = populatedQueue(SIZE);
527         for (int i = 0; i < SIZE; ++i) {
528             assertEquals(i, q.remove());
529         }
530         try {
531             q.remove();
532             shouldThrow();
533         } catch (NoSuchElementException success) {}
534     }
535
536     /**
537      * remove(x) removes x and returns true if present
538      */
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)));
543         }
544         for (int i = 0; i < SIZE; i+=2) {
545             assertTrue(q.remove(new Integer(i)));
546             assertFalse(q.remove(new Integer(i+1)));
547         }
548         assertTrue(q.isEmpty());
549     }
550
551     /**
552      * contains(x) reports true when elements added but not yet removed
553      */
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)));
560         }
561     }
562
563     /**
564      * clear removes all elements
565      */
566     public void testClear() {
567         ArrayBlockingQueue q = populatedQueue(SIZE);
568         q.clear();
569         assertTrue(q.isEmpty());
570         assertEquals(0, q.size());
571         assertEquals(SIZE, q.remainingCapacity());
572         q.add(one);
573         assertFalse(q.isEmpty());
574         assertTrue(q.contains(one));
575         q.clear();
576         assertTrue(q.isEmpty());
577     }
578
579     /**
580      * containsAll(c) is true when c contains a subset of elements
581      */
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));
589         }
590         assertTrue(p.containsAll(q));
591     }
592
593     /**
594      * retainAll(c) retains only those elements of c and reports true if changed
595      */
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);
601             if (i == 0)
602                 assertFalse(changed);
603             else
604                 assertTrue(changed);
605
606             assertTrue(q.containsAll(p));
607             assertEquals(SIZE-i, q.size());
608             p.remove();
609         }
610     }
611
612     /**
613      * removeAll(c) removes only those elements of c and reports true if changed
614      */
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));
624             }
625         }
626     }
627
628     /**
629      *  toArray contains all elements
630      */
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());
636     }
637
638     /**
639      * toArray(a) contains all elements
640      */
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());
647     }
648
649     /**
650      * toArray(null) throws NPE
651      */
652     public void testToArray_BadArg() {
653         ArrayBlockingQueue q = populatedQueue(SIZE);
654         try {
655             Object o[] = q.toArray(null);
656             shouldThrow();
657         } catch (NullPointerException success) {}
658     }
659
660     /**
661      * toArray with incompatible array type throws CCE
662      */
663     public void testToArray1_BadArg() {
664         ArrayBlockingQueue q = populatedQueue(SIZE);
665         try {
666             Object o[] = q.toArray(new String[10]);
667             shouldThrow();
668         } catch (ArrayStoreException success) {}
669     }
670
671
672     /**
673      * iterator iterates through all elements
674      */
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());
680         }
681     }
682
683     /**
684      * iterator.remove removes current element
685      */
686     public void testIteratorRemove () {
687         final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
688         q.add(two);
689         q.add(one);
690         q.add(three);
691
692         Iterator it = q.iterator();
693         it.next();
694         it.remove();
695
696         it = q.iterator();
697         assertSame(it.next(), one);
698         assertSame(it.next(), three);
699         assertFalse(it.hasNext());
700     }
701
702     /**
703      * iterator ordering is FIFO
704      */
705     public void testIteratorOrdering() {
706         final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
707         q.add(one);
708         q.add(two);
709         q.add(three);
710
711         assertEquals("queue should be full", 0, q.remainingCapacity());
712
713         int k = 0;
714         for (Iterator it = q.iterator(); it.hasNext();) {
715             assertEquals(++k, it.next());
716         }
717         assertEquals(3, k);
718     }
719
720     /**
721      * Modifications do not cause iterators to fail
722      */
723     public void testWeaklyConsistentIteration () {
724         final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
725         q.add(one);
726         q.add(two);
727         q.add(three);
728         for (Iterator it = q.iterator(); it.hasNext();) {
729             q.remove();
730             it.next();
731         }
732         assertEquals(0, q.size());
733     }
734
735
736     /**
737      * toString contains toStrings of elements
738      */
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);
744         }
745     }
746
747
748     /**
749      * offer transfers elements across Executor tasks
750      */
751     public void testOfferInExecutor() {
752         final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
753         q.add(one);
754         q.add(two);
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());
761             }});
762
763         executor.execute(new CheckedRunnable() {
764             public void realRun() throws InterruptedException {
765                 Thread.sleep(SMALL_DELAY_MS);
766                 assertSame(one, q.take());
767             }});
768
769         joinPool(executor);
770     }
771
772     /**
773      * poll retrieves elements across Executor threads
774      */
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());
783             }});
784
785         executor.execute(new CheckedRunnable() {
786             public void realRun() throws InterruptedException {
787                 Thread.sleep(SMALL_DELAY_MS);
788                 q.put(one);
789             }});
790
791         joinPool(executor);
792     }
793
794     /**
795      * A deserialized serialized queue has same elements in same order
796      */
797     public void testSerialization() throws Exception {
798         ArrayBlockingQueue q = populatedQueue(SIZE);
799
800         ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
801         ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
802         out.writeObject(q);
803         out.close();
804
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());
809         while (!q.isEmpty())
810             assertEquals(q.remove(), r.remove());
811     }
812
813     /**
814      * drainTo(null) throws NPE
815      */
816     public void testDrainToNull() {
817         ArrayBlockingQueue q = populatedQueue(SIZE);
818         try {
819             q.drainTo(null);
820             shouldThrow();
821         } catch (NullPointerException success) {}
822     }
823
824     /**
825      * drainTo(this) throws IAE
826      */
827     public void testDrainToSelf() {
828         ArrayBlockingQueue q = populatedQueue(SIZE);
829         try {
830             q.drainTo(q);
831             shouldThrow();
832         } catch (IllegalArgumentException success) {}
833     }
834
835     /**
836      * drainTo(c) empties queue into another collection c
837      */
838     public void testDrainTo() {
839         ArrayBlockingQueue q = populatedQueue(SIZE);
840         ArrayList l = new ArrayList();
841         q.drainTo(l);
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));
846         q.add(zero);
847         q.add(one);
848         assertFalse(q.isEmpty());
849         assertTrue(q.contains(zero));
850         assertTrue(q.contains(one));
851         l.clear();
852         q.drainTo(l);
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));
857     }
858
859     /**
860      * drainTo empties full queue, unblocking a waiting put.
861      */
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));
867             }});
868
869         t.start();
870         ArrayList l = new ArrayList();
871         q.drainTo(l);
872         assertTrue(l.size() >= SIZE);
873         for (int i = 0; i < SIZE; ++i)
874             assertEquals(l.get(i), new Integer(i));
875         t.join();
876         assertTrue(q.size() + l.size() >= SIZE);
877     }
878
879     /**
880      * drainTo(null, n) throws NPE
881      */
882     public void testDrainToNullN() {
883         ArrayBlockingQueue q = populatedQueue(SIZE);
884         try {
885             q.drainTo(null, 0);
886             shouldThrow();
887         } catch (NullPointerException success) {}
888     }
889
890     /**
891      * drainTo(this, n) throws IAE
892      */
893     public void testDrainToSelfN() {
894         ArrayBlockingQueue q = populatedQueue(SIZE);
895         try {
896             q.drainTo(q, 0);
897             shouldThrow();
898         } catch (IllegalArgumentException success) {}
899     }
900
901     /**
902      * drainTo(c, n) empties first max {n, size} elements of queue into c
903      */
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();
910             q.drainTo(l, i);
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) ;
917         }
918     }
919
920 }