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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines