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

Comparing jsr166/src/test/tck/PriorityBlockingQueueTest.java (file contents):
Revision 1.33 by jsr166, Thu Oct 28 17:57:26 2010 UTC vs.
Revision 1.83 by jsr166, Thu Sep 5 20:54:24 2019 UTC

# Line 1 | Line 1
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.*;
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.Comparator;
15 > import java.util.Iterator;
16 > import java.util.NoSuchElementException;
17 > import java.util.Queue;
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.PriorityBlockingQueue;
23 > import java.util.concurrent.ThreadLocalRandom;
24 >
25 > import junit.framework.Test;
26  
27   public class PriorityBlockingQueueTest extends JSR166TestCase {
28  
# Line 22 | Line 34 | public class PriorityBlockingQueueTest e
34  
35      public static class InitialCapacity extends BlockingQueueTest {
36          protected BlockingQueue emptyCollection() {
37 <            return new PriorityBlockingQueue(20);
37 >            ThreadLocalRandom rnd = ThreadLocalRandom.current();
38 >            int initialCapacity = rnd.nextInt(1, SIZE);
39 >            return new PriorityBlockingQueue(initialCapacity);
40          }
41      }
42  
43      public static void main(String[] args) {
44 <        junit.textui.TestRunner.run(suite());
44 >        main(suite(), args);
45      }
46  
47      public static Test suite() {
48 <        return newTestSuite(PriorityBlockingQueueTest.class,
49 <                            new Generic().testSuite(),
50 <                            new InitialCapacity().testSuite());
48 >        class Implementation implements CollectionImplementation {
49 >            public Class<?> klazz() { return PriorityBlockingQueue.class; }
50 >            public Collection emptyCollection() {
51 >                return new PriorityBlockingQueue();
52 >            }
53 >            public Object makeElement(int i) { return i; }
54 >            public boolean isConcurrent() { return true; }
55 >            public boolean permitsNulls() { return false; }
56 >        }
57 >        class ComparatorImplementation implements CollectionImplementation {
58 >            public Class<?> klazz() { return PriorityBlockingQueue.class; }
59 >            public Collection emptyCollection() {
60 >                ThreadLocalRandom rnd = ThreadLocalRandom.current();
61 >                int initialCapacity = rnd.nextInt(1, 10);
62 >                return new PriorityBlockingQueue(
63 >                    initialCapacity, new MyReverseComparator());
64 >            }
65 >            public Object makeElement(int i) { return i; }
66 >            public boolean isConcurrent() { return true; }
67 >            public boolean permitsNulls() { return false; }
68 >        }
69 >        return newTestSuite(
70 >            PriorityBlockingQueueTest.class,
71 >            new Generic().testSuite(),
72 >            new InitialCapacity().testSuite(),
73 >            CollectionTest.testSuite(new Implementation()),
74 >            CollectionTest.testSuite(new ComparatorImplementation()));
75      }
76  
39    private static final int NOCAP = Integer.MAX_VALUE;
40
77      /** Sample Comparator */
78 <    static class MyReverseComparator implements Comparator {
78 >    static class MyReverseComparator implements Comparator, java.io.Serializable {
79          public int compare(Object x, Object y) {
80              return ((Comparable)y).compareTo(x);
81          }
82      }
83  
84      /**
85 <     * Create a queue of given size containing consecutive
86 <     * Integers 0 ... n.
85 >     * Returns a new queue of given size containing consecutive
86 >     * Integers 0 ... n - 1.
87       */
88 <    private PriorityBlockingQueue populatedQueue(int n) {
89 <        PriorityBlockingQueue q = new PriorityBlockingQueue(n);
88 >    private static PriorityBlockingQueue<Integer> populatedQueue(int n) {
89 >        PriorityBlockingQueue<Integer> q =
90 >            new PriorityBlockingQueue<Integer>(n);
91          assertTrue(q.isEmpty());
92 <        for (int i = n-1; i >= 0; i-=2)
92 >        for (int i = n - 1; i >= 0; i -= 2)
93              assertTrue(q.offer(new Integer(i)));
94 <        for (int i = (n & 1); i < n; i+=2)
94 >        for (int i = (n & 1); i < n; i += 2)
95              assertTrue(q.offer(new Integer(i)));
96          assertFalse(q.isEmpty());
97 <        assertEquals(NOCAP, q.remainingCapacity());
97 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
98          assertEquals(n, q.size());
99 +        assertEquals((Integer) 0, q.peek());
100          return q;
101      }
102  
# Line 66 | Line 104 | public class PriorityBlockingQueueTest e
104       * A new queue has unbounded capacity
105       */
106      public void testConstructor1() {
107 <        assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity());
107 >        assertEquals(Integer.MAX_VALUE,
108 >                     new PriorityBlockingQueue(SIZE).remainingCapacity());
109      }
110  
111      /**
112 <     * Constructor throws IAE if capacity argument nonpositive
112 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
113       */
114      public void testConstructor2() {
115          try {
116 <            PriorityBlockingQueue q = new PriorityBlockingQueue(0);
116 >            new PriorityBlockingQueue(0);
117              shouldThrow();
118          } catch (IllegalArgumentException success) {}
119      }
# Line 84 | Line 123 | public class PriorityBlockingQueueTest e
123       */
124      public void testConstructor3() {
125          try {
126 <            PriorityBlockingQueue q = new PriorityBlockingQueue(null);
126 >            new PriorityBlockingQueue(null);
127              shouldThrow();
128          } catch (NullPointerException success) {}
129      }
# Line 93 | Line 132 | public class PriorityBlockingQueueTest e
132       * Initializing from Collection of null elements throws NPE
133       */
134      public void testConstructor4() {
135 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
136          try {
137 <            Integer[] ints = new Integer[SIZE];
98 <            PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
137 >            new PriorityBlockingQueue(elements);
138              shouldThrow();
139          } catch (NullPointerException success) {}
140      }
# Line 104 | Line 143 | public class PriorityBlockingQueueTest e
143       * Initializing from Collection with some null elements throws NPE
144       */
145      public void testConstructor5() {
146 +        Integer[] ints = new Integer[SIZE];
147 +        for (int i = 0; i < SIZE - 1; ++i)
148 +            ints[i] = i;
149 +        Collection<Integer> elements = Arrays.asList(ints);
150          try {
151 <            Integer[] ints = new Integer[SIZE];
109 <            for (int i = 0; i < SIZE-1; ++i)
110 <                ints[i] = new Integer(i);
111 <            PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
151 >            new PriorityBlockingQueue(elements);
152              shouldThrow();
153          } catch (NullPointerException success) {}
154      }
# Line 119 | Line 159 | public class PriorityBlockingQueueTest e
159      public void testConstructor6() {
160          Integer[] ints = new Integer[SIZE];
161          for (int i = 0; i < SIZE; ++i)
162 <            ints[i] = new Integer(i);
162 >            ints[i] = i;
163          PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
164          for (int i = 0; i < SIZE; ++i)
165              assertEquals(ints[i], q.poll());
# Line 136 | Line 176 | public class PriorityBlockingQueueTest e
176          for (int i = 0; i < SIZE; ++i)
177              ints[i] = new Integer(i);
178          q.addAll(Arrays.asList(ints));
179 <        for (int i = SIZE-1; i >= 0; --i)
179 >        for (int i = SIZE - 1; i >= 0; --i)
180              assertEquals(ints[i], q.poll());
181      }
182  
# Line 146 | Line 186 | public class PriorityBlockingQueueTest e
186      public void testEmpty() {
187          PriorityBlockingQueue q = new PriorityBlockingQueue(2);
188          assertTrue(q.isEmpty());
189 <        assertEquals(NOCAP, q.remainingCapacity());
189 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
190          q.add(one);
191          assertFalse(q.isEmpty());
192          q.add(two);
# Line 156 | Line 196 | public class PriorityBlockingQueueTest e
196      }
197  
198      /**
199 <     * remainingCapacity does not change when elements added or removed,
160 <     * but size does
199 >     * remainingCapacity() always returns Integer.MAX_VALUE
200       */
201      public void testRemainingCapacity() {
202 <        PriorityBlockingQueue q = populatedQueue(SIZE);
202 >        BlockingQueue q = populatedQueue(SIZE);
203          for (int i = 0; i < SIZE; ++i) {
204 <            assertEquals(NOCAP, q.remainingCapacity());
205 <            assertEquals(SIZE-i, q.size());
206 <            q.remove();
204 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
205 >            assertEquals(SIZE - i, q.size());
206 >            assertEquals(i, q.remove());
207          }
208          for (int i = 0; i < SIZE; ++i) {
209 <            assertEquals(NOCAP, q.remainingCapacity());
209 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
210              assertEquals(i, q.size());
211 <            q.add(new Integer(i));
211 >            assertTrue(q.add(i));
212          }
213      }
214  
215      /**
177     * offer(null) throws NPE
178     */
179    public void testOfferNull() {
180        try {
181            PriorityBlockingQueue q = new PriorityBlockingQueue(1);
182            q.offer(null);
183            shouldThrow();
184        } catch (NullPointerException success) {}
185    }
186
187    /**
188     * add(null) throws NPE
189     */
190    public void testAddNull() {
191        try {
192            PriorityBlockingQueue q = new PriorityBlockingQueue(1);
193            q.add(null);
194            shouldThrow();
195        } catch (NullPointerException success) {}
196    }
197
198    /**
216       * Offer of comparable element succeeds
217       */
218      public void testOffer() {
# Line 208 | Line 225 | public class PriorityBlockingQueueTest e
225       * Offer of non-Comparable throws CCE
226       */
227      public void testOfferNonComparable() {
228 +        PriorityBlockingQueue q = new PriorityBlockingQueue(1);
229          try {
212            PriorityBlockingQueue q = new PriorityBlockingQueue(1);
213            q.offer(new Object());
214            q.offer(new Object());
230              q.offer(new Object());
231              shouldThrow();
232 <        } catch (ClassCastException success) {}
232 >        } catch (ClassCastException success) {
233 >            assertTrue(q.isEmpty());
234 >            assertEquals(0, q.size());
235 >            assertNull(q.poll());
236 >        }
237      }
238  
239      /**
# Line 229 | Line 248 | public class PriorityBlockingQueueTest e
248      }
249  
250      /**
251 <     * addAll(null) throws NPE
233 <     */
234 <    public void testAddAll1() {
235 <        try {
236 <            PriorityBlockingQueue q = new PriorityBlockingQueue(1);
237 <            q.addAll(null);
238 <            shouldThrow();
239 <        } catch (NullPointerException success) {}
240 <    }
241 <
242 <    /**
243 <     * addAll(this) throws IAE
251 >     * addAll(this) throws IllegalArgumentException
252       */
253      public void testAddAllSelf() {
254 +        PriorityBlockingQueue q = populatedQueue(SIZE);
255          try {
247            PriorityBlockingQueue q = populatedQueue(SIZE);
256              q.addAll(q);
257              shouldThrow();
258          } catch (IllegalArgumentException success) {}
259      }
260  
261      /**
254     * addAll of a collection with null elements throws NPE
255     */
256    public void testAddAll2() {
257        try {
258            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
259            Integer[] ints = new Integer[SIZE];
260            q.addAll(Arrays.asList(ints));
261            shouldThrow();
262        } catch (NullPointerException success) {}
263    }
264
265    /**
262       * addAll of a collection with any null elements throws NPE after
263       * possibly adding some elements
264       */
265      public void testAddAll3() {
266 +        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
267 +        Integer[] ints = new Integer[SIZE];
268 +        for (int i = 0; i < SIZE - 1; ++i)
269 +            ints[i] = new Integer(i);
270          try {
271            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
272            Integer[] ints = new Integer[SIZE];
273            for (int i = 0; i < SIZE-1; ++i)
274                ints[i] = new Integer(i);
271              q.addAll(Arrays.asList(ints));
272              shouldThrow();
273          } catch (NullPointerException success) {}
# Line 283 | Line 279 | public class PriorityBlockingQueueTest e
279      public void testAddAll5() {
280          Integer[] empty = new Integer[0];
281          Integer[] ints = new Integer[SIZE];
282 <        for (int i = SIZE-1; i >= 0; --i)
282 >        for (int i = SIZE - 1; i >= 0; --i)
283              ints[i] = new Integer(i);
284          PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
285          assertFalse(q.addAll(Arrays.asList(empty)));
# Line 293 | Line 289 | public class PriorityBlockingQueueTest e
289      }
290  
291      /**
296     * put(null) throws NPE
297     */
298     public void testPutNull() {
299        try {
300            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
301            q.put(null);
302            shouldThrow();
303        } catch (NullPointerException success) {}
304     }
305
306    /**
292       * all elements successfully put are contained
293       */
294 <     public void testPut() {
295 <         PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
296 <         for (int i = 0; i < SIZE; ++i) {
297 <             Integer I = new Integer(i);
298 <             q.put(I);
299 <             assertTrue(q.contains(I));
300 <         }
301 <         assertEquals(SIZE, q.size());
294 >    public void testPut() {
295 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
296 >        for (int i = 0; i < SIZE; ++i) {
297 >            Integer x = new Integer(i);
298 >            q.put(x);
299 >            assertTrue(q.contains(x));
300 >        }
301 >        assertEquals(SIZE, q.size());
302      }
303  
304      /**
# Line 322 | Line 307 | public class PriorityBlockingQueueTest e
307      public void testPutWithTake() throws InterruptedException {
308          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
309          final int size = 4;
310 <        Thread t = new Thread(new CheckedRunnable() {
310 >        Thread t = newStartedThread(new CheckedRunnable() {
311              public void realRun() {
312                  for (int i = 0; i < size; i++)
313                      q.put(new Integer(0));
314              }});
315  
316 <        t.start();
317 <        Thread.sleep(SHORT_DELAY_MS);
333 <        assertEquals(q.size(), size);
316 >        awaitTermination(t);
317 >        assertEquals(size, q.size());
318          q.take();
335        t.interrupt();
336        t.join();
319      }
320  
321      /**
322 <     * timed offer does not time out
322 >     * Queue is unbounded, so timed offer never times out
323       */
324 <    public void testTimedOffer() throws InterruptedException {
324 >    public void testTimedOffer() {
325          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
326 <        Thread t = new Thread(new CheckedRunnable() {
326 >        Thread t = newStartedThread(new CheckedRunnable() {
327              public void realRun() {
328                  q.put(new Integer(0));
329                  q.put(new Integer(0));
# Line 349 | Line 331 | public class PriorityBlockingQueueTest e
331                  assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
332              }});
333  
334 <        t.start();
353 <        Thread.sleep(SMALL_DELAY_MS);
354 <        t.interrupt();
355 <        t.join();
334 >        awaitTermination(t);
335      }
336  
337      /**
# Line 370 | Line 349 | public class PriorityBlockingQueueTest e
349       */
350      public void testBlockingTake() throws InterruptedException {
351          final PriorityBlockingQueue q = populatedQueue(SIZE);
352 <        Thread t = new Thread(new CheckedRunnable() {
352 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
353 >        Thread t = newStartedThread(new CheckedRunnable() {
354              public void realRun() throws InterruptedException {
355 <                for (int i = 0; i < SIZE; ++i) {
356 <                    assertEquals(i, q.take());
357 <                }
355 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
356 >
357 >                Thread.currentThread().interrupt();
358 >                try {
359 >                    q.take();
360 >                    shouldThrow();
361 >                } catch (InterruptedException success) {}
362 >                assertFalse(Thread.interrupted());
363 >
364 >                pleaseInterrupt.countDown();
365                  try {
366                      q.take();
367                      shouldThrow();
368                  } catch (InterruptedException success) {}
369 +                assertFalse(Thread.interrupted());
370              }});
371  
372 <        t.start();
373 <        Thread.sleep(SHORT_DELAY_MS);
372 >        await(pleaseInterrupt);
373 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
374          t.interrupt();
375 <        t.join();
375 >        awaitTermination(t);
376      }
377  
390
378      /**
379       * poll succeeds unless empty
380       */
# Line 400 | Line 387 | public class PriorityBlockingQueueTest e
387      }
388  
389      /**
390 <     * timed pool with zero timeout succeeds when non-empty, else times out
390 >     * timed poll with zero timeout succeeds when non-empty, else times out
391       */
392      public void testTimedPoll0() throws InterruptedException {
393          PriorityBlockingQueue q = populatedQueue(SIZE);
# Line 411 | Line 398 | public class PriorityBlockingQueueTest e
398      }
399  
400      /**
401 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
401 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
402       */
403      public void testTimedPoll() throws InterruptedException {
404 <        PriorityBlockingQueue q = populatedQueue(SIZE);
404 >        PriorityBlockingQueue<Integer> q = populatedQueue(SIZE);
405          for (int i = 0; i < SIZE; ++i) {
406 <            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
407 <        }
408 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
406 >            long startTime = System.nanoTime();
407 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
408 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
409 >        }
410 >        long startTime = System.nanoTime();
411 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
412 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
413 >        checkEmpty(q);
414      }
415  
416      /**
# Line 426 | Line 418 | public class PriorityBlockingQueueTest e
418       * returning timeout status
419       */
420      public void testInterruptedTimedPoll() throws InterruptedException {
421 <        Thread t = new Thread(new CheckedRunnable() {
421 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
422 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
423 >        Thread t = newStartedThread(new CheckedRunnable() {
424              public void realRun() throws InterruptedException {
425 <                PriorityBlockingQueue q = populatedQueue(SIZE);
426 <                for (int i = 0; i < SIZE; ++i) {
427 <                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
428 <                }
425 >                long startTime = System.nanoTime();
426 >                for (int i = 0; i < SIZE; i++)
427 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
428 >
429 >                Thread.currentThread().interrupt();
430 >                try {
431 >                    q.poll(randomTimeout(), randomTimeUnit());
432 >                    shouldThrow();
433 >                } catch (InterruptedException success) {}
434 >                assertFalse(Thread.interrupted());
435 >
436 >                pleaseInterrupt.countDown();
437                  try {
438 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
438 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
439                      shouldThrow();
440                  } catch (InterruptedException success) {}
441 +                assertFalse(Thread.interrupted());
442 +
443 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
444              }});
445  
446 <        t.start();
447 <        Thread.sleep(SHORT_DELAY_MS);
446 >        await(pleaseInterrupt);
447 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
448          t.interrupt();
449 <        t.join();
449 >        awaitTermination(t);
450      }
451  
452      /**
# Line 488 | Line 493 | public class PriorityBlockingQueueTest e
493      }
494  
495      /**
491     * remove(x) removes x and returns true if present
492     */
493    public void testRemoveElement() {
494        PriorityBlockingQueue q = populatedQueue(SIZE);
495        for (int i = 1; i < SIZE; i+=2) {
496            assertTrue(q.remove(new Integer(i)));
497        }
498        for (int i = 0; i < SIZE; i+=2) {
499            assertTrue(q.remove(new Integer(i)));
500            assertFalse(q.remove(new Integer(i+1)));
501        }
502        assertTrue(q.isEmpty());
503    }
504
505    /**
496       * contains(x) reports true when elements added but not yet removed
497       */
498      public void testContains() {
# Line 557 | Line 547 | public class PriorityBlockingQueueTest e
547                  assertTrue(changed);
548  
549              assertTrue(q.containsAll(p));
550 <            assertEquals(SIZE-i, q.size());
550 >            assertEquals(SIZE - i, q.size());
551              p.remove();
552          }
553      }
# Line 570 | Line 560 | public class PriorityBlockingQueueTest e
560              PriorityBlockingQueue q = populatedQueue(SIZE);
561              PriorityBlockingQueue p = populatedQueue(i);
562              assertTrue(q.removeAll(p));
563 <            assertEquals(SIZE-i, q.size());
563 >            assertEquals(SIZE - i, q.size());
564              for (int j = 0; j < i; ++j) {
565 <                Integer I = (Integer)(p.remove());
566 <                assertFalse(q.contains(I));
565 >                Integer x = (Integer)(p.remove());
566 >                assertFalse(q.contains(x));
567              }
568          }
569      }
# Line 583 | Line 573 | public class PriorityBlockingQueueTest e
573       */
574      public void testToArray() throws InterruptedException {
575          PriorityBlockingQueue q = populatedQueue(SIZE);
576 <        Object[] o = q.toArray();
577 <        Arrays.sort(o);
578 <        for (int i = 0; i < o.length; i++)
579 <            assertEquals(o[i], q.take());
576 >        Object[] a = q.toArray();
577 >        assertSame(Object[].class, a.getClass());
578 >        Arrays.sort(a);
579 >        for (Object o : a)
580 >            assertSame(o, q.take());
581 >        assertTrue(q.isEmpty());
582      }
583  
584      /**
585       * toArray(a) contains all elements
586       */
587      public void testToArray2() throws InterruptedException {
588 <        PriorityBlockingQueue q = populatedQueue(SIZE);
588 >        PriorityBlockingQueue<Integer> q = populatedQueue(SIZE);
589          Integer[] ints = new Integer[SIZE];
590 <        ints = (Integer[])q.toArray(ints);
590 >        Integer[] array = q.toArray(ints);
591 >        assertSame(ints, array);
592          Arrays.sort(ints);
593 <        for (int i = 0; i < ints.length; i++)
594 <            assertEquals(ints[i], q.take());
595 <    }
603 <
604 <    /**
605 <     * toArray(null) throws NPE
606 <     */
607 <    public void testToArray_BadArg() {
608 <        PriorityBlockingQueue q = populatedQueue(SIZE);
609 <        try {
610 <            Object o[] = q.toArray(null);
611 <            shouldThrow();
612 <        } catch (NullPointerException success) {}
593 >        for (Integer o : ints)
594 >            assertSame(o, q.take());
595 >        assertTrue(q.isEmpty());
596      }
597  
598      /**
599 <     * toArray with incompatible array type throws CCE
599 >     * toArray(incompatible array type) throws ArrayStoreException
600       */
601      public void testToArray1_BadArg() {
602          PriorityBlockingQueue q = populatedQueue(SIZE);
603          try {
604 <            Object o[] = q.toArray(new String[10]);
604 >            q.toArray(new String[10]);
605              shouldThrow();
606          } catch (ArrayStoreException success) {}
607      }
# Line 628 | Line 611 | public class PriorityBlockingQueueTest e
611       */
612      public void testIterator() {
613          PriorityBlockingQueue q = populatedQueue(SIZE);
631        int i = 0;
614          Iterator it = q.iterator();
615 <        while (it.hasNext()) {
615 >        int i;
616 >        for (i = 0; it.hasNext(); i++)
617              assertTrue(q.contains(it.next()));
635            ++i;
636        }
618          assertEquals(i, SIZE);
619 +        assertIteratorExhausted(it);
620 +    }
621 +
622 +    /**
623 +     * iterator of empty collection has no elements
624 +     */
625 +    public void testEmptyIterator() {
626 +        assertIteratorExhausted(new PriorityBlockingQueue().iterator());
627      }
628  
629      /**
# Line 656 | Line 645 | public class PriorityBlockingQueueTest e
645          assertFalse(it.hasNext());
646      }
647  
659
648      /**
649       * toString contains toStrings of elements
650       */
# Line 664 | Line 652 | public class PriorityBlockingQueueTest e
652          PriorityBlockingQueue q = populatedQueue(SIZE);
653          String s = q.toString();
654          for (int i = 0; i < SIZE; ++i) {
655 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
655 >            assertTrue(s.contains(String.valueOf(i)));
656          }
657      }
658  
659      /**
660 <     * offer transfers elements across Executor tasks
660 >     * timed poll transfers elements across Executor tasks
661       */
662      public void testPollInExecutor() {
663          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
664 <        ExecutorService executor = Executors.newFixedThreadPool(2);
665 <        executor.execute(new CheckedRunnable() {
666 <            public void realRun() throws InterruptedException {
667 <                assertNull(q.poll());
668 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
669 <                assertTrue(q.isEmpty());
670 <            }});
671 <
672 <        executor.execute(new CheckedRunnable() {
673 <            public void realRun() throws InterruptedException {
674 <                Thread.sleep(SMALL_DELAY_MS);
675 <                q.put(one);
676 <            }});
677 <
678 <        joinPool(executor);
664 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
665 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
666 >        try (PoolCleaner cleaner = cleaner(executor)) {
667 >            executor.execute(new CheckedRunnable() {
668 >                public void realRun() throws InterruptedException {
669 >                    assertNull(q.poll());
670 >                    threadsStarted.await();
671 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
672 >                    checkEmpty(q);
673 >                }});
674 >
675 >            executor.execute(new CheckedRunnable() {
676 >                public void realRun() throws InterruptedException {
677 >                    threadsStarted.await();
678 >                    q.put(one);
679 >                }});
680 >        }
681      }
682  
683      /**
684 <     * A deserialized serialized queue has same elements
684 >     * A deserialized/reserialized queue has same elements
685       */
686      public void testSerialization() throws Exception {
687 <        PriorityBlockingQueue q = populatedQueue(SIZE);
688 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
699 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
700 <        out.writeObject(q);
701 <        out.close();
702 <
703 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
704 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
705 <        PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
706 <        assertEquals(q.size(), r.size());
707 <        while (!q.isEmpty())
708 <            assertEquals(q.remove(), r.remove());
709 <    }
710 <
711 <    /**
712 <     * drainTo(null) throws NPE
713 <     */
714 <    public void testDrainToNull() {
715 <        PriorityBlockingQueue q = populatedQueue(SIZE);
716 <        try {
717 <            q.drainTo(null);
718 <            shouldThrow();
719 <        } catch (NullPointerException success) {}
720 <    }
687 >        Queue x = populatedQueue(SIZE);
688 >        Queue y = serialClone(x);
689  
690 <    /**
691 <     * drainTo(this) throws IAE
692 <     */
693 <    public void testDrainToSelf() {
694 <        PriorityBlockingQueue q = populatedQueue(SIZE);
695 <        try {
696 <            q.drainTo(q);
729 <            shouldThrow();
730 <        } catch (IllegalArgumentException success) {}
690 >        assertNotSame(x, y);
691 >        assertEquals(x.size(), y.size());
692 >        while (!x.isEmpty()) {
693 >            assertFalse(y.isEmpty());
694 >            assertEquals(x.remove(), y.remove());
695 >        }
696 >        assertTrue(y.isEmpty());
697      }
698  
699      /**
# Line 737 | Line 703 | public class PriorityBlockingQueueTest e
703          PriorityBlockingQueue q = populatedQueue(SIZE);
704          ArrayList l = new ArrayList();
705          q.drainTo(l);
706 <        assertEquals(q.size(), 0);
707 <        assertEquals(l.size(), SIZE);
706 >        assertEquals(0, q.size());
707 >        assertEquals(SIZE, l.size());
708          for (int i = 0; i < SIZE; ++i)
709              assertEquals(l.get(i), new Integer(i));
710          q.add(zero);
# Line 748 | Line 714 | public class PriorityBlockingQueueTest e
714          assertTrue(q.contains(one));
715          l.clear();
716          q.drainTo(l);
717 <        assertEquals(q.size(), 0);
718 <        assertEquals(l.size(), 2);
717 >        assertEquals(0, q.size());
718 >        assertEquals(2, l.size());
719          for (int i = 0; i < 2; ++i)
720              assertEquals(l.get(i), new Integer(i));
721      }
# Line 761 | Line 727 | public class PriorityBlockingQueueTest e
727          final PriorityBlockingQueue q = populatedQueue(SIZE);
728          Thread t = new Thread(new CheckedRunnable() {
729              public void realRun() {
730 <                q.put(new Integer(SIZE+1));
730 >                q.put(new Integer(SIZE + 1));
731              }});
732  
733          t.start();
# Line 775 | Line 741 | public class PriorityBlockingQueueTest e
741      }
742  
743      /**
778     * drainTo(null, n) throws NPE
779     */
780    public void testDrainToNullN() {
781        PriorityBlockingQueue q = populatedQueue(SIZE);
782        try {
783            q.drainTo(null, 0);
784            shouldThrow();
785        } catch (NullPointerException success) {}
786    }
787
788    /**
789     * drainTo(this, n) throws IAE
790     */
791    public void testDrainToSelfN() {
792        PriorityBlockingQueue q = populatedQueue(SIZE);
793        try {
794            q.drainTo(q, 0);
795            shouldThrow();
796        } catch (IllegalArgumentException success) {}
797    }
798
799    /**
744       * drainTo(c, n) empties first min(n, size) elements of queue into c
745       */
746      public void testDrainToN() {
747 <        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2);
747 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE * 2);
748          for (int i = 0; i < SIZE + 2; ++i) {
749              for (int j = 0; j < SIZE; j++)
750                  assertTrue(q.offer(new Integer(j)));
751              ArrayList l = new ArrayList();
752              q.drainTo(l, i);
753              int k = (i < SIZE) ? i : SIZE;
754 <            assertEquals(l.size(), k);
755 <            assertEquals(q.size(), SIZE-k);
754 >            assertEquals(k, l.size());
755 >            assertEquals(SIZE - k, q.size());
756              for (int j = 0; j < k; ++j)
757                  assertEquals(l.get(j), new Integer(j));
758 <            while (q.poll() != null) ;
758 >            do {} while (q.poll() != null);
759 >        }
760 >    }
761 >
762 >    /**
763 >     * remove(null), contains(null) always return false
764 >     */
765 >    public void testNeverContainsNull() {
766 >        Collection<?>[] qs = {
767 >            new PriorityBlockingQueue<Object>(),
768 >            populatedQueue(2),
769 >        };
770 >
771 >        for (Collection<?> q : qs) {
772 >            assertFalse(q.contains(null));
773 >            assertFalse(q.remove(null));
774          }
775      }
776  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines