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.5 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.38 by jsr166, Sat May 21 06:24:33 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      /**
# 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 >     * 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
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());
287 >        q.addFirst(four);
288 >        assertSame(four, q.peekFirst());
289      }
290  
291      /**
# 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());
297 >        q.addLast(four);
298 >        assertSame(four, q.peekLast());
299      }
300  
301  
# 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());
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 >        delay(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 >        delay(SHORT_DELAY_MS);
645 >        assertEquals(q.remainingCapacity(), 0);
646 >        assertEquals(0, q.take());
647 >        delay(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 <                }
668 <            });
669 <
670 <        try {
671 <            t.start();
672 <            Thread.sleep(SMALL_DELAY_MS);
673 <            t.interrupt();
674 <            t.join();
675 <        } catch (Exception e){
683 <            unexpectedException();
684 <        }
658 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
659 >        Thread t = newStartedThread(new CheckedRunnable() {
660 >            public void realRun() throws InterruptedException {
661 >                q.put(new Object());
662 >                q.put(new Object());
663 >                long startTime = System.nanoTime();
664 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
665 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
666 >                pleaseInterrupt.countDown();
667 >                try {
668 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
669 >                    shouldThrow();
670 >                } catch (InterruptedException success) {}
671 >            }});
672 >
673 >        await(pleaseInterrupt);
674 >        t.interrupt();
675 >        awaitTermination(t);
676      }
677  
678      /**
679       * take retrieves elements in FIFO order
680       */
681 <    public void testTake() {
682 <        try {
683 <            LinkedBlockingDeque q = populatedDeque(SIZE);
684 <            for (int i = 0; i < SIZE; ++i) {
694 <                assertEquals(i, ((Integer)q.take()).intValue());
695 <            }
696 <        } catch (InterruptedException e){
697 <            unexpectedException();
698 <        }
699 <    }
700 <
701 <    /**
702 <     * take blocks interruptibly when empty
703 <     */
704 <    public void testTakeFromEmpty() {
705 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
706 <        Thread t = new Thread(new Runnable() {
707 <                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();
681 >    public void testTake() throws InterruptedException {
682 >        LinkedBlockingDeque q = populatedDeque(SIZE);
683 >        for (int i = 0; i < SIZE; ++i) {
684 >            assertEquals(i, q.take());
685          }
686      }
687  
688      /**
689       * Take removes existing elements until empty, then blocks interruptibly
690       */
691 <    public void testBlockingTake() {
692 <        Thread t = new Thread(new Runnable() {
693 <                public void run() {
694 <                    try {
695 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
696 <                        for (int i = 0; i < SIZE; ++i) {
697 <                            assertEquals(i, ((Integer)q.take()).intValue());
698 <                        }
699 <                        q.take();
700 <                        threadShouldThrow();
701 <                    } catch (InterruptedException success){
702 <                    }
703 <                }});
691 >    public void testBlockingTake() throws InterruptedException {
692 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
693 >        Thread t = new Thread(new CheckedRunnable() {
694 >            public void realRun() throws InterruptedException {
695 >                for (int i = 0; i < SIZE; ++i) {
696 >                    assertEquals(i, q.take());
697 >                }
698 >                try {
699 >                    q.take();
700 >                    shouldThrow();
701 >                } catch (InterruptedException success) {}
702 >            }});
703 >
704          t.start();
705 <        try {
706 <           Thread.sleep(SHORT_DELAY_MS);
707 <           t.interrupt();
744 <           t.join();
745 <        }
746 <        catch (InterruptedException ie) {
747 <            unexpectedException();
748 <        }
705 >        delay(SHORT_DELAY_MS);
706 >        t.interrupt();
707 >        t.join();
708      }
709  
710  
# Line 755 | Line 714 | public class LinkedBlockingDequeTest ext
714      public void testPoll() {
715          LinkedBlockingDeque q = populatedDeque(SIZE);
716          for (int i = 0; i < SIZE; ++i) {
717 <            assertEquals(i, ((Integer)q.poll()).intValue());
717 >            assertEquals(i, q.poll());
718          }
719 <        assertNull(q.poll());
719 >        assertNull(q.poll());
720      }
721  
722      /**
723       * timed poll with zero timeout succeeds when non-empty, else times out
724       */
725 <    public void testTimedPoll0() {
726 <        try {
727 <            LinkedBlockingDeque q = populatedDeque(SIZE);
728 <            for (int i = 0; i < SIZE; ++i) {
729 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
730 <            }
772 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
773 <        } catch (InterruptedException e){
774 <            unexpectedException();
775 <        }
725 >    public void testTimedPoll0() throws InterruptedException {
726 >        LinkedBlockingDeque q = populatedDeque(SIZE);
727 >        for (int i = 0; i < SIZE; ++i) {
728 >            assertEquals(i, q.poll(0, MILLISECONDS));
729 >        }
730 >        assertNull(q.poll(0, MILLISECONDS));
731      }
732  
733      /**
734       * timed poll with nonzero timeout succeeds when non-empty, else times out
735       */
736 <    public void testTimedPoll() {
737 <        try {
738 <            LinkedBlockingDeque q = populatedDeque(SIZE);
739 <            for (int i = 0; i < SIZE; ++i) {
740 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
741 <            }
787 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
788 <        } catch (InterruptedException e){
789 <            unexpectedException();
790 <        }
736 >    public void testTimedPoll() throws InterruptedException {
737 >        LinkedBlockingDeque q = populatedDeque(SIZE);
738 >        for (int i = 0; i < SIZE; ++i) {
739 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
740 >        }
741 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
742      }
743  
744      /**
745       * Interrupted timed poll throws InterruptedException instead of
746       * returning timeout status
747       */
748 <    public void testInterruptedTimedPoll() {
749 <        Thread t = new Thread(new Runnable() {
750 <                public void run() {
751 <                    try {
752 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
753 <                        for (int i = 0; i < SIZE; ++i) {
754 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
755 <                        }
756 <                        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) { }
748 >    public void testInterruptedTimedPoll() throws InterruptedException {
749 >        final BlockingQueue<Integer> q = populatedDeque(SIZE);
750 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
751 >        Thread t = newStartedThread(new CheckedRunnable() {
752 >            public void realRun() throws InterruptedException {
753 >                for (int i = 0; i < SIZE; ++i) {
754 >                    long t0 = System.nanoTime();
755 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
756 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
757                  }
758 <            });
759 <        try {
760 <            t.start();
761 <            Thread.sleep(SMALL_DELAY_MS);
762 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
763 <            t.interrupt();
764 <            t.join();
765 <        } catch (Exception e){
766 <            unexpectedException();
767 <        }
758 >                long t0 = System.nanoTime();
759 >                aboutToWait.countDown();
760 >                try {
761 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
762 >                    shouldThrow();
763 >                } catch (InterruptedException success) {
764 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
765 >                }
766 >            }});
767 >
768 >        aboutToWait.await();
769 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
770 >        t.interrupt();
771 >        awaitTermination(t, MEDIUM_DELAY_MS);
772 >        checkEmpty(q);
773      }
774  
847
775      /**
776       * putFirst(null) throws NPE
777       */
778 <     public void testPutFirstNull() {
779 <        try {
778 >    public void testPutFirstNull() throws InterruptedException {
779 >        try {
780              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
781              q.putFirst(null);
782              shouldThrow();
783 <        }
784 <        catch (NullPointerException success){
858 <        }
859 <        catch (InterruptedException ie) {
860 <            unexpectedException();
861 <        }
862 <     }
783 >        } catch (NullPointerException success) {}
784 >    }
785  
786      /**
787       * all elements successfully putFirst are contained
788       */
789 <     public void testPutFirst() {
790 <         try {
791 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
792 <             for (int i = 0; i < SIZE; ++i) {
793 <                 Integer I = new Integer(i);
794 <                 q.putFirst(I);
873 <                 assertTrue(q.contains(I));
874 <             }
875 <             assertEquals(0, q.remainingCapacity());
876 <         }
877 <        catch (InterruptedException ie) {
878 <            unexpectedException();
789 >    public void testPutFirst() throws InterruptedException {
790 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
791 >        for (int i = 0; i < SIZE; ++i) {
792 >            Integer I = new Integer(i);
793 >            q.putFirst(I);
794 >            assertTrue(q.contains(I));
795          }
796 +        assertEquals(0, q.remainingCapacity());
797      }
798  
799      /**
800       * putFirst blocks interruptibly if full
801       */
802 <    public void testBlockingPutFirst() {
803 <        Thread t = new Thread(new Runnable() {
804 <                public void run() {
805 <                    int added = 0;
806 <                    try {
807 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
808 <                        for (int i = 0; i < SIZE; ++i) {
809 <                            q.putFirst(new Integer(i));
810 <                            ++added;
811 <                        }
812 <                        q.putFirst(new Integer(SIZE));
813 <                        threadShouldThrow();
814 <                    } catch (InterruptedException ie){
815 <                        threadAssertEquals(added, SIZE);
899 <                    }
900 <                }});
802 >    public void testBlockingPutFirst() throws InterruptedException {
803 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
804 >        Thread t = new Thread(new CheckedRunnable() {
805 >            public void realRun() throws InterruptedException {
806 >                for (int i = 0; i < SIZE; ++i)
807 >                    q.putFirst(i);
808 >                assertEquals(SIZE, q.size());
809 >                assertEquals(0, q.remainingCapacity());
810 >                try {
811 >                    q.putFirst(99);
812 >                    shouldThrow();
813 >                } catch (InterruptedException success) {}
814 >            }});
815 >
816          t.start();
817 <        try {
818 <           Thread.sleep(SHORT_DELAY_MS);
819 <           t.interrupt();
820 <           t.join();
821 <        }
907 <        catch (InterruptedException ie) {
908 <            unexpectedException();
909 <        }
817 >        delay(SHORT_DELAY_MS);
818 >        t.interrupt();
819 >        t.join();
820 >        assertEquals(SIZE, q.size());
821 >        assertEquals(0, q.remainingCapacity());
822      }
823  
824      /**
825       * putFirst blocks waiting for take when full
826       */
827 <    public void testPutFirstWithTake() {
828 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
829 <        Thread t = new Thread(new Runnable() {
830 <                public void run() {
831 <                    int added = 0;
832 <                    try {
833 <                        q.putFirst(new Object());
834 <                        ++added;
835 <                        q.putFirst(new Object());
836 <                        ++added;
837 <                        q.putFirst(new Object());
838 <                        ++added;
839 <                        q.putFirst(new Object());
840 <                        ++added;
841 <                        threadShouldThrow();
842 <                    } catch (InterruptedException e){
843 <                        threadAssertTrue(added >= 2);
844 <                    }
845 <                }
846 <            });
847 <        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 <        }
827 >    public void testPutFirstWithTake() throws InterruptedException {
828 >        final int capacity = 2;
829 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
830 >        Thread t = new Thread(new CheckedRunnable() {
831 >            public void realRun() throws InterruptedException {
832 >                for (int i = 0; i < capacity + 1; i++)
833 >                    q.putFirst(i);
834 >                try {
835 >                    q.putFirst(99);
836 >                    shouldThrow();
837 >                } catch (InterruptedException success) {}
838 >            }});
839 >
840 >        t.start();
841 >        delay(SHORT_DELAY_MS);
842 >        assertEquals(q.remainingCapacity(), 0);
843 >        assertEquals(capacity - 1, q.take());
844 >        delay(SHORT_DELAY_MS);
845 >        t.interrupt();
846 >        t.join();
847 >        assertEquals(q.remainingCapacity(), 0);
848      }
849  
850      /**
851       * timed offerFirst times out if full and elements not taken
852       */
853 <    public void testTimedOfferFirst() {
853 >    public void testTimedOfferFirst() throws InterruptedException {
854          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
855 <        Thread t = new Thread(new Runnable() {
856 <                public void run() {
857 <                    try {
858 <                        q.putFirst(new Object());
859 <                        q.putFirst(new Object());
860 <                        threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
861 <                        q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
862 <                        threadShouldThrow();
863 <                    } catch (InterruptedException success){}
864 <                }
865 <            });
866 <
867 <        try {
868 <            t.start();
869 <            Thread.sleep(SMALL_DELAY_MS);
870 <            t.interrupt();
871 <            t.join();
872 <        } catch (Exception e){
969 <            unexpectedException();
970 <        }
855 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
856 >        Thread t = newStartedThread(new CheckedRunnable() {
857 >            public void realRun() throws InterruptedException {
858 >                q.putFirst(new Object());
859 >                q.putFirst(new Object());
860 >                long startTime = System.nanoTime();
861 >                assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
862 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
863 >                pleaseInterrupt.countDown();
864 >                try {
865 >                    q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
866 >                    shouldThrow();
867 >                } catch (InterruptedException success) {}
868 >            }});
869 >
870 >        await(pleaseInterrupt);
871 >        t.interrupt();
872 >        awaitTermination(t);
873      }
874  
875      /**
876       * take retrieves elements in FIFO order
877       */
878 <    public void testTakeFirst() {
879 <        try {
880 <            LinkedBlockingDeque q = populatedDeque(SIZE);
881 <            for (int i = 0; i < SIZE; ++i) {
882 <                assertEquals(i, ((Integer)q.takeFirst()).intValue());
981 <            }
982 <        } catch (InterruptedException e){
983 <            unexpectedException();
984 <        }
878 >    public void testTakeFirst() throws InterruptedException {
879 >        LinkedBlockingDeque q = populatedDeque(SIZE);
880 >        for (int i = 0; i < SIZE; ++i) {
881 >            assertEquals(i, q.takeFirst());
882 >        }
883      }
884  
885      /**
886       * takeFirst blocks interruptibly when empty
887       */
888 <    public void testTakeFirstFromEmpty() {
888 >    public void testTakeFirstFromEmpty() throws InterruptedException {
889          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
890 <        Thread t = new Thread(new Runnable() {
891 <                public void run() {
892 <                    try {
893 <                        q.takeFirst();
894 <                        threadShouldThrow();
895 <                    } catch (InterruptedException success){ }
896 <                }
897 <            });
898 <        try {
1001 <            t.start();
1002 <            Thread.sleep(SHORT_DELAY_MS);
1003 <            t.interrupt();
1004 <            t.join();
1005 <        } catch (Exception e){
1006 <            unexpectedException();
1007 <        }
890 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
891 >            public void realRun() throws InterruptedException {
892 >                q.takeFirst();
893 >            }};
894 >
895 >        t.start();
896 >        delay(SHORT_DELAY_MS);
897 >        t.interrupt();
898 >        t.join();
899      }
900  
901      /**
902       * TakeFirst removes existing elements until empty, then blocks interruptibly
903       */
904 <    public void testBlockingTakeFirst() {
905 <        Thread t = new Thread(new Runnable() {
906 <                public void run() {
907 <                    try {
908 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
909 <                        for (int i = 0; i < SIZE; ++i) {
910 <                            assertEquals(i, ((Integer)q.takeFirst()).intValue());
911 <                        }
912 <                        q.takeFirst();
913 <                        threadShouldThrow();
914 <                    } catch (InterruptedException success){
915 <                    }
1025 <                }});
904 >    public void testBlockingTakeFirst() throws InterruptedException {
905 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
906 >        Thread t = new Thread(new CheckedRunnable() {
907 >            public void realRun() throws InterruptedException {
908 >                for (int i = 0; i < SIZE; ++i)
909 >                    assertEquals(i, q.takeFirst());
910 >                try {
911 >                    q.takeFirst();
912 >                    shouldThrow();
913 >                } catch (InterruptedException success) {}
914 >            }});
915 >
916          t.start();
917 <        try {
918 <           Thread.sleep(SHORT_DELAY_MS);
919 <           t.interrupt();
1030 <           t.join();
1031 <        }
1032 <        catch (InterruptedException ie) {
1033 <            unexpectedException();
1034 <        }
917 >        delay(SHORT_DELAY_MS);
918 >        t.interrupt();
919 >        t.join();
920      }
921  
922  
923      /**
924       * timed pollFirst with zero timeout succeeds when non-empty, else times out
925       */
926 <    public void testTimedPollFirst0() {
927 <        try {
928 <            LinkedBlockingDeque q = populatedDeque(SIZE);
929 <            for (int i = 0; i < SIZE; ++i) {
930 <                assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue());
931 <            }
1047 <            assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS));
1048 <        } catch (InterruptedException e){
1049 <            unexpectedException();
1050 <        }
926 >    public void testTimedPollFirst0() throws InterruptedException {
927 >        LinkedBlockingDeque q = populatedDeque(SIZE);
928 >        for (int i = 0; i < SIZE; ++i) {
929 >            assertEquals(i, q.pollFirst(0, MILLISECONDS));
930 >        }
931 >        assertNull(q.pollFirst(0, MILLISECONDS));
932      }
933  
934      /**
935       * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
936       */
937 <    public void testTimedPollFirst() {
938 <        try {
939 <            LinkedBlockingDeque q = populatedDeque(SIZE);
940 <            for (int i = 0; i < SIZE; ++i) {
941 <                assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
942 <            }
1062 <            assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1063 <        } catch (InterruptedException e){
1064 <            unexpectedException();
1065 <        }
937 >    public void testTimedPollFirst() throws InterruptedException {
938 >        LinkedBlockingDeque q = populatedDeque(SIZE);
939 >        for (int i = 0; i < SIZE; ++i) {
940 >            assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
941 >        }
942 >        assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
943      }
944  
945      /**
946       * Interrupted timed pollFirst throws InterruptedException instead of
947       * returning timeout status
948       */
949 <    public void testInterruptedTimedPollFirst() {
950 <        Thread t = new Thread(new Runnable() {
951 <                public void run() {
952 <                    try {
953 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
954 <                        for (int i = 0; i < SIZE; ++i) {
955 <                            threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
956 <                        }
957 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
958 <                    } catch (InterruptedException success){
959 <                    }
960 <                }});
949 >    public void testInterruptedTimedPollFirst() throws InterruptedException {
950 >        Thread t = new Thread(new CheckedRunnable() {
951 >            public void realRun() throws InterruptedException {
952 >                LinkedBlockingDeque q = populatedDeque(SIZE);
953 >                for (int i = 0; i < SIZE; ++i) {
954 >                    assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
955 >                }
956 >                try {
957 >                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
958 >                    shouldThrow();
959 >                } catch (InterruptedException success) {}
960 >            }});
961 >
962          t.start();
963 <        try {
964 <           Thread.sleep(SHORT_DELAY_MS);
965 <           t.interrupt();
1088 <           t.join();
1089 <        }
1090 <        catch (InterruptedException ie) {
1091 <            unexpectedException();
1092 <        }
963 >        delay(SHORT_DELAY_MS);
964 >        t.interrupt();
965 >        t.join();
966      }
967  
968      /**
969 <     *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
970 <     *  on interruption throws
969 >     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
970 >     * on interruption throws
971       */
972 <    public void testTimedPollFirstWithOfferFirst() {
972 >    public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
973          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
974 <        Thread t = new Thread(new Runnable() {
975 <                public void run() {
976 <                    try {
977 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
978 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
979 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
980 <                        threadShouldThrow();
981 <                    } catch (InterruptedException success) { }
982 <                }
983 <            });
984 <        try {
985 <            t.start();
986 <            Thread.sleep(SMALL_DELAY_MS);
987 <            assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
988 <            t.interrupt();
1116 <            t.join();
1117 <        } catch (Exception e){
1118 <            unexpectedException();
1119 <        }
974 >        Thread t = new Thread(new CheckedRunnable() {
975 >            public void realRun() throws InterruptedException {
976 >                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
977 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
978 >                try {
979 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
980 >                    shouldThrow();
981 >                } catch (InterruptedException success) {}
982 >            }});
983 >
984 >        t.start();
985 >        delay(SMALL_DELAY_MS);
986 >        assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
987 >        t.interrupt();
988 >        t.join();
989      }
990  
991      /**
992       * putLast(null) throws NPE
993       */
994 <     public void testPutLastNull() {
995 <        try {
994 >    public void testPutLastNull() throws InterruptedException {
995 >        try {
996              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
997              q.putLast(null);
998              shouldThrow();
999 <        }
1000 <        catch (NullPointerException success){
1132 <        }
1133 <        catch (InterruptedException ie) {
1134 <            unexpectedException();
1135 <        }
1136 <     }
999 >        } catch (NullPointerException success) {}
1000 >    }
1001  
1002      /**
1003       * all elements successfully putLast are contained
1004       */
1005 <     public void testPutLast() {
1006 <         try {
1007 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1008 <             for (int i = 0; i < SIZE; ++i) {
1009 <                 Integer I = new Integer(i);
1010 <                 q.putLast(I);
1147 <                 assertTrue(q.contains(I));
1148 <             }
1149 <             assertEquals(0, q.remainingCapacity());
1150 <         }
1151 <        catch (InterruptedException ie) {
1152 <            unexpectedException();
1005 >    public void testPutLast() throws InterruptedException {
1006 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1007 >        for (int i = 0; i < SIZE; ++i) {
1008 >            Integer I = new Integer(i);
1009 >            q.putLast(I);
1010 >            assertTrue(q.contains(I));
1011          }
1012 +        assertEquals(0, q.remainingCapacity());
1013      }
1014  
1015      /**
1016       * putLast blocks interruptibly if full
1017       */
1018 <    public void testBlockingPutLast() {
1019 <        Thread t = new Thread(new Runnable() {
1020 <                public void run() {
1021 <                    int added = 0;
1022 <                    try {
1023 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1024 <                        for (int i = 0; i < SIZE; ++i) {
1025 <                            q.putLast(new Integer(i));
1026 <                            ++added;
1027 <                        }
1028 <                        q.putLast(new Integer(SIZE));
1029 <                        threadShouldThrow();
1030 <                    } catch (InterruptedException ie){
1031 <                        threadAssertEquals(added, SIZE);
1173 <                    }
1174 <                }});
1018 >    public void testBlockingPutLast() throws InterruptedException {
1019 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1020 >        Thread t = new Thread(new CheckedRunnable() {
1021 >            public void realRun() throws InterruptedException {
1022 >                for (int i = 0; i < SIZE; ++i)
1023 >                    q.putLast(i);
1024 >                assertEquals(SIZE, q.size());
1025 >                assertEquals(0, q.remainingCapacity());
1026 >                try {
1027 >                    q.putLast(99);
1028 >                    shouldThrow();
1029 >                } catch (InterruptedException success) {}
1030 >            }});
1031 >
1032          t.start();
1033 <        try {
1034 <           Thread.sleep(SHORT_DELAY_MS);
1035 <           t.interrupt();
1036 <           t.join();
1037 <        }
1181 <        catch (InterruptedException ie) {
1182 <            unexpectedException();
1183 <        }
1033 >        delay(SHORT_DELAY_MS);
1034 >        t.interrupt();
1035 >        t.join();
1036 >        assertEquals(SIZE, q.size());
1037 >        assertEquals(0, q.remainingCapacity());
1038      }
1039  
1040      /**
1041       * putLast blocks waiting for take when full
1042       */
1043 <    public void testPutLastWithTake() {
1044 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1045 <        Thread t = new Thread(new Runnable() {
1046 <                public void run() {
1047 <                    int added = 0;
1048 <                    try {
1049 <                        q.putLast(new Object());
1050 <                        ++added;
1051 <                        q.putLast(new Object());
1052 <                        ++added;
1053 <                        q.putLast(new Object());
1054 <                        ++added;
1055 <                        q.putLast(new Object());
1056 <                        ++added;
1057 <                        threadShouldThrow();
1058 <                    } catch (InterruptedException e){
1059 <                        threadAssertTrue(added >= 2);
1060 <                    }
1061 <                }
1062 <            });
1063 <        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 <        }
1043 >    public void testPutLastWithTake() throws InterruptedException {
1044 >        final int capacity = 2;
1045 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1046 >        Thread t = new Thread(new CheckedRunnable() {
1047 >            public void realRun() throws InterruptedException {
1048 >                for (int i = 0; i < capacity + 1; i++)
1049 >                    q.putLast(i);
1050 >                try {
1051 >                    q.putLast(99);
1052 >                    shouldThrow();
1053 >                } catch (InterruptedException success) {}
1054 >            }});
1055 >
1056 >        t.start();
1057 >        delay(SHORT_DELAY_MS);
1058 >        assertEquals(q.remainingCapacity(), 0);
1059 >        assertEquals(0, q.take());
1060 >        delay(SHORT_DELAY_MS);
1061 >        t.interrupt();
1062 >        t.join();
1063 >        assertEquals(q.remainingCapacity(), 0);
1064      }
1065  
1066      /**
1067       * timed offerLast times out if full and elements not taken
1068       */
1069 <    public void testTimedOfferLast() {
1069 >    public void testTimedOfferLast() throws InterruptedException {
1070          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1071 <        Thread t = new Thread(new Runnable() {
1072 <                public void run() {
1073 <                    try {
1074 <                        q.putLast(new Object());
1075 <                        q.putLast(new Object());
1076 <                        threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1077 <                        q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1078 <                        threadShouldThrow();
1079 <                    } catch (InterruptedException success){}
1080 <                }
1081 <            });
1082 <
1083 <        try {
1084 <            t.start();
1085 <            Thread.sleep(SMALL_DELAY_MS);
1086 <            t.interrupt();
1087 <            t.join();
1088 <        } catch (Exception e){
1243 <            unexpectedException();
1244 <        }
1071 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1072 >        Thread t = newStartedThread(new CheckedRunnable() {
1073 >            public void realRun() throws InterruptedException {
1074 >                q.putLast(new Object());
1075 >                q.putLast(new Object());
1076 >                long startTime = System.nanoTime();
1077 >                assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1078 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1079 >                pleaseInterrupt.countDown();
1080 >                try {
1081 >                    q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1082 >                    shouldThrow();
1083 >                } catch (InterruptedException success) {}
1084 >            }});
1085 >
1086 >        await(pleaseInterrupt);
1087 >        t.interrupt();
1088 >        awaitTermination(t);
1089      }
1090  
1091      /**
1092       * takeLast retrieves elements in FIFO order
1093       */
1094 <    public void testTakeLast() {
1095 <        try {
1096 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1097 <            for (int i = 0; i < SIZE; ++i) {
1098 <                assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1255 <            }
1256 <        } catch (InterruptedException e){
1257 <            unexpectedException();
1258 <        }
1094 >    public void testTakeLast() throws InterruptedException {
1095 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1096 >        for (int i = 0; i < SIZE; ++i) {
1097 >            assertEquals(SIZE-i-1, q.takeLast());
1098 >        }
1099      }
1100  
1101      /**
1102       * takeLast blocks interruptibly when empty
1103       */
1104 <    public void testTakeLastFromEmpty() {
1104 >    public void testTakeLastFromEmpty() throws InterruptedException {
1105          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1106 <        Thread t = new Thread(new Runnable() {
1107 <                public void run() {
1108 <                    try {
1109 <                        q.takeLast();
1110 <                        threadShouldThrow();
1111 <                    } catch (InterruptedException success){ }
1112 <                }
1113 <            });
1114 <        try {
1275 <            t.start();
1276 <            Thread.sleep(SHORT_DELAY_MS);
1277 <            t.interrupt();
1278 <            t.join();
1279 <        } catch (Exception e){
1280 <            unexpectedException();
1281 <        }
1106 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1107 >            public void realRun() throws InterruptedException {
1108 >                q.takeLast();
1109 >            }};
1110 >
1111 >        t.start();
1112 >        delay(SHORT_DELAY_MS);
1113 >        t.interrupt();
1114 >        t.join();
1115      }
1116  
1117      /**
1118       * TakeLast removes existing elements until empty, then blocks interruptibly
1119       */
1120 <    public void testBlockingTakeLast() {
1121 <        Thread t = new Thread(new Runnable() {
1122 <                public void run() {
1123 <                    try {
1124 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1125 <                        for (int i = 0; i < SIZE; ++i) {
1126 <                            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1127 <                        }
1128 <                        q.takeLast();
1129 <                        threadShouldThrow();
1130 <                    } catch (InterruptedException success){
1131 <                    }
1299 <                }});
1120 >    public void testBlockingTakeLast() throws InterruptedException {
1121 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1122 >        Thread t = new Thread(new CheckedRunnable() {
1123 >            public void realRun() throws InterruptedException {
1124 >                for (int i = 0; i < SIZE; ++i)
1125 >                    assertEquals(SIZE - 1 - i, q.takeLast());
1126 >                try {
1127 >                    q.takeLast();
1128 >                    shouldThrow();
1129 >                } catch (InterruptedException success) {}
1130 >            }});
1131 >
1132          t.start();
1133 <        try {
1134 <           Thread.sleep(SHORT_DELAY_MS);
1135 <           t.interrupt();
1304 <           t.join();
1305 <        }
1306 <        catch (InterruptedException ie) {
1307 <            unexpectedException();
1308 <        }
1133 >        delay(SHORT_DELAY_MS);
1134 >        t.interrupt();
1135 >        t.join();
1136      }
1137  
1311
1138      /**
1139       * timed pollLast with zero timeout succeeds when non-empty, else times out
1140       */
1141 <    public void testTimedPollLast0() {
1142 <        try {
1143 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1144 <            for (int i = 0; i < SIZE; ++i) {
1145 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue());
1146 <            }
1321 <            assertNull(q.pollLast(0, TimeUnit.MILLISECONDS));
1322 <        } catch (InterruptedException e){
1323 <            unexpectedException();
1324 <        }
1141 >    public void testTimedPollLast0() throws InterruptedException {
1142 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1143 >        for (int i = 0; i < SIZE; ++i) {
1144 >            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1145 >        }
1146 >        assertNull(q.pollLast(0, MILLISECONDS));
1147      }
1148  
1149      /**
1150       * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1151       */
1152 <    public void testTimedPollLast() {
1153 <        try {
1154 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1155 <            for (int i = 0; i < SIZE; ++i) {
1156 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1157 <            }
1336 <            assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1337 <        } catch (InterruptedException e){
1338 <            unexpectedException();
1339 <        }
1152 >    public void testTimedPollLast() throws InterruptedException {
1153 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1154 >        for (int i = 0; i < SIZE; ++i) {
1155 >            assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1156 >        }
1157 >        assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1158      }
1159  
1160      /**
1161       * Interrupted timed pollLast throws InterruptedException instead of
1162       * returning timeout status
1163       */
1164 <    public void testInterruptedTimedPollLast() {
1165 <        Thread t = new Thread(new Runnable() {
1166 <                public void run() {
1167 <                    try {
1168 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1169 <                        for (int i = 0; i < SIZE; ++i) {
1170 <                            threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1171 <                        }
1172 <                        threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1173 <                    } catch (InterruptedException success){
1174 <                    }
1175 <                }});
1164 >    public void testInterruptedTimedPollLast() throws InterruptedException {
1165 >        Thread t = new Thread(new CheckedRunnable() {
1166 >            public void realRun() throws InterruptedException {
1167 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1168 >                for (int i = 0; i < SIZE; ++i) {
1169 >                    assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1170 >                }
1171 >                try {
1172 >                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1173 >                    shouldThrow();
1174 >                } catch (InterruptedException success) {}
1175 >            }});
1176 >
1177          t.start();
1178 <        try {
1179 <           Thread.sleep(SHORT_DELAY_MS);
1180 <           t.interrupt();
1362 <           t.join();
1363 <        }
1364 <        catch (InterruptedException ie) {
1365 <            unexpectedException();
1366 <        }
1178 >        delay(SHORT_DELAY_MS);
1179 >        t.interrupt();
1180 >        t.join();
1181      }
1182  
1183      /**
1184 <     *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1185 <     *  on interruption throws
1184 >     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1185 >     * on interruption throws
1186       */
1187 <    public void testTimedPollWithOfferLast() {
1187 >    public void testTimedPollWithOfferLast() throws InterruptedException {
1188          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1189 <        Thread t = new Thread(new Runnable() {
1190 <                public void run() {
1191 <                    try {
1192 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1193 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1194 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1195 <                        threadShouldThrow();
1196 <                    } catch (InterruptedException success) { }
1197 <                }
1198 <            });
1199 <        try {
1200 <            t.start();
1201 <            Thread.sleep(SMALL_DELAY_MS);
1202 <            assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1203 <            t.interrupt();
1390 <            t.join();
1391 <        } catch (Exception e){
1392 <            unexpectedException();
1393 <        }
1189 >        Thread t = new Thread(new CheckedRunnable() {
1190 >            public void realRun() throws InterruptedException {
1191 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1192 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1193 >                try {
1194 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1195 >                    shouldThrow();
1196 >                } catch (InterruptedException success) {}
1197 >            }});
1198 >
1199 >        t.start();
1200 >        delay(SMALL_DELAY_MS);
1201 >        assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1202 >        t.interrupt();
1203 >        t.join();
1204      }
1205  
1206  
# Line 1400 | Line 1210 | public class LinkedBlockingDequeTest ext
1210      public void testElement() {
1211          LinkedBlockingDeque q = populatedDeque(SIZE);
1212          for (int i = 0; i < SIZE; ++i) {
1213 <            assertEquals(i, ((Integer)q.element()).intValue());
1213 >            assertEquals(i, q.element());
1214              q.poll();
1215          }
1216          try {
1217              q.element();
1218              shouldThrow();
1219 <        }
1410 <        catch (NoSuchElementException success) {}
1219 >        } catch (NoSuchElementException success) {}
1220      }
1221  
1222      /**
# Line 1416 | Line 1225 | public class LinkedBlockingDequeTest ext
1225      public void testRemoveElement() {
1226          LinkedBlockingDeque q = populatedDeque(SIZE);
1227          for (int i = 1; i < SIZE; i+=2) {
1228 <            assertTrue(q.remove(new Integer(i)));
1228 >            assertTrue(q.contains(i));
1229 >            assertTrue(q.remove(i));
1230 >            assertFalse(q.contains(i));
1231 >            assertTrue(q.contains(i-1));
1232          }
1233          for (int i = 0; i < SIZE; i+=2) {
1234 <            assertTrue(q.remove(new Integer(i)));
1235 <            assertFalse(q.remove(new Integer(i+1)));
1234 >            assertTrue(q.contains(i));
1235 >            assertTrue(q.remove(i));
1236 >            assertFalse(q.contains(i));
1237 >            assertFalse(q.remove(i+1));
1238 >            assertFalse(q.contains(i+1));
1239          }
1240          assertTrue(q.isEmpty());
1241      }
# Line 1503 | Line 1318 | public class LinkedBlockingDequeTest ext
1318      }
1319  
1320      /**
1321 <     * toArray contains all elements
1321 >     * toArray contains all elements in FIFO order
1322       */
1323 <    public void testToArray() {
1323 >    public void testToArray() throws InterruptedException{
1324          LinkedBlockingDeque q = populatedDeque(SIZE);
1325 <        Object[] o = q.toArray();
1326 <        try {
1327 <        for (int i = 0; i < o.length; i++)
1513 <            assertEquals(o[i], q.take());
1514 <        } catch (InterruptedException e){
1515 <            unexpectedException();
1516 <        }
1325 >        Object[] o = q.toArray();
1326 >        for (int i = 0; i < o.length; i++)
1327 >            assertSame(o[i], q.poll());
1328      }
1329  
1330      /**
1331 <     * toArray(a) contains all elements
1331 >     * toArray(a) contains all elements in FIFO order
1332       */
1333      public void testToArray2() {
1334 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1335 <        Integer[] ints = new Integer[SIZE];
1336 <        ints = (Integer[])q.toArray(ints);
1337 <        try {
1338 <            for (int i = 0; i < ints.length; i++)
1339 <                assertEquals(ints[i], q.take());
1529 <        } catch (InterruptedException e){
1530 <            unexpectedException();
1531 <        }
1334 >        LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1335 >        Integer[] ints = new Integer[SIZE];
1336 >        Integer[] array = q.toArray(ints);
1337 >        assertSame(ints, array);
1338 >        for (int i = 0; i < ints.length; i++)
1339 >            assertSame(ints[i], q.remove());
1340      }
1341  
1342      /**
1343 <     * toArray(null) throws NPE
1343 >     * toArray(null) throws NullPointerException
1344       */
1345 <    public void testToArray_BadArg() {
1346 <        try {
1347 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1348 <            Object o[] = q.toArray(null);
1349 <            shouldThrow();
1350 <        } catch (NullPointerException success){}
1345 >    public void testToArray_NullArg() {
1346 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1347 >        try {
1348 >            q.toArray(null);
1349 >            shouldThrow();
1350 >        } catch (NullPointerException success) {}
1351      }
1352  
1353      /**
1354 <     * toArray with incompatible array type throws CCE
1354 >     * toArray(incompatible array type) throws ArrayStoreException
1355       */
1356      public void testToArray1_BadArg() {
1357 <        try {
1358 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1359 <            Object o[] = q.toArray(new String[10] );
1360 <            shouldThrow();
1361 <        } catch (ArrayStoreException  success){}
1357 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1358 >        try {
1359 >            q.toArray(new String[10]);
1360 >            shouldThrow();
1361 >        } catch (ArrayStoreException success) {}
1362      }
1363  
1364  
1365      /**
1366       * iterator iterates through all elements
1367       */
1368 <    public void testIterator() {
1368 >    public void testIterator() throws InterruptedException {
1369          LinkedBlockingDeque q = populatedDeque(SIZE);
1370 <        Iterator it = q.iterator();
1371 <        try {
1372 <            while (it.hasNext()){
1373 <                assertEquals(it.next(), q.take());
1566 <            }
1567 <        } catch (InterruptedException e){
1568 <            unexpectedException();
1569 <        }
1370 >        Iterator it = q.iterator();
1371 >        while (it.hasNext()) {
1372 >            assertEquals(it.next(), q.take());
1373 >        }
1374      }
1375  
1376      /**
1377       * iterator.remove removes current element
1378       */
1379 <    public void testIteratorRemove () {
1379 >    public void testIteratorRemove() {
1380          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1381          q.add(two);
1382          q.add(one);
# Line 1583 | Line 1387 | public class LinkedBlockingDequeTest ext
1387          it.remove();
1388  
1389          it = q.iterator();
1390 <        assertEquals(it.next(), one);
1391 <        assertEquals(it.next(), three);
1390 >        assertSame(it.next(), one);
1391 >        assertSame(it.next(), three);
1392          assertFalse(it.hasNext());
1393      }
1394  
# Line 1600 | Line 1404 | public class LinkedBlockingDequeTest ext
1404          assertEquals(0, q.remainingCapacity());
1405          int k = 0;
1406          for (Iterator it = q.iterator(); it.hasNext();) {
1407 <            int i = ((Integer)(it.next())).intValue();
1604 <            assertEquals(++k, i);
1407 >            assertEquals(++k, it.next());
1408          }
1409          assertEquals(3, k);
1410      }
# Line 1609 | Line 1412 | public class LinkedBlockingDequeTest ext
1412      /**
1413       * Modifications do not cause iterators to fail
1414       */
1415 <    public void testWeaklyConsistentIteration () {
1415 >    public void testWeaklyConsistentIteration() {
1416          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1417          q.add(one);
1418          q.add(two);
1419          q.add(three);
1420 <        try {
1421 <            for (Iterator it = q.iterator(); it.hasNext();) {
1422 <                q.remove();
1620 <                it.next();
1621 <            }
1622 <        }
1623 <        catch (ConcurrentModificationException e) {
1624 <            unexpectedException();
1420 >        for (Iterator it = q.iterator(); it.hasNext();) {
1421 >            q.remove();
1422 >            it.next();
1423          }
1424          assertEquals(0, q.size());
1425      }
1426  
1427  
1428      /**
1429 <     *  Descending iterator iterates through all elements
1429 >     * Descending iterator iterates through all elements
1430       */
1431      public void testDescendingIterator() {
1432          LinkedBlockingDeque q = populatedDeque(SIZE);
1433          int i = 0;
1434 <        Iterator it = q.descendingIterator();
1434 >        Iterator it = q.descendingIterator();
1435          while (it.hasNext()) {
1436              assertTrue(q.contains(it.next()));
1437              ++i;
# Line 1642 | Line 1440 | public class LinkedBlockingDequeTest ext
1440          assertFalse(it.hasNext());
1441          try {
1442              it.next();
1443 <        } catch (NoSuchElementException success) {
1444 <        }
1443 >            shouldThrow();
1444 >        } catch (NoSuchElementException success) {}
1445      }
1446  
1447      /**
1448 <     *  Descending iterator ordering is reverse FIFO
1448 >     * Descending iterator ordering is reverse FIFO
1449       */
1450      public void testDescendingIteratorOrdering() {
1451          final LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1657 | Line 1455 | public class LinkedBlockingDequeTest ext
1455              q.add(new Integer(1));
1456              int k = 0;
1457              for (Iterator it = q.descendingIterator(); it.hasNext();) {
1458 <                int i = ((Integer)(it.next())).intValue();
1661 <                assertEquals(++k, i);
1458 >                assertEquals(++k, it.next());
1459              }
1460  
1461              assertEquals(3, k);
# Line 1671 | Line 1468 | public class LinkedBlockingDequeTest ext
1468      /**
1469       * descendingIterator.remove removes current element
1470       */
1471 <    public void testDescendingIteratorRemove () {
1471 >    public void testDescendingIteratorRemove() {
1472          final LinkedBlockingDeque q = new LinkedBlockingDeque();
1473          for (int iters = 0; iters < 100; ++iters) {
1474              q.add(new Integer(3));
# Line 1711 | Line 1508 | public class LinkedBlockingDequeTest ext
1508          q.add(one);
1509          q.add(two);
1510          ExecutorService executor = Executors.newFixedThreadPool(2);
1511 <        executor.execute(new Runnable() {
1512 <            public void run() {
1513 <                threadAssertFalse(q.offer(three));
1514 <                try {
1515 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1516 <                    threadAssertEquals(0, q.remainingCapacity());
1517 <                }
1518 <                catch (InterruptedException e) {
1519 <                    threadUnexpectedException();
1520 <                }
1521 <            }
1522 <        });
1726 <
1727 <        executor.execute(new Runnable() {
1728 <            public void run() {
1729 <                try {
1730 <                    Thread.sleep(SMALL_DELAY_MS);
1731 <                    threadAssertEquals(one, q.take());
1732 <                }
1733 <                catch (InterruptedException e) {
1734 <                    threadUnexpectedException();
1735 <                }
1736 <            }
1737 <        });
1511 >        executor.execute(new CheckedRunnable() {
1512 >            public void realRun() throws InterruptedException {
1513 >                assertFalse(q.offer(three));
1514 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1515 >                assertEquals(0, q.remainingCapacity());
1516 >            }});
1517 >
1518 >        executor.execute(new CheckedRunnable() {
1519 >            public void realRun() throws InterruptedException {
1520 >                delay(SMALL_DELAY_MS);
1521 >                assertSame(one, q.take());
1522 >            }});
1523  
1524          joinPool(executor);
1525      }
# Line 1745 | Line 1530 | public class LinkedBlockingDequeTest ext
1530      public void testPollInExecutor() {
1531          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1532          ExecutorService executor = Executors.newFixedThreadPool(2);
1533 <        executor.execute(new Runnable() {
1534 <            public void run() {
1535 <                threadAssertNull(q.poll());
1536 <                try {
1537 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1538 <                    threadAssertTrue(q.isEmpty());
1539 <                }
1540 <                catch (InterruptedException e) {
1541 <                    threadUnexpectedException();
1542 <                }
1543 <            }
1544 <        });
1760 <
1761 <        executor.execute(new Runnable() {
1762 <            public void run() {
1763 <                try {
1764 <                    Thread.sleep(SMALL_DELAY_MS);
1765 <                    q.put(one);
1766 <                }
1767 <                catch (InterruptedException e) {
1768 <                    threadUnexpectedException();
1769 <                }
1770 <            }
1771 <        });
1533 >        executor.execute(new CheckedRunnable() {
1534 >            public void realRun() throws InterruptedException {
1535 >                assertNull(q.poll());
1536 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1537 >                assertTrue(q.isEmpty());
1538 >            }});
1539 >
1540 >        executor.execute(new CheckedRunnable() {
1541 >            public void realRun() throws InterruptedException {
1542 >                delay(SMALL_DELAY_MS);
1543 >                q.put(one);
1544 >            }});
1545  
1546          joinPool(executor);
1547      }
# Line 1776 | Line 1549 | public class LinkedBlockingDequeTest ext
1549      /**
1550       * A deserialized serialized deque has same elements in same order
1551       */
1552 <    public void testSerialization() {
1552 >    public void testSerialization() throws Exception {
1553          LinkedBlockingDeque q = populatedDeque(SIZE);
1554  
1555 <        try {
1556 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1557 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1558 <            out.writeObject(q);
1559 <            out.close();
1560 <
1561 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1562 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1563 <            LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1564 <            assertEquals(q.size(), r.size());
1565 <            while (!q.isEmpty())
1793 <                assertEquals(q.remove(), r.remove());
1794 <        } catch (Exception e){
1795 <            unexpectedException();
1796 <        }
1555 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1556 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1557 >        out.writeObject(q);
1558 >        out.close();
1559 >
1560 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1561 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1562 >        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1563 >        assertEquals(q.size(), r.size());
1564 >        while (!q.isEmpty())
1565 >            assertEquals(q.remove(), r.remove());
1566      }
1567  
1568      /**
# Line 1804 | Line 1573 | public class LinkedBlockingDequeTest ext
1573          try {
1574              q.drainTo(null);
1575              shouldThrow();
1576 <        } catch (NullPointerException success) {
1808 <        }
1576 >        } catch (NullPointerException success) {}
1577      }
1578  
1579      /**
# Line 1816 | Line 1584 | public class LinkedBlockingDequeTest ext
1584          try {
1585              q.drainTo(q);
1586              shouldThrow();
1587 <        } catch (IllegalArgumentException success) {
1820 <        }
1587 >        } catch (IllegalArgumentException success) {}
1588      }
1589  
1590      /**
# Line 1847 | Line 1614 | public class LinkedBlockingDequeTest ext
1614      /**
1615       * drainTo empties full deque, unblocking a waiting put.
1616       */
1617 <    public void testDrainToWithActivePut() {
1617 >    public void testDrainToWithActivePut() throws InterruptedException {
1618          final LinkedBlockingDeque q = populatedDeque(SIZE);
1619 <        Thread t = new Thread(new Runnable() {
1620 <                public void run() {
1621 <                    try {
1622 <                        q.put(new Integer(SIZE+1));
1623 <                    } catch (InterruptedException ie){
1624 <                        threadUnexpectedException();
1625 <                    }
1626 <                }
1627 <            });
1628 <        try {
1629 <            t.start();
1630 <            ArrayList l = new ArrayList();
1631 <            q.drainTo(l);
1865 <            assertTrue(l.size() >= SIZE);
1866 <            for (int i = 0; i < SIZE; ++i)
1867 <                assertEquals(l.get(i), new Integer(i));
1868 <            t.join();
1869 <            assertTrue(q.size() + l.size() >= SIZE);
1870 <        } catch (Exception e){
1871 <            unexpectedException();
1872 <        }
1619 >        Thread t = new Thread(new CheckedRunnable() {
1620 >            public void realRun() throws InterruptedException {
1621 >                q.put(new Integer(SIZE+1));
1622 >            }});
1623 >
1624 >        t.start();
1625 >        ArrayList l = new ArrayList();
1626 >        q.drainTo(l);
1627 >        assertTrue(l.size() >= SIZE);
1628 >        for (int i = 0; i < SIZE; ++i)
1629 >            assertEquals(l.get(i), new Integer(i));
1630 >        t.join();
1631 >        assertTrue(q.size() + l.size() >= SIZE);
1632      }
1633  
1634      /**
# Line 1880 | Line 1639 | public class LinkedBlockingDequeTest ext
1639          try {
1640              q.drainTo(null, 0);
1641              shouldThrow();
1642 <        } catch (NullPointerException success) {
1884 <        }
1642 >        } catch (NullPointerException success) {}
1643      }
1644  
1645      /**
# Line 1892 | Line 1650 | public class LinkedBlockingDequeTest ext
1650          try {
1651              q.drainTo(q, 0);
1652              shouldThrow();
1653 <        } catch (IllegalArgumentException success) {
1896 <        }
1653 >        } catch (IllegalArgumentException success) {}
1654      }
1655  
1656      /**
1657 <     * drainTo(c, n) empties first max {n, size} elements of deque into c
1657 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
1658       */
1659      public void testDrainToN() {
1660          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1906 | Line 1663 | public class LinkedBlockingDequeTest ext
1663                  assertTrue(q.offer(new Integer(j)));
1664              ArrayList l = new ArrayList();
1665              q.drainTo(l, i);
1666 <            int k = (i < SIZE)? i : SIZE;
1666 >            int k = (i < SIZE) ? i : SIZE;
1667              assertEquals(l.size(), k);
1668              assertEquals(q.size(), SIZE-k);
1669              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines