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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines