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.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.29 by jsr166, Wed Oct 6 07:49:22 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) {
44 >    private LinkedBlockingQueue populatedQueue(int n) {
45          LinkedBlockingQueue q = new LinkedBlockingQueue(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(){
56 <        assertEquals(N, new LinkedBlockingQueue(N).remainingCapacity());
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 <        }
52 <        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 <        }
61 <        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[N];
89 >            Integer[] ints = new Integer[SIZE];
90              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
91 <            fail("Cannot make with null elements");
92 <        }
70 <        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[N];
101 <            for (int i = 0; i < N-1; ++i)
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 <        }
81 <        catch (NullPointerException success) {}
104 >            shouldThrow();
105 >        } catch (NullPointerException success) {}
106      }
107  
108 <    public void testConstructor6(){
109 <        try {
110 <            Integer[] ints = new Integer[N];
111 <            for (int i = 0; i < N; ++i)
112 <                ints[i] = new Integer(i);
113 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
114 <            for (int i = 0; i < N; ++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());
126          assertEquals("should have room for 2", 2, q.remainingCapacity());
127 <        q.add(new Integer(1));
127 >        q.add(one);
128          assertFalse(q.isEmpty());
129 <        q.add(new Integer(2));
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(new Integer(3)));
131 >        assertEquals(0, q.remainingCapacity());
132 >        assertFalse(q.offer(three));
133      }
134  
135 <    public void testRemainingCapacity(){
136 <        LinkedBlockingQueue q = fullQueue(N);
137 <        for (int i = 0; i < N; ++i) {
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());
142 <            assertEquals(N-i, q.size());
142 >            assertEquals(SIZE-i, q.size());
143              q.remove();
144          }
145 <        for (int i = 0; i < N; ++i) {
146 <            assertEquals(N-i, q.remainingCapacity());
145 >        for (int i = 0; i < SIZE; ++i) {
146 >            assertEquals(SIZE-i, q.remainingCapacity());
147              assertEquals(i, q.size());
148              q.add(new Integer(i));
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(new Integer(0)));
180 <        assertFalse(q.offer(new Integer(1)));
179 >        assertTrue(q.offer(zero));
180 >        assertFalse(q.offer(one));
181      }
182  
183 <    public void testAdd(){
184 <        try {
185 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
186 <            for (int i = 0; i < N; ++i) {
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(N));
194 <        } catch (IllegalStateException success){
195 <        }  
193 >            q.add(new Integer(SIZE));
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 <        }
207 <        catch (NullPointerException success) {}
205 >            shouldThrow();
206 >        } catch (NullPointerException success) {}
207 >    }
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 <    public void testAddAll2(){
219 >
220 >    /**
221 >     * addAll of a collection with null elements throws NPE
222 >     */
223 >    public void testAddAll2() {
224          try {
225 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
226 <            Integer[] ints = new Integer[N];
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 <        }
163 <        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(N);
239 <            Integer[] ints = new Integer[N];
240 <            for (int i = 0; i < N-1; ++i)
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 <        }
174 <        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[N];
254 <            for (int i = 0; i < N; ++i)
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 <        }
185 <        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[N];
264 <            for (int i = 0; i < N; ++i)
265 <                ints[i] = new Integer(i);
266 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
267 <            assertFalse(q.addAll(Arrays.asList(empty)));
268 <            assertTrue(q.addAll(Arrays.asList(ints)));
269 <            for (int i = 0; i < N; ++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 {
278 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
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 <        }
208 <        catch (NullPointerException success){
209 <        }  
210 <        catch (InterruptedException ie) {
211 <            fail("Unexpected exception");
212 <        }
283 >            shouldThrow();
284 >        } catch (NullPointerException success) {}
285       }
286  
287 <     public void testPut() {
288 <         try {
289 <             LinkedBlockingQueue q = new LinkedBlockingQueue(N);
290 <             for (int i = 0; i < N; ++i) {
291 <                 Integer I = new Integer(i);
292 <                 q.put(I);
293 <                 assertTrue(q.contains(I));
294 <             }
295 <             assertEquals(0, q.remainingCapacity());
224 <         }
225 <        catch (InterruptedException ie) {
226 <            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 testBlockingPut(){
301 <        Thread t = new Thread(new Runnable() {
302 <                public void run() {
303 <                    int added = 0;
304 <                    try {
305 <                        LinkedBlockingQueue q = new LinkedBlockingQueue(N);
306 <                        for (int i = 0; i < N; ++i) {
307 <                            q.put(new Integer(i));
308 <                            ++added;
309 <                        }
310 <                        q.put(new Integer(N));
311 <                        fail("put should block");
312 <                    } catch (InterruptedException ie){
313 <                        assertEquals(added, N);
314 <                    }  
315 <                }});
300 >    /**
301 >     * put blocks interruptibly if full
302 >     */
303 >    public void testBlockingPut() throws InterruptedException {
304 >        final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
305 >        Thread t = new Thread(new CheckedRunnable() {
306 >            public void realRun() throws InterruptedException {
307 >                for (int i = 0; i < SIZE; ++i)
308 >                    q.put(i);
309 >                assertEquals(SIZE, q.size());
310 >                assertEquals(0, q.remainingCapacity());
311 >                try {
312 >                    q.put(99);
313 >                    shouldThrow();
314 >                } catch (InterruptedException success) {}
315 >            }});
316 >
317          t.start();
318 <        try {
319 <           Thread.sleep(SHORT_DELAY_MS);
320 <           t.interrupt();
321 <           t.join();
322 <        }
252 <        catch (InterruptedException ie) {
253 <            fail("Unexpected exception");
254 <        }
318 >        Thread.sleep(SHORT_DELAY_MS);
319 >        t.interrupt();
320 >        t.join();
321 >        assertEquals(SIZE, q.size());
322 >        assertEquals(0, q.remainingCapacity());
323      }
324  
325 <    public void testPutWithTake() {
325 >    /**
326 >     * put blocks waiting for take when full
327 >     */
328 >    public void testPutWithTake() throws InterruptedException {
329 >        final int capacity = 2;
330          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
331 <        Thread t = new Thread(new Runnable() {
332 <                public void run(){
333 <                    int added = 0;
334 <                    try {
335 <                        q.put(new Object());
336 <                        ++added;
337 <                        q.put(new Object());
338 <                        ++added;
339 <                        q.put(new Object());
340 <                        ++added;
341 <                        q.put(new Object());
342 <                        ++added;
343 <                        fail("Should block");
344 <                    } catch (InterruptedException e){
345 <                        assertTrue(added >= 2);
346 <                    }
347 <                }
348 <            });
277 <        try {
278 <            t.start();
279 <            Thread.sleep(SHORT_DELAY_MS);
280 <            q.take();
281 <            t.interrupt();
282 <            t.join();
283 <        } catch (Exception e){
284 <            fail("Unexpected exception");
285 <        }
331 >        Thread t = new Thread(new CheckedRunnable() {
332 >            public void realRun() throws InterruptedException {
333 >                for (int i = 0; i < capacity + 1; i++)
334 >                    q.put(i);
335 >                try {
336 >                    q.put(99);
337 >                    shouldThrow();
338 >                } catch (InterruptedException success) {}
339 >            }});
340 >
341 >        t.start();
342 >        Thread.sleep(SHORT_DELAY_MS);
343 >        assertEquals(q.remainingCapacity(), 0);
344 >        assertEquals(0, q.take());
345 >        Thread.sleep(SHORT_DELAY_MS);
346 >        t.interrupt();
347 >        t.join();
348 >        assertEquals(q.remainingCapacity(), 0);
349      }
350  
351 <    public void testTimedOffer() {
351 >    /**
352 >     * timed offer times out if full and elements not taken
353 >     */
354 >    public void testTimedOffer() throws InterruptedException {
355          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
356 <        Thread t = new Thread(new Runnable() {
357 <                public void run(){
358 <                    try {
359 <                        q.put(new Object());
360 <                        q.put(new Object());
361 <                        assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
362 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
363 <                        fail("Should block");
364 <                    } catch (InterruptedException success){}
365 <                }
300 <            });
301 <        
302 <        try {
303 <            t.start();
304 <            Thread.sleep(SHORT_DELAY_MS);
305 <            t.interrupt();
306 <            t.join();
307 <        } catch (Exception e){
308 <            fail("Unexpected exception");
309 <        }
310 <    }
356 >        Thread t = new Thread(new CheckedRunnable() {
357 >            public void realRun() throws InterruptedException {
358 >                q.put(new Object());
359 >                q.put(new Object());
360 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
361 >                try {
362 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
363 >                    shouldThrow();
364 >                } catch (InterruptedException success) {}
365 >            }});
366  
367 <    public void testTake(){
368 <        try {
369 <            LinkedBlockingQueue q = fullQueue(N);
370 <            for (int i = 0; i < N; ++i) {
316 <                assertEquals(i, ((Integer)q.take()).intValue());
317 <            }
318 <        } catch (InterruptedException e){
319 <            fail("Unexpected exception");
320 <        }  
367 >        t.start();
368 >        Thread.sleep(SMALL_DELAY_MS);
369 >        t.interrupt();
370 >        t.join();
371      }
372  
373 <    public void testTakeFromEmpty() {
374 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
375 <        Thread t = new Thread(new Runnable() {
376 <                public void run(){
377 <                    try {
378 <                        q.take();
379 <                        fail("Should block");
330 <                    } catch (InterruptedException success){ }                
331 <                }
332 <            });
333 <        try {
334 <            t.start();
335 <            Thread.sleep(SHORT_DELAY_MS);
336 <            t.interrupt();
337 <            t.join();
338 <        } catch (Exception e){
339 <            fail("Unexpected exception");
373 >    /**
374 >     * take retrieves elements in FIFO order
375 >     */
376 >    public void testTake() throws InterruptedException {
377 >        LinkedBlockingQueue q = populatedQueue(SIZE);
378 >        for (int i = 0; i < SIZE; ++i) {
379 >            assertEquals(i, q.take());
380          }
381      }
382  
383 <    public void testBlockingTake(){
384 <        Thread t = new Thread(new Runnable() {
385 <                public void run() {
386 <                    try {
387 <                        LinkedBlockingQueue q = fullQueue(N);
388 <                        for (int i = 0; i < N; ++i) {
389 <                            assertEquals(i, ((Integer)q.take()).intValue());
390 <                        }
391 <                        q.take();
392 <                        fail("take should block");
353 <                    } catch (InterruptedException success){
354 <                    }  
355 <                }});
383 >    /**
384 >     * take blocks interruptibly when empty
385 >     */
386 >    public void testTakeFromEmpty() throws InterruptedException {
387 >        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
388 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
389 >            public void realRun() throws InterruptedException {
390 >                q.take();
391 >            }};
392 >
393          t.start();
394 <        try {
395 <           Thread.sleep(SHORT_DELAY_MS);
396 <           t.interrupt();
360 <           t.join();
361 <        }
362 <        catch (InterruptedException ie) {
363 <            fail("Unexpected exception");
364 <        }
394 >        Thread.sleep(SHORT_DELAY_MS);
395 >        t.interrupt();
396 >        t.join();
397      }
398  
399 +    /**
400 +     * Take removes existing elements until empty, then blocks interruptibly
401 +     */
402 +    public void testBlockingTake() throws InterruptedException {
403 +        final LinkedBlockingQueue q = populatedQueue(SIZE);
404 +        Thread t = new Thread(new CheckedRunnable() {
405 +            public void realRun() throws InterruptedException {
406 +                for (int i = 0; i < SIZE; ++i) {
407 +                    assertEquals(i, q.take());
408 +                }
409 +                try {
410 +                    q.take();
411 +                    shouldThrow();
412 +                } catch (InterruptedException success) {}
413 +            }});
414  
415 <    public void testPoll(){
416 <        LinkedBlockingQueue q = fullQueue(N);
417 <        for (int i = 0; i < N; ++i) {
418 <            assertEquals(i, ((Integer)q.poll()).intValue());
372 <        }
373 <        assertNull(q.poll());
415 >        t.start();
416 >        Thread.sleep(SHORT_DELAY_MS);
417 >        t.interrupt();
418 >        t.join();
419      }
420  
421 <    public void testTimedPoll0() {
422 <        try {
423 <            LinkedBlockingQueue q = fullQueue(N);
424 <            for (int i = 0; i < N; ++i) {
425 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
426 <            }
427 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
428 <        } catch (InterruptedException e){
429 <            fail("Unexpected exception");
385 <        }  
421 >    /**
422 >     * poll succeeds unless empty
423 >     */
424 >    public void testPoll() {
425 >        LinkedBlockingQueue q = populatedQueue(SIZE);
426 >        for (int i = 0; i < SIZE; ++i) {
427 >            assertEquals(i, q.poll());
428 >        }
429 >        assertNull(q.poll());
430      }
431  
432 <    public void testTimedPoll() {
433 <        try {
434 <            LinkedBlockingQueue q = fullQueue(N);
435 <            for (int i = 0; i < N; ++i) {
436 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
437 <            }
438 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
395 <        } catch (InterruptedException e){
396 <            fail("Unexpected exception");
397 <        }  
398 <    }
399 <
400 <    public void testInterruptedTimedPoll(){
401 <        Thread t = new Thread(new Runnable() {
402 <                public void run() {
403 <                    try {
404 <                        LinkedBlockingQueue q = fullQueue(N);
405 <                        for (int i = 0; i < N; ++i) {
406 <                            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
407 <                        }
408 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
409 <                    } catch (InterruptedException success){
410 <                    }  
411 <                }});
412 <        t.start();
413 <        try {
414 <           Thread.sleep(SHORT_DELAY_MS);
415 <           t.interrupt();
416 <           t.join();
432 >    /**
433 >     * timed pool with zero timeout succeeds when non-empty, else times out
434 >     */
435 >    public void testTimedPoll0() throws InterruptedException {
436 >        LinkedBlockingQueue q = populatedQueue(SIZE);
437 >        for (int i = 0; i < SIZE; ++i) {
438 >            assertEquals(i, q.poll(0, MILLISECONDS));
439          }
440 <        catch (InterruptedException ie) {
441 <            fail("Unexpected exception");
440 >        assertNull(q.poll(0, MILLISECONDS));
441 >    }
442 >
443 >    /**
444 >     * timed pool with nonzero timeout succeeds when non-empty, else times out
445 >     */
446 >    public void testTimedPoll() throws InterruptedException {
447 >        LinkedBlockingQueue q = populatedQueue(SIZE);
448 >        for (int i = 0; i < SIZE; ++i) {
449 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
450          }
451 +        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
452      }
453  
454 <    public void testTimedPollWithOffer(){
455 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
456 <        Thread t = new Thread(new Runnable() {
457 <                public void run(){
458 <                    try {
459 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
460 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
461 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
462 <                        fail("Should block");
463 <                    } catch (InterruptedException success) { }                
454 >    /**
455 >     * Interrupted timed poll throws InterruptedException instead of
456 >     * returning timeout status
457 >     */
458 >    public void testInterruptedTimedPoll() throws InterruptedException {
459 >        Thread t = new Thread(new CheckedRunnable() {
460 >            public void realRun() throws InterruptedException {
461 >                LinkedBlockingQueue q = populatedQueue(SIZE);
462 >                for (int i = 0; i < SIZE; ++i) {
463 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
464                  }
465 <            });
466 <        try {
467 <            t.start();
468 <            Thread.sleep(SHORT_DELAY_MS * 2);
469 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
470 <            t.interrupt();
471 <            t.join();
472 <        } catch (Exception e){
473 <            fail("Unexpected exception");
474 <        }
475 <    }  
476 <
477 <
478 <    public void testPeek(){
479 <        LinkedBlockingQueue q = fullQueue(N);
480 <        for (int i = 0; i < N; ++i) {
481 <            assertEquals(i, ((Integer)q.peek()).intValue());
482 <            q.poll();
465 >                try {
466 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
467 >                    shouldThrow();
468 >                } catch (InterruptedException success) {}
469 >            }});
470 >
471 >        t.start();
472 >        Thread.sleep(SHORT_DELAY_MS);
473 >        t.interrupt();
474 >        t.join();
475 >    }
476 >
477 >    /**
478 >     * peek returns next element, or null if empty
479 >     */
480 >    public void testPeek() {
481 >        LinkedBlockingQueue q = populatedQueue(SIZE);
482 >        for (int i = 0; i < SIZE; ++i) {
483 >            assertEquals(i, q.peek());
484 >            assertEquals(i, q.poll());
485              assertTrue(q.peek() == null ||
486 <                       i != ((Integer)q.peek()).intValue());
486 >                       !q.peek().equals(i));
487          }
488 <        assertNull(q.peek());
488 >        assertNull(q.peek());
489      }
490  
491 <    public void testElement(){
492 <        LinkedBlockingQueue q = fullQueue(N);
493 <        for (int i = 0; i < N; ++i) {
494 <            assertEquals(i, ((Integer)q.element()).intValue());
495 <            q.poll();
491 >    /**
492 >     * element returns next element, or throws NSEE if empty
493 >     */
494 >    public void testElement() {
495 >        LinkedBlockingQueue q = populatedQueue(SIZE);
496 >        for (int i = 0; i < SIZE; ++i) {
497 >            assertEquals(i, q.element());
498 >            assertEquals(i, q.poll());
499          }
500          try {
501              q.element();
502 <            fail("no such element");
503 <        }
468 <        catch (NoSuchElementException success) {}
502 >            shouldThrow();
503 >        } catch (NoSuchElementException success) {}
504      }
505  
506 <    public void testRemove(){
507 <        LinkedBlockingQueue q = fullQueue(N);
508 <        for (int i = 0; i < N; ++i) {
509 <            assertEquals(i, ((Integer)q.remove()).intValue());
506 >    /**
507 >     * remove removes next element, or throws NSEE if empty
508 >     */
509 >    public void testRemove() {
510 >        LinkedBlockingQueue q = populatedQueue(SIZE);
511 >        for (int i = 0; i < SIZE; ++i) {
512 >            assertEquals(i, q.remove());
513          }
514          try {
515              q.remove();
516 <            fail("remove should throw");
517 <        } catch (NoSuchElementException success){
480 <        }  
516 >            shouldThrow();
517 >        } catch (NoSuchElementException success) {}
518      }
519  
520 <    public void testRemoveElement(){
521 <        LinkedBlockingQueue q = fullQueue(N);
522 <        for (int i = 1; i < N; i+=2) {
520 >    /**
521 >     * remove(x) removes x and returns true if present
522 >     */
523 >    public void testRemoveElement() {
524 >        LinkedBlockingQueue q = populatedQueue(SIZE);
525 >        for (int i = 1; i < SIZE; i+=2) {
526              assertTrue(q.remove(new Integer(i)));
527          }
528 <        for (int i = 0; i < N; i+=2) {
528 >        for (int i = 0; i < SIZE; i+=2) {
529              assertTrue(q.remove(new Integer(i)));
530              assertFalse(q.remove(new Integer(i+1)));
531          }
532          assertTrue(q.isEmpty());
533      }
534 <        
535 <    public void testContains(){
536 <        LinkedBlockingQueue q = fullQueue(N);
537 <        for (int i = 0; i < N; ++i) {
534 >
535 >    /**
536 >     * An add following remove(x) succeeds
537 >     */
538 >    public void testRemoveElementAndAdd() throws InterruptedException {
539 >        LinkedBlockingQueue q = new LinkedBlockingQueue();
540 >        assertTrue(q.add(new Integer(1)));
541 >        assertTrue(q.add(new Integer(2)));
542 >        assertTrue(q.remove(new Integer(1)));
543 >        assertTrue(q.remove(new Integer(2)));
544 >        assertTrue(q.add(new Integer(3)));
545 >        assertTrue(q.take() != null);
546 >    }
547 >
548 >    /**
549 >     * contains(x) reports true when elements added but not yet removed
550 >     */
551 >    public void testContains() {
552 >        LinkedBlockingQueue q = populatedQueue(SIZE);
553 >        for (int i = 0; i < SIZE; ++i) {
554              assertTrue(q.contains(new Integer(i)));
555              q.poll();
556              assertFalse(q.contains(new Integer(i)));
557          }
558      }
559  
560 <    public void testClear(){
561 <        LinkedBlockingQueue q = fullQueue(N);
560 >    /**
561 >     * clear removes all elements
562 >     */
563 >    public void testClear() {
564 >        LinkedBlockingQueue q = populatedQueue(SIZE);
565          q.clear();
566          assertTrue(q.isEmpty());
567          assertEquals(0, q.size());
568 <        assertEquals(N, q.remainingCapacity());
569 <        q.add(new Integer(1));
568 >        assertEquals(SIZE, q.remainingCapacity());
569 >        q.add(one);
570          assertFalse(q.isEmpty());
571 +        assertTrue(q.contains(one));
572          q.clear();
573          assertTrue(q.isEmpty());
574      }
575  
576 <    public void testContainsAll(){
577 <        LinkedBlockingQueue q = fullQueue(N);
578 <        LinkedBlockingQueue p = new LinkedBlockingQueue(N);
579 <        for (int i = 0; i < N; ++i) {
576 >    /**
577 >     * containsAll(c) is true when c contains a subset of elements
578 >     */
579 >    public void testContainsAll() {
580 >        LinkedBlockingQueue q = populatedQueue(SIZE);
581 >        LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
582 >        for (int i = 0; i < SIZE; ++i) {
583              assertTrue(q.containsAll(p));
584              assertFalse(p.containsAll(q));
585              p.add(new Integer(i));
# Line 524 | Line 587 | public class LinkedBlockingQueueTest ext
587          assertTrue(p.containsAll(q));
588      }
589  
590 <    public void testRetainAll(){
591 <        LinkedBlockingQueue q = fullQueue(N);
592 <        LinkedBlockingQueue p = fullQueue(N);
593 <        for (int i = 0; i < N; ++i) {
590 >    /**
591 >     * retainAll(c) retains only those elements of c and reports true if changed
592 >     */
593 >    public void testRetainAll() {
594 >        LinkedBlockingQueue q = populatedQueue(SIZE);
595 >        LinkedBlockingQueue p = populatedQueue(SIZE);
596 >        for (int i = 0; i < SIZE; ++i) {
597              boolean changed = q.retainAll(p);
598              if (i == 0)
599                  assertFalse(changed);
# Line 535 | Line 601 | public class LinkedBlockingQueueTest ext
601                  assertTrue(changed);
602  
603              assertTrue(q.containsAll(p));
604 <            assertEquals(N-i, q.size());
604 >            assertEquals(SIZE-i, q.size());
605              p.remove();
606          }
607      }
608  
609 <    public void testRemoveAll(){
610 <        for (int i = 1; i < N; ++i) {
611 <            LinkedBlockingQueue q = fullQueue(N);
612 <            LinkedBlockingQueue p = fullQueue(i);
609 >    /**
610 >     * removeAll(c) removes only those elements of c and reports true if changed
611 >     */
612 >    public void testRemoveAll() {
613 >        for (int i = 1; i < SIZE; ++i) {
614 >            LinkedBlockingQueue q = populatedQueue(SIZE);
615 >            LinkedBlockingQueue p = populatedQueue(i);
616              assertTrue(q.removeAll(p));
617 <            assertEquals(N-i, q.size());
617 >            assertEquals(SIZE-i, q.size());
618              for (int j = 0; j < i; ++j) {
619                  Integer I = (Integer)(p.remove());
620                  assertFalse(q.contains(I));
# Line 553 | Line 622 | public class LinkedBlockingQueueTest ext
622          }
623      }
624  
625 <
626 <    public void testToArray(){
627 <        LinkedBlockingQueue q = fullQueue(N);
628 <        Object[] o = q.toArray();
629 <        try {
630 <        for(int i = 0; i < o.length; i++)
631 <            assertEquals(o[i], q.take());
632 <        } catch (InterruptedException e){
564 <            fail("Unexpected exception");
565 <        }    
566 <    }
567 <
568 <    public void testToArray2(){
569 <        LinkedBlockingQueue q = fullQueue(N);
570 <        Integer[] ints = new Integer[N];
571 <        ints = (Integer[])q.toArray(ints);
572 <        try {
573 <            for(int i = 0; i < ints.length; i++)
574 <                assertEquals(ints[i], q.take());
575 <        } catch (InterruptedException e){
576 <            fail("Unexpected exception");
577 <        }    
578 <    }
579 <    
580 <    public void testIterator(){
581 <        LinkedBlockingQueue q = fullQueue(N);
582 <        Iterator it = q.iterator();
583 <        try {
584 <            while(it.hasNext()){
585 <                assertEquals(it.next(), q.take());
586 <            }
587 <        } catch (InterruptedException e){
588 <            fail("Unexpected exception");
589 <        }    
625 >    /**
626 >     * toArray contains all elements
627 >     */
628 >    public void testToArray() throws InterruptedException {
629 >        LinkedBlockingQueue q = populatedQueue(SIZE);
630 >        Object[] o = q.toArray();
631 >        for (int i = 0; i < o.length; i++)
632 >            assertEquals(o[i], q.take());
633      }
634  
635 <    public void testIteratorOrdering() {
635 >    /**
636 >     * toArray(a) contains all elements
637 >     */
638 >    public void testToArray2() throws InterruptedException {
639 >        LinkedBlockingQueue q = populatedQueue(SIZE);
640 >        Integer[] ints = new Integer[SIZE];
641 >        ints = (Integer[])q.toArray(ints);
642 >        for (int i = 0; i < ints.length; i++)
643 >            assertEquals(ints[i], q.take());
644 >    }
645  
646 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
646 >    /**
647 >     * toArray(null) throws NPE
648 >     */
649 >    public void testToArray_BadArg() {
650 >        LinkedBlockingQueue q = populatedQueue(SIZE);
651 >        try {
652 >            Object o[] = q.toArray(null);
653 >            shouldThrow();
654 >        } catch (NullPointerException success) {}
655 >    }
656  
657 <        q.add(new Integer(1));
658 <        q.add(new Integer(2));
659 <        q.add(new Integer(3));
657 >    /**
658 >     * toArray with incompatible array type throws CCE
659 >     */
660 >    public void testToArray1_BadArg() {
661 >        LinkedBlockingQueue q = populatedQueue(SIZE);
662 >        try {
663 >            Object o[] = q.toArray(new String[10]);
664 >            shouldThrow();
665 >        } catch (ArrayStoreException success) {}
666 >    }
667  
600        assertEquals("queue should be full", 0, q.remainingCapacity());
668  
669 <        int k = 0;
670 <        for (Iterator it = q.iterator(); it.hasNext();) {
671 <            int i = ((Integer)(it.next())).intValue();
672 <            assertEquals("items should come out in order", ++k, i);
669 >    /**
670 >     * iterator iterates through all elements
671 >     */
672 >    public void testIterator() throws InterruptedException {
673 >        LinkedBlockingQueue q = populatedQueue(SIZE);
674 >        Iterator it = q.iterator();
675 >        while (it.hasNext()) {
676 >            assertEquals(it.next(), q.take());
677          }
607
608        assertEquals("should go through 3 elements", 3, k);
678      }
679  
680 <    public void testWeaklyConsistentIteration () {
681 <
680 >    /**
681 >     * iterator.remove removes current element
682 >     */
683 >    public void testIteratorRemove() {
684          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
685 +        q.add(two);
686 +        q.add(one);
687 +        q.add(three);
688 +
689 +        Iterator it = q.iterator();
690 +        it.next();
691 +        it.remove();
692 +
693 +        it = q.iterator();
694 +        assertSame(it.next(), one);
695 +        assertSame(it.next(), three);
696 +        assertFalse(it.hasNext());
697 +    }
698  
615        q.add(new Integer(1));
616        q.add(new Integer(2));
617        q.add(new Integer(3));
699  
700 <        try {
701 <            for (Iterator it = q.iterator(); it.hasNext();) {
702 <                q.remove();
703 <                it.next();
704 <            }
705 <        }
706 <        catch (ConcurrentModificationException e) {
707 <            fail("weakly consistent iterator; should not get CME");
700 >    /**
701 >     * iterator ordering is FIFO
702 >     */
703 >    public void testIteratorOrdering() {
704 >        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
705 >        q.add(one);
706 >        q.add(two);
707 >        q.add(three);
708 >        assertEquals(0, q.remainingCapacity());
709 >        int k = 0;
710 >        for (Iterator it = q.iterator(); it.hasNext();) {
711 >            assertEquals(++k, it.next());
712          }
713 +        assertEquals(3, k);
714 +    }
715  
716 <        assertEquals("queue should be empty again", 0, q.size());
716 >    /**
717 >     * Modifications do not cause iterators to fail
718 >     */
719 >    public void testWeaklyConsistentIteration() {
720 >        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
721 >        q.add(one);
722 >        q.add(two);
723 >        q.add(three);
724 >        for (Iterator it = q.iterator(); it.hasNext();) {
725 >            q.remove();
726 >            it.next();
727 >        }
728 >        assertEquals(0, q.size());
729      }
730  
731  
732 <    public void testToString(){
733 <        LinkedBlockingQueue q = fullQueue(N);
732 >    /**
733 >     * toString contains toStrings of elements
734 >     */
735 >    public void testToString() {
736 >        LinkedBlockingQueue q = populatedQueue(SIZE);
737          String s = q.toString();
738 <        for (int i = 0; i < N; ++i) {
738 >        for (int i = 0; i < SIZE; ++i) {
739              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
740          }
741 <    }        
741 >    }
742  
743  
744 +    /**
745 +     * offer transfers elements across Executor tasks
746 +     */
747      public void testOfferInExecutor() {
643
748          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
749 +        q.add(one);
750 +        q.add(two);
751 +        ExecutorService executor = Executors.newFixedThreadPool(2);
752 +        executor.execute(new CheckedRunnable() {
753 +            public void realRun() throws InterruptedException {
754 +                assertFalse(q.offer(three));
755 +                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
756 +                assertEquals(0, q.remainingCapacity());
757 +            }});
758 +
759 +        executor.execute(new CheckedRunnable() {
760 +            public void realRun() throws InterruptedException {
761 +                Thread.sleep(SMALL_DELAY_MS);
762 +                assertSame(one, q.take());
763 +            }});
764  
765 <        q.add(new Integer(1));
766 <        q.add(new Integer(2));
765 >        joinPool(executor);
766 >    }
767  
768 +    /**
769 +     * poll retrieves elements across Executor threads
770 +     */
771 +    public void testPollInExecutor() {
772 +        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
773          ExecutorService executor = Executors.newFixedThreadPool(2);
774 +        executor.execute(new CheckedRunnable() {
775 +            public void realRun() throws InterruptedException {
776 +                assertNull(q.poll());
777 +                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
778 +                assertTrue(q.isEmpty());
779 +            }});
780 +
781 +        executor.execute(new CheckedRunnable() {
782 +            public void realRun() throws InterruptedException {
783 +                Thread.sleep(SMALL_DELAY_MS);
784 +                q.put(one);
785 +            }});
786  
787 <        executor.execute(new Runnable() {
788 <            public void run() {
653 <                assertFalse("offer should be rejected", q.offer(new Integer(3)));
654 <                try {
655 <                    assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
656 <                    assertEquals(0, q.remainingCapacity());
657 <                }
658 <                catch (InterruptedException e) {
659 <                    fail("should not be interrupted");
660 <                }
661 <            }
662 <        });
787 >        joinPool(executor);
788 >    }
789  
790 <        executor.execute(new Runnable() {
791 <            public void run() {
792 <                try {
793 <                    Thread.sleep(MEDIUM_DELAY_MS);
794 <                    assertEquals("first item in queue should be 1", new Integer(1), q.take());
669 <                }
670 <                catch (InterruptedException e) {
671 <                    fail("should not be interrupted");
672 <                }
673 <            }
674 <        });
675 <        
676 <        executor.shutdown();
790 >    /**
791 >     * A deserialized serialized queue has same elements in same order
792 >     */
793 >    public void testSerialization() throws Exception {
794 >        LinkedBlockingQueue q = populatedQueue(SIZE);
795  
796 +        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
797 +        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
798 +        out.writeObject(q);
799 +        out.close();
800 +
801 +        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
802 +        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
803 +        LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
804 +        assertEquals(q.size(), r.size());
805 +        while (!q.isEmpty())
806 +            assertEquals(q.remove(), r.remove());
807      }
808  
809 <    public void testPollInExecutor() {
809 >    /**
810 >     * drainTo(null) throws NPE
811 >     */
812 >    public void testDrainToNull() {
813 >        LinkedBlockingQueue q = populatedQueue(SIZE);
814 >        try {
815 >            q.drainTo(null);
816 >            shouldThrow();
817 >        } catch (NullPointerException success) {}
818 >    }
819  
820 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
820 >    /**
821 >     * drainTo(this) throws IAE
822 >     */
823 >    public void testDrainToSelf() {
824 >        LinkedBlockingQueue q = populatedQueue(SIZE);
825 >        try {
826 >            q.drainTo(q);
827 >            shouldThrow();
828 >        } catch (IllegalArgumentException success) {}
829 >    }
830  
831 <        ExecutorService executor = Executors.newFixedThreadPool(2);
831 >    /**
832 >     * drainTo(c) empties queue into another collection c
833 >     */
834 >    public void testDrainTo() {
835 >        LinkedBlockingQueue q = populatedQueue(SIZE);
836 >        ArrayList l = new ArrayList();
837 >        q.drainTo(l);
838 >        assertEquals(q.size(), 0);
839 >        assertEquals(l.size(), SIZE);
840 >        for (int i = 0; i < SIZE; ++i)
841 >            assertEquals(l.get(i), new Integer(i));
842 >        q.add(zero);
843 >        q.add(one);
844 >        assertFalse(q.isEmpty());
845 >        assertTrue(q.contains(zero));
846 >        assertTrue(q.contains(one));
847 >        l.clear();
848 >        q.drainTo(l);
849 >        assertEquals(q.size(), 0);
850 >        assertEquals(l.size(), 2);
851 >        for (int i = 0; i < 2; ++i)
852 >            assertEquals(l.get(i), new Integer(i));
853 >    }
854  
855 <        executor.execute(new Runnable() {
856 <            public void run() {
857 <                assertNull("poll should fail", q.poll());
858 <                try {
859 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
860 <                    assertTrue(q.isEmpty());
861 <                }
862 <                catch (InterruptedException e) {
863 <                    fail("should not be interrupted");
695 <                }
696 <            }
697 <        });
855 >    /**
856 >     * drainTo empties full queue, unblocking a waiting put.
857 >     */
858 >    public void testDrainToWithActivePut() throws InterruptedException {
859 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
860 >        Thread t = new Thread(new CheckedRunnable() {
861 >            public void realRun() throws InterruptedException {
862 >                q.put(new Integer(SIZE+1));
863 >            }});
864  
865 <        executor.execute(new Runnable() {
866 <            public void run() {
867 <                try {
868 <                    Thread.sleep(MEDIUM_DELAY_MS);
869 <                    q.put(new Integer(1));
870 <                }
871 <                catch (InterruptedException e) {
872 <                    fail("should not be interrupted");
873 <                }
708 <            }
709 <        });
710 <        
711 <        executor.shutdown();
865 >        t.start();
866 >        ArrayList l = new ArrayList();
867 >        q.drainTo(l);
868 >        assertTrue(l.size() >= SIZE);
869 >        for (int i = 0; i < SIZE; ++i)
870 >            assertEquals(l.get(i), new Integer(i));
871 >        t.join();
872 >        assertTrue(q.size() + l.size() >= SIZE);
873 >    }
874  
875 +    /**
876 +     * drainTo(null, n) throws NPE
877 +     */
878 +    public void testDrainToNullN() {
879 +        LinkedBlockingQueue q = populatedQueue(SIZE);
880 +        try {
881 +            q.drainTo(null, 0);
882 +            shouldThrow();
883 +        } catch (NullPointerException success) {}
884 +    }
885 +
886 +    /**
887 +     * drainTo(this, n) throws IAE
888 +     */
889 +    public void testDrainToSelfN() {
890 +        LinkedBlockingQueue q = populatedQueue(SIZE);
891 +        try {
892 +            q.drainTo(q, 0);
893 +            shouldThrow();
894 +        } catch (IllegalArgumentException success) {}
895      }
896  
897 <    public void testSerialization() {
898 <        LinkedBlockingQueue q = fullQueue(N);
899 <
900 <        try {
901 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
902 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
903 <            out.writeObject(q);
904 <            out.close();
905 <
906 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
907 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
908 <            LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
909 <            assertEquals(q.size(), r.size());
910 <            while (!q.isEmpty())
911 <                assertEquals(q.remove(), r.remove());
912 <        } catch(Exception e){
731 <            e.printStackTrace();
732 <            fail("unexpected exception");
897 >    /**
898 >     * drainTo(c, n) empties first max {n, size} elements of queue into c
899 >     */
900 >    public void testDrainToN() {
901 >        LinkedBlockingQueue q = new LinkedBlockingQueue();
902 >        for (int i = 0; i < SIZE + 2; ++i) {
903 >            for (int j = 0; j < SIZE; j++)
904 >                assertTrue(q.offer(new Integer(j)));
905 >            ArrayList l = new ArrayList();
906 >            q.drainTo(l, i);
907 >            int k = (i < SIZE)? i : SIZE;
908 >            assertEquals(l.size(), k);
909 >            assertEquals(q.size(), SIZE-k);
910 >            for (int j = 0; j < k; ++j)
911 >                assertEquals(l.get(j), new Integer(j));
912 >            while (q.poll() != null) ;
913          }
914      }
915  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines