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

Comparing jsr166/src/test/tck/LinkedBlockingDequeTest.java (file contents):
Revision 1.3 by dl, Fri Sep 16 11:16:03 2005 UTC vs.
Revision 1.16 by jsr166, Sat Nov 21 22:00:46 2009 UTC

# Line 7 | Line 7
7   import junit.framework.*;
8   import java.util.*;
9   import java.util.concurrent.*;
10 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
11   import java.io.*;
12  
13   public class LinkedBlockingDequeTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run (suite());  
15 >        junit.textui.TestRunner.run (suite());
16      }
17  
18      public static Test suite() {
19 <        return new TestSuite(LinkedBlockingDequeTest.class);
19 >        return new TestSuite(LinkedBlockingDequeTest.class);
20      }
21  
22      /**
# Line 25 | Line 26 | public class LinkedBlockingDequeTest ext
26      private LinkedBlockingDeque populatedDeque(int n) {
27          LinkedBlockingDeque q = new LinkedBlockingDeque(n);
28          assertTrue(q.isEmpty());
29 <        for(int i = 0; i < n; i++)
30 <            assertTrue(q.offer(new Integer(i)));
29 >        for (int i = 0; i < n; i++)
30 >            assertTrue(q.offer(new Integer(i)));
31          assertFalse(q.isEmpty());
32          assertEquals(0, q.remainingCapacity());
33 <        assertEquals(n, q.size());
33 >        assertEquals(n, q.size());
34          return q;
35      }
36  
# Line 66 | Line 67 | public class LinkedBlockingDequeTest ext
67       * offer(null) throws NPE
68       */
69      public void testOfferFirstNull() {
70 <        try {
70 >        try {
71              LinkedBlockingDeque q = new LinkedBlockingDeque();
72              q.offerFirst(null);
73              shouldThrow();
74 <        } catch (NullPointerException success) {
74 <        }  
74 >        } catch (NullPointerException success) {}
75      }
76  
77      /**
78 <     * OfferFirst succeeds
78 >     * OfferFirst succeeds
79       */
80      public void testOfferFirst() {
81          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 84 | Line 84 | public class LinkedBlockingDequeTest ext
84      }
85  
86      /**
87 <     * OfferLast succeeds
87 >     * OfferLast succeeds
88       */
89      public void testOfferLast() {
90          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 100 | Line 100 | public class LinkedBlockingDequeTest ext
100          for (int i = 0; i < SIZE; ++i) {
101              assertEquals(i, ((Integer)q.pollFirst()).intValue());
102          }
103 <        assertNull(q.pollFirst());
103 >        assertNull(q.pollFirst());
104      }
105  
106      /**
# Line 111 | Line 111 | public class LinkedBlockingDequeTest ext
111          for (int i = SIZE-1; i >= 0; --i) {
112              assertEquals(i, ((Integer)q.pollLast()).intValue());
113          }
114 <        assertNull(q.pollLast());
114 >        assertNull(q.pollLast());
115      }
116  
117      /**
# Line 125 | Line 125 | public class LinkedBlockingDequeTest ext
125              assertTrue(q.peekFirst() == null ||
126                         i != ((Integer)q.peekFirst()).intValue());
127          }
128 <        assertNull(q.peekFirst());
128 >        assertNull(q.peekFirst());
129      }
130  
131      /**
# Line 139 | Line 139 | public class LinkedBlockingDequeTest ext
139              assertTrue(q.peek() == null ||
140                         i != ((Integer)q.peek()).intValue());
141          }
142 <        assertNull(q.peek());
142 >        assertNull(q.peek());
143      }
144  
145      /**
# Line 153 | Line 153 | public class LinkedBlockingDequeTest ext
153              assertTrue(q.peekLast() == null ||
154                         i != ((Integer)q.peekLast()).intValue());
155          }
156 <        assertNull(q.peekLast());
156 >        assertNull(q.peekLast());
157      }
158  
159      /**
# Line 168 | Line 168 | public class LinkedBlockingDequeTest ext
168          try {
169              q.getFirst();
170              shouldThrow();
171 <        }
172 <        catch (NoSuchElementException success) {}
171 >        } catch (NoSuchElementException success) {}
172 >        assertNull(q.peekFirst());
173      }
174  
175      /**
# Line 184 | Line 184 | public class LinkedBlockingDequeTest ext
184          try {
185              q.getLast();
186              shouldThrow();
187 <        }
188 <        catch (NoSuchElementException success) {}
189 <        assertNull(q.peekLast());
187 >        } catch (NoSuchElementException success) {}
188 >        assertNull(q.peekLast());
189      }
190  
191      /**
# Line 200 | Line 199 | public class LinkedBlockingDequeTest ext
199          try {
200              q.removeFirst();
201              shouldThrow();
202 <        } catch (NoSuchElementException success){
203 <        }  
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 215 | Line 229 | public class LinkedBlockingDequeTest ext
229          try {
230              q.remove();
231              shouldThrow();
232 <        } catch (NoSuchElementException success){
219 <        }  
232 >        } catch (NoSuchElementException success) {}
233      }
234  
235      /**
# Line 255 | Line 268 | public class LinkedBlockingDequeTest ext
268      public void testAddFirst() {
269          LinkedBlockingDeque q = populatedDeque(3);
270          q.pollLast();
271 <        q.addFirst(four);
272 <        assertEquals(four,q.peekFirst());
273 <    }  
271 >        q.addFirst(four);
272 >        assertEquals(four,q.peekFirst());
273 >    }
274  
275      /**
276       * peekLast returns element inserted with addLast
# Line 265 | Line 278 | public class LinkedBlockingDequeTest ext
278      public void testAddLast() {
279          LinkedBlockingDeque q = populatedDeque(3);
280          q.pollLast();
281 <        q.addLast(four);
282 <        assertEquals(four,q.peekLast());
283 <    }  
281 >        q.addLast(four);
282 >        assertEquals(four,q.peekLast());
283 >    }
284  
285  
286      /**
# Line 280 | 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 <        }
290 <        catch (IllegalArgumentException success) {}
302 >        } catch (IllegalArgumentException success) {}
303      }
304  
305      /**
# Line 297 | Line 309 | public class LinkedBlockingDequeTest ext
309          try {
310              LinkedBlockingDeque q = new LinkedBlockingDeque(null);
311              shouldThrow();
312 <        }
301 <        catch (NullPointerException success) {}
312 >        } catch (NullPointerException success) {}
313      }
314  
315      /**
# Line 309 | Line 320 | public class LinkedBlockingDequeTest ext
320              Integer[] ints = new Integer[SIZE];
321              LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
322              shouldThrow();
323 <        }
313 <        catch (NullPointerException success) {}
323 >        } catch (NullPointerException success) {}
324      }
325  
326      /**
# Line 323 | Line 333 | public class LinkedBlockingDequeTest ext
333                  ints[i] = new Integer(i);
334              LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
335              shouldThrow();
336 <        }
327 <        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)
340 <                assertEquals(ints[i], q.poll());
341 <        }
342 <        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 378 | Line 384 | public class LinkedBlockingDequeTest ext
384       * offer(null) throws NPE
385       */
386      public void testOfferNull() {
387 <        try {
387 >        try {
388              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
389              q.offer(null);
390              shouldThrow();
391 <        } catch (NullPointerException success) { }  
391 >        } catch (NullPointerException success) {}
392      }
393  
394      /**
395       * add(null) throws NPE
396       */
397      public void testAddNull() {
398 <        try {
398 >        try {
399              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
400              q.add(null);
401              shouldThrow();
402 <        } catch (NullPointerException success) { }  
402 >        } catch (NullPointerException success) {}
403      }
404  
405      /**
406       * push(null) throws NPE
407       */
408      public void testPushNull() {
409 <        try {
409 >        try {
410              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
411              q.push(null);
412              shouldThrow();
413 <        } catch (NullPointerException success) { }  
413 >        } catch (NullPointerException success) {}
414      }
415  
416      /**
417       * push succeeds if not full; throws ISE if full
418       */
419      public void testPush() {
420 <        try {
420 >        try {
421              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
422              for (int i = 0; i < SIZE; ++i) {
423                  Integer I = new Integer(i);
# Line 420 | 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 430 | Line 436 | public class LinkedBlockingDequeTest ext
436      public void testPushWithPeek() {
437          LinkedBlockingDeque q = populatedDeque(3);
438          q.pollLast();
439 <        q.push(four);
440 <        assertEquals(four,q.peekFirst());
441 <    }  
439 >        q.push(four);
440 >        assertEquals(four,q.peekFirst());
441 >    }
442  
443  
444      /**
# Line 446 | Line 452 | public class LinkedBlockingDequeTest ext
452          try {
453              q.pop();
454              shouldThrow();
455 <        } catch (NoSuchElementException success){
450 <        }  
455 >        } catch (NoSuchElementException success) {}
456      }
457  
458  
# Line 464 | Line 469 | public class LinkedBlockingDequeTest ext
469       * add succeeds if not full; throws ISE if full
470       */
471      public void testAdd() {
472 <        try {
472 >        try {
473              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
474              for (int i = 0; i < SIZE; ++i) {
475                  assertTrue(q.add(new Integer(i)));
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 483 | Line 488 | public class LinkedBlockingDequeTest ext
488              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
489              q.addAll(null);
490              shouldThrow();
491 <        }
487 <        catch (NullPointerException success) {}
491 >        } catch (NullPointerException success) {}
492      }
493  
494      /**
# Line 495 | Line 499 | public class LinkedBlockingDequeTest ext
499              LinkedBlockingDeque q = populatedDeque(SIZE);
500              q.addAll(q);
501              shouldThrow();
502 <        }
499 <        catch (IllegalArgumentException success) {}
502 >        } catch (IllegalArgumentException success) {}
503      }
504  
505      /**
# Line 508 | Line 511 | public class LinkedBlockingDequeTest ext
511              Integer[] ints = new Integer[SIZE];
512              q.addAll(Arrays.asList(ints));
513              shouldThrow();
514 <        }
512 <        catch (NullPointerException success) {}
514 >        } catch (NullPointerException success) {}
515      }
516      /**
517       * addAll of a collection with any null elements throws NPE after
# Line 523 | Line 525 | public class LinkedBlockingDequeTest ext
525                  ints[i] = new Integer(i);
526              q.addAll(Arrays.asList(ints));
527              shouldThrow();
528 <        }
527 <        catch (NullPointerException success) {}
528 >        } catch (NullPointerException success) {}
529      }
530      /**
531       * addAll throws ISE if not enough room
# Line 537 | Line 538 | public class LinkedBlockingDequeTest ext
538                  ints[i] = new Integer(i);
539              q.addAll(Arrays.asList(ints));
540              shouldThrow();
541 <        }
541 <        catch (IllegalStateException success) {}
541 >        } catch (IllegalStateException success) {}
542      }
543 +
544      /**
545       * Deque contains all elements, in traversal order, of successful addAll
546       */
547      public void testAddAll5() {
548 <        try {
549 <            Integer[] empty = new Integer[0];
550 <            Integer[] ints = new Integer[SIZE];
551 <            for (int i = 0; i < SIZE; ++i)
552 <                ints[i] = new Integer(i);
553 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
554 <            assertFalse(q.addAll(Arrays.asList(empty)));
555 <            assertTrue(q.addAll(Arrays.asList(ints)));
556 <            for (int i = 0; i < SIZE; ++i)
556 <                assertEquals(ints[i], q.poll());
557 <        }
558 <        finally {}
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)
556 >            assertEquals(ints[i], q.poll());
557      }
558  
559  
560      /**
561       * put(null) throws NPE
562       */
563 <     public void testPutNull() {
564 <        try {
563 >    public void testPutNull() throws InterruptedException {
564 >        try {
565              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
566              q.put(null);
567              shouldThrow();
568 <        }
569 <        catch (NullPointerException success){
572 <        }  
573 <        catch (InterruptedException ie) {
574 <            unexpectedException();
575 <        }
576 <     }
568 >        } catch (NullPointerException success) {}
569 >    }
570  
571      /**
572       * all elements successfully put are contained
573       */
574 <     public void testPut() {
575 <         try {
576 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
577 <             for (int i = 0; i < SIZE; ++i) {
578 <                 Integer I = new Integer(i);
579 <                 q.put(I);
587 <                 assertTrue(q.contains(I));
588 <             }
589 <             assertEquals(0, q.remainingCapacity());
590 <         }
591 <        catch (InterruptedException ie) {
592 <            unexpectedException();
574 >    public void testPut() throws InterruptedException {
575 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
576 >        for (int i = 0; i < SIZE; ++i) {
577 >            Integer I = new Integer(i);
578 >            q.put(I);
579 >            assertTrue(q.contains(I));
580          }
581 +        assertEquals(0, q.remainingCapacity());
582      }
583  
584      /**
585       * put blocks interruptibly if full
586       */
587 <    public void testBlockingPut() {
588 <        Thread t = new Thread(new Runnable() {
589 <                public void run() {
590 <                    int added = 0;
591 <                    try {
592 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
593 <                        for (int i = 0; i < SIZE; ++i) {
594 <                            q.put(new Integer(i));
595 <                            ++added;
596 <                        }
597 <                        q.put(new Integer(SIZE));
598 <                        threadShouldThrow();
599 <                    } catch (InterruptedException ie){
600 <                        threadAssertEquals(added, SIZE);
601 <                    }  
602 <                }});
603 <        t.start();
604 <        try {
605 <           Thread.sleep(SHORT_DELAY_MS);
606 <           t.interrupt();
607 <           t.join();
620 <        }
621 <        catch (InterruptedException ie) {
622 <            unexpectedException();
623 <        }
587 >    public void testBlockingPut() throws InterruptedException {
588 >        Thread t = new Thread(new CheckedRunnable() {
589 >            public void realRun() {
590 >                int added = 0;
591 >                try {
592 >                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
593 >                    for (int i = 0; i < SIZE; ++i) {
594 >                        q.put(new Integer(i));
595 >                        ++added;
596 >                    }
597 >                    q.put(new Integer(SIZE));
598 >                    threadShouldThrow();
599 >                } catch (InterruptedException success) {
600 >                    threadAssertEquals(added, SIZE);
601 >                }
602 >            }});
603 >
604 >        t.start();
605 >        Thread.sleep(SHORT_DELAY_MS);
606 >        t.interrupt();
607 >        t.join();
608      }
609  
610      /**
611       * put blocks waiting for take when full
612       */
613 <    public void testPutWithTake() {
613 >    public void testPutWithTake() throws InterruptedException {
614          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
615 <        Thread t = new Thread(new Runnable() {
616 <                public void run() {
617 <                    int added = 0;
618 <                    try {
619 <                        q.put(new Object());
620 <                        ++added;
621 <                        q.put(new Object());
622 <                        ++added;
623 <                        q.put(new Object());
624 <                        ++added;
625 <                        q.put(new Object());
626 <                        ++added;
627 <                        threadShouldThrow();
628 <                    } catch (InterruptedException e){
629 <                        threadAssertTrue(added >= 2);
646 <                    }
615 >        Thread t = new Thread(new CheckedRunnable() {
616 >            public void realRun() {
617 >                int added = 0;
618 >                try {
619 >                    q.put(new Object());
620 >                    ++added;
621 >                    q.put(new Object());
622 >                    ++added;
623 >                    q.put(new Object());
624 >                    ++added;
625 >                    q.put(new Object());
626 >                    ++added;
627 >                    threadShouldThrow();
628 >                } catch (InterruptedException success) {
629 >                    threadAssertTrue(added >= 2);
630                  }
631 <            });
632 <        try {
633 <            t.start();
634 <            Thread.sleep(SHORT_DELAY_MS);
635 <            q.take();
636 <            t.interrupt();
637 <            t.join();
655 <        } catch (Exception e){
656 <            unexpectedException();
657 <        }
631 >            }});
632 >
633 >        t.start();
634 >        Thread.sleep(SHORT_DELAY_MS);
635 >        q.take();
636 >        t.interrupt();
637 >        t.join();
638      }
639  
640      /**
641       * timed offer times out if full and elements not taken
642       */
643 <    public void testTimedOffer() {
643 >    public void testTimedOffer() throws InterruptedException {
644          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
645 <        Thread t = new Thread(new Runnable() {
646 <                public void run() {
647 <                    try {
648 <                        q.put(new Object());
649 <                        q.put(new Object());
650 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
651 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
652 <                        threadShouldThrow();
653 <                    } catch (InterruptedException success){}
654 <                }
655 <            });
656 <        
657 <        try {
658 <            t.start();
659 <            Thread.sleep(SMALL_DELAY_MS);
680 <            t.interrupt();
681 <            t.join();
682 <        } catch (Exception e){
683 <            unexpectedException();
684 <        }
645 >        Thread t = new Thread(new CheckedRunnable() {
646 >            public void realRun() throws InterruptedException {
647 >                q.put(new Object());
648 >                q.put(new Object());
649 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
650 >                try {
651 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
652 >                    shouldThrow();
653 >                } catch (InterruptedException success) {}
654 >            }});
655 >
656 >        t.start();
657 >        Thread.sleep(SMALL_DELAY_MS);
658 >        t.interrupt();
659 >        t.join();
660      }
661  
662      /**
663       * take retrieves elements in FIFO order
664       */
665 <    public void testTake() {
666 <        try {
667 <            LinkedBlockingDeque q = populatedDeque(SIZE);
668 <            for (int i = 0; i < SIZE; ++i) {
669 <                assertEquals(i, ((Integer)q.take()).intValue());
695 <            }
696 <        } catch (InterruptedException e){
697 <            unexpectedException();
698 <        }  
665 >    public void testTake() throws InterruptedException {
666 >        LinkedBlockingDeque q = populatedDeque(SIZE);
667 >        for (int i = 0; i < SIZE; ++i) {
668 >            assertEquals(i, ((Integer)q.take()).intValue());
669 >        }
670      }
671  
672      /**
673       * take blocks interruptibly when empty
674       */
675 <    public void testTakeFromEmpty() {
675 >    public void testTakeFromEmpty() throws InterruptedException {
676          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
677 <        Thread t = new Thread(new Runnable() {
678 <                public void run() {
679 <                    try {
680 <                        q.take();
681 <                        threadShouldThrow();
682 <                    } catch (InterruptedException success){ }                
683 <                }
684 <            });
685 <        try {
715 <            t.start();
716 <            Thread.sleep(SHORT_DELAY_MS);
717 <            t.interrupt();
718 <            t.join();
719 <        } catch (Exception e){
720 <            unexpectedException();
721 <        }
677 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
678 >            public void realRun() throws InterruptedException {
679 >                q.take();
680 >            }};
681 >
682 >        t.start();
683 >        Thread.sleep(SHORT_DELAY_MS);
684 >        t.interrupt();
685 >        t.join();
686      }
687  
688      /**
689       * Take removes existing elements until empty, then blocks interruptibly
690       */
691 <    public void testBlockingTake() {
692 <        Thread t = new Thread(new Runnable() {
693 <                public void run() {
694 <                    try {
695 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
696 <                        for (int i = 0; i < SIZE; ++i) {
697 <                            assertEquals(i, ((Integer)q.take()).intValue());
698 <                        }
699 <                        q.take();
700 <                        threadShouldThrow();
701 <                    } catch (InterruptedException success){
702 <                    }  
703 <                }});
704 <        t.start();
741 <        try {
742 <           Thread.sleep(SHORT_DELAY_MS);
743 <           t.interrupt();
744 <           t.join();
745 <        }
746 <        catch (InterruptedException ie) {
747 <            unexpectedException();
748 <        }
691 >    public void testBlockingTake() throws InterruptedException {
692 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
693 >            public void realRun() throws InterruptedException {
694 >                LinkedBlockingDeque q = populatedDeque(SIZE);
695 >                for (int i = 0; i < SIZE; ++i) {
696 >                    assertEquals(i, ((Integer)q.take()).intValue());
697 >                }
698 >                q.take();
699 >            }};
700 >
701 >        t.start();
702 >        Thread.sleep(SHORT_DELAY_MS);
703 >        t.interrupt();
704 >        t.join();
705      }
706  
707  
# Line 757 | Line 713 | public class LinkedBlockingDequeTest ext
713          for (int i = 0; i < SIZE; ++i) {
714              assertEquals(i, ((Integer)q.poll()).intValue());
715          }
716 <        assertNull(q.poll());
716 >        assertNull(q.poll());
717      }
718  
719      /**
720       * timed poll with zero timeout succeeds when non-empty, else times out
721       */
722 <    public void testTimedPoll0() {
723 <        try {
724 <            LinkedBlockingDeque q = populatedDeque(SIZE);
725 <            for (int i = 0; i < SIZE; ++i) {
726 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
727 <            }
772 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
773 <        } catch (InterruptedException e){
774 <            unexpectedException();
775 <        }  
722 >    public void testTimedPoll0() throws InterruptedException {
723 >        LinkedBlockingDeque q = populatedDeque(SIZE);
724 >        for (int i = 0; i < SIZE; ++i) {
725 >            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
726 >        }
727 >        assertNull(q.poll(0, MILLISECONDS));
728      }
729  
730      /**
731       * timed poll with nonzero timeout succeeds when non-empty, else times out
732       */
733 <    public void testTimedPoll() {
734 <        try {
735 <            LinkedBlockingDeque q = populatedDeque(SIZE);
736 <            for (int i = 0; i < SIZE; ++i) {
737 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
738 <            }
787 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
788 <        } catch (InterruptedException e){
789 <            unexpectedException();
790 <        }  
733 >    public void testTimedPoll() throws InterruptedException {
734 >        LinkedBlockingDeque q = populatedDeque(SIZE);
735 >        for (int i = 0; i < SIZE; ++i) {
736 >            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
737 >        }
738 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
739      }
740  
741      /**
742       * Interrupted timed poll throws InterruptedException instead of
743       * returning timeout status
744       */
745 <    public void testInterruptedTimedPoll() {
746 <        Thread t = new Thread(new Runnable() {
747 <                public void run() {
748 <                    try {
749 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
750 <                        for (int i = 0; i < SIZE; ++i) {
751 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
752 <                        }
753 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
754 <                    } catch (InterruptedException success){
755 <                    }  
756 <                }});
757 <        t.start();
758 <        try {
759 <           Thread.sleep(SHORT_DELAY_MS);
760 <           t.interrupt();
761 <           t.join();
814 <        }
815 <        catch (InterruptedException ie) {
816 <            unexpectedException();
817 <        }
745 >    public void testInterruptedTimedPoll() throws InterruptedException {
746 >        Thread t = new Thread(new CheckedRunnable() {
747 >            public void realRun() throws InterruptedException {
748 >                LinkedBlockingDeque q = populatedDeque(SIZE);
749 >                for (int i = 0; i < SIZE; ++i) {
750 >                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
751 >                }
752 >                try {
753 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
754 >                    shouldThrow();
755 >                } catch (InterruptedException success) {}
756 >            }});
757 >
758 >        t.start();
759 >        Thread.sleep(SHORT_DELAY_MS);
760 >        t.interrupt();
761 >        t.join();
762      }
763  
764      /**
765       *  timed poll before a delayed offer fails; after offer succeeds;
766       *  on interruption throws
767       */
768 <    public void testTimedPollWithOffer() {
768 >    public void testTimedPollWithOffer() throws InterruptedException {
769          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
770 <        Thread t = new Thread(new Runnable() {
771 <                public void run() {
772 <                    try {
773 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
774 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
775 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
776 <                        threadShouldThrow();
777 <                    } catch (InterruptedException success) { }                
778 <                }
779 <            });
780 <        try {
781 <            t.start();
782 <            Thread.sleep(SMALL_DELAY_MS);
783 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
784 <            t.interrupt();
785 <            t.join();
842 <        } catch (Exception e){
843 <            unexpectedException();
844 <        }
845 <    }  
770 >        Thread t = new Thread(new CheckedRunnable() {
771 >            public void realRun() throws InterruptedException {
772 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
773 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
774 >                try {
775 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
776 >                    shouldThrow();
777 >                } catch (InterruptedException success) {}
778 >            }});
779 >
780 >        t.start();
781 >        Thread.sleep(SMALL_DELAY_MS);
782 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
783 >        t.interrupt();
784 >        t.join();
785 >    }
786  
787  
788      /**
789       * putFirst(null) throws NPE
790       */
791 <     public void testPutFirstNull() {
792 <        try {
791 >     public void testPutFirstNull() throws InterruptedException {
792 >        try {
793              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
794              q.putFirst(null);
795              shouldThrow();
796 <        }
857 <        catch (NullPointerException success){
858 <        }  
859 <        catch (InterruptedException ie) {
860 <            unexpectedException();
861 <        }
796 >        } catch (NullPointerException success) {}
797       }
798  
799      /**
800       * all elements successfully putFirst are contained
801       */
802 <     public void testPutFirst() {
803 <         try {
804 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
805 <             for (int i = 0; i < SIZE; ++i) {
806 <                 Integer I = new Integer(i);
807 <                 q.putFirst(I);
873 <                 assertTrue(q.contains(I));
874 <             }
875 <             assertEquals(0, q.remainingCapacity());
802 >     public void testPutFirst() throws InterruptedException {
803 >         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
804 >         for (int i = 0; i < SIZE; ++i) {
805 >             Integer I = new Integer(i);
806 >             q.putFirst(I);
807 >             assertTrue(q.contains(I));
808           }
809 <        catch (InterruptedException ie) {
878 <            unexpectedException();
879 <        }
809 >         assertEquals(0, q.remainingCapacity());
810      }
811  
812      /**
813       * putFirst blocks interruptibly if full
814       */
815 <    public void testBlockingPutFirst() {
816 <        Thread t = new Thread(new Runnable() {
817 <                public void run() {
818 <                    int added = 0;
819 <                    try {
820 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
821 <                        for (int i = 0; i < SIZE; ++i) {
822 <                            q.putFirst(new Integer(i));
823 <                            ++added;
824 <                        }
825 <                        q.putFirst(new Integer(SIZE));
826 <                        threadShouldThrow();
827 <                    } catch (InterruptedException ie){
828 <                        threadAssertEquals(added, SIZE);
829 <                    }  
830 <                }});
831 <        t.start();
832 <        try {
833 <           Thread.sleep(SHORT_DELAY_MS);
834 <           t.interrupt();
835 <           t.join();
906 <        }
907 <        catch (InterruptedException ie) {
908 <            unexpectedException();
909 <        }
815 >    public void testBlockingPutFirst() throws InterruptedException {
816 >        Thread t = new Thread(new CheckedRunnable() {
817 >            public void realRun() {
818 >                int added = 0;
819 >                try {
820 >                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
821 >                    for (int i = 0; i < SIZE; ++i) {
822 >                        q.putFirst(new Integer(i));
823 >                        ++added;
824 >                    }
825 >                    q.putFirst(new Integer(SIZE));
826 >                    threadShouldThrow();
827 >                } catch (InterruptedException success) {
828 >                    threadAssertEquals(added, SIZE);
829 >                }
830 >            }});
831 >
832 >        t.start();
833 >        Thread.sleep(SHORT_DELAY_MS);
834 >        t.interrupt();
835 >        t.join();
836      }
837  
838      /**
839       * putFirst blocks waiting for take when full
840       */
841 <    public void testPutFirstWithTake() {
841 >    public void testPutFirstWithTake() throws InterruptedException {
842          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
843 <        Thread t = new Thread(new Runnable() {
844 <                public void run() {
845 <                    int added = 0;
846 <                    try {
847 <                        q.putFirst(new Object());
848 <                        ++added;
849 <                        q.putFirst(new Object());
850 <                        ++added;
851 <                        q.putFirst(new Object());
852 <                        ++added;
853 <                        q.putFirst(new Object());
854 <                        ++added;
855 <                        threadShouldThrow();
856 <                    } catch (InterruptedException e){
857 <                        threadAssertTrue(added >= 2);
932 <                    }
843 >        Thread t = new Thread(new CheckedRunnable() {
844 >            public void realRun() {
845 >                int added = 0;
846 >                try {
847 >                    q.putFirst(new Object());
848 >                    ++added;
849 >                    q.putFirst(new Object());
850 >                    ++added;
851 >                    q.putFirst(new Object());
852 >                    ++added;
853 >                    q.putFirst(new Object());
854 >                    ++added;
855 >                    threadShouldThrow();
856 >                } catch (InterruptedException success) {
857 >                    threadAssertTrue(added >= 2);
858                  }
859 <            });
860 <        try {
861 <            t.start();
862 <            Thread.sleep(SHORT_DELAY_MS);
863 <            q.take();
864 <            t.interrupt();
865 <            t.join();
941 <        } catch (Exception e){
942 <            unexpectedException();
943 <        }
859 >            }});
860 >
861 >        t.start();
862 >        Thread.sleep(SHORT_DELAY_MS);
863 >        q.take();
864 >        t.interrupt();
865 >        t.join();
866      }
867  
868      /**
869       * timed offerFirst times out if full and elements not taken
870       */
871 <    public void testTimedOfferFirst() {
871 >    public void testTimedOfferFirst() throws InterruptedException {
872          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
873 <        Thread t = new Thread(new Runnable() {
874 <                public void run() {
875 <                    try {
876 <                        q.putFirst(new Object());
877 <                        q.putFirst(new Object());
878 <                        threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
879 <                        q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
880 <                        threadShouldThrow();
881 <                    } catch (InterruptedException success){}
882 <                }
883 <            });
884 <        
885 <        try {
886 <            t.start();
887 <            Thread.sleep(SMALL_DELAY_MS);
966 <            t.interrupt();
967 <            t.join();
968 <        } catch (Exception e){
969 <            unexpectedException();
970 <        }
873 >        Thread t = new Thread(new CheckedRunnable() {
874 >            public void realRun() throws InterruptedException {
875 >                q.putFirst(new Object());
876 >                q.putFirst(new Object());
877 >                assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
878 >                try {
879 >                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
880 >                    shouldThrow();
881 >                } catch (InterruptedException success) {}
882 >            }});
883 >
884 >        t.start();
885 >        Thread.sleep(SMALL_DELAY_MS);
886 >        t.interrupt();
887 >        t.join();
888      }
889  
890      /**
891       * take retrieves elements in FIFO order
892       */
893 <    public void testTakeFirst() {
894 <        try {
895 <            LinkedBlockingDeque q = populatedDeque(SIZE);
896 <            for (int i = 0; i < SIZE; ++i) {
897 <                assertEquals(i, ((Integer)q.takeFirst()).intValue());
981 <            }
982 <        } catch (InterruptedException e){
983 <            unexpectedException();
984 <        }  
893 >    public void testTakeFirst() throws InterruptedException {
894 >        LinkedBlockingDeque q = populatedDeque(SIZE);
895 >        for (int i = 0; i < SIZE; ++i) {
896 >            assertEquals(i, ((Integer)q.takeFirst()).intValue());
897 >        }
898      }
899  
900      /**
901       * takeFirst blocks interruptibly when empty
902       */
903 <    public void testTakeFirstFromEmpty() {
903 >    public void testTakeFirstFromEmpty() throws InterruptedException {
904          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
905 <        Thread t = new Thread(new Runnable() {
906 <                public void run() {
907 <                    try {
908 <                        q.takeFirst();
909 <                        threadShouldThrow();
910 <                    } catch (InterruptedException success){ }                
911 <                }
912 <            });
913 <        try {
1001 <            t.start();
1002 <            Thread.sleep(SHORT_DELAY_MS);
1003 <            t.interrupt();
1004 <            t.join();
1005 <        } catch (Exception e){
1006 <            unexpectedException();
1007 <        }
905 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
906 >            public void realRun() throws InterruptedException {
907 >                q.takeFirst();
908 >            }};
909 >
910 >        t.start();
911 >        Thread.sleep(SHORT_DELAY_MS);
912 >        t.interrupt();
913 >        t.join();
914      }
915  
916      /**
917       * TakeFirst removes existing elements until empty, then blocks interruptibly
918       */
919 <    public void testBlockingTakeFirst() {
920 <        Thread t = new Thread(new Runnable() {
921 <                public void run() {
922 <                    try {
923 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
924 <                        for (int i = 0; i < SIZE; ++i) {
925 <                            assertEquals(i, ((Integer)q.takeFirst()).intValue());
926 <                        }
927 <                        q.takeFirst();
928 <                        threadShouldThrow();
929 <                    } catch (InterruptedException success){
930 <                    }  
931 <                }});
932 <        t.start();
1027 <        try {
1028 <           Thread.sleep(SHORT_DELAY_MS);
1029 <           t.interrupt();
1030 <           t.join();
1031 <        }
1032 <        catch (InterruptedException ie) {
1033 <            unexpectedException();
1034 <        }
919 >    public void testBlockingTakeFirst() throws InterruptedException {
920 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
921 >            public void realRun() throws InterruptedException {
922 >                LinkedBlockingDeque q = populatedDeque(SIZE);
923 >                for (int i = 0; i < SIZE; ++i) {
924 >                    assertEquals(i, ((Integer)q.takeFirst()).intValue());
925 >                }
926 >                q.takeFirst();
927 >            }};
928 >
929 >        t.start();
930 >        Thread.sleep(SHORT_DELAY_MS);
931 >        t.interrupt();
932 >        t.join();
933      }
934  
935  
936      /**
937       * timed pollFirst with zero timeout succeeds when non-empty, else times out
938       */
939 <    public void testTimedPollFirst0() {
940 <        try {
941 <            LinkedBlockingDeque q = populatedDeque(SIZE);
942 <            for (int i = 0; i < SIZE; ++i) {
943 <                assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue());
944 <            }
1047 <            assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS));
1048 <        } catch (InterruptedException e){
1049 <            unexpectedException();
1050 <        }  
939 >    public void testTimedPollFirst0() throws InterruptedException {
940 >        LinkedBlockingDeque q = populatedDeque(SIZE);
941 >        for (int i = 0; i < SIZE; ++i) {
942 >            assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
943 >        }
944 >        assertNull(q.pollFirst(0, MILLISECONDS));
945      }
946  
947      /**
948       * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
949       */
950 <    public void testTimedPollFirst() {
951 <        try {
952 <            LinkedBlockingDeque q = populatedDeque(SIZE);
953 <            for (int i = 0; i < SIZE; ++i) {
954 <                assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
955 <            }
1062 <            assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1063 <        } catch (InterruptedException e){
1064 <            unexpectedException();
1065 <        }  
950 >    public void testTimedPollFirst() throws InterruptedException {
951 >        LinkedBlockingDeque q = populatedDeque(SIZE);
952 >        for (int i = 0; i < SIZE; ++i) {
953 >            assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
954 >        }
955 >        assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
956      }
957  
958      /**
959       * Interrupted timed pollFirst throws InterruptedException instead of
960       * returning timeout status
961       */
962 <    public void testInterruptedTimedPollFirst() {
963 <        Thread t = new Thread(new Runnable() {
964 <                public void run() {
965 <                    try {
966 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
967 <                        for (int i = 0; i < SIZE; ++i) {
968 <                            threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
969 <                        }
970 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
971 <                    } catch (InterruptedException success){
972 <                    }  
973 <                }});
974 <        t.start();
975 <        try {
976 <           Thread.sleep(SHORT_DELAY_MS);
977 <           t.interrupt();
978 <           t.join();
1089 <        }
1090 <        catch (InterruptedException ie) {
1091 <            unexpectedException();
1092 <        }
962 >    public void testInterruptedTimedPollFirst() throws InterruptedException {
963 >        Thread t = new Thread(new CheckedRunnable() {
964 >            public void realRun() throws InterruptedException {
965 >                LinkedBlockingDeque q = populatedDeque(SIZE);
966 >                for (int i = 0; i < SIZE; ++i) {
967 >                    assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
968 >                }
969 >                try {
970 >                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
971 >                    shouldThrow();
972 >                } catch (InterruptedException success) {}
973 >            }});
974 >
975 >        t.start();
976 >        Thread.sleep(SHORT_DELAY_MS);
977 >        t.interrupt();
978 >        t.join();
979      }
980  
981      /**
982       *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
983       *  on interruption throws
984       */
985 <    public void testTimedPollFirstWithOfferFirst() {
985 >    public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
986          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
987 <        Thread t = new Thread(new Runnable() {
988 <                public void run() {
989 <                    try {
990 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
991 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
992 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
993 <                        threadShouldThrow();
994 <                    } catch (InterruptedException success) { }                
995 <                }
996 <            });
997 <        try {
998 <            t.start();
999 <            Thread.sleep(SMALL_DELAY_MS);
1000 <            assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1001 <            t.interrupt();
1002 <            t.join();
1117 <        } catch (Exception e){
1118 <            unexpectedException();
1119 <        }
1120 <    }  
987 >        Thread t = new Thread(new CheckedRunnable() {
988 >            public void realRun() throws InterruptedException {
989 >                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
990 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
991 >                try {
992 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
993 >                    shouldThrow();
994 >                } catch (InterruptedException success) {}
995 >            }});
996 >
997 >        t.start();
998 >        Thread.sleep(SMALL_DELAY_MS);
999 >        assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
1000 >        t.interrupt();
1001 >        t.join();
1002 >    }
1003  
1004      /**
1005       * putLast(null) throws NPE
1006       */
1007 <     public void testPutLastNull() {
1008 <        try {
1007 >     public void testPutLastNull() throws InterruptedException {
1008 >        try {
1009              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1010              q.putLast(null);
1011              shouldThrow();
1012 <        }
1131 <        catch (NullPointerException success){
1132 <        }  
1133 <        catch (InterruptedException ie) {
1134 <            unexpectedException();
1135 <        }
1012 >        } catch (NullPointerException success) {}
1013       }
1014  
1015      /**
1016       * all elements successfully putLast are contained
1017       */
1018 <     public void testPutLast() {
1019 <         try {
1020 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1021 <             for (int i = 0; i < SIZE; ++i) {
1022 <                 Integer I = new Integer(i);
1023 <                 q.putLast(I);
1147 <                 assertTrue(q.contains(I));
1148 <             }
1149 <             assertEquals(0, q.remainingCapacity());
1018 >     public void testPutLast() throws InterruptedException {
1019 >         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1020 >         for (int i = 0; i < SIZE; ++i) {
1021 >             Integer I = new Integer(i);
1022 >             q.putLast(I);
1023 >             assertTrue(q.contains(I));
1024           }
1025 <        catch (InterruptedException ie) {
1152 <            unexpectedException();
1153 <        }
1025 >         assertEquals(0, q.remainingCapacity());
1026      }
1027  
1028      /**
1029       * putLast blocks interruptibly if full
1030       */
1031 <    public void testBlockingPutLast() {
1032 <        Thread t = new Thread(new Runnable() {
1033 <                public void run() {
1034 <                    int added = 0;
1035 <                    try {
1036 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1037 <                        for (int i = 0; i < SIZE; ++i) {
1038 <                            q.putLast(new Integer(i));
1039 <                            ++added;
1040 <                        }
1041 <                        q.putLast(new Integer(SIZE));
1042 <                        threadShouldThrow();
1043 <                    } catch (InterruptedException ie){
1044 <                        threadAssertEquals(added, SIZE);
1045 <                    }  
1046 <                }});
1047 <        t.start();
1048 <        try {
1049 <           Thread.sleep(SHORT_DELAY_MS);
1050 <           t.interrupt();
1051 <           t.join();
1180 <        }
1181 <        catch (InterruptedException ie) {
1182 <            unexpectedException();
1183 <        }
1031 >    public void testBlockingPutLast() throws InterruptedException {
1032 >        Thread t = new Thread(new CheckedRunnable() {
1033 >            public void realRun() {
1034 >                int added = 0;
1035 >                try {
1036 >                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1037 >                    for (int i = 0; i < SIZE; ++i) {
1038 >                        q.putLast(new Integer(i));
1039 >                        ++added;
1040 >                    }
1041 >                    q.putLast(new Integer(SIZE));
1042 >                    threadShouldThrow();
1043 >                } catch (InterruptedException success) {
1044 >                    threadAssertEquals(added, SIZE);
1045 >                }
1046 >            }});
1047 >
1048 >        t.start();
1049 >        Thread.sleep(SHORT_DELAY_MS);
1050 >        t.interrupt();
1051 >        t.join();
1052      }
1053  
1054      /**
1055       * putLast blocks waiting for take when full
1056       */
1057 <    public void testPutLastWithTake() {
1057 >    public void testPutLastWithTake() throws InterruptedException {
1058          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1059 <        Thread t = new Thread(new Runnable() {
1060 <                public void run() {
1061 <                    int added = 0;
1062 <                    try {
1063 <                        q.putLast(new Object());
1064 <                        ++added;
1065 <                        q.putLast(new Object());
1066 <                        ++added;
1067 <                        q.putLast(new Object());
1068 <                        ++added;
1069 <                        q.putLast(new Object());
1070 <                        ++added;
1071 <                        threadShouldThrow();
1072 <                    } catch (InterruptedException e){
1073 <                        threadAssertTrue(added >= 2);
1206 <                    }
1059 >        Thread t = new Thread(new CheckedRunnable() {
1060 >            public void realRun() {
1061 >                int added = 0;
1062 >                try {
1063 >                    q.putLast(new Object());
1064 >                    ++added;
1065 >                    q.putLast(new Object());
1066 >                    ++added;
1067 >                    q.putLast(new Object());
1068 >                    ++added;
1069 >                    q.putLast(new Object());
1070 >                    ++added;
1071 >                    threadShouldThrow();
1072 >                } catch (InterruptedException success) {
1073 >                    threadAssertTrue(added >= 2);
1074                  }
1075 <            });
1076 <        try {
1077 <            t.start();
1078 <            Thread.sleep(SHORT_DELAY_MS);
1079 <            q.take();
1080 <            t.interrupt();
1081 <            t.join();
1215 <        } catch (Exception e){
1216 <            unexpectedException();
1217 <        }
1075 >            }});
1076 >
1077 >        t.start();
1078 >        Thread.sleep(SHORT_DELAY_MS);
1079 >        q.take();
1080 >        t.interrupt();
1081 >        t.join();
1082      }
1083  
1084      /**
1085       * timed offerLast times out if full and elements not taken
1086       */
1087 <    public void testTimedOfferLast() {
1087 >    public void testTimedOfferLast() throws InterruptedException {
1088          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1089 <        Thread t = new Thread(new Runnable() {
1090 <                public void run() {
1091 <                    try {
1092 <                        q.putLast(new Object());
1093 <                        q.putLast(new Object());
1094 <                        threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1095 <                        q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1096 <                        threadShouldThrow();
1097 <                    } catch (InterruptedException success){}
1098 <                }
1099 <            });
1100 <        
1101 <        try {
1102 <            t.start();
1103 <            Thread.sleep(SMALL_DELAY_MS);
1240 <            t.interrupt();
1241 <            t.join();
1242 <        } catch (Exception e){
1243 <            unexpectedException();
1244 <        }
1089 >        Thread t = new Thread(new CheckedRunnable() {
1090 >            public void realRun() throws InterruptedException {
1091 >                q.putLast(new Object());
1092 >                q.putLast(new Object());
1093 >                assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1094 >                try {
1095 >                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1096 >                    shouldThrow();
1097 >                } catch (InterruptedException success) {}
1098 >            }});
1099 >
1100 >        t.start();
1101 >        Thread.sleep(SMALL_DELAY_MS);
1102 >        t.interrupt();
1103 >        t.join();
1104      }
1105  
1106      /**
1107       * takeLast retrieves elements in FIFO order
1108       */
1109 <    public void testTakeLast() {
1110 <        try {
1111 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1112 <            for (int i = 0; i < SIZE; ++i) {
1113 <                assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1255 <            }
1256 <        } catch (InterruptedException e){
1257 <            unexpectedException();
1258 <        }  
1109 >    public void testTakeLast() throws InterruptedException {
1110 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1111 >        for (int i = 0; i < SIZE; ++i) {
1112 >            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1113 >        }
1114      }
1115  
1116      /**
1117       * takeLast blocks interruptibly when empty
1118       */
1119 <    public void testTakeLastFromEmpty() {
1119 >    public void testTakeLastFromEmpty() throws InterruptedException {
1120          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1121 <        Thread t = new Thread(new Runnable() {
1122 <                public void run() {
1123 <                    try {
1124 <                        q.takeLast();
1125 <                        threadShouldThrow();
1126 <                    } catch (InterruptedException success){ }                
1127 <                }
1128 <            });
1129 <        try {
1275 <            t.start();
1276 <            Thread.sleep(SHORT_DELAY_MS);
1277 <            t.interrupt();
1278 <            t.join();
1279 <        } catch (Exception e){
1280 <            unexpectedException();
1281 <        }
1121 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1122 >            public void realRun() throws InterruptedException {
1123 >                q.takeLast();
1124 >            }};
1125 >
1126 >        t.start();
1127 >        Thread.sleep(SHORT_DELAY_MS);
1128 >        t.interrupt();
1129 >        t.join();
1130      }
1131  
1132      /**
1133       * TakeLast removes existing elements until empty, then blocks interruptibly
1134       */
1135 <    public void testBlockingTakeLast() {
1136 <        Thread t = new Thread(new Runnable() {
1137 <                public void run() {
1138 <                    try {
1139 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1140 <                        for (int i = 0; i < SIZE; ++i) {
1141 <                            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1142 <                        }
1143 <                        q.takeLast();
1144 <                        threadShouldThrow();
1145 <                    } catch (InterruptedException success){
1146 <                    }  
1147 <                }});
1148 <        t.start();
1301 <        try {
1302 <           Thread.sleep(SHORT_DELAY_MS);
1303 <           t.interrupt();
1304 <           t.join();
1305 <        }
1306 <        catch (InterruptedException ie) {
1307 <            unexpectedException();
1308 <        }
1135 >    public void testBlockingTakeLast() throws InterruptedException {
1136 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1137 >            public void realRun() throws InterruptedException {
1138 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1139 >                for (int i = 0; i < SIZE; ++i) {
1140 >                    assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1141 >                }
1142 >                q.takeLast();
1143 >            }};
1144 >
1145 >        t.start();
1146 >        Thread.sleep(SHORT_DELAY_MS);
1147 >        t.interrupt();
1148 >        t.join();
1149      }
1150  
1151  
1152      /**
1153       * timed pollLast with zero timeout succeeds when non-empty, else times out
1154       */
1155 <    public void testTimedPollLast0() {
1156 <        try {
1157 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1158 <            for (int i = 0; i < SIZE; ++i) {
1159 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue());
1160 <            }
1321 <            assertNull(q.pollLast(0, TimeUnit.MILLISECONDS));
1322 <        } catch (InterruptedException e){
1323 <            unexpectedException();
1324 <        }  
1155 >    public void testTimedPollLast0() throws InterruptedException {
1156 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1157 >        for (int i = 0; i < SIZE; ++i) {
1158 >            assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1159 >        }
1160 >        assertNull(q.pollLast(0, MILLISECONDS));
1161      }
1162  
1163      /**
1164       * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1165       */
1166 <    public void testTimedPollLast() {
1167 <        try {
1168 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1169 <            for (int i = 0; i < SIZE; ++i) {
1170 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1171 <            }
1336 <            assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1337 <        } catch (InterruptedException e){
1338 <            unexpectedException();
1339 <        }  
1166 >    public void testTimedPollLast() throws InterruptedException {
1167 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1168 >        for (int i = 0; i < SIZE; ++i) {
1169 >            assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1170 >        }
1171 >        assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1172      }
1173  
1174      /**
1175       * Interrupted timed pollLast throws InterruptedException instead of
1176       * returning timeout status
1177       */
1178 <    public void testInterruptedTimedPollLast() {
1179 <        Thread t = new Thread(new Runnable() {
1180 <                public void run() {
1181 <                    try {
1182 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1183 <                        for (int i = 0; i < SIZE; ++i) {
1184 <                            threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1185 <                        }
1186 <                        threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1187 <                    } catch (InterruptedException success){
1188 <                    }  
1189 <                }});
1190 <        t.start();
1191 <        try {
1192 <           Thread.sleep(SHORT_DELAY_MS);
1193 <           t.interrupt();
1194 <           t.join();
1363 <        }
1364 <        catch (InterruptedException ie) {
1365 <            unexpectedException();
1366 <        }
1178 >    public void testInterruptedTimedPollLast() throws InterruptedException {
1179 >        Thread t = new Thread(new CheckedRunnable() {
1180 >            public void realRun() throws InterruptedException {
1181 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1182 >                for (int i = 0; i < SIZE; ++i) {
1183 >                    assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1184 >                }
1185 >                try {
1186 >                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1187 >                    shouldThrow();
1188 >                } catch (InterruptedException success) {}
1189 >            }});
1190 >
1191 >        t.start();
1192 >        Thread.sleep(SHORT_DELAY_MS);
1193 >        t.interrupt();
1194 >        t.join();
1195      }
1196  
1197      /**
1198       *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1199       *  on interruption throws
1200       */
1201 <    public void testTimedPollWithOfferLast() {
1201 >    public void testTimedPollWithOfferLast() throws InterruptedException {
1202          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1203 <        Thread t = new Thread(new Runnable() {
1204 <                public void run() {
1205 <                    try {
1206 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1207 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1208 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1209 <                        threadShouldThrow();
1210 <                    } catch (InterruptedException success) { }                
1211 <                }
1212 <            });
1213 <        try {
1214 <            t.start();
1215 <            Thread.sleep(SMALL_DELAY_MS);
1216 <            assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1217 <            t.interrupt();
1218 <            t.join();
1391 <        } catch (Exception e){
1392 <            unexpectedException();
1393 <        }
1394 <    }  
1203 >        Thread t = new Thread(new CheckedRunnable() {
1204 >            public void realRun() throws InterruptedException {
1205 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1206 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1207 >                try {
1208 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1209 >                    shouldThrow();
1210 >                } catch (InterruptedException success) {}
1211 >            }});
1212 >
1213 >        t.start();
1214 >        Thread.sleep(SMALL_DELAY_MS);
1215 >        assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1216 >        t.interrupt();
1217 >        t.join();
1218 >    }
1219  
1220  
1221      /**
# Line 1406 | Line 1230 | public class LinkedBlockingDequeTest ext
1230          try {
1231              q.element();
1232              shouldThrow();
1233 <        }
1410 <        catch (NoSuchElementException success) {}
1233 >        } catch (NoSuchElementException success) {}
1234      }
1235  
1236      /**
# Line 1424 | Line 1247 | public class LinkedBlockingDequeTest ext
1247          }
1248          assertTrue(q.isEmpty());
1249      }
1250 <        
1250 >
1251      /**
1252       * contains(x) reports true when elements added but not yet removed
1253       */
# Line 1505 | Line 1328 | public class LinkedBlockingDequeTest ext
1328      /**
1329       * toArray contains all elements
1330       */
1331 <    public void testToArray() {
1331 >    public void testToArray() throws InterruptedException{
1332          LinkedBlockingDeque q = populatedDeque(SIZE);
1333 <        Object[] o = q.toArray();
1334 <        try {
1335 <        for(int i = 0; i < o.length; i++)
1513 <            assertEquals(o[i], q.take());
1514 <        } catch (InterruptedException e){
1515 <            unexpectedException();
1516 <        }    
1333 >        Object[] o = q.toArray();
1334 >        for (int i = 0; i < o.length; i++)
1335 >            assertEquals(o[i], q.take());
1336      }
1337  
1338      /**
1339       * toArray(a) contains all elements
1340       */
1341 <    public void testToArray2() {
1341 >    public void testToArray2() throws InterruptedException {
1342          LinkedBlockingDeque q = populatedDeque(SIZE);
1343 <        Integer[] ints = new Integer[SIZE];
1344 <        ints = (Integer[])q.toArray(ints);
1345 <        try {
1346 <            for(int i = 0; i < ints.length; i++)
1528 <                assertEquals(ints[i], q.take());
1529 <        } catch (InterruptedException e){
1530 <            unexpectedException();
1531 <        }    
1343 >        Integer[] ints = new Integer[SIZE];
1344 >        ints = (Integer[])q.toArray(ints);
1345 >        for (int i = 0; i < ints.length; i++)
1346 >            assertEquals(ints[i], q.take());
1347      }
1348  
1349      /**
1350       * toArray(null) throws NPE
1351       */
1352      public void testToArray_BadArg() {
1353 <        try {
1353 >        try {
1354              LinkedBlockingDeque q = populatedDeque(SIZE);
1355 <            Object o[] = q.toArray(null);
1356 <            shouldThrow();
1357 <        } catch(NullPointerException success){}
1355 >            Object o[] = q.toArray(null);
1356 >            shouldThrow();
1357 >        } catch (NullPointerException success) {}
1358      }
1359  
1360      /**
1361       * toArray with incompatible array type throws CCE
1362       */
1363      public void testToArray1_BadArg() {
1364 <        try {
1364 >        try {
1365              LinkedBlockingDeque q = populatedDeque(SIZE);
1366 <            Object o[] = q.toArray(new String[10] );
1367 <            shouldThrow();
1368 <        } catch(ArrayStoreException  success){}
1366 >            Object o[] = q.toArray(new String[10] );
1367 >            shouldThrow();
1368 >        } catch (ArrayStoreException success) {}
1369      }
1370  
1371 <    
1371 >
1372      /**
1373       * iterator iterates through all elements
1374       */
1375 <    public void testIterator() {
1375 >    public void testIterator() throws InterruptedException {
1376          LinkedBlockingDeque q = populatedDeque(SIZE);
1377 <        Iterator it = q.iterator();
1378 <        try {
1379 <            while(it.hasNext()){
1380 <                assertEquals(it.next(), q.take());
1566 <            }
1567 <        } catch (InterruptedException e){
1568 <            unexpectedException();
1569 <        }    
1377 >        Iterator it = q.iterator();
1378 >        while (it.hasNext()) {
1379 >            assertEquals(it.next(), q.take());
1380 >        }
1381      }
1382  
1383      /**
# Line 1581 | Line 1392 | public class LinkedBlockingDequeTest ext
1392          Iterator it = q.iterator();
1393          it.next();
1394          it.remove();
1395 <        
1395 >
1396          it = q.iterator();
1397          assertEquals(it.next(), one);
1398          assertEquals(it.next(), three);
# Line 1614 | Line 1425 | public class LinkedBlockingDequeTest ext
1425          q.add(one);
1426          q.add(two);
1427          q.add(three);
1428 <        try {
1429 <            for (Iterator it = q.iterator(); it.hasNext();) {
1430 <                q.remove();
1620 <                it.next();
1621 <            }
1622 <        }
1623 <        catch (ConcurrentModificationException e) {
1624 <            unexpectedException();
1428 >        for (Iterator it = q.iterator(); it.hasNext();) {
1429 >            q.remove();
1430 >            it.next();
1431          }
1432          assertEquals(0, q.size());
1433      }
# Line 1633 | Line 1439 | public class LinkedBlockingDequeTest ext
1439      public void testDescendingIterator() {
1440          LinkedBlockingDeque q = populatedDeque(SIZE);
1441          int i = 0;
1442 <        Iterator it = q.descendingIterator();
1443 <        while(it.hasNext()) {
1442 >        Iterator it = q.descendingIterator();
1443 >        while (it.hasNext()) {
1444              assertTrue(q.contains(it.next()));
1445              ++i;
1446          }
# Line 1642 | Line 1448 | public class LinkedBlockingDequeTest ext
1448          assertFalse(it.hasNext());
1449          try {
1450              it.next();
1451 <        } catch(NoSuchElementException success) {
1452 <        }
1451 >            shouldThrow();
1452 >        } catch (NoSuchElementException success) {}
1453      }
1454  
1455      /**
# Line 1660 | Line 1466 | public class LinkedBlockingDequeTest ext
1466                  int i = ((Integer)(it.next())).intValue();
1467                  assertEquals(++k, i);
1468              }
1469 <            
1469 >
1470              assertEquals(3, k);
1471              q.remove();
1472              q.remove();
# Line 1700 | Line 1506 | public class LinkedBlockingDequeTest ext
1506          for (int i = 0; i < SIZE; ++i) {
1507              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1508          }
1509 <    }        
1509 >    }
1510  
1511  
1512      /**
# Line 1711 | Line 1517 | public class LinkedBlockingDequeTest ext
1517          q.add(one);
1518          q.add(two);
1519          ExecutorService executor = Executors.newFixedThreadPool(2);
1520 <        executor.execute(new Runnable() {
1521 <            public void run() {
1520 >        executor.execute(new CheckedRunnable() {
1521 >            public void realRun() throws InterruptedException {
1522                  threadAssertFalse(q.offer(three));
1523 <                try {
1524 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1525 <                    threadAssertEquals(0, q.remainingCapacity());
1526 <                }
1527 <                catch (InterruptedException e) {
1528 <                    threadUnexpectedException();
1529 <                }
1530 <            }
1531 <        });
1523 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1524 >                threadAssertEquals(0, q.remainingCapacity());
1525 >            }});
1526 >
1527 >        executor.execute(new CheckedRunnable() {
1528 >            public void realRun() throws InterruptedException {
1529 >                Thread.sleep(SMALL_DELAY_MS);
1530 >                threadAssertEquals(one, q.take());
1531 >            }});
1532  
1727        executor.execute(new Runnable() {
1728            public void run() {
1729                try {
1730                    Thread.sleep(SMALL_DELAY_MS);
1731                    threadAssertEquals(one, q.take());
1732                }
1733                catch (InterruptedException e) {
1734                    threadUnexpectedException();
1735                }
1736            }
1737        });
1738        
1533          joinPool(executor);
1534      }
1535  
# Line 1745 | Line 1539 | public class LinkedBlockingDequeTest ext
1539      public void testPollInExecutor() {
1540          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1541          ExecutorService executor = Executors.newFixedThreadPool(2);
1542 <        executor.execute(new Runnable() {
1543 <            public void run() {
1542 >        executor.execute(new CheckedRunnable() {
1543 >            public void realRun() throws InterruptedException {
1544                  threadAssertNull(q.poll());
1545 <                try {
1546 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1547 <                    threadAssertTrue(q.isEmpty());
1548 <                }
1549 <                catch (InterruptedException e) {
1550 <                    threadUnexpectedException();
1551 <                }
1552 <            }
1553 <        });
1545 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1546 >                threadAssertTrue(q.isEmpty());
1547 >            }});
1548 >
1549 >        executor.execute(new CheckedRunnable() {
1550 >            public void realRun() throws InterruptedException {
1551 >                Thread.sleep(SMALL_DELAY_MS);
1552 >                q.put(one);
1553 >            }});
1554  
1761        executor.execute(new Runnable() {
1762            public void run() {
1763                try {
1764                    Thread.sleep(SMALL_DELAY_MS);
1765                    q.put(one);
1766                }
1767                catch (InterruptedException e) {
1768                    threadUnexpectedException();
1769                }
1770            }
1771        });
1772        
1555          joinPool(executor);
1556      }
1557  
1558      /**
1559       * A deserialized serialized deque has same elements in same order
1560       */
1561 <    public void testSerialization() {
1561 >    public void testSerialization() throws Exception {
1562          LinkedBlockingDeque q = populatedDeque(SIZE);
1563  
1564 <        try {
1565 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1566 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1567 <            out.writeObject(q);
1568 <            out.close();
1569 <
1570 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1571 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1572 <            LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1573 <            assertEquals(q.size(), r.size());
1574 <            while (!q.isEmpty())
1793 <                assertEquals(q.remove(), r.remove());
1794 <        } catch(Exception e){
1795 <            unexpectedException();
1796 <        }
1564 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1565 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1566 >        out.writeObject(q);
1567 >        out.close();
1568 >
1569 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1570 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1571 >        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1572 >        assertEquals(q.size(), r.size());
1573 >        while (!q.isEmpty())
1574 >            assertEquals(q.remove(), r.remove());
1575      }
1576  
1577      /**
1578       * drainTo(null) throws NPE
1579 <     */
1579 >     */
1580      public void testDrainToNull() {
1581          LinkedBlockingDeque q = populatedDeque(SIZE);
1582          try {
1583              q.drainTo(null);
1584              shouldThrow();
1585 <        } catch(NullPointerException success) {
1808 <        }
1585 >        } catch (NullPointerException success) {}
1586      }
1587  
1588      /**
1589       * drainTo(this) throws IAE
1590 <     */
1590 >     */
1591      public void testDrainToSelf() {
1592          LinkedBlockingDeque q = populatedDeque(SIZE);
1593          try {
1594              q.drainTo(q);
1595              shouldThrow();
1596 <        } catch(IllegalArgumentException success) {
1820 <        }
1596 >        } catch (IllegalArgumentException success) {}
1597      }
1598  
1599      /**
1600       * drainTo(c) empties deque into another collection c
1601 <     */
1601 >     */
1602      public void testDrainTo() {
1603          LinkedBlockingDeque q = populatedDeque(SIZE);
1604          ArrayList l = new ArrayList();
1605          q.drainTo(l);
1606          assertEquals(q.size(), 0);
1607          assertEquals(l.size(), SIZE);
1608 <        for (int i = 0; i < SIZE; ++i)
1608 >        for (int i = 0; i < SIZE; ++i)
1609              assertEquals(l.get(i), new Integer(i));
1610          q.add(zero);
1611          q.add(one);
# Line 1840 | Line 1616 | public class LinkedBlockingDequeTest ext
1616          q.drainTo(l);
1617          assertEquals(q.size(), 0);
1618          assertEquals(l.size(), 2);
1619 <        for (int i = 0; i < 2; ++i)
1619 >        for (int i = 0; i < 2; ++i)
1620              assertEquals(l.get(i), new Integer(i));
1621      }
1622  
1623      /**
1624       * drainTo empties full deque, unblocking a waiting put.
1625 <     */
1626 <    public void testDrainToWithActivePut() {
1625 >     */
1626 >    public void testDrainToWithActivePut() throws InterruptedException {
1627          final LinkedBlockingDeque q = populatedDeque(SIZE);
1628 <        Thread t = new Thread(new Runnable() {
1629 <                public void run() {
1630 <                    try {
1631 <                        q.put(new Integer(SIZE+1));
1632 <                    } catch (InterruptedException ie){
1633 <                        threadUnexpectedException();
1634 <                    }
1635 <                }
1636 <            });
1637 <        try {
1638 <            t.start();
1639 <            ArrayList l = new ArrayList();
1640 <            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 <        }
1628 >        Thread t = new Thread(new CheckedRunnable() {
1629 >            public void realRun() throws InterruptedException {
1630 >                q.put(new Integer(SIZE+1));
1631 >            }});
1632 >
1633 >        t.start();
1634 >        ArrayList l = new ArrayList();
1635 >        q.drainTo(l);
1636 >        assertTrue(l.size() >= SIZE);
1637 >        for (int i = 0; i < SIZE; ++i)
1638 >            assertEquals(l.get(i), new Integer(i));
1639 >        t.join();
1640 >        assertTrue(q.size() + l.size() >= SIZE);
1641      }
1642  
1643      /**
1644       * drainTo(null, n) throws NPE
1645 <     */
1645 >     */
1646      public void testDrainToNullN() {
1647          LinkedBlockingDeque q = populatedDeque(SIZE);
1648          try {
1649              q.drainTo(null, 0);
1650              shouldThrow();
1651 <        } catch(NullPointerException success) {
1884 <        }
1651 >        } catch (NullPointerException success) {}
1652      }
1653  
1654      /**
1655       * drainTo(this, n) throws IAE
1656 <     */
1656 >     */
1657      public void testDrainToSelfN() {
1658          LinkedBlockingDeque q = populatedDeque(SIZE);
1659          try {
1660              q.drainTo(q, 0);
1661              shouldThrow();
1662 <        } catch(IllegalArgumentException success) {
1896 <        }
1662 >        } catch (IllegalArgumentException success) {}
1663      }
1664  
1665      /**
1666       * drainTo(c, n) empties first max {n, size} elements of deque into c
1667 <     */
1667 >     */
1668      public void testDrainToN() {
1669          LinkedBlockingDeque q = new LinkedBlockingDeque();
1670          for (int i = 0; i < SIZE + 2; ++i) {
1671 <            for(int j = 0; j < SIZE; j++)
1671 >            for (int j = 0; j < SIZE; j++)
1672                  assertTrue(q.offer(new Integer(j)));
1673              ArrayList l = new ArrayList();
1674              q.drainTo(l, i);
1675              int k = (i < SIZE)? i : SIZE;
1676              assertEquals(l.size(), k);
1677              assertEquals(q.size(), SIZE-k);
1678 <            for (int j = 0; j < k; ++j)
1678 >            for (int j = 0; j < k; ++j)
1679                  assertEquals(l.get(j), new Integer(j));
1680              while (q.poll() != null) ;
1681          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines