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.6 by jsr166, Mon Nov 16 05:30:07 2009 UTC vs.
Revision 1.50 by jsr166, Sun Nov 23 22:27:06 2014 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 java.io.*;
8 > import java.util.Arrays;
9 > import java.util.ArrayList;
10 > import java.util.Collection;
11 > import java.util.Deque;
12 > import java.util.Iterator;
13 > import java.util.NoSuchElementException;
14 > import java.util.Queue;
15 > import java.util.concurrent.BlockingDeque;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.CountDownLatch;
18 > import java.util.concurrent.Executors;
19 > import java.util.concurrent.ExecutorService;
20 > import java.util.concurrent.LinkedBlockingDeque;
21 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
22  
23   public class LinkedBlockingDequeTest extends JSR166TestCase {
24 +
25 +    public static class Unbounded extends BlockingQueueTest {
26 +        protected BlockingQueue emptyCollection() {
27 +            return new LinkedBlockingDeque();
28 +        }
29 +    }
30 +
31 +    public static class Bounded extends BlockingQueueTest {
32 +        protected BlockingQueue emptyCollection() {
33 +            return new LinkedBlockingDeque(SIZE);
34 +        }
35 +    }
36 +
37      public static void main(String[] args) {
38 <        junit.textui.TestRunner.run (suite());
38 >        junit.textui.TestRunner.run(suite());
39      }
40  
41      public static Test suite() {
42 <        return new TestSuite(LinkedBlockingDequeTest.class);
42 >        return newTestSuite(LinkedBlockingDequeTest.class,
43 >                            new Unbounded().testSuite(),
44 >                            new Bounded().testSuite());
45      }
46  
47      /**
48 <     * Create a deque of given size containing consecutive
48 >     * Returns a new deque of given size containing consecutive
49       * Integers 0 ... n.
50       */
51 <    private LinkedBlockingDeque populatedDeque(int n) {
52 <        LinkedBlockingDeque q = new LinkedBlockingDeque(n);
51 >    private LinkedBlockingDeque<Integer> populatedDeque(int n) {
52 >        LinkedBlockingDeque<Integer> q =
53 >            new LinkedBlockingDeque<Integer>(n);
54          assertTrue(q.isEmpty());
55 <        for (int i = 0; i < n; i++)
56 <            assertTrue(q.offer(new Integer(i)));
55 >        for (int i = 0; i < n; i++)
56 >            assertTrue(q.offer(new Integer(i)));
57          assertFalse(q.isEmpty());
58          assertEquals(0, q.remainingCapacity());
59 <        assertEquals(n, q.size());
59 >        assertEquals(n, q.size());
60          return q;
61      }
62  
# Line 63 | Line 90 | public class LinkedBlockingDequeTest ext
90      }
91  
92      /**
93 <     * offer(null) throws NPE
93 >     * offerFirst(null) throws NullPointerException
94       */
95      public void testOfferFirstNull() {
96 <        try {
97 <            LinkedBlockingDeque q = new LinkedBlockingDeque();
96 >        LinkedBlockingDeque q = new LinkedBlockingDeque();
97 >        try {
98              q.offerFirst(null);
99              shouldThrow();
100 <        } catch (NullPointerException success) {
101 <        }
100 >        } catch (NullPointerException success) {}
101 >    }
102 >
103 >    /**
104 >     * offerLast(null) throws NullPointerException
105 >     */
106 >    public void testOfferLastNull() {
107 >        LinkedBlockingDeque q = new LinkedBlockingDeque();
108 >        try {
109 >            q.offerLast(null);
110 >            shouldThrow();
111 >        } catch (NullPointerException success) {}
112      }
113  
114      /**
# Line 93 | Line 130 | public class LinkedBlockingDequeTest ext
130      }
131  
132      /**
133 <     *  pollFirst succeeds unless empty
133 >     * pollFirst succeeds unless empty
134       */
135      public void testPollFirst() {
136          LinkedBlockingDeque q = populatedDeque(SIZE);
137          for (int i = 0; i < SIZE; ++i) {
138 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
138 >            assertEquals(i, q.pollFirst());
139          }
140 <        assertNull(q.pollFirst());
140 >        assertNull(q.pollFirst());
141      }
142  
143      /**
144 <     *  pollLast succeeds unless empty
144 >     * pollLast succeeds unless empty
145       */
146      public void testPollLast() {
147          LinkedBlockingDeque q = populatedDeque(SIZE);
148          for (int i = SIZE-1; i >= 0; --i) {
149 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
149 >            assertEquals(i, q.pollLast());
150          }
151 <        assertNull(q.pollLast());
151 >        assertNull(q.pollLast());
152      }
153  
154      /**
155 <     *  peekFirst returns next element, or null if empty
155 >     * peekFirst returns next element, or null if empty
156       */
157      public void testPeekFirst() {
158          LinkedBlockingDeque q = populatedDeque(SIZE);
159          for (int i = 0; i < SIZE; ++i) {
160 <            assertEquals(i, ((Integer)q.peekFirst()).intValue());
161 <            q.pollFirst();
160 >            assertEquals(i, q.peekFirst());
161 >            assertEquals(i, q.pollFirst());
162              assertTrue(q.peekFirst() == null ||
163 <                       i != ((Integer)q.peekFirst()).intValue());
163 >                       !q.peekFirst().equals(i));
164          }
165 <        assertNull(q.peekFirst());
165 >        assertNull(q.peekFirst());
166      }
167  
168      /**
169 <     *  peek returns next element, or null if empty
169 >     * peek returns next element, or null if empty
170       */
171      public void testPeek() {
172          LinkedBlockingDeque q = populatedDeque(SIZE);
173          for (int i = 0; i < SIZE; ++i) {
174 <            assertEquals(i, ((Integer)q.peek()).intValue());
175 <            q.pollFirst();
174 >            assertEquals(i, q.peek());
175 >            assertEquals(i, q.pollFirst());
176              assertTrue(q.peek() == null ||
177 <                       i != ((Integer)q.peek()).intValue());
177 >                       !q.peek().equals(i));
178          }
179 <        assertNull(q.peek());
179 >        assertNull(q.peek());
180      }
181  
182      /**
183 <     *  peekLast returns next element, or null if empty
183 >     * peekLast returns next element, or null if empty
184       */
185      public void testPeekLast() {
186          LinkedBlockingDeque q = populatedDeque(SIZE);
187          for (int i = SIZE-1; i >= 0; --i) {
188 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
189 <            q.pollLast();
188 >            assertEquals(i, q.peekLast());
189 >            assertEquals(i, q.pollLast());
190              assertTrue(q.peekLast() == null ||
191 <                       i != ((Integer)q.peekLast()).intValue());
191 >                       !q.peekLast().equals(i));
192          }
193 <        assertNull(q.peekLast());
193 >        assertNull(q.peekLast());
194      }
195  
196      /**
197 <     * getFirst returns next getFirst, or throws NSEE if empty
197 >     * getFirst() returns first element, or throws NSEE if empty
198       */
199      public void testFirstElement() {
200          LinkedBlockingDeque q = populatedDeque(SIZE);
201          for (int i = 0; i < SIZE; ++i) {
202 <            assertEquals(i, ((Integer)q.getFirst()).intValue());
203 <            q.pollFirst();
202 >            assertEquals(i, q.getFirst());
203 >            assertEquals(i, q.pollFirst());
204          }
205          try {
206              q.getFirst();
207              shouldThrow();
208 <        }
209 <        catch (NoSuchElementException success) {}
208 >        } catch (NoSuchElementException success) {}
209 >        assertNull(q.peekFirst());
210      }
211  
212      /**
213 <     *  getLast returns next element, or throws NSEE if empty
213 >     * getLast() returns last element, or throws NSEE if empty
214       */
215      public void testLastElement() {
216          LinkedBlockingDeque q = populatedDeque(SIZE);
217          for (int i = SIZE-1; i >= 0; --i) {
218 <            assertEquals(i, ((Integer)q.getLast()).intValue());
219 <            q.pollLast();
218 >            assertEquals(i, q.getLast());
219 >            assertEquals(i, q.pollLast());
220          }
221          try {
222              q.getLast();
223              shouldThrow();
224 <        }
225 <        catch (NoSuchElementException success) {}
189 <        assertNull(q.peekLast());
224 >        } catch (NoSuchElementException success) {}
225 >        assertNull(q.peekLast());
226      }
227  
228      /**
229 <     *  removeFirst removes next element, or throws NSEE if empty
229 >     * removeFirst() removes first element, or throws NSEE if empty
230       */
231      public void testRemoveFirst() {
232          LinkedBlockingDeque q = populatedDeque(SIZE);
233          for (int i = 0; i < SIZE; ++i) {
234 <            assertEquals(i, ((Integer)q.removeFirst()).intValue());
234 >            assertEquals(i, q.removeFirst());
235          }
236          try {
237              q.removeFirst();
238              shouldThrow();
239 <        } catch (NoSuchElementException success) {
240 <        }
239 >        } catch (NoSuchElementException success) {}
240 >        assertNull(q.peekFirst());
241 >    }
242 >
243 >    /**
244 >     * removeLast() removes last element, or throws NSEE if empty
245 >     */
246 >    public void testRemoveLast() {
247 >        LinkedBlockingDeque q = populatedDeque(SIZE);
248 >        for (int i = SIZE - 1; i >= 0; --i) {
249 >            assertEquals(i, q.removeLast());
250 >        }
251 >        try {
252 >            q.removeLast();
253 >            shouldThrow();
254 >        } catch (NoSuchElementException success) {}
255 >        assertNull(q.peekLast());
256      }
257  
258      /**
259 <     *  remove removes next element, or throws NSEE if empty
259 >     * remove removes next element, or throws NSEE if empty
260       */
261      public void testRemove() {
262          LinkedBlockingDeque q = populatedDeque(SIZE);
263          for (int i = 0; i < SIZE; ++i) {
264 <            assertEquals(i, ((Integer)q.remove()).intValue());
264 >            assertEquals(i, q.remove());
265          }
266          try {
267              q.remove();
268              shouldThrow();
269 <        } catch (NoSuchElementException success) {
219 <        }
269 >        } catch (NoSuchElementException success) {}
270      }
271  
272      /**
# Line 255 | Line 305 | public class LinkedBlockingDequeTest ext
305      public void testAddFirst() {
306          LinkedBlockingDeque q = populatedDeque(3);
307          q.pollLast();
308 <        q.addFirst(four);
309 <        assertEquals(four,q.peekFirst());
308 >        q.addFirst(four);
309 >        assertSame(four, q.peekFirst());
310      }
311  
312      /**
# Line 265 | Line 315 | public class LinkedBlockingDequeTest ext
315      public void testAddLast() {
316          LinkedBlockingDeque q = populatedDeque(3);
317          q.pollLast();
318 <        q.addLast(four);
319 <        assertEquals(four,q.peekLast());
318 >        q.addLast(four);
319 >        assertSame(four, q.peekLast());
320      }
321  
272
322      /**
323       * A new deque has the indicated capacity, or Integer.MAX_VALUE if
324       * none given
# Line 280 | Line 329 | public class LinkedBlockingDequeTest ext
329      }
330  
331      /**
332 <     * Constructor throws IAE if  capacity argument nonpositive
332 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
333       */
334      public void testConstructor2() {
335          try {
336 <            LinkedBlockingDeque q = new LinkedBlockingDeque(0);
336 >            new LinkedBlockingDeque(0);
337              shouldThrow();
338 <        }
290 <        catch (IllegalArgumentException success) {}
338 >        } catch (IllegalArgumentException success) {}
339      }
340  
341      /**
342 <     * Initializing from null Collection throws NPE
342 >     * Initializing from null Collection throws NullPointerException
343       */
344      public void testConstructor3() {
345          try {
346 <            LinkedBlockingDeque q = new LinkedBlockingDeque(null);
346 >            new LinkedBlockingDeque(null);
347              shouldThrow();
348 <        }
301 <        catch (NullPointerException success) {}
348 >        } catch (NullPointerException success) {}
349      }
350  
351      /**
352 <     * Initializing from Collection of null elements throws NPE
352 >     * Initializing from Collection of null elements throws NullPointerException
353       */
354      public void testConstructor4() {
355 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
356          try {
357 <            Integer[] ints = new Integer[SIZE];
310 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
357 >            new LinkedBlockingDeque(elements);
358              shouldThrow();
359 <        }
313 <        catch (NullPointerException success) {}
359 >        } catch (NullPointerException success) {}
360      }
361  
362      /**
363 <     * Initializing from Collection with some null elements throws NPE
363 >     * Initializing from Collection with some null elements throws
364 >     * NullPointerException
365       */
366      public void testConstructor5() {
367 +        Integer[] ints = new Integer[SIZE];
368 +        for (int i = 0; i < SIZE-1; ++i)
369 +            ints[i] = i;
370 +        Collection<Integer> elements = Arrays.asList(ints);
371          try {
372 <            Integer[] ints = new Integer[SIZE];
322 <            for (int i = 0; i < SIZE-1; ++i)
323 <                ints[i] = new Integer(i);
324 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
372 >            new LinkedBlockingDeque(elements);
373              shouldThrow();
374 <        }
327 <        catch (NullPointerException success) {}
374 >        } catch (NullPointerException success) {}
375      }
376  
377      /**
378       * Deque contains all elements of collection used to initialize
379       */
380      public void testConstructor6() {
381 <        try {
382 <            Integer[] ints = new Integer[SIZE];
383 <            for (int i = 0; i < SIZE; ++i)
384 <                ints[i] = new Integer(i);
385 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
386 <            for (int i = 0; i < SIZE; ++i)
340 <                assertEquals(ints[i], q.poll());
341 <        }
342 <        finally {}
381 >        Integer[] ints = new Integer[SIZE];
382 >        for (int i = 0; i < SIZE; ++i)
383 >            ints[i] = i;
384 >        LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
385 >        for (int i = 0; i < SIZE; ++i)
386 >            assertEquals(ints[i], q.poll());
387      }
388  
389      /**
# Line 375 | Line 419 | public class LinkedBlockingDequeTest ext
419      }
420  
421      /**
378     * offer(null) throws NPE
379     */
380    public void testOfferNull() {
381        try {
382            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
383            q.offer(null);
384            shouldThrow();
385        } catch (NullPointerException success) { }
386    }
387
388    /**
389     * add(null) throws NPE
390     */
391    public void testAddNull() {
392        try {
393            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
394            q.add(null);
395            shouldThrow();
396        } catch (NullPointerException success) { }
397    }
398
399    /**
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  
437
459      /**
460 <     *  pop removes next element, or throws NSEE if empty
460 >     * pop removes next element, or throws NSEE if empty
461       */
462      public void testPop() {
463          LinkedBlockingDeque q = populatedDeque(SIZE);
464          for (int i = 0; i < SIZE; ++i) {
465 <            assertEquals(i, ((Integer)q.pop()).intValue());
465 >            assertEquals(i, q.pop());
466          }
467          try {
468              q.pop();
469              shouldThrow();
470 <        } catch (NoSuchElementException success) {
450 <        }
470 >        } catch (NoSuchElementException success) {}
471      }
472  
453
473      /**
474       * Offer succeeds if not full; fails if full
475       */
# Line 464 | Line 483 | public class LinkedBlockingDequeTest ext
483       * add succeeds if not full; throws ISE if full
484       */
485      public void testAdd() {
486 <        try {
487 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
488 <            for (int i = 0; i < SIZE; ++i) {
489 <                assertTrue(q.add(new Integer(i)));
471 <            }
472 <            assertEquals(0, q.remainingCapacity());
473 <            q.add(new Integer(SIZE));
474 <        } catch (IllegalStateException success) {
475 <        }
476 <    }
477 <
478 <    /**
479 <     * addAll(null) throws NPE
480 <     */
481 <    public void testAddAll1() {
486 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
487 >        for (int i = 0; i < SIZE; ++i)
488 >            assertTrue(q.add(new Integer(i)));
489 >        assertEquals(0, q.remainingCapacity());
490          try {
491 <            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
484 <            q.addAll(null);
491 >            q.add(new Integer(SIZE));
492              shouldThrow();
493 <        }
487 <        catch (NullPointerException success) {}
493 >        } catch (IllegalStateException success) {}
494      }
495  
496      /**
497       * addAll(this) throws IAE
498       */
499      public void testAddAllSelf() {
500 +        LinkedBlockingDeque q = populatedDeque(SIZE);
501          try {
495            LinkedBlockingDeque q = populatedDeque(SIZE);
502              q.addAll(q);
503              shouldThrow();
504 <        }
499 <        catch (IllegalArgumentException success) {}
504 >        } catch (IllegalArgumentException success) {}
505      }
506  
507      /**
503     * addAll of a collection with null elements throws NPE
504     */
505    public void testAddAll2() {
506        try {
507            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
508            Integer[] ints = new Integer[SIZE];
509            q.addAll(Arrays.asList(ints));
510            shouldThrow();
511        }
512        catch (NullPointerException success) {}
513    }
514    /**
508       * addAll of a collection with any null elements throws NPE after
509       * possibly adding some elements
510       */
511      public void testAddAll3() {
512 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
513 +        Integer[] ints = new Integer[SIZE];
514 +        for (int i = 0; i < SIZE-1; ++i)
515 +            ints[i] = new Integer(i);
516 +        Collection<Integer> elements = Arrays.asList(ints);
517          try {
518 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
521 <            Integer[] ints = new Integer[SIZE];
522 <            for (int i = 0; i < SIZE-1; ++i)
523 <                ints[i] = new Integer(i);
524 <            q.addAll(Arrays.asList(ints));
518 >            q.addAll(elements);
519              shouldThrow();
520 <        }
527 <        catch (NullPointerException success) {}
520 >        } catch (NullPointerException success) {}
521      }
522 +
523      /**
524 <     * addAll throws ISE if not enough room
524 >     * addAll throws IllegalStateException if not enough room
525       */
526      public void testAddAll4() {
527 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
528 +        Integer[] ints = new Integer[SIZE];
529 +        for (int i = 0; i < SIZE; ++i)
530 +            ints[i] = new Integer(i);
531 +        Collection<Integer> elements = Arrays.asList(ints);
532          try {
533 <            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
535 <            Integer[] ints = new Integer[SIZE];
536 <            for (int i = 0; i < SIZE; ++i)
537 <                ints[i] = new Integer(i);
538 <            q.addAll(Arrays.asList(ints));
533 >            q.addAll(elements);
534              shouldThrow();
535 <        }
541 <        catch (IllegalStateException success) {}
535 >        } catch (IllegalStateException success) {}
536      }
537 +
538      /**
539       * Deque contains all elements, in traversal order, of successful addAll
540       */
541      public void testAddAll5() {
542 <        try {
543 <            Integer[] empty = new Integer[0];
544 <            Integer[] ints = new Integer[SIZE];
545 <            for (int i = 0; i < SIZE; ++i)
546 <                ints[i] = new Integer(i);
547 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
548 <            assertFalse(q.addAll(Arrays.asList(empty)));
549 <            assertTrue(q.addAll(Arrays.asList(ints)));
550 <            for (int i = 0; i < SIZE; ++i)
556 <                assertEquals(ints[i], q.poll());
557 <        }
558 <        finally {}
542 >        Integer[] empty = new Integer[0];
543 >        Integer[] ints = new Integer[SIZE];
544 >        for (int i = 0; i < SIZE; ++i)
545 >            ints[i] = new Integer(i);
546 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
547 >        assertFalse(q.addAll(Arrays.asList(empty)));
548 >        assertTrue(q.addAll(Arrays.asList(ints)));
549 >        for (int i = 0; i < SIZE; ++i)
550 >            assertEquals(ints[i], q.poll());
551      }
552  
561
562    /**
563     * put(null) throws NPE
564     */
565     public void testPutNull() {
566        try {
567            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
568            q.put(null);
569            shouldThrow();
570        }
571        catch (NullPointerException success) {
572        }
573        catch (InterruptedException ie) {
574            unexpectedException();
575        }
576     }
577
553      /**
554       * all elements successfully put are contained
555       */
556 <     public void testPut() {
557 <         try {
558 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
559 <             for (int i = 0; i < SIZE; ++i) {
560 <                 Integer I = new Integer(i);
561 <                 q.put(I);
587 <                 assertTrue(q.contains(I));
588 <             }
589 <             assertEquals(0, q.remainingCapacity());
590 <         }
591 <        catch (InterruptedException ie) {
592 <            unexpectedException();
556 >    public void testPut() throws InterruptedException {
557 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
558 >        for (int i = 0; i < SIZE; ++i) {
559 >            Integer I = new Integer(i);
560 >            q.put(I);
561 >            assertTrue(q.contains(I));
562          }
563 +        assertEquals(0, q.remainingCapacity());
564      }
565  
566      /**
567       * put blocks interruptibly if full
568       */
569 <    public void testBlockingPut() {
570 <        Thread t = new Thread(new Runnable() {
571 <                public void run() {
572 <                    int added = 0;
573 <                    try {
574 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
575 <                        for (int i = 0; i < SIZE; ++i) {
576 <                            q.put(new Integer(i));
577 <                            ++added;
578 <                        }
579 <                        q.put(new Integer(SIZE));
580 <                        threadShouldThrow();
581 <                    } catch (InterruptedException ie) {
582 <                        threadAssertEquals(added, SIZE);
583 <                    }
584 <                }});
585 <        t.start();
586 <        try {
587 <           Thread.sleep(SHORT_DELAY_MS);
588 <           t.interrupt();
589 <           t.join();
590 <        }
591 <        catch (InterruptedException ie) {
592 <            unexpectedException();
593 <        }
569 >    public void testBlockingPut() throws InterruptedException {
570 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
571 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
572 >        Thread t = newStartedThread(new CheckedRunnable() {
573 >            public void realRun() throws InterruptedException {
574 >                for (int i = 0; i < SIZE; ++i)
575 >                    q.put(i);
576 >                assertEquals(SIZE, q.size());
577 >                assertEquals(0, q.remainingCapacity());
578 >
579 >                Thread.currentThread().interrupt();
580 >                try {
581 >                    q.put(99);
582 >                    shouldThrow();
583 >                } catch (InterruptedException success) {}
584 >                assertFalse(Thread.interrupted());
585 >
586 >                pleaseInterrupt.countDown();
587 >                try {
588 >                    q.put(99);
589 >                    shouldThrow();
590 >                } catch (InterruptedException success) {}
591 >                assertFalse(Thread.interrupted());
592 >            }});
593 >
594 >        await(pleaseInterrupt);
595 >        assertThreadStaysAlive(t);
596 >        t.interrupt();
597 >        awaitTermination(t);
598 >        assertEquals(SIZE, q.size());
599 >        assertEquals(0, q.remainingCapacity());
600      }
601  
602      /**
603 <     * put blocks waiting for take when full
603 >     * put blocks interruptibly waiting for take when full
604       */
605 <    public void testPutWithTake() {
606 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
607 <        Thread t = new Thread(new Runnable() {
608 <                public void run() {
609 <                    int added = 0;
610 <                    try {
611 <                        q.put(new Object());
612 <                        ++added;
613 <                        q.put(new Object());
614 <                        ++added;
615 <                        q.put(new Object());
616 <                        ++added;
617 <                        q.put(new Object());
618 <                        ++added;
619 <                        threadShouldThrow();
620 <                    } catch (InterruptedException e) {
621 <                        threadAssertTrue(added >= 2);
622 <                    }
623 <                }
624 <            });
625 <        try {
626 <            t.start();
627 <            Thread.sleep(SHORT_DELAY_MS);
628 <            q.take();
629 <            t.interrupt();
630 <            t.join();
631 <        } catch (Exception e) {
632 <            unexpectedException();
633 <        }
605 >    public void testPutWithTake() throws InterruptedException {
606 >        final int capacity = 2;
607 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
608 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
609 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
610 >        Thread t = newStartedThread(new CheckedRunnable() {
611 >            public void realRun() throws InterruptedException {
612 >                for (int i = 0; i < capacity; i++)
613 >                    q.put(i);
614 >                pleaseTake.countDown();
615 >                q.put(86);
616 >
617 >                pleaseInterrupt.countDown();
618 >                try {
619 >                    q.put(99);
620 >                    shouldThrow();
621 >                } catch (InterruptedException success) {}
622 >                assertFalse(Thread.interrupted());
623 >            }});
624 >
625 >        await(pleaseTake);
626 >        assertEquals(0, q.remainingCapacity());
627 >        assertEquals(0, q.take());
628 >
629 >        await(pleaseInterrupt);
630 >        assertThreadStaysAlive(t);
631 >        t.interrupt();
632 >        awaitTermination(t);
633 >        assertEquals(0, q.remainingCapacity());
634      }
635  
636      /**
637       * timed offer times out if full and elements not taken
638       */
639 <    public void testTimedOffer() {
639 >    public void testTimedOffer() throws InterruptedException {
640          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
641 <        Thread t = new Thread(new Runnable() {
642 <                public void run() {
643 <                    try {
644 <                        q.put(new Object());
645 <                        q.put(new Object());
646 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
647 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
648 <                        threadShouldThrow();
649 <                    } catch (InterruptedException success) {}
650 <                }
651 <            });
652 <
653 <        try {
654 <            t.start();
655 <            Thread.sleep(SMALL_DELAY_MS);
656 <            t.interrupt();
657 <            t.join();
658 <        } catch (Exception e) {
659 <            unexpectedException();
684 <        }
641 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
642 >        Thread t = newStartedThread(new CheckedRunnable() {
643 >            public void realRun() throws InterruptedException {
644 >                q.put(new Object());
645 >                q.put(new Object());
646 >                long startTime = System.nanoTime();
647 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
648 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
649 >                pleaseInterrupt.countDown();
650 >                try {
651 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
652 >                    shouldThrow();
653 >                } catch (InterruptedException success) {}
654 >            }});
655 >
656 >        await(pleaseInterrupt);
657 >        assertThreadStaysAlive(t);
658 >        t.interrupt();
659 >        awaitTermination(t);
660      }
661  
662      /**
663       * take retrieves elements in FIFO order
664       */
665 <    public void testTake() {
666 <        try {
667 <            LinkedBlockingDeque q = populatedDeque(SIZE);
668 <            for (int i = 0; i < SIZE; ++i) {
669 <                assertEquals(i, ((Integer)q.take()).intValue());
695 <            }
696 <        } catch (InterruptedException e) {
697 <            unexpectedException();
698 <        }
665 >    public void testTake() throws InterruptedException {
666 >        LinkedBlockingDeque q = populatedDeque(SIZE);
667 >        for (int i = 0; i < SIZE; ++i) {
668 >            assertEquals(i, q.take());
669 >        }
670      }
671  
672      /**
673 <     * take blocks interruptibly when empty
673 >     * take removes existing elements until empty, then blocks interruptibly
674       */
675 <    public void testTakeFromEmpty() {
676 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
677 <        Thread t = new Thread(new Runnable() {
678 <                public void run() {
679 <                    try {
680 <                        q.take();
681 <                        threadShouldThrow();
711 <                    } catch (InterruptedException success) { }
675 >    public void testBlockingTake() throws InterruptedException {
676 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
677 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
678 >        Thread t = newStartedThread(new CheckedRunnable() {
679 >            public void realRun() throws InterruptedException {
680 >                for (int i = 0; i < SIZE; ++i) {
681 >                    assertEquals(i, q.take());
682                  }
713            });
714        try {
715            t.start();
716            Thread.sleep(SHORT_DELAY_MS);
717            t.interrupt();
718            t.join();
719        } catch (Exception e) {
720            unexpectedException();
721        }
722    }
683  
684 <    /**
685 <     * Take removes existing elements until empty, then blocks interruptibly
686 <     */
687 <    public void testBlockingTake() {
688 <        Thread t = new Thread(new Runnable() {
689 <                public void run() {
730 <                    try {
731 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
732 <                        for (int i = 0; i < SIZE; ++i) {
733 <                            assertEquals(i, ((Integer)q.take()).intValue());
734 <                        }
735 <                        q.take();
736 <                        threadShouldThrow();
737 <                    } catch (InterruptedException success) {
738 <                    }
739 <                }});
740 <        t.start();
741 <        try {
742 <           Thread.sleep(SHORT_DELAY_MS);
743 <           t.interrupt();
744 <           t.join();
745 <        }
746 <        catch (InterruptedException ie) {
747 <            unexpectedException();
748 <        }
749 <    }
684 >                Thread.currentThread().interrupt();
685 >                try {
686 >                    q.take();
687 >                    shouldThrow();
688 >                } catch (InterruptedException success) {}
689 >                assertFalse(Thread.interrupted());
690  
691 +                pleaseInterrupt.countDown();
692 +                try {
693 +                    q.take();
694 +                    shouldThrow();
695 +                } catch (InterruptedException success) {}
696 +                assertFalse(Thread.interrupted());
697 +            }});
698 +
699 +        await(pleaseInterrupt);
700 +        assertThreadStaysAlive(t);
701 +        t.interrupt();
702 +        awaitTermination(t);
703 +    }
704  
705      /**
706       * poll succeeds unless empty
# Line 755 | Line 708 | public class LinkedBlockingDequeTest ext
708      public void testPoll() {
709          LinkedBlockingDeque q = populatedDeque(SIZE);
710          for (int i = 0; i < SIZE; ++i) {
711 <            assertEquals(i, ((Integer)q.poll()).intValue());
711 >            assertEquals(i, q.poll());
712          }
713 <        assertNull(q.poll());
713 >        assertNull(q.poll());
714      }
715  
716      /**
717       * timed poll with zero timeout succeeds when non-empty, else times out
718       */
719 <    public void testTimedPoll0() {
720 <        try {
721 <            LinkedBlockingDeque q = populatedDeque(SIZE);
722 <            for (int i = 0; i < SIZE; ++i) {
723 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
724 <            }
772 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
773 <        } catch (InterruptedException e) {
774 <            unexpectedException();
775 <        }
719 >    public void testTimedPoll0() throws InterruptedException {
720 >        LinkedBlockingDeque q = populatedDeque(SIZE);
721 >        for (int i = 0; i < SIZE; ++i) {
722 >            assertEquals(i, q.poll(0, MILLISECONDS));
723 >        }
724 >        assertNull(q.poll(0, MILLISECONDS));
725      }
726  
727      /**
728       * timed poll with nonzero timeout succeeds when non-empty, else times out
729       */
730 <    public void testTimedPoll() {
731 <        try {
732 <            LinkedBlockingDeque q = populatedDeque(SIZE);
733 <            for (int i = 0; i < SIZE; ++i) {
734 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
735 <            }
736 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
737 <        } catch (InterruptedException e) {
738 <            unexpectedException();
739 <        }
730 >    public void testTimedPoll() throws InterruptedException {
731 >        LinkedBlockingDeque q = populatedDeque(SIZE);
732 >        for (int i = 0; i < SIZE; ++i) {
733 >            long startTime = System.nanoTime();
734 >            assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
735 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
736 >        }
737 >        long startTime = System.nanoTime();
738 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
739 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
740 >        checkEmpty(q);
741      }
742  
743      /**
744       * Interrupted timed poll throws InterruptedException instead of
745       * returning timeout status
746       */
747 <    public void testInterruptedTimedPoll() {
748 <        Thread t = new Thread(new Runnable() {
749 <                public void run() {
750 <                    try {
751 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
752 <                        for (int i = 0; i < SIZE; ++i) {
753 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
754 <                        }
755 <                        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) { }
747 >    public void testInterruptedTimedPoll() throws InterruptedException {
748 >        final BlockingQueue<Integer> q = populatedDeque(SIZE);
749 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
750 >        Thread t = newStartedThread(new CheckedRunnable() {
751 >            public void realRun() throws InterruptedException {
752 >                for (int i = 0; i < SIZE; ++i) {
753 >                    long t0 = System.nanoTime();
754 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
755 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
756                  }
757 <            });
758 <        try {
759 <            t.start();
760 <            Thread.sleep(SMALL_DELAY_MS);
761 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
762 <            t.interrupt();
763 <            t.join();
764 <        } catch (Exception e) {
765 <            unexpectedException();
766 <        }
757 >                long t0 = System.nanoTime();
758 >                aboutToWait.countDown();
759 >                try {
760 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
761 >                    shouldThrow();
762 >                } catch (InterruptedException success) {
763 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
764 >                }
765 >            }});
766 >
767 >        aboutToWait.await();
768 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
769 >        t.interrupt();
770 >        awaitTermination(t, MEDIUM_DELAY_MS);
771 >        checkEmpty(q);
772      }
773  
847
774      /**
775       * putFirst(null) throws NPE
776       */
777 <     public void testPutFirstNull() {
778 <        try {
779 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
777 >    public void testPutFirstNull() throws InterruptedException {
778 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
779 >        try {
780              q.putFirst(null);
781              shouldThrow();
782 <        }
783 <        catch (NullPointerException success) {
858 <        }
859 <        catch (InterruptedException ie) {
860 <            unexpectedException();
861 <        }
862 <     }
782 >        } catch (NullPointerException success) {}
783 >    }
784  
785      /**
786       * all elements successfully putFirst are contained
787       */
788 <     public void testPutFirst() {
789 <         try {
790 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
791 <             for (int i = 0; i < SIZE; ++i) {
792 <                 Integer I = new Integer(i);
793 <                 q.putFirst(I);
873 <                 assertTrue(q.contains(I));
874 <             }
875 <             assertEquals(0, q.remainingCapacity());
876 <         }
877 <        catch (InterruptedException ie) {
878 <            unexpectedException();
788 >    public void testPutFirst() throws InterruptedException {
789 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
790 >        for (int i = 0; i < SIZE; ++i) {
791 >            Integer I = new Integer(i);
792 >            q.putFirst(I);
793 >            assertTrue(q.contains(I));
794          }
795 +        assertEquals(0, q.remainingCapacity());
796      }
797  
798      /**
799       * putFirst blocks interruptibly if full
800       */
801 <    public void testBlockingPutFirst() {
802 <        Thread t = new Thread(new Runnable() {
803 <                public void run() {
804 <                    int added = 0;
805 <                    try {
806 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
807 <                        for (int i = 0; i < SIZE; ++i) {
808 <                            q.putFirst(new Integer(i));
809 <                            ++added;
810 <                        }
811 <                        q.putFirst(new Integer(SIZE));
812 <                        threadShouldThrow();
813 <                    } catch (InterruptedException ie) {
814 <                        threadAssertEquals(added, SIZE);
815 <                    }
816 <                }});
817 <        t.start();
818 <        try {
819 <           Thread.sleep(SHORT_DELAY_MS);
820 <           t.interrupt();
821 <           t.join();
822 <        }
823 <        catch (InterruptedException ie) {
824 <            unexpectedException();
825 <        }
801 >    public void testBlockingPutFirst() throws InterruptedException {
802 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
803 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
804 >        Thread t = newStartedThread(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 >
811 >                Thread.currentThread().interrupt();
812 >                try {
813 >                    q.putFirst(99);
814 >                    shouldThrow();
815 >                } catch (InterruptedException success) {}
816 >                assertFalse(Thread.interrupted());
817 >
818 >                pleaseInterrupt.countDown();
819 >                try {
820 >                    q.putFirst(99);
821 >                    shouldThrow();
822 >                } catch (InterruptedException success) {}
823 >                assertFalse(Thread.interrupted());
824 >            }});
825 >
826 >        await(pleaseInterrupt);
827 >        assertThreadStaysAlive(t);
828 >        t.interrupt();
829 >        awaitTermination(t);
830 >        assertEquals(SIZE, q.size());
831 >        assertEquals(0, q.remainingCapacity());
832      }
833  
834      /**
835 <     * putFirst blocks waiting for take when full
835 >     * putFirst blocks interruptibly waiting for take when full
836       */
837 <    public void testPutFirstWithTake() {
838 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
839 <        Thread t = new Thread(new Runnable() {
840 <                public void run() {
841 <                    int added = 0;
842 <                    try {
843 <                        q.putFirst(new Object());
844 <                        ++added;
845 <                        q.putFirst(new Object());
846 <                        ++added;
847 <                        q.putFirst(new Object());
848 <                        ++added;
849 <                        q.putFirst(new Object());
850 <                        ++added;
851 <                        threadShouldThrow();
852 <                    } catch (InterruptedException e) {
853 <                        threadAssertTrue(added >= 2);
854 <                    }
855 <                }
856 <            });
857 <        try {
858 <            t.start();
859 <            Thread.sleep(SHORT_DELAY_MS);
860 <            q.take();
861 <            t.interrupt();
862 <            t.join();
863 <        } catch (Exception e) {
864 <            unexpectedException();
865 <        }
837 >    public void testPutFirstWithTake() throws InterruptedException {
838 >        final int capacity = 2;
839 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
840 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
841 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
842 >        Thread t = newStartedThread(new CheckedRunnable() {
843 >            public void realRun() throws InterruptedException {
844 >                for (int i = 0; i < capacity; i++)
845 >                    q.putFirst(i);
846 >                pleaseTake.countDown();
847 >                q.putFirst(86);
848 >
849 >                pleaseInterrupt.countDown();
850 >                try {
851 >                    q.putFirst(99);
852 >                    shouldThrow();
853 >                } catch (InterruptedException success) {}
854 >                assertFalse(Thread.interrupted());
855 >            }});
856 >
857 >        await(pleaseTake);
858 >        assertEquals(0, q.remainingCapacity());
859 >        assertEquals(capacity - 1, q.take());
860 >
861 >        await(pleaseInterrupt);
862 >        assertThreadStaysAlive(t);
863 >        t.interrupt();
864 >        awaitTermination(t);
865 >        assertEquals(0, q.remainingCapacity());
866      }
867  
868      /**
869       * timed offerFirst times out if full and elements not taken
870       */
871 <    public void testTimedOfferFirst() {
871 >    public void testTimedOfferFirst() throws InterruptedException {
872          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
873 <        Thread t = new Thread(new Runnable() {
874 <                public void run() {
875 <                    try {
876 <                        q.putFirst(new Object());
877 <                        q.putFirst(new Object());
878 <                        threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
879 <                        q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
880 <                        threadShouldThrow();
881 <                    } catch (InterruptedException success) {}
882 <                }
883 <            });
873 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
874 >        Thread t = newStartedThread(new CheckedRunnable() {
875 >            public void realRun() throws InterruptedException {
876 >                q.putFirst(new Object());
877 >                q.putFirst(new Object());
878 >                long startTime = System.nanoTime();
879 >                assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
880 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
881 >                pleaseInterrupt.countDown();
882 >                try {
883 >                    q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
884 >                    shouldThrow();
885 >                } catch (InterruptedException success) {}
886 >            }});
887 >
888 >        await(pleaseInterrupt);
889 >        assertThreadStaysAlive(t);
890 >        t.interrupt();
891 >        awaitTermination(t);
892 >    }
893  
894 <        try {
895 <            t.start();
896 <            Thread.sleep(SMALL_DELAY_MS);
897 <            t.interrupt();
898 <            t.join();
899 <        } catch (Exception e) {
900 <            unexpectedException();
894 >    /**
895 >     * take retrieves elements in FIFO order
896 >     */
897 >    public void testTakeFirst() throws InterruptedException {
898 >        LinkedBlockingDeque q = populatedDeque(SIZE);
899 >        for (int i = 0; i < SIZE; ++i) {
900 >            assertEquals(i, q.takeFirst());
901          }
902      }
903  
904      /**
905 <     * take retrieves elements in FIFO order
905 >     * takeFirst() blocks interruptibly when empty
906       */
907 <    public void testTakeFirst() {
908 <        try {
909 <            LinkedBlockingDeque q = populatedDeque(SIZE);
910 <            for (int i = 0; i < SIZE; ++i) {
911 <                assertEquals(i, ((Integer)q.takeFirst()).intValue());
912 <            }
913 <        } catch (InterruptedException e) {
914 <            unexpectedException();
915 <        }
907 >    public void testTakeFirstFromEmptyBlocksInterruptibly() {
908 >        final BlockingDeque q = new LinkedBlockingDeque();
909 >        final CountDownLatch threadStarted = new CountDownLatch(1);
910 >        Thread t = newStartedThread(new CheckedRunnable() {
911 >            public void realRun() {
912 >                threadStarted.countDown();
913 >                try {
914 >                    q.takeFirst();
915 >                    shouldThrow();
916 >                } catch (InterruptedException success) {}
917 >                assertFalse(Thread.interrupted());
918 >            }});
919 >
920 >        await(threadStarted);
921 >        assertThreadStaysAlive(t);
922 >        t.interrupt();
923 >        awaitTermination(t);
924 >    }
925 >
926 >    /**
927 >     * takeFirst() throws InterruptedException immediately if interrupted
928 >     * before waiting
929 >     */
930 >    public void testTakeFirstFromEmptyAfterInterrupt() {
931 >        final BlockingDeque q = new LinkedBlockingDeque();
932 >        Thread t = newStartedThread(new CheckedRunnable() {
933 >            public void realRun() {
934 >                Thread.currentThread().interrupt();
935 >                try {
936 >                    q.takeFirst();
937 >                    shouldThrow();
938 >                } catch (InterruptedException success) {}
939 >                assertFalse(Thread.interrupted());
940 >            }});
941 >
942 >        awaitTermination(t);
943      }
944  
945      /**
946 <     * takeFirst blocks interruptibly when empty
946 >     * takeLast() blocks interruptibly when empty
947       */
948 <    public void testTakeFirstFromEmpty() {
949 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
950 <        Thread t = new Thread(new Runnable() {
951 <                public void run() {
952 <                    try {
953 <                        q.takeFirst();
954 <                        threadShouldThrow();
955 <                    } catch (InterruptedException success) { }
956 <                }
957 <            });
958 <        try {
959 <            t.start();
960 <            Thread.sleep(SHORT_DELAY_MS);
961 <            t.interrupt();
962 <            t.join();
963 <        } catch (Exception e) {
964 <            unexpectedException();
1007 <        }
948 >    public void testTakeLastFromEmptyBlocksInterruptibly() {
949 >        final BlockingDeque q = new LinkedBlockingDeque();
950 >        final CountDownLatch threadStarted = new CountDownLatch(1);
951 >        Thread t = newStartedThread(new CheckedRunnable() {
952 >            public void realRun() {
953 >                threadStarted.countDown();
954 >                try {
955 >                    q.takeLast();
956 >                    shouldThrow();
957 >                } catch (InterruptedException success) {}
958 >                assertFalse(Thread.interrupted());
959 >            }});
960 >
961 >        await(threadStarted);
962 >        assertThreadStaysAlive(t);
963 >        t.interrupt();
964 >        awaitTermination(t);
965      }
966  
967      /**
968 <     * TakeFirst removes existing elements until empty, then blocks interruptibly
969 <     */
970 <    public void testBlockingTakeFirst() {
971 <        Thread t = new Thread(new Runnable() {
972 <                public void run() {
973 <                    try {
974 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
975 <                        for (int i = 0; i < SIZE; ++i) {
976 <                            assertEquals(i, ((Integer)q.takeFirst()).intValue());
977 <                        }
978 <                        q.takeFirst();
979 <                        threadShouldThrow();
980 <                    } catch (InterruptedException success) {
981 <                    }
982 <                }});
983 <        t.start();
1027 <        try {
1028 <           Thread.sleep(SHORT_DELAY_MS);
1029 <           t.interrupt();
1030 <           t.join();
1031 <        }
1032 <        catch (InterruptedException ie) {
1033 <            unexpectedException();
1034 <        }
968 >     * takeLast() throws InterruptedException immediately if interrupted
969 >     * before waiting
970 >     */
971 >    public void testTakeLastFromEmptyAfterInterrupt() {
972 >        final BlockingDeque q = new LinkedBlockingDeque();
973 >        Thread t = newStartedThread(new CheckedRunnable() {
974 >            public void realRun() {
975 >                Thread.currentThread().interrupt();
976 >                try {
977 >                    q.takeLast();
978 >                    shouldThrow();
979 >                } catch (InterruptedException success) {}
980 >                assertFalse(Thread.interrupted());
981 >            }});
982 >
983 >        awaitTermination(t);
984      }
985  
986 +    /**
987 +     * takeFirst removes existing elements until empty, then blocks interruptibly
988 +     */
989 +    public void testBlockingTakeFirst() throws InterruptedException {
990 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
991 +        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
992 +        Thread t = newStartedThread(new CheckedRunnable() {
993 +            public void realRun() throws InterruptedException {
994 +                for (int i = 0; i < SIZE; ++i) {
995 +                    assertEquals(i, q.takeFirst());
996 +                }
997 +
998 +                Thread.currentThread().interrupt();
999 +                try {
1000 +                    q.takeFirst();
1001 +                    shouldThrow();
1002 +                } catch (InterruptedException success) {}
1003 +                assertFalse(Thread.interrupted());
1004 +
1005 +                pleaseInterrupt.countDown();
1006 +                try {
1007 +                    q.takeFirst();
1008 +                    shouldThrow();
1009 +                } catch (InterruptedException success) {}
1010 +                assertFalse(Thread.interrupted());
1011 +            }});
1012 +
1013 +        await(pleaseInterrupt);
1014 +        assertThreadStaysAlive(t);
1015 +        t.interrupt();
1016 +        awaitTermination(t);
1017 +    }
1018  
1019      /**
1020       * timed pollFirst with zero timeout succeeds when non-empty, else times out
1021       */
1022 <    public void testTimedPollFirst0() {
1023 <        try {
1024 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1025 <            for (int i = 0; i < SIZE; ++i) {
1026 <                assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue());
1027 <            }
1047 <            assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS));
1048 <        } catch (InterruptedException e) {
1049 <            unexpectedException();
1050 <        }
1022 >    public void testTimedPollFirst0() throws InterruptedException {
1023 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1024 >        for (int i = 0; i < SIZE; ++i) {
1025 >            assertEquals(i, q.pollFirst(0, MILLISECONDS));
1026 >        }
1027 >        assertNull(q.pollFirst(0, MILLISECONDS));
1028      }
1029  
1030      /**
1031       * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
1032       */
1033 <    public void testTimedPollFirst() {
1034 <        try {
1035 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1036 <            for (int i = 0; i < SIZE; ++i) {
1037 <                assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1038 <            }
1039 <            assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1040 <        } catch (InterruptedException e) {
1041 <            unexpectedException();
1042 <        }
1033 >    public void testTimedPollFirst() throws InterruptedException {
1034 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1035 >        for (int i = 0; i < SIZE; ++i) {
1036 >            long startTime = System.nanoTime();
1037 >            assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1038 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1039 >        }
1040 >        long startTime = System.nanoTime();
1041 >        assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1042 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1043 >        checkEmpty(q);
1044      }
1045  
1046      /**
1047       * Interrupted timed pollFirst throws InterruptedException instead of
1048       * returning timeout status
1049       */
1050 <    public void testInterruptedTimedPollFirst() {
1051 <        Thread t = new Thread(new Runnable() {
1052 <                public void run() {
1053 <                    try {
1054 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1055 <                        for (int i = 0; i < SIZE; ++i) {
1056 <                            threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1057 <                        }
1058 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1059 <                    } catch (InterruptedException success) {
1060 <                    }
1061 <                }});
1062 <        t.start();
1063 <        try {
1064 <           Thread.sleep(SHORT_DELAY_MS);
1065 <           t.interrupt();
1066 <           t.join();
1067 <        }
1068 <        catch (InterruptedException ie) {
1069 <            unexpectedException();
1070 <        }
1050 >    public void testInterruptedTimedPollFirst() throws InterruptedException {
1051 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1052 >        Thread t = newStartedThread(new CheckedRunnable() {
1053 >            public void realRun() throws InterruptedException {
1054 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1055 >                for (int i = 0; i < SIZE; ++i) {
1056 >                    assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1057 >                }
1058 >
1059 >                Thread.currentThread().interrupt();
1060 >                try {
1061 >                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1062 >                    shouldThrow();
1063 >                } catch (InterruptedException success) {}
1064 >                assertFalse(Thread.interrupted());
1065 >
1066 >                pleaseInterrupt.countDown();
1067 >                try {
1068 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1069 >                    shouldThrow();
1070 >                } catch (InterruptedException success) {}
1071 >                assertFalse(Thread.interrupted());
1072 >            }});
1073 >
1074 >        await(pleaseInterrupt);
1075 >        assertThreadStaysAlive(t);
1076 >        t.interrupt();
1077 >        awaitTermination(t);
1078      }
1079  
1080      /**
1081 <     *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
1082 <     *  on interruption throws
1081 >     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
1082 >     * on interruption throws
1083       */
1084 <    public void testTimedPollFirstWithOfferFirst() {
1084 >    public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
1085          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1086 <        Thread t = new Thread(new Runnable() {
1087 <                public void run() {
1088 <                    try {
1089 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1090 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1091 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1092 <                        threadShouldThrow();
1093 <                    } catch (InterruptedException success) { }
1094 <                }
1095 <            });
1096 <        try {
1097 <            t.start();
1098 <            Thread.sleep(SMALL_DELAY_MS);
1099 <            assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1100 <            t.interrupt();
1101 <            t.join();
1102 <        } catch (Exception e) {
1103 <            unexpectedException();
1104 <        }
1086 >        final CheckedBarrier barrier = new CheckedBarrier(2);
1087 >        Thread t = newStartedThread(new CheckedRunnable() {
1088 >            public void realRun() throws InterruptedException {
1089 >                long startTime = System.nanoTime();
1090 >                assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1091 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1092 >
1093 >                barrier.await();
1094 >
1095 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1096 >
1097 >                Thread.currentThread().interrupt();
1098 >                try {
1099 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1100 >                    shouldThrow();
1101 >                } catch (InterruptedException success) {}
1102 >
1103 >                barrier.await();
1104 >                try {
1105 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1106 >                    shouldThrow();
1107 >                } catch (InterruptedException success) {}
1108 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1109 >            }});
1110 >
1111 >        barrier.await();
1112 >        long startTime = System.nanoTime();
1113 >        assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1114 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1115 >        barrier.await();
1116 >        assertThreadStaysAlive(t);
1117 >        t.interrupt();
1118 >        awaitTermination(t);
1119      }
1120  
1121      /**
1122       * putLast(null) throws NPE
1123       */
1124 <     public void testPutLastNull() {
1125 <        try {
1126 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1124 >    public void testPutLastNull() throws InterruptedException {
1125 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1126 >        try {
1127              q.putLast(null);
1128              shouldThrow();
1129 <        }
1130 <        catch (NullPointerException success) {
1132 <        }
1133 <        catch (InterruptedException ie) {
1134 <            unexpectedException();
1135 <        }
1136 <     }
1129 >        } catch (NullPointerException success) {}
1130 >    }
1131  
1132      /**
1133       * all elements successfully putLast are contained
1134       */
1135 <     public void testPutLast() {
1136 <         try {
1137 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1138 <             for (int i = 0; i < SIZE; ++i) {
1139 <                 Integer I = new Integer(i);
1140 <                 q.putLast(I);
1147 <                 assertTrue(q.contains(I));
1148 <             }
1149 <             assertEquals(0, q.remainingCapacity());
1150 <         }
1151 <        catch (InterruptedException ie) {
1152 <            unexpectedException();
1135 >    public void testPutLast() throws InterruptedException {
1136 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1137 >        for (int i = 0; i < SIZE; ++i) {
1138 >            Integer I = new Integer(i);
1139 >            q.putLast(I);
1140 >            assertTrue(q.contains(I));
1141          }
1142 +        assertEquals(0, q.remainingCapacity());
1143      }
1144  
1145      /**
1146       * putLast blocks interruptibly if full
1147       */
1148 <    public void testBlockingPutLast() {
1149 <        Thread t = new Thread(new Runnable() {
1150 <                public void run() {
1151 <                    int added = 0;
1152 <                    try {
1153 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1154 <                        for (int i = 0; i < SIZE; ++i) {
1155 <                            q.putLast(new Integer(i));
1156 <                            ++added;
1157 <                        }
1158 <                        q.putLast(new Integer(SIZE));
1159 <                        threadShouldThrow();
1160 <                    } catch (InterruptedException ie) {
1161 <                        threadAssertEquals(added, SIZE);
1162 <                    }
1163 <                }});
1164 <        t.start();
1165 <        try {
1166 <           Thread.sleep(SHORT_DELAY_MS);
1167 <           t.interrupt();
1168 <           t.join();
1169 <        }
1170 <        catch (InterruptedException ie) {
1171 <            unexpectedException();
1172 <        }
1148 >    public void testBlockingPutLast() throws InterruptedException {
1149 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1150 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1151 >        Thread t = newStartedThread(new CheckedRunnable() {
1152 >            public void realRun() throws InterruptedException {
1153 >                for (int i = 0; i < SIZE; ++i)
1154 >                    q.putLast(i);
1155 >                assertEquals(SIZE, q.size());
1156 >                assertEquals(0, q.remainingCapacity());
1157 >
1158 >                Thread.currentThread().interrupt();
1159 >                try {
1160 >                    q.putLast(99);
1161 >                    shouldThrow();
1162 >                } catch (InterruptedException success) {}
1163 >                assertFalse(Thread.interrupted());
1164 >
1165 >                pleaseInterrupt.countDown();
1166 >                try {
1167 >                    q.putLast(99);
1168 >                    shouldThrow();
1169 >                } catch (InterruptedException success) {}
1170 >                assertFalse(Thread.interrupted());
1171 >            }});
1172 >
1173 >        await(pleaseInterrupt);
1174 >        assertThreadStaysAlive(t);
1175 >        t.interrupt();
1176 >        awaitTermination(t);
1177 >        assertEquals(SIZE, q.size());
1178 >        assertEquals(0, q.remainingCapacity());
1179      }
1180  
1181      /**
1182 <     * putLast blocks waiting for take when full
1182 >     * putLast blocks interruptibly waiting for take when full
1183       */
1184 <    public void testPutLastWithTake() {
1185 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1186 <        Thread t = new Thread(new Runnable() {
1187 <                public void run() {
1188 <                    int added = 0;
1189 <                    try {
1190 <                        q.putLast(new Object());
1191 <                        ++added;
1192 <                        q.putLast(new Object());
1193 <                        ++added;
1194 <                        q.putLast(new Object());
1195 <                        ++added;
1196 <                        q.putLast(new Object());
1197 <                        ++added;
1198 <                        threadShouldThrow();
1199 <                    } catch (InterruptedException e) {
1200 <                        threadAssertTrue(added >= 2);
1201 <                    }
1202 <                }
1203 <            });
1204 <        try {
1205 <            t.start();
1206 <            Thread.sleep(SHORT_DELAY_MS);
1207 <            q.take();
1208 <            t.interrupt();
1209 <            t.join();
1210 <        } catch (Exception e) {
1211 <            unexpectedException();
1212 <        }
1184 >    public void testPutLastWithTake() throws InterruptedException {
1185 >        final int capacity = 2;
1186 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1187 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
1188 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1189 >        Thread t = newStartedThread(new CheckedRunnable() {
1190 >            public void realRun() throws InterruptedException {
1191 >                for (int i = 0; i < capacity; i++)
1192 >                    q.putLast(i);
1193 >                pleaseTake.countDown();
1194 >                q.putLast(86);
1195 >
1196 >                pleaseInterrupt.countDown();
1197 >                try {
1198 >                    q.putLast(99);
1199 >                    shouldThrow();
1200 >                } catch (InterruptedException success) {}
1201 >                assertFalse(Thread.interrupted());
1202 >            }});
1203 >
1204 >        await(pleaseTake);
1205 >        assertEquals(0, q.remainingCapacity());
1206 >        assertEquals(0, q.take());
1207 >
1208 >        await(pleaseInterrupt);
1209 >        assertThreadStaysAlive(t);
1210 >        t.interrupt();
1211 >        awaitTermination(t);
1212 >        assertEquals(0, q.remainingCapacity());
1213      }
1214  
1215      /**
1216       * timed offerLast times out if full and elements not taken
1217       */
1218 <    public void testTimedOfferLast() {
1218 >    public void testTimedOfferLast() throws InterruptedException {
1219          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1220 <        Thread t = new Thread(new Runnable() {
1221 <                public void run() {
1222 <                    try {
1223 <                        q.putLast(new Object());
1224 <                        q.putLast(new Object());
1225 <                        threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1226 <                        q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1227 <                        threadShouldThrow();
1228 <                    } catch (InterruptedException success) {}
1229 <                }
1230 <            });
1231 <
1232 <        try {
1233 <            t.start();
1234 <            Thread.sleep(SMALL_DELAY_MS);
1235 <            t.interrupt();
1236 <            t.join();
1237 <        } catch (Exception e) {
1238 <            unexpectedException();
1244 <        }
1220 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1221 >        Thread t = newStartedThread(new CheckedRunnable() {
1222 >            public void realRun() throws InterruptedException {
1223 >                q.putLast(new Object());
1224 >                q.putLast(new Object());
1225 >                long startTime = System.nanoTime();
1226 >                assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1227 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1228 >                pleaseInterrupt.countDown();
1229 >                try {
1230 >                    q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1231 >                    shouldThrow();
1232 >                } catch (InterruptedException success) {}
1233 >            }});
1234 >
1235 >        await(pleaseInterrupt);
1236 >        assertThreadStaysAlive(t);
1237 >        t.interrupt();
1238 >        awaitTermination(t);
1239      }
1240  
1241      /**
1242       * takeLast retrieves elements in FIFO order
1243       */
1244 <    public void testTakeLast() {
1245 <        try {
1246 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1247 <            for (int i = 0; i < SIZE; ++i) {
1248 <                assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1255 <            }
1256 <        } catch (InterruptedException e) {
1257 <            unexpectedException();
1258 <        }
1244 >    public void testTakeLast() throws InterruptedException {
1245 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1246 >        for (int i = 0; i < SIZE; ++i) {
1247 >            assertEquals(SIZE-i-1, q.takeLast());
1248 >        }
1249      }
1250  
1251      /**
1252 <     * takeLast blocks interruptibly when empty
1252 >     * takeLast removes existing elements until empty, then blocks interruptibly
1253       */
1254 <    public void testTakeLastFromEmpty() {
1255 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1256 <        Thread t = new Thread(new Runnable() {
1257 <                public void run() {
1258 <                    try {
1259 <                        q.takeLast();
1260 <                        threadShouldThrow();
1271 <                    } catch (InterruptedException success) { }
1254 >    public void testBlockingTakeLast() throws InterruptedException {
1255 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1256 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1257 >        Thread t = newStartedThread(new CheckedRunnable() {
1258 >            public void realRun() throws InterruptedException {
1259 >                for (int i = 0; i < SIZE; ++i) {
1260 >                    assertEquals(SIZE-i-1, q.takeLast());
1261                  }
1273            });
1274        try {
1275            t.start();
1276            Thread.sleep(SHORT_DELAY_MS);
1277            t.interrupt();
1278            t.join();
1279        } catch (Exception e) {
1280            unexpectedException();
1281        }
1282    }
1262  
1263 <    /**
1264 <     * TakeLast removes existing elements until empty, then blocks interruptibly
1265 <     */
1266 <    public void testBlockingTakeLast() {
1267 <        Thread t = new Thread(new Runnable() {
1268 <                public void run() {
1290 <                    try {
1291 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1292 <                        for (int i = 0; i < SIZE; ++i) {
1293 <                            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1294 <                        }
1295 <                        q.takeLast();
1296 <                        threadShouldThrow();
1297 <                    } catch (InterruptedException success) {
1298 <                    }
1299 <                }});
1300 <        t.start();
1301 <        try {
1302 <           Thread.sleep(SHORT_DELAY_MS);
1303 <           t.interrupt();
1304 <           t.join();
1305 <        }
1306 <        catch (InterruptedException ie) {
1307 <            unexpectedException();
1308 <        }
1309 <    }
1263 >                Thread.currentThread().interrupt();
1264 >                try {
1265 >                    q.takeLast();
1266 >                    shouldThrow();
1267 >                } catch (InterruptedException success) {}
1268 >                assertFalse(Thread.interrupted());
1269  
1270 +                pleaseInterrupt.countDown();
1271 +                try {
1272 +                    q.takeLast();
1273 +                    shouldThrow();
1274 +                } catch (InterruptedException success) {}
1275 +                assertFalse(Thread.interrupted());
1276 +            }});
1277 +
1278 +        await(pleaseInterrupt);
1279 +        assertThreadStaysAlive(t);
1280 +        t.interrupt();
1281 +        awaitTermination(t);
1282 +    }
1283  
1284      /**
1285       * timed pollLast with zero timeout succeeds when non-empty, else times out
1286       */
1287 <    public void testTimedPollLast0() {
1288 <        try {
1289 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1290 <            for (int i = 0; i < SIZE; ++i) {
1291 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue());
1292 <            }
1321 <            assertNull(q.pollLast(0, TimeUnit.MILLISECONDS));
1322 <        } catch (InterruptedException e) {
1323 <            unexpectedException();
1324 <        }
1287 >    public void testTimedPollLast0() throws InterruptedException {
1288 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1289 >        for (int i = 0; i < SIZE; ++i) {
1290 >            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1291 >        }
1292 >        assertNull(q.pollLast(0, MILLISECONDS));
1293      }
1294  
1295      /**
1296       * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1297       */
1298 <    public void testTimedPollLast() {
1299 <        try {
1300 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1301 <            for (int i = 0; i < SIZE; ++i) {
1302 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1303 <            }
1304 <            assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1305 <        } catch (InterruptedException e) {
1306 <            unexpectedException();
1307 <        }
1298 >    public void testTimedPollLast() throws InterruptedException {
1299 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1300 >        for (int i = 0; i < SIZE; ++i) {
1301 >            long startTime = System.nanoTime();
1302 >            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1303 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1304 >        }
1305 >        long startTime = System.nanoTime();
1306 >        assertNull(q.pollLast(timeoutMillis(), MILLISECONDS));
1307 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1308 >        checkEmpty(q);
1309      }
1310  
1311      /**
1312       * Interrupted timed pollLast throws InterruptedException instead of
1313       * returning timeout status
1314       */
1315 <    public void testInterruptedTimedPollLast() {
1316 <        Thread t = new Thread(new Runnable() {
1317 <                public void run() {
1318 <                    try {
1319 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1320 <                        for (int i = 0; i < SIZE; ++i) {
1321 <                            threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1322 <                        }
1323 <                        threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1324 <                    } catch (InterruptedException success) {
1325 <                    }
1326 <                }});
1327 <        t.start();
1328 <        try {
1329 <           Thread.sleep(SHORT_DELAY_MS);
1330 <           t.interrupt();
1331 <           t.join();
1332 <        }
1333 <        catch (InterruptedException ie) {
1334 <            unexpectedException();
1335 <        }
1315 >    public void testInterruptedTimedPollLast() throws InterruptedException {
1316 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1317 >        Thread t = newStartedThread(new CheckedRunnable() {
1318 >            public void realRun() throws InterruptedException {
1319 >                LinkedBlockingDeque q = populatedDeque(SIZE);
1320 >                for (int i = 0; i < SIZE; ++i) {
1321 >                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1322 >                }
1323 >
1324 >                Thread.currentThread().interrupt();
1325 >                try {
1326 >                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1327 >                    shouldThrow();
1328 >                } catch (InterruptedException success) {}
1329 >                assertFalse(Thread.interrupted());
1330 >
1331 >                pleaseInterrupt.countDown();
1332 >                try {
1333 >                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1334 >                    shouldThrow();
1335 >                } catch (InterruptedException success) {}
1336 >                assertFalse(Thread.interrupted());
1337 >            }});
1338 >
1339 >        await(pleaseInterrupt);
1340 >        assertThreadStaysAlive(t);
1341 >        t.interrupt();
1342 >        awaitTermination(t);
1343      }
1344  
1345      /**
1346 <     *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1347 <     *  on interruption throws
1346 >     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1347 >     * on interruption throws
1348       */
1349 <    public void testTimedPollWithOfferLast() {
1349 >    public void testTimedPollWithOfferLast() throws InterruptedException {
1350          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1351 <        Thread t = new Thread(new Runnable() {
1352 <                public void run() {
1353 <                    try {
1354 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1355 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1356 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1357 <                        threadShouldThrow();
1358 <                    } catch (InterruptedException success) { }
1359 <                }
1360 <            });
1385 <        try {
1386 <            t.start();
1387 <            Thread.sleep(SMALL_DELAY_MS);
1388 <            assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1389 <            t.interrupt();
1390 <            t.join();
1391 <        } catch (Exception e) {
1392 <            unexpectedException();
1393 <        }
1394 <    }
1351 >        final CheckedBarrier barrier = new CheckedBarrier(2);
1352 >        Thread t = newStartedThread(new CheckedRunnable() {
1353 >            public void realRun() throws InterruptedException {
1354 >                long startTime = System.nanoTime();
1355 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
1356 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1357 >
1358 >                barrier.await();
1359 >
1360 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1361  
1362 +                Thread.currentThread().interrupt();
1363 +                try {
1364 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1365 +                    shouldThrow();
1366 +                } catch (InterruptedException success) {}
1367 +                assertFalse(Thread.interrupted());
1368 +
1369 +                barrier.await();
1370 +                try {
1371 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1372 +                    shouldThrow();
1373 +                } catch (InterruptedException success) {}
1374 +                assertFalse(Thread.interrupted());
1375 +            }});
1376 +
1377 +        barrier.await();
1378 +        long startTime = System.nanoTime();
1379 +        assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS));
1380 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1381 +
1382 +        barrier.await();
1383 +        assertThreadStaysAlive(t);
1384 +        t.interrupt();
1385 +        awaitTermination(t);
1386 +    }
1387  
1388      /**
1389       * element returns next element, or throws NSEE if empty
# Line 1400 | Line 1391 | public class LinkedBlockingDequeTest ext
1391      public void testElement() {
1392          LinkedBlockingDeque q = populatedDeque(SIZE);
1393          for (int i = 0; i < SIZE; ++i) {
1394 <            assertEquals(i, ((Integer)q.element()).intValue());
1394 >            assertEquals(i, q.element());
1395              q.poll();
1396          }
1397          try {
1398              q.element();
1399              shouldThrow();
1400 <        }
1410 <        catch (NoSuchElementException success) {}
1411 <    }
1412 <
1413 <    /**
1414 <     * remove(x) removes x and returns true if present
1415 <     */
1416 <    public void testRemoveElement() {
1417 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1418 <        for (int i = 1; i < SIZE; i+=2) {
1419 <            assertTrue(q.remove(new Integer(i)));
1420 <        }
1421 <        for (int i = 0; i < SIZE; i+=2) {
1422 <            assertTrue(q.remove(new Integer(i)));
1423 <            assertFalse(q.remove(new Integer(i+1)));
1424 <        }
1425 <        assertTrue(q.isEmpty());
1400 >        } catch (NoSuchElementException success) {}
1401      }
1402  
1403      /**
# Line 1503 | Line 1478 | public class LinkedBlockingDequeTest ext
1478      }
1479  
1480      /**
1481 <     * toArray contains all elements
1481 >     * toArray contains all elements in FIFO order
1482       */
1483 <    public void testToArray() {
1483 >    public void testToArray() throws InterruptedException {
1484          LinkedBlockingDeque q = populatedDeque(SIZE);
1485 <        Object[] o = q.toArray();
1486 <        try {
1487 <        for (int i = 0; i < o.length; i++)
1513 <            assertEquals(o[i], q.take());
1514 <        } catch (InterruptedException e) {
1515 <            unexpectedException();
1516 <        }
1485 >        Object[] o = q.toArray();
1486 >        for (int i = 0; i < o.length; i++)
1487 >            assertSame(o[i], q.poll());
1488      }
1489  
1490      /**
1491 <     * toArray(a) contains all elements
1491 >     * toArray(a) contains all elements in FIFO order
1492       */
1493      public void testToArray2() {
1494 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1495 <        Integer[] ints = new Integer[SIZE];
1496 <        ints = (Integer[])q.toArray(ints);
1497 <        try {
1498 <            for (int i = 0; i < ints.length; i++)
1499 <                assertEquals(ints[i], q.take());
1529 <        } catch (InterruptedException e) {
1530 <            unexpectedException();
1531 <        }
1532 <    }
1533 <
1534 <    /**
1535 <     * toArray(null) throws NPE
1536 <     */
1537 <    public void testToArray_BadArg() {
1538 <        try {
1539 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1540 <            Object o[] = q.toArray(null);
1541 <            shouldThrow();
1542 <        } catch (NullPointerException success) {}
1494 >        LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1495 >        Integer[] ints = new Integer[SIZE];
1496 >        Integer[] array = q.toArray(ints);
1497 >        assertSame(ints, array);
1498 >        for (int i = 0; i < ints.length; i++)
1499 >            assertSame(ints[i], q.remove());
1500      }
1501  
1502      /**
1503 <     * toArray with incompatible array type throws CCE
1503 >     * toArray(incompatible array type) throws ArrayStoreException
1504       */
1505      public void testToArray1_BadArg() {
1506 <        try {
1507 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1508 <            Object o[] = q.toArray(new String[10] );
1509 <            shouldThrow();
1510 <        } catch (ArrayStoreException  success) {}
1506 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1507 >        try {
1508 >            q.toArray(new String[10]);
1509 >            shouldThrow();
1510 >        } catch (ArrayStoreException success) {}
1511      }
1512  
1556
1513      /**
1514       * iterator iterates through all elements
1515       */
1516 <    public void testIterator() {
1516 >    public void testIterator() throws InterruptedException {
1517          LinkedBlockingDeque q = populatedDeque(SIZE);
1518 <        Iterator it = q.iterator();
1519 <        try {
1520 <            while (it.hasNext()) {
1521 <                assertEquals(it.next(), q.take());
1566 <            }
1567 <        } catch (InterruptedException e) {
1568 <            unexpectedException();
1569 <        }
1518 >        Iterator it = q.iterator();
1519 >        while (it.hasNext()) {
1520 >            assertEquals(it.next(), q.take());
1521 >        }
1522      }
1523  
1524      /**
1525       * iterator.remove removes current element
1526       */
1527 <    public void testIteratorRemove () {
1527 >    public void testIteratorRemove() {
1528          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1529          q.add(two);
1530          q.add(one);
# Line 1583 | Line 1535 | public class LinkedBlockingDequeTest ext
1535          it.remove();
1536  
1537          it = q.iterator();
1538 <        assertEquals(it.next(), one);
1539 <        assertEquals(it.next(), three);
1538 >        assertSame(it.next(), one);
1539 >        assertSame(it.next(), three);
1540          assertFalse(it.hasNext());
1541      }
1542  
1591
1543      /**
1544       * iterator ordering is FIFO
1545       */
# Line 1600 | Line 1551 | public class LinkedBlockingDequeTest ext
1551          assertEquals(0, q.remainingCapacity());
1552          int k = 0;
1553          for (Iterator it = q.iterator(); it.hasNext();) {
1554 <            int i = ((Integer)(it.next())).intValue();
1604 <            assertEquals(++k, i);
1554 >            assertEquals(++k, it.next());
1555          }
1556          assertEquals(3, k);
1557      }
# Line 1609 | Line 1559 | public class LinkedBlockingDequeTest ext
1559      /**
1560       * Modifications do not cause iterators to fail
1561       */
1562 <    public void testWeaklyConsistentIteration () {
1562 >    public void testWeaklyConsistentIteration() {
1563          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1564          q.add(one);
1565          q.add(two);
1566          q.add(three);
1567 <        try {
1568 <            for (Iterator it = q.iterator(); it.hasNext();) {
1569 <                q.remove();
1620 <                it.next();
1621 <            }
1622 <        }
1623 <        catch (ConcurrentModificationException e) {
1624 <            unexpectedException();
1567 >        for (Iterator it = q.iterator(); it.hasNext();) {
1568 >            q.remove();
1569 >            it.next();
1570          }
1571          assertEquals(0, q.size());
1572      }
1573  
1629
1574      /**
1575 <     *  Descending iterator iterates through all elements
1575 >     * Descending iterator iterates through all elements
1576       */
1577      public void testDescendingIterator() {
1578          LinkedBlockingDeque q = populatedDeque(SIZE);
1579          int i = 0;
1580 <        Iterator it = q.descendingIterator();
1580 >        Iterator it = q.descendingIterator();
1581          while (it.hasNext()) {
1582              assertTrue(q.contains(it.next()));
1583              ++i;
# Line 1642 | Line 1586 | public class LinkedBlockingDequeTest ext
1586          assertFalse(it.hasNext());
1587          try {
1588              it.next();
1589 <        } catch (NoSuchElementException success) {
1590 <        }
1589 >            shouldThrow();
1590 >        } catch (NoSuchElementException success) {}
1591      }
1592  
1593      /**
1594 <     *  Descending iterator ordering is reverse FIFO
1594 >     * Descending iterator ordering is reverse FIFO
1595       */
1596      public void testDescendingIteratorOrdering() {
1597          final LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1657 | Line 1601 | public class LinkedBlockingDequeTest ext
1601              q.add(new Integer(1));
1602              int k = 0;
1603              for (Iterator it = q.descendingIterator(); it.hasNext();) {
1604 <                int i = ((Integer)(it.next())).intValue();
1661 <                assertEquals(++k, i);
1604 >                assertEquals(++k, it.next());
1605              }
1606  
1607              assertEquals(3, k);
# Line 1671 | Line 1614 | public class LinkedBlockingDequeTest ext
1614      /**
1615       * descendingIterator.remove removes current element
1616       */
1617 <    public void testDescendingIteratorRemove () {
1617 >    public void testDescendingIteratorRemove() {
1618          final LinkedBlockingDeque q = new LinkedBlockingDeque();
1619          for (int iters = 0; iters < 100; ++iters) {
1620              q.add(new Integer(3));
# Line 1690 | Line 1633 | public class LinkedBlockingDequeTest ext
1633          }
1634      }
1635  
1693
1636      /**
1637       * toString contains toStrings of elements
1638       */
# Line 1698 | Line 1640 | public class LinkedBlockingDequeTest ext
1640          LinkedBlockingDeque q = populatedDeque(SIZE);
1641          String s = q.toString();
1642          for (int i = 0; i < SIZE; ++i) {
1643 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1643 >            assertTrue(s.contains(String.valueOf(i)));
1644          }
1645      }
1646  
1705
1647      /**
1648       * offer transfers elements across Executor tasks
1649       */
# Line 1711 | Line 1652 | public class LinkedBlockingDequeTest ext
1652          q.add(one);
1653          q.add(two);
1654          ExecutorService executor = Executors.newFixedThreadPool(2);
1655 <        executor.execute(new Runnable() {
1656 <            public void run() {
1657 <                threadAssertFalse(q.offer(three));
1658 <                try {
1659 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1660 <                    threadAssertEquals(0, q.remainingCapacity());
1661 <                }
1662 <                catch (InterruptedException e) {
1663 <                    threadUnexpectedException();
1664 <                }
1665 <            }
1666 <        });
1667 <
1668 <        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 <        });
1655 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1656 >        executor.execute(new CheckedRunnable() {
1657 >            public void realRun() throws InterruptedException {
1658 >                assertFalse(q.offer(three));
1659 >                threadsStarted.await();
1660 >                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1661 >                assertEquals(0, q.remainingCapacity());
1662 >            }});
1663 >
1664 >        executor.execute(new CheckedRunnable() {
1665 >            public void realRun() throws InterruptedException {
1666 >                threadsStarted.await();
1667 >                assertSame(one, q.take());
1668 >            }});
1669  
1670          joinPool(executor);
1671      }
1672  
1673      /**
1674 <     * poll retrieves elements across Executor threads
1674 >     * timed poll retrieves elements across Executor threads
1675       */
1676      public void testPollInExecutor() {
1677          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1678 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1679          ExecutorService executor = Executors.newFixedThreadPool(2);
1680 <        executor.execute(new Runnable() {
1681 <            public void run() {
1682 <                threadAssertNull(q.poll());
1683 <                try {
1684 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1685 <                    threadAssertTrue(q.isEmpty());
1686 <                }
1687 <                catch (InterruptedException e) {
1688 <                    threadUnexpectedException();
1689 <                }
1690 <            }
1691 <        });
1692 <
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 <        });
1680 >        executor.execute(new CheckedRunnable() {
1681 >            public void realRun() throws InterruptedException {
1682 >                assertNull(q.poll());
1683 >                threadsStarted.await();
1684 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1685 >                checkEmpty(q);
1686 >            }});
1687 >
1688 >        executor.execute(new CheckedRunnable() {
1689 >            public void realRun() throws InterruptedException {
1690 >                threadsStarted.await();
1691 >                q.put(one);
1692 >            }});
1693  
1694          joinPool(executor);
1695      }
# Line 1776 | Line 1697 | public class LinkedBlockingDequeTest ext
1697      /**
1698       * A deserialized serialized deque has same elements in same order
1699       */
1700 <    public void testSerialization() {
1701 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1702 <
1703 <        try {
1704 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1705 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1706 <            out.writeObject(q);
1707 <            out.close();
1708 <
1709 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1710 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1790 <            LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1791 <            assertEquals(q.size(), r.size());
1792 <            while (!q.isEmpty())
1793 <                assertEquals(q.remove(), r.remove());
1794 <        } catch (Exception e) {
1795 <            unexpectedException();
1796 <        }
1797 <    }
1798 <
1799 <    /**
1800 <     * drainTo(null) throws NPE
1801 <     */
1802 <    public void testDrainToNull() {
1803 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1804 <        try {
1805 <            q.drainTo(null);
1806 <            shouldThrow();
1807 <        } catch (NullPointerException success) {
1808 <        }
1809 <    }
1810 <
1811 <    /**
1812 <     * drainTo(this) throws IAE
1813 <     */
1814 <    public void testDrainToSelf() {
1815 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1816 <        try {
1817 <            q.drainTo(q);
1818 <            shouldThrow();
1819 <        } catch (IllegalArgumentException success) {
1700 >    public void testSerialization() throws Exception {
1701 >        Queue x = populatedDeque(SIZE);
1702 >        Queue y = serialClone(x);
1703 >
1704 >        assertNotSame(y, x);
1705 >        assertEquals(x.size(), y.size());
1706 >        assertEquals(x.toString(), y.toString());
1707 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1708 >        while (!x.isEmpty()) {
1709 >            assertFalse(y.isEmpty());
1710 >            assertEquals(x.remove(), y.remove());
1711          }
1712 +        assertTrue(y.isEmpty());
1713      }
1714  
1715      /**
# Line 1827 | Line 1719 | public class LinkedBlockingDequeTest ext
1719          LinkedBlockingDeque q = populatedDeque(SIZE);
1720          ArrayList l = new ArrayList();
1721          q.drainTo(l);
1722 <        assertEquals(q.size(), 0);
1723 <        assertEquals(l.size(), SIZE);
1722 >        assertEquals(0, q.size());
1723 >        assertEquals(SIZE, l.size());
1724          for (int i = 0; i < SIZE; ++i)
1725              assertEquals(l.get(i), new Integer(i));
1726          q.add(zero);
# Line 1838 | Line 1730 | public class LinkedBlockingDequeTest ext
1730          assertTrue(q.contains(one));
1731          l.clear();
1732          q.drainTo(l);
1733 <        assertEquals(q.size(), 0);
1734 <        assertEquals(l.size(), 2);
1733 >        assertEquals(0, q.size());
1734 >        assertEquals(2, l.size());
1735          for (int i = 0; i < 2; ++i)
1736              assertEquals(l.get(i), new Integer(i));
1737      }
# Line 1847 | Line 1739 | public class LinkedBlockingDequeTest ext
1739      /**
1740       * drainTo empties full deque, unblocking a waiting put.
1741       */
1742 <    public void testDrainToWithActivePut() {
1742 >    public void testDrainToWithActivePut() throws InterruptedException {
1743          final LinkedBlockingDeque q = populatedDeque(SIZE);
1744 <        Thread t = new Thread(new Runnable() {
1745 <                public void run() {
1746 <                    try {
1747 <                        q.put(new Integer(SIZE+1));
1856 <                    } catch (InterruptedException ie) {
1857 <                        threadUnexpectedException();
1858 <                    }
1859 <                }
1860 <            });
1861 <        try {
1862 <            t.start();
1863 <            ArrayList l = new ArrayList();
1864 <            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 <        }
1873 <    }
1874 <
1875 <    /**
1876 <     * drainTo(null, n) throws NPE
1877 <     */
1878 <    public void testDrainToNullN() {
1879 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1880 <        try {
1881 <            q.drainTo(null, 0);
1882 <            shouldThrow();
1883 <        } catch (NullPointerException success) {
1884 <        }
1885 <    }
1744 >        Thread t = new Thread(new CheckedRunnable() {
1745 >            public void realRun() throws InterruptedException {
1746 >                q.put(new Integer(SIZE+1));
1747 >            }});
1748  
1749 <    /**
1750 <     * drainTo(this, n) throws IAE
1751 <     */
1752 <    public void testDrainToSelfN() {
1753 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1754 <        try {
1755 <            q.drainTo(q, 0);
1756 <            shouldThrow();
1895 <        } catch (IllegalArgumentException success) {
1896 <        }
1749 >        t.start();
1750 >        ArrayList l = new ArrayList();
1751 >        q.drainTo(l);
1752 >        assertTrue(l.size() >= SIZE);
1753 >        for (int i = 0; i < SIZE; ++i)
1754 >            assertEquals(l.get(i), new Integer(i));
1755 >        t.join();
1756 >        assertTrue(q.size() + l.size() >= SIZE);
1757      }
1758  
1759      /**
1760 <     * drainTo(c, n) empties first max {n, size} elements of deque into c
1760 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
1761       */
1762      public void testDrainToN() {
1763          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1906 | Line 1766 | public class LinkedBlockingDequeTest ext
1766                  assertTrue(q.offer(new Integer(j)));
1767              ArrayList l = new ArrayList();
1768              q.drainTo(l, i);
1769 <            int k = (i < SIZE)? i : SIZE;
1770 <            assertEquals(l.size(), k);
1771 <            assertEquals(q.size(), SIZE-k);
1769 >            int k = (i < SIZE) ? i : SIZE;
1770 >            assertEquals(k, l.size());
1771 >            assertEquals(SIZE-k, q.size());
1772              for (int j = 0; j < k; ++j)
1773                  assertEquals(l.get(j), new Integer(j));
1774              while (q.poll() != null) ;
1775          }
1776      }
1777  
1778 +    /**
1779 +     * remove(null), contains(null) always return false
1780 +     */
1781 +    public void testNeverContainsNull() {
1782 +        Deque<?>[] qs = {
1783 +            new LinkedBlockingDeque<Object>(),
1784 +            populatedDeque(2),
1785 +        };
1786 +
1787 +        for (Deque<?> q : qs) {
1788 +            assertFalse(q.contains(null));
1789 +            assertFalse(q.remove(null));
1790 +            assertFalse(q.removeFirstOccurrence(null));
1791 +            assertFalse(q.removeLastOccurrence(null));
1792 +        }
1793 +    }
1794 +
1795   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines