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.39 by jsr166, Fri May 27 20:07:24 2011 UTC vs.
Revision 1.84 by jsr166, Sun Aug 11 22:29:27 2019 UTC

# Line 4 | Line 4
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  
# Line 20 | Line 32 | public class LinkedBlockingDequeTest ext
32  
33      public static class Bounded extends BlockingQueueTest {
34          protected BlockingQueue emptyCollection() {
35 <            return new LinkedBlockingDeque(20);
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 +        class Implementation implements CollectionImplementation {
45 +            public Class<?> klazz() { return LinkedBlockingDeque.class; }
46 +            public Collection emptyCollection() { return new LinkedBlockingDeque(); }
47 +            public Object makeElement(int i) { return i; }
48 +            public boolean isConcurrent() { return true; }
49 +            public boolean permitsNulls() { return false; }
50 +        }
51          return newTestSuite(LinkedBlockingDequeTest.class,
52                              new Unbounded().testSuite(),
53 <                            new Bounded().testSuite());
53 >                            new Bounded().testSuite(),
54 >                            CollectionTest.testSuite(new Implementation()));
55      }
56  
57      /**
58 <     * Create a deque of given size containing consecutive
59 <     * Integers 0 ... n.
58 >     * Returns a new deque of given size containing consecutive
59 >     * Integers 0 ... n - 1.
60       */
61 <    private LinkedBlockingDeque<Integer> populatedDeque(int n) {
61 >    private static LinkedBlockingDeque<Integer> populatedDeque(int n) {
62          LinkedBlockingDeque<Integer> q =
63              new LinkedBlockingDeque<Integer>(n);
64          assertTrue(q.isEmpty());
# Line 47 | Line 67 | public class LinkedBlockingDequeTest ext
67          assertFalse(q.isEmpty());
68          assertEquals(0, q.remainingCapacity());
69          assertEquals(n, q.size());
70 +        assertEquals((Integer) 0, q.peekFirst());
71 +        assertEquals((Integer) (n - 1), q.peekLast());
72          return q;
73      }
74  
# Line 70 | Line 92 | public class LinkedBlockingDequeTest ext
92      public void testSize() {
93          LinkedBlockingDeque q = populatedDeque(SIZE);
94          for (int i = 0; i < SIZE; ++i) {
95 <            assertEquals(SIZE-i, q.size());
95 >            assertEquals(SIZE - i, q.size());
96              q.removeFirst();
97          }
98          for (int i = 0; i < SIZE; ++i) {
# Line 80 | Line 102 | public class LinkedBlockingDequeTest ext
102      }
103  
104      /**
105 <     * offer(null) throws NPE
105 >     * offerFirst(null) throws NullPointerException
106       */
107      public void testOfferFirstNull() {
108 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
109          try {
87            LinkedBlockingDeque q = new LinkedBlockingDeque();
110              q.offerFirst(null);
111              shouldThrow();
112          } catch (NullPointerException success) {}
113      }
114  
115      /**
116 +     * offerLast(null) throws NullPointerException
117 +     */
118 +    public void testOfferLastNull() {
119 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
120 +        try {
121 +            q.offerLast(null);
122 +            shouldThrow();
123 +        } catch (NullPointerException success) {}
124 +    }
125 +
126 +    /**
127       * OfferFirst succeeds
128       */
129      public void testOfferFirst() {
# Line 124 | Line 157 | public class LinkedBlockingDequeTest ext
157       */
158      public void testPollLast() {
159          LinkedBlockingDeque q = populatedDeque(SIZE);
160 <        for (int i = SIZE-1; i >= 0; --i) {
160 >        for (int i = SIZE - 1; i >= 0; --i) {
161              assertEquals(i, q.pollLast());
162          }
163          assertNull(q.pollLast());
# Line 163 | Line 196 | public class LinkedBlockingDequeTest ext
196       */
197      public void testPeekLast() {
198          LinkedBlockingDeque q = populatedDeque(SIZE);
199 <        for (int i = SIZE-1; i >= 0; --i) {
199 >        for (int i = SIZE - 1; i >= 0; --i) {
200              assertEquals(i, q.peekLast());
201              assertEquals(i, q.pollLast());
202              assertTrue(q.peekLast() == null ||
# Line 193 | Line 226 | public class LinkedBlockingDequeTest ext
226       */
227      public void testLastElement() {
228          LinkedBlockingDeque q = populatedDeque(SIZE);
229 <        for (int i = SIZE-1; i >= 0; --i) {
229 >        for (int i = SIZE - 1; i >= 0; --i) {
230              assertEquals(i, q.getLast());
231              assertEquals(i, q.pollLast());
232          }
# Line 253 | Line 286 | public class LinkedBlockingDequeTest ext
286       */
287      public void testRemoveFirstOccurrence() {
288          LinkedBlockingDeque q = populatedDeque(SIZE);
289 <        for (int i = 1; i < SIZE; i+=2) {
289 >        for (int i = 1; i < SIZE; i += 2) {
290              assertTrue(q.removeFirstOccurrence(new Integer(i)));
291          }
292 <        for (int i = 0; i < SIZE; i+=2) {
292 >        for (int i = 0; i < SIZE; i += 2) {
293              assertTrue(q.removeFirstOccurrence(new Integer(i)));
294 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
294 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
295          }
296          assertTrue(q.isEmpty());
297      }
# Line 268 | Line 301 | public class LinkedBlockingDequeTest ext
301       */
302      public void testRemoveLastOccurrence() {
303          LinkedBlockingDeque q = populatedDeque(SIZE);
304 <        for (int i = 1; i < SIZE; i+=2) {
304 >        for (int i = 1; i < SIZE; i += 2) {
305              assertTrue(q.removeLastOccurrence(new Integer(i)));
306          }
307 <        for (int i = 0; i < SIZE; i+=2) {
307 >        for (int i = 0; i < SIZE; i += 2) {
308              assertTrue(q.removeLastOccurrence(new Integer(i)));
309 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
309 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
310          }
311          assertTrue(q.isEmpty());
312      }
# Line 308 | Line 341 | public class LinkedBlockingDequeTest ext
341      }
342  
343      /**
344 <     * Constructor throws IAE if capacity argument nonpositive
344 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
345       */
346      public void testConstructor2() {
347          try {
348 <            LinkedBlockingDeque q = new LinkedBlockingDeque(0);
348 >            new LinkedBlockingDeque(0);
349              shouldThrow();
350          } catch (IllegalArgumentException success) {}
351      }
352  
353      /**
354 <     * Initializing from null Collection throws NPE
354 >     * Initializing from null Collection throws NullPointerException
355       */
356      public void testConstructor3() {
357          try {
358 <            LinkedBlockingDeque q = new LinkedBlockingDeque(null);
358 >            new LinkedBlockingDeque(null);
359              shouldThrow();
360          } catch (NullPointerException success) {}
361      }
362  
363      /**
364 <     * Initializing from Collection of null elements throws NPE
364 >     * Initializing from Collection of null elements throws NullPointerException
365       */
366      public void testConstructor4() {
367 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
368          try {
369 <            Integer[] ints = new Integer[SIZE];
336 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
369 >            new LinkedBlockingDeque(elements);
370              shouldThrow();
371          } catch (NullPointerException success) {}
372      }
373  
374      /**
375 <     * Initializing from Collection with some null elements throws NPE
375 >     * Initializing from Collection with some null elements throws
376 >     * NullPointerException
377       */
378      public void testConstructor5() {
379 +        Integer[] ints = new Integer[SIZE];
380 +        for (int i = 0; i < SIZE - 1; ++i)
381 +            ints[i] = i;
382 +        Collection<Integer> elements = Arrays.asList(ints);
383          try {
384 <            Integer[] ints = new Integer[SIZE];
347 <            for (int i = 0; i < SIZE-1; ++i)
348 <                ints[i] = new Integer(i);
349 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
384 >            new LinkedBlockingDeque(elements);
385              shouldThrow();
386          } catch (NullPointerException success) {}
387      }
# Line 357 | Line 392 | public class LinkedBlockingDequeTest ext
392      public void testConstructor6() {
393          Integer[] ints = new Integer[SIZE];
394          for (int i = 0; i < SIZE; ++i)
395 <            ints[i] = new Integer(i);
395 >            ints[i] = i;
396          LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
397          for (int i = 0; i < SIZE; ++i)
398              assertEquals(ints[i], q.poll());
# Line 382 | Line 417 | public class LinkedBlockingDequeTest ext
417       * remainingCapacity decreases on add, increases on remove
418       */
419      public void testRemainingCapacity() {
420 <        LinkedBlockingDeque q = populatedDeque(SIZE);
420 >        BlockingQueue q = populatedDeque(SIZE);
421          for (int i = 0; i < SIZE; ++i) {
422              assertEquals(i, q.remainingCapacity());
423 <            assertEquals(SIZE-i, q.size());
424 <            q.remove();
423 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
424 >            assertEquals(i, q.remove());
425          }
426          for (int i = 0; i < SIZE; ++i) {
427 <            assertEquals(SIZE-i, q.remainingCapacity());
428 <            assertEquals(i, q.size());
429 <            q.add(new Integer(i));
427 >            assertEquals(SIZE - i, q.remainingCapacity());
428 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
429 >            assertTrue(q.add(i));
430          }
431      }
432  
433      /**
399     * offer(null) throws NPE
400     */
401    public void testOfferNull() {
402        try {
403            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
404            q.offer(null);
405            shouldThrow();
406        } catch (NullPointerException success) {}
407    }
408
409    /**
410     * add(null) throws NPE
411     */
412    public void testAddNull() {
413        try {
414            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
415            q.add(null);
416            shouldThrow();
417        } catch (NullPointerException success) {}
418    }
419
420    /**
434       * push(null) throws NPE
435       */
436      public void testPushNull() {
437 +        LinkedBlockingDeque q = new LinkedBlockingDeque(1);
438          try {
425            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
439              q.push(null);
440              shouldThrow();
441          } catch (NullPointerException success) {}
442      }
443  
444      /**
445 <     * push succeeds if not full; throws ISE if full
445 >     * push succeeds if not full; throws IllegalStateException if full
446       */
447      public void testPush() {
448 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
449 +        for (int i = 0; i < SIZE; ++i) {
450 +            Integer x = new Integer(i);
451 +            q.push(x);
452 +            assertEquals(x, q.peek());
453 +        }
454 +        assertEquals(0, q.remainingCapacity());
455          try {
436            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
437            for (int i = 0; i < SIZE; ++i) {
438                Integer I = new Integer(i);
439                q.push(I);
440                assertEquals(I, q.peek());
441            }
442            assertEquals(0, q.remainingCapacity());
456              q.push(new Integer(SIZE));
457              shouldThrow();
458          } catch (IllegalStateException success) {}
# Line 479 | Line 492 | public class LinkedBlockingDequeTest ext
492      }
493  
494      /**
495 <     * add succeeds if not full; throws ISE if full
495 >     * add succeeds if not full; throws IllegalStateException if full
496       */
497      public void testAdd() {
498 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
499 +        for (int i = 0; i < SIZE; ++i)
500 +            assertTrue(q.add(new Integer(i)));
501 +        assertEquals(0, q.remainingCapacity());
502          try {
486            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
487            for (int i = 0; i < SIZE; ++i) {
488                assertTrue(q.add(new Integer(i)));
489            }
490            assertEquals(0, q.remainingCapacity());
503              q.add(new Integer(SIZE));
504              shouldThrow();
505          } catch (IllegalStateException success) {}
506      }
507  
508      /**
509 <     * addAll(null) throws NPE
498 <     */
499 <    public void testAddAll1() {
500 <        try {
501 <            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
502 <            q.addAll(null);
503 <            shouldThrow();
504 <        } catch (NullPointerException success) {}
505 <    }
506 <
507 <    /**
508 <     * addAll(this) throws IAE
509 >     * addAll(this) throws IllegalArgumentException
510       */
511      public void testAddAllSelf() {
512 +        LinkedBlockingDeque q = populatedDeque(SIZE);
513          try {
512            LinkedBlockingDeque q = populatedDeque(SIZE);
514              q.addAll(q);
515              shouldThrow();
516          } catch (IllegalArgumentException success) {}
517      }
518  
519      /**
519     * addAll of a collection with null elements throws NPE
520     */
521    public void testAddAll2() {
522        try {
523            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
524            Integer[] ints = new Integer[SIZE];
525            q.addAll(Arrays.asList(ints));
526            shouldThrow();
527        } catch (NullPointerException success) {}
528    }
529
530    /**
520       * addAll of a collection with any null elements throws NPE after
521       * possibly adding some elements
522       */
523      public void testAddAll3() {
524 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
525 +        Integer[] ints = new Integer[SIZE];
526 +        for (int i = 0; i < SIZE - 1; ++i)
527 +            ints[i] = new Integer(i);
528 +        Collection<Integer> elements = Arrays.asList(ints);
529          try {
530 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
537 <            Integer[] ints = new Integer[SIZE];
538 <            for (int i = 0; i < SIZE-1; ++i)
539 <                ints[i] = new Integer(i);
540 <            q.addAll(Arrays.asList(ints));
530 >            q.addAll(elements);
531              shouldThrow();
532          } catch (NullPointerException success) {}
533      }
534  
535      /**
536 <     * addAll throws ISE if not enough room
536 >     * addAll throws IllegalStateException if not enough room
537       */
538      public void testAddAll4() {
539 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
540 +        Integer[] ints = new Integer[SIZE];
541 +        for (int i = 0; i < SIZE; ++i)
542 +            ints[i] = new Integer(i);
543 +        Collection<Integer> elements = Arrays.asList(ints);
544          try {
545 <            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
551 <            Integer[] ints = new Integer[SIZE];
552 <            for (int i = 0; i < SIZE; ++i)
553 <                ints[i] = new Integer(i);
554 <            q.addAll(Arrays.asList(ints));
545 >            q.addAll(elements);
546              shouldThrow();
547          } catch (IllegalStateException success) {}
548      }
# Line 572 | Line 563 | public class LinkedBlockingDequeTest ext
563      }
564  
565      /**
575     * put(null) throws NPE
576     */
577    public void testPutNull() throws InterruptedException {
578        try {
579            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
580            q.put(null);
581            shouldThrow();
582        } catch (NullPointerException success) {}
583    }
584
585    /**
566       * all elements successfully put are contained
567       */
568      public void testPut() throws InterruptedException {
569          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
570          for (int i = 0; i < SIZE; ++i) {
571 <            Integer I = new Integer(i);
572 <            q.put(I);
573 <            assertTrue(q.contains(I));
571 >            Integer x = new Integer(i);
572 >            q.put(x);
573 >            assertTrue(q.contains(x));
574          }
575          assertEquals(0, q.remainingCapacity());
576      }
# Line 624 | Line 604 | public class LinkedBlockingDequeTest ext
604              }});
605  
606          await(pleaseInterrupt);
607 <        assertThreadStaysAlive(t);
607 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
608          t.interrupt();
609          awaitTermination(t);
610          assertEquals(SIZE, q.size());
# Line 646 | Line 626 | public class LinkedBlockingDequeTest ext
626                  pleaseTake.countDown();
627                  q.put(86);
628  
629 +                Thread.currentThread().interrupt();
630 +                try {
631 +                    q.put(99);
632 +                    shouldThrow();
633 +                } catch (InterruptedException success) {}
634 +                assertFalse(Thread.interrupted());
635 +
636                  pleaseInterrupt.countDown();
637                  try {
638                      q.put(99);
# Line 655 | Line 642 | public class LinkedBlockingDequeTest ext
642              }});
643  
644          await(pleaseTake);
645 <        assertEquals(q.remainingCapacity(), 0);
645 >        assertEquals(0, q.remainingCapacity());
646          assertEquals(0, q.take());
647  
648          await(pleaseInterrupt);
649 <        assertThreadStaysAlive(t);
649 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
650          t.interrupt();
651          awaitTermination(t);
652 <        assertEquals(q.remainingCapacity(), 0);
652 >        assertEquals(0, q.remainingCapacity());
653      }
654  
655      /**
656       * timed offer times out if full and elements not taken
657       */
658 <    public void testTimedOffer() throws InterruptedException {
658 >    public void testTimedOffer() {
659          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
660          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
661          Thread t = newStartedThread(new CheckedRunnable() {
# Line 676 | Line 663 | public class LinkedBlockingDequeTest ext
663                  q.put(new Object());
664                  q.put(new Object());
665                  long startTime = System.nanoTime();
666 +
667                  assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
668                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
669 +
670 +                Thread.currentThread().interrupt();
671 +                try {
672 +                    q.offer(new Object(), randomTimeout(), randomTimeUnit());
673 +                    shouldThrow();
674 +                } catch (InterruptedException success) {}
675 +                assertFalse(Thread.interrupted());
676 +
677                  pleaseInterrupt.countDown();
678                  try {
679 <                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
679 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
680                      shouldThrow();
681                  } catch (InterruptedException success) {}
682 +                assertFalse(Thread.interrupted());
683 +
684 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
685              }});
686  
687          await(pleaseInterrupt);
688 <        assertThreadStaysAlive(t);
688 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
689          t.interrupt();
690          awaitTermination(t);
691      }
# Line 709 | Line 708 | public class LinkedBlockingDequeTest ext
708          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
709          Thread t = newStartedThread(new CheckedRunnable() {
710              public void realRun() throws InterruptedException {
711 <                for (int i = 0; i < SIZE; ++i) {
713 <                    assertEquals(i, q.take());
714 <                }
711 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
712  
713                  Thread.currentThread().interrupt();
714                  try {
# Line 729 | Line 726 | public class LinkedBlockingDequeTest ext
726              }});
727  
728          await(pleaseInterrupt);
729 <        assertThreadStaysAlive(t);
729 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
730          t.interrupt();
731          awaitTermination(t);
732      }
# Line 778 | Line 775 | public class LinkedBlockingDequeTest ext
775       */
776      public void testInterruptedTimedPoll() throws InterruptedException {
777          final BlockingQueue<Integer> q = populatedDeque(SIZE);
778 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
778 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
779          Thread t = newStartedThread(new CheckedRunnable() {
780              public void realRun() throws InterruptedException {
781 <                for (int i = 0; i < SIZE; ++i) {
782 <                    long t0 = System.nanoTime();
781 >                long startTime = System.nanoTime();
782 >                for (int i = 0; i < SIZE; i++)
783                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
784 <                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
785 <                }
786 <                long t0 = System.nanoTime();
787 <                aboutToWait.countDown();
784 >
785 >                Thread.currentThread().interrupt();
786 >                try {
787 >                    q.poll(randomTimeout(), randomTimeUnit());
788 >                    shouldThrow();
789 >                } catch (InterruptedException success) {}
790 >                assertFalse(Thread.interrupted());
791 >
792 >                pleaseInterrupt.countDown();
793                  try {
794 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
794 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
795                      shouldThrow();
796 <                } catch (InterruptedException success) {
797 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
798 <                }
796 >                } catch (InterruptedException success) {}
797 >                assertFalse(Thread.interrupted());
798 >
799 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
800              }});
801  
802 <        aboutToWait.await();
803 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
802 >        await(pleaseInterrupt);
803 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
804          t.interrupt();
805 <        awaitTermination(t, MEDIUM_DELAY_MS);
805 >        awaitTermination(t);
806          checkEmpty(q);
807      }
808  
# Line 807 | Line 810 | public class LinkedBlockingDequeTest ext
810       * putFirst(null) throws NPE
811       */
812      public void testPutFirstNull() throws InterruptedException {
813 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
814          try {
811            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
815              q.putFirst(null);
816              shouldThrow();
817          } catch (NullPointerException success) {}
# Line 820 | Line 823 | public class LinkedBlockingDequeTest ext
823      public void testPutFirst() throws InterruptedException {
824          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
825          for (int i = 0; i < SIZE; ++i) {
826 <            Integer I = new Integer(i);
827 <            q.putFirst(I);
828 <            assertTrue(q.contains(I));
826 >            Integer x = new Integer(i);
827 >            q.putFirst(x);
828 >            assertTrue(q.contains(x));
829          }
830          assertEquals(0, q.remainingCapacity());
831      }
# Line 856 | Line 859 | public class LinkedBlockingDequeTest ext
859              }});
860  
861          await(pleaseInterrupt);
862 <        assertThreadStaysAlive(t);
862 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
863          t.interrupt();
864          awaitTermination(t);
865          assertEquals(SIZE, q.size());
# Line 887 | Line 890 | public class LinkedBlockingDequeTest ext
890              }});
891  
892          await(pleaseTake);
893 <        assertEquals(q.remainingCapacity(), 0);
893 >        assertEquals(0, q.remainingCapacity());
894          assertEquals(capacity - 1, q.take());
895  
896          await(pleaseInterrupt);
897 <        assertThreadStaysAlive(t);
897 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
898          t.interrupt();
899          awaitTermination(t);
900 <        assertEquals(q.remainingCapacity(), 0);
900 >        assertEquals(0, q.remainingCapacity());
901      }
902  
903      /**
904       * timed offerFirst times out if full and elements not taken
905       */
906 <    public void testTimedOfferFirst() throws InterruptedException {
906 >    public void testTimedOfferFirst() {
907          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
908          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
909          Thread t = newStartedThread(new CheckedRunnable() {
# Line 908 | Line 911 | public class LinkedBlockingDequeTest ext
911                  q.putFirst(new Object());
912                  q.putFirst(new Object());
913                  long startTime = System.nanoTime();
914 +
915                  assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
916                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
917 +
918 +                Thread.currentThread().interrupt();
919 +                try {
920 +                    q.offerFirst(new Object(), randomTimeout(), randomTimeUnit());
921 +                    shouldThrow();
922 +                } catch (InterruptedException success) {}
923 +                assertFalse(Thread.interrupted());
924 +
925                  pleaseInterrupt.countDown();
926                  try {
927 <                    q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
927 >                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
928                      shouldThrow();
929                  } catch (InterruptedException success) {}
930 +                assertFalse(Thread.interrupted());
931 +
932 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
933              }});
934  
935          await(pleaseInterrupt);
936 <        assertThreadStaysAlive(t);
936 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
937          t.interrupt();
938          awaitTermination(t);
939      }
# Line 950 | Line 965 | public class LinkedBlockingDequeTest ext
965              }});
966  
967          await(threadStarted);
968 <        assertThreadStaysAlive(t);
968 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
969          t.interrupt();
970          awaitTermination(t);
971      }
# Line 991 | Line 1006 | public class LinkedBlockingDequeTest ext
1006              }});
1007  
1008          await(threadStarted);
1009 <        assertThreadStaysAlive(t);
1009 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1010          t.interrupt();
1011          awaitTermination(t);
1012      }
# Line 1023 | Line 1038 | public class LinkedBlockingDequeTest ext
1038          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1039          Thread t = newStartedThread(new CheckedRunnable() {
1040              public void realRun() throws InterruptedException {
1041 <                for (int i = 0; i < SIZE; ++i) {
1027 <                    assertEquals(i, q.takeFirst());
1028 <                }
1041 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst());
1042  
1043                  Thread.currentThread().interrupt();
1044                  try {
# Line 1043 | Line 1056 | public class LinkedBlockingDequeTest ext
1056              }});
1057  
1058          await(pleaseInterrupt);
1059 <        assertThreadStaysAlive(t);
1059 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1060          t.interrupt();
1061          awaitTermination(t);
1062      }
# Line 1080 | Line 1093 | public class LinkedBlockingDequeTest ext
1093       * returning timeout status
1094       */
1095      public void testInterruptedTimedPollFirst() throws InterruptedException {
1096 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1097          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1098          Thread t = newStartedThread(new CheckedRunnable() {
1099              public void realRun() throws InterruptedException {
1100 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1101 <                for (int i = 0; i < SIZE; ++i) {
1100 >                long startTime = System.nanoTime();
1101 >                for (int i = 0; i < SIZE; i++)
1102                      assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1089                }
1103  
1104                  Thread.currentThread().interrupt();
1105                  try {
1106 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1106 >                    q.pollFirst(randomTimeout(), randomTimeUnit());
1107                      shouldThrow();
1108                  } catch (InterruptedException success) {}
1109                  assertFalse(Thread.interrupted());
1110  
1111                  pleaseInterrupt.countDown();
1112                  try {
1113 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1113 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1114                      shouldThrow();
1115                  } catch (InterruptedException success) {}
1116                  assertFalse(Thread.interrupted());
1117 +
1118 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1119              }});
1120  
1121          await(pleaseInterrupt);
1122 <        assertThreadStaysAlive(t);
1122 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1123          t.interrupt();
1124          awaitTermination(t);
1125      }
# Line 1128 | Line 1143 | public class LinkedBlockingDequeTest ext
1143  
1144                  Thread.currentThread().interrupt();
1145                  try {
1146 <                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1146 >                    q.pollFirst(randomTimeout(), randomTimeUnit());
1147                      shouldThrow();
1148                  } catch (InterruptedException success) {}
1149  
# Line 1137 | Line 1152 | public class LinkedBlockingDequeTest ext
1152                      q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1153                      shouldThrow();
1154                  } catch (InterruptedException success) {}
1155 +                assertFalse(Thread.interrupted());
1156 +
1157                  assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1158              }});
1159  
# Line 1145 | Line 1162 | public class LinkedBlockingDequeTest ext
1162          assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1163          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1164          barrier.await();
1165 <        assertThreadStaysAlive(t);
1165 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1166          t.interrupt();
1167          awaitTermination(t);
1168      }
# Line 1154 | Line 1171 | public class LinkedBlockingDequeTest ext
1171       * putLast(null) throws NPE
1172       */
1173      public void testPutLastNull() throws InterruptedException {
1174 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1175          try {
1158            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1176              q.putLast(null);
1177              shouldThrow();
1178          } catch (NullPointerException success) {}
# Line 1167 | Line 1184 | public class LinkedBlockingDequeTest ext
1184      public void testPutLast() throws InterruptedException {
1185          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1186          for (int i = 0; i < SIZE; ++i) {
1187 <            Integer I = new Integer(i);
1188 <            q.putLast(I);
1189 <            assertTrue(q.contains(I));
1187 >            Integer x = new Integer(i);
1188 >            q.putLast(x);
1189 >            assertTrue(q.contains(x));
1190          }
1191          assertEquals(0, q.remainingCapacity());
1192      }
# Line 1203 | Line 1220 | public class LinkedBlockingDequeTest ext
1220              }});
1221  
1222          await(pleaseInterrupt);
1223 <        assertThreadStaysAlive(t);
1223 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1224          t.interrupt();
1225          awaitTermination(t);
1226          assertEquals(SIZE, q.size());
# Line 1225 | Line 1242 | public class LinkedBlockingDequeTest ext
1242                  pleaseTake.countDown();
1243                  q.putLast(86);
1244  
1245 +                Thread.currentThread().interrupt();
1246 +                try {
1247 +                    q.putLast(99);
1248 +                    shouldThrow();
1249 +                } catch (InterruptedException success) {}
1250 +                assertFalse(Thread.interrupted());
1251 +
1252                  pleaseInterrupt.countDown();
1253                  try {
1254                      q.putLast(99);
# Line 1234 | Line 1258 | public class LinkedBlockingDequeTest ext
1258              }});
1259  
1260          await(pleaseTake);
1261 <        assertEquals(q.remainingCapacity(), 0);
1261 >        assertEquals(0, q.remainingCapacity());
1262          assertEquals(0, q.take());
1263  
1264          await(pleaseInterrupt);
1265 <        assertThreadStaysAlive(t);
1265 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1266          t.interrupt();
1267          awaitTermination(t);
1268 <        assertEquals(q.remainingCapacity(), 0);
1268 >        assertEquals(0, q.remainingCapacity());
1269      }
1270  
1271      /**
1272       * timed offerLast times out if full and elements not taken
1273       */
1274 <    public void testTimedOfferLast() throws InterruptedException {
1274 >    public void testTimedOfferLast() {
1275          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1276          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1277          Thread t = newStartedThread(new CheckedRunnable() {
# Line 1255 | Line 1279 | public class LinkedBlockingDequeTest ext
1279                  q.putLast(new Object());
1280                  q.putLast(new Object());
1281                  long startTime = System.nanoTime();
1282 +
1283                  assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1284                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1285 +
1286 +                Thread.currentThread().interrupt();
1287 +                try {
1288 +                    q.offerLast(new Object(), randomTimeout(), randomTimeUnit());
1289 +                    shouldThrow();
1290 +                } catch (InterruptedException success) {}
1291 +
1292                  pleaseInterrupt.countDown();
1293                  try {
1294 <                    q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1294 >                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1295                      shouldThrow();
1296                  } catch (InterruptedException success) {}
1297 +
1298 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1299              }});
1300  
1301          await(pleaseInterrupt);
1302 <        assertThreadStaysAlive(t);
1302 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1303          t.interrupt();
1304          awaitTermination(t);
1305      }
# Line 1276 | Line 1310 | public class LinkedBlockingDequeTest ext
1310      public void testTakeLast() throws InterruptedException {
1311          LinkedBlockingDeque q = populatedDeque(SIZE);
1312          for (int i = 0; i < SIZE; ++i) {
1313 <            assertEquals(SIZE-i-1, q.takeLast());
1313 >            assertEquals(SIZE - i - 1, q.takeLast());
1314          }
1315      }
1316  
# Line 1288 | Line 1322 | public class LinkedBlockingDequeTest ext
1322          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1323          Thread t = newStartedThread(new CheckedRunnable() {
1324              public void realRun() throws InterruptedException {
1325 <                for (int i = 0; i < SIZE; ++i) {
1326 <                    assertEquals(SIZE-i-1, q.takeLast());
1293 <                }
1325 >                for (int i = 0; i < SIZE; i++)
1326 >                    assertEquals(SIZE - i - 1, q.takeLast());
1327  
1328                  Thread.currentThread().interrupt();
1329                  try {
# Line 1308 | Line 1341 | public class LinkedBlockingDequeTest ext
1341              }});
1342  
1343          await(pleaseInterrupt);
1344 <        assertThreadStaysAlive(t);
1344 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1345          t.interrupt();
1346          awaitTermination(t);
1347      }
# Line 1319 | Line 1352 | public class LinkedBlockingDequeTest ext
1352      public void testTimedPollLast0() throws InterruptedException {
1353          LinkedBlockingDeque q = populatedDeque(SIZE);
1354          for (int i = 0; i < SIZE; ++i) {
1355 <            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1355 >            assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
1356          }
1357          assertNull(q.pollLast(0, MILLISECONDS));
1358      }
# Line 1331 | Line 1364 | public class LinkedBlockingDequeTest ext
1364          LinkedBlockingDeque q = populatedDeque(SIZE);
1365          for (int i = 0; i < SIZE; ++i) {
1366              long startTime = System.nanoTime();
1367 <            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1367 >            assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1368              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1369          }
1370          long startTime = System.nanoTime();
# Line 1345 | Line 1378 | public class LinkedBlockingDequeTest ext
1378       * returning timeout status
1379       */
1380      public void testInterruptedTimedPollLast() throws InterruptedException {
1381 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1382          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1383          Thread t = newStartedThread(new CheckedRunnable() {
1384              public void realRun() throws InterruptedException {
1385 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1386 <                for (int i = 0; i < SIZE; ++i) {
1387 <                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1388 <                }
1385 >                long startTime = System.nanoTime();
1386 >                for (int i = 0; i < SIZE; i++)
1387 >                    assertEquals(SIZE - i - 1,
1388 >                                 q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1389  
1390                  Thread.currentThread().interrupt();
1391                  try {
1392 <                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1392 >                    q.pollLast(randomTimeout(), randomTimeUnit());
1393                      shouldThrow();
1394                  } catch (InterruptedException success) {}
1395                  assertFalse(Thread.interrupted());
# Line 1366 | Line 1400 | public class LinkedBlockingDequeTest ext
1400                      shouldThrow();
1401                  } catch (InterruptedException success) {}
1402                  assertFalse(Thread.interrupted());
1403 +
1404 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1405              }});
1406  
1407          await(pleaseInterrupt);
1408 <        assertThreadStaysAlive(t);
1408 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1409          t.interrupt();
1410          awaitTermination(t);
1411 +        checkEmpty(q);
1412      }
1413  
1414      /**
# Line 1393 | Line 1430 | public class LinkedBlockingDequeTest ext
1430  
1431                  Thread.currentThread().interrupt();
1432                  try {
1433 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1433 >                    q.poll(randomTimeout(), randomTimeUnit());
1434                      shouldThrow();
1435                  } catch (InterruptedException success) {}
1436                  assertFalse(Thread.interrupted());
# Line 1404 | Line 1441 | public class LinkedBlockingDequeTest ext
1441                      shouldThrow();
1442                  } catch (InterruptedException success) {}
1443                  assertFalse(Thread.interrupted());
1444 +
1445 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1446              }});
1447  
1448          barrier.await();
# Line 1412 | Line 1451 | public class LinkedBlockingDequeTest ext
1451          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1452  
1453          barrier.await();
1454 <        assertThreadStaysAlive(t);
1454 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1455          t.interrupt();
1456          awaitTermination(t);
1457      }
# Line 1433 | Line 1472 | public class LinkedBlockingDequeTest ext
1472      }
1473  
1474      /**
1436     * remove(x) removes x and returns true if present
1437     */
1438    public void testRemoveElement() {
1439        LinkedBlockingDeque q = populatedDeque(SIZE);
1440        for (int i = 1; i < SIZE; i+=2) {
1441            assertTrue(q.contains(i));
1442            assertTrue(q.remove(i));
1443            assertFalse(q.contains(i));
1444            assertTrue(q.contains(i-1));
1445        }
1446        for (int i = 0; i < SIZE; i+=2) {
1447            assertTrue(q.contains(i));
1448            assertTrue(q.remove(i));
1449            assertFalse(q.contains(i));
1450            assertFalse(q.remove(i+1));
1451            assertFalse(q.contains(i+1));
1452        }
1453        assertTrue(q.isEmpty());
1454    }
1455
1456    /**
1475       * contains(x) reports true when elements added but not yet removed
1476       */
1477      public void testContains() {
# Line 1509 | Line 1527 | public class LinkedBlockingDequeTest ext
1527                  assertTrue(changed);
1528  
1529              assertTrue(q.containsAll(p));
1530 <            assertEquals(SIZE-i, q.size());
1530 >            assertEquals(SIZE - i, q.size());
1531              p.remove();
1532          }
1533      }
# Line 1522 | Line 1540 | public class LinkedBlockingDequeTest ext
1540              LinkedBlockingDeque q = populatedDeque(SIZE);
1541              LinkedBlockingDeque p = populatedDeque(i);
1542              assertTrue(q.removeAll(p));
1543 <            assertEquals(SIZE-i, q.size());
1543 >            assertEquals(SIZE - i, q.size());
1544              for (int j = 0; j < i; ++j) {
1545 <                Integer I = (Integer)(p.remove());
1546 <                assertFalse(q.contains(I));
1545 >                Integer x = (Integer)(p.remove());
1546 >                assertFalse(q.contains(x));
1547              }
1548          }
1549      }
# Line 1533 | Line 1551 | public class LinkedBlockingDequeTest ext
1551      /**
1552       * toArray contains all elements in FIFO order
1553       */
1554 <    public void testToArray() throws InterruptedException{
1554 >    public void testToArray() throws InterruptedException {
1555          LinkedBlockingDeque q = populatedDeque(SIZE);
1556 <        Object[] o = q.toArray();
1557 <        for (int i = 0; i < o.length; i++)
1558 <            assertSame(o[i], q.poll());
1556 >        Object[] a = q.toArray();
1557 >        assertSame(Object[].class, a.getClass());
1558 >        for (Object o : a)
1559 >            assertSame(o, q.poll());
1560 >        assertTrue(q.isEmpty());
1561      }
1562  
1563      /**
# Line 1548 | Line 1568 | public class LinkedBlockingDequeTest ext
1568          Integer[] ints = new Integer[SIZE];
1569          Integer[] array = q.toArray(ints);
1570          assertSame(ints, array);
1571 <        for (int i = 0; i < ints.length; i++)
1572 <            assertSame(ints[i], q.remove());
1573 <    }
1554 <
1555 <    /**
1556 <     * toArray(null) throws NullPointerException
1557 <     */
1558 <    public void testToArray_NullArg() {
1559 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1560 <        try {
1561 <            q.toArray(null);
1562 <            shouldThrow();
1563 <        } catch (NullPointerException success) {}
1571 >        for (Integer o : ints)
1572 >            assertSame(o, q.remove());
1573 >        assertTrue(q.isEmpty());
1574      }
1575  
1576      /**
# Line 1580 | Line 1590 | public class LinkedBlockingDequeTest ext
1590      public void testIterator() throws InterruptedException {
1591          LinkedBlockingDeque q = populatedDeque(SIZE);
1592          Iterator it = q.iterator();
1593 <        while (it.hasNext()) {
1593 >        int i;
1594 >        for (i = 0; it.hasNext(); i++)
1595 >            assertTrue(q.contains(it.next()));
1596 >        assertEquals(i, SIZE);
1597 >        assertIteratorExhausted(it);
1598 >
1599 >        it = q.iterator();
1600 >        for (i = 0; it.hasNext(); i++)
1601              assertEquals(it.next(), q.take());
1602 <        }
1602 >        assertEquals(i, SIZE);
1603 >        assertIteratorExhausted(it);
1604 >    }
1605 >
1606 >    /**
1607 >     * iterator of empty collection has no elements
1608 >     */
1609 >    public void testEmptyIterator() {
1610 >        Deque c = new LinkedBlockingDeque();
1611 >        assertIteratorExhausted(c.iterator());
1612 >        assertIteratorExhausted(c.descendingIterator());
1613      }
1614  
1615      /**
# Line 1715 | Line 1742 | public class LinkedBlockingDequeTest ext
1742          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1743          q.add(one);
1744          q.add(two);
1718        ExecutorService executor = Executors.newFixedThreadPool(2);
1745          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1746 <        executor.execute(new CheckedRunnable() {
1747 <            public void realRun() throws InterruptedException {
1748 <                assertFalse(q.offer(three));
1749 <                threadsStarted.await();
1750 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1751 <                assertEquals(0, q.remainingCapacity());
1752 <            }});
1753 <
1754 <        executor.execute(new CheckedRunnable() {
1755 <            public void realRun() throws InterruptedException {
1756 <                threadsStarted.await();
1757 <                assertSame(one, q.take());
1758 <            }});
1759 <
1760 <        joinPool(executor);
1746 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1747 >        try (PoolCleaner cleaner = cleaner(executor)) {
1748 >            executor.execute(new CheckedRunnable() {
1749 >                public void realRun() throws InterruptedException {
1750 >                    assertFalse(q.offer(three));
1751 >                    threadsStarted.await();
1752 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1753 >                    assertEquals(0, q.remainingCapacity());
1754 >                }});
1755 >
1756 >            executor.execute(new CheckedRunnable() {
1757 >                public void realRun() throws InterruptedException {
1758 >                    threadsStarted.await();
1759 >                    assertSame(one, q.take());
1760 >                }});
1761 >        }
1762      }
1763  
1764      /**
# Line 1740 | Line 1767 | public class LinkedBlockingDequeTest ext
1767      public void testPollInExecutor() {
1768          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1769          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1770 <        ExecutorService executor = Executors.newFixedThreadPool(2);
1771 <        executor.execute(new CheckedRunnable() {
1772 <            public void realRun() throws InterruptedException {
1773 <                assertNull(q.poll());
1774 <                threadsStarted.await();
1775 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1776 <                checkEmpty(q);
1777 <            }});
1778 <
1779 <        executor.execute(new CheckedRunnable() {
1780 <            public void realRun() throws InterruptedException {
1781 <                threadsStarted.await();
1782 <                q.put(one);
1783 <            }});
1784 <
1785 <        joinPool(executor);
1770 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1771 >        try (PoolCleaner cleaner = cleaner(executor)) {
1772 >            executor.execute(new CheckedRunnable() {
1773 >                public void realRun() throws InterruptedException {
1774 >                    assertNull(q.poll());
1775 >                    threadsStarted.await();
1776 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1777 >                    checkEmpty(q);
1778 >                }});
1779 >
1780 >            executor.execute(new CheckedRunnable() {
1781 >                public void realRun() throws InterruptedException {
1782 >                    threadsStarted.await();
1783 >                    q.put(one);
1784 >                }});
1785 >        }
1786      }
1787  
1788      /**
1789 <     * A deserialized serialized deque has same elements in same order
1789 >     * A deserialized/reserialized deque has same elements in same order
1790       */
1791      public void testSerialization() throws Exception {
1792 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1793 <
1767 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1768 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1769 <        out.writeObject(q);
1770 <        out.close();
1771 <
1772 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1773 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1774 <        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1775 <        assertEquals(q.size(), r.size());
1776 <        while (!q.isEmpty())
1777 <            assertEquals(q.remove(), r.remove());
1778 <    }
1779 <
1780 <    /**
1781 <     * drainTo(null) throws NPE
1782 <     */
1783 <    public void testDrainToNull() {
1784 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1785 <        try {
1786 <            q.drainTo(null);
1787 <            shouldThrow();
1788 <        } catch (NullPointerException success) {}
1789 <    }
1792 >        Queue x = populatedDeque(SIZE);
1793 >        Queue y = serialClone(x);
1794  
1795 <    /**
1796 <     * drainTo(this) throws IAE
1797 <     */
1798 <    public void testDrainToSelf() {
1799 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1800 <        try {
1801 <            q.drainTo(q);
1802 <            shouldThrow();
1803 <        } catch (IllegalArgumentException success) {}
1795 >        assertNotSame(y, x);
1796 >        assertEquals(x.size(), y.size());
1797 >        assertEquals(x.toString(), y.toString());
1798 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1799 >        while (!x.isEmpty()) {
1800 >            assertFalse(y.isEmpty());
1801 >            assertEquals(x.remove(), y.remove());
1802 >        }
1803 >        assertTrue(y.isEmpty());
1804      }
1805  
1806      /**
# Line 1806 | Line 1810 | public class LinkedBlockingDequeTest ext
1810          LinkedBlockingDeque q = populatedDeque(SIZE);
1811          ArrayList l = new ArrayList();
1812          q.drainTo(l);
1813 <        assertEquals(q.size(), 0);
1814 <        assertEquals(l.size(), SIZE);
1813 >        assertEquals(0, q.size());
1814 >        assertEquals(SIZE, l.size());
1815          for (int i = 0; i < SIZE; ++i)
1816              assertEquals(l.get(i), new Integer(i));
1817          q.add(zero);
# Line 1817 | Line 1821 | public class LinkedBlockingDequeTest ext
1821          assertTrue(q.contains(one));
1822          l.clear();
1823          q.drainTo(l);
1824 <        assertEquals(q.size(), 0);
1825 <        assertEquals(l.size(), 2);
1824 >        assertEquals(0, q.size());
1825 >        assertEquals(2, l.size());
1826          for (int i = 0; i < 2; ++i)
1827              assertEquals(l.get(i), new Integer(i));
1828      }
# Line 1830 | Line 1834 | public class LinkedBlockingDequeTest ext
1834          final LinkedBlockingDeque q = populatedDeque(SIZE);
1835          Thread t = new Thread(new CheckedRunnable() {
1836              public void realRun() throws InterruptedException {
1837 <                q.put(new Integer(SIZE+1));
1837 >                q.put(new Integer(SIZE + 1));
1838              }});
1839  
1840          t.start();
# Line 1844 | Line 1848 | public class LinkedBlockingDequeTest ext
1848      }
1849  
1850      /**
1847     * drainTo(null, n) throws NPE
1848     */
1849    public void testDrainToNullN() {
1850        LinkedBlockingDeque q = populatedDeque(SIZE);
1851        try {
1852            q.drainTo(null, 0);
1853            shouldThrow();
1854        } catch (NullPointerException success) {}
1855    }
1856
1857    /**
1858     * drainTo(this, n) throws IAE
1859     */
1860    public void testDrainToSelfN() {
1861        LinkedBlockingDeque q = populatedDeque(SIZE);
1862        try {
1863            q.drainTo(q, 0);
1864            shouldThrow();
1865        } catch (IllegalArgumentException success) {}
1866    }
1867
1868    /**
1851       * drainTo(c, n) empties first min(n, size) elements of queue into c
1852       */
1853      public void testDrainToN() {
# Line 1876 | Line 1858 | public class LinkedBlockingDequeTest ext
1858              ArrayList l = new ArrayList();
1859              q.drainTo(l, i);
1860              int k = (i < SIZE) ? i : SIZE;
1861 <            assertEquals(l.size(), k);
1862 <            assertEquals(q.size(), SIZE-k);
1861 >            assertEquals(k, l.size());
1862 >            assertEquals(SIZE - k, q.size());
1863              for (int j = 0; j < k; ++j)
1864                  assertEquals(l.get(j), new Integer(j));
1865 <            while (q.poll() != null) ;
1865 >            do {} while (q.poll() != null);
1866 >        }
1867 >    }
1868 >
1869 >    /**
1870 >     * remove(null), contains(null) always return false
1871 >     */
1872 >    public void testNeverContainsNull() {
1873 >        Deque<?>[] qs = {
1874 >            new LinkedBlockingDeque<Object>(),
1875 >            populatedDeque(2),
1876 >        };
1877 >
1878 >        for (Deque<?> q : qs) {
1879 >            assertFalse(q.contains(null));
1880 >            assertFalse(q.remove(null));
1881 >            assertFalse(q.removeFirstOccurrence(null));
1882 >            assertFalse(q.removeLastOccurrence(null));
1883          }
1884      }
1885  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines