ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ArrayBlockingQueueTest.java (file contents):
Revision 1.47 by jsr166, Fri May 27 20:07:24 2011 UTC vs.
Revision 1.73 by jsr166, Mon Oct 17 01:52:04 2016 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.io.*;
10 >
11 > import java.util.ArrayList;
12 > import java.util.Arrays;
13 > import java.util.Collection;
14 > import java.util.Iterator;
15 > import java.util.NoSuchElementException;
16 > import java.util.Queue;
17 > import java.util.concurrent.ArrayBlockingQueue;
18 > import java.util.concurrent.BlockingQueue;
19 > import java.util.concurrent.CountDownLatch;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 >
23 > import junit.framework.Test;
24  
25   public class ArrayBlockingQueueTest extends JSR166TestCase {
26  
27      public static class Fair extends BlockingQueueTest {
28          protected BlockingQueue emptyCollection() {
29 <            return new ArrayBlockingQueue(20, true);
29 >            return new ArrayBlockingQueue(SIZE, true);
30          }
31      }
32  
33      public static class NonFair extends BlockingQueueTest {
34          protected BlockingQueue emptyCollection() {
35 <            return new ArrayBlockingQueue(20, false);
35 >            return new ArrayBlockingQueue(SIZE, false);
36          }
37      }
38  
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run(suite());
40 >        main(suite(), args);
41      }
42  
43      public static Test suite() {
44 +        class Implementation implements CollectionImplementation {
45 +            public Class<?> klazz() { return ArrayBlockingQueue.class; }
46 +            public Collection emptyCollection() { return new ArrayBlockingQueue(SIZE, false); }
47 +            public Object makeElement(int i) { return i; }
48 +            public boolean isConcurrent() { return true; }
49 +            public boolean permitsNulls() { return false; }
50 +        }
51          return newTestSuite(ArrayBlockingQueueTest.class,
52                              new Fair().testSuite(),
53 <                            new NonFair().testSuite());
53 >                            new NonFair().testSuite(),
54 >                            CollectionTest.testSuite(new Implementation()));
55      }
56  
57      /**
58 <     * Create a queue of given size containing consecutive
59 <     * Integers 0 ... n.
58 >     * Returns a new queue of given size containing consecutive
59 >     * Integers 0 ... n - 1.
60       */
61      private ArrayBlockingQueue<Integer> populatedQueue(int n) {
62          ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
# Line 48 | Line 66 | public class ArrayBlockingQueueTest exte
66          assertFalse(q.isEmpty());
67          assertEquals(0, q.remainingCapacity());
68          assertEquals(n, q.size());
69 +        assertEquals((Integer) 0, q.peek());
70          return q;
71      }
72  
# Line 63 | Line 82 | public class ArrayBlockingQueueTest exte
82       */
83      public void testConstructor2() {
84          try {
85 <            ArrayBlockingQueue q = new ArrayBlockingQueue(0);
85 >            new ArrayBlockingQueue(0);
86              shouldThrow();
87          } catch (IllegalArgumentException success) {}
88      }
# Line 73 | Line 92 | public class ArrayBlockingQueueTest exte
92       */
93      public void testConstructor3() {
94          try {
95 <            ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
95 >            new ArrayBlockingQueue(1, true, null);
96              shouldThrow();
97          } catch (NullPointerException success) {}
98      }
# Line 82 | Line 101 | public class ArrayBlockingQueueTest exte
101       * Initializing from Collection of null elements throws NPE
102       */
103      public void testConstructor4() {
104 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
105          try {
106 <            Integer[] ints = new Integer[SIZE];
87 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
106 >            new ArrayBlockingQueue(SIZE, false, elements);
107              shouldThrow();
108          } catch (NullPointerException success) {}
109      }
# Line 93 | Line 112 | public class ArrayBlockingQueueTest exte
112       * Initializing from Collection with some null elements throws NPE
113       */
114      public void testConstructor5() {
115 +        Integer[] ints = new Integer[SIZE];
116 +        for (int i = 0; i < SIZE - 1; ++i)
117 +            ints[i] = i;
118 +        Collection<Integer> elements = Arrays.asList(ints);
119          try {
120 <            Integer[] ints = new Integer[SIZE];
98 <            for (int i = 0; i < SIZE-1; ++i)
99 <                ints[i] = new Integer(i);
100 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
120 >            new ArrayBlockingQueue(SIZE, false, elements);
121              shouldThrow();
122          } catch (NullPointerException success) {}
123      }
# Line 106 | Line 126 | public class ArrayBlockingQueueTest exte
126       * Initializing from too large collection throws IAE
127       */
128      public void testConstructor6() {
129 +        Integer[] ints = new Integer[SIZE];
130 +        for (int i = 0; i < SIZE; ++i)
131 +            ints[i] = i;
132 +        Collection<Integer> elements = Arrays.asList(ints);
133          try {
134 <            Integer[] ints = new Integer[SIZE];
111 <            for (int i = 0; i < SIZE; ++i)
112 <                ints[i] = new Integer(i);
113 <            ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
134 >            new ArrayBlockingQueue(SIZE - 1, false, elements);
135              shouldThrow();
136          } catch (IllegalArgumentException success) {}
137      }
# Line 121 | Line 142 | public class ArrayBlockingQueueTest exte
142      public void testConstructor7() {
143          Integer[] ints = new Integer[SIZE];
144          for (int i = 0; i < SIZE; ++i)
145 <            ints[i] = new Integer(i);
146 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
145 >            ints[i] = i;
146 >        Collection<Integer> elements = Arrays.asList(ints);
147 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, elements);
148          for (int i = 0; i < SIZE; ++i)
149              assertEquals(ints[i], q.poll());
150      }
# Line 146 | Line 168 | public class ArrayBlockingQueueTest exte
168       * remainingCapacity decreases on add, increases on remove
169       */
170      public void testRemainingCapacity() {
171 <        ArrayBlockingQueue q = populatedQueue(SIZE);
171 >        BlockingQueue q = populatedQueue(SIZE);
172          for (int i = 0; i < SIZE; ++i) {
173              assertEquals(i, q.remainingCapacity());
174 <            assertEquals(SIZE-i, q.size());
175 <            q.remove();
174 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
175 >            assertEquals(i, q.remove());
176          }
177          for (int i = 0; i < SIZE; ++i) {
178 <            assertEquals(SIZE-i, q.remainingCapacity());
179 <            assertEquals(i, q.size());
180 <            q.add(new Integer(i));
178 >            assertEquals(SIZE - i, q.remainingCapacity());
179 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
180 >            assertTrue(q.add(i));
181          }
182      }
183  
184      /**
163     * offer(null) throws NPE
164     */
165    public void testOfferNull() {
166        try {
167            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
168            q.offer(null);
169            shouldThrow();
170        } catch (NullPointerException success) {}
171    }
172
173    /**
174     * add(null) throws NPE
175     */
176    public void testAddNull() {
177        try {
178            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
179            q.add(null);
180            shouldThrow();
181        } catch (NullPointerException success) {}
182    }
183
184    /**
185       * Offer succeeds if not full; fails if full
186       */
187      public void testOffer() {
# Line 194 | Line 194 | public class ArrayBlockingQueueTest exte
194       * add succeeds if not full; throws ISE if full
195       */
196      public void testAdd() {
197 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
198 +        for (int i = 0; i < SIZE; ++i) {
199 +            assertTrue(q.add(new Integer(i)));
200 +        }
201 +        assertEquals(0, q.remainingCapacity());
202          try {
198            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
199            for (int i = 0; i < SIZE; ++i) {
200                assertTrue(q.add(new Integer(i)));
201            }
202            assertEquals(0, q.remainingCapacity());
203              q.add(new Integer(SIZE));
204              shouldThrow();
205          } catch (IllegalStateException success) {}
206      }
207  
208      /**
209     * addAll(null) throws NPE
210     */
211    public void testAddAll1() {
212        try {
213            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
214            q.addAll(null);
215            shouldThrow();
216        } catch (NullPointerException success) {}
217    }
218
219    /**
209       * addAll(this) throws IAE
210       */
211      public void testAddAllSelf() {
212 +        ArrayBlockingQueue q = populatedQueue(SIZE);
213          try {
224            ArrayBlockingQueue q = populatedQueue(SIZE);
214              q.addAll(q);
215              shouldThrow();
216          } catch (IllegalArgumentException success) {}
217      }
218  
219      /**
231     * addAll of a collection with null elements throws NPE
232     */
233    public void testAddAll2() {
234        try {
235            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
236            Integer[] ints = new Integer[SIZE];
237            q.addAll(Arrays.asList(ints));
238            shouldThrow();
239        } catch (NullPointerException success) {}
240    }
241
242    /**
220       * addAll of a collection with any null elements throws NPE after
221       * possibly adding some elements
222       */
223      public void testAddAll3() {
224 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
225 +        Integer[] ints = new Integer[SIZE];
226 +        for (int i = 0; i < SIZE - 1; ++i)
227 +            ints[i] = new Integer(i);
228          try {
248            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
249            Integer[] ints = new Integer[SIZE];
250            for (int i = 0; i < SIZE-1; ++i)
251                ints[i] = new Integer(i);
229              q.addAll(Arrays.asList(ints));
230              shouldThrow();
231          } catch (NullPointerException success) {}
# Line 258 | Line 235 | public class ArrayBlockingQueueTest exte
235       * addAll throws ISE if not enough room
236       */
237      public void testAddAll4() {
238 +        ArrayBlockingQueue q = new ArrayBlockingQueue(1);
239 +        Integer[] ints = new Integer[SIZE];
240 +        for (int i = 0; i < SIZE; ++i)
241 +            ints[i] = new Integer(i);
242          try {
262            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
263            Integer[] ints = new Integer[SIZE];
264            for (int i = 0; i < SIZE; ++i)
265                ints[i] = new Integer(i);
243              q.addAll(Arrays.asList(ints));
244              shouldThrow();
245          } catch (IllegalStateException success) {}
# Line 284 | Line 261 | public class ArrayBlockingQueueTest exte
261      }
262  
263      /**
287     * put(null) throws NPE
288     */
289    public void testPutNull() throws InterruptedException {
290        try {
291            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
292            q.put(null);
293            shouldThrow();
294        } catch (NullPointerException success) {}
295    }
296
297    /**
264       * all elements successfully put are contained
265       */
266      public void testPut() throws InterruptedException {
267          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
268          for (int i = 0; i < SIZE; ++i) {
269 <            Integer I = new Integer(i);
270 <            q.put(I);
271 <            assertTrue(q.contains(I));
269 >            Integer x = new Integer(i);
270 >            q.put(x);
271 >            assertTrue(q.contains(x));
272          }
273          assertEquals(0, q.remainingCapacity());
274      }
# Line 367 | Line 333 | public class ArrayBlockingQueueTest exte
333              }});
334  
335          await(pleaseTake);
336 <        assertEquals(q.remainingCapacity(), 0);
336 >        assertEquals(0, q.remainingCapacity());
337          assertEquals(0, q.take());
338  
339          await(pleaseInterrupt);
340          assertThreadStaysAlive(t);
341          t.interrupt();
342          awaitTermination(t);
343 <        assertEquals(q.remainingCapacity(), 0);
343 >        assertEquals(0, q.remainingCapacity());
344      }
345  
346      /**
# Line 494 | Line 460 | public class ArrayBlockingQueueTest exte
460          final CountDownLatch aboutToWait = new CountDownLatch(1);
461          Thread t = newStartedThread(new CheckedRunnable() {
462              public void realRun() throws InterruptedException {
463 +                long startTime = System.nanoTime();
464                  for (int i = 0; i < SIZE; ++i) {
498                    long t0 = System.nanoTime();
465                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
500                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
466                  }
502                long t0 = System.nanoTime();
467                  aboutToWait.countDown();
468                  try {
469 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
469 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
470                      shouldThrow();
471                  } catch (InterruptedException success) {
472 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
472 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
473                  }
474              }});
475  
476 <        aboutToWait.await();
477 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
476 >        await(aboutToWait);
477 >        waitForThreadToEnterWaitState(t);
478          t.interrupt();
479 <        awaitTermination(t, MEDIUM_DELAY_MS);
479 >        awaitTermination(t);
480          checkEmpty(q);
481      }
482  
# Line 560 | Line 524 | public class ArrayBlockingQueueTest exte
524      }
525  
526      /**
563     * remove(x) removes x and returns true if present
564     */
565    public void testRemoveElement() {
566        ArrayBlockingQueue q = populatedQueue(SIZE);
567        for (int i = 1; i < SIZE; i+=2) {
568            assertTrue(q.remove(new Integer(i)));
569        }
570        for (int i = 0; i < SIZE; i+=2) {
571            assertTrue(q.remove(new Integer(i)));
572            assertFalse(q.remove(new Integer(i+1)));
573        }
574        assertTrue(q.isEmpty());
575    }
576
577    /**
527       * contains(x) reports true when elements added but not yet removed
528       */
529      public void testContains() {
# Line 630 | Line 579 | public class ArrayBlockingQueueTest exte
579                  assertTrue(changed);
580  
581              assertTrue(q.containsAll(p));
582 <            assertEquals(SIZE-i, q.size());
582 >            assertEquals(SIZE - i, q.size());
583              p.remove();
584          }
585      }
# Line 643 | Line 592 | public class ArrayBlockingQueueTest exte
592              ArrayBlockingQueue q = populatedQueue(SIZE);
593              ArrayBlockingQueue p = populatedQueue(i);
594              assertTrue(q.removeAll(p));
595 <            assertEquals(SIZE-i, q.size());
595 >            assertEquals(SIZE - i, q.size());
596              for (int j = 0; j < i; ++j) {
597 <                Integer I = (Integer)(p.remove());
598 <                assertFalse(q.contains(I));
597 >                Integer x = (Integer)(p.remove());
598 >                assertFalse(q.contains(x));
599              }
600          }
601      }
602  
603 <    /**
604 <     * toArray contains all elements in FIFO order
656 <     */
657 <    public void testToArray() {
658 <        ArrayBlockingQueue q = populatedQueue(SIZE);
603 >    void checkToArray(ArrayBlockingQueue q) {
604 >        int size = q.size();
605          Object[] o = q.toArray();
606 <        for (int i = 0; i < o.length; i++)
607 <            assertSame(o[i], q.poll());
606 >        assertEquals(size, o.length);
607 >        Iterator it = q.iterator();
608 >        for (int i = 0; i < size; i++) {
609 >            Integer x = (Integer) it.next();
610 >            assertEquals((Integer)o[0] + i, (int) x);
611 >            assertSame(o[i], x);
612 >        }
613      }
614  
615      /**
616 <     * toArray(a) contains all elements in FIFO order
616 >     * toArray() contains all elements in FIFO order
617       */
618 <    public void testToArray2() {
619 <        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
620 <        Integer[] ints = new Integer[SIZE];
621 <        Integer[] array = q.toArray(ints);
622 <        assertSame(ints, array);
623 <        for (int i = 0; i < ints.length; i++)
624 <            assertSame(ints[i], q.poll());
618 >    public void testToArray() {
619 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
620 >        for (int i = 0; i < SIZE; i++) {
621 >            checkToArray(q);
622 >            q.add(i);
623 >        }
624 >        // Provoke wraparound
625 >        for (int i = 0; i < SIZE; i++) {
626 >            checkToArray(q);
627 >            assertEquals(i, q.poll());
628 >            checkToArray(q);
629 >            q.add(SIZE + i);
630 >        }
631 >        for (int i = 0; i < SIZE; i++) {
632 >            checkToArray(q);
633 >            assertEquals(SIZE + i, q.poll());
634 >        }
635 >    }
636 >
637 >    void checkToArray2(ArrayBlockingQueue q) {
638 >        int size = q.size();
639 >        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
640 >        Integer[] a2 = new Integer[size];
641 >        Integer[] a3 = new Integer[size + 2];
642 >        if (size > 0) Arrays.fill(a1, 42);
643 >        Arrays.fill(a2, 42);
644 >        Arrays.fill(a3, 42);
645 >        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
646 >        Integer[] b2 = (Integer[]) q.toArray(a2);
647 >        Integer[] b3 = (Integer[]) q.toArray(a3);
648 >        assertSame(a2, b2);
649 >        assertSame(a3, b3);
650 >        Iterator it = q.iterator();
651 >        for (int i = 0; i < size; i++) {
652 >            Integer x = (Integer) it.next();
653 >            assertSame(b1[i], x);
654 >            assertEquals(b1[0] + i, (int) x);
655 >            assertSame(b2[i], x);
656 >            assertSame(b3[i], x);
657 >        }
658 >        assertNull(a3[size]);
659 >        assertEquals(42, (int) a3[size + 1]);
660 >        if (size > 0) {
661 >            assertNotSame(a1, b1);
662 >            assertEquals(size, b1.length);
663 >            for (int i = 0; i < a1.length; i++) {
664 >                assertEquals(42, (int) a1[i]);
665 >            }
666 >        }
667      }
668  
669      /**
670 <     * toArray(null) throws NullPointerException
670 >     * toArray(a) contains all elements in FIFO order
671       */
672 <    public void testToArray_NullArg() {
673 <        ArrayBlockingQueue q = populatedQueue(SIZE);
674 <        try {
675 <            q.toArray(null);
676 <            shouldThrow();
677 <        } catch (NullPointerException success) {}
672 >    public void testToArray2() {
673 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
674 >        for (int i = 0; i < SIZE; i++) {
675 >            checkToArray2(q);
676 >            q.add(i);
677 >        }
678 >        // Provoke wraparound
679 >        for (int i = 0; i < SIZE; i++) {
680 >            checkToArray2(q);
681 >            assertEquals(i, q.poll());
682 >            checkToArray2(q);
683 >            q.add(SIZE + i);
684 >        }
685 >        for (int i = 0; i < SIZE; i++) {
686 >            checkToArray2(q);
687 >            assertEquals(SIZE + i, q.poll());
688 >        }
689      }
690  
691      /**
# Line 701 | Line 705 | public class ArrayBlockingQueueTest exte
705      public void testIterator() throws InterruptedException {
706          ArrayBlockingQueue q = populatedQueue(SIZE);
707          Iterator it = q.iterator();
708 <        while (it.hasNext()) {
708 >        int i;
709 >        for (i = 0; it.hasNext(); i++)
710 >            assertTrue(q.contains(it.next()));
711 >        assertEquals(i, SIZE);
712 >        assertIteratorExhausted(it);
713 >
714 >        it = q.iterator();
715 >        for (i = 0; it.hasNext(); i++)
716              assertEquals(it.next(), q.take());
717 <        }
717 >        assertEquals(i, SIZE);
718 >        assertIteratorExhausted(it);
719 >    }
720 >
721 >    /**
722 >     * iterator of empty collection has no elements
723 >     */
724 >    public void testEmptyIterator() {
725 >        assertIteratorExhausted(new ArrayBlockingQueue(SIZE).iterator());
726      }
727  
728      /**
# Line 776 | Line 795 | public class ArrayBlockingQueueTest exte
795          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
796          q.add(one);
797          q.add(two);
779        ExecutorService executor = Executors.newFixedThreadPool(2);
798          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
799 <        executor.execute(new CheckedRunnable() {
800 <            public void realRun() throws InterruptedException {
801 <                assertFalse(q.offer(three));
802 <                threadsStarted.await();
803 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
804 <                assertEquals(0, q.remainingCapacity());
805 <            }});
806 <
807 <        executor.execute(new CheckedRunnable() {
808 <            public void realRun() throws InterruptedException {
809 <                threadsStarted.await();
810 <                assertEquals(0, q.remainingCapacity());
811 <                assertSame(one, q.take());
812 <            }});
813 <
814 <        joinPool(executor);
799 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
800 >        try (PoolCleaner cleaner = cleaner(executor)) {
801 >            executor.execute(new CheckedRunnable() {
802 >                public void realRun() throws InterruptedException {
803 >                    assertFalse(q.offer(three));
804 >                    threadsStarted.await();
805 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
806 >                    assertEquals(0, q.remainingCapacity());
807 >                }});
808 >
809 >            executor.execute(new CheckedRunnable() {
810 >                public void realRun() throws InterruptedException {
811 >                    threadsStarted.await();
812 >                    assertEquals(0, q.remainingCapacity());
813 >                    assertSame(one, q.take());
814 >                }});
815 >        }
816      }
817  
818      /**
# Line 802 | Line 821 | public class ArrayBlockingQueueTest exte
821      public void testPollInExecutor() {
822          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
823          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
824 <        ExecutorService executor = Executors.newFixedThreadPool(2);
825 <        executor.execute(new CheckedRunnable() {
826 <            public void realRun() throws InterruptedException {
827 <                assertNull(q.poll());
828 <                threadsStarted.await();
829 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
830 <                checkEmpty(q);
831 <            }});
832 <
833 <        executor.execute(new CheckedRunnable() {
834 <            public void realRun() throws InterruptedException {
835 <                threadsStarted.await();
836 <                q.put(one);
837 <            }});
838 <
839 <        joinPool(executor);
824 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
825 >        try (PoolCleaner cleaner = cleaner(executor)) {
826 >            executor.execute(new CheckedRunnable() {
827 >                public void realRun() throws InterruptedException {
828 >                    assertNull(q.poll());
829 >                    threadsStarted.await();
830 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
831 >                    checkEmpty(q);
832 >                }});
833 >
834 >            executor.execute(new CheckedRunnable() {
835 >                public void realRun() throws InterruptedException {
836 >                    threadsStarted.await();
837 >                    q.put(one);
838 >                }});
839 >        }
840      }
841  
842      /**
843       * A deserialized serialized queue has same elements in same order
844       */
845      public void testSerialization() throws Exception {
846 <        ArrayBlockingQueue q = populatedQueue(SIZE);
846 >        Queue x = populatedQueue(SIZE);
847 >        Queue y = serialClone(x);
848  
849 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
850 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
851 <        out.writeObject(q);
852 <        out.close();
853 <
854 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
855 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
856 <        ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
857 <        assertEquals(q.size(), r.size());
838 <        while (!q.isEmpty())
839 <            assertEquals(q.remove(), r.remove());
840 <    }
841 <
842 <    /**
843 <     * drainTo(null) throws NPE
844 <     */
845 <    public void testDrainToNull() {
846 <        ArrayBlockingQueue q = populatedQueue(SIZE);
847 <        try {
848 <            q.drainTo(null);
849 <            shouldThrow();
850 <        } catch (NullPointerException success) {}
851 <    }
852 <
853 <    /**
854 <     * drainTo(this) throws IAE
855 <     */
856 <    public void testDrainToSelf() {
857 <        ArrayBlockingQueue q = populatedQueue(SIZE);
858 <        try {
859 <            q.drainTo(q);
860 <            shouldThrow();
861 <        } catch (IllegalArgumentException success) {}
849 >        assertNotSame(x, y);
850 >        assertEquals(x.size(), y.size());
851 >        assertEquals(x.toString(), y.toString());
852 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
853 >        while (!x.isEmpty()) {
854 >            assertFalse(y.isEmpty());
855 >            assertEquals(x.remove(), y.remove());
856 >        }
857 >        assertTrue(y.isEmpty());
858      }
859  
860      /**
# Line 868 | Line 864 | public class ArrayBlockingQueueTest exte
864          ArrayBlockingQueue q = populatedQueue(SIZE);
865          ArrayList l = new ArrayList();
866          q.drainTo(l);
867 <        assertEquals(q.size(), 0);
868 <        assertEquals(l.size(), SIZE);
867 >        assertEquals(0, q.size());
868 >        assertEquals(SIZE, l.size());
869          for (int i = 0; i < SIZE; ++i)
870              assertEquals(l.get(i), new Integer(i));
871          q.add(zero);
# Line 879 | Line 875 | public class ArrayBlockingQueueTest exte
875          assertTrue(q.contains(one));
876          l.clear();
877          q.drainTo(l);
878 <        assertEquals(q.size(), 0);
879 <        assertEquals(l.size(), 2);
878 >        assertEquals(0, q.size());
879 >        assertEquals(2, l.size());
880          for (int i = 0; i < 2; ++i)
881              assertEquals(l.get(i), new Integer(i));
882      }
# Line 892 | Line 888 | public class ArrayBlockingQueueTest exte
888          final ArrayBlockingQueue q = populatedQueue(SIZE);
889          Thread t = new Thread(new CheckedRunnable() {
890              public void realRun() throws InterruptedException {
891 <                q.put(new Integer(SIZE+1));
891 >                q.put(new Integer(SIZE + 1));
892              }});
893  
894          t.start();
# Line 906 | Line 902 | public class ArrayBlockingQueueTest exte
902      }
903  
904      /**
909     * drainTo(null, n) throws NPE
910     */
911    public void testDrainToNullN() {
912        ArrayBlockingQueue q = populatedQueue(SIZE);
913        try {
914            q.drainTo(null, 0);
915            shouldThrow();
916        } catch (NullPointerException success) {}
917    }
918
919    /**
920     * drainTo(this, n) throws IAE
921     */
922    public void testDrainToSelfN() {
923        ArrayBlockingQueue q = populatedQueue(SIZE);
924        try {
925            q.drainTo(q, 0);
926            shouldThrow();
927        } catch (IllegalArgumentException success) {}
928    }
929
930    /**
905       * drainTo(c, n) empties first min(n, size) elements of queue into c
906       */
907      public void testDrainToN() {
908 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
908 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2);
909          for (int i = 0; i < SIZE + 2; ++i) {
910              for (int j = 0; j < SIZE; j++)
911                  assertTrue(q.offer(new Integer(j)));
912              ArrayList l = new ArrayList();
913              q.drainTo(l, i);
914              int k = (i < SIZE) ? i : SIZE;
915 <            assertEquals(l.size(), k);
916 <            assertEquals(q.size(), SIZE-k);
915 >            assertEquals(k, l.size());
916 >            assertEquals(SIZE - k, q.size());
917              for (int j = 0; j < k; ++j)
918                  assertEquals(l.get(j), new Integer(j));
919 <            while (q.poll() != null) ;
919 >            do {} while (q.poll() != null);
920          }
921      }
922  
923 +    /**
924 +     * remove(null), contains(null) always return false
925 +     */
926 +    public void testNeverContainsNull() {
927 +        Collection<?>[] qs = {
928 +            new ArrayBlockingQueue<Object>(10),
929 +            populatedQueue(2),
930 +        };
931 +
932 +        for (Collection<?> q : qs) {
933 +            assertFalse(q.contains(null));
934 +            assertFalse(q.remove(null));
935 +        }
936 +    }
937   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines