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.8 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.15 by jsr166, Sat Nov 21 21:12:55 2009 UTC

# Line 71 | Line 71 | public class LinkedBlockingDequeTest ext
71              LinkedBlockingDeque q = new LinkedBlockingDeque();
72              q.offerFirst(null);
73              shouldThrow();
74 <        } catch (NullPointerException success) {
75 <        }
74 >        } catch (NullPointerException success) {}
75      }
76  
77      /**
# Line 169 | Line 168 | public class LinkedBlockingDequeTest ext
168          try {
169              q.getFirst();
170              shouldThrow();
171 <        }
172 <        catch (NoSuchElementException success) {}
171 >        } catch (NoSuchElementException success) {}
172 >        assertNull(q.peekFirst());
173      }
174  
175      /**
# Line 185 | Line 184 | public class LinkedBlockingDequeTest ext
184          try {
185              q.getLast();
186              shouldThrow();
187 <        }
189 <        catch (NoSuchElementException success) {}
187 >        } catch (NoSuchElementException success) {}
188          assertNull(q.peekLast());
189      }
190  
# Line 201 | Line 199 | public class LinkedBlockingDequeTest ext
199          try {
200              q.removeFirst();
201              shouldThrow();
202 <        } catch (NoSuchElementException success) {
202 >        } catch (NoSuchElementException success) {}
203 >        assertNull(q.peekFirst());
204 >    }
205 >
206 >    /**
207 >     *  removeLast removes last element, or throws NSEE if empty
208 >     */
209 >    public void testRemoveLast() {
210 >        LinkedBlockingDeque q = populatedDeque(SIZE);
211 >        for (int i = SIZE - 1; i >= 0; --i) {
212 >            assertEquals(i, ((Integer)q.removeLast()).intValue());
213          }
214 +        try {
215 +            q.removeLast();
216 +            shouldThrow();
217 +        } catch (NoSuchElementException success) {}
218 +        assertNull(q.peekLast());
219      }
220  
221      /**
# Line 216 | Line 229 | public class LinkedBlockingDequeTest ext
229          try {
230              q.remove();
231              shouldThrow();
232 <        } catch (NoSuchElementException success) {
220 <        }
232 >        } catch (NoSuchElementException success) {}
233      }
234  
235      /**
# Line 281 | Line 293 | public class LinkedBlockingDequeTest ext
293      }
294  
295      /**
296 <     * Constructor throws IAE if  capacity argument nonpositive
296 >     * Constructor throws IAE if capacity argument nonpositive
297       */
298      public void testConstructor2() {
299          try {
300              LinkedBlockingDeque q = new LinkedBlockingDeque(0);
301              shouldThrow();
302 <        }
291 <        catch (IllegalArgumentException success) {}
302 >        } catch (IllegalArgumentException success) {}
303      }
304  
305      /**
# Line 298 | Line 309 | public class LinkedBlockingDequeTest ext
309          try {
310              LinkedBlockingDeque q = new LinkedBlockingDeque(null);
311              shouldThrow();
312 <        }
302 <        catch (NullPointerException success) {}
312 >        } catch (NullPointerException success) {}
313      }
314  
315      /**
# Line 310 | Line 320 | public class LinkedBlockingDequeTest ext
320              Integer[] ints = new Integer[SIZE];
321              LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
322              shouldThrow();
323 <        }
314 <        catch (NullPointerException success) {}
323 >        } catch (NullPointerException success) {}
324      }
325  
326      /**
# Line 324 | Line 333 | public class LinkedBlockingDequeTest ext
333                  ints[i] = new Integer(i);
334              LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
335              shouldThrow();
336 <        }
328 <        catch (NullPointerException success) {}
336 >        } catch (NullPointerException success) {}
337      }
338  
339      /**
340       * Deque contains all elements of collection used to initialize
341       */
342      public void testConstructor6() {
343 <        try {
344 <            Integer[] ints = new Integer[SIZE];
345 <            for (int i = 0; i < SIZE; ++i)
346 <                ints[i] = new Integer(i);
347 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
348 <            for (int i = 0; i < SIZE; ++i)
341 <                assertEquals(ints[i], q.poll());
342 <        }
343 <        finally {}
343 >        Integer[] ints = new Integer[SIZE];
344 >        for (int i = 0; i < SIZE; ++i)
345 >            ints[i] = new Integer(i);
346 >        LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
347 >        for (int i = 0; i < SIZE; ++i)
348 >            assertEquals(ints[i], q.poll());
349      }
350  
351      /**
# Line 383 | Line 388 | public class LinkedBlockingDequeTest ext
388              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
389              q.offer(null);
390              shouldThrow();
391 <        } catch (NullPointerException success) { }
391 >        } catch (NullPointerException success) {}
392      }
393  
394      /**
# Line 394 | Line 399 | public class LinkedBlockingDequeTest ext
399              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
400              q.add(null);
401              shouldThrow();
402 <        } catch (NullPointerException success) { }
402 >        } catch (NullPointerException success) {}
403      }
404  
405      /**
# Line 405 | Line 410 | public class LinkedBlockingDequeTest ext
410              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
411              q.push(null);
412              shouldThrow();
413 <        } catch (NullPointerException success) { }
413 >        } catch (NullPointerException success) {}
414      }
415  
416      /**
# Line 421 | Line 426 | public class LinkedBlockingDequeTest ext
426              }
427              assertEquals(0, q.remainingCapacity());
428              q.push(new Integer(SIZE));
429 <        } catch (IllegalStateException success) {
430 <        }
429 >            shouldThrow();
430 >        } catch (IllegalStateException success) {}
431      }
432  
433      /**
# Line 447 | Line 452 | public class LinkedBlockingDequeTest ext
452          try {
453              q.pop();
454              shouldThrow();
455 <        } catch (NoSuchElementException success) {
451 <        }
455 >        } catch (NoSuchElementException success) {}
456      }
457  
458  
# Line 472 | Line 476 | public class LinkedBlockingDequeTest ext
476              }
477              assertEquals(0, q.remainingCapacity());
478              q.add(new Integer(SIZE));
479 <        } catch (IllegalStateException success) {
480 <        }
479 >            shouldThrow();
480 >        } catch (IllegalStateException success) {}
481      }
482  
483      /**
# Line 484 | Line 488 | public class LinkedBlockingDequeTest ext
488              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
489              q.addAll(null);
490              shouldThrow();
491 <        }
488 <        catch (NullPointerException success) {}
491 >        } catch (NullPointerException success) {}
492      }
493  
494      /**
# Line 496 | Line 499 | public class LinkedBlockingDequeTest ext
499              LinkedBlockingDeque q = populatedDeque(SIZE);
500              q.addAll(q);
501              shouldThrow();
502 <        }
500 <        catch (IllegalArgumentException success) {}
502 >        } catch (IllegalArgumentException success) {}
503      }
504  
505      /**
# Line 509 | Line 511 | public class LinkedBlockingDequeTest ext
511              Integer[] ints = new Integer[SIZE];
512              q.addAll(Arrays.asList(ints));
513              shouldThrow();
514 <        }
513 <        catch (NullPointerException success) {}
514 >        } catch (NullPointerException success) {}
515      }
516      /**
517       * addAll of a collection with any null elements throws NPE after
# Line 524 | Line 525 | public class LinkedBlockingDequeTest ext
525                  ints[i] = new Integer(i);
526              q.addAll(Arrays.asList(ints));
527              shouldThrow();
528 <        }
528 <        catch (NullPointerException success) {}
528 >        } catch (NullPointerException success) {}
529      }
530      /**
531       * addAll throws ISE if not enough room
# Line 538 | Line 538 | public class LinkedBlockingDequeTest ext
538                  ints[i] = new Integer(i);
539              q.addAll(Arrays.asList(ints));
540              shouldThrow();
541 <        }
542 <        catch (IllegalStateException success) {}
541 >        } catch (IllegalStateException success) {}
542      }
543      /**
544       * Deque contains all elements, in traversal order, of successful addAll
545       */
546      public void testAddAll5() {
547 <        try {
548 <            Integer[] empty = new Integer[0];
549 <            Integer[] ints = new Integer[SIZE];
550 <            for (int i = 0; i < SIZE; ++i)
551 <                ints[i] = new Integer(i);
552 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
553 <            assertFalse(q.addAll(Arrays.asList(empty)));
554 <            assertTrue(q.addAll(Arrays.asList(ints)));
555 <            for (int i = 0; i < SIZE; ++i)
557 <                assertEquals(ints[i], q.poll());
558 <        }
559 <        finally {}
547 >        Integer[] empty = new Integer[0];
548 >        Integer[] ints = new Integer[SIZE];
549 >        for (int i = 0; i < SIZE; ++i)
550 >            ints[i] = new Integer(i);
551 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
552 >        assertFalse(q.addAll(Arrays.asList(empty)));
553 >        assertTrue(q.addAll(Arrays.asList(ints)));
554 >        for (int i = 0; i < SIZE; ++i)
555 >            assertEquals(ints[i], q.poll());
556      }
557  
558  
559      /**
560       * put(null) throws NPE
561       */
562 <     public void testPutNull() {
562 >    public void testPutNull() throws InterruptedException {
563          try {
564              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
565              q.put(null);
566              shouldThrow();
567 <        }
568 <        catch (NullPointerException success) {
573 <        }
574 <        catch (InterruptedException ie) {
575 <            unexpectedException();
576 <        }
577 <     }
567 >        } catch (NullPointerException success) {}
568 >    }
569  
570      /**
571       * all elements successfully put are contained
572       */
573 <     public void testPut() {
574 <         try {
575 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
576 <             for (int i = 0; i < SIZE; ++i) {
577 <                 Integer I = new Integer(i);
578 <                 q.put(I);
588 <                 assertTrue(q.contains(I));
589 <             }
590 <             assertEquals(0, q.remainingCapacity());
591 <         }
592 <        catch (InterruptedException ie) {
593 <            unexpectedException();
573 >    public void testPut() throws InterruptedException {
574 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
575 >        for (int i = 0; i < SIZE; ++i) {
576 >            Integer I = new Integer(i);
577 >            q.put(I);
578 >            assertTrue(q.contains(I));
579          }
580 +        assertEquals(0, q.remainingCapacity());
581      }
582  
583      /**
584       * put blocks interruptibly if full
585       */
586 <    public void testBlockingPut() {
587 <        Thread t = new Thread(new Runnable() {
588 <                public void run() {
589 <                    int added = 0;
590 <                    try {
591 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
592 <                        for (int i = 0; i < SIZE; ++i) {
593 <                            q.put(new Integer(i));
594 <                            ++added;
609 <                        }
610 <                        q.put(new Integer(SIZE));
611 <                        threadShouldThrow();
612 <                    } catch (InterruptedException ie) {
613 <                        threadAssertEquals(added, SIZE);
586 >    public void testBlockingPut() throws InterruptedException {
587 >        Thread t = new Thread(new CheckedRunnable() {
588 >            public void realRun() {
589 >                int added = 0;
590 >                try {
591 >                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
592 >                    for (int i = 0; i < SIZE; ++i) {
593 >                        q.put(new Integer(i));
594 >                        ++added;
595                      }
596 <                }});
596 >                    q.put(new Integer(SIZE));
597 >                    threadShouldThrow();
598 >                } catch (InterruptedException success) {
599 >                    threadAssertEquals(added, SIZE);
600 >                }
601 >            }});
602 >
603          t.start();
604 <        try {
605 <           Thread.sleep(SHORT_DELAY_MS);
606 <           t.interrupt();
620 <           t.join();
621 <        }
622 <        catch (InterruptedException ie) {
623 <            unexpectedException();
624 <        }
604 >        Thread.sleep(SHORT_DELAY_MS);
605 >        t.interrupt();
606 >        t.join();
607      }
608  
609      /**
610       * put blocks waiting for take when full
611       */
612 <    public void testPutWithTake() {
612 >    public void testPutWithTake() throws InterruptedException {
613          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
614 <        Thread t = new Thread(new Runnable() {
615 <                public void run() {
616 <                    int added = 0;
617 <                    try {
618 <                        q.put(new Object());
619 <                        ++added;
620 <                        q.put(new Object());
621 <                        ++added;
622 <                        q.put(new Object());
623 <                        ++added;
624 <                        q.put(new Object());
625 <                        ++added;
626 <                        threadShouldThrow();
627 <                    } catch (InterruptedException e) {
628 <                        threadAssertTrue(added >= 2);
647 <                    }
614 >        Thread t = new Thread(new CheckedRunnable() {
615 >            public void realRun() {
616 >                int added = 0;
617 >                try {
618 >                    q.put(new Object());
619 >                    ++added;
620 >                    q.put(new Object());
621 >                    ++added;
622 >                    q.put(new Object());
623 >                    ++added;
624 >                    q.put(new Object());
625 >                    ++added;
626 >                    threadShouldThrow();
627 >                } catch (InterruptedException success) {
628 >                    threadAssertTrue(added >= 2);
629                  }
630 <            });
631 <        try {
632 <            t.start();
633 <            Thread.sleep(SHORT_DELAY_MS);
634 <            q.take();
635 <            t.interrupt();
636 <            t.join();
656 <        } catch (Exception e) {
657 <            unexpectedException();
658 <        }
630 >            }});
631 >
632 >        t.start();
633 >        Thread.sleep(SHORT_DELAY_MS);
634 >        q.take();
635 >        t.interrupt();
636 >        t.join();
637      }
638  
639      /**
640       * timed offer times out if full and elements not taken
641       */
642 <    public void testTimedOffer() {
642 >    public void testTimedOffer() throws InterruptedException {
643          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
644 <        Thread t = new Thread(new Runnable() {
645 <                public void run() {
646 <                    try {
647 <                        q.put(new Object());
648 <                        q.put(new Object());
649 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
650 <                        q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
673 <                        threadShouldThrow();
674 <                    } catch (InterruptedException success) {}
675 <                }
676 <            });
644 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
645 >            public void realRun() throws InterruptedException {
646 >                q.put(new Object());
647 >                q.put(new Object());
648 >                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
649 >                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
650 >            }};
651  
652 <        try {
653 <            t.start();
654 <            Thread.sleep(SMALL_DELAY_MS);
655 <            t.interrupt();
682 <            t.join();
683 <        } catch (Exception e) {
684 <            unexpectedException();
685 <        }
652 >        t.start();
653 >        Thread.sleep(SMALL_DELAY_MS);
654 >        t.interrupt();
655 >        t.join();
656      }
657  
658      /**
659       * take retrieves elements in FIFO order
660       */
661 <    public void testTake() {
662 <        try {
663 <            LinkedBlockingDeque q = populatedDeque(SIZE);
664 <            for (int i = 0; i < SIZE; ++i) {
695 <                assertEquals(i, ((Integer)q.take()).intValue());
696 <            }
697 <        } catch (InterruptedException e) {
698 <            unexpectedException();
661 >    public void testTake() throws InterruptedException {
662 >        LinkedBlockingDeque q = populatedDeque(SIZE);
663 >        for (int i = 0; i < SIZE; ++i) {
664 >            assertEquals(i, ((Integer)q.take()).intValue());
665          }
666      }
667  
668      /**
669       * take blocks interruptibly when empty
670       */
671 <    public void testTakeFromEmpty() {
671 >    public void testTakeFromEmpty() throws InterruptedException {
672          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
673 <        Thread t = new Thread(new Runnable() {
674 <                public void run() {
675 <                    try {
676 <                        q.take();
677 <                        threadShouldThrow();
678 <                    } catch (InterruptedException success) { }
679 <                }
680 <            });
681 <        try {
716 <            t.start();
717 <            Thread.sleep(SHORT_DELAY_MS);
718 <            t.interrupt();
719 <            t.join();
720 <        } catch (Exception e) {
721 <            unexpectedException();
722 <        }
673 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
674 >            public void realRun() throws InterruptedException {
675 >                q.take();
676 >            }};
677 >
678 >        t.start();
679 >        Thread.sleep(SHORT_DELAY_MS);
680 >        t.interrupt();
681 >        t.join();
682      }
683  
684      /**
685       * Take removes existing elements until empty, then blocks interruptibly
686       */
687 <    public void testBlockingTake() {
688 <        Thread t = new Thread(new Runnable() {
689 <                public void run() {
690 <                    try {
691 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
692 <                        for (int i = 0; i < SIZE; ++i) {
693 <                            assertEquals(i, ((Integer)q.take()).intValue());
694 <                        }
695 <                        q.take();
696 <                        threadShouldThrow();
738 <                    } catch (InterruptedException success) {
739 <                    }
740 <                }});
687 >    public void testBlockingTake() throws InterruptedException {
688 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
689 >            public void realRun() throws InterruptedException {
690 >                LinkedBlockingDeque q = populatedDeque(SIZE);
691 >                for (int i = 0; i < SIZE; ++i) {
692 >                    assertEquals(i, ((Integer)q.take()).intValue());
693 >                }
694 >                q.take();
695 >            }};
696 >
697          t.start();
698 <        try {
699 <           Thread.sleep(SHORT_DELAY_MS);
700 <           t.interrupt();
745 <           t.join();
746 <        }
747 <        catch (InterruptedException ie) {
748 <            unexpectedException();
749 <        }
698 >        Thread.sleep(SHORT_DELAY_MS);
699 >        t.interrupt();
700 >        t.join();
701      }
702  
703  
# Line 764 | Line 715 | public class LinkedBlockingDequeTest ext
715      /**
716       * timed poll with zero timeout succeeds when non-empty, else times out
717       */
718 <    public void testTimedPoll0() {
719 <        try {
720 <            LinkedBlockingDeque q = populatedDeque(SIZE);
721 <            for (int i = 0; i < SIZE; ++i) {
771 <                assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
772 <            }
773 <            assertNull(q.poll(0, MILLISECONDS));
774 <        } catch (InterruptedException e) {
775 <            unexpectedException();
718 >    public void testTimedPoll0() throws InterruptedException {
719 >        LinkedBlockingDeque q = populatedDeque(SIZE);
720 >        for (int i = 0; i < SIZE; ++i) {
721 >            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
722          }
723 +        assertNull(q.poll(0, MILLISECONDS));
724      }
725  
726      /**
727       * timed poll with nonzero timeout succeeds when non-empty, else times out
728       */
729 <    public void testTimedPoll() {
730 <        try {
731 <            LinkedBlockingDeque q = populatedDeque(SIZE);
732 <            for (int i = 0; i < SIZE; ++i) {
786 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
787 <            }
788 <            assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
789 <        } catch (InterruptedException e) {
790 <            unexpectedException();
729 >    public void testTimedPoll() throws InterruptedException {
730 >        LinkedBlockingDeque q = populatedDeque(SIZE);
731 >        for (int i = 0; i < SIZE; ++i) {
732 >            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
733          }
734 +        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
735      }
736  
737      /**
738       * Interrupted timed poll throws InterruptedException instead of
739       * returning timeout status
740       */
741 <    public void testInterruptedTimedPoll() {
742 <        Thread t = new Thread(new Runnable() {
743 <                public void run() {
744 <                    try {
745 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
746 <                        for (int i = 0; i < SIZE; ++i) {
747 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
748 <                        }
749 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
750 <                    } catch (InterruptedException success) {
751 <                    }
752 <                }});
741 >    public void testInterruptedTimedPoll() throws InterruptedException {
742 >        Thread t = new Thread(new CheckedRunnable() {
743 >            public void realRun() throws InterruptedException {
744 >                LinkedBlockingDeque q = populatedDeque(SIZE);
745 >                for (int i = 0; i < SIZE; ++i) {
746 >                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
747 >                }
748 >                try {
749 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
750 >                    shouldThrow();
751 >                } catch (InterruptedException success) {}
752 >            }});
753 >
754          t.start();
755 <        try {
756 <           Thread.sleep(SHORT_DELAY_MS);
757 <           t.interrupt();
814 <           t.join();
815 <        }
816 <        catch (InterruptedException ie) {
817 <            unexpectedException();
818 <        }
755 >        Thread.sleep(SHORT_DELAY_MS);
756 >        t.interrupt();
757 >        t.join();
758      }
759  
760      /**
761       *  timed poll before a delayed offer fails; after offer succeeds;
762       *  on interruption throws
763       */
764 <    public void testTimedPollWithOffer() {
764 >    public void testTimedPollWithOffer() throws InterruptedException {
765          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
766 <        Thread t = new Thread(new Runnable() {
767 <                public void run() {
768 <                    try {
769 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
770 <                        q.poll(LONG_DELAY_MS, MILLISECONDS);
771 <                        q.poll(LONG_DELAY_MS, MILLISECONDS);
772 <                        threadShouldThrow();
773 <                    } catch (InterruptedException success) { }
774 <                }
775 <            });
776 <        try {
777 <            t.start();
778 <            Thread.sleep(SMALL_DELAY_MS);
779 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
780 <            t.interrupt();
842 <            t.join();
843 <        } catch (Exception e) {
844 <            unexpectedException();
845 <        }
766 >        Thread t = new Thread(new CheckedRunnable() {
767 >            public void realRun() throws InterruptedException {
768 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
769 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
770 >                try {
771 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
772 >                    shouldThrow();
773 >                } catch (InterruptedException success) {}
774 >            }});
775 >
776 >        t.start();
777 >        Thread.sleep(SMALL_DELAY_MS);
778 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
779 >        t.interrupt();
780 >        t.join();
781      }
782  
783  
784      /**
785       * putFirst(null) throws NPE
786       */
787 <     public void testPutFirstNull() {
787 >     public void testPutFirstNull() throws InterruptedException {
788          try {
789              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
790              q.putFirst(null);
791              shouldThrow();
792 <        }
858 <        catch (NullPointerException success) {
859 <        }
860 <        catch (InterruptedException ie) {
861 <            unexpectedException();
862 <        }
792 >        } catch (NullPointerException success) {}
793       }
794  
795      /**
796       * all elements successfully putFirst are contained
797       */
798 <     public void testPutFirst() {
799 <         try {
800 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
801 <             for (int i = 0; i < SIZE; ++i) {
802 <                 Integer I = new Integer(i);
803 <                 q.putFirst(I);
874 <                 assertTrue(q.contains(I));
875 <             }
876 <             assertEquals(0, q.remainingCapacity());
798 >     public void testPutFirst() throws InterruptedException {
799 >         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
800 >         for (int i = 0; i < SIZE; ++i) {
801 >             Integer I = new Integer(i);
802 >             q.putFirst(I);
803 >             assertTrue(q.contains(I));
804           }
805 <        catch (InterruptedException ie) {
879 <            unexpectedException();
880 <        }
805 >         assertEquals(0, q.remainingCapacity());
806      }
807  
808      /**
809       * putFirst blocks interruptibly if full
810       */
811 <    public void testBlockingPutFirst() {
812 <        Thread t = new Thread(new Runnable() {
813 <                public void run() {
814 <                    int added = 0;
815 <                    try {
816 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
817 <                        for (int i = 0; i < SIZE; ++i) {
818 <                            q.putFirst(new Integer(i));
819 <                            ++added;
895 <                        }
896 <                        q.putFirst(new Integer(SIZE));
897 <                        threadShouldThrow();
898 <                    } catch (InterruptedException ie) {
899 <                        threadAssertEquals(added, SIZE);
811 >    public void testBlockingPutFirst() throws InterruptedException {
812 >        Thread t = new Thread(new CheckedRunnable() {
813 >            public void realRun() {
814 >                int added = 0;
815 >                try {
816 >                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
817 >                    for (int i = 0; i < SIZE; ++i) {
818 >                        q.putFirst(new Integer(i));
819 >                        ++added;
820                      }
821 <                }});
821 >                    q.putFirst(new Integer(SIZE));
822 >                    threadShouldThrow();
823 >                } catch (InterruptedException success) {
824 >                    threadAssertEquals(added, SIZE);
825 >                }
826 >            }});
827 >
828          t.start();
829 <        try {
830 <           Thread.sleep(SHORT_DELAY_MS);
831 <           t.interrupt();
906 <           t.join();
907 <        }
908 <        catch (InterruptedException ie) {
909 <            unexpectedException();
910 <        }
829 >        Thread.sleep(SHORT_DELAY_MS);
830 >        t.interrupt();
831 >        t.join();
832      }
833  
834      /**
835       * putFirst blocks waiting for take when full
836       */
837 <    public void testPutFirstWithTake() {
837 >    public void testPutFirstWithTake() throws InterruptedException {
838          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
839 <        Thread t = new Thread(new Runnable() {
840 <                public void run() {
841 <                    int added = 0;
842 <                    try {
843 <                        q.putFirst(new Object());
844 <                        ++added;
845 <                        q.putFirst(new Object());
846 <                        ++added;
847 <                        q.putFirst(new Object());
848 <                        ++added;
849 <                        q.putFirst(new Object());
850 <                        ++added;
851 <                        threadShouldThrow();
852 <                    } catch (InterruptedException e) {
853 <                        threadAssertTrue(added >= 2);
933 <                    }
839 >        Thread t = new Thread(new CheckedRunnable() {
840 >            public void realRun() {
841 >                int added = 0;
842 >                try {
843 >                    q.putFirst(new Object());
844 >                    ++added;
845 >                    q.putFirst(new Object());
846 >                    ++added;
847 >                    q.putFirst(new Object());
848 >                    ++added;
849 >                    q.putFirst(new Object());
850 >                    ++added;
851 >                    threadShouldThrow();
852 >                } catch (InterruptedException success) {
853 >                    threadAssertTrue(added >= 2);
854                  }
855 <            });
856 <        try {
857 <            t.start();
858 <            Thread.sleep(SHORT_DELAY_MS);
859 <            q.take();
860 <            t.interrupt();
861 <            t.join();
942 <        } catch (Exception e) {
943 <            unexpectedException();
944 <        }
855 >            }});
856 >
857 >        t.start();
858 >        Thread.sleep(SHORT_DELAY_MS);
859 >        q.take();
860 >        t.interrupt();
861 >        t.join();
862      }
863  
864      /**
865       * timed offerFirst times out if full and elements not taken
866       */
867 <    public void testTimedOfferFirst() {
867 >    public void testTimedOfferFirst() throws InterruptedException {
868          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
869 <        Thread t = new Thread(new Runnable() {
870 <                public void run() {
871 <                    try {
872 <                        q.putFirst(new Object());
873 <                        q.putFirst(new Object());
874 <                        threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
875 <                        q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
959 <                        threadShouldThrow();
960 <                    } catch (InterruptedException success) {}
961 <                }
962 <            });
869 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
870 >            public void realRun() throws InterruptedException {
871 >                q.putFirst(new Object());
872 >                q.putFirst(new Object());
873 >                threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
874 >                q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
875 >            }};
876  
877 <        try {
878 <            t.start();
879 <            Thread.sleep(SMALL_DELAY_MS);
880 <            t.interrupt();
968 <            t.join();
969 <        } catch (Exception e) {
970 <            unexpectedException();
971 <        }
877 >        t.start();
878 >        Thread.sleep(SMALL_DELAY_MS);
879 >        t.interrupt();
880 >        t.join();
881      }
882  
883      /**
884       * take retrieves elements in FIFO order
885       */
886 <    public void testTakeFirst() {
887 <        try {
888 <            LinkedBlockingDeque q = populatedDeque(SIZE);
889 <            for (int i = 0; i < SIZE; ++i) {
981 <                assertEquals(i, ((Integer)q.takeFirst()).intValue());
982 <            }
983 <        } catch (InterruptedException e) {
984 <            unexpectedException();
886 >    public void testTakeFirst() throws InterruptedException {
887 >        LinkedBlockingDeque q = populatedDeque(SIZE);
888 >        for (int i = 0; i < SIZE; ++i) {
889 >            assertEquals(i, ((Integer)q.takeFirst()).intValue());
890          }
891      }
892  
893      /**
894       * takeFirst blocks interruptibly when empty
895       */
896 <    public void testTakeFirstFromEmpty() {
896 >    public void testTakeFirstFromEmpty() throws InterruptedException {
897          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
898 <        Thread t = new Thread(new Runnable() {
899 <                public void run() {
900 <                    try {
901 <                        q.takeFirst();
902 <                        threadShouldThrow();
903 <                    } catch (InterruptedException success) { }
904 <                }
905 <            });
906 <        try {
1002 <            t.start();
1003 <            Thread.sleep(SHORT_DELAY_MS);
1004 <            t.interrupt();
1005 <            t.join();
1006 <        } catch (Exception e) {
1007 <            unexpectedException();
1008 <        }
898 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
899 >            public void realRun() throws InterruptedException {
900 >                q.takeFirst();
901 >            }};
902 >
903 >        t.start();
904 >        Thread.sleep(SHORT_DELAY_MS);
905 >        t.interrupt();
906 >        t.join();
907      }
908  
909      /**
910       * TakeFirst removes existing elements until empty, then blocks interruptibly
911       */
912 <    public void testBlockingTakeFirst() {
913 <        Thread t = new Thread(new Runnable() {
914 <                public void run() {
915 <                    try {
916 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
917 <                        for (int i = 0; i < SIZE; ++i) {
918 <                            assertEquals(i, ((Integer)q.takeFirst()).intValue());
919 <                        }
920 <                        q.takeFirst();
921 <                        threadShouldThrow();
1024 <                    } catch (InterruptedException success) {
1025 <                    }
1026 <                }});
912 >    public void testBlockingTakeFirst() throws InterruptedException {
913 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
914 >            public void realRun() throws InterruptedException {
915 >                LinkedBlockingDeque q = populatedDeque(SIZE);
916 >                for (int i = 0; i < SIZE; ++i) {
917 >                    assertEquals(i, ((Integer)q.takeFirst()).intValue());
918 >                }
919 >                q.takeFirst();
920 >            }};
921 >
922          t.start();
923 <        try {
924 <           Thread.sleep(SHORT_DELAY_MS);
925 <           t.interrupt();
1031 <           t.join();
1032 <        }
1033 <        catch (InterruptedException ie) {
1034 <            unexpectedException();
1035 <        }
923 >        Thread.sleep(SHORT_DELAY_MS);
924 >        t.interrupt();
925 >        t.join();
926      }
927  
928  
929      /**
930       * timed pollFirst with zero timeout succeeds when non-empty, else times out
931       */
932 <    public void testTimedPollFirst0() {
933 <        try {
934 <            LinkedBlockingDeque q = populatedDeque(SIZE);
935 <            for (int i = 0; i < SIZE; ++i) {
1046 <                assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
1047 <            }
1048 <            assertNull(q.pollFirst(0, MILLISECONDS));
1049 <        } catch (InterruptedException e) {
1050 <            unexpectedException();
932 >    public void testTimedPollFirst0() throws InterruptedException {
933 >        LinkedBlockingDeque q = populatedDeque(SIZE);
934 >        for (int i = 0; i < SIZE; ++i) {
935 >            assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
936          }
937 +        assertNull(q.pollFirst(0, MILLISECONDS));
938      }
939  
940      /**
941       * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
942       */
943 <    public void testTimedPollFirst() {
944 <        try {
945 <            LinkedBlockingDeque q = populatedDeque(SIZE);
946 <            for (int i = 0; i < SIZE; ++i) {
1061 <                assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1062 <            }
1063 <            assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1064 <        } catch (InterruptedException e) {
1065 <            unexpectedException();
943 >    public void testTimedPollFirst() throws InterruptedException {
944 >        LinkedBlockingDeque q = populatedDeque(SIZE);
945 >        for (int i = 0; i < SIZE; ++i) {
946 >            assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
947          }
948 +        assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
949      }
950  
951      /**
952       * Interrupted timed pollFirst throws InterruptedException instead of
953       * returning timeout status
954       */
955 <    public void testInterruptedTimedPollFirst() {
956 <        Thread t = new Thread(new Runnable() {
957 <                public void run() {
958 <                    try {
959 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
960 <                        for (int i = 0; i < SIZE; ++i) {
961 <                            threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
962 <                        }
963 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
964 <                    } catch (InterruptedException success) {
965 <                    }
966 <                }});
955 >    public void testInterruptedTimedPollFirst() throws InterruptedException {
956 >        Thread t = new Thread(new CheckedRunnable() {
957 >            public void realRun() throws InterruptedException {
958 >                LinkedBlockingDeque q = populatedDeque(SIZE);
959 >                for (int i = 0; i < SIZE; ++i) {
960 >                    assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
961 >                }
962 >                try {
963 >                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
964 >                    shouldThrow();
965 >                } catch (InterruptedException success) {}
966 >            }});
967 >
968          t.start();
969 <        try {
970 <           Thread.sleep(SHORT_DELAY_MS);
971 <           t.interrupt();
1089 <           t.join();
1090 <        }
1091 <        catch (InterruptedException ie) {
1092 <            unexpectedException();
1093 <        }
969 >        Thread.sleep(SHORT_DELAY_MS);
970 >        t.interrupt();
971 >        t.join();
972      }
973  
974      /**
975       *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
976       *  on interruption throws
977       */
978 <    public void testTimedPollFirstWithOfferFirst() {
978 >    public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
979          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
980 <        Thread t = new Thread(new Runnable() {
981 <                public void run() {
982 <                    try {
983 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
984 <                        q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
985 <                        q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
986 <                        threadShouldThrow();
987 <                    } catch (InterruptedException success) { }
988 <                }
989 <            });
990 <        try {
991 <            t.start();
992 <            Thread.sleep(SMALL_DELAY_MS);
993 <            assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
994 <            t.interrupt();
1117 <            t.join();
1118 <        } catch (Exception e) {
1119 <            unexpectedException();
1120 <        }
980 >        Thread t = new Thread(new CheckedRunnable() {
981 >            public void realRun() throws InterruptedException {
982 >                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
983 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
984 >                try {
985 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
986 >                    shouldThrow();
987 >                } catch (InterruptedException success) {}
988 >            }});
989 >
990 >        t.start();
991 >        Thread.sleep(SMALL_DELAY_MS);
992 >        assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
993 >        t.interrupt();
994 >        t.join();
995      }
996  
997      /**
998       * putLast(null) throws NPE
999       */
1000 <     public void testPutLastNull() {
1000 >     public void testPutLastNull() throws InterruptedException {
1001          try {
1002              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1003              q.putLast(null);
1004              shouldThrow();
1005 <        }
1132 <        catch (NullPointerException success) {
1133 <        }
1134 <        catch (InterruptedException ie) {
1135 <            unexpectedException();
1136 <        }
1005 >        } catch (NullPointerException success) {}
1006       }
1007  
1008      /**
1009       * all elements successfully putLast are contained
1010       */
1011 <     public void testPutLast() {
1012 <         try {
1013 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1014 <             for (int i = 0; i < SIZE; ++i) {
1015 <                 Integer I = new Integer(i);
1016 <                 q.putLast(I);
1148 <                 assertTrue(q.contains(I));
1149 <             }
1150 <             assertEquals(0, q.remainingCapacity());
1011 >     public void testPutLast() throws InterruptedException {
1012 >         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1013 >         for (int i = 0; i < SIZE; ++i) {
1014 >             Integer I = new Integer(i);
1015 >             q.putLast(I);
1016 >             assertTrue(q.contains(I));
1017           }
1018 <        catch (InterruptedException ie) {
1153 <            unexpectedException();
1154 <        }
1018 >         assertEquals(0, q.remainingCapacity());
1019      }
1020  
1021      /**
1022       * putLast blocks interruptibly if full
1023       */
1024 <    public void testBlockingPutLast() {
1025 <        Thread t = new Thread(new Runnable() {
1026 <                public void run() {
1027 <                    int added = 0;
1028 <                    try {
1029 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1030 <                        for (int i = 0; i < SIZE; ++i) {
1031 <                            q.putLast(new Integer(i));
1032 <                            ++added;
1169 <                        }
1170 <                        q.putLast(new Integer(SIZE));
1171 <                        threadShouldThrow();
1172 <                    } catch (InterruptedException ie) {
1173 <                        threadAssertEquals(added, SIZE);
1024 >    public void testBlockingPutLast() throws InterruptedException {
1025 >        Thread t = new Thread(new CheckedRunnable() {
1026 >            public void realRun() {
1027 >                int added = 0;
1028 >                try {
1029 >                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1030 >                    for (int i = 0; i < SIZE; ++i) {
1031 >                        q.putLast(new Integer(i));
1032 >                        ++added;
1033                      }
1034 <                }});
1034 >                    q.putLast(new Integer(SIZE));
1035 >                    threadShouldThrow();
1036 >                } catch (InterruptedException success) {
1037 >                    threadAssertEquals(added, SIZE);
1038 >                }
1039 >            }});
1040 >
1041          t.start();
1042 <        try {
1043 <           Thread.sleep(SHORT_DELAY_MS);
1044 <           t.interrupt();
1180 <           t.join();
1181 <        }
1182 <        catch (InterruptedException ie) {
1183 <            unexpectedException();
1184 <        }
1042 >        Thread.sleep(SHORT_DELAY_MS);
1043 >        t.interrupt();
1044 >        t.join();
1045      }
1046  
1047      /**
1048       * putLast blocks waiting for take when full
1049       */
1050 <    public void testPutLastWithTake() {
1050 >    public void testPutLastWithTake() throws InterruptedException {
1051          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1052 <        Thread t = new Thread(new Runnable() {
1053 <                public void run() {
1054 <                    int added = 0;
1055 <                    try {
1056 <                        q.putLast(new Object());
1057 <                        ++added;
1058 <                        q.putLast(new Object());
1059 <                        ++added;
1060 <                        q.putLast(new Object());
1061 <                        ++added;
1062 <                        q.putLast(new Object());
1063 <                        ++added;
1064 <                        threadShouldThrow();
1065 <                    } catch (InterruptedException e) {
1066 <                        threadAssertTrue(added >= 2);
1207 <                    }
1052 >        Thread t = new Thread(new CheckedRunnable() {
1053 >            public void realRun() {
1054 >                int added = 0;
1055 >                try {
1056 >                    q.putLast(new Object());
1057 >                    ++added;
1058 >                    q.putLast(new Object());
1059 >                    ++added;
1060 >                    q.putLast(new Object());
1061 >                    ++added;
1062 >                    q.putLast(new Object());
1063 >                    ++added;
1064 >                    threadShouldThrow();
1065 >                } catch (InterruptedException success) {
1066 >                    threadAssertTrue(added >= 2);
1067                  }
1068 <            });
1069 <        try {
1070 <            t.start();
1071 <            Thread.sleep(SHORT_DELAY_MS);
1072 <            q.take();
1073 <            t.interrupt();
1074 <            t.join();
1216 <        } catch (Exception e) {
1217 <            unexpectedException();
1218 <        }
1068 >            }});
1069 >
1070 >        t.start();
1071 >        Thread.sleep(SHORT_DELAY_MS);
1072 >        q.take();
1073 >        t.interrupt();
1074 >        t.join();
1075      }
1076  
1077      /**
1078       * timed offerLast times out if full and elements not taken
1079       */
1080 <    public void testTimedOfferLast() {
1080 >    public void testTimedOfferLast() throws InterruptedException {
1081          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1082 <        Thread t = new Thread(new Runnable() {
1083 <                public void run() {
1084 <                    try {
1085 <                        q.putLast(new Object());
1086 <                        q.putLast(new Object());
1087 <                        threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1088 <                        q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1233 <                        threadShouldThrow();
1234 <                    } catch (InterruptedException success) {}
1235 <                }
1236 <            });
1082 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1083 >            public void realRun() throws InterruptedException {
1084 >                q.putLast(new Object());
1085 >                q.putLast(new Object());
1086 >                threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1087 >                q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1088 >            }};
1089  
1090 <        try {
1091 <            t.start();
1092 <            Thread.sleep(SMALL_DELAY_MS);
1093 <            t.interrupt();
1242 <            t.join();
1243 <        } catch (Exception e) {
1244 <            unexpectedException();
1245 <        }
1090 >        t.start();
1091 >        Thread.sleep(SMALL_DELAY_MS);
1092 >        t.interrupt();
1093 >        t.join();
1094      }
1095  
1096      /**
1097       * takeLast retrieves elements in FIFO order
1098       */
1099 <    public void testTakeLast() {
1100 <        try {
1101 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1102 <            for (int i = 0; i < SIZE; ++i) {
1255 <                assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1256 <            }
1257 <        } catch (InterruptedException e) {
1258 <            unexpectedException();
1099 >    public void testTakeLast() throws InterruptedException {
1100 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1101 >        for (int i = 0; i < SIZE; ++i) {
1102 >            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1103          }
1104      }
1105  
1106      /**
1107       * takeLast blocks interruptibly when empty
1108       */
1109 <    public void testTakeLastFromEmpty() {
1109 >    public void testTakeLastFromEmpty() throws InterruptedException {
1110          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1111 <        Thread t = new Thread(new Runnable() {
1112 <                public void run() {
1113 <                    try {
1114 <                        q.takeLast();
1115 <                        threadShouldThrow();
1116 <                    } catch (InterruptedException success) { }
1117 <                }
1118 <            });
1119 <        try {
1276 <            t.start();
1277 <            Thread.sleep(SHORT_DELAY_MS);
1278 <            t.interrupt();
1279 <            t.join();
1280 <        } catch (Exception e) {
1281 <            unexpectedException();
1282 <        }
1111 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1112 >            public void realRun() throws InterruptedException {
1113 >                q.takeLast();
1114 >            }};
1115 >
1116 >        t.start();
1117 >        Thread.sleep(SHORT_DELAY_MS);
1118 >        t.interrupt();
1119 >        t.join();
1120      }
1121  
1122      /**
1123       * TakeLast removes existing elements until empty, then blocks interruptibly
1124       */
1125 <    public void testBlockingTakeLast() {
1126 <        Thread t = new Thread(new Runnable() {
1127 <                public void run() {
1128 <                    try {
1129 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1130 <                        for (int i = 0; i < SIZE; ++i) {
1131 <                            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1132 <                        }
1133 <                        q.takeLast();
1134 <                        threadShouldThrow();
1298 <                    } catch (InterruptedException success) {
1299 <                    }
1300 <                }});
1125 >    public void testBlockingTakeLast() throws InterruptedException {
1126 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1127 >            public void realRun() throws InterruptedException {
1128 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1129 >                for (int i = 0; i < SIZE; ++i) {
1130 >                    assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1131 >                }
1132 >                q.takeLast();
1133 >            }};
1134 >
1135          t.start();
1136 <        try {
1137 <           Thread.sleep(SHORT_DELAY_MS);
1138 <           t.interrupt();
1305 <           t.join();
1306 <        }
1307 <        catch (InterruptedException ie) {
1308 <            unexpectedException();
1309 <        }
1136 >        Thread.sleep(SHORT_DELAY_MS);
1137 >        t.interrupt();
1138 >        t.join();
1139      }
1140  
1141  
1142      /**
1143       * timed pollLast with zero timeout succeeds when non-empty, else times out
1144       */
1145 <    public void testTimedPollLast0() {
1146 <        try {
1147 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1148 <            for (int i = 0; i < SIZE; ++i) {
1320 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1321 <            }
1322 <            assertNull(q.pollLast(0, MILLISECONDS));
1323 <        } catch (InterruptedException e) {
1324 <            unexpectedException();
1145 >    public void testTimedPollLast0() throws InterruptedException {
1146 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1147 >        for (int i = 0; i < SIZE; ++i) {
1148 >            assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1149          }
1150 +        assertNull(q.pollLast(0, MILLISECONDS));
1151      }
1152  
1153      /**
1154       * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1155       */
1156 <    public void testTimedPollLast() {
1157 <        try {
1158 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1159 <            for (int i = 0; i < SIZE; ++i) {
1335 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1336 <            }
1337 <            assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1338 <        } catch (InterruptedException e) {
1339 <            unexpectedException();
1156 >    public void testTimedPollLast() throws InterruptedException {
1157 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1158 >        for (int i = 0; i < SIZE; ++i) {
1159 >            assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1160          }
1161 +        assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1162      }
1163  
1164      /**
1165       * Interrupted timed pollLast throws InterruptedException instead of
1166       * returning timeout status
1167       */
1168 <    public void testInterruptedTimedPollLast() {
1169 <        Thread t = new Thread(new Runnable() {
1170 <                public void run() {
1171 <                    try {
1172 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1173 <                        for (int i = 0; i < SIZE; ++i) {
1174 <                            threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1175 <                        }
1176 <                        threadAssertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1177 <                    } catch (InterruptedException success) {
1178 <                    }
1179 <                }});
1168 >    public void testInterruptedTimedPollLast() throws InterruptedException {
1169 >        Thread t = new Thread(new CheckedRunnable() {
1170 >            public void realRun() throws InterruptedException {
1171 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1172 >                for (int i = 0; i < SIZE; ++i) {
1173 >                    assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1174 >                }
1175 >                try {
1176 >                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1177 >                    shouldThrow();
1178 >                } catch (InterruptedException success) {}
1179 >            }});
1180 >
1181          t.start();
1182 <        try {
1183 <           Thread.sleep(SHORT_DELAY_MS);
1184 <           t.interrupt();
1363 <           t.join();
1364 <        }
1365 <        catch (InterruptedException ie) {
1366 <            unexpectedException();
1367 <        }
1182 >        Thread.sleep(SHORT_DELAY_MS);
1183 >        t.interrupt();
1184 >        t.join();
1185      }
1186  
1187      /**
1188       *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1189       *  on interruption throws
1190       */
1191 <    public void testTimedPollWithOfferLast() {
1191 >    public void testTimedPollWithOfferLast() throws InterruptedException {
1192          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1193 <        Thread t = new Thread(new Runnable() {
1194 <                public void run() {
1195 <                    try {
1196 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1197 <                        q.poll(LONG_DELAY_MS, MILLISECONDS);
1198 <                        q.poll(LONG_DELAY_MS, MILLISECONDS);
1199 <                        threadShouldThrow();
1200 <                    } catch (InterruptedException success) { }
1201 <                }
1202 <            });
1203 <        try {
1204 <            t.start();
1205 <            Thread.sleep(SMALL_DELAY_MS);
1206 <            assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1207 <            t.interrupt();
1391 <            t.join();
1392 <        } catch (Exception e) {
1393 <            unexpectedException();
1394 <        }
1193 >        Thread t = new Thread(new CheckedRunnable() {
1194 >            public void realRun() throws InterruptedException {
1195 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1196 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1197 >                try {
1198 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1199 >                    shouldThrow();
1200 >                } catch (InterruptedException success) {}
1201 >            }});
1202 >
1203 >        t.start();
1204 >        Thread.sleep(SMALL_DELAY_MS);
1205 >        assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1206 >        t.interrupt();
1207 >        t.join();
1208      }
1209  
1210  
# Line 1407 | Line 1220 | public class LinkedBlockingDequeTest ext
1220          try {
1221              q.element();
1222              shouldThrow();
1223 <        }
1411 <        catch (NoSuchElementException success) {}
1223 >        } catch (NoSuchElementException success) {}
1224      }
1225  
1226      /**
# Line 1506 | Line 1318 | public class LinkedBlockingDequeTest ext
1318      /**
1319       * toArray contains all elements
1320       */
1321 <    public void testToArray() {
1321 >    public void testToArray() throws InterruptedException{
1322          LinkedBlockingDeque q = populatedDeque(SIZE);
1323          Object[] o = q.toArray();
1512        try {
1324          for (int i = 0; i < o.length; i++)
1325              assertEquals(o[i], q.take());
1515        } catch (InterruptedException e) {
1516            unexpectedException();
1517        }
1326      }
1327  
1328      /**
1329       * toArray(a) contains all elements
1330       */
1331 <    public void testToArray2() {
1331 >    public void testToArray2() throws InterruptedException {
1332          LinkedBlockingDeque q = populatedDeque(SIZE);
1333          Integer[] ints = new Integer[SIZE];
1334          ints = (Integer[])q.toArray(ints);
1335 <        try {
1336 <            for (int i = 0; i < ints.length; i++)
1529 <                assertEquals(ints[i], q.take());
1530 <        } catch (InterruptedException e) {
1531 <            unexpectedException();
1532 <        }
1335 >        for (int i = 0; i < ints.length; i++)
1336 >            assertEquals(ints[i], q.take());
1337      }
1338  
1339      /**
# Line 1551 | Line 1355 | public class LinkedBlockingDequeTest ext
1355              LinkedBlockingDeque q = populatedDeque(SIZE);
1356              Object o[] = q.toArray(new String[10] );
1357              shouldThrow();
1358 <        } catch (ArrayStoreException  success) {}
1358 >        } catch (ArrayStoreException success) {}
1359      }
1360  
1361  
1362      /**
1363       * iterator iterates through all elements
1364       */
1365 <    public void testIterator() {
1365 >    public void testIterator() throws InterruptedException {
1366          LinkedBlockingDeque q = populatedDeque(SIZE);
1367          Iterator it = q.iterator();
1368 <        try {
1369 <            while (it.hasNext()) {
1566 <                assertEquals(it.next(), q.take());
1567 <            }
1568 <        } catch (InterruptedException e) {
1569 <            unexpectedException();
1368 >        while (it.hasNext()) {
1369 >            assertEquals(it.next(), q.take());
1370          }
1371      }
1372  
# Line 1615 | Line 1415 | public class LinkedBlockingDequeTest ext
1415          q.add(one);
1416          q.add(two);
1417          q.add(three);
1418 <        try {
1419 <            for (Iterator it = q.iterator(); it.hasNext();) {
1420 <                q.remove();
1621 <                it.next();
1622 <            }
1623 <        }
1624 <        catch (ConcurrentModificationException e) {
1625 <            unexpectedException();
1418 >        for (Iterator it = q.iterator(); it.hasNext();) {
1419 >            q.remove();
1420 >            it.next();
1421          }
1422          assertEquals(0, q.size());
1423      }
# Line 1643 | Line 1438 | public class LinkedBlockingDequeTest ext
1438          assertFalse(it.hasNext());
1439          try {
1440              it.next();
1441 <        } catch (NoSuchElementException success) {
1442 <        }
1441 >            shouldThrow();
1442 >        } catch (NoSuchElementException success) {}
1443      }
1444  
1445      /**
# Line 1712 | Line 1507 | public class LinkedBlockingDequeTest ext
1507          q.add(one);
1508          q.add(two);
1509          ExecutorService executor = Executors.newFixedThreadPool(2);
1510 <        executor.execute(new Runnable() {
1511 <            public void run() {
1510 >        executor.execute(new CheckedRunnable() {
1511 >            public void realRun() throws InterruptedException {
1512                  threadAssertFalse(q.offer(three));
1513 <                try {
1514 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1515 <                    threadAssertEquals(0, q.remainingCapacity());
1516 <                }
1517 <                catch (InterruptedException e) {
1518 <                    threadUnexpectedException();
1519 <                }
1520 <            }
1521 <        });
1727 <
1728 <        executor.execute(new Runnable() {
1729 <            public void run() {
1730 <                try {
1731 <                    Thread.sleep(SMALL_DELAY_MS);
1732 <                    threadAssertEquals(one, q.take());
1733 <                }
1734 <                catch (InterruptedException e) {
1735 <                    threadUnexpectedException();
1736 <                }
1737 <            }
1738 <        });
1513 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1514 >                threadAssertEquals(0, q.remainingCapacity());
1515 >            }});
1516 >
1517 >        executor.execute(new CheckedRunnable() {
1518 >            public void realRun() throws InterruptedException {
1519 >                Thread.sleep(SMALL_DELAY_MS);
1520 >                threadAssertEquals(one, q.take());
1521 >            }});
1522  
1523          joinPool(executor);
1524      }
# Line 1746 | Line 1529 | public class LinkedBlockingDequeTest ext
1529      public void testPollInExecutor() {
1530          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1531          ExecutorService executor = Executors.newFixedThreadPool(2);
1532 <        executor.execute(new Runnable() {
1533 <            public void run() {
1532 >        executor.execute(new CheckedRunnable() {
1533 >            public void realRun() throws InterruptedException {
1534                  threadAssertNull(q.poll());
1535 <                try {
1536 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1537 <                    threadAssertTrue(q.isEmpty());
1538 <                }
1539 <                catch (InterruptedException e) {
1540 <                    threadUnexpectedException();
1541 <                }
1542 <            }
1543 <        });
1761 <
1762 <        executor.execute(new Runnable() {
1763 <            public void run() {
1764 <                try {
1765 <                    Thread.sleep(SMALL_DELAY_MS);
1766 <                    q.put(one);
1767 <                }
1768 <                catch (InterruptedException e) {
1769 <                    threadUnexpectedException();
1770 <                }
1771 <            }
1772 <        });
1535 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1536 >                threadAssertTrue(q.isEmpty());
1537 >            }});
1538 >
1539 >        executor.execute(new CheckedRunnable() {
1540 >            public void realRun() throws InterruptedException {
1541 >                Thread.sleep(SMALL_DELAY_MS);
1542 >                q.put(one);
1543 >            }});
1544  
1545          joinPool(executor);
1546      }
# Line 1777 | Line 1548 | public class LinkedBlockingDequeTest ext
1548      /**
1549       * A deserialized serialized deque has same elements in same order
1550       */
1551 <    public void testSerialization() {
1551 >    public void testSerialization() throws Exception {
1552          LinkedBlockingDeque q = populatedDeque(SIZE);
1553  
1554 <        try {
1555 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1556 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1557 <            out.writeObject(q);
1558 <            out.close();
1559 <
1560 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1561 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1562 <            LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1563 <            assertEquals(q.size(), r.size());
1564 <            while (!q.isEmpty())
1794 <                assertEquals(q.remove(), r.remove());
1795 <        } catch (Exception e) {
1796 <            unexpectedException();
1797 <        }
1554 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1555 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1556 >        out.writeObject(q);
1557 >        out.close();
1558 >
1559 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1560 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1561 >        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1562 >        assertEquals(q.size(), r.size());
1563 >        while (!q.isEmpty())
1564 >            assertEquals(q.remove(), r.remove());
1565      }
1566  
1567      /**
# Line 1805 | Line 1572 | public class LinkedBlockingDequeTest ext
1572          try {
1573              q.drainTo(null);
1574              shouldThrow();
1575 <        } catch (NullPointerException success) {
1809 <        }
1575 >        } catch (NullPointerException success) {}
1576      }
1577  
1578      /**
# Line 1817 | Line 1583 | public class LinkedBlockingDequeTest ext
1583          try {
1584              q.drainTo(q);
1585              shouldThrow();
1586 <        } catch (IllegalArgumentException success) {
1821 <        }
1586 >        } catch (IllegalArgumentException success) {}
1587      }
1588  
1589      /**
# Line 1848 | Line 1613 | public class LinkedBlockingDequeTest ext
1613      /**
1614       * drainTo empties full deque, unblocking a waiting put.
1615       */
1616 <    public void testDrainToWithActivePut() {
1616 >    public void testDrainToWithActivePut() throws InterruptedException {
1617          final LinkedBlockingDeque q = populatedDeque(SIZE);
1618 <        Thread t = new Thread(new Runnable() {
1619 <                public void run() {
1620 <                    try {
1621 <                        q.put(new Integer(SIZE+1));
1622 <                    } catch (InterruptedException ie) {
1623 <                        threadUnexpectedException();
1624 <                    }
1625 <                }
1626 <            });
1627 <        try {
1628 <            t.start();
1629 <            ArrayList l = new ArrayList();
1630 <            q.drainTo(l);
1866 <            assertTrue(l.size() >= SIZE);
1867 <            for (int i = 0; i < SIZE; ++i)
1868 <                assertEquals(l.get(i), new Integer(i));
1869 <            t.join();
1870 <            assertTrue(q.size() + l.size() >= SIZE);
1871 <        } catch (Exception e) {
1872 <            unexpectedException();
1873 <        }
1618 >        Thread t = new Thread(new CheckedRunnable() {
1619 >            public void realRun() throws InterruptedException {
1620 >                q.put(new Integer(SIZE+1));
1621 >            }});
1622 >
1623 >        t.start();
1624 >        ArrayList l = new ArrayList();
1625 >        q.drainTo(l);
1626 >        assertTrue(l.size() >= SIZE);
1627 >        for (int i = 0; i < SIZE; ++i)
1628 >            assertEquals(l.get(i), new Integer(i));
1629 >        t.join();
1630 >        assertTrue(q.size() + l.size() >= SIZE);
1631      }
1632  
1633      /**
# Line 1881 | Line 1638 | public class LinkedBlockingDequeTest ext
1638          try {
1639              q.drainTo(null, 0);
1640              shouldThrow();
1641 <        } catch (NullPointerException success) {
1885 <        }
1641 >        } catch (NullPointerException success) {}
1642      }
1643  
1644      /**
# Line 1893 | Line 1649 | public class LinkedBlockingDequeTest ext
1649          try {
1650              q.drainTo(q, 0);
1651              shouldThrow();
1652 <        } catch (IllegalArgumentException success) {
1897 <        }
1652 >        } catch (IllegalArgumentException success) {}
1653      }
1654  
1655      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines