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.1 by dl, Tue Dec 28 16:15:59 2004 UTC vs.
Revision 1.36 by jsr166, Thu Apr 14 22:55:08 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
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 +
15 +    public static class Unbounded extends BlockingQueueTest {
16 +        protected BlockingQueue emptyCollection() {
17 +            return new LinkedBlockingDeque();
18 +        }
19 +    }
20 +
21 +    public static class Bounded extends BlockingQueueTest {
22 +        protected BlockingQueue emptyCollection() {
23 +            return new LinkedBlockingDeque(20);
24 +        }
25 +    }
26 +
27      public static void main(String[] args) {
28 <        junit.textui.TestRunner.run (suite());  
28 >        junit.textui.TestRunner.run(suite());
29      }
30  
31      public static Test suite() {
32 <        return new TestSuite(LinkedBlockingDequeTest.class);
32 >        return newTestSuite(LinkedBlockingDequeTest.class,
33 >                            new Unbounded().testSuite(),
34 >                            new Bounded().testSuite());
35      }
36  
37      /**
38       * Create a deque of given size containing consecutive
39       * Integers 0 ... n.
40       */
41 <    private LinkedBlockingDeque populatedDeque(int n) {
42 <        LinkedBlockingDeque q = new LinkedBlockingDeque(n);
41 >    private LinkedBlockingDeque<Integer> populatedDeque(int n) {
42 >        LinkedBlockingDeque<Integer> q =
43 >            new LinkedBlockingDeque<Integer>(n);
44          assertTrue(q.isEmpty());
45 <        for(int i = 0; i < n; i++)
46 <            assertTrue(q.offer(new Integer(i)));
45 >        for (int i = 0; i < n; i++)
46 >            assertTrue(q.offer(new Integer(i)));
47          assertFalse(q.isEmpty());
48          assertEquals(0, q.remainingCapacity());
49 <        assertEquals(n, q.size());
49 >        assertEquals(n, q.size());
50          return q;
51      }
52  
# Line 66 | Line 83 | public class LinkedBlockingDequeTest ext
83       * offer(null) throws NPE
84       */
85      public void testOfferFirstNull() {
86 <        try {
86 >        try {
87              LinkedBlockingDeque q = new LinkedBlockingDeque();
88              q.offerFirst(null);
89              shouldThrow();
90 <        } catch (NullPointerException success) {
74 <        }  
90 >        } catch (NullPointerException success) {}
91      }
92  
93      /**
94 <     * OfferFirst succeeds
94 >     * OfferFirst succeeds
95       */
96      public void testOfferFirst() {
97          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 84 | Line 100 | public class LinkedBlockingDequeTest ext
100      }
101  
102      /**
103 <     * OfferLast succeeds
103 >     * OfferLast succeeds
104       */
105      public void testOfferLast() {
106          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 93 | Line 109 | public class LinkedBlockingDequeTest ext
109      }
110  
111      /**
112 <     *  pollFirst succeeds unless empty
112 >     * pollFirst succeeds unless empty
113       */
114      public void testPollFirst() {
115          LinkedBlockingDeque q = populatedDeque(SIZE);
116          for (int i = 0; i < SIZE; ++i) {
117 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
117 >            assertEquals(i, q.pollFirst());
118          }
119 <        assertNull(q.pollFirst());
119 >        assertNull(q.pollFirst());
120      }
121  
122      /**
123 <     *  pollLast succeeds unless empty
123 >     * pollLast succeeds unless empty
124       */
125      public void testPollLast() {
126          LinkedBlockingDeque q = populatedDeque(SIZE);
127          for (int i = SIZE-1; i >= 0; --i) {
128 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
128 >            assertEquals(i, q.pollLast());
129          }
130 <        assertNull(q.pollLast());
130 >        assertNull(q.pollLast());
131      }
132  
133      /**
134 <     *  peekFirst returns next element, or null if empty
134 >     * peekFirst returns next element, or null if empty
135       */
136      public void testPeekFirst() {
137          LinkedBlockingDeque q = populatedDeque(SIZE);
138          for (int i = 0; i < SIZE; ++i) {
139 <            assertEquals(i, ((Integer)q.peekFirst()).intValue());
140 <            q.pollFirst();
139 >            assertEquals(i, q.peekFirst());
140 >            assertEquals(i, q.pollFirst());
141              assertTrue(q.peekFirst() == null ||
142 <                       i != ((Integer)q.peekFirst()).intValue());
142 >                       !q.peekFirst().equals(i));
143          }
144 <        assertNull(q.peekFirst());
144 >        assertNull(q.peekFirst());
145      }
146  
147      /**
148 <     *  peek returns next element, or null if empty
148 >     * peek returns next element, or null if empty
149       */
150      public void testPeek() {
151          LinkedBlockingDeque q = populatedDeque(SIZE);
152          for (int i = 0; i < SIZE; ++i) {
153 <            assertEquals(i, ((Integer)q.peek()).intValue());
154 <            q.pollFirst();
153 >            assertEquals(i, q.peek());
154 >            assertEquals(i, q.pollFirst());
155              assertTrue(q.peek() == null ||
156 <                       i != ((Integer)q.peek()).intValue());
156 >                       !q.peek().equals(i));
157          }
158 <        assertNull(q.peek());
158 >        assertNull(q.peek());
159      }
160  
161      /**
162 <     *  peekLast returns next element, or null if empty
162 >     * peekLast returns next element, or null if empty
163       */
164      public void testPeekLast() {
165          LinkedBlockingDeque q = populatedDeque(SIZE);
166          for (int i = SIZE-1; i >= 0; --i) {
167 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
168 <            q.pollLast();
167 >            assertEquals(i, q.peekLast());
168 >            assertEquals(i, q.pollLast());
169              assertTrue(q.peekLast() == null ||
170 <                       i != ((Integer)q.peekLast()).intValue());
170 >                       !q.peekLast().equals(i));
171          }
172 <        assertNull(q.peekLast());
172 >        assertNull(q.peekLast());
173      }
174  
175      /**
176 <     * getFirst returns next getFirst, or throws NSEE if empty
176 >     * getFirst() returns first element, or throws NSEE if empty
177       */
178      public void testFirstElement() {
179          LinkedBlockingDeque q = populatedDeque(SIZE);
180          for (int i = 0; i < SIZE; ++i) {
181 <            assertEquals(i, ((Integer)q.getFirst()).intValue());
182 <            q.pollFirst();
181 >            assertEquals(i, q.getFirst());
182 >            assertEquals(i, q.pollFirst());
183          }
184          try {
185              q.getFirst();
186              shouldThrow();
187 <        }
188 <        catch (NoSuchElementException success) {}
187 >        } catch (NoSuchElementException success) {}
188 >        assertNull(q.peekFirst());
189      }
190  
191      /**
192 <     *  getLast returns next element, or throws NSEE if empty
192 >     * getLast() returns last element, or throws NSEE if empty
193       */
194      public void testLastElement() {
195          LinkedBlockingDeque q = populatedDeque(SIZE);
196          for (int i = SIZE-1; i >= 0; --i) {
197 <            assertEquals(i, ((Integer)q.getLast()).intValue());
198 <            q.pollLast();
197 >            assertEquals(i, q.getLast());
198 >            assertEquals(i, q.pollLast());
199          }
200          try {
201              q.getLast();
202              shouldThrow();
203 <        }
204 <        catch (NoSuchElementException success) {}
189 <        assertNull(q.peekLast());
203 >        } catch (NoSuchElementException success) {}
204 >        assertNull(q.peekLast());
205      }
206  
207      /**
208 <     *  removeFirst removes next element, or throws NSEE if empty
208 >     * removeFirst() removes first element, or throws NSEE if empty
209       */
210      public void testRemoveFirst() {
211          LinkedBlockingDeque q = populatedDeque(SIZE);
212          for (int i = 0; i < SIZE; ++i) {
213 <            assertEquals(i, ((Integer)q.removeFirst()).intValue());
213 >            assertEquals(i, q.removeFirst());
214          }
215          try {
216              q.removeFirst();
217              shouldThrow();
218 <        } catch (NoSuchElementException success){
219 <        }  
218 >        } catch (NoSuchElementException success) {}
219 >        assertNull(q.peekFirst());
220      }
221  
222      /**
223 <     *  remove removes next element, or throws NSEE if empty
223 >     * removeLast() removes last element, or throws NSEE if empty
224 >     */
225 >    public void testRemoveLast() {
226 >        LinkedBlockingDeque q = populatedDeque(SIZE);
227 >        for (int i = SIZE - 1; i >= 0; --i) {
228 >            assertEquals(i, q.removeLast());
229 >        }
230 >        try {
231 >            q.removeLast();
232 >            shouldThrow();
233 >        } catch (NoSuchElementException success) {}
234 >        assertNull(q.peekLast());
235 >    }
236 >
237 >    /**
238 >     * remove removes next element, or throws NSEE if empty
239       */
240      public void testRemove() {
241          LinkedBlockingDeque q = populatedDeque(SIZE);
242          for (int i = 0; i < SIZE; ++i) {
243 <            assertEquals(i, ((Integer)q.remove()).intValue());
243 >            assertEquals(i, q.remove());
244          }
245          try {
246              q.remove();
247              shouldThrow();
248 <        } catch (NoSuchElementException success){
219 <        }  
248 >        } catch (NoSuchElementException success) {}
249      }
250  
251      /**
# Line 255 | Line 284 | public class LinkedBlockingDequeTest ext
284      public void testAddFirst() {
285          LinkedBlockingDeque q = populatedDeque(3);
286          q.pollLast();
287 <        q.addFirst(four);
288 <        assertEquals(four,q.peekFirst());
289 <    }  
287 >        q.addFirst(four);
288 >        assertSame(four, q.peekFirst());
289 >    }
290  
291      /**
292       * peekLast returns element inserted with addLast
# Line 265 | Line 294 | public class LinkedBlockingDequeTest ext
294      public void testAddLast() {
295          LinkedBlockingDeque q = populatedDeque(3);
296          q.pollLast();
297 <        q.addLast(four);
298 <        assertEquals(four,q.peekLast());
299 <    }  
297 >        q.addLast(four);
298 >        assertSame(four, q.peekLast());
299 >    }
300  
301  
302      /**
# Line 280 | Line 309 | public class LinkedBlockingDequeTest ext
309      }
310  
311      /**
312 <     * Constructor throws IAE if  capacity argument nonpositive
312 >     * Constructor throws IAE if capacity argument nonpositive
313       */
314      public void testConstructor2() {
315          try {
316              LinkedBlockingDeque q = new LinkedBlockingDeque(0);
317              shouldThrow();
318 <        }
290 <        catch (IllegalArgumentException success) {}
318 >        } catch (IllegalArgumentException success) {}
319      }
320  
321      /**
# Line 297 | Line 325 | public class LinkedBlockingDequeTest ext
325          try {
326              LinkedBlockingDeque q = new LinkedBlockingDeque(null);
327              shouldThrow();
328 <        }
301 <        catch (NullPointerException success) {}
328 >        } catch (NullPointerException success) {}
329      }
330  
331      /**
# Line 309 | Line 336 | public class LinkedBlockingDequeTest ext
336              Integer[] ints = new Integer[SIZE];
337              LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
338              shouldThrow();
339 <        }
313 <        catch (NullPointerException success) {}
339 >        } catch (NullPointerException success) {}
340      }
341  
342      /**
# Line 323 | Line 349 | public class LinkedBlockingDequeTest ext
349                  ints[i] = new Integer(i);
350              LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
351              shouldThrow();
352 <        }
327 <        catch (NullPointerException success) {}
352 >        } catch (NullPointerException success) {}
353      }
354  
355      /**
356       * Deque contains all elements of collection used to initialize
357       */
358      public void testConstructor6() {
359 <        try {
360 <            Integer[] ints = new Integer[SIZE];
361 <            for (int i = 0; i < SIZE; ++i)
362 <                ints[i] = new Integer(i);
363 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
364 <            for (int i = 0; i < SIZE; ++i)
340 <                assertEquals(ints[i], q.poll());
341 <        }
342 <        finally {}
359 >        Integer[] ints = new Integer[SIZE];
360 >        for (int i = 0; i < SIZE; ++i)
361 >            ints[i] = new Integer(i);
362 >        LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
363 >        for (int i = 0; i < SIZE; ++i)
364 >            assertEquals(ints[i], q.poll());
365      }
366  
367      /**
# Line 378 | Line 400 | public class LinkedBlockingDequeTest ext
400       * offer(null) throws NPE
401       */
402      public void testOfferNull() {
403 <        try {
403 >        try {
404              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
405              q.offer(null);
406              shouldThrow();
407 <        } catch (NullPointerException success) { }  
407 >        } catch (NullPointerException success) {}
408      }
409  
410      /**
411       * add(null) throws NPE
412       */
413      public void testAddNull() {
414 <        try {
414 >        try {
415              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
416              q.add(null);
417              shouldThrow();
418 <        } catch (NullPointerException success) { }  
418 >        } catch (NullPointerException success) {}
419      }
420  
421      /**
422       * push(null) throws NPE
423       */
424      public void testPushNull() {
425 <        try {
425 >        try {
426              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
427              q.push(null);
428              shouldThrow();
429 <        } catch (NullPointerException success) { }  
429 >        } catch (NullPointerException success) {}
430      }
431  
432      /**
433       * push succeeds if not full; throws ISE if full
434       */
435      public void testPush() {
436 <        try {
436 >        try {
437              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
438              for (int i = 0; i < SIZE; ++i) {
439                  Integer I = new Integer(i);
# Line 420 | Line 442 | public class LinkedBlockingDequeTest ext
442              }
443              assertEquals(0, q.remainingCapacity());
444              q.push(new Integer(SIZE));
445 <        } catch (IllegalStateException success){
446 <        }  
445 >            shouldThrow();
446 >        } catch (IllegalStateException success) {}
447      }
448  
449      /**
# Line 430 | Line 452 | public class LinkedBlockingDequeTest ext
452      public void testPushWithPeek() {
453          LinkedBlockingDeque q = populatedDeque(3);
454          q.pollLast();
455 <        q.push(four);
456 <        assertEquals(four,q.peekFirst());
457 <    }  
455 >        q.push(four);
456 >        assertSame(four, q.peekFirst());
457 >    }
458  
459  
460      /**
461 <     *  pop removes next element, or throws NSEE if empty
461 >     * pop removes next element, or throws NSEE if empty
462       */
463      public void testPop() {
464          LinkedBlockingDeque q = populatedDeque(SIZE);
465          for (int i = 0; i < SIZE; ++i) {
466 <            assertEquals(i, ((Integer)q.pop()).intValue());
466 >            assertEquals(i, q.pop());
467          }
468          try {
469              q.pop();
470              shouldThrow();
471 <        } catch (NoSuchElementException success){
450 <        }  
471 >        } catch (NoSuchElementException success) {}
472      }
473  
474  
# Line 464 | Line 485 | public class LinkedBlockingDequeTest ext
485       * add succeeds if not full; throws ISE if full
486       */
487      public void testAdd() {
488 <        try {
488 >        try {
489              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
490              for (int i = 0; i < SIZE; ++i) {
491                  assertTrue(q.add(new Integer(i)));
492              }
493              assertEquals(0, q.remainingCapacity());
494              q.add(new Integer(SIZE));
495 <        } catch (IllegalStateException success){
496 <        }  
495 >            shouldThrow();
496 >        } catch (IllegalStateException success) {}
497      }
498  
499      /**
# Line 483 | Line 504 | public class LinkedBlockingDequeTest ext
504              LinkedBlockingDeque q = new LinkedBlockingDeque(1);
505              q.addAll(null);
506              shouldThrow();
507 <        }
487 <        catch (NullPointerException success) {}
507 >        } catch (NullPointerException success) {}
508      }
509  
510      /**
# Line 495 | Line 515 | public class LinkedBlockingDequeTest ext
515              LinkedBlockingDeque q = populatedDeque(SIZE);
516              q.addAll(q);
517              shouldThrow();
518 <        }
499 <        catch (IllegalArgumentException success) {}
518 >        } catch (IllegalArgumentException success) {}
519      }
520  
521      /**
# Line 508 | Line 527 | public class LinkedBlockingDequeTest ext
527              Integer[] ints = new Integer[SIZE];
528              q.addAll(Arrays.asList(ints));
529              shouldThrow();
530 <        }
512 <        catch (NullPointerException success) {}
530 >        } catch (NullPointerException success) {}
531      }
532 +
533      /**
534       * addAll of a collection with any null elements throws NPE after
535       * possibly adding some elements
# Line 523 | Line 542 | public class LinkedBlockingDequeTest ext
542                  ints[i] = new Integer(i);
543              q.addAll(Arrays.asList(ints));
544              shouldThrow();
545 <        }
527 <        catch (NullPointerException success) {}
545 >        } catch (NullPointerException success) {}
546      }
547 +
548      /**
549       * addAll throws ISE if not enough room
550       */
# Line 537 | Line 556 | public class LinkedBlockingDequeTest ext
556                  ints[i] = new Integer(i);
557              q.addAll(Arrays.asList(ints));
558              shouldThrow();
559 <        }
541 <        catch (IllegalStateException success) {}
559 >        } catch (IllegalStateException success) {}
560      }
561 +
562      /**
563       * Deque contains all elements, in traversal order, of successful addAll
564       */
565      public void testAddAll5() {
566 <        try {
567 <            Integer[] empty = new Integer[0];
568 <            Integer[] ints = new Integer[SIZE];
569 <            for (int i = 0; i < SIZE; ++i)
570 <                ints[i] = new Integer(i);
571 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
572 <            assertFalse(q.addAll(Arrays.asList(empty)));
573 <            assertTrue(q.addAll(Arrays.asList(ints)));
574 <            for (int i = 0; i < SIZE; ++i)
556 <                assertEquals(ints[i], q.poll());
557 <        }
558 <        finally {}
566 >        Integer[] empty = new Integer[0];
567 >        Integer[] ints = new Integer[SIZE];
568 >        for (int i = 0; i < SIZE; ++i)
569 >            ints[i] = new Integer(i);
570 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
571 >        assertFalse(q.addAll(Arrays.asList(empty)));
572 >        assertTrue(q.addAll(Arrays.asList(ints)));
573 >        for (int i = 0; i < SIZE; ++i)
574 >            assertEquals(ints[i], q.poll());
575      }
576  
577  
578      /**
579       * put(null) throws NPE
580       */
581 <     public void testPutNull() {
582 <        try {
581 >    public void testPutNull() throws InterruptedException {
582 >        try {
583              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
584              q.put(null);
585              shouldThrow();
586 <        }
587 <        catch (NullPointerException success){
572 <        }  
573 <        catch (InterruptedException ie) {
574 <            unexpectedException();
575 <        }
576 <     }
586 >        } catch (NullPointerException success) {}
587 >    }
588  
589      /**
590       * all elements successfully put are contained
591       */
592 <     public void testPut() {
593 <         try {
594 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
595 <             for (int i = 0; i < SIZE; ++i) {
596 <                 Integer I = new Integer(i);
597 <                 q.put(I);
587 <                 assertTrue(q.contains(I));
588 <             }
589 <             assertEquals(0, q.remainingCapacity());
590 <         }
591 <        catch (InterruptedException ie) {
592 <            unexpectedException();
592 >    public void testPut() throws InterruptedException {
593 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
594 >        for (int i = 0; i < SIZE; ++i) {
595 >            Integer I = new Integer(i);
596 >            q.put(I);
597 >            assertTrue(q.contains(I));
598          }
599 +        assertEquals(0, q.remainingCapacity());
600      }
601  
602      /**
603       * put blocks interruptibly if full
604       */
605 <    public void testBlockingPut() {
606 <        Thread t = new Thread(new Runnable() {
607 <                public void run() {
608 <                    int added = 0;
609 <                    try {
610 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
611 <                        for (int i = 0; i < SIZE; ++i) {
612 <                            q.put(new Integer(i));
613 <                            ++added;
614 <                        }
615 <                        q.put(new Integer(SIZE));
616 <                        threadShouldThrow();
617 <                    } catch (InterruptedException ie){
618 <                        threadAssertEquals(added, SIZE);
613 <                    }  
614 <                }});
605 >    public void testBlockingPut() throws InterruptedException {
606 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
607 >        Thread t = new Thread(new CheckedRunnable() {
608 >            public void realRun() throws InterruptedException {
609 >                for (int i = 0; i < SIZE; ++i)
610 >                    q.put(i);
611 >                assertEquals(SIZE, q.size());
612 >                assertEquals(0, q.remainingCapacity());
613 >                try {
614 >                    q.put(99);
615 >                    shouldThrow();
616 >                } catch (InterruptedException success) {}
617 >            }});
618 >
619          t.start();
620 <        try {
621 <           Thread.sleep(SHORT_DELAY_MS);
622 <           t.interrupt();
623 <           t.join();
624 <        }
621 <        catch (InterruptedException ie) {
622 <            unexpectedException();
623 <        }
620 >        Thread.sleep(SHORT_DELAY_MS);
621 >        t.interrupt();
622 >        t.join();
623 >        assertEquals(SIZE, q.size());
624 >        assertEquals(0, q.remainingCapacity());
625      }
626  
627      /**
628       * put blocks waiting for take when full
629       */
630 <    public void testPutWithTake() {
631 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
632 <        Thread t = new Thread(new Runnable() {
633 <                public void run() {
634 <                    int added = 0;
635 <                    try {
636 <                        q.put(new Object());
637 <                        ++added;
638 <                        q.put(new Object());
639 <                        ++added;
640 <                        q.put(new Object());
641 <                        ++added;
642 <                        q.put(new Object());
643 <                        ++added;
644 <                        threadShouldThrow();
645 <                    } catch (InterruptedException e){
646 <                        threadAssertTrue(added >= 2);
647 <                    }
648 <                }
649 <            });
650 <        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 <        }
630 >    public void testPutWithTake() throws InterruptedException {
631 >        final int capacity = 2;
632 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
633 >        Thread t = new Thread(new CheckedRunnable() {
634 >            public void realRun() throws InterruptedException {
635 >                for (int i = 0; i < capacity + 1; i++)
636 >                    q.put(i);
637 >                try {
638 >                    q.put(99);
639 >                    shouldThrow();
640 >                } catch (InterruptedException success) {}
641 >            }});
642 >
643 >        t.start();
644 >        Thread.sleep(SHORT_DELAY_MS);
645 >        assertEquals(q.remainingCapacity(), 0);
646 >        assertEquals(0, q.take());
647 >        Thread.sleep(SHORT_DELAY_MS);
648 >        t.interrupt();
649 >        t.join();
650 >        assertEquals(q.remainingCapacity(), 0);
651      }
652  
653      /**
654       * timed offer times out if full and elements not taken
655       */
656 <    public void testTimedOffer() {
656 >    public void testTimedOffer() throws InterruptedException {
657          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
658 <        Thread t = new Thread(new Runnable() {
659 <                public void run() {
660 <                    try {
661 <                        q.put(new Object());
662 <                        q.put(new Object());
663 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
664 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
665 <                        threadShouldThrow();
666 <                    } catch (InterruptedException success){}
667 <                }
675 <            });
676 <        
677 <        try {
678 <            t.start();
679 <            Thread.sleep(SMALL_DELAY_MS);
680 <            t.interrupt();
681 <            t.join();
682 <        } catch (Exception e){
683 <            unexpectedException();
684 <        }
685 <    }
658 >        Thread t = new Thread(new CheckedRunnable() {
659 >            public void realRun() throws InterruptedException {
660 >                q.put(new Object());
661 >                q.put(new Object());
662 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
663 >                try {
664 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
665 >                    shouldThrow();
666 >                } catch (InterruptedException success) {}
667 >            }});
668  
669 <    /**
670 <     * take retrieves elements in FIFO order
671 <     */
672 <    public void testTake() {
691 <        try {
692 <            LinkedBlockingDeque q = populatedDeque(SIZE);
693 <            for (int i = 0; i < SIZE; ++i) {
694 <                assertEquals(i, ((Integer)q.take()).intValue());
695 <            }
696 <        } catch (InterruptedException e){
697 <            unexpectedException();
698 <        }  
669 >        t.start();
670 >        Thread.sleep(SMALL_DELAY_MS);
671 >        t.interrupt();
672 >        t.join();
673      }
674  
675      /**
676 <     * take blocks interruptibly when empty
676 >     * take retrieves elements in FIFO order
677       */
678 <    public void testTakeFromEmpty() {
679 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
680 <        Thread t = new Thread(new Runnable() {
681 <                public void run() {
708 <                    try {
709 <                        q.take();
710 <                        threadShouldThrow();
711 <                    } catch (InterruptedException success){ }                
712 <                }
713 <            });
714 <        try {
715 <            t.start();
716 <            Thread.sleep(SHORT_DELAY_MS);
717 <            t.interrupt();
718 <            t.join();
719 <        } catch (Exception e){
720 <            unexpectedException();
678 >    public void testTake() throws InterruptedException {
679 >        LinkedBlockingDeque q = populatedDeque(SIZE);
680 >        for (int i = 0; i < SIZE; ++i) {
681 >            assertEquals(i, q.take());
682          }
683      }
684  
685      /**
686       * Take removes existing elements until empty, then blocks interruptibly
687       */
688 <    public void testBlockingTake() {
689 <        Thread t = new Thread(new Runnable() {
690 <                public void run() {
691 <                    try {
692 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
693 <                        for (int i = 0; i < SIZE; ++i) {
694 <                            assertEquals(i, ((Integer)q.take()).intValue());
695 <                        }
696 <                        q.take();
697 <                        threadShouldThrow();
698 <                    } catch (InterruptedException success){
699 <                    }  
700 <                }});
688 >    public void testBlockingTake() throws InterruptedException {
689 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
690 >        Thread t = new Thread(new CheckedRunnable() {
691 >            public void realRun() throws InterruptedException {
692 >                for (int i = 0; i < SIZE; ++i) {
693 >                    assertEquals(i, q.take());
694 >                }
695 >                try {
696 >                    q.take();
697 >                    shouldThrow();
698 >                } catch (InterruptedException success) {}
699 >            }});
700 >
701          t.start();
702 <        try {
703 <           Thread.sleep(SHORT_DELAY_MS);
704 <           t.interrupt();
744 <           t.join();
745 <        }
746 <        catch (InterruptedException ie) {
747 <            unexpectedException();
748 <        }
702 >        Thread.sleep(SHORT_DELAY_MS);
703 >        t.interrupt();
704 >        t.join();
705      }
706  
707  
# Line 755 | Line 711 | public class LinkedBlockingDequeTest ext
711      public void testPoll() {
712          LinkedBlockingDeque q = populatedDeque(SIZE);
713          for (int i = 0; i < SIZE; ++i) {
714 <            assertEquals(i, ((Integer)q.poll()).intValue());
714 >            assertEquals(i, q.poll());
715          }
716 <        assertNull(q.poll());
716 >        assertNull(q.poll());
717      }
718  
719      /**
720       * timed poll with zero timeout succeeds when non-empty, else times out
721       */
722 <    public void testTimedPoll0() {
723 <        try {
724 <            LinkedBlockingDeque q = populatedDeque(SIZE);
725 <            for (int i = 0; i < SIZE; ++i) {
726 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
727 <            }
772 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
773 <        } catch (InterruptedException e){
774 <            unexpectedException();
775 <        }  
722 >    public void testTimedPoll0() throws InterruptedException {
723 >        LinkedBlockingDeque q = populatedDeque(SIZE);
724 >        for (int i = 0; i < SIZE; ++i) {
725 >            assertEquals(i, q.poll(0, MILLISECONDS));
726 >        }
727 >        assertNull(q.poll(0, MILLISECONDS));
728      }
729  
730      /**
731       * timed poll with nonzero timeout succeeds when non-empty, else times out
732       */
733 <    public void testTimedPoll() {
734 <        try {
735 <            LinkedBlockingDeque q = populatedDeque(SIZE);
736 <            for (int i = 0; i < SIZE; ++i) {
737 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
738 <            }
787 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
788 <        } catch (InterruptedException e){
789 <            unexpectedException();
790 <        }  
733 >    public void testTimedPoll() throws InterruptedException {
734 >        LinkedBlockingDeque q = populatedDeque(SIZE);
735 >        for (int i = 0; i < SIZE; ++i) {
736 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
737 >        }
738 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
739      }
740  
741      /**
742       * Interrupted timed poll throws InterruptedException instead of
743       * returning timeout status
744       */
745 <    public void testInterruptedTimedPoll() {
746 <        Thread t = new Thread(new Runnable() {
747 <                public void run() {
748 <                    try {
749 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
750 <                        for (int i = 0; i < SIZE; ++i) {
751 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
752 <                        }
753 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
806 <                    } catch (InterruptedException success){
807 <                    }  
808 <                }});
809 <        t.start();
810 <        try {
811 <           Thread.sleep(SHORT_DELAY_MS);
812 <           t.interrupt();
813 <           t.join();
814 <        }
815 <        catch (InterruptedException ie) {
816 <            unexpectedException();
817 <        }
818 <    }
819 <
820 <    /**
821 <     *  timed poll before a delayed offer fails; after offer succeeds;
822 <     *  on interruption throws
823 <     */
824 <    public void testTimedPollWithOffer() {
825 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
826 <        Thread t = new Thread(new Runnable() {
827 <                public void run() {
828 <                    try {
829 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
830 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
831 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
832 <                        threadShouldThrow();
833 <                    } catch (InterruptedException success) { }                
745 >    public void testInterruptedTimedPoll() throws InterruptedException {
746 >        final BlockingQueue<Integer> q = populatedDeque(SIZE);
747 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
748 >        Thread t = newStartedThread(new CheckedRunnable() {
749 >            public void realRun() throws InterruptedException {
750 >                for (int i = 0; i < SIZE; ++i) {
751 >                    long t0 = System.nanoTime();
752 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
753 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
754                  }
755 <            });
756 <        try {
757 <            t.start();
758 <            Thread.sleep(SMALL_DELAY_MS);
759 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
760 <            t.interrupt();
761 <            t.join();
762 <        } catch (Exception e){
763 <            unexpectedException();
764 <        }
765 <    }  
766 <
755 >                long t0 = System.nanoTime();
756 >                aboutToWait.countDown();
757 >                try {
758 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
759 >                    shouldThrow();
760 >                } catch (InterruptedException success) {
761 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
762 >                }
763 >            }});
764 >
765 >        aboutToWait.await();
766 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
767 >        t.interrupt();
768 >        awaitTermination(t, MEDIUM_DELAY_MS);
769 >        checkEmpty(q);
770 >    }
771  
772      /**
773       * putFirst(null) throws NPE
774       */
775 <     public void testPutFirstNull() {
776 <        try {
775 >    public void testPutFirstNull() throws InterruptedException {
776 >        try {
777              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
778              q.putFirst(null);
779              shouldThrow();
780 <        }
781 <        catch (NullPointerException success){
858 <        }  
859 <        catch (InterruptedException ie) {
860 <            unexpectedException();
861 <        }
862 <     }
780 >        } catch (NullPointerException success) {}
781 >    }
782  
783      /**
784       * all elements successfully putFirst are contained
785       */
786 <     public void testPutFirst() {
787 <         try {
788 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
789 <             for (int i = 0; i < SIZE; ++i) {
790 <                 Integer I = new Integer(i);
791 <                 q.putFirst(I);
873 <                 assertTrue(q.contains(I));
874 <             }
875 <             assertEquals(0, q.remainingCapacity());
876 <         }
877 <        catch (InterruptedException ie) {
878 <            unexpectedException();
786 >    public void testPutFirst() throws InterruptedException {
787 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
788 >        for (int i = 0; i < SIZE; ++i) {
789 >            Integer I = new Integer(i);
790 >            q.putFirst(I);
791 >            assertTrue(q.contains(I));
792          }
793 +        assertEquals(0, q.remainingCapacity());
794      }
795  
796      /**
797       * putFirst blocks interruptibly if full
798       */
799 <    public void testBlockingPutFirst() {
800 <        Thread t = new Thread(new Runnable() {
801 <                public void run() {
802 <                    int added = 0;
803 <                    try {
804 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
805 <                        for (int i = 0; i < SIZE; ++i) {
806 <                            q.putFirst(new Integer(i));
807 <                            ++added;
808 <                        }
809 <                        q.putFirst(new Integer(SIZE));
810 <                        threadShouldThrow();
811 <                    } catch (InterruptedException ie){
812 <                        threadAssertEquals(added, SIZE);
899 <                    }  
900 <                }});
799 >    public void testBlockingPutFirst() throws InterruptedException {
800 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
801 >        Thread t = new Thread(new CheckedRunnable() {
802 >            public void realRun() throws InterruptedException {
803 >                for (int i = 0; i < SIZE; ++i)
804 >                    q.putFirst(i);
805 >                assertEquals(SIZE, q.size());
806 >                assertEquals(0, q.remainingCapacity());
807 >                try {
808 >                    q.putFirst(99);
809 >                    shouldThrow();
810 >                } catch (InterruptedException success) {}
811 >            }});
812 >
813          t.start();
814 <        try {
815 <           Thread.sleep(SHORT_DELAY_MS);
816 <           t.interrupt();
817 <           t.join();
818 <        }
907 <        catch (InterruptedException ie) {
908 <            unexpectedException();
909 <        }
814 >        Thread.sleep(SHORT_DELAY_MS);
815 >        t.interrupt();
816 >        t.join();
817 >        assertEquals(SIZE, q.size());
818 >        assertEquals(0, q.remainingCapacity());
819      }
820  
821      /**
822       * putFirst blocks waiting for take when full
823       */
824 <    public void testPutFirstWithTake() {
825 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
826 <        Thread t = new Thread(new Runnable() {
827 <                public void run() {
828 <                    int added = 0;
829 <                    try {
830 <                        q.putFirst(new Object());
831 <                        ++added;
832 <                        q.putFirst(new Object());
833 <                        ++added;
834 <                        q.putFirst(new Object());
835 <                        ++added;
836 <                        q.putFirst(new Object());
837 <                        ++added;
838 <                        threadShouldThrow();
839 <                    } catch (InterruptedException e){
840 <                        threadAssertTrue(added >= 2);
841 <                    }
842 <                }
843 <            });
844 <        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 <        }
824 >    public void testPutFirstWithTake() throws InterruptedException {
825 >        final int capacity = 2;
826 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
827 >        Thread t = new Thread(new CheckedRunnable() {
828 >            public void realRun() throws InterruptedException {
829 >                for (int i = 0; i < capacity + 1; i++)
830 >                    q.putFirst(i);
831 >                try {
832 >                    q.putFirst(99);
833 >                    shouldThrow();
834 >                } catch (InterruptedException success) {}
835 >            }});
836 >
837 >        t.start();
838 >        Thread.sleep(SHORT_DELAY_MS);
839 >        assertEquals(q.remainingCapacity(), 0);
840 >        assertEquals(capacity - 1, q.take());
841 >        Thread.sleep(SHORT_DELAY_MS);
842 >        t.interrupt();
843 >        t.join();
844 >        assertEquals(q.remainingCapacity(), 0);
845      }
846  
847      /**
848       * timed offerFirst times out if full and elements not taken
849       */
850 <    public void testTimedOfferFirst() {
850 >    public void testTimedOfferFirst() throws InterruptedException {
851          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
852 <        Thread t = new Thread(new Runnable() {
853 <                public void run() {
854 <                    try {
855 <                        q.putFirst(new Object());
856 <                        q.putFirst(new Object());
857 <                        threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
858 <                        q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
859 <                        threadShouldThrow();
860 <                    } catch (InterruptedException success){}
861 <                }
862 <            });
863 <        
864 <        try {
865 <            t.start();
866 <            Thread.sleep(SMALL_DELAY_MS);
966 <            t.interrupt();
967 <            t.join();
968 <        } catch (Exception e){
969 <            unexpectedException();
970 <        }
852 >        Thread t = new Thread(new CheckedRunnable() {
853 >            public void realRun() throws InterruptedException {
854 >                q.putFirst(new Object());
855 >                q.putFirst(new Object());
856 >                assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
857 >                try {
858 >                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
859 >                    shouldThrow();
860 >                } catch (InterruptedException success) {}
861 >            }});
862 >
863 >        t.start();
864 >        Thread.sleep(SMALL_DELAY_MS);
865 >        t.interrupt();
866 >        t.join();
867      }
868  
869      /**
870       * take retrieves elements in FIFO order
871       */
872 <    public void testTakeFirst() {
873 <        try {
874 <            LinkedBlockingDeque q = populatedDeque(SIZE);
875 <            for (int i = 0; i < SIZE; ++i) {
876 <                assertEquals(i, ((Integer)q.takeFirst()).intValue());
981 <            }
982 <        } catch (InterruptedException e){
983 <            unexpectedException();
984 <        }  
872 >    public void testTakeFirst() throws InterruptedException {
873 >        LinkedBlockingDeque q = populatedDeque(SIZE);
874 >        for (int i = 0; i < SIZE; ++i) {
875 >            assertEquals(i, q.takeFirst());
876 >        }
877      }
878  
879      /**
880       * takeFirst blocks interruptibly when empty
881       */
882 <    public void testTakeFirstFromEmpty() {
882 >    public void testTakeFirstFromEmpty() throws InterruptedException {
883          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
884 <        Thread t = new Thread(new Runnable() {
885 <                public void run() {
886 <                    try {
887 <                        q.takeFirst();
888 <                        threadShouldThrow();
889 <                    } catch (InterruptedException success){ }                
890 <                }
891 <            });
892 <        try {
1001 <            t.start();
1002 <            Thread.sleep(SHORT_DELAY_MS);
1003 <            t.interrupt();
1004 <            t.join();
1005 <        } catch (Exception e){
1006 <            unexpectedException();
1007 <        }
884 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
885 >            public void realRun() throws InterruptedException {
886 >                q.takeFirst();
887 >            }};
888 >
889 >        t.start();
890 >        Thread.sleep(SHORT_DELAY_MS);
891 >        t.interrupt();
892 >        t.join();
893      }
894  
895      /**
896       * TakeFirst removes existing elements until empty, then blocks interruptibly
897       */
898 <    public void testBlockingTakeFirst() {
899 <        Thread t = new Thread(new Runnable() {
900 <                public void run() {
901 <                    try {
902 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
903 <                        for (int i = 0; i < SIZE; ++i) {
904 <                            assertEquals(i, ((Integer)q.takeFirst()).intValue());
905 <                        }
906 <                        q.takeFirst();
907 <                        threadShouldThrow();
908 <                    } catch (InterruptedException success){
909 <                    }  
1025 <                }});
898 >    public void testBlockingTakeFirst() throws InterruptedException {
899 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
900 >        Thread t = new Thread(new CheckedRunnable() {
901 >            public void realRun() throws InterruptedException {
902 >                for (int i = 0; i < SIZE; ++i)
903 >                    assertEquals(i, q.takeFirst());
904 >                try {
905 >                    q.takeFirst();
906 >                    shouldThrow();
907 >                } catch (InterruptedException success) {}
908 >            }});
909 >
910          t.start();
911 <        try {
912 <           Thread.sleep(SHORT_DELAY_MS);
913 <           t.interrupt();
1030 <           t.join();
1031 <        }
1032 <        catch (InterruptedException ie) {
1033 <            unexpectedException();
1034 <        }
911 >        Thread.sleep(SHORT_DELAY_MS);
912 >        t.interrupt();
913 >        t.join();
914      }
915  
916  
917      /**
918       * timed pollFirst with zero timeout succeeds when non-empty, else times out
919       */
920 <    public void testTimedPollFirst0() {
921 <        try {
922 <            LinkedBlockingDeque q = populatedDeque(SIZE);
923 <            for (int i = 0; i < SIZE; ++i) {
924 <                assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue());
925 <            }
1047 <            assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS));
1048 <        } catch (InterruptedException e){
1049 <            unexpectedException();
1050 <        }  
920 >    public void testTimedPollFirst0() throws InterruptedException {
921 >        LinkedBlockingDeque q = populatedDeque(SIZE);
922 >        for (int i = 0; i < SIZE; ++i) {
923 >            assertEquals(i, q.pollFirst(0, MILLISECONDS));
924 >        }
925 >        assertNull(q.pollFirst(0, MILLISECONDS));
926      }
927  
928      /**
929       * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
930       */
931 <    public void testTimedPollFirst() {
932 <        try {
933 <            LinkedBlockingDeque q = populatedDeque(SIZE);
934 <            for (int i = 0; i < SIZE; ++i) {
935 <                assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
936 <            }
1062 <            assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1063 <        } catch (InterruptedException e){
1064 <            unexpectedException();
1065 <        }  
931 >    public void testTimedPollFirst() throws InterruptedException {
932 >        LinkedBlockingDeque q = populatedDeque(SIZE);
933 >        for (int i = 0; i < SIZE; ++i) {
934 >            assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
935 >        }
936 >        assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
937      }
938  
939      /**
940       * Interrupted timed pollFirst throws InterruptedException instead of
941       * returning timeout status
942       */
943 <    public void testInterruptedTimedPollFirst() {
944 <        Thread t = new Thread(new Runnable() {
945 <                public void run() {
946 <                    try {
947 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
948 <                        for (int i = 0; i < SIZE; ++i) {
949 <                            threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
950 <                        }
951 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
952 <                    } catch (InterruptedException success){
953 <                    }  
954 <                }});
943 >    public void testInterruptedTimedPollFirst() throws InterruptedException {
944 >        Thread t = new Thread(new CheckedRunnable() {
945 >            public void realRun() throws InterruptedException {
946 >                LinkedBlockingDeque q = populatedDeque(SIZE);
947 >                for (int i = 0; i < SIZE; ++i) {
948 >                    assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
949 >                }
950 >                try {
951 >                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
952 >                    shouldThrow();
953 >                } catch (InterruptedException success) {}
954 >            }});
955 >
956          t.start();
957 <        try {
958 <           Thread.sleep(SHORT_DELAY_MS);
959 <           t.interrupt();
1088 <           t.join();
1089 <        }
1090 <        catch (InterruptedException ie) {
1091 <            unexpectedException();
1092 <        }
957 >        Thread.sleep(SHORT_DELAY_MS);
958 >        t.interrupt();
959 >        t.join();
960      }
961  
962      /**
963 <     *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
964 <     *  on interruption throws
963 >     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
964 >     * on interruption throws
965       */
966 <    public void testTimedPollFirstWithOfferFirst() {
966 >    public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
967          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
968 <        Thread t = new Thread(new Runnable() {
969 <                public void run() {
970 <                    try {
971 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
972 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
973 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
974 <                        threadShouldThrow();
975 <                    } catch (InterruptedException success) { }                
976 <                }
977 <            });
978 <        try {
979 <            t.start();
980 <            Thread.sleep(SMALL_DELAY_MS);
981 <            assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
982 <            t.interrupt();
983 <            t.join();
1117 <        } catch (Exception e){
1118 <            unexpectedException();
1119 <        }
1120 <    }  
968 >        Thread t = new Thread(new CheckedRunnable() {
969 >            public void realRun() throws InterruptedException {
970 >                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
971 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
972 >                try {
973 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
974 >                    shouldThrow();
975 >                } catch (InterruptedException success) {}
976 >            }});
977 >
978 >        t.start();
979 >        Thread.sleep(SMALL_DELAY_MS);
980 >        assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
981 >        t.interrupt();
982 >        t.join();
983 >    }
984  
985      /**
986       * putLast(null) throws NPE
987       */
988 <     public void testPutLastNull() {
989 <        try {
988 >    public void testPutLastNull() throws InterruptedException {
989 >        try {
990              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
991              q.putLast(null);
992              shouldThrow();
993 <        }
994 <        catch (NullPointerException success){
1132 <        }  
1133 <        catch (InterruptedException ie) {
1134 <            unexpectedException();
1135 <        }
1136 <     }
993 >        } catch (NullPointerException success) {}
994 >    }
995  
996      /**
997       * all elements successfully putLast are contained
998       */
999 <     public void testPutLast() {
1000 <         try {
1001 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1002 <             for (int i = 0; i < SIZE; ++i) {
1003 <                 Integer I = new Integer(i);
1004 <                 q.putLast(I);
1147 <                 assertTrue(q.contains(I));
1148 <             }
1149 <             assertEquals(0, q.remainingCapacity());
1150 <         }
1151 <        catch (InterruptedException ie) {
1152 <            unexpectedException();
999 >    public void testPutLast() throws InterruptedException {
1000 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1001 >        for (int i = 0; i < SIZE; ++i) {
1002 >            Integer I = new Integer(i);
1003 >            q.putLast(I);
1004 >            assertTrue(q.contains(I));
1005          }
1006 +        assertEquals(0, q.remainingCapacity());
1007      }
1008  
1009      /**
1010       * putLast blocks interruptibly if full
1011       */
1012 <    public void testBlockingPutLast() {
1013 <        Thread t = new Thread(new Runnable() {
1014 <                public void run() {
1015 <                    int added = 0;
1016 <                    try {
1017 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1018 <                        for (int i = 0; i < SIZE; ++i) {
1019 <                            q.putLast(new Integer(i));
1020 <                            ++added;
1021 <                        }
1022 <                        q.putLast(new Integer(SIZE));
1023 <                        threadShouldThrow();
1024 <                    } catch (InterruptedException ie){
1025 <                        threadAssertEquals(added, SIZE);
1173 <                    }  
1174 <                }});
1012 >    public void testBlockingPutLast() throws InterruptedException {
1013 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1014 >        Thread t = new Thread(new CheckedRunnable() {
1015 >            public void realRun() throws InterruptedException {
1016 >                for (int i = 0; i < SIZE; ++i)
1017 >                    q.putLast(i);
1018 >                assertEquals(SIZE, q.size());
1019 >                assertEquals(0, q.remainingCapacity());
1020 >                try {
1021 >                    q.putLast(99);
1022 >                    shouldThrow();
1023 >                } catch (InterruptedException success) {}
1024 >            }});
1025 >
1026          t.start();
1027 <        try {
1028 <           Thread.sleep(SHORT_DELAY_MS);
1029 <           t.interrupt();
1030 <           t.join();
1031 <        }
1181 <        catch (InterruptedException ie) {
1182 <            unexpectedException();
1183 <        }
1027 >        Thread.sleep(SHORT_DELAY_MS);
1028 >        t.interrupt();
1029 >        t.join();
1030 >        assertEquals(SIZE, q.size());
1031 >        assertEquals(0, q.remainingCapacity());
1032      }
1033  
1034      /**
1035       * putLast blocks waiting for take when full
1036       */
1037 <    public void testPutLastWithTake() {
1038 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1039 <        Thread t = new Thread(new Runnable() {
1040 <                public void run() {
1041 <                    int added = 0;
1042 <                    try {
1043 <                        q.putLast(new Object());
1044 <                        ++added;
1045 <                        q.putLast(new Object());
1046 <                        ++added;
1047 <                        q.putLast(new Object());
1048 <                        ++added;
1049 <                        q.putLast(new Object());
1050 <                        ++added;
1051 <                        threadShouldThrow();
1052 <                    } catch (InterruptedException e){
1053 <                        threadAssertTrue(added >= 2);
1054 <                    }
1055 <                }
1056 <            });
1057 <        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 <        }
1037 >    public void testPutLastWithTake() throws InterruptedException {
1038 >        final int capacity = 2;
1039 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1040 >        Thread t = new Thread(new CheckedRunnable() {
1041 >            public void realRun() throws InterruptedException {
1042 >                for (int i = 0; i < capacity + 1; i++)
1043 >                    q.putLast(i);
1044 >                try {
1045 >                    q.putLast(99);
1046 >                    shouldThrow();
1047 >                } catch (InterruptedException success) {}
1048 >            }});
1049 >
1050 >        t.start();
1051 >        Thread.sleep(SHORT_DELAY_MS);
1052 >        assertEquals(q.remainingCapacity(), 0);
1053 >        assertEquals(0, q.take());
1054 >        Thread.sleep(SHORT_DELAY_MS);
1055 >        t.interrupt();
1056 >        t.join();
1057 >        assertEquals(q.remainingCapacity(), 0);
1058      }
1059  
1060      /**
1061       * timed offerLast times out if full and elements not taken
1062       */
1063 <    public void testTimedOfferLast() {
1063 >    public void testTimedOfferLast() throws InterruptedException {
1064          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1065 <        Thread t = new Thread(new Runnable() {
1066 <                public void run() {
1067 <                    try {
1068 <                        q.putLast(new Object());
1069 <                        q.putLast(new Object());
1070 <                        threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1071 <                        q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1072 <                        threadShouldThrow();
1073 <                    } catch (InterruptedException success){}
1074 <                }
1075 <            });
1076 <        
1077 <        try {
1078 <            t.start();
1079 <            Thread.sleep(SMALL_DELAY_MS);
1240 <            t.interrupt();
1241 <            t.join();
1242 <        } catch (Exception e){
1243 <            unexpectedException();
1244 <        }
1065 >        Thread t = new Thread(new CheckedRunnable() {
1066 >            public void realRun() throws InterruptedException {
1067 >                q.putLast(new Object());
1068 >                q.putLast(new Object());
1069 >                assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1070 >                try {
1071 >                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1072 >                    shouldThrow();
1073 >                } catch (InterruptedException success) {}
1074 >            }});
1075 >
1076 >        t.start();
1077 >        Thread.sleep(SMALL_DELAY_MS);
1078 >        t.interrupt();
1079 >        t.join();
1080      }
1081  
1082      /**
1083       * takeLast retrieves elements in FIFO order
1084       */
1085 <    public void testTakeLast() {
1086 <        try {
1087 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1088 <            for (int i = 0; i < SIZE; ++i) {
1089 <                assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1255 <            }
1256 <        } catch (InterruptedException e){
1257 <            unexpectedException();
1258 <        }  
1085 >    public void testTakeLast() throws InterruptedException {
1086 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1087 >        for (int i = 0; i < SIZE; ++i) {
1088 >            assertEquals(SIZE-i-1, q.takeLast());
1089 >        }
1090      }
1091  
1092      /**
1093       * takeLast blocks interruptibly when empty
1094       */
1095 <    public void testTakeLastFromEmpty() {
1095 >    public void testTakeLastFromEmpty() throws InterruptedException {
1096          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1097 <        Thread t = new Thread(new Runnable() {
1098 <                public void run() {
1099 <                    try {
1100 <                        q.takeLast();
1101 <                        threadShouldThrow();
1102 <                    } catch (InterruptedException success){ }                
1103 <                }
1104 <            });
1105 <        try {
1275 <            t.start();
1276 <            Thread.sleep(SHORT_DELAY_MS);
1277 <            t.interrupt();
1278 <            t.join();
1279 <        } catch (Exception e){
1280 <            unexpectedException();
1281 <        }
1097 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1098 >            public void realRun() throws InterruptedException {
1099 >                q.takeLast();
1100 >            }};
1101 >
1102 >        t.start();
1103 >        Thread.sleep(SHORT_DELAY_MS);
1104 >        t.interrupt();
1105 >        t.join();
1106      }
1107  
1108      /**
1109       * TakeLast removes existing elements until empty, then blocks interruptibly
1110       */
1111 <    public void testBlockingTakeLast() {
1112 <        Thread t = new Thread(new Runnable() {
1113 <                public void run() {
1114 <                    try {
1115 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1116 <                        for (int i = 0; i < SIZE; ++i) {
1117 <                            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1118 <                        }
1119 <                        q.takeLast();
1120 <                        threadShouldThrow();
1121 <                    } catch (InterruptedException success){
1122 <                    }  
1299 <                }});
1111 >    public void testBlockingTakeLast() throws InterruptedException {
1112 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1113 >        Thread t = new Thread(new CheckedRunnable() {
1114 >            public void realRun() throws InterruptedException {
1115 >                for (int i = 0; i < SIZE; ++i)
1116 >                    assertEquals(SIZE - 1 - i, q.takeLast());
1117 >                try {
1118 >                    q.takeLast();
1119 >                    shouldThrow();
1120 >                } catch (InterruptedException success) {}
1121 >            }});
1122 >
1123          t.start();
1124 <        try {
1125 <           Thread.sleep(SHORT_DELAY_MS);
1126 <           t.interrupt();
1304 <           t.join();
1305 <        }
1306 <        catch (InterruptedException ie) {
1307 <            unexpectedException();
1308 <        }
1124 >        Thread.sleep(SHORT_DELAY_MS);
1125 >        t.interrupt();
1126 >        t.join();
1127      }
1128  
1311
1129      /**
1130       * timed pollLast with zero timeout succeeds when non-empty, else times out
1131       */
1132 <    public void testTimedPollLast0() {
1133 <        try {
1134 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1135 <            for (int i = 0; i < SIZE; ++i) {
1136 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue());
1137 <            }
1321 <            assertNull(q.pollLast(0, TimeUnit.MILLISECONDS));
1322 <        } catch (InterruptedException e){
1323 <            unexpectedException();
1324 <        }  
1132 >    public void testTimedPollLast0() throws InterruptedException {
1133 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1134 >        for (int i = 0; i < SIZE; ++i) {
1135 >            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1136 >        }
1137 >        assertNull(q.pollLast(0, MILLISECONDS));
1138      }
1139  
1140      /**
1141       * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1142       */
1143 <    public void testTimedPollLast() {
1144 <        try {
1145 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1146 <            for (int i = 0; i < SIZE; ++i) {
1147 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1148 <            }
1336 <            assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1337 <        } catch (InterruptedException e){
1338 <            unexpectedException();
1339 <        }  
1143 >    public void testTimedPollLast() throws InterruptedException {
1144 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1145 >        for (int i = 0; i < SIZE; ++i) {
1146 >            assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1147 >        }
1148 >        assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1149      }
1150  
1151      /**
1152       * Interrupted timed pollLast throws InterruptedException instead of
1153       * returning timeout status
1154       */
1155 <    public void testInterruptedTimedPollLast() {
1156 <        Thread t = new Thread(new Runnable() {
1157 <                public void run() {
1158 <                    try {
1159 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1160 <                        for (int i = 0; i < SIZE; ++i) {
1161 <                            threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1162 <                        }
1163 <                        threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1164 <                    } catch (InterruptedException success){
1165 <                    }  
1166 <                }});
1155 >    public void testInterruptedTimedPollLast() throws InterruptedException {
1156 >        Thread t = new Thread(new CheckedRunnable() {
1157 >            public void realRun() throws InterruptedException {
1158 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1159 >                for (int i = 0; i < SIZE; ++i) {
1160 >                    assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1161 >                }
1162 >                try {
1163 >                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1164 >                    shouldThrow();
1165 >                } catch (InterruptedException success) {}
1166 >            }});
1167 >
1168          t.start();
1169 <        try {
1170 <           Thread.sleep(SHORT_DELAY_MS);
1171 <           t.interrupt();
1362 <           t.join();
1363 <        }
1364 <        catch (InterruptedException ie) {
1365 <            unexpectedException();
1366 <        }
1169 >        Thread.sleep(SHORT_DELAY_MS);
1170 >        t.interrupt();
1171 >        t.join();
1172      }
1173  
1174      /**
1175 <     *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1176 <     *  on interruption throws
1175 >     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1176 >     * on interruption throws
1177       */
1178 <    public void testTimedPollWithOfferLast() {
1178 >    public void testTimedPollWithOfferLast() throws InterruptedException {
1179          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1180 <        Thread t = new Thread(new Runnable() {
1181 <                public void run() {
1182 <                    try {
1183 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1184 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1185 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1186 <                        threadShouldThrow();
1187 <                    } catch (InterruptedException success) { }                
1188 <                }
1189 <            });
1190 <        try {
1191 <            t.start();
1192 <            Thread.sleep(SMALL_DELAY_MS);
1193 <            assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1194 <            t.interrupt();
1195 <            t.join();
1391 <        } catch (Exception e){
1392 <            unexpectedException();
1393 <        }
1394 <    }  
1180 >        Thread t = new Thread(new CheckedRunnable() {
1181 >            public void realRun() throws InterruptedException {
1182 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1183 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1184 >                try {
1185 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1186 >                    shouldThrow();
1187 >                } catch (InterruptedException success) {}
1188 >            }});
1189 >
1190 >        t.start();
1191 >        Thread.sleep(SMALL_DELAY_MS);
1192 >        assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1193 >        t.interrupt();
1194 >        t.join();
1195 >    }
1196  
1197  
1198      /**
# Line 1400 | Line 1201 | public class LinkedBlockingDequeTest ext
1201      public void testElement() {
1202          LinkedBlockingDeque q = populatedDeque(SIZE);
1203          for (int i = 0; i < SIZE; ++i) {
1204 <            assertEquals(i, ((Integer)q.element()).intValue());
1204 >            assertEquals(i, q.element());
1205              q.poll();
1206          }
1207          try {
1208              q.element();
1209              shouldThrow();
1210 <        }
1410 <        catch (NoSuchElementException success) {}
1210 >        } catch (NoSuchElementException success) {}
1211      }
1212  
1213      /**
# Line 1416 | Line 1216 | public class LinkedBlockingDequeTest ext
1216      public void testRemoveElement() {
1217          LinkedBlockingDeque q = populatedDeque(SIZE);
1218          for (int i = 1; i < SIZE; i+=2) {
1219 <            assertTrue(q.remove(new Integer(i)));
1219 >            assertTrue(q.contains(i));
1220 >            assertTrue(q.remove(i));
1221 >            assertFalse(q.contains(i));
1222 >            assertTrue(q.contains(i-1));
1223          }
1224          for (int i = 0; i < SIZE; i+=2) {
1225 <            assertTrue(q.remove(new Integer(i)));
1226 <            assertFalse(q.remove(new Integer(i+1)));
1225 >            assertTrue(q.contains(i));
1226 >            assertTrue(q.remove(i));
1227 >            assertFalse(q.contains(i));
1228 >            assertFalse(q.remove(i+1));
1229 >            assertFalse(q.contains(i+1));
1230          }
1231          assertTrue(q.isEmpty());
1232      }
1233 <        
1233 >
1234      /**
1235       * contains(x) reports true when elements added but not yet removed
1236       */
# Line 1503 | Line 1309 | public class LinkedBlockingDequeTest ext
1309      }
1310  
1311      /**
1312 <     * toArray contains all elements
1312 >     * toArray contains all elements in FIFO order
1313       */
1314 <    public void testToArray() {
1314 >    public void testToArray() throws InterruptedException{
1315          LinkedBlockingDeque q = populatedDeque(SIZE);
1316 <        Object[] o = q.toArray();
1317 <        try {
1318 <        for(int i = 0; i < o.length; i++)
1513 <            assertEquals(o[i], q.take());
1514 <        } catch (InterruptedException e){
1515 <            unexpectedException();
1516 <        }    
1316 >        Object[] o = q.toArray();
1317 >        for (int i = 0; i < o.length; i++)
1318 >            assertSame(o[i], q.poll());
1319      }
1320  
1321      /**
1322 <     * toArray(a) contains all elements
1322 >     * toArray(a) contains all elements in FIFO order
1323       */
1324      public void testToArray2() {
1325 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1326 <        Integer[] ints = new Integer[SIZE];
1327 <        ints = (Integer[])q.toArray(ints);
1328 <        try {
1329 <            for(int i = 0; i < ints.length; i++)
1330 <                assertEquals(ints[i], q.take());
1529 <        } catch (InterruptedException e){
1530 <            unexpectedException();
1531 <        }    
1325 >        LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1326 >        Integer[] ints = new Integer[SIZE];
1327 >        Integer[] array = q.toArray(ints);
1328 >        assertSame(ints, array);
1329 >        for (int i = 0; i < ints.length; i++)
1330 >            assertSame(ints[i], q.remove());
1331      }
1332  
1333      /**
1334 <     * toArray(null) throws NPE
1334 >     * toArray(null) throws NullPointerException
1335       */
1336 <    public void testToArray_BadArg() {
1337 <        try {
1338 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1339 <            Object o[] = q.toArray(null);
1340 <            shouldThrow();
1341 <        } catch(NullPointerException success){}
1336 >    public void testToArray_NullArg() {
1337 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1338 >        try {
1339 >            q.toArray(null);
1340 >            shouldThrow();
1341 >        } catch (NullPointerException success) {}
1342      }
1343  
1344      /**
1345 <     * toArray with incompatible array type throws CCE
1345 >     * toArray(incompatible array type) throws ArrayStoreException
1346       */
1347      public void testToArray1_BadArg() {
1348 <        try {
1349 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1350 <            Object o[] = q.toArray(new String[10] );
1351 <            shouldThrow();
1352 <        } catch(ArrayStoreException  success){}
1348 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1349 >        try {
1350 >            q.toArray(new String[10]);
1351 >            shouldThrow();
1352 >        } catch (ArrayStoreException success) {}
1353      }
1354  
1355 <    
1355 >
1356      /**
1357       * iterator iterates through all elements
1358       */
1359 <    public void testIterator() {
1359 >    public void testIterator() throws InterruptedException {
1360          LinkedBlockingDeque q = populatedDeque(SIZE);
1361 <        Iterator it = q.iterator();
1362 <        try {
1363 <            while(it.hasNext()){
1364 <                assertEquals(it.next(), q.take());
1566 <            }
1567 <        } catch (InterruptedException e){
1568 <            unexpectedException();
1569 <        }    
1361 >        Iterator it = q.iterator();
1362 >        while (it.hasNext()) {
1363 >            assertEquals(it.next(), q.take());
1364 >        }
1365      }
1366  
1367      /**
1368       * iterator.remove removes current element
1369       */
1370 <    public void testIteratorRemove () {
1370 >    public void testIteratorRemove() {
1371          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1372          q.add(two);
1373          q.add(one);
# Line 1581 | Line 1376 | public class LinkedBlockingDequeTest ext
1376          Iterator it = q.iterator();
1377          it.next();
1378          it.remove();
1379 <        
1379 >
1380          it = q.iterator();
1381 <        assertEquals(it.next(), one);
1382 <        assertEquals(it.next(), three);
1381 >        assertSame(it.next(), one);
1382 >        assertSame(it.next(), three);
1383          assertFalse(it.hasNext());
1384      }
1385  
# Line 1600 | Line 1395 | public class LinkedBlockingDequeTest ext
1395          assertEquals(0, q.remainingCapacity());
1396          int k = 0;
1397          for (Iterator it = q.iterator(); it.hasNext();) {
1398 <            int i = ((Integer)(it.next())).intValue();
1604 <            assertEquals(++k, i);
1398 >            assertEquals(++k, it.next());
1399          }
1400          assertEquals(3, k);
1401      }
# Line 1609 | Line 1403 | public class LinkedBlockingDequeTest ext
1403      /**
1404       * Modifications do not cause iterators to fail
1405       */
1406 <    public void testWeaklyConsistentIteration () {
1406 >    public void testWeaklyConsistentIteration() {
1407          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1408          q.add(one);
1409          q.add(two);
1410          q.add(three);
1411 +        for (Iterator it = q.iterator(); it.hasNext();) {
1412 +            q.remove();
1413 +            it.next();
1414 +        }
1415 +        assertEquals(0, q.size());
1416 +    }
1417 +
1418 +
1419 +    /**
1420 +     * Descending iterator iterates through all elements
1421 +     */
1422 +    public void testDescendingIterator() {
1423 +        LinkedBlockingDeque q = populatedDeque(SIZE);
1424 +        int i = 0;
1425 +        Iterator it = q.descendingIterator();
1426 +        while (it.hasNext()) {
1427 +            assertTrue(q.contains(it.next()));
1428 +            ++i;
1429 +        }
1430 +        assertEquals(i, SIZE);
1431 +        assertFalse(it.hasNext());
1432          try {
1433 <            for (Iterator it = q.iterator(); it.hasNext();) {
1434 <                q.remove();
1435 <                it.next();
1433 >            it.next();
1434 >            shouldThrow();
1435 >        } catch (NoSuchElementException success) {}
1436 >    }
1437 >
1438 >    /**
1439 >     * Descending iterator ordering is reverse FIFO
1440 >     */
1441 >    public void testDescendingIteratorOrdering() {
1442 >        final LinkedBlockingDeque q = new LinkedBlockingDeque();
1443 >        for (int iters = 0; iters < 100; ++iters) {
1444 >            q.add(new Integer(3));
1445 >            q.add(new Integer(2));
1446 >            q.add(new Integer(1));
1447 >            int k = 0;
1448 >            for (Iterator it = q.descendingIterator(); it.hasNext();) {
1449 >                assertEquals(++k, it.next());
1450              }
1451 +
1452 +            assertEquals(3, k);
1453 +            q.remove();
1454 +            q.remove();
1455 +            q.remove();
1456          }
1457 <        catch (ConcurrentModificationException e) {
1458 <            unexpectedException();
1457 >    }
1458 >
1459 >    /**
1460 >     * descendingIterator.remove removes current element
1461 >     */
1462 >    public void testDescendingIteratorRemove() {
1463 >        final LinkedBlockingDeque q = new LinkedBlockingDeque();
1464 >        for (int iters = 0; iters < 100; ++iters) {
1465 >            q.add(new Integer(3));
1466 >            q.add(new Integer(2));
1467 >            q.add(new Integer(1));
1468 >            Iterator it = q.descendingIterator();
1469 >            assertEquals(it.next(), new Integer(1));
1470 >            it.remove();
1471 >            assertEquals(it.next(), new Integer(2));
1472 >            it = q.descendingIterator();
1473 >            assertEquals(it.next(), new Integer(2));
1474 >            assertEquals(it.next(), new Integer(3));
1475 >            it.remove();
1476 >            assertFalse(it.hasNext());
1477 >            q.remove();
1478          }
1626        assertEquals(0, q.size());
1479      }
1480  
1481  
# Line 1636 | Line 1488 | public class LinkedBlockingDequeTest ext
1488          for (int i = 0; i < SIZE; ++i) {
1489              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1490          }
1491 <    }        
1491 >    }
1492  
1493  
1494      /**
# Line 1647 | Line 1499 | public class LinkedBlockingDequeTest ext
1499          q.add(one);
1500          q.add(two);
1501          ExecutorService executor = Executors.newFixedThreadPool(2);
1502 <        executor.execute(new Runnable() {
1503 <            public void run() {
1504 <                threadAssertFalse(q.offer(three));
1505 <                try {
1506 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1507 <                    threadAssertEquals(0, q.remainingCapacity());
1508 <                }
1509 <                catch (InterruptedException e) {
1510 <                    threadUnexpectedException();
1511 <                }
1512 <            }
1513 <        });
1502 >        executor.execute(new CheckedRunnable() {
1503 >            public void realRun() throws InterruptedException {
1504 >                assertFalse(q.offer(three));
1505 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1506 >                assertEquals(0, q.remainingCapacity());
1507 >            }});
1508 >
1509 >        executor.execute(new CheckedRunnable() {
1510 >            public void realRun() throws InterruptedException {
1511 >                Thread.sleep(SMALL_DELAY_MS);
1512 >                assertSame(one, q.take());
1513 >            }});
1514  
1663        executor.execute(new Runnable() {
1664            public void run() {
1665                try {
1666                    Thread.sleep(SMALL_DELAY_MS);
1667                    threadAssertEquals(one, q.take());
1668                }
1669                catch (InterruptedException e) {
1670                    threadUnexpectedException();
1671                }
1672            }
1673        });
1674        
1515          joinPool(executor);
1516      }
1517  
# Line 1681 | Line 1521 | public class LinkedBlockingDequeTest ext
1521      public void testPollInExecutor() {
1522          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1523          ExecutorService executor = Executors.newFixedThreadPool(2);
1524 <        executor.execute(new Runnable() {
1525 <            public void run() {
1526 <                threadAssertNull(q.poll());
1527 <                try {
1528 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1529 <                    threadAssertTrue(q.isEmpty());
1530 <                }
1531 <                catch (InterruptedException e) {
1532 <                    threadUnexpectedException();
1533 <                }
1534 <            }
1535 <        });
1524 >        executor.execute(new CheckedRunnable() {
1525 >            public void realRun() throws InterruptedException {
1526 >                assertNull(q.poll());
1527 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1528 >                assertTrue(q.isEmpty());
1529 >            }});
1530 >
1531 >        executor.execute(new CheckedRunnable() {
1532 >            public void realRun() throws InterruptedException {
1533 >                Thread.sleep(SMALL_DELAY_MS);
1534 >                q.put(one);
1535 >            }});
1536  
1697        executor.execute(new Runnable() {
1698            public void run() {
1699                try {
1700                    Thread.sleep(SMALL_DELAY_MS);
1701                    q.put(one);
1702                }
1703                catch (InterruptedException e) {
1704                    threadUnexpectedException();
1705                }
1706            }
1707        });
1708        
1537          joinPool(executor);
1538      }
1539  
1540      /**
1541       * A deserialized serialized deque has same elements in same order
1542       */
1543 <    public void testSerialization() {
1543 >    public void testSerialization() throws Exception {
1544          LinkedBlockingDeque q = populatedDeque(SIZE);
1545  
1546 <        try {
1547 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1548 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1549 <            out.writeObject(q);
1550 <            out.close();
1551 <
1552 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1553 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1554 <            LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1555 <            assertEquals(q.size(), r.size());
1556 <            while (!q.isEmpty())
1729 <                assertEquals(q.remove(), r.remove());
1730 <        } catch(Exception e){
1731 <            unexpectedException();
1732 <        }
1546 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1547 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1548 >        out.writeObject(q);
1549 >        out.close();
1550 >
1551 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1552 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1553 >        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1554 >        assertEquals(q.size(), r.size());
1555 >        while (!q.isEmpty())
1556 >            assertEquals(q.remove(), r.remove());
1557      }
1558  
1559      /**
1560       * drainTo(null) throws NPE
1561 <     */
1561 >     */
1562      public void testDrainToNull() {
1563          LinkedBlockingDeque q = populatedDeque(SIZE);
1564          try {
1565              q.drainTo(null);
1566              shouldThrow();
1567 <        } catch(NullPointerException success) {
1744 <        }
1567 >        } catch (NullPointerException success) {}
1568      }
1569  
1570      /**
1571       * drainTo(this) throws IAE
1572 <     */
1572 >     */
1573      public void testDrainToSelf() {
1574          LinkedBlockingDeque q = populatedDeque(SIZE);
1575          try {
1576              q.drainTo(q);
1577              shouldThrow();
1578 <        } catch(IllegalArgumentException success) {
1756 <        }
1578 >        } catch (IllegalArgumentException success) {}
1579      }
1580  
1581      /**
1582       * drainTo(c) empties deque into another collection c
1583 <     */
1583 >     */
1584      public void testDrainTo() {
1585          LinkedBlockingDeque q = populatedDeque(SIZE);
1586          ArrayList l = new ArrayList();
1587          q.drainTo(l);
1588          assertEquals(q.size(), 0);
1589          assertEquals(l.size(), SIZE);
1590 <        for (int i = 0; i < SIZE; ++i)
1590 >        for (int i = 0; i < SIZE; ++i)
1591              assertEquals(l.get(i), new Integer(i));
1592          q.add(zero);
1593          q.add(one);
# Line 1776 | Line 1598 | public class LinkedBlockingDequeTest ext
1598          q.drainTo(l);
1599          assertEquals(q.size(), 0);
1600          assertEquals(l.size(), 2);
1601 <        for (int i = 0; i < 2; ++i)
1601 >        for (int i = 0; i < 2; ++i)
1602              assertEquals(l.get(i), new Integer(i));
1603      }
1604  
1605      /**
1606       * drainTo empties full deque, unblocking a waiting put.
1607 <     */
1608 <    public void testDrainToWithActivePut() {
1607 >     */
1608 >    public void testDrainToWithActivePut() throws InterruptedException {
1609          final LinkedBlockingDeque q = populatedDeque(SIZE);
1610 <        Thread t = new Thread(new Runnable() {
1611 <                public void run() {
1612 <                    try {
1613 <                        q.put(new Integer(SIZE+1));
1614 <                    } catch (InterruptedException ie){
1615 <                        threadUnexpectedException();
1616 <                    }
1617 <                }
1618 <            });
1619 <        try {
1620 <            t.start();
1621 <            ArrayList l = new ArrayList();
1622 <            q.drainTo(l);
1801 <            assertTrue(l.size() >= SIZE);
1802 <            for (int i = 0; i < SIZE; ++i)
1803 <                assertEquals(l.get(i), new Integer(i));
1804 <            t.join();
1805 <            assertTrue(q.size() + l.size() >= SIZE);
1806 <        } catch(Exception e){
1807 <            unexpectedException();
1808 <        }
1610 >        Thread t = new Thread(new CheckedRunnable() {
1611 >            public void realRun() throws InterruptedException {
1612 >                q.put(new Integer(SIZE+1));
1613 >            }});
1614 >
1615 >        t.start();
1616 >        ArrayList l = new ArrayList();
1617 >        q.drainTo(l);
1618 >        assertTrue(l.size() >= SIZE);
1619 >        for (int i = 0; i < SIZE; ++i)
1620 >            assertEquals(l.get(i), new Integer(i));
1621 >        t.join();
1622 >        assertTrue(q.size() + l.size() >= SIZE);
1623      }
1624  
1625      /**
1626       * drainTo(null, n) throws NPE
1627 <     */
1627 >     */
1628      public void testDrainToNullN() {
1629          LinkedBlockingDeque q = populatedDeque(SIZE);
1630          try {
1631              q.drainTo(null, 0);
1632              shouldThrow();
1633 <        } catch(NullPointerException success) {
1820 <        }
1633 >        } catch (NullPointerException success) {}
1634      }
1635  
1636      /**
1637       * drainTo(this, n) throws IAE
1638 <     */
1638 >     */
1639      public void testDrainToSelfN() {
1640          LinkedBlockingDeque q = populatedDeque(SIZE);
1641          try {
1642              q.drainTo(q, 0);
1643              shouldThrow();
1644 <        } catch(IllegalArgumentException success) {
1832 <        }
1644 >        } catch (IllegalArgumentException success) {}
1645      }
1646  
1647      /**
1648 <     * drainTo(c, n) empties first max {n, size} elements of deque into c
1649 <     */
1648 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
1649 >     */
1650      public void testDrainToN() {
1651          LinkedBlockingDeque q = new LinkedBlockingDeque();
1652          for (int i = 0; i < SIZE + 2; ++i) {
1653 <            for(int j = 0; j < SIZE; j++)
1653 >            for (int j = 0; j < SIZE; j++)
1654                  assertTrue(q.offer(new Integer(j)));
1655              ArrayList l = new ArrayList();
1656              q.drainTo(l, i);
1657 <            int k = (i < SIZE)? i : SIZE;
1657 >            int k = (i < SIZE) ? i : SIZE;
1658              assertEquals(l.size(), k);
1659              assertEquals(q.size(), SIZE-k);
1660 <            for (int j = 0; j < k; ++j)
1660 >            for (int j = 0; j < k; ++j)
1661                  assertEquals(l.get(j), new Integer(j));
1662              while (q.poll() != null) ;
1663          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines