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.2 by dl, Wed Sep 14 23:50:31 2005 UTC vs.
Revision 1.12 by jsr166, Sat Nov 21 10:29:50 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines