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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.46 by jsr166, Tue May 31 16:16:24 2011 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.*;
10 > import java.util.Arrays;
11 > import java.util.ArrayList;
12 > import java.util.Collection;
13 > import java.util.Iterator;
14 > import java.util.NoSuchElementException;
15 > import java.util.Queue;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.CountDownLatch;
18 > import java.util.concurrent.LinkedBlockingQueue;
19 > import java.util.concurrent.Executors;
20 > import java.util.concurrent.ExecutorService;
21 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
22  
23   public class LinkedBlockingQueueTest extends JSR166TestCase {
24  
25 +    public static class Unbounded extends BlockingQueueTest {
26 +        protected BlockingQueue emptyCollection() {
27 +            return new LinkedBlockingQueue();
28 +        }
29 +    }
30 +
31 +    public static class Bounded extends BlockingQueueTest {
32 +        protected BlockingQueue emptyCollection() {
33 +            return new LinkedBlockingQueue(20);
34 +        }
35 +    }
36 +
37      public static void main(String[] args) {
38 <        junit.textui.TestRunner.run (suite());  
38 >        junit.textui.TestRunner.run(suite());
39      }
40  
41      public static Test suite() {
42 <        return new TestSuite(LinkedBlockingQueueTest.class);
42 >        return newTestSuite(LinkedBlockingQueueTest.class,
43 >                            new Unbounded().testSuite(),
44 >                            new Bounded().testSuite());
45      }
46  
23
47      /**
48       * Create a queue of given size containing consecutive
49       * Integers 0 ... n.
50       */
51 <    private LinkedBlockingQueue populatedQueue(int n) {
52 <        LinkedBlockingQueue q = new LinkedBlockingQueue(n);
51 >    private LinkedBlockingQueue<Integer> populatedQueue(int n) {
52 >        LinkedBlockingQueue<Integer> q =
53 >            new LinkedBlockingQueue<Integer>(n);
54          assertTrue(q.isEmpty());
55 <        for(int i = 0; i < n; i++)
56 <            assertTrue(q.offer(new Integer(i)));
55 >        for (int i = 0; i < n; i++)
56 >            assertTrue(q.offer(new Integer(i)));
57          assertFalse(q.isEmpty());
58          assertEquals(0, q.remainingCapacity());
59 <        assertEquals(n, q.size());
59 >        assertEquals(n, q.size());
60          return q;
61      }
62 <
63 <    public void testConstructor1(){
62 >
63 >    /**
64 >     * A new queue has the indicated capacity, or Integer.MAX_VALUE if
65 >     * none given
66 >     */
67 >    public void testConstructor1() {
68          assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity());
69 +        assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity());
70      }
71  
72 <    public void testConstructor2(){
72 >    /**
73 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
74 >     */
75 >    public void testConstructor2() {
76          try {
77 <            LinkedBlockingQueue q = new LinkedBlockingQueue(0);
78 <            fail("Cannot make zero-sized");
79 <        }
48 <        catch (IllegalArgumentException success) {}
77 >            new LinkedBlockingQueue(0);
78 >            shouldThrow();
79 >        } catch (IllegalArgumentException success) {}
80      }
81  
82 <    public void testConstructor3(){
83 <
82 >    /**
83 >     * Initializing from null Collection throws NullPointerException
84 >     */
85 >    public void testConstructor3() {
86          try {
87 <            LinkedBlockingQueue q = new LinkedBlockingQueue(null);
88 <            fail("Cannot make from null collection");
89 <        }
57 <        catch (NullPointerException success) {}
87 >            new LinkedBlockingQueue(null);
88 >            shouldThrow();
89 >        } catch (NullPointerException success) {}
90      }
91  
92 <    public void testConstructor4(){
92 >    /**
93 >     * Initializing from Collection of null elements throws NullPointerException
94 >     */
95 >    public void testConstructor4() {
96 >        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
97          try {
98 <            Integer[] ints = new Integer[SIZE];
99 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
100 <            fail("Cannot make with null elements");
65 <        }
66 <        catch (NullPointerException success) {}
98 >            new LinkedBlockingQueue(elements);
99 >            shouldThrow();
100 >        } catch (NullPointerException success) {}
101      }
102  
103 <    public void testConstructor5(){
104 <        try {
105 <            Integer[] ints = new Integer[SIZE];
106 <            for (int i = 0; i < SIZE-1; ++i)
107 <                ints[i] = new Integer(i);
108 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
109 <            fail("Cannot make with null elements");
110 <        }
111 <        catch (NullPointerException success) {}
103 >    /**
104 >     * Initializing from Collection with some null elements throws
105 >     * NullPointerException
106 >     */
107 >    public void testConstructor5() {
108 >        Integer[] ints = new Integer[SIZE];
109 >        for (int i = 0; i < SIZE-1; ++i)
110 >            ints[i] = new Integer(i);
111 >        Collection<Integer> elements = Arrays.asList(ints);
112 >        try {
113 >            new LinkedBlockingQueue(elements);
114 >            shouldThrow();
115 >        } catch (NullPointerException success) {}
116      }
117  
118 <    public void testConstructor6(){
119 <        try {
120 <            Integer[] ints = new Integer[SIZE];
121 <            for (int i = 0; i < SIZE; ++i)
122 <                ints[i] = new Integer(i);
123 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
124 <            for (int i = 0; i < SIZE; ++i)
125 <                assertEquals(ints[i], q.poll());
126 <        }
127 <        finally {}
118 >    /**
119 >     * Queue contains all elements of collection used to initialize
120 >     */
121 >    public void testConstructor6() {
122 >        Integer[] ints = new Integer[SIZE];
123 >        for (int i = 0; i < SIZE; ++i)
124 >            ints[i] = new Integer(i);
125 >        LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
126 >        for (int i = 0; i < SIZE; ++i)
127 >            assertEquals(ints[i], q.poll());
128      }
129  
130 +    /**
131 +     * Queue transitions from empty to full when elements added
132 +     */
133      public void testEmptyFull() {
134          LinkedBlockingQueue q = new LinkedBlockingQueue(2);
135          assertTrue(q.isEmpty());
# Line 97 | Line 138 | public class LinkedBlockingQueueTest ext
138          assertFalse(q.isEmpty());
139          q.add(two);
140          assertFalse(q.isEmpty());
141 <        assertEquals("queue should be full", 0, q.remainingCapacity());
142 <        assertFalse("offer should be rejected", q.offer(three));
141 >        assertEquals(0, q.remainingCapacity());
142 >        assertFalse(q.offer(three));
143      }
144  
145 <    public void testRemainingCapacity(){
145 >    /**
146 >     * remainingCapacity decreases on add, increases on remove
147 >     */
148 >    public void testRemainingCapacity() {
149          LinkedBlockingQueue q = populatedQueue(SIZE);
150          for (int i = 0; i < SIZE; ++i) {
151              assertEquals(i, q.remainingCapacity());
# Line 115 | Line 159 | public class LinkedBlockingQueueTest ext
159          }
160      }
161  
162 <    public void testOfferNull(){
163 <        try {
164 <            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
165 <            q.offer(null);
122 <            fail("should throw NPE");
123 <        } catch (NullPointerException success) { }  
124 <    }
125 <
126 <    public void testOffer(){
162 >    /**
163 >     * Offer succeeds if not full; fails if full
164 >     */
165 >    public void testOffer() {
166          LinkedBlockingQueue q = new LinkedBlockingQueue(1);
167          assertTrue(q.offer(zero));
168          assertFalse(q.offer(one));
169      }
170  
171 <    public void testAdd(){
172 <        try {
173 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
174 <            for (int i = 0; i < SIZE; ++i) {
175 <                assertTrue(q.add(new Integer(i)));
176 <            }
177 <            assertEquals(0, q.remainingCapacity());
171 >    /**
172 >     * add succeeds if not full; throws IllegalStateException if full
173 >     */
174 >    public void testAdd() {
175 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
176 >        for (int i = 0; i < SIZE; ++i)
177 >            assertTrue(q.add(new Integer(i)));
178 >        assertEquals(0, q.remainingCapacity());
179 >        try {
180              q.add(new Integer(SIZE));
181 <        } catch (IllegalStateException success){
182 <        }  
181 >            shouldThrow();
182 >        } catch (IllegalStateException success) {}
183      }
184  
185 <    public void testAddAll1(){
185 >    /**
186 >     * addAll(this) throws IllegalArgumentException
187 >     */
188 >    public void testAddAllSelf() {
189 >        LinkedBlockingQueue q = populatedQueue(SIZE);
190          try {
191 <            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
192 <            q.addAll(null);
193 <            fail("Cannot add null collection");
149 <        }
150 <        catch (NullPointerException success) {}
151 <    }
152 <    public void testAddAll2(){
153 <        try {
154 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
155 <            Integer[] ints = new Integer[SIZE];
156 <            q.addAll(Arrays.asList(ints));
157 <            fail("Cannot add null elements");
158 <        }
159 <        catch (NullPointerException success) {}
160 <    }
161 <    public void testAddAll3(){
162 <        try {
163 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
164 <            Integer[] ints = new Integer[SIZE];
165 <            for (int i = 0; i < SIZE-1; ++i)
166 <                ints[i] = new Integer(i);
167 <            q.addAll(Arrays.asList(ints));
168 <            fail("Cannot add null elements");
169 <        }
170 <        catch (NullPointerException success) {}
171 <    }
172 <    public void testAddAll4(){
173 <        try {
174 <            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
175 <            Integer[] ints = new Integer[SIZE];
176 <            for (int i = 0; i < SIZE; ++i)
177 <                ints[i] = new Integer(i);
178 <            q.addAll(Arrays.asList(ints));
179 <            fail("Cannot add with insufficient capacity");
180 <        }
181 <        catch (IllegalStateException success) {}
182 <    }
183 <    public void testAddAll5(){
184 <        try {
185 <            Integer[] empty = new Integer[0];
186 <            Integer[] ints = new Integer[SIZE];
187 <            for (int i = 0; i < SIZE; ++i)
188 <                ints[i] = new Integer(i);
189 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
190 <            assertFalse(q.addAll(Arrays.asList(empty)));
191 <            assertTrue(q.addAll(Arrays.asList(ints)));
192 <            for (int i = 0; i < SIZE; ++i)
193 <                assertEquals(ints[i], q.poll());
194 <        }
195 <        finally {}
196 <    }
197 <
198 <     public void testPutNull() {
199 <        try {
200 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
201 <            q.put(null);
202 <            fail("put should throw NPE");
203 <        }
204 <        catch (NullPointerException success){
205 <        }  
206 <        catch (InterruptedException ie) {
207 <            fail("Unexpected exception");
208 <        }
209 <     }
210 <
211 <     public void testPut() {
212 <         try {
213 <             LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
214 <             for (int i = 0; i < SIZE; ++i) {
215 <                 Integer I = new Integer(i);
216 <                 q.put(I);
217 <                 assertTrue(q.contains(I));
218 <             }
219 <             assertEquals(0, q.remainingCapacity());
220 <         }
221 <        catch (InterruptedException ie) {
222 <            fail("Unexpected exception");
223 <        }
224 <    }
225 <
226 <    public void testBlockingPut(){
227 <        Thread t = new Thread(new Runnable() {
228 <                public void run() {
229 <                    int added = 0;
230 <                    try {
231 <                        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
232 <                        for (int i = 0; i < SIZE; ++i) {
233 <                            q.put(new Integer(i));
234 <                            ++added;
235 <                        }
236 <                        q.put(new Integer(SIZE));
237 <                        threadFail("put should block");
238 <                    } catch (InterruptedException ie){
239 <                        threadAssertEquals(added, SIZE);
240 <                    }  
241 <                }});
242 <        t.start();
243 <        try {
244 <           Thread.sleep(SHORT_DELAY_MS);
245 <           t.interrupt();
246 <           t.join();
247 <        }
248 <        catch (InterruptedException ie) {
249 <            fail("Unexpected exception");
250 <        }
191 >            q.addAll(q);
192 >            shouldThrow();
193 >        } catch (IllegalArgumentException success) {}
194      }
195  
196 <    public void testPutWithTake() {
197 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
198 <        Thread t = new Thread(new Runnable() {
199 <                public void run(){
200 <                    int added = 0;
201 <                    try {
202 <                        q.put(new Object());
203 <                        ++added;
204 <                        q.put(new Object());
205 <                        ++added;
206 <                        q.put(new Object());
207 <                        ++added;
208 <                        q.put(new Object());
209 <                        ++added;
267 <                        threadFail("Should block");
268 <                    } catch (InterruptedException e){
269 <                        threadAssertTrue(added >= 2);
270 <                    }
271 <                }
272 <            });
273 <        try {
274 <            t.start();
275 <            Thread.sleep(SHORT_DELAY_MS);
276 <            q.take();
277 <            t.interrupt();
278 <            t.join();
279 <        } catch (Exception e){
280 <            fail("Unexpected exception");
281 <        }
196 >    /**
197 >     * addAll of a collection with any null elements throws NPE after
198 >     * possibly adding some elements
199 >     */
200 >    public void testAddAll3() {
201 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
202 >        Integer[] ints = new Integer[SIZE];
203 >        for (int i = 0; i < SIZE-1; ++i)
204 >            ints[i] = new Integer(i);
205 >        Collection<Integer> elements = Arrays.asList(ints);
206 >        try {
207 >            q.addAll(elements);
208 >            shouldThrow();
209 >        } catch (NullPointerException success) {}
210      }
211  
212 <    public void testTimedOffer() {
213 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
214 <        Thread t = new Thread(new Runnable() {
215 <                public void run(){
216 <                    try {
217 <                        q.put(new Object());
218 <                        q.put(new Object());
219 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
220 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
221 <                        threadFail("Should block");
222 <                    } catch (InterruptedException success){}
223 <                }
224 <            });
297 <        
298 <        try {
299 <            t.start();
300 <            Thread.sleep(SMALL_DELAY_MS);
301 <            t.interrupt();
302 <            t.join();
303 <        } catch (Exception e){
304 <            fail("Unexpected exception");
305 <        }
212 >    /**
213 >     * addAll throws IllegalStateException if not enough room
214 >     */
215 >    public void testAddAll4() {
216 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1);
217 >        Integer[] ints = new Integer[SIZE];
218 >        for (int i = 0; i < SIZE; ++i)
219 >            ints[i] = new Integer(i);
220 >        Collection<Integer> elements = Arrays.asList(ints);
221 >        try {
222 >            q.addAll(elements);
223 >            shouldThrow();
224 >        } catch (IllegalStateException success) {}
225      }
226  
227 <    public void testTake(){
228 <        try {
229 <            LinkedBlockingQueue q = populatedQueue(SIZE);
230 <            for (int i = 0; i < SIZE; ++i) {
231 <                assertEquals(i, ((Integer)q.take()).intValue());
232 <            }
233 <        } catch (InterruptedException e){
234 <            fail("Unexpected exception");
235 <        }  
227 >    /**
228 >     * Queue contains all elements, in traversal order, of successful addAll
229 >     */
230 >    public void testAddAll5() {
231 >        Integer[] empty = new Integer[0];
232 >        Integer[] ints = new Integer[SIZE];
233 >        for (int i = 0; i < SIZE; ++i)
234 >            ints[i] = new Integer(i);
235 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
236 >        assertFalse(q.addAll(Arrays.asList(empty)));
237 >        assertTrue(q.addAll(Arrays.asList(ints)));
238 >        for (int i = 0; i < SIZE; ++i)
239 >            assertEquals(ints[i], q.poll());
240      }
241  
242 <    public void testTakeFromEmpty() {
243 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
244 <        Thread t = new Thread(new Runnable() {
245 <                public void run(){
246 <                    try {
247 <                        q.take();
248 <                        threadFail("Should block");
249 <                    } catch (InterruptedException success){ }                
250 <                }
328 <            });
329 <        try {
330 <            t.start();
331 <            Thread.sleep(SHORT_DELAY_MS);
332 <            t.interrupt();
333 <            t.join();
334 <        } catch (Exception e){
335 <            fail("Unexpected exception");
242 >    /**
243 >     * all elements successfully put are contained
244 >     */
245 >    public void testPut() throws InterruptedException {
246 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
247 >        for (int i = 0; i < SIZE; ++i) {
248 >            Integer I = new Integer(i);
249 >            q.put(I);
250 >            assertTrue(q.contains(I));
251          }
252 +        assertEquals(0, q.remainingCapacity());
253      }
254  
255 <    public void testBlockingTake(){
256 <        Thread t = new Thread(new Runnable() {
257 <                public void run() {
258 <                    try {
259 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
260 <                        for (int i = 0; i < SIZE; ++i) {
261 <                            assertEquals(i, ((Integer)q.take()).intValue());
262 <                        }
263 <                        q.take();
264 <                        threadFail("take should block");
265 <                    } catch (InterruptedException success){
266 <                    }  
267 <                }});
268 <        t.start();
269 <        try {
270 <           Thread.sleep(SHORT_DELAY_MS);
271 <           t.interrupt();
272 <           t.join();
273 <        }
274 <        catch (InterruptedException ie) {
275 <            fail("Unexpected exception");
276 <        }
255 >    /**
256 >     * put blocks interruptibly if full
257 >     */
258 >    public void testBlockingPut() throws InterruptedException {
259 >        final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
260 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
261 >        Thread t = newStartedThread(new CheckedRunnable() {
262 >            public void realRun() throws InterruptedException {
263 >                for (int i = 0; i < SIZE; ++i)
264 >                    q.put(i);
265 >                assertEquals(SIZE, q.size());
266 >                assertEquals(0, q.remainingCapacity());
267 >
268 >                Thread.currentThread().interrupt();
269 >                try {
270 >                    q.put(99);
271 >                    shouldThrow();
272 >                } catch (InterruptedException success) {}
273 >                assertFalse(Thread.interrupted());
274 >
275 >                pleaseInterrupt.countDown();
276 >                try {
277 >                    q.put(99);
278 >                    shouldThrow();
279 >                } catch (InterruptedException success) {}
280 >                assertFalse(Thread.interrupted());
281 >            }});
282 >
283 >        await(pleaseInterrupt);
284 >        assertThreadStaysAlive(t);
285 >        t.interrupt();
286 >        awaitTermination(t);
287 >        assertEquals(SIZE, q.size());
288 >        assertEquals(0, q.remainingCapacity());
289 >    }
290 >
291 >    /**
292 >     * put blocks interruptibly waiting for take when full
293 >     */
294 >    public void testPutWithTake() throws InterruptedException {
295 >        final int capacity = 2;
296 >        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
297 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
298 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
299 >        Thread t = newStartedThread(new CheckedRunnable() {
300 >            public void realRun() throws InterruptedException {
301 >                for (int i = 0; i < capacity; i++)
302 >                    q.put(i);
303 >                pleaseTake.countDown();
304 >                q.put(86);
305 >
306 >                pleaseInterrupt.countDown();
307 >                try {
308 >                    q.put(99);
309 >                    shouldThrow();
310 >                } catch (InterruptedException success) {}
311 >                assertFalse(Thread.interrupted());
312 >            }});
313 >
314 >        await(pleaseTake);
315 >        assertEquals(q.remainingCapacity(), 0);
316 >        assertEquals(0, q.take());
317 >
318 >        await(pleaseInterrupt);
319 >        assertThreadStaysAlive(t);
320 >        t.interrupt();
321 >        awaitTermination(t);
322 >        assertEquals(q.remainingCapacity(), 0);
323      }
324  
325 +    /**
326 +     * timed offer times out if full and elements not taken
327 +     */
328 +    public void testTimedOffer() {
329 +        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
330 +        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
331 +        Thread t = newStartedThread(new CheckedRunnable() {
332 +            public void realRun() throws InterruptedException {
333 +                q.put(new Object());
334 +                q.put(new Object());
335 +                long startTime = System.nanoTime();
336 +                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
337 +                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
338 +                pleaseInterrupt.countDown();
339 +                try {
340 +                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
341 +                    shouldThrow();
342 +                } catch (InterruptedException success) {}
343 +            }});
344 +
345 +        await(pleaseInterrupt);
346 +        assertThreadStaysAlive(t);
347 +        t.interrupt();
348 +        awaitTermination(t);
349 +    }
350  
351 <    public void testPoll(){
351 >    /**
352 >     * take retrieves elements in FIFO order
353 >     */
354 >    public void testTake() throws InterruptedException {
355          LinkedBlockingQueue q = populatedQueue(SIZE);
356          for (int i = 0; i < SIZE; ++i) {
357 <            assertEquals(i, ((Integer)q.poll()).intValue());
357 >            assertEquals(i, q.take());
358          }
369        assertNull(q.poll());
359      }
360  
361 <    public void testTimedPoll0() {
362 <        try {
363 <            LinkedBlockingQueue q = populatedQueue(SIZE);
364 <            for (int i = 0; i < SIZE; ++i) {
365 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
366 <            }
367 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
368 <        } catch (InterruptedException e){
369 <            fail("Unexpected exception");
370 <        }  
361 >    /**
362 >     * Take removes existing elements until empty, then blocks interruptibly
363 >     */
364 >    public void testBlockingTake() throws InterruptedException {
365 >        final BlockingQueue q = populatedQueue(SIZE);
366 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
367 >        Thread t = newStartedThread(new CheckedRunnable() {
368 >            public void realRun() throws InterruptedException {
369 >                for (int i = 0; i < SIZE; ++i) {
370 >                    assertEquals(i, q.take());
371 >                }
372 >
373 >                Thread.currentThread().interrupt();
374 >                try {
375 >                    q.take();
376 >                    shouldThrow();
377 >                } catch (InterruptedException success) {}
378 >                assertFalse(Thread.interrupted());
379 >
380 >                pleaseInterrupt.countDown();
381 >                try {
382 >                    q.take();
383 >                    shouldThrow();
384 >                } catch (InterruptedException success) {}
385 >                assertFalse(Thread.interrupted());
386 >            }});
387 >
388 >        await(pleaseInterrupt);
389 >        assertThreadStaysAlive(t);
390 >        t.interrupt();
391 >        awaitTermination(t);
392      }
393  
394 <    public void testTimedPoll() {
395 <        try {
396 <            LinkedBlockingQueue q = populatedQueue(SIZE);
397 <            for (int i = 0; i < SIZE; ++i) {
398 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
399 <            }
400 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
391 <        } catch (InterruptedException e){
392 <            fail("Unexpected exception");
393 <        }  
394 <    }
395 <
396 <    public void testInterruptedTimedPoll(){
397 <        Thread t = new Thread(new Runnable() {
398 <                public void run() {
399 <                    try {
400 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
401 <                        for (int i = 0; i < SIZE; ++i) {
402 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
403 <                        }
404 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
405 <                    } catch (InterruptedException success){
406 <                    }  
407 <                }});
408 <        t.start();
409 <        try {
410 <           Thread.sleep(SHORT_DELAY_MS);
411 <           t.interrupt();
412 <           t.join();
413 <        }
414 <        catch (InterruptedException ie) {
415 <            fail("Unexpected exception");
394 >    /**
395 >     * poll succeeds unless empty
396 >     */
397 >    public void testPoll() {
398 >        LinkedBlockingQueue q = populatedQueue(SIZE);
399 >        for (int i = 0; i < SIZE; ++i) {
400 >            assertEquals(i, q.poll());
401          }
402 +        assertNull(q.poll());
403      }
404  
405 <    public void testTimedPollWithOffer(){
406 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
407 <        Thread t = new Thread(new Runnable() {
408 <                public void run(){
409 <                    try {
410 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
411 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
426 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
427 <                        threadFail("Should block");
428 <                    } catch (InterruptedException success) { }                
429 <                }
430 <            });
431 <        try {
432 <            t.start();
433 <            Thread.sleep(SMALL_DELAY_MS);
434 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
435 <            t.interrupt();
436 <            t.join();
437 <        } catch (Exception e){
438 <            fail("Unexpected exception");
405 >    /**
406 >     * timed poll with zero timeout succeeds when non-empty, else times out
407 >     */
408 >    public void testTimedPoll0() throws InterruptedException {
409 >        LinkedBlockingQueue q = populatedQueue(SIZE);
410 >        for (int i = 0; i < SIZE; ++i) {
411 >            assertEquals(i, q.poll(0, MILLISECONDS));
412          }
413 <    }  
413 >        assertNull(q.poll(0, MILLISECONDS));
414 >    }
415 >
416 >    /**
417 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
418 >     */
419 >    public void testTimedPoll() throws InterruptedException {
420 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
421 >        for (int i = 0; i < SIZE; ++i) {
422 >            long startTime = System.nanoTime();
423 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
424 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
425 >        }
426 >        long startTime = System.nanoTime();
427 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
428 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
429 >        checkEmpty(q);
430 >    }
431  
432 +    /**
433 +     * Interrupted timed poll throws InterruptedException instead of
434 +     * returning timeout status
435 +     */
436 +    public void testInterruptedTimedPoll() throws InterruptedException {
437 +        final BlockingQueue<Integer> q = populatedQueue(SIZE);
438 +        final CountDownLatch aboutToWait = new CountDownLatch(1);
439 +        Thread t = newStartedThread(new CheckedRunnable() {
440 +            public void realRun() throws InterruptedException {
441 +                for (int i = 0; i < SIZE; ++i) {
442 +                    long t0 = System.nanoTime();
443 +                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
444 +                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
445 +                }
446 +                long t0 = System.nanoTime();
447 +                aboutToWait.countDown();
448 +                try {
449 +                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
450 +                    shouldThrow();
451 +                } catch (InterruptedException success) {
452 +                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
453 +                }
454 +            }});
455 +
456 +        aboutToWait.await();
457 +        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
458 +        t.interrupt();
459 +        awaitTermination(t, MEDIUM_DELAY_MS);
460 +        checkEmpty(q);
461 +    }
462  
463 <    public void testPeek(){
463 >    /**
464 >     * peek returns next element, or null if empty
465 >     */
466 >    public void testPeek() {
467          LinkedBlockingQueue q = populatedQueue(SIZE);
468          for (int i = 0; i < SIZE; ++i) {
469 <            assertEquals(i, ((Integer)q.peek()).intValue());
470 <            q.poll();
469 >            assertEquals(i, q.peek());
470 >            assertEquals(i, q.poll());
471              assertTrue(q.peek() == null ||
472 <                       i != ((Integer)q.peek()).intValue());
472 >                       !q.peek().equals(i));
473          }
474 <        assertNull(q.peek());
474 >        assertNull(q.peek());
475      }
476  
477 <    public void testElement(){
477 >    /**
478 >     * element returns next element, or throws NSEE if empty
479 >     */
480 >    public void testElement() {
481          LinkedBlockingQueue q = populatedQueue(SIZE);
482          for (int i = 0; i < SIZE; ++i) {
483 <            assertEquals(i, ((Integer)q.element()).intValue());
484 <            q.poll();
483 >            assertEquals(i, q.element());
484 >            assertEquals(i, q.poll());
485          }
486          try {
487              q.element();
488 <            fail("no such element");
489 <        }
464 <        catch (NoSuchElementException success) {}
488 >            shouldThrow();
489 >        } catch (NoSuchElementException success) {}
490      }
491  
492 <    public void testRemove(){
492 >    /**
493 >     * remove removes next element, or throws NSEE if empty
494 >     */
495 >    public void testRemove() {
496          LinkedBlockingQueue q = populatedQueue(SIZE);
497          for (int i = 0; i < SIZE; ++i) {
498 <            assertEquals(i, ((Integer)q.remove()).intValue());
498 >            assertEquals(i, q.remove());
499          }
500          try {
501              q.remove();
502 <            fail("remove should throw");
503 <        } catch (NoSuchElementException success){
476 <        }  
502 >            shouldThrow();
503 >        } catch (NoSuchElementException success) {}
504      }
505  
506 <    public void testRemoveElement(){
506 >    /**
507 >     * remove(x) removes x and returns true if present
508 >     */
509 >    public void testRemoveElement() {
510          LinkedBlockingQueue q = populatedQueue(SIZE);
511          for (int i = 1; i < SIZE; i+=2) {
512 <            assertTrue(q.remove(new Integer(i)));
512 >            assertTrue(q.contains(i));
513 >            assertTrue(q.remove(i));
514 >            assertFalse(q.contains(i));
515 >            assertTrue(q.contains(i-1));
516          }
517          for (int i = 0; i < SIZE; i+=2) {
518 <            assertTrue(q.remove(new Integer(i)));
519 <            assertFalse(q.remove(new Integer(i+1)));
518 >            assertTrue(q.contains(i));
519 >            assertTrue(q.remove(i));
520 >            assertFalse(q.contains(i));
521 >            assertFalse(q.remove(i+1));
522 >            assertFalse(q.contains(i+1));
523          }
524          assertTrue(q.isEmpty());
525      }
526 <        
527 <    public void testContains(){
526 >
527 >    /**
528 >     * An add following remove(x) succeeds
529 >     */
530 >    public void testRemoveElementAndAdd() throws InterruptedException {
531 >        LinkedBlockingQueue q = new LinkedBlockingQueue();
532 >        assertTrue(q.add(new Integer(1)));
533 >        assertTrue(q.add(new Integer(2)));
534 >        assertTrue(q.remove(new Integer(1)));
535 >        assertTrue(q.remove(new Integer(2)));
536 >        assertTrue(q.add(new Integer(3)));
537 >        assertTrue(q.take() != null);
538 >    }
539 >
540 >    /**
541 >     * contains(x) reports true when elements added but not yet removed
542 >     */
543 >    public void testContains() {
544          LinkedBlockingQueue q = populatedQueue(SIZE);
545          for (int i = 0; i < SIZE; ++i) {
546              assertTrue(q.contains(new Integer(i)));
# Line 497 | Line 549 | public class LinkedBlockingQueueTest ext
549          }
550      }
551  
552 <    public void testClear(){
552 >    /**
553 >     * clear removes all elements
554 >     */
555 >    public void testClear() {
556          LinkedBlockingQueue q = populatedQueue(SIZE);
557          q.clear();
558          assertTrue(q.isEmpty());
# Line 505 | Line 560 | public class LinkedBlockingQueueTest ext
560          assertEquals(SIZE, q.remainingCapacity());
561          q.add(one);
562          assertFalse(q.isEmpty());
563 +        assertTrue(q.contains(one));
564          q.clear();
565          assertTrue(q.isEmpty());
566      }
567  
568 <    public void testContainsAll(){
568 >    /**
569 >     * containsAll(c) is true when c contains a subset of elements
570 >     */
571 >    public void testContainsAll() {
572          LinkedBlockingQueue q = populatedQueue(SIZE);
573          LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
574          for (int i = 0; i < SIZE; ++i) {
# Line 520 | Line 579 | public class LinkedBlockingQueueTest ext
579          assertTrue(p.containsAll(q));
580      }
581  
582 <    public void testRetainAll(){
582 >    /**
583 >     * retainAll(c) retains only those elements of c and reports true if changed
584 >     */
585 >    public void testRetainAll() {
586          LinkedBlockingQueue q = populatedQueue(SIZE);
587          LinkedBlockingQueue p = populatedQueue(SIZE);
588          for (int i = 0; i < SIZE; ++i) {
# Line 536 | Line 598 | public class LinkedBlockingQueueTest ext
598          }
599      }
600  
601 <    public void testRemoveAll(){
601 >    /**
602 >     * removeAll(c) removes only those elements of c and reports true if changed
603 >     */
604 >    public void testRemoveAll() {
605          for (int i = 1; i < SIZE; ++i) {
606              LinkedBlockingQueue q = populatedQueue(SIZE);
607              LinkedBlockingQueue p = populatedQueue(i);
# Line 549 | Line 614 | public class LinkedBlockingQueueTest ext
614          }
615      }
616  
617 +    /**
618 +     * toArray contains all elements in FIFO order
619 +     */
620 +    public void testToArray() {
621 +        LinkedBlockingQueue q = populatedQueue(SIZE);
622 +        Object[] o = q.toArray();
623 +        for (int i = 0; i < o.length; i++)
624 +            assertSame(o[i], q.poll());
625 +    }
626 +
627 +    /**
628 +     * toArray(a) contains all elements in FIFO order
629 +     */
630 +    public void testToArray2() throws InterruptedException {
631 +        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
632 +        Integer[] ints = new Integer[SIZE];
633 +        Integer[] array = q.toArray(ints);
634 +        assertSame(ints, array);
635 +        for (int i = 0; i < ints.length; i++)
636 +            assertSame(ints[i], q.poll());
637 +    }
638  
639 <    public void testToArray(){
639 >    /**
640 >     * toArray(incompatible array type) throws ArrayStoreException
641 >     */
642 >    public void testToArray1_BadArg() {
643          LinkedBlockingQueue q = populatedQueue(SIZE);
644 <        Object[] o = q.toArray();
645 <        try {
646 <        for(int i = 0; i < o.length; i++)
647 <            assertEquals(o[i], q.take());
559 <        } catch (InterruptedException e){
560 <            fail("Unexpected exception");
561 <        }    
562 <    }
563 <
564 <    public void testToArray2(){
565 <        LinkedBlockingQueue q = populatedQueue(SIZE);
566 <        Integer[] ints = new Integer[SIZE];
567 <        ints = (Integer[])q.toArray(ints);
568 <        try {
569 <            for(int i = 0; i < ints.length; i++)
570 <                assertEquals(ints[i], q.take());
571 <        } catch (InterruptedException e){
572 <            fail("Unexpected exception");
573 <        }    
574 <    }
575 <    
576 <    public void testIterator(){
577 <        LinkedBlockingQueue q = populatedQueue(SIZE);
578 <        Iterator it = q.iterator();
579 <        try {
580 <            while(it.hasNext()){
581 <                assertEquals(it.next(), q.take());
582 <            }
583 <        } catch (InterruptedException e){
584 <            fail("Unexpected exception");
585 <        }    
644 >        try {
645 >            q.toArray(new String[10]);
646 >            shouldThrow();
647 >        } catch (ArrayStoreException success) {}
648      }
649  
650 <    public void testIteratorOrdering() {
650 >    /**
651 >     * iterator iterates through all elements
652 >     */
653 >    public void testIterator() throws InterruptedException {
654 >        LinkedBlockingQueue q = populatedQueue(SIZE);
655 >        Iterator it = q.iterator();
656 >        while (it.hasNext()) {
657 >            assertEquals(it.next(), q.take());
658 >        }
659 >    }
660  
661 +    /**
662 +     * iterator.remove removes current element
663 +     */
664 +    public void testIteratorRemove() {
665          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
591
592        q.add(one);
666          q.add(two);
667 +        q.add(one);
668          q.add(three);
669  
670 <        assertEquals("queue should be full", 0, q.remainingCapacity());
670 >        Iterator it = q.iterator();
671 >        it.next();
672 >        it.remove();
673 >
674 >        it = q.iterator();
675 >        assertSame(it.next(), one);
676 >        assertSame(it.next(), three);
677 >        assertFalse(it.hasNext());
678 >    }
679  
680 +    /**
681 +     * iterator ordering is FIFO
682 +     */
683 +    public void testIteratorOrdering() {
684 +        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
685 +        q.add(one);
686 +        q.add(two);
687 +        q.add(three);
688 +        assertEquals(0, q.remainingCapacity());
689          int k = 0;
690          for (Iterator it = q.iterator(); it.hasNext();) {
691 <            int i = ((Integer)(it.next())).intValue();
601 <            assertEquals("items should come out in order", ++k, i);
691 >            assertEquals(++k, it.next());
692          }
693 <
604 <        assertEquals("should go through 3 elements", 3, k);
693 >        assertEquals(3, k);
694      }
695  
696 <    public void testWeaklyConsistentIteration () {
697 <
696 >    /**
697 >     * Modifications do not cause iterators to fail
698 >     */
699 >    public void testWeaklyConsistentIteration() {
700          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
610
701          q.add(one);
702          q.add(two);
703          q.add(three);
704 <
705 <        try {
706 <            for (Iterator it = q.iterator(); it.hasNext();) {
617 <                q.remove();
618 <                it.next();
619 <            }
620 <        }
621 <        catch (ConcurrentModificationException e) {
622 <            fail("weakly consistent iterator; should not get CME");
704 >        for (Iterator it = q.iterator(); it.hasNext();) {
705 >            q.remove();
706 >            it.next();
707          }
708 <
625 <        assertEquals("queue should be empty again", 0, q.size());
708 >        assertEquals(0, q.size());
709      }
710  
711 <
712 <    public void testToString(){
711 >    /**
712 >     * toString contains toStrings of elements
713 >     */
714 >    public void testToString() {
715          LinkedBlockingQueue q = populatedQueue(SIZE);
716          String s = q.toString();
717          for (int i = 0; i < SIZE; ++i) {
718 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
718 >            assertTrue(s.contains(String.valueOf(i)));
719          }
720 <    }        
636 <
720 >    }
721  
722 +    /**
723 +     * offer transfers elements across Executor tasks
724 +     */
725      public void testOfferInExecutor() {
639
726          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
641
727          q.add(one);
728          q.add(two);
644
729          ExecutorService executor = Executors.newFixedThreadPool(2);
730 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
731 +        executor.execute(new CheckedRunnable() {
732 +            public void realRun() throws InterruptedException {
733 +                assertFalse(q.offer(three));
734 +                threadsStarted.await();
735 +                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
736 +                assertEquals(0, q.remainingCapacity());
737 +            }});
738 +
739 +        executor.execute(new CheckedRunnable() {
740 +            public void realRun() throws InterruptedException {
741 +                threadsStarted.await();
742 +                assertSame(one, q.take());
743 +            }});
744  
647        executor.execute(new Runnable() {
648            public void run() {
649                threadAssertFalse(q.offer(three));
650                try {
651                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
652                    threadAssertEquals(0, q.remainingCapacity());
653                }
654                catch (InterruptedException e) {
655                    threadFail("should not be interrupted");
656                }
657            }
658        });
659
660        executor.execute(new Runnable() {
661            public void run() {
662                try {
663                    Thread.sleep(SMALL_DELAY_MS);
664                    threadAssertEquals(one, q.take());
665                }
666                catch (InterruptedException e) {
667                    threadFail("should not be interrupted");
668                }
669            }
670        });
671        
745          joinPool(executor);
673
746      }
747  
748 +    /**
749 +     * timed poll retrieves elements across Executor threads
750 +     */
751      public void testPollInExecutor() {
677
752          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
753 <
753 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
754          ExecutorService executor = Executors.newFixedThreadPool(2);
755 +        executor.execute(new CheckedRunnable() {
756 +            public void realRun() throws InterruptedException {
757 +                assertNull(q.poll());
758 +                threadsStarted.await();
759 +                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
760 +                checkEmpty(q);
761 +            }});
762 +
763 +        executor.execute(new CheckedRunnable() {
764 +            public void realRun() throws InterruptedException {
765 +                threadsStarted.await();
766 +                q.put(one);
767 +            }});
768  
682        executor.execute(new Runnable() {
683            public void run() {
684                threadAssertNull(q.poll());
685                try {
686                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
687                    threadAssertTrue(q.isEmpty());
688                }
689                catch (InterruptedException e) {
690                    threadFail("should not be interrupted");
691                }
692            }
693        });
694
695        executor.execute(new Runnable() {
696            public void run() {
697                try {
698                    Thread.sleep(SMALL_DELAY_MS);
699                    q.put(one);
700                }
701                catch (InterruptedException e) {
702                    threadFail("should not be interrupted");
703                }
704            }
705        });
706        
769          joinPool(executor);
770      }
771  
772 <    public void testSerialization() {
772 >    /**
773 >     * A deserialized serialized queue has same elements in same order
774 >     */
775 >    public void testSerialization() throws Exception {
776 >        Queue x = populatedQueue(SIZE);
777 >        Queue y = serialClone(x);
778 >
779 >        assertTrue(x != y);
780 >        assertEquals(x.size(), y.size());
781 >        assertEquals(x.toString(), y.toString());
782 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
783 >        while (!x.isEmpty()) {
784 >            assertFalse(y.isEmpty());
785 >            assertEquals(x.remove(), y.remove());
786 >        }
787 >        assertTrue(y.isEmpty());
788 >    }
789 >
790 >    /**
791 >     * drainTo(c) empties queue into another collection c
792 >     */
793 >    public void testDrainTo() {
794          LinkedBlockingQueue q = populatedQueue(SIZE);
795 +        ArrayList l = new ArrayList();
796 +        q.drainTo(l);
797 +        assertEquals(q.size(), 0);
798 +        assertEquals(l.size(), SIZE);
799 +        for (int i = 0; i < SIZE; ++i)
800 +            assertEquals(l.get(i), new Integer(i));
801 +        q.add(zero);
802 +        q.add(one);
803 +        assertFalse(q.isEmpty());
804 +        assertTrue(q.contains(zero));
805 +        assertTrue(q.contains(one));
806 +        l.clear();
807 +        q.drainTo(l);
808 +        assertEquals(q.size(), 0);
809 +        assertEquals(l.size(), 2);
810 +        for (int i = 0; i < 2; ++i)
811 +            assertEquals(l.get(i), new Integer(i));
812 +    }
813  
814 <        try {
815 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
816 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
817 <            out.writeObject(q);
818 <            out.close();
819 <
820 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
821 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
822 <            LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
823 <            assertEquals(q.size(), r.size());
824 <            while (!q.isEmpty())
825 <                assertEquals(q.remove(), r.remove());
826 <        } catch(Exception e){
827 <            e.printStackTrace();
828 <            fail("unexpected exception");
814 >    /**
815 >     * drainTo empties full queue, unblocking a waiting put.
816 >     */
817 >    public void testDrainToWithActivePut() throws InterruptedException {
818 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
819 >        Thread t = new Thread(new CheckedRunnable() {
820 >            public void realRun() throws InterruptedException {
821 >                q.put(new Integer(SIZE+1));
822 >            }});
823 >
824 >        t.start();
825 >        ArrayList l = new ArrayList();
826 >        q.drainTo(l);
827 >        assertTrue(l.size() >= SIZE);
828 >        for (int i = 0; i < SIZE; ++i)
829 >            assertEquals(l.get(i), new Integer(i));
830 >        t.join();
831 >        assertTrue(q.size() + l.size() >= SIZE);
832 >    }
833 >
834 >    /**
835 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
836 >     */
837 >    public void testDrainToN() {
838 >        LinkedBlockingQueue q = new LinkedBlockingQueue();
839 >        for (int i = 0; i < SIZE + 2; ++i) {
840 >            for (int j = 0; j < SIZE; j++)
841 >                assertTrue(q.offer(new Integer(j)));
842 >            ArrayList l = new ArrayList();
843 >            q.drainTo(l, i);
844 >            int k = (i < SIZE) ? i : SIZE;
845 >            assertEquals(l.size(), k);
846 >            assertEquals(q.size(), SIZE-k);
847 >            for (int j = 0; j < k; ++j)
848 >                assertEquals(l.get(j), new Integer(j));
849 >            while (q.poll() != null) ;
850          }
851      }
852  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines