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.6 by dl, Sun Oct 5 23:00:40 2003 UTC vs.
Revision 1.19 by jsr166, Sat Nov 21 19:11:53 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       * A new queue has the indicated capacity, or Integer.MAX_VALUE if
43       * none given
# Line 46 | Line 48 | public class LinkedBlockingQueueTest ext
48      }
49  
50      /**
51 <     * Constructor throws IAE if  capacity argument nonpositive
51 >     * Constructor throws IAE if capacity argument nonpositive
52       */
53      public void testConstructor2() {
54          try {
55              LinkedBlockingQueue q = new LinkedBlockingQueue(0);
56              shouldThrow();
57 <        }
56 <        catch (IllegalArgumentException success) {}
57 >        } catch (IllegalArgumentException success) {}
58      }
59  
60      /**
# Line 63 | Line 64 | public class LinkedBlockingQueueTest ext
64          try {
65              LinkedBlockingQueue q = new LinkedBlockingQueue(null);
66              shouldThrow();
67 <        }
67 <        catch (NullPointerException success) {}
67 >        } catch (NullPointerException success) {}
68      }
69  
70      /**
# Line 75 | Line 75 | public class LinkedBlockingQueueTest ext
75              Integer[] ints = new Integer[SIZE];
76              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
77              shouldThrow();
78 <        }
79 <        catch (NullPointerException success) {}
78 >        } catch (NullPointerException success) {}
79      }
80  
81      /**
# Line 89 | Line 88 | public class LinkedBlockingQueueTest ext
88                  ints[i] = new Integer(i);
89              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
90              shouldThrow();
91 <        }
93 <        catch (NullPointerException success) {}
91 >        } catch (NullPointerException success) {}
92      }
93  
94      /**
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)
106 <                assertEquals(ints[i], q.poll());
107 <        }
108 <        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      /**
# Line 144 | Line 139 | public class LinkedBlockingQueueTest ext
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       * add(null) throws NPE
151       */
152      public void testAddNull() {
153 <        try {
153 >        try {
154              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
155              q.add(null);
156              shouldThrow();
157 <        } catch (NullPointerException success) { }  
157 >        } catch (NullPointerException success) {}
158      }
159  
160      /**
# Line 175 | Line 170 | public class LinkedBlockingQueueTest ext
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      /**
# Line 194 | Line 189 | public class LinkedBlockingQueueTest ext
189              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
190              q.addAll(null);
191              shouldThrow();
192 <        }
198 <        catch (NullPointerException success) {}
192 >        } catch (NullPointerException success) {}
193      }
194  
195      /**
# Line 206 | Line 200 | public class LinkedBlockingQueueTest ext
200              LinkedBlockingQueue q = populatedQueue(SIZE);
201              q.addAll(q);
202              shouldThrow();
203 <        }
210 <        catch (IllegalArgumentException success) {}
203 >        } catch (IllegalArgumentException success) {}
204      }
205  
206      /**
# Line 219 | Line 212 | public class LinkedBlockingQueueTest ext
212              Integer[] ints = new Integer[SIZE];
213              q.addAll(Arrays.asList(ints));
214              shouldThrow();
215 <        }
223 <        catch (NullPointerException success) {}
215 >        } catch (NullPointerException success) {}
216      }
217      /**
218       * addAll of a collection with any null elements throws NPE after
# Line 234 | Line 226 | public class LinkedBlockingQueueTest ext
226                  ints[i] = new Integer(i);
227              q.addAll(Arrays.asList(ints));
228              shouldThrow();
229 <        }
238 <        catch (NullPointerException success) {}
229 >        } catch (NullPointerException success) {}
230      }
231      /**
232       * addAll throws ISE if not enough room
# Line 248 | Line 239 | public class LinkedBlockingQueueTest ext
239                  ints[i] = new Integer(i);
240              q.addAll(Arrays.asList(ints));
241              shouldThrow();
242 <        }
252 <        catch (IllegalStateException success) {}
242 >        } catch (IllegalStateException success) {}
243      }
244      /**
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)
267 <                assertEquals(ints[i], q.poll());
268 <        }
269 <        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       * 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 <        }
281 <        catch (NullPointerException success){
282 <        }  
283 <        catch (InterruptedException ie) {
284 <            unexpectedException();
285 <        }
267 >        } catch (NullPointerException success) {}
268       }
269  
270      /**
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);
297 <                 assertTrue(q.contains(I));
298 <             }
299 <             assertEquals(0, q.remainingCapacity());
300 <         }
301 <        catch (InterruptedException ie) {
302 <            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       * put blocks interruptibly if full
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 <                }});
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();
329 <           t.join();
330 <        }
331 <        catch (InterruptedException ie) {
332 <            unexpectedException();
333 <        }
304 >        Thread.sleep(SHORT_DELAY_MS);
305 >        t.interrupt();
306 >        t.join();
307      }
308  
309      /**
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);
356 <                    }
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();
365 <        } catch (Exception e){
366 <            unexpectedException();
367 <        }
330 >            }});
331 >
332 >        t.start();
333 >        Thread.sleep(SHORT_DELAY_MS);
334 >        q.take();
335 >        t.interrupt();
336 >        t.join();
337      }
338  
339      /**
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 <        
387 <        try {
388 <            t.start();
389 <            Thread.sleep(SMALL_DELAY_MS);
390 <            t.interrupt();
391 <            t.join();
392 <        } catch (Exception e){
393 <            unexpectedException();
394 <        }
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       * 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());
405 <            }
406 <        } catch (InterruptedException e){
407 <            unexpectedException();
408 <        }  
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       * 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 {
425 <            t.start();
426 <            Thread.sleep(SHORT_DELAY_MS);
427 <            t.interrupt();
428 <            t.join();
429 <        } catch (Exception e){
430 <            unexpectedException();
431 <        }
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       * Take removes existing elements until empty, then blocks interruptibly
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();
447 <                    } catch (InterruptedException success){
448 <                    }  
449 <                }});
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();
454 <           t.join();
455 <        }
456 <        catch (InterruptedException ie) {
457 <            unexpectedException();
458 <        }
398 >        Thread.sleep(SHORT_DELAY_MS);
399 >        t.interrupt();
400 >        t.join();
401      }
402  
403  
# Line 467 | Line 409 | public class LinkedBlockingQueueTest ext
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       * 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 <            }
482 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
483 <        } catch (InterruptedException e){
484 <            unexpectedException();
485 <        }  
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       * 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 <            }
497 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
498 <        } catch (InterruptedException e){
499 <            unexpectedException();
500 <        }  
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       * Interrupted timed poll throws InterruptedException instead of
439       * returning timeout status
440       */
441 <    public void testInterruptedTimedPoll() {
442 <        Thread t = new Thread(new Runnable() {
443 <                public void run() {
444 <                    try {
445 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
446 <                        for (int i = 0; i < SIZE; ++i) {
447 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
448 <                        }
449 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
450 <                    } catch (InterruptedException success){
451 <                    }  
452 <                }});
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();
523 <           t.join();
524 <        }
525 <        catch (InterruptedException ie) {
526 <            unexpectedException();
527 <        }
455 >        Thread.sleep(SHORT_DELAY_MS);
456 >        t.interrupt();
457 >        t.join();
458      }
459  
460      /**
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 <                }
475 <            });
476 <        try {
477 <            t.start();
478 <            Thread.sleep(SMALL_DELAY_MS);
549 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
550 <            t.interrupt();
551 <            t.join();
552 <        } catch (Exception e){
553 <            unexpectedException();
554 <        }
555 <    }  
466 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
467 >            public void realRun() throws InterruptedException {
468 >                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
469 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
470 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
471 >            }};
472 >
473 >        t.start();
474 >        Thread.sleep(SMALL_DELAY_MS);
475 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
476 >        t.interrupt();
477 >        t.join();
478 >    }
479  
480      /**
481       * peek returns next element, or null if empty
# Line 565 | Line 488 | public class LinkedBlockingQueueTest ext
488              assertTrue(q.peek() == null ||
489                         i != ((Integer)q.peek()).intValue());
490          }
491 <        assertNull(q.peek());
491 >        assertNull(q.peek());
492      }
493  
494      /**
# Line 580 | Line 503 | public class LinkedBlockingQueueTest ext
503          try {
504              q.element();
505              shouldThrow();
506 <        }
584 <        catch (NoSuchElementException success) {}
506 >        } catch (NoSuchElementException success) {}
507      }
508  
509      /**
# Line 595 | Line 517 | public class LinkedBlockingQueueTest ext
517          try {
518              q.remove();
519              shouldThrow();
520 <        } catch (NoSuchElementException success){
599 <        }  
520 >        } catch (NoSuchElementException success) {}
521      }
522  
523      /**
# Line 613 | Line 534 | public class LinkedBlockingQueueTest ext
534          }
535          assertTrue(q.isEmpty());
536      }
537 <        
537 >
538 >    /**
539 >     * An add following remove(x) succeeds
540 >     */
541 >    public void testRemoveElementAndAdd() throws InterruptedException {
542 >        LinkedBlockingQueue q = new LinkedBlockingQueue();
543 >        assertTrue(q.add(new Integer(1)));
544 >        assertTrue(q.add(new Integer(2)));
545 >        assertTrue(q.remove(new Integer(1)));
546 >        assertTrue(q.remove(new Integer(2)));
547 >        assertTrue(q.add(new Integer(3)));
548 >        assertTrue(q.take() != null);
549 >    }
550 >
551      /**
552       * contains(x) reports true when elements added but not yet removed
553       */
# Line 637 | Line 571 | public class LinkedBlockingQueueTest ext
571          assertEquals(SIZE, q.remainingCapacity());
572          q.add(one);
573          assertFalse(q.isEmpty());
574 +        assertTrue(q.contains(one));
575          q.clear();
576          assertTrue(q.isEmpty());
577      }
# Line 693 | Line 628 | public class LinkedBlockingQueueTest ext
628      /**
629       * toArray contains all elements
630       */
631 <    public void testToArray() {
631 >    public void testToArray() throws InterruptedException {
632          LinkedBlockingQueue q = populatedQueue(SIZE);
633 <        Object[] o = q.toArray();
634 <        try {
635 <        for(int i = 0; i < o.length; i++)
701 <            assertEquals(o[i], q.take());
702 <        } catch (InterruptedException e){
703 <            unexpectedException();
704 <        }    
633 >        Object[] o = q.toArray();
634 >        for (int i = 0; i < o.length; i++)
635 >            assertEquals(o[i], q.take());
636      }
637  
638      /**
639       * toArray(a) contains all elements
640       */
641 <    public void testToArray2() {
641 >    public void testToArray2() throws InterruptedException {
642          LinkedBlockingQueue q = populatedQueue(SIZE);
643 <        Integer[] ints = new Integer[SIZE];
644 <        ints = (Integer[])q.toArray(ints);
645 <        try {
646 <            for(int i = 0; i < ints.length; i++)
716 <                assertEquals(ints[i], q.take());
717 <        } catch (InterruptedException e){
718 <            unexpectedException();
719 <        }    
643 >        Integer[] ints = new Integer[SIZE];
644 >        ints = (Integer[])q.toArray(ints);
645 >        for (int i = 0; i < ints.length; i++)
646 >            assertEquals(ints[i], q.take());
647      }
648  
649      /**
650       * toArray(null) throws NPE
651       */
652      public void testToArray_BadArg() {
653 <        try {
653 >        try {
654              LinkedBlockingQueue q = populatedQueue(SIZE);
655 <            Object o[] = q.toArray(null);
656 <            shouldThrow();
657 <        } catch(NullPointerException success){}
655 >            Object o[] = q.toArray(null);
656 >            shouldThrow();
657 >        } catch (NullPointerException success) {}
658      }
659  
660      /**
661 <     * toArray with incompatable array type throws CCE
661 >     * toArray with incompatible array type throws CCE
662       */
663      public void testToArray1_BadArg() {
664 <        try {
664 >        try {
665              LinkedBlockingQueue q = populatedQueue(SIZE);
666 <            Object o[] = q.toArray(new String[10] );
667 <            shouldThrow();
668 <        } catch(ArrayStoreException  success){}
666 >            Object o[] = q.toArray(new String[10] );
667 >            shouldThrow();
668 >        } catch (ArrayStoreException success) {}
669      }
670  
671 <    
671 >
672      /**
673       * iterator iterates through all elements
674       */
675 <    public void testIterator() {
675 >    public void testIterator() throws InterruptedException {
676          LinkedBlockingQueue q = populatedQueue(SIZE);
677 <        Iterator it = q.iterator();
678 <        try {
679 <            while(it.hasNext()){
680 <                assertEquals(it.next(), q.take());
754 <            }
755 <        } catch (InterruptedException e){
756 <            unexpectedException();
757 <        }    
677 >        Iterator it = q.iterator();
678 >        while (it.hasNext()) {
679 >            assertEquals(it.next(), q.take());
680 >        }
681      }
682  
683      /**
# Line 769 | Line 692 | public class LinkedBlockingQueueTest ext
692          Iterator it = q.iterator();
693          it.next();
694          it.remove();
695 <        
695 >
696          it = q.iterator();
697          assertEquals(it.next(), one);
698          assertEquals(it.next(), three);
# Line 802 | Line 725 | public class LinkedBlockingQueueTest ext
725          q.add(one);
726          q.add(two);
727          q.add(three);
728 <        try {
729 <            for (Iterator it = q.iterator(); it.hasNext();) {
730 <                q.remove();
808 <                it.next();
809 <            }
810 <        }
811 <        catch (ConcurrentModificationException e) {
812 <            unexpectedException();
728 >        for (Iterator it = q.iterator(); it.hasNext();) {
729 >            q.remove();
730 >            it.next();
731          }
732          assertEquals(0, q.size());
733      }
# Line 824 | Line 742 | public class LinkedBlockingQueueTest ext
742          for (int i = 0; i < SIZE; ++i) {
743              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
744          }
745 <    }        
745 >    }
746  
747  
748      /**
# Line 835 | Line 753 | public class LinkedBlockingQueueTest ext
753          q.add(one);
754          q.add(two);
755          ExecutorService executor = Executors.newFixedThreadPool(2);
756 <        executor.execute(new Runnable() {
757 <            public void run() {
756 >        executor.execute(new CheckedRunnable() {
757 >            public void realRun() throws InterruptedException {
758                  threadAssertFalse(q.offer(three));
759 <                try {
760 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
761 <                    threadAssertEquals(0, q.remainingCapacity());
762 <                }
763 <                catch (InterruptedException e) {
764 <                    threadUnexpectedException();
765 <                }
766 <            }
767 <        });
759 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
760 >                threadAssertEquals(0, q.remainingCapacity());
761 >            }});
762 >
763 >        executor.execute(new CheckedRunnable() {
764 >            public void realRun() throws InterruptedException {
765 >                Thread.sleep(SMALL_DELAY_MS);
766 >                threadAssertEquals(one, q.take());
767 >            }});
768  
851        executor.execute(new Runnable() {
852            public void run() {
853                try {
854                    Thread.sleep(SMALL_DELAY_MS);
855                    threadAssertEquals(one, q.take());
856                }
857                catch (InterruptedException e) {
858                    threadUnexpectedException();
859                }
860            }
861        });
862        
769          joinPool(executor);
770      }
771  
# Line 869 | Line 775 | public class LinkedBlockingQueueTest ext
775      public void testPollInExecutor() {
776          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
777          ExecutorService executor = Executors.newFixedThreadPool(2);
778 <        executor.execute(new Runnable() {
779 <            public void run() {
778 >        executor.execute(new CheckedRunnable() {
779 >            public void realRun() throws InterruptedException {
780                  threadAssertNull(q.poll());
781 <                try {
782 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
783 <                    threadAssertTrue(q.isEmpty());
784 <                }
785 <                catch (InterruptedException e) {
786 <                    threadUnexpectedException();
787 <                }
788 <            }
789 <        });
781 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
782 >                threadAssertTrue(q.isEmpty());
783 >            }});
784 >
785 >        executor.execute(new CheckedRunnable() {
786 >            public void realRun() throws InterruptedException {
787 >                Thread.sleep(SMALL_DELAY_MS);
788 >                q.put(one);
789 >            }});
790  
885        executor.execute(new Runnable() {
886            public void run() {
887                try {
888                    Thread.sleep(SMALL_DELAY_MS);
889                    q.put(one);
890                }
891                catch (InterruptedException e) {
892                    threadUnexpectedException();
893                }
894            }
895        });
896        
791          joinPool(executor);
792      }
793  
794      /**
795       * A deserialized serialized queue has same elements in same order
796       */
797 <    public void testSerialization() {
797 >    public void testSerialization() throws Exception {
798          LinkedBlockingQueue q = populatedQueue(SIZE);
799  
800 <        try {
801 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
802 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
803 <            out.writeObject(q);
804 <            out.close();
805 <
806 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
807 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
808 <            LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
809 <            assertEquals(q.size(), r.size());
810 <            while (!q.isEmpty())
917 <                assertEquals(q.remove(), r.remove());
918 <        } catch(Exception e){
919 <            unexpectedException();
920 <        }
800 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
801 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
802 >        out.writeObject(q);
803 >        out.close();
804 >
805 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
806 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
807 >        LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
808 >        assertEquals(q.size(), r.size());
809 >        while (!q.isEmpty())
810 >            assertEquals(q.remove(), r.remove());
811      }
812  
813      /**
814       * drainTo(null) throws NPE
815 <     */
815 >     */
816      public void testDrainToNull() {
817          LinkedBlockingQueue q = populatedQueue(SIZE);
818          try {
819              q.drainTo(null);
820              shouldThrow();
821 <        } catch(NullPointerException success) {
932 <        }
821 >        } catch (NullPointerException success) {}
822      }
823  
824      /**
825       * drainTo(this) throws IAE
826 <     */
826 >     */
827      public void testDrainToSelf() {
828          LinkedBlockingQueue q = populatedQueue(SIZE);
829          try {
830              q.drainTo(q);
831              shouldThrow();
832 <        } catch(IllegalArgumentException success) {
944 <        }
832 >        } catch (IllegalArgumentException success) {}
833      }
834  
835      /**
836       * drainTo(c) empties queue into another collection c
837 <     */
837 >     */
838      public void testDrainTo() {
839          LinkedBlockingQueue q = populatedQueue(SIZE);
840          ArrayList l = new ArrayList();
841          q.drainTo(l);
842          assertEquals(q.size(), 0);
843          assertEquals(l.size(), SIZE);
844 <        for (int i = 0; i < SIZE; ++i)
844 >        for (int i = 0; i < SIZE; ++i)
845 >            assertEquals(l.get(i), new Integer(i));
846 >        q.add(zero);
847 >        q.add(one);
848 >        assertFalse(q.isEmpty());
849 >        assertTrue(q.contains(zero));
850 >        assertTrue(q.contains(one));
851 >        l.clear();
852 >        q.drainTo(l);
853 >        assertEquals(q.size(), 0);
854 >        assertEquals(l.size(), 2);
855 >        for (int i = 0; i < 2; ++i)
856              assertEquals(l.get(i), new Integer(i));
857      }
858  
859      /**
860       * drainTo empties full queue, unblocking a waiting put.
861 <     */
862 <    public void testDrainToWithActivePut() {
861 >     */
862 >    public void testDrainToWithActivePut() throws InterruptedException {
863          final LinkedBlockingQueue q = populatedQueue(SIZE);
864 <        Thread t = new Thread(new Runnable() {
865 <                public void run() {
866 <                    try {
867 <                        q.put(new Integer(SIZE+1));
868 <                    } catch (InterruptedException ie){
869 <                        threadUnexpectedException();
870 <                    }
871 <                }
872 <            });
873 <        try {
874 <            t.start();
875 <            ArrayList l = new ArrayList();
876 <            q.drainTo(l);
978 <            assertTrue(l.size() >= SIZE);
979 <            for (int i = 0; i < SIZE; ++i)
980 <                assertEquals(l.get(i), new Integer(i));
981 <            t.join();
982 <            assertTrue(q.size() + l.size() == SIZE+1);
983 <        } catch(Exception e){
984 <            unexpectedException();
985 <        }
864 >        Thread t = new Thread(new CheckedRunnable() {
865 >            public void realRun() throws InterruptedException {
866 >                q.put(new Integer(SIZE+1));
867 >            }});
868 >
869 >        t.start();
870 >        ArrayList l = new ArrayList();
871 >        q.drainTo(l);
872 >        assertTrue(l.size() >= SIZE);
873 >        for (int i = 0; i < SIZE; ++i)
874 >            assertEquals(l.get(i), new Integer(i));
875 >        t.join();
876 >        assertTrue(q.size() + l.size() >= SIZE);
877      }
878  
879      /**
880       * drainTo(null, n) throws NPE
881 <     */
881 >     */
882      public void testDrainToNullN() {
883          LinkedBlockingQueue q = populatedQueue(SIZE);
884          try {
885              q.drainTo(null, 0);
886              shouldThrow();
887 <        } catch(NullPointerException success) {
997 <        }
887 >        } catch (NullPointerException success) {}
888      }
889  
890      /**
891       * drainTo(this, n) throws IAE
892 <     */
892 >     */
893      public void testDrainToSelfN() {
894          LinkedBlockingQueue q = populatedQueue(SIZE);
895          try {
896              q.drainTo(q, 0);
897              shouldThrow();
898 <        } catch(IllegalArgumentException success) {
1009 <        }
898 >        } catch (IllegalArgumentException success) {}
899      }
900  
901      /**
902       * drainTo(c, n) empties first max {n, size} elements of queue into c
903 <     */
903 >     */
904      public void testDrainToN() {
905 +        LinkedBlockingQueue q = new LinkedBlockingQueue();
906          for (int i = 0; i < SIZE + 2; ++i) {
907 <            LinkedBlockingQueue q = populatedQueue(SIZE);
907 >            for (int j = 0; j < SIZE; j++)
908 >                assertTrue(q.offer(new Integer(j)));
909              ArrayList l = new ArrayList();
910              q.drainTo(l, i);
911              int k = (i < SIZE)? i : SIZE;
1021            assertEquals(q.size(), SIZE-k);
912              assertEquals(l.size(), k);
913 <            for (int j = 0; j < k; ++j)
913 >            assertEquals(q.size(), SIZE-k);
914 >            for (int j = 0; j < k; ++j)
915                  assertEquals(l.get(j), new Integer(j));
916 +            while (q.poll() != null) ;
917          }
918      }
919  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines