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.42 by dl, Fri May 6 16:43:45 2011 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14  
15 < public class LinkedBlockingQueueTest extends TestCase {
15 > public class LinkedBlockingQueueTest extends JSR166TestCase {
16  
17 <    private static int N = 10;
18 <    private static long SHORT_DELAY_MS = 100;
19 <    private static long MEDIUM_DELAY_MS = 1000;
20 <    private static long LONG_DELAY_MS = 10000;
17 >    public static class Unbounded extends BlockingQueueTest {
18 >        protected BlockingQueue emptyCollection() {
19 >            return new LinkedBlockingQueue();
20 >        }
21 >    }
22 >
23 >    public static class Bounded extends BlockingQueueTest {
24 >        protected BlockingQueue emptyCollection() {
25 >            return new LinkedBlockingQueue(20);
26 >        }
27 >    }
28  
29      public static void main(String[] args) {
30 <        junit.textui.TestRunner.run (suite());  
30 >        junit.textui.TestRunner.run(suite());
31      }
32  
33      public static Test suite() {
34 <        return new TestSuite(LinkedBlockingQueueTest.class);
34 >        return newTestSuite(LinkedBlockingQueueTest.class,
35 >                            new Unbounded().testSuite(),
36 >                            new Bounded().testSuite());
37      }
38  
39 +
40      /**
41       * Create a queue of given size containing consecutive
42       * Integers 0 ... n.
43       */
44 <    private LinkedBlockingQueue fullQueue(int n) {
45 <        LinkedBlockingQueue q = new LinkedBlockingQueue(n);
44 >    private LinkedBlockingQueue<Integer> populatedQueue(int n) {
45 >        LinkedBlockingQueue<Integer> q =
46 >            new LinkedBlockingQueue<Integer>(n);
47          assertTrue(q.isEmpty());
48 <        for(int i = 0; i < n; i++)
49 <            assertTrue(q.offer(new Integer(i)));
48 >        for (int i = 0; i < n; i++)
49 >            assertTrue(q.offer(new Integer(i)));
50          assertFalse(q.isEmpty());
51          assertEquals(0, q.remainingCapacity());
52 <        assertEquals(n, q.size());
52 >        assertEquals(n, q.size());
53          return q;
54      }
55 <
56 <    public void testConstructor1(){
57 <        assertEquals(N, new LinkedBlockingQueue(N).remainingCapacity());
55 >
56 >    /**
57 >     * A new queue has the indicated capacity, or Integer.MAX_VALUE if
58 >     * none given
59 >     */
60 >    public void testConstructor1() {
61 >        assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity());
62 >        assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity());
63      }
64  
65 <    public void testConstructor2(){
65 >    /**
66 >     * Constructor throws IAE if capacity argument nonpositive
67 >     */
68 >    public void testConstructor2() {
69          try {
70              LinkedBlockingQueue q = new LinkedBlockingQueue(0);
71 <            fail("Cannot make zero-sized");
72 <        }
52 <        catch (IllegalArgumentException success) {}
71 >            shouldThrow();
72 >        } catch (IllegalArgumentException success) {}
73      }
74  
75 <    public void testConstructor3(){
76 <
75 >    /**
76 >     * Initializing from null Collection throws NPE
77 >     */
78 >    public void testConstructor3() {
79          try {
80              LinkedBlockingQueue q = new LinkedBlockingQueue(null);
81 <            fail("Cannot make from null collection");
82 <        }
61 <        catch (NullPointerException success) {}
81 >            shouldThrow();
82 >        } catch (NullPointerException success) {}
83      }
84  
85 <    public void testConstructor4(){
85 >    /**
86 >     * Initializing from Collection of null elements throws NPE
87 >     */
88 >    public void testConstructor4() {
89          try {
90 <            Integer[] ints = new Integer[N];
90 >            Integer[] ints = new Integer[SIZE];
91              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
92 <            fail("Cannot make with null elements");
93 <        }
70 <        catch (NullPointerException success) {}
92 >            shouldThrow();
93 >        } catch (NullPointerException success) {}
94      }
95  
96 <    public void testConstructor5(){
96 >    /**
97 >     * Initializing from Collection with some null elements throws NPE
98 >     */
99 >    public void testConstructor5() {
100          try {
101 <            Integer[] ints = new Integer[N];
102 <            for (int i = 0; i < N-1; ++i)
101 >            Integer[] ints = new Integer[SIZE];
102 >            for (int i = 0; i < SIZE-1; ++i)
103                  ints[i] = new Integer(i);
104              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
105 <            fail("Cannot make with null elements");
106 <        }
81 <        catch (NullPointerException success) {}
105 >            shouldThrow();
106 >        } catch (NullPointerException success) {}
107      }
108  
109 <    public void testConstructor6(){
110 <        try {
111 <            Integer[] ints = new Integer[N];
112 <            for (int i = 0; i < N; ++i)
113 <                ints[i] = new Integer(i);
114 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
115 <            for (int i = 0; i < N; ++i)
116 <                assertEquals(ints[i], q.poll());
117 <        }
118 <        finally {}
109 >    /**
110 >     * Queue contains all elements of collection used to initialize
111 >     */
112 >    public void testConstructor6() {
113 >        Integer[] ints = new Integer[SIZE];
114 >        for (int i = 0; i < SIZE; ++i)
115 >            ints[i] = new Integer(i);
116 >        LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
117 >        for (int i = 0; i < SIZE; ++i)
118 >            assertEquals(ints[i], q.poll());
119      }
120  
121 +    /**
122 +     * Queue transitions from empty to full when elements added
123 +     */
124      public void testEmptyFull() {
125          LinkedBlockingQueue q = new LinkedBlockingQueue(2);
126          assertTrue(q.isEmpty());
127          assertEquals("should have room for 2", 2, q.remainingCapacity());
128 <        q.add(new Integer(1));
128 >        q.add(one);
129          assertFalse(q.isEmpty());
130 <        q.add(new Integer(2));
130 >        q.add(two);
131          assertFalse(q.isEmpty());
132 <        assertEquals("queue should be full", 0, q.remainingCapacity());
133 <        assertFalse("offer should be rejected", q.offer(new Integer(3)));
132 >        assertEquals(0, q.remainingCapacity());
133 >        assertFalse(q.offer(three));
134      }
135  
136 <    public void testRemainingCapacity(){
137 <        LinkedBlockingQueue q = fullQueue(N);
138 <        for (int i = 0; i < N; ++i) {
136 >    /**
137 >     * remainingCapacity decreases on add, increases on remove
138 >     */
139 >    public void testRemainingCapacity() {
140 >        LinkedBlockingQueue q = populatedQueue(SIZE);
141 >        for (int i = 0; i < SIZE; ++i) {
142              assertEquals(i, q.remainingCapacity());
143 <            assertEquals(N-i, q.size());
143 >            assertEquals(SIZE-i, q.size());
144              q.remove();
145          }
146 <        for (int i = 0; i < N; ++i) {
147 <            assertEquals(N-i, q.remainingCapacity());
146 >        for (int i = 0; i < SIZE; ++i) {
147 >            assertEquals(SIZE-i, q.remainingCapacity());
148              assertEquals(i, q.size());
149              q.add(new Integer(i));
150          }
151      }
152  
153 <    public void testOfferNull(){
154 <        try {
153 >    /**
154 >     * offer(null) throws NPE
155 >     */
156 >    public void testOfferNull() {
157 >        try {
158              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
159              q.offer(null);
160 <            fail("should throw NPE");
161 <        } catch (NullPointerException success) { }  
160 >            shouldThrow();
161 >        } catch (NullPointerException success) {}
162      }
163  
164 <    public void testOffer(){
164 >    /**
165 >     * add(null) throws NPE
166 >     */
167 >    public void testAddNull() {
168 >        try {
169 >            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
170 >            q.add(null);
171 >            shouldThrow();
172 >        } catch (NullPointerException success) {}
173 >    }
174 >
175 >    /**
176 >     * Offer succeeds if not full; fails if full
177 >     */
178 >    public void testOffer() {
179          LinkedBlockingQueue q = new LinkedBlockingQueue(1);
180 <        assertTrue(q.offer(new Integer(0)));
181 <        assertFalse(q.offer(new Integer(1)));
180 >        assertTrue(q.offer(zero));
181 >        assertFalse(q.offer(one));
182      }
183  
184 <    public void testAdd(){
185 <        try {
186 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
187 <            for (int i = 0; i < N; ++i) {
184 >    /**
185 >     * add succeeds if not full; throws ISE if full
186 >     */
187 >    public void testAdd() {
188 >        try {
189 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
190 >            for (int i = 0; i < SIZE; ++i) {
191                  assertTrue(q.add(new Integer(i)));
192              }
193              assertEquals(0, q.remainingCapacity());
194 <            q.add(new Integer(N));
195 <        } catch (IllegalStateException success){
196 <        }  
194 >            q.add(new Integer(SIZE));
195 >            shouldThrow();
196 >        } catch (IllegalStateException success) {}
197      }
198  
199 <    public void testAddAll1(){
199 >    /**
200 >     * addAll(null) throws NPE
201 >     */
202 >    public void testAddAll1() {
203          try {
204              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
205              q.addAll(null);
206 <            fail("Cannot add null collection");
207 <        }
154 <        catch (NullPointerException success) {}
206 >            shouldThrow();
207 >        } catch (NullPointerException success) {}
208      }
209 <    public void testAddAll2(){
209 >
210 >    /**
211 >     * addAll(this) throws IAE
212 >     */
213 >    public void testAddAllSelf() {
214 >        try {
215 >            LinkedBlockingQueue q = populatedQueue(SIZE);
216 >            q.addAll(q);
217 >            shouldThrow();
218 >        } catch (IllegalArgumentException success) {}
219 >    }
220 >
221 >    /**
222 >     * addAll of a collection with null elements throws NPE
223 >     */
224 >    public void testAddAll2() {
225          try {
226 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
227 <            Integer[] ints = new Integer[N];
226 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
227 >            Integer[] ints = new Integer[SIZE];
228              q.addAll(Arrays.asList(ints));
229 <            fail("Cannot add null elements");
230 <        }
163 <        catch (NullPointerException success) {}
229 >            shouldThrow();
230 >        } catch (NullPointerException success) {}
231      }
232 <    public void testAddAll3(){
232 >
233 >    /**
234 >     * addAll of a collection with any null elements throws NPE after
235 >     * possibly adding some elements
236 >     */
237 >    public void testAddAll3() {
238          try {
239 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
240 <            Integer[] ints = new Integer[N];
241 <            for (int i = 0; i < N-1; ++i)
239 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
240 >            Integer[] ints = new Integer[SIZE];
241 >            for (int i = 0; i < SIZE-1; ++i)
242                  ints[i] = new Integer(i);
243              q.addAll(Arrays.asList(ints));
244 <            fail("Cannot add null elements");
245 <        }
174 <        catch (NullPointerException success) {}
244 >            shouldThrow();
245 >        } catch (NullPointerException success) {}
246      }
247 <    public void testAddAll4(){
247 >
248 >    /**
249 >     * addAll throws ISE if not enough room
250 >     */
251 >    public void testAddAll4() {
252          try {
253              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
254 <            Integer[] ints = new Integer[N];
255 <            for (int i = 0; i < N; ++i)
254 >            Integer[] ints = new Integer[SIZE];
255 >            for (int i = 0; i < SIZE; ++i)
256                  ints[i] = new Integer(i);
257              q.addAll(Arrays.asList(ints));
258 <            fail("Cannot add with insufficient capacity");
259 <        }
185 <        catch (IllegalStateException success) {}
258 >            shouldThrow();
259 >        } catch (IllegalStateException success) {}
260      }
261 <    public void testAddAll5(){
262 <        try {
263 <            Integer[] empty = new Integer[0];
264 <            Integer[] ints = new Integer[N];
265 <            for (int i = 0; i < N; ++i)
266 <                ints[i] = new Integer(i);
267 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
268 <            assertFalse(q.addAll(Arrays.asList(empty)));
269 <            assertTrue(q.addAll(Arrays.asList(ints)));
270 <            for (int i = 0; i < N; ++i)
271 <                assertEquals(ints[i], q.poll());
272 <        }
273 <        finally {}
261 >
262 >    /**
263 >     * Queue contains all elements, in traversal order, of successful addAll
264 >     */
265 >    public void testAddAll5() {
266 >        Integer[] empty = new Integer[0];
267 >        Integer[] ints = new Integer[SIZE];
268 >        for (int i = 0; i < SIZE; ++i)
269 >            ints[i] = new Integer(i);
270 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
271 >        assertFalse(q.addAll(Arrays.asList(empty)));
272 >        assertTrue(q.addAll(Arrays.asList(ints)));
273 >        for (int i = 0; i < SIZE; ++i)
274 >            assertEquals(ints[i], q.poll());
275      }
276  
277 <     public void testPutNull() {
278 <        try {
279 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
277 >    /**
278 >     * put(null) throws NPE
279 >     */
280 >    public void testPutNull() throws InterruptedException {
281 >        try {
282 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
283              q.put(null);
284 <            fail("put should throw NPE");
285 <        }
286 <        catch (NullPointerException success){
287 <        }  
288 <        catch (InterruptedException ie) {
289 <            fail("Unexpected exception");
290 <        }
291 <     }
292 <
293 <     public void testPut() {
294 <         try {
295 <             LinkedBlockingQueue q = new LinkedBlockingQueue(N);
296 <             for (int i = 0; i < N; ++i) {
219 <                 Integer I = new Integer(i);
220 <                 q.put(I);
221 <                 assertTrue(q.contains(I));
222 <             }
223 <             assertEquals(0, q.remainingCapacity());
224 <         }
225 <        catch (InterruptedException ie) {
226 <            fail("Unexpected exception");
284 >            shouldThrow();
285 >        } catch (NullPointerException success) {}
286 >    }
287 >
288 >    /**
289 >     * all elements successfully put are contained
290 >     */
291 >    public void testPut() throws InterruptedException {
292 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
293 >        for (int i = 0; i < SIZE; ++i) {
294 >            Integer I = new Integer(i);
295 >            q.put(I);
296 >            assertTrue(q.contains(I));
297          }
298 +        assertEquals(0, q.remainingCapacity());
299      }
300  
301 <    public void testBlockingPut(){
302 <        Thread t = new Thread(new Runnable() {
303 <                public void run() {
304 <                    int added = 0;
305 <                    try {
306 <                        LinkedBlockingQueue q = new LinkedBlockingQueue(N);
307 <                        for (int i = 0; i < N; ++i) {
308 <                            q.put(new Integer(i));
309 <                            ++added;
310 <                        }
311 <                        q.put(new Integer(N));
312 <                        fail("put should block");
313 <                    } catch (InterruptedException ie){
314 <                        assertEquals(added, N);
315 <                    }  
316 <                }});
301 >    /**
302 >     * put blocks interruptibly if full
303 >     */
304 >    public void testBlockingPut() throws InterruptedException {
305 >        final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
306 >        Thread t = new Thread(new CheckedRunnable() {
307 >            public void realRun() throws InterruptedException {
308 >                for (int i = 0; i < SIZE; ++i)
309 >                    q.put(i);
310 >                assertEquals(SIZE, q.size());
311 >                assertEquals(0, q.remainingCapacity());
312 >                try {
313 >                    q.put(99);
314 >                    shouldThrow();
315 >                } catch (InterruptedException success) {}
316 >            }});
317 >
318          t.start();
319 <        try {
320 <           Thread.sleep(SHORT_DELAY_MS);
321 <           t.interrupt();
322 <           t.join();
323 <        }
252 <        catch (InterruptedException ie) {
253 <            fail("Unexpected exception");
254 <        }
319 >        delay(SHORT_DELAY_MS);
320 >        t.interrupt();
321 >        t.join();
322 >        assertEquals(SIZE, q.size());
323 >        assertEquals(0, q.remainingCapacity());
324      }
325  
326 <    public void testPutWithTake() {
326 >    /**
327 >     * put blocks waiting for take when full
328 >     */
329 >    public void testPutWithTake() throws InterruptedException {
330 >        final int capacity = 2;
331          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
332 <        Thread t = new Thread(new Runnable() {
333 <                public void run(){
334 <                    int added = 0;
335 <                    try {
336 <                        q.put(new Object());
337 <                        ++added;
338 <                        q.put(new Object());
339 <                        ++added;
340 <                        q.put(new Object());
341 <                        ++added;
342 <                        q.put(new Object());
343 <                        ++added;
344 <                        fail("Should block");
345 <                    } catch (InterruptedException e){
346 <                        assertTrue(added >= 2);
347 <                    }
348 <                }
349 <            });
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 <        }
332 >        Thread t = new Thread(new CheckedRunnable() {
333 >            public void realRun() throws InterruptedException {
334 >                for (int i = 0; i < capacity + 1; i++)
335 >                    q.put(i);
336 >                try {
337 >                    q.put(99);
338 >                    shouldThrow();
339 >                } catch (InterruptedException success) {}
340 >            }});
341 >
342 >        t.start();
343 >        delay(SHORT_DELAY_MS);
344 >        assertEquals(q.remainingCapacity(), 0);
345 >        assertEquals(0, q.take());
346 >        delay(SHORT_DELAY_MS);
347 >        t.interrupt();
348 >        t.join();
349 >        assertEquals(q.remainingCapacity(), 0);
350      }
351  
352 <    public void testTimedOffer() {
352 >    /**
353 >     * timed offer times out if full and elements not taken
354 >     */
355 >    public void testTimedOffer() throws InterruptedException {
356          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
357 <        Thread t = new Thread(new Runnable() {
358 <                public void run(){
359 <                    try {
360 <                        q.put(new Object());
361 <                        q.put(new Object());
362 <                        assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
363 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
364 <                        fail("Should block");
365 <                    } catch (InterruptedException success){}
366 <                }
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 <    }
357 >        Thread t = new Thread(new CheckedRunnable() {
358 >            public void realRun() throws InterruptedException {
359 >                q.put(new Object());
360 >                q.put(new Object());
361 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
362 >                try {
363 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
364 >                    shouldThrow();
365 >                } catch (InterruptedException success) {}
366 >            }});
367  
368 <    public void testTake(){
369 <        try {
370 <            LinkedBlockingQueue q = fullQueue(N);
371 <            for (int i = 0; i < N; ++i) {
316 <                assertEquals(i, ((Integer)q.take()).intValue());
317 <            }
318 <        } catch (InterruptedException e){
319 <            fail("Unexpected exception");
320 <        }  
368 >        t.start();
369 >        delay(SMALL_DELAY_MS);
370 >        t.interrupt();
371 >        t.join();
372      }
373  
374 <    public void testTakeFromEmpty() {
375 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
376 <        Thread t = new Thread(new Runnable() {
377 <                public void run(){
378 <                    try {
379 <                        q.take();
380 <                        fail("Should block");
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");
374 >    /**
375 >     * take retrieves elements in FIFO order
376 >     */
377 >    public void testTake() throws InterruptedException {
378 >        LinkedBlockingQueue q = populatedQueue(SIZE);
379 >        for (int i = 0; i < SIZE; ++i) {
380 >            assertEquals(i, q.take());
381          }
382      }
383  
384 <    public void testBlockingTake(){
385 <        Thread t = new Thread(new Runnable() {
386 <                public void run() {
387 <                    try {
388 <                        LinkedBlockingQueue q = fullQueue(N);
389 <                        for (int i = 0; i < N; ++i) {
390 <                            assertEquals(i, ((Integer)q.take()).intValue());
391 <                        }
392 <                        q.take();
393 <                        fail("take should block");
394 <                    } catch (InterruptedException success){
395 <                    }  
396 <                }});
384 >    /**
385 >     * Take removes existing elements until empty, then blocks interruptibly
386 >     */
387 >    public void testBlockingTake() throws InterruptedException {
388 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
389 >        Thread t = new Thread(new CheckedRunnable() {
390 >            public void realRun() throws InterruptedException {
391 >                for (int i = 0; i < SIZE; ++i) {
392 >                    assertEquals(i, q.take());
393 >                }
394 >                try {
395 >                    q.take();
396 >                    shouldThrow();
397 >                } catch (InterruptedException success) {}
398 >            }});
399 >
400          t.start();
401 <        try {
402 <           Thread.sleep(SHORT_DELAY_MS);
403 <           t.interrupt();
360 <           t.join();
361 <        }
362 <        catch (InterruptedException ie) {
363 <            fail("Unexpected exception");
364 <        }
401 >        delay(SHORT_DELAY_MS);
402 >        t.interrupt();
403 >        t.join();
404      }
405  
406 <
407 <    public void testPoll(){
408 <        LinkedBlockingQueue q = fullQueue(N);
409 <        for (int i = 0; i < N; ++i) {
410 <            assertEquals(i, ((Integer)q.poll()).intValue());
406 >    /**
407 >     * poll succeeds unless empty
408 >     */
409 >    public void testPoll() {
410 >        LinkedBlockingQueue q = populatedQueue(SIZE);
411 >        for (int i = 0; i < SIZE; ++i) {
412 >            assertEquals(i, q.poll());
413          }
414 <        assertNull(q.poll());
414 >        assertNull(q.poll());
415      }
416  
417 <    public void testTimedPoll0() {
418 <        try {
419 <            LinkedBlockingQueue q = fullQueue(N);
420 <            for (int i = 0; i < N; ++i) {
421 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
422 <            }
423 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
424 <        } catch (InterruptedException e){
425 <            fail("Unexpected exception");
385 <        }  
417 >    /**
418 >     * timed poll with zero timeout succeeds when non-empty, else times out
419 >     */
420 >    public void testTimedPoll0() throws InterruptedException {
421 >        LinkedBlockingQueue q = populatedQueue(SIZE);
422 >        for (int i = 0; i < SIZE; ++i) {
423 >            assertEquals(i, q.poll(0, MILLISECONDS));
424 >        }
425 >        assertNull(q.poll(0, MILLISECONDS));
426      }
427  
428 <    public void testTimedPoll() {
429 <        try {
430 <            LinkedBlockingQueue q = fullQueue(N);
431 <            for (int i = 0; i < N; ++i) {
432 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
433 <            }
434 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
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();
417 <        }
418 <        catch (InterruptedException ie) {
419 <            fail("Unexpected exception");
428 >    /**
429 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
430 >     */
431 >    public void testTimedPoll() throws InterruptedException {
432 >        LinkedBlockingQueue q = populatedQueue(SIZE);
433 >        for (int i = 0; i < SIZE; ++i) {
434 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
435          }
436 +        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
437      }
438  
439 <    public void testTimedPollWithOffer(){
440 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
441 <        Thread t = new Thread(new Runnable() {
442 <                public void run(){
443 <                    try {
444 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
445 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
446 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
447 <                        fail("Should block");
448 <                    } catch (InterruptedException success) { }                
439 >    /**
440 >     * Interrupted timed poll throws InterruptedException instead of
441 >     * returning timeout status
442 >     */
443 >    public void testInterruptedTimedPoll() throws InterruptedException {
444 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
445 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
446 >        Thread t = newStartedThread(new CheckedRunnable() {
447 >            public void realRun() throws InterruptedException {
448 >                for (int i = 0; i < SIZE; ++i) {
449 >                    long t0 = System.nanoTime();
450 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
451 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
452                  }
453 <            });
454 <        try {
455 <            t.start();
456 <            Thread.sleep(SHORT_DELAY_MS * 2);
457 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
458 <            t.interrupt();
459 <            t.join();
460 <        } catch (Exception e){
461 <            fail("Unexpected exception");
462 <        }
463 <    }  
464 <
465 <
466 <    public void testPeek(){
467 <        LinkedBlockingQueue q = fullQueue(N);
468 <        for (int i = 0; i < N; ++i) {
469 <            assertEquals(i, ((Integer)q.peek()).intValue());
470 <            q.poll();
453 >                long t0 = System.nanoTime();
454 >                aboutToWait.countDown();
455 >                try {
456 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
457 >                    shouldThrow();
458 >                } catch (InterruptedException success) {
459 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
460 >                }
461 >            }});
462 >
463 >        aboutToWait.await();
464 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
465 >        t.interrupt();
466 >        awaitTermination(t, MEDIUM_DELAY_MS);
467 >        checkEmpty(q);
468 >    }
469 >
470 >    /**
471 >     * peek returns next element, or null if empty
472 >     */
473 >    public void testPeek() {
474 >        LinkedBlockingQueue q = populatedQueue(SIZE);
475 >        for (int i = 0; i < SIZE; ++i) {
476 >            assertEquals(i, q.peek());
477 >            assertEquals(i, q.poll());
478              assertTrue(q.peek() == null ||
479 <                       i != ((Integer)q.peek()).intValue());
479 >                       !q.peek().equals(i));
480          }
481 <        assertNull(q.peek());
481 >        assertNull(q.peek());
482      }
483  
484 <    public void testElement(){
485 <        LinkedBlockingQueue q = fullQueue(N);
486 <        for (int i = 0; i < N; ++i) {
487 <            assertEquals(i, ((Integer)q.element()).intValue());
488 <            q.poll();
484 >    /**
485 >     * element returns next element, or throws NSEE if empty
486 >     */
487 >    public void testElement() {
488 >        LinkedBlockingQueue q = populatedQueue(SIZE);
489 >        for (int i = 0; i < SIZE; ++i) {
490 >            assertEquals(i, q.element());
491 >            assertEquals(i, q.poll());
492          }
493          try {
494              q.element();
495 <            fail("no such element");
496 <        }
468 <        catch (NoSuchElementException success) {}
495 >            shouldThrow();
496 >        } catch (NoSuchElementException success) {}
497      }
498  
499 <    public void testRemove(){
500 <        LinkedBlockingQueue q = fullQueue(N);
501 <        for (int i = 0; i < N; ++i) {
502 <            assertEquals(i, ((Integer)q.remove()).intValue());
499 >    /**
500 >     * remove removes next element, or throws NSEE if empty
501 >     */
502 >    public void testRemove() {
503 >        LinkedBlockingQueue q = populatedQueue(SIZE);
504 >        for (int i = 0; i < SIZE; ++i) {
505 >            assertEquals(i, q.remove());
506          }
507          try {
508              q.remove();
509 <            fail("remove should throw");
510 <        } catch (NoSuchElementException success){
480 <        }  
509 >            shouldThrow();
510 >        } catch (NoSuchElementException success) {}
511      }
512  
513 <    public void testRemoveElement(){
514 <        LinkedBlockingQueue q = fullQueue(N);
515 <        for (int i = 1; i < N; i+=2) {
516 <            assertTrue(q.remove(new Integer(i)));
517 <        }
518 <        for (int i = 0; i < N; i+=2) {
519 <            assertTrue(q.remove(new Integer(i)));
520 <            assertFalse(q.remove(new Integer(i+1)));
513 >    /**
514 >     * remove(x) removes x and returns true if present
515 >     */
516 >    public void testRemoveElement() {
517 >        LinkedBlockingQueue q = populatedQueue(SIZE);
518 >        for (int i = 1; i < SIZE; i+=2) {
519 >            assertTrue(q.contains(i));
520 >            assertTrue(q.remove(i));
521 >            assertFalse(q.contains(i));
522 >            assertTrue(q.contains(i-1));
523 >        }
524 >        for (int i = 0; i < SIZE; i+=2) {
525 >            assertTrue(q.contains(i));
526 >            assertTrue(q.remove(i));
527 >            assertFalse(q.contains(i));
528 >            assertFalse(q.remove(i+1));
529 >            assertFalse(q.contains(i+1));
530          }
531          assertTrue(q.isEmpty());
532      }
533 <        
534 <    public void testContains(){
535 <        LinkedBlockingQueue q = fullQueue(N);
536 <        for (int i = 0; i < N; ++i) {
533 >
534 >    /**
535 >     * An add following remove(x) succeeds
536 >     */
537 >    public void testRemoveElementAndAdd() throws InterruptedException {
538 >        LinkedBlockingQueue q = new LinkedBlockingQueue();
539 >        assertTrue(q.add(new Integer(1)));
540 >        assertTrue(q.add(new Integer(2)));
541 >        assertTrue(q.remove(new Integer(1)));
542 >        assertTrue(q.remove(new Integer(2)));
543 >        assertTrue(q.add(new Integer(3)));
544 >        assertTrue(q.take() != null);
545 >    }
546 >
547 >    /**
548 >     * contains(x) reports true when elements added but not yet removed
549 >     */
550 >    public void testContains() {
551 >        LinkedBlockingQueue q = populatedQueue(SIZE);
552 >        for (int i = 0; i < SIZE; ++i) {
553              assertTrue(q.contains(new Integer(i)));
554              q.poll();
555              assertFalse(q.contains(new Integer(i)));
556          }
557      }
558  
559 <    public void testClear(){
560 <        LinkedBlockingQueue q = fullQueue(N);
559 >    /**
560 >     * clear removes all elements
561 >     */
562 >    public void testClear() {
563 >        LinkedBlockingQueue q = populatedQueue(SIZE);
564          q.clear();
565          assertTrue(q.isEmpty());
566          assertEquals(0, q.size());
567 <        assertEquals(N, q.remainingCapacity());
568 <        q.add(new Integer(1));
567 >        assertEquals(SIZE, q.remainingCapacity());
568 >        q.add(one);
569          assertFalse(q.isEmpty());
570 +        assertTrue(q.contains(one));
571          q.clear();
572          assertTrue(q.isEmpty());
573      }
574  
575 <    public void testContainsAll(){
576 <        LinkedBlockingQueue q = fullQueue(N);
577 <        LinkedBlockingQueue p = new LinkedBlockingQueue(N);
578 <        for (int i = 0; i < N; ++i) {
575 >    /**
576 >     * containsAll(c) is true when c contains a subset of elements
577 >     */
578 >    public void testContainsAll() {
579 >        LinkedBlockingQueue q = populatedQueue(SIZE);
580 >        LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
581 >        for (int i = 0; i < SIZE; ++i) {
582              assertTrue(q.containsAll(p));
583              assertFalse(p.containsAll(q));
584              p.add(new Integer(i));
# Line 524 | Line 586 | public class LinkedBlockingQueueTest ext
586          assertTrue(p.containsAll(q));
587      }
588  
589 <    public void testRetainAll(){
590 <        LinkedBlockingQueue q = fullQueue(N);
591 <        LinkedBlockingQueue p = fullQueue(N);
592 <        for (int i = 0; i < N; ++i) {
589 >    /**
590 >     * retainAll(c) retains only those elements of c and reports true if changed
591 >     */
592 >    public void testRetainAll() {
593 >        LinkedBlockingQueue q = populatedQueue(SIZE);
594 >        LinkedBlockingQueue p = populatedQueue(SIZE);
595 >        for (int i = 0; i < SIZE; ++i) {
596              boolean changed = q.retainAll(p);
597              if (i == 0)
598                  assertFalse(changed);
# Line 535 | Line 600 | public class LinkedBlockingQueueTest ext
600                  assertTrue(changed);
601  
602              assertTrue(q.containsAll(p));
603 <            assertEquals(N-i, q.size());
603 >            assertEquals(SIZE-i, q.size());
604              p.remove();
605          }
606      }
607  
608 <    public void testRemoveAll(){
609 <        for (int i = 1; i < N; ++i) {
610 <            LinkedBlockingQueue q = fullQueue(N);
611 <            LinkedBlockingQueue p = fullQueue(i);
608 >    /**
609 >     * removeAll(c) removes only those elements of c and reports true if changed
610 >     */
611 >    public void testRemoveAll() {
612 >        for (int i = 1; i < SIZE; ++i) {
613 >            LinkedBlockingQueue q = populatedQueue(SIZE);
614 >            LinkedBlockingQueue p = populatedQueue(i);
615              assertTrue(q.removeAll(p));
616 <            assertEquals(N-i, q.size());
616 >            assertEquals(SIZE-i, q.size());
617              for (int j = 0; j < i; ++j) {
618                  Integer I = (Integer)(p.remove());
619                  assertFalse(q.contains(I));
# Line 553 | Line 621 | public class LinkedBlockingQueueTest ext
621          }
622      }
623  
624 <
625 <    public void testToArray(){
626 <        LinkedBlockingQueue q = fullQueue(N);
627 <        Object[] o = q.toArray();
628 <        try {
629 <        for(int i = 0; i < o.length; i++)
630 <            assertEquals(o[i], q.take());
631 <        } 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 <        }    
624 >    /**
625 >     * toArray contains all elements in FIFO order
626 >     */
627 >    public void testToArray() {
628 >        LinkedBlockingQueue q = populatedQueue(SIZE);
629 >        Object[] o = q.toArray();
630 >        for (int i = 0; i < o.length; i++)
631 >            assertSame(o[i], q.poll());
632      }
633  
634 <    public void testIteratorOrdering() {
634 >    /**
635 >     * toArray(a) contains all elements in FIFO order
636 >     */
637 >    public void testToArray2() throws InterruptedException {
638 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
639 >        Integer[] ints = new Integer[SIZE];
640 >        Integer[] array = q.toArray(ints);
641 >        assertSame(ints, array);
642 >        for (int i = 0; i < ints.length; i++)
643 >            assertSame(ints[i], q.poll());
644 >    }
645  
646 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
646 >    /**
647 >     * toArray(null) throws NullPointerException
648 >     */
649 >    public void testToArray_NullArg() {
650 >        LinkedBlockingQueue q = populatedQueue(SIZE);
651 >        try {
652 >            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(incompatible array type) throws ArrayStoreException
659 >     */
660 >    public void testToArray1_BadArg() {
661 >        LinkedBlockingQueue q = populatedQueue(SIZE);
662 >        try {
663 >            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 +                delay(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 +                delay(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 <                }
874 <            }
875 <        });
876 <        
877 <        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 min(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