ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
(Generate patch)

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines