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

Comparing jsr166/src/test/tck/LinkedBlockingQueueTest.java (file contents):
Revision 1.6 by dl, Sun Oct 5 23:00:40 2003 UTC vs.
Revision 1.77 by jsr166, Sun Jan 7 22:59:18 2018 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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/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.*;
12 < import java.io.*;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 >
11 > import java.util.ArrayList;
12 > import java.util.Arrays;
13 > import java.util.Collection;
14 > import java.util.Iterator;
15 > import java.util.NoSuchElementException;
16 > import java.util.Queue;
17 > import java.util.concurrent.BlockingQueue;
18 > import java.util.concurrent.CountDownLatch;
19 > import java.util.concurrent.Executors;
20 > import java.util.concurrent.ExecutorService;
21 > import java.util.concurrent.LinkedBlockingQueue;
22 >
23 > import junit.framework.Test;
24  
25   public class LinkedBlockingQueueTest extends JSR166TestCase {
26  
27 +    public static class Unbounded extends BlockingQueueTest {
28 +        protected BlockingQueue emptyCollection() {
29 +            return new LinkedBlockingQueue();
30 +        }
31 +    }
32 +
33 +    public static class Bounded extends BlockingQueueTest {
34 +        protected BlockingQueue emptyCollection() {
35 +            return new LinkedBlockingQueue(SIZE);
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(LinkedBlockingQueueTest.class);
44 >        class Implementation implements CollectionImplementation {
45 >            public Class<?> klazz() { return LinkedBlockingQueue.class; }
46 >            public Collection emptyCollection() { return new LinkedBlockingQueue(); }
47 >            public Object makeElement(int i) { return i; }
48 >            public boolean isConcurrent() { return true; }
49 >            public boolean permitsNulls() { return false; }
50 >        }
51 >        return newTestSuite(LinkedBlockingQueueTest.class,
52 >                            new Unbounded().testSuite(),
53 >                            new Bounded().testSuite(),
54 >                            CollectionTest.testSuite(new Implementation()));
55      }
56  
23
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 LinkedBlockingQueue populatedQueue(int n) {
62 <        LinkedBlockingQueue q = new LinkedBlockingQueue(n);
61 >    private static LinkedBlockingQueue<Integer> populatedQueue(int n) {
62 >        LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<>(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, or Integer.MAX_VALUE if
75       * none given
# Line 46 | Line 80 | public class LinkedBlockingQueueTest ext
80      }
81  
82      /**
83 <     * Constructor throws IAE if  capacity argument nonpositive
83 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
84       */
85      public void testConstructor2() {
86          try {
87 <            LinkedBlockingQueue q = new LinkedBlockingQueue(0);
87 >            new LinkedBlockingQueue(0);
88              shouldThrow();
89 <        }
56 <        catch (IllegalArgumentException success) {}
89 >        } catch (IllegalArgumentException success) {}
90      }
91  
92      /**
93 <     * Initializing from null Collection throws NPE
93 >     * Initializing from null Collection throws NullPointerException
94       */
95      public void testConstructor3() {
96          try {
97 <            LinkedBlockingQueue q = new LinkedBlockingQueue(null);
97 >            new LinkedBlockingQueue(null);
98              shouldThrow();
99 <        }
67 <        catch (NullPointerException success) {}
99 >        } catch (NullPointerException success) {}
100      }
101  
102      /**
103 <     * Initializing from Collection of null elements throws NPE
103 >     * Initializing from Collection of null elements throws NullPointerException
104       */
105      public void testConstructor4() {
106 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
107          try {
108 <            Integer[] ints = new Integer[SIZE];
76 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
108 >            new LinkedBlockingQueue(elements);
109              shouldThrow();
110 <        }
79 <        catch (NullPointerException success) {}
110 >        } catch (NullPointerException success) {}
111      }
112  
113      /**
114 <     * Initializing from Collection with some null elements throws NPE
114 >     * Initializing from Collection with some null elements throws
115 >     * NullPointerException
116       */
117      public void testConstructor5() {
118 +        Integer[] ints = new Integer[SIZE];
119 +        for (int i = 0; i < SIZE - 1; ++i)
120 +            ints[i] = new Integer(i);
121 +        Collection<Integer> elements = Arrays.asList(ints);
122          try {
123 <            Integer[] ints = new Integer[SIZE];
88 <            for (int i = 0; i < SIZE-1; ++i)
89 <                ints[i] = new Integer(i);
90 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
123 >            new LinkedBlockingQueue(elements);
124              shouldThrow();
125 <        }
93 <        catch (NullPointerException success) {}
125 >        } catch (NullPointerException success) {}
126      }
127  
128      /**
129       * Queue contains all elements of collection used to initialize
130       */
131      public void testConstructor6() {
132 <        try {
133 <            Integer[] ints = new Integer[SIZE];
134 <            for (int i = 0; i < SIZE; ++i)
135 <                ints[i] = new Integer(i);
136 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
137 <            for (int i = 0; i < SIZE; ++i)
106 <                assertEquals(ints[i], q.poll());
107 <        }
108 <        finally {}
132 >        Integer[] ints = new Integer[SIZE];
133 >        for (int i = 0; i < SIZE; ++i)
134 >            ints[i] = new Integer(i);
135 >        LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
136 >        for (int i = 0; i < SIZE; ++i)
137 >            assertEquals(ints[i], q.poll());
138      }
139  
140      /**
# Line 127 | Line 156 | public class LinkedBlockingQueueTest ext
156       * remainingCapacity decreases on add, increases on remove
157       */
158      public void testRemainingCapacity() {
159 <        LinkedBlockingQueue q = populatedQueue(SIZE);
159 >        BlockingQueue q = populatedQueue(SIZE);
160          for (int i = 0; i < SIZE; ++i) {
161              assertEquals(i, q.remainingCapacity());
162 <            assertEquals(SIZE-i, q.size());
163 <            q.remove();
162 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
163 >            assertEquals(i, q.remove());
164          }
165          for (int i = 0; i < SIZE; ++i) {
166 <            assertEquals(SIZE-i, q.remainingCapacity());
167 <            assertEquals(i, q.size());
168 <            q.add(new Integer(i));
166 >            assertEquals(SIZE - i, q.remainingCapacity());
167 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
168 >            assertTrue(q.add(i));
169          }
170      }
171  
172      /**
144     * offer(null) throws NPE
145     */
146    public void testOfferNull() {
147        try {
148            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
149            q.offer(null);
150            shouldThrow();
151        } catch (NullPointerException success) { }  
152    }
153
154    /**
155     * add(null) throws NPE
156     */
157    public void testAddNull() {
158        try {
159            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
160            q.add(null);
161            shouldThrow();
162        } catch (NullPointerException success) { }  
163    }
164
165    /**
173       * Offer succeeds if not full; fails if full
174       */
175      public void testOffer() {
# Line 172 | Line 179 | public class LinkedBlockingQueueTest ext
179      }
180  
181      /**
182 <     * add succeeds if not full; throws ISE if full
182 >     * add succeeds if not full; throws IllegalStateException if full
183       */
184      public void testAdd() {
185 <        try {
186 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
187 <            for (int i = 0; i < SIZE; ++i) {
188 <                assertTrue(q.add(new Integer(i)));
182 <            }
183 <            assertEquals(0, q.remainingCapacity());
184 <            q.add(new Integer(SIZE));
185 <        } catch (IllegalStateException success){
186 <        }  
187 <    }
188 <
189 <    /**
190 <     * addAll(null) throws NPE
191 <     */
192 <    public void testAddAll1() {
185 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
186 >        for (int i = 0; i < SIZE; ++i)
187 >            assertTrue(q.add(new Integer(i)));
188 >        assertEquals(0, q.remainingCapacity());
189          try {
190 <            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
195 <            q.addAll(null);
190 >            q.add(new Integer(SIZE));
191              shouldThrow();
192 <        }
198 <        catch (NullPointerException success) {}
192 >        } catch (IllegalStateException success) {}
193      }
194  
195      /**
196 <     * addAll(this) throws IAE
196 >     * addAll(this) throws IllegalArgumentException
197       */
198      public void testAddAllSelf() {
199 +        LinkedBlockingQueue q = populatedQueue(SIZE);
200          try {
206            LinkedBlockingQueue q = populatedQueue(SIZE);
201              q.addAll(q);
202              shouldThrow();
203 <        }
210 <        catch (IllegalArgumentException success) {}
203 >        } catch (IllegalArgumentException success) {}
204      }
205  
206      /**
214     * addAll of a collection with null elements throws NPE
215     */
216    public void testAddAll2() {
217        try {
218            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
219            Integer[] ints = new Integer[SIZE];
220            q.addAll(Arrays.asList(ints));
221            shouldThrow();
222        }
223        catch (NullPointerException success) {}
224    }
225    /**
207       * addAll of a collection with any null elements throws NPE after
208       * possibly adding some elements
209       */
210      public void testAddAll3() {
211 +        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
212 +        Integer[] ints = new Integer[SIZE];
213 +        for (int i = 0; i < SIZE - 1; ++i)
214 +            ints[i] = new Integer(i);
215 +        Collection<Integer> elements = Arrays.asList(ints);
216          try {
217 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
232 <            Integer[] ints = new Integer[SIZE];
233 <            for (int i = 0; i < SIZE-1; ++i)
234 <                ints[i] = new Integer(i);
235 <            q.addAll(Arrays.asList(ints));
217 >            q.addAll(elements);
218              shouldThrow();
219 <        }
238 <        catch (NullPointerException success) {}
219 >        } catch (NullPointerException success) {}
220      }
221 +
222      /**
223 <     * addAll throws ISE if not enough room
223 >     * addAll throws IllegalStateException if not enough room
224       */
225      public void testAddAll4() {
226 +        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1);
227 +        Integer[] ints = new Integer[SIZE];
228 +        for (int i = 0; i < SIZE; ++i)
229 +            ints[i] = new Integer(i);
230 +        Collection<Integer> elements = Arrays.asList(ints);
231          try {
232 <            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
246 <            Integer[] ints = new Integer[SIZE];
247 <            for (int i = 0; i < SIZE; ++i)
248 <                ints[i] = new Integer(i);
249 <            q.addAll(Arrays.asList(ints));
232 >            q.addAll(elements);
233              shouldThrow();
234 <        }
252 <        catch (IllegalStateException success) {}
234 >        } catch (IllegalStateException success) {}
235      }
236 +
237      /**
238       * Queue contains all elements, in traversal order, of successful addAll
239       */
240      public void testAddAll5() {
241 <        try {
242 <            Integer[] empty = new Integer[0];
243 <            Integer[] ints = new Integer[SIZE];
244 <            for (int i = 0; i < SIZE; ++i)
245 <                ints[i] = new Integer(i);
246 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
247 <            assertFalse(q.addAll(Arrays.asList(empty)));
248 <            assertTrue(q.addAll(Arrays.asList(ints)));
249 <            for (int i = 0; i < SIZE; ++i)
267 <                assertEquals(ints[i], q.poll());
268 <        }
269 <        finally {}
241 >        Integer[] empty = new Integer[0];
242 >        Integer[] ints = new Integer[SIZE];
243 >        for (int i = 0; i < SIZE; ++i)
244 >            ints[i] = new Integer(i);
245 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
246 >        assertFalse(q.addAll(Arrays.asList(empty)));
247 >        assertTrue(q.addAll(Arrays.asList(ints)));
248 >        for (int i = 0; i < SIZE; ++i)
249 >            assertEquals(ints[i], q.poll());
250      }
251  
252      /**
273     * put(null) throws NPE
274     */
275     public void testPutNull() {
276        try {
277            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
278            q.put(null);
279            shouldThrow();
280        }
281        catch (NullPointerException success){
282        }  
283        catch (InterruptedException ie) {
284            unexpectedException();
285        }
286     }
287
288    /**
253       * all elements successfully put are contained
254       */
255 <     public void testPut() {
256 <         try {
257 <             LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
258 <             for (int i = 0; i < SIZE; ++i) {
259 <                 Integer I = new Integer(i);
260 <                 q.put(I);
297 <                 assertTrue(q.contains(I));
298 <             }
299 <             assertEquals(0, q.remainingCapacity());
300 <         }
301 <        catch (InterruptedException ie) {
302 <            unexpectedException();
255 >    public void testPut() throws InterruptedException {
256 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
257 >        for (int i = 0; i < SIZE; ++i) {
258 >            Integer x = new Integer(i);
259 >            q.put(x);
260 >            assertTrue(q.contains(x));
261          }
262 +        assertEquals(0, q.remainingCapacity());
263      }
264  
265      /**
266       * put blocks interruptibly if full
267       */
268 <    public void testBlockingPut() {
269 <        Thread t = new Thread(new Runnable() {
270 <                public void run() {
271 <                    int added = 0;
272 <                    try {
273 <                        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
274 <                        for (int i = 0; i < SIZE; ++i) {
275 <                            q.put(new Integer(i));
276 <                            ++added;
277 <                        }
278 <                        q.put(new Integer(SIZE));
279 <                        threadShouldThrow();
280 <                    } catch (InterruptedException ie){
281 <                        threadAssertEquals(added, SIZE);
282 <                    }  
283 <                }});
284 <        t.start();
285 <        try {
286 <           Thread.sleep(SHORT_DELAY_MS);
287 <           t.interrupt();
288 <           t.join();
289 <        }
290 <        catch (InterruptedException ie) {
291 <            unexpectedException();
292 <        }
268 >    public void testBlockingPut() throws InterruptedException {
269 >        final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
270 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
271 >        Thread t = newStartedThread(new CheckedRunnable() {
272 >            public void realRun() throws InterruptedException {
273 >                for (int i = 0; i < SIZE; ++i)
274 >                    q.put(i);
275 >                assertEquals(SIZE, q.size());
276 >                assertEquals(0, q.remainingCapacity());
277 >
278 >                Thread.currentThread().interrupt();
279 >                try {
280 >                    q.put(99);
281 >                    shouldThrow();
282 >                } catch (InterruptedException success) {}
283 >                assertFalse(Thread.interrupted());
284 >
285 >                pleaseInterrupt.countDown();
286 >                try {
287 >                    q.put(99);
288 >                    shouldThrow();
289 >                } catch (InterruptedException success) {}
290 >                assertFalse(Thread.interrupted());
291 >            }});
292 >
293 >        await(pleaseInterrupt);
294 >        assertThreadBlocks(t, Thread.State.WAITING);
295 >        t.interrupt();
296 >        awaitTermination(t);
297 >        assertEquals(SIZE, q.size());
298 >        assertEquals(0, q.remainingCapacity());
299      }
300  
301      /**
302 <     * put blocks waiting for take when full
302 >     * put blocks interruptibly waiting for take when full
303       */
304 <    public void testPutWithTake() {
304 >    public void testPutWithTake() throws InterruptedException {
305 >        final int capacity = 2;
306          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
307 <        Thread t = new Thread(new Runnable() {
308 <                public void run() {
309 <                    int added = 0;
310 <                    try {
311 <                        q.put(new Object());
312 <                        ++added;
313 <                        q.put(new Object());
314 <                        ++added;
315 <                        q.put(new Object());
316 <                        ++added;
317 <                        q.put(new Object());
318 <                        ++added;
319 <                        threadShouldThrow();
320 <                    } catch (InterruptedException e){
321 <                        threadAssertTrue(added >= 2);
322 <                    }
323 <                }
324 <            });
325 <        try {
326 <            t.start();
327 <            Thread.sleep(SHORT_DELAY_MS);
328 <            q.take();
329 <            t.interrupt();
330 <            t.join();
331 <        } catch (Exception e){
332 <            unexpectedException();
333 <        }
307 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
308 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
309 >        Thread t = newStartedThread(new CheckedRunnable() {
310 >            public void realRun() throws InterruptedException {
311 >                for (int i = 0; i < capacity; i++)
312 >                    q.put(i);
313 >                pleaseTake.countDown();
314 >                q.put(86);
315 >
316 >                Thread.currentThread().interrupt();
317 >                try {
318 >                    q.put(99);
319 >                    shouldThrow();
320 >                } catch (InterruptedException success) {}
321 >                assertFalse(Thread.interrupted());
322 >
323 >                pleaseInterrupt.countDown();
324 >                try {
325 >                    q.put(99);
326 >                    shouldThrow();
327 >                } catch (InterruptedException success) {}
328 >                assertFalse(Thread.interrupted());
329 >            }});
330 >
331 >        await(pleaseTake);
332 >        assertEquals(0, q.remainingCapacity());
333 >        assertEquals(0, q.take());
334 >
335 >        await(pleaseInterrupt);
336 >        assertThreadBlocks(t, Thread.State.WAITING);
337 >        t.interrupt();
338 >        awaitTermination(t);
339 >        assertEquals(0, q.remainingCapacity());
340      }
341  
342      /**
# Line 372 | Line 344 | public class LinkedBlockingQueueTest ext
344       */
345      public void testTimedOffer() {
346          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
347 <        Thread t = new Thread(new Runnable() {
348 <                public void run() {
349 <                    try {
350 <                        q.put(new Object());
351 <                        q.put(new Object());
352 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
353 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
354 <                        threadShouldThrow();
355 <                    } catch (InterruptedException success){}
384 <                }
385 <            });
386 <        
387 <        try {
388 <            t.start();
389 <            Thread.sleep(SMALL_DELAY_MS);
390 <            t.interrupt();
391 <            t.join();
392 <        } catch (Exception e){
393 <            unexpectedException();
394 <        }
395 <    }
347 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
348 >        Thread t = newStartedThread(new CheckedRunnable() {
349 >            public void realRun() throws InterruptedException {
350 >                q.put(new Object());
351 >                q.put(new Object());
352 >
353 >                long startTime = System.nanoTime();
354 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
355 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
356  
357 <    /**
358 <     * take retrieves elements in FIFO order
359 <     */
360 <    public void testTake() {
361 <        try {
362 <            LinkedBlockingQueue q = populatedQueue(SIZE);
363 <            for (int i = 0; i < SIZE; ++i) {
364 <                assertEquals(i, ((Integer)q.take()).intValue());
365 <            }
366 <        } catch (InterruptedException e){
367 <            unexpectedException();
368 <        }  
357 >                Thread.currentThread().interrupt();
358 >                try {
359 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
360 >                    shouldThrow();
361 >                } catch (InterruptedException success) {}
362 >                assertFalse(Thread.interrupted());
363 >
364 >                pleaseInterrupt.countDown();
365 >                try {
366 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
367 >                    shouldThrow();
368 >                } catch (InterruptedException success) {}
369 >                assertFalse(Thread.interrupted());
370 >            }});
371 >
372 >        await(pleaseInterrupt);
373 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
374 >        t.interrupt();
375 >        awaitTermination(t);
376      }
377  
378      /**
379 <     * take blocks interruptibly when empty
379 >     * take retrieves elements in FIFO order
380       */
381 <    public void testTakeFromEmpty() {
382 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
383 <        Thread t = new Thread(new Runnable() {
384 <                public void run() {
418 <                    try {
419 <                        q.take();
420 <                        threadShouldThrow();
421 <                    } catch (InterruptedException success){ }                
422 <                }
423 <            });
424 <        try {
425 <            t.start();
426 <            Thread.sleep(SHORT_DELAY_MS);
427 <            t.interrupt();
428 <            t.join();
429 <        } catch (Exception e){
430 <            unexpectedException();
381 >    public void testTake() throws InterruptedException {
382 >        LinkedBlockingQueue q = populatedQueue(SIZE);
383 >        for (int i = 0; i < SIZE; ++i) {
384 >            assertEquals(i, q.take());
385          }
386      }
387  
388      /**
389       * Take removes existing elements until empty, then blocks interruptibly
390       */
391 <    public void testBlockingTake() {
392 <        Thread t = new Thread(new Runnable() {
393 <                public void run() {
394 <                    try {
395 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
396 <                        for (int i = 0; i < SIZE; ++i) {
397 <                            assertEquals(i, ((Integer)q.take()).intValue());
398 <                        }
399 <                        q.take();
400 <                        threadShouldThrow();
401 <                    } catch (InterruptedException success){
402 <                    }  
403 <                }});
450 <        t.start();
451 <        try {
452 <           Thread.sleep(SHORT_DELAY_MS);
453 <           t.interrupt();
454 <           t.join();
455 <        }
456 <        catch (InterruptedException ie) {
457 <            unexpectedException();
458 <        }
459 <    }
391 >    public void testBlockingTake() throws InterruptedException {
392 >        final BlockingQueue q = populatedQueue(SIZE);
393 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
394 >        Thread t = newStartedThread(new CheckedRunnable() {
395 >            public void realRun() throws InterruptedException {
396 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
397 >
398 >                Thread.currentThread().interrupt();
399 >                try {
400 >                    q.take();
401 >                    shouldThrow();
402 >                } catch (InterruptedException success) {}
403 >                assertFalse(Thread.interrupted());
404  
405 +                pleaseInterrupt.countDown();
406 +                try {
407 +                    q.take();
408 +                    shouldThrow();
409 +                } catch (InterruptedException success) {}
410 +                assertFalse(Thread.interrupted());
411 +            }});
412 +
413 +        await(pleaseInterrupt);
414 +        assertThreadBlocks(t, Thread.State.WAITING);
415 +        t.interrupt();
416 +        awaitTermination(t);
417 +    }
418  
419      /**
420       * poll succeeds unless empty
# Line 465 | Line 422 | public class LinkedBlockingQueueTest ext
422      public void testPoll() {
423          LinkedBlockingQueue q = populatedQueue(SIZE);
424          for (int i = 0; i < SIZE; ++i) {
425 <            assertEquals(i, ((Integer)q.poll()).intValue());
425 >            assertEquals(i, q.poll());
426          }
427 <        assertNull(q.poll());
427 >        assertNull(q.poll());
428      }
429  
430      /**
431 <     * timed pool with zero timeout succeeds when non-empty, else times out
431 >     * timed poll with zero timeout succeeds when non-empty, else times out
432       */
433 <    public void testTimedPoll0() {
434 <        try {
435 <            LinkedBlockingQueue q = populatedQueue(SIZE);
436 <            for (int i = 0; i < SIZE; ++i) {
437 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
438 <            }
482 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
483 <        } catch (InterruptedException e){
484 <            unexpectedException();
485 <        }  
433 >    public void testTimedPoll0() throws InterruptedException {
434 >        LinkedBlockingQueue q = populatedQueue(SIZE);
435 >        for (int i = 0; i < SIZE; ++i) {
436 >            assertEquals(i, q.poll(0, MILLISECONDS));
437 >        }
438 >        assertNull(q.poll(0, MILLISECONDS));
439      }
440  
441      /**
442 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
442 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
443       */
444 <    public void testTimedPoll() {
445 <        try {
446 <            LinkedBlockingQueue q = populatedQueue(SIZE);
447 <            for (int i = 0; i < SIZE; ++i) {
448 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
449 <            }
450 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
451 <        } catch (InterruptedException e){
452 <            unexpectedException();
453 <        }  
444 >    public void testTimedPoll() throws InterruptedException {
445 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
446 >        for (int i = 0; i < SIZE; ++i) {
447 >            long startTime = System.nanoTime();
448 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
449 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
450 >        }
451 >        long startTime = System.nanoTime();
452 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
453 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
454 >        checkEmpty(q);
455      }
456  
457      /**
458       * Interrupted timed poll throws InterruptedException instead of
459       * returning timeout status
460       */
461 <    public void testInterruptedTimedPoll() {
462 <        Thread t = new Thread(new Runnable() {
463 <                public void run() {
464 <                    try {
465 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
466 <                        for (int i = 0; i < SIZE; ++i) {
467 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
468 <                        }
515 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
516 <                    } catch (InterruptedException success){
517 <                    }  
518 <                }});
519 <        t.start();
520 <        try {
521 <           Thread.sleep(SHORT_DELAY_MS);
522 <           t.interrupt();
523 <           t.join();
524 <        }
525 <        catch (InterruptedException ie) {
526 <            unexpectedException();
527 <        }
528 <    }
461 >    public void testInterruptedTimedPoll() throws InterruptedException {
462 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
463 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
464 >        Thread t = newStartedThread(new CheckedRunnable() {
465 >            public void realRun() throws InterruptedException {
466 >                long startTime = System.nanoTime();
467 >                for (int i = 0; i < SIZE; i++)
468 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
469  
470 <    /**
471 <     *  timed poll before a delayed offer fails; after offer succeeds;
472 <     *  on interruption throws
473 <     */
474 <    public void testTimedPollWithOffer() {
475 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
476 <        Thread t = new Thread(new Runnable() {
477 <                public void run() {
478 <                    try {
479 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
480 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
481 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
482 <                        threadShouldThrow();
483 <                    } catch (InterruptedException success) { }                
484 <                }
485 <            });
486 <        try {
487 <            t.start();
488 <            Thread.sleep(SMALL_DELAY_MS);
489 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
490 <            t.interrupt();
491 <            t.join();
492 <        } catch (Exception e){
553 <            unexpectedException();
554 <        }
555 <    }  
470 >                Thread.currentThread().interrupt();
471 >                try {
472 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
473 >                    shouldThrow();
474 >                } catch (InterruptedException success) {}
475 >                assertFalse(Thread.interrupted());
476 >
477 >                pleaseInterrupt.countDown();
478 >                try {
479 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
480 >                    shouldThrow();
481 >                } catch (InterruptedException success) {}
482 >                assertFalse(Thread.interrupted());
483 >
484 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
485 >            }});
486 >
487 >        await(pleaseInterrupt);
488 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
489 >        t.interrupt();
490 >        awaitTermination(t);
491 >        checkEmpty(q);
492 >    }
493  
494      /**
495       * peek returns next element, or null if empty
# Line 560 | Line 497 | public class LinkedBlockingQueueTest ext
497      public void testPeek() {
498          LinkedBlockingQueue q = populatedQueue(SIZE);
499          for (int i = 0; i < SIZE; ++i) {
500 <            assertEquals(i, ((Integer)q.peek()).intValue());
501 <            q.poll();
500 >            assertEquals(i, q.peek());
501 >            assertEquals(i, q.poll());
502              assertTrue(q.peek() == null ||
503 <                       i != ((Integer)q.peek()).intValue());
503 >                       !q.peek().equals(i));
504          }
505 <        assertNull(q.peek());
505 >        assertNull(q.peek());
506      }
507  
508      /**
# Line 574 | Line 511 | public class LinkedBlockingQueueTest ext
511      public void testElement() {
512          LinkedBlockingQueue q = populatedQueue(SIZE);
513          for (int i = 0; i < SIZE; ++i) {
514 <            assertEquals(i, ((Integer)q.element()).intValue());
515 <            q.poll();
514 >            assertEquals(i, q.element());
515 >            assertEquals(i, q.poll());
516          }
517          try {
518              q.element();
519              shouldThrow();
520 <        }
584 <        catch (NoSuchElementException success) {}
520 >        } catch (NoSuchElementException success) {}
521      }
522  
523      /**
# Line 590 | Line 526 | public class LinkedBlockingQueueTest ext
526      public void testRemove() {
527          LinkedBlockingQueue q = populatedQueue(SIZE);
528          for (int i = 0; i < SIZE; ++i) {
529 <            assertEquals(i, ((Integer)q.remove()).intValue());
529 >            assertEquals(i, q.remove());
530          }
531          try {
532              q.remove();
533              shouldThrow();
534 <        } catch (NoSuchElementException success){
599 <        }  
534 >        } catch (NoSuchElementException success) {}
535      }
536  
537      /**
538 <     * remove(x) removes x and returns true if present
538 >     * An add following remove(x) succeeds
539       */
540 <    public void testRemoveElement() {
541 <        LinkedBlockingQueue q = populatedQueue(SIZE);
542 <        for (int i = 1; i < SIZE; i+=2) {
543 <            assertTrue(q.remove(new Integer(i)));
544 <        }
545 <        for (int i = 0; i < SIZE; i+=2) {
546 <            assertTrue(q.remove(new Integer(i)));
547 <            assertFalse(q.remove(new Integer(i+1)));
613 <        }
614 <        assertTrue(q.isEmpty());
540 >    public void testRemoveElementAndAdd() throws InterruptedException {
541 >        LinkedBlockingQueue q = new LinkedBlockingQueue();
542 >        assertTrue(q.add(new Integer(1)));
543 >        assertTrue(q.add(new Integer(2)));
544 >        assertTrue(q.remove(new Integer(1)));
545 >        assertTrue(q.remove(new Integer(2)));
546 >        assertTrue(q.add(new Integer(3)));
547 >        assertNotNull(q.take());
548      }
549 <        
549 >
550      /**
551       * contains(x) reports true when elements added but not yet removed
552       */
# Line 637 | Line 570 | public class LinkedBlockingQueueTest ext
570          assertEquals(SIZE, q.remainingCapacity());
571          q.add(one);
572          assertFalse(q.isEmpty());
573 +        assertTrue(q.contains(one));
574          q.clear();
575          assertTrue(q.isEmpty());
576      }
# Line 669 | Line 603 | public class LinkedBlockingQueueTest ext
603                  assertTrue(changed);
604  
605              assertTrue(q.containsAll(p));
606 <            assertEquals(SIZE-i, q.size());
606 >            assertEquals(SIZE - i, q.size());
607              p.remove();
608          }
609      }
# Line 682 | Line 616 | public class LinkedBlockingQueueTest ext
616              LinkedBlockingQueue q = populatedQueue(SIZE);
617              LinkedBlockingQueue p = populatedQueue(i);
618              assertTrue(q.removeAll(p));
619 <            assertEquals(SIZE-i, q.size());
619 >            assertEquals(SIZE - i, q.size());
620              for (int j = 0; j < i; ++j) {
621 <                Integer I = (Integer)(p.remove());
622 <                assertFalse(q.contains(I));
621 >                Integer x = (Integer)(p.remove());
622 >                assertFalse(q.contains(x));
623              }
624          }
625      }
626  
627      /**
628 <     * toArray contains all elements
628 >     * toArray contains all elements in FIFO order
629       */
630      public void testToArray() {
631          LinkedBlockingQueue q = populatedQueue(SIZE);
632 <        Object[] o = q.toArray();
633 <        try {
634 <        for(int i = 0; i < o.length; i++)
701 <            assertEquals(o[i], q.take());
702 <        } catch (InterruptedException e){
703 <            unexpectedException();
704 <        }    
632 >        Object[] o = q.toArray();
633 >        for (int i = 0; i < o.length; i++)
634 >            assertSame(o[i], q.poll());
635      }
636  
637      /**
638 <     * toArray(a) contains all elements
638 >     * toArray(a) contains all elements in FIFO order
639       */
640 <    public void testToArray2() {
641 <        LinkedBlockingQueue 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());
717 <        } catch (InterruptedException e){
718 <            unexpectedException();
719 <        }    
640 >    public void testToArray2() throws InterruptedException {
641 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
642 >        Integer[] ints = new Integer[SIZE];
643 >        Integer[] array = q.toArray(ints);
644 >        assertSame(ints, array);
645 >        for (int i = 0; i < ints.length; i++)
646 >            assertSame(ints[i], q.poll());
647      }
648  
649      /**
650 <     * toArray(null) throws NPE
650 >     * toArray(incompatible array type) throws ArrayStoreException
651       */
652 <    public void testToArray_BadArg() {
653 <        try {
654 <            LinkedBlockingQueue q = populatedQueue(SIZE);
655 <            Object o[] = q.toArray(null);
656 <            shouldThrow();
657 <        } catch(NullPointerException success){}
652 >    public void testToArray1_BadArg() {
653 >        LinkedBlockingQueue q = populatedQueue(SIZE);
654 >        try {
655 >            q.toArray(new String[10]);
656 >            shouldThrow();
657 >        } catch (ArrayStoreException success) {}
658      }
659  
660      /**
661 <     * toArray with incompatable array type throws CCE
661 >     * iterator iterates through all elements
662       */
663 <    public void testToArray1_BadArg() {
664 <        try {
665 <            LinkedBlockingQueue q = populatedQueue(SIZE);
666 <            Object o[] = q.toArray(new String[10] );
667 <            shouldThrow();
668 <        } catch(ArrayStoreException  success){}
663 >    public void testIterator() throws InterruptedException {
664 >        LinkedBlockingQueue q = populatedQueue(SIZE);
665 >        Iterator it = q.iterator();
666 >        int i;
667 >        for (i = 0; it.hasNext(); i++)
668 >            assertTrue(q.contains(it.next()));
669 >        assertEquals(i, SIZE);
670 >        assertIteratorExhausted(it);
671 >
672 >        it = q.iterator();
673 >        for (i = 0; it.hasNext(); i++)
674 >            assertEquals(it.next(), q.take());
675 >        assertEquals(i, SIZE);
676 >        assertIteratorExhausted(it);
677      }
678  
744    
679      /**
680 <     * iterator iterates through all elements
680 >     * iterator of empty collection has no elements
681       */
682 <    public void testIterator() {
683 <        LinkedBlockingQueue q = populatedQueue(SIZE);
750 <        Iterator it = q.iterator();
751 <        try {
752 <            while(it.hasNext()){
753 <                assertEquals(it.next(), q.take());
754 <            }
755 <        } catch (InterruptedException e){
756 <            unexpectedException();
757 <        }    
682 >    public void testEmptyIterator() {
683 >        assertIteratorExhausted(new LinkedBlockingQueue().iterator());
684      }
685  
686      /**
687       * iterator.remove removes current element
688       */
689 <    public void testIteratorRemove () {
689 >    public void testIteratorRemove() {
690          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
691          q.add(two);
692          q.add(one);
# Line 769 | Line 695 | public class LinkedBlockingQueueTest ext
695          Iterator it = q.iterator();
696          it.next();
697          it.remove();
698 <        
698 >
699          it = q.iterator();
700 <        assertEquals(it.next(), one);
701 <        assertEquals(it.next(), three);
700 >        assertSame(it.next(), one);
701 >        assertSame(it.next(), three);
702          assertFalse(it.hasNext());
703      }
704  
779
705      /**
706       * iterator ordering is FIFO
707       */
# Line 788 | Line 713 | public class LinkedBlockingQueueTest ext
713          assertEquals(0, q.remainingCapacity());
714          int k = 0;
715          for (Iterator it = q.iterator(); it.hasNext();) {
716 <            int i = ((Integer)(it.next())).intValue();
792 <            assertEquals(++k, i);
716 >            assertEquals(++k, it.next());
717          }
718          assertEquals(3, k);
719      }
# Line 797 | Line 721 | public class LinkedBlockingQueueTest ext
721      /**
722       * Modifications do not cause iterators to fail
723       */
724 <    public void testWeaklyConsistentIteration () {
724 >    public void testWeaklyConsistentIteration() {
725          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
726          q.add(one);
727          q.add(two);
728          q.add(three);
729 <        try {
730 <            for (Iterator it = q.iterator(); it.hasNext();) {
731 <                q.remove();
808 <                it.next();
809 <            }
810 <        }
811 <        catch (ConcurrentModificationException e) {
812 <            unexpectedException();
729 >        for (Iterator it = q.iterator(); it.hasNext();) {
730 >            q.remove();
731 >            it.next();
732          }
733          assertEquals(0, q.size());
734      }
735  
817
736      /**
737       * toString contains toStrings of elements
738       */
# Line 822 | Line 740 | public class LinkedBlockingQueueTest ext
740          LinkedBlockingQueue q = populatedQueue(SIZE);
741          String s = q.toString();
742          for (int i = 0; i < SIZE; ++i) {
743 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
743 >            assertTrue(s.contains(String.valueOf(i)));
744          }
745 <    }        
828 <
745 >    }
746  
747      /**
748       * offer transfers elements across Executor tasks
# Line 834 | Line 751 | public class LinkedBlockingQueueTest ext
751          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
752          q.add(one);
753          q.add(two);
754 <        ExecutorService executor = Executors.newFixedThreadPool(2);
755 <        executor.execute(new Runnable() {
756 <            public void run() {
757 <                threadAssertFalse(q.offer(three));
758 <                try {
759 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
760 <                    threadAssertEquals(0, q.remainingCapacity());
761 <                }
762 <                catch (InterruptedException e) {
763 <                    threadUnexpectedException();
847 <                }
848 <            }
849 <        });
754 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
755 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
756 >        try (PoolCleaner cleaner = cleaner(executor)) {
757 >            executor.execute(new CheckedRunnable() {
758 >                public void realRun() throws InterruptedException {
759 >                    assertFalse(q.offer(three));
760 >                    threadsStarted.await();
761 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
762 >                    assertEquals(0, q.remainingCapacity());
763 >                }});
764  
765 <        executor.execute(new Runnable() {
766 <            public void run() {
767 <                try {
768 <                    Thread.sleep(SMALL_DELAY_MS);
769 <                    threadAssertEquals(one, q.take());
770 <                }
857 <                catch (InterruptedException e) {
858 <                    threadUnexpectedException();
859 <                }
860 <            }
861 <        });
862 <        
863 <        joinPool(executor);
765 >            executor.execute(new CheckedRunnable() {
766 >                public void realRun() throws InterruptedException {
767 >                    threadsStarted.await();
768 >                    assertSame(one, q.take());
769 >                }});
770 >        }
771      }
772  
773      /**
774 <     * poll retrieves elements across Executor threads
774 >     * timed poll retrieves elements across Executor threads
775       */
776      public void testPollInExecutor() {
777          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
778 <        ExecutorService executor = Executors.newFixedThreadPool(2);
779 <        executor.execute(new Runnable() {
780 <            public void run() {
781 <                threadAssertNull(q.poll());
782 <                try {
783 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
784 <                    threadAssertTrue(q.isEmpty());
785 <                }
786 <                catch (InterruptedException e) {
787 <                    threadUnexpectedException();
881 <                }
882 <            }
883 <        });
778 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
779 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
780 >        try (PoolCleaner cleaner = cleaner(executor)) {
781 >            executor.execute(new CheckedRunnable() {
782 >                public void realRun() throws InterruptedException {
783 >                    assertNull(q.poll());
784 >                    threadsStarted.await();
785 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
786 >                    checkEmpty(q);
787 >                }});
788  
789 <        executor.execute(new Runnable() {
790 <            public void run() {
791 <                try {
888 <                    Thread.sleep(SMALL_DELAY_MS);
789 >            executor.execute(new CheckedRunnable() {
790 >                public void realRun() throws InterruptedException {
791 >                    threadsStarted.await();
792                      q.put(one);
793 <                }
891 <                catch (InterruptedException e) {
892 <                    threadUnexpectedException();
893 <                }
894 <            }
895 <        });
896 <        
897 <        joinPool(executor);
898 <    }
899 <
900 <    /**
901 <     * A deserialized serialized queue has same elements in same order
902 <     */
903 <    public void testSerialization() {
904 <        LinkedBlockingQueue q = populatedQueue(SIZE);
905 <
906 <        try {
907 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
908 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
909 <            out.writeObject(q);
910 <            out.close();
911 <
912 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
913 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
914 <            LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
915 <            assertEquals(q.size(), r.size());
916 <            while (!q.isEmpty())
917 <                assertEquals(q.remove(), r.remove());
918 <        } catch(Exception e){
919 <            unexpectedException();
920 <        }
921 <    }
922 <
923 <    /**
924 <     * drainTo(null) throws NPE
925 <     */
926 <    public void testDrainToNull() {
927 <        LinkedBlockingQueue q = populatedQueue(SIZE);
928 <        try {
929 <            q.drainTo(null);
930 <            shouldThrow();
931 <        } catch(NullPointerException success) {
793 >                }});
794          }
795      }
796  
797      /**
798 <     * drainTo(this) throws IAE
799 <     */
800 <    public void testDrainToSelf() {
801 <        LinkedBlockingQueue q = populatedQueue(SIZE);
802 <        try {
803 <            q.drainTo(q);
804 <            shouldThrow();
805 <        } catch(IllegalArgumentException success) {
798 >     * A deserialized/reserialized queue has same elements in same order
799 >     */
800 >    public void testSerialization() throws Exception {
801 >        Queue x = populatedQueue(SIZE);
802 >        Queue y = serialClone(x);
803 >
804 >        assertNotSame(x, y);
805 >        assertEquals(x.size(), y.size());
806 >        assertEquals(x.toString(), y.toString());
807 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
808 >        while (!x.isEmpty()) {
809 >            assertFalse(y.isEmpty());
810 >            assertEquals(x.remove(), y.remove());
811          }
812 +        assertTrue(y.isEmpty());
813      }
814  
815      /**
816       * drainTo(c) empties queue into another collection c
817 <     */
817 >     */
818      public void testDrainTo() {
819          LinkedBlockingQueue q = populatedQueue(SIZE);
820          ArrayList l = new ArrayList();
821          q.drainTo(l);
822 <        assertEquals(q.size(), 0);
823 <        assertEquals(l.size(), SIZE);
824 <        for (int i = 0; i < SIZE; ++i)
822 >        assertEquals(0, q.size());
823 >        assertEquals(SIZE, l.size());
824 >        for (int i = 0; i < SIZE; ++i)
825 >            assertEquals(l.get(i), new Integer(i));
826 >        q.add(zero);
827 >        q.add(one);
828 >        assertFalse(q.isEmpty());
829 >        assertTrue(q.contains(zero));
830 >        assertTrue(q.contains(one));
831 >        l.clear();
832 >        q.drainTo(l);
833 >        assertEquals(0, q.size());
834 >        assertEquals(2, l.size());
835 >        for (int i = 0; i < 2; ++i)
836              assertEquals(l.get(i), new Integer(i));
837      }
838  
839      /**
840       * drainTo empties full queue, unblocking a waiting put.
841 <     */
842 <    public void testDrainToWithActivePut() {
841 >     */
842 >    public void testDrainToWithActivePut() throws InterruptedException {
843          final LinkedBlockingQueue q = populatedQueue(SIZE);
844 <        Thread t = new Thread(new Runnable() {
845 <                public void run() {
846 <                    try {
847 <                        q.put(new Integer(SIZE+1));
969 <                    } catch (InterruptedException ie){
970 <                        threadUnexpectedException();
971 <                    }
972 <                }
973 <            });
974 <        try {
975 <            t.start();
976 <            ArrayList l = new ArrayList();
977 <            q.drainTo(l);
978 <            assertTrue(l.size() >= SIZE);
979 <            for (int i = 0; i < SIZE; ++i)
980 <                assertEquals(l.get(i), new Integer(i));
981 <            t.join();
982 <            assertTrue(q.size() + l.size() == SIZE+1);
983 <        } catch(Exception e){
984 <            unexpectedException();
985 <        }
986 <    }
987 <
988 <    /**
989 <     * drainTo(null, n) throws NPE
990 <     */
991 <    public void testDrainToNullN() {
992 <        LinkedBlockingQueue q = populatedQueue(SIZE);
993 <        try {
994 <            q.drainTo(null, 0);
995 <            shouldThrow();
996 <        } catch(NullPointerException success) {
997 <        }
998 <    }
844 >        Thread t = new Thread(new CheckedRunnable() {
845 >            public void realRun() throws InterruptedException {
846 >                q.put(new Integer(SIZE + 1));
847 >            }});
848  
849 <    /**
850 <     * drainTo(this, n) throws IAE
851 <     */
852 <    public void testDrainToSelfN() {
853 <        LinkedBlockingQueue q = populatedQueue(SIZE);
854 <        try {
855 <            q.drainTo(q, 0);
856 <            shouldThrow();
1008 <        } catch(IllegalArgumentException success) {
1009 <        }
849 >        t.start();
850 >        ArrayList l = new ArrayList();
851 >        q.drainTo(l);
852 >        assertTrue(l.size() >= SIZE);
853 >        for (int i = 0; i < SIZE; ++i)
854 >            assertEquals(l.get(i), new Integer(i));
855 >        t.join();
856 >        assertTrue(q.size() + l.size() >= SIZE);
857      }
858  
859      /**
860 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
861 <     */
860 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
861 >     */
862      public void testDrainToN() {
863 +        LinkedBlockingQueue q = new LinkedBlockingQueue();
864          for (int i = 0; i < SIZE + 2; ++i) {
865 <            LinkedBlockingQueue q = populatedQueue(SIZE);
865 >            for (int j = 0; j < SIZE; j++)
866 >                assertTrue(q.offer(new Integer(j)));
867              ArrayList l = new ArrayList();
868              q.drainTo(l, i);
869 <            int k = (i < SIZE)? i : SIZE;
870 <            assertEquals(q.size(), SIZE-k);
871 <            assertEquals(l.size(), k);
872 <            for (int j = 0; j < k; ++j)
869 >            int k = (i < SIZE) ? i : SIZE;
870 >            assertEquals(k, l.size());
871 >            assertEquals(SIZE - k, q.size());
872 >            for (int j = 0; j < k; ++j)
873                  assertEquals(l.get(j), new Integer(j));
874 +            do {} while (q.poll() != null);
875 +        }
876 +    }
877 +
878 +    /**
879 +     * remove(null), contains(null) always return false
880 +     */
881 +    public void testNeverContainsNull() {
882 +        Collection<?>[] qs = {
883 +            new LinkedBlockingQueue<Object>(),
884 +            populatedQueue(2),
885 +        };
886 +
887 +        for (Collection<?> q : qs) {
888 +            assertFalse(q.contains(null));
889 +            assertFalse(q.remove(null));
890          }
891      }
892  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines