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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines