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.4 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.17 by jsr166, Sun Nov 22 00:17:37 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      /**
# 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());
271 >        q.addFirst(four);
272 >        assertEquals(four,q.peekFirst());
273      }
274  
275      /**
# 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());
281 >        q.addLast(four);
282 >        assertEquals(four,q.peekLast());
283      }
284  
285  
# 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());
439 >        q.push(four);
440 >        assertEquals(four,q.peekFirst());
441      }
442  
443  
# 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();
619 <           t.join();
620 <        }
621 <        catch (InterruptedException ie) {
622 <            unexpectedException();
623 <        }
587 >    public void testBlockingPut() throws InterruptedException {
588 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
589 >        Thread t = new Thread(new CheckedRunnable() {
590 >            public void realRun() throws InterruptedException {
591 >                for (int i = 0; i < SIZE; ++i)
592 >                    q.put(i);
593 >                assertEquals(SIZE, q.size());
594 >                assertEquals(0, q.remainingCapacity());
595 >                try {
596 >                    q.put(99);
597 >                    shouldThrow();
598 >                } catch (InterruptedException success) {}
599 >            }});
600 >
601 >        t.start();
602 >        Thread.sleep(SHORT_DELAY_MS);
603 >        t.interrupt();
604 >        t.join();
605 >        assertEquals(SIZE, q.size());
606 >        assertEquals(0, q.remainingCapacity());
607      }
608  
609      /**
610       * put blocks waiting for take when full
611       */
612 <    public void testPutWithTake() {
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);
629 <                    }
630 <                }
631 <            });
632 <        try {
650 <            t.start();
651 <            Thread.sleep(SHORT_DELAY_MS);
652 <            q.take();
653 <            t.interrupt();
654 <            t.join();
655 <        } catch (Exception e){
656 <            unexpectedException();
657 <        }
612 >    public void testPutWithTake() throws InterruptedException {
613 >        final int capacity = 2;
614 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
615 >        Thread t = new Thread(new CheckedRunnable() {
616 >            public void realRun() throws InterruptedException {
617 >                for (int i = 0; i < capacity + 1; i++)
618 >                    q.put(i);
619 >                try {
620 >                    q.put(99);
621 >                    shouldThrow();
622 >                } catch (InterruptedException success) {}
623 >            }});
624 >
625 >        t.start();
626 >        Thread.sleep(SHORT_DELAY_MS);
627 >        assertEquals(q.remainingCapacity(), 0);
628 >        assertEquals(0, q.take());
629 >        Thread.sleep(SHORT_DELAY_MS);
630 >        t.interrupt();
631 >        t.join();
632 >        assertEquals(q.remainingCapacity(), 0);
633      }
634  
635      /**
636       * timed offer times out if full and elements not taken
637       */
638 <    public void testTimedOffer() {
638 >    public void testTimedOffer() throws InterruptedException {
639          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
640 <        Thread t = new Thread(new Runnable() {
641 <                public void run() {
642 <                    try {
643 <                        q.put(new Object());
644 <                        q.put(new Object());
645 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
646 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
647 <                        threadShouldThrow();
648 <                    } catch (InterruptedException success){}
649 <                }
675 <            });
640 >        Thread t = new Thread(new CheckedRunnable() {
641 >            public void realRun() throws InterruptedException {
642 >                q.put(new Object());
643 >                q.put(new Object());
644 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
645 >                try {
646 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
647 >                    shouldThrow();
648 >                } catch (InterruptedException success) {}
649 >            }});
650  
651 <        try {
652 <            t.start();
653 <            Thread.sleep(SMALL_DELAY_MS);
654 <            t.interrupt();
681 <            t.join();
682 <        } catch (Exception e){
683 <            unexpectedException();
684 <        }
651 >        t.start();
652 >        Thread.sleep(SMALL_DELAY_MS);
653 >        t.interrupt();
654 >        t.join();
655      }
656  
657      /**
658       * take retrieves elements in FIFO order
659       */
660 <    public void testTake() {
661 <        try {
662 <            LinkedBlockingDeque q = populatedDeque(SIZE);
663 <            for (int i = 0; i < SIZE; ++i) {
664 <                assertEquals(i, ((Integer)q.take()).intValue());
695 <            }
696 <        } catch (InterruptedException e){
697 <            unexpectedException();
698 <        }
660 >    public void testTake() throws InterruptedException {
661 >        LinkedBlockingDeque q = populatedDeque(SIZE);
662 >        for (int i = 0; i < SIZE; ++i) {
663 >            assertEquals(i, ((Integer)q.take()).intValue());
664 >        }
665      }
666  
667      /**
668       * take blocks interruptibly when empty
669       */
670 <    public void testTakeFromEmpty() {
670 >    public void testTakeFromEmpty() throws InterruptedException {
671          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
672 <        Thread t = new Thread(new Runnable() {
673 <                public void run() {
674 <                    try {
675 <                        q.take();
676 <                        threadShouldThrow();
677 <                    } catch (InterruptedException success){ }
678 <                }
679 <            });
680 <        try {
715 <            t.start();
716 <            Thread.sleep(SHORT_DELAY_MS);
717 <            t.interrupt();
718 <            t.join();
719 <        } catch (Exception e){
720 <            unexpectedException();
721 <        }
672 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
673 >            public void realRun() throws InterruptedException {
674 >                q.take();
675 >            }};
676 >
677 >        t.start();
678 >        Thread.sleep(SHORT_DELAY_MS);
679 >        t.interrupt();
680 >        t.join();
681      }
682  
683      /**
684       * Take removes existing elements until empty, then blocks interruptibly
685       */
686 <    public void testBlockingTake() {
687 <        Thread t = new Thread(new Runnable() {
688 <                public void run() {
689 <                    try {
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 <                        threadShouldThrow();
696 <                    } catch (InterruptedException success){
697 <                    }
698 <                }});
699 <        t.start();
700 <        try {
701 <           Thread.sleep(SHORT_DELAY_MS);
702 <           t.interrupt();
744 <           t.join();
745 <        }
746 <        catch (InterruptedException ie) {
747 <            unexpectedException();
748 <        }
686 >    public void testBlockingTake() throws InterruptedException {
687 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
688 >        Thread t = new Thread(new CheckedRunnable() {
689 >            public void realRun() throws InterruptedException {
690 >                for (int i = 0; i < SIZE; ++i) {
691 >                    assertEquals(i, ((Integer)q.take()).intValue());
692 >                }
693 >                try {
694 >                    q.take();
695 >                    shouldThrow();
696 >                } catch (InterruptedException success) {}
697 >            }});
698 >
699 >        t.start();
700 >        Thread.sleep(SHORT_DELAY_MS);
701 >        t.interrupt();
702 >        t.join();
703      }
704  
705  
# Line 757 | Line 711 | public class LinkedBlockingDequeTest ext
711          for (int i = 0; i < SIZE; ++i) {
712              assertEquals(i, ((Integer)q.poll()).intValue());
713          }
714 <        assertNull(q.poll());
714 >        assertNull(q.poll());
715      }
716  
717      /**
718       * timed poll with zero timeout succeeds when non-empty, else times out
719       */
720 <    public void testTimedPoll0() {
721 <        try {
722 <            LinkedBlockingDeque q = populatedDeque(SIZE);
723 <            for (int i = 0; i < SIZE; ++i) {
724 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
725 <            }
772 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
773 <        } catch (InterruptedException e){
774 <            unexpectedException();
775 <        }
720 >    public void testTimedPoll0() throws InterruptedException {
721 >        LinkedBlockingDeque q = populatedDeque(SIZE);
722 >        for (int i = 0; i < SIZE; ++i) {
723 >            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
724 >        }
725 >        assertNull(q.poll(0, MILLISECONDS));
726      }
727  
728      /**
729       * timed poll with nonzero timeout succeeds when non-empty, else times out
730       */
731 <    public void testTimedPoll() {
732 <        try {
733 <            LinkedBlockingDeque q = populatedDeque(SIZE);
734 <            for (int i = 0; i < SIZE; ++i) {
735 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
736 <            }
787 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
788 <        } catch (InterruptedException e){
789 <            unexpectedException();
790 <        }
731 >    public void testTimedPoll() throws InterruptedException {
732 >        LinkedBlockingDeque q = populatedDeque(SIZE);
733 >        for (int i = 0; i < SIZE; ++i) {
734 >            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
735 >        }
736 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
737      }
738  
739      /**
740       * Interrupted timed poll throws InterruptedException instead of
741       * returning timeout status
742       */
743 <    public void testInterruptedTimedPoll() {
744 <        Thread t = new Thread(new Runnable() {
745 <                public void run() {
746 <                    try {
747 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
748 <                        for (int i = 0; i < SIZE; ++i) {
749 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
750 <                        }
751 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
752 <                    } catch (InterruptedException success){
753 <                    }
754 <                }});
755 <        t.start();
756 <        try {
757 <           Thread.sleep(SHORT_DELAY_MS);
758 <           t.interrupt();
759 <           t.join();
814 <        }
815 <        catch (InterruptedException ie) {
816 <            unexpectedException();
817 <        }
743 >    public void testInterruptedTimedPoll() throws InterruptedException {
744 >        Thread t = new Thread(new CheckedRunnable() {
745 >            public void realRun() throws InterruptedException {
746 >                LinkedBlockingDeque q = populatedDeque(SIZE);
747 >                for (int i = 0; i < SIZE; ++i) {
748 >                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
749 >                }
750 >                try {
751 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
752 >                    shouldThrow();
753 >                } catch (InterruptedException success) {}
754 >            }});
755 >
756 >        t.start();
757 >        Thread.sleep(SHORT_DELAY_MS);
758 >        t.interrupt();
759 >        t.join();
760      }
761  
762      /**
763       *  timed poll before a delayed offer fails; after offer succeeds;
764       *  on interruption throws
765       */
766 <    public void testTimedPollWithOffer() {
766 >    public void testTimedPollWithOffer() throws InterruptedException {
767          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
768 <        Thread t = new Thread(new Runnable() {
769 <                public void run() {
770 <                    try {
771 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
772 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
773 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
774 <                        threadShouldThrow();
775 <                    } catch (InterruptedException success) { }
776 <                }
777 <            });
778 <        try {
779 <            t.start();
780 <            Thread.sleep(SMALL_DELAY_MS);
781 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
782 <            t.interrupt();
841 <            t.join();
842 <        } catch (Exception e){
843 <            unexpectedException();
844 <        }
768 >        Thread t = new Thread(new CheckedRunnable() {
769 >            public void realRun() throws InterruptedException {
770 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
771 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
772 >                try {
773 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
774 >                    shouldThrow();
775 >                } catch (InterruptedException success) {}
776 >            }});
777 >
778 >        t.start();
779 >        Thread.sleep(SMALL_DELAY_MS);
780 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
781 >        t.interrupt();
782 >        t.join();
783      }
784  
785  
786      /**
787       * putFirst(null) throws NPE
788       */
789 <     public void testPutFirstNull() {
790 <        try {
789 >     public void testPutFirstNull() throws InterruptedException {
790 >        try {
791              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
792              q.putFirst(null);
793              shouldThrow();
794 <        }
857 <        catch (NullPointerException success){
858 <        }
859 <        catch (InterruptedException ie) {
860 <            unexpectedException();
861 <        }
794 >        } catch (NullPointerException success) {}
795       }
796  
797      /**
798       * all elements successfully putFirst are contained
799       */
800 <     public void testPutFirst() {
801 <         try {
802 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
803 <             for (int i = 0; i < SIZE; ++i) {
804 <                 Integer I = new Integer(i);
805 <                 q.putFirst(I);
873 <                 assertTrue(q.contains(I));
874 <             }
875 <             assertEquals(0, q.remainingCapacity());
800 >     public void testPutFirst() throws InterruptedException {
801 >         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
802 >         for (int i = 0; i < SIZE; ++i) {
803 >             Integer I = new Integer(i);
804 >             q.putFirst(I);
805 >             assertTrue(q.contains(I));
806           }
807 <        catch (InterruptedException ie) {
878 <            unexpectedException();
879 <        }
807 >         assertEquals(0, q.remainingCapacity());
808      }
809  
810      /**
811       * putFirst blocks interruptibly if full
812       */
813 <    public void testBlockingPutFirst() {
814 <        Thread t = new Thread(new Runnable() {
815 <                public void run() {
816 <                    int added = 0;
817 <                    try {
818 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
819 <                        for (int i = 0; i < SIZE; ++i) {
820 <                            q.putFirst(new Integer(i));
821 <                            ++added;
822 <                        }
823 <                        q.putFirst(new Integer(SIZE));
824 <                        threadShouldThrow();
825 <                    } catch (InterruptedException ie){
826 <                        threadAssertEquals(added, SIZE);
827 <                    }
828 <                }});
829 <        t.start();
830 <        try {
831 <           Thread.sleep(SHORT_DELAY_MS);
832 <           t.interrupt();
905 <           t.join();
906 <        }
907 <        catch (InterruptedException ie) {
908 <            unexpectedException();
909 <        }
813 >    public void testBlockingPutFirst() throws InterruptedException {
814 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
815 >        Thread t = new Thread(new CheckedRunnable() {
816 >            public void realRun() throws InterruptedException {
817 >                for (int i = 0; i < SIZE; ++i)
818 >                    q.putFirst(i);
819 >                assertEquals(SIZE, q.size());
820 >                assertEquals(0, q.remainingCapacity());
821 >                try {
822 >                    q.putFirst(99);
823 >                    shouldThrow();
824 >                } catch (InterruptedException success) {}
825 >            }});
826 >
827 >        t.start();
828 >        Thread.sleep(SHORT_DELAY_MS);
829 >        t.interrupt();
830 >        t.join();
831 >        assertEquals(SIZE, q.size());
832 >        assertEquals(0, q.remainingCapacity());
833      }
834  
835      /**
836       * putFirst blocks waiting for take when full
837       */
838 <    public void testPutFirstWithTake() {
839 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
840 <        Thread t = new Thread(new Runnable() {
841 <                public void run() {
842 <                    int added = 0;
843 <                    try {
844 <                        q.putFirst(new Object());
845 <                        ++added;
846 <                        q.putFirst(new Object());
847 <                        ++added;
848 <                        q.putFirst(new Object());
849 <                        ++added;
850 <                        q.putFirst(new Object());
851 <                        ++added;
852 <                        threadShouldThrow();
853 <                    } catch (InterruptedException e){
854 <                        threadAssertTrue(added >= 2);
855 <                    }
856 <                }
857 <            });
858 <        try {
936 <            t.start();
937 <            Thread.sleep(SHORT_DELAY_MS);
938 <            q.take();
939 <            t.interrupt();
940 <            t.join();
941 <        } catch (Exception e){
942 <            unexpectedException();
943 <        }
838 >    public void testPutFirstWithTake() throws InterruptedException {
839 >        final int capacity = 2;
840 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
841 >        Thread t = new Thread(new CheckedRunnable() {
842 >            public void realRun() throws InterruptedException {
843 >                for (int i = 0; i < capacity + 1; i++)
844 >                    q.putFirst(i);
845 >                try {
846 >                    q.putFirst(99);
847 >                    shouldThrow();
848 >                } catch (InterruptedException success) {}
849 >            }});
850 >
851 >        t.start();
852 >        Thread.sleep(SHORT_DELAY_MS);
853 >        assertEquals(q.remainingCapacity(), 0);
854 >        assertEquals(capacity - 1, q.take());
855 >        Thread.sleep(SHORT_DELAY_MS);
856 >        t.interrupt();
857 >        t.join();
858 >        assertEquals(q.remainingCapacity(), 0);
859      }
860  
861      /**
862       * timed offerFirst times out if full and elements not taken
863       */
864 <    public void testTimedOfferFirst() {
864 >    public void testTimedOfferFirst() throws InterruptedException {
865          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
866 <        Thread t = new Thread(new Runnable() {
867 <                public void run() {
868 <                    try {
869 <                        q.putFirst(new Object());
870 <                        q.putFirst(new Object());
871 <                        threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
872 <                        q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
873 <                        threadShouldThrow();
874 <                    } catch (InterruptedException success){}
875 <                }
961 <            });
866 >        Thread t = new Thread(new CheckedRunnable() {
867 >            public void realRun() throws InterruptedException {
868 >                q.putFirst(new Object());
869 >                q.putFirst(new Object());
870 >                assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
871 >                try {
872 >                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
873 >                    shouldThrow();
874 >                } catch (InterruptedException success) {}
875 >            }});
876  
877 <        try {
878 <            t.start();
879 <            Thread.sleep(SMALL_DELAY_MS);
880 <            t.interrupt();
967 <            t.join();
968 <        } catch (Exception e){
969 <            unexpectedException();
970 <        }
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) {
890 <                assertEquals(i, ((Integer)q.takeFirst()).intValue());
981 <            }
982 <        } catch (InterruptedException e){
983 <            unexpectedException();
984 <        }
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 {
1001 <            t.start();
1002 <            Thread.sleep(SHORT_DELAY_MS);
1003 <            t.interrupt();
1004 <            t.join();
1005 <        } catch (Exception e){
1006 <            unexpectedException();
1007 <        }
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();
922 <                    } catch (InterruptedException success){
923 <                    }
924 <                }});
925 <        t.start();
926 <        try {
927 <           Thread.sleep(SHORT_DELAY_MS);
1029 <           t.interrupt();
1030 <           t.join();
1031 <        }
1032 <        catch (InterruptedException ie) {
1033 <            unexpectedException();
1034 <        }
912 >    public void testBlockingTakeFirst() throws InterruptedException {
913 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
914 >        Thread t = new Thread(new CheckedRunnable() {
915 >            public void realRun() throws InterruptedException {
916 >                for (int i = 0; i < SIZE; ++i)
917 >                    assertEquals(i, q.takeFirst());
918 >                try {
919 >                    q.takeFirst();
920 >                    shouldThrow();
921 >                } catch (InterruptedException success) {}
922 >            }});
923 >
924 >        t.start();
925 >        Thread.sleep(SHORT_DELAY_MS);
926 >        t.interrupt();
927 >        t.join();
928      }
929  
930  
931      /**
932       * timed pollFirst with zero timeout succeeds when non-empty, else times out
933       */
934 <    public void testTimedPollFirst0() {
935 <        try {
936 <            LinkedBlockingDeque q = populatedDeque(SIZE);
937 <            for (int i = 0; i < SIZE; ++i) {
938 <                assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue());
939 <            }
1047 <            assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS));
1048 <        } catch (InterruptedException e){
1049 <            unexpectedException();
1050 <        }
934 >    public void testTimedPollFirst0() throws InterruptedException {
935 >        LinkedBlockingDeque q = populatedDeque(SIZE);
936 >        for (int i = 0; i < SIZE; ++i) {
937 >            assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
938 >        }
939 >        assertNull(q.pollFirst(0, MILLISECONDS));
940      }
941  
942      /**
943       * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
944       */
945 <    public void testTimedPollFirst() {
946 <        try {
947 <            LinkedBlockingDeque q = populatedDeque(SIZE);
948 <            for (int i = 0; i < SIZE; ++i) {
949 <                assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
950 <            }
1062 <            assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1063 <        } catch (InterruptedException e){
1064 <            unexpectedException();
1065 <        }
945 >    public void testTimedPollFirst() throws InterruptedException {
946 >        LinkedBlockingDeque q = populatedDeque(SIZE);
947 >        for (int i = 0; i < SIZE; ++i) {
948 >            assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
949 >        }
950 >        assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
951      }
952  
953      /**
954       * Interrupted timed pollFirst throws InterruptedException instead of
955       * returning timeout status
956       */
957 <    public void testInterruptedTimedPollFirst() {
958 <        Thread t = new Thread(new Runnable() {
959 <                public void run() {
960 <                    try {
961 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
962 <                        for (int i = 0; i < SIZE; ++i) {
963 <                            threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
964 <                        }
965 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
966 <                    } catch (InterruptedException success){
967 <                    }
968 <                }});
969 <        t.start();
970 <        try {
971 <           Thread.sleep(SHORT_DELAY_MS);
972 <           t.interrupt();
973 <           t.join();
1089 <        }
1090 <        catch (InterruptedException ie) {
1091 <            unexpectedException();
1092 <        }
957 >    public void testInterruptedTimedPollFirst() throws InterruptedException {
958 >        Thread t = new Thread(new CheckedRunnable() {
959 >            public void realRun() throws InterruptedException {
960 >                LinkedBlockingDeque q = populatedDeque(SIZE);
961 >                for (int i = 0; i < SIZE; ++i) {
962 >                    assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
963 >                }
964 >                try {
965 >                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
966 >                    shouldThrow();
967 >                } catch (InterruptedException success) {}
968 >            }});
969 >
970 >        t.start();
971 >        Thread.sleep(SHORT_DELAY_MS);
972 >        t.interrupt();
973 >        t.join();
974      }
975  
976      /**
977       *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
978       *  on interruption throws
979       */
980 <    public void testTimedPollFirstWithOfferFirst() {
980 >    public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
981          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
982 <        Thread t = new Thread(new Runnable() {
983 <                public void run() {
984 <                    try {
985 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
986 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
987 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
988 <                        threadShouldThrow();
989 <                    } catch (InterruptedException success) { }
990 <                }
991 <            });
992 <        try {
993 <            t.start();
994 <            Thread.sleep(SMALL_DELAY_MS);
995 <            assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
996 <            t.interrupt();
1116 <            t.join();
1117 <        } catch (Exception e){
1118 <            unexpectedException();
1119 <        }
982 >        Thread t = new Thread(new CheckedRunnable() {
983 >            public void realRun() throws InterruptedException {
984 >                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
985 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
986 >                try {
987 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
988 >                    shouldThrow();
989 >                } catch (InterruptedException success) {}
990 >            }});
991 >
992 >        t.start();
993 >        Thread.sleep(SMALL_DELAY_MS);
994 >        assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
995 >        t.interrupt();
996 >        t.join();
997      }
998  
999      /**
1000       * putLast(null) throws NPE
1001       */
1002 <     public void testPutLastNull() {
1003 <        try {
1002 >     public void testPutLastNull() throws InterruptedException {
1003 >        try {
1004              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1005              q.putLast(null);
1006              shouldThrow();
1007 <        }
1131 <        catch (NullPointerException success){
1132 <        }
1133 <        catch (InterruptedException ie) {
1134 <            unexpectedException();
1135 <        }
1007 >        } catch (NullPointerException success) {}
1008       }
1009  
1010      /**
1011       * all elements successfully putLast are contained
1012       */
1013 <     public void testPutLast() {
1014 <         try {
1015 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1016 <             for (int i = 0; i < SIZE; ++i) {
1017 <                 Integer I = new Integer(i);
1018 <                 q.putLast(I);
1147 <                 assertTrue(q.contains(I));
1148 <             }
1149 <             assertEquals(0, q.remainingCapacity());
1013 >     public void testPutLast() throws InterruptedException {
1014 >         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1015 >         for (int i = 0; i < SIZE; ++i) {
1016 >             Integer I = new Integer(i);
1017 >             q.putLast(I);
1018 >             assertTrue(q.contains(I));
1019           }
1020 <        catch (InterruptedException ie) {
1152 <            unexpectedException();
1153 <        }
1020 >         assertEquals(0, q.remainingCapacity());
1021      }
1022  
1023      /**
1024       * putLast blocks interruptibly if full
1025       */
1026 <    public void testBlockingPutLast() {
1027 <        Thread t = new Thread(new Runnable() {
1028 <                public void run() {
1029 <                    int added = 0;
1030 <                    try {
1031 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1032 <                        for (int i = 0; i < SIZE; ++i) {
1033 <                            q.putLast(new Integer(i));
1034 <                            ++added;
1035 <                        }
1036 <                        q.putLast(new Integer(SIZE));
1037 <                        threadShouldThrow();
1038 <                    } catch (InterruptedException ie){
1039 <                        threadAssertEquals(added, SIZE);
1040 <                    }
1041 <                }});
1042 <        t.start();
1043 <        try {
1044 <           Thread.sleep(SHORT_DELAY_MS);
1045 <           t.interrupt();
1179 <           t.join();
1180 <        }
1181 <        catch (InterruptedException ie) {
1182 <            unexpectedException();
1183 <        }
1026 >    public void testBlockingPutLast() throws InterruptedException {
1027 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1028 >        Thread t = new Thread(new CheckedRunnable() {
1029 >            public void realRun() throws InterruptedException {
1030 >                for (int i = 0; i < SIZE; ++i)
1031 >                    q.putLast(i);
1032 >                assertEquals(SIZE, q.size());
1033 >                assertEquals(0, q.remainingCapacity());
1034 >                try {
1035 >                    q.putLast(99);
1036 >                    shouldThrow();
1037 >                } catch (InterruptedException success) {}
1038 >            }});
1039 >
1040 >        t.start();
1041 >        Thread.sleep(SHORT_DELAY_MS);
1042 >        t.interrupt();
1043 >        t.join();
1044 >        assertEquals(SIZE, q.size());
1045 >        assertEquals(0, q.remainingCapacity());
1046      }
1047  
1048      /**
1049       * putLast blocks waiting for take when full
1050       */
1051 <    public void testPutLastWithTake() {
1052 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1053 <        Thread t = new Thread(new Runnable() {
1054 <                public void run() {
1055 <                    int added = 0;
1056 <                    try {
1057 <                        q.putLast(new Object());
1058 <                        ++added;
1059 <                        q.putLast(new Object());
1060 <                        ++added;
1061 <                        q.putLast(new Object());
1062 <                        ++added;
1063 <                        q.putLast(new Object());
1064 <                        ++added;
1065 <                        threadShouldThrow();
1066 <                    } catch (InterruptedException e){
1067 <                        threadAssertTrue(added >= 2);
1068 <                    }
1069 <                }
1070 <            });
1071 <        try {
1210 <            t.start();
1211 <            Thread.sleep(SHORT_DELAY_MS);
1212 <            q.take();
1213 <            t.interrupt();
1214 <            t.join();
1215 <        } catch (Exception e){
1216 <            unexpectedException();
1217 <        }
1051 >    public void testPutLastWithTake() throws InterruptedException {
1052 >        final int capacity = 2;
1053 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1054 >        Thread t = new Thread(new CheckedRunnable() {
1055 >            public void realRun() throws InterruptedException {
1056 >                for (int i = 0; i < capacity + 1; i++)
1057 >                    q.putLast(i);
1058 >                try {
1059 >                    q.putLast(99);
1060 >                    shouldThrow();
1061 >                } catch (InterruptedException success) {}
1062 >            }});
1063 >
1064 >        t.start();
1065 >        Thread.sleep(SHORT_DELAY_MS);
1066 >        assertEquals(q.remainingCapacity(), 0);
1067 >        assertEquals(0, q.take());
1068 >        Thread.sleep(SHORT_DELAY_MS);
1069 >        t.interrupt();
1070 >        t.join();
1071 >        assertEquals(q.remainingCapacity(), 0);
1072      }
1073  
1074      /**
1075       * timed offerLast times out if full and elements not taken
1076       */
1077 <    public void testTimedOfferLast() {
1077 >    public void testTimedOfferLast() throws InterruptedException {
1078          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1079 <        Thread t = new Thread(new Runnable() {
1080 <                public void run() {
1081 <                    try {
1082 <                        q.putLast(new Object());
1083 <                        q.putLast(new Object());
1084 <                        threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1085 <                        q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1086 <                        threadShouldThrow();
1087 <                    } catch (InterruptedException success){}
1088 <                }
1235 <            });
1079 >        Thread t = new Thread(new CheckedRunnable() {
1080 >            public void realRun() throws InterruptedException {
1081 >                q.putLast(new Object());
1082 >                q.putLast(new Object());
1083 >                assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1084 >                try {
1085 >                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1086 >                    shouldThrow();
1087 >                } catch (InterruptedException success) {}
1088 >            }});
1089  
1090 <        try {
1091 <            t.start();
1092 <            Thread.sleep(SMALL_DELAY_MS);
1093 <            t.interrupt();
1241 <            t.join();
1242 <        } catch (Exception e){
1243 <            unexpectedException();
1244 <        }
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) {
1103 <                assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1255 <            }
1256 <        } catch (InterruptedException e){
1257 <            unexpectedException();
1258 <        }
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 {
1275 <            t.start();
1276 <            Thread.sleep(SHORT_DELAY_MS);
1277 <            t.interrupt();
1278 <            t.join();
1279 <        } catch (Exception e){
1280 <            unexpectedException();
1281 <        }
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();
1135 <                    } catch (InterruptedException success){
1298 <                    }
1299 <                }});
1300 <        t.start();
1301 <        try {
1302 <           Thread.sleep(SHORT_DELAY_MS);
1303 <           t.interrupt();
1304 <           t.join();
1305 <        }
1306 <        catch (InterruptedException ie) {
1307 <            unexpectedException();
1308 <        }
1309 <    }
1125 >    public void testBlockingTakeLast() throws InterruptedException {
1126 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1127 >        Thread t = new Thread(new CheckedRunnable() {
1128 >            public void realRun() throws InterruptedException {
1129 >                for (int i = 0; i < SIZE; ++i)
1130 >                    assertEquals(SIZE - 1 - i, q.takeLast());
1131 >                try {
1132 >                    q.takeLast();
1133 >                    shouldThrow();
1134 >                } catch (InterruptedException success) {}
1135 >            }});
1136  
1137 +        t.start();
1138 +        Thread.sleep(SHORT_DELAY_MS);
1139 +        t.interrupt();
1140 +        t.join();
1141 +    }
1142  
1143      /**
1144       * timed pollLast with zero timeout succeeds when non-empty, else times out
1145       */
1146 <    public void testTimedPollLast0() {
1147 <        try {
1148 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1149 <            for (int i = 0; i < SIZE; ++i) {
1150 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue());
1151 <            }
1321 <            assertNull(q.pollLast(0, TimeUnit.MILLISECONDS));
1322 <        } catch (InterruptedException e){
1323 <            unexpectedException();
1324 <        }
1146 >    public void testTimedPollLast0() throws InterruptedException {
1147 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1148 >        for (int i = 0; i < SIZE; ++i) {
1149 >            assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1150 >        }
1151 >        assertNull(q.pollLast(0, MILLISECONDS));
1152      }
1153  
1154      /**
1155       * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1156       */
1157 <    public void testTimedPollLast() {
1158 <        try {
1159 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1160 <            for (int i = 0; i < SIZE; ++i) {
1161 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1162 <            }
1336 <            assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1337 <        } catch (InterruptedException e){
1338 <            unexpectedException();
1339 <        }
1157 >    public void testTimedPollLast() throws InterruptedException {
1158 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1159 >        for (int i = 0; i < SIZE; ++i) {
1160 >            assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1161 >        }
1162 >        assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1163      }
1164  
1165      /**
1166       * Interrupted timed pollLast throws InterruptedException instead of
1167       * returning timeout status
1168       */
1169 <    public void testInterruptedTimedPollLast() {
1170 <        Thread t = new Thread(new Runnable() {
1171 <                public void run() {
1172 <                    try {
1173 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1174 <                        for (int i = 0; i < SIZE; ++i) {
1175 <                            threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1176 <                        }
1177 <                        threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1178 <                    } catch (InterruptedException success){
1179 <                    }
1180 <                }});
1181 <        t.start();
1182 <        try {
1183 <           Thread.sleep(SHORT_DELAY_MS);
1184 <           t.interrupt();
1185 <           t.join();
1363 <        }
1364 <        catch (InterruptedException ie) {
1365 <            unexpectedException();
1366 <        }
1169 >    public void testInterruptedTimedPollLast() throws InterruptedException {
1170 >        Thread t = new Thread(new CheckedRunnable() {
1171 >            public void realRun() throws InterruptedException {
1172 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1173 >                for (int i = 0; i < SIZE; ++i) {
1174 >                    assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1175 >                }
1176 >                try {
1177 >                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1178 >                    shouldThrow();
1179 >                } catch (InterruptedException success) {}
1180 >            }});
1181 >
1182 >        t.start();
1183 >        Thread.sleep(SHORT_DELAY_MS);
1184 >        t.interrupt();
1185 >        t.join();
1186      }
1187  
1188      /**
1189       *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1190       *  on interruption throws
1191       */
1192 <    public void testTimedPollWithOfferLast() {
1192 >    public void testTimedPollWithOfferLast() throws InterruptedException {
1193          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1194 <        Thread t = new Thread(new Runnable() {
1195 <                public void run() {
1196 <                    try {
1197 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1198 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1199 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1200 <                        threadShouldThrow();
1201 <                    } catch (InterruptedException success) { }
1202 <                }
1203 <            });
1204 <        try {
1205 <            t.start();
1206 <            Thread.sleep(SMALL_DELAY_MS);
1207 <            assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1208 <            t.interrupt();
1390 <            t.join();
1391 <        } catch (Exception e){
1392 <            unexpectedException();
1393 <        }
1194 >        Thread t = new Thread(new CheckedRunnable() {
1195 >            public void realRun() throws InterruptedException {
1196 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1197 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1198 >                try {
1199 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1200 >                    shouldThrow();
1201 >                } catch (InterruptedException success) {}
1202 >            }});
1203 >
1204 >        t.start();
1205 >        Thread.sleep(SMALL_DELAY_MS);
1206 >        assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1207 >        t.interrupt();
1208 >        t.join();
1209      }
1210  
1211  
# Line 1406 | Line 1221 | public class LinkedBlockingDequeTest ext
1221          try {
1222              q.element();
1223              shouldThrow();
1224 <        }
1410 <        catch (NoSuchElementException success) {}
1224 >        } catch (NoSuchElementException success) {}
1225      }
1226  
1227      /**
# Line 1505 | Line 1319 | public class LinkedBlockingDequeTest ext
1319      /**
1320       * toArray contains all elements
1321       */
1322 <    public void testToArray() {
1322 >    public void testToArray() throws InterruptedException{
1323          LinkedBlockingDeque q = populatedDeque(SIZE);
1324 <        Object[] o = q.toArray();
1325 <        try {
1326 <        for(int i = 0; i < o.length; i++)
1513 <            assertEquals(o[i], q.take());
1514 <        } catch (InterruptedException e){
1515 <            unexpectedException();
1516 <        }
1324 >        Object[] o = q.toArray();
1325 >        for (int i = 0; i < o.length; i++)
1326 >            assertEquals(o[i], q.take());
1327      }
1328  
1329      /**
1330       * toArray(a) contains all elements
1331       */
1332 <    public void testToArray2() {
1332 >    public void testToArray2() throws InterruptedException {
1333          LinkedBlockingDeque q = populatedDeque(SIZE);
1334 <        Integer[] ints = new Integer[SIZE];
1335 <        ints = (Integer[])q.toArray(ints);
1336 <        try {
1337 <            for(int i = 0; i < ints.length; i++)
1528 <                assertEquals(ints[i], q.take());
1529 <        } catch (InterruptedException e){
1530 <            unexpectedException();
1531 <        }
1334 >        Integer[] ints = new Integer[SIZE];
1335 >        ints = (Integer[])q.toArray(ints);
1336 >        for (int i = 0; i < ints.length; i++)
1337 >            assertEquals(ints[i], q.take());
1338      }
1339  
1340      /**
1341       * toArray(null) throws NPE
1342       */
1343      public void testToArray_BadArg() {
1344 <        try {
1344 >        try {
1345              LinkedBlockingDeque q = populatedDeque(SIZE);
1346 <            Object o[] = q.toArray(null);
1347 <            shouldThrow();
1348 <        } catch(NullPointerException success){}
1346 >            Object o[] = q.toArray(null);
1347 >            shouldThrow();
1348 >        } catch (NullPointerException success) {}
1349      }
1350  
1351      /**
1352       * toArray with incompatible array type throws CCE
1353       */
1354      public void testToArray1_BadArg() {
1355 <        try {
1355 >        try {
1356              LinkedBlockingDeque q = populatedDeque(SIZE);
1357 <            Object o[] = q.toArray(new String[10] );
1358 <            shouldThrow();
1359 <        } catch(ArrayStoreException  success){}
1357 >            Object o[] = q.toArray(new String[10] );
1358 >            shouldThrow();
1359 >        } catch (ArrayStoreException success) {}
1360      }
1361  
1362  
1363      /**
1364       * iterator iterates through all elements
1365       */
1366 <    public void testIterator() {
1366 >    public void testIterator() throws InterruptedException {
1367          LinkedBlockingDeque q = populatedDeque(SIZE);
1368 <        Iterator it = q.iterator();
1369 <        try {
1370 <            while(it.hasNext()){
1371 <                assertEquals(it.next(), q.take());
1566 <            }
1567 <        } catch (InterruptedException e){
1568 <            unexpectedException();
1569 <        }
1368 >        Iterator it = q.iterator();
1369 >        while (it.hasNext()) {
1370 >            assertEquals(it.next(), q.take());
1371 >        }
1372      }
1373  
1374      /**
# Line 1614 | Line 1416 | public class LinkedBlockingDequeTest ext
1416          q.add(one);
1417          q.add(two);
1418          q.add(three);
1419 <        try {
1420 <            for (Iterator it = q.iterator(); it.hasNext();) {
1421 <                q.remove();
1620 <                it.next();
1621 <            }
1622 <        }
1623 <        catch (ConcurrentModificationException e) {
1624 <            unexpectedException();
1419 >        for (Iterator it = q.iterator(); it.hasNext();) {
1420 >            q.remove();
1421 >            it.next();
1422          }
1423          assertEquals(0, q.size());
1424      }
# Line 1633 | Line 1430 | public class LinkedBlockingDequeTest ext
1430      public void testDescendingIterator() {
1431          LinkedBlockingDeque q = populatedDeque(SIZE);
1432          int i = 0;
1433 <        Iterator it = q.descendingIterator();
1434 <        while(it.hasNext()) {
1433 >        Iterator it = q.descendingIterator();
1434 >        while (it.hasNext()) {
1435              assertTrue(q.contains(it.next()));
1436              ++i;
1437          }
# Line 1642 | Line 1439 | public class LinkedBlockingDequeTest ext
1439          assertFalse(it.hasNext());
1440          try {
1441              it.next();
1442 <        } catch(NoSuchElementException success) {
1443 <        }
1442 >            shouldThrow();
1443 >        } catch (NoSuchElementException success) {}
1444      }
1445  
1446      /**
# Line 1711 | Line 1508 | public class LinkedBlockingDequeTest ext
1508          q.add(one);
1509          q.add(two);
1510          ExecutorService executor = Executors.newFixedThreadPool(2);
1511 <        executor.execute(new Runnable() {
1512 <            public void run() {
1511 >        executor.execute(new CheckedRunnable() {
1512 >            public void realRun() throws InterruptedException {
1513                  threadAssertFalse(q.offer(three));
1514 <                try {
1515 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1516 <                    threadAssertEquals(0, q.remainingCapacity());
1517 <                }
1518 <                catch (InterruptedException e) {
1519 <                    threadUnexpectedException();
1520 <                }
1521 <            }
1522 <        });
1726 <
1727 <        executor.execute(new Runnable() {
1728 <            public void run() {
1729 <                try {
1730 <                    Thread.sleep(SMALL_DELAY_MS);
1731 <                    threadAssertEquals(one, q.take());
1732 <                }
1733 <                catch (InterruptedException e) {
1734 <                    threadUnexpectedException();
1735 <                }
1736 <            }
1737 <        });
1514 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1515 >                threadAssertEquals(0, q.remainingCapacity());
1516 >            }});
1517 >
1518 >        executor.execute(new CheckedRunnable() {
1519 >            public void realRun() throws InterruptedException {
1520 >                Thread.sleep(SMALL_DELAY_MS);
1521 >                threadAssertEquals(one, q.take());
1522 >            }});
1523  
1524          joinPool(executor);
1525      }
# Line 1745 | Line 1530 | public class LinkedBlockingDequeTest ext
1530      public void testPollInExecutor() {
1531          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1532          ExecutorService executor = Executors.newFixedThreadPool(2);
1533 <        executor.execute(new Runnable() {
1534 <            public void run() {
1533 >        executor.execute(new CheckedRunnable() {
1534 >            public void realRun() throws InterruptedException {
1535                  threadAssertNull(q.poll());
1536 <                try {
1537 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1538 <                    threadAssertTrue(q.isEmpty());
1539 <                }
1540 <                catch (InterruptedException e) {
1541 <                    threadUnexpectedException();
1542 <                }
1543 <            }
1544 <        });
1760 <
1761 <        executor.execute(new Runnable() {
1762 <            public void run() {
1763 <                try {
1764 <                    Thread.sleep(SMALL_DELAY_MS);
1765 <                    q.put(one);
1766 <                }
1767 <                catch (InterruptedException e) {
1768 <                    threadUnexpectedException();
1769 <                }
1770 <            }
1771 <        });
1536 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1537 >                threadAssertTrue(q.isEmpty());
1538 >            }});
1539 >
1540 >        executor.execute(new CheckedRunnable() {
1541 >            public void realRun() throws InterruptedException {
1542 >                Thread.sleep(SMALL_DELAY_MS);
1543 >                q.put(one);
1544 >            }});
1545  
1546          joinPool(executor);
1547      }
# Line 1776 | Line 1549 | public class LinkedBlockingDequeTest ext
1549      /**
1550       * A deserialized serialized deque has same elements in same order
1551       */
1552 <    public void testSerialization() {
1552 >    public void testSerialization() throws Exception {
1553          LinkedBlockingDeque q = populatedDeque(SIZE);
1554  
1555 <        try {
1556 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1557 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1558 <            out.writeObject(q);
1559 <            out.close();
1560 <
1561 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1562 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1563 <            LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1564 <            assertEquals(q.size(), r.size());
1565 <            while (!q.isEmpty())
1793 <                assertEquals(q.remove(), r.remove());
1794 <        } catch(Exception e){
1795 <            unexpectedException();
1796 <        }
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())
1565 >            assertEquals(q.remove(), r.remove());
1566      }
1567  
1568      /**
# Line 1804 | Line 1573 | public class LinkedBlockingDequeTest ext
1573          try {
1574              q.drainTo(null);
1575              shouldThrow();
1576 <        } catch(NullPointerException success) {
1808 <        }
1576 >        } catch (NullPointerException success) {}
1577      }
1578  
1579      /**
# Line 1816 | Line 1584 | public class LinkedBlockingDequeTest ext
1584          try {
1585              q.drainTo(q);
1586              shouldThrow();
1587 <        } catch(IllegalArgumentException success) {
1820 <        }
1587 >        } catch (IllegalArgumentException success) {}
1588      }
1589  
1590      /**
# Line 1847 | Line 1614 | public class LinkedBlockingDequeTest ext
1614      /**
1615       * drainTo empties full deque, unblocking a waiting put.
1616       */
1617 <    public void testDrainToWithActivePut() {
1617 >    public void testDrainToWithActivePut() throws InterruptedException {
1618          final LinkedBlockingDeque q = populatedDeque(SIZE);
1619 <        Thread t = new Thread(new Runnable() {
1620 <                public void run() {
1621 <                    try {
1622 <                        q.put(new Integer(SIZE+1));
1623 <                    } catch (InterruptedException ie){
1624 <                        threadUnexpectedException();
1625 <                    }
1626 <                }
1627 <            });
1628 <        try {
1629 <            t.start();
1630 <            ArrayList l = new ArrayList();
1631 <            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 <        }
1619 >        Thread t = new Thread(new CheckedRunnable() {
1620 >            public void realRun() throws InterruptedException {
1621 >                q.put(new Integer(SIZE+1));
1622 >            }});
1623 >
1624 >        t.start();
1625 >        ArrayList l = new ArrayList();
1626 >        q.drainTo(l);
1627 >        assertTrue(l.size() >= SIZE);
1628 >        for (int i = 0; i < SIZE; ++i)
1629 >            assertEquals(l.get(i), new Integer(i));
1630 >        t.join();
1631 >        assertTrue(q.size() + l.size() >= SIZE);
1632      }
1633  
1634      /**
# Line 1880 | Line 1639 | public class LinkedBlockingDequeTest ext
1639          try {
1640              q.drainTo(null, 0);
1641              shouldThrow();
1642 <        } catch(NullPointerException success) {
1884 <        }
1642 >        } catch (NullPointerException success) {}
1643      }
1644  
1645      /**
# Line 1892 | Line 1650 | public class LinkedBlockingDequeTest ext
1650          try {
1651              q.drainTo(q, 0);
1652              shouldThrow();
1653 <        } catch(IllegalArgumentException success) {
1896 <        }
1653 >        } catch (IllegalArgumentException success) {}
1654      }
1655  
1656      /**
# Line 1902 | Line 1659 | public class LinkedBlockingDequeTest ext
1659      public void testDrainToN() {
1660          LinkedBlockingDeque q = new LinkedBlockingDeque();
1661          for (int i = 0; i < SIZE + 2; ++i) {
1662 <            for(int j = 0; j < SIZE; j++)
1662 >            for (int j = 0; j < SIZE; j++)
1663                  assertTrue(q.offer(new Integer(j)));
1664              ArrayList l = new ArrayList();
1665              q.drainTo(l, i);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines