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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines