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.8 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.60 by jsr166, Sun May 24 01:42:14 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines