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.38 by jsr166, Sat May 21 06:24:33 2011 UTC vs.
Revision 1.50 by jsr166, Sun Nov 23 22:27:06 2014 UTC

# Line 5 | Line 5
5   */
6  
7   import junit.framework.*;
8 < import java.util.*;
9 < import java.util.concurrent.*;
8 > import java.util.Arrays;
9 > import java.util.ArrayList;
10 > import java.util.Collection;
11 > import java.util.Deque;
12 > import java.util.Iterator;
13 > import java.util.NoSuchElementException;
14 > import java.util.Queue;
15 > import java.util.concurrent.BlockingDeque;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.CountDownLatch;
18 > import java.util.concurrent.Executors;
19 > import java.util.concurrent.ExecutorService;
20 > import java.util.concurrent.LinkedBlockingDeque;
21   import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 import java.io.*;
22  
23   public class LinkedBlockingDequeTest extends JSR166TestCase {
24  
# Line 20 | Line 30 | public class LinkedBlockingDequeTest ext
30  
31      public static class Bounded extends BlockingQueueTest {
32          protected BlockingQueue emptyCollection() {
33 <            return new LinkedBlockingDeque(20);
33 >            return new LinkedBlockingDeque(SIZE);
34          }
35      }
36  
# Line 35 | Line 45 | public class LinkedBlockingDequeTest ext
45      }
46  
47      /**
48 <     * Create a deque of given size containing consecutive
48 >     * Returns a new deque of given size containing consecutive
49       * Integers 0 ... n.
50       */
51      private LinkedBlockingDeque<Integer> populatedDeque(int n) {
# Line 80 | Line 90 | public class LinkedBlockingDequeTest ext
90      }
91  
92      /**
93 <     * offer(null) throws NPE
93 >     * offerFirst(null) throws NullPointerException
94       */
95      public void testOfferFirstNull() {
96 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
97          try {
87            LinkedBlockingDeque q = new LinkedBlockingDeque();
98              q.offerFirst(null);
99              shouldThrow();
100          } catch (NullPointerException success) {}
101      }
102  
103      /**
104 +     * offerLast(null) throws NullPointerException
105 +     */
106 +    public void testOfferLastNull() {
107 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
108 +        try {
109 +            q.offerLast(null);
110 +            shouldThrow();
111 +        } catch (NullPointerException success) {}
112 +    }
113 +
114 +    /**
115       * OfferFirst succeeds
116       */
117      public void testOfferFirst() {
# Line 298 | Line 319 | public class LinkedBlockingDequeTest ext
319          assertSame(four, q.peekLast());
320      }
321  
301
322      /**
323       * A new deque has the indicated capacity, or Integer.MAX_VALUE if
324       * none given
# Line 309 | Line 329 | public class LinkedBlockingDequeTest ext
329      }
330  
331      /**
332 <     * Constructor throws IAE if capacity argument nonpositive
332 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
333       */
334      public void testConstructor2() {
335          try {
336 <            LinkedBlockingDeque q = new LinkedBlockingDeque(0);
336 >            new LinkedBlockingDeque(0);
337              shouldThrow();
338          } catch (IllegalArgumentException success) {}
339      }
340  
341      /**
342 <     * Initializing from null Collection throws NPE
342 >     * Initializing from null Collection throws NullPointerException
343       */
344      public void testConstructor3() {
345          try {
346 <            LinkedBlockingDeque q = new LinkedBlockingDeque(null);
346 >            new LinkedBlockingDeque(null);
347              shouldThrow();
348          } catch (NullPointerException success) {}
349      }
350  
351      /**
352 <     * Initializing from Collection of null elements throws NPE
352 >     * Initializing from Collection of null elements throws NullPointerException
353       */
354      public void testConstructor4() {
355 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
356          try {
357 <            Integer[] ints = new Integer[SIZE];
337 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
357 >            new LinkedBlockingDeque(elements);
358              shouldThrow();
359          } catch (NullPointerException success) {}
360      }
361  
362      /**
363 <     * Initializing from Collection with some null elements throws NPE
363 >     * Initializing from Collection with some null elements throws
364 >     * NullPointerException
365       */
366      public void testConstructor5() {
367 +        Integer[] ints = new Integer[SIZE];
368 +        for (int i = 0; i < SIZE-1; ++i)
369 +            ints[i] = i;
370 +        Collection<Integer> elements = Arrays.asList(ints);
371          try {
372 <            Integer[] ints = new Integer[SIZE];
348 <            for (int i = 0; i < SIZE-1; ++i)
349 <                ints[i] = new Integer(i);
350 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
372 >            new LinkedBlockingDeque(elements);
373              shouldThrow();
374          } catch (NullPointerException success) {}
375      }
# Line 358 | Line 380 | public class LinkedBlockingDequeTest ext
380      public void testConstructor6() {
381          Integer[] ints = new Integer[SIZE];
382          for (int i = 0; i < SIZE; ++i)
383 <            ints[i] = new Integer(i);
383 >            ints[i] = i;
384          LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
385          for (int i = 0; i < SIZE; ++i)
386              assertEquals(ints[i], q.poll());
# Line 397 | Line 419 | public class LinkedBlockingDequeTest ext
419      }
420  
421      /**
400     * offer(null) throws NPE
401     */
402    public void testOfferNull() {
403        try {
404            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
405            q.offer(null);
406            shouldThrow();
407        } catch (NullPointerException success) {}
408    }
409
410    /**
411     * add(null) throws NPE
412     */
413    public void testAddNull() {
414        try {
415            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
416            q.add(null);
417            shouldThrow();
418        } catch (NullPointerException success) {}
419    }
420
421    /**
422       * push(null) throws NPE
423       */
424      public void testPushNull() {
# Line 456 | Line 456 | public class LinkedBlockingDequeTest ext
456          assertSame(four, q.peekFirst());
457      }
458  
459
459      /**
460       * pop removes next element, or throws NSEE if empty
461       */
# Line 471 | Line 470 | public class LinkedBlockingDequeTest ext
470          } catch (NoSuchElementException success) {}
471      }
472  
474
473      /**
474       * Offer succeeds if not full; fails if full
475       */
# Line 485 | Line 483 | public class LinkedBlockingDequeTest ext
483       * add succeeds if not full; throws ISE if full
484       */
485      public void testAdd() {
486 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
487 +        for (int i = 0; i < SIZE; ++i)
488 +            assertTrue(q.add(new Integer(i)));
489 +        assertEquals(0, q.remainingCapacity());
490          try {
489            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
490            for (int i = 0; i < SIZE; ++i) {
491                assertTrue(q.add(new Integer(i)));
492            }
493            assertEquals(0, q.remainingCapacity());
491              q.add(new Integer(SIZE));
492              shouldThrow();
493          } catch (IllegalStateException success) {}
494      }
495  
496      /**
500     * addAll(null) throws NPE
501     */
502    public void testAddAll1() {
503        try {
504            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
505            q.addAll(null);
506            shouldThrow();
507        } catch (NullPointerException success) {}
508    }
509
510    /**
497       * addAll(this) throws IAE
498       */
499      public void testAddAllSelf() {
500 +        LinkedBlockingDeque q = populatedDeque(SIZE);
501          try {
515            LinkedBlockingDeque q = populatedDeque(SIZE);
502              q.addAll(q);
503              shouldThrow();
504          } catch (IllegalArgumentException success) {}
505      }
506  
507      /**
522     * addAll of a collection with null elements throws NPE
523     */
524    public void testAddAll2() {
525        try {
526            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
527            Integer[] ints = new Integer[SIZE];
528            q.addAll(Arrays.asList(ints));
529            shouldThrow();
530        } catch (NullPointerException success) {}
531    }
532
533    /**
508       * addAll of a collection with any null elements throws NPE after
509       * possibly adding some elements
510       */
511      public void testAddAll3() {
512 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
513 +        Integer[] ints = new Integer[SIZE];
514 +        for (int i = 0; i < SIZE-1; ++i)
515 +            ints[i] = new Integer(i);
516 +        Collection<Integer> elements = Arrays.asList(ints);
517          try {
518 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
540 <            Integer[] ints = new Integer[SIZE];
541 <            for (int i = 0; i < SIZE-1; ++i)
542 <                ints[i] = new Integer(i);
543 <            q.addAll(Arrays.asList(ints));
518 >            q.addAll(elements);
519              shouldThrow();
520          } catch (NullPointerException success) {}
521      }
522  
523      /**
524 <     * addAll throws ISE if not enough room
524 >     * addAll throws IllegalStateException if not enough room
525       */
526      public void testAddAll4() {
527 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
528 +        Integer[] ints = new Integer[SIZE];
529 +        for (int i = 0; i < SIZE; ++i)
530 +            ints[i] = new Integer(i);
531 +        Collection<Integer> elements = Arrays.asList(ints);
532          try {
533 <            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
554 <            Integer[] ints = new Integer[SIZE];
555 <            for (int i = 0; i < SIZE; ++i)
556 <                ints[i] = new Integer(i);
557 <            q.addAll(Arrays.asList(ints));
533 >            q.addAll(elements);
534              shouldThrow();
535          } catch (IllegalStateException success) {}
536      }
# Line 574 | Line 550 | public class LinkedBlockingDequeTest ext
550              assertEquals(ints[i], q.poll());
551      }
552  
577
578    /**
579     * put(null) throws NPE
580     */
581    public void testPutNull() throws InterruptedException {
582        try {
583            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
584            q.put(null);
585            shouldThrow();
586        } catch (NullPointerException success) {}
587    }
588
553      /**
554       * all elements successfully put are contained
555       */
# Line 604 | Line 568 | public class LinkedBlockingDequeTest ext
568       */
569      public void testBlockingPut() throws InterruptedException {
570          final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
571 <        Thread t = new Thread(new CheckedRunnable() {
571 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
572 >        Thread t = newStartedThread(new CheckedRunnable() {
573              public void realRun() throws InterruptedException {
574                  for (int i = 0; i < SIZE; ++i)
575                      q.put(i);
576                  assertEquals(SIZE, q.size());
577                  assertEquals(0, q.remainingCapacity());
578 +
579 +                Thread.currentThread().interrupt();
580 +                try {
581 +                    q.put(99);
582 +                    shouldThrow();
583 +                } catch (InterruptedException success) {}
584 +                assertFalse(Thread.interrupted());
585 +
586 +                pleaseInterrupt.countDown();
587                  try {
588                      q.put(99);
589                      shouldThrow();
590                  } catch (InterruptedException success) {}
591 +                assertFalse(Thread.interrupted());
592              }});
593  
594 <        t.start();
595 <        delay(SHORT_DELAY_MS);
594 >        await(pleaseInterrupt);
595 >        assertThreadStaysAlive(t);
596          t.interrupt();
597 <        t.join();
597 >        awaitTermination(t);
598          assertEquals(SIZE, q.size());
599          assertEquals(0, q.remainingCapacity());
600      }
601  
602      /**
603 <     * put blocks waiting for take when full
603 >     * put blocks interruptibly waiting for take when full
604       */
605      public void testPutWithTake() throws InterruptedException {
606          final int capacity = 2;
607          final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
608 <        Thread t = new Thread(new CheckedRunnable() {
608 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
609 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
610 >        Thread t = newStartedThread(new CheckedRunnable() {
611              public void realRun() throws InterruptedException {
612 <                for (int i = 0; i < capacity + 1; i++)
612 >                for (int i = 0; i < capacity; i++)
613                      q.put(i);
614 +                pleaseTake.countDown();
615 +                q.put(86);
616 +
617 +                pleaseInterrupt.countDown();
618                  try {
619                      q.put(99);
620                      shouldThrow();
621                  } catch (InterruptedException success) {}
622 +                assertFalse(Thread.interrupted());
623              }});
624  
625 <        t.start();
626 <        delay(SHORT_DELAY_MS);
645 <        assertEquals(q.remainingCapacity(), 0);
625 >        await(pleaseTake);
626 >        assertEquals(0, q.remainingCapacity());
627          assertEquals(0, q.take());
628 <        delay(SHORT_DELAY_MS);
628 >
629 >        await(pleaseInterrupt);
630 >        assertThreadStaysAlive(t);
631          t.interrupt();
632 <        t.join();
633 <        assertEquals(q.remainingCapacity(), 0);
632 >        awaitTermination(t);
633 >        assertEquals(0, q.remainingCapacity());
634      }
635  
636      /**
# Line 671 | Line 654 | public class LinkedBlockingDequeTest ext
654              }});
655  
656          await(pleaseInterrupt);
657 +        assertThreadStaysAlive(t);
658          t.interrupt();
659          awaitTermination(t);
660      }
# Line 686 | Line 670 | public class LinkedBlockingDequeTest ext
670      }
671  
672      /**
673 <     * Take removes existing elements until empty, then blocks interruptibly
673 >     * take removes existing elements until empty, then blocks interruptibly
674       */
675      public void testBlockingTake() throws InterruptedException {
676          final LinkedBlockingDeque q = populatedDeque(SIZE);
677 <        Thread t = new Thread(new CheckedRunnable() {
677 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
678 >        Thread t = newStartedThread(new CheckedRunnable() {
679              public void realRun() throws InterruptedException {
680                  for (int i = 0; i < SIZE; ++i) {
681                      assertEquals(i, q.take());
682                  }
683 +
684 +                Thread.currentThread().interrupt();
685                  try {
686                      q.take();
687                      shouldThrow();
688                  } catch (InterruptedException success) {}
689 +                assertFalse(Thread.interrupted());
690 +
691 +                pleaseInterrupt.countDown();
692 +                try {
693 +                    q.take();
694 +                    shouldThrow();
695 +                } catch (InterruptedException success) {}
696 +                assertFalse(Thread.interrupted());
697              }});
698  
699 <        t.start();
700 <        delay(SHORT_DELAY_MS);
699 >        await(pleaseInterrupt);
700 >        assertThreadStaysAlive(t);
701          t.interrupt();
702 <        t.join();
702 >        awaitTermination(t);
703      }
704  
710
705      /**
706       * poll succeeds unless empty
707       */
# Line 736 | Line 730 | public class LinkedBlockingDequeTest ext
730      public void testTimedPoll() throws InterruptedException {
731          LinkedBlockingDeque q = populatedDeque(SIZE);
732          for (int i = 0; i < SIZE; ++i) {
733 <            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
734 <        }
735 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
733 >            long startTime = System.nanoTime();
734 >            assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
735 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
736 >        }
737 >        long startTime = System.nanoTime();
738 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
739 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
740 >        checkEmpty(q);
741      }
742  
743      /**
# Line 776 | Line 775 | public class LinkedBlockingDequeTest ext
775       * putFirst(null) throws NPE
776       */
777      public void testPutFirstNull() throws InterruptedException {
778 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
779          try {
780            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
780              q.putFirst(null);
781              shouldThrow();
782          } catch (NullPointerException success) {}
# Line 801 | Line 800 | public class LinkedBlockingDequeTest ext
800       */
801      public void testBlockingPutFirst() throws InterruptedException {
802          final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
803 <        Thread t = new Thread(new CheckedRunnable() {
803 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
804 >        Thread t = newStartedThread(new CheckedRunnable() {
805              public void realRun() throws InterruptedException {
806                  for (int i = 0; i < SIZE; ++i)
807                      q.putFirst(i);
808                  assertEquals(SIZE, q.size());
809                  assertEquals(0, q.remainingCapacity());
810 +
811 +                Thread.currentThread().interrupt();
812                  try {
813                      q.putFirst(99);
814                      shouldThrow();
815                  } catch (InterruptedException success) {}
816 +                assertFalse(Thread.interrupted());
817 +
818 +                pleaseInterrupt.countDown();
819 +                try {
820 +                    q.putFirst(99);
821 +                    shouldThrow();
822 +                } catch (InterruptedException success) {}
823 +                assertFalse(Thread.interrupted());
824              }});
825  
826 <        t.start();
827 <        delay(SHORT_DELAY_MS);
826 >        await(pleaseInterrupt);
827 >        assertThreadStaysAlive(t);
828          t.interrupt();
829 <        t.join();
829 >        awaitTermination(t);
830          assertEquals(SIZE, q.size());
831          assertEquals(0, q.remainingCapacity());
832      }
833  
834      /**
835 <     * putFirst blocks waiting for take when full
835 >     * putFirst blocks interruptibly waiting for take when full
836       */
837      public void testPutFirstWithTake() throws InterruptedException {
838          final int capacity = 2;
839          final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
840 <        Thread t = new Thread(new CheckedRunnable() {
840 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
841 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
842 >        Thread t = newStartedThread(new CheckedRunnable() {
843              public void realRun() throws InterruptedException {
844 <                for (int i = 0; i < capacity + 1; i++)
844 >                for (int i = 0; i < capacity; i++)
845                      q.putFirst(i);
846 +                pleaseTake.countDown();
847 +                q.putFirst(86);
848 +
849 +                pleaseInterrupt.countDown();
850                  try {
851                      q.putFirst(99);
852                      shouldThrow();
853                  } catch (InterruptedException success) {}
854 +                assertFalse(Thread.interrupted());
855              }});
856  
857 <        t.start();
858 <        delay(SHORT_DELAY_MS);
842 <        assertEquals(q.remainingCapacity(), 0);
857 >        await(pleaseTake);
858 >        assertEquals(0, q.remainingCapacity());
859          assertEquals(capacity - 1, q.take());
860 <        delay(SHORT_DELAY_MS);
860 >
861 >        await(pleaseInterrupt);
862 >        assertThreadStaysAlive(t);
863          t.interrupt();
864 <        t.join();
865 <        assertEquals(q.remainingCapacity(), 0);
864 >        awaitTermination(t);
865 >        assertEquals(0, q.remainingCapacity());
866      }
867  
868      /**
# Line 868 | Line 886 | public class LinkedBlockingDequeTest ext
886              }});
887  
888          await(pleaseInterrupt);
889 +        assertThreadStaysAlive(t);
890          t.interrupt();
891          awaitTermination(t);
892      }
# Line 883 | Line 902 | public class LinkedBlockingDequeTest ext
902      }
903  
904      /**
905 <     * takeFirst blocks interruptibly when empty
905 >     * takeFirst() blocks interruptibly when empty
906       */
907 <    public void testTakeFirstFromEmpty() throws InterruptedException {
908 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
909 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
910 <            public void realRun() throws InterruptedException {
911 <                q.takeFirst();
912 <            }};
907 >    public void testTakeFirstFromEmptyBlocksInterruptibly() {
908 >        final BlockingDeque q = new LinkedBlockingDeque();
909 >        final CountDownLatch threadStarted = new CountDownLatch(1);
910 >        Thread t = newStartedThread(new CheckedRunnable() {
911 >            public void realRun() {
912 >                threadStarted.countDown();
913 >                try {
914 >                    q.takeFirst();
915 >                    shouldThrow();
916 >                } catch (InterruptedException success) {}
917 >                assertFalse(Thread.interrupted());
918 >            }});
919  
920 <        t.start();
921 <        delay(SHORT_DELAY_MS);
920 >        await(threadStarted);
921 >        assertThreadStaysAlive(t);
922          t.interrupt();
923 <        t.join();
923 >        awaitTermination(t);
924      }
925  
926      /**
927 <     * TakeFirst removes existing elements until empty, then blocks interruptibly
927 >     * takeFirst() throws InterruptedException immediately if interrupted
928 >     * before waiting
929 >     */
930 >    public void testTakeFirstFromEmptyAfterInterrupt() {
931 >        final BlockingDeque q = new LinkedBlockingDeque();
932 >        Thread t = newStartedThread(new CheckedRunnable() {
933 >            public void realRun() {
934 >                Thread.currentThread().interrupt();
935 >                try {
936 >                    q.takeFirst();
937 >                    shouldThrow();
938 >                } catch (InterruptedException success) {}
939 >                assertFalse(Thread.interrupted());
940 >            }});
941 >
942 >        awaitTermination(t);
943 >    }
944 >
945 >    /**
946 >     * takeLast() blocks interruptibly when empty
947 >     */
948 >    public void testTakeLastFromEmptyBlocksInterruptibly() {
949 >        final BlockingDeque q = new LinkedBlockingDeque();
950 >        final CountDownLatch threadStarted = new CountDownLatch(1);
951 >        Thread t = newStartedThread(new CheckedRunnable() {
952 >            public void realRun() {
953 >                threadStarted.countDown();
954 >                try {
955 >                    q.takeLast();
956 >                    shouldThrow();
957 >                } catch (InterruptedException success) {}
958 >                assertFalse(Thread.interrupted());
959 >            }});
960 >
961 >        await(threadStarted);
962 >        assertThreadStaysAlive(t);
963 >        t.interrupt();
964 >        awaitTermination(t);
965 >    }
966 >
967 >    /**
968 >     * takeLast() throws InterruptedException immediately if interrupted
969 >     * before waiting
970 >     */
971 >    public void testTakeLastFromEmptyAfterInterrupt() {
972 >        final BlockingDeque q = new LinkedBlockingDeque();
973 >        Thread t = newStartedThread(new CheckedRunnable() {
974 >            public void realRun() {
975 >                Thread.currentThread().interrupt();
976 >                try {
977 >                    q.takeLast();
978 >                    shouldThrow();
979 >                } catch (InterruptedException success) {}
980 >                assertFalse(Thread.interrupted());
981 >            }});
982 >
983 >        awaitTermination(t);
984 >    }
985 >
986 >    /**
987 >     * takeFirst removes existing elements until empty, then blocks interruptibly
988       */
989      public void testBlockingTakeFirst() throws InterruptedException {
990          final LinkedBlockingDeque q = populatedDeque(SIZE);
991 <        Thread t = new Thread(new CheckedRunnable() {
991 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
992 >        Thread t = newStartedThread(new CheckedRunnable() {
993              public void realRun() throws InterruptedException {
994 <                for (int i = 0; i < SIZE; ++i)
994 >                for (int i = 0; i < SIZE; ++i) {
995                      assertEquals(i, q.takeFirst());
996 +                }
997 +
998 +                Thread.currentThread().interrupt();
999                  try {
1000                      q.takeFirst();
1001                      shouldThrow();
1002                  } catch (InterruptedException success) {}
1003 +                assertFalse(Thread.interrupted());
1004 +
1005 +                pleaseInterrupt.countDown();
1006 +                try {
1007 +                    q.takeFirst();
1008 +                    shouldThrow();
1009 +                } catch (InterruptedException success) {}
1010 +                assertFalse(Thread.interrupted());
1011              }});
1012  
1013 <        t.start();
1014 <        delay(SHORT_DELAY_MS);
1013 >        await(pleaseInterrupt);
1014 >        assertThreadStaysAlive(t);
1015          t.interrupt();
1016 <        t.join();
1016 >        awaitTermination(t);
1017      }
1018  
922
1019      /**
1020       * timed pollFirst with zero timeout succeeds when non-empty, else times out
1021       */
# Line 937 | Line 1033 | public class LinkedBlockingDequeTest ext
1033      public void testTimedPollFirst() throws InterruptedException {
1034          LinkedBlockingDeque q = populatedDeque(SIZE);
1035          for (int i = 0; i < SIZE; ++i) {
1036 <            assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1037 <        }
1038 <        assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1036 >            long startTime = System.nanoTime();
1037 >            assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1038 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1039 >        }
1040 >        long startTime = System.nanoTime();
1041 >        assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1042 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1043 >        checkEmpty(q);
1044      }
1045  
1046      /**
# Line 947 | Line 1048 | public class LinkedBlockingDequeTest ext
1048       * returning timeout status
1049       */
1050      public void testInterruptedTimedPollFirst() throws InterruptedException {
1051 <        Thread t = new Thread(new CheckedRunnable() {
1051 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1052 >        Thread t = newStartedThread(new CheckedRunnable() {
1053              public void realRun() throws InterruptedException {
1054                  LinkedBlockingDeque q = populatedDeque(SIZE);
1055                  for (int i = 0; i < SIZE; ++i) {
1056 <                    assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1056 >                    assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1057                  }
1058 +
1059 +                Thread.currentThread().interrupt();
1060                  try {
1061                      q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1062                      shouldThrow();
1063                  } catch (InterruptedException success) {}
1064 +                assertFalse(Thread.interrupted());
1065 +
1066 +                pleaseInterrupt.countDown();
1067 +                try {
1068 +                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1069 +                    shouldThrow();
1070 +                } catch (InterruptedException success) {}
1071 +                assertFalse(Thread.interrupted());
1072              }});
1073  
1074 <        t.start();
1075 <        delay(SHORT_DELAY_MS);
1074 >        await(pleaseInterrupt);
1075 >        assertThreadStaysAlive(t);
1076          t.interrupt();
1077 <        t.join();
1077 >        awaitTermination(t);
1078      }
1079  
1080      /**
# Line 971 | Line 1083 | public class LinkedBlockingDequeTest ext
1083       */
1084      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
1085          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1086 <        Thread t = new Thread(new CheckedRunnable() {
1086 >        final CheckedBarrier barrier = new CheckedBarrier(2);
1087 >        Thread t = newStartedThread(new CheckedRunnable() {
1088              public void realRun() throws InterruptedException {
1089 <                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1089 >                long startTime = System.nanoTime();
1090 >                assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1091 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1092 >
1093 >                barrier.await();
1094 >
1095                  assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1096 +
1097 +                Thread.currentThread().interrupt();
1098 +                try {
1099 +                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1100 +                    shouldThrow();
1101 +                } catch (InterruptedException success) {}
1102 +
1103 +                barrier.await();
1104                  try {
1105                      q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1106                      shouldThrow();
1107                  } catch (InterruptedException success) {}
1108 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1109              }});
1110  
1111 <        t.start();
1112 <        delay(SMALL_DELAY_MS);
1113 <        assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
1111 >        barrier.await();
1112 >        long startTime = System.nanoTime();
1113 >        assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1114 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1115 >        barrier.await();
1116 >        assertThreadStaysAlive(t);
1117          t.interrupt();
1118 <        t.join();
1118 >        awaitTermination(t);
1119      }
1120  
1121      /**
1122       * putLast(null) throws NPE
1123       */
1124      public void testPutLastNull() throws InterruptedException {
1125 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1126          try {
996            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1127              q.putLast(null);
1128              shouldThrow();
1129          } catch (NullPointerException success) {}
# Line 1017 | Line 1147 | public class LinkedBlockingDequeTest ext
1147       */
1148      public void testBlockingPutLast() throws InterruptedException {
1149          final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1150 <        Thread t = new Thread(new CheckedRunnable() {
1150 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1151 >        Thread t = newStartedThread(new CheckedRunnable() {
1152              public void realRun() throws InterruptedException {
1153                  for (int i = 0; i < SIZE; ++i)
1154                      q.putLast(i);
1155                  assertEquals(SIZE, q.size());
1156                  assertEquals(0, q.remainingCapacity());
1157 +
1158 +                Thread.currentThread().interrupt();
1159 +                try {
1160 +                    q.putLast(99);
1161 +                    shouldThrow();
1162 +                } catch (InterruptedException success) {}
1163 +                assertFalse(Thread.interrupted());
1164 +
1165 +                pleaseInterrupt.countDown();
1166                  try {
1167                      q.putLast(99);
1168                      shouldThrow();
1169                  } catch (InterruptedException success) {}
1170 +                assertFalse(Thread.interrupted());
1171              }});
1172  
1173 <        t.start();
1174 <        delay(SHORT_DELAY_MS);
1173 >        await(pleaseInterrupt);
1174 >        assertThreadStaysAlive(t);
1175          t.interrupt();
1176 <        t.join();
1176 >        awaitTermination(t);
1177          assertEquals(SIZE, q.size());
1178          assertEquals(0, q.remainingCapacity());
1179      }
1180  
1181      /**
1182 <     * putLast blocks waiting for take when full
1182 >     * putLast blocks interruptibly waiting for take when full
1183       */
1184      public void testPutLastWithTake() throws InterruptedException {
1185          final int capacity = 2;
1186          final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1187 <        Thread t = new Thread(new CheckedRunnable() {
1187 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
1188 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1189 >        Thread t = newStartedThread(new CheckedRunnable() {
1190              public void realRun() throws InterruptedException {
1191 <                for (int i = 0; i < capacity + 1; i++)
1191 >                for (int i = 0; i < capacity; i++)
1192                      q.putLast(i);
1193 +                pleaseTake.countDown();
1194 +                q.putLast(86);
1195 +
1196 +                pleaseInterrupt.countDown();
1197                  try {
1198                      q.putLast(99);
1199                      shouldThrow();
1200                  } catch (InterruptedException success) {}
1201 +                assertFalse(Thread.interrupted());
1202              }});
1203  
1204 <        t.start();
1205 <        delay(SHORT_DELAY_MS);
1058 <        assertEquals(q.remainingCapacity(), 0);
1204 >        await(pleaseTake);
1205 >        assertEquals(0, q.remainingCapacity());
1206          assertEquals(0, q.take());
1207 <        delay(SHORT_DELAY_MS);
1207 >
1208 >        await(pleaseInterrupt);
1209 >        assertThreadStaysAlive(t);
1210          t.interrupt();
1211 <        t.join();
1212 <        assertEquals(q.remainingCapacity(), 0);
1211 >        awaitTermination(t);
1212 >        assertEquals(0, q.remainingCapacity());
1213      }
1214  
1215      /**
# Line 1084 | Line 1233 | public class LinkedBlockingDequeTest ext
1233              }});
1234  
1235          await(pleaseInterrupt);
1236 +        assertThreadStaysAlive(t);
1237          t.interrupt();
1238          awaitTermination(t);
1239      }
# Line 1099 | Line 1249 | public class LinkedBlockingDequeTest ext
1249      }
1250  
1251      /**
1252 <     * takeLast blocks interruptibly when empty
1103 <     */
1104 <    public void testTakeLastFromEmpty() throws InterruptedException {
1105 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1106 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1107 <            public void realRun() throws InterruptedException {
1108 <                q.takeLast();
1109 <            }};
1110 <
1111 <        t.start();
1112 <        delay(SHORT_DELAY_MS);
1113 <        t.interrupt();
1114 <        t.join();
1115 <    }
1116 <
1117 <    /**
1118 <     * TakeLast removes existing elements until empty, then blocks interruptibly
1252 >     * takeLast removes existing elements until empty, then blocks interruptibly
1253       */
1254      public void testBlockingTakeLast() throws InterruptedException {
1255          final LinkedBlockingDeque q = populatedDeque(SIZE);
1256 <        Thread t = new Thread(new CheckedRunnable() {
1256 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1257 >        Thread t = newStartedThread(new CheckedRunnable() {
1258              public void realRun() throws InterruptedException {
1259 <                for (int i = 0; i < SIZE; ++i)
1260 <                    assertEquals(SIZE - 1 - i, q.takeLast());
1259 >                for (int i = 0; i < SIZE; ++i) {
1260 >                    assertEquals(SIZE-i-1, q.takeLast());
1261 >                }
1262 >
1263 >                Thread.currentThread().interrupt();
1264 >                try {
1265 >                    q.takeLast();
1266 >                    shouldThrow();
1267 >                } catch (InterruptedException success) {}
1268 >                assertFalse(Thread.interrupted());
1269 >
1270 >                pleaseInterrupt.countDown();
1271                  try {
1272                      q.takeLast();
1273                      shouldThrow();
1274                  } catch (InterruptedException success) {}
1275 +                assertFalse(Thread.interrupted());
1276              }});
1277  
1278 <        t.start();
1279 <        delay(SHORT_DELAY_MS);
1278 >        await(pleaseInterrupt);
1279 >        assertThreadStaysAlive(t);
1280          t.interrupt();
1281 <        t.join();
1281 >        awaitTermination(t);
1282      }
1283  
1284      /**
# Line 1152 | Line 1298 | public class LinkedBlockingDequeTest ext
1298      public void testTimedPollLast() throws InterruptedException {
1299          LinkedBlockingDeque q = populatedDeque(SIZE);
1300          for (int i = 0; i < SIZE; ++i) {
1301 <            assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1302 <        }
1303 <        assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1301 >            long startTime = System.nanoTime();
1302 >            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1303 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1304 >        }
1305 >        long startTime = System.nanoTime();
1306 >        assertNull(q.pollLast(timeoutMillis(), MILLISECONDS));
1307 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1308 >        checkEmpty(q);
1309      }
1310  
1311      /**
# Line 1162 | Line 1313 | public class LinkedBlockingDequeTest ext
1313       * returning timeout status
1314       */
1315      public void testInterruptedTimedPollLast() throws InterruptedException {
1316 <        Thread t = new Thread(new CheckedRunnable() {
1316 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1317 >        Thread t = newStartedThread(new CheckedRunnable() {
1318              public void realRun() throws InterruptedException {
1319                  LinkedBlockingDeque q = populatedDeque(SIZE);
1320                  for (int i = 0; i < SIZE; ++i) {
1321 <                    assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1321 >                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1322                  }
1323 +
1324 +                Thread.currentThread().interrupt();
1325                  try {
1326 <                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1326 >                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1327                      shouldThrow();
1328                  } catch (InterruptedException success) {}
1329 +                assertFalse(Thread.interrupted());
1330 +
1331 +                pleaseInterrupt.countDown();
1332 +                try {
1333 +                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1334 +                    shouldThrow();
1335 +                } catch (InterruptedException success) {}
1336 +                assertFalse(Thread.interrupted());
1337              }});
1338  
1339 <        t.start();
1340 <        delay(SHORT_DELAY_MS);
1339 >        await(pleaseInterrupt);
1340 >        assertThreadStaysAlive(t);
1341          t.interrupt();
1342 <        t.join();
1342 >        awaitTermination(t);
1343      }
1344  
1345      /**
# Line 1186 | Line 1348 | public class LinkedBlockingDequeTest ext
1348       */
1349      public void testTimedPollWithOfferLast() throws InterruptedException {
1350          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1351 <        Thread t = new Thread(new CheckedRunnable() {
1351 >        final CheckedBarrier barrier = new CheckedBarrier(2);
1352 >        Thread t = newStartedThread(new CheckedRunnable() {
1353              public void realRun() throws InterruptedException {
1354 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1354 >                long startTime = System.nanoTime();
1355 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
1356 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1357 >
1358 >                barrier.await();
1359 >
1360                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1361 +
1362 +                Thread.currentThread().interrupt();
1363                  try {
1364                      q.poll(LONG_DELAY_MS, MILLISECONDS);
1365                      shouldThrow();
1366                  } catch (InterruptedException success) {}
1367 +                assertFalse(Thread.interrupted());
1368 +
1369 +                barrier.await();
1370 +                try {
1371 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1372 +                    shouldThrow();
1373 +                } catch (InterruptedException success) {}
1374 +                assertFalse(Thread.interrupted());
1375              }});
1376  
1377 <        t.start();
1378 <        delay(SMALL_DELAY_MS);
1379 <        assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1377 >        barrier.await();
1378 >        long startTime = System.nanoTime();
1379 >        assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS));
1380 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1381 >
1382 >        barrier.await();
1383 >        assertThreadStaysAlive(t);
1384          t.interrupt();
1385 <        t.join();
1385 >        awaitTermination(t);
1386      }
1387  
1206
1388      /**
1389       * element returns next element, or throws NSEE if empty
1390       */
# Line 1220 | Line 1401 | public class LinkedBlockingDequeTest ext
1401      }
1402  
1403      /**
1223     * remove(x) removes x and returns true if present
1224     */
1225    public void testRemoveElement() {
1226        LinkedBlockingDeque q = populatedDeque(SIZE);
1227        for (int i = 1; i < SIZE; i+=2) {
1228            assertTrue(q.contains(i));
1229            assertTrue(q.remove(i));
1230            assertFalse(q.contains(i));
1231            assertTrue(q.contains(i-1));
1232        }
1233        for (int i = 0; i < SIZE; i+=2) {
1234            assertTrue(q.contains(i));
1235            assertTrue(q.remove(i));
1236            assertFalse(q.contains(i));
1237            assertFalse(q.remove(i+1));
1238            assertFalse(q.contains(i+1));
1239        }
1240        assertTrue(q.isEmpty());
1241    }
1242
1243    /**
1404       * contains(x) reports true when elements added but not yet removed
1405       */
1406      public void testContains() {
# Line 1320 | Line 1480 | public class LinkedBlockingDequeTest ext
1480      /**
1481       * toArray contains all elements in FIFO order
1482       */
1483 <    public void testToArray() throws InterruptedException{
1483 >    public void testToArray() throws InterruptedException {
1484          LinkedBlockingDeque q = populatedDeque(SIZE);
1485          Object[] o = q.toArray();
1486          for (int i = 0; i < o.length; i++)
# Line 1340 | Line 1500 | public class LinkedBlockingDequeTest ext
1500      }
1501  
1502      /**
1343     * toArray(null) throws NullPointerException
1344     */
1345    public void testToArray_NullArg() {
1346        LinkedBlockingDeque q = populatedDeque(SIZE);
1347        try {
1348            q.toArray(null);
1349            shouldThrow();
1350        } catch (NullPointerException success) {}
1351    }
1352
1353    /**
1503       * toArray(incompatible array type) throws ArrayStoreException
1504       */
1505      public void testToArray1_BadArg() {
# Line 1361 | Line 1510 | public class LinkedBlockingDequeTest ext
1510          } catch (ArrayStoreException success) {}
1511      }
1512  
1364
1513      /**
1514       * iterator iterates through all elements
1515       */
# Line 1392 | Line 1540 | public class LinkedBlockingDequeTest ext
1540          assertFalse(it.hasNext());
1541      }
1542  
1395
1543      /**
1544       * iterator ordering is FIFO
1545       */
# Line 1424 | Line 1571 | public class LinkedBlockingDequeTest ext
1571          assertEquals(0, q.size());
1572      }
1573  
1427
1574      /**
1575       * Descending iterator iterates through all elements
1576       */
# Line 1487 | Line 1633 | public class LinkedBlockingDequeTest ext
1633          }
1634      }
1635  
1490
1636      /**
1637       * toString contains toStrings of elements
1638       */
# Line 1495 | Line 1640 | public class LinkedBlockingDequeTest ext
1640          LinkedBlockingDeque q = populatedDeque(SIZE);
1641          String s = q.toString();
1642          for (int i = 0; i < SIZE; ++i) {
1643 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1643 >            assertTrue(s.contains(String.valueOf(i)));
1644          }
1645      }
1646  
1502
1647      /**
1648       * offer transfers elements across Executor tasks
1649       */
# Line 1508 | Line 1652 | public class LinkedBlockingDequeTest ext
1652          q.add(one);
1653          q.add(two);
1654          ExecutorService executor = Executors.newFixedThreadPool(2);
1655 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1656          executor.execute(new CheckedRunnable() {
1657              public void realRun() throws InterruptedException {
1658                  assertFalse(q.offer(three));
1659 <                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1659 >                threadsStarted.await();
1660 >                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1661                  assertEquals(0, q.remainingCapacity());
1662              }});
1663  
1664          executor.execute(new CheckedRunnable() {
1665              public void realRun() throws InterruptedException {
1666 <                delay(SMALL_DELAY_MS);
1666 >                threadsStarted.await();
1667                  assertSame(one, q.take());
1668              }});
1669  
# Line 1525 | Line 1671 | public class LinkedBlockingDequeTest ext
1671      }
1672  
1673      /**
1674 <     * poll retrieves elements across Executor threads
1674 >     * timed poll retrieves elements across Executor threads
1675       */
1676      public void testPollInExecutor() {
1677          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1678 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1679          ExecutorService executor = Executors.newFixedThreadPool(2);
1680          executor.execute(new CheckedRunnable() {
1681              public void realRun() throws InterruptedException {
1682                  assertNull(q.poll());
1683 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1684 <                assertTrue(q.isEmpty());
1683 >                threadsStarted.await();
1684 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1685 >                checkEmpty(q);
1686              }});
1687  
1688          executor.execute(new CheckedRunnable() {
1689              public void realRun() throws InterruptedException {
1690 <                delay(SMALL_DELAY_MS);
1690 >                threadsStarted.await();
1691                  q.put(one);
1692              }});
1693  
# Line 1550 | Line 1698 | public class LinkedBlockingDequeTest ext
1698       * A deserialized serialized deque has same elements in same order
1699       */
1700      public void testSerialization() throws Exception {
1701 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1702 <
1555 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1556 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1557 <        out.writeObject(q);
1558 <        out.close();
1559 <
1560 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1561 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1562 <        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1563 <        assertEquals(q.size(), r.size());
1564 <        while (!q.isEmpty())
1565 <            assertEquals(q.remove(), r.remove());
1566 <    }
1701 >        Queue x = populatedDeque(SIZE);
1702 >        Queue y = serialClone(x);
1703  
1704 <    /**
1705 <     * drainTo(null) throws NPE
1706 <     */
1707 <    public void testDrainToNull() {
1708 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1709 <        try {
1710 <            q.drainTo(null);
1711 <            shouldThrow();
1712 <        } catch (NullPointerException success) {}
1577 <    }
1578 <
1579 <    /**
1580 <     * drainTo(this) throws IAE
1581 <     */
1582 <    public void testDrainToSelf() {
1583 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1584 <        try {
1585 <            q.drainTo(q);
1586 <            shouldThrow();
1587 <        } catch (IllegalArgumentException success) {}
1704 >        assertNotSame(y, x);
1705 >        assertEquals(x.size(), y.size());
1706 >        assertEquals(x.toString(), y.toString());
1707 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1708 >        while (!x.isEmpty()) {
1709 >            assertFalse(y.isEmpty());
1710 >            assertEquals(x.remove(), y.remove());
1711 >        }
1712 >        assertTrue(y.isEmpty());
1713      }
1714  
1715      /**
# Line 1594 | Line 1719 | public class LinkedBlockingDequeTest ext
1719          LinkedBlockingDeque q = populatedDeque(SIZE);
1720          ArrayList l = new ArrayList();
1721          q.drainTo(l);
1722 <        assertEquals(q.size(), 0);
1723 <        assertEquals(l.size(), SIZE);
1722 >        assertEquals(0, q.size());
1723 >        assertEquals(SIZE, l.size());
1724          for (int i = 0; i < SIZE; ++i)
1725              assertEquals(l.get(i), new Integer(i));
1726          q.add(zero);
# Line 1605 | Line 1730 | public class LinkedBlockingDequeTest ext
1730          assertTrue(q.contains(one));
1731          l.clear();
1732          q.drainTo(l);
1733 <        assertEquals(q.size(), 0);
1734 <        assertEquals(l.size(), 2);
1733 >        assertEquals(0, q.size());
1734 >        assertEquals(2, l.size());
1735          for (int i = 0; i < 2; ++i)
1736              assertEquals(l.get(i), new Integer(i));
1737      }
# Line 1632 | Line 1757 | public class LinkedBlockingDequeTest ext
1757      }
1758  
1759      /**
1635     * drainTo(null, n) throws NPE
1636     */
1637    public void testDrainToNullN() {
1638        LinkedBlockingDeque q = populatedDeque(SIZE);
1639        try {
1640            q.drainTo(null, 0);
1641            shouldThrow();
1642        } catch (NullPointerException success) {}
1643    }
1644
1645    /**
1646     * drainTo(this, n) throws IAE
1647     */
1648    public void testDrainToSelfN() {
1649        LinkedBlockingDeque q = populatedDeque(SIZE);
1650        try {
1651            q.drainTo(q, 0);
1652            shouldThrow();
1653        } catch (IllegalArgumentException success) {}
1654    }
1655
1656    /**
1760       * drainTo(c, n) empties first min(n, size) elements of queue into c
1761       */
1762      public void testDrainToN() {
# Line 1664 | Line 1767 | public class LinkedBlockingDequeTest ext
1767              ArrayList l = new ArrayList();
1768              q.drainTo(l, i);
1769              int k = (i < SIZE) ? i : SIZE;
1770 <            assertEquals(l.size(), k);
1771 <            assertEquals(q.size(), SIZE-k);
1770 >            assertEquals(k, l.size());
1771 >            assertEquals(SIZE-k, q.size());
1772              for (int j = 0; j < k; ++j)
1773                  assertEquals(l.get(j), new Integer(j));
1774              while (q.poll() != null) ;
1775          }
1776      }
1777  
1778 +    /**
1779 +     * remove(null), contains(null) always return false
1780 +     */
1781 +    public void testNeverContainsNull() {
1782 +        Deque<?>[] qs = {
1783 +            new LinkedBlockingDeque<Object>(),
1784 +            populatedDeque(2),
1785 +        };
1786 +
1787 +        for (Deque<?> q : qs) {
1788 +            assertFalse(q.contains(null));
1789 +            assertFalse(q.remove(null));
1790 +            assertFalse(q.removeFirstOccurrence(null));
1791 +            assertFalse(q.removeLastOccurrence(null));
1792 +        }
1793 +    }
1794 +
1795   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines