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

Comparing jsr166/src/test/tck/LinkedBlockingDequeTest.java (file contents):
Revision 1.3 by dl, Fri Sep 16 11:16:03 2005 UTC vs.
Revision 1.9 by jsr166, Sat Nov 21 08:37:39 2009 UTC

# Line 7 | Line 7
7   import junit.framework.*;
8   import java.util.*;
9   import java.util.concurrent.*;
10 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
11   import java.io.*;
12  
13   public class LinkedBlockingDequeTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run (suite());  
15 >        junit.textui.TestRunner.run (suite());
16      }
17  
18      public static Test suite() {
19 <        return new TestSuite(LinkedBlockingDequeTest.class);
19 >        return new TestSuite(LinkedBlockingDequeTest.class);
20      }
21  
22      /**
# Line 25 | Line 26 | public class LinkedBlockingDequeTest ext
26      private LinkedBlockingDeque populatedDeque(int n) {
27          LinkedBlockingDeque q = new LinkedBlockingDeque(n);
28          assertTrue(q.isEmpty());
29 <        for(int i = 0; i < n; i++)
30 <            assertTrue(q.offer(new Integer(i)));
29 >        for (int i = 0; i < n; i++)
30 >            assertTrue(q.offer(new Integer(i)));
31          assertFalse(q.isEmpty());
32          assertEquals(0, q.remainingCapacity());
33 <        assertEquals(n, q.size());
33 >        assertEquals(n, q.size());
34          return q;
35      }
36  
# Line 66 | Line 67 | public class LinkedBlockingDequeTest ext
67       * offer(null) throws NPE
68       */
69      public void testOfferFirstNull() {
70 <        try {
70 >        try {
71              LinkedBlockingDeque q = new LinkedBlockingDeque();
72              q.offerFirst(null);
73              shouldThrow();
74 <        } catch (NullPointerException success) {
74 <        }  
74 >        } catch (NullPointerException success) {}
75      }
76  
77      /**
78 <     * OfferFirst succeeds
78 >     * OfferFirst succeeds
79       */
80      public void testOfferFirst() {
81          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 84 | Line 84 | public class LinkedBlockingDequeTest ext
84      }
85  
86      /**
87 <     * OfferLast succeeds
87 >     * OfferLast succeeds
88       */
89      public void testOfferLast() {
90          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 100 | Line 100 | public class LinkedBlockingDequeTest ext
100          for (int i = 0; i < SIZE; ++i) {
101              assertEquals(i, ((Integer)q.pollFirst()).intValue());
102          }
103 <        assertNull(q.pollFirst());
103 >        assertNull(q.pollFirst());
104      }
105  
106      /**
# Line 111 | Line 111 | public class LinkedBlockingDequeTest ext
111          for (int i = SIZE-1; i >= 0; --i) {
112              assertEquals(i, ((Integer)q.pollLast()).intValue());
113          }
114 <        assertNull(q.pollLast());
114 >        assertNull(q.pollLast());
115      }
116  
117      /**
# Line 125 | Line 125 | public class LinkedBlockingDequeTest ext
125              assertTrue(q.peekFirst() == null ||
126                         i != ((Integer)q.peekFirst()).intValue());
127          }
128 <        assertNull(q.peekFirst());
128 >        assertNull(q.peekFirst());
129      }
130  
131      /**
# Line 139 | Line 139 | public class LinkedBlockingDequeTest ext
139              assertTrue(q.peek() == null ||
140                         i != ((Integer)q.peek()).intValue());
141          }
142 <        assertNull(q.peek());
142 >        assertNull(q.peek());
143      }
144  
145      /**
# Line 153 | Line 153 | public class LinkedBlockingDequeTest ext
153              assertTrue(q.peekLast() == null ||
154                         i != ((Integer)q.peekLast()).intValue());
155          }
156 <        assertNull(q.peekLast());
156 >        assertNull(q.peekLast());
157      }
158  
159      /**
# Line 168 | Line 168 | public class LinkedBlockingDequeTest ext
168          try {
169              q.getFirst();
170              shouldThrow();
171 <        }
172 <        catch (NoSuchElementException success) {}
171 >        } catch (NoSuchElementException success) {}
172      }
173  
174      /**
# Line 184 | Line 183 | public class LinkedBlockingDequeTest ext
183          try {
184              q.getLast();
185              shouldThrow();
186 <        }
187 <        catch (NoSuchElementException success) {}
189 <        assertNull(q.peekLast());
186 >        } catch (NoSuchElementException success) {}
187 >        assertNull(q.peekLast());
188      }
189  
190      /**
# Line 200 | Line 198 | public class LinkedBlockingDequeTest ext
198          try {
199              q.removeFirst();
200              shouldThrow();
201 <        } catch (NoSuchElementException success){
204 <        }  
201 >        } catch (NoSuchElementException success) {}
202      }
203  
204      /**
# Line 215 | Line 212 | public class LinkedBlockingDequeTest ext
212          try {
213              q.remove();
214              shouldThrow();
215 <        } catch (NoSuchElementException success){
219 <        }  
215 >        } catch (NoSuchElementException success) {}
216      }
217  
218      /**
# Line 255 | Line 251 | public class LinkedBlockingDequeTest ext
251      public void testAddFirst() {
252          LinkedBlockingDeque q = populatedDeque(3);
253          q.pollLast();
254 <        q.addFirst(four);
255 <        assertEquals(four,q.peekFirst());
256 <    }  
254 >        q.addFirst(four);
255 >        assertEquals(four,q.peekFirst());
256 >    }
257  
258      /**
259       * peekLast returns element inserted with addLast
# Line 265 | Line 261 | public class LinkedBlockingDequeTest ext
261      public void testAddLast() {
262          LinkedBlockingDeque q = populatedDeque(3);
263          q.pollLast();
264 <        q.addLast(four);
265 <        assertEquals(four,q.peekLast());
266 <    }  
264 >        q.addLast(four);
265 >        assertEquals(four,q.peekLast());
266 >    }
267  
268  
269      /**
# Line 286 | Line 282 | public class LinkedBlockingDequeTest ext
282          try {
283              LinkedBlockingDeque q = new LinkedBlockingDeque(0);
284              shouldThrow();
285 <        }
290 <        catch (IllegalArgumentException success) {}
285 >        } catch (IllegalArgumentException success) {}
286      }
287  
288      /**
# Line 297 | Line 292 | public class LinkedBlockingDequeTest ext
292          try {
293              LinkedBlockingDeque q = new LinkedBlockingDeque(null);
294              shouldThrow();
295 <        }
301 <        catch (NullPointerException success) {}
295 >        } catch (NullPointerException success) {}
296      }
297  
298      /**
# Line 309 | Line 303 | public class LinkedBlockingDequeTest ext
303              Integer[] ints = new Integer[SIZE];
304              LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
305              shouldThrow();
306 <        }
313 <        catch (NullPointerException success) {}
306 >        } catch (NullPointerException success) {}
307      }
308  
309      /**
# Line 323 | Line 316 | public class LinkedBlockingDequeTest ext
316                  ints[i] = new Integer(i);
317              LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
318              shouldThrow();
319 <        }
327 <        catch (NullPointerException success) {}
319 >        } catch (NullPointerException success) {}
320      }
321  
322      /**
# Line 378 | Line 370 | public class LinkedBlockingDequeTest ext
370       * offer(null) throws NPE
371       */
372      public void testOfferNull() {
373 <        try {
373 >        try {
374              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
375              q.offer(null);
376              shouldThrow();
377 <        } catch (NullPointerException success) { }  
377 >        } catch (NullPointerException success) {}
378      }
379  
380      /**
381       * add(null) throws NPE
382       */
383      public void testAddNull() {
384 <        try {
384 >        try {
385              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
386              q.add(null);
387              shouldThrow();
388 <        } catch (NullPointerException success) { }  
388 >        } catch (NullPointerException success) {}
389      }
390  
391      /**
392       * push(null) throws NPE
393       */
394      public void testPushNull() {
395 <        try {
395 >        try {
396              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
397              q.push(null);
398              shouldThrow();
399 <        } catch (NullPointerException success) { }  
399 >        } catch (NullPointerException success) {}
400      }
401  
402      /**
403       * push succeeds if not full; throws ISE if full
404       */
405      public void testPush() {
406 <        try {
406 >        try {
407              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
408              for (int i = 0; i < SIZE; ++i) {
409                  Integer I = new Integer(i);
# Line 420 | Line 412 | public class LinkedBlockingDequeTest ext
412              }
413              assertEquals(0, q.remainingCapacity());
414              q.push(new Integer(SIZE));
415 <        } catch (IllegalStateException success){
416 <        }  
415 >            shouldThrow();
416 >        } catch (IllegalStateException success) {}
417      }
418  
419      /**
# Line 430 | Line 422 | public class LinkedBlockingDequeTest ext
422      public void testPushWithPeek() {
423          LinkedBlockingDeque q = populatedDeque(3);
424          q.pollLast();
425 <        q.push(four);
426 <        assertEquals(four,q.peekFirst());
427 <    }  
425 >        q.push(four);
426 >        assertEquals(four,q.peekFirst());
427 >    }
428  
429  
430      /**
# Line 446 | Line 438 | public class LinkedBlockingDequeTest ext
438          try {
439              q.pop();
440              shouldThrow();
441 <        } catch (NoSuchElementException success){
450 <        }  
441 >        } catch (NoSuchElementException success) {}
442      }
443  
444  
# Line 464 | Line 455 | public class LinkedBlockingDequeTest ext
455       * add succeeds if not full; throws ISE if full
456       */
457      public void testAdd() {
458 <        try {
458 >        try {
459              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
460              for (int i = 0; i < SIZE; ++i) {
461                  assertTrue(q.add(new Integer(i)));
462              }
463              assertEquals(0, q.remainingCapacity());
464              q.add(new Integer(SIZE));
465 <        } catch (IllegalStateException success){
466 <        }  
465 >            shouldThrow();
466 >        } catch (IllegalStateException success) {}
467      }
468  
469      /**
# Line 483 | Line 474 | public class LinkedBlockingDequeTest ext
474              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
475              q.addAll(null);
476              shouldThrow();
477 <        }
487 <        catch (NullPointerException success) {}
477 >        } catch (NullPointerException success) {}
478      }
479  
480      /**
# Line 495 | Line 485 | public class LinkedBlockingDequeTest ext
485              LinkedBlockingDeque q = populatedDeque(SIZE);
486              q.addAll(q);
487              shouldThrow();
488 <        }
499 <        catch (IllegalArgumentException success) {}
488 >        } catch (IllegalArgumentException success) {}
489      }
490  
491      /**
# Line 508 | Line 497 | public class LinkedBlockingDequeTest ext
497              Integer[] ints = new Integer[SIZE];
498              q.addAll(Arrays.asList(ints));
499              shouldThrow();
500 <        }
512 <        catch (NullPointerException success) {}
500 >        } catch (NullPointerException success) {}
501      }
502      /**
503       * addAll of a collection with any null elements throws NPE after
# Line 523 | Line 511 | public class LinkedBlockingDequeTest ext
511                  ints[i] = new Integer(i);
512              q.addAll(Arrays.asList(ints));
513              shouldThrow();
514 <        }
527 <        catch (NullPointerException success) {}
514 >        } catch (NullPointerException success) {}
515      }
516      /**
517       * addAll throws ISE if not enough room
# Line 537 | Line 524 | public class LinkedBlockingDequeTest ext
524                  ints[i] = new Integer(i);
525              q.addAll(Arrays.asList(ints));
526              shouldThrow();
527 <        }
541 <        catch (IllegalStateException success) {}
527 >        } catch (IllegalStateException success) {}
528      }
529      /**
530       * Deque contains all elements, in traversal order, of successful addAll
# Line 562 | Line 548 | public class LinkedBlockingDequeTest ext
548      /**
549       * put(null) throws NPE
550       */
551 <     public void testPutNull() {
552 <        try {
551 >     public void testPutNull() throws InterruptedException {
552 >        try {
553              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
554              q.put(null);
555              shouldThrow();
556 <        }
571 <        catch (NullPointerException success){
572 <        }  
573 <        catch (InterruptedException ie) {
574 <            unexpectedException();
575 <        }
556 >        } catch (NullPointerException success) {}
557       }
558  
559      /**
560       * all elements successfully put are contained
561       */
562 <     public void testPut() {
563 <         try {
564 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
565 <             for (int i = 0; i < SIZE; ++i) {
566 <                 Integer I = new Integer(i);
567 <                 q.put(I);
587 <                 assertTrue(q.contains(I));
588 <             }
589 <             assertEquals(0, q.remainingCapacity());
562 >     public void testPut() throws InterruptedException {
563 >         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
564 >         for (int i = 0; i < SIZE; ++i) {
565 >             Integer I = new Integer(i);
566 >             q.put(I);
567 >             assertTrue(q.contains(I));
568           }
569 <        catch (InterruptedException ie) {
592 <            unexpectedException();
593 <        }
569 >         assertEquals(0, q.remainingCapacity());
570      }
571  
572      /**
573       * put blocks interruptibly if full
574       */
575 <    public void testBlockingPut() {
576 <        Thread t = new Thread(new Runnable() {
577 <                public void run() {
578 <                    int added = 0;
579 <                    try {
580 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
581 <                        for (int i = 0; i < SIZE; ++i) {
582 <                            q.put(new Integer(i));
583 <                            ++added;
584 <                        }
585 <                        q.put(new Integer(SIZE));
586 <                        threadShouldThrow();
587 <                    } catch (InterruptedException ie){
588 <                        threadAssertEquals(added, SIZE);
589 <                    }  
590 <                }});
575 >    public void testBlockingPut() throws InterruptedException {
576 >        Thread t = new Thread(new CheckedRunnable() {
577 >            public void realRun() {
578 >                int added = 0;
579 >                try {
580 >                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
581 >                    for (int i = 0; i < SIZE; ++i) {
582 >                        q.put(new Integer(i));
583 >                        ++added;
584 >                    }
585 >                    q.put(new Integer(SIZE));
586 >                    threadShouldThrow();
587 >                } catch (InterruptedException success) {
588 >                    threadAssertEquals(added, SIZE);
589 >                }
590 >            }});
591 >
592          t.start();
593 <        try {
594 <           Thread.sleep(SHORT_DELAY_MS);
595 <           t.interrupt();
619 <           t.join();
620 <        }
621 <        catch (InterruptedException ie) {
622 <            unexpectedException();
623 <        }
593 >        Thread.sleep(SHORT_DELAY_MS);
594 >        t.interrupt();
595 >        t.join();
596      }
597  
598      /**
599       * put blocks waiting for take when full
600       */
601 <    public void testPutWithTake() {
601 >    public void testPutWithTake() throws InterruptedException {
602          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
603          Thread t = new Thread(new Runnable() {
604                  public void run() {
# Line 640 | Line 612 | public class LinkedBlockingDequeTest ext
612                          ++added;
613                          q.put(new Object());
614                          ++added;
615 <                        threadShouldThrow();
616 <                    } catch (InterruptedException e){
615 >                        threadShouldThrow();
616 >                    } catch (InterruptedException success) {
617                          threadAssertTrue(added >= 2);
618                      }
619                  }
620              });
621 <        try {
622 <            t.start();
623 <            Thread.sleep(SHORT_DELAY_MS);
624 <            q.take();
625 <            t.interrupt();
626 <            t.join();
655 <        } catch (Exception e){
656 <            unexpectedException();
657 <        }
621 >
622 >        t.start();
623 >        Thread.sleep(SHORT_DELAY_MS);
624 >        q.take();
625 >        t.interrupt();
626 >        t.join();
627      }
628  
629      /**
630       * timed offer times out if full and elements not taken
631       */
632 <    public void testTimedOffer() {
632 >    public void testTimedOffer() throws InterruptedException {
633          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
634 <        Thread t = new Thread(new Runnable() {
635 <                public void run() {
636 <                    try {
637 <                        q.put(new Object());
638 <                        q.put(new Object());
639 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
640 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
641 <                        threadShouldThrow();
642 <                    } catch (InterruptedException success){}
643 <                }
644 <            });
645 <        
677 <        try {
678 <            t.start();
679 <            Thread.sleep(SMALL_DELAY_MS);
680 <            t.interrupt();
681 <            t.join();
682 <        } catch (Exception e){
683 <            unexpectedException();
684 <        }
634 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
635 >            public void realRun() throws InterruptedException {
636 >                q.put(new Object());
637 >                q.put(new Object());
638 >                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
639 >                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
640 >            }};
641 >
642 >        t.start();
643 >        Thread.sleep(SMALL_DELAY_MS);
644 >        t.interrupt();
645 >        t.join();
646      }
647  
648      /**
649       * take retrieves elements in FIFO order
650       */
651 <    public void testTake() {
652 <        try {
653 <            LinkedBlockingDeque q = populatedDeque(SIZE);
654 <            for (int i = 0; i < SIZE; ++i) {
655 <                assertEquals(i, ((Integer)q.take()).intValue());
695 <            }
696 <        } catch (InterruptedException e){
697 <            unexpectedException();
698 <        }  
651 >    public void testTake() throws InterruptedException {
652 >        LinkedBlockingDeque q = populatedDeque(SIZE);
653 >        for (int i = 0; i < SIZE; ++i) {
654 >            assertEquals(i, ((Integer)q.take()).intValue());
655 >        }
656      }
657  
658      /**
659       * take blocks interruptibly when empty
660       */
661 <    public void testTakeFromEmpty() {
661 >    public void testTakeFromEmpty() throws InterruptedException {
662          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
663 <        Thread t = new Thread(new Runnable() {
664 <                public void run() {
665 <                    try {
666 <                        q.take();
667 <                        threadShouldThrow();
668 <                    } catch (InterruptedException success){ }                
669 <                }
670 <            });
671 <        try {
715 <            t.start();
716 <            Thread.sleep(SHORT_DELAY_MS);
717 <            t.interrupt();
718 <            t.join();
719 <        } catch (Exception e){
720 <            unexpectedException();
721 <        }
663 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
664 >            public void realRun() throws InterruptedException {
665 >                q.take();
666 >            }};
667 >
668 >        t.start();
669 >        Thread.sleep(SHORT_DELAY_MS);
670 >        t.interrupt();
671 >        t.join();
672      }
673  
674      /**
675       * Take removes existing elements until empty, then blocks interruptibly
676       */
677 <    public void testBlockingTake() {
678 <        Thread t = new Thread(new Runnable() {
679 <                public void run() {
680 <                    try {
681 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
682 <                        for (int i = 0; i < SIZE; ++i) {
683 <                            assertEquals(i, ((Integer)q.take()).intValue());
684 <                        }
685 <                        q.take();
686 <                        threadShouldThrow();
737 <                    } catch (InterruptedException success){
738 <                    }  
739 <                }});
677 >    public void testBlockingTake() throws InterruptedException {
678 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
679 >            public void realRun() throws InterruptedException {
680 >                LinkedBlockingDeque q = populatedDeque(SIZE);
681 >                for (int i = 0; i < SIZE; ++i) {
682 >                    assertEquals(i, ((Integer)q.take()).intValue());
683 >                }
684 >                q.take();
685 >            }};
686 >
687          t.start();
688 <        try {
689 <           Thread.sleep(SHORT_DELAY_MS);
690 <           t.interrupt();
744 <           t.join();
745 <        }
746 <        catch (InterruptedException ie) {
747 <            unexpectedException();
748 <        }
688 >        Thread.sleep(SHORT_DELAY_MS);
689 >        t.interrupt();
690 >        t.join();
691      }
692  
693  
# Line 757 | Line 699 | public class LinkedBlockingDequeTest ext
699          for (int i = 0; i < SIZE; ++i) {
700              assertEquals(i, ((Integer)q.poll()).intValue());
701          }
702 <        assertNull(q.poll());
702 >        assertNull(q.poll());
703      }
704  
705      /**
706       * timed poll with zero timeout succeeds when non-empty, else times out
707       */
708 <    public void testTimedPoll0() {
709 <        try {
710 <            LinkedBlockingDeque q = populatedDeque(SIZE);
711 <            for (int i = 0; i < SIZE; ++i) {
712 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
713 <            }
772 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
773 <        } catch (InterruptedException e){
774 <            unexpectedException();
775 <        }  
708 >    public void testTimedPoll0() throws InterruptedException {
709 >        LinkedBlockingDeque q = populatedDeque(SIZE);
710 >        for (int i = 0; i < SIZE; ++i) {
711 >            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
712 >        }
713 >        assertNull(q.poll(0, MILLISECONDS));
714      }
715  
716      /**
717       * timed poll with nonzero timeout succeeds when non-empty, else times out
718       */
719 <    public void testTimedPoll() {
720 <        try {
721 <            LinkedBlockingDeque q = populatedDeque(SIZE);
722 <            for (int i = 0; i < SIZE; ++i) {
723 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
724 <            }
787 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
788 <        } catch (InterruptedException e){
789 <            unexpectedException();
790 <        }  
719 >    public void testTimedPoll() throws InterruptedException {
720 >        LinkedBlockingDeque q = populatedDeque(SIZE);
721 >        for (int i = 0; i < SIZE; ++i) {
722 >            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
723 >        }
724 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
725      }
726  
727      /**
728       * Interrupted timed poll throws InterruptedException instead of
729       * returning timeout status
730       */
731 <    public void testInterruptedTimedPoll() {
732 <        Thread t = new Thread(new Runnable() {
733 <                public void run() {
734 <                    try {
735 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
736 <                        for (int i = 0; i < SIZE; ++i) {
737 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
738 <                        }
739 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
740 <                    } catch (InterruptedException success){
807 <                    }  
808 <                }});
731 >    public void testInterruptedTimedPoll() throws InterruptedException {
732 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
733 >            public void realRun() throws InterruptedException {
734 >                LinkedBlockingDeque q = populatedDeque(SIZE);
735 >                for (int i = 0; i < SIZE; ++i) {
736 >                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
737 >                }
738 >                q.poll(SMALL_DELAY_MS, MILLISECONDS);
739 >            }};
740 >
741          t.start();
742 <        try {
743 <           Thread.sleep(SHORT_DELAY_MS);
744 <           t.interrupt();
813 <           t.join();
814 <        }
815 <        catch (InterruptedException ie) {
816 <            unexpectedException();
817 <        }
742 >        Thread.sleep(SHORT_DELAY_MS);
743 >        t.interrupt();
744 >        t.join();
745      }
746  
747      /**
748       *  timed poll before a delayed offer fails; after offer succeeds;
749       *  on interruption throws
750       */
751 <    public void testTimedPollWithOffer() {
751 >    public void testTimedPollWithOffer() throws InterruptedException {
752          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
753 <        Thread t = new Thread(new Runnable() {
754 <                public void run() {
755 <                    try {
756 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
757 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
758 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
759 <                        threadShouldThrow();
760 <                    } catch (InterruptedException success) { }                
761 <                }
762 <            });
763 <        try {
764 <            t.start();
765 <            Thread.sleep(SMALL_DELAY_MS);
839 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
840 <            t.interrupt();
841 <            t.join();
842 <        } catch (Exception e){
843 <            unexpectedException();
844 <        }
845 <    }  
753 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
754 >            public void realRun() throws InterruptedException {
755 >                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
756 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
757 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
758 >            }};
759 >
760 >        t.start();
761 >        Thread.sleep(SMALL_DELAY_MS);
762 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
763 >        t.interrupt();
764 >        t.join();
765 >    }
766  
767  
768      /**
769       * putFirst(null) throws NPE
770       */
771 <     public void testPutFirstNull() {
772 <        try {
771 >     public void testPutFirstNull() throws InterruptedException {
772 >        try {
773              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
774              q.putFirst(null);
775              shouldThrow();
776 <        }
857 <        catch (NullPointerException success){
858 <        }  
859 <        catch (InterruptedException ie) {
860 <            unexpectedException();
861 <        }
776 >        } catch (NullPointerException success) {}
777       }
778  
779      /**
780       * all elements successfully putFirst are contained
781       */
782 <     public void testPutFirst() {
783 <         try {
784 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
785 <             for (int i = 0; i < SIZE; ++i) {
786 <                 Integer I = new Integer(i);
787 <                 q.putFirst(I);
873 <                 assertTrue(q.contains(I));
874 <             }
875 <             assertEquals(0, q.remainingCapacity());
782 >     public void testPutFirst() throws InterruptedException {
783 >         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
784 >         for (int i = 0; i < SIZE; ++i) {
785 >             Integer I = new Integer(i);
786 >             q.putFirst(I);
787 >             assertTrue(q.contains(I));
788           }
789 <        catch (InterruptedException ie) {
878 <            unexpectedException();
879 <        }
789 >         assertEquals(0, q.remainingCapacity());
790      }
791  
792      /**
793       * putFirst blocks interruptibly if full
794       */
795 <    public void testBlockingPutFirst() {
795 >    public void testBlockingPutFirst() throws InterruptedException {
796          Thread t = new Thread(new Runnable() {
797                  public void run() {
798                      int added = 0;
# Line 894 | Line 804 | public class LinkedBlockingDequeTest ext
804                          }
805                          q.putFirst(new Integer(SIZE));
806                          threadShouldThrow();
807 <                    } catch (InterruptedException ie){
807 >                    } catch (InterruptedException success) {
808                          threadAssertEquals(added, SIZE);
809 <                    }  
809 >                    }
810                  }});
811 +
812          t.start();
813 <        try {
814 <           Thread.sleep(SHORT_DELAY_MS);
815 <           t.interrupt();
905 <           t.join();
906 <        }
907 <        catch (InterruptedException ie) {
908 <            unexpectedException();
909 <        }
813 >        Thread.sleep(SHORT_DELAY_MS);
814 >        t.interrupt();
815 >        t.join();
816      }
817  
818      /**
819       * putFirst blocks waiting for take when full
820       */
821 <    public void testPutFirstWithTake() {
821 >    public void testPutFirstWithTake() throws InterruptedException {
822          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
823          Thread t = new Thread(new Runnable() {
824                  public void run() {
# Line 926 | Line 832 | public class LinkedBlockingDequeTest ext
832                          ++added;
833                          q.putFirst(new Object());
834                          ++added;
835 <                        threadShouldThrow();
836 <                    } catch (InterruptedException e){
835 >                        threadShouldThrow();
836 >                    } catch (InterruptedException success) {
837                          threadAssertTrue(added >= 2);
838                      }
839                  }
840              });
841 <        try {
842 <            t.start();
843 <            Thread.sleep(SHORT_DELAY_MS);
844 <            q.take();
845 <            t.interrupt();
846 <            t.join();
941 <        } catch (Exception e){
942 <            unexpectedException();
943 <        }
841 >
842 >        t.start();
843 >        Thread.sleep(SHORT_DELAY_MS);
844 >        q.take();
845 >        t.interrupt();
846 >        t.join();
847      }
848  
849      /**
850       * timed offerFirst times out if full and elements not taken
851       */
852 <    public void testTimedOfferFirst() {
852 >    public void testTimedOfferFirst() throws InterruptedException {
853          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
854 <        Thread t = new Thread(new Runnable() {
855 <                public void run() {
856 <                    try {
857 <                        q.putFirst(new Object());
858 <                        q.putFirst(new Object());
859 <                        threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
860 <                        q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
861 <                        threadShouldThrow();
862 <                    } catch (InterruptedException success){}
863 <                }
864 <            });
865 <        
963 <        try {
964 <            t.start();
965 <            Thread.sleep(SMALL_DELAY_MS);
966 <            t.interrupt();
967 <            t.join();
968 <        } catch (Exception e){
969 <            unexpectedException();
970 <        }
854 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
855 >            public void realRun() throws InterruptedException {
856 >                q.putFirst(new Object());
857 >                q.putFirst(new Object());
858 >                threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
859 >                q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
860 >            }};
861 >
862 >        t.start();
863 >        Thread.sleep(SMALL_DELAY_MS);
864 >        t.interrupt();
865 >        t.join();
866      }
867  
868      /**
869       * take retrieves elements in FIFO order
870       */
871 <    public void testTakeFirst() {
872 <        try {
873 <            LinkedBlockingDeque q = populatedDeque(SIZE);
874 <            for (int i = 0; i < SIZE; ++i) {
875 <                assertEquals(i, ((Integer)q.takeFirst()).intValue());
981 <            }
982 <        } catch (InterruptedException e){
983 <            unexpectedException();
984 <        }  
871 >    public void testTakeFirst() throws InterruptedException {
872 >        LinkedBlockingDeque q = populatedDeque(SIZE);
873 >        for (int i = 0; i < SIZE; ++i) {
874 >            assertEquals(i, ((Integer)q.takeFirst()).intValue());
875 >        }
876      }
877  
878      /**
879       * takeFirst blocks interruptibly when empty
880       */
881 <    public void testTakeFirstFromEmpty() {
881 >    public void testTakeFirstFromEmpty() throws InterruptedException {
882          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
883 <        Thread t = new Thread(new Runnable() {
884 <                public void run() {
885 <                    try {
886 <                        q.takeFirst();
887 <                        threadShouldThrow();
888 <                    } catch (InterruptedException success){ }                
889 <                }
890 <            });
891 <        try {
1001 <            t.start();
1002 <            Thread.sleep(SHORT_DELAY_MS);
1003 <            t.interrupt();
1004 <            t.join();
1005 <        } catch (Exception e){
1006 <            unexpectedException();
1007 <        }
883 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
884 >            public void realRun() throws InterruptedException {
885 >                q.takeFirst();
886 >            }};
887 >
888 >        t.start();
889 >        Thread.sleep(SHORT_DELAY_MS);
890 >        t.interrupt();
891 >        t.join();
892      }
893  
894      /**
895       * TakeFirst removes existing elements until empty, then blocks interruptibly
896       */
897 <    public void testBlockingTakeFirst() {
898 <        Thread t = new Thread(new Runnable() {
899 <                public void run() {
900 <                    try {
901 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
902 <                        for (int i = 0; i < SIZE; ++i) {
903 <                            assertEquals(i, ((Integer)q.takeFirst()).intValue());
904 <                        }
905 <                        q.takeFirst();
906 <                        threadShouldThrow();
1023 <                    } catch (InterruptedException success){
1024 <                    }  
1025 <                }});
897 >    public void testBlockingTakeFirst() throws InterruptedException {
898 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
899 >            public void realRun() throws InterruptedException {
900 >                LinkedBlockingDeque q = populatedDeque(SIZE);
901 >                for (int i = 0; i < SIZE; ++i) {
902 >                    assertEquals(i, ((Integer)q.takeFirst()).intValue());
903 >                }
904 >                q.takeFirst();
905 >            }};
906 >
907          t.start();
908 <        try {
909 <           Thread.sleep(SHORT_DELAY_MS);
910 <           t.interrupt();
1030 <           t.join();
1031 <        }
1032 <        catch (InterruptedException ie) {
1033 <            unexpectedException();
1034 <        }
908 >        Thread.sleep(SHORT_DELAY_MS);
909 >        t.interrupt();
910 >        t.join();
911      }
912  
913  
914      /**
915       * timed pollFirst with zero timeout succeeds when non-empty, else times out
916       */
917 <    public void testTimedPollFirst0() {
918 <        try {
919 <            LinkedBlockingDeque q = populatedDeque(SIZE);
920 <            for (int i = 0; i < SIZE; ++i) {
921 <                assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue());
922 <            }
1047 <            assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS));
1048 <        } catch (InterruptedException e){
1049 <            unexpectedException();
1050 <        }  
917 >    public void testTimedPollFirst0() throws InterruptedException {
918 >        LinkedBlockingDeque q = populatedDeque(SIZE);
919 >        for (int i = 0; i < SIZE; ++i) {
920 >            assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
921 >        }
922 >        assertNull(q.pollFirst(0, MILLISECONDS));
923      }
924  
925      /**
926       * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
927       */
928 <    public void testTimedPollFirst() {
929 <        try {
930 <            LinkedBlockingDeque q = populatedDeque(SIZE);
931 <            for (int i = 0; i < SIZE; ++i) {
932 <                assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
933 <            }
1062 <            assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1063 <        } catch (InterruptedException e){
1064 <            unexpectedException();
1065 <        }  
928 >    public void testTimedPollFirst() throws InterruptedException {
929 >        LinkedBlockingDeque q = populatedDeque(SIZE);
930 >        for (int i = 0; i < SIZE; ++i) {
931 >            assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
932 >        }
933 >        assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
934      }
935  
936      /**
937       * Interrupted timed pollFirst throws InterruptedException instead of
938       * returning timeout status
939       */
940 <    public void testInterruptedTimedPollFirst() {
941 <        Thread t = new Thread(new Runnable() {
942 <                public void run() {
943 <                    try {
944 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
945 <                        for (int i = 0; i < SIZE; ++i) {
946 <                            threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
947 <                        }
948 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
949 <                    } catch (InterruptedException success){
1082 <                    }  
1083 <                }});
940 >    public void testInterruptedTimedPollFirst() throws InterruptedException {
941 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
942 >            public void realRun() throws InterruptedException {
943 >                LinkedBlockingDeque q = populatedDeque(SIZE);
944 >                for (int i = 0; i < SIZE; ++i) {
945 >                    threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
946 >                }
947 >                q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
948 >            }};
949 >
950          t.start();
951 <        try {
952 <           Thread.sleep(SHORT_DELAY_MS);
953 <           t.interrupt();
1088 <           t.join();
1089 <        }
1090 <        catch (InterruptedException ie) {
1091 <            unexpectedException();
1092 <        }
951 >        Thread.sleep(SHORT_DELAY_MS);
952 >        t.interrupt();
953 >        t.join();
954      }
955  
956      /**
957       *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
958       *  on interruption throws
959       */
960 <    public void testTimedPollFirstWithOfferFirst() {
960 >    public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
961          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
962 <        Thread t = new Thread(new Runnable() {
963 <                public void run() {
964 <                    try {
965 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
966 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
967 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
968 <                        threadShouldThrow();
969 <                    } catch (InterruptedException success) { }                
970 <                }
971 <            });
972 <        try {
973 <            t.start();
974 <            Thread.sleep(SMALL_DELAY_MS);
1114 <            assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1115 <            t.interrupt();
1116 <            t.join();
1117 <        } catch (Exception e){
1118 <            unexpectedException();
1119 <        }
1120 <    }  
962 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
963 >            public void realRun() throws InterruptedException {
964 >                threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
965 >                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
966 >                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
967 >            }};
968 >
969 >        t.start();
970 >        Thread.sleep(SMALL_DELAY_MS);
971 >        assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
972 >        t.interrupt();
973 >        t.join();
974 >    }
975  
976      /**
977       * putLast(null) throws NPE
978       */
979 <     public void testPutLastNull() {
980 <        try {
979 >     public void testPutLastNull() throws InterruptedException {
980 >        try {
981              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
982              q.putLast(null);
983              shouldThrow();
984 <        }
1131 <        catch (NullPointerException success){
1132 <        }  
1133 <        catch (InterruptedException ie) {
1134 <            unexpectedException();
1135 <        }
984 >        } catch (NullPointerException success) {}
985       }
986  
987      /**
988       * all elements successfully putLast are contained
989       */
990 <     public void testPutLast() {
991 <         try {
992 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
993 <             for (int i = 0; i < SIZE; ++i) {
994 <                 Integer I = new Integer(i);
995 <                 q.putLast(I);
1147 <                 assertTrue(q.contains(I));
1148 <             }
1149 <             assertEquals(0, q.remainingCapacity());
990 >     public void testPutLast() throws InterruptedException {
991 >         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
992 >         for (int i = 0; i < SIZE; ++i) {
993 >             Integer I = new Integer(i);
994 >             q.putLast(I);
995 >             assertTrue(q.contains(I));
996           }
997 <        catch (InterruptedException ie) {
1152 <            unexpectedException();
1153 <        }
997 >         assertEquals(0, q.remainingCapacity());
998      }
999  
1000      /**
1001       * putLast blocks interruptibly if full
1002       */
1003 <    public void testBlockingPutLast() {
1003 >    public void testBlockingPutLast() throws InterruptedException {
1004          Thread t = new Thread(new Runnable() {
1005                  public void run() {
1006                      int added = 0;
# Line 1168 | Line 1012 | public class LinkedBlockingDequeTest ext
1012                          }
1013                          q.putLast(new Integer(SIZE));
1014                          threadShouldThrow();
1015 <                    } catch (InterruptedException ie){
1015 >                    } catch (InterruptedException success) {
1016                          threadAssertEquals(added, SIZE);
1017 <                    }  
1017 >                    }
1018                  }});
1019          t.start();
1020 <        try {
1021 <           Thread.sleep(SHORT_DELAY_MS);
1022 <           t.interrupt();
1179 <           t.join();
1180 <        }
1181 <        catch (InterruptedException ie) {
1182 <            unexpectedException();
1183 <        }
1020 >        Thread.sleep(SHORT_DELAY_MS);
1021 >        t.interrupt();
1022 >        t.join();
1023      }
1024  
1025      /**
1026       * putLast blocks waiting for take when full
1027       */
1028 <    public void testPutLastWithTake() {
1028 >    public void testPutLastWithTake() throws InterruptedException {
1029          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1030          Thread t = new Thread(new Runnable() {
1031                  public void run() {
# Line 1200 | Line 1039 | public class LinkedBlockingDequeTest ext
1039                          ++added;
1040                          q.putLast(new Object());
1041                          ++added;
1042 <                        threadShouldThrow();
1043 <                    } catch (InterruptedException e){
1042 >                        threadShouldThrow();
1043 >                    } catch (InterruptedException success) {
1044                          threadAssertTrue(added >= 2);
1045                      }
1046                  }
1047              });
1048 <        try {
1049 <            t.start();
1050 <            Thread.sleep(SHORT_DELAY_MS);
1051 <            q.take();
1052 <            t.interrupt();
1053 <            t.join();
1215 <        } catch (Exception e){
1216 <            unexpectedException();
1217 <        }
1048 >
1049 >        t.start();
1050 >        Thread.sleep(SHORT_DELAY_MS);
1051 >        q.take();
1052 >        t.interrupt();
1053 >        t.join();
1054      }
1055  
1056      /**
1057       * timed offerLast times out if full and elements not taken
1058       */
1059 <    public void testTimedOfferLast() {
1059 >    public void testTimedOfferLast() throws InterruptedException {
1060          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1061 <        Thread t = new Thread(new Runnable() {
1062 <                public void run() {
1063 <                    try {
1064 <                        q.putLast(new Object());
1065 <                        q.putLast(new Object());
1066 <                        threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1067 <                        q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1068 <                        threadShouldThrow();
1069 <                    } catch (InterruptedException success){}
1070 <                }
1071 <            });
1072 <        
1237 <        try {
1238 <            t.start();
1239 <            Thread.sleep(SMALL_DELAY_MS);
1240 <            t.interrupt();
1241 <            t.join();
1242 <        } catch (Exception e){
1243 <            unexpectedException();
1244 <        }
1061 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1062 >            public void realRun() throws InterruptedException {
1063 >                q.putLast(new Object());
1064 >                q.putLast(new Object());
1065 >                threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1066 >                q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1067 >            }};
1068 >
1069 >        t.start();
1070 >        Thread.sleep(SMALL_DELAY_MS);
1071 >        t.interrupt();
1072 >        t.join();
1073      }
1074  
1075      /**
1076       * takeLast retrieves elements in FIFO order
1077       */
1078 <    public void testTakeLast() {
1079 <        try {
1080 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1081 <            for (int i = 0; i < SIZE; ++i) {
1082 <                assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1255 <            }
1256 <        } catch (InterruptedException e){
1257 <            unexpectedException();
1258 <        }  
1078 >    public void testTakeLast() throws InterruptedException {
1079 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1080 >        for (int i = 0; i < SIZE; ++i) {
1081 >            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1082 >        }
1083      }
1084  
1085      /**
1086       * takeLast blocks interruptibly when empty
1087       */
1088 <    public void testTakeLastFromEmpty() {
1088 >    public void testTakeLastFromEmpty() throws InterruptedException {
1089          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1090 <        Thread t = new Thread(new Runnable() {
1091 <                public void run() {
1092 <                    try {
1093 <                        q.takeLast();
1094 <                        threadShouldThrow();
1095 <                    } catch (InterruptedException success){ }                
1096 <                }
1097 <            });
1098 <        try {
1275 <            t.start();
1276 <            Thread.sleep(SHORT_DELAY_MS);
1277 <            t.interrupt();
1278 <            t.join();
1279 <        } catch (Exception e){
1280 <            unexpectedException();
1281 <        }
1090 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1091 >            public void realRun() throws InterruptedException {
1092 >                q.takeLast();
1093 >            }};
1094 >
1095 >        t.start();
1096 >        Thread.sleep(SHORT_DELAY_MS);
1097 >        t.interrupt();
1098 >        t.join();
1099      }
1100  
1101      /**
1102       * TakeLast removes existing elements until empty, then blocks interruptibly
1103       */
1104 <    public void testBlockingTakeLast() {
1105 <        Thread t = new Thread(new Runnable() {
1106 <                public void run() {
1107 <                    try {
1108 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1109 <                        for (int i = 0; i < SIZE; ++i) {
1110 <                            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1111 <                        }
1112 <                        q.takeLast();
1113 <                        threadShouldThrow();
1297 <                    } catch (InterruptedException success){
1298 <                    }  
1299 <                }});
1104 >    public void testBlockingTakeLast() throws InterruptedException {
1105 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1106 >            public void realRun() throws InterruptedException {
1107 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1108 >                for (int i = 0; i < SIZE; ++i) {
1109 >                    assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1110 >                }
1111 >                q.takeLast();
1112 >            }};
1113 >
1114          t.start();
1115 <        try {
1116 <           Thread.sleep(SHORT_DELAY_MS);
1117 <           t.interrupt();
1304 <           t.join();
1305 <        }
1306 <        catch (InterruptedException ie) {
1307 <            unexpectedException();
1308 <        }
1115 >        Thread.sleep(SHORT_DELAY_MS);
1116 >        t.interrupt();
1117 >        t.join();
1118      }
1119  
1120  
1121      /**
1122       * timed pollLast with zero timeout succeeds when non-empty, else times out
1123       */
1124 <    public void testTimedPollLast0() {
1125 <        try {
1126 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1127 <            for (int i = 0; i < SIZE; ++i) {
1128 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue());
1129 <            }
1321 <            assertNull(q.pollLast(0, TimeUnit.MILLISECONDS));
1322 <        } catch (InterruptedException e){
1323 <            unexpectedException();
1324 <        }  
1124 >    public void testTimedPollLast0() throws InterruptedException {
1125 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1126 >        for (int i = 0; i < SIZE; ++i) {
1127 >            assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1128 >        }
1129 >        assertNull(q.pollLast(0, MILLISECONDS));
1130      }
1131  
1132      /**
1133       * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1134       */
1135 <    public void testTimedPollLast() {
1136 <        try {
1137 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1138 <            for (int i = 0; i < SIZE; ++i) {
1139 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1140 <            }
1336 <            assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1337 <        } catch (InterruptedException e){
1338 <            unexpectedException();
1339 <        }  
1135 >    public void testTimedPollLast() throws InterruptedException {
1136 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1137 >        for (int i = 0; i < SIZE; ++i) {
1138 >            assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1139 >        }
1140 >        assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1141      }
1142  
1143      /**
1144       * Interrupted timed pollLast throws InterruptedException instead of
1145       * returning timeout status
1146       */
1147 <    public void testInterruptedTimedPollLast() {
1148 <        Thread t = new Thread(new Runnable() {
1149 <                public void run() {
1150 <                    try {
1151 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1152 <                        for (int i = 0; i < SIZE; ++i) {
1153 <                            threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1154 <                        }
1155 <                        threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1156 <                    } catch (InterruptedException success){
1356 <                    }  
1357 <                }});
1147 >    public void testInterruptedTimedPollLast() throws InterruptedException {
1148 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1149 >            public void realRun() throws InterruptedException {
1150 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1151 >                for (int i = 0; i < SIZE; ++i) {
1152 >                    threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1153 >                }
1154 >                q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1155 >            }};
1156 >
1157          t.start();
1158 <        try {
1159 <           Thread.sleep(SHORT_DELAY_MS);
1160 <           t.interrupt();
1362 <           t.join();
1363 <        }
1364 <        catch (InterruptedException ie) {
1365 <            unexpectedException();
1366 <        }
1158 >        Thread.sleep(SHORT_DELAY_MS);
1159 >        t.interrupt();
1160 >        t.join();
1161      }
1162  
1163      /**
1164       *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1165       *  on interruption throws
1166       */
1167 <    public void testTimedPollWithOfferLast() {
1167 >    public void testTimedPollWithOfferLast() throws InterruptedException {
1168          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1169 <        Thread t = new Thread(new Runnable() {
1170 <                public void run() {
1171 <                    try {
1172 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1173 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1174 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1175 <                        threadShouldThrow();
1176 <                    } catch (InterruptedException success) { }                
1177 <                }
1178 <            });
1179 <        try {
1180 <            t.start();
1181 <            Thread.sleep(SMALL_DELAY_MS);
1182 <            assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1183 <            t.interrupt();
1184 <            t.join();
1391 <        } catch (Exception e){
1392 <            unexpectedException();
1393 <        }
1394 <    }  
1169 >        Thread t = new Thread(new CheckedRunnable() {
1170 >            public void realRun() throws InterruptedException {
1171 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1172 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1173 >                try {
1174 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1175 >                    shouldThrow();
1176 >                } catch (InterruptedException success) {}
1177 >            }});
1178 >
1179 >        t.start();
1180 >        Thread.sleep(SMALL_DELAY_MS);
1181 >        assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1182 >        t.interrupt();
1183 >        t.join();
1184 >    }
1185  
1186  
1187      /**
# Line 1406 | Line 1196 | public class LinkedBlockingDequeTest ext
1196          try {
1197              q.element();
1198              shouldThrow();
1199 <        }
1410 <        catch (NoSuchElementException success) {}
1199 >        } catch (NoSuchElementException success) {}
1200      }
1201  
1202      /**
# Line 1424 | Line 1213 | public class LinkedBlockingDequeTest ext
1213          }
1214          assertTrue(q.isEmpty());
1215      }
1216 <        
1216 >
1217      /**
1218       * contains(x) reports true when elements added but not yet removed
1219       */
# Line 1505 | Line 1294 | public class LinkedBlockingDequeTest ext
1294      /**
1295       * toArray contains all elements
1296       */
1297 <    public void testToArray() {
1297 >    public void testToArray() throws InterruptedException{
1298          LinkedBlockingDeque q = populatedDeque(SIZE);
1299 <        Object[] o = q.toArray();
1300 <        try {
1301 <        for(int i = 0; i < o.length; i++)
1513 <            assertEquals(o[i], q.take());
1514 <        } catch (InterruptedException e){
1515 <            unexpectedException();
1516 <        }    
1299 >        Object[] o = q.toArray();
1300 >        for (int i = 0; i < o.length; i++)
1301 >            assertEquals(o[i], q.take());
1302      }
1303  
1304      /**
1305       * toArray(a) contains all elements
1306       */
1307 <    public void testToArray2() {
1307 >    public void testToArray2() throws InterruptedException {
1308          LinkedBlockingDeque q = populatedDeque(SIZE);
1309 <        Integer[] ints = new Integer[SIZE];
1310 <        ints = (Integer[])q.toArray(ints);
1311 <        try {
1312 <            for(int i = 0; i < ints.length; i++)
1528 <                assertEquals(ints[i], q.take());
1529 <        } catch (InterruptedException e){
1530 <            unexpectedException();
1531 <        }    
1309 >        Integer[] ints = new Integer[SIZE];
1310 >        ints = (Integer[])q.toArray(ints);
1311 >        for (int i = 0; i < ints.length; i++)
1312 >            assertEquals(ints[i], q.take());
1313      }
1314  
1315      /**
1316       * toArray(null) throws NPE
1317       */
1318      public void testToArray_BadArg() {
1319 <        try {
1319 >        try {
1320              LinkedBlockingDeque q = populatedDeque(SIZE);
1321 <            Object o[] = q.toArray(null);
1322 <            shouldThrow();
1323 <        } catch(NullPointerException success){}
1321 >            Object o[] = q.toArray(null);
1322 >            shouldThrow();
1323 >        } catch (NullPointerException success) {}
1324      }
1325  
1326      /**
1327       * toArray with incompatible array type throws CCE
1328       */
1329      public void testToArray1_BadArg() {
1330 <        try {
1330 >        try {
1331              LinkedBlockingDeque q = populatedDeque(SIZE);
1332 <            Object o[] = q.toArray(new String[10] );
1333 <            shouldThrow();
1334 <        } catch(ArrayStoreException  success){}
1332 >            Object o[] = q.toArray(new String[10] );
1333 >            shouldThrow();
1334 >        } catch (ArrayStoreException success) {}
1335      }
1336  
1337 <    
1337 >
1338      /**
1339       * iterator iterates through all elements
1340       */
1341 <    public void testIterator() {
1341 >    public void testIterator() throws InterruptedException {
1342          LinkedBlockingDeque q = populatedDeque(SIZE);
1343 <        Iterator it = q.iterator();
1344 <        try {
1345 <            while(it.hasNext()){
1346 <                assertEquals(it.next(), q.take());
1566 <            }
1567 <        } catch (InterruptedException e){
1568 <            unexpectedException();
1569 <        }    
1343 >        Iterator it = q.iterator();
1344 >        while (it.hasNext()) {
1345 >            assertEquals(it.next(), q.take());
1346 >        }
1347      }
1348  
1349      /**
# Line 1581 | Line 1358 | public class LinkedBlockingDequeTest ext
1358          Iterator it = q.iterator();
1359          it.next();
1360          it.remove();
1361 <        
1361 >
1362          it = q.iterator();
1363          assertEquals(it.next(), one);
1364          assertEquals(it.next(), three);
# Line 1614 | Line 1391 | public class LinkedBlockingDequeTest ext
1391          q.add(one);
1392          q.add(two);
1393          q.add(three);
1394 <        try {
1395 <            for (Iterator it = q.iterator(); it.hasNext();) {
1396 <                q.remove();
1620 <                it.next();
1621 <            }
1622 <        }
1623 <        catch (ConcurrentModificationException e) {
1624 <            unexpectedException();
1394 >        for (Iterator it = q.iterator(); it.hasNext();) {
1395 >            q.remove();
1396 >            it.next();
1397          }
1398          assertEquals(0, q.size());
1399      }
# Line 1633 | Line 1405 | public class LinkedBlockingDequeTest ext
1405      public void testDescendingIterator() {
1406          LinkedBlockingDeque q = populatedDeque(SIZE);
1407          int i = 0;
1408 <        Iterator it = q.descendingIterator();
1409 <        while(it.hasNext()) {
1408 >        Iterator it = q.descendingIterator();
1409 >        while (it.hasNext()) {
1410              assertTrue(q.contains(it.next()));
1411              ++i;
1412          }
# Line 1642 | Line 1414 | public class LinkedBlockingDequeTest ext
1414          assertFalse(it.hasNext());
1415          try {
1416              it.next();
1417 <        } catch(NoSuchElementException success) {
1418 <        }
1417 >            shouldThrow();
1418 >        } catch (NoSuchElementException success) {}
1419      }
1420  
1421      /**
# Line 1660 | Line 1432 | public class LinkedBlockingDequeTest ext
1432                  int i = ((Integer)(it.next())).intValue();
1433                  assertEquals(++k, i);
1434              }
1435 <            
1435 >
1436              assertEquals(3, k);
1437              q.remove();
1438              q.remove();
# Line 1700 | Line 1472 | public class LinkedBlockingDequeTest ext
1472          for (int i = 0; i < SIZE; ++i) {
1473              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1474          }
1475 <    }        
1475 >    }
1476  
1477  
1478      /**
# Line 1711 | Line 1483 | public class LinkedBlockingDequeTest ext
1483          q.add(one);
1484          q.add(two);
1485          ExecutorService executor = Executors.newFixedThreadPool(2);
1486 <        executor.execute(new Runnable() {
1487 <            public void run() {
1486 >        executor.execute(new CheckedRunnable() {
1487 >            public void realRun() throws InterruptedException {
1488                  threadAssertFalse(q.offer(three));
1489 <                try {
1490 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1491 <                    threadAssertEquals(0, q.remainingCapacity());
1492 <                }
1493 <                catch (InterruptedException e) {
1494 <                    threadUnexpectedException();
1495 <                }
1496 <            }
1497 <        });
1489 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1490 >                threadAssertEquals(0, q.remainingCapacity());
1491 >            }});
1492 >
1493 >        executor.execute(new CheckedRunnable() {
1494 >            public void realRun() throws InterruptedException {
1495 >                Thread.sleep(SMALL_DELAY_MS);
1496 >                threadAssertEquals(one, q.take());
1497 >            }});
1498  
1727        executor.execute(new Runnable() {
1728            public void run() {
1729                try {
1730                    Thread.sleep(SMALL_DELAY_MS);
1731                    threadAssertEquals(one, q.take());
1732                }
1733                catch (InterruptedException e) {
1734                    threadUnexpectedException();
1735                }
1736            }
1737        });
1738        
1499          joinPool(executor);
1500      }
1501  
# Line 1745 | Line 1505 | public class LinkedBlockingDequeTest ext
1505      public void testPollInExecutor() {
1506          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1507          ExecutorService executor = Executors.newFixedThreadPool(2);
1508 <        executor.execute(new Runnable() {
1509 <            public void run() {
1508 >        executor.execute(new CheckedRunnable() {
1509 >            public void realRun() throws InterruptedException {
1510                  threadAssertNull(q.poll());
1511 <                try {
1512 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1513 <                    threadAssertTrue(q.isEmpty());
1514 <                }
1515 <                catch (InterruptedException e) {
1516 <                    threadUnexpectedException();
1517 <                }
1518 <            }
1519 <        });
1511 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1512 >                threadAssertTrue(q.isEmpty());
1513 >            }});
1514 >
1515 >        executor.execute(new CheckedRunnable() {
1516 >            public void realRun() throws InterruptedException {
1517 >                Thread.sleep(SMALL_DELAY_MS);
1518 >                q.put(one);
1519 >            }});
1520  
1761        executor.execute(new Runnable() {
1762            public void run() {
1763                try {
1764                    Thread.sleep(SMALL_DELAY_MS);
1765                    q.put(one);
1766                }
1767                catch (InterruptedException e) {
1768                    threadUnexpectedException();
1769                }
1770            }
1771        });
1772        
1521          joinPool(executor);
1522      }
1523  
1524      /**
1525       * A deserialized serialized deque has same elements in same order
1526       */
1527 <    public void testSerialization() {
1527 >    public void testSerialization() throws Exception {
1528          LinkedBlockingDeque q = populatedDeque(SIZE);
1529  
1530 <        try {
1531 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1532 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1533 <            out.writeObject(q);
1534 <            out.close();
1535 <
1536 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1537 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1538 <            LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1539 <            assertEquals(q.size(), r.size());
1540 <            while (!q.isEmpty())
1793 <                assertEquals(q.remove(), r.remove());
1794 <        } catch(Exception e){
1795 <            unexpectedException();
1796 <        }
1530 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1531 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1532 >        out.writeObject(q);
1533 >        out.close();
1534 >
1535 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1536 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1537 >        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1538 >        assertEquals(q.size(), r.size());
1539 >        while (!q.isEmpty())
1540 >            assertEquals(q.remove(), r.remove());
1541      }
1542  
1543      /**
1544       * drainTo(null) throws NPE
1545 <     */
1545 >     */
1546      public void testDrainToNull() {
1547          LinkedBlockingDeque q = populatedDeque(SIZE);
1548          try {
1549              q.drainTo(null);
1550              shouldThrow();
1551 <        } catch(NullPointerException success) {
1808 <        }
1551 >        } catch (NullPointerException success) {}
1552      }
1553  
1554      /**
1555       * drainTo(this) throws IAE
1556 <     */
1556 >     */
1557      public void testDrainToSelf() {
1558          LinkedBlockingDeque q = populatedDeque(SIZE);
1559          try {
1560              q.drainTo(q);
1561              shouldThrow();
1562 <        } catch(IllegalArgumentException success) {
1562 >        } catch (IllegalArgumentException success) {
1563          }
1564      }
1565  
1566      /**
1567       * drainTo(c) empties deque into another collection c
1568 <     */
1568 >     */
1569      public void testDrainTo() {
1570          LinkedBlockingDeque q = populatedDeque(SIZE);
1571          ArrayList l = new ArrayList();
1572          q.drainTo(l);
1573          assertEquals(q.size(), 0);
1574          assertEquals(l.size(), SIZE);
1575 <        for (int i = 0; i < SIZE; ++i)
1575 >        for (int i = 0; i < SIZE; ++i)
1576              assertEquals(l.get(i), new Integer(i));
1577          q.add(zero);
1578          q.add(one);
# Line 1840 | Line 1583 | public class LinkedBlockingDequeTest ext
1583          q.drainTo(l);
1584          assertEquals(q.size(), 0);
1585          assertEquals(l.size(), 2);
1586 <        for (int i = 0; i < 2; ++i)
1586 >        for (int i = 0; i < 2; ++i)
1587              assertEquals(l.get(i), new Integer(i));
1588      }
1589  
1590      /**
1591       * drainTo empties full deque, unblocking a waiting put.
1592 <     */
1593 <    public void testDrainToWithActivePut() {
1592 >     */
1593 >    public void testDrainToWithActivePut() throws InterruptedException {
1594          final LinkedBlockingDeque q = populatedDeque(SIZE);
1595 <        Thread t = new Thread(new Runnable() {
1596 <                public void run() {
1597 <                    try {
1598 <                        q.put(new Integer(SIZE+1));
1599 <                    } catch (InterruptedException ie){
1600 <                        threadUnexpectedException();
1601 <                    }
1602 <                }
1603 <            });
1604 <        try {
1605 <            t.start();
1606 <            ArrayList l = new ArrayList();
1607 <            q.drainTo(l);
1865 <            assertTrue(l.size() >= SIZE);
1866 <            for (int i = 0; i < SIZE; ++i)
1867 <                assertEquals(l.get(i), new Integer(i));
1868 <            t.join();
1869 <            assertTrue(q.size() + l.size() >= SIZE);
1870 <        } catch(Exception e){
1871 <            unexpectedException();
1872 <        }
1595 >        Thread t = new Thread(new CheckedRunnable() {
1596 >            public void realRun() throws InterruptedException {
1597 >                q.put(new Integer(SIZE+1));
1598 >            }});
1599 >
1600 >        t.start();
1601 >        ArrayList l = new ArrayList();
1602 >        q.drainTo(l);
1603 >        assertTrue(l.size() >= SIZE);
1604 >        for (int i = 0; i < SIZE; ++i)
1605 >            assertEquals(l.get(i), new Integer(i));
1606 >        t.join();
1607 >        assertTrue(q.size() + l.size() >= SIZE);
1608      }
1609  
1610      /**
1611       * drainTo(null, n) throws NPE
1612 <     */
1612 >     */
1613      public void testDrainToNullN() {
1614          LinkedBlockingDeque q = populatedDeque(SIZE);
1615          try {
1616              q.drainTo(null, 0);
1617              shouldThrow();
1618 <        } catch(NullPointerException success) {
1884 <        }
1618 >        } catch (NullPointerException success) {}
1619      }
1620  
1621      /**
1622       * drainTo(this, n) throws IAE
1623 <     */
1623 >     */
1624      public void testDrainToSelfN() {
1625          LinkedBlockingDeque q = populatedDeque(SIZE);
1626          try {
1627              q.drainTo(q, 0);
1628              shouldThrow();
1629 <        } catch(IllegalArgumentException success) {
1896 <        }
1629 >        } catch (IllegalArgumentException success) {}
1630      }
1631  
1632      /**
1633       * drainTo(c, n) empties first max {n, size} elements of deque into c
1634 <     */
1634 >     */
1635      public void testDrainToN() {
1636          LinkedBlockingDeque q = new LinkedBlockingDeque();
1637          for (int i = 0; i < SIZE + 2; ++i) {
1638 <            for(int j = 0; j < SIZE; j++)
1638 >            for (int j = 0; j < SIZE; j++)
1639                  assertTrue(q.offer(new Integer(j)));
1640              ArrayList l = new ArrayList();
1641              q.drainTo(l, i);
1642              int k = (i < SIZE)? i : SIZE;
1643              assertEquals(l.size(), k);
1644              assertEquals(q.size(), SIZE-k);
1645 <            for (int j = 0; j < k; ++j)
1645 >            for (int j = 0; j < k; ++j)
1646                  assertEquals(l.get(j), new Integer(j));
1647              while (q.poll() != null) ;
1648          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines