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.8 by dl, Mon Dec 29 19:05:40 2003 UTC vs.
Revision 1.73 by jsr166, Mon Oct 17 01:52:04 2016 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
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
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 static java.util.concurrent.TimeUnit.MILLISECONDS;
10  
11 < import junit.framework.*;
12 < import java.util.*;
13 < import java.util.concurrent.*;
14 < import java.io.*;
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(SIZE, true);
30 +        }
31 +    }
32 +
33 +    public static class NonFair extends BlockingQueueTest {
34 +        protected BlockingQueue emptyCollection() {
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 <        return new TestSuite(ArrayBlockingQueueTest.class);
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(),
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 populatedQueue(int n) {
62 <        ArrayBlockingQueue q = new ArrayBlockingQueue(n);
61 >    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
62 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
63          assertTrue(q.isEmpty());
64 <        for(int i = 0; i < n; i++)
65 <            assertTrue(q.offer(new Integer(i)));
64 >        for (int i = 0; i < n; i++)
65 >            assertTrue(q.offer(new Integer(i)));
66          assertFalse(q.isEmpty());
67          assertEquals(0, q.remainingCapacity());
68 <        assertEquals(n, q.size());
68 >        assertEquals(n, q.size());
69 >        assertEquals((Integer) 0, q.peek());
70          return q;
71      }
72 <
72 >
73      /**
74       * A new queue has the indicated capacity
75       */
# Line 43 | Line 78 | public class ArrayBlockingQueueTest exte
78      }
79  
80      /**
81 <     * Constructor throws IAE if  capacity argument nonpositive
81 >     * Constructor throws IAE if capacity argument nonpositive
82       */
83      public void testConstructor2() {
84          try {
85 <            ArrayBlockingQueue q = new ArrayBlockingQueue(0);
85 >            new ArrayBlockingQueue(0);
86              shouldThrow();
87 <        }
53 <        catch (IllegalArgumentException success) {}
87 >        } catch (IllegalArgumentException success) {}
88      }
89  
90      /**
# Line 58 | 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 <        }
64 <        catch (NullPointerException success) {}
97 >        } catch (NullPointerException success) {}
98      }
99  
100      /**
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];
73 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
106 >            new ArrayBlockingQueue(SIZE, false, elements);
107              shouldThrow();
108 <        }
76 <        catch (NullPointerException success) {}
108 >        } catch (NullPointerException success) {}
109      }
110  
111      /**
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];
85 <            for (int i = 0; i < SIZE-1; ++i)
86 <                ints[i] = new Integer(i);
87 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
120 >            new ArrayBlockingQueue(SIZE, false, elements);
121              shouldThrow();
122 <        }
90 <        catch (NullPointerException success) {}
122 >        } catch (NullPointerException success) {}
123      }
124  
125      /**
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];
99 <            for (int i = 0; i < SIZE; ++i)
100 <                ints[i] = new Integer(i);
101 <            ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
134 >            new ArrayBlockingQueue(SIZE - 1, false, elements);
135              shouldThrow();
136 <        }
104 <        catch (IllegalArgumentException success) {}
136 >        } catch (IllegalArgumentException success) {}
137      }
138  
139      /**
140       * Queue contains all elements of collection used to initialize
141       */
142      public void testConstructor7() {
143 <        try {
144 <            Integer[] ints = new Integer[SIZE];
145 <            for (int i = 0; i < SIZE; ++i)
146 <                ints[i] = new Integer(i);
147 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
148 <            for (int i = 0; i < SIZE; ++i)
149 <                assertEquals(ints[i], q.poll());
118 <        }
119 <        finally {}
143 >        Integer[] ints = new Integer[SIZE];
144 >        for (int i = 0; i < SIZE; ++i)
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      }
151  
152      /**
# Line 138 | 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      /**
155     *  offer(null) throws NPE
156     */
157    public void testOfferNull() {
158        try {
159            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
160            q.offer(null);
161            shouldThrow();
162        } catch (NullPointerException success) { }  
163    }
164
165    /**
166     *  add(null) throws NPE
167     */
168    public void testAddNull() {
169        try {
170            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
171            q.add(null);
172            shouldThrow();
173        } catch (NullPointerException success) { }  
174    }
175
176    /**
185       * Offer succeeds if not full; fails if full
186       */
187      public void testOffer() {
# Line 186 | Line 194 | public class ArrayBlockingQueueTest exte
194       * add succeeds if not full; throws ISE if full
195       */
196      public void testAdd() {
197 <        try {
198 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
199 <            for (int i = 0; i < SIZE; ++i) {
200 <                assertTrue(q.add(new Integer(i)));
201 <            }
194 <            assertEquals(0, q.remainingCapacity());
195 <            q.add(new Integer(SIZE));
196 <        } catch (IllegalStateException success){
197 <        }  
198 <    }
199 <
200 <    /**
201 <     *  addAll(null) throws NPE
202 <     */
203 <    public void testAddAll1() {
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 {
203 <            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
206 <            q.addAll(null);
203 >            q.add(new Integer(SIZE));
204              shouldThrow();
205 <        }
209 <        catch (NullPointerException success) {}
205 >        } catch (IllegalStateException success) {}
206      }
207  
208      /**
209       * addAll(this) throws IAE
210       */
211      public void testAddAllSelf() {
212 +        ArrayBlockingQueue q = populatedQueue(SIZE);
213          try {
217            ArrayBlockingQueue q = populatedQueue(SIZE);
214              q.addAll(q);
215              shouldThrow();
216 <        }
221 <        catch (IllegalArgumentException success) {}
216 >        } catch (IllegalArgumentException success) {}
217      }
218  
224
225    /**
226     *  addAll of a collection with null elements throws NPE
227     */
228    public void testAddAll2() {
229        try {
230            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
231            Integer[] ints = new Integer[SIZE];
232            q.addAll(Arrays.asList(ints));
233            shouldThrow();
234        }
235        catch (NullPointerException success) {}
236    }
219      /**
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 {
243            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
244            Integer[] ints = new Integer[SIZE];
245            for (int i = 0; i < SIZE-1; ++i)
246                ints[i] = new Integer(i);
229              q.addAll(Arrays.asList(ints));
230              shouldThrow();
231 <        }
250 <        catch (NullPointerException success) {}
231 >        } catch (NullPointerException success) {}
232      }
233 +
234      /**
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 {
257            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
258            Integer[] ints = new Integer[SIZE];
259            for (int i = 0; i < SIZE; ++i)
260                ints[i] = new Integer(i);
243              q.addAll(Arrays.asList(ints));
244              shouldThrow();
245 <        }
264 <        catch (IllegalStateException success) {}
245 >        } catch (IllegalStateException success) {}
246      }
247 +
248      /**
249       * Queue contains all elements, in traversal order, of successful addAll
250       */
251      public void testAddAll5() {
252 <        try {
253 <            Integer[] empty = new Integer[0];
254 <            Integer[] ints = new Integer[SIZE];
255 <            for (int i = 0; i < SIZE; ++i)
256 <                ints[i] = new Integer(i);
257 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
258 <            assertFalse(q.addAll(Arrays.asList(empty)));
259 <            assertTrue(q.addAll(Arrays.asList(ints)));
260 <            for (int i = 0; i < SIZE; ++i)
279 <                assertEquals(ints[i], q.poll());
280 <        }
281 <        finally {}
252 >        Integer[] empty = new Integer[0];
253 >        Integer[] ints = new Integer[SIZE];
254 >        for (int i = 0; i < SIZE; ++i)
255 >            ints[i] = new Integer(i);
256 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
257 >        assertFalse(q.addAll(Arrays.asList(empty)));
258 >        assertTrue(q.addAll(Arrays.asList(ints)));
259 >        for (int i = 0; i < SIZE; ++i)
260 >            assertEquals(ints[i], q.poll());
261      }
262  
263      /**
285     *  put(null) throws NPE
286     */
287     public void testPutNull() {
288        try {
289            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
290            q.put(null);
291            shouldThrow();
292        }
293        catch (NullPointerException success){
294        }  
295        catch (InterruptedException ie) {
296            unexpectedException();
297        }
298     }
299
300    /**
264       * all elements successfully put are contained
265       */
266 <     public void testPut() {
267 <         try {
268 <             ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
269 <             for (int i = 0; i < SIZE; ++i) {
270 <                 Integer I = new Integer(i);
271 <                 q.put(I);
309 <                 assertTrue(q.contains(I));
310 <             }
311 <             assertEquals(0, q.remainingCapacity());
312 <         }
313 <        catch (InterruptedException ie) {
314 <            unexpectedException();
266 >    public void testPut() throws InterruptedException {
267 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
268 >        for (int i = 0; i < SIZE; ++i) {
269 >            Integer x = new Integer(i);
270 >            q.put(x);
271 >            assertTrue(q.contains(x));
272          }
273 +        assertEquals(0, q.remainingCapacity());
274      }
275  
276      /**
277       * put blocks interruptibly if full
278       */
279 <    public void testBlockingPut() {
280 <        Thread t = new Thread(new Runnable() {
281 <                public void run() {
282 <                    int added = 0;
283 <                    try {
284 <                        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
285 <                        for (int i = 0; i < SIZE; ++i) {
286 <                            q.put(new Integer(i));
287 <                            ++added;
288 <                        }
289 <                        q.put(new Integer(SIZE));
290 <                        threadShouldThrow();
291 <                    } catch (InterruptedException ie){
292 <                        threadAssertEquals(added, SIZE);
293 <                    }  
294 <                }});
295 <        try {
296 <            t.start();
297 <           Thread.sleep(SHORT_DELAY_MS);
298 <           t.interrupt();
299 <           t.join();
300 <        }
301 <        catch (InterruptedException ie) {
302 <            unexpectedException();
303 <        }
279 >    public void testBlockingPut() throws InterruptedException {
280 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
281 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282 >        Thread t = newStartedThread(new CheckedRunnable() {
283 >            public void realRun() throws InterruptedException {
284 >                for (int i = 0; i < SIZE; ++i)
285 >                    q.put(i);
286 >                assertEquals(SIZE, q.size());
287 >                assertEquals(0, q.remainingCapacity());
288 >
289 >                Thread.currentThread().interrupt();
290 >                try {
291 >                    q.put(99);
292 >                    shouldThrow();
293 >                } catch (InterruptedException success) {}
294 >                assertFalse(Thread.interrupted());
295 >
296 >                pleaseInterrupt.countDown();
297 >                try {
298 >                    q.put(99);
299 >                    shouldThrow();
300 >                } catch (InterruptedException success) {}
301 >                assertFalse(Thread.interrupted());
302 >            }});
303 >
304 >        await(pleaseInterrupt);
305 >        assertThreadStaysAlive(t);
306 >        t.interrupt();
307 >        awaitTermination(t);
308 >        assertEquals(SIZE, q.size());
309 >        assertEquals(0, q.remainingCapacity());
310      }
311  
312      /**
313 <     * put blocks waiting for take when full
313 >     * put blocks interruptibly waiting for take when full
314       */
315 <    public void testPutWithTake() {
316 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
317 <        Thread t = new Thread(new Runnable() {
318 <                public void run() {
319 <                    int added = 0;
320 <                    try {
321 <                        q.put(new Object());
322 <                        ++added;
323 <                        q.put(new Object());
324 <                        ++added;
325 <                        q.put(new Object());
326 <                        ++added;
327 <                        q.put(new Object());
328 <                        ++added;
329 <                        threadShouldThrow();
330 <                    } catch (InterruptedException e){
331 <                        threadAssertTrue(added >= 2);
332 <                    }
333 <                }
334 <            });
335 <        try {
336 <            t.start();
337 <            Thread.sleep(SHORT_DELAY_MS);
338 <            q.take();
339 <            t.interrupt();
340 <            t.join();
341 <        } catch (Exception e){
342 <            unexpectedException();
343 <        }
315 >    public void testPutWithTake() throws InterruptedException {
316 >        final int capacity = 2;
317 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
318 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
319 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
320 >        Thread t = newStartedThread(new CheckedRunnable() {
321 >            public void realRun() throws InterruptedException {
322 >                for (int i = 0; i < capacity; i++)
323 >                    q.put(i);
324 >                pleaseTake.countDown();
325 >                q.put(86);
326 >
327 >                pleaseInterrupt.countDown();
328 >                try {
329 >                    q.put(99);
330 >                    shouldThrow();
331 >                } catch (InterruptedException success) {}
332 >                assertFalse(Thread.interrupted());
333 >            }});
334 >
335 >        await(pleaseTake);
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(0, q.remainingCapacity());
344      }
345  
346      /**
347       * timed offer times out if full and elements not taken
348       */
349 <    public void testTimedOffer() {
349 >    public void testTimedOffer() throws InterruptedException {
350          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
351 <        Thread t = new Thread(new Runnable() {
352 <                public void run() {
353 <                    try {
354 <                        q.put(new Object());
355 <                        q.put(new Object());
356 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
357 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
358 <                        threadShouldThrow();
359 <                    } catch (InterruptedException success){}
360 <                }
361 <            });
362 <        
363 <        try {
364 <            t.start();
365 <            Thread.sleep(SHORT_DELAY_MS);
366 <            t.interrupt();
367 <            t.join();
368 <        } catch (Exception e){
369 <            unexpectedException();
406 <        }
351 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
352 >        Thread t = newStartedThread(new CheckedRunnable() {
353 >            public void realRun() throws InterruptedException {
354 >                q.put(new Object());
355 >                q.put(new Object());
356 >                long startTime = System.nanoTime();
357 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
358 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
359 >                pleaseInterrupt.countDown();
360 >                try {
361 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
362 >                    shouldThrow();
363 >                } catch (InterruptedException success) {}
364 >            }});
365 >
366 >        await(pleaseInterrupt);
367 >        assertThreadStaysAlive(t);
368 >        t.interrupt();
369 >        awaitTermination(t);
370      }
371  
372      /**
373       * take retrieves elements in FIFO order
374       */
375 <    public void testTake() {
376 <        try {
377 <            ArrayBlockingQueue q = populatedQueue(SIZE);
378 <            for (int i = 0; i < SIZE; ++i) {
416 <                assertEquals(i, ((Integer)q.take()).intValue());
417 <            }
418 <        } catch (InterruptedException e){
419 <            unexpectedException();
420 <        }  
421 <    }
422 <
423 <    /**
424 <     * take blocks interruptibly when empty
425 <     */
426 <    public void testTakeFromEmpty() {
427 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
428 <        Thread t = new Thread(new Runnable() {
429 <                public void run() {
430 <                    try {
431 <                        q.take();
432 <                        threadShouldThrow();
433 <                    } catch (InterruptedException success){ }                
434 <                }
435 <            });
436 <        try {
437 <            t.start();
438 <            Thread.sleep(SHORT_DELAY_MS);
439 <            t.interrupt();
440 <            t.join();
441 <        } catch (Exception e){
442 <            unexpectedException();
375 >    public void testTake() throws InterruptedException {
376 >        ArrayBlockingQueue q = populatedQueue(SIZE);
377 >        for (int i = 0; i < SIZE; ++i) {
378 >            assertEquals(i, q.take());
379          }
380      }
381  
382      /**
383       * Take removes existing elements until empty, then blocks interruptibly
384       */
385 <    public void testBlockingTake() {
386 <        Thread t = new Thread(new Runnable() {
387 <                public void run() {
388 <                    try {
389 <                        ArrayBlockingQueue q = populatedQueue(SIZE);
390 <                        for (int i = 0; i < SIZE; ++i) {
391 <                            threadAssertEquals(i, ((Integer)q.take()).intValue());
392 <                        }
457 <                        q.take();
458 <                        threadShouldThrow();
459 <                    } catch (InterruptedException success){
460 <                    }  
461 <                }});
462 <        try {
463 <            t.start();
464 <            Thread.sleep(SHORT_DELAY_MS);
465 <            t.interrupt();
466 <            t.join();
467 <        }
468 <        catch (InterruptedException ie) {
469 <            unexpectedException();
470 <        }
471 <    }
385 >    public void testBlockingTake() throws InterruptedException {
386 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
387 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
388 >        Thread t = newStartedThread(new CheckedRunnable() {
389 >            public void realRun() throws InterruptedException {
390 >                for (int i = 0; i < SIZE; ++i) {
391 >                    assertEquals(i, q.take());
392 >                }
393  
394 +                Thread.currentThread().interrupt();
395 +                try {
396 +                    q.take();
397 +                    shouldThrow();
398 +                } catch (InterruptedException success) {}
399 +                assertFalse(Thread.interrupted());
400 +
401 +                pleaseInterrupt.countDown();
402 +                try {
403 +                    q.take();
404 +                    shouldThrow();
405 +                } catch (InterruptedException success) {}
406 +                assertFalse(Thread.interrupted());
407 +            }});
408 +
409 +        await(pleaseInterrupt);
410 +        assertThreadStaysAlive(t);
411 +        t.interrupt();
412 +        awaitTermination(t);
413 +    }
414  
415      /**
416       * poll succeeds unless empty
# Line 477 | Line 418 | public class ArrayBlockingQueueTest exte
418      public void testPoll() {
419          ArrayBlockingQueue q = populatedQueue(SIZE);
420          for (int i = 0; i < SIZE; ++i) {
421 <            assertEquals(i, ((Integer)q.poll()).intValue());
421 >            assertEquals(i, q.poll());
422          }
423 <        assertNull(q.poll());
423 >        assertNull(q.poll());
424      }
425  
426      /**
427 <     * timed pool with zero timeout succeeds when non-empty, else times out
427 >     * timed poll with zero timeout succeeds when non-empty, else times out
428       */
429 <    public void testTimedPoll0() {
430 <        try {
431 <            ArrayBlockingQueue q = populatedQueue(SIZE);
432 <            for (int i = 0; i < SIZE; ++i) {
433 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
434 <            }
435 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
495 <        } catch (InterruptedException e){
496 <            unexpectedException();
497 <        }  
429 >    public void testTimedPoll0() throws InterruptedException {
430 >        ArrayBlockingQueue q = populatedQueue(SIZE);
431 >        for (int i = 0; i < SIZE; ++i) {
432 >            assertEquals(i, q.poll(0, MILLISECONDS));
433 >        }
434 >        assertNull(q.poll(0, MILLISECONDS));
435 >        checkEmpty(q);
436      }
437  
438      /**
439 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
439 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
440       */
441 <    public void testTimedPoll() {
442 <        try {
443 <            ArrayBlockingQueue q = populatedQueue(SIZE);
444 <            for (int i = 0; i < SIZE; ++i) {
445 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
446 <            }
447 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
448 <        } catch (InterruptedException e){
449 <            unexpectedException();
450 <        }  
441 >    public void testTimedPoll() throws InterruptedException {
442 >        ArrayBlockingQueue q = populatedQueue(SIZE);
443 >        for (int i = 0; i < SIZE; ++i) {
444 >            long startTime = System.nanoTime();
445 >            assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
446 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
447 >        }
448 >        long startTime = System.nanoTime();
449 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
450 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
451 >        checkEmpty(q);
452      }
453  
454      /**
455       * Interrupted timed poll throws InterruptedException instead of
456       * returning timeout status
457       */
458 <    public void testInterruptedTimedPoll() {
459 <        Thread t = new Thread(new Runnable() {
460 <                public void run() {
461 <                    try {
462 <                        ArrayBlockingQueue q = populatedQueue(SIZE);
463 <                        for (int i = 0; i < SIZE; ++i) {
464 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
465 <                        }
527 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
528 <                    } catch (InterruptedException success){
529 <                    }  
530 <                }});
531 <        try {
532 <            t.start();
533 <            Thread.sleep(SHORT_DELAY_MS);
534 <            t.interrupt();
535 <            t.join();
536 <        }
537 <        catch (InterruptedException ie) {
538 <            unexpectedException();
539 <        }
540 <    }
541 <
542 <    /**
543 <     *  timed poll before a delayed offer fails; after offer succeeds;
544 <     *  on interruption throws
545 <     */
546 <    public void testTimedPollWithOffer() {
547 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
548 <        Thread t = new Thread(new Runnable() {
549 <                public void run() {
550 <                    try {
551 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
552 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
553 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
554 <                        threadShouldThrow();
555 <                    } catch (InterruptedException success) { }                
458 >    public void testInterruptedTimedPoll() throws InterruptedException {
459 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
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) {
465 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
466                  }
467 <            });
468 <        try {
469 <            t.start();
470 <            Thread.sleep(SMALL_DELAY_MS);
471 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
472 <            t.interrupt();
473 <            t.join();
474 <        } catch (Exception e){
475 <            unexpectedException();
476 <        }
477 <    }  
478 <
467 >                aboutToWait.countDown();
468 >                try {
469 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
470 >                    shouldThrow();
471 >                } catch (InterruptedException success) {
472 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
473 >                }
474 >            }});
475 >
476 >        await(aboutToWait);
477 >        waitForThreadToEnterWaitState(t);
478 >        t.interrupt();
479 >        awaitTermination(t);
480 >        checkEmpty(q);
481 >    }
482  
483      /**
484       * peek returns next element, or null if empty
# Line 573 | Line 486 | public class ArrayBlockingQueueTest exte
486      public void testPeek() {
487          ArrayBlockingQueue q = populatedQueue(SIZE);
488          for (int i = 0; i < SIZE; ++i) {
489 <            assertEquals(i, ((Integer)q.peek()).intValue());
490 <            q.poll();
489 >            assertEquals(i, q.peek());
490 >            assertEquals(i, q.poll());
491              assertTrue(q.peek() == null ||
492 <                       i != ((Integer)q.peek()).intValue());
492 >                       !q.peek().equals(i));
493          }
494 <        assertNull(q.peek());
494 >        assertNull(q.peek());
495      }
496  
497      /**
# Line 587 | Line 500 | public class ArrayBlockingQueueTest exte
500      public void testElement() {
501          ArrayBlockingQueue q = populatedQueue(SIZE);
502          for (int i = 0; i < SIZE; ++i) {
503 <            assertEquals(i, ((Integer)q.element()).intValue());
504 <            q.poll();
503 >            assertEquals(i, q.element());
504 >            assertEquals(i, q.poll());
505          }
506          try {
507              q.element();
508              shouldThrow();
509 <        }
597 <        catch (NoSuchElementException success) {}
509 >        } catch (NoSuchElementException success) {}
510      }
511  
512      /**
# Line 603 | Line 515 | public class ArrayBlockingQueueTest exte
515      public void testRemove() {
516          ArrayBlockingQueue q = populatedQueue(SIZE);
517          for (int i = 0; i < SIZE; ++i) {
518 <            assertEquals(i, ((Integer)q.remove()).intValue());
518 >            assertEquals(i, q.remove());
519          }
520          try {
521              q.remove();
522              shouldThrow();
523 <        } catch (NoSuchElementException success){
612 <        }  
523 >        } catch (NoSuchElementException success) {}
524      }
525  
526      /**
616     * remove(x) removes x and returns true if present
617     */
618    public void testRemoveElement() {
619        ArrayBlockingQueue q = populatedQueue(SIZE);
620        for (int i = 1; i < SIZE; i+=2) {
621            assertTrue(q.remove(new Integer(i)));
622        }
623        for (int i = 0; i < SIZE; i+=2) {
624            assertTrue(q.remove(new Integer(i)));
625            assertFalse(q.remove(new Integer(i+1)));
626        }
627        assertTrue(q.isEmpty());
628    }
629        
630    /**
527       * contains(x) reports true when elements added but not yet removed
528       */
529      public void testContains() {
530          ArrayBlockingQueue q = populatedQueue(SIZE);
531          for (int i = 0; i < SIZE; ++i) {
532              assertTrue(q.contains(new Integer(i)));
533 <            q.poll();
533 >            assertEquals(i, q.poll());
534              assertFalse(q.contains(new Integer(i)));
535          }
536      }
# Line 650 | Line 546 | public class ArrayBlockingQueueTest exte
546          assertEquals(SIZE, q.remainingCapacity());
547          q.add(one);
548          assertFalse(q.isEmpty());
549 +        assertTrue(q.contains(one));
550          q.clear();
551          assertTrue(q.isEmpty());
552      }
# Line 682 | 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 695 | 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 +    void checkToArray(ArrayBlockingQueue q) {
604 +        int size = q.size();
605 +        Object[] o = q.toArray();
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 contains all elements
616 >     * toArray() contains all elements in FIFO order
617       */
618      public void testToArray() {
619 <        ArrayBlockingQueue q = populatedQueue(SIZE);
620 <        Object[] o = q.toArray();
621 <        try {
622 <        for(int i = 0; i < o.length; i++)
623 <            assertEquals(o[i], q.take());
624 <        } catch (InterruptedException e){
625 <            unexpectedException();
626 <        }    
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 <    /**
638 <     * toArray(a) contains all elements
639 <     */
640 <    public void testToArray2() {
641 <        ArrayBlockingQueue q = populatedQueue(SIZE);
642 <        Integer[] ints = new Integer[SIZE];
643 <        ints = (Integer[])q.toArray(ints);
644 <        try {
645 <            for(int i = 0; i < ints.length; i++)
646 <                assertEquals(ints[i], q.take());
647 <        } catch (InterruptedException e){
648 <            unexpectedException();
649 <        }    
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 NPE
670 >     * toArray(a) contains all elements in FIFO order
671       */
672 <    public void testToArray_BadArg() {
673 <        try {
674 <            ArrayBlockingQueue q = populatedQueue(SIZE);
675 <            Object o[] = 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      /**
692 <     * toArray with incompatible array type throws CCE
692 >     * toArray(incompatible array type) throws ArrayStoreException
693       */
694      public void testToArray1_BadArg() {
695 <        try {
696 <            ArrayBlockingQueue q = populatedQueue(SIZE);
697 <            Object o[] = q.toArray(new String[10] );
698 <            shouldThrow();
699 <        } catch(ArrayStoreException  success){}
695 >        ArrayBlockingQueue q = populatedQueue(SIZE);
696 >        try {
697 >            q.toArray(new String[10]);
698 >            shouldThrow();
699 >        } catch (ArrayStoreException success) {}
700      }
701  
757    
702      /**
703       * iterator iterates through all elements
704       */
705 <    public void testIterator() {
705 >    public void testIterator() throws InterruptedException {
706          ArrayBlockingQueue q = populatedQueue(SIZE);
707 <        Iterator it = q.iterator();
708 <        try {
709 <            while(it.hasNext()){
710 <                assertEquals(it.next(), q.take());
711 <            }
712 <        } catch (InterruptedException e){
713 <            unexpectedException();
714 <        }    
707 >        Iterator it = q.iterator();
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 >        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      /**
729       * iterator.remove removes current element
730       */
731 <    public void testIteratorRemove () {
731 >    public void testIteratorRemove() {
732          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
733          q.add(two);
734          q.add(one);
# Line 782 | Line 737 | public class ArrayBlockingQueueTest exte
737          Iterator it = q.iterator();
738          it.next();
739          it.remove();
740 <        
740 >
741          it = q.iterator();
742 <        assertEquals(it.next(), one);
743 <        assertEquals(it.next(), three);
742 >        assertSame(it.next(), one);
743 >        assertSame(it.next(), three);
744          assertFalse(it.hasNext());
745      }
746  
# Line 802 | Line 757 | public class ArrayBlockingQueueTest exte
757  
758          int k = 0;
759          for (Iterator it = q.iterator(); it.hasNext();) {
760 <            int i = ((Integer)(it.next())).intValue();
806 <            assertEquals(++k, i);
760 >            assertEquals(++k, it.next());
761          }
762          assertEquals(3, k);
763      }
# Line 811 | Line 765 | public class ArrayBlockingQueueTest exte
765      /**
766       * Modifications do not cause iterators to fail
767       */
768 <    public void testWeaklyConsistentIteration () {
768 >    public void testWeaklyConsistentIteration() {
769          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
770          q.add(one);
771          q.add(two);
772          q.add(three);
773 <        try {
774 <            for (Iterator it = q.iterator(); it.hasNext();) {
775 <                q.remove();
822 <                it.next();
823 <            }
824 <        }
825 <        catch (ConcurrentModificationException e) {
826 <            unexpectedException();
773 >        for (Iterator it = q.iterator(); it.hasNext();) {
774 >            q.remove();
775 >            it.next();
776          }
777          assertEquals(0, q.size());
778      }
779  
831
780      /**
781       * toString contains toStrings of elements
782       */
# Line 836 | Line 784 | public class ArrayBlockingQueueTest exte
784          ArrayBlockingQueue q = populatedQueue(SIZE);
785          String s = q.toString();
786          for (int i = 0; i < SIZE; ++i) {
787 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
787 >            assertTrue(s.contains(String.valueOf(i)));
788          }
789 <    }        
842 <
789 >    }
790  
791      /**
792       * offer transfers elements across Executor tasks
# Line 848 | Line 795 | public class ArrayBlockingQueueTest exte
795          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
796          q.add(one);
797          q.add(two);
798 <        ExecutorService executor = Executors.newFixedThreadPool(2);
799 <        executor.execute(new Runnable() {
800 <            public void run() {
801 <                threadAssertFalse(q.offer(three));
802 <                try {
803 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
804 <                    threadAssertEquals(0, q.remainingCapacity());
805 <                }
806 <                catch (InterruptedException e) {
807 <                    threadUnexpectedException();
861 <                }
862 <            }
863 <        });
864 <
865 <        executor.execute(new Runnable() {
866 <            public void run() {
867 <                try {
868 <                    Thread.sleep(SMALL_DELAY_MS);
869 <                    threadAssertEquals(one, q.take());
870 <                }
871 <                catch (InterruptedException e) {
872 <                    threadUnexpectedException();
873 <                }
874 <            }
875 <        });
876 <        
877 <        joinPool(executor);
798 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
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      /**
819 <     * poll retrieves elements across Executor threads
819 >     * timed poll retrieves elements across Executor threads
820       */
821      public void testPollInExecutor() {
822          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
823 <        ExecutorService executor = Executors.newFixedThreadPool(2);
824 <        executor.execute(new Runnable() {
825 <            public void run() {
826 <                threadAssertNull(q.poll());
827 <                try {
828 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
829 <                    threadAssertTrue(q.isEmpty());
830 <                }
831 <                catch (InterruptedException e) {
832 <                    threadUnexpectedException();
896 <                }
897 <            }
898 <        });
823 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
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 Runnable() {
835 <            public void run() {
836 <                try {
903 <                    Thread.sleep(SMALL_DELAY_MS);
834 >            executor.execute(new CheckedRunnable() {
835 >                public void realRun() throws InterruptedException {
836 >                    threadsStarted.await();
837                      q.put(one);
838 <                }
839 <                catch (InterruptedException e) {
907 <                    threadUnexpectedException();
908 <                }
909 <            }
910 <        });
911 <        
912 <        joinPool(executor);
838 >                }});
839 >        }
840      }
841  
842      /**
843       * A deserialized serialized queue has same elements in same order
844       */
845 <    public void testSerialization() {
846 <        ArrayBlockingQueue q = populatedQueue(SIZE);
847 <
848 <        try {
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));
929 <            ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
930 <            assertEquals(q.size(), r.size());
931 <            while (!q.isEmpty())
932 <                assertEquals(q.remove(), r.remove());
933 <        } catch(Exception e){
934 <            unexpectedException();
935 <        }
936 <    }
937 <
938 <    /**
939 <     * drainTo(null) throws NPE
940 <     */
941 <    public void testDrainToNull() {
942 <        ArrayBlockingQueue q = populatedQueue(SIZE);
943 <        try {
944 <            q.drainTo(null);
945 <            shouldThrow();
946 <        } catch(NullPointerException success) {
947 <        }
948 <    }
949 <
950 <    /**
951 <     * drainTo(this) throws IAE
952 <     */
953 <    public void testDrainToSelf() {
954 <        ArrayBlockingQueue q = populatedQueue(SIZE);
955 <        try {
956 <            q.drainTo(q);
957 <            shouldThrow();
958 <        } catch(IllegalArgumentException success) {
845 >    public void testSerialization() throws Exception {
846 >        Queue x = populatedQueue(SIZE);
847 >        Queue y = serialClone(x);
848 >
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      /**
861       * drainTo(c) empties queue into another collection c
862 <     */
862 >     */
863      public void testDrainTo() {
864          ArrayBlockingQueue q = populatedQueue(SIZE);
865          ArrayList l = new ArrayList();
866          q.drainTo(l);
867 <        assertEquals(q.size(), 0);
868 <        assertEquals(l.size(), SIZE);
869 <        for (int i = 0; i < SIZE; ++i)
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);
872 >        q.add(one);
873 >        assertFalse(q.isEmpty());
874 >        assertTrue(q.contains(zero));
875 >        assertTrue(q.contains(one));
876 >        l.clear();
877 >        q.drainTo(l);
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      }
883  
884      /**
885       * drainTo empties full queue, unblocking a waiting put.
886 <     */
887 <    public void testDrainToWithActivePut() {
886 >     */
887 >    public void testDrainToWithActivePut() throws InterruptedException {
888          final ArrayBlockingQueue q = populatedQueue(SIZE);
889 <        Thread t = new Thread(new Runnable() {
890 <                public void run() {
891 <                    try {
892 <                        q.put(new Integer(SIZE+1));
984 <                    } catch (InterruptedException ie){
985 <                        threadUnexpectedException();
986 <                    }
987 <                }
988 <            });
989 <        try {
990 <            t.start();
991 <            ArrayList l = new ArrayList();
992 <            q.drainTo(l);
993 <            assertTrue(l.size() >= SIZE);
994 <            for (int i = 0; i < SIZE; ++i)
995 <                assertEquals(l.get(i), new Integer(i));
996 <            t.join();
997 <            assertTrue(q.size() + l.size() == SIZE+1);
998 <        } catch(Exception e){
999 <            unexpectedException();
1000 <        }
1001 <    }
889 >        Thread t = new Thread(new CheckedRunnable() {
890 >            public void realRun() throws InterruptedException {
891 >                q.put(new Integer(SIZE + 1));
892 >            }});
893  
894 <    /**
895 <     * drainTo(null, n) throws NPE
896 <     */
897 <    public void testDrainToNullN() {
898 <        ArrayBlockingQueue q = populatedQueue(SIZE);
899 <        try {
900 <            q.drainTo(null, 0);
901 <            shouldThrow();
1011 <        } catch(NullPointerException success) {
1012 <        }
1013 <    }
1014 <
1015 <    /**
1016 <     * drainTo(this, n) throws IAE
1017 <     */
1018 <    public void testDrainToSelfN() {
1019 <        ArrayBlockingQueue q = populatedQueue(SIZE);
1020 <        try {
1021 <            q.drainTo(q, 0);
1022 <            shouldThrow();
1023 <        } catch(IllegalArgumentException success) {
1024 <        }
894 >        t.start();
895 >        ArrayList l = new ArrayList();
896 >        q.drainTo(l);
897 >        assertTrue(l.size() >= SIZE);
898 >        for (int i = 0; i < SIZE; ++i)
899 >            assertEquals(l.get(i), new Integer(i));
900 >        t.join();
901 >        assertTrue(q.size() + l.size() >= SIZE);
902      }
903  
904      /**
905 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
906 <     */
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);
909          for (int i = 0; i < SIZE + 2; ++i) {
910 <            ArrayBlockingQueue q = populatedQueue(SIZE);
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(q.size(), SIZE-k);
916 <            assertEquals(l.size(), k);
917 <            for (int j = 0; j < k; ++j)
914 >            int k = (i < SIZE) ? i : SIZE;
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 +            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