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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines