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.16 by jsr166, Sat Nov 21 02:33:20 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 {
# Line 55 | Line 59 | public class LinkedBlockingQueueTest ext
59      }
60  
61      /**
62 <     *
62 >     * Initializing from null Collection throws NPE
63       */
64      public void testConstructor3() {
61
65          try {
66              LinkedBlockingQueue q = new LinkedBlockingQueue(null);
67              shouldThrow();
# Line 67 | Line 70 | public class LinkedBlockingQueueTest ext
70      }
71  
72      /**
73 <     *
73 >     * Initializing from Collection of null elements throws NPE
74       */
75      public void testConstructor4() {
76          try {
# Line 79 | Line 82 | public class LinkedBlockingQueueTest ext
82      }
83  
84      /**
85 <     *
85 >     * Initializing from Collection with some null elements throws NPE
86       */
87      public void testConstructor5() {
88          try {
# Line 93 | Line 96 | public class LinkedBlockingQueueTest ext
96      }
97  
98      /**
99 <     *
99 >     * Queue contains all elements of collection used to initialize
100       */
101      public void testConstructor6() {
102          try {
# Line 108 | Line 111 | public class LinkedBlockingQueueTest ext
111      }
112  
113      /**
114 <     *
114 >     * Queue transitions from empty to full when elements added
115       */
116      public void testEmptyFull() {
117          LinkedBlockingQueue q = new LinkedBlockingQueue(2);
# Line 123 | Line 126 | public class LinkedBlockingQueueTest ext
126      }
127  
128      /**
129 <     *
129 >     * remainingCapacity decreases on add, increases on remove
130       */
131      public void testRemainingCapacity() {
132          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 140 | Line 143 | public class LinkedBlockingQueueTest ext
143      }
144  
145      /**
146 <     *
146 >     * offer(null) throws NPE
147       */
148      public void testOfferNull() {
149 <        try {
149 >        try {
150              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
151              q.offer(null);
152              shouldThrow();
153 <        } catch (NullPointerException success) { }  
153 >        } catch (NullPointerException success) { }
154 >    }
155 >
156 >    /**
157 >     * add(null) throws NPE
158 >     */
159 >    public void testAddNull() {
160 >        try {
161 >            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
162 >            q.add(null);
163 >            shouldThrow();
164 >        } catch (NullPointerException success) { }
165      }
166  
167      /**
168 <     *
168 >     * Offer succeeds if not full; fails if full
169       */
170      public void testOffer() {
171          LinkedBlockingQueue q = new LinkedBlockingQueue(1);
# Line 160 | Line 174 | public class LinkedBlockingQueueTest ext
174      }
175  
176      /**
177 <     *
177 >     * add succeeds if not full; throws ISE if full
178       */
179      public void testAdd() {
180 <        try {
180 >        try {
181              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
182              for (int i = 0; i < SIZE; ++i) {
183                  assertTrue(q.add(new Integer(i)));
184              }
185              assertEquals(0, q.remainingCapacity());
186              q.add(new Integer(SIZE));
187 <        } catch (IllegalStateException success){
188 <        }  
187 >        } catch (IllegalStateException success) {
188 >        }
189      }
190  
191      /**
192 <     *
192 >     * addAll(null) throws NPE
193       */
194      public void testAddAll1() {
195          try {
# Line 185 | Line 199 | public class LinkedBlockingQueueTest ext
199          }
200          catch (NullPointerException success) {}
201      }
202 +
203      /**
204 <     *
204 >     * addAll(this) throws IAE
205 >     */
206 >    public void testAddAllSelf() {
207 >        try {
208 >            LinkedBlockingQueue q = populatedQueue(SIZE);
209 >            q.addAll(q);
210 >            shouldThrow();
211 >        }
212 >        catch (IllegalArgumentException success) {}
213 >    }
214 >
215 >    /**
216 >     * addAll of a collection with null elements throws NPE
217       */
218      public void testAddAll2() {
219          try {
# Line 198 | Line 225 | public class LinkedBlockingQueueTest ext
225          catch (NullPointerException success) {}
226      }
227      /**
228 <     *
228 >     * addAll of a collection with any null elements throws NPE after
229 >     * possibly adding some elements
230       */
231      public void testAddAll3() {
232          try {
# Line 212 | Line 240 | public class LinkedBlockingQueueTest ext
240          catch (NullPointerException success) {}
241      }
242      /**
243 <     *
243 >     * addAll throws ISE if not enough room
244       */
245      public void testAddAll4() {
246          try {
# Line 226 | Line 254 | public class LinkedBlockingQueueTest ext
254          catch (IllegalStateException success) {}
255      }
256      /**
257 <     *
257 >     * Queue contains all elements, in traversal order, of successful addAll
258       */
259      public void testAddAll5() {
260          try {
# Line 244 | Line 272 | public class LinkedBlockingQueueTest ext
272      }
273  
274      /**
275 <     *
275 >     * put(null) throws NPE
276       */
277       public void testPutNull() {
278 <        try {
278 >        try {
279              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
280              q.put(null);
281              shouldThrow();
282 <        }
283 <        catch (NullPointerException success){
284 <        }  
282 >        }
283 >        catch (NullPointerException success) {
284 >        }
285          catch (InterruptedException ie) {
286 <            unexpectedException();
286 >            unexpectedException();
287          }
288       }
289  
290      /**
291 <     *
291 >     * all elements successfully put are contained
292       */
293       public void testPut() {
294           try {
# Line 273 | Line 301 | public class LinkedBlockingQueueTest ext
301               assertEquals(0, q.remainingCapacity());
302           }
303          catch (InterruptedException ie) {
304 <            unexpectedException();
304 >            unexpectedException();
305          }
306      }
307  
308      /**
309 <     *
309 >     * put blocks interruptibly if full
310       */
311      public void testBlockingPut() {
312          Thread t = new Thread(new Runnable() {
# Line 292 | Line 320 | public class LinkedBlockingQueueTest ext
320                          }
321                          q.put(new Integer(SIZE));
322                          threadShouldThrow();
323 <                    } catch (InterruptedException ie){
323 >                    } catch (InterruptedException ie) {
324                          threadAssertEquals(added, SIZE);
325 <                    }  
325 >                    }
326                  }});
327          t.start();
328 <        try {
329 <           Thread.sleep(SHORT_DELAY_MS);
328 >        try {
329 >           Thread.sleep(SHORT_DELAY_MS);
330             t.interrupt();
331             t.join();
332          }
333          catch (InterruptedException ie) {
334 <            unexpectedException();
334 >            unexpectedException();
335          }
336      }
337  
338      /**
339 <     *
339 >     * put blocks waiting for take when full
340       */
341      public void testPutWithTake() {
342          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
# Line 324 | Line 352 | public class LinkedBlockingQueueTest ext
352                          ++added;
353                          q.put(new Object());
354                          ++added;
355 <                        threadShouldThrow();
356 <                    } catch (InterruptedException e){
355 >                        threadShouldThrow();
356 >                    } catch (InterruptedException e) {
357                          threadAssertTrue(added >= 2);
358                      }
359                  }
# Line 336 | Line 364 | public class LinkedBlockingQueueTest ext
364              q.take();
365              t.interrupt();
366              t.join();
367 <        } catch (Exception e){
367 >        } catch (Exception e) {
368              unexpectedException();
369          }
370      }
371  
372      /**
373 <     *
373 >     * timed offer times out if full and elements not taken
374       */
375      public void testTimedOffer() {
376          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
# Line 351 | Line 379 | public class LinkedBlockingQueueTest ext
379                      try {
380                          q.put(new Object());
381                          q.put(new Object());
382 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
383 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
384 <                        threadShouldThrow();
385 <                    } catch (InterruptedException success){}
382 >                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
383 >                        q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
384 >                        threadShouldThrow();
385 >                    } catch (InterruptedException success) {}
386                  }
387              });
388 <        
388 >
389          try {
390              t.start();
391              Thread.sleep(SMALL_DELAY_MS);
392              t.interrupt();
393              t.join();
394 <        } catch (Exception e){
394 >        } catch (Exception e) {
395              unexpectedException();
396          }
397      }
398  
399      /**
400 <     *
400 >     * take retrieves elements in FIFO order
401       */
402      public void testTake() {
403 <        try {
403 >        try {
404              LinkedBlockingQueue q = populatedQueue(SIZE);
405              for (int i = 0; i < SIZE; ++i) {
406                  assertEquals(i, ((Integer)q.take()).intValue());
407              }
408 <        } catch (InterruptedException e){
409 <            unexpectedException();
410 <        }  
408 >        } catch (InterruptedException e) {
409 >            unexpectedException();
410 >        }
411      }
412  
413      /**
414 <     *
414 >     * take blocks interruptibly when empty
415       */
416      public void testTakeFromEmpty() {
417          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
# Line 391 | Line 419 | public class LinkedBlockingQueueTest ext
419                  public void run() {
420                      try {
421                          q.take();
422 <                        threadShouldThrow();
423 <                    } catch (InterruptedException success){ }                
422 >                        threadShouldThrow();
423 >                    } catch (InterruptedException success) { }
424                  }
425              });
426          try {
# Line 400 | Line 428 | public class LinkedBlockingQueueTest ext
428              Thread.sleep(SHORT_DELAY_MS);
429              t.interrupt();
430              t.join();
431 <        } catch (Exception e){
431 >        } catch (Exception e) {
432              unexpectedException();
433          }
434      }
435  
436      /**
437 <     *
437 >     * Take removes existing elements until empty, then blocks interruptibly
438       */
439      public void testBlockingTake() {
440          Thread t = new Thread(new Runnable() {
# Line 418 | Line 446 | public class LinkedBlockingQueueTest ext
446                          }
447                          q.take();
448                          threadShouldThrow();
449 <                    } catch (InterruptedException success){
450 <                    }  
449 >                    } catch (InterruptedException success) {
450 >                    }
451                  }});
452          t.start();
453 <        try {
454 <           Thread.sleep(SHORT_DELAY_MS);
453 >        try {
454 >           Thread.sleep(SHORT_DELAY_MS);
455             t.interrupt();
456             t.join();
457          }
458          catch (InterruptedException ie) {
459 <            unexpectedException();
459 >            unexpectedException();
460          }
461      }
462  
463  
464      /**
465 <     *
465 >     * poll succeeds unless empty
466       */
467      public void testPoll() {
468          LinkedBlockingQueue q = populatedQueue(SIZE);
469          for (int i = 0; i < SIZE; ++i) {
470              assertEquals(i, ((Integer)q.poll()).intValue());
471          }
472 <        assertNull(q.poll());
472 >        assertNull(q.poll());
473      }
474  
475      /**
476 <     *
476 >     * timed pool with zero timeout succeeds when non-empty, else times out
477       */
478      public void testTimedPoll0() {
479          try {
480              LinkedBlockingQueue q = populatedQueue(SIZE);
481              for (int i = 0; i < SIZE; ++i) {
482 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
482 >                assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
483              }
484 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
485 <        } catch (InterruptedException e){
486 <            unexpectedException();
487 <        }  
484 >            assertNull(q.poll(0, MILLISECONDS));
485 >        } catch (InterruptedException e) {
486 >            unexpectedException();
487 >        }
488      }
489  
490      /**
491 <     *
491 >     * timed pool with nonzero timeout succeeds when non-empty, else times out
492       */
493      public void testTimedPoll() {
494          try {
495              LinkedBlockingQueue q = populatedQueue(SIZE);
496              for (int i = 0; i < SIZE; ++i) {
497 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
497 >                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
498              }
499 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
500 <        } catch (InterruptedException e){
501 <            unexpectedException();
502 <        }  
499 >            assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
500 >        } catch (InterruptedException e) {
501 >            unexpectedException();
502 >        }
503      }
504  
505      /**
506 <     *
506 >     * Interrupted timed poll throws InterruptedException instead of
507 >     * returning timeout status
508       */
509      public void testInterruptedTimedPoll() {
510          Thread t = new Thread(new Runnable() {
# Line 483 | Line 512 | public class LinkedBlockingQueueTest ext
512                      try {
513                          LinkedBlockingQueue q = populatedQueue(SIZE);
514                          for (int i = 0; i < SIZE; ++i) {
515 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
515 >                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
516                          }
517 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
518 <                    } catch (InterruptedException success){
519 <                    }  
517 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
518 >                    } catch (InterruptedException success) {
519 >                    }
520                  }});
521          t.start();
522 <        try {
523 <           Thread.sleep(SHORT_DELAY_MS);
522 >        try {
523 >           Thread.sleep(SHORT_DELAY_MS);
524             t.interrupt();
525             t.join();
526          }
527          catch (InterruptedException ie) {
528 <            unexpectedException();
528 >            unexpectedException();
529          }
530      }
531  
532      /**
533 <     *
533 >     *  timed poll before a delayed offer fails; after offer succeeds;
534 >     *  on interruption throws
535       */
536      public void testTimedPollWithOffer() {
537          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
538          Thread t = new Thread(new Runnable() {
539                  public void run() {
540                      try {
541 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
542 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
543 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
544 <                        threadShouldThrow();
545 <                    } catch (InterruptedException success) { }                
541 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
542 >                        q.poll(LONG_DELAY_MS, MILLISECONDS);
543 >                        q.poll(LONG_DELAY_MS, MILLISECONDS);
544 >                        threadShouldThrow();
545 >                    } catch (InterruptedException success) { }
546                  }
547              });
548          try {
549              t.start();
550              Thread.sleep(SMALL_DELAY_MS);
551 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
551 >            assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
552              t.interrupt();
553              t.join();
554 <        } catch (Exception e){
554 >        } catch (Exception e) {
555              unexpectedException();
556          }
557 <    }  
528 <
557 >    }
558  
559      /**
560 <     *
560 >     * peek returns next element, or null if empty
561       */
562      public void testPeek() {
563          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 538 | Line 567 | public class LinkedBlockingQueueTest ext
567              assertTrue(q.peek() == null ||
568                         i != ((Integer)q.peek()).intValue());
569          }
570 <        assertNull(q.peek());
570 >        assertNull(q.peek());
571      }
572  
573      /**
574 <     *
574 >     * element returns next element, or throws NSEE if empty
575       */
576      public void testElement() {
577          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 558 | Line 587 | public class LinkedBlockingQueueTest ext
587      }
588  
589      /**
590 <     *
590 >     * remove removes next element, or throws NSEE if empty
591       */
592      public void testRemove() {
593          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 568 | Line 597 | public class LinkedBlockingQueueTest ext
597          try {
598              q.remove();
599              shouldThrow();
600 <        } catch (NoSuchElementException success){
601 <        }  
600 >        } catch (NoSuchElementException success) {
601 >        }
602      }
603  
604      /**
605 <     *
605 >     * remove(x) removes x and returns true if present
606       */
607      public void testRemoveElement() {
608          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 586 | Line 615 | public class LinkedBlockingQueueTest ext
615          }
616          assertTrue(q.isEmpty());
617      }
618 <        
618 >
619 >    /**
620 >     * An add following remove(x) succeeds
621 >     */
622 >    public void testRemoveElementAndAdd() {
623 >        try {
624 >            LinkedBlockingQueue q = new LinkedBlockingQueue();
625 >            assertTrue(q.add(new Integer(1)));
626 >            assertTrue(q.add(new Integer(2)));
627 >            assertTrue(q.remove(new Integer(1)));
628 >            assertTrue(q.remove(new Integer(2)));
629 >            assertTrue(q.add(new Integer(3)));
630 >            assertTrue(q.take() != null);
631 >        } catch (Exception e) {
632 >            unexpectedException();
633 >        }
634 >    }
635 >
636      /**
637 <     *
637 >     * contains(x) reports true when elements added but not yet removed
638       */
639      public void testContains() {
640          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 600 | Line 646 | public class LinkedBlockingQueueTest ext
646      }
647  
648      /**
649 <     *
649 >     * clear removes all elements
650       */
651      public void testClear() {
652          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 610 | Line 656 | public class LinkedBlockingQueueTest ext
656          assertEquals(SIZE, q.remainingCapacity());
657          q.add(one);
658          assertFalse(q.isEmpty());
659 +        assertTrue(q.contains(one));
660          q.clear();
661          assertTrue(q.isEmpty());
662      }
663  
664      /**
665 <     *
665 >     * containsAll(c) is true when c contains a subset of elements
666       */
667      public void testContainsAll() {
668          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 629 | Line 676 | public class LinkedBlockingQueueTest ext
676      }
677  
678      /**
679 <     *
679 >     * retainAll(c) retains only those elements of c and reports true if changed
680       */
681      public void testRetainAll() {
682          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 648 | Line 695 | public class LinkedBlockingQueueTest ext
695      }
696  
697      /**
698 <     *
698 >     * removeAll(c) removes only those elements of c and reports true if changed
699       */
700      public void testRemoveAll() {
701          for (int i = 1; i < SIZE; ++i) {
# Line 663 | Line 710 | public class LinkedBlockingQueueTest ext
710          }
711      }
712  
666
713      /**
714 <     *
714 >     * toArray contains all elements
715       */
716      public void testToArray() {
717          LinkedBlockingQueue q = populatedQueue(SIZE);
718 <        Object[] o = q.toArray();
719 <        try {
720 <        for(int i = 0; i < o.length; i++)
721 <            assertEquals(o[i], q.take());
722 <        } catch (InterruptedException e){
723 <            unexpectedException();
724 <        }    
718 >        Object[] o = q.toArray();
719 >        try {
720 >        for (int i = 0; i < o.length; i++)
721 >            assertEquals(o[i], q.take());
722 >        } catch (InterruptedException e) {
723 >            unexpectedException();
724 >        }
725      }
726  
727      /**
728 <     *
728 >     * toArray(a) contains all elements
729       */
730      public void testToArray2() {
731          LinkedBlockingQueue q = populatedQueue(SIZE);
732 <        Integer[] ints = new Integer[SIZE];
733 <        ints = (Integer[])q.toArray(ints);
734 <        try {
735 <            for(int i = 0; i < ints.length; i++)
736 <                assertEquals(ints[i], q.take());
737 <        } catch (InterruptedException e){
738 <            unexpectedException();
739 <        }    
732 >        Integer[] ints = new Integer[SIZE];
733 >        ints = (Integer[])q.toArray(ints);
734 >        try {
735 >            for (int i = 0; i < ints.length; i++)
736 >                assertEquals(ints[i], q.take());
737 >        } catch (InterruptedException e) {
738 >            unexpectedException();
739 >        }
740 >    }
741 >
742 >    /**
743 >     * toArray(null) throws NPE
744 >     */
745 >    public void testToArray_BadArg() {
746 >        try {
747 >            LinkedBlockingQueue q = populatedQueue(SIZE);
748 >            Object o[] = q.toArray(null);
749 >            shouldThrow();
750 >        } catch (NullPointerException success) {}
751 >    }
752 >
753 >    /**
754 >     * toArray with incompatible array type throws CCE
755 >     */
756 >    public void testToArray1_BadArg() {
757 >        try {
758 >            LinkedBlockingQueue q = populatedQueue(SIZE);
759 >            Object o[] = q.toArray(new String[10] );
760 >            shouldThrow();
761 >        } catch (ArrayStoreException  success) {}
762      }
763 <    
763 >
764 >
765      /**
766 <     *
766 >     * iterator iterates through all elements
767       */
768      public void testIterator() {
769          LinkedBlockingQueue q = populatedQueue(SIZE);
770 <        Iterator it = q.iterator();
771 <        try {
772 <            while(it.hasNext()){
773 <                assertEquals(it.next(), q.take());
774 <            }
775 <        } catch (InterruptedException e){
776 <            unexpectedException();
777 <        }    
770 >        Iterator it = q.iterator();
771 >        try {
772 >            while (it.hasNext()) {
773 >                assertEquals(it.next(), q.take());
774 >            }
775 >        } catch (InterruptedException e) {
776 >            unexpectedException();
777 >        }
778      }
779  
780      /**
781 <     *
781 >     * iterator.remove removes current element
782       */
783 <    public void testIteratorOrdering() {
715 <
783 >    public void testIteratorRemove () {
784          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
785 +        q.add(two);
786 +        q.add(one);
787 +        q.add(three);
788 +
789 +        Iterator it = q.iterator();
790 +        it.next();
791 +        it.remove();
792 +
793 +        it = q.iterator();
794 +        assertEquals(it.next(), one);
795 +        assertEquals(it.next(), three);
796 +        assertFalse(it.hasNext());
797 +    }
798 +
799  
800 +    /**
801 +     * iterator ordering is FIFO
802 +     */
803 +    public void testIteratorOrdering() {
804 +        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
805          q.add(one);
806          q.add(two);
807          q.add(three);
721
808          assertEquals(0, q.remainingCapacity());
723
809          int k = 0;
810          for (Iterator it = q.iterator(); it.hasNext();) {
811              int i = ((Integer)(it.next())).intValue();
812              assertEquals(++k, i);
813          }
729
814          assertEquals(3, k);
815      }
816  
817      /**
818 <     *
818 >     * Modifications do not cause iterators to fail
819       */
820      public void testWeaklyConsistentIteration () {
737
821          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
739
822          q.add(one);
823          q.add(two);
824          q.add(three);
743
825          try {
826              for (Iterator it = q.iterator(); it.hasNext();) {
827                  q.remove();
# Line 750 | Line 831 | public class LinkedBlockingQueueTest ext
831          catch (ConcurrentModificationException e) {
832              unexpectedException();
833          }
753
834          assertEquals(0, q.size());
835      }
836  
837  
838      /**
839 <     *
839 >     * toString contains toStrings of elements
840       */
841      public void testToString() {
842          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 764 | Line 844 | public class LinkedBlockingQueueTest ext
844          for (int i = 0; i < SIZE; ++i) {
845              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
846          }
847 <    }        
847 >    }
848  
849  
850      /**
851 <     *
851 >     * offer transfers elements across Executor tasks
852       */
853      public void testOfferInExecutor() {
774
854          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
776
855          q.add(one);
856          q.add(two);
779
857          ExecutorService executor = Executors.newFixedThreadPool(2);
781
858          executor.execute(new Runnable() {
859              public void run() {
860                  threadAssertFalse(q.offer(three));
861                  try {
862 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
862 >                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
863                      threadAssertEquals(0, q.remainingCapacity());
864                  }
865                  catch (InterruptedException e) {
# Line 803 | Line 879 | public class LinkedBlockingQueueTest ext
879                  }
880              }
881          });
806        
807        joinPool(executor);
882  
883 +        joinPool(executor);
884      }
885  
886      /**
887 <     *
887 >     * poll retrieves elements across Executor threads
888       */
889      public void testPollInExecutor() {
815
890          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
817
891          ExecutorService executor = Executors.newFixedThreadPool(2);
819
892          executor.execute(new Runnable() {
893              public void run() {
894                  threadAssertNull(q.poll());
895                  try {
896 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
896 >                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
897                      threadAssertTrue(q.isEmpty());
898                  }
899                  catch (InterruptedException e) {
# Line 841 | Line 913 | public class LinkedBlockingQueueTest ext
913                  }
914              }
915          });
916 <        
916 >
917          joinPool(executor);
918      }
919  
920      /**
921 <     *
921 >     * A deserialized serialized queue has same elements in same order
922       */
923      public void testSerialization() {
924          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 861 | Line 933 | public class LinkedBlockingQueueTest ext
933              ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
934              LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
935              assertEquals(q.size(), r.size());
936 <            while (!q.isEmpty())
936 >            while (!q.isEmpty())
937                  assertEquals(q.remove(), r.remove());
938 <        } catch(Exception e){
938 >        } catch (Exception e) {
939 >            unexpectedException();
940 >        }
941 >    }
942 >
943 >    /**
944 >     * drainTo(null) throws NPE
945 >     */
946 >    public void testDrainToNull() {
947 >        LinkedBlockingQueue q = populatedQueue(SIZE);
948 >        try {
949 >            q.drainTo(null);
950 >            shouldThrow();
951 >        } catch (NullPointerException success) {
952 >        }
953 >    }
954 >
955 >    /**
956 >     * drainTo(this) throws IAE
957 >     */
958 >    public void testDrainToSelf() {
959 >        LinkedBlockingQueue q = populatedQueue(SIZE);
960 >        try {
961 >            q.drainTo(q);
962 >            shouldThrow();
963 >        } catch (IllegalArgumentException success) {
964 >        }
965 >    }
966 >
967 >    /**
968 >     * drainTo(c) empties queue into another collection c
969 >     */
970 >    public void testDrainTo() {
971 >        LinkedBlockingQueue q = populatedQueue(SIZE);
972 >        ArrayList l = new ArrayList();
973 >        q.drainTo(l);
974 >        assertEquals(q.size(), 0);
975 >        assertEquals(l.size(), SIZE);
976 >        for (int i = 0; i < SIZE; ++i)
977 >            assertEquals(l.get(i), new Integer(i));
978 >        q.add(zero);
979 >        q.add(one);
980 >        assertFalse(q.isEmpty());
981 >        assertTrue(q.contains(zero));
982 >        assertTrue(q.contains(one));
983 >        l.clear();
984 >        q.drainTo(l);
985 >        assertEquals(q.size(), 0);
986 >        assertEquals(l.size(), 2);
987 >        for (int i = 0; i < 2; ++i)
988 >            assertEquals(l.get(i), new Integer(i));
989 >    }
990 >
991 >    /**
992 >     * drainTo empties full queue, unblocking a waiting put.
993 >     */
994 >    public void testDrainToWithActivePut() {
995 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
996 >        Thread t = new Thread(new Runnable() {
997 >                public void run() {
998 >                    try {
999 >                        q.put(new Integer(SIZE+1));
1000 >                    } catch (InterruptedException ie) {
1001 >                        threadUnexpectedException();
1002 >                    }
1003 >                }
1004 >            });
1005 >        try {
1006 >            t.start();
1007 >            ArrayList l = new ArrayList();
1008 >            q.drainTo(l);
1009 >            assertTrue(l.size() >= SIZE);
1010 >            for (int i = 0; i < SIZE; ++i)
1011 >                assertEquals(l.get(i), new Integer(i));
1012 >            t.join();
1013 >            assertTrue(q.size() + l.size() >= SIZE);
1014 >        } catch (Exception e) {
1015              unexpectedException();
1016          }
1017      }
1018  
1019 +    /**
1020 +     * drainTo(null, n) throws NPE
1021 +     */
1022 +    public void testDrainToNullN() {
1023 +        LinkedBlockingQueue q = populatedQueue(SIZE);
1024 +        try {
1025 +            q.drainTo(null, 0);
1026 +            shouldThrow();
1027 +        } catch (NullPointerException success) {
1028 +        }
1029 +    }
1030 +
1031 +    /**
1032 +     * drainTo(this, n) throws IAE
1033 +     */
1034 +    public void testDrainToSelfN() {
1035 +        LinkedBlockingQueue q = populatedQueue(SIZE);
1036 +        try {
1037 +            q.drainTo(q, 0);
1038 +            shouldThrow();
1039 +        } catch (IllegalArgumentException success) {
1040 +        }
1041 +    }
1042 +
1043 +    /**
1044 +     * drainTo(c, n) empties first max {n, size} elements of queue into c
1045 +     */
1046 +    public void testDrainToN() {
1047 +        LinkedBlockingQueue q = new LinkedBlockingQueue();
1048 +        for (int i = 0; i < SIZE + 2; ++i) {
1049 +            for (int j = 0; j < SIZE; j++)
1050 +                assertTrue(q.offer(new Integer(j)));
1051 +            ArrayList l = new ArrayList();
1052 +            q.drainTo(l, i);
1053 +            int k = (i < SIZE)? i : SIZE;
1054 +            assertEquals(l.size(), k);
1055 +            assertEquals(q.size(), SIZE-k);
1056 +            for (int j = 0; j < k; ++j)
1057 +                assertEquals(l.get(j), new Integer(j));
1058 +            while (q.poll() != null) ;
1059 +        }
1060 +    }
1061 +
1062   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines