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.37 by jsr166, Fri Nov 5 00:17:22 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines