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.76 by jsr166, Sun Nov 6 02:40:38 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 > import java.util.concurrent.ThreadLocalRandom;
23 >
24 > import junit.framework.Test;
25  
26   public class ArrayBlockingQueueTest extends JSR166TestCase {
27  
28 +    public static void main(String[] args) {
29 +        main(suite(), args);
30 +    }
31 +
32 +    public static Test suite() {
33 +        class Implementation implements CollectionImplementation {
34 +            public Class<?> klazz() { return ArrayBlockingQueue.class; }
35 +            public Collection emptyCollection() {
36 +                boolean fair = ThreadLocalRandom.current().nextBoolean();
37 +                return populatedQueue(0, SIZE, 2 * SIZE, fair);
38 +            }
39 +            public Object makeElement(int i) { return i; }
40 +            public boolean isConcurrent() { return true; }
41 +            public boolean permitsNulls() { return false; }
42 +        }
43 +
44 +        return newTestSuite(
45 +            ArrayBlockingQueueTest.class,
46 +            new Fair().testSuite(),
47 +            new NonFair().testSuite(),
48 +            CollectionTest.testSuite(new Implementation()));
49 +    }
50 +
51      public static class Fair extends BlockingQueueTest {
52          protected BlockingQueue emptyCollection() {
53 <            return new ArrayBlockingQueue(20, true);
53 >            return populatedQueue(0, SIZE, 2 * SIZE, true);
54          }
55      }
56  
57      public static class NonFair extends BlockingQueueTest {
58          protected BlockingQueue emptyCollection() {
59 <            return new ArrayBlockingQueue(20, false);
59 >            return populatedQueue(0, SIZE, 2 * SIZE, false);
60          }
61      }
62  
63 <    public static void main(String[] args) {
64 <        junit.textui.TestRunner.run(suite());
65 <    }
66 <
67 <    public static Test suite() {
68 <        return newTestSuite(ArrayBlockingQueueTest.class,
35 <                            new Fair().testSuite(),
36 <                            new NonFair().testSuite());
63 >    /**
64 >     * Returns a new queue of given size containing consecutive
65 >     * Integers 0 ... n - 1.
66 >     */
67 >    static ArrayBlockingQueue<Integer> populatedQueue(int n) {
68 >        return populatedQueue(n, n, n, false);
69      }
70  
71      /**
72 <     * Create a queue of given size containing consecutive
73 <     * Integers 0 ... n.
72 >     * Returns a new queue of given size containing consecutive
73 >     * Integers 0 ... n - 1, with given capacity range and fairness.
74       */
75 <    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
76 <        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
75 >    static ArrayBlockingQueue<Integer> populatedQueue(
76 >        int size, int minCapacity, int maxCapacity, boolean fair) {
77 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
78 >        int capacity = rnd.nextInt(minCapacity, maxCapacity + 1);
79 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(capacity);
80          assertTrue(q.isEmpty());
81 <        for (int i = 0; i < n; i++)
82 <            assertTrue(q.offer(new Integer(i)));
83 <        assertFalse(q.isEmpty());
84 <        assertEquals(0, q.remainingCapacity());
85 <        assertEquals(n, q.size());
81 >        // shuffle circular array elements so they wrap
82 >        {
83 >            int n = rnd.nextInt(capacity);
84 >            for (int i = 0; i < n; i++) q.add(42);
85 >            for (int i = 0; i < n; i++) q.remove();
86 >        }
87 >        for (int i = 0; i < size; i++)
88 >            assertTrue(q.offer((Integer) i));
89 >        assertEquals(size == 0, q.isEmpty());
90 >        assertEquals(capacity - size, q.remainingCapacity());
91 >        assertEquals(size, q.size());
92 >        if (size > 0)
93 >            assertEquals((Integer) 0, q.peek());
94          return q;
95      }
96  
# Line 63 | Line 106 | public class ArrayBlockingQueueTest exte
106       */
107      public void testConstructor2() {
108          try {
109 <            ArrayBlockingQueue q = new ArrayBlockingQueue(0);
109 >            new ArrayBlockingQueue(0);
110              shouldThrow();
111          } catch (IllegalArgumentException success) {}
112      }
# Line 73 | Line 116 | public class ArrayBlockingQueueTest exte
116       */
117      public void testConstructor3() {
118          try {
119 <            ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
119 >            new ArrayBlockingQueue(1, true, null);
120              shouldThrow();
121          } catch (NullPointerException success) {}
122      }
# Line 82 | Line 125 | public class ArrayBlockingQueueTest exte
125       * Initializing from Collection of null elements throws NPE
126       */
127      public void testConstructor4() {
128 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
129          try {
130 <            Integer[] ints = new Integer[SIZE];
87 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
130 >            new ArrayBlockingQueue(SIZE, false, elements);
131              shouldThrow();
132          } catch (NullPointerException success) {}
133      }
# Line 93 | Line 136 | public class ArrayBlockingQueueTest exte
136       * Initializing from Collection with some null elements throws NPE
137       */
138      public void testConstructor5() {
139 +        Integer[] ints = new Integer[SIZE];
140 +        for (int i = 0; i < SIZE - 1; ++i)
141 +            ints[i] = i;
142 +        Collection<Integer> elements = Arrays.asList(ints);
143          try {
144 <            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));
144 >            new ArrayBlockingQueue(SIZE, false, elements);
145              shouldThrow();
146          } catch (NullPointerException success) {}
147      }
# Line 106 | Line 150 | public class ArrayBlockingQueueTest exte
150       * Initializing from too large collection throws IAE
151       */
152      public void testConstructor6() {
153 +        Integer[] ints = new Integer[SIZE];
154 +        for (int i = 0; i < SIZE; ++i)
155 +            ints[i] = i;
156 +        Collection<Integer> elements = Arrays.asList(ints);
157          try {
158 <            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));
158 >            new ArrayBlockingQueue(SIZE - 1, false, elements);
159              shouldThrow();
160          } catch (IllegalArgumentException success) {}
161      }
# Line 121 | Line 166 | public class ArrayBlockingQueueTest exte
166      public void testConstructor7() {
167          Integer[] ints = new Integer[SIZE];
168          for (int i = 0; i < SIZE; ++i)
169 <            ints[i] = new Integer(i);
170 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
169 >            ints[i] = i;
170 >        Collection<Integer> elements = Arrays.asList(ints);
171 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, elements);
172          for (int i = 0; i < SIZE; ++i)
173              assertEquals(ints[i], q.poll());
174      }
# Line 146 | Line 192 | public class ArrayBlockingQueueTest exte
192       * remainingCapacity decreases on add, increases on remove
193       */
194      public void testRemainingCapacity() {
195 <        ArrayBlockingQueue q = populatedQueue(SIZE);
195 >        BlockingQueue q = populatedQueue(SIZE);
196          for (int i = 0; i < SIZE; ++i) {
197              assertEquals(i, q.remainingCapacity());
198 <            assertEquals(SIZE-i, q.size());
199 <            q.remove();
198 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
199 >            assertEquals(i, q.remove());
200          }
201          for (int i = 0; i < SIZE; ++i) {
202 <            assertEquals(SIZE-i, q.remainingCapacity());
203 <            assertEquals(i, q.size());
204 <            q.add(new Integer(i));
202 >            assertEquals(SIZE - i, q.remainingCapacity());
203 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
204 >            assertTrue(q.add(i));
205          }
206      }
207  
208      /**
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    /**
209       * Offer succeeds if not full; fails if full
210       */
211      public void testOffer() {
# Line 194 | Line 218 | public class ArrayBlockingQueueTest exte
218       * add succeeds if not full; throws ISE if full
219       */
220      public void testAdd() {
221 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
222 +        for (int i = 0; i < SIZE; ++i) {
223 +            assertTrue(q.add(new Integer(i)));
224 +        }
225 +        assertEquals(0, q.remainingCapacity());
226          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());
227              q.add(new Integer(SIZE));
228              shouldThrow();
229          } catch (IllegalStateException success) {}
230      }
231  
232      /**
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    /**
233       * addAll(this) throws IAE
234       */
235      public void testAddAllSelf() {
236 +        ArrayBlockingQueue q = populatedQueue(SIZE);
237          try {
224            ArrayBlockingQueue q = populatedQueue(SIZE);
238              q.addAll(q);
239              shouldThrow();
240          } catch (IllegalArgumentException success) {}
241      }
242  
243      /**
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    /**
244       * addAll of a collection with any null elements throws NPE after
245       * possibly adding some elements
246       */
247      public void testAddAll3() {
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);
252          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);
253              q.addAll(Arrays.asList(ints));
254              shouldThrow();
255          } catch (NullPointerException success) {}
# Line 258 | Line 259 | public class ArrayBlockingQueueTest exte
259       * addAll throws ISE if not enough room
260       */
261      public void testAddAll4() {
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);
266          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);
267              q.addAll(Arrays.asList(ints));
268              shouldThrow();
269          } catch (IllegalStateException success) {}
# Line 284 | Line 285 | public class ArrayBlockingQueueTest exte
285      }
286  
287      /**
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    /**
288       * all elements successfully put are contained
289       */
290      public void testPut() throws InterruptedException {
291          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
292          for (int i = 0; i < SIZE; ++i) {
293 <            Integer I = new Integer(i);
294 <            q.put(I);
295 <            assertTrue(q.contains(I));
293 >            Integer x = new Integer(i);
294 >            q.put(x);
295 >            assertTrue(q.contains(x));
296          }
297          assertEquals(0, q.remainingCapacity());
298      }
# Line 367 | Line 357 | public class ArrayBlockingQueueTest exte
357              }});
358  
359          await(pleaseTake);
360 <        assertEquals(q.remainingCapacity(), 0);
360 >        assertEquals(0, q.remainingCapacity());
361          assertEquals(0, q.take());
362  
363          await(pleaseInterrupt);
364          assertThreadStaysAlive(t);
365          t.interrupt();
366          awaitTermination(t);
367 <        assertEquals(q.remainingCapacity(), 0);
367 >        assertEquals(0, q.remainingCapacity());
368      }
369  
370      /**
# Line 494 | Line 484 | public class ArrayBlockingQueueTest exte
484          final CountDownLatch aboutToWait = new CountDownLatch(1);
485          Thread t = newStartedThread(new CheckedRunnable() {
486              public void realRun() throws InterruptedException {
487 +                long startTime = System.nanoTime();
488                  for (int i = 0; i < SIZE; ++i) {
498                    long t0 = System.nanoTime();
489                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
500                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
490                  }
502                long t0 = System.nanoTime();
491                  aboutToWait.countDown();
492                  try {
493 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
493 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
494                      shouldThrow();
495                  } catch (InterruptedException success) {
496 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
496 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
497                  }
498              }});
499  
500 <        aboutToWait.await();
501 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
500 >        await(aboutToWait);
501 >        waitForThreadToEnterWaitState(t);
502          t.interrupt();
503 <        awaitTermination(t, MEDIUM_DELAY_MS);
503 >        awaitTermination(t);
504          checkEmpty(q);
505      }
506  
# Line 560 | Line 548 | public class ArrayBlockingQueueTest exte
548      }
549  
550      /**
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    /**
551       * contains(x) reports true when elements added but not yet removed
552       */
553      public void testContains() {
# Line 630 | Line 603 | public class ArrayBlockingQueueTest exte
603                  assertTrue(changed);
604  
605              assertTrue(q.containsAll(p));
606 <            assertEquals(SIZE-i, q.size());
606 >            assertEquals(SIZE - i, q.size());
607              p.remove();
608          }
609      }
# Line 643 | Line 616 | public class ArrayBlockingQueueTest exte
616              ArrayBlockingQueue q = populatedQueue(SIZE);
617              ArrayBlockingQueue p = populatedQueue(i);
618              assertTrue(q.removeAll(p));
619 <            assertEquals(SIZE-i, q.size());
619 >            assertEquals(SIZE - i, q.size());
620              for (int j = 0; j < i; ++j) {
621 <                Integer I = (Integer)(p.remove());
622 <                assertFalse(q.contains(I));
621 >                Integer x = (Integer)(p.remove());
622 >                assertFalse(q.contains(x));
623              }
624          }
625      }
626  
627 <    /**
628 <     * toArray contains all elements in FIFO order
629 <     */
630 <    public void testToArray() {
631 <        ArrayBlockingQueue q = populatedQueue(SIZE);
632 <        Object[] o = q.toArray();
633 <        for (int i = 0; i < o.length; i++)
634 <            assertSame(o[i], q.poll());
635 <    }
636 <
637 <    /**
638 <     * toArray(a) contains all elements in FIFO order
639 <     */
640 <    public void testToArray2() {
641 <        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
642 <        Integer[] ints = new Integer[SIZE];
643 <        Integer[] array = q.toArray(ints);
644 <        assertSame(ints, array);
645 <        for (int i = 0; i < ints.length; i++)
646 <            assertSame(ints[i], q.poll());
627 >    void checkToArray(ArrayBlockingQueue<Integer> q) {
628 >        int size = q.size();
629 >        Object[] a1 = q.toArray();
630 >        assertEquals(size, a1.length);
631 >        Integer[] a2 = q.toArray(new Integer[0]);
632 >        assertEquals(size, a2.length);
633 >        Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]);
634 >        assertEquals(size, a3.length);
635 >        Integer[] a4 = new Integer[size];
636 >        assertSame(a4, q.toArray(a4));
637 >        Integer[] a5 = new Integer[size + 1];
638 >        Arrays.fill(a5, 42);
639 >        assertSame(a5, q.toArray(a5));
640 >        Integer[] a6 = new Integer[size + 2];
641 >        Arrays.fill(a6, 42);
642 >        assertSame(a6, q.toArray(a6));
643 >        Object[][] as = { a1, a2, a3, a4, a5, a6 };
644 >        for (Object[] a : as) {
645 >            if (a.length > size) assertNull(a[size]);
646 >            if (a.length > size + 1) assertEquals(42, a[size + 1]);
647 >        }
648 >        Iterator it = q.iterator();
649 >        Integer s = q.peek();
650 >        for (int i = 0; i < size; i++) {
651 >            Integer x = (Integer) it.next();
652 >            assertEquals(s + i, (int) x);
653 >            for (Object[] a : as)
654 >                assertSame(a1[i], x);
655 >        }
656      }
657  
658      /**
659 <     * toArray(null) throws NullPointerException
659 >     * toArray() and toArray(a) contain all elements in FIFO order
660       */
661 <    public void testToArray_NullArg() {
662 <        ArrayBlockingQueue q = populatedQueue(SIZE);
663 <        try {
664 <            q.toArray(null);
665 <            shouldThrow();
666 <        } catch (NullPointerException success) {}
661 >    public void testToArray() {
662 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
663 >        final int size = rnd.nextInt(6);
664 >        final int capacity = Math.max(1, size + rnd.nextInt(size + 1));
665 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(capacity);
666 >        for (int i = 0; i < size; i++) {
667 >            checkToArray(q);
668 >            q.add(i);
669 >        }
670 >        // Provoke wraparound
671 >        int added = size * 2;
672 >        for (int i = 0; i < added; i++) {
673 >            checkToArray(q);
674 >            assertEquals((Integer) i, q.poll());
675 >            q.add(size + i);
676 >        }
677 >        for (int i = 0; i < size; i++) {
678 >            checkToArray(q);
679 >            assertEquals((Integer) (added + i), q.poll());
680 >        }
681      }
682  
683      /**
684       * toArray(incompatible array type) throws ArrayStoreException
685       */
686 <    public void testToArray1_BadArg() {
686 >    public void testToArray_incompatibleArrayType() {
687          ArrayBlockingQueue q = populatedQueue(SIZE);
688          try {
689              q.toArray(new String[10]);
690              shouldThrow();
691          } catch (ArrayStoreException success) {}
692 +        try {
693 +            q.toArray(new String[0]);
694 +            shouldThrow();
695 +        } catch (ArrayStoreException success) {}
696      }
697  
698      /**
# Line 701 | Line 701 | public class ArrayBlockingQueueTest exte
701      public void testIterator() throws InterruptedException {
702          ArrayBlockingQueue q = populatedQueue(SIZE);
703          Iterator it = q.iterator();
704 <        while (it.hasNext()) {
704 >        int i;
705 >        for (i = 0; it.hasNext(); i++)
706 >            assertTrue(q.contains(it.next()));
707 >        assertEquals(i, SIZE);
708 >        assertIteratorExhausted(it);
709 >
710 >        it = q.iterator();
711 >        for (i = 0; it.hasNext(); i++)
712              assertEquals(it.next(), q.take());
713 <        }
713 >        assertEquals(i, SIZE);
714 >        assertIteratorExhausted(it);
715 >    }
716 >
717 >    /**
718 >     * iterator of empty collection has no elements
719 >     */
720 >    public void testEmptyIterator() {
721 >        assertIteratorExhausted(new ArrayBlockingQueue(SIZE).iterator());
722      }
723  
724      /**
# Line 776 | Line 791 | public class ArrayBlockingQueueTest exte
791          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
792          q.add(one);
793          q.add(two);
779        ExecutorService executor = Executors.newFixedThreadPool(2);
794          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
795 <        executor.execute(new CheckedRunnable() {
796 <            public void realRun() throws InterruptedException {
797 <                assertFalse(q.offer(three));
798 <                threadsStarted.await();
799 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
800 <                assertEquals(0, q.remainingCapacity());
801 <            }});
802 <
803 <        executor.execute(new CheckedRunnable() {
804 <            public void realRun() throws InterruptedException {
805 <                threadsStarted.await();
806 <                assertEquals(0, q.remainingCapacity());
807 <                assertSame(one, q.take());
808 <            }});
809 <
810 <        joinPool(executor);
795 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
796 >        try (PoolCleaner cleaner = cleaner(executor)) {
797 >            executor.execute(new CheckedRunnable() {
798 >                public void realRun() throws InterruptedException {
799 >                    assertFalse(q.offer(three));
800 >                    threadsStarted.await();
801 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
802 >                    assertEquals(0, q.remainingCapacity());
803 >                }});
804 >
805 >            executor.execute(new CheckedRunnable() {
806 >                public void realRun() throws InterruptedException {
807 >                    threadsStarted.await();
808 >                    assertEquals(0, q.remainingCapacity());
809 >                    assertSame(one, q.take());
810 >                }});
811 >        }
812      }
813  
814      /**
# Line 802 | Line 817 | public class ArrayBlockingQueueTest exte
817      public void testPollInExecutor() {
818          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
819          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
820 <        ExecutorService executor = Executors.newFixedThreadPool(2);
821 <        executor.execute(new CheckedRunnable() {
822 <            public void realRun() throws InterruptedException {
823 <                assertNull(q.poll());
824 <                threadsStarted.await();
825 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
826 <                checkEmpty(q);
827 <            }});
828 <
829 <        executor.execute(new CheckedRunnable() {
830 <            public void realRun() throws InterruptedException {
831 <                threadsStarted.await();
832 <                q.put(one);
833 <            }});
834 <
835 <        joinPool(executor);
820 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
821 >        try (PoolCleaner cleaner = cleaner(executor)) {
822 >            executor.execute(new CheckedRunnable() {
823 >                public void realRun() throws InterruptedException {
824 >                    assertNull(q.poll());
825 >                    threadsStarted.await();
826 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
827 >                    checkEmpty(q);
828 >                }});
829 >
830 >            executor.execute(new CheckedRunnable() {
831 >                public void realRun() throws InterruptedException {
832 >                    threadsStarted.await();
833 >                    q.put(one);
834 >                }});
835 >        }
836      }
837  
838      /**
839       * A deserialized serialized queue has same elements in same order
840       */
841      public void testSerialization() throws Exception {
842 <        ArrayBlockingQueue q = populatedQueue(SIZE);
843 <
829 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
830 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
831 <        out.writeObject(q);
832 <        out.close();
833 <
834 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
835 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
836 <        ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
837 <        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 <    }
842 >        Queue x = populatedQueue(SIZE);
843 >        Queue y = serialClone(x);
844  
845 <    /**
846 <     * drainTo(this) throws IAE
847 <     */
848 <    public void testDrainToSelf() {
849 <        ArrayBlockingQueue q = populatedQueue(SIZE);
850 <        try {
851 <            q.drainTo(q);
852 <            shouldThrow();
853 <        } catch (IllegalArgumentException success) {}
845 >        assertNotSame(x, y);
846 >        assertEquals(x.size(), y.size());
847 >        assertEquals(x.toString(), y.toString());
848 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
849 >        while (!x.isEmpty()) {
850 >            assertFalse(y.isEmpty());
851 >            assertEquals(x.remove(), y.remove());
852 >        }
853 >        assertTrue(y.isEmpty());
854      }
855  
856      /**
# Line 868 | Line 860 | public class ArrayBlockingQueueTest exte
860          ArrayBlockingQueue q = populatedQueue(SIZE);
861          ArrayList l = new ArrayList();
862          q.drainTo(l);
863 <        assertEquals(q.size(), 0);
864 <        assertEquals(l.size(), SIZE);
863 >        assertEquals(0, q.size());
864 >        assertEquals(SIZE, l.size());
865          for (int i = 0; i < SIZE; ++i)
866              assertEquals(l.get(i), new Integer(i));
867          q.add(zero);
# Line 879 | Line 871 | public class ArrayBlockingQueueTest exte
871          assertTrue(q.contains(one));
872          l.clear();
873          q.drainTo(l);
874 <        assertEquals(q.size(), 0);
875 <        assertEquals(l.size(), 2);
874 >        assertEquals(0, q.size());
875 >        assertEquals(2, l.size());
876          for (int i = 0; i < 2; ++i)
877              assertEquals(l.get(i), new Integer(i));
878      }
# Line 892 | Line 884 | public class ArrayBlockingQueueTest exte
884          final ArrayBlockingQueue q = populatedQueue(SIZE);
885          Thread t = new Thread(new CheckedRunnable() {
886              public void realRun() throws InterruptedException {
887 <                q.put(new Integer(SIZE+1));
887 >                q.put(new Integer(SIZE + 1));
888              }});
889  
890          t.start();
# Line 906 | Line 898 | public class ArrayBlockingQueueTest exte
898      }
899  
900      /**
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    /**
901       * drainTo(c, n) empties first min(n, size) elements of queue into c
902       */
903      public void testDrainToN() {
904 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
904 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2);
905          for (int i = 0; i < SIZE + 2; ++i) {
906              for (int j = 0; j < SIZE; j++)
907                  assertTrue(q.offer(new Integer(j)));
908              ArrayList l = new ArrayList();
909              q.drainTo(l, i);
910              int k = (i < SIZE) ? i : SIZE;
911 <            assertEquals(l.size(), k);
912 <            assertEquals(q.size(), SIZE-k);
911 >            assertEquals(k, l.size());
912 >            assertEquals(SIZE - k, q.size());
913              for (int j = 0; j < k; ++j)
914                  assertEquals(l.get(j), new Integer(j));
915 <            while (q.poll() != null) ;
915 >            do {} while (q.poll() != null);
916          }
917      }
918  
919 +    /**
920 +     * remove(null), contains(null) always return false
921 +     */
922 +    public void testNeverContainsNull() {
923 +        Collection<?>[] qs = {
924 +            new ArrayBlockingQueue<Object>(10),
925 +            populatedQueue(2),
926 +        };
927 +
928 +        for (Collection<?> q : qs) {
929 +            assertFalse(q.contains(null));
930 +            assertFalse(q.remove(null));
931 +        }
932 +    }
933   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines