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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines