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.3 by dl, Fri Sep 16 11:16:03 2005 UTC vs.
Revision 1.65 by jsr166, Sun Oct 16 20:44:18 2016 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines