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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines