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.15 by jsr166, Sat Nov 21 21:12:55 2009 UTC vs.
Revision 1.54 by jsr166, Wed Dec 31 20:17:39 2014 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 import junit.framework.*;
8 import java.util.*;
9 import java.util.concurrent.*;
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 >        junit.textui.TestRunner.run(suite());
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 64 | Line 92 | public class LinkedBlockingDequeTest ext
92      }
93  
94      /**
95 <     * offer(null) throws NPE
95 >     * offerFirst(null) throws NullPointerException
96       */
97      public void testOfferFirstNull() {
98 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
99          try {
71            LinkedBlockingDeque q = new LinkedBlockingDeque();
100              q.offerFirst(null);
101              shouldThrow();
102          } catch (NullPointerException success) {}
103      }
104  
105      /**
106 +     * offerLast(null) throws NullPointerException
107 +     */
108 +    public void testOfferLastNull() {
109 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
110 +        try {
111 +            q.offerLast(null);
112 +            shouldThrow();
113 +        } catch (NullPointerException success) {}
114 +    }
115 +
116 +    /**
117       * OfferFirst succeeds
118       */
119      public void testOfferFirst() {
# Line 93 | Line 132 | public class LinkedBlockingDequeTest ext
132      }
133  
134      /**
135 <     *  pollFirst succeeds unless empty
135 >     * pollFirst succeeds unless empty
136       */
137      public void testPollFirst() {
138          LinkedBlockingDeque q = populatedDeque(SIZE);
139          for (int i = 0; i < SIZE; ++i) {
140 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
140 >            assertEquals(i, q.pollFirst());
141          }
142          assertNull(q.pollFirst());
143      }
144  
145      /**
146 <     *  pollLast succeeds unless empty
146 >     * pollLast succeeds unless empty
147       */
148      public void testPollLast() {
149          LinkedBlockingDeque q = populatedDeque(SIZE);
150          for (int i = SIZE-1; i >= 0; --i) {
151 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
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();
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();
# Line 173 | Line 212 | public class LinkedBlockingDequeTest ext
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();
220 >            assertEquals(i, q.getLast());
221 >            assertEquals(i, q.pollLast());
222          }
223          try {
224              q.getLast();
# Line 189 | Line 228 | public class LinkedBlockingDequeTest ext
228      }
229  
230      /**
231 <     *  removeFirst removes next element, or throws NSEE if empty
231 >     * removeFirst() removes first element, or throws NSEE if empty
232       */
233      public void testRemoveFirst() {
234          LinkedBlockingDeque q = populatedDeque(SIZE);
235          for (int i = 0; i < SIZE; ++i) {
236 <            assertEquals(i, ((Integer)q.removeFirst()).intValue());
236 >            assertEquals(i, q.removeFirst());
237          }
238          try {
239              q.removeFirst();
# Line 204 | Line 243 | public class LinkedBlockingDequeTest ext
243      }
244  
245      /**
246 <     *  removeLast removes last element, or throws NSEE if empty
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, ((Integer)q.removeLast()).intValue());
251 >            assertEquals(i, q.removeLast());
252          }
253          try {
254              q.removeLast();
# Line 219 | Line 258 | public class LinkedBlockingDequeTest ext
258      }
259  
260      /**
261 <     *  remove removes next element, or throws NSEE if empty
261 >     * remove removes next element, or throws NSEE if empty
262       */
263      public void testRemove() {
264          LinkedBlockingDeque q = populatedDeque(SIZE);
265          for (int i = 0; i < SIZE; ++i) {
266 <            assertEquals(i, ((Integer)q.remove()).intValue());
266 >            assertEquals(i, q.remove());
267          }
268          try {
269              q.remove();
# Line 237 | 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)));
285          }
# Line 252 | 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)));
300          }
# Line 269 | 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 279 | 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  
285
324      /**
325       * A new deque has the indicated capacity, or Integer.MAX_VALUE if
326       * none given
# Line 293 | Line 331 | public class LinkedBlockingDequeTest ext
331      }
332  
333      /**
334 <     * Constructor throws IAE if capacity argument nonpositive
334 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
335       */
336      public void testConstructor2() {
337          try {
338 <            LinkedBlockingDeque q = new LinkedBlockingDeque(0);
338 >            new LinkedBlockingDeque(0);
339              shouldThrow();
340          } catch (IllegalArgumentException success) {}
341      }
342  
343      /**
344 <     * Initializing from null Collection throws NPE
344 >     * Initializing from null Collection throws NullPointerException
345       */
346      public void testConstructor3() {
347          try {
348 <            LinkedBlockingDeque q = new LinkedBlockingDeque(null);
348 >            new LinkedBlockingDeque(null);
349              shouldThrow();
350          } catch (NullPointerException success) {}
351      }
352  
353      /**
354 <     * Initializing from Collection of null elements throws NPE
354 >     * Initializing from Collection of null elements throws NullPointerException
355       */
356      public void testConstructor4() {
357 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
358          try {
359 <            Integer[] ints = new Integer[SIZE];
321 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
359 >            new LinkedBlockingDeque(elements);
360              shouldThrow();
361          } catch (NullPointerException success) {}
362      }
363  
364      /**
365 <     * Initializing from Collection with some null elements throws NPE
365 >     * Initializing from Collection with some null elements throws
366 >     * NullPointerException
367       */
368      public void testConstructor5() {
369 +        Integer[] ints = new Integer[SIZE];
370 +        for (int i = 0; i < SIZE-1; ++i)
371 +            ints[i] = i;
372 +        Collection<Integer> elements = Arrays.asList(ints);
373          try {
374 <            Integer[] ints = new Integer[SIZE];
332 <            for (int i = 0; i < SIZE-1; ++i)
333 <                ints[i] = new Integer(i);
334 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
374 >            new LinkedBlockingDeque(elements);
375              shouldThrow();
376          } catch (NullPointerException success) {}
377      }
# Line 342 | Line 382 | public class LinkedBlockingDequeTest ext
382      public void testConstructor6() {
383          Integer[] ints = new Integer[SIZE];
384          for (int i = 0; i < SIZE; ++i)
385 <            ints[i] = new Integer(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());
# Line 381 | Line 421 | public class LinkedBlockingDequeTest ext
421      }
422  
423      /**
384     * offer(null) throws NPE
385     */
386    public void testOfferNull() {
387        try {
388            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
389            q.offer(null);
390            shouldThrow();
391        } catch (NullPointerException success) {}
392    }
393
394    /**
395     * add(null) throws NPE
396     */
397    public void testAddNull() {
398        try {
399            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
400            q.add(null);
401            shouldThrow();
402        } catch (NullPointerException success) {}
403    }
404
405    /**
424       * push(null) throws NPE
425       */
426      public void testPushNull() {
# Line 420 | Line 438 | public class LinkedBlockingDequeTest ext
438          try {
439              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
440              for (int i = 0; i < SIZE; ++i) {
441 <                Integer I = new Integer(i);
442 <                q.push(I);
443 <                assertEquals(I, q.peek());
441 >                Integer x = new Integer(i);
442 >                q.push(x);
443 >                assertEquals(x, q.peek());
444              }
445              assertEquals(0, q.remainingCapacity());
446              q.push(new Integer(SIZE));
# Line 437 | 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  
443
461      /**
462 <     *  pop removes next element, or throws NSEE if empty
462 >     * pop removes next element, or throws NSEE if empty
463       */
464      public void testPop() {
465          LinkedBlockingDeque q = populatedDeque(SIZE);
466          for (int i = 0; i < SIZE; ++i) {
467 <            assertEquals(i, ((Integer)q.pop()).intValue());
467 >            assertEquals(i, q.pop());
468          }
469          try {
470              q.pop();
# Line 455 | Line 472 | public class LinkedBlockingDequeTest ext
472          } catch (NoSuchElementException success) {}
473      }
474  
458
475      /**
476       * Offer succeeds if not full; fails if full
477       */
# Line 469 | 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 {
473            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
474            for (int i = 0; i < SIZE; ++i) {
475                assertTrue(q.add(new Integer(i)));
476            }
477            assertEquals(0, q.remainingCapacity());
493              q.add(new Integer(SIZE));
494              shouldThrow();
495          } catch (IllegalStateException success) {}
496      }
497  
498      /**
484     * addAll(null) throws NPE
485     */
486    public void testAddAll1() {
487        try {
488            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
489            q.addAll(null);
490            shouldThrow();
491        } catch (NullPointerException success) {}
492    }
493
494    /**
499       * addAll(this) throws IAE
500       */
501      public void testAddAllSelf() {
502 +        LinkedBlockingDeque q = populatedDeque(SIZE);
503          try {
499            LinkedBlockingDeque q = populatedDeque(SIZE);
504              q.addAll(q);
505              shouldThrow();
506          } catch (IllegalArgumentException success) {}
507      }
508  
509      /**
506     * addAll of a collection with null elements throws NPE
507     */
508    public void testAddAll2() {
509        try {
510            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
511            Integer[] ints = new Integer[SIZE];
512            q.addAll(Arrays.asList(ints));
513            shouldThrow();
514        } catch (NullPointerException success) {}
515    }
516    /**
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);
523 <            Integer[] ints = new Integer[SIZE];
524 <            for (int i = 0; i < SIZE-1; ++i)
525 <                ints[i] = new Integer(i);
526 <            q.addAll(Arrays.asList(ints));
520 >            q.addAll(elements);
521              shouldThrow();
522          } catch (NullPointerException success) {}
523      }
524 +
525      /**
526 <     * addAll throws ISE if not enough room
526 >     * addAll throws IllegalStateException if not enough room
527       */
528      public void testAddAll4() {
529 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
530 +        Integer[] ints = new Integer[SIZE];
531 +        for (int i = 0; i < SIZE; ++i)
532 +            ints[i] = new Integer(i);
533 +        Collection<Integer> elements = Arrays.asList(ints);
534          try {
535 <            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
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          } catch (IllegalStateException success) {}
538      }
539 +
540      /**
541       * Deque contains all elements, in traversal order, of successful addAll
542       */
# Line 555 | Line 552 | public class LinkedBlockingDequeTest ext
552              assertEquals(ints[i], q.poll());
553      }
554  
558
559    /**
560     * put(null) throws NPE
561     */
562    public void testPutNull() throws InterruptedException {
563        try {
564            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
565            q.put(null);
566            shouldThrow();
567        } catch (NullPointerException success) {}
568    }
569
555      /**
556       * all elements successfully put are contained
557       */
558      public void testPut() throws InterruptedException {
559          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
560          for (int i = 0; i < SIZE; ++i) {
561 <            Integer I = new Integer(i);
562 <            q.put(I);
563 <            assertTrue(q.contains(I));
561 >            Integer x = new Integer(i);
562 >            q.put(x);
563 >            assertTrue(q.contains(x));
564          }
565          assertEquals(0, q.remainingCapacity());
566      }
# Line 584 | Line 569 | public class LinkedBlockingDequeTest ext
569       * put blocks interruptibly if full
570       */
571      public void testBlockingPut() throws InterruptedException {
572 <        Thread t = new Thread(new CheckedRunnable() {
573 <            public void realRun() {
574 <                int added = 0;
572 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
573 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
574 >        Thread t = newStartedThread(new CheckedRunnable() {
575 >            public void realRun() throws InterruptedException {
576 >                for (int i = 0; i < SIZE; ++i)
577 >                    q.put(i);
578 >                assertEquals(SIZE, q.size());
579 >                assertEquals(0, q.remainingCapacity());
580 >
581 >                Thread.currentThread().interrupt();
582                  try {
583 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
584 <                    for (int i = 0; i < SIZE; ++i) {
585 <                        q.put(new Integer(i));
586 <                        ++added;
587 <                    }
588 <                    q.put(new Integer(SIZE));
589 <                    threadShouldThrow();
590 <                } catch (InterruptedException success) {
591 <                    threadAssertEquals(added, SIZE);
592 <                }
583 >                    q.put(99);
584 >                    shouldThrow();
585 >                } catch (InterruptedException success) {}
586 >                assertFalse(Thread.interrupted());
587 >
588 >                pleaseInterrupt.countDown();
589 >                try {
590 >                    q.put(99);
591 >                    shouldThrow();
592 >                } catch (InterruptedException success) {}
593 >                assertFalse(Thread.interrupted());
594              }});
595  
596 <        t.start();
597 <        Thread.sleep(SHORT_DELAY_MS);
596 >        await(pleaseInterrupt);
597 >        assertThreadStaysAlive(t);
598          t.interrupt();
599 <        t.join();
599 >        awaitTermination(t);
600 >        assertEquals(SIZE, q.size());
601 >        assertEquals(0, q.remainingCapacity());
602      }
603  
604      /**
605 <     * put blocks waiting for take when full
605 >     * put blocks interruptibly waiting for take when full
606       */
607      public void testPutWithTake() throws InterruptedException {
608 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
609 <        Thread t = new Thread(new CheckedRunnable() {
610 <            public void realRun() {
611 <                int added = 0;
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(new Object());
622 <                    ++added;
623 <                    q.put(new Object());
624 <                    ++added;
622 <                    q.put(new Object());
623 <                    ++added;
624 <                    q.put(new Object());
625 <                    ++added;
626 <                    threadShouldThrow();
627 <                } catch (InterruptedException success) {
628 <                    threadAssertTrue(added >= 2);
629 <                }
621 >                    q.put(99);
622 >                    shouldThrow();
623 >                } catch (InterruptedException success) {}
624 >                assertFalse(Thread.interrupted());
625              }});
626  
627 <        t.start();
628 <        Thread.sleep(SHORT_DELAY_MS);
629 <        q.take();
627 >        await(pleaseTake);
628 >        assertEquals(0, q.remainingCapacity());
629 >        assertEquals(0, q.take());
630 >
631 >        await(pleaseInterrupt);
632 >        assertThreadStaysAlive(t);
633          t.interrupt();
634 <        t.join();
634 >        awaitTermination(t);
635 >        assertEquals(0, q.remainingCapacity());
636      }
637  
638      /**
# Line 641 | Line 640 | public class LinkedBlockingDequeTest ext
640       */
641      public void testTimedOffer() throws InterruptedException {
642          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
643 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
643 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
644 >        Thread t = newStartedThread(new CheckedRunnable() {
645              public void realRun() throws InterruptedException {
646                  q.put(new Object());
647                  q.put(new Object());
648 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
649 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
650 <            }};
648 >                long startTime = System.nanoTime();
649 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
650 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
651 >                pleaseInterrupt.countDown();
652 >                try {
653 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
654 >                    shouldThrow();
655 >                } catch (InterruptedException success) {}
656 >            }});
657  
658 <        t.start();
659 <        Thread.sleep(SMALL_DELAY_MS);
658 >        await(pleaseInterrupt);
659 >        assertThreadStaysAlive(t);
660          t.interrupt();
661 <        t.join();
661 >        awaitTermination(t);
662      }
663  
664      /**
# Line 661 | Line 667 | public class LinkedBlockingDequeTest ext
667      public void testTake() throws InterruptedException {
668          LinkedBlockingDeque q = populatedDeque(SIZE);
669          for (int i = 0; i < SIZE; ++i) {
670 <            assertEquals(i, ((Integer)q.take()).intValue());
670 >            assertEquals(i, q.take());
671          }
672      }
673  
674      /**
675 <     * take blocks interruptibly when empty
670 <     */
671 <    public void testTakeFromEmpty() throws InterruptedException {
672 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
673 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
674 <            public void realRun() throws InterruptedException {
675 <                q.take();
676 <            }};
677 <
678 <        t.start();
679 <        Thread.sleep(SHORT_DELAY_MS);
680 <        t.interrupt();
681 <        t.join();
682 <    }
683 <
684 <    /**
685 <     * Take removes existing elements until empty, then blocks interruptibly
675 >     * take removes existing elements until empty, then blocks interruptibly
676       */
677      public void testBlockingTake() throws InterruptedException {
678 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
678 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
679 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
680 >        Thread t = newStartedThread(new CheckedRunnable() {
681              public void realRun() throws InterruptedException {
690                LinkedBlockingDeque q = populatedDeque(SIZE);
682                  for (int i = 0; i < SIZE; ++i) {
683 <                    assertEquals(i, ((Integer)q.take()).intValue());
683 >                    assertEquals(i, q.take());
684                  }
694                q.take();
695            }};
685  
686 <        t.start();
687 <        Thread.sleep(SHORT_DELAY_MS);
686 >                Thread.currentThread().interrupt();
687 >                try {
688 >                    q.take();
689 >                    shouldThrow();
690 >                } catch (InterruptedException success) {}
691 >                assertFalse(Thread.interrupted());
692 >
693 >                pleaseInterrupt.countDown();
694 >                try {
695 >                    q.take();
696 >                    shouldThrow();
697 >                } catch (InterruptedException success) {}
698 >                assertFalse(Thread.interrupted());
699 >            }});
700 >
701 >        await(pleaseInterrupt);
702 >        assertThreadStaysAlive(t);
703          t.interrupt();
704 <        t.join();
704 >        awaitTermination(t);
705      }
706  
703
707      /**
708       * poll succeeds unless empty
709       */
710      public void testPoll() {
711          LinkedBlockingDeque q = populatedDeque(SIZE);
712          for (int i = 0; i < SIZE; ++i) {
713 <            assertEquals(i, ((Integer)q.poll()).intValue());
713 >            assertEquals(i, q.poll());
714          }
715          assertNull(q.poll());
716      }
# Line 718 | Line 721 | public class LinkedBlockingDequeTest ext
721      public void testTimedPoll0() throws InterruptedException {
722          LinkedBlockingDeque q = populatedDeque(SIZE);
723          for (int i = 0; i < SIZE; ++i) {
724 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
724 >            assertEquals(i, q.poll(0, MILLISECONDS));
725          }
726          assertNull(q.poll(0, MILLISECONDS));
727      }
# Line 729 | Line 732 | public class LinkedBlockingDequeTest ext
732      public void testTimedPoll() throws InterruptedException {
733          LinkedBlockingDeque q = populatedDeque(SIZE);
734          for (int i = 0; i < SIZE; ++i) {
735 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
736 <        }
737 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
735 >            long startTime = System.nanoTime();
736 >            assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
737 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
738 >        }
739 >        long startTime = System.nanoTime();
740 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
741 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
742 >        checkEmpty(q);
743      }
744  
745      /**
# Line 739 | Line 747 | public class LinkedBlockingDequeTest ext
747       * returning timeout status
748       */
749      public void testInterruptedTimedPoll() throws InterruptedException {
750 <        Thread t = new Thread(new CheckedRunnable() {
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 {
744                LinkedBlockingDeque q = populatedDeque(SIZE);
754                  for (int i = 0; i < SIZE; ++i) {
755 <                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
755 >                    long t0 = System.nanoTime();
756 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
757 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
758                  }
759 +                long t0 = System.nanoTime();
760 +                aboutToWait.countDown();
761                  try {
762 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
762 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
763                      shouldThrow();
764 <                } catch (InterruptedException success) {}
765 <            }});
766 <
754 <        t.start();
755 <        Thread.sleep(SHORT_DELAY_MS);
756 <        t.interrupt();
757 <        t.join();
758 <    }
759 <
760 <    /**
761 <     *  timed poll before a delayed offer fails; after offer succeeds;
762 <     *  on interruption throws
763 <     */
764 <    public void testTimedPollWithOffer() throws InterruptedException {
765 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
766 <        Thread t = new Thread(new CheckedRunnable() {
767 <            public void realRun() throws InterruptedException {
768 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
769 <                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
770 <                try {
771 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
772 <                    shouldThrow();
773 <                } catch (InterruptedException success) {}
764 >                } catch (InterruptedException success) {
765 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
766 >                }
767              }});
768  
769 <        t.start();
770 <        Thread.sleep(SMALL_DELAY_MS);
778 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
769 >        aboutToWait.await();
770 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
771          t.interrupt();
772 <        t.join();
772 >        awaitTermination(t, MEDIUM_DELAY_MS);
773 >        checkEmpty(q);
774      }
775  
783
776      /**
777       * putFirst(null) throws NPE
778       */
779 <     public void testPutFirstNull() throws InterruptedException {
779 >    public void testPutFirstNull() throws InterruptedException {
780 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
781          try {
789            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
782              q.putFirst(null);
783              shouldThrow();
784          } catch (NullPointerException success) {}
785 <     }
785 >    }
786  
787      /**
788       * all elements successfully putFirst are contained
789       */
790 <     public void testPutFirst() throws InterruptedException {
791 <         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
792 <         for (int i = 0; i < SIZE; ++i) {
793 <             Integer I = new Integer(i);
794 <             q.putFirst(I);
795 <             assertTrue(q.contains(I));
796 <         }
797 <         assertEquals(0, q.remainingCapacity());
790 >    public void testPutFirst() throws InterruptedException {
791 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
792 >        for (int i = 0; i < SIZE; ++i) {
793 >            Integer x = new Integer(i);
794 >            q.putFirst(x);
795 >            assertTrue(q.contains(x));
796 >        }
797 >        assertEquals(0, q.remainingCapacity());
798      }
799  
800      /**
801       * putFirst blocks interruptibly if full
802       */
803      public void testBlockingPutFirst() throws InterruptedException {
804 <        Thread t = new Thread(new CheckedRunnable() {
805 <            public void realRun() {
806 <                int added = 0;
804 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
805 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
806 >        Thread t = newStartedThread(new CheckedRunnable() {
807 >            public void realRun() throws InterruptedException {
808 >                for (int i = 0; i < SIZE; ++i)
809 >                    q.putFirst(i);
810 >                assertEquals(SIZE, q.size());
811 >                assertEquals(0, q.remainingCapacity());
812 >
813 >                Thread.currentThread().interrupt();
814                  try {
815 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
816 <                    for (int i = 0; i < SIZE; ++i) {
817 <                        q.putFirst(new Integer(i));
818 <                        ++added;
819 <                    }
820 <                    q.putFirst(new Integer(SIZE));
821 <                    threadShouldThrow();
822 <                } catch (InterruptedException success) {
823 <                    threadAssertEquals(added, SIZE);
824 <                }
815 >                    q.putFirst(99);
816 >                    shouldThrow();
817 >                } catch (InterruptedException success) {}
818 >                assertFalse(Thread.interrupted());
819 >
820 >                pleaseInterrupt.countDown();
821 >                try {
822 >                    q.putFirst(99);
823 >                    shouldThrow();
824 >                } catch (InterruptedException success) {}
825 >                assertFalse(Thread.interrupted());
826              }});
827  
828 <        t.start();
829 <        Thread.sleep(SHORT_DELAY_MS);
828 >        await(pleaseInterrupt);
829 >        assertThreadStaysAlive(t);
830          t.interrupt();
831 <        t.join();
831 >        awaitTermination(t);
832 >        assertEquals(SIZE, q.size());
833 >        assertEquals(0, q.remainingCapacity());
834      }
835  
836      /**
837 <     * putFirst blocks waiting for take when full
837 >     * putFirst blocks interruptibly waiting for take when full
838       */
839      public void testPutFirstWithTake() throws InterruptedException {
840 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
841 <        Thread t = new Thread(new CheckedRunnable() {
842 <            public void realRun() {
843 <                int added = 0;
840 >        final int capacity = 2;
841 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
842 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
843 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
844 >        Thread t = newStartedThread(new CheckedRunnable() {
845 >            public void realRun() throws InterruptedException {
846 >                for (int i = 0; i < capacity; i++)
847 >                    q.putFirst(i);
848 >                pleaseTake.countDown();
849 >                q.putFirst(86);
850 >
851 >                pleaseInterrupt.countDown();
852                  try {
853 <                    q.putFirst(new Object());
854 <                    ++added;
855 <                    q.putFirst(new Object());
856 <                    ++added;
847 <                    q.putFirst(new Object());
848 <                    ++added;
849 <                    q.putFirst(new Object());
850 <                    ++added;
851 <                    threadShouldThrow();
852 <                } catch (InterruptedException success) {
853 <                    threadAssertTrue(added >= 2);
854 <                }
853 >                    q.putFirst(99);
854 >                    shouldThrow();
855 >                } catch (InterruptedException success) {}
856 >                assertFalse(Thread.interrupted());
857              }});
858  
859 <        t.start();
860 <        Thread.sleep(SHORT_DELAY_MS);
861 <        q.take();
859 >        await(pleaseTake);
860 >        assertEquals(0, q.remainingCapacity());
861 >        assertEquals(capacity - 1, q.take());
862 >
863 >        await(pleaseInterrupt);
864 >        assertThreadStaysAlive(t);
865          t.interrupt();
866 <        t.join();
866 >        awaitTermination(t);
867 >        assertEquals(0, q.remainingCapacity());
868      }
869  
870      /**
# Line 866 | Line 872 | public class LinkedBlockingDequeTest ext
872       */
873      public void testTimedOfferFirst() throws InterruptedException {
874          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
875 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
875 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
876 >        Thread t = newStartedThread(new CheckedRunnable() {
877              public void realRun() throws InterruptedException {
878                  q.putFirst(new Object());
879                  q.putFirst(new Object());
880 <                threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
881 <                q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
882 <            }};
880 >                long startTime = System.nanoTime();
881 >                assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
882 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
883 >                pleaseInterrupt.countDown();
884 >                try {
885 >                    q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
886 >                    shouldThrow();
887 >                } catch (InterruptedException success) {}
888 >            }});
889  
890 <        t.start();
891 <        Thread.sleep(SMALL_DELAY_MS);
890 >        await(pleaseInterrupt);
891 >        assertThreadStaysAlive(t);
892          t.interrupt();
893 <        t.join();
893 >        awaitTermination(t);
894      }
895  
896      /**
# Line 886 | Line 899 | public class LinkedBlockingDequeTest ext
899      public void testTakeFirst() throws InterruptedException {
900          LinkedBlockingDeque q = populatedDeque(SIZE);
901          for (int i = 0; i < SIZE; ++i) {
902 <            assertEquals(i, ((Integer)q.takeFirst()).intValue());
902 >            assertEquals(i, q.takeFirst());
903          }
904      }
905  
906      /**
907 <     * takeFirst blocks interruptibly when empty
907 >     * takeFirst() blocks interruptibly when empty
908       */
909 <    public void testTakeFirstFromEmpty() throws InterruptedException {
910 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
911 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
912 <            public void realRun() throws InterruptedException {
913 <                q.takeFirst();
914 <            }};
909 >    public void testTakeFirstFromEmptyBlocksInterruptibly() {
910 >        final BlockingDeque q = new LinkedBlockingDeque();
911 >        final CountDownLatch threadStarted = new CountDownLatch(1);
912 >        Thread t = newStartedThread(new CheckedRunnable() {
913 >            public void realRun() {
914 >                threadStarted.countDown();
915 >                try {
916 >                    q.takeFirst();
917 >                    shouldThrow();
918 >                } catch (InterruptedException success) {}
919 >                assertFalse(Thread.interrupted());
920 >            }});
921  
922 <        t.start();
923 <        Thread.sleep(SHORT_DELAY_MS);
922 >        await(threadStarted);
923 >        assertThreadStaysAlive(t);
924          t.interrupt();
925 <        t.join();
925 >        awaitTermination(t);
926      }
927  
928      /**
929 <     * TakeFirst removes existing elements until empty, then blocks interruptibly
929 >     * takeFirst() throws InterruptedException immediately if interrupted
930 >     * before waiting
931 >     */
932 >    public void testTakeFirstFromEmptyAfterInterrupt() {
933 >        final BlockingDeque q = new LinkedBlockingDeque();
934 >        Thread t = newStartedThread(new CheckedRunnable() {
935 >            public void realRun() {
936 >                Thread.currentThread().interrupt();
937 >                try {
938 >                    q.takeFirst();
939 >                    shouldThrow();
940 >                } catch (InterruptedException success) {}
941 >                assertFalse(Thread.interrupted());
942 >            }});
943 >
944 >        awaitTermination(t);
945 >    }
946 >
947 >    /**
948 >     * takeLast() blocks interruptibly when empty
949 >     */
950 >    public void testTakeLastFromEmptyBlocksInterruptibly() {
951 >        final BlockingDeque q = new LinkedBlockingDeque();
952 >        final CountDownLatch threadStarted = new CountDownLatch(1);
953 >        Thread t = newStartedThread(new CheckedRunnable() {
954 >            public void realRun() {
955 >                threadStarted.countDown();
956 >                try {
957 >                    q.takeLast();
958 >                    shouldThrow();
959 >                } catch (InterruptedException success) {}
960 >                assertFalse(Thread.interrupted());
961 >            }});
962 >
963 >        await(threadStarted);
964 >        assertThreadStaysAlive(t);
965 >        t.interrupt();
966 >        awaitTermination(t);
967 >    }
968 >
969 >    /**
970 >     * takeLast() throws InterruptedException immediately if interrupted
971 >     * before waiting
972 >     */
973 >    public void testTakeLastFromEmptyAfterInterrupt() {
974 >        final BlockingDeque q = new LinkedBlockingDeque();
975 >        Thread t = newStartedThread(new CheckedRunnable() {
976 >            public void realRun() {
977 >                Thread.currentThread().interrupt();
978 >                try {
979 >                    q.takeLast();
980 >                    shouldThrow();
981 >                } catch (InterruptedException success) {}
982 >                assertFalse(Thread.interrupted());
983 >            }});
984 >
985 >        awaitTermination(t);
986 >    }
987 >
988 >    /**
989 >     * takeFirst removes existing elements until empty, then blocks interruptibly
990       */
991      public void testBlockingTakeFirst() throws InterruptedException {
992 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
992 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
993 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
994 >        Thread t = newStartedThread(new CheckedRunnable() {
995              public void realRun() throws InterruptedException {
915                LinkedBlockingDeque q = populatedDeque(SIZE);
996                  for (int i = 0; i < SIZE; ++i) {
997 <                    assertEquals(i, ((Integer)q.takeFirst()).intValue());
997 >                    assertEquals(i, q.takeFirst());
998                  }
919                q.takeFirst();
920            }};
999  
1000 <        t.start();
1001 <        Thread.sleep(SHORT_DELAY_MS);
1000 >                Thread.currentThread().interrupt();
1001 >                try {
1002 >                    q.takeFirst();
1003 >                    shouldThrow();
1004 >                } catch (InterruptedException success) {}
1005 >                assertFalse(Thread.interrupted());
1006 >
1007 >                pleaseInterrupt.countDown();
1008 >                try {
1009 >                    q.takeFirst();
1010 >                    shouldThrow();
1011 >                } catch (InterruptedException success) {}
1012 >                assertFalse(Thread.interrupted());
1013 >            }});
1014 >
1015 >        await(pleaseInterrupt);
1016 >        assertThreadStaysAlive(t);
1017          t.interrupt();
1018 <        t.join();
1018 >        awaitTermination(t);
1019      }
1020  
928
1021      /**
1022       * timed pollFirst with zero timeout succeeds when non-empty, else times out
1023       */
1024      public void testTimedPollFirst0() throws InterruptedException {
1025          LinkedBlockingDeque q = populatedDeque(SIZE);
1026          for (int i = 0; i < SIZE; ++i) {
1027 <            assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
1027 >            assertEquals(i, q.pollFirst(0, MILLISECONDS));
1028          }
1029          assertNull(q.pollFirst(0, MILLISECONDS));
1030      }
# Line 943 | Line 1035 | public class LinkedBlockingDequeTest ext
1035      public void testTimedPollFirst() throws InterruptedException {
1036          LinkedBlockingDeque q = populatedDeque(SIZE);
1037          for (int i = 0; i < SIZE; ++i) {
1038 <            assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1039 <        }
1040 <        assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1038 >            long startTime = System.nanoTime();
1039 >            assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1040 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1041 >        }
1042 >        long startTime = System.nanoTime();
1043 >        assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1044 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1045 >        checkEmpty(q);
1046      }
1047  
1048      /**
# Line 953 | Line 1050 | public class LinkedBlockingDequeTest ext
1050       * returning timeout status
1051       */
1052      public void testInterruptedTimedPollFirst() throws InterruptedException {
1053 <        Thread t = new Thread(new CheckedRunnable() {
1053 >        final 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, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
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 <        t.start();
1077 <        Thread.sleep(SHORT_DELAY_MS);
1076 >        await(pleaseInterrupt);
1077 >        assertThreadStaysAlive(t);
1078          t.interrupt();
1079 <        t.join();
1079 >        awaitTermination(t);
1080      }
1081  
1082      /**
1083 <     *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
1084 <     *  on interruption throws
1083 >     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
1084 >     * on interruption throws
1085       */
1086      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
1087          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1088 <        Thread t = new Thread(new CheckedRunnable() {
1088 >        final CheckedBarrier barrier = new CheckedBarrier(2);
1089 >        Thread t = newStartedThread(new CheckedRunnable() {
1090              public void realRun() throws InterruptedException {
1091 <                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
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 <        t.start();
1114 <        Thread.sleep(SMALL_DELAY_MS);
1115 <        assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
1113 >        barrier.await();
1114 >        long startTime = System.nanoTime();
1115 >        assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1116 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1117 >        barrier.await();
1118 >        assertThreadStaysAlive(t);
1119          t.interrupt();
1120 <        t.join();
1120 >        awaitTermination(t);
1121      }
1122  
1123      /**
1124       * putLast(null) throws NPE
1125       */
1126 <     public void testPutLastNull() throws InterruptedException {
1126 >    public void testPutLastNull() throws InterruptedException {
1127 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1128          try {
1002            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1129              q.putLast(null);
1130              shouldThrow();
1131          } catch (NullPointerException success) {}
1132 <     }
1132 >    }
1133  
1134      /**
1135       * all elements successfully putLast are contained
1136       */
1137 <     public void testPutLast() throws InterruptedException {
1138 <         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1139 <         for (int i = 0; i < SIZE; ++i) {
1140 <             Integer I = new Integer(i);
1141 <             q.putLast(I);
1142 <             assertTrue(q.contains(I));
1143 <         }
1144 <         assertEquals(0, q.remainingCapacity());
1137 >    public void testPutLast() throws InterruptedException {
1138 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1139 >        for (int i = 0; i < SIZE; ++i) {
1140 >            Integer x = new Integer(i);
1141 >            q.putLast(x);
1142 >            assertTrue(q.contains(x));
1143 >        }
1144 >        assertEquals(0, q.remainingCapacity());
1145      }
1146  
1147      /**
1148       * putLast blocks interruptibly if full
1149       */
1150      public void testBlockingPutLast() throws InterruptedException {
1151 <        Thread t = new Thread(new CheckedRunnable() {
1152 <            public void realRun() {
1153 <                int added = 0;
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 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1163 <                    for (int i = 0; i < SIZE; ++i) {
1164 <                        q.putLast(new Integer(i));
1165 <                        ++added;
1166 <                    }
1167 <                    q.putLast(new Integer(SIZE));
1168 <                    threadShouldThrow();
1169 <                } catch (InterruptedException success) {
1170 <                    threadAssertEquals(added, SIZE);
1171 <                }
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 <        t.start();
1176 <        Thread.sleep(SHORT_DELAY_MS);
1175 >        await(pleaseInterrupt);
1176 >        assertThreadStaysAlive(t);
1177          t.interrupt();
1178 <        t.join();
1178 >        awaitTermination(t);
1179 >        assertEquals(SIZE, q.size());
1180 >        assertEquals(0, q.remainingCapacity());
1181      }
1182  
1183      /**
1184 <     * putLast blocks waiting for take when full
1184 >     * putLast blocks interruptibly waiting for take when full
1185       */
1186      public void testPutLastWithTake() throws InterruptedException {
1187 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1188 <        Thread t = new Thread(new CheckedRunnable() {
1189 <            public void realRun() {
1190 <                int added = 0;
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(new Object());
1201 <                    ++added;
1202 <                    q.putLast(new Object());
1203 <                    ++added;
1060 <                    q.putLast(new Object());
1061 <                    ++added;
1062 <                    q.putLast(new Object());
1063 <                    ++added;
1064 <                    threadShouldThrow();
1065 <                } catch (InterruptedException success) {
1066 <                    threadAssertTrue(added >= 2);
1067 <                }
1200 >                    q.putLast(99);
1201 >                    shouldThrow();
1202 >                } catch (InterruptedException success) {}
1203 >                assertFalse(Thread.interrupted());
1204              }});
1205  
1206 <        t.start();
1207 <        Thread.sleep(SHORT_DELAY_MS);
1208 <        q.take();
1206 >        await(pleaseTake);
1207 >        assertEquals(0, q.remainingCapacity());
1208 >        assertEquals(0, q.take());
1209 >
1210 >        await(pleaseInterrupt);
1211 >        assertThreadStaysAlive(t);
1212          t.interrupt();
1213 <        t.join();
1213 >        awaitTermination(t);
1214 >        assertEquals(0, q.remainingCapacity());
1215      }
1216  
1217      /**
# Line 1079 | Line 1219 | public class LinkedBlockingDequeTest ext
1219       */
1220      public void testTimedOfferLast() throws InterruptedException {
1221          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1222 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1222 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1223 >        Thread t = newStartedThread(new CheckedRunnable() {
1224              public void realRun() throws InterruptedException {
1225                  q.putLast(new Object());
1226                  q.putLast(new Object());
1227 <                threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1228 <                q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1229 <            }};
1227 >                long startTime = System.nanoTime();
1228 >                assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1229 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1230 >                pleaseInterrupt.countDown();
1231 >                try {
1232 >                    q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1233 >                    shouldThrow();
1234 >                } catch (InterruptedException success) {}
1235 >            }});
1236  
1237 <        t.start();
1238 <        Thread.sleep(SMALL_DELAY_MS);
1237 >        await(pleaseInterrupt);
1238 >        assertThreadStaysAlive(t);
1239          t.interrupt();
1240 <        t.join();
1240 >        awaitTermination(t);
1241      }
1242  
1243      /**
# Line 1099 | Line 1246 | public class LinkedBlockingDequeTest ext
1246      public void testTakeLast() throws InterruptedException {
1247          LinkedBlockingDeque q = populatedDeque(SIZE);
1248          for (int i = 0; i < SIZE; ++i) {
1249 <            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1249 >            assertEquals(SIZE-i-1, q.takeLast());
1250          }
1251      }
1252  
1253      /**
1254 <     * takeLast blocks interruptibly when empty
1108 <     */
1109 <    public void testTakeLastFromEmpty() throws InterruptedException {
1110 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1111 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1112 <            public void realRun() throws InterruptedException {
1113 <                q.takeLast();
1114 <            }};
1115 <
1116 <        t.start();
1117 <        Thread.sleep(SHORT_DELAY_MS);
1118 <        t.interrupt();
1119 <        t.join();
1120 <    }
1121 <
1122 <    /**
1123 <     * TakeLast removes existing elements until empty, then blocks interruptibly
1254 >     * takeLast removes existing elements until empty, then blocks interruptibly
1255       */
1256      public void testBlockingTakeLast() throws InterruptedException {
1257 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1257 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1258 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1259 >        Thread t = newStartedThread(new CheckedRunnable() {
1260              public void realRun() throws InterruptedException {
1128                LinkedBlockingDeque q = populatedDeque(SIZE);
1261                  for (int i = 0; i < SIZE; ++i) {
1262 <                    assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1262 >                    assertEquals(SIZE-i-1, q.takeLast());
1263                  }
1132                q.takeLast();
1133            }};
1264  
1265 <        t.start();
1266 <        Thread.sleep(SHORT_DELAY_MS);
1265 >                Thread.currentThread().interrupt();
1266 >                try {
1267 >                    q.takeLast();
1268 >                    shouldThrow();
1269 >                } catch (InterruptedException success) {}
1270 >                assertFalse(Thread.interrupted());
1271 >
1272 >                pleaseInterrupt.countDown();
1273 >                try {
1274 >                    q.takeLast();
1275 >                    shouldThrow();
1276 >                } catch (InterruptedException success) {}
1277 >                assertFalse(Thread.interrupted());
1278 >            }});
1279 >
1280 >        await(pleaseInterrupt);
1281 >        assertThreadStaysAlive(t);
1282          t.interrupt();
1283 <        t.join();
1283 >        awaitTermination(t);
1284      }
1285  
1141
1286      /**
1287       * timed pollLast with zero timeout succeeds when non-empty, else times out
1288       */
1289      public void testTimedPollLast0() throws InterruptedException {
1290          LinkedBlockingDeque q = populatedDeque(SIZE);
1291          for (int i = 0; i < SIZE; ++i) {
1292 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1292 >            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1293          }
1294          assertNull(q.pollLast(0, MILLISECONDS));
1295      }
# Line 1156 | Line 1300 | public class LinkedBlockingDequeTest ext
1300      public void testTimedPollLast() throws InterruptedException {
1301          LinkedBlockingDeque q = populatedDeque(SIZE);
1302          for (int i = 0; i < SIZE; ++i) {
1303 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1304 <        }
1305 <        assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1303 >            long startTime = System.nanoTime();
1304 >            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1305 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1306 >        }
1307 >        long startTime = System.nanoTime();
1308 >        assertNull(q.pollLast(timeoutMillis(), MILLISECONDS));
1309 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1310 >        checkEmpty(q);
1311      }
1312  
1313      /**
# Line 1166 | Line 1315 | public class LinkedBlockingDequeTest ext
1315       * returning timeout status
1316       */
1317      public void testInterruptedTimedPollLast() throws InterruptedException {
1318 <        Thread t = new Thread(new CheckedRunnable() {
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, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1323 >                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1324                  }
1325 +
1326 +                Thread.currentThread().interrupt();
1327                  try {
1328 <                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1328 >                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1329                      shouldThrow();
1330                  } catch (InterruptedException success) {}
1331 +                assertFalse(Thread.interrupted());
1332 +
1333 +                pleaseInterrupt.countDown();
1334 +                try {
1335 +                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1336 +                    shouldThrow();
1337 +                } catch (InterruptedException success) {}
1338 +                assertFalse(Thread.interrupted());
1339              }});
1340  
1341 <        t.start();
1342 <        Thread.sleep(SHORT_DELAY_MS);
1341 >        await(pleaseInterrupt);
1342 >        assertThreadStaysAlive(t);
1343          t.interrupt();
1344 <        t.join();
1344 >        awaitTermination(t);
1345      }
1346  
1347      /**
1348 <     *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1349 <     *  on interruption throws
1348 >     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1349 >     * on interruption throws
1350       */
1351      public void testTimedPollWithOfferLast() throws InterruptedException {
1352          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1353 <        Thread t = new Thread(new CheckedRunnable() {
1353 >        final CheckedBarrier barrier = new CheckedBarrier(2);
1354 >        Thread t = newStartedThread(new CheckedRunnable() {
1355              public void realRun() throws InterruptedException {
1356 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1356 >                long startTime = System.nanoTime();
1357 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
1358 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1359 >
1360 >                barrier.await();
1361 >
1362                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1363 +
1364 +                Thread.currentThread().interrupt();
1365 +                try {
1366 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1367 +                    shouldThrow();
1368 +                } catch (InterruptedException success) {}
1369 +                assertFalse(Thread.interrupted());
1370 +
1371 +                barrier.await();
1372                  try {
1373                      q.poll(LONG_DELAY_MS, MILLISECONDS);
1374                      shouldThrow();
1375                  } catch (InterruptedException success) {}
1376 +                assertFalse(Thread.interrupted());
1377              }});
1378  
1379 <        t.start();
1380 <        Thread.sleep(SMALL_DELAY_MS);
1381 <        assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1379 >        barrier.await();
1380 >        long startTime = System.nanoTime();
1381 >        assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS));
1382 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1383 >
1384 >        barrier.await();
1385 >        assertThreadStaysAlive(t);
1386          t.interrupt();
1387 <        t.join();
1387 >        awaitTermination(t);
1388      }
1389  
1210
1390      /**
1391       * element returns next element, or throws NSEE if empty
1392       */
1393      public void testElement() {
1394          LinkedBlockingDeque q = populatedDeque(SIZE);
1395          for (int i = 0; i < SIZE; ++i) {
1396 <            assertEquals(i, ((Integer)q.element()).intValue());
1396 >            assertEquals(i, q.element());
1397              q.poll();
1398          }
1399          try {
# Line 1224 | Line 1403 | public class LinkedBlockingDequeTest ext
1403      }
1404  
1405      /**
1227     * remove(x) removes x and returns true if present
1228     */
1229    public void testRemoveElement() {
1230        LinkedBlockingDeque q = populatedDeque(SIZE);
1231        for (int i = 1; i < SIZE; i+=2) {
1232            assertTrue(q.remove(new Integer(i)));
1233        }
1234        for (int i = 0; i < SIZE; i+=2) {
1235            assertTrue(q.remove(new Integer(i)));
1236            assertFalse(q.remove(new Integer(i+1)));
1237        }
1238        assertTrue(q.isEmpty());
1239    }
1240
1241    /**
1406       * contains(x) reports true when elements added but not yet removed
1407       */
1408      public void testContains() {
# Line 1309 | Line 1473 | public class LinkedBlockingDequeTest ext
1473              assertTrue(q.removeAll(p));
1474              assertEquals(SIZE-i, q.size());
1475              for (int j = 0; j < i; ++j) {
1476 <                Integer I = (Integer)(p.remove());
1477 <                assertFalse(q.contains(I));
1476 >                Integer x = (Integer)(p.remove());
1477 >                assertFalse(q.contains(x));
1478              }
1479          }
1480      }
1481  
1482      /**
1483 <     * toArray contains all elements
1483 >     * toArray contains all elements in FIFO order
1484       */
1485 <    public void testToArray() throws InterruptedException{
1485 >    public void testToArray() throws InterruptedException {
1486          LinkedBlockingDeque q = populatedDeque(SIZE);
1487          Object[] o = q.toArray();
1488          for (int i = 0; i < o.length; i++)
1489 <            assertEquals(o[i], q.take());
1489 >            assertSame(o[i], q.poll());
1490      }
1491  
1492      /**
1493 <     * toArray(a) contains all elements
1493 >     * toArray(a) contains all elements in FIFO order
1494       */
1495 <    public void testToArray2() throws InterruptedException {
1496 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1495 >    public void testToArray2() {
1496 >        LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1497          Integer[] ints = new Integer[SIZE];
1498 <        ints = (Integer[])q.toArray(ints);
1498 >        Integer[] array = q.toArray(ints);
1499 >        assertSame(ints, array);
1500          for (int i = 0; i < ints.length; i++)
1501 <            assertEquals(ints[i], q.take());
1501 >            assertSame(ints[i], q.remove());
1502      }
1503  
1504      /**
1505 <     * toArray(null) throws NPE
1341 <     */
1342 <    public void testToArray_BadArg() {
1343 <        try {
1344 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1345 <            Object o[] = q.toArray(null);
1346 <            shouldThrow();
1347 <        } catch (NullPointerException success) {}
1348 <    }
1349 <
1350 <    /**
1351 <     * toArray with incompatible array type throws CCE
1505 >     * toArray(incompatible array type) throws ArrayStoreException
1506       */
1507      public void testToArray1_BadArg() {
1508 +        LinkedBlockingDeque q = populatedDeque(SIZE);
1509          try {
1510 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1356 <            Object o[] = q.toArray(new String[10] );
1510 >            q.toArray(new String[10]);
1511              shouldThrow();
1512          } catch (ArrayStoreException success) {}
1513      }
1514  
1361
1515      /**
1516       * iterator iterates through all elements
1517       */
# Line 1373 | Line 1526 | public class LinkedBlockingDequeTest ext
1526      /**
1527       * iterator.remove removes current element
1528       */
1529 <    public void testIteratorRemove () {
1529 >    public void testIteratorRemove() {
1530          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1531          q.add(two);
1532          q.add(one);
# Line 1384 | Line 1537 | public class LinkedBlockingDequeTest ext
1537          it.remove();
1538  
1539          it = q.iterator();
1540 <        assertEquals(it.next(), one);
1541 <        assertEquals(it.next(), three);
1540 >        assertSame(it.next(), one);
1541 >        assertSame(it.next(), three);
1542          assertFalse(it.hasNext());
1543      }
1544  
1392
1545      /**
1546       * iterator ordering is FIFO
1547       */
# Line 1401 | Line 1553 | public class LinkedBlockingDequeTest ext
1553          assertEquals(0, q.remainingCapacity());
1554          int k = 0;
1555          for (Iterator it = q.iterator(); it.hasNext();) {
1556 <            int i = ((Integer)(it.next())).intValue();
1405 <            assertEquals(++k, i);
1556 >            assertEquals(++k, it.next());
1557          }
1558          assertEquals(3, k);
1559      }
# Line 1410 | Line 1561 | public class LinkedBlockingDequeTest ext
1561      /**
1562       * Modifications do not cause iterators to fail
1563       */
1564 <    public void testWeaklyConsistentIteration () {
1564 >    public void testWeaklyConsistentIteration() {
1565          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1566          q.add(one);
1567          q.add(two);
# Line 1422 | Line 1573 | public class LinkedBlockingDequeTest ext
1573          assertEquals(0, q.size());
1574      }
1575  
1425
1576      /**
1577 <     *  Descending iterator iterates through all elements
1577 >     * Descending iterator iterates through all elements
1578       */
1579      public void testDescendingIterator() {
1580          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 1443 | Line 1593 | public class LinkedBlockingDequeTest ext
1593      }
1594  
1595      /**
1596 <     *  Descending iterator ordering is reverse FIFO
1596 >     * Descending iterator ordering is reverse FIFO
1597       */
1598      public void testDescendingIteratorOrdering() {
1599          final LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1453 | Line 1603 | public class LinkedBlockingDequeTest ext
1603              q.add(new Integer(1));
1604              int k = 0;
1605              for (Iterator it = q.descendingIterator(); it.hasNext();) {
1606 <                int i = ((Integer)(it.next())).intValue();
1457 <                assertEquals(++k, i);
1606 >                assertEquals(++k, it.next());
1607              }
1608  
1609              assertEquals(3, k);
# Line 1467 | Line 1616 | public class LinkedBlockingDequeTest ext
1616      /**
1617       * descendingIterator.remove removes current element
1618       */
1619 <    public void testDescendingIteratorRemove () {
1619 >    public void testDescendingIteratorRemove() {
1620          final LinkedBlockingDeque q = new LinkedBlockingDeque();
1621          for (int iters = 0; iters < 100; ++iters) {
1622              q.add(new Integer(3));
# Line 1486 | Line 1635 | public class LinkedBlockingDequeTest ext
1635          }
1636      }
1637  
1489
1638      /**
1639       * toString contains toStrings of elements
1640       */
# Line 1494 | Line 1642 | public class LinkedBlockingDequeTest ext
1642          LinkedBlockingDeque q = populatedDeque(SIZE);
1643          String s = q.toString();
1644          for (int i = 0; i < SIZE; ++i) {
1645 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1645 >            assertTrue(s.contains(String.valueOf(i)));
1646          }
1647      }
1648  
1501
1649      /**
1650       * offer transfers elements across Executor tasks
1651       */
# Line 1507 | Line 1654 | public class LinkedBlockingDequeTest ext
1654          q.add(one);
1655          q.add(two);
1656          ExecutorService executor = Executors.newFixedThreadPool(2);
1657 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1658          executor.execute(new CheckedRunnable() {
1659              public void realRun() throws InterruptedException {
1660 <                threadAssertFalse(q.offer(three));
1661 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1662 <                threadAssertEquals(0, q.remainingCapacity());
1660 >                assertFalse(q.offer(three));
1661 >                threadsStarted.await();
1662 >                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1663 >                assertEquals(0, q.remainingCapacity());
1664              }});
1665  
1666          executor.execute(new CheckedRunnable() {
1667              public void realRun() throws InterruptedException {
1668 <                Thread.sleep(SMALL_DELAY_MS);
1669 <                threadAssertEquals(one, q.take());
1668 >                threadsStarted.await();
1669 >                assertSame(one, q.take());
1670              }});
1671  
1672          joinPool(executor);
1673      }
1674  
1675      /**
1676 <     * poll retrieves elements across Executor threads
1676 >     * timed poll retrieves elements across Executor threads
1677       */
1678      public void testPollInExecutor() {
1679          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1680 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1681          ExecutorService executor = Executors.newFixedThreadPool(2);
1682          executor.execute(new CheckedRunnable() {
1683              public void realRun() throws InterruptedException {
1684 <                threadAssertNull(q.poll());
1685 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1686 <                threadAssertTrue(q.isEmpty());
1684 >                assertNull(q.poll());
1685 >                threadsStarted.await();
1686 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1687 >                checkEmpty(q);
1688              }});
1689  
1690          executor.execute(new CheckedRunnable() {
1691              public void realRun() throws InterruptedException {
1692 <                Thread.sleep(SMALL_DELAY_MS);
1692 >                threadsStarted.await();
1693                  q.put(one);
1694              }});
1695  
# Line 1549 | Line 1700 | public class LinkedBlockingDequeTest ext
1700       * A deserialized serialized deque has same elements in same order
1701       */
1702      public void testSerialization() throws Exception {
1703 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1704 <
1554 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1555 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1556 <        out.writeObject(q);
1557 <        out.close();
1703 >        Queue x = populatedDeque(SIZE);
1704 >        Queue y = serialClone(x);
1705  
1706 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1707 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1708 <        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1709 <        assertEquals(q.size(), r.size());
1710 <        while (!q.isEmpty())
1711 <            assertEquals(q.remove(), r.remove());
1712 <    }
1713 <
1714 <    /**
1568 <     * drainTo(null) throws NPE
1569 <     */
1570 <    public void testDrainToNull() {
1571 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1572 <        try {
1573 <            q.drainTo(null);
1574 <            shouldThrow();
1575 <        } catch (NullPointerException success) {}
1576 <    }
1577 <
1578 <    /**
1579 <     * drainTo(this) throws IAE
1580 <     */
1581 <    public void testDrainToSelf() {
1582 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1583 <        try {
1584 <            q.drainTo(q);
1585 <            shouldThrow();
1586 <        } catch (IllegalArgumentException success) {}
1706 >        assertNotSame(y, x);
1707 >        assertEquals(x.size(), y.size());
1708 >        assertEquals(x.toString(), y.toString());
1709 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1710 >        while (!x.isEmpty()) {
1711 >            assertFalse(y.isEmpty());
1712 >            assertEquals(x.remove(), y.remove());
1713 >        }
1714 >        assertTrue(y.isEmpty());
1715      }
1716  
1717      /**
# Line 1593 | Line 1721 | public class LinkedBlockingDequeTest ext
1721          LinkedBlockingDeque q = populatedDeque(SIZE);
1722          ArrayList l = new ArrayList();
1723          q.drainTo(l);
1724 <        assertEquals(q.size(), 0);
1725 <        assertEquals(l.size(), SIZE);
1724 >        assertEquals(0, q.size());
1725 >        assertEquals(SIZE, l.size());
1726          for (int i = 0; i < SIZE; ++i)
1727              assertEquals(l.get(i), new Integer(i));
1728          q.add(zero);
# Line 1604 | Line 1732 | public class LinkedBlockingDequeTest ext
1732          assertTrue(q.contains(one));
1733          l.clear();
1734          q.drainTo(l);
1735 <        assertEquals(q.size(), 0);
1736 <        assertEquals(l.size(), 2);
1735 >        assertEquals(0, q.size());
1736 >        assertEquals(2, l.size());
1737          for (int i = 0; i < 2; ++i)
1738              assertEquals(l.get(i), new Integer(i));
1739      }
# Line 1631 | Line 1759 | public class LinkedBlockingDequeTest ext
1759      }
1760  
1761      /**
1762 <     * drainTo(null, n) throws NPE
1635 <     */
1636 <    public void testDrainToNullN() {
1637 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1638 <        try {
1639 <            q.drainTo(null, 0);
1640 <            shouldThrow();
1641 <        } catch (NullPointerException success) {}
1642 <    }
1643 <
1644 <    /**
1645 <     * drainTo(this, n) throws IAE
1646 <     */
1647 <    public void testDrainToSelfN() {
1648 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1649 <        try {
1650 <            q.drainTo(q, 0);
1651 <            shouldThrow();
1652 <        } catch (IllegalArgumentException success) {}
1653 <    }
1654 <
1655 <    /**
1656 <     * drainTo(c, n) empties first max {n, size} elements of deque into c
1762 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
1763       */
1764      public void testDrainToN() {
1765          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1662 | Line 1768 | public class LinkedBlockingDequeTest ext
1768                  assertTrue(q.offer(new Integer(j)));
1769              ArrayList l = new ArrayList();
1770              q.drainTo(l, i);
1771 <            int k = (i < SIZE)? i : SIZE;
1772 <            assertEquals(l.size(), k);
1773 <            assertEquals(q.size(), SIZE-k);
1771 >            int k = (i < SIZE) ? i : SIZE;
1772 >            assertEquals(k, l.size());
1773 >            assertEquals(SIZE-k, q.size());
1774              for (int j = 0; j < k; ++j)
1775                  assertEquals(l.get(j), new Integer(j));
1776 <            while (q.poll() != null) ;
1776 >            do {} while (q.poll() != null);
1777 >        }
1778 >    }
1779 >
1780 >    /**
1781 >     * remove(null), contains(null) always return false
1782 >     */
1783 >    public void testNeverContainsNull() {
1784 >        Deque<?>[] qs = {
1785 >            new LinkedBlockingDeque<Object>(),
1786 >            populatedDeque(2),
1787 >        };
1788 >
1789 >        for (Deque<?> q : qs) {
1790 >            assertFalse(q.contains(null));
1791 >            assertFalse(q.remove(null));
1792 >            assertFalse(q.removeFirstOccurrence(null));
1793 >            assertFalse(q.removeLastOccurrence(null));
1794          }
1795      }
1796  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines