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.7 by jsr166, Sat Nov 21 02:07:26 2009 UTC vs.
Revision 1.12 by jsr166, Sat Nov 21 10:29:50 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 {
# Line 70 | Line 71 | public class LinkedBlockingDequeTest ext
71              LinkedBlockingDeque q = new LinkedBlockingDeque();
72              q.offerFirst(null);
73              shouldThrow();
74 <        } catch (NullPointerException success) {
74 <        }
74 >        } catch (NullPointerException success) {}
75      }
76  
77      /**
# 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 <        }
188 <        catch (NoSuchElementException success) {}
186 >        } catch (NoSuchElementException success) {}
187          assertNull(q.peekLast());
188      }
189  
# 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 280 | Line 276 | public class LinkedBlockingDequeTest ext
276      }
277  
278      /**
279 <     * Constructor throws IAE if  capacity argument nonpositive
279 >     * Constructor throws IAE if capacity argument nonpositive
280       */
281      public void testConstructor2() {
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      /**
323       * Deque contains all elements of collection used to initialize
324       */
325      public void testConstructor6() {
326 <        try {
327 <            Integer[] ints = new Integer[SIZE];
328 <            for (int i = 0; i < SIZE; ++i)
329 <                ints[i] = new Integer(i);
330 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
331 <            for (int i = 0; i < SIZE; ++i)
340 <                assertEquals(ints[i], q.poll());
341 <        }
342 <        finally {}
326 >        Integer[] ints = new Integer[SIZE];
327 >        for (int i = 0; i < SIZE; ++i)
328 >            ints[i] = new Integer(i);
329 >        LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
330 >        for (int i = 0; i < SIZE; ++i)
331 >            assertEquals(ints[i], q.poll());
332      }
333  
334      /**
# Line 382 | Line 371 | public class LinkedBlockingDequeTest ext
371              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
372              q.offer(null);
373              shouldThrow();
374 <        } catch (NullPointerException success) { }
374 >        } catch (NullPointerException success) {}
375      }
376  
377      /**
# Line 393 | Line 382 | public class LinkedBlockingDequeTest ext
382              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
383              q.add(null);
384              shouldThrow();
385 <        } catch (NullPointerException success) { }
385 >        } catch (NullPointerException success) {}
386      }
387  
388      /**
# Line 404 | Line 393 | public class LinkedBlockingDequeTest ext
393              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
394              q.push(null);
395              shouldThrow();
396 <        } catch (NullPointerException success) { }
396 >        } catch (NullPointerException success) {}
397      }
398  
399      /**
# Line 420 | Line 409 | public class LinkedBlockingDequeTest ext
409              }
410              assertEquals(0, q.remainingCapacity());
411              q.push(new Integer(SIZE));
412 <        } catch (IllegalStateException success) {
413 <        }
412 >            shouldThrow();
413 >        } catch (IllegalStateException success) {}
414      }
415  
416      /**
# Line 446 | Line 435 | public class LinkedBlockingDequeTest ext
435          try {
436              q.pop();
437              shouldThrow();
438 <        } catch (NoSuchElementException success) {
450 <        }
438 >        } catch (NoSuchElementException success) {}
439      }
440  
441  
# Line 471 | Line 459 | public class LinkedBlockingDequeTest ext
459              }
460              assertEquals(0, q.remainingCapacity());
461              q.add(new Integer(SIZE));
462 <        } catch (IllegalStateException success) {
463 <        }
462 >            shouldThrow();
463 >        } catch (IllegalStateException success) {}
464      }
465  
466      /**
# Line 483 | Line 471 | public class LinkedBlockingDequeTest ext
471              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
472              q.addAll(null);
473              shouldThrow();
474 <        }
487 <        catch (NullPointerException success) {}
474 >        } catch (NullPointerException success) {}
475      }
476  
477      /**
# Line 495 | Line 482 | public class LinkedBlockingDequeTest ext
482              LinkedBlockingDeque q = populatedDeque(SIZE);
483              q.addAll(q);
484              shouldThrow();
485 <        }
499 <        catch (IllegalArgumentException success) {}
485 >        } catch (IllegalArgumentException success) {}
486      }
487  
488      /**
# Line 508 | Line 494 | public class LinkedBlockingDequeTest ext
494              Integer[] ints = new Integer[SIZE];
495              q.addAll(Arrays.asList(ints));
496              shouldThrow();
497 <        }
512 <        catch (NullPointerException success) {}
497 >        } catch (NullPointerException success) {}
498      }
499      /**
500       * addAll of a collection with any null elements throws NPE after
# Line 523 | Line 508 | public class LinkedBlockingDequeTest ext
508                  ints[i] = new Integer(i);
509              q.addAll(Arrays.asList(ints));
510              shouldThrow();
511 <        }
527 <        catch (NullPointerException success) {}
511 >        } catch (NullPointerException success) {}
512      }
513      /**
514       * addAll throws ISE if not enough room
# Line 537 | Line 521 | public class LinkedBlockingDequeTest ext
521                  ints[i] = new Integer(i);
522              q.addAll(Arrays.asList(ints));
523              shouldThrow();
524 <        }
541 <        catch (IllegalStateException success) {}
524 >        } catch (IllegalStateException success) {}
525      }
526      /**
527       * Deque contains all elements, in traversal order, of successful addAll
528       */
529      public void testAddAll5() {
530 <        try {
531 <            Integer[] empty = new Integer[0];
532 <            Integer[] ints = new Integer[SIZE];
533 <            for (int i = 0; i < SIZE; ++i)
534 <                ints[i] = new Integer(i);
535 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
536 <            assertFalse(q.addAll(Arrays.asList(empty)));
537 <            assertTrue(q.addAll(Arrays.asList(ints)));
538 <            for (int i = 0; i < SIZE; ++i)
556 <                assertEquals(ints[i], q.poll());
557 <        }
558 <        finally {}
530 >        Integer[] empty = new Integer[0];
531 >        Integer[] ints = new Integer[SIZE];
532 >        for (int i = 0; i < SIZE; ++i)
533 >            ints[i] = new Integer(i);
534 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
535 >        assertFalse(q.addAll(Arrays.asList(empty)));
536 >        assertTrue(q.addAll(Arrays.asList(ints)));
537 >        for (int i = 0; i < SIZE; ++i)
538 >            assertEquals(ints[i], q.poll());
539      }
540  
541  
542      /**
543       * put(null) throws NPE
544       */
545 <     public void testPutNull() {
545 >    public void testPutNull() throws InterruptedException {
546          try {
547              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
548              q.put(null);
549              shouldThrow();
550 <        }
551 <        catch (NullPointerException success) {
572 <        }
573 <        catch (InterruptedException ie) {
574 <            unexpectedException();
575 <        }
576 <     }
550 >        } catch (NullPointerException success) {}
551 >    }
552  
553      /**
554       * all elements successfully put are contained
555       */
556 <     public void testPut() {
557 <         try {
558 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
559 <             for (int i = 0; i < SIZE; ++i) {
560 <                 Integer I = new Integer(i);
561 <                 q.put(I);
587 <                 assertTrue(q.contains(I));
588 <             }
589 <             assertEquals(0, q.remainingCapacity());
590 <         }
591 <        catch (InterruptedException ie) {
592 <            unexpectedException();
556 >    public void testPut() throws InterruptedException {
557 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
558 >        for (int i = 0; i < SIZE; ++i) {
559 >            Integer I = new Integer(i);
560 >            q.put(I);
561 >            assertTrue(q.contains(I));
562          }
563 +        assertEquals(0, q.remainingCapacity());
564      }
565  
566      /**
567       * put blocks interruptibly if full
568       */
569 <    public void testBlockingPut() {
570 <        Thread t = new Thread(new Runnable() {
571 <                public void run() {
572 <                    int added = 0;
573 <                    try {
574 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
575 <                        for (int i = 0; i < SIZE; ++i) {
576 <                            q.put(new Integer(i));
577 <                            ++added;
608 <                        }
609 <                        q.put(new Integer(SIZE));
610 <                        threadShouldThrow();
611 <                    } catch (InterruptedException ie) {
612 <                        threadAssertEquals(added, SIZE);
569 >    public void testBlockingPut() throws InterruptedException {
570 >        Thread t = new Thread(new CheckedRunnable() {
571 >            public void realRun() {
572 >                int added = 0;
573 >                try {
574 >                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
575 >                    for (int i = 0; i < SIZE; ++i) {
576 >                        q.put(new Integer(i));
577 >                        ++added;
578                      }
579 <                }});
579 >                    q.put(new Integer(SIZE));
580 >                    threadShouldThrow();
581 >                } catch (InterruptedException success) {
582 >                    threadAssertEquals(added, SIZE);
583 >                }
584 >            }});
585 >
586          t.start();
587 <        try {
588 <           Thread.sleep(SHORT_DELAY_MS);
589 <           t.interrupt();
619 <           t.join();
620 <        }
621 <        catch (InterruptedException ie) {
622 <            unexpectedException();
623 <        }
587 >        Thread.sleep(SHORT_DELAY_MS);
588 >        t.interrupt();
589 >        t.join();
590      }
591  
592      /**
593       * put blocks waiting for take when full
594       */
595 <    public void testPutWithTake() {
595 >    public void testPutWithTake() throws InterruptedException {
596          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
597 <        Thread t = new Thread(new Runnable() {
598 <                public void run() {
599 <                    int added = 0;
600 <                    try {
601 <                        q.put(new Object());
602 <                        ++added;
603 <                        q.put(new Object());
604 <                        ++added;
605 <                        q.put(new Object());
606 <                        ++added;
607 <                        q.put(new Object());
608 <                        ++added;
609 <                        threadShouldThrow();
610 <                    } catch (InterruptedException e) {
611 <                        threadAssertTrue(added >= 2);
646 <                    }
597 >        Thread t = new Thread(new CheckedRunnable() {
598 >            public void realRun() {
599 >                int added = 0;
600 >                try {
601 >                    q.put(new Object());
602 >                    ++added;
603 >                    q.put(new Object());
604 >                    ++added;
605 >                    q.put(new Object());
606 >                    ++added;
607 >                    q.put(new Object());
608 >                    ++added;
609 >                    threadShouldThrow();
610 >                } catch (InterruptedException success) {
611 >                    threadAssertTrue(added >= 2);
612                  }
613 <            });
614 <        try {
615 <            t.start();
616 <            Thread.sleep(SHORT_DELAY_MS);
617 <            q.take();
618 <            t.interrupt();
619 <            t.join();
655 <        } catch (Exception e) {
656 <            unexpectedException();
657 <        }
613 >            }});
614 >
615 >        t.start();
616 >        Thread.sleep(SHORT_DELAY_MS);
617 >        q.take();
618 >        t.interrupt();
619 >        t.join();
620      }
621  
622      /**
623       * timed offer times out if full and elements not taken
624       */
625 <    public void testTimedOffer() {
625 >    public void testTimedOffer() throws InterruptedException {
626          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
627 <        Thread t = new Thread(new Runnable() {
628 <                public void run() {
629 <                    try {
630 <                        q.put(new Object());
631 <                        q.put(new Object());
632 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
633 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
672 <                        threadShouldThrow();
673 <                    } catch (InterruptedException success) {}
674 <                }
675 <            });
627 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
628 >            public void realRun() throws InterruptedException {
629 >                q.put(new Object());
630 >                q.put(new Object());
631 >                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
632 >                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
633 >            }};
634  
635 <        try {
636 <            t.start();
637 <            Thread.sleep(SMALL_DELAY_MS);
638 <            t.interrupt();
681 <            t.join();
682 <        } catch (Exception e) {
683 <            unexpectedException();
684 <        }
635 >        t.start();
636 >        Thread.sleep(SMALL_DELAY_MS);
637 >        t.interrupt();
638 >        t.join();
639      }
640  
641      /**
642       * take retrieves elements in FIFO order
643       */
644 <    public void testTake() {
645 <        try {
646 <            LinkedBlockingDeque q = populatedDeque(SIZE);
647 <            for (int i = 0; i < SIZE; ++i) {
694 <                assertEquals(i, ((Integer)q.take()).intValue());
695 <            }
696 <        } catch (InterruptedException e) {
697 <            unexpectedException();
644 >    public void testTake() throws InterruptedException {
645 >        LinkedBlockingDeque q = populatedDeque(SIZE);
646 >        for (int i = 0; i < SIZE; ++i) {
647 >            assertEquals(i, ((Integer)q.take()).intValue());
648          }
649      }
650  
651      /**
652       * take blocks interruptibly when empty
653       */
654 <    public void testTakeFromEmpty() {
654 >    public void testTakeFromEmpty() throws InterruptedException {
655          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
656 <        Thread t = new Thread(new Runnable() {
657 <                public void run() {
658 <                    try {
659 <                        q.take();
660 <                        threadShouldThrow();
661 <                    } catch (InterruptedException success) { }
662 <                }
663 <            });
664 <        try {
715 <            t.start();
716 <            Thread.sleep(SHORT_DELAY_MS);
717 <            t.interrupt();
718 <            t.join();
719 <        } catch (Exception e) {
720 <            unexpectedException();
721 <        }
656 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
657 >            public void realRun() throws InterruptedException {
658 >                q.take();
659 >            }};
660 >
661 >        t.start();
662 >        Thread.sleep(SHORT_DELAY_MS);
663 >        t.interrupt();
664 >        t.join();
665      }
666  
667      /**
668       * Take removes existing elements until empty, then blocks interruptibly
669       */
670 <    public void testBlockingTake() {
671 <        Thread t = new Thread(new Runnable() {
672 <                public void run() {
673 <                    try {
674 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
675 <                        for (int i = 0; i < SIZE; ++i) {
676 <                            assertEquals(i, ((Integer)q.take()).intValue());
677 <                        }
678 <                        q.take();
679 <                        threadShouldThrow();
737 <                    } catch (InterruptedException success) {
738 <                    }
739 <                }});
670 >    public void testBlockingTake() throws InterruptedException {
671 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
672 >            public void realRun() throws InterruptedException {
673 >                LinkedBlockingDeque q = populatedDeque(SIZE);
674 >                for (int i = 0; i < SIZE; ++i) {
675 >                    assertEquals(i, ((Integer)q.take()).intValue());
676 >                }
677 >                q.take();
678 >            }};
679 >
680          t.start();
681 <        try {
682 <           Thread.sleep(SHORT_DELAY_MS);
683 <           t.interrupt();
744 <           t.join();
745 <        }
746 <        catch (InterruptedException ie) {
747 <            unexpectedException();
748 <        }
681 >        Thread.sleep(SHORT_DELAY_MS);
682 >        t.interrupt();
683 >        t.join();
684      }
685  
686  
# Line 763 | Line 698 | public class LinkedBlockingDequeTest ext
698      /**
699       * timed poll with zero timeout succeeds when non-empty, else times out
700       */
701 <    public void testTimedPoll0() {
702 <        try {
703 <            LinkedBlockingDeque q = populatedDeque(SIZE);
704 <            for (int i = 0; i < SIZE; ++i) {
770 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
771 <            }
772 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
773 <        } catch (InterruptedException e) {
774 <            unexpectedException();
701 >    public void testTimedPoll0() throws InterruptedException {
702 >        LinkedBlockingDeque q = populatedDeque(SIZE);
703 >        for (int i = 0; i < SIZE; ++i) {
704 >            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
705          }
706 +        assertNull(q.poll(0, MILLISECONDS));
707      }
708  
709      /**
710       * timed poll with nonzero timeout succeeds when non-empty, else times out
711       */
712 <    public void testTimedPoll() {
713 <        try {
714 <            LinkedBlockingDeque q = populatedDeque(SIZE);
715 <            for (int i = 0; i < SIZE; ++i) {
785 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
786 <            }
787 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
788 <        } catch (InterruptedException e) {
789 <            unexpectedException();
712 >    public void testTimedPoll() throws InterruptedException {
713 >        LinkedBlockingDeque q = populatedDeque(SIZE);
714 >        for (int i = 0; i < SIZE; ++i) {
715 >            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
716          }
717 +        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
718      }
719  
720      /**
721       * Interrupted timed poll throws InterruptedException instead of
722       * returning timeout status
723       */
724 <    public void testInterruptedTimedPoll() {
725 <        Thread t = new Thread(new Runnable() {
726 <                public void run() {
727 <                    try {
728 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
729 <                        for (int i = 0; i < SIZE; ++i) {
730 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
731 <                        }
732 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
733 <                    } catch (InterruptedException success) {
807 <                    }
808 <                }});
724 >    public void testInterruptedTimedPoll() throws InterruptedException {
725 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
726 >            public void realRun() throws InterruptedException {
727 >                LinkedBlockingDeque q = populatedDeque(SIZE);
728 >                for (int i = 0; i < SIZE; ++i) {
729 >                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
730 >                }
731 >                q.poll(SMALL_DELAY_MS, MILLISECONDS);
732 >            }};
733 >
734          t.start();
735 <        try {
736 <           Thread.sleep(SHORT_DELAY_MS);
737 <           t.interrupt();
813 <           t.join();
814 <        }
815 <        catch (InterruptedException ie) {
816 <            unexpectedException();
817 <        }
735 >        Thread.sleep(SHORT_DELAY_MS);
736 >        t.interrupt();
737 >        t.join();
738      }
739  
740      /**
741       *  timed poll before a delayed offer fails; after offer succeeds;
742       *  on interruption throws
743       */
744 <    public void testTimedPollWithOffer() {
744 >    public void testTimedPollWithOffer() throws InterruptedException {
745          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
746 <        Thread t = new Thread(new Runnable() {
747 <                public void run() {
748 <                    try {
749 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
750 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
751 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
752 <                        threadShouldThrow();
753 <                    } catch (InterruptedException success) { }
754 <                }
755 <            });
756 <        try {
757 <            t.start();
838 <            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 <        }
746 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
747 >            public void realRun() throws InterruptedException {
748 >                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
749 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
750 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
751 >            }};
752 >
753 >        t.start();
754 >        Thread.sleep(SMALL_DELAY_MS);
755 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
756 >        t.interrupt();
757 >        t.join();
758      }
759  
760  
761      /**
762       * putFirst(null) throws NPE
763       */
764 <     public void testPutFirstNull() {
764 >     public void testPutFirstNull() throws InterruptedException {
765          try {
766              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
767              q.putFirst(null);
768              shouldThrow();
769 <        }
857 <        catch (NullPointerException success) {
858 <        }
859 <        catch (InterruptedException ie) {
860 <            unexpectedException();
861 <        }
769 >        } catch (NullPointerException success) {}
770       }
771  
772      /**
773       * all elements successfully putFirst are contained
774       */
775 <     public void testPutFirst() {
776 <         try {
777 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
778 <             for (int i = 0; i < SIZE; ++i) {
779 <                 Integer I = new Integer(i);
780 <                 q.putFirst(I);
873 <                 assertTrue(q.contains(I));
874 <             }
875 <             assertEquals(0, q.remainingCapacity());
775 >     public void testPutFirst() throws InterruptedException {
776 >         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
777 >         for (int i = 0; i < SIZE; ++i) {
778 >             Integer I = new Integer(i);
779 >             q.putFirst(I);
780 >             assertTrue(q.contains(I));
781           }
782 <        catch (InterruptedException ie) {
878 <            unexpectedException();
879 <        }
782 >         assertEquals(0, q.remainingCapacity());
783      }
784  
785      /**
786       * putFirst blocks interruptibly if full
787       */
788 <    public void testBlockingPutFirst() {
789 <        Thread t = new Thread(new Runnable() {
790 <                public void run() {
791 <                    int added = 0;
792 <                    try {
793 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
794 <                        for (int i = 0; i < SIZE; ++i) {
795 <                            q.putFirst(new Integer(i));
796 <                            ++added;
894 <                        }
895 <                        q.putFirst(new Integer(SIZE));
896 <                        threadShouldThrow();
897 <                    } catch (InterruptedException ie) {
898 <                        threadAssertEquals(added, SIZE);
788 >    public void testBlockingPutFirst() throws InterruptedException {
789 >        Thread t = new Thread(new CheckedRunnable() {
790 >            public void realRun() {
791 >                int added = 0;
792 >                try {
793 >                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
794 >                    for (int i = 0; i < SIZE; ++i) {
795 >                        q.putFirst(new Integer(i));
796 >                        ++added;
797                      }
798 <                }});
798 >                    q.putFirst(new Integer(SIZE));
799 >                    threadShouldThrow();
800 >                } catch (InterruptedException success) {
801 >                    threadAssertEquals(added, SIZE);
802 >                }
803 >            }});
804 >
805          t.start();
806 <        try {
807 <           Thread.sleep(SHORT_DELAY_MS);
808 <           t.interrupt();
905 <           t.join();
906 <        }
907 <        catch (InterruptedException ie) {
908 <            unexpectedException();
909 <        }
806 >        Thread.sleep(SHORT_DELAY_MS);
807 >        t.interrupt();
808 >        t.join();
809      }
810  
811      /**
812       * putFirst blocks waiting for take when full
813       */
814 <    public void testPutFirstWithTake() {
814 >    public void testPutFirstWithTake() throws InterruptedException {
815          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
816 <        Thread t = new Thread(new Runnable() {
817 <                public void run() {
818 <                    int added = 0;
819 <                    try {
820 <                        q.putFirst(new Object());
821 <                        ++added;
822 <                        q.putFirst(new Object());
823 <                        ++added;
824 <                        q.putFirst(new Object());
825 <                        ++added;
826 <                        q.putFirst(new Object());
827 <                        ++added;
828 <                        threadShouldThrow();
829 <                    } catch (InterruptedException e) {
830 <                        threadAssertTrue(added >= 2);
932 <                    }
816 >        Thread t = new Thread(new CheckedRunnable() {
817 >            public void realRun() {
818 >                int added = 0;
819 >                try {
820 >                    q.putFirst(new Object());
821 >                    ++added;
822 >                    q.putFirst(new Object());
823 >                    ++added;
824 >                    q.putFirst(new Object());
825 >                    ++added;
826 >                    q.putFirst(new Object());
827 >                    ++added;
828 >                    threadShouldThrow();
829 >                } catch (InterruptedException success) {
830 >                    threadAssertTrue(added >= 2);
831                  }
832 <            });
833 <        try {
834 <            t.start();
835 <            Thread.sleep(SHORT_DELAY_MS);
836 <            q.take();
837 <            t.interrupt();
838 <            t.join();
941 <        } catch (Exception e) {
942 <            unexpectedException();
943 <        }
832 >            }});
833 >
834 >        t.start();
835 >        Thread.sleep(SHORT_DELAY_MS);
836 >        q.take();
837 >        t.interrupt();
838 >        t.join();
839      }
840  
841      /**
842       * timed offerFirst times out if full and elements not taken
843       */
844 <    public void testTimedOfferFirst() {
844 >    public void testTimedOfferFirst() throws InterruptedException {
845          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
846 <        Thread t = new Thread(new Runnable() {
847 <                public void run() {
848 <                    try {
849 <                        q.putFirst(new Object());
850 <                        q.putFirst(new Object());
851 <                        threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
852 <                        q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
958 <                        threadShouldThrow();
959 <                    } catch (InterruptedException success) {}
960 <                }
961 <            });
846 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
847 >            public void realRun() throws InterruptedException {
848 >                q.putFirst(new Object());
849 >                q.putFirst(new Object());
850 >                threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
851 >                q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
852 >            }};
853  
854 <        try {
855 <            t.start();
856 <            Thread.sleep(SMALL_DELAY_MS);
857 <            t.interrupt();
967 <            t.join();
968 <        } catch (Exception e) {
969 <            unexpectedException();
970 <        }
854 >        t.start();
855 >        Thread.sleep(SMALL_DELAY_MS);
856 >        t.interrupt();
857 >        t.join();
858      }
859  
860      /**
861       * take retrieves elements in FIFO order
862       */
863 <    public void testTakeFirst() {
864 <        try {
865 <            LinkedBlockingDeque q = populatedDeque(SIZE);
866 <            for (int i = 0; i < SIZE; ++i) {
980 <                assertEquals(i, ((Integer)q.takeFirst()).intValue());
981 <            }
982 <        } catch (InterruptedException e) {
983 <            unexpectedException();
863 >    public void testTakeFirst() throws InterruptedException {
864 >        LinkedBlockingDeque q = populatedDeque(SIZE);
865 >        for (int i = 0; i < SIZE; ++i) {
866 >            assertEquals(i, ((Integer)q.takeFirst()).intValue());
867          }
868      }
869  
870      /**
871       * takeFirst blocks interruptibly when empty
872       */
873 <    public void testTakeFirstFromEmpty() {
873 >    public void testTakeFirstFromEmpty() throws InterruptedException {
874          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
875 <        Thread t = new Thread(new Runnable() {
876 <                public void run() {
877 <                    try {
878 <                        q.takeFirst();
879 <                        threadShouldThrow();
880 <                    } catch (InterruptedException success) { }
881 <                }
882 <            });
883 <        try {
1001 <            t.start();
1002 <            Thread.sleep(SHORT_DELAY_MS);
1003 <            t.interrupt();
1004 <            t.join();
1005 <        } catch (Exception e) {
1006 <            unexpectedException();
1007 <        }
875 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
876 >            public void realRun() throws InterruptedException {
877 >                q.takeFirst();
878 >            }};
879 >
880 >        t.start();
881 >        Thread.sleep(SHORT_DELAY_MS);
882 >        t.interrupt();
883 >        t.join();
884      }
885  
886      /**
887       * TakeFirst removes existing elements until empty, then blocks interruptibly
888       */
889 <    public void testBlockingTakeFirst() {
890 <        Thread t = new Thread(new Runnable() {
891 <                public void run() {
892 <                    try {
893 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
894 <                        for (int i = 0; i < SIZE; ++i) {
895 <                            assertEquals(i, ((Integer)q.takeFirst()).intValue());
896 <                        }
897 <                        q.takeFirst();
898 <                        threadShouldThrow();
1023 <                    } catch (InterruptedException success) {
1024 <                    }
1025 <                }});
889 >    public void testBlockingTakeFirst() throws InterruptedException {
890 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
891 >            public void realRun() throws InterruptedException {
892 >                LinkedBlockingDeque q = populatedDeque(SIZE);
893 >                for (int i = 0; i < SIZE; ++i) {
894 >                    assertEquals(i, ((Integer)q.takeFirst()).intValue());
895 >                }
896 >                q.takeFirst();
897 >            }};
898 >
899          t.start();
900 <        try {
901 <           Thread.sleep(SHORT_DELAY_MS);
902 <           t.interrupt();
1030 <           t.join();
1031 <        }
1032 <        catch (InterruptedException ie) {
1033 <            unexpectedException();
1034 <        }
900 >        Thread.sleep(SHORT_DELAY_MS);
901 >        t.interrupt();
902 >        t.join();
903      }
904  
905  
906      /**
907       * timed pollFirst with zero timeout succeeds when non-empty, else times out
908       */
909 <    public void testTimedPollFirst0() {
910 <        try {
911 <            LinkedBlockingDeque q = populatedDeque(SIZE);
912 <            for (int i = 0; i < SIZE; ++i) {
1045 <                assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue());
1046 <            }
1047 <            assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS));
1048 <        } catch (InterruptedException e) {
1049 <            unexpectedException();
909 >    public void testTimedPollFirst0() throws InterruptedException {
910 >        LinkedBlockingDeque q = populatedDeque(SIZE);
911 >        for (int i = 0; i < SIZE; ++i) {
912 >            assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
913          }
914 +        assertNull(q.pollFirst(0, MILLISECONDS));
915      }
916  
917      /**
918       * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
919       */
920 <    public void testTimedPollFirst() {
921 <        try {
922 <            LinkedBlockingDeque q = populatedDeque(SIZE);
923 <            for (int i = 0; i < SIZE; ++i) {
1060 <                assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1061 <            }
1062 <            assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1063 <        } catch (InterruptedException e) {
1064 <            unexpectedException();
920 >    public void testTimedPollFirst() throws InterruptedException {
921 >        LinkedBlockingDeque q = populatedDeque(SIZE);
922 >        for (int i = 0; i < SIZE; ++i) {
923 >            assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
924          }
925 +        assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
926      }
927  
928      /**
929       * Interrupted timed pollFirst throws InterruptedException instead of
930       * returning timeout status
931       */
932 <    public void testInterruptedTimedPollFirst() {
933 <        Thread t = new Thread(new Runnable() {
934 <                public void run() {
935 <                    try {
936 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
937 <                        for (int i = 0; i < SIZE; ++i) {
938 <                            threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
939 <                        }
940 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
941 <                    } catch (InterruptedException success) {
1082 <                    }
1083 <                }});
932 >    public void testInterruptedTimedPollFirst() throws InterruptedException {
933 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
934 >            public void realRun() throws InterruptedException {
935 >                LinkedBlockingDeque q = populatedDeque(SIZE);
936 >                for (int i = 0; i < SIZE; ++i) {
937 >                    threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
938 >                }
939 >                q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
940 >            }};
941 >
942          t.start();
943 <        try {
944 <           Thread.sleep(SHORT_DELAY_MS);
945 <           t.interrupt();
1088 <           t.join();
1089 <        }
1090 <        catch (InterruptedException ie) {
1091 <            unexpectedException();
1092 <        }
943 >        Thread.sleep(SHORT_DELAY_MS);
944 >        t.interrupt();
945 >        t.join();
946      }
947  
948      /**
949       *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
950       *  on interruption throws
951       */
952 <    public void testTimedPollFirstWithOfferFirst() {
952 >    public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
953          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
954 <        Thread t = new Thread(new Runnable() {
955 <                public void run() {
956 <                    try {
957 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
958 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
959 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
960 <                        threadShouldThrow();
961 <                    } catch (InterruptedException success) { }
962 <                }
963 <            });
964 <        try {
965 <            t.start();
1113 <            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 <        }
954 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
955 >            public void realRun() throws InterruptedException {
956 >                threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
957 >                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
958 >                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
959 >            }};
960 >
961 >        t.start();
962 >        Thread.sleep(SMALL_DELAY_MS);
963 >        assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
964 >        t.interrupt();
965 >        t.join();
966      }
967  
968      /**
969       * putLast(null) throws NPE
970       */
971 <     public void testPutLastNull() {
971 >     public void testPutLastNull() throws InterruptedException {
972          try {
973              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
974              q.putLast(null);
975              shouldThrow();
976 <        }
1131 <        catch (NullPointerException success) {
1132 <        }
1133 <        catch (InterruptedException ie) {
1134 <            unexpectedException();
1135 <        }
976 >        } catch (NullPointerException success) {}
977       }
978  
979      /**
980       * all elements successfully putLast are contained
981       */
982 <     public void testPutLast() {
983 <         try {
984 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
985 <             for (int i = 0; i < SIZE; ++i) {
986 <                 Integer I = new Integer(i);
987 <                 q.putLast(I);
1147 <                 assertTrue(q.contains(I));
1148 <             }
1149 <             assertEquals(0, q.remainingCapacity());
982 >     public void testPutLast() throws InterruptedException {
983 >         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
984 >         for (int i = 0; i < SIZE; ++i) {
985 >             Integer I = new Integer(i);
986 >             q.putLast(I);
987 >             assertTrue(q.contains(I));
988           }
989 <        catch (InterruptedException ie) {
1152 <            unexpectedException();
1153 <        }
989 >         assertEquals(0, q.remainingCapacity());
990      }
991  
992      /**
993       * putLast blocks interruptibly if full
994       */
995 <    public void testBlockingPutLast() {
996 <        Thread t = new Thread(new Runnable() {
997 <                public void run() {
998 <                    int added = 0;
999 <                    try {
1000 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1001 <                        for (int i = 0; i < SIZE; ++i) {
1002 <                            q.putLast(new Integer(i));
1003 <                            ++added;
1168 <                        }
1169 <                        q.putLast(new Integer(SIZE));
1170 <                        threadShouldThrow();
1171 <                    } catch (InterruptedException ie) {
1172 <                        threadAssertEquals(added, SIZE);
995 >    public void testBlockingPutLast() throws InterruptedException {
996 >        Thread t = new Thread(new CheckedRunnable() {
997 >            public void realRun() {
998 >                int added = 0;
999 >                try {
1000 >                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1001 >                    for (int i = 0; i < SIZE; ++i) {
1002 >                        q.putLast(new Integer(i));
1003 >                        ++added;
1004                      }
1005 <                }});
1005 >                    q.putLast(new Integer(SIZE));
1006 >                    threadShouldThrow();
1007 >                } catch (InterruptedException success) {
1008 >                    threadAssertEquals(added, SIZE);
1009 >                }
1010 >            }});
1011 >
1012          t.start();
1013 <        try {
1014 <           Thread.sleep(SHORT_DELAY_MS);
1015 <           t.interrupt();
1179 <           t.join();
1180 <        }
1181 <        catch (InterruptedException ie) {
1182 <            unexpectedException();
1183 <        }
1013 >        Thread.sleep(SHORT_DELAY_MS);
1014 >        t.interrupt();
1015 >        t.join();
1016      }
1017  
1018      /**
1019       * putLast blocks waiting for take when full
1020       */
1021 <    public void testPutLastWithTake() {
1021 >    public void testPutLastWithTake() throws InterruptedException {
1022          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1023 <        Thread t = new Thread(new Runnable() {
1024 <                public void run() {
1025 <                    int added = 0;
1026 <                    try {
1027 <                        q.putLast(new Object());
1028 <                        ++added;
1029 <                        q.putLast(new Object());
1030 <                        ++added;
1031 <                        q.putLast(new Object());
1032 <                        ++added;
1033 <                        q.putLast(new Object());
1034 <                        ++added;
1035 <                        threadShouldThrow();
1036 <                    } catch (InterruptedException e) {
1037 <                        threadAssertTrue(added >= 2);
1206 <                    }
1023 >        Thread t = new Thread(new CheckedRunnable() {
1024 >            public void realRun() {
1025 >                int added = 0;
1026 >                try {
1027 >                    q.putLast(new Object());
1028 >                    ++added;
1029 >                    q.putLast(new Object());
1030 >                    ++added;
1031 >                    q.putLast(new Object());
1032 >                    ++added;
1033 >                    q.putLast(new Object());
1034 >                    ++added;
1035 >                    threadShouldThrow();
1036 >                } catch (InterruptedException success) {
1037 >                    threadAssertTrue(added >= 2);
1038                  }
1039 <            });
1040 <        try {
1041 <            t.start();
1042 <            Thread.sleep(SHORT_DELAY_MS);
1043 <            q.take();
1044 <            t.interrupt();
1045 <            t.join();
1215 <        } catch (Exception e) {
1216 <            unexpectedException();
1217 <        }
1039 >            }});
1040 >
1041 >        t.start();
1042 >        Thread.sleep(SHORT_DELAY_MS);
1043 >        q.take();
1044 >        t.interrupt();
1045 >        t.join();
1046      }
1047  
1048      /**
1049       * timed offerLast times out if full and elements not taken
1050       */
1051 <    public void testTimedOfferLast() {
1051 >    public void testTimedOfferLast() throws InterruptedException {
1052          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1053 <        Thread t = new Thread(new Runnable() {
1054 <                public void run() {
1055 <                    try {
1056 <                        q.putLast(new Object());
1057 <                        q.putLast(new Object());
1058 <                        threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1059 <                        q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1232 <                        threadShouldThrow();
1233 <                    } catch (InterruptedException success) {}
1234 <                }
1235 <            });
1053 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1054 >            public void realRun() throws InterruptedException {
1055 >                q.putLast(new Object());
1056 >                q.putLast(new Object());
1057 >                threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1058 >                q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1059 >            }};
1060  
1061 <        try {
1062 <            t.start();
1063 <            Thread.sleep(SMALL_DELAY_MS);
1064 <            t.interrupt();
1241 <            t.join();
1242 <        } catch (Exception e) {
1243 <            unexpectedException();
1244 <        }
1061 >        t.start();
1062 >        Thread.sleep(SMALL_DELAY_MS);
1063 >        t.interrupt();
1064 >        t.join();
1065      }
1066  
1067      /**
1068       * takeLast retrieves elements in FIFO order
1069       */
1070 <    public void testTakeLast() {
1071 <        try {
1072 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1073 <            for (int i = 0; i < SIZE; ++i) {
1254 <                assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1255 <            }
1256 <        } catch (InterruptedException e) {
1257 <            unexpectedException();
1070 >    public void testTakeLast() throws InterruptedException {
1071 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1072 >        for (int i = 0; i < SIZE; ++i) {
1073 >            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1074          }
1075      }
1076  
1077      /**
1078       * takeLast blocks interruptibly when empty
1079       */
1080 <    public void testTakeLastFromEmpty() {
1080 >    public void testTakeLastFromEmpty() throws InterruptedException {
1081          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1082 <        Thread t = new Thread(new Runnable() {
1083 <                public void run() {
1084 <                    try {
1085 <                        q.takeLast();
1086 <                        threadShouldThrow();
1087 <                    } catch (InterruptedException success) { }
1088 <                }
1089 <            });
1090 <        try {
1275 <            t.start();
1276 <            Thread.sleep(SHORT_DELAY_MS);
1277 <            t.interrupt();
1278 <            t.join();
1279 <        } catch (Exception e) {
1280 <            unexpectedException();
1281 <        }
1082 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1083 >            public void realRun() throws InterruptedException {
1084 >                q.takeLast();
1085 >            }};
1086 >
1087 >        t.start();
1088 >        Thread.sleep(SHORT_DELAY_MS);
1089 >        t.interrupt();
1090 >        t.join();
1091      }
1092  
1093      /**
1094       * TakeLast removes existing elements until empty, then blocks interruptibly
1095       */
1096 <    public void testBlockingTakeLast() {
1097 <        Thread t = new Thread(new Runnable() {
1098 <                public void run() {
1099 <                    try {
1100 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1101 <                        for (int i = 0; i < SIZE; ++i) {
1102 <                            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1103 <                        }
1104 <                        q.takeLast();
1105 <                        threadShouldThrow();
1297 <                    } catch (InterruptedException success) {
1298 <                    }
1299 <                }});
1096 >    public void testBlockingTakeLast() throws InterruptedException {
1097 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1098 >            public void realRun() throws InterruptedException {
1099 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1100 >                for (int i = 0; i < SIZE; ++i) {
1101 >                    assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1102 >                }
1103 >                q.takeLast();
1104 >            }};
1105 >
1106          t.start();
1107 <        try {
1108 <           Thread.sleep(SHORT_DELAY_MS);
1109 <           t.interrupt();
1304 <           t.join();
1305 <        }
1306 <        catch (InterruptedException ie) {
1307 <            unexpectedException();
1308 <        }
1107 >        Thread.sleep(SHORT_DELAY_MS);
1108 >        t.interrupt();
1109 >        t.join();
1110      }
1111  
1112  
1113      /**
1114       * timed pollLast with zero timeout succeeds when non-empty, else times out
1115       */
1116 <    public void testTimedPollLast0() {
1117 <        try {
1118 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1119 <            for (int i = 0; i < SIZE; ++i) {
1319 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue());
1320 <            }
1321 <            assertNull(q.pollLast(0, TimeUnit.MILLISECONDS));
1322 <        } catch (InterruptedException e) {
1323 <            unexpectedException();
1116 >    public void testTimedPollLast0() throws InterruptedException {
1117 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1118 >        for (int i = 0; i < SIZE; ++i) {
1119 >            assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1120          }
1121 +        assertNull(q.pollLast(0, MILLISECONDS));
1122      }
1123  
1124      /**
1125       * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1126       */
1127 <    public void testTimedPollLast() {
1128 <        try {
1129 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1130 <            for (int i = 0; i < SIZE; ++i) {
1334 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1335 <            }
1336 <            assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1337 <        } catch (InterruptedException e) {
1338 <            unexpectedException();
1127 >    public void testTimedPollLast() throws InterruptedException {
1128 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1129 >        for (int i = 0; i < SIZE; ++i) {
1130 >            assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1131          }
1132 +        assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1133      }
1134  
1135      /**
1136       * Interrupted timed pollLast throws InterruptedException instead of
1137       * returning timeout status
1138       */
1139 <    public void testInterruptedTimedPollLast() {
1140 <        Thread t = new Thread(new Runnable() {
1141 <                public void run() {
1142 <                    try {
1143 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1144 <                        for (int i = 0; i < SIZE; ++i) {
1145 <                            threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1146 <                        }
1147 <                        threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1148 <                    } catch (InterruptedException success) {
1356 <                    }
1357 <                }});
1139 >    public void testInterruptedTimedPollLast() throws InterruptedException {
1140 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1141 >            public void realRun() throws InterruptedException {
1142 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1143 >                for (int i = 0; i < SIZE; ++i) {
1144 >                    threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1145 >                }
1146 >                q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1147 >            }};
1148 >
1149          t.start();
1150 <        try {
1151 <           Thread.sleep(SHORT_DELAY_MS);
1152 <           t.interrupt();
1362 <           t.join();
1363 <        }
1364 <        catch (InterruptedException ie) {
1365 <            unexpectedException();
1366 <        }
1150 >        Thread.sleep(SHORT_DELAY_MS);
1151 >        t.interrupt();
1152 >        t.join();
1153      }
1154  
1155      /**
1156       *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1157       *  on interruption throws
1158       */
1159 <    public void testTimedPollWithOfferLast() {
1159 >    public void testTimedPollWithOfferLast() throws InterruptedException {
1160          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1161 <        Thread t = new Thread(new Runnable() {
1162 <                public void run() {
1163 <                    try {
1164 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1165 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1166 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1167 <                        threadShouldThrow();
1168 <                    } catch (InterruptedException success) { }
1169 <                }
1170 <            });
1171 <        try {
1172 <            t.start();
1173 <            Thread.sleep(SMALL_DELAY_MS);
1174 <            assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1175 <            t.interrupt();
1390 <            t.join();
1391 <        } catch (Exception e) {
1392 <            unexpectedException();
1393 <        }
1161 >        Thread t = new Thread(new CheckedRunnable() {
1162 >            public void realRun() throws InterruptedException {
1163 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1164 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1165 >                try {
1166 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1167 >                    shouldThrow();
1168 >                } catch (InterruptedException success) {}
1169 >            }});
1170 >
1171 >        t.start();
1172 >        Thread.sleep(SMALL_DELAY_MS);
1173 >        assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1174 >        t.interrupt();
1175 >        t.join();
1176      }
1177  
1178  
# Line 1406 | Line 1188 | public class LinkedBlockingDequeTest ext
1188          try {
1189              q.element();
1190              shouldThrow();
1191 <        }
1410 <        catch (NoSuchElementException success) {}
1191 >        } catch (NoSuchElementException success) {}
1192      }
1193  
1194      /**
# Line 1505 | Line 1286 | public class LinkedBlockingDequeTest ext
1286      /**
1287       * toArray contains all elements
1288       */
1289 <    public void testToArray() {
1289 >    public void testToArray() throws InterruptedException{
1290          LinkedBlockingDeque q = populatedDeque(SIZE);
1291          Object[] o = q.toArray();
1511        try {
1292          for (int i = 0; i < o.length; i++)
1293              assertEquals(o[i], q.take());
1514        } catch (InterruptedException e) {
1515            unexpectedException();
1516        }
1294      }
1295  
1296      /**
1297       * toArray(a) contains all elements
1298       */
1299 <    public void testToArray2() {
1299 >    public void testToArray2() throws InterruptedException {
1300          LinkedBlockingDeque q = populatedDeque(SIZE);
1301          Integer[] ints = new Integer[SIZE];
1302          ints = (Integer[])q.toArray(ints);
1303 <        try {
1304 <            for (int i = 0; i < ints.length; i++)
1528 <                assertEquals(ints[i], q.take());
1529 <        } catch (InterruptedException e) {
1530 <            unexpectedException();
1531 <        }
1303 >        for (int i = 0; i < ints.length; i++)
1304 >            assertEquals(ints[i], q.take());
1305      }
1306  
1307      /**
# Line 1550 | Line 1323 | public class LinkedBlockingDequeTest ext
1323              LinkedBlockingDeque q = populatedDeque(SIZE);
1324              Object o[] = q.toArray(new String[10] );
1325              shouldThrow();
1326 <        } catch (ArrayStoreException  success) {}
1326 >        } catch (ArrayStoreException success) {}
1327      }
1328  
1329  
1330      /**
1331       * iterator iterates through all elements
1332       */
1333 <    public void testIterator() {
1333 >    public void testIterator() throws InterruptedException {
1334          LinkedBlockingDeque q = populatedDeque(SIZE);
1335          Iterator it = q.iterator();
1336 <        try {
1337 <            while (it.hasNext()) {
1565 <                assertEquals(it.next(), q.take());
1566 <            }
1567 <        } catch (InterruptedException e) {
1568 <            unexpectedException();
1336 >        while (it.hasNext()) {
1337 >            assertEquals(it.next(), q.take());
1338          }
1339      }
1340  
# Line 1614 | Line 1383 | public class LinkedBlockingDequeTest ext
1383          q.add(one);
1384          q.add(two);
1385          q.add(three);
1386 <        try {
1387 <            for (Iterator it = q.iterator(); it.hasNext();) {
1388 <                q.remove();
1620 <                it.next();
1621 <            }
1622 <        }
1623 <        catch (ConcurrentModificationException e) {
1624 <            unexpectedException();
1386 >        for (Iterator it = q.iterator(); it.hasNext();) {
1387 >            q.remove();
1388 >            it.next();
1389          }
1390          assertEquals(0, q.size());
1391      }
# Line 1642 | Line 1406 | public class LinkedBlockingDequeTest ext
1406          assertFalse(it.hasNext());
1407          try {
1408              it.next();
1409 <        } catch (NoSuchElementException success) {
1410 <        }
1409 >            shouldThrow();
1410 >        } catch (NoSuchElementException success) {}
1411      }
1412  
1413      /**
# Line 1711 | Line 1475 | public class LinkedBlockingDequeTest ext
1475          q.add(one);
1476          q.add(two);
1477          ExecutorService executor = Executors.newFixedThreadPool(2);
1478 <        executor.execute(new Runnable() {
1479 <            public void run() {
1478 >        executor.execute(new CheckedRunnable() {
1479 >            public void realRun() throws InterruptedException {
1480                  threadAssertFalse(q.offer(three));
1481 <                try {
1482 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1483 <                    threadAssertEquals(0, q.remainingCapacity());
1484 <                }
1485 <                catch (InterruptedException e) {
1486 <                    threadUnexpectedException();
1487 <                }
1488 <            }
1489 <        });
1726 <
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 <        });
1481 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1482 >                threadAssertEquals(0, q.remainingCapacity());
1483 >            }});
1484 >
1485 >        executor.execute(new CheckedRunnable() {
1486 >            public void realRun() throws InterruptedException {
1487 >                Thread.sleep(SMALL_DELAY_MS);
1488 >                threadAssertEquals(one, q.take());
1489 >            }});
1490  
1491          joinPool(executor);
1492      }
# Line 1745 | Line 1497 | public class LinkedBlockingDequeTest ext
1497      public void testPollInExecutor() {
1498          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1499          ExecutorService executor = Executors.newFixedThreadPool(2);
1500 <        executor.execute(new Runnable() {
1501 <            public void run() {
1500 >        executor.execute(new CheckedRunnable() {
1501 >            public void realRun() throws InterruptedException {
1502                  threadAssertNull(q.poll());
1503 <                try {
1504 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1505 <                    threadAssertTrue(q.isEmpty());
1506 <                }
1507 <                catch (InterruptedException e) {
1508 <                    threadUnexpectedException();
1509 <                }
1510 <            }
1511 <        });
1760 <
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 <        });
1503 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1504 >                threadAssertTrue(q.isEmpty());
1505 >            }});
1506 >
1507 >        executor.execute(new CheckedRunnable() {
1508 >            public void realRun() throws InterruptedException {
1509 >                Thread.sleep(SMALL_DELAY_MS);
1510 >                q.put(one);
1511 >            }});
1512  
1513          joinPool(executor);
1514      }
# Line 1776 | Line 1516 | public class LinkedBlockingDequeTest ext
1516      /**
1517       * A deserialized serialized deque has same elements in same order
1518       */
1519 <    public void testSerialization() {
1519 >    public void testSerialization() throws Exception {
1520          LinkedBlockingDeque q = populatedDeque(SIZE);
1521  
1522 <        try {
1523 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1524 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1525 <            out.writeObject(q);
1526 <            out.close();
1527 <
1528 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1529 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1530 <            LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1531 <            assertEquals(q.size(), r.size());
1532 <            while (!q.isEmpty())
1793 <                assertEquals(q.remove(), r.remove());
1794 <        } catch (Exception e) {
1795 <            unexpectedException();
1796 <        }
1522 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1523 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1524 >        out.writeObject(q);
1525 >        out.close();
1526 >
1527 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1528 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1529 >        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1530 >        assertEquals(q.size(), r.size());
1531 >        while (!q.isEmpty())
1532 >            assertEquals(q.remove(), r.remove());
1533      }
1534  
1535      /**
# Line 1804 | Line 1540 | public class LinkedBlockingDequeTest ext
1540          try {
1541              q.drainTo(null);
1542              shouldThrow();
1543 <        } catch (NullPointerException success) {
1808 <        }
1543 >        } catch (NullPointerException success) {}
1544      }
1545  
1546      /**
# Line 1816 | Line 1551 | public class LinkedBlockingDequeTest ext
1551          try {
1552              q.drainTo(q);
1553              shouldThrow();
1554 <        } catch (IllegalArgumentException success) {
1820 <        }
1554 >        } catch (IllegalArgumentException success) {}
1555      }
1556  
1557      /**
# Line 1847 | Line 1581 | public class LinkedBlockingDequeTest ext
1581      /**
1582       * drainTo empties full deque, unblocking a waiting put.
1583       */
1584 <    public void testDrainToWithActivePut() {
1584 >    public void testDrainToWithActivePut() throws InterruptedException {
1585          final LinkedBlockingDeque q = populatedDeque(SIZE);
1586 <        Thread t = new Thread(new Runnable() {
1587 <                public void run() {
1588 <                    try {
1589 <                        q.put(new Integer(SIZE+1));
1590 <                    } catch (InterruptedException ie) {
1591 <                        threadUnexpectedException();
1592 <                    }
1593 <                }
1594 <            });
1595 <        try {
1596 <            t.start();
1597 <            ArrayList l = new ArrayList();
1598 <            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 <        }
1586 >        Thread t = new Thread(new CheckedRunnable() {
1587 >            public void realRun() throws InterruptedException {
1588 >                q.put(new Integer(SIZE+1));
1589 >            }});
1590 >
1591 >        t.start();
1592 >        ArrayList l = new ArrayList();
1593 >        q.drainTo(l);
1594 >        assertTrue(l.size() >= SIZE);
1595 >        for (int i = 0; i < SIZE; ++i)
1596 >            assertEquals(l.get(i), new Integer(i));
1597 >        t.join();
1598 >        assertTrue(q.size() + l.size() >= SIZE);
1599      }
1600  
1601      /**
# Line 1880 | Line 1606 | public class LinkedBlockingDequeTest ext
1606          try {
1607              q.drainTo(null, 0);
1608              shouldThrow();
1609 <        } catch (NullPointerException success) {
1884 <        }
1609 >        } catch (NullPointerException success) {}
1610      }
1611  
1612      /**
# Line 1892 | Line 1617 | public class LinkedBlockingDequeTest ext
1617          try {
1618              q.drainTo(q, 0);
1619              shouldThrow();
1620 <        } catch (IllegalArgumentException success) {
1896 <        }
1620 >        } catch (IllegalArgumentException success) {}
1621      }
1622  
1623      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines